1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042 8043 8044 8045 8046 8047 8048 8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059 8060 8061 8062 8063 8064 8065 8066 8067 8068 8069 8070 8071 8072 8073 8074 8075 8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130 8131 8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153 8154 8155 8156 8157 8158 8159 8160 8161 8162 8163 8164 8165 8166 8167 8168 8169 8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180 8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200 8201 8202 8203 8204 8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217 8218 8219 8220 8221 8222 8223 8224 8225 8226 8227 8228 8229 8230 8231 8232 8233 8234 8235 8236 8237 8238 8239 8240 8241 8242 8243 8244 8245 8246 8247 8248 8249 8250 8251 8252 8253 8254 8255 8256 8257 8258 8259 8260 8261 8262 8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293 8294 8295 8296 8297 8298 8299 8300 8301 8302 8303 8304 8305 8306 8307 8308 8309 8310 8311 8312 8313 8314 8315 8316 8317 8318 8319 8320 8321 8322 8323 8324 8325 8326 8327 8328 8329 8330 8331 8332 8333 8334 8335 8336 8337 8338 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351 8352 8353 8354 8355 8356 8357 8358 8359 8360 8361 8362 8363 8364 8365 8366 8367 8368 8369 8370 8371 8372 8373 8374 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8390 8391 8392 8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431 8432 8433 8434 8435 8436 8437 8438 8439 8440 8441 8442 8443 8444 8445 8446 8447 8448 8449 8450 8451 8452 8453 8454 8455 8456 8457 8458 8459 8460 8461 8462 8463 8464 8465 8466 8467 8468 8469 8470 8471 8472 8473 8474 8475 8476 8477 8478 8479 8480 8481 8482 8483 8484 8485 8486 8487 8488 8489 8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503 8504 8505 8506 8507 8508 8509 8510 8511 8512 8513 8514 8515 8516 8517 8518 8519 8520 8521 8522 8523 8524 8525 8526 8527 8528 8529 8530 8531 8532 8533 8534 8535 8536 8537 8538 8539 8540 8541 8542 8543 8544 8545 8546 8547 8548 8549 8550 8551 8552 8553 8554 8555 8556 8557 8558 8559 8560 8561 8562 8563 8564 8565 8566 8567 8568 8569 8570 8571 8572 8573 8574 8575 8576 8577 8578 8579 8580 8581 8582 8583 8584 8585 8586 8587 8588 8589 8590 8591 8592 8593 8594 8595 8596 8597 8598 8599 8600 8601 8602 8603 8604 8605 8606 8607 8608 8609 8610 8611 8612 8613 8614 8615 8616 8617 8618 8619 8620 8621 8622 8623 8624 8625 8626 8627 8628 8629 8630 8631 8632 8633 8634 8635 8636 8637 8638 8639 8640 8641 8642 8643 8644 8645 8646 8647 8648 8649 8650 8651 8652 8653 8654 8655 8656 8657 8658 8659 8660 8661 8662 8663 8664 8665 8666 8667 8668 8669 8670 8671 8672 8673 8674 8675 8676 8677 8678 8679 8680 8681 8682 8683 8684 8685 8686 8687 8688 8689 8690 8691 8692 8693 8694 8695 8696 8697 8698 8699 8700 8701 8702 8703 8704 8705 8706 8707 8708 8709 8710 8711 8712 8713 8714 8715 8716 8717 8718 8719 8720 8721 8722 8723 8724 8725 8726 8727 8728 8729 8730 8731 8732 8733 8734 8735 8736 8737 8738 8739 8740 8741 8742 8743 8744 8745 8746 8747 8748 8749 8750 8751 8752 8753 8754 8755 8756 8757 8758 8759 8760 8761 8762 8763 8764 8765 8766 8767 8768 8769 8770 8771 8772 8773 8774 8775 8776 8777 8778 8779 8780 8781 8782 8783 8784 8785 8786 8787 8788 8789 8790 8791 8792 8793 8794 8795 8796 8797 8798 8799 8800 8801 8802 8803 8804 8805 8806 8807 8808 8809 8810 8811 8812 8813 8814 8815 8816 8817 8818 8819 8820 8821 8822 8823 8824 8825 8826 8827 8828 8829 8830 8831 8832 8833 8834 8835 8836 8837 8838 8839 8840 8841 8842 8843 8844 8845 8846 8847 8848 8849 8850 8851 8852 8853 8854 8855 8856 8857 8858 8859 8860 8861 8862 8863 8864 8865 8866 8867 8868 8869 8870 8871 8872 8873 8874 8875 8876 8877 8878 8879 8880 8881 8882 8883 8884 8885 8886 8887 8888 8889 8890 8891 8892 8893 8894 8895 8896 8897 8898 8899 8900 8901 8902 8903 8904 8905 8906 8907 8908 8909 8910 8911 8912 8913 8914 8915 8916 8917 8918 8919 8920 8921 8922 8923 8924 8925 8926 8927 8928 8929 8930 8931 8932 8933 8934 8935 8936 8937 8938 8939 8940 8941 8942 8943 8944 8945 8946 8947 8948 8949 8950 8951 8952 8953 8954 8955 8956 8957 8958 8959 8960 8961 8962 8963 8964 8965 8966 8967 8968 8969 8970 8971 8972 8973 8974 8975 8976 8977 8978 8979 8980 8981 8982 8983 8984 8985 8986 8987 8988 8989 8990 8991 8992 8993 8994 8995 8996 8997 8998 8999 9000 9001 9002 9003 9004 9005 9006 9007 9008 9009 9010 9011 9012 9013 9014 9015 9016 9017 9018 9019 9020 9021 9022 9023 9024 9025 9026 9027 9028 9029 9030 9031 9032 9033 9034 9035 9036 9037 9038 9039 9040 9041 9042 9043 9044 9045 9046 9047 9048 9049 9050 9051 9052 9053 9054 9055 9056 9057 9058 9059 9060 9061 9062 9063 9064 9065 9066 9067 9068 9069 9070 9071 9072 9073 9074 9075 9076 9077 9078 9079 9080 9081 9082 9083 9084 9085 9086 9087 9088 9089 9090 9091 9092 9093 9094 9095 9096 9097 9098 9099 9100 9101 9102 9103 9104 9105 9106 9107 9108 9109 9110 9111 9112 9113 9114 9115 9116 9117 9118 9119 9120 9121 9122 9123 9124 9125 9126 9127 9128 9129 9130 9131 9132 9133 9134 9135 9136 9137 9138 9139 9140 9141 9142 9143 9144 9145 9146 9147 9148 9149 9150 9151 9152 9153 9154 9155 9156 9157 9158 9159 9160 9161 9162 9163 9164 9165 9166 9167 9168 9169 9170 9171 9172 9173 9174 9175 9176 9177 9178 9179 9180 9181 9182 9183 9184 9185 9186 9187 9188 9189 9190 9191 9192 9193 9194 9195 9196 9197 9198 9199 9200 9201 9202 9203 9204 9205 9206 9207 9208 9209 9210 9211 9212 9213 9214 9215 9216 9217 9218 9219 9220 9221 9222 9223 9224 9225 9226 9227 9228 9229 9230 9231 9232 9233 9234 9235 9236 9237 9238 9239 9240 9241 9242 9243 9244 9245 9246 9247 9248 9249 9250 9251 9252 9253 9254 9255 9256 9257 9258 9259 9260 9261 9262 9263 9264 9265 9266 9267 9268 9269 9270 9271 9272 9273 9274 9275 9276 9277 9278 9279 9280 9281 9282 9283 9284 9285 9286 9287 9288 9289 9290 9291 9292 9293 9294 9295 9296 9297 9298 9299 9300 9301 9302 9303 9304 9305 9306 9307 9308 9309 9310 9311 9312 9313 9314 9315 9316 9317 9318 9319 9320 9321 9322 9323 9324 9325 9326 9327 9328 9329 9330 9331 9332 9333 9334 9335 9336 9337 9338 9339 9340 9341 9342 9343 9344 9345 9346 9347 9348 9349 9350 9351 9352 9353 9354 9355 9356 9357 9358 9359 9360 9361 9362 9363 9364 9365 9366 9367 9368 9369 9370 9371 9372 9373 9374 9375 9376 9377 9378 9379 9380 9381 9382 9383 9384 9385 9386 9387 9388 9389 9390 9391 9392 9393 9394 9395 9396 9397 9398 9399 9400 9401 9402 9403 9404 9405 9406 9407 9408 9409 9410 9411 9412 9413 9414 9415 9416 9417 9418 9419 9420 9421 9422 9423 9424 9425 9426 9427 9428 9429 9430 9431 9432 9433 9434 9435 9436 9437 9438 9439 9440 9441 9442 9443 9444 9445 9446 9447 9448 9449 9450 9451 9452 9453 9454 9455 9456 9457 9458 9459 9460 9461 9462 9463 9464 9465 9466 9467 9468 9469 9470 9471 9472 9473 9474 9475 9476 9477 9478 9479 9480 9481 9482 9483 9484 9485 9486 9487 9488 9489 9490 9491 9492 9493 9494 9495 9496 9497 9498 9499 9500 9501 9502 9503 9504 9505 9506 9507 9508 9509 9510 9511 9512 9513 9514 9515 9516 9517 9518 9519 9520 9521 9522 9523 9524 9525 9526 9527 9528 9529 9530 9531 9532 9533 9534 9535 9536 9537 9538 9539 9540 9541 9542 9543 9544 9545 9546 9547 9548 9549 9550 9551 9552 9553 9554 9555 9556 9557 9558 9559 9560 9561 9562 9563 9564 9565 9566 9567 9568 9569 9570 9571 9572 9573 9574 9575 9576 9577 9578 9579 9580 9581 9582 9583 9584 9585 9586 9587 9588 9589 9590 9591 9592 9593 9594 9595 9596 9597 9598 9599 9600 9601 9602 9603 9604 9605 9606 9607 9608 9609 9610 9611 9612 9613 9614 9615 9616 9617 9618 9619 9620 9621 9622 9623 9624 9625 9626 9627 9628 9629 9630 9631 9632 9633 9634 9635 9636 9637 9638 9639 9640 9641 9642 9643 9644 9645 9646 9647 9648 9649 9650 9651 9652 9653 9654 9655 9656 9657 9658 9659 9660 9661 9662 9663 9664 9665 9666 9667 9668 9669 9670 9671 9672 9673 9674 9675 9676 9677 9678 9679 9680 9681 9682 9683 9684 9685 9686 9687 9688 9689 9690 9691 9692 9693 9694 9695 9696 9697 9698 9699 9700 9701 9702 9703 9704 9705 9706 9707 9708 9709 9710 9711 9712 9713 9714 9715 9716 9717 9718 9719 9720 9721 9722 9723 9724 9725 9726 9727 9728 9729 9730 9731 9732 9733 9734 9735 9736 9737 9738 9739 9740 9741 9742 9743 9744 9745 9746 9747 9748 9749 9750 9751 9752 9753 9754 9755 9756 9757 9758 9759 9760 9761 9762 9763 9764 9765 9766 9767 9768 9769 9770 9771 9772 9773 9774 9775 9776 9777 9778 9779 9780 9781 9782 9783 9784 9785 9786 9787 9788 9789 9790 9791 9792 9793 9794 9795 9796 9797 9798 9799 9800 9801 9802 9803 9804 9805 9806 9807 9808 9809 9810 9811 9812 9813 9814 9815 9816 9817 9818 9819 9820 9821 9822 9823 9824 9825 9826 9827 9828 9829 9830 9831 9832 9833 9834 9835 9836 9837 9838 9839 9840 9841 9842 9843 9844 9845 9846 9847 9848 9849 9850 9851 9852 9853 9854 9855 9856 9857 9858 9859 9860 9861 9862 9863 9864 9865 9866 9867 9868 9869 9870 9871 9872 9873 9874 9875 9876 9877 9878 9879 9880 9881 9882 9883 9884 9885 9886 9887 9888 9889 9890 9891 9892 9893 9894 9895 9896 9897 9898 9899 9900 9901 9902 9903 9904 9905 9906 9907 9908 9909 9910 9911 9912 9913 9914 9915 9916 9917 9918 9919 9920 9921 9922 9923 9924 9925 9926 9927 9928 9929 9930 9931 9932 9933 9934 9935 9936 9937 9938 9939 9940 9941 9942 9943 9944 9945 9946 9947 9948 9949 9950 9951 9952 9953 9954 9955 9956 9957 9958 9959 9960 9961 9962 9963 9964 9965 9966 9967 9968 9969 9970 9971 9972 9973 9974 9975 9976 9977 9978 9979 9980 9981 9982 9983 9984 9985 9986 9987 9988 9989 9990 9991 9992 9993 9994 9995 9996 9997 9998 9999 10000 10001 10002 10003 10004 10005 10006 10007 10008 10009 10010 10011 10012 10013 10014 10015 10016 10017 10018 10019 10020 10021 10022 10023 10024 10025 10026 10027 10028 10029 10030 10031 10032 10033 10034 10035 10036 10037 10038 10039 10040 10041 10042 10043 10044 10045 10046 10047 10048 10049 10050 10051 10052 10053 10054 10055 10056 10057 10058 10059 10060 10061 10062 10063 10064 10065 10066 10067 10068 10069 10070 10071 10072 10073 10074 10075 10076 10077 10078 10079 10080 10081 10082 10083 10084 10085 10086 10087 10088 10089 10090 10091 10092 10093 10094 10095 10096 10097 10098 10099 10100 10101 10102 10103 10104 10105 10106 10107 10108 10109 10110 10111 10112 10113 10114 10115 10116 10117 10118 10119 10120 10121 10122 10123 10124 10125 10126 10127 10128 10129 10130 10131 10132 10133 10134 10135 10136 10137 10138 10139 10140 10141 10142 10143 10144 10145 10146 10147 10148 10149 10150 10151 10152 10153 10154 10155 10156 10157 10158 10159 10160 10161 10162 10163 10164 10165 10166 10167 10168 10169 10170 10171 10172 10173 10174 10175 10176 10177 10178 10179 10180 10181 10182 10183 10184 10185 10186 10187 10188 10189 10190 10191 10192 10193 10194 10195 10196 10197 10198 10199 10200 10201 10202 10203 10204 10205 10206 10207 10208 10209 10210 10211 10212 10213 10214 10215 10216 10217 10218 10219 10220 10221 10222 10223 10224 10225 10226 10227 10228 10229 10230 10231 10232 10233 10234 10235 10236 10237 10238 10239 10240 10241 10242 10243 10244 10245 10246 10247 10248 10249 10250 10251 10252 10253 10254 10255 10256 10257 10258 10259 10260 10261 10262 10263 10264 10265 10266 10267 10268 10269 10270 10271 10272 10273 10274 10275 10276 10277 10278 10279 10280 10281 10282 10283 10284 10285 10286 10287 10288 10289 10290 10291 10292 10293 10294 10295 10296 10297 10298 10299 10300 10301 10302 10303 10304 10305 10306 10307 10308 10309 10310 10311 10312 10313 10314 10315 10316 10317 10318 10319 10320 10321 10322 10323 10324 10325 10326 10327 10328 10329 10330 10331 10332 10333 10334 10335 10336 10337 10338 10339 10340 10341 10342 10343 10344 10345 10346 10347 10348 10349 10350 10351 10352 10353 10354 10355 10356 10357 10358 10359 10360 10361 10362 10363 10364 10365 10366 10367 10368 10369 10370 10371 10372 10373 10374 10375 10376 10377 10378 10379 10380 10381 10382 10383 10384 10385 10386 10387 10388 10389 10390 10391 10392 10393 10394 10395 10396 10397 10398 10399 10400 10401 10402 10403 10404 10405 10406 10407 10408 10409 10410 10411 10412 10413 10414 10415 10416 10417 10418 10419 10420 10421 10422 10423 10424 10425 10426 10427 10428 10429 10430 10431 10432 10433 10434 10435 10436 10437 10438 10439 10440 10441 10442 10443 10444 10445 10446 10447 10448 10449 10450 10451 10452 10453 10454 10455 10456 10457 10458 10459 10460 10461 10462 10463 10464 10465 10466 10467 10468 10469 10470 10471 10472 10473 10474 10475 10476 10477 10478 10479 10480 10481 10482 10483 10484 10485 10486 10487 10488 10489 10490 10491 10492 10493 10494 10495 10496 10497 10498 10499 10500 10501 10502 10503 10504 10505 10506 10507 10508 10509 10510 10511 10512 10513 10514 10515 10516 10517 10518 10519 10520 10521 10522 10523 10524 10525 10526 10527 10528 10529 10530 10531 10532 10533 10534 10535 10536 10537 10538 10539 10540 10541 10542 10543 10544 10545 10546 10547 10548 10549 10550 10551 10552 10553 10554 10555 10556 10557 10558 10559 10560 10561 10562 10563 10564 10565 10566 10567 10568 10569 10570 10571 10572 10573 10574 10575 10576 10577 10578 10579 10580 10581 10582 10583 10584 10585 10586 10587 10588 10589 10590 10591 10592 10593 10594 10595 10596 10597 10598 10599 10600 10601 10602 10603 10604 10605 10606 10607 10608 10609 10610 10611 10612 10613 10614 10615 10616 10617 10618 10619 10620 10621 10622 10623 10624 10625 10626 10627 10628 10629 10630 10631 10632 10633 10634 10635 10636 10637 10638 10639 10640 10641 10642 10643 10644 10645 10646 10647 10648 10649 10650 10651 10652 10653 10654 10655 10656 10657 10658 10659 10660 10661 10662 10663 10664 10665 10666 10667 10668 10669 10670 10671 10672 10673 10674 10675 10676 10677 10678 10679 10680 10681 10682 10683 10684 10685 10686 10687 10688 10689 10690 10691 10692 10693 10694 10695 10696 10697 10698 10699 10700 10701 10702 10703 10704 10705 10706 10707 10708 10709 10710 10711 10712 10713 10714 10715 10716 10717 10718 10719 10720 10721 10722 10723 10724 10725 10726 10727 10728 10729 10730 10731 10732 10733 10734 10735 10736 10737 10738 10739 10740 10741 10742 10743 10744 10745 10746 10747 10748 10749 10750 10751 10752 10753 10754 10755 10756 10757 10758 10759 10760 10761 10762 10763 10764 10765 10766 10767 10768 10769 10770 10771 10772 10773 10774 10775 10776 10777 10778 10779 10780 10781 10782 10783 10784 10785 10786 10787 10788 10789 10790 10791 10792 10793 10794 10795 10796 10797 10798 10799 10800 10801 10802 10803 10804 10805 10806 10807 10808 10809 10810 10811 10812 10813 10814 10815 10816 10817 10818 10819 10820 10821 10822 10823 10824 10825 10826 10827 10828 10829 10830 10831 10832 10833 10834 10835 10836 10837 10838 10839 10840 10841 10842 10843 10844 10845 10846 10847 10848 10849 10850 10851 10852 10853 10854 10855 10856 10857 10858 10859 10860 10861 10862 10863 10864 10865 10866 10867 10868 10869 10870 10871 10872 10873 10874 10875 10876 10877 10878 10879 10880 10881 10882 10883 10884 10885 10886 10887 10888 10889 10890 10891 10892 10893 10894 10895 10896 10897 10898 10899 10900 10901 10902 10903 10904 10905 10906 10907 10908 10909 10910 10911 10912 10913 10914 10915 10916 10917 10918 10919 10920 10921 10922 10923 10924 10925 10926 10927 10928 10929 10930 10931 10932 10933 10934 10935 10936 10937 10938 10939 10940 10941 10942 10943 10944 10945 10946 10947 10948 10949 10950 10951 10952 10953 10954 10955 10956 10957 10958 10959 10960 10961 10962 10963 10964 10965 10966 10967 10968 10969 10970 10971 10972 10973 10974 10975 10976 10977 10978 10979 10980 10981 10982 10983 10984 10985 10986 10987 10988 10989 10990 10991 10992 10993 10994 10995 10996 10997 10998 10999 11000 11001 11002 11003 11004 11005 11006 11007 11008 11009 11010 11011 11012 11013 11014 11015 11016 11017 11018 11019 11020 11021 11022 11023 11024 11025 11026 11027 11028 11029 11030 11031 11032 11033 11034 11035 11036 11037 11038 11039 11040 11041 11042 11043 11044 11045 11046 11047 11048 11049 11050 11051 11052 11053 11054 11055 11056 11057 11058 11059 11060 11061 11062 11063 11064 11065 11066 11067 11068 11069 11070 11071 11072 11073 11074 11075 11076 11077 11078 11079 11080 11081 11082 11083 11084 11085 11086 11087 11088 11089 11090 11091 11092 11093 11094 11095 11096 11097 11098 11099 11100 11101 11102 11103 11104 11105 11106 11107 11108 11109 11110 11111 11112 11113 11114 11115 11116 11117 11118 11119 11120 11121 11122 11123 11124 11125 11126 11127 11128 11129 11130 11131 11132 11133 11134 11135 11136 11137 11138 11139 11140 11141 11142 11143 11144 11145 11146 11147 11148 11149 11150 11151 11152 11153 11154 11155 11156 11157 11158 11159 11160 11161 11162 11163 11164 11165 11166 11167 11168 11169 11170 11171 11172 11173 11174 11175 11176 11177 11178 11179 11180 11181 11182 11183 11184 11185 11186 11187 11188 11189 11190 11191 11192 11193 11194 11195 11196 11197 11198 11199 11200 11201 11202 11203 11204 11205 11206 11207 11208 11209 11210 11211 11212 11213 11214 11215 11216 11217 11218 11219 11220 11221 11222 11223 11224 11225 11226 11227 11228 11229 11230 11231 11232 11233 11234 11235 11236 11237 11238 11239 11240 11241 11242 11243 11244 11245 11246 11247 11248 11249 11250 11251 11252 11253 11254 11255 11256 11257 11258 11259 11260 11261 11262 11263 11264 11265 11266 11267 11268 11269 11270 11271 11272 11273 11274 11275 11276 11277 11278 11279 11280 11281 11282 11283 11284 11285 11286 11287 11288 11289 11290 11291 11292 11293 11294 11295 11296 11297 11298 11299 11300 11301 11302 11303 11304 11305 11306 11307 11308 11309 11310 11311 11312 11313 11314 11315 11316 11317 11318 11319 11320 11321 11322 11323 11324 11325 11326 11327 11328 11329 11330 11331 11332 11333 11334 11335 11336 11337 11338 11339 11340 11341 11342 11343 11344 11345 11346 11347 11348 11349 11350 11351 11352 11353 11354 11355 11356 11357 11358 11359 11360 11361 11362 11363 11364 11365 11366 11367 11368 11369 11370 11371 11372 11373 11374 11375 11376 11377 11378 11379 11380 11381 11382 11383 11384 11385 11386 11387 11388 11389 11390 11391 11392 11393 11394 11395 11396 11397 11398 11399 11400 11401 11402 11403 11404 11405 11406 11407 11408 11409 11410 11411 11412 11413 11414 11415 11416 11417 11418 11419 11420 11421 11422 11423 11424 11425 11426 11427 11428 11429 11430 11431 11432 11433 11434 11435 11436 11437 11438 11439 11440 11441 11442 11443 11444 11445 11446 11447 11448 11449 11450 11451 11452 11453 11454 11455 11456 11457 11458 11459 11460 11461 11462 11463 11464 11465 11466 11467 11468 11469 11470 11471 11472 11473 11474 11475 11476 11477 11478 11479 11480 11481 11482 11483 11484 11485 11486 11487 11488 11489 11490 11491 11492 11493 11494 11495 11496 11497 11498 11499 11500 11501 11502 11503 11504 11505 11506 11507 11508 11509 11510 11511 11512 11513 11514 11515 11516 11517 11518 11519 11520 11521 11522 11523 11524 11525 11526 11527 11528 11529 11530 11531 11532 11533 11534 11535 11536 11537 11538 11539 11540 11541 11542 11543 11544 11545 11546 11547 11548 11549 11550 11551 11552 11553 11554 11555 11556 11557 11558 11559 11560 11561 11562 11563 11564 11565 11566 11567 11568 11569 11570 11571 11572 11573 11574 11575 11576 11577 11578 11579 11580 11581 11582 11583 11584 11585 11586 11587 11588 11589 11590 11591 11592 11593 11594 11595 11596 11597 11598 11599 11600 11601 11602 11603 11604 11605 11606 11607 11608 11609 11610 11611 11612 11613 11614 11615 11616 11617 11618 11619 11620 11621 11622 11623 11624 11625 11626 11627 11628 11629 11630 11631 11632 11633 11634 11635 11636 11637 11638 11639 11640 11641 11642 11643 11644 11645 11646 11647 11648 11649 11650 11651 11652 11653 11654 11655 11656 11657 11658 11659 11660 11661 11662 11663 11664 11665 11666 11667 11668 11669 11670 11671 11672 11673 11674 11675 11676 11677 11678 11679 11680 11681 11682 11683 11684 11685 11686 11687 11688 11689 11690 11691 11692 11693 11694 11695 11696 11697 11698 11699 11700 11701 11702 11703 11704 11705 11706 11707 11708 11709 11710 11711 11712 11713 11714 11715 11716 11717 11718 11719 11720 11721 11722 11723 11724 11725 11726 11727 11728 11729 11730 11731 11732 11733 11734 11735 11736 11737 11738 11739 11740 11741 11742 11743 11744 11745 11746 11747 11748 11749 11750 11751 11752 11753 11754 11755 11756 11757 11758 11759 11760 11761 11762 11763 11764 11765 11766 11767 11768 11769 11770 11771 11772 11773 11774 11775 11776 11777 11778 11779 11780 11781 11782 11783 11784 11785 11786 11787 11788 11789 11790 11791 11792 11793 11794 11795 11796 11797 11798 11799 11800 11801 11802 11803 11804 11805 11806 11807 11808 11809 11810 11811 11812 11813 11814 11815 11816 11817 11818 11819 11820 11821 11822 11823 11824 11825 11826 11827 11828 11829 11830 11831 11832 11833 11834 11835 11836 11837 11838 11839 11840 11841 11842 11843 11844 11845 11846 11847 11848 11849 11850 11851 11852 11853 11854 11855 11856 11857 11858 11859 11860 11861 11862 11863 11864 11865 11866 11867 11868 11869 11870 11871 11872 11873 11874 11875 11876 11877 11878 11879 11880 11881 11882 11883 11884 11885 11886 11887 11888 11889 11890 11891 11892 11893 11894 11895 11896 11897 11898 11899 11900 11901 11902 11903 11904 11905 11906 11907 11908 11909 11910 11911 11912 11913 11914 11915 11916 11917 11918 11919 11920 11921 11922 11923 11924 11925 11926 11927 11928 11929 11930 11931 11932 11933 11934 11935 11936 11937 11938 11939 11940 11941 11942 11943 11944 11945 11946 11947 11948 11949 11950 11951 11952 11953 11954 11955 11956 11957 11958 11959 11960 11961 11962 11963 11964 11965 11966 11967 11968 11969 11970 11971 11972 11973 11974 11975 11976 11977 11978 11979 11980 11981 11982 11983 11984 11985 11986 11987 11988 11989 11990 11991 11992 11993 11994 11995 11996 11997 11998 11999 12000 12001 12002 12003 12004 12005 12006 12007 12008 12009 12010 12011 12012 12013 12014 12015 12016 12017 12018 12019 12020 12021 12022 12023 12024 12025 12026 12027 12028 12029 12030 12031 12032 12033 12034 12035 12036 12037 12038 12039 12040 12041 12042 12043 12044 12045 12046 12047 12048 12049 12050 12051 12052 12053 12054 12055 12056 12057 12058 12059 12060 12061 12062 12063 12064 12065 12066 12067 12068 12069 12070 12071 12072 12073 12074 12075 12076 12077 12078 12079 12080 12081 12082 12083 12084 12085 12086 12087 12088 12089 12090 12091 12092 12093 12094 12095 12096 12097 12098 12099 12100 12101 12102 12103 12104 12105 12106 12107 12108 12109 12110 12111 12112 12113 12114 12115 12116 12117 12118 12119 12120 12121 12122 12123 12124 12125 12126 12127 12128 12129 12130 12131 12132 12133 12134 12135 12136 12137 12138 12139 12140 12141 12142 12143 12144 12145 12146 12147 12148 12149 12150 12151 12152 12153 12154 12155 12156 12157 12158 12159 12160 12161 12162 12163 12164 12165 12166 12167 12168 12169 12170 12171 12172 12173 12174 12175 12176 12177 12178 12179 12180 12181 12182 12183 12184 12185 12186 12187 12188 12189 12190 12191 12192 12193 12194 12195 12196 12197 12198 12199 12200 12201 12202 12203 12204 12205 12206 12207 12208 12209 12210 12211 12212 12213 12214 12215 12216 12217 12218 12219 12220 12221 12222 12223 12224 12225 12226 12227 12228 12229 12230 12231 12232 12233 12234 12235 12236 12237 12238 12239 12240 12241 12242 12243 12244 12245 12246 12247 12248 12249 12250 12251 12252 12253 12254 12255 12256 12257 12258 12259 12260 12261 12262 12263 12264 12265 12266 12267 12268 12269 12270 12271 12272 12273 12274 12275 12276 12277 12278 12279 12280 12281 12282 12283 12284 12285 12286 12287 12288 12289 12290 12291 12292 12293 12294 12295 12296 12297 12298 12299 12300 12301 12302 12303 12304 12305 12306 12307 12308 12309 12310 12311 12312 12313 12314 12315 12316 12317 12318 12319 12320 12321 12322 12323 12324 12325 12326 12327 12328 12329 12330 12331 12332 12333 12334 12335 12336 12337 12338 12339 12340 12341 12342 12343 12344 12345 12346 12347 12348 12349 12350 12351 12352 12353 12354 12355 12356 12357 12358 12359 12360 12361 12362 12363 12364 12365 12366 12367 12368 12369 12370 12371 12372 12373 12374 12375 12376 12377 12378 12379 12380 12381 12382 12383 12384 12385 12386 12387 12388 12389 12390 12391 12392 12393 12394 12395 12396 12397 12398 12399 12400 12401 12402 12403 12404 12405 12406 12407 12408 12409 12410 12411 12412 12413 12414 12415 12416 12417 12418 12419 12420 12421 12422 12423 12424 12425 12426 12427 12428 12429 12430 12431 12432 12433 12434 12435 12436 12437 12438 12439 12440 12441 12442 12443 12444 12445 12446 12447 12448 12449 12450 12451 12452 12453 12454 12455 12456 12457 12458 12459 12460 12461 12462 12463 12464 12465 12466 12467 12468 12469 12470 12471 12472 12473 12474 12475 12476 12477 12478 12479 12480 12481 12482 12483 12484 12485 12486 12487 12488 12489 12490 12491 12492 12493 12494 12495 12496 12497 12498 12499 12500 12501 12502 12503 12504 12505 12506 12507 12508 12509 12510 12511 12512 12513 12514 12515 12516 12517 12518 12519 12520 12521 12522 12523 12524 12525 12526 12527 12528 12529 12530 12531 12532 12533 12534 12535 12536 12537 12538 12539 12540 12541 12542 12543 12544 12545 12546 12547 12548 12549 12550 12551 12552 12553 12554 12555 12556 12557 12558 12559 12560 12561 12562 12563 12564 12565 12566 12567 12568 12569 12570 12571 12572 12573 12574 12575 12576 12577 12578 12579 12580 12581 12582 12583 12584 12585 12586 12587 12588 12589 12590 12591 12592 12593 12594 12595 12596 12597 12598 12599 12600 12601 12602 12603 12604 12605 12606 12607 12608 12609 12610 12611 12612 12613 12614 12615 12616 12617 12618 12619 12620 12621 12622 12623 12624 12625 12626 12627 12628 12629 12630 12631 12632 12633 12634 12635 12636 12637 12638 12639 12640 12641 12642 12643 12644 12645 12646 12647 12648 12649 12650 12651 12652 12653 12654 12655 12656 12657 12658 12659 12660 12661 12662 12663 12664 12665 12666 12667 12668 12669 12670 12671 12672 12673 12674 12675 12676 12677 12678 12679 12680 12681 12682 12683 12684 12685 12686 12687 12688 12689 12690 12691 12692 12693 12694 12695 12696 12697 12698 12699 12700 12701 12702 12703 12704 12705 12706 12707 12708 12709 12710 12711 12712 12713 12714 12715 12716 12717 12718 12719 12720 12721 12722 12723 12724 12725 12726 12727 12728 12729 12730 12731 12732 12733 12734 12735 12736 12737 12738 12739 12740 12741 12742 12743 12744 12745 12746 12747 12748 12749 12750 12751 12752 12753 12754 12755 12756 12757 12758 12759 12760 12761 12762 12763 12764 12765 12766 12767 12768 12769 12770 12771 12772 12773 12774 12775 12776 12777 12778 12779 12780 12781 12782 12783 12784 12785 12786 12787 12788 12789 12790 12791 12792 12793 12794 12795 12796 12797 12798 12799 12800 12801 12802 12803 12804 12805 12806 12807 12808 12809 12810 12811 12812 12813 12814 12815 12816 12817 12818 12819 12820 12821 12822 12823 12824 12825 12826 12827 12828 12829 12830 12831 12832 12833 12834 12835 12836 12837 12838 12839 12840 12841 12842 12843 12844 12845 12846 12847 12848 12849 12850 12851 12852 12853 12854 12855 12856 12857 12858 12859 12860 12861 12862 12863 12864 12865 12866 12867 12868 12869 12870 12871 12872 12873 12874 12875 12876 12877 12878 12879 12880 12881 12882 12883 12884 12885 12886 12887 12888 12889 12890 12891 12892 12893 12894 12895 12896 12897 12898 12899 12900 12901 12902 12903 12904 12905 12906 12907 12908 12909 12910 12911 12912 12913 12914 12915 12916 12917 12918 12919 12920 12921 12922 12923 12924 12925 12926 12927 12928 12929 12930 12931 12932 12933 12934 12935 12936 12937 12938 12939 12940 12941 12942 12943 12944 12945 12946 12947 12948 12949 12950 12951 12952 12953 12954 12955 12956 12957 12958 12959 12960 12961 12962 12963 12964 12965 12966 12967 12968 12969 12970 12971 12972 12973 12974 12975 12976 12977 12978 12979 12980 12981 12982 12983 12984 12985 12986 12987 12988 12989 12990 12991 12992 12993 12994 12995 12996 12997 12998 12999 13000 13001 13002 13003 13004 13005 13006 13007 13008 13009 13010 13011 13012 13013 13014 13015 13016 13017 13018 13019 13020 13021 13022 13023 13024 13025 13026 13027 13028 13029 13030 13031 13032 13033 13034 13035 13036 13037 13038 13039 13040 13041 13042 13043 13044 13045 13046 13047 13048 13049 13050 13051 13052 13053 13054 13055 13056 13057 13058 13059 13060 13061 13062 13063 13064 13065 13066 13067 13068 13069 13070 13071 13072 13073 13074 13075 13076 13077 13078 13079 13080 13081 13082 13083 13084 13085 13086 13087 13088 13089 13090 13091 13092 13093 13094 13095 13096 13097 13098 13099 13100 13101 13102 13103 13104 13105 13106 13107 13108 13109 13110 13111 13112 13113 13114 13115 13116 13117 13118 13119 13120 13121 13122 13123 13124 13125 13126 13127 13128 13129 13130 13131 13132 13133 13134 13135 13136 13137 13138 13139 13140 13141 13142 13143 13144 13145 13146 13147 13148 13149 13150 13151 13152 13153 13154 13155 13156 13157 13158 13159 13160 13161 13162 13163 13164 13165 13166 13167 13168 13169 13170 13171 13172 13173 13174 13175 13176 13177 13178 13179 13180 13181 13182 13183 13184 13185 13186 13187 13188 13189 13190 13191 13192 13193 13194 13195 13196 13197 13198 13199 13200 13201 13202 13203 13204 13205 13206 13207 13208 13209 13210 13211 13212 13213 13214 13215 13216 13217 13218 13219 13220 13221 13222 13223 13224 13225 13226 13227 13228 13229 13230 13231 13232 13233 13234 13235 13236 13237 13238 13239 13240 13241 13242 13243 13244 13245 13246 13247 13248 13249 13250 13251 13252 13253 13254 13255 13256 13257 13258 13259 13260 13261 13262 13263 13264 13265 13266 13267 13268 13269 13270 13271 13272 13273 13274 13275 13276 13277 13278 13279 13280 13281 13282 13283 13284 13285 13286 13287 13288 13289 13290 13291 13292 13293 13294 13295 13296 13297 13298 13299 13300 13301 13302 13303 13304 13305 13306 13307 13308 13309 13310 13311 13312 13313 13314 13315 13316 13317 13318 13319 13320 13321 13322 13323 13324 13325 13326 13327 13328 13329 13330 13331 13332 13333 13334 13335 13336 13337 13338 13339 13340 13341 13342 13343 13344 13345 13346 13347 13348 13349 13350 13351 13352 13353 13354 13355 13356 13357 13358 13359 13360 13361 13362 13363 13364 13365 13366 13367 13368 13369 13370 13371 13372 13373 13374 13375 13376 13377 13378 13379 13380 13381 13382 13383 13384 13385 13386 13387 13388 13389 13390 13391 13392 13393 13394 13395 13396 13397 13398 13399 13400 13401 13402 13403 13404 13405 13406 13407 13408 13409 13410 13411 13412 13413 13414 13415 13416 13417 13418 13419 13420 13421 13422 13423 13424 13425 13426 13427 13428 13429 13430 13431 13432 13433 13434 13435 13436 13437 13438 13439 13440 13441 13442 13443 13444 13445 13446 13447 13448 13449 13450 13451 13452 13453 13454 13455 13456 13457 13458 13459 13460 13461 13462 13463 13464 13465 13466 13467 13468 13469 13470 13471 13472 13473 13474 13475 13476 13477 13478 13479 13480 13481 13482 13483 13484 13485 13486 13487 13488 13489 13490 13491 13492 13493 13494 13495 13496 13497 13498 13499 13500 13501 13502 13503 13504 13505 13506 13507 13508 13509 13510 13511 13512 13513 13514 13515 13516 13517 13518 13519 13520 13521 13522 13523 13524 13525 13526 13527 13528 13529 13530 13531 13532 13533 13534 13535 13536 13537 13538 13539 13540 13541 13542 13543 13544 13545 13546 13547 13548 13549 13550 13551 13552 13553 13554 13555 13556 13557 13558 13559 13560 13561 13562 13563 13564 13565 13566 13567 13568 13569 13570 13571 13572 13573 13574 13575 13576 13577 13578 13579 13580 13581 13582 13583 13584 13585 13586 13587 13588 13589 13590 13591 13592 13593 13594 13595 13596 13597 13598 13599 13600 13601 13602 13603 13604 13605 13606 13607 13608 13609 13610 13611 13612 13613 13614 13615 13616 13617 13618 13619 13620 13621 13622 13623 13624 13625 13626 13627 13628 13629 13630 13631 13632 13633 13634 13635 13636 13637 13638 13639 13640 13641 13642 13643 13644 13645 13646 13647 13648 13649 13650 13651 13652 13653 13654 13655 13656 13657 13658 13659 13660 13661 13662 13663 13664 13665 13666 13667 13668 13669 13670 13671 13672 13673 13674 13675 13676 13677 13678 13679 13680 13681 13682 13683 13684 13685 13686 13687 13688 13689 13690 13691 13692 13693 13694 13695 13696 13697 13698 13699 13700 13701 13702 13703 13704 13705 13706 13707 13708 13709 13710 13711 13712 13713 13714 13715 13716 13717 13718 13719 13720 13721 13722 13723 13724 13725 13726 13727 13728 13729 13730 13731 13732 13733 13734 13735 13736 13737 13738 13739 13740 13741 13742 13743 13744 13745 13746 13747 13748 13749 13750 13751 13752 13753 13754 13755 13756 13757 13758
|
/**********************************************************************
compile.c - ruby node tree -> VM instruction sequence
$Author$
created at: 04/01/01 03:42:15 JST
Copyright (C) 2004-2007 Koichi Sasada
**********************************************************************/
#include "ruby/internal/config.h"
#include <math.h>
#ifdef HAVE_DLADDR
# include <dlfcn.h>
#endif
#include "encindex.h"
#include "id_table.h"
#include "internal.h"
#include "internal/array.h"
#include "internal/compile.h"
#include "internal/complex.h"
#include "internal/encoding.h"
#include "internal/error.h"
#include "internal/gc.h"
#include "internal/hash.h"
#include "internal/numeric.h"
#include "internal/object.h"
#include "internal/rational.h"
#include "internal/re.h"
#include "internal/symbol.h"
#include "internal/thread.h"
#include "internal/variable.h"
#include "iseq.h"
#include "ruby/re.h"
#include "ruby/util.h"
#include "vm_core.h"
#include "vm_callinfo.h"
#include "vm_debug.h"
#include "yjit.h"
#include "builtin.h"
#include "insns.inc"
#include "insns_info.inc"
#undef RUBY_UNTYPED_DATA_WARNING
#define RUBY_UNTYPED_DATA_WARNING 0
#define FIXNUM_INC(n, i) ((n)+(INT2FIX(i)&~FIXNUM_FLAG))
#define FIXNUM_OR(n, i) ((n)|INT2FIX(i))
typedef struct iseq_link_element {
enum {
ISEQ_ELEMENT_ANCHOR,
ISEQ_ELEMENT_LABEL,
ISEQ_ELEMENT_INSN,
ISEQ_ELEMENT_ADJUST,
ISEQ_ELEMENT_TRACE,
} type;
struct iseq_link_element *next;
struct iseq_link_element *prev;
} LINK_ELEMENT;
typedef struct iseq_link_anchor {
LINK_ELEMENT anchor;
LINK_ELEMENT *last;
} LINK_ANCHOR;
typedef enum {
LABEL_RESCUE_NONE,
LABEL_RESCUE_BEG,
LABEL_RESCUE_END,
LABEL_RESCUE_TYPE_MAX
} LABEL_RESCUE_TYPE;
typedef struct iseq_label_data {
LINK_ELEMENT link;
int label_no;
int position;
int sc_state;
int sp;
int refcnt;
unsigned int set: 1;
unsigned int rescued: 2;
unsigned int unremovable: 1;
} LABEL;
typedef struct iseq_insn_data {
LINK_ELEMENT link;
enum ruby_vminsn_type insn_id;
int operand_size;
int sc_state;
VALUE *operands;
struct {
int line_no;
int node_id;
rb_event_flag_t events;
} insn_info;
} INSN;
typedef struct iseq_adjust_data {
LINK_ELEMENT link;
LABEL *label;
int line_no;
} ADJUST;
typedef struct iseq_trace_data {
LINK_ELEMENT link;
rb_event_flag_t event;
long data;
} TRACE;
struct ensure_range {
LABEL *begin;
LABEL *end;
struct ensure_range *next;
};
struct iseq_compile_data_ensure_node_stack {
const void *ensure_node;
struct iseq_compile_data_ensure_node_stack *prev;
struct ensure_range *erange;
};
const ID rb_iseq_shared_exc_local_tbl[] = {idERROR_INFO};
/**
* debug function(macro) interface depend on CPDEBUG
* if it is less than 0, runtime option is in effect.
*
* debug level:
* 0: no debug output
* 1: show node type
* 2: show node important parameters
* ...
* 5: show other parameters
* 10: show every AST array
*/
#ifndef CPDEBUG
#define CPDEBUG 0
#endif
#if CPDEBUG >= 0
#define compile_debug CPDEBUG
#else
#define compile_debug ISEQ_COMPILE_DATA(iseq)->option->debug_level
#endif
#if CPDEBUG
#define compile_debug_print_indent(level) \
ruby_debug_print_indent((level), compile_debug, gl_node_level * 2)
#define debugp(header, value) (void) \
(compile_debug_print_indent(1) && \
ruby_debug_print_value(1, compile_debug, (header), (value)))
#define debugi(header, id) (void) \
(compile_debug_print_indent(1) && \
ruby_debug_print_id(1, compile_debug, (header), (id)))
#define debugp_param(header, value) (void) \
(compile_debug_print_indent(1) && \
ruby_debug_print_value(1, compile_debug, (header), (value)))
#define debugp_verbose(header, value) (void) \
(compile_debug_print_indent(2) && \
ruby_debug_print_value(2, compile_debug, (header), (value)))
#define debugp_verbose_node(header, value) (void) \
(compile_debug_print_indent(10) && \
ruby_debug_print_value(10, compile_debug, (header), (value)))
#define debug_node_start(node) ((void) \
(compile_debug_print_indent(1) && \
(ruby_debug_print_node(1, CPDEBUG, "", (const NODE *)(node)), gl_node_level)), \
gl_node_level++)
#define debug_node_end() gl_node_level --
#else
#define debugi(header, id) ((void)0)
#define debugp(header, value) ((void)0)
#define debugp_verbose(header, value) ((void)0)
#define debugp_verbose_node(header, value) ((void)0)
#define debugp_param(header, value) ((void)0)
#define debug_node_start(node) ((void)0)
#define debug_node_end() ((void)0)
#endif
#if CPDEBUG > 1 || CPDEBUG < 0
#undef printf
#define printf ruby_debug_printf
#define debugs if (compile_debug_print_indent(1)) ruby_debug_printf
#define debug_compile(msg, v) ((void)(compile_debug_print_indent(1) && fputs((msg), stderr)), (v))
#else
#define debugs if(0)printf
#define debug_compile(msg, v) (v)
#endif
#define LVAR_ERRINFO (1)
/* create new label */
#define NEW_LABEL(l) new_label_body(iseq, (l))
#define LABEL_FORMAT "<L%03d>"
#define NEW_ISEQ(node, name, type, line_no) \
new_child_iseq(iseq, (node), rb_fstring(name), 0, (type), (line_no))
#define NEW_CHILD_ISEQ(node, name, type, line_no) \
new_child_iseq(iseq, (node), rb_fstring(name), iseq, (type), (line_no))
/* add instructions */
#define ADD_SEQ(seq1, seq2) \
APPEND_LIST((seq1), (seq2))
/* add an instruction */
#define ADD_INSN(seq, line_node, insn) \
ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_body(iseq, (line_node), BIN(insn), 0))
/* insert an instruction before next */
#define INSERT_BEFORE_INSN(next, line_node, insn) \
ELEM_INSERT_PREV(&(next)->link, (LINK_ELEMENT *) new_insn_body(iseq, (line_node), BIN(insn), 0))
/* insert an instruction after prev */
#define INSERT_AFTER_INSN(prev, line_node, insn) \
ELEM_INSERT_NEXT(&(prev)->link, (LINK_ELEMENT *) new_insn_body(iseq, (line_node), BIN(insn), 0))
/* add an instruction with some operands (1, 2, 3, 5) */
#define ADD_INSN1(seq, line_node, insn, op1) \
ADD_ELEM((seq), (LINK_ELEMENT *) \
new_insn_body(iseq, (line_node), BIN(insn), 1, (VALUE)(op1)))
/* insert an instruction with some operands (1, 2, 3, 5) before next */
#define INSERT_BEFORE_INSN1(next, line_node, insn, op1) \
ELEM_INSERT_PREV(&(next)->link, (LINK_ELEMENT *) \
new_insn_body(iseq, (line_node), BIN(insn), 1, (VALUE)(op1)))
/* insert an instruction with some operands (1, 2, 3, 5) after prev */
#define INSERT_AFTER_INSN1(prev, line_node, insn, op1) \
ELEM_INSERT_NEXT(&(prev)->link, (LINK_ELEMENT *) \
new_insn_body(iseq, (line_node), BIN(insn), 1, (VALUE)(op1)))
#define LABEL_REF(label) ((label)->refcnt++)
/* add an instruction with label operand (alias of ADD_INSN1) */
#define ADD_INSNL(seq, line_node, insn, label) (ADD_INSN1(seq, line_node, insn, label), LABEL_REF(label))
#define ADD_INSN2(seq, line_node, insn, op1, op2) \
ADD_ELEM((seq), (LINK_ELEMENT *) \
new_insn_body(iseq, (line_node), BIN(insn), 2, (VALUE)(op1), (VALUE)(op2)))
#define ADD_INSN3(seq, line_node, insn, op1, op2, op3) \
ADD_ELEM((seq), (LINK_ELEMENT *) \
new_insn_body(iseq, (line_node), BIN(insn), 3, (VALUE)(op1), (VALUE)(op2), (VALUE)(op3)))
/* Specific Insn factory */
#define ADD_SEND(seq, line_node, id, argc) \
ADD_SEND_R((seq), (line_node), (id), (argc), NULL, (VALUE)INT2FIX(0), NULL)
#define ADD_SEND_WITH_FLAG(seq, line_node, id, argc, flag) \
ADD_SEND_R((seq), (line_node), (id), (argc), NULL, (VALUE)(flag), NULL)
#define ADD_SEND_WITH_BLOCK(seq, line_node, id, argc, block) \
ADD_SEND_R((seq), (line_node), (id), (argc), (block), (VALUE)INT2FIX(0), NULL)
#define ADD_CALL_RECEIVER(seq, line_node) \
ADD_INSN((seq), (line_node), putself)
#define ADD_CALL(seq, line_node, id, argc) \
ADD_SEND_R((seq), (line_node), (id), (argc), NULL, (VALUE)INT2FIX(VM_CALL_FCALL), NULL)
#define ADD_CALL_WITH_BLOCK(seq, line_node, id, argc, block) \
ADD_SEND_R((seq), (line_node), (id), (argc), (block), (VALUE)INT2FIX(VM_CALL_FCALL), NULL)
#define ADD_SEND_R(seq, line_node, id, argc, block, flag, keywords) \
ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_send(iseq, (line_node), (id), (VALUE)(argc), (block), (VALUE)(flag), (keywords)))
#define ADD_TRACE(seq, event) \
ADD_ELEM((seq), (LINK_ELEMENT *)new_trace_body(iseq, (event), 0))
#define ADD_TRACE_WITH_DATA(seq, event, data) \
ADD_ELEM((seq), (LINK_ELEMENT *)new_trace_body(iseq, (event), (data)))
static void iseq_add_getlocal(rb_iseq_t *iseq, LINK_ANCHOR *const seq, const NODE *const line_node, int idx, int level);
static void iseq_add_setlocal(rb_iseq_t *iseq, LINK_ANCHOR *const seq, const NODE *const line_node, int idx, int level);
#define ADD_GETLOCAL(seq, line_node, idx, level) iseq_add_getlocal(iseq, (seq), (line_node), (idx), (level))
#define ADD_SETLOCAL(seq, line_node, idx, level) iseq_add_setlocal(iseq, (seq), (line_node), (idx), (level))
/* add label */
#define ADD_LABEL(seq, label) \
ADD_ELEM((seq), (LINK_ELEMENT *) (label))
#define APPEND_LABEL(seq, before, label) \
APPEND_ELEM((seq), (before), (LINK_ELEMENT *) (label))
#define ADD_ADJUST(seq, line_node, label) \
ADD_ELEM((seq), (LINK_ELEMENT *) new_adjust_body(iseq, (label), nd_line(line_node)))
#define ADD_ADJUST_RESTORE(seq, label) \
ADD_ELEM((seq), (LINK_ELEMENT *) new_adjust_body(iseq, (label), -1))
#define LABEL_UNREMOVABLE(label) \
((label) ? (LABEL_REF(label), (label)->unremovable=1) : 0)
#define ADD_CATCH_ENTRY(type, ls, le, iseqv, lc) do { \
VALUE _e = rb_ary_new3(5, (type), \
(VALUE)(ls) | 1, (VALUE)(le) | 1, \
(VALUE)(iseqv), (VALUE)(lc) | 1); \
LABEL_UNREMOVABLE(ls); \
LABEL_REF(le); \
LABEL_REF(lc); \
if (NIL_P(ISEQ_COMPILE_DATA(iseq)->catch_table_ary)) \
RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->catch_table_ary, rb_ary_hidden_new(3)); \
rb_ary_push(ISEQ_COMPILE_DATA(iseq)->catch_table_ary, freeze_hide_obj(_e)); \
} while (0)
/* compile node */
#define COMPILE(anchor, desc, node) \
(debug_compile("== " desc "\n", \
iseq_compile_each(iseq, (anchor), (node), 0)))
/* compile node, this node's value will be popped */
#define COMPILE_POPPED(anchor, desc, node) \
(debug_compile("== " desc "\n", \
iseq_compile_each(iseq, (anchor), (node), 1)))
/* compile node, which is popped when 'popped' is true */
#define COMPILE_(anchor, desc, node, popped) \
(debug_compile("== " desc "\n", \
iseq_compile_each(iseq, (anchor), (node), (popped))))
#define COMPILE_RECV(anchor, desc, node, recv) \
(private_recv_p(node) ? \
(ADD_INSN(anchor, node, putself), VM_CALL_FCALL) : \
COMPILE(anchor, desc, recv) ? 0 : -1)
#define OPERAND_AT(insn, idx) \
(((INSN*)(insn))->operands[(idx)])
#define INSN_OF(insn) \
(((INSN*)(insn))->insn_id)
#define IS_INSN(link) ((link)->type == ISEQ_ELEMENT_INSN)
#define IS_LABEL(link) ((link)->type == ISEQ_ELEMENT_LABEL)
#define IS_ADJUST(link) ((link)->type == ISEQ_ELEMENT_ADJUST)
#define IS_TRACE(link) ((link)->type == ISEQ_ELEMENT_TRACE)
#define IS_INSN_ID(iobj, insn) (INSN_OF(iobj) == BIN(insn))
#define IS_NEXT_INSN_ID(link, insn) \
((link)->next && IS_INSN((link)->next) && IS_INSN_ID((link)->next, insn))
/* error */
#if CPDEBUG > 0
RBIMPL_ATTR_NORETURN()
#endif
RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 3, 4)
static void
append_compile_error(const rb_iseq_t *iseq, int line, const char *fmt, ...)
{
VALUE err_info = ISEQ_COMPILE_DATA(iseq)->err_info;
VALUE file = rb_iseq_path(iseq);
VALUE err = err_info == Qtrue ? Qfalse : err_info;
va_list args;
va_start(args, fmt);
err = rb_syntax_error_append(err, file, line, -1, NULL, fmt, args);
va_end(args);
if (NIL_P(err_info)) {
RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->err_info, err);
rb_set_errinfo(err);
}
else if (!err_info) {
RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->err_info, Qtrue);
}
if (compile_debug) {
if (SPECIAL_CONST_P(err)) err = rb_eSyntaxError;
rb_exc_fatal(err);
}
}
#if 0
static void
compile_bug(rb_iseq_t *iseq, int line, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
rb_report_bug_valist(rb_iseq_path(iseq), line, fmt, args);
va_end(args);
abort();
}
#endif
#define COMPILE_ERROR append_compile_error
#define ERROR_ARGS_AT(n) iseq, nd_line(n),
#define ERROR_ARGS ERROR_ARGS_AT(node)
#define EXPECT_NODE(prefix, node, ndtype, errval) \
do { \
const NODE *error_node = (node); \
enum node_type error_type = nd_type(error_node); \
if (error_type != (ndtype)) { \
COMPILE_ERROR(ERROR_ARGS_AT(error_node) \
prefix ": " #ndtype " is expected, but %s", \
ruby_node_name(error_type)); \
return errval; \
} \
} while (0)
#define EXPECT_NODE_NONULL(prefix, parent, ndtype, errval) \
do { \
COMPILE_ERROR(ERROR_ARGS_AT(parent) \
prefix ": must be " #ndtype ", but 0"); \
return errval; \
} while (0)
#define UNKNOWN_NODE(prefix, node, errval) \
do { \
const NODE *error_node = (node); \
COMPILE_ERROR(ERROR_ARGS_AT(error_node) prefix ": unknown node (%s)", \
ruby_node_name(nd_type(error_node))); \
return errval; \
} while (0)
#define COMPILE_OK 1
#define COMPILE_NG 0
#define CHECK(sub) if (!(sub)) {BEFORE_RETURN;return COMPILE_NG;}
#define NO_CHECK(sub) (void)(sub)
#define BEFORE_RETURN
/* leave name uninitialized so that compiler warn if INIT_ANCHOR is
* missing */
#define DECL_ANCHOR(name) \
LINK_ANCHOR name[1] = {{{ISEQ_ELEMENT_ANCHOR,},}}
#define INIT_ANCHOR(name) \
(name->last = &name->anchor)
static inline VALUE
freeze_hide_obj(VALUE obj)
{
OBJ_FREEZE(obj);
RBASIC_CLEAR_CLASS(obj);
return obj;
}
#include "optinsn.inc"
#if OPT_INSTRUCTIONS_UNIFICATION
#include "optunifs.inc"
#endif
/* for debug */
#if CPDEBUG < 0
#define ISEQ_ARG iseq,
#define ISEQ_ARG_DECLARE rb_iseq_t *iseq,
#else
#define ISEQ_ARG
#define ISEQ_ARG_DECLARE
#endif
#if CPDEBUG
#define gl_node_level ISEQ_COMPILE_DATA(iseq)->node_level
#endif
static void dump_disasm_list_with_cursor(const LINK_ELEMENT *link, const LINK_ELEMENT *curr, const LABEL *dest);
static void dump_disasm_list(const LINK_ELEMENT *elem);
static int insn_data_length(INSN *iobj);
static int calc_sp_depth(int depth, INSN *iobj);
static INSN *new_insn_body(rb_iseq_t *iseq, const NODE *const line_node, enum ruby_vminsn_type insn_id, int argc, ...);
static LABEL *new_label_body(rb_iseq_t *iseq, long line);
static ADJUST *new_adjust_body(rb_iseq_t *iseq, LABEL *label, int line);
static TRACE *new_trace_body(rb_iseq_t *iseq, rb_event_flag_t event, long data);
static int iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *anchor, const NODE *n, int);
static int iseq_setup(rb_iseq_t *iseq, LINK_ANCHOR *const anchor);
static int iseq_setup_insn(rb_iseq_t *iseq, LINK_ANCHOR *const anchor);
static int iseq_optimize(rb_iseq_t *iseq, LINK_ANCHOR *const anchor);
static int iseq_insns_unification(rb_iseq_t *iseq, LINK_ANCHOR *const anchor);
static int iseq_set_local_table(rb_iseq_t *iseq, const rb_ast_id_table_t *tbl);
static int iseq_set_exception_local_table(rb_iseq_t *iseq);
static int iseq_set_arguments(rb_iseq_t *iseq, LINK_ANCHOR *const anchor, const NODE *const node);
static int iseq_set_sequence(rb_iseq_t *iseq, LINK_ANCHOR *const anchor);
static int iseq_set_exception_table(rb_iseq_t *iseq);
static int iseq_set_optargs_table(rb_iseq_t *iseq);
static int compile_defined_expr(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, VALUE needstr);
static int compile_hash(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node, int method_call_keywords, int popped);
/*
* To make Array to LinkedList, use link_anchor
*/
static void
verify_list(ISEQ_ARG_DECLARE const char *info, LINK_ANCHOR *const anchor)
{
#if CPDEBUG
int flag = 0;
LINK_ELEMENT *list, *plist;
if (!compile_debug) return;
list = anchor->anchor.next;
plist = &anchor->anchor;
while (list) {
if (plist != list->prev) {
flag += 1;
}
plist = list;
list = list->next;
}
if (anchor->last != plist && anchor->last != 0) {
flag |= 0x70000;
}
if (flag != 0) {
rb_bug("list verify error: %08x (%s)", flag, info);
}
#endif
}
#if CPDEBUG < 0
#define verify_list(info, anchor) verify_list(iseq, (info), (anchor))
#endif
static void
verify_call_cache(rb_iseq_t *iseq)
{
#if CPDEBUG
VALUE *original = rb_iseq_original_iseq(iseq);
size_t i = 0;
while (i < ISEQ_BODY(iseq)->iseq_size) {
VALUE insn = original[i];
const char *types = insn_op_types(insn);
for (int j=0; types[j]; j++) {
if (types[j] == TS_CALLDATA) {
struct rb_call_data *cd = (struct rb_call_data *)original[i+j+1];
const struct rb_callinfo *ci = cd->ci;
const struct rb_callcache *cc = cd->cc;
if (cc != vm_cc_empty()) {
vm_ci_dump(ci);
rb_bug("call cache is not initialized by vm_cc_empty()");
}
}
}
i += insn_len(insn);
}
for (unsigned int i=0; i<ISEQ_BODY(iseq)->ci_size; i++) {
struct rb_call_data *cd = &ISEQ_BODY(iseq)->call_data[i];
const struct rb_callinfo *ci = cd->ci;
const struct rb_callcache *cc = cd->cc;
if (cc != NULL && cc != vm_cc_empty()) {
vm_ci_dump(ci);
rb_bug("call cache is not initialized by vm_cc_empty()");
}
}
#endif
}
/*
* elem1, elem2 => elem1, elem2, elem
*/
static void
ADD_ELEM(ISEQ_ARG_DECLARE LINK_ANCHOR *const anchor, LINK_ELEMENT *elem)
{
elem->prev = anchor->last;
anchor->last->next = elem;
anchor->last = elem;
verify_list("add", anchor);
}
/*
* elem1, before, elem2 => elem1, before, elem, elem2
*/
static void
APPEND_ELEM(ISEQ_ARG_DECLARE LINK_ANCHOR *const anchor, LINK_ELEMENT *before, LINK_ELEMENT *elem)
{
elem->prev = before;
elem->next = before->next;
elem->next->prev = elem;
before->next = elem;
if (before == anchor->last) anchor->last = elem;
verify_list("add", anchor);
}
#if CPDEBUG < 0
#define ADD_ELEM(anchor, elem) ADD_ELEM(iseq, (anchor), (elem))
#define APPEND_ELEM(anchor, before, elem) APPEND_ELEM(iseq, (anchor), (before), (elem))
#endif
static int
branch_coverage_valid_p(rb_iseq_t *iseq, int first_line)
{
if (!ISEQ_COVERAGE(iseq)) return 0;
if (!ISEQ_BRANCH_COVERAGE(iseq)) return 0;
if (first_line <= 0) return 0;
return 1;
}
static VALUE
decl_branch_base(rb_iseq_t *iseq, const NODE *node, const char *type)
{
const int first_lineno = nd_first_lineno(node), first_column = nd_first_column(node);
const int last_lineno = nd_last_lineno(node), last_column = nd_last_column(node);
if (!branch_coverage_valid_p(iseq, first_lineno)) return Qundef;
/*
* if !structure[node]
* structure[node] = [type, first_lineno, first_column, last_lineno, last_column, branches = {}]
* else
* branches = structure[node][5]
* end
*/
VALUE structure = RARRAY_AREF(ISEQ_BRANCH_COVERAGE(iseq), 0);
VALUE key = (VALUE)node | 1; // FIXNUM for hash key
VALUE branch_base = rb_hash_aref(structure, key);
VALUE branches;
if (NIL_P(branch_base)) {
branch_base = rb_ary_hidden_new(6);
rb_hash_aset(structure, key, branch_base);
rb_ary_push(branch_base, ID2SYM(rb_intern(type)));
rb_ary_push(branch_base, INT2FIX(first_lineno));
rb_ary_push(branch_base, INT2FIX(first_column));
rb_ary_push(branch_base, INT2FIX(last_lineno));
rb_ary_push(branch_base, INT2FIX(last_column));
branches = rb_hash_new();
rb_obj_hide(branches);
rb_ary_push(branch_base, branches);
}
else {
branches = RARRAY_AREF(branch_base, 5);
}
return branches;
}
static NODE
generate_dummy_line_node(int lineno, int node_id)
{
NODE dummy = { 0 };
nd_set_line(&dummy, lineno);
nd_set_node_id(&dummy, node_id);
return dummy;
}
static void
add_trace_branch_coverage(rb_iseq_t *iseq, LINK_ANCHOR *const seq, const NODE *node, int branch_id, const char *type, VALUE branches)
{
const int first_lineno = nd_first_lineno(node), first_column = nd_first_column(node);
const int last_lineno = nd_last_lineno(node), last_column = nd_last_column(node);
if (!branch_coverage_valid_p(iseq, first_lineno)) return;
/*
* if !branches[branch_id]
* branches[branch_id] = [type, first_lineno, first_column, last_lineno, last_column, counter_idx]
* else
* counter_idx= branches[branch_id][5]
* end
*/
VALUE key = INT2FIX(branch_id);
VALUE branch = rb_hash_aref(branches, key);
long counter_idx;
if (NIL_P(branch)) {
branch = rb_ary_hidden_new(6);
rb_hash_aset(branches, key, branch);
rb_ary_push(branch, ID2SYM(rb_intern(type)));
rb_ary_push(branch, INT2FIX(first_lineno));
rb_ary_push(branch, INT2FIX(first_column));
rb_ary_push(branch, INT2FIX(last_lineno));
rb_ary_push(branch, INT2FIX(last_column));
VALUE counters = RARRAY_AREF(ISEQ_BRANCH_COVERAGE(iseq), 1);
counter_idx = RARRAY_LEN(counters);
rb_ary_push(branch, LONG2FIX(counter_idx));
rb_ary_push(counters, INT2FIX(0));
}
else {
counter_idx = FIX2LONG(RARRAY_AREF(branch, 5));
}
ADD_TRACE_WITH_DATA(seq, RUBY_EVENT_COVERAGE_BRANCH, counter_idx);
NODE dummy_line_node = generate_dummy_line_node(last_lineno, nd_node_id(node));
ADD_INSN(seq, &dummy_line_node, nop);
}
#define ISEQ_LAST_LINE(iseq) (ISEQ_COMPILE_DATA(iseq)->last_line)
static int
validate_label(st_data_t name, st_data_t label, st_data_t arg)
{
rb_iseq_t *iseq = (rb_iseq_t *)arg;
LABEL *lobj = (LABEL *)label;
if (!lobj->link.next) {
do {
COMPILE_ERROR(iseq, lobj->position,
"%"PRIsVALUE": undefined label",
rb_sym2str((VALUE)name));
} while (0);
}
return ST_CONTINUE;
}
static void
validate_labels(rb_iseq_t *iseq, st_table *labels_table)
{
st_foreach(labels_table, validate_label, (st_data_t)iseq);
st_free_table(labels_table);
}
static NODE *
get_nd_recv(const NODE *node)
{
switch (nd_type(node)) {
case NODE_CALL:
return RNODE_CALL(node)->nd_recv;
case NODE_OPCALL:
return RNODE_OPCALL(node)->nd_recv;
case NODE_FCALL:
return 0;
case NODE_QCALL:
return RNODE_QCALL(node)->nd_recv;
case NODE_VCALL:
return 0;
case NODE_ATTRASGN:
return RNODE_ATTRASGN(node)->nd_recv;
case NODE_OP_ASGN1:
return RNODE_OP_ASGN1(node)->nd_recv;
case NODE_OP_ASGN2:
return RNODE_OP_ASGN2(node)->nd_recv;
default:
rb_bug("unexpected node: %s", ruby_node_name(nd_type(node)));
}
}
static ID
get_node_call_nd_mid(const NODE *node)
{
switch (nd_type(node)) {
case NODE_CALL:
return RNODE_CALL(node)->nd_mid;
case NODE_OPCALL:
return RNODE_OPCALL(node)->nd_mid;
case NODE_FCALL:
return RNODE_FCALL(node)->nd_mid;
case NODE_QCALL:
return RNODE_QCALL(node)->nd_mid;
case NODE_VCALL:
return RNODE_VCALL(node)->nd_mid;
case NODE_ATTRASGN:
return RNODE_ATTRASGN(node)->nd_mid;
default:
rb_bug("unexpected node: %s", ruby_node_name(nd_type(node)));
}
}
static NODE *
get_nd_args(const NODE *node)
{
switch (nd_type(node)) {
case NODE_CALL:
return RNODE_CALL(node)->nd_args;
case NODE_OPCALL:
return RNODE_OPCALL(node)->nd_args;
case NODE_FCALL:
return RNODE_FCALL(node)->nd_args;
case NODE_QCALL:
return RNODE_QCALL(node)->nd_args;
case NODE_VCALL:
return 0;
case NODE_ATTRASGN:
return RNODE_ATTRASGN(node)->nd_args;
default:
rb_bug("unexpected node: %s", ruby_node_name(nd_type(node)));
}
}
static ID
get_node_colon_nd_mid(const NODE *node)
{
switch (nd_type(node)) {
case NODE_COLON2:
return RNODE_COLON2(node)->nd_mid;
case NODE_COLON3:
return RNODE_COLON3(node)->nd_mid;
default:
rb_bug("unexpected node: %s", ruby_node_name(nd_type(node)));
}
}
static ID
get_nd_vid(const NODE *node)
{
switch (nd_type(node)) {
case NODE_LASGN:
return RNODE_LASGN(node)->nd_vid;
case NODE_DASGN:
return RNODE_DASGN(node)->nd_vid;
case NODE_IASGN:
return RNODE_IASGN(node)->nd_vid;
case NODE_CVASGN:
return RNODE_CVASGN(node)->nd_vid;
default:
rb_bug("unexpected node: %s", ruby_node_name(nd_type(node)));
}
}
static NODE *
get_nd_value(const NODE *node)
{
switch (nd_type(node)) {
case NODE_LASGN:
return RNODE_LASGN(node)->nd_value;
case NODE_DASGN:
return RNODE_DASGN(node)->nd_value;
default:
rb_bug("unexpected node: %s", ruby_node_name(nd_type(node)));
}
}
VALUE
rb_iseq_compile_callback(rb_iseq_t *iseq, const struct rb_iseq_new_with_callback_callback_func * ifunc)
{
DECL_ANCHOR(ret);
INIT_ANCHOR(ret);
(*ifunc->func)(iseq, ret, ifunc->data);
NODE dummy_line_node = generate_dummy_line_node(ISEQ_COMPILE_DATA(iseq)->last_line, -1);
ADD_INSN(ret, &dummy_line_node, leave);
CHECK(iseq_setup_insn(iseq, ret));
return iseq_setup(iseq, ret);
}
VALUE
rb_iseq_compile_node(rb_iseq_t *iseq, const NODE *node)
{
DECL_ANCHOR(ret);
INIT_ANCHOR(ret);
if (IMEMO_TYPE_P(node, imemo_ifunc)) {
rb_raise(rb_eArgError, "unexpected imemo_ifunc");
}
if (node == 0) {
NO_CHECK(COMPILE(ret, "nil", node));
iseq_set_local_table(iseq, 0);
}
/* assume node is T_NODE */
else if (nd_type_p(node, NODE_SCOPE)) {
/* iseq type of top, method, class, block */
iseq_set_local_table(iseq, RNODE_SCOPE(node)->nd_tbl);
iseq_set_arguments(iseq, ret, (NODE *)RNODE_SCOPE(node)->nd_args);
switch (ISEQ_BODY(iseq)->type) {
case ISEQ_TYPE_BLOCK:
{
LABEL *start = ISEQ_COMPILE_DATA(iseq)->start_label = NEW_LABEL(0);
LABEL *end = ISEQ_COMPILE_DATA(iseq)->end_label = NEW_LABEL(0);
start->rescued = LABEL_RESCUE_BEG;
end->rescued = LABEL_RESCUE_END;
ADD_TRACE(ret, RUBY_EVENT_B_CALL);
NODE dummy_line_node = generate_dummy_line_node(ISEQ_BODY(iseq)->location.first_lineno, -1);
ADD_INSN (ret, &dummy_line_node, nop);
ADD_LABEL(ret, start);
CHECK(COMPILE(ret, "block body", RNODE_SCOPE(node)->nd_body));
ADD_LABEL(ret, end);
ADD_TRACE(ret, RUBY_EVENT_B_RETURN);
ISEQ_COMPILE_DATA(iseq)->last_line = ISEQ_BODY(iseq)->location.code_location.end_pos.lineno;
/* wide range catch handler must put at last */
ADD_CATCH_ENTRY(CATCH_TYPE_REDO, start, end, NULL, start);
ADD_CATCH_ENTRY(CATCH_TYPE_NEXT, start, end, NULL, end);
break;
}
case ISEQ_TYPE_CLASS:
{
ADD_TRACE(ret, RUBY_EVENT_CLASS);
CHECK(COMPILE(ret, "scoped node", RNODE_SCOPE(node)->nd_body));
ADD_TRACE(ret, RUBY_EVENT_END);
ISEQ_COMPILE_DATA(iseq)->last_line = nd_line(node);
break;
}
case ISEQ_TYPE_METHOD:
{
ISEQ_COMPILE_DATA(iseq)->root_node = RNODE_SCOPE(node)->nd_body;
ADD_TRACE(ret, RUBY_EVENT_CALL);
CHECK(COMPILE(ret, "scoped node", RNODE_SCOPE(node)->nd_body));
ISEQ_COMPILE_DATA(iseq)->root_node = RNODE_SCOPE(node)->nd_body;
ADD_TRACE(ret, RUBY_EVENT_RETURN);
ISEQ_COMPILE_DATA(iseq)->last_line = nd_line(node);
break;
}
default: {
CHECK(COMPILE(ret, "scoped node", RNODE_SCOPE(node)->nd_body));
break;
}
}
}
else {
const char *m;
#define INVALID_ISEQ_TYPE(type) \
ISEQ_TYPE_##type: m = #type; goto invalid_iseq_type
switch (ISEQ_BODY(iseq)->type) {
case INVALID_ISEQ_TYPE(METHOD);
case INVALID_ISEQ_TYPE(CLASS);
case INVALID_ISEQ_TYPE(BLOCK);
case INVALID_ISEQ_TYPE(EVAL);
case INVALID_ISEQ_TYPE(MAIN);
case INVALID_ISEQ_TYPE(TOP);
#undef INVALID_ISEQ_TYPE /* invalid iseq types end */
case ISEQ_TYPE_RESCUE:
iseq_set_exception_local_table(iseq);
CHECK(COMPILE(ret, "rescue", node));
break;
case ISEQ_TYPE_ENSURE:
iseq_set_exception_local_table(iseq);
CHECK(COMPILE_POPPED(ret, "ensure", node));
break;
case ISEQ_TYPE_PLAIN:
CHECK(COMPILE(ret, "ensure", node));
break;
default:
COMPILE_ERROR(ERROR_ARGS "unknown scope: %d", ISEQ_BODY(iseq)->type);
return COMPILE_NG;
invalid_iseq_type:
COMPILE_ERROR(ERROR_ARGS "compile/ISEQ_TYPE_%s should not be reached", m);
return COMPILE_NG;
}
}
if (ISEQ_BODY(iseq)->type == ISEQ_TYPE_RESCUE || ISEQ_BODY(iseq)->type == ISEQ_TYPE_ENSURE) {
NODE dummy_line_node = generate_dummy_line_node(0, -1);
ADD_GETLOCAL(ret, &dummy_line_node, LVAR_ERRINFO, 0);
ADD_INSN1(ret, &dummy_line_node, throw, INT2FIX(0) /* continue throw */ );
}
else {
NODE dummy_line_node = generate_dummy_line_node(ISEQ_COMPILE_DATA(iseq)->last_line, -1);
ADD_INSN(ret, &dummy_line_node, leave);
}
#if OPT_SUPPORT_JOKE
if (ISEQ_COMPILE_DATA(iseq)->labels_table) {
st_table *labels_table = ISEQ_COMPILE_DATA(iseq)->labels_table;
ISEQ_COMPILE_DATA(iseq)->labels_table = 0;
validate_labels(iseq, labels_table);
}
#endif
CHECK(iseq_setup_insn(iseq, ret));
return iseq_setup(iseq, ret);
}
static VALUE rb_translate_prism(pm_parser_t *parser, rb_iseq_t *iseq, pm_scope_node_t *scope_node, LINK_ANCHOR *const ret);
VALUE
rb_iseq_compile_prism_node(rb_iseq_t * iseq, pm_scope_node_t *scope_node, pm_parser_t *parser)
{
DECL_ANCHOR(ret);
INIT_ANCHOR(ret);
CHECK(rb_translate_prism(parser, iseq, scope_node, ret));
CHECK(iseq_setup_insn(iseq, ret));
return iseq_setup(iseq, ret);
}
static int
rb_iseq_translate_threaded_code(rb_iseq_t *iseq)
{
#if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
const void * const *table = rb_vm_get_insns_address_table();
unsigned int i;
VALUE *encoded = (VALUE *)ISEQ_BODY(iseq)->iseq_encoded;
for (i = 0; i < ISEQ_BODY(iseq)->iseq_size; /* */ ) {
int insn = (int)ISEQ_BODY(iseq)->iseq_encoded[i];
int len = insn_len(insn);
encoded[i] = (VALUE)table[insn];
i += len;
}
FL_SET((VALUE)iseq, ISEQ_TRANSLATED);
#endif
#if USE_YJIT
rb_yjit_live_iseq_count++;
#endif
return COMPILE_OK;
}
VALUE *
rb_iseq_original_iseq(const rb_iseq_t *iseq) /* cold path */
{
VALUE *original_code;
if (ISEQ_ORIGINAL_ISEQ(iseq)) return ISEQ_ORIGINAL_ISEQ(iseq);
original_code = ISEQ_ORIGINAL_ISEQ_ALLOC(iseq, ISEQ_BODY(iseq)->iseq_size);
MEMCPY(original_code, ISEQ_BODY(iseq)->iseq_encoded, VALUE, ISEQ_BODY(iseq)->iseq_size);
#if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
{
unsigned int i;
for (i = 0; i < ISEQ_BODY(iseq)->iseq_size; /* */ ) {
const void *addr = (const void *)original_code[i];
const int insn = rb_vm_insn_addr2insn(addr);
original_code[i] = insn;
i += insn_len(insn);
}
}
#endif
return original_code;
}
/*********************************************/
/* definition of data structure for compiler */
/*********************************************/
/*
* On 32-bit SPARC, GCC by default generates SPARC V7 code that may require
* 8-byte word alignment. On the other hand, Oracle Solaris Studio seems to
* generate SPARCV8PLUS code with unaligned memory access instructions.
* That is why the STRICT_ALIGNMENT is defined only with GCC.
*/
#if defined(__sparc) && SIZEOF_VOIDP == 4 && defined(__GNUC__)
#define STRICT_ALIGNMENT
#endif
/*
* Some OpenBSD platforms (including sparc64) require strict alignment.
*/
#if defined(__OpenBSD__)
#include <sys/endian.h>
#ifdef __STRICT_ALIGNMENT
#define STRICT_ALIGNMENT
#endif
#endif
#ifdef STRICT_ALIGNMENT
#if defined(HAVE_TRUE_LONG_LONG) && SIZEOF_LONG_LONG > SIZEOF_VALUE
#define ALIGNMENT_SIZE SIZEOF_LONG_LONG
#else
#define ALIGNMENT_SIZE SIZEOF_VALUE
#endif
#define PADDING_SIZE_MAX ((size_t)((ALIGNMENT_SIZE) - 1))
#define ALIGNMENT_SIZE_MASK PADDING_SIZE_MAX
/* Note: ALIGNMENT_SIZE == (2 ** N) is expected. */
#else
#define PADDING_SIZE_MAX 0
#endif /* STRICT_ALIGNMENT */
#ifdef STRICT_ALIGNMENT
/* calculate padding size for aligned memory access */
static size_t
calc_padding(void *ptr, size_t size)
{
size_t mis;
size_t padding = 0;
mis = (size_t)ptr & ALIGNMENT_SIZE_MASK;
if (mis > 0) {
padding = ALIGNMENT_SIZE - mis;
}
/*
* On 32-bit sparc or equivalents, when a single VALUE is requested
* and padding == sizeof(VALUE), it is clear that no padding is needed.
*/
#if ALIGNMENT_SIZE > SIZEOF_VALUE
if (size == sizeof(VALUE) && padding == sizeof(VALUE)) {
padding = 0;
}
#endif
return padding;
}
#endif /* STRICT_ALIGNMENT */
static void *
compile_data_alloc_with_arena(struct iseq_compile_data_storage **arena, size_t size)
{
void *ptr = 0;
struct iseq_compile_data_storage *storage = *arena;
#ifdef STRICT_ALIGNMENT
size_t padding = calc_padding((void *)&storage->buff[storage->pos], size);
#else
const size_t padding = 0; /* expected to be optimized by compiler */
#endif /* STRICT_ALIGNMENT */
if (size >= INT_MAX - padding) rb_memerror();
if (storage->pos + size + padding > storage->size) {
unsigned int alloc_size = storage->size;
while (alloc_size < size + PADDING_SIZE_MAX) {
if (alloc_size >= INT_MAX / 2) rb_memerror();
alloc_size *= 2;
}
storage->next = (void *)ALLOC_N(char, alloc_size +
offsetof(struct iseq_compile_data_storage, buff));
storage = *arena = storage->next;
storage->next = 0;
storage->pos = 0;
storage->size = alloc_size;
#ifdef STRICT_ALIGNMENT
padding = calc_padding((void *)&storage->buff[storage->pos], size);
#endif /* STRICT_ALIGNMENT */
}
#ifdef STRICT_ALIGNMENT
storage->pos += (int)padding;
#endif /* STRICT_ALIGNMENT */
ptr = (void *)&storage->buff[storage->pos];
storage->pos += (int)size;
return ptr;
}
static void *
compile_data_alloc(rb_iseq_t *iseq, size_t size)
{
struct iseq_compile_data_storage ** arena = &ISEQ_COMPILE_DATA(iseq)->node.storage_current;
return compile_data_alloc_with_arena(arena, size);
}
static inline void *
compile_data_alloc2(rb_iseq_t *iseq, size_t x, size_t y)
{
size_t size = rb_size_mul_or_raise(x, y, rb_eRuntimeError);
return compile_data_alloc(iseq, size);
}
static inline void *
compile_data_calloc2(rb_iseq_t *iseq, size_t x, size_t y)
{
size_t size = rb_size_mul_or_raise(x, y, rb_eRuntimeError);
void *p = compile_data_alloc(iseq, size);
memset(p, 0, size);
return p;
}
static INSN *
compile_data_alloc_insn(rb_iseq_t *iseq)
{
struct iseq_compile_data_storage ** arena = &ISEQ_COMPILE_DATA(iseq)->insn.storage_current;
return (INSN *)compile_data_alloc_with_arena(arena, sizeof(INSN));
}
static LABEL *
compile_data_alloc_label(rb_iseq_t *iseq)
{
return (LABEL *)compile_data_alloc(iseq, sizeof(LABEL));
}
static ADJUST *
compile_data_alloc_adjust(rb_iseq_t *iseq)
{
return (ADJUST *)compile_data_alloc(iseq, sizeof(ADJUST));
}
static TRACE *
compile_data_alloc_trace(rb_iseq_t *iseq)
{
return (TRACE *)compile_data_alloc(iseq, sizeof(TRACE));
}
/*
* elem1, elemX => elem1, elem2, elemX
*/
static void
ELEM_INSERT_NEXT(LINK_ELEMENT *elem1, LINK_ELEMENT *elem2)
{
elem2->next = elem1->next;
elem2->prev = elem1;
elem1->next = elem2;
if (elem2->next) {
elem2->next->prev = elem2;
}
}
/*
* elem1, elemX => elemX, elem2, elem1
*/
static void
ELEM_INSERT_PREV(LINK_ELEMENT *elem1, LINK_ELEMENT *elem2)
{
elem2->prev = elem1->prev;
elem2->next = elem1;
elem1->prev = elem2;
if (elem2->prev) {
elem2->prev->next = elem2;
}
}
/*
* elemX, elem1, elemY => elemX, elem2, elemY
*/
static void
ELEM_REPLACE(LINK_ELEMENT *elem1, LINK_ELEMENT *elem2)
{
elem2->prev = elem1->prev;
elem2->next = elem1->next;
if (elem1->prev) {
elem1->prev->next = elem2;
}
if (elem1->next) {
elem1->next->prev = elem2;
}
}
static void
ELEM_REMOVE(LINK_ELEMENT *elem)
{
elem->prev->next = elem->next;
if (elem->next) {
elem->next->prev = elem->prev;
}
}
static LINK_ELEMENT *
FIRST_ELEMENT(const LINK_ANCHOR *const anchor)
{
return anchor->anchor.next;
}
static LINK_ELEMENT *
LAST_ELEMENT(LINK_ANCHOR *const anchor)
{
return anchor->last;
}
static LINK_ELEMENT *
ELEM_FIRST_INSN(LINK_ELEMENT *elem)
{
while (elem) {
switch (elem->type) {
case ISEQ_ELEMENT_INSN:
case ISEQ_ELEMENT_ADJUST:
return elem;
default:
elem = elem->next;
}
}
return NULL;
}
static int
LIST_INSN_SIZE_ONE(const LINK_ANCHOR *const anchor)
{
LINK_ELEMENT *first_insn = ELEM_FIRST_INSN(FIRST_ELEMENT(anchor));
if (first_insn != NULL &&
ELEM_FIRST_INSN(first_insn->next) == NULL) {
return TRUE;
}
else {
return FALSE;
}
}
static int
LIST_INSN_SIZE_ZERO(const LINK_ANCHOR *const anchor)
{
if (ELEM_FIRST_INSN(FIRST_ELEMENT(anchor)) == NULL) {
return TRUE;
}
else {
return FALSE;
}
}
/*
* anc1: e1, e2, e3
* anc2: e4, e5
*#=>
* anc1: e1, e2, e3, e4, e5
* anc2: e4, e5 (broken)
*/
static void
APPEND_LIST(ISEQ_ARG_DECLARE LINK_ANCHOR *const anc1, LINK_ANCHOR *const anc2)
{
if (anc2->anchor.next) {
anc1->last->next = anc2->anchor.next;
anc2->anchor.next->prev = anc1->last;
anc1->last = anc2->last;
}
verify_list("append", anc1);
}
#if CPDEBUG < 0
#define APPEND_LIST(anc1, anc2) APPEND_LIST(iseq, (anc1), (anc2))
#endif
#if CPDEBUG && 0
static void
debug_list(ISEQ_ARG_DECLARE LINK_ANCHOR *const anchor, LINK_ELEMENT *cur)
{
LINK_ELEMENT *list = FIRST_ELEMENT(anchor);
printf("----\n");
printf("anch: %p, frst: %p, last: %p\n", (void *)&anchor->anchor,
(void *)anchor->anchor.next, (void *)anchor->last);
while (list) {
printf("curr: %p, next: %p, prev: %p, type: %d\n", (void *)list, (void *)list->next,
(void *)list->prev, (int)list->type);
list = list->next;
}
printf("----\n");
dump_disasm_list_with_cursor(anchor->anchor.next, cur, 0);
verify_list("debug list", anchor);
}
#if CPDEBUG < 0
#define debug_list(anc, cur) debug_list(iseq, (anc), (cur))
#endif
#else
#define debug_list(anc, cur) ((void)0)
#endif
static TRACE *
new_trace_body(rb_iseq_t *iseq, rb_event_flag_t event, long data)
{
TRACE *trace = compile_data_alloc_trace(iseq);
trace->link.type = ISEQ_ELEMENT_TRACE;
trace->link.next = NULL;
trace->event = event;
trace->data = data;
return trace;
}
static LABEL *
new_label_body(rb_iseq_t *iseq, long line)
{
LABEL *labelobj = compile_data_alloc_label(iseq);
labelobj->link.type = ISEQ_ELEMENT_LABEL;
labelobj->link.next = 0;
labelobj->label_no = ISEQ_COMPILE_DATA(iseq)->label_no++;
labelobj->sc_state = 0;
labelobj->sp = -1;
labelobj->refcnt = 0;
labelobj->set = 0;
labelobj->rescued = LABEL_RESCUE_NONE;
labelobj->unremovable = 0;
return labelobj;
}
static ADJUST *
new_adjust_body(rb_iseq_t *iseq, LABEL *label, int line)
{
ADJUST *adjust = compile_data_alloc_adjust(iseq);
adjust->link.type = ISEQ_ELEMENT_ADJUST;
adjust->link.next = 0;
adjust->label = label;
adjust->line_no = line;
LABEL_UNREMOVABLE(label);
return adjust;
}
static void
iseq_insn_each_markable_object(INSN *insn, void (*func)(VALUE, VALUE), VALUE data)
{
const char *types = insn_op_types(insn->insn_id);
for (int j = 0; types[j]; j++) {
char type = types[j];
switch (type) {
case TS_CDHASH:
case TS_ISEQ:
case TS_VALUE:
case TS_IC: // constant path array
case TS_CALLDATA: // ci is stored.
func(OPERAND_AT(insn, j), data);
break;
default:
break;
}
}
}
static void
iseq_insn_each_object_write_barrier(VALUE obj, VALUE iseq)
{
RB_OBJ_WRITTEN(iseq, Qundef, obj);
}
static INSN *
new_insn_core(rb_iseq_t *iseq, const NODE *line_node,
int insn_id, int argc, VALUE *argv)
{
INSN *iobj = compile_data_alloc_insn(iseq);
/* printf("insn_id: %d, line: %d\n", insn_id, nd_line(line_node)); */
iobj->link.type = ISEQ_ELEMENT_INSN;
iobj->link.next = 0;
iobj->insn_id = insn_id;
iobj->insn_info.line_no = nd_line(line_node);
iobj->insn_info.node_id = nd_node_id(line_node);
iobj->insn_info.events = 0;
iobj->operands = argv;
iobj->operand_size = argc;
iobj->sc_state = 0;
iseq_insn_each_markable_object(iobj, iseq_insn_each_object_write_barrier, (VALUE)iseq);
return iobj;
}
static INSN *
new_insn_body(rb_iseq_t *iseq, const NODE *const line_node, enum ruby_vminsn_type insn_id, int argc, ...)
{
VALUE *operands = 0;
va_list argv;
if (argc > 0) {
int i;
va_start(argv, argc);
operands = compile_data_alloc2(iseq, sizeof(VALUE), argc);
for (i = 0; i < argc; i++) {
VALUE v = va_arg(argv, VALUE);
operands[i] = v;
}
va_end(argv);
}
return new_insn_core(iseq, line_node, insn_id, argc, operands);
}
static const struct rb_callinfo *
new_callinfo(rb_iseq_t *iseq, ID mid, int argc, unsigned int flag, struct rb_callinfo_kwarg *kw_arg, int has_blockiseq)
{
VM_ASSERT(argc >= 0);
if (!(flag & (VM_CALL_ARGS_SPLAT | VM_CALL_ARGS_BLOCKARG | VM_CALL_KW_SPLAT)) &&
kw_arg == NULL && !has_blockiseq) {
flag |= VM_CALL_ARGS_SIMPLE;
}
if (kw_arg) {
flag |= VM_CALL_KWARG;
argc += kw_arg->keyword_len;
}
ISEQ_BODY(iseq)->ci_size++;
const struct rb_callinfo *ci = vm_ci_new(mid, flag, argc, kw_arg);
RB_OBJ_WRITTEN(iseq, Qundef, ci);
return ci;
}
static INSN *
new_insn_send(rb_iseq_t *iseq, const NODE *const line_node, ID id, VALUE argc, const rb_iseq_t *blockiseq, VALUE flag, struct rb_callinfo_kwarg *keywords)
{
VALUE *operands = compile_data_calloc2(iseq, sizeof(VALUE), 2);
VALUE ci = (VALUE)new_callinfo(iseq, id, FIX2INT(argc), FIX2INT(flag), keywords, blockiseq != NULL);
operands[0] = ci;
operands[1] = (VALUE)blockiseq;
if (blockiseq) {
RB_OBJ_WRITTEN(iseq, Qundef, blockiseq);
}
INSN *insn = new_insn_core(iseq, line_node, BIN(send), 2, operands);
RB_OBJ_WRITTEN(iseq, Qundef, ci);
RB_GC_GUARD(ci);
return insn;
}
static rb_iseq_t *
new_child_iseq(rb_iseq_t *iseq, const NODE *const node,
VALUE name, const rb_iseq_t *parent, enum rb_iseq_type type, int line_no)
{
rb_iseq_t *ret_iseq;
rb_ast_body_t ast;
ast.root = node;
ast.frozen_string_literal = -1;
ast.coverage_enabled = -1;
ast.script_lines = ISEQ_BODY(iseq)->variable.script_lines;
debugs("[new_child_iseq]> ---------------------------------------\n");
int isolated_depth = ISEQ_COMPILE_DATA(iseq)->isolated_depth;
ret_iseq = rb_iseq_new_with_opt(&ast, name,
rb_iseq_path(iseq), rb_iseq_realpath(iseq),
line_no, parent,
isolated_depth ? isolated_depth + 1 : 0,
type, ISEQ_COMPILE_DATA(iseq)->option);
debugs("[new_child_iseq]< ---------------------------------------\n");
return ret_iseq;
}
static rb_iseq_t *
new_child_iseq_with_callback(rb_iseq_t *iseq, const struct rb_iseq_new_with_callback_callback_func *ifunc,
VALUE name, const rb_iseq_t *parent, enum rb_iseq_type type, int line_no)
{
rb_iseq_t *ret_iseq;
debugs("[new_child_iseq_with_callback]> ---------------------------------------\n");
ret_iseq = rb_iseq_new_with_callback(ifunc, name,
rb_iseq_path(iseq), rb_iseq_realpath(iseq),
line_no, parent, type, ISEQ_COMPILE_DATA(iseq)->option);
debugs("[new_child_iseq_with_callback]< ---------------------------------------\n");
return ret_iseq;
}
static void
set_catch_except_p(rb_iseq_t *iseq)
{
RUBY_ASSERT(ISEQ_COMPILE_DATA(iseq));
ISEQ_COMPILE_DATA(iseq)->catch_except_p = true;
if (ISEQ_BODY(iseq)->parent_iseq != NULL) {
if (ISEQ_COMPILE_DATA(ISEQ_BODY(iseq)->parent_iseq)) {
set_catch_except_p((rb_iseq_t *) ISEQ_BODY(iseq)->parent_iseq);
}
}
}
/* Set body->catch_except_p to true if the ISeq may catch an exception. If it is false,
JIT-ed code may be optimized. If we are extremely conservative, we should set true
if catch table exists. But we want to optimize while loop, which always has catch
table entries for break/next/redo.
So this function sets true for limited ISeqs with break/next/redo catch table entries
whose child ISeq would really raise an exception. */
static void
update_catch_except_flags(rb_iseq_t *iseq, struct rb_iseq_constant_body *body)
{
unsigned int pos;
size_t i;
int insn;
const struct iseq_catch_table *ct = body->catch_table;
/* This assumes that a block has parent_iseq which may catch an exception from the block, and that
BREAK/NEXT/REDO catch table entries are used only when `throw` insn is used in the block. */
pos = 0;
while (pos < body->iseq_size) {
insn = rb_vm_insn_decode(body->iseq_encoded[pos]);
if (insn == BIN(throw)) {
set_catch_except_p(iseq);
break;
}
pos += insn_len(insn);
}
if (ct == NULL)
return;
for (i = 0; i < ct->size; i++) {
const struct iseq_catch_table_entry *entry =
UNALIGNED_MEMBER_PTR(ct, entries[i]);
if (entry->type != CATCH_TYPE_BREAK
&& entry->type != CATCH_TYPE_NEXT
&& entry->type != CATCH_TYPE_REDO) {
RUBY_ASSERT(ISEQ_COMPILE_DATA(iseq));
ISEQ_COMPILE_DATA(iseq)->catch_except_p = true;
break;
}
}
}
static void
iseq_insert_nop_between_end_and_cont(rb_iseq_t *iseq)
{
VALUE catch_table_ary = ISEQ_COMPILE_DATA(iseq)->catch_table_ary;
if (NIL_P(catch_table_ary)) return;
unsigned int i, tlen = (unsigned int)RARRAY_LEN(catch_table_ary);
const VALUE *tptr = RARRAY_CONST_PTR(catch_table_ary);
for (i = 0; i < tlen; i++) {
const VALUE *ptr = RARRAY_CONST_PTR(tptr[i]);
LINK_ELEMENT *end = (LINK_ELEMENT *)(ptr[2] & ~1);
LINK_ELEMENT *cont = (LINK_ELEMENT *)(ptr[4] & ~1);
LINK_ELEMENT *e;
enum rb_catch_type ct = (enum rb_catch_type)(ptr[0] & 0xffff);
if (ct != CATCH_TYPE_BREAK
&& ct != CATCH_TYPE_NEXT
&& ct != CATCH_TYPE_REDO) {
for (e = end; e && (IS_LABEL(e) || IS_TRACE(e)); e = e->next) {
if (e == cont) {
NODE dummy_line_node = generate_dummy_line_node(0, -1);
INSN *nop = new_insn_core(iseq, &dummy_line_node, BIN(nop), 0, 0);
ELEM_INSERT_NEXT(end, &nop->link);
break;
}
}
}
}
}
static int
iseq_setup_insn(rb_iseq_t *iseq, LINK_ANCHOR *const anchor)
{
if (RTEST(ISEQ_COMPILE_DATA(iseq)->err_info))
return COMPILE_NG;
/* debugs("[compile step 2] (iseq_array_to_linkedlist)\n"); */
if (compile_debug > 5)
dump_disasm_list(FIRST_ELEMENT(anchor));
debugs("[compile step 3.1 (iseq_optimize)]\n");
iseq_optimize(iseq, anchor);
if (compile_debug > 5)
dump_disasm_list(FIRST_ELEMENT(anchor));
if (ISEQ_COMPILE_DATA(iseq)->option->instructions_unification) {
debugs("[compile step 3.2 (iseq_insns_unification)]\n");
iseq_insns_unification(iseq, anchor);
if (compile_debug > 5)
dump_disasm_list(FIRST_ELEMENT(anchor));
}
debugs("[compile step 3.4 (iseq_insert_nop_between_end_and_cont)]\n");
iseq_insert_nop_between_end_and_cont(iseq);
if (compile_debug > 5)
dump_disasm_list(FIRST_ELEMENT(anchor));
return COMPILE_OK;
}
static int
iseq_setup(rb_iseq_t *iseq, LINK_ANCHOR *const anchor)
{
if (RTEST(ISEQ_COMPILE_DATA(iseq)->err_info))
return COMPILE_NG;
debugs("[compile step 4.1 (iseq_set_sequence)]\n");
if (!iseq_set_sequence(iseq, anchor)) return COMPILE_NG;
if (compile_debug > 5)
dump_disasm_list(FIRST_ELEMENT(anchor));
debugs("[compile step 4.2 (iseq_set_exception_table)]\n");
if (!iseq_set_exception_table(iseq)) return COMPILE_NG;
debugs("[compile step 4.3 (set_optargs_table)] \n");
if (!iseq_set_optargs_table(iseq)) return COMPILE_NG;
debugs("[compile step 5 (iseq_translate_threaded_code)] \n");
if (!rb_iseq_translate_threaded_code(iseq)) return COMPILE_NG;
debugs("[compile step 6 (update_catch_except_flags)] \n");
RUBY_ASSERT(ISEQ_COMPILE_DATA(iseq));
update_catch_except_flags(iseq, ISEQ_BODY(iseq));
debugs("[compile step 6.1 (remove unused catch tables)] \n");
RUBY_ASSERT(ISEQ_COMPILE_DATA(iseq));
if (!ISEQ_COMPILE_DATA(iseq)->catch_except_p && ISEQ_BODY(iseq)->catch_table) {
xfree(ISEQ_BODY(iseq)->catch_table);
ISEQ_BODY(iseq)->catch_table = NULL;
}
#if VM_INSN_INFO_TABLE_IMPL == 2
if (ISEQ_BODY(iseq)->insns_info.succ_index_table == NULL) {
debugs("[compile step 7 (rb_iseq_insns_info_encode_positions)] \n");
rb_iseq_insns_info_encode_positions(iseq);
}
#endif
if (compile_debug > 1) {
VALUE str = rb_iseq_disasm(iseq);
printf("%s\n", StringValueCStr(str));
}
verify_call_cache(iseq);
debugs("[compile step: finish]\n");
return COMPILE_OK;
}
static int
iseq_set_exception_local_table(rb_iseq_t *iseq)
{
ISEQ_BODY(iseq)->local_table_size = numberof(rb_iseq_shared_exc_local_tbl);
ISEQ_BODY(iseq)->local_table = rb_iseq_shared_exc_local_tbl;
return COMPILE_OK;
}
static int
get_lvar_level(const rb_iseq_t *iseq)
{
int lev = 0;
while (iseq != ISEQ_BODY(iseq)->local_iseq) {
lev++;
iseq = ISEQ_BODY(iseq)->parent_iseq;
}
return lev;
}
static int
get_dyna_var_idx_at_raw(const rb_iseq_t *iseq, ID id)
{
unsigned int i;
for (i = 0; i < ISEQ_BODY(iseq)->local_table_size; i++) {
if (ISEQ_BODY(iseq)->local_table[i] == id) {
return (int)i;
}
}
return -1;
}
static int
get_local_var_idx(const rb_iseq_t *iseq, ID id)
{
int idx = get_dyna_var_idx_at_raw(ISEQ_BODY(iseq)->local_iseq, id);
if (idx < 0) {
COMPILE_ERROR(iseq, ISEQ_LAST_LINE(iseq),
"get_local_var_idx: %d", idx);
}
return idx;
}
static int
get_dyna_var_idx(const rb_iseq_t *iseq, ID id, int *level, int *ls)
{
int lv = 0, idx = -1;
const rb_iseq_t *const topmost_iseq = iseq;
while (iseq) {
idx = get_dyna_var_idx_at_raw(iseq, id);
if (idx >= 0) {
break;
}
iseq = ISEQ_BODY(iseq)->parent_iseq;
lv++;
}
if (idx < 0) {
COMPILE_ERROR(topmost_iseq, ISEQ_LAST_LINE(topmost_iseq),
"get_dyna_var_idx: -1");
}
*level = lv;
*ls = ISEQ_BODY(iseq)->local_table_size;
return idx;
}
static int
iseq_local_block_param_p(const rb_iseq_t *iseq, unsigned int idx, unsigned int level)
{
const struct rb_iseq_constant_body *body;
while (level > 0) {
iseq = ISEQ_BODY(iseq)->parent_iseq;
level--;
}
body = ISEQ_BODY(iseq);
if (body->local_iseq == iseq && /* local variables */
body->param.flags.has_block &&
body->local_table_size - body->param.block_start == idx) {
return TRUE;
}
else {
return FALSE;
}
}
static int
iseq_block_param_id_p(const rb_iseq_t *iseq, ID id, int *pidx, int *plevel)
{
int level, ls;
int idx = get_dyna_var_idx(iseq, id, &level, &ls);
if (iseq_local_block_param_p(iseq, ls - idx, level)) {
*pidx = ls - idx;
*plevel = level;
return TRUE;
}
else {
return FALSE;
}
}
static void
access_outer_variables(const rb_iseq_t *iseq, int level, ID id, bool write)
{
int isolated_depth = ISEQ_COMPILE_DATA(iseq)->isolated_depth;
if (isolated_depth && level >= isolated_depth) {
if (id == rb_intern("yield")) {
COMPILE_ERROR(iseq, ISEQ_LAST_LINE(iseq), "can not yield from isolated Proc");
}
else {
COMPILE_ERROR(iseq, ISEQ_LAST_LINE(iseq), "can not access variable `%s' from isolated Proc", rb_id2name(id));
}
}
for (int i=0; i<level; i++) {
VALUE val;
struct rb_id_table *ovs = ISEQ_BODY(iseq)->outer_variables;
if (!ovs) {
ovs = ISEQ_BODY(iseq)->outer_variables = rb_id_table_create(8);
}
if (rb_id_table_lookup(ISEQ_BODY(iseq)->outer_variables, id, &val)) {
if (write && !val) {
rb_id_table_insert(ISEQ_BODY(iseq)->outer_variables, id, Qtrue);
}
}
else {
rb_id_table_insert(ISEQ_BODY(iseq)->outer_variables, id, RBOOL(write));
}
iseq = ISEQ_BODY(iseq)->parent_iseq;
}
}
static ID
iseq_lvar_id(const rb_iseq_t *iseq, int idx, int level)
{
for (int i=0; i<level; i++) {
iseq = ISEQ_BODY(iseq)->parent_iseq;
}
ID id = ISEQ_BODY(iseq)->local_table[ISEQ_BODY(iseq)->local_table_size - idx];
// fprintf(stderr, "idx:%d level:%d ID:%s\n", idx, level, rb_id2name(id));
return id;
}
static void
iseq_add_getlocal(rb_iseq_t *iseq, LINK_ANCHOR *const seq, const NODE *const line_node, int idx, int level)
{
if (iseq_local_block_param_p(iseq, idx, level)) {
ADD_INSN2(seq, line_node, getblockparam, INT2FIX((idx) + VM_ENV_DATA_SIZE - 1), INT2FIX(level));
}
else {
ADD_INSN2(seq, line_node, getlocal, INT2FIX((idx) + VM_ENV_DATA_SIZE - 1), INT2FIX(level));
}
if (level > 0) access_outer_variables(iseq, level, iseq_lvar_id(iseq, idx, level), Qfalse);
}
static void
iseq_add_setlocal(rb_iseq_t *iseq, LINK_ANCHOR *const seq, const NODE *const line_node, int idx, int level)
{
if (iseq_local_block_param_p(iseq, idx, level)) {
ADD_INSN2(seq, line_node, setblockparam, INT2FIX((idx) + VM_ENV_DATA_SIZE - 1), INT2FIX(level));
}
else {
ADD_INSN2(seq, line_node, setlocal, INT2FIX((idx) + VM_ENV_DATA_SIZE - 1), INT2FIX(level));
}
if (level > 0) access_outer_variables(iseq, level, iseq_lvar_id(iseq, idx, level), Qtrue);
}
static void
iseq_calc_param_size(rb_iseq_t *iseq)
{
struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
if (body->param.flags.has_opt ||
body->param.flags.has_post ||
body->param.flags.has_rest ||
body->param.flags.has_block ||
body->param.flags.has_kw ||
body->param.flags.has_kwrest) {
if (body->param.flags.has_block) {
body->param.size = body->param.block_start + 1;
}
else if (body->param.flags.has_kwrest) {
body->param.size = body->param.keyword->rest_start + 1;
}
else if (body->param.flags.has_kw) {
body->param.size = body->param.keyword->bits_start + 1;
}
else if (body->param.flags.has_post) {
body->param.size = body->param.post_start + body->param.post_num;
}
else if (body->param.flags.has_rest) {
body->param.size = body->param.rest_start + 1;
}
else if (body->param.flags.has_opt) {
body->param.size = body->param.lead_num + body->param.opt_num;
}
else {
UNREACHABLE;
}
}
else {
body->param.size = body->param.lead_num;
}
}
static int
iseq_set_arguments_keywords(rb_iseq_t *iseq, LINK_ANCHOR *const optargs,
const struct rb_args_info *args, int arg_size)
{
const rb_node_kw_arg_t *node = args->kw_args;
struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
struct rb_iseq_param_keyword *keyword;
const VALUE default_values = rb_ary_hidden_new(1);
const VALUE complex_mark = rb_str_tmp_new(0);
int kw = 0, rkw = 0, di = 0, i;
body->param.flags.has_kw = TRUE;
body->param.keyword = keyword = ZALLOC_N(struct rb_iseq_param_keyword, 1);
while (node) {
kw++;
node = node->nd_next;
}
arg_size += kw;
keyword->bits_start = arg_size++;
node = args->kw_args;
while (node) {
const NODE *val_node = get_nd_value(node->nd_body);
VALUE dv;
if (val_node == NODE_SPECIAL_REQUIRED_KEYWORD) {
++rkw;
}
else {
switch (nd_type(val_node)) {
case NODE_LIT:
dv = RNODE_LIT(val_node)->nd_lit;
break;
case NODE_NIL:
dv = Qnil;
break;
case NODE_TRUE:
dv = Qtrue;
break;
case NODE_FALSE:
dv = Qfalse;
break;
default:
NO_CHECK(COMPILE_POPPED(optargs, "kwarg", RNODE(node))); /* nd_type_p(node, NODE_KW_ARG) */
dv = complex_mark;
}
keyword->num = ++di;
rb_ary_push(default_values, dv);
}
node = node->nd_next;
}
keyword->num = kw;
if (RNODE_DVAR(args->kw_rest_arg)->nd_vid != 0) {
keyword->rest_start = arg_size++;
body->param.flags.has_kwrest = TRUE;
}
keyword->required_num = rkw;
keyword->table = &body->local_table[keyword->bits_start - keyword->num];
{
VALUE *dvs = ALLOC_N(VALUE, RARRAY_LEN(default_values));
for (i = 0; i < RARRAY_LEN(default_values); i++) {
VALUE dv = RARRAY_AREF(default_values, i);
if (dv == complex_mark) dv = Qundef;
if (!SPECIAL_CONST_P(dv)) {
RB_OBJ_WRITTEN(iseq, Qundef, dv);
}
dvs[i] = dv;
}
keyword->default_values = dvs;
}
return arg_size;
}
static int
iseq_set_arguments(rb_iseq_t *iseq, LINK_ANCHOR *const optargs, const NODE *const node_args)
{
debugs("iseq_set_arguments: %s\n", node_args ? "" : "0");
if (node_args) {
struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
struct rb_args_info *args = &RNODE_ARGS(node_args)->nd_ainfo;
ID rest_id = 0;
int last_comma = 0;
ID block_id = 0;
int arg_size;
EXPECT_NODE("iseq_set_arguments", node_args, NODE_ARGS, COMPILE_NG);
body->param.flags.ruby2_keywords = args->ruby2_keywords;
body->param.lead_num = arg_size = (int)args->pre_args_num;
if (body->param.lead_num > 0) body->param.flags.has_lead = TRUE;
debugs(" - argc: %d\n", body->param.lead_num);
rest_id = args->rest_arg;
if (rest_id == NODE_SPECIAL_EXCESSIVE_COMMA) {
last_comma = 1;
rest_id = 0;
}
block_id = args->block_arg;
if (args->opt_args) {
const rb_node_opt_arg_t *node = args->opt_args;
LABEL *label;
VALUE labels = rb_ary_hidden_new(1);
VALUE *opt_table;
int i = 0, j;
while (node) {
label = NEW_LABEL(nd_line(RNODE(node)));
rb_ary_push(labels, (VALUE)label | 1);
ADD_LABEL(optargs, label);
NO_CHECK(COMPILE_POPPED(optargs, "optarg", node->nd_body));
node = node->nd_next;
i += 1;
}
/* last label */
label = NEW_LABEL(nd_line(node_args));
rb_ary_push(labels, (VALUE)label | 1);
ADD_LABEL(optargs, label);
opt_table = ALLOC_N(VALUE, i+1);
MEMCPY(opt_table, RARRAY_CONST_PTR(labels), VALUE, i+1);
for (j = 0; j < i+1; j++) {
opt_table[j] &= ~1;
}
rb_ary_clear(labels);
body->param.flags.has_opt = TRUE;
body->param.opt_num = i;
body->param.opt_table = opt_table;
arg_size += i;
}
if (rest_id) {
body->param.rest_start = arg_size++;
body->param.flags.has_rest = TRUE;
assert(body->param.rest_start != -1);
}
if (args->first_post_arg) {
body->param.post_start = arg_size;
body->param.post_num = args->post_args_num;
body->param.flags.has_post = TRUE;
arg_size += args->post_args_num;
if (body->param.flags.has_rest) { /* TODO: why that? */
body->param.post_start = body->param.rest_start + 1;
}
}
if (args->kw_args) {
arg_size = iseq_set_arguments_keywords(iseq, optargs, args, arg_size);
}
else if (args->kw_rest_arg) {
struct rb_iseq_param_keyword *keyword = ZALLOC_N(struct rb_iseq_param_keyword, 1);
keyword->rest_start = arg_size++;
body->param.keyword = keyword;
body->param.flags.has_kwrest = TRUE;
}
else if (args->no_kwarg) {
body->param.flags.accepts_no_kwarg = TRUE;
}
if (block_id) {
body->param.block_start = arg_size++;
body->param.flags.has_block = TRUE;
}
iseq_calc_param_size(iseq);
body->param.size = arg_size;
if (args->pre_init) { /* m_init */
NO_CHECK(COMPILE_POPPED(optargs, "init arguments (m)", args->pre_init));
}
if (args->post_init) { /* p_init */
NO_CHECK(COMPILE_POPPED(optargs, "init arguments (p)", args->post_init));
}
if (body->type == ISEQ_TYPE_BLOCK) {
if (body->param.flags.has_opt == FALSE &&
body->param.flags.has_post == FALSE &&
body->param.flags.has_rest == FALSE &&
body->param.flags.has_kw == FALSE &&
body->param.flags.has_kwrest == FALSE) {
if (body->param.lead_num == 1 && last_comma == 0) {
/* {|a|} */
body->param.flags.ambiguous_param0 = TRUE;
}
}
}
}
return COMPILE_OK;
}
static int
iseq_set_local_table(rb_iseq_t *iseq, const rb_ast_id_table_t *tbl)
{
unsigned int size = tbl ? tbl->size : 0;
if (size > 0) {
ID *ids = (ID *)ALLOC_N(ID, size);
MEMCPY(ids, tbl->ids, ID, size);
ISEQ_BODY(iseq)->local_table = ids;
}
ISEQ_BODY(iseq)->local_table_size = size;
debugs("iseq_set_local_table: %u\n", ISEQ_BODY(iseq)->local_table_size);
return COMPILE_OK;
}
int
rb_iseq_cdhash_cmp(VALUE val, VALUE lit)
{
int tval, tlit;
if (val == lit) {
return 0;
}
else if ((tlit = OBJ_BUILTIN_TYPE(lit)) == -1) {
return val != lit;
}
else if ((tval = OBJ_BUILTIN_TYPE(val)) == -1) {
return -1;
}
else if (tlit != tval) {
return -1;
}
else if (tlit == T_SYMBOL) {
return val != lit;
}
else if (tlit == T_STRING) {
return rb_str_hash_cmp(lit, val);
}
else if (tlit == T_BIGNUM) {
long x = FIX2LONG(rb_big_cmp(lit, val));
/* Given lit and val are both Bignum, x must be -1, 0, 1.
* There is no need to call rb_fix2int here. */
RUBY_ASSERT((x == 1) || (x == 0) || (x == -1));
return (int)x;
}
else if (tlit == T_FLOAT) {
return rb_float_cmp(lit, val);
}
else if (tlit == T_RATIONAL) {
const struct RRational *rat1 = RRATIONAL(val);
const struct RRational *rat2 = RRATIONAL(lit);
return rb_iseq_cdhash_cmp(rat1->num, rat2->num) || rb_iseq_cdhash_cmp(rat1->den, rat2->den);
}
else if (tlit == T_COMPLEX) {
const struct RComplex *comp1 = RCOMPLEX(val);
const struct RComplex *comp2 = RCOMPLEX(lit);
return rb_iseq_cdhash_cmp(comp1->real, comp2->real) || rb_iseq_cdhash_cmp(comp1->imag, comp2->imag);
}
else if (tlit == T_REGEXP) {
return rb_reg_equal(val, lit) ? 0 : -1;
}
else {
UNREACHABLE_RETURN(-1);
}
}
st_index_t
rb_iseq_cdhash_hash(VALUE a)
{
switch (OBJ_BUILTIN_TYPE(a)) {
case -1:
case T_SYMBOL:
return (st_index_t)a;
case T_STRING:
return rb_str_hash(a);
case T_BIGNUM:
return FIX2LONG(rb_big_hash(a));
case T_FLOAT:
return rb_dbl_long_hash(RFLOAT_VALUE(a));
case T_RATIONAL:
return rb_rational_hash(a);
case T_COMPLEX:
return rb_complex_hash(a);
case T_REGEXP:
return NUM2LONG(rb_reg_hash(a));
default:
UNREACHABLE_RETURN(0);
}
}
static const struct st_hash_type cdhash_type = {
rb_iseq_cdhash_cmp,
rb_iseq_cdhash_hash,
};
struct cdhash_set_label_struct {
VALUE hash;
int pos;
int len;
};
static int
cdhash_set_label_i(VALUE key, VALUE val, VALUE ptr)
{
struct cdhash_set_label_struct *data = (struct cdhash_set_label_struct *)ptr;
LABEL *lobj = (LABEL *)(val & ~1);
rb_hash_aset(data->hash, key, INT2FIX(lobj->position - (data->pos+data->len)));
return ST_CONTINUE;
}
static inline VALUE
get_ivar_ic_value(rb_iseq_t *iseq,ID id)
{
return INT2FIX(ISEQ_BODY(iseq)->ivc_size++);
}
static inline VALUE
get_cvar_ic_value(rb_iseq_t *iseq,ID id)
{
VALUE val;
struct rb_id_table *tbl = ISEQ_COMPILE_DATA(iseq)->ivar_cache_table;
if (tbl) {
if (rb_id_table_lookup(tbl,id,&val)) {
return val;
}
}
else {
tbl = rb_id_table_create(1);
ISEQ_COMPILE_DATA(iseq)->ivar_cache_table = tbl;
}
val = INT2FIX(ISEQ_BODY(iseq)->icvarc_size++);
rb_id_table_insert(tbl,id,val);
return val;
}
#define BADINSN_DUMP(anchor, list, dest) \
dump_disasm_list_with_cursor(FIRST_ELEMENT(anchor), list, dest)
#define BADINSN_ERROR \
(xfree(generated_iseq), \
xfree(insns_info), \
BADINSN_DUMP(anchor, list, NULL), \
COMPILE_ERROR)
static int
fix_sp_depth(rb_iseq_t *iseq, LINK_ANCHOR *const anchor)
{
int stack_max = 0, sp = 0, line = 0;
LINK_ELEMENT *list;
for (list = FIRST_ELEMENT(anchor); list; list = list->next) {
if (IS_LABEL(list)) {
LABEL *lobj = (LABEL *)list;
lobj->set = TRUE;
}
}
for (list = FIRST_ELEMENT(anchor); list; list = list->next) {
switch (list->type) {
case ISEQ_ELEMENT_INSN:
{
int j, len, insn;
const char *types;
VALUE *operands;
INSN *iobj = (INSN *)list;
/* update sp */
sp = calc_sp_depth(sp, iobj);
if (sp < 0) {
BADINSN_DUMP(anchor, list, NULL);
COMPILE_ERROR(iseq, iobj->insn_info.line_no,
"argument stack underflow (%d)", sp);
return -1;
}
if (sp > stack_max) {
stack_max = sp;
}
line = iobj->insn_info.line_no;
/* fprintf(stderr, "insn: %-16s, sp: %d\n", insn_name(iobj->insn_id), sp); */
operands = iobj->operands;
insn = iobj->insn_id;
types = insn_op_types(insn);
len = insn_len(insn);
/* operand check */
if (iobj->operand_size != len - 1) {
/* printf("operand size miss! (%d, %d)\n", iobj->operand_size, len); */
BADINSN_DUMP(anchor, list, NULL);
COMPILE_ERROR(iseq, iobj->insn_info.line_no,
"operand size miss! (%d for %d)",
iobj->operand_size, len - 1);
return -1;
}
for (j = 0; types[j]; j++) {
if (types[j] == TS_OFFSET) {
/* label(destination position) */
LABEL *lobj = (LABEL *)operands[j];
if (!lobj->set) {
BADINSN_DUMP(anchor, list, NULL);
COMPILE_ERROR(iseq, iobj->insn_info.line_no,
"unknown label: "LABEL_FORMAT, lobj->label_no);
return -1;
}
if (lobj->sp == -1) {
lobj->sp = sp;
}
else if (lobj->sp != sp) {
debugs("%s:%d: sp inconsistency found but ignored (" LABEL_FORMAT " sp: %d, calculated sp: %d)\n",
RSTRING_PTR(rb_iseq_path(iseq)), line,
lobj->label_no, lobj->sp, sp);
}
}
}
break;
}
case ISEQ_ELEMENT_LABEL:
{
LABEL *lobj = (LABEL *)list;
if (lobj->sp == -1) {
lobj->sp = sp;
}
else {
if (lobj->sp != sp) {
debugs("%s:%d: sp inconsistency found but ignored (" LABEL_FORMAT " sp: %d, calculated sp: %d)\n",
RSTRING_PTR(rb_iseq_path(iseq)), line,
lobj->label_no, lobj->sp, sp);
}
sp = lobj->sp;
}
break;
}
case ISEQ_ELEMENT_TRACE:
{
/* ignore */
break;
}
case ISEQ_ELEMENT_ADJUST:
{
ADJUST *adjust = (ADJUST *)list;
int orig_sp = sp;
sp = adjust->label ? adjust->label->sp : 0;
if (adjust->line_no != -1 && orig_sp - sp < 0) {
BADINSN_DUMP(anchor, list, NULL);
COMPILE_ERROR(iseq, adjust->line_no,
"iseq_set_sequence: adjust bug %d < %d",
orig_sp, sp);
return -1;
}
break;
}
default:
BADINSN_DUMP(anchor, list, NULL);
COMPILE_ERROR(iseq, line, "unknown list type: %d", list->type);
return -1;
}
}
return stack_max;
}
static int
add_insn_info(struct iseq_insn_info_entry *insns_info, unsigned int *positions,
int insns_info_index, int code_index, const INSN *iobj)
{
if (insns_info_index == 0 ||
insns_info[insns_info_index-1].line_no != iobj->insn_info.line_no ||
#ifdef USE_ISEQ_NODE_ID
insns_info[insns_info_index-1].node_id != iobj->insn_info.node_id ||
#endif
insns_info[insns_info_index-1].events != iobj->insn_info.events) {
insns_info[insns_info_index].line_no = iobj->insn_info.line_no;
#ifdef USE_ISEQ_NODE_ID
insns_info[insns_info_index].node_id = iobj->insn_info.node_id;
#endif
insns_info[insns_info_index].events = iobj->insn_info.events;
positions[insns_info_index] = code_index;
return TRUE;
}
return FALSE;
}
static int
add_adjust_info(struct iseq_insn_info_entry *insns_info, unsigned int *positions,
int insns_info_index, int code_index, const ADJUST *adjust)
{
insns_info[insns_info_index].line_no = adjust->line_no;
insns_info[insns_info_index].events = 0;
positions[insns_info_index] = code_index;
return TRUE;
}
static ID *
array_to_idlist(VALUE arr)
{
RUBY_ASSERT(RB_TYPE_P(arr, T_ARRAY));
long size = RARRAY_LEN(arr);
ID *ids = (ID *)ALLOC_N(ID, size + 1);
for (int i = 0; i < size; i++) {
VALUE sym = RARRAY_AREF(arr, i);
ids[i] = SYM2ID(sym);
}
ids[size] = 0;
return ids;
}
static VALUE
idlist_to_array(const ID *ids)
{
VALUE arr = rb_ary_new();
while (*ids) {
rb_ary_push(arr, ID2SYM(*ids++));
}
return arr;
}
/**
ruby insn object list -> raw instruction sequence
*/
static int
iseq_set_sequence(rb_iseq_t *iseq, LINK_ANCHOR *const anchor)
{
struct iseq_insn_info_entry *insns_info;
struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
unsigned int *positions;
LINK_ELEMENT *list;
VALUE *generated_iseq;
rb_event_flag_t events = 0;
long data = 0;
int insn_num, code_index, insns_info_index, sp = 0;
int stack_max = fix_sp_depth(iseq, anchor);
if (stack_max < 0) return COMPILE_NG;
/* fix label position */
insn_num = code_index = 0;
for (list = FIRST_ELEMENT(anchor); list; list = list->next) {
switch (list->type) {
case ISEQ_ELEMENT_INSN:
{
INSN *iobj = (INSN *)list;
/* update sp */
sp = calc_sp_depth(sp, iobj);
insn_num++;
events = iobj->insn_info.events |= events;
if (ISEQ_COVERAGE(iseq)) {
if (ISEQ_LINE_COVERAGE(iseq) && (events & RUBY_EVENT_COVERAGE_LINE) &&
!(rb_get_coverage_mode() & COVERAGE_TARGET_ONESHOT_LINES)) {
int line = iobj->insn_info.line_no - 1;
if (line >= 0 && line < RARRAY_LEN(ISEQ_LINE_COVERAGE(iseq))) {
RARRAY_ASET(ISEQ_LINE_COVERAGE(iseq), line, INT2FIX(0));
}
}
if (ISEQ_BRANCH_COVERAGE(iseq) && (events & RUBY_EVENT_COVERAGE_BRANCH)) {
while (RARRAY_LEN(ISEQ_PC2BRANCHINDEX(iseq)) <= code_index) {
rb_ary_push(ISEQ_PC2BRANCHINDEX(iseq), Qnil);
}
RARRAY_ASET(ISEQ_PC2BRANCHINDEX(iseq), code_index, INT2FIX(data));
}
}
code_index += insn_data_length(iobj);
events = 0;
data = 0;
break;
}
case ISEQ_ELEMENT_LABEL:
{
LABEL *lobj = (LABEL *)list;
lobj->position = code_index;
if (lobj->sp != sp) {
debugs("%s: sp inconsistency found but ignored (" LABEL_FORMAT " sp: %d, calculated sp: %d)\n",
RSTRING_PTR(rb_iseq_path(iseq)),
lobj->label_no, lobj->sp, sp);
}
sp = lobj->sp;
break;
}
case ISEQ_ELEMENT_TRACE:
{
TRACE *trace = (TRACE *)list;
events |= trace->event;
if (trace->event & RUBY_EVENT_COVERAGE_BRANCH) data = trace->data;
break;
}
case ISEQ_ELEMENT_ADJUST:
{
ADJUST *adjust = (ADJUST *)list;
if (adjust->line_no != -1) {
int orig_sp = sp;
sp = adjust->label ? adjust->label->sp : 0;
if (orig_sp - sp > 0) {
if (orig_sp - sp > 1) code_index++; /* 1 operand */
code_index++; /* insn */
insn_num++;
}
}
break;
}
default: break;
}
}
/* make instruction sequence */
generated_iseq = ALLOC_N(VALUE, code_index);
insns_info = ALLOC_N(struct iseq_insn_info_entry, insn_num);
positions = ALLOC_N(unsigned int, insn_num);
if (ISEQ_IS_SIZE(body)) {
body->is_entries = ZALLOC_N(union iseq_inline_storage_entry, ISEQ_IS_SIZE(body));
}
else {
body->is_entries = NULL;
}
body->call_data = ZALLOC_N(struct rb_call_data, body->ci_size);
ISEQ_COMPILE_DATA(iseq)->ci_index = 0;
// Calculate the bitmask buffer size.
// Round the generated_iseq size up to the nearest multiple
// of the number of bits in an unsigned long.
// Allocate enough room for the bitmask list
iseq_bits_t * mark_offset_bits;
int code_size = code_index;
iseq_bits_t tmp[1] = {0};
bool needs_bitmap = false;
if (ISEQ_MBITS_BUFLEN(code_index) == 1) {
mark_offset_bits = tmp;
}
else {
mark_offset_bits = ZALLOC_N(iseq_bits_t, ISEQ_MBITS_BUFLEN(code_index));
}
list = FIRST_ELEMENT(anchor);
insns_info_index = code_index = sp = 0;
while (list) {
switch (list->type) {
case ISEQ_ELEMENT_INSN:
{
int j, len, insn;
const char *types;
VALUE *operands;
INSN *iobj = (INSN *)list;
/* update sp */
sp = calc_sp_depth(sp, iobj);
/* fprintf(stderr, "insn: %-16s, sp: %d\n", insn_name(iobj->insn_id), sp); */
operands = iobj->operands;
insn = iobj->insn_id;
generated_iseq[code_index] = insn;
types = insn_op_types(insn);
len = insn_len(insn);
for (j = 0; types[j]; j++) {
char type = types[j];
/* printf("--> [%c - (%d-%d)]\n", type, k, j); */
switch (type) {
case TS_OFFSET:
{
/* label(destination position) */
LABEL *lobj = (LABEL *)operands[j];
generated_iseq[code_index + 1 + j] = lobj->position - (code_index + len);
break;
}
case TS_CDHASH:
{
VALUE map = operands[j];
struct cdhash_set_label_struct data;
data.hash = map;
data.pos = code_index;
data.len = len;
rb_hash_foreach(map, cdhash_set_label_i, (VALUE)&data);
rb_hash_rehash(map);
freeze_hide_obj(map);
generated_iseq[code_index + 1 + j] = map;
ISEQ_MBITS_SET(mark_offset_bits, code_index + 1 + j);
RB_OBJ_WRITTEN(iseq, Qundef, map);
needs_bitmap = true;
break;
}
case TS_LINDEX:
case TS_NUM: /* ulong */
generated_iseq[code_index + 1 + j] = FIX2INT(operands[j]);
break;
case TS_ISEQ: /* iseq */
case TS_VALUE: /* VALUE */
{
VALUE v = operands[j];
generated_iseq[code_index + 1 + j] = v;
/* to mark ruby object */
if (!SPECIAL_CONST_P(v)) {
RB_OBJ_WRITTEN(iseq, Qundef, v);
ISEQ_MBITS_SET(mark_offset_bits, code_index + 1 + j);
needs_bitmap = true;
}
break;
}
/* [ TS_IVC | TS_ICVARC | TS_ISE | TS_IC ] */
case TS_IC: /* inline cache: constants */
{
unsigned int ic_index = ISEQ_COMPILE_DATA(iseq)->ic_index++;
IC ic = &ISEQ_IS_ENTRY_START(body, type)[ic_index].ic_cache;
if (UNLIKELY(ic_index >= body->ic_size)) {
BADINSN_DUMP(anchor, &iobj->link, 0);
COMPILE_ERROR(iseq, iobj->insn_info.line_no,
"iseq_set_sequence: ic_index overflow: index: %d, size: %d",
ic_index, ISEQ_IS_SIZE(body));
}
ic->segments = array_to_idlist(operands[j]);
generated_iseq[code_index + 1 + j] = (VALUE)ic;
}
break;
case TS_IVC: /* inline ivar cache */
{
unsigned int ic_index = FIX2UINT(operands[j]);
IVC cache = ((IVC)&body->is_entries[ic_index]);
if (insn == BIN(setinstancevariable)) {
cache->iv_set_name = SYM2ID(operands[j - 1]);
}
else {
cache->iv_set_name = 0;
}
vm_ic_attr_index_initialize(cache, INVALID_SHAPE_ID);
}
case TS_ISE: /* inline storage entry: `once` insn */
case TS_ICVARC: /* inline cvar cache */
{
unsigned int ic_index = FIX2UINT(operands[j]);
IC ic = &ISEQ_IS_ENTRY_START(body, type)[ic_index].ic_cache;
if (UNLIKELY(ic_index >= ISEQ_IS_SIZE(body))) {
BADINSN_DUMP(anchor, &iobj->link, 0);
COMPILE_ERROR(iseq, iobj->insn_info.line_no,
"iseq_set_sequence: ic_index overflow: index: %d, size: %d",
ic_index, ISEQ_IS_SIZE(body));
}
generated_iseq[code_index + 1 + j] = (VALUE)ic;
break;
}
case TS_CALLDATA:
{
const struct rb_callinfo *source_ci = (const struct rb_callinfo *)operands[j];
assert(ISEQ_COMPILE_DATA(iseq)->ci_index <= body->ci_size);
struct rb_call_data *cd = &body->call_data[ISEQ_COMPILE_DATA(iseq)->ci_index++];
cd->ci = source_ci;
cd->cc = vm_cc_empty();
generated_iseq[code_index + 1 + j] = (VALUE)cd;
break;
}
case TS_ID: /* ID */
generated_iseq[code_index + 1 + j] = SYM2ID(operands[j]);
break;
case TS_FUNCPTR:
generated_iseq[code_index + 1 + j] = operands[j];
break;
case TS_BUILTIN:
generated_iseq[code_index + 1 + j] = operands[j];
break;
default:
BADINSN_ERROR(iseq, iobj->insn_info.line_no,
"unknown operand type: %c", type);
return COMPILE_NG;
}
}
if (add_insn_info(insns_info, positions, insns_info_index, code_index, iobj)) insns_info_index++;
code_index += len;
break;
}
case ISEQ_ELEMENT_LABEL:
{
LABEL *lobj = (LABEL *)list;
if (lobj->sp != sp) {
debugs("%s: sp inconsistency found but ignored (" LABEL_FORMAT " sp: %d, calculated sp: %d)\n",
RSTRING_PTR(rb_iseq_path(iseq)),
lobj->label_no, lobj->sp, sp);
}
sp = lobj->sp;
break;
}
case ISEQ_ELEMENT_ADJUST:
{
ADJUST *adjust = (ADJUST *)list;
int orig_sp = sp;
if (adjust->label) {
sp = adjust->label->sp;
}
else {
sp = 0;
}
if (adjust->line_no != -1) {
const int diff = orig_sp - sp;
if (diff > 0) {
if (insns_info_index == 0) {
COMPILE_ERROR(iseq, adjust->line_no,
"iseq_set_sequence: adjust bug (ISEQ_ELEMENT_ADJUST must not be the first in iseq)");
}
if (add_adjust_info(insns_info, positions, insns_info_index, code_index, adjust)) insns_info_index++;
}
if (diff > 1) {
generated_iseq[code_index++] = BIN(adjuststack);
generated_iseq[code_index++] = orig_sp - sp;
}
else if (diff == 1) {
generated_iseq[code_index++] = BIN(pop);
}
else if (diff < 0) {
int label_no = adjust->label ? adjust->label->label_no : -1;
xfree(generated_iseq);
xfree(insns_info);
xfree(positions);
if (ISEQ_MBITS_BUFLEN(code_size) > 1) {
xfree(mark_offset_bits);
}
debug_list(anchor, list);
COMPILE_ERROR(iseq, adjust->line_no,
"iseq_set_sequence: adjust bug to %d %d < %d",
label_no, orig_sp, sp);
return COMPILE_NG;
}
}
break;
}
default:
/* ignore */
break;
}
list = list->next;
}
body->iseq_encoded = (void *)generated_iseq;
body->iseq_size = code_index;
body->stack_max = stack_max;
if (ISEQ_MBITS_BUFLEN(body->iseq_size) == 1) {
body->mark_bits.single = mark_offset_bits[0];
}
else {
if (needs_bitmap) {
body->mark_bits.list = mark_offset_bits;
}
else {
body->mark_bits.list = 0;
ruby_xfree(mark_offset_bits);
}
}
/* get rid of memory leak when REALLOC failed */
body->insns_info.body = insns_info;
body->insns_info.positions = positions;
REALLOC_N(insns_info, struct iseq_insn_info_entry, insns_info_index);
body->insns_info.body = insns_info;
REALLOC_N(positions, unsigned int, insns_info_index);
body->insns_info.positions = positions;
body->insns_info.size = insns_info_index;
return COMPILE_OK;
}
static int
label_get_position(LABEL *lobj)
{
return lobj->position;
}
static int
label_get_sp(LABEL *lobj)
{
return lobj->sp;
}
static int
iseq_set_exception_table(rb_iseq_t *iseq)
{
const VALUE *tptr, *ptr;
unsigned int tlen, i;
struct iseq_catch_table_entry *entry;
ISEQ_BODY(iseq)->catch_table = NULL;
VALUE catch_table_ary = ISEQ_COMPILE_DATA(iseq)->catch_table_ary;
if (NIL_P(catch_table_ary)) return COMPILE_OK;
tlen = (int)RARRAY_LEN(catch_table_ary);
tptr = RARRAY_CONST_PTR(catch_table_ary);
if (tlen > 0) {
struct iseq_catch_table *table = xmalloc(iseq_catch_table_bytes(tlen));
table->size = tlen;
for (i = 0; i < table->size; i++) {
ptr = RARRAY_CONST_PTR(tptr[i]);
entry = UNALIGNED_MEMBER_PTR(table, entries[i]);
entry->type = (enum rb_catch_type)(ptr[0] & 0xffff);
entry->start = label_get_position((LABEL *)(ptr[1] & ~1));
entry->end = label_get_position((LABEL *)(ptr[2] & ~1));
entry->iseq = (rb_iseq_t *)ptr[3];
RB_OBJ_WRITTEN(iseq, Qundef, entry->iseq);
/* stack depth */
if (ptr[4]) {
LABEL *lobj = (LABEL *)(ptr[4] & ~1);
entry->cont = label_get_position(lobj);
entry->sp = label_get_sp(lobj);
/* TODO: Dirty Hack! Fix me */
if (entry->type == CATCH_TYPE_RESCUE ||
entry->type == CATCH_TYPE_BREAK ||
entry->type == CATCH_TYPE_NEXT) {
entry->sp--;
}
}
else {
entry->cont = 0;
}
}
ISEQ_BODY(iseq)->catch_table = table;
RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->catch_table_ary, 0); /* free */
}
RB_GC_GUARD(catch_table_ary);
return COMPILE_OK;
}
/*
* set optional argument table
* def foo(a, b=expr1, c=expr2)
* =>
* b:
* expr1
* c:
* expr2
*/
static int
iseq_set_optargs_table(rb_iseq_t *iseq)
{
int i;
VALUE *opt_table = (VALUE *)ISEQ_BODY(iseq)->param.opt_table;
if (ISEQ_BODY(iseq)->param.flags.has_opt) {
for (i = 0; i < ISEQ_BODY(iseq)->param.opt_num + 1; i++) {
opt_table[i] = label_get_position((LABEL *)opt_table[i]);
}
}
return COMPILE_OK;
}
static LINK_ELEMENT *
get_destination_insn(INSN *iobj)
{
LABEL *lobj = (LABEL *)OPERAND_AT(iobj, 0);
LINK_ELEMENT *list;
rb_event_flag_t events = 0;
list = lobj->link.next;
while (list) {
switch (list->type) {
case ISEQ_ELEMENT_INSN:
case ISEQ_ELEMENT_ADJUST:
goto found;
case ISEQ_ELEMENT_LABEL:
/* ignore */
break;
case ISEQ_ELEMENT_TRACE:
{
TRACE *trace = (TRACE *)list;
events |= trace->event;
}
break;
default: break;
}
list = list->next;
}
found:
if (list && IS_INSN(list)) {
INSN *iobj = (INSN *)list;
iobj->insn_info.events |= events;
}
return list;
}
static LINK_ELEMENT *
get_next_insn(INSN *iobj)
{
LINK_ELEMENT *list = iobj->link.next;
while (list) {
if (IS_INSN(list) || IS_ADJUST(list)) {
return list;
}
list = list->next;
}
return 0;
}
static LINK_ELEMENT *
get_prev_insn(INSN *iobj)
{
LINK_ELEMENT *list = iobj->link.prev;
while (list) {
if (IS_INSN(list) || IS_ADJUST(list)) {
return list;
}
list = list->prev;
}
return 0;
}
static void
unref_destination(INSN *iobj, int pos)
{
LABEL *lobj = (LABEL *)OPERAND_AT(iobj, pos);
--lobj->refcnt;
if (!lobj->refcnt) ELEM_REMOVE(&lobj->link);
}
static void
replace_destination(INSN *dobj, INSN *nobj)
{
VALUE n = OPERAND_AT(nobj, 0);
LABEL *dl = (LABEL *)OPERAND_AT(dobj, 0);
LABEL *nl = (LABEL *)n;
--dl->refcnt;
++nl->refcnt;
OPERAND_AT(dobj, 0) = n;
if (!dl->refcnt) ELEM_REMOVE(&dl->link);
}
static LABEL*
find_destination(INSN *i)
{
int pos, len = insn_len(i->insn_id);
for (pos = 0; pos < len; ++pos) {
if (insn_op_types(i->insn_id)[pos] == TS_OFFSET) {
return (LABEL *)OPERAND_AT(i, pos);
}
}
return 0;
}
static int
remove_unreachable_chunk(rb_iseq_t *iseq, LINK_ELEMENT *i)
{
LINK_ELEMENT *first = i, *end;
int *unref_counts = 0, nlabels = ISEQ_COMPILE_DATA(iseq)->label_no;
if (!i) return 0;
unref_counts = ALLOCA_N(int, nlabels);
MEMZERO(unref_counts, int, nlabels);
end = i;
do {
LABEL *lab;
if (IS_INSN(i)) {
if (IS_INSN_ID(i, leave)) {
end = i;
break;
}
else if ((lab = find_destination((INSN *)i)) != 0) {
unref_counts[lab->label_no]++;
}
}
else if (IS_LABEL(i)) {
lab = (LABEL *)i;
if (lab->unremovable) return 0;
if (lab->refcnt > unref_counts[lab->label_no]) {
if (i == first) return 0;
break;
}
continue;
}
else if (IS_TRACE(i)) {
/* do nothing */
}
else if (IS_ADJUST(i)) {
return 0;
}
end = i;
} while ((i = i->next) != 0);
i = first;
do {
if (IS_INSN(i)) {
struct rb_iseq_constant_body *body = ISEQ_BODY(iseq);
VALUE insn = INSN_OF(i);
int pos, len = insn_len(insn);
for (pos = 0; pos < len; ++pos) {
switch (insn_op_types(insn)[pos]) {
case TS_OFFSET:
unref_destination((INSN *)i, pos);
break;
case TS_CALLDATA:
--(body->ci_size);
break;
}
}
}
ELEM_REMOVE(i);
} while ((i != end) && (i = i->next) != 0);
return 1;
}
static int
iseq_pop_newarray(rb_iseq_t *iseq, INSN *iobj)
{
switch (OPERAND_AT(iobj, 0)) {
case INT2FIX(0): /* empty array */
ELEM_REMOVE(&iobj->link);
return TRUE;
case INT2FIX(1): /* single element array */
ELEM_REMOVE(&iobj->link);
return FALSE;
default:
iobj->insn_id = BIN(adjuststack);
return TRUE;
}
}
static int
is_frozen_putstring(INSN *insn, VALUE *op)
{
if (IS_INSN_ID(insn, putstring)) {
*op = OPERAND_AT(insn, 0);
return 1;
}
else if (IS_INSN_ID(insn, putobject)) { /* frozen_string_literal */
*op = OPERAND_AT(insn, 0);
return RB_TYPE_P(*op, T_STRING);
}
return 0;
}
static int
optimize_checktype(rb_iseq_t *iseq, INSN *iobj)
{
/*
* putobject obj
* dup
* checktype T_XXX
* branchif l1
* l2:
* ...
* l1:
*
* => obj is a T_XXX
*
* putobject obj (T_XXX)
* jump L1
* L1:
*
* => obj is not a T_XXX
*
* putobject obj (T_XXX)
* jump L2
* L2:
*/
int line, node_id;
INSN *niobj, *ciobj, *dup = 0;
LABEL *dest = 0;
VALUE type;
switch (INSN_OF(iobj)) {
case BIN(putstring):
type = INT2FIX(T_STRING);
break;
case BIN(putnil):
type = INT2FIX(T_NIL);
break;
case BIN(putobject):
type = INT2FIX(TYPE(OPERAND_AT(iobj, 0)));
break;
default: return FALSE;
}
ciobj = (INSN *)get_next_insn(iobj);
if (IS_INSN_ID(ciobj, jump)) {
ciobj = (INSN *)get_next_insn((INSN*)OPERAND_AT(ciobj, 0));
}
if (IS_INSN_ID(ciobj, dup)) {
ciobj = (INSN *)get_next_insn(dup = ciobj);
}
if (!ciobj || !IS_INSN_ID(ciobj, checktype)) return FALSE;
niobj = (INSN *)get_next_insn(ciobj);
if (!niobj) {
/* TODO: putobject true/false */
return FALSE;
}
switch (INSN_OF(niobj)) {
case BIN(branchif):
if (OPERAND_AT(ciobj, 0) == type) {
dest = (LABEL *)OPERAND_AT(niobj, 0);
}
break;
case BIN(branchunless):
if (OPERAND_AT(ciobj, 0) != type) {
dest = (LABEL *)OPERAND_AT(niobj, 0);
}
break;
default:
return FALSE;
}
line = ciobj->insn_info.line_no;
node_id = ciobj->insn_info.node_id;
NODE dummy_line_node = generate_dummy_line_node(line, node_id);
if (!dest) {
if (niobj->link.next && IS_LABEL(niobj->link.next)) {
dest = (LABEL *)niobj->link.next; /* reuse label */
}
else {
dest = NEW_LABEL(line);
ELEM_INSERT_NEXT(&niobj->link, &dest->link);
}
}
INSERT_AFTER_INSN1(iobj, &dummy_line_node, jump, dest);
LABEL_REF(dest);
if (!dup) INSERT_AFTER_INSN(iobj, &dummy_line_node, pop);
return TRUE;
}
static const struct rb_callinfo *
ci_flag_set(const rb_iseq_t *iseq, const struct rb_callinfo *ci, unsigned int add)
{
const struct rb_callinfo *nci = vm_ci_new(vm_ci_mid(ci),
vm_ci_flag(ci) | add,
vm_ci_argc(ci),
vm_ci_kwarg(ci));
RB_OBJ_WRITTEN(iseq, ci, nci);
return nci;
}
static const struct rb_callinfo *
ci_argc_set(const rb_iseq_t *iseq, const struct rb_callinfo *ci, int argc)
{
const struct rb_callinfo *nci = vm_ci_new(vm_ci_mid(ci),
vm_ci_flag(ci),
argc,
vm_ci_kwarg(ci));
RB_OBJ_WRITTEN(iseq, ci, nci);
return nci;
}
static int
iseq_peephole_optimize(rb_iseq_t *iseq, LINK_ELEMENT *list, const int do_tailcallopt)
{
INSN *const iobj = (INSN *)list;
again:
optimize_checktype(iseq, iobj);
if (IS_INSN_ID(iobj, jump)) {
INSN *niobj, *diobj, *piobj;
diobj = (INSN *)get_destination_insn(iobj);
niobj = (INSN *)get_next_insn(iobj);
if (diobj == niobj) {
/*
* jump LABEL
* LABEL:
* =>
* LABEL:
*/
unref_destination(iobj, 0);
ELEM_REMOVE(&iobj->link);
return COMPILE_OK;
}
else if (iobj != diobj && IS_INSN(&diobj->link) &&
IS_INSN_ID(diobj, jump) &&
OPERAND_AT(iobj, 0) != OPERAND_AT(diobj, 0) &&
diobj->insn_info.events == 0) {
/*
* useless jump elimination:
* jump LABEL1
* ...
* LABEL1:
* jump LABEL2
*
* => in this case, first jump instruction should jump to
* LABEL2 directly
*/
replace_destination(iobj, diobj);
remove_unreachable_chunk(iseq, iobj->link.next);
goto again;
}
else if (IS_INSN_ID(diobj, leave)) {
/*
* jump LABEL
* ...
* LABEL:
* leave
* =>
* leave
* ...
* LABEL:
* leave
*/
/* replace */
unref_destination(iobj, 0);
iobj->insn_id = BIN(leave);
iobj->operand_size = 0;
iobj->insn_info = diobj->insn_info;
goto again;
}
else if (IS_INSN(iobj->link.prev) &&
(piobj = (INSN *)iobj->link.prev) &&
(IS_INSN_ID(piobj, branchif) ||
IS_INSN_ID(piobj, branchunless))) {
INSN *pdiobj = (INSN *)get_destination_insn(piobj);
if (niobj == pdiobj) {
int refcnt = IS_LABEL(piobj->link.next) ?
((LABEL *)piobj->link.next)->refcnt : 0;
/*
* useless jump elimination (if/unless destination):
* if L1
* jump L2
* L1:
* ...
* L2:
*
* ==>
* unless L2
* L1:
* ...
* L2:
*/
piobj->insn_id = (IS_INSN_ID(piobj, branchif))
? BIN(branchunless) : BIN(branchif);
replace_destination(piobj, iobj);
if (refcnt <= 1) {
ELEM_REMOVE(&iobj->link);
}
else {
/* TODO: replace other branch destinations too */
}
return COMPILE_OK;
}
else if (diobj == pdiobj) {
/*
* useless jump elimination (if/unless before jump):
* L1:
* ...
* if L1
* jump L1
*
* ==>
* L1:
* ...
* pop
* jump L1
*/
NODE dummy_line_node = generate_dummy_line_node(iobj->insn_info.line_no, iobj->insn_info.node_id);
INSN *popiobj = new_insn_core(iseq, &dummy_line_node, BIN(pop), 0, 0);
ELEM_REPLACE(&piobj->link, &popiobj->link);
}
}
if (remove_unreachable_chunk(iseq, iobj->link.next)) {
goto again;
}
}
/*
* putstring "beg"
* putstring "end"
* newrange excl
*
* ==>
*
* putobject "beg".."end"
*/
if (IS_INSN_ID(iobj, newrange)) {
INSN *const range = iobj;
INSN *beg, *end;
VALUE str_beg, str_end;
if ((end = (INSN *)get_prev_insn(range)) != 0 &&
is_frozen_putstring(end, &str_end) &&
(beg = (INSN *)get_prev_insn(end)) != 0 &&
is_frozen_putstring(beg, &str_beg)) {
int excl = FIX2INT(OPERAND_AT(range, 0));
VALUE lit_range = rb_range_new(str_beg, str_end, excl);
ELEM_REMOVE(&beg->link);
ELEM_REMOVE(&end->link);
range->insn_id = BIN(putobject);
OPERAND_AT(range, 0) = lit_range;
RB_OBJ_WRITTEN(iseq, Qundef, lit_range);
}
}
if (IS_INSN_ID(iobj, leave)) {
remove_unreachable_chunk(iseq, iobj->link.next);
}
/*
* ...
* duparray [...]
* concatarray
* =>
* ...
* putobject [...]
* concatarray
*/
if (IS_INSN_ID(iobj, duparray)) {
LINK_ELEMENT *next = iobj->link.next;
if (IS_INSN(next) && IS_INSN_ID(next, concatarray)) {
iobj->insn_id = BIN(putobject);
}
}
if (IS_INSN_ID(iobj, branchif) ||
IS_INSN_ID(iobj, branchnil) ||
IS_INSN_ID(iobj, branchunless)) {
/*
* if L1
* ...
* L1:
* jump L2
* =>
* if L2
*/
INSN *nobj = (INSN *)get_destination_insn(iobj);
/* This is super nasty hack!!!
*
* This jump-jump optimization may ignore event flags of the jump
* instruction being skipped. Actually, Line 2 TracePoint event
* is never fired in the following code:
*
* 1: raise if 1 == 2
* 2: while true
* 3: break
* 4: end
*
* This is critical for coverage measurement. [Bug #15980]
*
* This is a stopgap measure: stop the jump-jump optimization if
* coverage measurement is enabled and if the skipped instruction
* has any event flag.
*
* Note that, still, TracePoint Line event does not occur on Line 2.
* This should be fixed in future.
*/
int stop_optimization =
ISEQ_COVERAGE(iseq) && ISEQ_LINE_COVERAGE(iseq) &&
nobj->link.type == ISEQ_ELEMENT_INSN &&
nobj->insn_info.events;
if (!stop_optimization) {
INSN *pobj = (INSN *)iobj->link.prev;
int prev_dup = 0;
if (pobj) {
if (!IS_INSN(&pobj->link))
pobj = 0;
else if (IS_INSN_ID(pobj, dup))
prev_dup = 1;
}
for (;;) {
if (IS_INSN(&nobj->link) && IS_INSN_ID(nobj, jump)) {
replace_destination(iobj, nobj);
}
else if (prev_dup && IS_INSN_ID(nobj, dup) &&
!!(nobj = (INSN *)nobj->link.next) &&
/* basic blocks, with no labels in the middle */
nobj->insn_id == iobj->insn_id) {
/*
* dup
* if L1
* ...
* L1:
* dup
* if L2
* =>
* dup
* if L2
* ...
* L1:
* dup
* if L2
*/
replace_destination(iobj, nobj);
}
else if (pobj) {
/*
* putnil
* if L1
* =>
* # nothing
*
* putobject true
* if L1
* =>
* jump L1
*
* putstring ".."
* if L1
* =>
* jump L1
*
* putstring ".."
* dup
* if L1
* =>
* putstring ".."
* jump L1
*
*/
int cond;
if (prev_dup && IS_INSN(pobj->link.prev)) {
pobj = (INSN *)pobj->link.prev;
}
if (IS_INSN_ID(pobj, putobject)) {
cond = (IS_INSN_ID(iobj, branchif) ?
OPERAND_AT(pobj, 0) != Qfalse :
IS_INSN_ID(iobj, branchunless) ?
OPERAND_AT(pobj, 0) == Qfalse :
FALSE);
}
else if (IS_INSN_ID(pobj, putstring) ||
IS_INSN_ID(pobj, duparray) ||
IS_INSN_ID(pobj, newarray)) {
cond = IS_INSN_ID(iobj, branchif);
}
else if (IS_INSN_ID(pobj, putnil)) {
cond = !IS_INSN_ID(iobj, branchif);
}
else break;
if (prev_dup || !IS_INSN_ID(pobj, newarray)) {
ELEM_REMOVE(iobj->link.prev);
}
else if (!iseq_pop_newarray(iseq, pobj)) {
NODE dummy_line_node = generate_dummy_line_node(pobj->insn_info.line_no, pobj->insn_info.node_id);
pobj = new_insn_core(iseq, &dummy_line_node, BIN(pop), 0, NULL);
ELEM_INSERT_PREV(&iobj->link, &pobj->link);
}
if (cond) {
if (prev_dup) {
NODE dummy_line_node = generate_dummy_line_node(pobj->insn_info.line_no, pobj->insn_info.node_id);
pobj = new_insn_core(iseq, &dummy_line_node, BIN(putnil), 0, NULL);
ELEM_INSERT_NEXT(&iobj->link, &pobj->link);
}
iobj->insn_id = BIN(jump);
goto again;
}
else {
unref_destination(iobj, 0);
ELEM_REMOVE(&iobj->link);
}
break;
}
else break;
nobj = (INSN *)get_destination_insn(nobj);
}
}
}
if (IS_INSN_ID(iobj, pop)) {
/*
* putself / putnil / putobject obj / putstring "..."
* pop
* =>
* # do nothing
*/
LINK_ELEMENT *prev = iobj->link.prev;
if (IS_INSN(prev)) {
enum ruby_vminsn_type previ = ((INSN *)prev)->insn_id;
if (previ == BIN(putobject) || previ == BIN(putnil) ||
previ == BIN(putself) || previ == BIN(putstring) ||
previ == BIN(dup) ||
previ == BIN(getlocal) ||
previ == BIN(getblockparam) ||
previ == BIN(getblockparamproxy) ||
previ == BIN(getinstancevariable) ||
previ == BIN(duparray)) {
/* just push operand or static value and pop soon, no
* side effects */
ELEM_REMOVE(prev);
ELEM_REMOVE(&iobj->link);
}
else if (previ == BIN(newarray) && iseq_pop_newarray(iseq, (INSN*)prev)) {
ELEM_REMOVE(&iobj->link);
}
else if (previ == BIN(concatarray)) {
INSN *piobj = (INSN *)prev;
NODE dummy_line_node = generate_dummy_line_node(piobj->insn_info.line_no, piobj->insn_info.node_id);
INSERT_BEFORE_INSN1(piobj, &dummy_line_node, splatarray, Qfalse);
INSN_OF(piobj) = BIN(pop);
}
else if (previ == BIN(concatstrings)) {
if (OPERAND_AT(prev, 0) == INT2FIX(1)) {
ELEM_REMOVE(prev);
}
else {
ELEM_REMOVE(&iobj->link);
INSN_OF(prev) = BIN(adjuststack);
}
}
}
}
if (IS_INSN_ID(iobj, newarray) ||
IS_INSN_ID(iobj, duparray) ||
IS_INSN_ID(iobj, expandarray) ||
IS_INSN_ID(iobj, concatarray) ||
IS_INSN_ID(iobj, splatarray) ||
0) {
/*
* newarray N
* splatarray
* =>
* newarray N
* newarray always puts an array
*/
LINK_ELEMENT *next = iobj->link.next;
if (IS_INSN(next) && IS_INSN_ID(next, splatarray)) {
/* remove splatarray following always-array insn */
ELEM_REMOVE(next);
}
}
if (IS_INSN_ID(iobj, newarray)) {
LINK_ELEMENT *next = iobj->link.next;
if (IS_INSN(next) && IS_INSN_ID(next, expandarray) &&
OPERAND_AT(next, 1) == INT2FIX(0)) {
VALUE op1, op2;
op1 = OPERAND_AT(iobj, 0);
op2 = OPERAND_AT(next, 0);
ELEM_REMOVE(next);
if (op1 == op2) {
/*
* newarray 2
* expandarray 2, 0
* =>
* swap
*/
if (op1 == INT2FIX(2)) {
INSN_OF(iobj) = BIN(swap);
iobj->operand_size = 0;
}
/*
* newarray X
* expandarray X, 0
* =>
* opt_reverse X
*/
else {
INSN_OF(iobj) = BIN(opt_reverse);
}
}
else {
NODE dummy_line_node = generate_dummy_line_node(iobj->insn_info.line_no, iobj->insn_info.node_id);
long diff = FIX2LONG(op1) - FIX2LONG(op2);
INSN_OF(iobj) = BIN(opt_reverse);
OPERAND_AT(iobj, 0) = OPERAND_AT(next, 0);
if (op1 > op2) {
/* X > Y
* newarray X
* expandarray Y, 0
* =>
* pop * (Y-X)
* opt_reverse Y
*/
for (; diff > 0; diff--) {
INSERT_BEFORE_INSN(iobj, &dummy_line_node, pop);
}
}
else { /* (op1 < op2) */
/* X < Y
* newarray X
* expandarray Y, 0
* =>
* putnil * (Y-X)
* opt_reverse Y
*/
for (; diff < 0; diff++) {
INSERT_BEFORE_INSN(iobj, &dummy_line_node, putnil);
}
}
}
}
}
if (IS_INSN_ID(iobj, duparray)) {
LINK_ELEMENT *next = iobj->link.next;
/*
* duparray obj
* expandarray X, 0
* =>
* putobject obj
* expandarray X, 0
*/
if (IS_INSN(next) && IS_INSN_ID(next, expandarray)) {
INSN_OF(iobj) = BIN(putobject);
}
}
if (IS_INSN_ID(iobj, anytostring)) {
LINK_ELEMENT *next = iobj->link.next;
/*
* anytostring
* concatstrings 1
* =>
* anytostring
*/
if (IS_INSN(next) && IS_INSN_ID(next, concatstrings) &&
OPERAND_AT(next, 0) == INT2FIX(1)) {
ELEM_REMOVE(next);
}
}
if (IS_INSN_ID(iobj, putstring) ||
(IS_INSN_ID(iobj, putobject) && RB_TYPE_P(OPERAND_AT(iobj, 0), T_STRING))) {
/*
* putstring ""
* concatstrings N
* =>
* concatstrings N-1
*/
if (IS_NEXT_INSN_ID(&iobj->link, concatstrings) &&
RSTRING_LEN(OPERAND_AT(iobj, 0)) == 0) {
INSN *next = (INSN *)iobj->link.next;
if ((OPERAND_AT(next, 0) = FIXNUM_INC(OPERAND_AT(next, 0), -1)) == INT2FIX(1)) {
ELEM_REMOVE(&next->link);
}
ELEM_REMOVE(&iobj->link);
}
}
if (IS_INSN_ID(iobj, concatstrings)) {
/*
* concatstrings N
* concatstrings M
* =>
* concatstrings N+M-1
*/
LINK_ELEMENT *next = iobj->link.next;
INSN *jump = 0;
if (IS_INSN(next) && IS_INSN_ID(next, jump))
next = get_destination_insn(jump = (INSN *)next);
if (IS_INSN(next) && IS_INSN_ID(next, concatstrings)) {
int n = FIX2INT(OPERAND_AT(iobj, 0)) + FIX2INT(OPERAND_AT(next, 0)) - 1;
OPERAND_AT(iobj, 0) = INT2FIX(n);
if (jump) {
LABEL *label = ((LABEL *)OPERAND_AT(jump, 0));
if (!--label->refcnt) {
ELEM_REMOVE(&label->link);
}
else {
label = NEW_LABEL(0);
OPERAND_AT(jump, 0) = (VALUE)label;
}
label->refcnt++;
ELEM_INSERT_NEXT(next, &label->link);
CHECK(iseq_peephole_optimize(iseq, get_next_insn(jump), do_tailcallopt));
}
else {
ELEM_REMOVE(next);
}
}
}
if (do_tailcallopt &&
(IS_INSN_ID(iobj, send) ||
IS_INSN_ID(iobj, opt_aref_with) ||
IS_INSN_ID(iobj, opt_aset_with) ||
IS_INSN_ID(iobj, invokesuper))) {
/*
* send ...
* leave
* =>
* send ..., ... | VM_CALL_TAILCALL, ...
* leave # unreachable
*/
INSN *piobj = NULL;
if (iobj->link.next) {
LINK_ELEMENT *next = iobj->link.next;
do {
if (!IS_INSN(next)) {
next = next->next;
continue;
}
switch (INSN_OF(next)) {
case BIN(nop):
next = next->next;
break;
case BIN(jump):
/* if cond
* return tailcall
* end
*/
next = get_destination_insn((INSN *)next);
break;
case BIN(leave):
piobj = iobj;
/* fall through */
default:
next = NULL;
break;
}
} while (next);
}
if (piobj) {
const struct rb_callinfo *ci = (struct rb_callinfo *)OPERAND_AT(piobj, 0);
if (IS_INSN_ID(piobj, send) ||
IS_INSN_ID(piobj, invokesuper)) {
if (OPERAND_AT(piobj, 1) == 0) { /* no blockiseq */
ci = ci_flag_set(iseq, ci, VM_CALL_TAILCALL);
OPERAND_AT(piobj, 0) = (VALUE)ci;
RB_OBJ_WRITTEN(iseq, Qundef, ci);
}
}
else {
ci = ci_flag_set(iseq, ci, VM_CALL_TAILCALL);
OPERAND_AT(piobj, 0) = (VALUE)ci;
RB_OBJ_WRITTEN(iseq, Qundef, ci);
}
}
}
if (IS_INSN_ID(iobj, dup)) {
if (IS_NEXT_INSN_ID(&iobj->link, setlocal)) {
LINK_ELEMENT *set1 = iobj->link.next, *set2 = NULL;
/*
* dup
* setlocal x, y
* setlocal x, y
* =>
* dup
* setlocal x, y
*/
if (IS_NEXT_INSN_ID(set1, setlocal)) {
set2 = set1->next;
if (OPERAND_AT(set1, 0) == OPERAND_AT(set2, 0) &&
OPERAND_AT(set1, 1) == OPERAND_AT(set2, 1)) {
ELEM_REMOVE(set1);
ELEM_REMOVE(&iobj->link);
}
}
/*
* dup
* setlocal x, y
* dup
* setlocal x, y
* =>
* dup
* setlocal x, y
*/
else if (IS_NEXT_INSN_ID(set1, dup) &&
IS_NEXT_INSN_ID(set1->next, setlocal)) {
set2 = set1->next->next;
if (OPERAND_AT(set1, 0) == OPERAND_AT(set2, 0) &&
OPERAND_AT(set1, 1) == OPERAND_AT(set2, 1)) {
ELEM_REMOVE(set1->next);
ELEM_REMOVE(set2);
}
}
}
}
/*
* getlocal x, y
* dup
* setlocal x, y
* =>
* dup
*/
if (IS_INSN_ID(iobj, getlocal)) {
LINK_ELEMENT *niobj = &iobj->link;
if (IS_NEXT_INSN_ID(niobj, dup)) {
niobj = niobj->next;
}
if (IS_NEXT_INSN_ID(niobj, setlocal)) {
LINK_ELEMENT *set1 = niobj->next;
if (OPERAND_AT(iobj, 0) == OPERAND_AT(set1, 0) &&
OPERAND_AT(iobj, 1) == OPERAND_AT(set1, 1)) {
ELEM_REMOVE(set1);
ELEM_REMOVE(niobj);
}
}
}
/*
* opt_invokebuiltin_delegate
* trace
* leave
* =>
* opt_invokebuiltin_delegate_leave
* trace
* leave
*/
if (IS_INSN_ID(iobj, opt_invokebuiltin_delegate)) {
if (IS_TRACE(iobj->link.next)) {
if (IS_NEXT_INSN_ID(iobj->link.next, leave)) {
iobj->insn_id = BIN(opt_invokebuiltin_delegate_leave);
const struct rb_builtin_function *bf = (const struct rb_builtin_function *)iobj->operands[0];
if (iobj == (INSN *)list && bf->argc == 0 && (iseq->body->builtin_attrs & BUILTIN_ATTR_LEAF)) {
iseq->body->builtin_attrs |= BUILTIN_ATTR_SINGLE_NOARG_INLINE;
}
}
}
}
/*
* getblockparam
* branchif / branchunless
* =>
* getblockparamproxy
* branchif / branchunless
*/
if (IS_INSN_ID(iobj, getblockparam)) {
if (IS_NEXT_INSN_ID(&iobj->link, branchif) || IS_NEXT_INSN_ID(&iobj->link, branchunless)) {
iobj->insn_id = BIN(getblockparamproxy);
}
}
if (IS_INSN_ID(iobj, splatarray) && OPERAND_AT(iobj, 0) == Qtrue) {
LINK_ELEMENT *niobj = &iobj->link;
/*
* Eliminate array allocation for f(1, *a)
*
* splatarray true
* send ARGS_SPLAT and not KW_SPLAT|ARGS_BLOCKARG
* =>
* splatarray false
* send
*/
if (IS_NEXT_INSN_ID(niobj, send)) {
niobj = niobj->next;
unsigned int flag = vm_ci_flag((const struct rb_callinfo *)OPERAND_AT(niobj, 0));
if ((flag & VM_CALL_ARGS_SPLAT) && !(flag & (VM_CALL_KW_SPLAT|VM_CALL_ARGS_BLOCKARG))) {
OPERAND_AT(iobj, 0) = Qfalse;
}
} else if (IS_NEXT_INSN_ID(niobj, getlocal) || IS_NEXT_INSN_ID(niobj, getinstancevariable)) {
niobj = niobj->next;
if (IS_NEXT_INSN_ID(niobj, send)) {
niobj = niobj->next;
unsigned int flag = vm_ci_flag((const struct rb_callinfo *)OPERAND_AT(niobj, 0));
if ((flag & VM_CALL_ARGS_SPLAT)) {
/*
* Eliminate array allocation for f(1, *a, &lvar) and f(1, *a, &@iv)
*
* splatarray true
* getlocal / getinstancevariable
* send ARGS_SPLAT|ARGS_BLOCKARG and not KW_SPLAT
* =>
* splatarray false
* getlocal / getinstancevariable
* send
*/
if ((flag & VM_CALL_ARGS_BLOCKARG) && !(flag & VM_CALL_KW_SPLAT)) {
OPERAND_AT(iobj, 0) = Qfalse;
}
/*
* Eliminate array allocation for f(*a, **lvar) and f(*a, **@iv)
*
* splatarray true
* getlocal / getinstancevariable
* send ARGS_SPLAT|KW_SPLAT and not ARGS_BLOCKARG
* =>
* splatarray false
* getlocal / getinstancevariable
* send
*/
else if (!(flag & VM_CALL_ARGS_BLOCKARG) && (flag & VM_CALL_KW_SPLAT)) {
OPERAND_AT(iobj, 0) = Qfalse;
}
}
}
else if (IS_NEXT_INSN_ID(niobj, getlocal) || IS_NEXT_INSN_ID(niobj, getinstancevariable) ||
IS_NEXT_INSN_ID(niobj, getblockparamproxy)) {
niobj = niobj->next;
/*
* Eliminate array allocation for f(*a, **lvar, &lvar) and f(*a, **@iv, &@iv)
*
* splatarray true
* getlocal / getinstancevariable
* getlocal / getinstancevariable / getblockparamproxy
* send ARGS_SPLAT|KW_SPLAT|ARGS_BLOCKARG
* =>
* splatarray false
* getlocal / getinstancevariable
* getlocal / getinstancevariable / getblockparamproxy
* send
*/
if (IS_NEXT_INSN_ID(niobj, send)) {
niobj = niobj->next;
unsigned int flag = vm_ci_flag((const struct rb_callinfo *)OPERAND_AT(niobj, 0));
if ((flag & VM_CALL_ARGS_SPLAT) && (flag & VM_CALL_KW_SPLAT) && (flag & VM_CALL_ARGS_BLOCKARG)) {
OPERAND_AT(iobj, 0) = Qfalse;
}
}
}
}
else if (IS_NEXT_INSN_ID(niobj, getblockparamproxy)) {
niobj = niobj->next;
if (IS_NEXT_INSN_ID(niobj, send)) {
niobj = niobj->next;
unsigned int flag = vm_ci_flag((const struct rb_callinfo *)OPERAND_AT(niobj, 0));
/*
* Eliminate array allocation for f(1, *a, &arg)
*
* splatarray true
* getblockparamproxy
* send ARGS_SPLAT|ARGS_BLOCKARG and not KW_SPLAT
* =>
* splatarray false
* getblockparamproxy
* send
*/
if ((flag & VM_CALL_ARGS_BLOCKARG) & (flag & VM_CALL_ARGS_SPLAT) && !(flag & VM_CALL_KW_SPLAT)) {
OPERAND_AT(iobj, 0) = Qfalse;
}
}
}
else if (IS_NEXT_INSN_ID(niobj, duphash)) {
niobj = niobj->next;
/*
* Eliminate array allocation for f(*a, kw: 1)
*
* splatarray true
* duphash
* send ARGS_SPLAT|KW_SPLAT|KW_SPLAT_MUT and not ARGS_BLOCKARG
* =>
* splatarray false
* duphash
* send
*/
if (IS_NEXT_INSN_ID(niobj, send)) {
niobj = niobj->next;
unsigned int flag = vm_ci_flag((const struct rb_callinfo *)OPERAND_AT(niobj, 0));
if ((flag & VM_CALL_ARGS_SPLAT) && (flag & VM_CALL_KW_SPLAT) &&
(flag & VM_CALL_KW_SPLAT_MUT) && !(flag & VM_CALL_ARGS_BLOCKARG)) {
OPERAND_AT(iobj, 0) = Qfalse;
}
}
else if (IS_NEXT_INSN_ID(niobj, getlocal) || IS_NEXT_INSN_ID(niobj, getinstancevariable) ||
IS_NEXT_INSN_ID(niobj, getblockparamproxy)) {
niobj = niobj->next;
/*
* Eliminate array allocation for f(*a, kw: 1, &lvar) and f(*a, kw: 1, &@iv)
*
* splatarray true
* duphash
* getlocal / getinstancevariable / getblockparamproxy
* send ARGS_SPLAT|KW_SPLAT|KW_SPLAT_MUT|ARGS_BLOCKARG
* =>
* splatarray false
* duphash
* getlocal / getinstancevariable / getblockparamproxy
* send
*/
if (IS_NEXT_INSN_ID(niobj, send)) {
niobj = niobj->next;
unsigned int flag = vm_ci_flag((const struct rb_callinfo *)OPERAND_AT(niobj, 0));
if ((flag & VM_CALL_ARGS_SPLAT) && (flag & VM_CALL_KW_SPLAT) &&
(flag & VM_CALL_KW_SPLAT_MUT) && (flag & VM_CALL_ARGS_BLOCKARG)) {
OPERAND_AT(iobj, 0) = Qfalse;
}
}
}
}
}
return COMPILE_OK;
}
static int
insn_set_specialized_instruction(rb_iseq_t *iseq, INSN *iobj, int insn_id)
{
iobj->insn_id = insn_id;
iobj->operand_size = insn_len(insn_id) - 1;
iobj->insn_info.events |= RUBY_EVENT_C_CALL | RUBY_EVENT_C_RETURN;
if (insn_id == BIN(opt_neq)) {
VALUE original_ci = iobj->operands[0];
iobj->operand_size = 2;
iobj->operands = compile_data_calloc2(iseq, iobj->operand_size, sizeof(VALUE));
iobj->operands[0] = (VALUE)new_callinfo(iseq, idEq, 1, 0, NULL, FALSE);
iobj->operands[1] = original_ci;
}
return COMPILE_OK;
}
static int
iseq_specialized_instruction(rb_iseq_t *iseq, INSN *iobj)
{
if (IS_INSN_ID(iobj, newarray) && iobj->link.next &&
IS_INSN(iobj->link.next)) {
/*
* [a, b, ...].max/min -> a, b, c, opt_newarray_max/min
*/
INSN *niobj = (INSN *)iobj->link.next;
if (IS_INSN_ID(niobj, send)) {
const struct rb_callinfo *ci = (struct rb_callinfo *)OPERAND_AT(niobj, 0);
if ((vm_ci_flag(ci) & VM_CALL_ARGS_SIMPLE) && vm_ci_argc(ci) == 0) {
switch (vm_ci_mid(ci)) {
case idMax:
case idMin:
case idHash:
{
VALUE num = iobj->operands[0];
iobj->insn_id = BIN(opt_newarray_send);
iobj->operands = compile_data_calloc2(iseq, insn_len(iobj->insn_id) - 1, sizeof(VALUE));
iobj->operands[0] = num;
iobj->operands[1] = rb_id2sym(vm_ci_mid(ci));
iobj->operand_size = insn_len(iobj->insn_id) - 1;
ELEM_REMOVE(&niobj->link);
return COMPILE_OK;
}
}
}
}
}
if (IS_INSN_ID(iobj, send)) {
const struct rb_callinfo *ci = (struct rb_callinfo *)OPERAND_AT(iobj, 0);
const rb_iseq_t *blockiseq = (rb_iseq_t *)OPERAND_AT(iobj, 1);
#define SP_INSN(opt) insn_set_specialized_instruction(iseq, iobj, BIN(opt_##opt))
if (vm_ci_flag(ci) & VM_CALL_ARGS_SIMPLE) {
switch (vm_ci_argc(ci)) {
case 0:
switch (vm_ci_mid(ci)) {
case idLength: SP_INSN(length); return COMPILE_OK;
case idSize: SP_INSN(size); return COMPILE_OK;
case idEmptyP: SP_INSN(empty_p);return COMPILE_OK;
case idNilP: SP_INSN(nil_p); return COMPILE_OK;
case idSucc: SP_INSN(succ); return COMPILE_OK;
case idNot: SP_INSN(not); return COMPILE_OK;
}
break;
case 1:
switch (vm_ci_mid(ci)) {
case idPLUS: SP_INSN(plus); return COMPILE_OK;
case idMINUS: SP_INSN(minus); return COMPILE_OK;
case idMULT: SP_INSN(mult); return COMPILE_OK;
case idDIV: SP_INSN(div); return COMPILE_OK;
case idMOD: SP_INSN(mod); return COMPILE_OK;
case idEq: SP_INSN(eq); return COMPILE_OK;
case idNeq: SP_INSN(neq); return COMPILE_OK;
case idEqTilde:SP_INSN(regexpmatch2);return COMPILE_OK;
case idLT: SP_INSN(lt); return COMPILE_OK;
case idLE: SP_INSN(le); return COMPILE_OK;
case idGT: SP_INSN(gt); return COMPILE_OK;
case idGE: SP_INSN(ge); return COMPILE_OK;
case idLTLT: SP_INSN(ltlt); return COMPILE_OK;
case idAREF: SP_INSN(aref); return COMPILE_OK;
case idAnd: SP_INSN(and); return COMPILE_OK;
case idOr: SP_INSN(or); return COMPILE_OK;
}
break;
case 2:
switch (vm_ci_mid(ci)) {
case idASET: SP_INSN(aset); return COMPILE_OK;
}
break;
}
}
if ((vm_ci_flag(ci) & VM_CALL_ARGS_BLOCKARG) == 0 && blockiseq == NULL) {
iobj->insn_id = BIN(opt_send_without_block);
iobj->operand_size = insn_len(iobj->insn_id) - 1;
}
}
#undef SP_INSN
return COMPILE_OK;
}
static inline int
tailcallable_p(rb_iseq_t *iseq)
{
switch (ISEQ_BODY(iseq)->type) {
case ISEQ_TYPE_TOP:
case ISEQ_TYPE_EVAL:
case ISEQ_TYPE_MAIN:
/* not tail callable because cfp will be over popped */
case ISEQ_TYPE_RESCUE:
case ISEQ_TYPE_ENSURE:
/* rescue block can't tail call because of errinfo */
return FALSE;
default:
return TRUE;
}
}
static int
iseq_optimize(rb_iseq_t *iseq, LINK_ANCHOR *const anchor)
{
LINK_ELEMENT *list;
const int do_peepholeopt = ISEQ_COMPILE_DATA(iseq)->option->peephole_optimization;
const int do_tailcallopt = tailcallable_p(iseq) &&
ISEQ_COMPILE_DATA(iseq)->option->tailcall_optimization;
const int do_si = ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction;
const int do_ou = ISEQ_COMPILE_DATA(iseq)->option->operands_unification;
int rescue_level = 0;
int tailcallopt = do_tailcallopt;
list = FIRST_ELEMENT(anchor);
int do_block_optimization = 0;
if (ISEQ_BODY(iseq)->type == ISEQ_TYPE_BLOCK && !ISEQ_COMPILE_DATA(iseq)->catch_except_p) {
do_block_optimization = 1;
}
while (list) {
if (IS_INSN(list)) {
if (do_peepholeopt) {
iseq_peephole_optimize(iseq, list, tailcallopt);
}
if (do_si) {
iseq_specialized_instruction(iseq, (INSN *)list);
}
if (do_ou) {
insn_operands_unification((INSN *)list);
}
if (do_block_optimization) {
INSN * item = (INSN *)list;
if (IS_INSN_ID(item, jump)) {
do_block_optimization = 0;
}
}
}
if (IS_LABEL(list)) {
switch (((LABEL *)list)->rescued) {
case LABEL_RESCUE_BEG:
rescue_level++;
tailcallopt = FALSE;
break;
case LABEL_RESCUE_END:
if (!--rescue_level) tailcallopt = do_tailcallopt;
break;
}
}
list = list->next;
}
if (do_block_optimization) {
LINK_ELEMENT * le = FIRST_ELEMENT(anchor)->next;
if (IS_INSN(le) && IS_INSN_ID((INSN *)le, nop)) {
ELEM_REMOVE(le);
}
}
return COMPILE_OK;
}
#if OPT_INSTRUCTIONS_UNIFICATION
static INSN *
new_unified_insn(rb_iseq_t *iseq,
int insn_id, int size, LINK_ELEMENT *seq_list)
{
INSN *iobj = 0;
LINK_ELEMENT *list = seq_list;
int i, argc = 0;
VALUE *operands = 0, *ptr = 0;
/* count argc */
for (i = 0; i < size; i++) {
iobj = (INSN *)list;
argc += iobj->operand_size;
list = list->next;
}
if (argc > 0) {
ptr = operands = compile_data_alloc2(iseq, sizeof(VALUE), argc);
}
/* copy operands */
list = seq_list;
for (i = 0; i < size; i++) {
iobj = (INSN *)list;
MEMCPY(ptr, iobj->operands, VALUE, iobj->operand_size);
ptr += iobj->operand_size;
list = list->next;
}
NODE dummy_line_node = generate_dummy_line_node(iobj->insn_info.line_no, iobj->insn_info.node_id);
return new_insn_core(iseq, &dummy_line_node, insn_id, argc, operands);
}
#endif
/*
* This scheme can get more performance if do this optimize with
* label address resolving.
* It's future work (if compile time was bottle neck).
*/
static int
iseq_insns_unification(rb_iseq_t *iseq, LINK_ANCHOR *const anchor)
{
#if OPT_INSTRUCTIONS_UNIFICATION
LINK_ELEMENT *list;
INSN *iobj, *niobj;
int id, k;
intptr_t j;
list = FIRST_ELEMENT(anchor);
while (list) {
if (IS_INSN(list)) {
iobj = (INSN *)list;
id = iobj->insn_id;
if (unified_insns_data[id] != 0) {
const int *const *entry = unified_insns_data[id];
for (j = 1; j < (intptr_t)entry[0]; j++) {
const int *unified = entry[j];
LINK_ELEMENT *li = list->next;
for (k = 2; k < unified[1]; k++) {
if (!IS_INSN(li) ||
((INSN *)li)->insn_id != unified[k]) {
goto miss;
}
li = li->next;
}
/* matched */
niobj =
new_unified_insn(iseq, unified[0], unified[1] - 1,
list);
/* insert to list */
niobj->link.prev = (LINK_ELEMENT *)iobj->link.prev;
niobj->link.next = li;
if (li) {
li->prev = (LINK_ELEMENT *)niobj;
}
list->prev->next = (LINK_ELEMENT *)niobj;
list = (LINK_ELEMENT *)niobj;
break;
miss:;
}
}
}
list = list->next;
}
#endif
return COMPILE_OK;
}
static int
all_string_result_p(const NODE *node)
{
if (!node) return FALSE;
switch (nd_type(node)) {
case NODE_STR: case NODE_DSTR:
return TRUE;
case NODE_IF: case NODE_UNLESS:
if (!RNODE_IF(node)->nd_body || !RNODE_IF(node)->nd_else) return FALSE;
if (all_string_result_p(RNODE_IF(node)->nd_body))
return all_string_result_p(RNODE_IF(node)->nd_else);
return FALSE;
case NODE_AND: case NODE_OR:
if (!RNODE_AND(node)->nd_2nd)
return all_string_result_p(RNODE_AND(node)->nd_1st);
if (!all_string_result_p(RNODE_AND(node)->nd_1st))
return FALSE;
return all_string_result_p(RNODE_AND(node)->nd_2nd);
default:
return FALSE;
}
}
static int
compile_dstr_fragments(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, int *cntp)
{
const struct RNode_LIST *list = RNODE_DSTR(node)->nd_next;
VALUE lit = RNODE_DSTR(node)->nd_lit;
LINK_ELEMENT *first_lit = 0;
int cnt = 0;
debugp_param("nd_lit", lit);
if (!NIL_P(lit)) {
cnt++;
if (!RB_TYPE_P(lit, T_STRING)) {
COMPILE_ERROR(ERROR_ARGS "dstr: must be string: %s",
rb_builtin_type_name(TYPE(lit)));
return COMPILE_NG;
}
lit = rb_fstring(lit);
ADD_INSN1(ret, node, putobject, lit);
RB_OBJ_WRITTEN(iseq, Qundef, lit);
if (RSTRING_LEN(lit) == 0) first_lit = LAST_ELEMENT(ret);
}
while (list) {
const NODE *const head = list->nd_head;
if (nd_type_p(head, NODE_STR)) {
lit = rb_fstring(RNODE_STR(head)->nd_lit);
ADD_INSN1(ret, head, putobject, lit);
RB_OBJ_WRITTEN(iseq, Qundef, lit);
lit = Qnil;
}
else {
CHECK(COMPILE(ret, "each string", head));
}
cnt++;
list = (struct RNode_LIST *)list->nd_next;
}
if (NIL_P(lit) && first_lit) {
ELEM_REMOVE(first_lit);
--cnt;
}
*cntp = cnt;
return COMPILE_OK;
}
static int
compile_block(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node, int popped)
{
while (node && nd_type_p(node, NODE_BLOCK)) {
CHECK(COMPILE_(ret, "BLOCK body", RNODE_BLOCK(node)->nd_head,
(RNODE_BLOCK(node)->nd_next ? 1 : popped)));
node = RNODE_BLOCK(node)->nd_next;
}
if (node) {
CHECK(COMPILE_(ret, "BLOCK next", RNODE_BLOCK(node)->nd_next, popped));
}
return COMPILE_OK;
}
static int
compile_dstr(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node)
{
int cnt;
if (!RNODE_DSTR(node)->nd_next) {
VALUE lit = rb_fstring(RNODE_DSTR(node)->nd_lit);
ADD_INSN1(ret, node, putstring, lit);
RB_OBJ_WRITTEN(iseq, Qundef, lit);
}
else {
CHECK(compile_dstr_fragments(iseq, ret, node, &cnt));
ADD_INSN1(ret, node, concatstrings, INT2FIX(cnt));
}
return COMPILE_OK;
}
static int
compile_dregx(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, int popped)
{
int cnt;
if (!RNODE_DREGX(node)->nd_next) {
VALUE match = RNODE_DREGX(node)->nd_lit;
if (RB_TYPE_P(match, T_REGEXP)) {
if (!popped) {
ADD_INSN1(ret, node, putobject, match);
RB_OBJ_WRITTEN(iseq, Qundef, match);
}
return COMPILE_OK;
}
}
CHECK(compile_dstr_fragments(iseq, ret, node, &cnt));
ADD_INSN2(ret, node, toregexp, INT2FIX(RNODE_DREGX(node)->nd_cflag), INT2FIX(cnt));
if (popped) {
ADD_INSN(ret, node, pop);
}
return COMPILE_OK;
}
static int
compile_flip_flop(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, int again,
LABEL *then_label, LABEL *else_label)
{
const int line = nd_line(node);
LABEL *lend = NEW_LABEL(line);
rb_num_t cnt = ISEQ_FLIP_CNT_INCREMENT(ISEQ_BODY(iseq)->local_iseq)
+ VM_SVAR_FLIPFLOP_START;
VALUE key = INT2FIX(cnt);
ADD_INSN2(ret, node, getspecial, key, INT2FIX(0));
ADD_INSNL(ret, node, branchif, lend);
/* *flip == 0 */
CHECK(COMPILE(ret, "flip2 beg", RNODE_FLIP2(node)->nd_beg));
ADD_INSNL(ret, node, branchunless, else_label);
ADD_INSN1(ret, node, putobject, Qtrue);
ADD_INSN1(ret, node, setspecial, key);
if (!again) {
ADD_INSNL(ret, node, jump, then_label);
}
/* *flip == 1 */
ADD_LABEL(ret, lend);
CHECK(COMPILE(ret, "flip2 end", RNODE_FLIP2(node)->nd_end));
ADD_INSNL(ret, node, branchunless, then_label);
ADD_INSN1(ret, node, putobject, Qfalse);
ADD_INSN1(ret, node, setspecial, key);
ADD_INSNL(ret, node, jump, then_label);
return COMPILE_OK;
}
static int
compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *ret, const NODE *cond,
LABEL *then_label, LABEL *else_label);
#define COMPILE_SINGLE 2
static int
compile_logical(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *cond,
LABEL *then_label, LABEL *else_label)
{
DECL_ANCHOR(seq);
INIT_ANCHOR(seq);
LABEL *label = NEW_LABEL(nd_line(cond));
if (!then_label) then_label = label;
else if (!else_label) else_label = label;
CHECK(compile_branch_condition(iseq, seq, cond, then_label, else_label));
if (LIST_INSN_SIZE_ONE(seq)) {
INSN *insn = (INSN *)ELEM_FIRST_INSN(FIRST_ELEMENT(seq));
if (insn->insn_id == BIN(jump) && (LABEL *)(insn->operands[0]) == label)
return COMPILE_OK;
}
if (!label->refcnt) {
return COMPILE_SINGLE;
}
ADD_LABEL(seq, label);
ADD_SEQ(ret, seq);
return COMPILE_OK;
}
static int
compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *ret, const NODE *cond,
LABEL *then_label, LABEL *else_label)
{
int ok;
DECL_ANCHOR(ignore);
again:
switch (nd_type(cond)) {
case NODE_AND:
CHECK(ok = compile_logical(iseq, ret, RNODE_AND(cond)->nd_1st, NULL, else_label));
cond = RNODE_AND(cond)->nd_2nd;
if (ok == COMPILE_SINGLE) {
INIT_ANCHOR(ignore);
ret = ignore;
then_label = NEW_LABEL(nd_line(cond));
}
goto again;
case NODE_OR:
CHECK(ok = compile_logical(iseq, ret, RNODE_OR(cond)->nd_1st, then_label, NULL));
cond = RNODE_OR(cond)->nd_2nd;
if (ok == COMPILE_SINGLE) {
INIT_ANCHOR(ignore);
ret = ignore;
else_label = NEW_LABEL(nd_line(cond));
}
goto again;
case NODE_LIT: /* NODE_LIT is always true */
case NODE_TRUE:
case NODE_STR:
case NODE_ZLIST:
case NODE_LAMBDA:
/* printf("useless condition eliminate (%s)\n", ruby_node_name(nd_type(cond))); */
ADD_INSNL(ret, cond, jump, then_label);
return COMPILE_OK;
case NODE_FALSE:
case NODE_NIL:
/* printf("useless condition eliminate (%s)\n", ruby_node_name(nd_type(cond))); */
ADD_INSNL(ret, cond, jump, else_label);
return COMPILE_OK;
case NODE_LIST:
case NODE_ARGSCAT:
case NODE_DREGX:
case NODE_DSTR:
CHECK(COMPILE_POPPED(ret, "branch condition", cond));
ADD_INSNL(ret, cond, jump, then_label);
return COMPILE_OK;
case NODE_FLIP2:
CHECK(compile_flip_flop(iseq, ret, cond, TRUE, then_label, else_label));
return COMPILE_OK;
case NODE_FLIP3:
CHECK(compile_flip_flop(iseq, ret, cond, FALSE, then_label, else_label));
return COMPILE_OK;
case NODE_DEFINED:
CHECK(compile_defined_expr(iseq, ret, cond, Qfalse));
break;
default:
{
DECL_ANCHOR(cond_seq);
INIT_ANCHOR(cond_seq);
CHECK(COMPILE(cond_seq, "branch condition", cond));
if (LIST_INSN_SIZE_ONE(cond_seq)) {
INSN *insn = (INSN *)ELEM_FIRST_INSN(FIRST_ELEMENT(cond_seq));
if (insn->insn_id == BIN(putobject)) {
if (RTEST(insn->operands[0])) {
ADD_INSNL(ret, cond, jump, then_label);
// maybe unreachable
return COMPILE_OK;
}
else {
ADD_INSNL(ret, cond, jump, else_label);
return COMPILE_OK;
}
}
}
ADD_SEQ(ret, cond_seq);
}
break;
}
ADD_INSNL(ret, cond, branchunless, else_label);
ADD_INSNL(ret, cond, jump, then_label);
return COMPILE_OK;
}
#define HASH_BRACE 1
static int
keyword_node_p(const NODE *const node)
{
return nd_type_p(node, NODE_HASH) && (RNODE_HASH(node)->nd_brace & HASH_BRACE) != HASH_BRACE;
}
static int
compile_keyword_arg(rb_iseq_t *iseq, LINK_ANCHOR *const ret,
const NODE *const root_node,
struct rb_callinfo_kwarg **const kw_arg_ptr,
unsigned int *flag)
{
RUBY_ASSERT(nd_type_p(root_node, NODE_HASH));
RUBY_ASSERT(kw_arg_ptr != NULL);
RUBY_ASSERT(flag != NULL);
if (RNODE_HASH(root_node)->nd_head && nd_type_p(RNODE_HASH(root_node)->nd_head, NODE_LIST)) {
const NODE *node = RNODE_HASH(root_node)->nd_head;
int seen_nodes = 0;
while (node) {
const NODE *key_node = RNODE_LIST(node)->nd_head;
seen_nodes++;
assert(nd_type_p(node, NODE_LIST));
if (key_node && nd_type_p(key_node, NODE_LIT) && SYMBOL_P(RNODE_LIT(key_node)->nd_lit)) {
/* can be keywords */
}
else {
if (flag) {
*flag |= VM_CALL_KW_SPLAT;
if (seen_nodes > 1 || RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
/* A new hash will be created for the keyword arguments
* in this case, so mark the method as passing mutable
* keyword splat.
*/
*flag |= VM_CALL_KW_SPLAT_MUT;
}
}
return FALSE;
}
node = RNODE_LIST(node)->nd_next; /* skip value node */
node = RNODE_LIST(node)->nd_next;
}
/* may be keywords */
node = RNODE_HASH(root_node)->nd_head;
{
int len = (int)RNODE_LIST(node)->as.nd_alen / 2;
struct rb_callinfo_kwarg *kw_arg =
rb_xmalloc_mul_add(len, sizeof(VALUE), sizeof(struct rb_callinfo_kwarg));
VALUE *keywords = kw_arg->keywords;
int i = 0;
kw_arg->references = 0;
kw_arg->keyword_len = len;
*kw_arg_ptr = kw_arg;
for (i=0; node != NULL; i++, node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
const NODE *key_node = RNODE_LIST(node)->nd_head;
const NODE *val_node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head;
keywords[i] = RNODE_LIT(key_node)->nd_lit;
NO_CHECK(COMPILE(ret, "keyword values", val_node));
}
assert(i == len);
return TRUE;
}
}
return FALSE;
}
static int
compile_args(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node, NODE **kwnode_ptr)
{
int len = 0;
for (; node; len++, node = RNODE_LIST(node)->nd_next) {
if (CPDEBUG > 0) {
EXPECT_NODE("compile_args", node, NODE_LIST, -1);
}
if (RNODE_LIST(node)->nd_next == NULL && keyword_node_p(RNODE_LIST(node)->nd_head)) { /* last node is kwnode */
*kwnode_ptr = RNODE_LIST(node)->nd_head;
}
else {
RUBY_ASSERT(!keyword_node_p(RNODE_LIST(node)->nd_head));
NO_CHECK(COMPILE_(ret, "array element", RNODE_LIST(node)->nd_head, FALSE));
}
}
return len;
}
static inline int
static_literal_node_p(const NODE *node, const rb_iseq_t *iseq)
{
switch (nd_type(node)) {
case NODE_LIT:
case NODE_NIL:
case NODE_TRUE:
case NODE_FALSE:
return TRUE;
case NODE_STR:
return ISEQ_COMPILE_DATA(iseq)->option->frozen_string_literal;
default:
return FALSE;
}
}
static inline VALUE
static_literal_value(const NODE *node, rb_iseq_t *iseq)
{
switch (nd_type(node)) {
case NODE_NIL:
return Qnil;
case NODE_TRUE:
return Qtrue;
case NODE_FALSE:
return Qfalse;
case NODE_STR:
if (ISEQ_COMPILE_DATA(iseq)->option->debug_frozen_string_literal || RTEST(ruby_debug)) {
VALUE lit;
VALUE debug_info = rb_ary_new_from_args(2, rb_iseq_path(iseq), INT2FIX((int)nd_line(node)));
lit = rb_str_dup(RNODE_STR(node)->nd_lit);
rb_ivar_set(lit, id_debug_created_info, rb_obj_freeze(debug_info));
return rb_str_freeze(lit);
}
else {
return rb_fstring(RNODE_STR(node)->nd_lit);
}
default:
return RNODE_LIT(node)->nd_lit;
}
}
static int
compile_array(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node, int popped)
{
const NODE *line_node = node;
if (nd_type_p(node, NODE_ZLIST)) {
if (!popped) {
ADD_INSN1(ret, line_node, newarray, INT2FIX(0));
}
return 0;
}
EXPECT_NODE("compile_array", node, NODE_LIST, -1);
if (popped) {
for (; node; node = RNODE_LIST(node)->nd_next) {
NO_CHECK(COMPILE_(ret, "array element", RNODE_LIST(node)->nd_head, popped));
}
return 1;
}
/* Compilation of an array literal.
* The following code is essentially the same as:
*
* for (int count = 0; node; count++; node->nd_next) {
* compile(node->nd_head);
* }
* ADD_INSN(newarray, count);
*
* However, there are three points.
*
* - The code above causes stack overflow for a big string literal.
* The following limits the stack length up to max_stack_len.
*
* [x1,x2,...,x10000] =>
* push x1 ; push x2 ; ...; push x256; newarray 256;
* push x257; push x258; ...; push x512; newarray 256; concatarray;
* push x513; push x514; ...; push x768; newarray 256; concatarray;
* ...
*
* - Long subarray can be optimized by pre-allocating a hidden array.
*
* [1,2,3,...,100] =>
* duparray [1,2,3,...,100]
*
* [x, 1,2,3,...,100, z] =>
* push x; newarray 1;
* putobject [1,2,3,...,100] (<- hidden array); concatarray;
* push z; newarray 1; concatarray
*
* - If the last element is a keyword, newarraykwsplat should be emitted
* to check and remove empty keyword arguments hash from array.
* (Note: a keyword is NODE_HASH which is not static_literal_node_p.)
*
* [1,2,3,**kw] =>
* putobject 1; putobject 2; putobject 3; push kw; newarraykwsplat
*/
const int max_stack_len = 0x100;
const int min_tmp_ary_len = 0x40;
int stack_len = 0;
int first_chunk = 1;
/* Convert pushed elements to an array, and concatarray if needed */
#define FLUSH_CHUNK(newarrayinsn) \
if (stack_len) { \
ADD_INSN1(ret, line_node, newarrayinsn, INT2FIX(stack_len)); \
if (!first_chunk) ADD_INSN(ret, line_node, concatarray); \
first_chunk = stack_len = 0; \
}
while (node) {
int count = 1;
/* pre-allocation check (this branch can be omittable) */
if (static_literal_node_p(RNODE_LIST(node)->nd_head, iseq)) {
/* count the elements that are optimizable */
const NODE *node_tmp = RNODE_LIST(node)->nd_next;
for (; node_tmp && static_literal_node_p(RNODE_LIST(node_tmp)->nd_head, iseq); node_tmp = RNODE_LIST(node_tmp)->nd_next)
count++;
if ((first_chunk && stack_len == 0 && !node_tmp) || count >= min_tmp_ary_len) {
/* The literal contains only optimizable elements, or the subarray is long enough */
VALUE ary = rb_ary_hidden_new(count);
/* Create a hidden array */
for (; count; count--, node = RNODE_LIST(node)->nd_next)
rb_ary_push(ary, static_literal_value(RNODE_LIST(node)->nd_head, iseq));
OBJ_FREEZE(ary);
/* Emit optimized code */
FLUSH_CHUNK(newarray);
if (first_chunk) {
ADD_INSN1(ret, line_node, duparray, ary);
first_chunk = 0;
}
else {
ADD_INSN1(ret, line_node, putobject, ary);
ADD_INSN(ret, line_node, concatarray);
}
RB_OBJ_WRITTEN(iseq, Qundef, ary);
}
}
/* Base case: Compile "count" elements */
for (; count; count--, node = RNODE_LIST(node)->nd_next) {
if (CPDEBUG > 0) {
EXPECT_NODE("compile_array", node, NODE_LIST, -1);
}
NO_CHECK(COMPILE_(ret, "array element", RNODE_LIST(node)->nd_head, 0));
stack_len++;
if (!RNODE_LIST(node)->nd_next && keyword_node_p(RNODE_LIST(node)->nd_head)) {
/* Reached the end, and the last element is a keyword */
FLUSH_CHUNK(newarraykwsplat);
return 1;
}
/* If there are many pushed elements, flush them to avoid stack overflow */
if (stack_len >= max_stack_len) FLUSH_CHUNK(newarray);
}
}
FLUSH_CHUNK(newarray);
#undef FLUSH_CHUNK
return 1;
}
/* Compile an array containing the single element represented by node */
static int
compile_array_1(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node)
{
if (static_literal_node_p(node, iseq)) {
VALUE ary = rb_ary_hidden_new(1);
rb_ary_push(ary, static_literal_value(node, iseq));
OBJ_FREEZE(ary);
ADD_INSN1(ret, node, duparray, ary);
}
else {
CHECK(COMPILE_(ret, "array element", node, FALSE));
if (keyword_node_p(node)) {
ADD_INSN1(ret, node, newarraykwsplat, INT2FIX(1));
}
else {
ADD_INSN1(ret, node, newarray, INT2FIX(1));
}
}
return 1;
}
static inline int
static_literal_node_pair_p(const NODE *node, const rb_iseq_t *iseq)
{
return RNODE_LIST(node)->nd_head && static_literal_node_p(RNODE_LIST(node)->nd_head, iseq) && static_literal_node_p(RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head, iseq);
}
static int
compile_hash(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node, int method_call_keywords, int popped)
{
const NODE *line_node = node;
node = RNODE_HASH(node)->nd_head;
if (!node || nd_type_p(node, NODE_ZLIST)) {
if (!popped) {
ADD_INSN1(ret, line_node, newhash, INT2FIX(0));
}
return 0;
}
EXPECT_NODE("compile_hash", node, NODE_LIST, -1);
if (popped) {
for (; node; node = RNODE_LIST(node)->nd_next) {
NO_CHECK(COMPILE_(ret, "hash element", RNODE_LIST(node)->nd_head, popped));
}
return 1;
}
/* Compilation of a hash literal (or keyword arguments).
* This is very similar to compile_array, but there are some differences:
*
* - It contains key-value pairs. So we need to take every two elements.
* We can assume that the length is always even.
*
* - Merging is done by a method call (id_core_hash_merge_ptr).
* Sometimes we need to insert the receiver, so "anchor" is needed.
* In addition, a method call is much slower than concatarray.
* So it pays only when the subsequence is really long.
* (min_tmp_hash_len must be much larger than min_tmp_ary_len.)
*
* - We need to handle keyword splat: **kw.
* For **kw, the key part (node->nd_head) is NULL, and the value part
* (node->nd_next->nd_head) is "kw".
* The code is a bit difficult to avoid hash allocation for **{}.
*/
const int max_stack_len = 0x100;
const int min_tmp_hash_len = 0x800;
int stack_len = 0;
int first_chunk = 1;
DECL_ANCHOR(anchor);
INIT_ANCHOR(anchor);
/* Convert pushed elements to a hash, and merge if needed */
#define FLUSH_CHUNK() \
if (stack_len) { \
if (first_chunk) { \
APPEND_LIST(ret, anchor); \
ADD_INSN1(ret, line_node, newhash, INT2FIX(stack_len)); \
} \
else { \
ADD_INSN1(ret, line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE)); \
ADD_INSN(ret, line_node, swap); \
APPEND_LIST(ret, anchor); \
ADD_SEND(ret, line_node, id_core_hash_merge_ptr, INT2FIX(stack_len + 1)); \
} \
INIT_ANCHOR(anchor); \
first_chunk = stack_len = 0; \
}
while (node) {
int count = 1;
/* pre-allocation check (this branch can be omittable) */
if (static_literal_node_pair_p(node, iseq)) {
/* count the elements that are optimizable */
const NODE *node_tmp = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next;
for (; node_tmp && static_literal_node_pair_p(node_tmp, iseq); node_tmp = RNODE_LIST(RNODE_LIST(node_tmp)->nd_next)->nd_next)
count++;
if ((first_chunk && stack_len == 0 && !node_tmp) || count >= min_tmp_hash_len) {
/* The literal contains only optimizable elements, or the subsequence is long enough */
VALUE ary = rb_ary_hidden_new(count);
/* Create a hidden hash */
for (; count; count--, node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
VALUE elem[2];
elem[0] = static_literal_value(RNODE_LIST(node)->nd_head, iseq);
elem[1] = static_literal_value(RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head, iseq);
rb_ary_cat(ary, elem, 2);
}
VALUE hash = rb_hash_new_with_size(RARRAY_LEN(ary) / 2);
rb_hash_bulk_insert(RARRAY_LEN(ary), RARRAY_CONST_PTR(ary), hash);
hash = rb_obj_hide(hash);
OBJ_FREEZE(hash);
/* Emit optimized code */
FLUSH_CHUNK();
if (first_chunk) {
ADD_INSN1(ret, line_node, duphash, hash);
first_chunk = 0;
}
else {
ADD_INSN1(ret, line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
ADD_INSN(ret, line_node, swap);
ADD_INSN1(ret, line_node, putobject, hash);
ADD_SEND(ret, line_node, id_core_hash_merge_kwd, INT2FIX(2));
}
RB_OBJ_WRITTEN(iseq, Qundef, hash);
}
}
/* Base case: Compile "count" elements */
for (; count; count--, node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
if (CPDEBUG > 0) {
EXPECT_NODE("compile_hash", node, NODE_LIST, -1);
}
if (RNODE_LIST(node)->nd_head) {
/* Normal key-value pair */
NO_CHECK(COMPILE_(anchor, "hash key element", RNODE_LIST(node)->nd_head, 0));
NO_CHECK(COMPILE_(anchor, "hash value element", RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head, 0));
stack_len += 2;
/* If there are many pushed elements, flush them to avoid stack overflow */
if (stack_len >= max_stack_len) FLUSH_CHUNK();
}
else {
/* kwsplat case: foo(..., **kw, ...) */
FLUSH_CHUNK();
const NODE *kw = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head;
int empty_kw = nd_type_p(kw, NODE_LIT) && RB_TYPE_P(RNODE_LIT(kw)->nd_lit, T_HASH); /* foo( ..., **{}, ...) */
int first_kw = first_chunk && stack_len == 0; /* foo(1,2,3, **kw, ...) */
int last_kw = !RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next; /* foo( ..., **kw) */
int only_kw = last_kw && first_kw; /* foo(1,2,3, **kw) */
if (empty_kw) {
if (only_kw && method_call_keywords) {
/* **{} appears at the only keyword argument in method call,
* so it won't be modified.
* kw is a special NODE_LIT that contains a special empty hash,
* so this emits: putobject {}.
* This is only done for method calls and not for literal hashes,
* because literal hashes should always result in a new hash.
*/
NO_CHECK(COMPILE(ret, "keyword splat", kw));
}
else if (first_kw) {
/* **{} appears as the first keyword argument, so it may be modified.
* We need to create a fresh hash object.
*/
ADD_INSN1(ret, line_node, newhash, INT2FIX(0));
}
/* Any empty keyword splats that are not the first can be ignored.
* since merging an empty hash into the existing hash is the same
* as not merging it. */
}
else {
if (only_kw && method_call_keywords) {
/* **kw is only keyword argument in method call.
* Use directly. This will be not be flagged as mutable.
* This is only done for method calls and not for literal hashes,
* because literal hashes should always result in a new hash.
*/
NO_CHECK(COMPILE(ret, "keyword splat", kw));
}
else {
/* There is more than one keyword argument, or this is not a method
* call. In that case, we need to add an empty hash (if first keyword),
* or merge the hash to the accumulated hash (if not the first keyword).
*/
ADD_INSN1(ret, line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
if (first_kw) ADD_INSN1(ret, line_node, newhash, INT2FIX(0));
else ADD_INSN(ret, line_node, swap);
NO_CHECK(COMPILE(ret, "keyword splat", kw));
ADD_SEND(ret, line_node, id_core_hash_merge_kwd, INT2FIX(2));
}
}
first_chunk = 0;
}
}
}
FLUSH_CHUNK();
#undef FLUSH_CHUNK
return 1;
}
VALUE
rb_node_case_when_optimizable_literal(const NODE *const node)
{
switch (nd_type(node)) {
case NODE_LIT: {
VALUE v = RNODE_LIT(node)->nd_lit;
double ival;
if (RB_FLOAT_TYPE_P(v) &&
modf(RFLOAT_VALUE(v), &ival) == 0.0) {
return FIXABLE(ival) ? LONG2FIX((long)ival) : rb_dbl2big(ival);
}
if (RB_TYPE_P(v, T_RATIONAL) || RB_TYPE_P(v, T_COMPLEX)) {
return Qundef;
}
if (SYMBOL_P(v) || rb_obj_is_kind_of(v, rb_cNumeric)) {
return v;
}
break;
}
case NODE_NIL:
return Qnil;
case NODE_TRUE:
return Qtrue;
case NODE_FALSE:
return Qfalse;
case NODE_STR:
return rb_fstring(RNODE_STR(node)->nd_lit);
}
return Qundef;
}
static int
when_vals(rb_iseq_t *iseq, LINK_ANCHOR *const cond_seq, const NODE *vals,
LABEL *l1, int only_special_literals, VALUE literals)
{
while (vals) {
const NODE *val = RNODE_LIST(vals)->nd_head;
VALUE lit = rb_node_case_when_optimizable_literal(val);
if (UNDEF_P(lit)) {
only_special_literals = 0;
}
else if (NIL_P(rb_hash_lookup(literals, lit))) {
rb_hash_aset(literals, lit, (VALUE)(l1) | 1);
}
if (nd_type_p(val, NODE_STR)) {
debugp_param("nd_lit", RNODE_STR(val)->nd_lit);
lit = rb_fstring(RNODE_STR(val)->nd_lit);
ADD_INSN1(cond_seq, val, putobject, lit);
RB_OBJ_WRITTEN(iseq, Qundef, lit);
}
else {
if (!COMPILE(cond_seq, "when cond", val)) return -1;
}
// Emit pattern === target
ADD_INSN1(cond_seq, vals, topn, INT2FIX(1));
ADD_CALL(cond_seq, vals, idEqq, INT2FIX(1));
ADD_INSNL(cond_seq, val, branchif, l1);
vals = RNODE_LIST(vals)->nd_next;
}
return only_special_literals;
}
static int
when_splat_vals(rb_iseq_t *iseq, LINK_ANCHOR *const cond_seq, const NODE *vals,
LABEL *l1, int only_special_literals, VALUE literals)
{
const NODE *line_node = vals;
switch (nd_type(vals)) {
case NODE_LIST:
if (when_vals(iseq, cond_seq, vals, l1, only_special_literals, literals) < 0)
return COMPILE_NG;
break;
case NODE_SPLAT:
ADD_INSN (cond_seq, line_node, dup);
CHECK(COMPILE(cond_seq, "when splat", RNODE_SPLAT(vals)->nd_head));
ADD_INSN1(cond_seq, line_node, splatarray, Qfalse);
ADD_INSN1(cond_seq, line_node, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_CASE | VM_CHECKMATCH_ARRAY));
ADD_INSNL(cond_seq, line_node, branchif, l1);
break;
case NODE_ARGSCAT:
CHECK(when_splat_vals(iseq, cond_seq, RNODE_ARGSCAT(vals)->nd_head, l1, only_special_literals, literals));
CHECK(when_splat_vals(iseq, cond_seq, RNODE_ARGSCAT(vals)->nd_body, l1, only_special_literals, literals));
break;
case NODE_ARGSPUSH:
CHECK(when_splat_vals(iseq, cond_seq, RNODE_ARGSPUSH(vals)->nd_head, l1, only_special_literals, literals));
ADD_INSN (cond_seq, line_node, dup);
CHECK(COMPILE(cond_seq, "when argspush body", RNODE_ARGSPUSH(vals)->nd_body));
ADD_INSN1(cond_seq, line_node, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_CASE));
ADD_INSNL(cond_seq, line_node, branchif, l1);
break;
default:
ADD_INSN (cond_seq, line_node, dup);
CHECK(COMPILE(cond_seq, "when val", vals));
ADD_INSN1(cond_seq, line_node, splatarray, Qfalse);
ADD_INSN1(cond_seq, line_node, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_CASE | VM_CHECKMATCH_ARRAY));
ADD_INSNL(cond_seq, line_node, branchif, l1);
break;
}
return COMPILE_OK;
}
/* Multiple Assignment Handling
*
* In order to handle evaluation of multiple assignment such that the left hand side
* is evaluated before the right hand side, we need to process the left hand side
* and see if there are any attributes that need to be assigned, or constants set
* on explicit objects. If so, we add instructions to evaluate the receiver of
* any assigned attributes or constants before we process the right hand side.
*
* For a multiple assignment such as:
*
* l1.m1, l2[0] = r3, r4
*
* We start off evaluating l1 and l2, then we evaluate r3 and r4, then we
* assign the result of r3 to l1.m1, and then the result of r4 to l2.m2.
* On the VM stack, this looks like:
*
* self # putself
* l1 # send
* l1, self # putself
* l1, l2 # send
* l1, l2, 0 # putobject 0
* l1, l2, 0, [r3, r4] # after evaluation of RHS
* l1, l2, 0, [r3, r4], r4, r3 # expandarray
* l1, l2, 0, [r3, r4], r4, r3, l1 # topn 5
* l1, l2, 0, [r3, r4], r4, l1, r3 # swap
* l1, l2, 0, [r3, r4], r4, m1= # send
* l1, l2, 0, [r3, r4], r4 # pop
* l1, l2, 0, [r3, r4], r4, l2 # topn 3
* l1, l2, 0, [r3, r4], r4, l2, 0 # topn 3
* l1, l2, 0, [r3, r4], r4, l2, 0, r4 # topn 2
* l1, l2, 0, [r3, r4], r4, []= # send
* l1, l2, 0, [r3, r4], r4 # pop
* l1, l2, 0, [r3, r4] # pop
* [r3, r4], l2, 0, [r3, r4] # setn 3
* [r3, r4], l2, 0 # pop
* [r3, r4], l2 # pop
* [r3, r4] # pop
*
* This is made more complex when you have to handle splats, post args,
* and arbitrary levels of nesting. You need to keep track of the total
* number of attributes to set, and for each attribute, how many entries
* are on the stack before the final attribute, in order to correctly
* calculate the topn value to use to get the receiver of the attribute
* setter method.
*
* A brief description of the VM stack for simple multiple assignment
* with no splat (rhs_array will not be present if the return value of
* the multiple assignment is not needed):
*
* lhs_attr1, lhs_attr2, ..., rhs_array, ..., rhs_arg2, rhs_arg1
*
* For multiple assignment with splats, while processing the part before
* the splat (splat+post here is an array of the splat and the post arguments):
*
* lhs_attr1, lhs_attr2, ..., rhs_array, splat+post, ..., rhs_arg2, rhs_arg1
*
* When processing the splat and post arguments:
*
* lhs_attr1, lhs_attr2, ..., rhs_array, ..., post_arg2, post_arg1, splat
*
* When processing nested multiple assignment, existing values on the stack
* are kept. So for:
*
* (l1.m1, l2.m2), l3.m3, l4* = [r1, r2], r3, r4
*
* The stack layout would be the following before processing the nested
* multiple assignment:
*
* l1, l2, [[r1, r2], r3, r4], [r4], r3, [r1, r2]
*
* In order to handle this correctly, we need to keep track of the nesting
* level for each attribute assignment, as well as the attribute number
* (left hand side attributes are processed left to right) and number of
* arguments to pass to the setter method. struct masgn_lhs_node tracks
* this information.
*
* We also need to track information for the entire multiple assignment, such
* as the total number of arguments, and the current nesting level, to
* handle both nested multiple assignment as well as cases where the
* rhs is not needed. We also need to keep track of all attribute
* assignments in this, which we do using a linked listed. struct masgn_state
* tracks this information.
*/
struct masgn_lhs_node {
INSN *before_insn;
struct masgn_lhs_node *next;
const NODE *line_node;
int argn;
int num_args;
int lhs_pos;
};
struct masgn_state {
struct masgn_lhs_node *first_memo;
struct masgn_lhs_node *last_memo;
int lhs_level;
int num_args;
bool nested;
};
static int
add_masgn_lhs_node(struct masgn_state *state, int lhs_pos, const NODE *line_node, int argc, INSN *before_insn)
{
if (!state) {
rb_bug("no masgn_state");
}
struct masgn_lhs_node *memo;
memo = malloc(sizeof(struct masgn_lhs_node));
if (!memo) {
return COMPILE_NG;
}
memo->before_insn = before_insn;
memo->line_node = line_node;
memo->argn = state->num_args + 1;
memo->num_args = argc;
state->num_args += argc;
memo->lhs_pos = lhs_pos;
memo->next = NULL;
if (!state->first_memo) {
state->first_memo = memo;
}
else {
state->last_memo->next = memo;
}
state->last_memo = memo;
return COMPILE_OK;
}
static int compile_massign0(rb_iseq_t *iseq, LINK_ANCHOR *const pre, LINK_ANCHOR *const rhs, LINK_ANCHOR *const lhs, LINK_ANCHOR *const post, const NODE *const node, struct masgn_state *state, int popped);
static int
compile_massign_lhs(rb_iseq_t *iseq, LINK_ANCHOR *const pre, LINK_ANCHOR *const rhs, LINK_ANCHOR *const lhs, LINK_ANCHOR *const post, const NODE *const node, struct masgn_state *state, int lhs_pos)
{
switch (nd_type(node)) {
case NODE_ATTRASGN: {
INSN *iobj;
const NODE *line_node = node;
CHECK(COMPILE_POPPED(pre, "masgn lhs (NODE_ATTRASGN)", node));
bool safenav_call = false;
LINK_ELEMENT *insn_element = LAST_ELEMENT(pre);
iobj = (INSN *)get_prev_insn((INSN *)insn_element); /* send insn */
ASSUME(iobj);
ELEM_REMOVE(insn_element);
if (!IS_INSN_ID(iobj, send)) {
safenav_call = true;
iobj = (INSN *)get_prev_insn(iobj);
ELEM_INSERT_NEXT(&iobj->link, insn_element);
}
(pre->last = iobj->link.prev)->next = 0;
const struct rb_callinfo *ci = (struct rb_callinfo *)OPERAND_AT(iobj, 0);
int argc = vm_ci_argc(ci) + 1;
ci = ci_argc_set(iseq, ci, argc);
OPERAND_AT(iobj, 0) = (VALUE)ci;
RB_OBJ_WRITTEN(iseq, Qundef, ci);
if (argc == 1) {
ADD_INSN(lhs, line_node, swap);
}
else {
ADD_INSN1(lhs, line_node, topn, INT2FIX(argc));
}
if (!add_masgn_lhs_node(state, lhs_pos, line_node, argc, (INSN *)LAST_ELEMENT(lhs))) {
return COMPILE_NG;
}
iobj->link.prev = lhs->last;
lhs->last->next = &iobj->link;
for (lhs->last = &iobj->link; lhs->last->next; lhs->last = lhs->last->next);
if (vm_ci_flag(ci) & VM_CALL_ARGS_SPLAT) {
int argc = vm_ci_argc(ci);
ci = ci_argc_set(iseq, ci, argc - 1);
OPERAND_AT(iobj, 0) = (VALUE)ci;
RB_OBJ_WRITTEN(iseq, Qundef, iobj);
INSERT_BEFORE_INSN1(iobj, line_node, newarray, INT2FIX(1));
INSERT_BEFORE_INSN(iobj, line_node, concatarray);
}
if (!safenav_call) {
ADD_INSN(lhs, line_node, pop);
if (argc != 1) {
ADD_INSN(lhs, line_node, pop);
}
}
for (int i=0; i < argc; i++) {
ADD_INSN(post, line_node, pop);
}
break;
}
case NODE_MASGN: {
DECL_ANCHOR(nest_rhs);
INIT_ANCHOR(nest_rhs);
DECL_ANCHOR(nest_lhs);
INIT_ANCHOR(nest_lhs);
int prev_level = state->lhs_level;
bool prev_nested = state->nested;
state->nested = 1;
state->lhs_level = lhs_pos - 1;
CHECK(compile_massign0(iseq, pre, nest_rhs, nest_lhs, post, node, state, 1));
state->lhs_level = prev_level;
state->nested = prev_nested;
ADD_SEQ(lhs, nest_rhs);
ADD_SEQ(lhs, nest_lhs);
break;
}
case NODE_CDECL:
if (!RNODE_CDECL(node)->nd_vid) {
/* Special handling only needed for expr::C, not for C */
INSN *iobj;
CHECK(COMPILE_POPPED(pre, "masgn lhs (NODE_CDECL)", node));
LINK_ELEMENT *insn_element = LAST_ELEMENT(pre);
iobj = (INSN *)insn_element; /* setconstant insn */
ELEM_REMOVE((LINK_ELEMENT *)get_prev_insn((INSN *)get_prev_insn(iobj)));
ELEM_REMOVE((LINK_ELEMENT *)get_prev_insn(iobj));
ELEM_REMOVE(insn_element);
pre->last = iobj->link.prev;
ADD_ELEM(lhs, (LINK_ELEMENT *)iobj);
if (!add_masgn_lhs_node(state, lhs_pos, node, 1, (INSN *)LAST_ELEMENT(lhs))) {
return COMPILE_NG;
}
ADD_INSN(post, node, pop);
break;
}
/* Fallthrough */
default: {
DECL_ANCHOR(anchor);
INIT_ANCHOR(anchor);
CHECK(COMPILE_POPPED(anchor, "masgn lhs", node));
ELEM_REMOVE(FIRST_ELEMENT(anchor));
ADD_SEQ(lhs, anchor);
}
}
return COMPILE_OK;
}
static int
compile_massign_opt_lhs(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *lhsn)
{
if (lhsn) {
CHECK(compile_massign_opt_lhs(iseq, ret, RNODE_LIST(lhsn)->nd_next));
CHECK(compile_massign_lhs(iseq, ret, ret, ret, ret, RNODE_LIST(lhsn)->nd_head, NULL, 0));
}
return COMPILE_OK;
}
static int
compile_massign_opt(rb_iseq_t *iseq, LINK_ANCHOR *const ret,
const NODE *rhsn, const NODE *orig_lhsn)
{
VALUE mem[64];
const int memsize = numberof(mem);
int memindex = 0;
int llen = 0, rlen = 0;
int i;
const NODE *lhsn = orig_lhsn;
#define MEMORY(v) { \
int i; \
if (memindex == memsize) return 0; \
for (i=0; i<memindex; i++) { \
if (mem[i] == (v)) return 0; \
} \
mem[memindex++] = (v); \
}
if (rhsn == 0 || !nd_type_p(rhsn, NODE_LIST)) {
return 0;
}
while (lhsn) {
const NODE *ln = RNODE_LIST(lhsn)->nd_head;
switch (nd_type(ln)) {
case NODE_LASGN:
case NODE_DASGN:
case NODE_IASGN:
case NODE_CVASGN:
MEMORY(get_nd_vid(ln));
break;
default:
return 0;
}
lhsn = RNODE_LIST(lhsn)->nd_next;
llen++;
}
while (rhsn) {
if (llen <= rlen) {
NO_CHECK(COMPILE_POPPED(ret, "masgn val (popped)", RNODE_LIST(rhsn)->nd_head));
}
else {
NO_CHECK(COMPILE(ret, "masgn val", RNODE_LIST(rhsn)->nd_head));
}
rhsn = RNODE_LIST(rhsn)->nd_next;
rlen++;
}
if (llen > rlen) {
for (i=0; i<llen-rlen; i++) {
ADD_INSN(ret, orig_lhsn, putnil);
}
}
compile_massign_opt_lhs(iseq, ret, orig_lhsn);
return 1;
}
static int
compile_massign0(rb_iseq_t *iseq, LINK_ANCHOR *const pre, LINK_ANCHOR *const rhs, LINK_ANCHOR *const lhs, LINK_ANCHOR *const post, const NODE *const node, struct masgn_state *state, int popped)
{
const NODE *rhsn = RNODE_MASGN(node)->nd_value;
const NODE *splatn = RNODE_MASGN(node)->nd_args;
const NODE *lhsn = RNODE_MASGN(node)->nd_head;
const NODE *lhsn_count = lhsn;
int lhs_splat = (splatn && NODE_NAMED_REST_P(splatn)) ? 1 : 0;
int llen = 0;
int lpos = 0;
while (lhsn_count) {
llen++;
lhsn_count = RNODE_LIST(lhsn_count)->nd_next;
}
while (lhsn) {
CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, RNODE_LIST(lhsn)->nd_head, state, (llen - lpos) + lhs_splat + state->lhs_level));
lpos++;
lhsn = RNODE_LIST(lhsn)->nd_next;
}
if (lhs_splat) {
if (nd_type_p(splatn, NODE_POSTARG)) {
/*a, b, *r, p1, p2 */
const NODE *postn = RNODE_POSTARG(splatn)->nd_2nd;
const NODE *restn = RNODE_POSTARG(splatn)->nd_1st;
int plen = (int)RNODE_LIST(postn)->as.nd_alen;
int ppos = 0;
int flag = 0x02 | (NODE_NAMED_REST_P(restn) ? 0x01 : 0x00);
ADD_INSN2(lhs, splatn, expandarray, INT2FIX(plen), INT2FIX(flag));
if (NODE_NAMED_REST_P(restn)) {
CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, restn, state, 1 + plen + state->lhs_level));
}
while (postn) {
CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, RNODE_LIST(postn)->nd_head, state, (plen - ppos) + state->lhs_level));
ppos++;
postn = RNODE_LIST(postn)->nd_next;
}
}
else {
/* a, b, *r */
CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, splatn, state, 1 + state->lhs_level));
}
}
if (!state->nested) {
NO_CHECK(COMPILE(rhs, "normal masgn rhs", rhsn));
}
if (!popped) {
ADD_INSN(rhs, node, dup);
}
ADD_INSN2(rhs, node, expandarray, INT2FIX(llen), INT2FIX(lhs_splat));
return COMPILE_OK;
}
static int
compile_massign(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, int popped)
{
if (!popped || RNODE_MASGN(node)->nd_args || !compile_massign_opt(iseq, ret, RNODE_MASGN(node)->nd_value, RNODE_MASGN(node)->nd_head)) {
struct masgn_state state;
state.lhs_level = popped ? 0 : 1;
state.nested = 0;
state.num_args = 0;
state.first_memo = NULL;
state.last_memo = NULL;
DECL_ANCHOR(pre);
INIT_ANCHOR(pre);
DECL_ANCHOR(rhs);
INIT_ANCHOR(rhs);
DECL_ANCHOR(lhs);
INIT_ANCHOR(lhs);
DECL_ANCHOR(post);
INIT_ANCHOR(post);
int ok = compile_massign0(iseq, pre, rhs, lhs, post, node, &state, popped);
struct masgn_lhs_node *memo = state.first_memo, *tmp_memo;
while (memo) {
VALUE topn_arg = INT2FIX((state.num_args - memo->argn) + memo->lhs_pos);
for (int i = 0; i < memo->num_args; i++) {
INSERT_BEFORE_INSN1(memo->before_insn, memo->line_node, topn, topn_arg);
}
tmp_memo = memo->next;
free(memo);
memo = tmp_memo;
}
CHECK(ok);
ADD_SEQ(ret, pre);
ADD_SEQ(ret, rhs);
ADD_SEQ(ret, lhs);
if (!popped && state.num_args >= 1) {
/* make sure rhs array is returned before popping */
ADD_INSN1(ret, node, setn, INT2FIX(state.num_args));
}
ADD_SEQ(ret, post);
}
return COMPILE_OK;
}
static VALUE
collect_const_segments(rb_iseq_t *iseq, const NODE *node)
{
VALUE arr = rb_ary_new();
for (;;) {
switch (nd_type(node)) {
case NODE_CONST:
rb_ary_unshift(arr, ID2SYM(RNODE_CONST(node)->nd_vid));
return arr;
case NODE_COLON3:
rb_ary_unshift(arr, ID2SYM(RNODE_COLON3(node)->nd_mid));
rb_ary_unshift(arr, ID2SYM(idNULL));
return arr;
case NODE_COLON2:
rb_ary_unshift(arr, ID2SYM(RNODE_COLON2(node)->nd_mid));
node = RNODE_COLON2(node)->nd_head;
break;
default:
return Qfalse;
}
}
}
static int
compile_const_prefix(rb_iseq_t *iseq, const NODE *const node,
LINK_ANCHOR *const pref, LINK_ANCHOR *const body)
{
switch (nd_type(node)) {
case NODE_CONST:
debugi("compile_const_prefix - colon", RNODE_CONST(node)->nd_vid);
ADD_INSN1(body, node, putobject, Qtrue);
ADD_INSN1(body, node, getconstant, ID2SYM(RNODE_CONST(node)->nd_vid));
break;
case NODE_COLON3:
debugi("compile_const_prefix - colon3", RNODE_COLON3(node)->nd_mid);
ADD_INSN(body, node, pop);
ADD_INSN1(body, node, putobject, rb_cObject);
ADD_INSN1(body, node, putobject, Qtrue);
ADD_INSN1(body, node, getconstant, ID2SYM(RNODE_COLON3(node)->nd_mid));
break;
case NODE_COLON2:
CHECK(compile_const_prefix(iseq, RNODE_COLON2(node)->nd_head, pref, body));
debugi("compile_const_prefix - colon2", RNODE_COLON2(node)->nd_mid);
ADD_INSN1(body, node, putobject, Qfalse);
ADD_INSN1(body, node, getconstant, ID2SYM(RNODE_COLON2(node)->nd_mid));
break;
default:
CHECK(COMPILE(pref, "const colon2 prefix", node));
break;
}
return COMPILE_OK;
}
static int
compile_cpath(LINK_ANCHOR *const ret, rb_iseq_t *iseq, const NODE *cpath)
{
if (nd_type_p(cpath, NODE_COLON3)) {
/* toplevel class ::Foo */
ADD_INSN1(ret, cpath, putobject, rb_cObject);
return VM_DEFINECLASS_FLAG_SCOPED;
}
else if (nd_type_p(cpath, NODE_COLON2) && RNODE_COLON2(cpath)->nd_head) {
/* Bar::Foo */
NO_CHECK(COMPILE(ret, "nd_else->nd_head", RNODE_COLON2(cpath)->nd_head));
return VM_DEFINECLASS_FLAG_SCOPED;
}
else {
/* class at cbase Foo */
ADD_INSN1(ret, cpath, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
return 0;
}
}
static inline int
private_recv_p(const NODE *node)
{
NODE *recv = get_nd_recv(node);
if (recv && nd_type_p(recv, NODE_SELF)) {
return RNODE_SELF(recv)->nd_state != 0;
}
return 0;
}
static void
defined_expr(rb_iseq_t *iseq, LINK_ANCHOR *const ret,
const NODE *const node, LABEL **lfinish, VALUE needstr);
static int
compile_call(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, const enum node_type type, const NODE *const line_node, int popped, bool assume_receiver);
static void
defined_expr0(rb_iseq_t *iseq, LINK_ANCHOR *const ret,
const NODE *const node, LABEL **lfinish, VALUE needstr,
bool keep_result)
{
enum defined_type expr_type = DEFINED_NOT_DEFINED;
enum node_type type;
const int line = nd_line(node);
const NODE *line_node = node;
switch (type = nd_type(node)) {
/* easy literals */
case NODE_NIL:
expr_type = DEFINED_NIL;
break;
case NODE_SELF:
expr_type = DEFINED_SELF;
break;
case NODE_TRUE:
expr_type = DEFINED_TRUE;
break;
case NODE_FALSE:
expr_type = DEFINED_FALSE;
break;
case NODE_LIST:{
const NODE *vals = node;
do {
defined_expr0(iseq, ret, RNODE_LIST(vals)->nd_head, lfinish, Qfalse, false);
if (!lfinish[1]) {
lfinish[1] = NEW_LABEL(line);
}
ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
} while ((vals = RNODE_LIST(vals)->nd_next) != NULL);
}
/* fall through */
case NODE_STR:
case NODE_LIT:
case NODE_ZLIST:
case NODE_AND:
case NODE_OR:
default:
expr_type = DEFINED_EXPR;
break;
/* variables */
case NODE_LVAR:
case NODE_DVAR:
expr_type = DEFINED_LVAR;
break;
#define PUSH_VAL(type) (needstr == Qfalse ? Qtrue : rb_iseq_defined_string(type))
case NODE_IVAR:
ADD_INSN3(ret, line_node, definedivar,
ID2SYM(RNODE_IVAR(node)->nd_vid), get_ivar_ic_value(iseq,RNODE_IVAR(node)->nd_vid), PUSH_VAL(DEFINED_IVAR));
return;
case NODE_GVAR:
ADD_INSN(ret, line_node, putnil);
ADD_INSN3(ret, line_node, defined, INT2FIX(DEFINED_GVAR),
ID2SYM(RNODE_GVAR(node)->nd_vid), PUSH_VAL(DEFINED_GVAR));
return;
case NODE_CVAR:
ADD_INSN(ret, line_node, putnil);
ADD_INSN3(ret, line_node, defined, INT2FIX(DEFINED_CVAR),
ID2SYM(RNODE_CVAR(node)->nd_vid), PUSH_VAL(DEFINED_CVAR));
return;
case NODE_CONST:
ADD_INSN(ret, line_node, putnil);
ADD_INSN3(ret, line_node, defined, INT2FIX(DEFINED_CONST),
ID2SYM(RNODE_CONST(node)->nd_vid), PUSH_VAL(DEFINED_CONST));
return;
case NODE_COLON2:
if (!lfinish[1]) {
lfinish[1] = NEW_LABEL(line);
}
defined_expr0(iseq, ret, RNODE_COLON2(node)->nd_head, lfinish, Qfalse, false);
ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
NO_CHECK(COMPILE(ret, "defined/colon2#nd_head", RNODE_COLON2(node)->nd_head));
if (rb_is_const_id(RNODE_COLON2(node)->nd_mid)) {
ADD_INSN3(ret, line_node, defined, INT2FIX(DEFINED_CONST_FROM),
ID2SYM(RNODE_COLON2(node)->nd_mid), PUSH_VAL(DEFINED_CONST));
}
else {
ADD_INSN3(ret, line_node, defined, INT2FIX(DEFINED_METHOD),
ID2SYM(RNODE_COLON2(node)->nd_mid), PUSH_VAL(DEFINED_METHOD));
}
return;
case NODE_COLON3:
ADD_INSN1(ret, line_node, putobject, rb_cObject);
ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_CONST_FROM), ID2SYM(RNODE_COLON3(node)->nd_mid), PUSH_VAL(DEFINED_CONST));
return;
/* method dispatch */
case NODE_CALL:
case NODE_OPCALL:
case NODE_VCALL:
case NODE_FCALL:
case NODE_ATTRASGN:{
const int explicit_receiver =
(type == NODE_CALL || type == NODE_OPCALL ||
(type == NODE_ATTRASGN && !private_recv_p(node)));
if (get_nd_args(node) || explicit_receiver) {
if (!lfinish[1]) {
lfinish[1] = NEW_LABEL(line);
}
if (!lfinish[2]) {
lfinish[2] = NEW_LABEL(line);
}
}
if (get_nd_args(node)) {
defined_expr0(iseq, ret, get_nd_args(node), lfinish, Qfalse, false);
ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
}
if (explicit_receiver) {
defined_expr0(iseq, ret, get_nd_recv(node), lfinish, Qfalse, true);
switch (nd_type(get_nd_recv(node))) {
case NODE_CALL:
case NODE_OPCALL:
case NODE_VCALL:
case NODE_FCALL:
case NODE_ATTRASGN:
ADD_INSNL(ret, line_node, branchunless, lfinish[2]);
compile_call(iseq, ret, get_nd_recv(node), nd_type(get_nd_recv(node)), line_node, 0, true);
break;
default:
ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
NO_CHECK(COMPILE(ret, "defined/recv", get_nd_recv(node)));
break;
}
if (keep_result) {
ADD_INSN(ret, line_node, dup);
}
ADD_INSN3(ret, line_node, defined, INT2FIX(DEFINED_METHOD),
ID2SYM(get_node_call_nd_mid(node)), PUSH_VAL(DEFINED_METHOD));
}
else {
ADD_INSN(ret, line_node, putself);
if (keep_result) {
ADD_INSN(ret, line_node, dup);
}
ADD_INSN3(ret, line_node, defined, INT2FIX(DEFINED_FUNC),
ID2SYM(get_node_call_nd_mid(node)), PUSH_VAL(DEFINED_METHOD));
}
return;
}
case NODE_YIELD:
ADD_INSN(ret, line_node, putnil);
ADD_INSN3(ret, line_node, defined, INT2FIX(DEFINED_YIELD), 0,
PUSH_VAL(DEFINED_YIELD));
return;
case NODE_BACK_REF:
case NODE_NTH_REF:
ADD_INSN(ret, line_node, putnil);
ADD_INSN3(ret, line_node, defined, INT2FIX(DEFINED_REF),
INT2FIX((RNODE_BACK_REF(node)->nd_nth << 1) | (type == NODE_BACK_REF)),
PUSH_VAL(DEFINED_GVAR));
return;
case NODE_SUPER:
case NODE_ZSUPER:
ADD_INSN(ret, line_node, putnil);
ADD_INSN3(ret, line_node, defined, INT2FIX(DEFINED_ZSUPER), 0,
PUSH_VAL(DEFINED_ZSUPER));
return;
#undef PUSH_VAL
case NODE_OP_ASGN1:
case NODE_OP_ASGN2:
case NODE_OP_ASGN_OR:
case NODE_OP_ASGN_AND:
case NODE_MASGN:
case NODE_LASGN:
case NODE_DASGN:
case NODE_GASGN:
case NODE_IASGN:
case NODE_CDECL:
case NODE_CVASGN:
expr_type = DEFINED_ASGN;
break;
}
assert(expr_type != DEFINED_NOT_DEFINED);
if (needstr != Qfalse) {
VALUE str = rb_iseq_defined_string(expr_type);
ADD_INSN1(ret, line_node, putobject, str);
}
else {
ADD_INSN1(ret, line_node, putobject, Qtrue);
}
}
static void
build_defined_rescue_iseq(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const void *unused)
{
NODE dummy_line_node = generate_dummy_line_node(0, -1);
ADD_INSN(ret, &dummy_line_node, putnil);
iseq_set_exception_local_table(iseq);
}
static void
defined_expr(rb_iseq_t *iseq, LINK_ANCHOR *const ret,
const NODE *const node, LABEL **lfinish, VALUE needstr)
{
LINK_ELEMENT *lcur = ret->last;
defined_expr0(iseq, ret, node, lfinish, needstr, false);
if (lfinish[1]) {
int line = nd_line(node);
LABEL *lstart = NEW_LABEL(line);
LABEL *lend = NEW_LABEL(line);
const rb_iseq_t *rescue;
struct rb_iseq_new_with_callback_callback_func *ifunc =
rb_iseq_new_with_callback_new_callback(build_defined_rescue_iseq, NULL);
rescue = new_child_iseq_with_callback(iseq, ifunc,
rb_str_concat(rb_str_new2("defined guard in "),
ISEQ_BODY(iseq)->location.label),
iseq, ISEQ_TYPE_RESCUE, 0);
lstart->rescued = LABEL_RESCUE_BEG;
lend->rescued = LABEL_RESCUE_END;
APPEND_LABEL(ret, lcur, lstart);
ADD_LABEL(ret, lend);
ADD_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue, lfinish[1]);
}
}
static int
compile_defined_expr(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, VALUE needstr)
{
const int line = nd_line(node);
const NODE *line_node = node;
if (!RNODE_DEFINED(node)->nd_head) {
VALUE str = rb_iseq_defined_string(DEFINED_NIL);
ADD_INSN1(ret, line_node, putobject, str);
}
else {
LABEL *lfinish[3];
LINK_ELEMENT *last = ret->last;
lfinish[0] = NEW_LABEL(line);
lfinish[1] = 0;
lfinish[2] = 0;
defined_expr(iseq, ret, RNODE_DEFINED(node)->nd_head, lfinish, needstr);
if (lfinish[1]) {
ELEM_INSERT_NEXT(last, &new_insn_body(iseq, line_node, BIN(putnil), 0)->link);
ADD_INSN(ret, line_node, swap);
if (lfinish[2]) {
ADD_LABEL(ret, lfinish[2]);
}
ADD_INSN(ret, line_node, pop);
ADD_LABEL(ret, lfinish[1]);
}
ADD_LABEL(ret, lfinish[0]);
}
return COMPILE_OK;
}
static VALUE
make_name_for_block(const rb_iseq_t *orig_iseq)
{
int level = 1;
const rb_iseq_t *iseq = orig_iseq;
if (ISEQ_BODY(orig_iseq)->parent_iseq != 0) {
while (ISEQ_BODY(orig_iseq)->local_iseq != iseq) {
if (ISEQ_BODY(iseq)->type == ISEQ_TYPE_BLOCK) {
level++;
}
iseq = ISEQ_BODY(iseq)->parent_iseq;
}
}
if (level == 1) {
return rb_sprintf("block in %"PRIsVALUE, ISEQ_BODY(iseq)->location.label);
}
else {
return rb_sprintf("block (%d levels) in %"PRIsVALUE, level, ISEQ_BODY(iseq)->location.label);
}
}
static void
push_ensure_entry(rb_iseq_t *iseq,
struct iseq_compile_data_ensure_node_stack *enl,
struct ensure_range *er, const void *const node)
{
enl->ensure_node = node;
enl->prev = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack; /* prev */
enl->erange = er;
ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enl;
}
static void
add_ensure_range(rb_iseq_t *iseq, struct ensure_range *erange,
LABEL *lstart, LABEL *lend)
{
struct ensure_range *ne =
compile_data_alloc(iseq, sizeof(struct ensure_range));
while (erange->next != 0) {
erange = erange->next;
}
ne->next = 0;
ne->begin = lend;
ne->end = erange->end;
erange->end = lstart;
erange->next = ne;
}
static bool
can_add_ensure_iseq(const rb_iseq_t *iseq)
{
struct iseq_compile_data_ensure_node_stack *e;
if (ISEQ_COMPILE_DATA(iseq)->in_rescue && (e = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack) != NULL) {
while (e) {
if (e->ensure_node) return false;
e = e->prev;
}
}
return true;
}
static void
add_ensure_iseq(LINK_ANCHOR *const ret, rb_iseq_t *iseq, int is_return)
{
assert(can_add_ensure_iseq(iseq));
struct iseq_compile_data_ensure_node_stack *enlp =
ISEQ_COMPILE_DATA(iseq)->ensure_node_stack;
struct iseq_compile_data_ensure_node_stack *prev_enlp = enlp;
DECL_ANCHOR(ensure);
INIT_ANCHOR(ensure);
while (enlp) {
if (enlp->erange != NULL) {
DECL_ANCHOR(ensure_part);
LABEL *lstart = NEW_LABEL(0);
LABEL *lend = NEW_LABEL(0);
INIT_ANCHOR(ensure_part);
add_ensure_range(iseq, enlp->erange, lstart, lend);
ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enlp->prev;
ADD_LABEL(ensure_part, lstart);
NO_CHECK(COMPILE_POPPED(ensure_part, "ensure part", enlp->ensure_node));
ADD_LABEL(ensure_part, lend);
ADD_SEQ(ensure, ensure_part);
}
else {
if (!is_return) {
break;
}
}
enlp = enlp->prev;
}
ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = prev_enlp;
ADD_SEQ(ret, ensure);
}
#if RUBY_DEBUG
static int
check_keyword(const NODE *node)
{
/* This check is essentially a code clone of compile_keyword_arg. */
if (nd_type_p(node, NODE_LIST)) {
while (RNODE_LIST(node)->nd_next) {
node = RNODE_LIST(node)->nd_next;
}
node = RNODE_LIST(node)->nd_head;
}
return keyword_node_p(node);
}
#endif
static bool
keyword_node_single_splat_p(NODE *kwnode)
{
RUBY_ASSERT(keyword_node_p(kwnode));
NODE *node = RNODE_HASH(kwnode)->nd_head;
return RNODE_LIST(node)->nd_head == NULL &&
RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next == NULL;
}
static int
setup_args_core(rb_iseq_t *iseq, LINK_ANCHOR *const args, const NODE *argn,
int dup_rest, unsigned int *flag_ptr, struct rb_callinfo_kwarg **kwarg_ptr)
{
if (!argn) return 0;
NODE *kwnode = NULL;
switch (nd_type(argn)) {
case NODE_LIST: {
// f(x, y, z)
int len = compile_args(iseq, args, argn, &kwnode);
RUBY_ASSERT(flag_ptr == NULL || (*flag_ptr & VM_CALL_ARGS_SPLAT) == 0);
if (kwnode) {
if (compile_keyword_arg(iseq, args, kwnode, kwarg_ptr, flag_ptr)) {
len -= 1;
}
else {
compile_hash(iseq, args, kwnode, TRUE, FALSE);
}
}
return len;
}
case NODE_SPLAT: {
// f(*a)
NO_CHECK(COMPILE(args, "args (splat)", RNODE_SPLAT(argn)->nd_head));
ADD_INSN1(args, argn, splatarray, RBOOL(dup_rest));
if (flag_ptr) *flag_ptr |= VM_CALL_ARGS_SPLAT;
RUBY_ASSERT(flag_ptr == NULL || (*flag_ptr & VM_CALL_KW_SPLAT) == 0);
return 1;
}
case NODE_ARGSCAT: {
if (flag_ptr) *flag_ptr |= VM_CALL_ARGS_SPLAT;
int argc = setup_args_core(iseq, args, RNODE_ARGSCAT(argn)->nd_head, 1, NULL, NULL);
if (nd_type_p(RNODE_ARGSCAT(argn)->nd_body, NODE_LIST)) {
int rest_len = compile_args(iseq, args, RNODE_ARGSCAT(argn)->nd_body, &kwnode);
if (kwnode) rest_len--;
ADD_INSN1(args, argn, newarray, INT2FIX(rest_len));
}
else {
RUBY_ASSERT(!check_keyword(RNODE_ARGSCAT(argn)->nd_body));
NO_CHECK(COMPILE(args, "args (cat: splat)", RNODE_ARGSCAT(argn)->nd_body));
}
if (nd_type_p(RNODE_ARGSCAT(argn)->nd_head, NODE_LIST)) {
ADD_INSN1(args, argn, splatarray, Qtrue);
argc += 1;
}
else {
ADD_INSN1(args, argn, splatarray, Qfalse);
ADD_INSN(args, argn, concatarray);
}
// f(..., *a, ..., k1:1, ...) #=> f(..., *[*a, ...], **{k1:1, ...})
if (kwnode) {
// kwsplat
*flag_ptr |= VM_CALL_KW_SPLAT;
compile_hash(iseq, args, kwnode, TRUE, FALSE);
argc += 1;
}
return argc;
}
case NODE_ARGSPUSH: {
if (flag_ptr) *flag_ptr |= VM_CALL_ARGS_SPLAT;
int argc = setup_args_core(iseq, args, RNODE_ARGSPUSH(argn)->nd_head, 1, NULL, NULL);
if (nd_type_p(RNODE_ARGSPUSH(argn)->nd_body, NODE_LIST)) {
int rest_len = compile_args(iseq, args, RNODE_ARGSPUSH(argn)->nd_body, &kwnode);
if (kwnode) rest_len--;
ADD_INSN1(args, argn, newarray, INT2FIX(rest_len));
ADD_INSN1(args, argn, newarray, INT2FIX(1));
ADD_INSN(args, argn, concatarray);
}
else {
if (keyword_node_p(RNODE_ARGSPUSH(argn)->nd_body)) {
kwnode = RNODE_ARGSPUSH(argn)->nd_body;
}
else {
NO_CHECK(COMPILE(args, "args (cat: splat)", RNODE_ARGSPUSH(argn)->nd_body));
ADD_INSN1(args, argn, newarray, INT2FIX(1));
ADD_INSN(args, argn, concatarray);
}
}
if (kwnode) {
// f(*a, k:1)
*flag_ptr |= VM_CALL_KW_SPLAT;
if (!keyword_node_single_splat_p(kwnode)) {
*flag_ptr |= VM_CALL_KW_SPLAT_MUT;
}
compile_hash(iseq, args, kwnode, TRUE, FALSE);
argc += 1;
}
return argc;
}
default: {
UNKNOWN_NODE("setup_arg", argn, Qnil);
}
}
}
static VALUE
setup_args(rb_iseq_t *iseq, LINK_ANCHOR *const args, const NODE *argn,
unsigned int *flag, struct rb_callinfo_kwarg **keywords)
{
VALUE ret;
if (argn && nd_type_p(argn, NODE_BLOCK_PASS)) {
unsigned int dup_rest = 1;
DECL_ANCHOR(arg_block);
INIT_ANCHOR(arg_block);
NO_CHECK(COMPILE(arg_block, "block", RNODE_BLOCK_PASS(argn)->nd_body));
*flag |= VM_CALL_ARGS_BLOCKARG;
if (LIST_INSN_SIZE_ONE(arg_block)) {
LINK_ELEMENT *elem = FIRST_ELEMENT(arg_block);
if (IS_INSN(elem)) {
INSN *iobj = (INSN *)elem;
if (iobj->insn_id == BIN(getblockparam)) {
iobj->insn_id = BIN(getblockparamproxy);
}
dup_rest = 0;
}
}
ret = INT2FIX(setup_args_core(iseq, args, RNODE_BLOCK_PASS(argn)->nd_head, dup_rest, flag, keywords));
ADD_SEQ(args, arg_block);
}
else {
ret = INT2FIX(setup_args_core(iseq, args, argn, 0, flag, keywords));
}
return ret;
}
static void
build_postexe_iseq(rb_iseq_t *iseq, LINK_ANCHOR *ret, const void *ptr)
{
const NODE *body = ptr;
int line = nd_line(body);
VALUE argc = INT2FIX(0);
const rb_iseq_t *block = NEW_CHILD_ISEQ(body, make_name_for_block(ISEQ_BODY(iseq)->parent_iseq), ISEQ_TYPE_BLOCK, line);
ADD_INSN1(ret, body, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
ADD_CALL_WITH_BLOCK(ret, body, id_core_set_postexe, argc, block);
RB_OBJ_WRITTEN(iseq, Qundef, (VALUE)block);
iseq_set_local_table(iseq, 0);
}
static void
compile_named_capture_assign(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node)
{
const NODE *vars;
LINK_ELEMENT *last;
int line = nd_line(node);
const NODE *line_node = node;
LABEL *fail_label = NEW_LABEL(line), *end_label = NEW_LABEL(line);
#if !(defined(NAMED_CAPTURE_BY_SVAR) && NAMED_CAPTURE_BY_SVAR-0)
ADD_INSN1(ret, line_node, getglobal, ID2SYM(idBACKREF));
#else
ADD_INSN2(ret, line_node, getspecial, INT2FIX(1) /* '~' */, INT2FIX(0));
#endif
ADD_INSN(ret, line_node, dup);
ADD_INSNL(ret, line_node, branchunless, fail_label);
for (vars = node; vars; vars = RNODE_BLOCK(vars)->nd_next) {
INSN *cap;
if (RNODE_BLOCK(vars)->nd_next) {
ADD_INSN(ret, line_node, dup);
}
last = ret->last;
NO_CHECK(COMPILE_POPPED(ret, "capture", RNODE_BLOCK(vars)->nd_head));
last = last->next; /* putobject :var */
cap = new_insn_send(iseq, line_node, idAREF, INT2FIX(1),
NULL, INT2FIX(0), NULL);
ELEM_INSERT_PREV(last->next, (LINK_ELEMENT *)cap);
#if !defined(NAMED_CAPTURE_SINGLE_OPT) || NAMED_CAPTURE_SINGLE_OPT-0
if (!RNODE_BLOCK(vars)->nd_next && vars == node) {
/* only one name */
DECL_ANCHOR(nom);
INIT_ANCHOR(nom);
ADD_INSNL(nom, line_node, jump, end_label);
ADD_LABEL(nom, fail_label);
# if 0 /* $~ must be MatchData or nil */
ADD_INSN(nom, line_node, pop);
ADD_INSN(nom, line_node, putnil);
# endif
ADD_LABEL(nom, end_label);
(nom->last->next = cap->link.next)->prev = nom->last;
(cap->link.next = nom->anchor.next)->prev = &cap->link;
return;
}
#endif
}
ADD_INSNL(ret, line_node, jump, end_label);
ADD_LABEL(ret, fail_label);
ADD_INSN(ret, line_node, pop);
for (vars = node; vars; vars = RNODE_BLOCK(vars)->nd_next) {
last = ret->last;
NO_CHECK(COMPILE_POPPED(ret, "capture", RNODE_BLOCK(vars)->nd_head));
last = last->next; /* putobject :var */
((INSN*)last)->insn_id = BIN(putnil);
((INSN*)last)->operand_size = 0;
}
ADD_LABEL(ret, end_label);
}
static int
optimizable_range_item_p(const NODE *n)
{
if (!n) return FALSE;
switch (nd_type(n)) {
case NODE_LIT:
return RB_INTEGER_TYPE_P(RNODE_LIT(n)->nd_lit);
case NODE_NIL:
return TRUE;
default:
return FALSE;
}
}
static int
compile_if(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, int popped, const enum node_type type)
{
const NODE *const node_body = type == NODE_IF ? RNODE_IF(node)->nd_body : RNODE_UNLESS(node)->nd_else;
const NODE *const node_else = type == NODE_IF ? RNODE_IF(node)->nd_else : RNODE_UNLESS(node)->nd_body;
const int line = nd_line(node);
const NODE *line_node = node;
DECL_ANCHOR(cond_seq);
LABEL *then_label, *else_label, *end_label;
VALUE branches = Qfalse;
INIT_ANCHOR(cond_seq);
then_label = NEW_LABEL(line);
else_label = NEW_LABEL(line);
end_label = 0;
compile_branch_condition(iseq, cond_seq, RNODE_IF(node)->nd_cond, then_label, else_label);
ADD_SEQ(ret, cond_seq);
if (then_label->refcnt && else_label->refcnt) {
branches = decl_branch_base(iseq, node, type == NODE_IF ? "if" : "unless");
}
if (then_label->refcnt) {
ADD_LABEL(ret, then_label);
DECL_ANCHOR(then_seq);
INIT_ANCHOR(then_seq);
CHECK(COMPILE_(then_seq, "then", node_body, popped));
if (else_label->refcnt) {
add_trace_branch_coverage(
iseq,
ret,
node_body ? node_body : node,
0,
type == NODE_IF ? "then" : "else",
branches);
end_label = NEW_LABEL(line);
ADD_INSNL(then_seq, line_node, jump, end_label);
if (!popped) {
ADD_INSN(then_seq, line_node, pop);
}
}
ADD_SEQ(ret, then_seq);
}
if (else_label->refcnt) {
ADD_LABEL(ret, else_label);
DECL_ANCHOR(else_seq);
INIT_ANCHOR(else_seq);
CHECK(COMPILE_(else_seq, "else", node_else, popped));
if (then_label->refcnt) {
add_trace_branch_coverage(
iseq,
ret,
node_else ? node_else : node,
1,
type == NODE_IF ? "else" : "then",
branches);
}
ADD_SEQ(ret, else_seq);
}
if (end_label) {
ADD_LABEL(ret, end_label);
}
return COMPILE_OK;
}
static int
compile_case(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const orig_node, int popped)
{
const NODE *vals;
const NODE *node = orig_node;
LABEL *endlabel, *elselabel;
DECL_ANCHOR(head);
DECL_ANCHOR(body_seq);
DECL_ANCHOR(cond_seq);
int only_special_literals = 1;
VALUE literals = rb_hash_new();
int line;
enum node_type type;
const NODE *line_node;
VALUE branches = Qfalse;
int branch_id = 0;
INIT_ANCHOR(head);
INIT_ANCHOR(body_seq);
INIT_ANCHOR(cond_seq);
RHASH_TBL_RAW(literals)->type = &cdhash_type;
CHECK(COMPILE(head, "case base", RNODE_CASE(node)->nd_head));
branches = decl_branch_base(iseq, node, "case");
node = RNODE_CASE(node)->nd_body;
EXPECT_NODE("NODE_CASE", node, NODE_WHEN, COMPILE_NG);
type = nd_type(node);
line = nd_line(node);
line_node = node;
endlabel = NEW_LABEL(line);
elselabel = NEW_LABEL(line);
ADD_SEQ(ret, head); /* case VAL */
while (type == NODE_WHEN) {
LABEL *l1;
l1 = NEW_LABEL(line);
ADD_LABEL(body_seq, l1);
ADD_INSN(body_seq, line_node, pop);
add_trace_branch_coverage(
iseq,
body_seq,
RNODE_WHEN(node)->nd_body ? RNODE_WHEN(node)->nd_body : node,
branch_id++,
"when",
branches);
CHECK(COMPILE_(body_seq, "when body", RNODE_WHEN(node)->nd_body, popped));
ADD_INSNL(body_seq, line_node, jump, endlabel);
vals = RNODE_WHEN(node)->nd_head;
if (vals) {
switch (nd_type(vals)) {
case NODE_LIST:
only_special_literals = when_vals(iseq, cond_seq, vals, l1, only_special_literals, literals);
if (only_special_literals < 0) return COMPILE_NG;
break;
case NODE_SPLAT:
case NODE_ARGSCAT:
case NODE_ARGSPUSH:
only_special_literals = 0;
CHECK(when_splat_vals(iseq, cond_seq, vals, l1, only_special_literals, literals));
break;
default:
UNKNOWN_NODE("NODE_CASE", vals, COMPILE_NG);
}
}
else {
EXPECT_NODE_NONULL("NODE_CASE", node, NODE_LIST, COMPILE_NG);
}
node = RNODE_WHEN(node)->nd_next;
if (!node) {
break;
}
type = nd_type(node);
line = nd_line(node);
line_node = node;
}
/* else */
if (node) {
ADD_LABEL(cond_seq, elselabel);
ADD_INSN(cond_seq, line_node, pop);
add_trace_branch_coverage(iseq, cond_seq, node, branch_id, "else", branches);
CHECK(COMPILE_(cond_seq, "else", node, popped));
ADD_INSNL(cond_seq, line_node, jump, endlabel);
}
else {
debugs("== else (implicit)\n");
ADD_LABEL(cond_seq, elselabel);
ADD_INSN(cond_seq, orig_node, pop);
add_trace_branch_coverage(iseq, cond_seq, orig_node, branch_id, "else", branches);
if (!popped) {
ADD_INSN(cond_seq, orig_node, putnil);
}
ADD_INSNL(cond_seq, orig_node, jump, endlabel);
}
if (only_special_literals && ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
ADD_INSN(ret, orig_node, dup);
ADD_INSN2(ret, orig_node, opt_case_dispatch, literals, elselabel);
RB_OBJ_WRITTEN(iseq, Qundef, literals);
LABEL_REF(elselabel);
}
ADD_SEQ(ret, cond_seq);
ADD_SEQ(ret, body_seq);
ADD_LABEL(ret, endlabel);
return COMPILE_OK;
}
static int
compile_case2(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const orig_node, int popped)
{
const NODE *vals;
const NODE *val;
const NODE *node = RNODE_CASE2(orig_node)->nd_body;
LABEL *endlabel;
DECL_ANCHOR(body_seq);
VALUE branches = Qfalse;
int branch_id = 0;
branches = decl_branch_base(iseq, orig_node, "case");
INIT_ANCHOR(body_seq);
endlabel = NEW_LABEL(nd_line(node));
while (node && nd_type_p(node, NODE_WHEN)) {
const int line = nd_line(node);
LABEL *l1 = NEW_LABEL(line);
ADD_LABEL(body_seq, l1);
add_trace_branch_coverage(
iseq,
body_seq,
RNODE_WHEN(node)->nd_body ? RNODE_WHEN(node)->nd_body : node,
branch_id++,
"when",
branches);
CHECK(COMPILE_(body_seq, "when", RNODE_WHEN(node)->nd_body, popped));
ADD_INSNL(body_seq, node, jump, endlabel);
vals = RNODE_WHEN(node)->nd_head;
if (!vals) {
EXPECT_NODE_NONULL("NODE_WHEN", node, NODE_LIST, COMPILE_NG);
}
switch (nd_type(vals)) {
case NODE_LIST:
while (vals) {
LABEL *lnext;
val = RNODE_LIST(vals)->nd_head;
lnext = NEW_LABEL(nd_line(val));
debug_compile("== when2\n", (void)0);
CHECK(compile_branch_condition(iseq, ret, val, l1, lnext));
ADD_LABEL(ret, lnext);
vals = RNODE_LIST(vals)->nd_next;
}
break;
case NODE_SPLAT:
case NODE_ARGSCAT:
case NODE_ARGSPUSH:
ADD_INSN(ret, vals, putnil);
CHECK(COMPILE(ret, "when2/cond splat", vals));
ADD_INSN1(ret, vals, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_WHEN | VM_CHECKMATCH_ARRAY));
ADD_INSNL(ret, vals, branchif, l1);
break;
default:
UNKNOWN_NODE("NODE_WHEN", vals, COMPILE_NG);
}
node = RNODE_WHEN(node)->nd_next;
}
/* else */
add_trace_branch_coverage(
iseq,
ret,
node ? node : orig_node,
branch_id,
"else",
branches);
CHECK(COMPILE_(ret, "else", node, popped));
ADD_INSNL(ret, orig_node, jump, endlabel);
ADD_SEQ(ret, body_seq);
ADD_LABEL(ret, endlabel);
return COMPILE_OK;
}
static int iseq_compile_pattern_match(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, LABEL *unmatched, bool in_single_pattern, bool in_alt_pattern, int base_index, bool use_deconstructed_cache);
static int iseq_compile_pattern_constant(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, LABEL *match_failed, bool in_single_pattern, int base_index);
static int iseq_compile_array_deconstruct(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, LABEL *deconstruct, LABEL *deconstructed, LABEL *match_failed, LABEL *type_error, bool in_single_pattern, int base_index, bool use_deconstructed_cache);
static int iseq_compile_pattern_set_general_errmsg(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, VALUE errmsg, int base_index);
static int iseq_compile_pattern_set_length_errmsg(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, VALUE errmsg, VALUE pattern_length, int base_index);
static int iseq_compile_pattern_set_eqq_errmsg(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, int base_index);
#define CASE3_BI_OFFSET_DECONSTRUCTED_CACHE 0
#define CASE3_BI_OFFSET_ERROR_STRING 1
#define CASE3_BI_OFFSET_KEY_ERROR_P 2
#define CASE3_BI_OFFSET_KEY_ERROR_MATCHEE 3
#define CASE3_BI_OFFSET_KEY_ERROR_KEY 4
static int
iseq_compile_pattern_each(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, LABEL *matched, LABEL *unmatched, bool in_single_pattern, bool in_alt_pattern, int base_index, bool use_deconstructed_cache)
{
const int line = nd_line(node);
const NODE *line_node = node;
switch (nd_type(node)) {
case NODE_ARYPTN: {
/*
* if pattern.use_rest_num?
* rest_num = 0
* end
* if pattern.has_constant_node?
* unless pattern.constant === obj
* goto match_failed
* end
* end
* unless obj.respond_to?(:deconstruct)
* goto match_failed
* end
* d = obj.deconstruct
* unless Array === d
* goto type_error
* end
* min_argc = pattern.pre_args_num + pattern.post_args_num
* if pattern.has_rest_arg?
* unless d.length >= min_argc
* goto match_failed
* end
* else
* unless d.length == min_argc
* goto match_failed
* end
* end
* pattern.pre_args_num.each do |i|
* unless pattern.pre_args[i].match?(d[i])
* goto match_failed
* end
* end
* if pattern.use_rest_num?
* rest_num = d.length - min_argc
* if pattern.has_rest_arg? && pattern.has_rest_arg_id # not `*`, but `*rest`
* unless pattern.rest_arg.match?(d[pattern.pre_args_num, rest_num])
* goto match_failed
* end
* end
* end
* pattern.post_args_num.each do |i|
* j = pattern.pre_args_num + i
* j += rest_num
* unless pattern.post_args[i].match?(d[j])
* goto match_failed
* end
* end
* goto matched
* type_error:
* FrozenCore.raise TypeError
* match_failed:
* goto unmatched
*/
const NODE *args = RNODE_ARYPTN(node)->pre_args;
const int pre_args_num = RNODE_ARYPTN(node)->pre_args ? rb_long2int(RNODE_LIST(RNODE_ARYPTN(node)->pre_args)->as.nd_alen) : 0;
const int post_args_num = RNODE_ARYPTN(node)->post_args ? rb_long2int(RNODE_LIST(RNODE_ARYPTN(node)->post_args)->as.nd_alen) : 0;
const int min_argc = pre_args_num + post_args_num;
const int use_rest_num = RNODE_ARYPTN(node)->rest_arg && (NODE_NAMED_REST_P(RNODE_ARYPTN(node)->rest_arg) ||
(!NODE_NAMED_REST_P(RNODE_ARYPTN(node)->rest_arg) && post_args_num > 0));
LABEL *match_failed, *type_error, *deconstruct, *deconstructed;
int i;
match_failed = NEW_LABEL(line);
type_error = NEW_LABEL(line);
deconstruct = NEW_LABEL(line);
deconstructed = NEW_LABEL(line);
if (use_rest_num) {
ADD_INSN1(ret, line_node, putobject, INT2FIX(0)); /* allocate stack for rest_num */
ADD_INSN(ret, line_node, swap);
if (base_index) {
base_index++;
}
}
CHECK(iseq_compile_pattern_constant(iseq, ret, node, match_failed, in_single_pattern, base_index));
CHECK(iseq_compile_array_deconstruct(iseq, ret, node, deconstruct, deconstructed, match_failed, type_error, in_single_pattern, base_index, use_deconstructed_cache));
ADD_INSN(ret, line_node, dup);
ADD_SEND(ret, line_node, idLength, INT2FIX(0));
ADD_INSN1(ret, line_node, putobject, INT2FIX(min_argc));
ADD_SEND(ret, line_node, RNODE_ARYPTN(node)->rest_arg ? idGE : idEq, INT2FIX(1)); // (1)
if (in_single_pattern) {
CHECK(iseq_compile_pattern_set_length_errmsg(iseq, ret, node,
RNODE_ARYPTN(node)->rest_arg ? rb_fstring_lit("%p length mismatch (given %p, expected %p+)") :
rb_fstring_lit("%p length mismatch (given %p, expected %p)"),
INT2FIX(min_argc), base_index + 1 /* (1) */));
}
ADD_INSNL(ret, line_node, branchunless, match_failed);
for (i = 0; i < pre_args_num; i++) {
ADD_INSN(ret, line_node, dup);
ADD_INSN1(ret, line_node, putobject, INT2FIX(i));
ADD_SEND(ret, line_node, idAREF, INT2FIX(1)); // (2)
CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_LIST(args)->nd_head, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 /* (2) */, false));
args = RNODE_LIST(args)->nd_next;
}
if (RNODE_ARYPTN(node)->rest_arg) {
if (NODE_NAMED_REST_P(RNODE_ARYPTN(node)->rest_arg)) {
ADD_INSN(ret, line_node, dup);
ADD_INSN1(ret, line_node, putobject, INT2FIX(pre_args_num));
ADD_INSN1(ret, line_node, topn, INT2FIX(1));
ADD_SEND(ret, line_node, idLength, INT2FIX(0));
ADD_INSN1(ret, line_node, putobject, INT2FIX(min_argc));
ADD_SEND(ret, line_node, idMINUS, INT2FIX(1));
ADD_INSN1(ret, line_node, setn, INT2FIX(4));
ADD_SEND(ret, line_node, idAREF, INT2FIX(2)); // (3)
CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_ARYPTN(node)->rest_arg, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 /* (3) */, false));
}
else {
if (post_args_num > 0) {
ADD_INSN(ret, line_node, dup);
ADD_SEND(ret, line_node, idLength, INT2FIX(0));
ADD_INSN1(ret, line_node, putobject, INT2FIX(min_argc));
ADD_SEND(ret, line_node, idMINUS, INT2FIX(1));
ADD_INSN1(ret, line_node, setn, INT2FIX(2));
ADD_INSN(ret, line_node, pop);
}
}
}
args = RNODE_ARYPTN(node)->post_args;
for (i = 0; i < post_args_num; i++) {
ADD_INSN(ret, line_node, dup);
ADD_INSN1(ret, line_node, putobject, INT2FIX(pre_args_num + i));
ADD_INSN1(ret, line_node, topn, INT2FIX(3));
ADD_SEND(ret, line_node, idPLUS, INT2FIX(1));
ADD_SEND(ret, line_node, idAREF, INT2FIX(1)); // (4)
CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_LIST(args)->nd_head, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 /* (4) */, false));
args = RNODE_LIST(args)->nd_next;
}
ADD_INSN(ret, line_node, pop);
if (use_rest_num) {
ADD_INSN(ret, line_node, pop);
}
ADD_INSNL(ret, line_node, jump, matched);
ADD_INSN(ret, line_node, putnil);
if (use_rest_num) {
ADD_INSN(ret, line_node, putnil);
}
ADD_LABEL(ret, type_error);
ADD_INSN1(ret, line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
ADD_INSN1(ret, line_node, putobject, rb_eTypeError);
ADD_INSN1(ret, line_node, putobject, rb_fstring_lit("deconstruct must return Array"));
ADD_SEND(ret, line_node, id_core_raise, INT2FIX(2));
ADD_INSN(ret, line_node, pop);
ADD_LABEL(ret, match_failed);
ADD_INSN(ret, line_node, pop);
if (use_rest_num) {
ADD_INSN(ret, line_node, pop);
}
ADD_INSNL(ret, line_node, jump, unmatched);
break;
}
case NODE_FNDPTN: {
/*
* if pattern.has_constant_node?
* unless pattern.constant === obj
* goto match_failed
* end
* end
* unless obj.respond_to?(:deconstruct)
* goto match_failed
* end
* d = obj.deconstruct
* unless Array === d
* goto type_error
* end
* unless d.length >= pattern.args_num
* goto match_failed
* end
*
* begin
* len = d.length
* limit = d.length - pattern.args_num
* i = 0
* while i <= limit
* if pattern.args_num.times.all? {|j| pattern.args[j].match?(d[i+j]) }
* if pattern.has_pre_rest_arg_id
* unless pattern.pre_rest_arg.match?(d[0, i])
* goto find_failed
* end
* end
* if pattern.has_post_rest_arg_id
* unless pattern.post_rest_arg.match?(d[i+pattern.args_num, len])
* goto find_failed
* end
* end
* goto find_succeeded
* end
* i+=1
* end
* find_failed:
* goto match_failed
* find_succeeded:
* end
*
* goto matched
* type_error:
* FrozenCore.raise TypeError
* match_failed:
* goto unmatched
*/
const NODE *args = RNODE_FNDPTN(node)->args;
const int args_num = RNODE_FNDPTN(node)->args ? rb_long2int(RNODE_LIST(RNODE_FNDPTN(node)->args)->as.nd_alen) : 0;
LABEL *match_failed, *type_error, *deconstruct, *deconstructed;
match_failed = NEW_LABEL(line);
type_error = NEW_LABEL(line);
deconstruct = NEW_LABEL(line);
deconstructed = NEW_LABEL(line);
CHECK(iseq_compile_pattern_constant(iseq, ret, node, match_failed, in_single_pattern, base_index));
CHECK(iseq_compile_array_deconstruct(iseq, ret, node, deconstruct, deconstructed, match_failed, type_error, in_single_pattern, base_index, use_deconstructed_cache));
ADD_INSN(ret, line_node, dup);
ADD_SEND(ret, line_node, idLength, INT2FIX(0));
ADD_INSN1(ret, line_node, putobject, INT2FIX(args_num));
ADD_SEND(ret, line_node, idGE, INT2FIX(1)); // (1)
if (in_single_pattern) {
CHECK(iseq_compile_pattern_set_length_errmsg(iseq, ret, node, rb_fstring_lit("%p length mismatch (given %p, expected %p+)"), INT2FIX(args_num), base_index + 1 /* (1) */));
}
ADD_INSNL(ret, line_node, branchunless, match_failed);
{
LABEL *while_begin = NEW_LABEL(nd_line(node));
LABEL *next_loop = NEW_LABEL(nd_line(node));
LABEL *find_succeeded = NEW_LABEL(line);
LABEL *find_failed = NEW_LABEL(nd_line(node));
int j;
ADD_INSN(ret, line_node, dup); /* allocate stack for len */
ADD_SEND(ret, line_node, idLength, INT2FIX(0)); // (2)
ADD_INSN(ret, line_node, dup); /* allocate stack for limit */
ADD_INSN1(ret, line_node, putobject, INT2FIX(args_num));
ADD_SEND(ret, line_node, idMINUS, INT2FIX(1)); // (3)
ADD_INSN1(ret, line_node, putobject, INT2FIX(0)); /* allocate stack for i */ // (4)
ADD_LABEL(ret, while_begin);
ADD_INSN(ret, line_node, dup);
ADD_INSN1(ret, line_node, topn, INT2FIX(2));
ADD_SEND(ret, line_node, idLE, INT2FIX(1));
ADD_INSNL(ret, line_node, branchunless, find_failed);
for (j = 0; j < args_num; j++) {
ADD_INSN1(ret, line_node, topn, INT2FIX(3));
ADD_INSN1(ret, line_node, topn, INT2FIX(1));
if (j != 0) {
ADD_INSN1(ret, line_node, putobject, INT2FIX(j));
ADD_SEND(ret, line_node, idPLUS, INT2FIX(1));
}
ADD_SEND(ret, line_node, idAREF, INT2FIX(1)); // (5)
CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_LIST(args)->nd_head, next_loop, in_single_pattern, in_alt_pattern, base_index + 4 /* (2), (3), (4), (5) */, false));
args = RNODE_LIST(args)->nd_next;
}
if (NODE_NAMED_REST_P(RNODE_FNDPTN(node)->pre_rest_arg)) {
ADD_INSN1(ret, line_node, topn, INT2FIX(3));
ADD_INSN1(ret, line_node, putobject, INT2FIX(0));
ADD_INSN1(ret, line_node, topn, INT2FIX(2));
ADD_SEND(ret, line_node, idAREF, INT2FIX(2)); // (6)
CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_FNDPTN(node)->pre_rest_arg, find_failed, in_single_pattern, in_alt_pattern, base_index + 4 /* (2), (3), (4), (6) */, false));
}
if (NODE_NAMED_REST_P(RNODE_FNDPTN(node)->post_rest_arg)) {
ADD_INSN1(ret, line_node, topn, INT2FIX(3));
ADD_INSN1(ret, line_node, topn, INT2FIX(1));
ADD_INSN1(ret, line_node, putobject, INT2FIX(args_num));
ADD_SEND(ret, line_node, idPLUS, INT2FIX(1));
ADD_INSN1(ret, line_node, topn, INT2FIX(3));
ADD_SEND(ret, line_node, idAREF, INT2FIX(2)); // (7)
CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_FNDPTN(node)->post_rest_arg, find_failed, in_single_pattern, in_alt_pattern, base_index + 4 /* (2), (3),(4), (7) */, false));
}
ADD_INSNL(ret, line_node, jump, find_succeeded);
ADD_LABEL(ret, next_loop);
ADD_INSN1(ret, line_node, putobject, INT2FIX(1));
ADD_SEND(ret, line_node, idPLUS, INT2FIX(1));
ADD_INSNL(ret, line_node, jump, while_begin);
ADD_LABEL(ret, find_failed);
ADD_INSN1(ret, line_node, adjuststack, INT2FIX(3));
if (in_single_pattern) {
ADD_INSN1(ret, line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
ADD_INSN1(ret, line_node, putobject, rb_fstring_lit("%p does not match to find pattern"));
ADD_INSN1(ret, line_node, topn, INT2FIX(2));
ADD_SEND(ret, line_node, id_core_sprintf, INT2FIX(2)); // (8)
ADD_INSN1(ret, line_node, setn, INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 /* (8) */)); // (9)
ADD_INSN1(ret, line_node, putobject, Qfalse);
ADD_INSN1(ret, line_node, setn, INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 /* (8), (9) */));
ADD_INSN(ret, line_node, pop);
ADD_INSN(ret, line_node, pop);
}
ADD_INSNL(ret, line_node, jump, match_failed);
ADD_INSN1(ret, line_node, dupn, INT2FIX(3));
ADD_LABEL(ret, find_succeeded);
ADD_INSN1(ret, line_node, adjuststack, INT2FIX(3));
}
ADD_INSN(ret, line_node, pop);
ADD_INSNL(ret, line_node, jump, matched);
ADD_INSN(ret, line_node, putnil);
ADD_LABEL(ret, type_error);
ADD_INSN1(ret, line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
ADD_INSN1(ret, line_node, putobject, rb_eTypeError);
ADD_INSN1(ret, line_node, putobject, rb_fstring_lit("deconstruct must return Array"));
ADD_SEND(ret, line_node, id_core_raise, INT2FIX(2));
ADD_INSN(ret, line_node, pop);
ADD_LABEL(ret, match_failed);
ADD_INSN(ret, line_node, pop);
ADD_INSNL(ret, line_node, jump, unmatched);
break;
}
case NODE_HSHPTN: {
/*
* keys = nil
* if pattern.has_kw_args_node? && !pattern.has_kw_rest_arg_node?
* keys = pattern.kw_args_node.keys
* end
* if pattern.has_constant_node?
* unless pattern.constant === obj
* goto match_failed
* end
* end
* unless obj.respond_to?(:deconstruct_keys)
* goto match_failed
* end
* d = obj.deconstruct_keys(keys)
* unless Hash === d
* goto type_error
* end
* if pattern.has_kw_rest_arg_node?
* d = d.dup
* end
* if pattern.has_kw_args_node?
* pattern.kw_args_node.each |k,|
* unless d.key?(k)
* goto match_failed
* end
* end
* pattern.kw_args_node.each |k, pat|
* if pattern.has_kw_rest_arg_node?
* unless pat.match?(d.delete(k))
* goto match_failed
* end
* else
* unless pat.match?(d[k])
* goto match_failed
* end
* end
* end
* else
* unless d.empty?
* goto match_failed
* end
* end
* if pattern.has_kw_rest_arg_node?
* if pattern.no_rest_keyword?
* unless d.empty?
* goto match_failed
* end
* else
* unless pattern.kw_rest_arg_node.match?(d)
* goto match_failed
* end
* end
* end
* goto matched
* type_error:
* FrozenCore.raise TypeError
* match_failed:
* goto unmatched
*/
LABEL *match_failed, *type_error;
VALUE keys = Qnil;
match_failed = NEW_LABEL(line);
type_error = NEW_LABEL(line);
if (RNODE_HSHPTN(node)->nd_pkwargs && !RNODE_HSHPTN(node)->nd_pkwrestarg) {
const NODE *kw_args = RNODE_HASH(RNODE_HSHPTN(node)->nd_pkwargs)->nd_head;
keys = rb_ary_new_capa(kw_args ? RNODE_LIST(kw_args)->as.nd_alen/2 : 0);
while (kw_args) {
rb_ary_push(keys, RNODE_LIT(RNODE_LIST(kw_args)->nd_head)->nd_lit);
kw_args = RNODE_LIST(RNODE_LIST(kw_args)->nd_next)->nd_next;
}
}
CHECK(iseq_compile_pattern_constant(iseq, ret, node, match_failed, in_single_pattern, base_index));
ADD_INSN(ret, line_node, dup);
ADD_INSN1(ret, line_node, putobject, ID2SYM(rb_intern("deconstruct_keys")));
ADD_SEND(ret, line_node, idRespond_to, INT2FIX(1)); // (1)
if (in_single_pattern) {
CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit("%p does not respond to #deconstruct_keys"), base_index + 1 /* (1) */));
}
ADD_INSNL(ret, line_node, branchunless, match_failed);
if (NIL_P(keys)) {
ADD_INSN(ret, line_node, putnil);
}
else {
ADD_INSN1(ret, line_node, duparray, keys);
RB_OBJ_WRITTEN(iseq, Qundef, rb_obj_hide(keys));
}
ADD_SEND(ret, line_node, rb_intern("deconstruct_keys"), INT2FIX(1)); // (2)
ADD_INSN(ret, line_node, dup);
ADD_INSN1(ret, line_node, checktype, INT2FIX(T_HASH));
ADD_INSNL(ret, line_node, branchunless, type_error);
if (RNODE_HSHPTN(node)->nd_pkwrestarg) {
ADD_SEND(ret, line_node, rb_intern("dup"), INT2FIX(0));
}
if (RNODE_HSHPTN(node)->nd_pkwargs) {
int i;
int keys_num;
const NODE *args;
args = RNODE_HASH(RNODE_HSHPTN(node)->nd_pkwargs)->nd_head;
if (args) {
DECL_ANCHOR(match_values);
INIT_ANCHOR(match_values);
keys_num = rb_long2int(RNODE_LIST(args)->as.nd_alen) / 2;
for (i = 0; i < keys_num; i++) {
NODE *key_node = RNODE_LIST(args)->nd_head;
NODE *value_node = RNODE_LIST(RNODE_LIST(args)->nd_next)->nd_head;
VALUE key;
if (!nd_type_p(key_node, NODE_LIT)) {
UNKNOWN_NODE("NODE_IN", key_node, COMPILE_NG);
}
key = RNODE_LIT(key_node)->nd_lit;
ADD_INSN(ret, line_node, dup);
ADD_INSN1(ret, line_node, putobject, key);
ADD_SEND(ret, line_node, rb_intern("key?"), INT2FIX(1)); // (3)
if (in_single_pattern) {
LABEL *match_succeeded;
match_succeeded = NEW_LABEL(line);
ADD_INSN(ret, line_node, dup);
ADD_INSNL(ret, line_node, branchif, match_succeeded);
ADD_INSN1(ret, line_node, putobject, rb_str_freeze(rb_sprintf("key not found: %+"PRIsVALUE, key))); // (4)
ADD_INSN1(ret, line_node, setn, INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 2 /* (3), (4) */));
ADD_INSN1(ret, line_node, putobject, Qtrue); // (5)
ADD_INSN1(ret, line_node, setn, INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 3 /* (3), (4), (5) */));
ADD_INSN1(ret, line_node, topn, INT2FIX(3)); // (6)
ADD_INSN1(ret, line_node, setn, INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_MATCHEE + 4 /* (3), (4), (5), (6) */));
ADD_INSN1(ret, line_node, putobject, key); // (7)
ADD_INSN1(ret, line_node, setn, INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_KEY + 5 /* (3), (4), (5), (6), (7) */));
ADD_INSN1(ret, line_node, adjuststack, INT2FIX(4));
ADD_LABEL(ret, match_succeeded);
}
ADD_INSNL(ret, line_node, branchunless, match_failed);
ADD_INSN(match_values, line_node, dup);
ADD_INSN1(match_values, line_node, putobject, key);
ADD_SEND(match_values, line_node, RNODE_HSHPTN(node)->nd_pkwrestarg ? rb_intern("delete") : idAREF, INT2FIX(1)); // (8)
CHECK(iseq_compile_pattern_match(iseq, match_values, value_node, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 /* (8) */, false));
args = RNODE_LIST(RNODE_LIST(args)->nd_next)->nd_next;
}
ADD_SEQ(ret, match_values);
}
}
else {
ADD_INSN(ret, line_node, dup);
ADD_SEND(ret, line_node, idEmptyP, INT2FIX(0)); // (9)
if (in_single_pattern) {
CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit("%p is not empty"), base_index + 1 /* (9) */));
}
ADD_INSNL(ret, line_node, branchunless, match_failed);
}
if (RNODE_HSHPTN(node)->nd_pkwrestarg) {
if (RNODE_HSHPTN(node)->nd_pkwrestarg == NODE_SPECIAL_NO_REST_KEYWORD) {
ADD_INSN(ret, line_node, dup);
ADD_SEND(ret, line_node, idEmptyP, INT2FIX(0)); // (10)
if (in_single_pattern) {
CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit("rest of %p is not empty"), base_index + 1 /* (10) */));
}
ADD_INSNL(ret, line_node, branchunless, match_failed);
}
else {
ADD_INSN(ret, line_node, dup); // (11)
CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_HSHPTN(node)->nd_pkwrestarg, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 /* (11) */, false));
}
}
ADD_INSN(ret, line_node, pop);
ADD_INSNL(ret, line_node, jump, matched);
ADD_INSN(ret, line_node, putnil);
ADD_LABEL(ret, type_error);
ADD_INSN1(ret, line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
ADD_INSN1(ret, line_node, putobject, rb_eTypeError);
ADD_INSN1(ret, line_node, putobject, rb_fstring_lit("deconstruct_keys must return Hash"));
ADD_SEND(ret, line_node, id_core_raise, INT2FIX(2));
ADD_INSN(ret, line_node, pop);
ADD_LABEL(ret, match_failed);
ADD_INSN(ret, line_node, pop);
ADD_INSNL(ret, line_node, jump, unmatched);
break;
}
case NODE_LIT:
case NODE_STR:
case NODE_XSTR:
case NODE_DSTR:
case NODE_DSYM:
case NODE_DREGX:
case NODE_LIST:
case NODE_ZLIST:
case NODE_LAMBDA:
case NODE_DOT2:
case NODE_DOT3:
case NODE_CONST:
case NODE_LVAR:
case NODE_DVAR:
case NODE_IVAR:
case NODE_CVAR:
case NODE_GVAR:
case NODE_TRUE:
case NODE_FALSE:
case NODE_SELF:
case NODE_NIL:
case NODE_COLON2:
case NODE_COLON3:
case NODE_BEGIN:
case NODE_BLOCK:
CHECK(COMPILE(ret, "case in literal", node)); // (1)
if (in_single_pattern) {
ADD_INSN1(ret, line_node, dupn, INT2FIX(2));
}
ADD_INSN1(ret, line_node, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_CASE)); // (2)
if (in_single_pattern) {
CHECK(iseq_compile_pattern_set_eqq_errmsg(iseq, ret, node, base_index + 2 /* (1), (2) */));
}
ADD_INSNL(ret, line_node, branchif, matched);
ADD_INSNL(ret, line_node, jump, unmatched);
break;
case NODE_LASGN: {
struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
ID id = RNODE_LASGN(node)->nd_vid;
int idx = ISEQ_BODY(body->local_iseq)->local_table_size - get_local_var_idx(iseq, id);
if (in_alt_pattern) {
const char *name = rb_id2name(id);
if (name && strlen(name) > 0 && name[0] != '_') {
COMPILE_ERROR(ERROR_ARGS "illegal variable in alternative pattern (%"PRIsVALUE")",
rb_id2str(id));
return COMPILE_NG;
}
}
ADD_SETLOCAL(ret, line_node, idx, get_lvar_level(iseq));
ADD_INSNL(ret, line_node, jump, matched);
break;
}
case NODE_DASGN: {
int idx, lv, ls;
ID id = RNODE_DASGN(node)->nd_vid;
idx = get_dyna_var_idx(iseq, id, &lv, &ls);
if (in_alt_pattern) {
const char *name = rb_id2name(id);
if (name && strlen(name) > 0 && name[0] != '_') {
COMPILE_ERROR(ERROR_ARGS "illegal variable in alternative pattern (%"PRIsVALUE")",
rb_id2str(id));
return COMPILE_NG;
}
}
if (idx < 0) {
COMPILE_ERROR(ERROR_ARGS "NODE_DASGN: unknown id (%"PRIsVALUE")",
rb_id2str(id));
return COMPILE_NG;
}
ADD_SETLOCAL(ret, line_node, ls - idx, lv);
ADD_INSNL(ret, line_node, jump, matched);
break;
}
case NODE_IF:
case NODE_UNLESS: {
LABEL *match_failed;
match_failed = unmatched;
CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_IF(node)->nd_body, unmatched, in_single_pattern, in_alt_pattern, base_index, use_deconstructed_cache));
CHECK(COMPILE(ret, "case in if", RNODE_IF(node)->nd_cond));
if (in_single_pattern) {
LABEL *match_succeeded;
match_succeeded = NEW_LABEL(line);
ADD_INSN(ret, line_node, dup);
if (nd_type_p(node, NODE_IF)) {
ADD_INSNL(ret, line_node, branchif, match_succeeded);
}
else {
ADD_INSNL(ret, line_node, branchunless, match_succeeded);
}
ADD_INSN1(ret, line_node, putobject, rb_fstring_lit("guard clause does not return true")); // (1)
ADD_INSN1(ret, line_node, setn, INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 /* (1) */)); // (2)
ADD_INSN1(ret, line_node, putobject, Qfalse);
ADD_INSN1(ret, line_node, setn, INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 /* (1), (2) */));
ADD_INSN(ret, line_node, pop);
ADD_INSN(ret, line_node, pop);
ADD_LABEL(ret, match_succeeded);
}
if (nd_type_p(node, NODE_IF)) {
ADD_INSNL(ret, line_node, branchunless, match_failed);
}
else {
ADD_INSNL(ret, line_node, branchif, match_failed);
}
ADD_INSNL(ret, line_node, jump, matched);
break;
}
case NODE_HASH: {
NODE *n;
LABEL *match_failed;
match_failed = NEW_LABEL(line);
n = RNODE_HASH(node)->nd_head;
if (! (nd_type_p(n, NODE_LIST) && RNODE_LIST(n)->as.nd_alen == 2)) {
COMPILE_ERROR(ERROR_ARGS "unexpected node");
return COMPILE_NG;
}
ADD_INSN(ret, line_node, dup); // (1)
CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_LIST(n)->nd_head, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 /* (1) */, use_deconstructed_cache));
CHECK(iseq_compile_pattern_each(iseq, ret, RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_head, matched, match_failed, in_single_pattern, in_alt_pattern, base_index, false));
ADD_INSN(ret, line_node, putnil);
ADD_LABEL(ret, match_failed);
ADD_INSN(ret, line_node, pop);
ADD_INSNL(ret, line_node, jump, unmatched);
break;
}
case NODE_OR: {
LABEL *match_succeeded, *fin;
match_succeeded = NEW_LABEL(line);
fin = NEW_LABEL(line);
ADD_INSN(ret, line_node, dup); // (1)
CHECK(iseq_compile_pattern_each(iseq, ret, RNODE_OR(node)->nd_1st, match_succeeded, fin, in_single_pattern, true, base_index + 1 /* (1) */, use_deconstructed_cache));
ADD_LABEL(ret, match_succeeded);
ADD_INSN(ret, line_node, pop);
ADD_INSNL(ret, line_node, jump, matched);
ADD_INSN(ret, line_node, putnil);
ADD_LABEL(ret, fin);
CHECK(iseq_compile_pattern_each(iseq, ret, RNODE_OR(node)->nd_2nd, matched, unmatched, in_single_pattern, true, base_index, use_deconstructed_cache));
break;
}
default:
UNKNOWN_NODE("NODE_IN", node, COMPILE_NG);
}
return COMPILE_OK;
}
static int
iseq_compile_pattern_match(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, LABEL *unmatched, bool in_single_pattern, bool in_alt_pattern, int base_index, bool use_deconstructed_cache)
{
LABEL *fin = NEW_LABEL(nd_line(node));
CHECK(iseq_compile_pattern_each(iseq, ret, node, fin, unmatched, in_single_pattern, in_alt_pattern, base_index, use_deconstructed_cache));
ADD_LABEL(ret, fin);
return COMPILE_OK;
}
static int
iseq_compile_pattern_constant(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, LABEL *match_failed, bool in_single_pattern, int base_index)
{
const NODE *line_node = node;
if (RNODE_ARYPTN(node)->nd_pconst) {
ADD_INSN(ret, line_node, dup); // (1)
CHECK(COMPILE(ret, "constant", RNODE_ARYPTN(node)->nd_pconst)); // (2)
if (in_single_pattern) {
ADD_INSN1(ret, line_node, dupn, INT2FIX(2));
}
ADD_INSN1(ret, line_node, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_CASE)); // (3)
if (in_single_pattern) {
CHECK(iseq_compile_pattern_set_eqq_errmsg(iseq, ret, node, base_index + 3 /* (1), (2), (3) */));
}
ADD_INSNL(ret, line_node, branchunless, match_failed);
}
return COMPILE_OK;
}
static int
iseq_compile_array_deconstruct(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, LABEL *deconstruct, LABEL *deconstructed, LABEL *match_failed, LABEL *type_error, bool in_single_pattern, int base_index, bool use_deconstructed_cache)
{
const NODE *line_node = node;
// NOTE: this optimization allows us to re-use the #deconstruct value
// (or its absence).
if (use_deconstructed_cache) {
// If value is nil then we haven't tried to deconstruct
ADD_INSN1(ret, line_node, topn, INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE));
ADD_INSNL(ret, line_node, branchnil, deconstruct);
// If false then the value is not deconstructable
ADD_INSN1(ret, line_node, topn, INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE));
ADD_INSNL(ret, line_node, branchunless, match_failed);
// Drop value, add deconstructed to the stack and jump
ADD_INSN(ret, line_node, pop); // (1)
ADD_INSN1(ret, line_node, topn, INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE - 1 /* (1) */));
ADD_INSNL(ret, line_node, jump, deconstructed);
}
else {
ADD_INSNL(ret, line_node, jump, deconstruct);
}
ADD_LABEL(ret, deconstruct);
ADD_INSN(ret, line_node, dup);
ADD_INSN1(ret, line_node, putobject, ID2SYM(rb_intern("deconstruct")));
ADD_SEND(ret, line_node, idRespond_to, INT2FIX(1)); // (2)
// Cache the result of respond_to? (in case it's false is stays there, if true - it's overwritten after #deconstruct)
if (use_deconstructed_cache) {
ADD_INSN1(ret, line_node, setn, INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE + 1 /* (2) */));
}
if (in_single_pattern) {
CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit("%p does not respond to #deconstruct"), base_index + 1 /* (2) */));
}
ADD_INSNL(ret, line_node, branchunless, match_failed);
ADD_SEND(ret, line_node, rb_intern("deconstruct"), INT2FIX(0));
// Cache the result (if it's cacheable - currently, only top-level array patterns)
if (use_deconstructed_cache) {
ADD_INSN1(ret, line_node, setn, INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE));
}
ADD_INSN(ret, line_node, dup);
ADD_INSN1(ret, line_node, checktype, INT2FIX(T_ARRAY));
ADD_INSNL(ret, line_node, branchunless, type_error);
ADD_LABEL(ret, deconstructed);
return COMPILE_OK;
}
static int
iseq_compile_pattern_set_general_errmsg(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, VALUE errmsg, int base_index)
{
/*
* if match_succeeded?
* goto match_succeeded
* end
* error_string = FrozenCore.sprintf(errmsg, matchee)
* key_error_p = false
* match_succeeded:
*/
const int line = nd_line(node);
const NODE *line_node = node;
LABEL *match_succeeded = NEW_LABEL(line);
ADD_INSN(ret, line_node, dup);
ADD_INSNL(ret, line_node, branchif, match_succeeded);
ADD_INSN1(ret, line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
ADD_INSN1(ret, line_node, putobject, errmsg);
ADD_INSN1(ret, line_node, topn, INT2FIX(3));
ADD_SEND(ret, line_node, id_core_sprintf, INT2FIX(2)); // (1)
ADD_INSN1(ret, line_node, setn, INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 /* (1) */)); // (2)
ADD_INSN1(ret, line_node, putobject, Qfalse);
ADD_INSN1(ret, line_node, setn, INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 /* (1), (2) */));
ADD_INSN(ret, line_node, pop);
ADD_INSN(ret, line_node, pop);
ADD_LABEL(ret, match_succeeded);
return COMPILE_OK;
}
static int
iseq_compile_pattern_set_length_errmsg(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, VALUE errmsg, VALUE pattern_length, int base_index)
{
/*
* if match_succeeded?
* goto match_succeeded
* end
* error_string = FrozenCore.sprintf(errmsg, matchee, matchee.length, pat.length)
* key_error_p = false
* match_succeeded:
*/
const int line = nd_line(node);
const NODE *line_node = node;
LABEL *match_succeeded = NEW_LABEL(line);
ADD_INSN(ret, line_node, dup);
ADD_INSNL(ret, line_node, branchif, match_succeeded);
ADD_INSN1(ret, line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
ADD_INSN1(ret, line_node, putobject, errmsg);
ADD_INSN1(ret, line_node, topn, INT2FIX(3));
ADD_INSN(ret, line_node, dup);
ADD_SEND(ret, line_node, idLength, INT2FIX(0));
ADD_INSN1(ret, line_node, putobject, pattern_length);
ADD_SEND(ret, line_node, id_core_sprintf, INT2FIX(4)); // (1)
ADD_INSN1(ret, line_node, setn, INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 /* (1) */)); // (2)
ADD_INSN1(ret, line_node, putobject, Qfalse);
ADD_INSN1(ret, line_node, setn, INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2/* (1), (2) */));
ADD_INSN(ret, line_node, pop);
ADD_INSN(ret, line_node, pop);
ADD_LABEL(ret, match_succeeded);
return COMPILE_OK;
}
static int
iseq_compile_pattern_set_eqq_errmsg(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, int base_index)
{
/*
* if match_succeeded?
* goto match_succeeded
* end
* error_string = FrozenCore.sprintf("%p === %p does not return true", pat, matchee)
* key_error_p = false
* match_succeeded:
*/
const int line = nd_line(node);
const NODE *line_node = node;
LABEL *match_succeeded = NEW_LABEL(line);
ADD_INSN(ret, line_node, dup);
ADD_INSNL(ret, line_node, branchif, match_succeeded);
ADD_INSN1(ret, line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
ADD_INSN1(ret, line_node, putobject, rb_fstring_lit("%p === %p does not return true"));
ADD_INSN1(ret, line_node, topn, INT2FIX(3));
ADD_INSN1(ret, line_node, topn, INT2FIX(5));
ADD_SEND(ret, line_node, id_core_sprintf, INT2FIX(3)); // (1)
ADD_INSN1(ret, line_node, setn, INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 /* (1) */)); // (2)
ADD_INSN1(ret, line_node, putobject, Qfalse);
ADD_INSN1(ret, line_node, setn, INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 /* (1), (2) */));
ADD_INSN(ret, line_node, pop);
ADD_INSN(ret, line_node, pop);
ADD_LABEL(ret, match_succeeded);
ADD_INSN1(ret, line_node, setn, INT2FIX(2));
ADD_INSN(ret, line_node, pop);
ADD_INSN(ret, line_node, pop);
return COMPILE_OK;
}
static int
compile_case3(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const orig_node, int popped)
{
const NODE *pattern;
const NODE *node = orig_node;
LABEL *endlabel, *elselabel;
DECL_ANCHOR(head);
DECL_ANCHOR(body_seq);
DECL_ANCHOR(cond_seq);
int line;
enum node_type type;
const NODE *line_node;
VALUE branches = 0;
int branch_id = 0;
bool single_pattern;
INIT_ANCHOR(head);
INIT_ANCHOR(body_seq);
INIT_ANCHOR(cond_seq);
branches = decl_branch_base(iseq, node, "case");
node = RNODE_CASE3(node)->nd_body;
EXPECT_NODE("NODE_CASE3", node, NODE_IN, COMPILE_NG);
type = nd_type(node);
line = nd_line(node);
line_node = node;
single_pattern = !RNODE_IN(node)->nd_next;
endlabel = NEW_LABEL(line);
elselabel = NEW_LABEL(line);
if (single_pattern) {
/* allocate stack for ... */
ADD_INSN(head, line_node, putnil); /* key_error_key */
ADD_INSN(head, line_node, putnil); /* key_error_matchee */
ADD_INSN1(head, line_node, putobject, Qfalse); /* key_error_p */
ADD_INSN(head, line_node, putnil); /* error_string */
}
ADD_INSN(head, line_node, putnil); /* allocate stack for cached #deconstruct value */
CHECK(COMPILE(head, "case base", RNODE_CASE3(orig_node)->nd_head));
ADD_SEQ(ret, head); /* case VAL */
while (type == NODE_IN) {
LABEL *l1;
if (branch_id) {
ADD_INSN(body_seq, line_node, putnil);
}
l1 = NEW_LABEL(line);
ADD_LABEL(body_seq, l1);
ADD_INSN1(body_seq, line_node, adjuststack, INT2FIX(single_pattern ? 6 : 2));
add_trace_branch_coverage(
iseq,
body_seq,
RNODE_IN(node)->nd_body ? RNODE_IN(node)->nd_body : node,
branch_id++,
"in",
branches);
CHECK(COMPILE_(body_seq, "in body", RNODE_IN(node)->nd_body, popped));
ADD_INSNL(body_seq, line_node, jump, endlabel);
pattern = RNODE_IN(node)->nd_head;
if (pattern) {
int pat_line = nd_line(pattern);
LABEL *next_pat = NEW_LABEL(pat_line);
ADD_INSN (cond_seq, pattern, dup); /* dup case VAL */
// NOTE: set base_index (it's "under" the matchee value, so it's position is 2)
CHECK(iseq_compile_pattern_each(iseq, cond_seq, pattern, l1, next_pat, single_pattern, false, 2, true));
ADD_LABEL(cond_seq, next_pat);
LABEL_UNREMOVABLE(next_pat);
}
else {
COMPILE_ERROR(ERROR_ARGS "unexpected node");
return COMPILE_NG;
}
node = RNODE_IN(node)->nd_next;
if (!node) {
break;
}
type = nd_type(node);
line = nd_line(node);
line_node = node;
}
/* else */
if (node) {
ADD_LABEL(cond_seq, elselabel);
ADD_INSN(cond_seq, line_node, pop);
ADD_INSN(cond_seq, line_node, pop); /* discard cached #deconstruct value */
add_trace_branch_coverage(iseq, cond_seq, node, branch_id, "else", branches);
CHECK(COMPILE_(cond_seq, "else", node, popped));
ADD_INSNL(cond_seq, line_node, jump, endlabel);
ADD_INSN(cond_seq, line_node, putnil);
if (popped) {
ADD_INSN(cond_seq, line_node, putnil);
}
}
else {
debugs("== else (implicit)\n");
ADD_LABEL(cond_seq, elselabel);
add_trace_branch_coverage(iseq, cond_seq, orig_node, branch_id, "else", branches);
ADD_INSN1(cond_seq, orig_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
if (single_pattern) {
/*
* if key_error_p
* FrozenCore.raise NoMatchingPatternKeyError.new(FrozenCore.sprintf("%p: %s", case_val, error_string), matchee: key_error_matchee, key: key_error_key)
* else
* FrozenCore.raise NoMatchingPatternError, FrozenCore.sprintf("%p: %s", case_val, error_string)
* end
*/
LABEL *key_error, *fin;
struct rb_callinfo_kwarg *kw_arg;
key_error = NEW_LABEL(line);
fin = NEW_LABEL(line);
kw_arg = rb_xmalloc_mul_add(2, sizeof(VALUE), sizeof(struct rb_callinfo_kwarg));
kw_arg->references = 0;
kw_arg->keyword_len = 2;
kw_arg->keywords[0] = ID2SYM(rb_intern("matchee"));
kw_arg->keywords[1] = ID2SYM(rb_intern("key"));
ADD_INSN1(cond_seq, orig_node, topn, INT2FIX(CASE3_BI_OFFSET_KEY_ERROR_P + 2));
ADD_INSNL(cond_seq, orig_node, branchif, key_error);
ADD_INSN1(cond_seq, orig_node, putobject, rb_eNoMatchingPatternError);
ADD_INSN1(cond_seq, orig_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
ADD_INSN1(cond_seq, orig_node, putobject, rb_fstring_lit("%p: %s"));
ADD_INSN1(cond_seq, orig_node, topn, INT2FIX(4)); /* case VAL */
ADD_INSN1(cond_seq, orig_node, topn, INT2FIX(CASE3_BI_OFFSET_ERROR_STRING + 6));
ADD_SEND(cond_seq, orig_node, id_core_sprintf, INT2FIX(3));
ADD_SEND(cond_seq, orig_node, id_core_raise, INT2FIX(2));
ADD_INSNL(cond_seq, orig_node, jump, fin);
ADD_LABEL(cond_seq, key_error);
ADD_INSN1(cond_seq, orig_node, putobject, rb_eNoMatchingPatternKeyError);
ADD_INSN1(cond_seq, orig_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
ADD_INSN1(cond_seq, orig_node, putobject, rb_fstring_lit("%p: %s"));
ADD_INSN1(cond_seq, orig_node, topn, INT2FIX(4)); /* case VAL */
ADD_INSN1(cond_seq, orig_node, topn, INT2FIX(CASE3_BI_OFFSET_ERROR_STRING + 6));
ADD_SEND(cond_seq, orig_node, id_core_sprintf, INT2FIX(3));
ADD_INSN1(cond_seq, orig_node, topn, INT2FIX(CASE3_BI_OFFSET_KEY_ERROR_MATCHEE + 4));
ADD_INSN1(cond_seq, orig_node, topn, INT2FIX(CASE3_BI_OFFSET_KEY_ERROR_KEY + 5));
ADD_SEND_R(cond_seq, orig_node, rb_intern("new"), INT2FIX(1), NULL, INT2FIX(VM_CALL_KWARG), kw_arg);
ADD_SEND(cond_seq, orig_node, id_core_raise, INT2FIX(1));
ADD_LABEL(cond_seq, fin);
}
else {
ADD_INSN1(cond_seq, orig_node, putobject, rb_eNoMatchingPatternError);
ADD_INSN1(cond_seq, orig_node, topn, INT2FIX(2));
ADD_SEND(cond_seq, orig_node, id_core_raise, INT2FIX(2));
}
ADD_INSN1(cond_seq, orig_node, adjuststack, INT2FIX(single_pattern ? 7 : 3));
if (!popped) {
ADD_INSN(cond_seq, orig_node, putnil);
}
ADD_INSNL(cond_seq, orig_node, jump, endlabel);
ADD_INSN1(cond_seq, orig_node, dupn, INT2FIX(single_pattern ? 5 : 1));
if (popped) {
ADD_INSN(cond_seq, line_node, putnil);
}
}
ADD_SEQ(ret, cond_seq);
ADD_SEQ(ret, body_seq);
ADD_LABEL(ret, endlabel);
return COMPILE_OK;
}
#undef CASE3_BI_OFFSET_DECONSTRUCTED_CACHE
#undef CASE3_BI_OFFSET_ERROR_STRING
#undef CASE3_BI_OFFSET_KEY_ERROR_P
#undef CASE3_BI_OFFSET_KEY_ERROR_MATCHEE
#undef CASE3_BI_OFFSET_KEY_ERROR_KEY
static int
compile_loop(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, int popped, const enum node_type type)
{
const int line = (int)nd_line(node);
const NODE *line_node = node;
LABEL *prev_start_label = ISEQ_COMPILE_DATA(iseq)->start_label;
LABEL *prev_end_label = ISEQ_COMPILE_DATA(iseq)->end_label;
LABEL *prev_redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label;
int prev_loopval_popped = ISEQ_COMPILE_DATA(iseq)->loopval_popped;
VALUE branches = Qfalse;
struct iseq_compile_data_ensure_node_stack enl;
LABEL *next_label = ISEQ_COMPILE_DATA(iseq)->start_label = NEW_LABEL(line); /* next */
LABEL *redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label = NEW_LABEL(line); /* redo */
LABEL *break_label = ISEQ_COMPILE_DATA(iseq)->end_label = NEW_LABEL(line); /* break */
LABEL *end_label = NEW_LABEL(line);
LABEL *adjust_label = NEW_LABEL(line);
LABEL *next_catch_label = NEW_LABEL(line);
LABEL *tmp_label = NULL;
ISEQ_COMPILE_DATA(iseq)->loopval_popped = 0;
push_ensure_entry(iseq, &enl, NULL, NULL);
if (RNODE_WHILE(node)->nd_state == 1) {
ADD_INSNL(ret, line_node, jump, next_label);
}
else {
tmp_label = NEW_LABEL(line);
ADD_INSNL(ret, line_node, jump, tmp_label);
}
ADD_LABEL(ret, adjust_label);
ADD_INSN(ret, line_node, putnil);
ADD_LABEL(ret, next_catch_label);
ADD_INSN(ret, line_node, pop);
ADD_INSNL(ret, line_node, jump, next_label);
if (tmp_label) ADD_LABEL(ret, tmp_label);
ADD_LABEL(ret, redo_label);
branches = decl_branch_base(iseq, node, type == NODE_WHILE ? "while" : "until");
add_trace_branch_coverage(
iseq,
ret,
RNODE_WHILE(node)->nd_body ? RNODE_WHILE(node)->nd_body : node,
0,
"body",
branches);
CHECK(COMPILE_POPPED(ret, "while body", RNODE_WHILE(node)->nd_body));
ADD_LABEL(ret, next_label); /* next */
if (type == NODE_WHILE) {
compile_branch_condition(iseq, ret, RNODE_WHILE(node)->nd_cond,
redo_label, end_label);
}
else {
/* until */
compile_branch_condition(iseq, ret, RNODE_WHILE(node)->nd_cond,
end_label, redo_label);
}
ADD_LABEL(ret, end_label);
ADD_ADJUST_RESTORE(ret, adjust_label);
if (UNDEF_P(RNODE_WHILE(node)->nd_state)) {
/* ADD_INSN(ret, line_node, putundef); */
COMPILE_ERROR(ERROR_ARGS "unsupported: putundef");
return COMPILE_NG;
}
else {
ADD_INSN(ret, line_node, putnil);
}
ADD_LABEL(ret, break_label); /* break */
if (popped) {
ADD_INSN(ret, line_node, pop);
}
ADD_CATCH_ENTRY(CATCH_TYPE_BREAK, redo_label, break_label, NULL,
break_label);
ADD_CATCH_ENTRY(CATCH_TYPE_NEXT, redo_label, break_label, NULL,
next_catch_label);
ADD_CATCH_ENTRY(CATCH_TYPE_REDO, redo_label, break_label, NULL,
ISEQ_COMPILE_DATA(iseq)->redo_label);
ISEQ_COMPILE_DATA(iseq)->start_label = prev_start_label;
ISEQ_COMPILE_DATA(iseq)->end_label = prev_end_label;
ISEQ_COMPILE_DATA(iseq)->redo_label = prev_redo_label;
ISEQ_COMPILE_DATA(iseq)->loopval_popped = prev_loopval_popped;
ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack->prev;
return COMPILE_OK;
}
static int
compile_iter(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, int popped)
{
const int line = nd_line(node);
const NODE *line_node = node;
const rb_iseq_t *prevblock = ISEQ_COMPILE_DATA(iseq)->current_block;
LABEL *retry_label = NEW_LABEL(line);
LABEL *retry_end_l = NEW_LABEL(line);
const rb_iseq_t *child_iseq;
ADD_LABEL(ret, retry_label);
if (nd_type_p(node, NODE_FOR)) {
CHECK(COMPILE(ret, "iter caller (for)", RNODE_FOR(node)->nd_iter));
ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq =
NEW_CHILD_ISEQ(RNODE_FOR(node)->nd_body, make_name_for_block(iseq),
ISEQ_TYPE_BLOCK, line);
ADD_SEND_WITH_BLOCK(ret, line_node, idEach, INT2FIX(0), child_iseq);
}
else {
ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq =
NEW_CHILD_ISEQ(RNODE_ITER(node)->nd_body, make_name_for_block(iseq),
ISEQ_TYPE_BLOCK, line);
CHECK(COMPILE(ret, "iter caller", RNODE_ITER(node)->nd_iter));
}
{
// We need to put the label "retry_end_l" immediately after the last "send" instruction.
// This because vm_throw checks if the break cont is equal to the index of next insn of the "send".
// (Otherwise, it is considered "break from proc-closure". See "TAG_BREAK" handling in "vm_throw_start".)
//
// Normally, "send" instruction is at the last.
// However, qcall under branch coverage measurement adds some instructions after the "send".
//
// Note that "invokesuper" appears instead of "send".
INSN *iobj;
LINK_ELEMENT *last_elem = LAST_ELEMENT(ret);
iobj = IS_INSN(last_elem) ? (INSN*) last_elem : (INSN*) get_prev_insn((INSN*) last_elem);
while (INSN_OF(iobj) != BIN(send) && INSN_OF(iobj) != BIN(invokesuper)) {
iobj = (INSN*) get_prev_insn(iobj);
}
ELEM_INSERT_NEXT(&iobj->link, (LINK_ELEMENT*) retry_end_l);
// LINK_ANCHOR has a pointer to the last element, but ELEM_INSERT_NEXT does not update it
// even if we add an insn to the last of LINK_ANCHOR. So this updates it manually.
if (&iobj->link == LAST_ELEMENT(ret)) {
ret->last = (LINK_ELEMENT*) retry_end_l;
}
}
if (popped) {
ADD_INSN(ret, line_node, pop);
}
ISEQ_COMPILE_DATA(iseq)->current_block = prevblock;
ADD_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, child_iseq, retry_end_l);
return COMPILE_OK;
}
static int
compile_for_masgn(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, int popped)
{
/* massign to var in "for"
* (args.length == 1 && Array.try_convert(args[0])) || args
*/
const NODE *line_node = node;
const NODE *var = RNODE_FOR_MASGN(node)->nd_var;
LABEL *not_single = NEW_LABEL(nd_line(var));
LABEL *not_ary = NEW_LABEL(nd_line(var));
CHECK(COMPILE(ret, "for var", var));
ADD_INSN(ret, line_node, dup);
ADD_CALL(ret, line_node, idLength, INT2FIX(0));
ADD_INSN1(ret, line_node, putobject, INT2FIX(1));
ADD_CALL(ret, line_node, idEq, INT2FIX(1));
ADD_INSNL(ret, line_node, branchunless, not_single);
ADD_INSN(ret, line_node, dup);
ADD_INSN1(ret, line_node, putobject, INT2FIX(0));
ADD_CALL(ret, line_node, idAREF, INT2FIX(1));
ADD_INSN1(ret, line_node, putobject, rb_cArray);
ADD_INSN(ret, line_node, swap);
ADD_CALL(ret, line_node, rb_intern("try_convert"), INT2FIX(1));
ADD_INSN(ret, line_node, dup);
ADD_INSNL(ret, line_node, branchunless, not_ary);
ADD_INSN(ret, line_node, swap);
ADD_LABEL(ret, not_ary);
ADD_INSN(ret, line_node, pop);
ADD_LABEL(ret, not_single);
return COMPILE_OK;
}
static int
compile_break(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, int popped)
{
const NODE *line_node = node;
unsigned long throw_flag = 0;
if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
/* while/until */
LABEL *splabel = NEW_LABEL(0);
ADD_LABEL(ret, splabel);
ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->redo_label);
CHECK(COMPILE_(ret, "break val (while/until)", RNODE_BREAK(node)->nd_stts,
ISEQ_COMPILE_DATA(iseq)->loopval_popped));
add_ensure_iseq(ret, iseq, 0);
ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
ADD_ADJUST_RESTORE(ret, splabel);
if (!popped) {
ADD_INSN(ret, line_node, putnil);
}
}
else {
const rb_iseq_t *ip = iseq;
while (ip) {
if (!ISEQ_COMPILE_DATA(ip)) {
ip = 0;
break;
}
if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
throw_flag = VM_THROW_NO_ESCAPE_FLAG;
}
else if (ISEQ_BODY(ip)->type == ISEQ_TYPE_BLOCK) {
throw_flag = 0;
}
else if (ISEQ_BODY(ip)->type == ISEQ_TYPE_EVAL) {
COMPILE_ERROR(ERROR_ARGS "Can't escape from eval with break");
return COMPILE_NG;
}
else {
ip = ISEQ_BODY(ip)->parent_iseq;
continue;
}
/* escape from block */
CHECK(COMPILE(ret, "break val (block)", RNODE_BREAK(node)->nd_stts));
ADD_INSN1(ret, line_node, throw, INT2FIX(throw_flag | TAG_BREAK));
if (popped) {
ADD_INSN(ret, line_node, pop);
}
return COMPILE_OK;
}
COMPILE_ERROR(ERROR_ARGS "Invalid break");
return COMPILE_NG;
}
return COMPILE_OK;
}
static int
compile_next(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, int popped)
{
const NODE *line_node = node;
unsigned long throw_flag = 0;
if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
LABEL *splabel = NEW_LABEL(0);
debugs("next in while loop\n");
ADD_LABEL(ret, splabel);
CHECK(COMPILE(ret, "next val/valid syntax?", RNODE_NEXT(node)->nd_stts));
add_ensure_iseq(ret, iseq, 0);
ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->redo_label);
ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
ADD_ADJUST_RESTORE(ret, splabel);
if (!popped) {
ADD_INSN(ret, line_node, putnil);
}
}
else if (ISEQ_COMPILE_DATA(iseq)->end_label && can_add_ensure_iseq(iseq)) {
LABEL *splabel = NEW_LABEL(0);
debugs("next in block\n");
ADD_LABEL(ret, splabel);
ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->start_label);
CHECK(COMPILE(ret, "next val", RNODE_NEXT(node)->nd_stts));
add_ensure_iseq(ret, iseq, 0);
ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
ADD_ADJUST_RESTORE(ret, splabel);
if (!popped) {
ADD_INSN(ret, line_node, putnil);
}
}
else {
const rb_iseq_t *ip = iseq;
while (ip) {
if (!ISEQ_COMPILE_DATA(ip)) {
ip = 0;
break;
}
throw_flag = VM_THROW_NO_ESCAPE_FLAG;
if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
/* while loop */
break;
}
else if (ISEQ_BODY(ip)->type == ISEQ_TYPE_BLOCK) {
break;
}
else if (ISEQ_BODY(ip)->type == ISEQ_TYPE_EVAL) {
COMPILE_ERROR(ERROR_ARGS "Can't escape from eval with next");
return COMPILE_NG;
}
ip = ISEQ_BODY(ip)->parent_iseq;
}
if (ip != 0) {
CHECK(COMPILE(ret, "next val", RNODE_NEXT(node)->nd_stts));
ADD_INSN1(ret, line_node, throw, INT2FIX(throw_flag | TAG_NEXT));
if (popped) {
ADD_INSN(ret, line_node, pop);
}
}
else {
COMPILE_ERROR(ERROR_ARGS "Invalid next");
return COMPILE_NG;
}
}
return COMPILE_OK;
}
static int
compile_redo(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, int popped)
{
const NODE *line_node = node;
if (ISEQ_COMPILE_DATA(iseq)->redo_label && can_add_ensure_iseq(iseq)) {
LABEL *splabel = NEW_LABEL(0);
debugs("redo in while");
ADD_LABEL(ret, splabel);
ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->redo_label);
add_ensure_iseq(ret, iseq, 0);
ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->redo_label);
ADD_ADJUST_RESTORE(ret, splabel);
if (!popped) {
ADD_INSN(ret, line_node, putnil);
}
}
else if (ISEQ_BODY(iseq)->type != ISEQ_TYPE_EVAL && ISEQ_COMPILE_DATA(iseq)->start_label && can_add_ensure_iseq(iseq)) {
LABEL *splabel = NEW_LABEL(0);
debugs("redo in block");
ADD_LABEL(ret, splabel);
add_ensure_iseq(ret, iseq, 0);
ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->start_label);
ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
ADD_ADJUST_RESTORE(ret, splabel);
if (!popped) {
ADD_INSN(ret, line_node, putnil);
}
}
else {
const rb_iseq_t *ip = iseq;
while (ip) {
if (!ISEQ_COMPILE_DATA(ip)) {
ip = 0;
break;
}
if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
break;
}
else if (ISEQ_BODY(ip)->type == ISEQ_TYPE_BLOCK) {
break;
}
else if (ISEQ_BODY(ip)->type == ISEQ_TYPE_EVAL) {
COMPILE_ERROR(ERROR_ARGS "Can't escape from eval with redo");
return COMPILE_NG;
}
ip = ISEQ_BODY(ip)->parent_iseq;
}
if (ip != 0) {
ADD_INSN(ret, line_node, putnil);
ADD_INSN1(ret, line_node, throw, INT2FIX(VM_THROW_NO_ESCAPE_FLAG | TAG_REDO));
if (popped) {
ADD_INSN(ret, line_node, pop);
}
}
else {
COMPILE_ERROR(ERROR_ARGS "Invalid redo");
return COMPILE_NG;
}
}
return COMPILE_OK;
}
static int
compile_retry(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, int popped)
{
const NODE *line_node = node;
if (ISEQ_BODY(iseq)->type == ISEQ_TYPE_RESCUE) {
ADD_INSN(ret, line_node, putnil);
ADD_INSN1(ret, line_node, throw, INT2FIX(TAG_RETRY));
if (popped) {
ADD_INSN(ret, line_node, pop);
}
}
else {
COMPILE_ERROR(ERROR_ARGS "Invalid retry");
return COMPILE_NG;
}
return COMPILE_OK;
}
static int
compile_rescue(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, int popped)
{
const int line = nd_line(node);
const NODE *line_node = node;
LABEL *lstart = NEW_LABEL(line);
LABEL *lend = NEW_LABEL(line);
LABEL *lcont = NEW_LABEL(line);
const rb_iseq_t *rescue = NEW_CHILD_ISEQ(RNODE_RESCUE(node)->nd_resq,
rb_str_concat(rb_str_new2("rescue in "),
ISEQ_BODY(iseq)->location.label),
ISEQ_TYPE_RESCUE, line);
lstart->rescued = LABEL_RESCUE_BEG;
lend->rescued = LABEL_RESCUE_END;
ADD_LABEL(ret, lstart);
bool prev_in_rescue = ISEQ_COMPILE_DATA(iseq)->in_rescue;
ISEQ_COMPILE_DATA(iseq)->in_rescue = true;
{
CHECK(COMPILE(ret, "rescue head", RNODE_RESCUE(node)->nd_head));
}
ISEQ_COMPILE_DATA(iseq)->in_rescue = prev_in_rescue;
ADD_LABEL(ret, lend);
if (RNODE_RESCUE(node)->nd_else) {
ADD_INSN(ret, line_node, pop);
CHECK(COMPILE(ret, "rescue else", RNODE_RESCUE(node)->nd_else));
}
ADD_INSN(ret, line_node, nop);
ADD_LABEL(ret, lcont);
if (popped) {
ADD_INSN(ret, line_node, pop);
}
/* register catch entry */
ADD_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue, lcont);
ADD_CATCH_ENTRY(CATCH_TYPE_RETRY, lend, lcont, NULL, lstart);
return COMPILE_OK;
}
static int
compile_resbody(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, int popped)
{
const int line = nd_line(node);
const NODE *line_node = node;
const NODE *resq = node;
const NODE *narg;
LABEL *label_miss, *label_hit;
while (resq) {
label_miss = NEW_LABEL(line);
label_hit = NEW_LABEL(line);
narg = RNODE_RESBODY(resq)->nd_args;
if (narg) {
switch (nd_type(narg)) {
case NODE_LIST:
while (narg) {
ADD_GETLOCAL(ret, line_node, LVAR_ERRINFO, 0);
CHECK(COMPILE(ret, "rescue arg", RNODE_LIST(narg)->nd_head));
ADD_INSN1(ret, line_node, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
ADD_INSNL(ret, line_node, branchif, label_hit);
narg = RNODE_LIST(narg)->nd_next;
}
break;
case NODE_SPLAT:
case NODE_ARGSCAT:
case NODE_ARGSPUSH:
ADD_GETLOCAL(ret, line_node, LVAR_ERRINFO, 0);
CHECK(COMPILE(ret, "rescue/cond splat", narg));
ADD_INSN1(ret, line_node, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_RESCUE | VM_CHECKMATCH_ARRAY));
ADD_INSNL(ret, line_node, branchif, label_hit);
break;
default:
UNKNOWN_NODE("NODE_RESBODY", narg, COMPILE_NG);
}
}
else {
ADD_GETLOCAL(ret, line_node, LVAR_ERRINFO, 0);
ADD_INSN1(ret, line_node, putobject, rb_eStandardError);
ADD_INSN1(ret, line_node, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
ADD_INSNL(ret, line_node, branchif, label_hit);
}
ADD_INSNL(ret, line_node, jump, label_miss);
ADD_LABEL(ret, label_hit);
ADD_TRACE(ret, RUBY_EVENT_RESCUE);
if (nd_type(RNODE_RESBODY(resq)->nd_body) == NODE_BEGIN && RNODE_BEGIN(RNODE_RESBODY(resq)->nd_body)->nd_body == NULL) {
// empty body
int lineno = nd_line(RNODE_RESBODY(resq)->nd_body);
NODE dummy_line_node = generate_dummy_line_node(lineno, -1);
ADD_INSN(ret, &dummy_line_node, putnil);
}
else {
CHECK(COMPILE(ret, "resbody body", RNODE_RESBODY(resq)->nd_body));
}
if (ISEQ_COMPILE_DATA(iseq)->option->tailcall_optimization) {
ADD_INSN(ret, line_node, nop);
}
ADD_INSN(ret, line_node, leave);
ADD_LABEL(ret, label_miss);
resq = RNODE_RESBODY(resq)->nd_head;
}
return COMPILE_OK;
}
static int
compile_ensure(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, int popped)
{
const int line = nd_line(node);
const NODE *line_node = node;
DECL_ANCHOR(ensr);
const rb_iseq_t *ensure = NEW_CHILD_ISEQ(RNODE_ENSURE(node)->nd_ensr,
rb_str_concat(rb_str_new2 ("ensure in "), ISEQ_BODY(iseq)->location.label),
ISEQ_TYPE_ENSURE, line);
LABEL *lstart = NEW_LABEL(line);
LABEL *lend = NEW_LABEL(line);
LABEL *lcont = NEW_LABEL(line);
LINK_ELEMENT *last;
int last_leave = 0;
struct ensure_range er;
struct iseq_compile_data_ensure_node_stack enl;
struct ensure_range *erange;
INIT_ANCHOR(ensr);
CHECK(COMPILE_POPPED(ensr, "ensure ensr", RNODE_ENSURE(node)->nd_ensr));
last = ensr->last;
last_leave = last && IS_INSN(last) && IS_INSN_ID(last, leave);
er.begin = lstart;
er.end = lend;
er.next = 0;
push_ensure_entry(iseq, &enl, &er, RNODE_ENSURE(node)->nd_ensr);
ADD_LABEL(ret, lstart);
CHECK(COMPILE_(ret, "ensure head", RNODE_ENSURE(node)->nd_head, (popped | last_leave)));
ADD_LABEL(ret, lend);
ADD_SEQ(ret, ensr);
if (!popped && last_leave) ADD_INSN(ret, line_node, putnil);
ADD_LABEL(ret, lcont);
if (last_leave) ADD_INSN(ret, line_node, pop);
erange = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack->erange;
if (lstart->link.next != &lend->link) {
while (erange) {
ADD_CATCH_ENTRY(CATCH_TYPE_ENSURE, erange->begin, erange->end,
ensure, lcont);
erange = erange->next;
}
}
ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enl.prev;
return COMPILE_OK;
}
static int
compile_return(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, int popped)
{
const NODE *line_node = node;
if (iseq) {
enum rb_iseq_type type = ISEQ_BODY(iseq)->type;
const rb_iseq_t *is = iseq;
enum rb_iseq_type t = type;
const NODE *retval = RNODE_RETURN(node)->nd_stts;
LABEL *splabel = 0;
while (t == ISEQ_TYPE_RESCUE || t == ISEQ_TYPE_ENSURE) {
if (!(is = ISEQ_BODY(is)->parent_iseq)) break;
t = ISEQ_BODY(is)->type;
}
switch (t) {
case ISEQ_TYPE_TOP:
case ISEQ_TYPE_MAIN:
if (retval) {
rb_warn("argument of top-level return is ignored");
}
if (is == iseq) {
/* plain top-level, leave directly */
type = ISEQ_TYPE_METHOD;
}
break;
default:
break;
}
if (type == ISEQ_TYPE_METHOD) {
splabel = NEW_LABEL(0);
ADD_LABEL(ret, splabel);
ADD_ADJUST(ret, line_node, 0);
}
CHECK(COMPILE(ret, "return nd_stts (return val)", retval));
if (type == ISEQ_TYPE_METHOD && can_add_ensure_iseq(iseq)) {
add_ensure_iseq(ret, iseq, 1);
ADD_TRACE(ret, RUBY_EVENT_RETURN);
ADD_INSN(ret, line_node, leave);
ADD_ADJUST_RESTORE(ret, splabel);
if (!popped) {
ADD_INSN(ret, line_node, putnil);
}
}
else {
ADD_INSN1(ret, line_node, throw, INT2FIX(TAG_RETURN));
if (popped) {
ADD_INSN(ret, line_node, pop);
}
}
}
return COMPILE_OK;
}
static int
compile_evstr(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, int popped)
{
CHECK(COMPILE_(ret, "nd_body", node, popped));
if (!popped && !all_string_result_p(node)) {
const NODE *line_node = node;
const unsigned int flag = VM_CALL_FCALL;
// Note, this dup could be removed if we are willing to change anytostring. It pops
// two VALUEs off the stack when it could work by replacing the top most VALUE.
ADD_INSN(ret, line_node, dup);
ADD_INSN1(ret, line_node, objtostring, new_callinfo(iseq, idTo_s, 0, flag, NULL, FALSE));
ADD_INSN(ret, line_node, anytostring);
}
return COMPILE_OK;
}
static void
compile_lvar(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *line_node, ID id)
{
int idx = ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->local_table_size - get_local_var_idx(iseq, id);
debugs("id: %s idx: %d\n", rb_id2name(id), idx);
ADD_GETLOCAL(ret, line_node, idx, get_lvar_level(iseq));
}
static LABEL *
qcall_branch_start(rb_iseq_t *iseq, LINK_ANCHOR *const recv, VALUE *branches, const NODE *node, const NODE *line_node)
{
LABEL *else_label = NEW_LABEL(nd_line(line_node));
VALUE br = 0;
br = decl_branch_base(iseq, node, "&.");
*branches = br;
ADD_INSN(recv, line_node, dup);
ADD_INSNL(recv, line_node, branchnil, else_label);
add_trace_branch_coverage(iseq, recv, node, 0, "then", br);
return else_label;
}
static void
qcall_branch_end(rb_iseq_t *iseq, LINK_ANCHOR *const ret, LABEL *else_label, VALUE branches, const NODE *node, const NODE *line_node)
{
LABEL *end_label;
if (!else_label) return;
end_label = NEW_LABEL(nd_line(line_node));
ADD_INSNL(ret, line_node, jump, end_label);
ADD_LABEL(ret, else_label);
add_trace_branch_coverage(iseq, ret, node, 1, "else", branches);
ADD_LABEL(ret, end_label);
}
static int
compile_call_precheck_freeze(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, const NODE *line_node, int popped)
{
/* optimization shortcut
* "literal".freeze -> opt_str_freeze("literal")
*/
if (get_nd_recv(node) && nd_type_p(get_nd_recv(node), NODE_STR) &&
(get_node_call_nd_mid(node) == idFreeze || get_node_call_nd_mid(node) == idUMinus) &&
get_nd_args(node) == NULL &&
ISEQ_COMPILE_DATA(iseq)->current_block == NULL &&
ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
VALUE str = rb_fstring(RNODE_STR(get_nd_recv(node))->nd_lit);
if (get_node_call_nd_mid(node) == idUMinus) {
ADD_INSN2(ret, line_node, opt_str_uminus, str,
new_callinfo(iseq, idUMinus, 0, 0, NULL, FALSE));
}
else {
ADD_INSN2(ret, line_node, opt_str_freeze, str,
new_callinfo(iseq, idFreeze, 0, 0, NULL, FALSE));
}
RB_OBJ_WRITTEN(iseq, Qundef, str);
if (popped) {
ADD_INSN(ret, line_node, pop);
}
return TRUE;
}
/* optimization shortcut
* obj["literal"] -> opt_aref_with(obj, "literal")
*/
if (get_node_call_nd_mid(node) == idAREF && !private_recv_p(node) && get_nd_args(node) &&
nd_type_p(get_nd_args(node), NODE_LIST) && RNODE_LIST(get_nd_args(node))->as.nd_alen == 1 &&
nd_type_p(RNODE_LIST(get_nd_args(node))->nd_head, NODE_STR) &&
ISEQ_COMPILE_DATA(iseq)->current_block == NULL &&
!ISEQ_COMPILE_DATA(iseq)->option->frozen_string_literal &&
ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
VALUE str = rb_fstring(RNODE_STR(RNODE_LIST(get_nd_args(node))->nd_head)->nd_lit);
CHECK(COMPILE(ret, "recv", get_nd_recv(node)));
ADD_INSN2(ret, line_node, opt_aref_with, str,
new_callinfo(iseq, idAREF, 1, 0, NULL, FALSE));
RB_OBJ_WRITTEN(iseq, Qundef, str);
if (popped) {
ADD_INSN(ret, line_node, pop);
}
return TRUE;
}
return FALSE;
}
static int
iseq_has_builtin_function_table(const rb_iseq_t *iseq)
{
return ISEQ_COMPILE_DATA(iseq)->builtin_function_table != NULL;
}
static const struct rb_builtin_function *
iseq_builtin_function_lookup(const rb_iseq_t *iseq, const char *name)
{
int i;
const struct rb_builtin_function *table = ISEQ_COMPILE_DATA(iseq)->builtin_function_table;
for (i=0; table[i].index != -1; i++) {
if (strcmp(table[i].name, name) == 0) {
return &table[i];
}
}
return NULL;
}
static const char *
iseq_builtin_function_name(const enum node_type type, const NODE *recv, ID mid)
{
const char *name = rb_id2name(mid);
static const char prefix[] = "__builtin_";
const size_t prefix_len = sizeof(prefix) - 1;
switch (type) {
case NODE_CALL:
if (recv) {
switch (nd_type(recv)) {
case NODE_VCALL:
if (RNODE_VCALL(recv)->nd_mid == rb_intern("__builtin")) {
return name;
}
break;
case NODE_CONST:
if (RNODE_CONST(recv)->nd_vid == rb_intern("Primitive")) {
return name;
}
break;
default: break;
}
}
break;
case NODE_VCALL:
case NODE_FCALL:
if (UNLIKELY(strncmp(prefix, name, prefix_len) == 0)) {
return &name[prefix_len];
}
break;
default: break;
}
return NULL;
}
static int
delegate_call_p(const rb_iseq_t *iseq, unsigned int argc, const LINK_ANCHOR *args, unsigned int *pstart_index)
{
if (argc == 0) {
*pstart_index = 0;
return TRUE;
}
else if (argc <= ISEQ_BODY(iseq)->local_table_size) {
unsigned int start=0;
// local_table: [p1, p2, p3, l1, l2, l3]
// arguments: [p3, l1, l2] -> 2
for (start = 0;
argc + start <= ISEQ_BODY(iseq)->local_table_size;
start++) {
const LINK_ELEMENT *elem = FIRST_ELEMENT(args);
for (unsigned int i=start; i-start<argc; i++) {
if (IS_INSN(elem) &&
INSN_OF(elem) == BIN(getlocal)) {
int local_index = FIX2INT(OPERAND_AT(elem, 0));
int local_level = FIX2INT(OPERAND_AT(elem, 1));
if (local_level == 0) {
unsigned int index = ISEQ_BODY(iseq)->local_table_size - (local_index - VM_ENV_DATA_SIZE + 1);
if (0) { // for debug
fprintf(stderr, "lvar:%s (%d), id:%s (%d) local_index:%d, local_size:%d\n",
rb_id2name(ISEQ_BODY(iseq)->local_table[i]), i,
rb_id2name(ISEQ_BODY(iseq)->local_table[index]), index,
local_index, (int)ISEQ_BODY(iseq)->local_table_size);
}
if (i == index) {
elem = elem->next;
continue; /* for */
}
else {
goto next;
}
}
else {
goto fail; // level != 0 is unsupported
}
}
else {
goto fail; // insn is not a getlocal
}
}
goto success;
next:;
}
fail:
return FALSE;
success:
*pstart_index = start;
return TRUE;
}
else {
return FALSE;
}
}
// Compile Primitive.attr! :leaf, ...
static int
compile_builtin_attr(rb_iseq_t *iseq, const NODE *node)
{
VALUE symbol;
VALUE string;
if (!node) goto no_arg;
while (node) {
if (!nd_type_p(node, NODE_LIST)) goto bad_arg;
const NODE *next = RNODE_LIST(node)->nd_next;
node = RNODE_LIST(node)->nd_head;
if (!node) goto no_arg;
if (!nd_type_p(node, NODE_LIT)) goto bad_arg;
symbol = RNODE_LIT(node)->nd_lit;
if (!SYMBOL_P(symbol)) goto non_symbol_arg;
string = rb_sym_to_s(symbol);
if (strcmp(RSTRING_PTR(string), "leaf") == 0) {
ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_LEAF;
}
else if (strcmp(RSTRING_PTR(string), "no_gc") == 0) {
ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_NO_GC;
}
else {
goto unknown_arg;
}
node = next;
}
return COMPILE_OK;
no_arg:
COMPILE_ERROR(ERROR_ARGS "attr!: no argument");
return COMPILE_NG;
non_symbol_arg:
COMPILE_ERROR(ERROR_ARGS "non symbol argument to attr!: %s", rb_builtin_class_name(symbol));
return COMPILE_NG;
unknown_arg:
COMPILE_ERROR(ERROR_ARGS "unknown argument to attr!: %s", RSTRING_PTR(string));
return COMPILE_NG;
bad_arg:
UNKNOWN_NODE("attr!", node, COMPILE_NG);
}
static int
compile_builtin_arg(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node, const NODE *line_node, int popped)
{
if (!node) goto no_arg;
if (!nd_type_p(node, NODE_LIST)) goto bad_arg;
if (RNODE_LIST(node)->nd_next) goto too_many_arg;
node = RNODE_LIST(node)->nd_head;
if (!node) goto no_arg;
if (!nd_type_p(node, NODE_LIT)) goto bad_arg;
VALUE name = RNODE_LIT(node)->nd_lit;
if (!SYMBOL_P(name)) goto non_symbol_arg;
if (!popped) {
compile_lvar(iseq, ret, line_node, SYM2ID(name));
}
return COMPILE_OK;
no_arg:
COMPILE_ERROR(ERROR_ARGS "arg!: no argument");
return COMPILE_NG;
too_many_arg:
COMPILE_ERROR(ERROR_ARGS "arg!: too many argument");
return COMPILE_NG;
non_symbol_arg:
COMPILE_ERROR(ERROR_ARGS "non symbol argument to arg!: %s",
rb_builtin_class_name(name));
return COMPILE_NG;
bad_arg:
UNKNOWN_NODE("arg!", node, COMPILE_NG);
}
static NODE *
mandatory_node(const rb_iseq_t *iseq, const NODE *cond_node)
{
const NODE *node = ISEQ_COMPILE_DATA(iseq)->root_node;
if (nd_type(node) == NODE_IF && RNODE_IF(node)->nd_cond == cond_node) {
return RNODE_IF(node)->nd_body;
}
else {
rb_bug("mandatory_node: can't find mandatory node");
}
}
static int
compile_builtin_mandatory_only_method(rb_iseq_t *iseq, const NODE *node, const NODE *line_node)
{
// arguments
struct rb_args_info args = {
.pre_args_num = ISEQ_BODY(iseq)->param.lead_num,
};
rb_node_args_t args_node;
rb_node_init(RNODE(&args_node), NODE_ARGS);
args_node.nd_ainfo = args;
// local table without non-mandatory parameters
const int skip_local_size = ISEQ_BODY(iseq)->param.size - ISEQ_BODY(iseq)->param.lead_num;
const int table_size = ISEQ_BODY(iseq)->local_table_size - skip_local_size;
VALUE idtmp = 0;
rb_ast_id_table_t *tbl = ALLOCV(idtmp, sizeof(rb_ast_id_table_t) + table_size * sizeof(ID));
tbl->size = table_size;
int i;
// lead parameters
for (i=0; i<ISEQ_BODY(iseq)->param.lead_num; i++) {
tbl->ids[i] = ISEQ_BODY(iseq)->local_table[i];
}
// local variables
for (; i<table_size; i++) {
tbl->ids[i] = ISEQ_BODY(iseq)->local_table[i + skip_local_size];
}
rb_node_scope_t scope_node;
rb_node_init(RNODE(&scope_node), NODE_SCOPE);
scope_node.nd_tbl = tbl;
scope_node.nd_body = mandatory_node(iseq, node);
scope_node.nd_args = &args_node;
rb_ast_body_t ast = {
.root = RNODE(&scope_node),
.frozen_string_literal = -1,
.coverage_enabled = -1,
.script_lines = ISEQ_BODY(iseq)->variable.script_lines,
};
ISEQ_BODY(iseq)->mandatory_only_iseq =
rb_iseq_new_with_opt(&ast, rb_iseq_base_label(iseq),
rb_iseq_path(iseq), rb_iseq_realpath(iseq),
nd_line(line_node), NULL, 0,
ISEQ_TYPE_METHOD, ISEQ_COMPILE_DATA(iseq)->option);
ALLOCV_END(idtmp);
return COMPILE_OK;
}
static int
compile_builtin_function_call(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, const NODE *line_node, int popped,
const rb_iseq_t *parent_block, LINK_ANCHOR *args, const char *builtin_func)
{
NODE *args_node = get_nd_args(node);
if (parent_block != NULL) {
COMPILE_ERROR(ERROR_ARGS_AT(line_node) "should not call builtins here.");
return COMPILE_NG;
}
else {
# define BUILTIN_INLINE_PREFIX "_bi"
char inline_func[sizeof(BUILTIN_INLINE_PREFIX) + DECIMAL_SIZE_OF(int)];
bool cconst = false;
retry:;
const struct rb_builtin_function *bf = iseq_builtin_function_lookup(iseq, builtin_func);
if (bf == NULL) {
if (strcmp("cstmt!", builtin_func) == 0 ||
strcmp("cexpr!", builtin_func) == 0) {
// ok
}
else if (strcmp("cconst!", builtin_func) == 0) {
cconst = true;
}
else if (strcmp("cinit!", builtin_func) == 0) {
// ignore
GET_VM()->builtin_inline_index++;
return COMPILE_OK;
}
else if (strcmp("attr!", builtin_func) == 0) {
return compile_builtin_attr(iseq, args_node);
}
else if (strcmp("arg!", builtin_func) == 0) {
return compile_builtin_arg(iseq, ret, args_node, line_node, popped);
}
else if (strcmp("mandatory_only?", builtin_func) == 0) {
if (popped) {
rb_bug("mandatory_only? should be in if condition");
}
else if (!LIST_INSN_SIZE_ZERO(ret)) {
rb_bug("mandatory_only? should be put on top");
}
ADD_INSN1(ret, line_node, putobject, Qfalse);
return compile_builtin_mandatory_only_method(iseq, node, line_node);
}
else if (1) {
rb_bug("can't find builtin function:%s", builtin_func);
}
else {
COMPILE_ERROR(ERROR_ARGS "can't find builtin function:%s", builtin_func);
return COMPILE_NG;
}
if (GET_VM()->builtin_inline_index == INT_MAX) {
rb_bug("builtin inline function index overflow:%s", builtin_func);
}
int inline_index = GET_VM()->builtin_inline_index++;
snprintf(inline_func, sizeof(inline_func), BUILTIN_INLINE_PREFIX "%d", inline_index);
builtin_func = inline_func;
args_node = NULL;
goto retry;
}
if (cconst) {
typedef VALUE(*builtin_func0)(void *, VALUE);
VALUE const_val = (*(builtin_func0)bf->func_ptr)(NULL, Qnil);
ADD_INSN1(ret, line_node, putobject, const_val);
return COMPILE_OK;
}
// fprintf(stderr, "func_name:%s -> %p\n", builtin_func, bf->func_ptr);
unsigned int flag = 0;
struct rb_callinfo_kwarg *keywords = NULL;
VALUE argc = setup_args(iseq, args, args_node, &flag, &keywords);
if (FIX2INT(argc) != bf->argc) {
COMPILE_ERROR(ERROR_ARGS "argc is not match for builtin function:%s (expect %d but %d)",
builtin_func, bf->argc, FIX2INT(argc));
return COMPILE_NG;
}
unsigned int start_index;
if (delegate_call_p(iseq, FIX2INT(argc), args, &start_index)) {
ADD_INSN2(ret, line_node, opt_invokebuiltin_delegate, bf, INT2FIX(start_index));
}
else {
ADD_SEQ(ret, args);
ADD_INSN1(ret, line_node, invokebuiltin, bf);
}
if (popped) ADD_INSN(ret, line_node, pop);
return COMPILE_OK;
}
}
static int
compile_call(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, const enum node_type type, const NODE *const line_node, int popped, bool assume_receiver)
{
/* call: obj.method(...)
* fcall: func(...)
* vcall: func
*/
DECL_ANCHOR(recv);
DECL_ANCHOR(args);
ID mid = get_node_call_nd_mid(node);
VALUE argc;
unsigned int flag = 0;
struct rb_callinfo_kwarg *keywords = NULL;
const rb_iseq_t *parent_block = ISEQ_COMPILE_DATA(iseq)->current_block;
LABEL *else_label = NULL;
VALUE branches = Qfalse;
ISEQ_COMPILE_DATA(iseq)->current_block = NULL;
INIT_ANCHOR(recv);
INIT_ANCHOR(args);
#if OPT_SUPPORT_JOKE
if (nd_type_p(node, NODE_VCALL)) {
ID id_bitblt;
ID id_answer;
CONST_ID(id_bitblt, "bitblt");
CONST_ID(id_answer, "the_answer_to_life_the_universe_and_everything");
if (mid == id_bitblt) {
ADD_INSN(ret, line_node, bitblt);
return COMPILE_OK;
}
else if (mid == id_answer) {
ADD_INSN(ret, line_node, answer);
return COMPILE_OK;
}
}
/* only joke */
{
ID goto_id;
ID label_id;
CONST_ID(goto_id, "__goto__");
CONST_ID(label_id, "__label__");
if (nd_type_p(node, NODE_FCALL) &&
(mid == goto_id || mid == label_id)) {
LABEL *label;
st_data_t data;
st_table *labels_table = ISEQ_COMPILE_DATA(iseq)->labels_table;
VALUE label_name;
if (!labels_table) {
labels_table = st_init_numtable();
ISEQ_COMPILE_DATA(iseq)->labels_table = labels_table;
}
if (nd_type_p(node->nd_args->nd_head, NODE_LIT) &&
SYMBOL_P(node->nd_args->nd_head->nd_lit)) {
label_name = node->nd_args->nd_head->nd_lit;
if (!st_lookup(labels_table, (st_data_t)label_name, &data)) {
label = NEW_LABEL(nd_line(line_node));
label->position = nd_line(line_node);
st_insert(labels_table, (st_data_t)label_name, (st_data_t)label);
}
else {
label = (LABEL *)data;
}
}
else {
COMPILE_ERROR(ERROR_ARGS "invalid goto/label format");
return COMPILE_NG;
}
if (mid == goto_id) {
ADD_INSNL(ret, line_node, jump, label);
}
else {
ADD_LABEL(ret, label);
}
return COMPILE_OK;
}
}
#endif
const char *builtin_func;
if (UNLIKELY(iseq_has_builtin_function_table(iseq)) &&
(builtin_func = iseq_builtin_function_name(type, get_nd_recv(node), mid)) != NULL) {
return compile_builtin_function_call(iseq, ret, node, line_node, popped, parent_block, args, builtin_func);
}
/* receiver */
if (!assume_receiver) {
if (type == NODE_CALL || type == NODE_OPCALL || type == NODE_QCALL) {
int idx, level;
if (mid == idCall &&
nd_type_p(get_nd_recv(node), NODE_LVAR) &&
iseq_block_param_id_p(iseq, RNODE_LVAR(get_nd_recv(node))->nd_vid, &idx, &level)) {
ADD_INSN2(recv, get_nd_recv(node), getblockparamproxy, INT2FIX(idx + VM_ENV_DATA_SIZE - 1), INT2FIX(level));
}
else if (private_recv_p(node)) {
ADD_INSN(recv, node, putself);
flag |= VM_CALL_FCALL;
}
else {
CHECK(COMPILE(recv, "recv", get_nd_recv(node)));
}
if (type == NODE_QCALL) {
else_label = qcall_branch_start(iseq, recv, &branches, node, line_node);
}
}
else if (type == NODE_FCALL || type == NODE_VCALL) {
ADD_CALL_RECEIVER(recv, line_node);
}
}
/* args */
if (type != NODE_VCALL) {
argc = setup_args(iseq, args, get_nd_args(node), &flag, &keywords);
CHECK(!NIL_P(argc));
}
else {
argc = INT2FIX(0);
}
ADD_SEQ(ret, recv);
ADD_SEQ(ret, args);
debugp_param("call args argc", argc);
debugp_param("call method", ID2SYM(mid));
switch ((int)type) {
case NODE_VCALL:
flag |= VM_CALL_VCALL;
/* VCALL is funcall, so fall through */
case NODE_FCALL:
flag |= VM_CALL_FCALL;
}
if ((flag & VM_CALL_ARGS_BLOCKARG) && (flag & VM_CALL_KW_SPLAT) && !(flag & VM_CALL_KW_SPLAT_MUT)) {
ADD_INSN(ret, line_node, splatkw);
}
ADD_SEND_R(ret, line_node, mid, argc, parent_block, INT2FIX(flag), keywords);
qcall_branch_end(iseq, ret, else_label, branches, node, line_node);
if (popped) {
ADD_INSN(ret, line_node, pop);
}
return COMPILE_OK;
}
static int
compile_op_asgn1(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, int popped)
{
const int line = nd_line(node);
VALUE argc;
unsigned int flag = 0;
int asgnflag = 0;
ID id = RNODE_OP_ASGN1(node)->nd_mid;
int boff = 0;
int keyword_len = 0;
struct rb_callinfo_kwarg *keywords = NULL;
/*
* a[x] (op)= y
*
* nil # nil
* eval a # nil a
* eval x # nil a x
* dupn 2 # nil a x a x
* send :[] # nil a x a[x]
* eval y # nil a x a[x] y
* send op # nil a x ret
* setn 3 # ret a x ret
* send []= # ret ?
* pop # ret
*/
/*
* nd_recv[nd_args->nd_body] (nd_mid)= nd_args->nd_head;
* NODE_OP_ASGN nd_recv
* nd_args->nd_head
* nd_args->nd_body
* nd_mid
*/
if (!popped) {
ADD_INSN(ret, node, putnil);
}
asgnflag = COMPILE_RECV(ret, "NODE_OP_ASGN1 recv", node, RNODE_OP_ASGN1(node)->nd_recv);
CHECK(asgnflag != -1);
switch (nd_type(RNODE_OP_ASGN1(node)->nd_index)) {
case NODE_ZLIST:
argc = INT2FIX(0);
break;
case NODE_BLOCK_PASS:
boff = 1;
/* fall through */
default:
argc = setup_args(iseq, ret, RNODE_OP_ASGN1(node)->nd_index, &flag, &keywords);
if (flag & VM_CALL_KW_SPLAT) {
if (boff) {
ADD_INSN(ret, node, splatkw);
}
else {
/* Make sure to_hash is only called once and not twice */
ADD_INSN(ret, node, dup);
ADD_INSN(ret, node, splatkw);
ADD_INSN(ret, node, pop);
}
}
CHECK(!NIL_P(argc));
}
int dup_argn = FIX2INT(argc) + 1 + boff;
if (keywords) {
keyword_len = keywords->keyword_len;
dup_argn += keyword_len;
}
ADD_INSN1(ret, node, dupn, INT2FIX(dup_argn));
flag |= asgnflag;
ADD_SEND_R(ret, node, idAREF, argc, NULL, INT2FIX(flag & ~VM_CALL_KW_SPLAT_MUT), keywords);
if (id == idOROP || id == idANDOP) {
/* a[x] ||= y or a[x] &&= y
unless/if a[x]
a[x]= y
else
nil
end
*/
LABEL *label = NEW_LABEL(line);
LABEL *lfin = NEW_LABEL(line);
ADD_INSN(ret, node, dup);
if (id == idOROP) {
ADD_INSNL(ret, node, branchif, label);
}
else { /* idANDOP */
ADD_INSNL(ret, node, branchunless, label);
}
ADD_INSN(ret, node, pop);
CHECK(COMPILE(ret, "NODE_OP_ASGN1 nd_rvalue: ", RNODE_OP_ASGN1(node)->nd_rvalue));
if (!popped) {
ADD_INSN1(ret, node, setn, INT2FIX(dup_argn+1));
}
if (flag & VM_CALL_ARGS_SPLAT) {
if (flag & VM_CALL_KW_SPLAT) {
ADD_INSN1(ret, node, topn, INT2FIX(2 + boff));
ADD_INSN(ret, node, swap);
ADD_INSN1(ret, node, newarray, INT2FIX(1));
ADD_INSN(ret, node, concatarray);
ADD_INSN1(ret, node, setn, INT2FIX(2 + boff));
ADD_INSN(ret, node, pop);
}
else {
ADD_INSN1(ret, node, newarray, INT2FIX(1));
if (boff > 0) {
ADD_INSN1(ret, node, dupn, INT2FIX(3));
ADD_INSN(ret, node, swap);
ADD_INSN(ret, node, pop);
}
ADD_INSN(ret, node, concatarray);
if (boff > 0) {
ADD_INSN1(ret, node, setn, INT2FIX(3));
ADD_INSN(ret, node, pop);
ADD_INSN(ret, node, pop);
}
}
ADD_SEND_R(ret, node, idASET, argc, NULL, INT2FIX(flag), keywords);
}
else if (flag & VM_CALL_KW_SPLAT) {
if (boff > 0) {
ADD_INSN1(ret, node, topn, INT2FIX(2));
ADD_INSN(ret, node, swap);
ADD_INSN1(ret, node, setn, INT2FIX(3));
ADD_INSN(ret, node, pop);
}
ADD_INSN(ret, node, swap);
ADD_SEND_R(ret, node, idASET, FIXNUM_INC(argc, 1), NULL, INT2FIX(flag), keywords);
}
else if (keyword_len) {
ADD_INSN1(ret, node, opt_reverse, INT2FIX(keyword_len+boff+1));
ADD_INSN1(ret, node, opt_reverse, INT2FIX(keyword_len+boff+0));
ADD_SEND_R(ret, node, idASET, FIXNUM_INC(argc, 1), NULL, INT2FIX(flag), keywords);
}
else {
if (boff > 0)
ADD_INSN(ret, node, swap);
ADD_SEND_R(ret, node, idASET, FIXNUM_INC(argc, 1), NULL, INT2FIX(flag), keywords);
}
ADD_INSN(ret, node, pop);
ADD_INSNL(ret, node, jump, lfin);
ADD_LABEL(ret, label);
if (!popped) {
ADD_INSN1(ret, node, setn, INT2FIX(dup_argn+1));
}
ADD_INSN1(ret, node, adjuststack, INT2FIX(dup_argn+1));
ADD_LABEL(ret, lfin);
}
else {
CHECK(COMPILE(ret, "NODE_OP_ASGN1 nd_rvalue: ", RNODE_OP_ASGN1(node)->nd_rvalue));
ADD_SEND(ret, node, id, INT2FIX(1));
if (!popped) {
ADD_INSN1(ret, node, setn, INT2FIX(dup_argn+1));
}
if (flag & VM_CALL_ARGS_SPLAT) {
if (flag & VM_CALL_KW_SPLAT) {
ADD_INSN1(ret, node, topn, INT2FIX(2 + boff));
ADD_INSN(ret, node, swap);
ADD_INSN1(ret, node, newarray, INT2FIX(1));
ADD_INSN(ret, node, concatarray);
ADD_INSN1(ret, node, setn, INT2FIX(2 + boff));
ADD_INSN(ret, node, pop);
}
else {
ADD_INSN1(ret, node, newarray, INT2FIX(1));
if (boff > 0) {
ADD_INSN1(ret, node, dupn, INT2FIX(3));
ADD_INSN(ret, node, swap);
ADD_INSN(ret, node, pop);
}
ADD_INSN(ret, node, concatarray);
if (boff > 0) {
ADD_INSN1(ret, node, setn, INT2FIX(3));
ADD_INSN(ret, node, pop);
ADD_INSN(ret, node, pop);
}
}
ADD_SEND_R(ret, node, idASET, argc, NULL, INT2FIX(flag), keywords);
}
else if (flag & VM_CALL_KW_SPLAT) {
if (boff > 0) {
ADD_INSN1(ret, node, topn, INT2FIX(2));
ADD_INSN(ret, node, swap);
ADD_INSN1(ret, node, setn, INT2FIX(3));
ADD_INSN(ret, node, pop);
}
ADD_INSN(ret, node, swap);
ADD_SEND_R(ret, node, idASET, FIXNUM_INC(argc, 1), NULL, INT2FIX(flag), keywords);
}
else if (keyword_len) {
ADD_INSN(ret, node, dup);
ADD_INSN1(ret, node, opt_reverse, INT2FIX(keyword_len+boff+2));
ADD_INSN1(ret, node, opt_reverse, INT2FIX(keyword_len+boff+1));
ADD_INSN(ret, node, pop);
ADD_SEND_R(ret, node, idASET, FIXNUM_INC(argc, 1), NULL, INT2FIX(flag), keywords);
}
else {
if (boff > 0)
ADD_INSN(ret, node, swap);
ADD_SEND_R(ret, node, idASET, FIXNUM_INC(argc, 1), NULL, INT2FIX(flag), keywords);
}
ADD_INSN(ret, node, pop);
}
return COMPILE_OK;
}
static int
compile_op_asgn2(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, int popped)
{
const int line = nd_line(node);
ID atype = RNODE_OP_ASGN2(node)->nd_mid;
ID vid = RNODE_OP_ASGN2(node)->nd_vid, aid = rb_id_attrset(vid);
int asgnflag;
LABEL *lfin = NEW_LABEL(line);
LABEL *lcfin = NEW_LABEL(line);
LABEL *lskip = 0;
/*
class C; attr_accessor :c; end
r = C.new
r.a &&= v # asgn2
eval r # r
dup # r r
eval r.a # r o
# or
dup # r o o
if lcfin # r o
pop # r
eval v # r v
swap # v r
topn 1 # v r v
send a= # v ?
jump lfin # v ?
lcfin: # r o
swap # o r
lfin: # o ?
pop # o
# or (popped)
if lcfin # r
eval v # r v
send a= # ?
jump lfin # ?
lcfin: # r
lfin: # ?
pop #
# and
dup # r o o
unless lcfin
pop # r
eval v # r v
swap # v r
topn 1 # v r v
send a= # v ?
jump lfin # v ?
# others
eval v # r o v
send ?? # r w
send a= # w
*/
asgnflag = COMPILE_RECV(ret, "NODE_OP_ASGN2#recv", node, RNODE_OP_ASGN2(node)->nd_recv);
CHECK(asgnflag != -1);
if (RNODE_OP_ASGN2(node)->nd_aid) {
lskip = NEW_LABEL(line);
ADD_INSN(ret, node, dup);
ADD_INSNL(ret, node, branchnil, lskip);
}
ADD_INSN(ret, node, dup);
ADD_SEND_WITH_FLAG(ret, node, vid, INT2FIX(0), INT2FIX(asgnflag));
if (atype == idOROP || atype == idANDOP) {
if (!popped) {
ADD_INSN(ret, node, dup);
}
if (atype == idOROP) {
ADD_INSNL(ret, node, branchif, lcfin);
}
else { /* idANDOP */
ADD_INSNL(ret, node, branchunless, lcfin);
}
if (!popped) {
ADD_INSN(ret, node, pop);
}
CHECK(COMPILE(ret, "NODE_OP_ASGN2 val", RNODE_OP_ASGN2(node)->nd_value));
if (!popped) {
ADD_INSN(ret, node, swap);
ADD_INSN1(ret, node, topn, INT2FIX(1));
}
ADD_SEND_WITH_FLAG(ret, node, aid, INT2FIX(1), INT2FIX(asgnflag));
ADD_INSNL(ret, node, jump, lfin);
ADD_LABEL(ret, lcfin);
if (!popped) {
ADD_INSN(ret, node, swap);
}
ADD_LABEL(ret, lfin);
}
else {
CHECK(COMPILE(ret, "NODE_OP_ASGN2 val", RNODE_OP_ASGN2(node)->nd_value));
ADD_SEND(ret, node, atype, INT2FIX(1));
if (!popped) {
ADD_INSN(ret, node, swap);
ADD_INSN1(ret, node, topn, INT2FIX(1));
}
ADD_SEND_WITH_FLAG(ret, node, aid, INT2FIX(1), INT2FIX(asgnflag));
}
if (lskip && popped) {
ADD_LABEL(ret, lskip);
}
ADD_INSN(ret, node, pop);
if (lskip && !popped) {
ADD_LABEL(ret, lskip);
}
return COMPILE_OK;
}
static int
compile_op_cdecl(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, int popped)
{
const int line = nd_line(node);
LABEL *lfin = 0;
LABEL *lassign = 0;
ID mid;
switch (nd_type(RNODE_OP_CDECL(node)->nd_head)) {
case NODE_COLON3:
ADD_INSN1(ret, node, putobject, rb_cObject);
break;
case NODE_COLON2:
CHECK(COMPILE(ret, "NODE_OP_CDECL/colon2#nd_head", RNODE_COLON2(RNODE_OP_CDECL(node)->nd_head)->nd_head));
break;
default:
COMPILE_ERROR(ERROR_ARGS "%s: invalid node in NODE_OP_CDECL",
ruby_node_name(nd_type(RNODE_OP_CDECL(node)->nd_head)));
return COMPILE_NG;
}
mid = get_node_colon_nd_mid(RNODE_OP_CDECL(node)->nd_head);
/* cref */
if (RNODE_OP_CDECL(node)->nd_aid == idOROP) {
lassign = NEW_LABEL(line);
ADD_INSN(ret, node, dup); /* cref cref */
ADD_INSN3(ret, node, defined, INT2FIX(DEFINED_CONST_FROM),
ID2SYM(mid), Qtrue); /* cref bool */
ADD_INSNL(ret, node, branchunless, lassign); /* cref */
}
ADD_INSN(ret, node, dup); /* cref cref */
ADD_INSN1(ret, node, putobject, Qtrue);
ADD_INSN1(ret, node, getconstant, ID2SYM(mid)); /* cref obj */
if (RNODE_OP_CDECL(node)->nd_aid == idOROP || RNODE_OP_CDECL(node)->nd_aid == idANDOP) {
lfin = NEW_LABEL(line);
if (!popped) ADD_INSN(ret, node, dup); /* cref [obj] obj */
if (RNODE_OP_CDECL(node)->nd_aid == idOROP)
ADD_INSNL(ret, node, branchif, lfin);
else /* idANDOP */
ADD_INSNL(ret, node, branchunless, lfin);
/* cref [obj] */
if (!popped) ADD_INSN(ret, node, pop); /* cref */
if (lassign) ADD_LABEL(ret, lassign);
CHECK(COMPILE(ret, "NODE_OP_CDECL#nd_value", RNODE_OP_CDECL(node)->nd_value));
/* cref value */
if (popped)
ADD_INSN1(ret, node, topn, INT2FIX(1)); /* cref value cref */
else {
ADD_INSN1(ret, node, dupn, INT2FIX(2)); /* cref value cref value */
ADD_INSN(ret, node, swap); /* cref value value cref */
}
ADD_INSN1(ret, node, setconstant, ID2SYM(mid)); /* cref [value] */
ADD_LABEL(ret, lfin); /* cref [value] */
if (!popped) ADD_INSN(ret, node, swap); /* [value] cref */
ADD_INSN(ret, node, pop); /* [value] */
}
else {
CHECK(COMPILE(ret, "NODE_OP_CDECL#nd_value", RNODE_OP_CDECL(node)->nd_value));
/* cref obj value */
ADD_CALL(ret, node, RNODE_OP_CDECL(node)->nd_aid, INT2FIX(1));
/* cref value */
ADD_INSN(ret, node, swap); /* value cref */
if (!popped) {
ADD_INSN1(ret, node, topn, INT2FIX(1)); /* value cref value */
ADD_INSN(ret, node, swap); /* value value cref */
}
ADD_INSN1(ret, node, setconstant, ID2SYM(mid));
}
return COMPILE_OK;
}
static int
compile_op_log(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, int popped, const enum node_type type)
{
const int line = nd_line(node);
LABEL *lfin = NEW_LABEL(line);
LABEL *lassign;
if (type == NODE_OP_ASGN_OR && !nd_type_p(RNODE_OP_ASGN_OR(node)->nd_head, NODE_IVAR)) {
LABEL *lfinish[2];
lfinish[0] = lfin;
lfinish[1] = 0;
defined_expr(iseq, ret, RNODE_OP_ASGN_OR(node)->nd_head, lfinish, Qfalse);
lassign = lfinish[1];
if (!lassign) {
lassign = NEW_LABEL(line);
}
ADD_INSNL(ret, node, branchunless, lassign);
}
else {
lassign = NEW_LABEL(line);
}
CHECK(COMPILE(ret, "NODE_OP_ASGN_AND/OR#nd_head", RNODE_OP_ASGN_OR(node)->nd_head));
if (!popped) {
ADD_INSN(ret, node, dup);
}
if (type == NODE_OP_ASGN_AND) {
ADD_INSNL(ret, node, branchunless, lfin);
}
else {
ADD_INSNL(ret, node, branchif, lfin);
}
if (!popped) {
ADD_INSN(ret, node, pop);
}
ADD_LABEL(ret, lassign);
CHECK(COMPILE_(ret, "NODE_OP_ASGN_AND/OR#nd_value", RNODE_OP_ASGN_OR(node)->nd_value, popped));
ADD_LABEL(ret, lfin);
return COMPILE_OK;
}
static int
compile_super(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, int popped, const enum node_type type)
{
struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
DECL_ANCHOR(args);
int argc;
unsigned int flag = 0;
struct rb_callinfo_kwarg *keywords = NULL;
const rb_iseq_t *parent_block = ISEQ_COMPILE_DATA(iseq)->current_block;
INIT_ANCHOR(args);
ISEQ_COMPILE_DATA(iseq)->current_block = NULL;
if (type == NODE_SUPER) {
VALUE vargc = setup_args(iseq, args, RNODE_SUPER(node)->nd_args, &flag, &keywords);
CHECK(!NIL_P(vargc));
argc = FIX2INT(vargc);
if ((flag & VM_CALL_ARGS_BLOCKARG) && (flag & VM_CALL_KW_SPLAT) && !(flag & VM_CALL_KW_SPLAT_MUT)) {
ADD_INSN(args, node, splatkw);
}
}
else {
/* NODE_ZSUPER */
int i;
const rb_iseq_t *liseq = body->local_iseq;
const struct rb_iseq_constant_body *const local_body = ISEQ_BODY(liseq);
const struct rb_iseq_param_keyword *const local_kwd = local_body->param.keyword;
int lvar_level = get_lvar_level(iseq);
argc = local_body->param.lead_num;
/* normal arguments */
for (i = 0; i < local_body->param.lead_num; i++) {
int idx = local_body->local_table_size - i;
ADD_GETLOCAL(args, node, idx, lvar_level);
}
if (local_body->param.flags.has_opt) {
/* optional arguments */
int j;
for (j = 0; j < local_body->param.opt_num; j++) {
int idx = local_body->local_table_size - (i + j);
ADD_GETLOCAL(args, node, idx, lvar_level);
}
i += j;
argc = i;
}
if (local_body->param.flags.has_rest) {
/* rest argument */
int idx = local_body->local_table_size - local_body->param.rest_start;
ADD_GETLOCAL(args, node, idx, lvar_level);
ADD_INSN1(args, node, splatarray, Qfalse);
argc = local_body->param.rest_start + 1;
flag |= VM_CALL_ARGS_SPLAT;
}
if (local_body->param.flags.has_post) {
/* post arguments */
int post_len = local_body->param.post_num;
int post_start = local_body->param.post_start;
if (local_body->param.flags.has_rest) {
int j;
for (j=0; j<post_len; j++) {
int idx = local_body->local_table_size - (post_start + j);
ADD_GETLOCAL(args, node, idx, lvar_level);
}
ADD_INSN1(args, node, newarray, INT2FIX(j));
ADD_INSN (args, node, concatarray);
/* argc is settled at above */
}
else {
int j;
for (j=0; j<post_len; j++) {
int idx = local_body->local_table_size - (post_start + j);
ADD_GETLOCAL(args, node, idx, lvar_level);
}
argc = post_len + post_start;
}
}
if (local_body->param.flags.has_kw) { /* TODO: support keywords */
int local_size = local_body->local_table_size;
argc++;
ADD_INSN1(args, node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
if (local_body->param.flags.has_kwrest) {
int idx = local_body->local_table_size - local_kwd->rest_start;
ADD_GETLOCAL(args, node, idx, lvar_level);
assert(local_kwd->num > 0);
ADD_SEND (args, node, rb_intern("dup"), INT2FIX(0));
}
else {
ADD_INSN1(args, node, newhash, INT2FIX(0));
}
for (i = 0; i < local_kwd->num; ++i) {
ID id = local_kwd->table[i];
int idx = local_size - get_local_var_idx(liseq, id);
ADD_INSN1(args, node, putobject, ID2SYM(id));
ADD_GETLOCAL(args, node, idx, lvar_level);
}
ADD_SEND(args, node, id_core_hash_merge_ptr, INT2FIX(i * 2 + 1));
flag |= VM_CALL_KW_SPLAT| VM_CALL_KW_SPLAT_MUT;
}
else if (local_body->param.flags.has_kwrest) {
int idx = local_body->local_table_size - local_kwd->rest_start;
ADD_GETLOCAL(args, node, idx, lvar_level);
argc++;
flag |= VM_CALL_KW_SPLAT;
}
}
flag |= VM_CALL_SUPER | VM_CALL_FCALL;
if (type == NODE_ZSUPER) flag |= VM_CALL_ZSUPER;
ADD_INSN(ret, node, putself);
ADD_SEQ(ret, args);
ADD_INSN2(ret, node, invokesuper,
new_callinfo(iseq, 0, argc, flag, keywords, parent_block != NULL),
parent_block);
if (popped) {
ADD_INSN(ret, node, pop);
}
return COMPILE_OK;
}
static int
compile_yield(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, int popped)
{
DECL_ANCHOR(args);
VALUE argc;
unsigned int flag = 0;
struct rb_callinfo_kwarg *keywords = NULL;
INIT_ANCHOR(args);
switch (ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->type) {
case ISEQ_TYPE_TOP:
case ISEQ_TYPE_MAIN:
case ISEQ_TYPE_CLASS:
COMPILE_ERROR(ERROR_ARGS "Invalid yield");
return COMPILE_NG;
default: /* valid */;
}
if (RNODE_YIELD(node)->nd_head) {
argc = setup_args(iseq, args, RNODE_YIELD(node)->nd_head, &flag, &keywords);
CHECK(!NIL_P(argc));
}
else {
argc = INT2FIX(0);
}
ADD_SEQ(ret, args);
ADD_INSN1(ret, node, invokeblock, new_callinfo(iseq, 0, FIX2INT(argc), flag, keywords, FALSE));
if (popped) {
ADD_INSN(ret, node, pop);
}
int level = 0;
const rb_iseq_t *tmp_iseq = iseq;
for (; tmp_iseq != ISEQ_BODY(iseq)->local_iseq; level++ ) {
tmp_iseq = ISEQ_BODY(tmp_iseq)->parent_iseq;
}
if (level > 0) access_outer_variables(iseq, level, rb_intern("yield"), true);
return COMPILE_OK;
}
static int
compile_match(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, int popped, const enum node_type type)
{
DECL_ANCHOR(recv);
DECL_ANCHOR(val);
INIT_ANCHOR(recv);
INIT_ANCHOR(val);
switch ((int)type) {
case NODE_MATCH:
ADD_INSN1(recv, node, putobject, RNODE_MATCH(node)->nd_lit);
ADD_INSN2(val, node, getspecial, INT2FIX(0),
INT2FIX(0));
break;
case NODE_MATCH2:
CHECK(COMPILE(recv, "receiver", RNODE_MATCH2(node)->nd_recv));
CHECK(COMPILE(val, "value", RNODE_MATCH2(node)->nd_value));
break;
case NODE_MATCH3:
CHECK(COMPILE(recv, "receiver", RNODE_MATCH3(node)->nd_value));
CHECK(COMPILE(val, "value", RNODE_MATCH3(node)->nd_recv));
break;
}
ADD_SEQ(ret, recv);
ADD_SEQ(ret, val);
ADD_SEND(ret, node, idEqTilde, INT2FIX(1));
if (nd_type_p(node, NODE_MATCH2) && RNODE_MATCH2(node)->nd_args) {
compile_named_capture_assign(iseq, ret, RNODE_MATCH2(node)->nd_args);
}
if (popped) {
ADD_INSN(ret, node, pop);
}
return COMPILE_OK;
}
static int
compile_colon2(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, int popped)
{
if (rb_is_const_id(RNODE_COLON2(node)->nd_mid)) {
/* constant */
VALUE segments;
if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache &&
(segments = collect_const_segments(iseq, node))) {
ISEQ_BODY(iseq)->ic_size++;
ADD_INSN1(ret, node, opt_getconstant_path, segments);
RB_OBJ_WRITTEN(iseq, Qundef, segments);
}
else {
/* constant */
DECL_ANCHOR(pref);
DECL_ANCHOR(body);
INIT_ANCHOR(pref);
INIT_ANCHOR(body);
CHECK(compile_const_prefix(iseq, node, pref, body));
if (LIST_INSN_SIZE_ZERO(pref)) {
ADD_INSN(ret, node, putnil);
ADD_SEQ(ret, body);
}
else {
ADD_SEQ(ret, pref);
ADD_SEQ(ret, body);
}
}
}
else {
/* function call */
ADD_CALL_RECEIVER(ret, node);
CHECK(COMPILE(ret, "colon2#nd_head", RNODE_COLON2(node)->nd_head));
ADD_CALL(ret, node, RNODE_COLON2(node)->nd_mid, INT2FIX(1));
}
if (popped) {
ADD_INSN(ret, node, pop);
}
return COMPILE_OK;
}
static int
compile_colon3(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, int popped)
{
debugi("colon3#nd_mid", RNODE_COLON3(node)->nd_mid);
/* add cache insn */
if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache) {
ISEQ_BODY(iseq)->ic_size++;
VALUE segments = rb_ary_new_from_args(2, ID2SYM(idNULL), ID2SYM(RNODE_COLON3(node)->nd_mid));
ADD_INSN1(ret, node, opt_getconstant_path, segments);
RB_OBJ_WRITTEN(iseq, Qundef, segments);
}
else {
ADD_INSN1(ret, node, putobject, rb_cObject);
ADD_INSN1(ret, node, putobject, Qtrue);
ADD_INSN1(ret, node, getconstant, ID2SYM(RNODE_COLON3(node)->nd_mid));
}
if (popped) {
ADD_INSN(ret, node, pop);
}
return COMPILE_OK;
}
static int
compile_dots(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, int popped, const int excl)
{
VALUE flag = INT2FIX(excl);
const NODE *b = RNODE_DOT2(node)->nd_beg;
const NODE *e = RNODE_DOT2(node)->nd_end;
if (optimizable_range_item_p(b) && optimizable_range_item_p(e)) {
if (!popped) {
VALUE bv = nd_type_p(b, NODE_LIT) ? RNODE_LIT(b)->nd_lit : Qnil;
VALUE ev = nd_type_p(e, NODE_LIT) ? RNODE_LIT(e)->nd_lit : Qnil;
VALUE val = rb_range_new(bv, ev, excl);
ADD_INSN1(ret, node, putobject, val);
RB_OBJ_WRITTEN(iseq, Qundef, val);
}
}
else {
CHECK(COMPILE_(ret, "min", b, popped));
CHECK(COMPILE_(ret, "max", e, popped));
if (!popped) {
ADD_INSN1(ret, node, newrange, flag);
}
}
return COMPILE_OK;
}
static int
compile_errinfo(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, int popped)
{
if (!popped) {
if (ISEQ_BODY(iseq)->type == ISEQ_TYPE_RESCUE) {
ADD_GETLOCAL(ret, node, LVAR_ERRINFO, 0);
}
else {
const rb_iseq_t *ip = iseq;
int level = 0;
while (ip) {
if (ISEQ_BODY(ip)->type == ISEQ_TYPE_RESCUE) {
break;
}
ip = ISEQ_BODY(ip)->parent_iseq;
level++;
}
if (ip) {
ADD_GETLOCAL(ret, node, LVAR_ERRINFO, level);
}
else {
ADD_INSN(ret, node, putnil);
}
}
}
return COMPILE_OK;
}
static int
compile_kw_arg(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, int popped)
{
struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
LABEL *end_label = NEW_LABEL(nd_line(node));
const NODE *default_value = get_nd_value(RNODE_KW_ARG(node)->nd_body);
if (default_value == NODE_SPECIAL_REQUIRED_KEYWORD) {
/* required argument. do nothing */
COMPILE_ERROR(ERROR_ARGS "unreachable");
return COMPILE_NG;
}
else if (nd_type_p(default_value, NODE_LIT) ||
nd_type_p(default_value, NODE_NIL) ||
nd_type_p(default_value, NODE_TRUE) ||
nd_type_p(default_value, NODE_FALSE)) {
COMPILE_ERROR(ERROR_ARGS "unreachable");
return COMPILE_NG;
}
else {
/* if keywordcheck(_kw_bits, nth_keyword)
* kw = default_value
* end
*/
int kw_bits_idx = body->local_table_size - body->param.keyword->bits_start;
int keyword_idx = body->param.keyword->num;
ADD_INSN2(ret, node, checkkeyword, INT2FIX(kw_bits_idx + VM_ENV_DATA_SIZE - 1), INT2FIX(keyword_idx));
ADD_INSNL(ret, node, branchif, end_label);
CHECK(COMPILE_POPPED(ret, "keyword default argument", RNODE_KW_ARG(node)->nd_body));
ADD_LABEL(ret, end_label);
}
return COMPILE_OK;
}
static int
compile_attrasgn(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, int popped)
{
DECL_ANCHOR(recv);
DECL_ANCHOR(args);
unsigned int flag = 0;
ID mid = RNODE_ATTRASGN(node)->nd_mid;
VALUE argc;
LABEL *else_label = NULL;
VALUE branches = Qfalse;
struct rb_callinfo_kwarg *keywords = NULL;
/* optimization shortcut
* obj["literal"] = value -> opt_aset_with(obj, "literal", value)
*/
if (!ISEQ_COMPILE_DATA(iseq)->in_masgn &&
mid == idASET && !private_recv_p(node) && RNODE_ATTRASGN(node)->nd_args &&
nd_type_p(RNODE_ATTRASGN(node)->nd_args, NODE_LIST) && RNODE_LIST(RNODE_ATTRASGN(node)->nd_args)->as.nd_alen == 2 &&
nd_type_p(RNODE_LIST(RNODE_ATTRASGN(node)->nd_args)->nd_head, NODE_STR) &&
ISEQ_COMPILE_DATA(iseq)->current_block == NULL &&
!ISEQ_COMPILE_DATA(iseq)->option->frozen_string_literal &&
ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction)
{
VALUE str = rb_fstring(RNODE_STR(RNODE_LIST(RNODE_ATTRASGN(node)->nd_args)->nd_head)->nd_lit);
CHECK(COMPILE(ret, "recv", RNODE_ATTRASGN(node)->nd_recv));
CHECK(COMPILE(ret, "value", RNODE_LIST(RNODE_LIST(RNODE_ATTRASGN(node)->nd_args)->nd_next)->nd_head));
if (!popped) {
ADD_INSN(ret, node, swap);
ADD_INSN1(ret, node, topn, INT2FIX(1));
}
ADD_INSN2(ret, node, opt_aset_with, str,
new_callinfo(iseq, idASET, 2, 0, NULL, FALSE));
RB_OBJ_WRITTEN(iseq, Qundef, str);
ADD_INSN(ret, node, pop);
return COMPILE_OK;
}
INIT_ANCHOR(recv);
INIT_ANCHOR(args);
argc = setup_args(iseq, args, RNODE_ATTRASGN(node)->nd_args, &flag, &keywords);
CHECK(!NIL_P(argc));
int asgnflag = COMPILE_RECV(recv, "recv", node, RNODE_ATTRASGN(node)->nd_recv);
CHECK(asgnflag != -1);
flag |= (unsigned int)asgnflag;
debugp_param("argc", argc);
debugp_param("nd_mid", ID2SYM(mid));
if (!rb_is_attrset_id(mid)) {
/* safe nav attr */
mid = rb_id_attrset(mid);
else_label = qcall_branch_start(iseq, recv, &branches, node, node);
}
if (!popped) {
ADD_INSN(ret, node, putnil);
ADD_SEQ(ret, recv);
ADD_SEQ(ret, args);
if (flag & VM_CALL_ARGS_BLOCKARG) {
ADD_INSN1(ret, node, topn, INT2FIX(1));
if (flag & VM_CALL_ARGS_SPLAT) {
ADD_INSN1(ret, node, putobject, INT2FIX(-1));
ADD_SEND_WITH_FLAG(ret, node, idAREF, INT2FIX(1), INT2FIX(asgnflag));
}
ADD_INSN1(ret, node, setn, FIXNUM_INC(argc, 3));
ADD_INSN (ret, node, pop);
}
else if (flag & VM_CALL_ARGS_SPLAT) {
ADD_INSN(ret, node, dup);
ADD_INSN1(ret, node, putobject, INT2FIX(-1));
ADD_SEND_WITH_FLAG(ret, node, idAREF, INT2FIX(1), INT2FIX(asgnflag));
ADD_INSN1(ret, node, setn, FIXNUM_INC(argc, 2));
ADD_INSN (ret, node, pop);
}
else {
ADD_INSN1(ret, node, setn, FIXNUM_INC(argc, 1));
}
}
else {
ADD_SEQ(ret, recv);
ADD_SEQ(ret, args);
}
ADD_SEND_WITH_FLAG(ret, node, mid, argc, INT2FIX(flag));
qcall_branch_end(iseq, ret, else_label, branches, node, node);
ADD_INSN(ret, node, pop);
return COMPILE_OK;
}
static int iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, int popped);
/**
compile each node
self: InstructionSequence
node: Ruby compiled node
popped: This node will be popped
*/
static int
iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *ret, const NODE *node, int popped)
{
if (node == 0) {
if (!popped) {
int lineno = ISEQ_COMPILE_DATA(iseq)->last_line;
if (lineno == 0) lineno = FIX2INT(rb_iseq_first_lineno(iseq));
debugs("node: NODE_NIL(implicit)\n");
NODE dummy_line_node = generate_dummy_line_node(lineno, -1);
ADD_INSN(ret, &dummy_line_node, putnil);
}
return COMPILE_OK;
}
return iseq_compile_each0(iseq, ret, node, popped);
}
static int
iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, int popped)
{
const int line = (int)nd_line(node);
const enum node_type type = nd_type(node);
struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
if (ISEQ_COMPILE_DATA(iseq)->last_line == line) {
/* ignore */
}
else {
if (nd_fl_newline(node)) {
int event = RUBY_EVENT_LINE;
ISEQ_COMPILE_DATA(iseq)->last_line = line;
if (ISEQ_COVERAGE(iseq) && ISEQ_LINE_COVERAGE(iseq)) {
event |= RUBY_EVENT_COVERAGE_LINE;
}
ADD_TRACE(ret, event);
}
}
debug_node_start(node);
#undef BEFORE_RETURN
#define BEFORE_RETURN debug_node_end()
switch (type) {
case NODE_BLOCK:
CHECK(compile_block(iseq, ret, node, popped));
break;
case NODE_IF:
case NODE_UNLESS:
CHECK(compile_if(iseq, ret, node, popped, type));
break;
case NODE_CASE:
CHECK(compile_case(iseq, ret, node, popped));
break;
case NODE_CASE2:
CHECK(compile_case2(iseq, ret, node, popped));
break;
case NODE_CASE3:
CHECK(compile_case3(iseq, ret, node, popped));
break;
case NODE_WHILE:
case NODE_UNTIL:
CHECK(compile_loop(iseq, ret, node, popped, type));
break;
case NODE_FOR:
case NODE_ITER:
CHECK(compile_iter(iseq, ret, node, popped));
break;
case NODE_FOR_MASGN:
CHECK(compile_for_masgn(iseq, ret, node, popped));
break;
case NODE_BREAK:
CHECK(compile_break(iseq, ret, node, popped));
break;
case NODE_NEXT:
CHECK(compile_next(iseq, ret, node, popped));
break;
case NODE_REDO:
CHECK(compile_redo(iseq, ret, node, popped));
break;
case NODE_RETRY:
CHECK(compile_retry(iseq, ret, node, popped));
break;
case NODE_BEGIN:{
CHECK(COMPILE_(ret, "NODE_BEGIN", RNODE_BEGIN(node)->nd_body, popped));
break;
}
case NODE_RESCUE:
CHECK(compile_rescue(iseq, ret, node, popped));
break;
case NODE_RESBODY:
CHECK(compile_resbody(iseq, ret, node, popped));
break;
case NODE_ENSURE:
CHECK(compile_ensure(iseq, ret, node, popped));
break;
case NODE_AND:
case NODE_OR:{
LABEL *end_label = NEW_LABEL(line);
CHECK(COMPILE(ret, "nd_1st", RNODE_OR(node)->nd_1st));
if (!popped) {
ADD_INSN(ret, node, dup);
}
if (type == NODE_AND) {
ADD_INSNL(ret, node, branchunless, end_label);
}
else {
ADD_INSNL(ret, node, branchif, end_label);
}
if (!popped) {
ADD_INSN(ret, node, pop);
}
CHECK(COMPILE_(ret, "nd_2nd", RNODE_OR(node)->nd_2nd, popped));
ADD_LABEL(ret, end_label);
break;
}
case NODE_MASGN:{
bool prev_in_masgn = ISEQ_COMPILE_DATA(iseq)->in_masgn;
ISEQ_COMPILE_DATA(iseq)->in_masgn = true;
compile_massign(iseq, ret, node, popped);
ISEQ_COMPILE_DATA(iseq)->in_masgn = prev_in_masgn;
break;
}
case NODE_LASGN:{
ID id = RNODE_LASGN(node)->nd_vid;
int idx = ISEQ_BODY(body->local_iseq)->local_table_size - get_local_var_idx(iseq, id);
debugs("lvar: %s idx: %d\n", rb_id2name(id), idx);
CHECK(COMPILE(ret, "rvalue", RNODE_LASGN(node)->nd_value));
if (!popped) {
ADD_INSN(ret, node, dup);
}
ADD_SETLOCAL(ret, node, idx, get_lvar_level(iseq));
break;
}
case NODE_DASGN: {
int idx, lv, ls;
ID id = RNODE_DASGN(node)->nd_vid;
CHECK(COMPILE(ret, "dvalue", RNODE_DASGN(node)->nd_value));
debugi("dassn id", rb_id2str(id) ? id : '*');
if (!popped) {
ADD_INSN(ret, node, dup);
}
idx = get_dyna_var_idx(iseq, id, &lv, &ls);
if (idx < 0) {
COMPILE_ERROR(ERROR_ARGS "NODE_DASGN: unknown id (%"PRIsVALUE")",
rb_id2str(id));
goto ng;
}
ADD_SETLOCAL(ret, node, ls - idx, lv);
break;
}
case NODE_GASGN:{
CHECK(COMPILE(ret, "lvalue", RNODE_GASGN(node)->nd_value));
if (!popped) {
ADD_INSN(ret, node, dup);
}
ADD_INSN1(ret, node, setglobal, ID2SYM(RNODE_GASGN(node)->nd_vid));
break;
}
case NODE_IASGN:{
CHECK(COMPILE(ret, "lvalue", RNODE_IASGN(node)->nd_value));
if (!popped) {
ADD_INSN(ret, node, dup);
}
ADD_INSN2(ret, node, setinstancevariable,
ID2SYM(RNODE_IASGN(node)->nd_vid),
get_ivar_ic_value(iseq,RNODE_IASGN(node)->nd_vid));
break;
}
case NODE_CDECL:{
if (RNODE_CDECL(node)->nd_vid) {
CHECK(COMPILE(ret, "lvalue", RNODE_CDECL(node)->nd_value));
if (!popped) {
ADD_INSN(ret, node, dup);
}
ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
ADD_INSN1(ret, node, setconstant, ID2SYM(RNODE_CDECL(node)->nd_vid));
}
else {
compile_cpath(ret, iseq, RNODE_CDECL(node)->nd_else);
CHECK(COMPILE(ret, "lvalue", RNODE_CDECL(node)->nd_value));
ADD_INSN(ret, node, swap);
if (!popped) {
ADD_INSN1(ret, node, topn, INT2FIX(1));
ADD_INSN(ret, node, swap);
}
ADD_INSN1(ret, node, setconstant, ID2SYM(get_node_colon_nd_mid(RNODE_CDECL(node)->nd_else)));
}
break;
}
case NODE_CVASGN:{
CHECK(COMPILE(ret, "cvasgn val", RNODE_CVASGN(node)->nd_value));
if (!popped) {
ADD_INSN(ret, node, dup);
}
ADD_INSN2(ret, node, setclassvariable,
ID2SYM(RNODE_CVASGN(node)->nd_vid),
get_cvar_ic_value(iseq, RNODE_CVASGN(node)->nd_vid));
break;
}
case NODE_OP_ASGN1:
CHECK(compile_op_asgn1(iseq, ret, node, popped));
break;
case NODE_OP_ASGN2:
CHECK(compile_op_asgn2(iseq, ret, node, popped));
break;
case NODE_OP_CDECL:
CHECK(compile_op_cdecl(iseq, ret, node, popped));
break;
case NODE_OP_ASGN_AND:
case NODE_OP_ASGN_OR:
CHECK(compile_op_log(iseq, ret, node, popped, type));
break;
case NODE_CALL: /* obj.foo */
case NODE_OPCALL: /* foo[] */
if (compile_call_precheck_freeze(iseq, ret, node, node, popped) == TRUE) {
break;
}
case NODE_QCALL: /* obj&.foo */
case NODE_FCALL: /* foo() */
case NODE_VCALL: /* foo (variable or call) */
if (compile_call(iseq, ret, node, type, node, popped, false) == COMPILE_NG) {
goto ng;
}
break;
case NODE_SUPER:
case NODE_ZSUPER:
CHECK(compile_super(iseq, ret, node, popped, type));
break;
case NODE_LIST:{
CHECK(compile_array(iseq, ret, node, popped) >= 0);
break;
}
case NODE_ZLIST:{
if (!popped) {
ADD_INSN1(ret, node, newarray, INT2FIX(0));
}
break;
}
case NODE_HASH:
CHECK(compile_hash(iseq, ret, node, FALSE, popped) >= 0);
break;
case NODE_RETURN:
CHECK(compile_return(iseq, ret, node, popped));
break;
case NODE_YIELD:
CHECK(compile_yield(iseq, ret, node, popped));
break;
case NODE_LVAR:{
if (!popped) {
compile_lvar(iseq, ret, node, RNODE_LVAR(node)->nd_vid);
}
break;
}
case NODE_DVAR:{
int lv, idx, ls;
debugi("nd_vid", RNODE_DVAR(node)->nd_vid);
if (!popped) {
idx = get_dyna_var_idx(iseq, RNODE_DVAR(node)->nd_vid, &lv, &ls);
if (idx < 0) {
COMPILE_ERROR(ERROR_ARGS "unknown dvar (%"PRIsVALUE")",
rb_id2str(RNODE_DVAR(node)->nd_vid));
goto ng;
}
ADD_GETLOCAL(ret, node, ls - idx, lv);
}
break;
}
case NODE_GVAR:{
ADD_INSN1(ret, node, getglobal, ID2SYM(RNODE_GVAR(node)->nd_vid));
if (popped) {
ADD_INSN(ret, node, pop);
}
break;
}
case NODE_IVAR:{
debugi("nd_vid", RNODE_IVAR(node)->nd_vid);
if (!popped) {
ADD_INSN2(ret, node, getinstancevariable,
ID2SYM(RNODE_IVAR(node)->nd_vid),
get_ivar_ic_value(iseq, RNODE_IVAR(node)->nd_vid));
}
break;
}
case NODE_CONST:{
debugi("nd_vid", RNODE_CONST(node)->nd_vid);
if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache) {
body->ic_size++;
VALUE segments = rb_ary_new_from_args(1, ID2SYM(RNODE_CONST(node)->nd_vid));
ADD_INSN1(ret, node, opt_getconstant_path, segments);
RB_OBJ_WRITTEN(iseq, Qundef, segments);
}
else {
ADD_INSN(ret, node, putnil);
ADD_INSN1(ret, node, putobject, Qtrue);
ADD_INSN1(ret, node, getconstant, ID2SYM(RNODE_CONST(node)->nd_vid));
}
if (popped) {
ADD_INSN(ret, node, pop);
}
break;
}
case NODE_CVAR:{
if (!popped) {
ADD_INSN2(ret, node, getclassvariable,
ID2SYM(RNODE_CVAR(node)->nd_vid),
get_cvar_ic_value(iseq, RNODE_CVAR(node)->nd_vid));
}
break;
}
case NODE_NTH_REF:{
if (!popped) {
if (!RNODE_NTH_REF(node)->nd_nth) {
ADD_INSN(ret, node, putnil);
break;
}
ADD_INSN2(ret, node, getspecial, INT2FIX(1) /* '~' */,
INT2FIX(RNODE_NTH_REF(node)->nd_nth << 1));
}
break;
}
case NODE_BACK_REF:{
if (!popped) {
ADD_INSN2(ret, node, getspecial, INT2FIX(1) /* '~' */,
INT2FIX(0x01 | (RNODE_BACK_REF(node)->nd_nth << 1)));
}
break;
}
case NODE_MATCH:
case NODE_MATCH2:
case NODE_MATCH3:
CHECK(compile_match(iseq, ret, node, popped, type));
break;
case NODE_LIT:{
debugp_param("lit", RNODE_LIT(node)->nd_lit);
if (!popped) {
if (UNLIKELY(RNODE_LIT(node)->nd_lit == rb_mRubyVMFrozenCore)) {
ADD_INSN1(ret, node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE)); // [Bug #20569]
}
else {
ADD_INSN1(ret, node, putobject, RNODE_LIT(node)->nd_lit);
}
RB_OBJ_WRITTEN(iseq, Qundef, RNODE_LIT(node)->nd_lit);
}
break;
}
case NODE_STR:{
debugp_param("nd_lit", RNODE_STR(node)->nd_lit);
if (!popped) {
VALUE lit = RNODE_STR(node)->nd_lit;
if (!ISEQ_COMPILE_DATA(iseq)->option->frozen_string_literal) {
lit = rb_fstring(lit);
ADD_INSN1(ret, node, putstring, lit);
RB_OBJ_WRITTEN(iseq, Qundef, lit);
}
else {
if (ISEQ_COMPILE_DATA(iseq)->option->debug_frozen_string_literal || RTEST(ruby_debug)) {
VALUE debug_info = rb_ary_new_from_args(2, rb_iseq_path(iseq), INT2FIX(line));
lit = rb_str_dup(lit);
rb_ivar_set(lit, id_debug_created_info, rb_obj_freeze(debug_info));
lit = rb_str_freeze(lit);
}
else {
lit = rb_fstring(lit);
}
ADD_INSN1(ret, node, putobject, lit);
RB_OBJ_WRITTEN(iseq, Qundef, lit);
}
}
break;
}
case NODE_DSTR:{
compile_dstr(iseq, ret, node);
if (popped) {
ADD_INSN(ret, node, pop);
}
break;
}
case NODE_XSTR:{
ADD_CALL_RECEIVER(ret, node);
VALUE str = rb_fstring(RNODE_XSTR(node)->nd_lit);
ADD_INSN1(ret, node, putobject, str);
RB_OBJ_WRITTEN(iseq, Qundef, str);
ADD_CALL(ret, node, idBackquote, INT2FIX(1));
if (popped) {
ADD_INSN(ret, node, pop);
}
break;
}
case NODE_DXSTR:{
ADD_CALL_RECEIVER(ret, node);
compile_dstr(iseq, ret, node);
ADD_CALL(ret, node, idBackquote, INT2FIX(1));
if (popped) {
ADD_INSN(ret, node, pop);
}
break;
}
case NODE_EVSTR:
CHECK(compile_evstr(iseq, ret, RNODE_EVSTR(node)->nd_body, popped));
break;
case NODE_DREGX:
compile_dregx(iseq, ret, node, popped);
break;
case NODE_ONCE:{
int ic_index = body->ise_size++;
const rb_iseq_t *block_iseq;
block_iseq = NEW_CHILD_ISEQ(RNODE_ONCE(node)->nd_body, make_name_for_block(iseq), ISEQ_TYPE_PLAIN, line);
ADD_INSN2(ret, node, once, block_iseq, INT2FIX(ic_index));
RB_OBJ_WRITTEN(iseq, Qundef, (VALUE)block_iseq);
if (popped) {
ADD_INSN(ret, node, pop);
}
break;
}
case NODE_ARGSCAT:{
if (popped) {
CHECK(COMPILE(ret, "argscat head", RNODE_ARGSCAT(node)->nd_head));
ADD_INSN1(ret, node, splatarray, Qfalse);
ADD_INSN(ret, node, pop);
CHECK(COMPILE(ret, "argscat body", RNODE_ARGSCAT(node)->nd_body));
ADD_INSN1(ret, node, splatarray, Qfalse);
ADD_INSN(ret, node, pop);
}
else {
CHECK(COMPILE(ret, "argscat head", RNODE_ARGSCAT(node)->nd_head));
CHECK(COMPILE(ret, "argscat body", RNODE_ARGSCAT(node)->nd_body));
ADD_INSN(ret, node, concatarray);
}
break;
}
case NODE_ARGSPUSH:{
if (popped) {
CHECK(COMPILE(ret, "argspush head", RNODE_ARGSPUSH(node)->nd_head));
ADD_INSN1(ret, node, splatarray, Qfalse);
ADD_INSN(ret, node, pop);
CHECK(COMPILE_(ret, "argspush body", RNODE_ARGSPUSH(node)->nd_body, popped));
}
else {
CHECK(COMPILE(ret, "argspush head", RNODE_ARGSPUSH(node)->nd_head));
CHECK(compile_array_1(iseq, ret, RNODE_ARGSPUSH(node)->nd_body));
ADD_INSN(ret, node, concatarray);
}
break;
}
case NODE_SPLAT:{
CHECK(COMPILE(ret, "splat", RNODE_SPLAT(node)->nd_head));
ADD_INSN1(ret, node, splatarray, Qtrue);
if (popped) {
ADD_INSN(ret, node, pop);
}
break;
}
case NODE_DEFN:{
ID mid = RNODE_DEFN(node)->nd_mid;
const rb_iseq_t *method_iseq = NEW_ISEQ(RNODE_DEFN(node)->nd_defn,
rb_id2str(mid),
ISEQ_TYPE_METHOD, line);
debugp_param("defn/iseq", rb_iseqw_new(method_iseq));
ADD_INSN2(ret, node, definemethod, ID2SYM(mid), method_iseq);
RB_OBJ_WRITTEN(iseq, Qundef, (VALUE)method_iseq);
if (!popped) {
ADD_INSN1(ret, node, putobject, ID2SYM(mid));
}
break;
}
case NODE_DEFS:{
ID mid = RNODE_DEFS(node)->nd_mid;
const rb_iseq_t * singleton_method_iseq = NEW_ISEQ(RNODE_DEFS(node)->nd_defn,
rb_id2str(mid),
ISEQ_TYPE_METHOD, line);
debugp_param("defs/iseq", rb_iseqw_new(singleton_method_iseq));
CHECK(COMPILE(ret, "defs: recv", RNODE_DEFS(node)->nd_recv));
ADD_INSN2(ret, node, definesmethod, ID2SYM(mid), singleton_method_iseq);
RB_OBJ_WRITTEN(iseq, Qundef, (VALUE)singleton_method_iseq);
if (!popped) {
ADD_INSN1(ret, node, putobject, ID2SYM(mid));
}
break;
}
case NODE_ALIAS:{
ADD_INSN1(ret, node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
ADD_INSN1(ret, node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CBASE));
CHECK(COMPILE(ret, "alias arg1", RNODE_ALIAS(node)->nd_1st));
CHECK(COMPILE(ret, "alias arg2", RNODE_ALIAS(node)->nd_2nd));
ADD_SEND(ret, node, id_core_set_method_alias, INT2FIX(3));
if (popped) {
ADD_INSN(ret, node, pop);
}
break;
}
case NODE_VALIAS:{
ADD_INSN1(ret, node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
ADD_INSN1(ret, node, putobject, ID2SYM(RNODE_VALIAS(node)->nd_alias));
ADD_INSN1(ret, node, putobject, ID2SYM(RNODE_VALIAS(node)->nd_orig));
ADD_SEND(ret, node, id_core_set_variable_alias, INT2FIX(2));
if (popped) {
ADD_INSN(ret, node, pop);
}
break;
}
case NODE_UNDEF:{
ADD_INSN1(ret, node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
ADD_INSN1(ret, node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CBASE));
CHECK(COMPILE(ret, "undef arg", RNODE_UNDEF(node)->nd_undef));
ADD_SEND(ret, node, id_core_undef_method, INT2FIX(2));
if (popped) {
ADD_INSN(ret, node, pop);
}
break;
}
case NODE_CLASS:{
const rb_iseq_t *class_iseq = NEW_CHILD_ISEQ(RNODE_CLASS(node)->nd_body,
rb_str_freeze(rb_sprintf("<class:%"PRIsVALUE">", rb_id2str(get_node_colon_nd_mid(RNODE_CLASS(node)->nd_cpath)))),
ISEQ_TYPE_CLASS, line);
const int flags = VM_DEFINECLASS_TYPE_CLASS |
(RNODE_CLASS(node)->nd_super ? VM_DEFINECLASS_FLAG_HAS_SUPERCLASS : 0) |
compile_cpath(ret, iseq, RNODE_CLASS(node)->nd_cpath);
CHECK(COMPILE(ret, "super", RNODE_CLASS(node)->nd_super));
ADD_INSN3(ret, node, defineclass, ID2SYM(get_node_colon_nd_mid(RNODE_CLASS(node)->nd_cpath)), class_iseq, INT2FIX(flags));
RB_OBJ_WRITTEN(iseq, Qundef, (VALUE)class_iseq);
if (popped) {
ADD_INSN(ret, node, pop);
}
break;
}
case NODE_MODULE:{
const rb_iseq_t *module_iseq = NEW_CHILD_ISEQ(RNODE_MODULE(node)->nd_body,
rb_str_freeze(rb_sprintf("<module:%"PRIsVALUE">", rb_id2str(get_node_colon_nd_mid(RNODE_MODULE(node)->nd_cpath)))),
ISEQ_TYPE_CLASS, line);
const int flags = VM_DEFINECLASS_TYPE_MODULE |
compile_cpath(ret, iseq, RNODE_MODULE(node)->nd_cpath);
ADD_INSN (ret, node, putnil); /* dummy */
ADD_INSN3(ret, node, defineclass, ID2SYM(get_node_colon_nd_mid(RNODE_MODULE(node)->nd_cpath)), module_iseq, INT2FIX(flags));
RB_OBJ_WRITTEN(iseq, Qundef, (VALUE)module_iseq);
if (popped) {
ADD_INSN(ret, node, pop);
}
break;
}
case NODE_SCLASS:{
ID singletonclass;
const rb_iseq_t *singleton_class = NEW_ISEQ(RNODE_SCLASS(node)->nd_body, rb_fstring_lit("singleton class"),
ISEQ_TYPE_CLASS, line);
CHECK(COMPILE(ret, "sclass#recv", RNODE_SCLASS(node)->nd_recv));
ADD_INSN (ret, node, putnil);
CONST_ID(singletonclass, "singletonclass");
ADD_INSN3(ret, node, defineclass,
ID2SYM(singletonclass), singleton_class,
INT2FIX(VM_DEFINECLASS_TYPE_SINGLETON_CLASS));
RB_OBJ_WRITTEN(iseq, Qundef, (VALUE)singleton_class);
if (popped) {
ADD_INSN(ret, node, pop);
}
break;
}
case NODE_COLON2:
CHECK(compile_colon2(iseq, ret, node, popped));
break;
case NODE_COLON3:
CHECK(compile_colon3(iseq, ret, node, popped));
break;
case NODE_DOT2:
CHECK(compile_dots(iseq, ret, node, popped, FALSE));
break;
case NODE_DOT3:
CHECK(compile_dots(iseq, ret, node, popped, TRUE));
break;
case NODE_FLIP2:
case NODE_FLIP3:{
LABEL *lend = NEW_LABEL(line);
LABEL *ltrue = NEW_LABEL(line);
LABEL *lfalse = NEW_LABEL(line);
CHECK(compile_flip_flop(iseq, ret, node, type == NODE_FLIP2,
ltrue, lfalse));
ADD_LABEL(ret, ltrue);
ADD_INSN1(ret, node, putobject, Qtrue);
ADD_INSNL(ret, node, jump, lend);
ADD_LABEL(ret, lfalse);
ADD_INSN1(ret, node, putobject, Qfalse);
ADD_LABEL(ret, lend);
break;
}
case NODE_SELF:{
if (!popped) {
ADD_INSN(ret, node, putself);
}
break;
}
case NODE_NIL:{
if (!popped) {
ADD_INSN(ret, node, putnil);
}
break;
}
case NODE_TRUE:{
if (!popped) {
ADD_INSN1(ret, node, putobject, Qtrue);
}
break;
}
case NODE_FALSE:{
if (!popped) {
ADD_INSN1(ret, node, putobject, Qfalse);
}
break;
}
case NODE_ERRINFO:
CHECK(compile_errinfo(iseq, ret, node, popped));
break;
case NODE_DEFINED:
if (!popped) {
CHECK(compile_defined_expr(iseq, ret, node, Qtrue));
}
break;
case NODE_POSTEXE:{
/* compiled to:
* ONCE{ rb_mRubyVMFrozenCore::core#set_postexe{ ... } }
*/
int is_index = body->ise_size++;
struct rb_iseq_new_with_callback_callback_func *ifunc =
rb_iseq_new_with_callback_new_callback(build_postexe_iseq, RNODE_POSTEXE(node)->nd_body);
const rb_iseq_t *once_iseq =
new_child_iseq_with_callback(iseq, ifunc,
rb_fstring(make_name_for_block(iseq)), iseq, ISEQ_TYPE_BLOCK, line);
ADD_INSN2(ret, node, once, once_iseq, INT2FIX(is_index));
RB_OBJ_WRITTEN(iseq, Qundef, (VALUE)once_iseq);
if (popped) {
ADD_INSN(ret, node, pop);
}
break;
}
case NODE_KW_ARG:
CHECK(compile_kw_arg(iseq, ret, node, popped));
break;
case NODE_DSYM:{
compile_dstr(iseq, ret, node);
if (!popped) {
ADD_INSN(ret, node, intern);
}
else {
ADD_INSN(ret, node, pop);
}
break;
}
case NODE_ATTRASGN:
CHECK(compile_attrasgn(iseq, ret, node, popped));
break;
case NODE_LAMBDA:{
/* compile same as lambda{...} */
const rb_iseq_t *block = NEW_CHILD_ISEQ(RNODE_LAMBDA(node)->nd_body, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, line);
VALUE argc = INT2FIX(0);
ADD_INSN1(ret, node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
ADD_CALL_WITH_BLOCK(ret, node, idLambda, argc, block);
RB_OBJ_WRITTEN(iseq, Qundef, (VALUE)block);
if (popped) {
ADD_INSN(ret, node, pop);
}
break;
}
default:
UNKNOWN_NODE("iseq_compile_each", node, COMPILE_NG);
ng:
debug_node_end();
return COMPILE_NG;
}
debug_node_end();
return COMPILE_OK;
}
/***************************/
/* instruction information */
/***************************/
static int
insn_data_length(INSN *iobj)
{
return insn_len(iobj->insn_id);
}
static int
calc_sp_depth(int depth, INSN *insn)
{
return comptime_insn_stack_increase(depth, insn->insn_id, insn->operands);
}
static VALUE
opobj_inspect(VALUE obj)
{
if (!SPECIAL_CONST_P(obj) && !RBASIC_CLASS(obj)) {
switch (BUILTIN_TYPE(obj)) {
case T_STRING:
obj = rb_str_new_cstr(RSTRING_PTR(obj));
break;
case T_ARRAY:
obj = rb_ary_dup(obj);
break;
default:
break;
}
}
return rb_inspect(obj);
}
static VALUE
insn_data_to_s_detail(INSN *iobj)
{
VALUE str = rb_sprintf("%-20s ", insn_name(iobj->insn_id));
if (iobj->operands) {
const char *types = insn_op_types(iobj->insn_id);
int j;
for (j = 0; types[j]; j++) {
char type = types[j];
switch (type) {
case TS_OFFSET: /* label(destination position) */
{
LABEL *lobj = (LABEL *)OPERAND_AT(iobj, j);
rb_str_catf(str, LABEL_FORMAT, lobj->label_no);
break;
}
break;
case TS_ISEQ: /* iseq */
{
rb_iseq_t *iseq = (rb_iseq_t *)OPERAND_AT(iobj, j);
VALUE val = Qnil;
if (0 && iseq) { /* TODO: invalidate now */
val = (VALUE)iseq;
}
rb_str_concat(str, opobj_inspect(val));
}
break;
case TS_LINDEX:
case TS_NUM: /* ulong */
case TS_VALUE: /* VALUE */
{
VALUE v = OPERAND_AT(iobj, j);
if (!CLASS_OF(v))
rb_str_cat2(str, "<hidden>");
else {
rb_str_concat(str, opobj_inspect(v));
}
break;
}
case TS_ID: /* ID */
rb_str_concat(str, opobj_inspect(OPERAND_AT(iobj, j)));
break;
case TS_IC: /* inline cache */
rb_str_concat(str, opobj_inspect(OPERAND_AT(iobj, j)));
break;
case TS_IVC: /* inline ivar cache */
rb_str_catf(str, "<ivc:%d>", FIX2INT(OPERAND_AT(iobj, j)));
break;
case TS_ICVARC: /* inline cvar cache */
rb_str_catf(str, "<icvarc:%d>", FIX2INT(OPERAND_AT(iobj, j)));
break;
case TS_ISE: /* inline storage entry */
rb_str_catf(str, "<ise:%d>", FIX2INT(OPERAND_AT(iobj, j)));
break;
case TS_CALLDATA: /* we store these as call infos at compile time */
{
const struct rb_callinfo *ci = (struct rb_callinfo *)OPERAND_AT(iobj, j);
rb_str_cat2(str, "<calldata:");
if (vm_ci_mid(ci)) rb_str_catf(str, "%"PRIsVALUE, rb_id2str(vm_ci_mid(ci)));
rb_str_catf(str, ", %d>", vm_ci_argc(ci));
break;
}
case TS_CDHASH: /* case/when condition cache */
rb_str_cat2(str, "<ch>");
break;
case TS_FUNCPTR:
{
void *func = (void *)OPERAND_AT(iobj, j);
#ifdef HAVE_DLADDR
Dl_info info;
if (dladdr(func, &info) && info.dli_sname) {
rb_str_cat2(str, info.dli_sname);
break;
}
#endif
rb_str_catf(str, "<%p>", func);
}
break;
case TS_BUILTIN:
rb_str_cat2(str, "<TS_BUILTIN>");
break;
default:{
rb_raise(rb_eSyntaxError, "unknown operand type: %c", type);
}
}
if (types[j + 1]) {
rb_str_cat2(str, ", ");
}
}
}
return str;
}
static void
dump_disasm_list(const LINK_ELEMENT *link)
{
dump_disasm_list_with_cursor(link, NULL, NULL);
}
static void
dump_disasm_list_with_cursor(const LINK_ELEMENT *link, const LINK_ELEMENT *curr, const LABEL *dest)
{
int pos = 0;
INSN *iobj;
LABEL *lobj;
VALUE str;
printf("-- raw disasm--------\n");
while (link) {
if (curr) printf(curr == link ? "*" : " ");
switch (link->type) {
case ISEQ_ELEMENT_INSN:
{
iobj = (INSN *)link;
str = insn_data_to_s_detail(iobj);
printf(" %04d %-65s(%4u)\n", pos, StringValueCStr(str), iobj->insn_info.line_no);
pos += insn_data_length(iobj);
break;
}
case ISEQ_ELEMENT_LABEL:
{
lobj = (LABEL *)link;
printf(LABEL_FORMAT" [sp: %d]%s\n", lobj->label_no, lobj->sp,
dest == lobj ? " <---" : "");
break;
}
case ISEQ_ELEMENT_TRACE:
{
TRACE *trace = (TRACE *)link;
printf(" trace: %0x\n", trace->event);
break;
}
case ISEQ_ELEMENT_ADJUST:
{
ADJUST *adjust = (ADJUST *)link;
printf(" adjust: [label: %d]\n", adjust->label ? adjust->label->label_no : -1);
break;
}
default:
/* ignore */
rb_raise(rb_eSyntaxError, "dump_disasm_list error: %ld\n", FIX2LONG(link->type));
}
link = link->next;
}
printf("---------------------\n");
fflush(stdout);
}
int
rb_insn_len(VALUE insn)
{
return insn_len(insn);
}
const char *
rb_insns_name(int i)
{
return insn_name(i);
}
VALUE
rb_insns_name_array(void)
{
VALUE ary = rb_ary_new_capa(VM_INSTRUCTION_SIZE);
int i;
for (i = 0; i < VM_INSTRUCTION_SIZE; i++) {
rb_ary_push(ary, rb_fstring_cstr(insn_name(i)));
}
return rb_obj_freeze(ary);
}
static LABEL *
register_label(rb_iseq_t *iseq, struct st_table *labels_table, VALUE obj)
{
LABEL *label = 0;
st_data_t tmp;
obj = rb_to_symbol_type(obj);
if (st_lookup(labels_table, obj, &tmp) == 0) {
label = NEW_LABEL(0);
st_insert(labels_table, obj, (st_data_t)label);
}
else {
label = (LABEL *)tmp;
}
LABEL_REF(label);
return label;
}
static VALUE
get_exception_sym2type(VALUE sym)
{
static VALUE symRescue, symEnsure, symRetry;
static VALUE symBreak, symRedo, symNext;
if (symRescue == 0) {
symRescue = ID2SYM(rb_intern_const("rescue"));
symEnsure = ID2SYM(rb_intern_const("ensure"));
symRetry = ID2SYM(rb_intern_const("retry"));
symBreak = ID2SYM(rb_intern_const("break"));
symRedo = ID2SYM(rb_intern_const("redo"));
symNext = ID2SYM(rb_intern_const("next"));
}
if (sym == symRescue) return CATCH_TYPE_RESCUE;
if (sym == symEnsure) return CATCH_TYPE_ENSURE;
if (sym == symRetry) return CATCH_TYPE_RETRY;
if (sym == symBreak) return CATCH_TYPE_BREAK;
if (sym == symRedo) return CATCH_TYPE_REDO;
if (sym == symNext) return CATCH_TYPE_NEXT;
rb_raise(rb_eSyntaxError, "invalid exception symbol: %+"PRIsVALUE, sym);
return 0;
}
static int
iseq_build_from_ary_exception(rb_iseq_t *iseq, struct st_table *labels_table,
VALUE exception)
{
int i;
for (i=0; i<RARRAY_LEN(exception); i++) {
const rb_iseq_t *eiseq;
VALUE v, type;
LABEL *lstart, *lend, *lcont;
unsigned int sp;
v = rb_to_array_type(RARRAY_AREF(exception, i));
if (RARRAY_LEN(v) != 6) {
rb_raise(rb_eSyntaxError, "wrong exception entry");
}
type = get_exception_sym2type(RARRAY_AREF(v, 0));
if (NIL_P(RARRAY_AREF(v, 1))) {
eiseq = NULL;
}
else {
eiseq = rb_iseqw_to_iseq(rb_iseq_load(RARRAY_AREF(v, 1), (VALUE)iseq, Qnil));
}
lstart = register_label(iseq, labels_table, RARRAY_AREF(v, 2));
lend = register_label(iseq, labels_table, RARRAY_AREF(v, 3));
lcont = register_label(iseq, labels_table, RARRAY_AREF(v, 4));
sp = NUM2UINT(RARRAY_AREF(v, 5));
/* TODO: Dirty Hack! Fix me */
if (type == CATCH_TYPE_RESCUE ||
type == CATCH_TYPE_BREAK ||
type == CATCH_TYPE_NEXT) {
++sp;
}
lcont->sp = sp;
ADD_CATCH_ENTRY(type, lstart, lend, eiseq, lcont);
RB_GC_GUARD(v);
}
return COMPILE_OK;
}
static struct st_table *
insn_make_insn_table(void)
{
struct st_table *table;
int i;
table = st_init_numtable_with_size(VM_INSTRUCTION_SIZE);
for (i=0; i<VM_INSTRUCTION_SIZE; i++) {
st_insert(table, ID2SYM(rb_intern_const(insn_name(i))), i);
}
return table;
}
static const rb_iseq_t *
iseq_build_load_iseq(const rb_iseq_t *iseq, VALUE op)
{
VALUE iseqw;
const rb_iseq_t *loaded_iseq;
if (RB_TYPE_P(op, T_ARRAY)) {
iseqw = rb_iseq_load(op, (VALUE)iseq, Qnil);
}
else if (CLASS_OF(op) == rb_cISeq) {
iseqw = op;
}
else {
rb_raise(rb_eSyntaxError, "ISEQ is required");
}
loaded_iseq = rb_iseqw_to_iseq(iseqw);
return loaded_iseq;
}
static VALUE
iseq_build_callinfo_from_hash(rb_iseq_t *iseq, VALUE op)
{
ID mid = 0;
int orig_argc = 0;
unsigned int flag = 0;
struct rb_callinfo_kwarg *kw_arg = 0;
if (!NIL_P(op)) {
VALUE vmid = rb_hash_aref(op, ID2SYM(rb_intern_const("mid")));
VALUE vflag = rb_hash_aref(op, ID2SYM(rb_intern_const("flag")));
VALUE vorig_argc = rb_hash_aref(op, ID2SYM(rb_intern_const("orig_argc")));
VALUE vkw_arg = rb_hash_aref(op, ID2SYM(rb_intern_const("kw_arg")));
if (!NIL_P(vmid)) mid = SYM2ID(vmid);
if (!NIL_P(vflag)) flag = NUM2UINT(vflag);
if (!NIL_P(vorig_argc)) orig_argc = FIX2INT(vorig_argc);
if (!NIL_P(vkw_arg)) {
int i;
int len = RARRAY_LENINT(vkw_arg);
size_t n = rb_callinfo_kwarg_bytes(len);
kw_arg = xmalloc(n);
kw_arg->references = 0;
kw_arg->keyword_len = len;
for (i = 0; i < len; i++) {
VALUE kw = RARRAY_AREF(vkw_arg, i);
SYM2ID(kw); /* make immortal */
kw_arg->keywords[i] = kw;
}
}
}
const struct rb_callinfo *ci = new_callinfo(iseq, mid, orig_argc, flag, kw_arg, (flag & VM_CALL_ARGS_SIMPLE) == 0);
RB_OBJ_WRITTEN(iseq, Qundef, ci);
return (VALUE)ci;
}
static rb_event_flag_t
event_name_to_flag(VALUE sym)
{
#define CHECK_EVENT(ev) if (sym == ID2SYM(rb_intern_const(#ev))) return ev;
CHECK_EVENT(RUBY_EVENT_LINE);
CHECK_EVENT(RUBY_EVENT_CLASS);
CHECK_EVENT(RUBY_EVENT_END);
CHECK_EVENT(RUBY_EVENT_CALL);
CHECK_EVENT(RUBY_EVENT_RETURN);
CHECK_EVENT(RUBY_EVENT_B_CALL);
CHECK_EVENT(RUBY_EVENT_B_RETURN);
CHECK_EVENT(RUBY_EVENT_RESCUE);
#undef CHECK_EVENT
return RUBY_EVENT_NONE;
}
static int
iseq_build_from_ary_body(rb_iseq_t *iseq, LINK_ANCHOR *const anchor,
VALUE body, VALUE node_ids, VALUE labels_wrapper)
{
/* TODO: body should be frozen */
long i, len = RARRAY_LEN(body);
struct st_table *labels_table = DATA_PTR(labels_wrapper);
int j;
int line_no = 0, node_id = -1, insn_idx = 0;
int ret = COMPILE_OK;
/*
* index -> LABEL *label
*/
static struct st_table *insn_table;
if (insn_table == 0) {
insn_table = insn_make_insn_table();
}
for (i=0; i<len; i++) {
VALUE obj = RARRAY_AREF(body, i);
if (SYMBOL_P(obj)) {
rb_event_flag_t event;
if ((event = event_name_to_flag(obj)) != RUBY_EVENT_NONE) {
ADD_TRACE(anchor, event);
}
else {
LABEL *label = register_label(iseq, labels_table, obj);
ADD_LABEL(anchor, label);
}
}
else if (FIXNUM_P(obj)) {
line_no = NUM2INT(obj);
}
else if (RB_TYPE_P(obj, T_ARRAY)) {
VALUE *argv = 0;
int argc = RARRAY_LENINT(obj) - 1;
st_data_t insn_id;
VALUE insn;
if (node_ids) {
node_id = NUM2INT(rb_ary_entry(node_ids, insn_idx++));
}
insn = (argc < 0) ? Qnil : RARRAY_AREF(obj, 0);
if (st_lookup(insn_table, (st_data_t)insn, &insn_id) == 0) {
/* TODO: exception */
COMPILE_ERROR(iseq, line_no,
"unknown instruction: %+"PRIsVALUE, insn);
ret = COMPILE_NG;
break;
}
if (argc != insn_len((VALUE)insn_id)-1) {
COMPILE_ERROR(iseq, line_no,
"operand size mismatch");
ret = COMPILE_NG;
break;
}
if (argc > 0) {
argv = compile_data_calloc2(iseq, sizeof(VALUE), argc);
// add element before operand setup to make GC root
NODE dummy_line_node = generate_dummy_line_node(line_no, node_id);
ADD_ELEM(anchor,
(LINK_ELEMENT*)new_insn_core(iseq, &dummy_line_node,
(enum ruby_vminsn_type)insn_id, argc, argv));
for (j=0; j<argc; j++) {
VALUE op = rb_ary_entry(obj, j+1);
switch (insn_op_type((VALUE)insn_id, j)) {
case TS_OFFSET: {
LABEL *label = register_label(iseq, labels_table, op);
argv[j] = (VALUE)label;
break;
}
case TS_LINDEX:
case TS_NUM:
(void)NUM2INT(op);
argv[j] = op;
break;
case TS_VALUE:
argv[j] = op;
RB_OBJ_WRITTEN(iseq, Qundef, op);
break;
case TS_ISEQ:
{
if (op != Qnil) {
VALUE v = (VALUE)iseq_build_load_iseq(iseq, op);
argv[j] = v;
RB_OBJ_WRITTEN(iseq, Qundef, v);
}
else {
argv[j] = 0;
}
}
break;
case TS_ISE:
argv[j] = op;
if (NUM2UINT(op) >= ISEQ_BODY(iseq)->ise_size) {
ISEQ_BODY(iseq)->ise_size = NUM2INT(op) + 1;
}
break;
case TS_IC:
{
VALUE segments = rb_ary_new();
op = rb_to_array_type(op);
for (int i = 0; i < RARRAY_LEN(op); i++) {
VALUE sym = RARRAY_AREF(op, i);
sym = rb_to_symbol_type(sym);
rb_ary_push(segments, sym);
}
RB_GC_GUARD(op);
argv[j] = segments;
RB_OBJ_WRITTEN(iseq, Qundef, segments);
ISEQ_BODY(iseq)->ic_size++;
}
break;
case TS_IVC: /* inline ivar cache */
argv[j] = op;
if (NUM2UINT(op) >= ISEQ_BODY(iseq)->ivc_size) {
ISEQ_BODY(iseq)->ivc_size = NUM2INT(op) + 1;
}
break;
case TS_ICVARC: /* inline cvar cache */
argv[j] = op;
if (NUM2UINT(op) >= ISEQ_BODY(iseq)->icvarc_size) {
ISEQ_BODY(iseq)->icvarc_size = NUM2INT(op) + 1;
}
break;
case TS_CALLDATA:
argv[j] = iseq_build_callinfo_from_hash(iseq, op);
break;
case TS_ID:
argv[j] = rb_to_symbol_type(op);
break;
case TS_CDHASH:
{
int i;
VALUE map = rb_hash_new_with_size(RARRAY_LEN(op)/2);
RHASH_TBL_RAW(map)->type = &cdhash_type;
op = rb_to_array_type(op);
for (i=0; i<RARRAY_LEN(op); i+=2) {
VALUE key = RARRAY_AREF(op, i);
VALUE sym = RARRAY_AREF(op, i+1);
LABEL *label =
register_label(iseq, labels_table, sym);
rb_hash_aset(map, key, (VALUE)label | 1);
}
RB_GC_GUARD(op);
argv[j] = map;
RB_OBJ_WRITTEN(iseq, Qundef, map);
}
break;
case TS_FUNCPTR:
{
#if SIZEOF_VALUE <= SIZEOF_LONG
long funcptr = NUM2LONG(op);
#else
LONG_LONG funcptr = NUM2LL(op);
#endif
argv[j] = (VALUE)funcptr;
}
break;
default:
rb_raise(rb_eSyntaxError, "unknown operand: %c", insn_op_type((VALUE)insn_id, j));
}
}
}
else {
NODE dummy_line_node = generate_dummy_line_node(line_no, node_id);
ADD_ELEM(anchor,
(LINK_ELEMENT*)new_insn_core(iseq, &dummy_line_node,
(enum ruby_vminsn_type)insn_id, argc, NULL));
}
}
else {
rb_raise(rb_eTypeError, "unexpected object for instruction");
}
}
DATA_PTR(labels_wrapper) = 0;
validate_labels(iseq, labels_table);
if (!ret) return ret;
return iseq_setup(iseq, anchor);
}
#define CHECK_ARRAY(v) rb_to_array_type(v)
#define CHECK_SYMBOL(v) rb_to_symbol_type(v)
static int
int_param(int *dst, VALUE param, VALUE sym)
{
VALUE val = rb_hash_aref(param, sym);
if (FIXNUM_P(val)) {
*dst = FIX2INT(val);
return TRUE;
}
else if (!NIL_P(val)) {
rb_raise(rb_eTypeError, "invalid %+"PRIsVALUE" Fixnum: %+"PRIsVALUE,
sym, val);
}
return FALSE;
}
static const struct rb_iseq_param_keyword *
iseq_build_kw(rb_iseq_t *iseq, VALUE params, VALUE keywords)
{
int i, j;
int len = RARRAY_LENINT(keywords);
int default_len;
VALUE key, sym, default_val;
VALUE *dvs;
ID *ids;
struct rb_iseq_param_keyword *keyword = ZALLOC(struct rb_iseq_param_keyword);
ISEQ_BODY(iseq)->param.flags.has_kw = TRUE;
keyword->num = len;
#define SYM(s) ID2SYM(rb_intern_const(#s))
(void)int_param(&keyword->bits_start, params, SYM(kwbits));
i = keyword->bits_start - keyword->num;
ids = (ID *)&ISEQ_BODY(iseq)->local_table[i];
#undef SYM
/* required args */
for (i = 0; i < len; i++) {
VALUE val = RARRAY_AREF(keywords, i);
if (!SYMBOL_P(val)) {
goto default_values;
}
ids[i] = SYM2ID(val);
keyword->required_num++;
}
default_values: /* note: we intentionally preserve `i' from previous loop */
default_len = len - i;
if (default_len == 0) {
keyword->table = ids;
return keyword;
}
else if (default_len < 0) {
UNREACHABLE;
}
dvs = ALLOC_N(VALUE, (unsigned int)default_len);
for (j = 0; i < len; i++, j++) {
key = RARRAY_AREF(keywords, i);
CHECK_ARRAY(key);
switch (RARRAY_LEN(key)) {
case 1:
sym = RARRAY_AREF(key, 0);
default_val = Qundef;
break;
case 2:
sym = RARRAY_AREF(key, 0);
default_val = RARRAY_AREF(key, 1);
break;
default:
rb_raise(rb_eTypeError, "keyword default has unsupported len %+"PRIsVALUE, key);
}
ids[i] = SYM2ID(sym);
dvs[j] = default_val;
}
keyword->table = ids;
keyword->default_values = dvs;
return keyword;
}
static void
iseq_insn_each_object_mark_and_pin(VALUE obj, VALUE _)
{
rb_gc_mark(obj);
}
void
rb_iseq_mark_and_pin_insn_storage(struct iseq_compile_data_storage *storage)
{
INSN *iobj = 0;
size_t size = sizeof(INSN);
unsigned int pos = 0;
while (storage) {
#ifdef STRICT_ALIGNMENT
size_t padding = calc_padding((void *)&storage->buff[pos], size);
#else
const size_t padding = 0; /* expected to be optimized by compiler */
#endif /* STRICT_ALIGNMENT */
size_t offset = pos + size + padding;
if (offset > storage->size || offset > storage->pos) {
pos = 0;
storage = storage->next;
}
else {
#ifdef STRICT_ALIGNMENT
pos += (int)padding;
#endif /* STRICT_ALIGNMENT */
iobj = (INSN *)&storage->buff[pos];
if (iobj->operands) {
iseq_insn_each_markable_object(iobj, iseq_insn_each_object_mark_and_pin, (VALUE)0);
}
pos += (int)size;
}
}
}
void
rb_iseq_build_from_ary(rb_iseq_t *iseq, VALUE misc, VALUE locals, VALUE params,
VALUE exception, VALUE body)
{
#define SYM(s) ID2SYM(rb_intern_const(#s))
int i, len;
unsigned int arg_size, local_size, stack_max;
ID *tbl;
struct st_table *labels_table = st_init_numtable();
VALUE labels_wrapper = Data_Wrap_Struct(0, rb_mark_set, st_free_table, labels_table);
VALUE arg_opt_labels = rb_hash_aref(params, SYM(opt));
VALUE keywords = rb_hash_aref(params, SYM(keyword));
VALUE sym_arg_rest = ID2SYM(rb_intern_const("#arg_rest"));
DECL_ANCHOR(anchor);
INIT_ANCHOR(anchor);
len = RARRAY_LENINT(locals);
ISEQ_BODY(iseq)->local_table_size = len;
ISEQ_BODY(iseq)->local_table = tbl = len > 0 ? (ID *)ALLOC_N(ID, ISEQ_BODY(iseq)->local_table_size) : NULL;
for (i = 0; i < len; i++) {
VALUE lv = RARRAY_AREF(locals, i);
if (sym_arg_rest == lv) {
tbl[i] = 0;
}
else {
tbl[i] = FIXNUM_P(lv) ? (ID)FIX2LONG(lv) : SYM2ID(CHECK_SYMBOL(lv));
}
}
#define INT_PARAM(F) int_param(&ISEQ_BODY(iseq)->param.F, params, SYM(F))
if (INT_PARAM(lead_num)) {
ISEQ_BODY(iseq)->param.flags.has_lead = TRUE;
}
if (INT_PARAM(post_num)) ISEQ_BODY(iseq)->param.flags.has_post = TRUE;
if (INT_PARAM(post_start)) ISEQ_BODY(iseq)->param.flags.has_post = TRUE;
if (INT_PARAM(rest_start)) ISEQ_BODY(iseq)->param.flags.has_rest = TRUE;
if (INT_PARAM(block_start)) ISEQ_BODY(iseq)->param.flags.has_block = TRUE;
#undef INT_PARAM
{
#define INT_PARAM(F) F = (int_param(&x, misc, SYM(F)) ? (unsigned int)x : 0)
int x;
INT_PARAM(arg_size);
INT_PARAM(local_size);
INT_PARAM(stack_max);
#undef INT_PARAM
}
VALUE node_ids = Qfalse;
#ifdef USE_ISEQ_NODE_ID
node_ids = rb_hash_aref(misc, ID2SYM(rb_intern("node_ids")));
if (!RB_TYPE_P(node_ids, T_ARRAY)) {
rb_raise(rb_eTypeError, "node_ids is not an array");
}
#endif
if (RB_TYPE_P(arg_opt_labels, T_ARRAY)) {
len = RARRAY_LENINT(arg_opt_labels);
ISEQ_BODY(iseq)->param.flags.has_opt = !!(len - 1 >= 0);
if (ISEQ_BODY(iseq)->param.flags.has_opt) {
VALUE *opt_table = ALLOC_N(VALUE, len);
for (i = 0; i < len; i++) {
VALUE ent = RARRAY_AREF(arg_opt_labels, i);
LABEL *label = register_label(iseq, labels_table, ent);
opt_table[i] = (VALUE)label;
}
ISEQ_BODY(iseq)->param.opt_num = len - 1;
ISEQ_BODY(iseq)->param.opt_table = opt_table;
}
}
else if (!NIL_P(arg_opt_labels)) {
rb_raise(rb_eTypeError, ":opt param is not an array: %+"PRIsVALUE,
arg_opt_labels);
}
if (RB_TYPE_P(keywords, T_ARRAY)) {
ISEQ_BODY(iseq)->param.keyword = iseq_build_kw(iseq, params, keywords);
}
else if (!NIL_P(keywords)) {
rb_raise(rb_eTypeError, ":keywords param is not an array: %+"PRIsVALUE,
keywords);
}
if (Qtrue == rb_hash_aref(params, SYM(ambiguous_param0))) {
ISEQ_BODY(iseq)->param.flags.ambiguous_param0 = TRUE;
}
if (int_param(&i, params, SYM(kwrest))) {
struct rb_iseq_param_keyword *keyword = (struct rb_iseq_param_keyword *)ISEQ_BODY(iseq)->param.keyword;
if (keyword == NULL) {
ISEQ_BODY(iseq)->param.keyword = keyword = ZALLOC(struct rb_iseq_param_keyword);
}
keyword->rest_start = i;
ISEQ_BODY(iseq)->param.flags.has_kwrest = TRUE;
}
#undef SYM
iseq_calc_param_size(iseq);
/* exception */
iseq_build_from_ary_exception(iseq, labels_table, exception);
/* body */
iseq_build_from_ary_body(iseq, anchor, body, node_ids, labels_wrapper);
ISEQ_BODY(iseq)->param.size = arg_size;
ISEQ_BODY(iseq)->local_table_size = local_size;
ISEQ_BODY(iseq)->stack_max = stack_max;
}
/* for parser */
int
rb_dvar_defined(ID id, const rb_iseq_t *iseq)
{
if (iseq) {
const struct rb_iseq_constant_body *body = ISEQ_BODY(iseq);
while (body->type == ISEQ_TYPE_BLOCK ||
body->type == ISEQ_TYPE_RESCUE ||
body->type == ISEQ_TYPE_ENSURE ||
body->type == ISEQ_TYPE_EVAL ||
body->type == ISEQ_TYPE_MAIN
) {
unsigned int i;
for (i = 0; i < body->local_table_size; i++) {
if (body->local_table[i] == id) {
return 1;
}
}
iseq = body->parent_iseq;
body = ISEQ_BODY(iseq);
}
}
return 0;
}
int
rb_local_defined(ID id, const rb_iseq_t *iseq)
{
if (iseq) {
unsigned int i;
const struct rb_iseq_constant_body *const body = ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq);
for (i=0; i<body->local_table_size; i++) {
if (body->local_table[i] == id) {
return 1;
}
}
}
return 0;
}
/* ISeq binary format */
#ifndef IBF_ISEQ_DEBUG
#define IBF_ISEQ_DEBUG 0
#endif
#ifndef IBF_ISEQ_ENABLE_LOCAL_BUFFER
#define IBF_ISEQ_ENABLE_LOCAL_BUFFER 0
#endif
typedef uint32_t ibf_offset_t;
#define IBF_OFFSET(ptr) ((ibf_offset_t)(VALUE)(ptr))
#define IBF_MAJOR_VERSION ISEQ_MAJOR_VERSION
#ifdef RUBY_DEVEL
#define IBF_DEVEL_VERSION 4
#define IBF_MINOR_VERSION (ISEQ_MINOR_VERSION * 10000 + IBF_DEVEL_VERSION)
#else
#define IBF_MINOR_VERSION ISEQ_MINOR_VERSION
#endif
static const char IBF_ENDIAN_MARK =
#ifdef WORDS_BIGENDIAN
'b'
#else
'l'
#endif
;
struct ibf_header {
char magic[4]; /* YARB */
uint32_t major_version;
uint32_t minor_version;
uint32_t size;
uint32_t extra_size;
uint32_t iseq_list_size;
uint32_t global_object_list_size;
ibf_offset_t iseq_list_offset;
ibf_offset_t global_object_list_offset;
uint8_t endian;
uint8_t wordsize; /* assume no 2048-bit CPU */
};
struct ibf_dump_buffer {
VALUE str;
st_table *obj_table; /* obj -> obj number */
};
struct ibf_dump {
st_table *iseq_table; /* iseq -> iseq number */
struct ibf_dump_buffer global_buffer;
struct ibf_dump_buffer *current_buffer;
};
struct ibf_load_buffer {
const char *buff;
ibf_offset_t size;
VALUE obj_list; /* [obj0, ...] */
unsigned int obj_list_size;
ibf_offset_t obj_list_offset;
};
struct ibf_load {
const struct ibf_header *header;
VALUE iseq_list; /* [iseq0, ...] */
struct ibf_load_buffer global_buffer;
VALUE loader_obj;
rb_iseq_t *iseq;
VALUE str;
struct ibf_load_buffer *current_buffer;
};
struct pinned_list {
long size;
VALUE buffer[1];
};
static void
pinned_list_mark(void *ptr)
{
long i;
struct pinned_list *list = (struct pinned_list *)ptr;
for (i = 0; i < list->size; i++) {
if (list->buffer[i]) {
rb_gc_mark(list->buffer[i]);
}
}
}
static const rb_data_type_t pinned_list_type = {
"pinned_list",
{
pinned_list_mark,
RUBY_DEFAULT_FREE,
NULL, // No external memory to report,
},
0, 0, RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_EMBEDDABLE
};
static VALUE
pinned_list_fetch(VALUE list, long offset)
{
struct pinned_list * ptr;
TypedData_Get_Struct(list, struct pinned_list, &pinned_list_type, ptr);
if (offset >= ptr->size) {
rb_raise(rb_eIndexError, "object index out of range: %ld", offset);
}
return ptr->buffer[offset];
}
static void
pinned_list_store(VALUE list, long offset, VALUE object)
{
struct pinned_list * ptr;
TypedData_Get_Struct(list, struct pinned_list, &pinned_list_type, ptr);
if (offset >= ptr->size) {
rb_raise(rb_eIndexError, "object index out of range: %ld", offset);
}
RB_OBJ_WRITE(list, &ptr->buffer[offset], object);
}
static VALUE
pinned_list_new(long size)
{
size_t memsize = offsetof(struct pinned_list, buffer) + size * sizeof(VALUE);
VALUE obj_list = rb_data_typed_object_zalloc(0, memsize, &pinned_list_type);
struct pinned_list * ptr = RTYPEDDATA_GET_DATA(obj_list);
ptr->size = size;
return obj_list;
}
static ibf_offset_t
ibf_dump_pos(struct ibf_dump *dump)
{
long pos = RSTRING_LEN(dump->current_buffer->str);
#if SIZEOF_LONG > SIZEOF_INT
if (pos >= UINT_MAX) {
rb_raise(rb_eRuntimeError, "dump size exceeds");
}
#endif
return (unsigned int)pos;
}
static void
ibf_dump_align(struct ibf_dump *dump, size_t align)
{
ibf_offset_t pos = ibf_dump_pos(dump);
if (pos % align) {
static const char padding[sizeof(VALUE)];
size_t size = align - ((size_t)pos % align);
#if SIZEOF_LONG > SIZEOF_INT
if (pos + size >= UINT_MAX) {
rb_raise(rb_eRuntimeError, "dump size exceeds");
}
#endif
for (; size > sizeof(padding); size -= sizeof(padding)) {
rb_str_cat(dump->current_buffer->str, padding, sizeof(padding));
}
rb_str_cat(dump->current_buffer->str, padding, size);
}
}
static ibf_offset_t
ibf_dump_write(struct ibf_dump *dump, const void *buff, unsigned long size)
{
ibf_offset_t pos = ibf_dump_pos(dump);
rb_str_cat(dump->current_buffer->str, (const char *)buff, size);
/* TODO: overflow check */
return pos;
}
static ibf_offset_t
ibf_dump_write_byte(struct ibf_dump *dump, unsigned char byte)
{
return ibf_dump_write(dump, &byte, sizeof(unsigned char));
}
static void
ibf_dump_overwrite(struct ibf_dump *dump, void *buff, unsigned int size, long offset)
{
VALUE str = dump->current_buffer->str;
char *ptr = RSTRING_PTR(str);
if ((unsigned long)(size + offset) > (unsigned long)RSTRING_LEN(str))
rb_bug("ibf_dump_overwrite: overflow");
memcpy(ptr + offset, buff, size);
}
static const void *
ibf_load_ptr(const struct ibf_load *load, ibf_offset_t *offset, int size)
{
ibf_offset_t beg = *offset;
*offset += size;
return load->current_buffer->buff + beg;
}
static void *
ibf_load_alloc(const struct ibf_load *load, ibf_offset_t offset, size_t x, size_t y)
{
void *buff = ruby_xmalloc2(x, y);
size_t size = x * y;
memcpy(buff, load->current_buffer->buff + offset, size);
return buff;
}
#define IBF_W_ALIGN(type) (RUBY_ALIGNOF(type) > 1 ? ibf_dump_align(dump, RUBY_ALIGNOF(type)) : (void)0)
#define IBF_W(b, type, n) (IBF_W_ALIGN(type), (type *)(VALUE)IBF_WP(b, type, n))
#define IBF_WV(variable) ibf_dump_write(dump, &(variable), sizeof(variable))
#define IBF_WP(b, type, n) ibf_dump_write(dump, (b), sizeof(type) * (n))
#define IBF_R(val, type, n) (type *)ibf_load_alloc(load, IBF_OFFSET(val), sizeof(type), (n))
#define IBF_ZERO(variable) memset(&(variable), 0, sizeof(variable))
static int
ibf_table_lookup(struct st_table *table, st_data_t key)
{
st_data_t val;
if (st_lookup(table, key, &val)) {
return (int)val;
}
else {
return -1;
}
}
static int
ibf_table_find_or_insert(struct st_table *table, st_data_t key)
{
int index = ibf_table_lookup(table, key);
if (index < 0) { /* not found */
index = (int)table->num_entries;
st_insert(table, key, (st_data_t)index);
}
return index;
}
/* dump/load generic */
static void ibf_dump_object_list(struct ibf_dump *dump, ibf_offset_t *obj_list_offset, unsigned int *obj_list_size);
static VALUE ibf_load_object(const struct ibf_load *load, VALUE object_index);
static rb_iseq_t *ibf_load_iseq(const struct ibf_load *load, const rb_iseq_t *index_iseq);
static st_table *
ibf_dump_object_table_new(void)
{
st_table *obj_table = st_init_numtable(); /* need free */
st_insert(obj_table, (st_data_t)Qnil, (st_data_t)0); /* 0th is nil */
return obj_table;
}
static VALUE
ibf_dump_object(struct ibf_dump *dump, VALUE obj)
{
return ibf_table_find_or_insert(dump->current_buffer->obj_table, (st_data_t)obj);
}
static VALUE
ibf_dump_id(struct ibf_dump *dump, ID id)
{
if (id == 0 || rb_id2name(id) == NULL) {
return 0;
}
return ibf_dump_object(dump, rb_id2sym(id));
}
static ID
ibf_load_id(const struct ibf_load *load, const ID id_index)
{
if (id_index == 0) {
return 0;
}
VALUE sym = ibf_load_object(load, id_index);
return rb_sym2id(sym);
}
/* dump/load: code */
static ibf_offset_t ibf_dump_iseq_each(struct ibf_dump *dump, const rb_iseq_t *iseq);
static int
ibf_dump_iseq(struct ibf_dump *dump, const rb_iseq_t *iseq)
{
if (iseq == NULL) {
return -1;
}
else {
return ibf_table_find_or_insert(dump->iseq_table, (st_data_t)iseq);
}
}
static unsigned char
ibf_load_byte(const struct ibf_load *load, ibf_offset_t *offset)
{
if (*offset >= load->current_buffer->size) { rb_raise(rb_eRuntimeError, "invalid bytecode"); }
return (unsigned char)load->current_buffer->buff[(*offset)++];
}
/*
* Small uint serialization
* 0x00000000_00000000 - 0x00000000_0000007f: 1byte | XXXX XXX1 |
* 0x00000000_00000080 - 0x00000000_00003fff: 2byte | XXXX XX10 | XXXX XXXX |
* 0x00000000_00004000 - 0x00000000_001fffff: 3byte | XXXX X100 | XXXX XXXX | XXXX XXXX |
* 0x00000000_00020000 - 0x00000000_0fffffff: 4byte | XXXX 1000 | XXXX XXXX | XXXX XXXX | XXXX XXXX |
* ...
* 0x00010000_00000000 - 0x00ffffff_ffffffff: 8byte | 1000 0000 | XXXX XXXX | XXXX XXXX | XXXX XXXX | XXXX XXXX | XXXX XXXX | XXXX XXXX | XXXX XXXX |
* 0x01000000_00000000 - 0xffffffff_ffffffff: 9byte | 0000 0000 | XXXX XXXX | XXXX XXXX | XXXX XXXX | XXXX XXXX | XXXX XXXX | XXXX XXXX | XXXX XXXX | XXXX XXXX |
*/
static void
ibf_dump_write_small_value(struct ibf_dump *dump, VALUE x)
{
if (sizeof(VALUE) > 8 || CHAR_BIT != 8) {
ibf_dump_write(dump, &x, sizeof(VALUE));
return;
}
enum { max_byte_length = sizeof(VALUE) + 1 };
unsigned char bytes[max_byte_length];
ibf_offset_t n;
for (n = 0; n < sizeof(VALUE) && (x >> (7 - n)); n++, x >>= 8) {
bytes[max_byte_length - 1 - n] = (unsigned char)x;
}
x <<= 1;
x |= 1;
x <<= n;
bytes[max_byte_length - 1 - n] = (unsigned char)x;
n++;
ibf_dump_write(dump, bytes + max_byte_length - n, n);
}
static VALUE
ibf_load_small_value(const struct ibf_load *load, ibf_offset_t *offset)
{
if (sizeof(VALUE) > 8 || CHAR_BIT != 8) {
union { char s[sizeof(VALUE)]; VALUE v; } x;
memcpy(x.s, load->current_buffer->buff + *offset, sizeof(VALUE));
*offset += sizeof(VALUE);
return x.v;
}
enum { max_byte_length = sizeof(VALUE) + 1 };
const unsigned char *buffer = (const unsigned char *)load->current_buffer->buff;
const unsigned char c = buffer[*offset];
ibf_offset_t n =
c & 1 ? 1 :
c == 0 ? 9 : ntz_int32(c) + 1;
VALUE x = (VALUE)c >> n;
if (*offset + n > load->current_buffer->size) {
rb_raise(rb_eRuntimeError, "invalid byte sequence");
}
ibf_offset_t i;
for (i = 1; i < n; i++) {
x <<= 8;
x |= (VALUE)buffer[*offset + i];
}
*offset += n;
return x;
}
static void
ibf_dump_builtin(struct ibf_dump *dump, const struct rb_builtin_function *bf)
{
// short: index
// short: name.length
// bytes: name
// // omit argc (only verify with name)
ibf_dump_write_small_value(dump, (VALUE)bf->index);
size_t len = strlen(bf->name);
ibf_dump_write_small_value(dump, (VALUE)len);
ibf_dump_write(dump, bf->name, len);
}
static const struct rb_builtin_function *
ibf_load_builtin(const struct ibf_load *load, ibf_offset_t *offset)
{
int i = (int)ibf_load_small_value(load, offset);
int len = (int)ibf_load_small_value(load, offset);
const char *name = (char *)ibf_load_ptr(load, offset, len);
if (0) {
fprintf(stderr, "%.*s!!\n", len, name);
}
const struct rb_builtin_function *table = GET_VM()->builtin_function_table;
if (table == NULL) rb_raise(rb_eArgError, "builtin function table is not provided");
if (strncmp(table[i].name, name, len) != 0) {
rb_raise(rb_eArgError, "builtin function index (%d) mismatch (expect %s but %s)", i, name, table[i].name);
}
// fprintf(stderr, "load-builtin: name:%s(%d)\n", table[i].name, table[i].argc);
return &table[i];
}
static ibf_offset_t
ibf_dump_code(struct ibf_dump *dump, const rb_iseq_t *iseq)
{
const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
const int iseq_size = body->iseq_size;
int code_index;
const VALUE *orig_code = rb_iseq_original_iseq(iseq);
ibf_offset_t offset = ibf_dump_pos(dump);
for (code_index=0; code_index<iseq_size;) {
const VALUE insn = orig_code[code_index++];
const char *types = insn_op_types(insn);
int op_index;
/* opcode */
if (insn >= 0x100) { rb_raise(rb_eRuntimeError, "invalid instruction"); }
ibf_dump_write_small_value(dump, insn);
/* operands */
for (op_index=0; types[op_index]; op_index++, code_index++) {
VALUE op = orig_code[code_index];
VALUE wv;
switch (types[op_index]) {
case TS_CDHASH:
case TS_VALUE:
wv = ibf_dump_object(dump, op);
break;
case TS_ISEQ:
wv = (VALUE)ibf_dump_iseq(dump, (const rb_iseq_t *)op);
break;
case TS_IC:
{
IC ic = (IC)op;
VALUE arr = idlist_to_array(ic->segments);
wv = ibf_dump_object(dump, arr);
}
break;
case TS_ISE:
case TS_IVC:
case TS_ICVARC:
{
union iseq_inline_storage_entry *is = (union iseq_inline_storage_entry *)op;
wv = is - ISEQ_IS_ENTRY_START(body, types[op_index]);
}
break;
case TS_CALLDATA:
{
goto skip_wv;
}
case TS_ID:
wv = ibf_dump_id(dump, (ID)op);
break;
case TS_FUNCPTR:
rb_raise(rb_eRuntimeError, "TS_FUNCPTR is not supported");
goto skip_wv;
case TS_BUILTIN:
ibf_dump_builtin(dump, (const struct rb_builtin_function *)op);
goto skip_wv;
default:
wv = op;
break;
}
ibf_dump_write_small_value(dump, wv);
skip_wv:;
}
assert(insn_len(insn) == op_index+1);
}
return offset;
}
static VALUE *
ibf_load_code(const struct ibf_load *load, rb_iseq_t *iseq, ibf_offset_t bytecode_offset, ibf_offset_t bytecode_size, unsigned int iseq_size)
{
VALUE iseqv = (VALUE)iseq;
unsigned int code_index;
ibf_offset_t reading_pos = bytecode_offset;
VALUE *code = ALLOC_N(VALUE, iseq_size);
struct rb_iseq_constant_body *load_body = ISEQ_BODY(iseq);
struct rb_call_data *cd_entries = load_body->call_data;
int ic_index = 0;
iseq_bits_t * mark_offset_bits;
iseq_bits_t tmp[1] = {0};
if (ISEQ_MBITS_BUFLEN(iseq_size) == 1) {
mark_offset_bits = tmp;
}
else {
mark_offset_bits = ZALLOC_N(iseq_bits_t, ISEQ_MBITS_BUFLEN(iseq_size));
}
bool needs_bitmap = false;
for (code_index=0; code_index<iseq_size;) {
/* opcode */
const VALUE insn = code[code_index] = ibf_load_small_value(load, &reading_pos);
const char *types = insn_op_types(insn);
int op_index;
code_index++;
/* operands */
for (op_index=0; types[op_index]; op_index++, code_index++) {
const char operand_type = types[op_index];
switch (operand_type) {
case TS_VALUE:
{
VALUE op = ibf_load_small_value(load, &reading_pos);
VALUE v = ibf_load_object(load, op);
code[code_index] = v;
if (!SPECIAL_CONST_P(v)) {
RB_OBJ_WRITTEN(iseqv, Qundef, v);
ISEQ_MBITS_SET(mark_offset_bits, code_index);
needs_bitmap = true;
}
break;
}
case TS_CDHASH:
{
VALUE op = ibf_load_small_value(load, &reading_pos);
VALUE v = ibf_load_object(load, op);
v = rb_hash_dup(v); // hash dumped as frozen
RHASH_TBL_RAW(v)->type = &cdhash_type;
rb_hash_rehash(v); // hash function changed
freeze_hide_obj(v);
// Overwrite the existing hash in the object list. This
// is to keep the object alive during load time.
// [Bug #17984] [ruby-core:104259]
pinned_list_store(load->current_buffer->obj_list, (long)op, v);
code[code_index] = v;
ISEQ_MBITS_SET(mark_offset_bits, code_index);
RB_OBJ_WRITTEN(iseqv, Qundef, v);
needs_bitmap = true;
break;
}
case TS_ISEQ:
{
VALUE op = (VALUE)ibf_load_small_value(load, &reading_pos);
VALUE v = (VALUE)ibf_load_iseq(load, (const rb_iseq_t *)op);
code[code_index] = v;
if (!SPECIAL_CONST_P(v)) {
RB_OBJ_WRITTEN(iseqv, Qundef, v);
ISEQ_MBITS_SET(mark_offset_bits, code_index);
needs_bitmap = true;
}
break;
}
case TS_IC:
{
VALUE op = ibf_load_small_value(load, &reading_pos);
VALUE arr = ibf_load_object(load, op);
IC ic = &ISEQ_IS_IC_ENTRY(load_body, ic_index++);
ic->segments = array_to_idlist(arr);
code[code_index] = (VALUE)ic;
}
break;
case TS_ISE:
case TS_ICVARC:
case TS_IVC:
{
unsigned int op = (unsigned int)ibf_load_small_value(load, &reading_pos);
ISE ic = ISEQ_IS_ENTRY_START(load_body, operand_type) + op;
code[code_index] = (VALUE)ic;
if (operand_type == TS_IVC) {
IVC cache = (IVC)ic;
if (insn == BIN(setinstancevariable)) {
ID iv_name = (ID)code[code_index - 1];
cache->iv_set_name = iv_name;
}
else {
cache->iv_set_name = 0;
}
vm_ic_attr_index_initialize(cache, INVALID_SHAPE_ID);
}
}
break;
case TS_CALLDATA:
{
code[code_index] = (VALUE)cd_entries++;
}
break;
case TS_ID:
{
VALUE op = ibf_load_small_value(load, &reading_pos);
code[code_index] = ibf_load_id(load, (ID)(VALUE)op);
}
break;
case TS_FUNCPTR:
rb_raise(rb_eRuntimeError, "TS_FUNCPTR is not supported");
break;
case TS_BUILTIN:
code[code_index] = (VALUE)ibf_load_builtin(load, &reading_pos);
break;
default:
code[code_index] = ibf_load_small_value(load, &reading_pos);
continue;
}
}
if (insn_len(insn) != op_index+1) {
rb_raise(rb_eRuntimeError, "operand size mismatch");
}
}
load_body->iseq_encoded = code;
load_body->iseq_size = code_index;
if (ISEQ_MBITS_BUFLEN(load_body->iseq_size) == 1) {
load_body->mark_bits.single = mark_offset_bits[0];
}
else {
if (needs_bitmap) {
load_body->mark_bits.list = mark_offset_bits;
}
else {
load_body->mark_bits.list = 0;
ruby_xfree(mark_offset_bits);
}
}
assert(code_index == iseq_size);
assert(reading_pos == bytecode_offset + bytecode_size);
return code;
}
static ibf_offset_t
ibf_dump_param_opt_table(struct ibf_dump *dump, const rb_iseq_t *iseq)
{
int opt_num = ISEQ_BODY(iseq)->param.opt_num;
if (opt_num > 0) {
IBF_W_ALIGN(VALUE);
return ibf_dump_write(dump, ISEQ_BODY(iseq)->param.opt_table, sizeof(VALUE) * (opt_num + 1));
}
else {
return ibf_dump_pos(dump);
}
}
static VALUE *
ibf_load_param_opt_table(const struct ibf_load *load, ibf_offset_t opt_table_offset, int opt_num)
{
if (opt_num > 0) {
VALUE *table = ALLOC_N(VALUE, opt_num+1);
MEMCPY(table, load->current_buffer->buff + opt_table_offset, VALUE, opt_num+1);
return table;
}
else {
return NULL;
}
}
static ibf_offset_t
ibf_dump_param_keyword(struct ibf_dump *dump, const rb_iseq_t *iseq)
{
const struct rb_iseq_param_keyword *kw = ISEQ_BODY(iseq)->param.keyword;
if (kw) {
struct rb_iseq_param_keyword dump_kw = *kw;
int dv_num = kw->num - kw->required_num;
ID *ids = kw->num > 0 ? ALLOCA_N(ID, kw->num) : NULL;
VALUE *dvs = dv_num > 0 ? ALLOCA_N(VALUE, dv_num) : NULL;
int i;
for (i=0; i<kw->num; i++) ids[i] = (ID)ibf_dump_id(dump, kw->table[i]);
for (i=0; i<dv_num; i++) dvs[i] = (VALUE)ibf_dump_object(dump, kw->default_values[i]);
dump_kw.table = IBF_W(ids, ID, kw->num);
dump_kw.default_values = IBF_W(dvs, VALUE, dv_num);
IBF_W_ALIGN(struct rb_iseq_param_keyword);
return ibf_dump_write(dump, &dump_kw, sizeof(struct rb_iseq_param_keyword) * 1);
}
else {
return 0;
}
}
static const struct rb_iseq_param_keyword *
ibf_load_param_keyword(const struct ibf_load *load, ibf_offset_t param_keyword_offset)
{
if (param_keyword_offset) {
struct rb_iseq_param_keyword *kw = IBF_R(param_keyword_offset, struct rb_iseq_param_keyword, 1);
ID *ids = IBF_R(kw->table, ID, kw->num);
int dv_num = kw->num - kw->required_num;
VALUE *dvs = IBF_R(kw->default_values, VALUE, dv_num);
int i;
for (i=0; i<kw->num; i++) {
ids[i] = ibf_load_id(load, ids[i]);
}
for (i=0; i<dv_num; i++) {
dvs[i] = ibf_load_object(load, dvs[i]);
}
kw->table = ids;
kw->default_values = dvs;
return kw;
}
else {
return NULL;
}
}
static ibf_offset_t
ibf_dump_insns_info_body(struct ibf_dump *dump, const rb_iseq_t *iseq)
{
ibf_offset_t offset = ibf_dump_pos(dump);
const struct iseq_insn_info_entry *entries = ISEQ_BODY(iseq)->insns_info.body;
unsigned int i;
for (i = 0; i < ISEQ_BODY(iseq)->insns_info.size; i++) {
ibf_dump_write_small_value(dump, entries[i].line_no);
#ifdef USE_ISEQ_NODE_ID
ibf_dump_write_small_value(dump, entries[i].node_id);
#endif
ibf_dump_write_small_value(dump, entries[i].events);
}
return offset;
}
static struct iseq_insn_info_entry *
ibf_load_insns_info_body(const struct ibf_load *load, ibf_offset_t body_offset, unsigned int size)
{
ibf_offset_t reading_pos = body_offset;
struct iseq_insn_info_entry *entries = ALLOC_N(struct iseq_insn_info_entry, size);
unsigned int i;
for (i = 0; i < size; i++) {
entries[i].line_no = (int)ibf_load_small_value(load, &reading_pos);
#ifdef USE_ISEQ_NODE_ID
entries[i].node_id = (int)ibf_load_small_value(load, &reading_pos);
#endif
entries[i].events = (rb_event_flag_t)ibf_load_small_value(load, &reading_pos);
}
return entries;
}
static ibf_offset_t
ibf_dump_insns_info_positions(struct ibf_dump *dump, const unsigned int *positions, unsigned int size)
{
ibf_offset_t offset = ibf_dump_pos(dump);
unsigned int last = 0;
unsigned int i;
for (i = 0; i < size; i++) {
ibf_dump_write_small_value(dump, positions[i] - last);
last = positions[i];
}
return offset;
}
static unsigned int *
ibf_load_insns_info_positions(const struct ibf_load *load, ibf_offset_t positions_offset, unsigned int size)
{
ibf_offset_t reading_pos = positions_offset;
unsigned int *positions = ALLOC_N(unsigned int, size);
unsigned int last = 0;
unsigned int i;
for (i = 0; i < size; i++) {
positions[i] = last + (unsigned int)ibf_load_small_value(load, &reading_pos);
last = positions[i];
}
return positions;
}
static ibf_offset_t
ibf_dump_local_table(struct ibf_dump *dump, const rb_iseq_t *iseq)
{
const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
const int size = body->local_table_size;
ID *table = ALLOCA_N(ID, size);
int i;
for (i=0; i<size; i++) {
table[i] = ibf_dump_id(dump, body->local_table[i]);
}
IBF_W_ALIGN(ID);
return ibf_dump_write(dump, table, sizeof(ID) * size);
}
static ID *
ibf_load_local_table(const struct ibf_load *load, ibf_offset_t local_table_offset, int size)
{
if (size > 0) {
ID *table = IBF_R(local_table_offset, ID, size);
int i;
for (i=0; i<size; i++) {
table[i] = ibf_load_id(load, table[i]);
}
return table;
}
else {
return NULL;
}
}
static ibf_offset_t
ibf_dump_catch_table(struct ibf_dump *dump, const rb_iseq_t *iseq)
{
const struct iseq_catch_table *table = ISEQ_BODY(iseq)->catch_table;
if (table) {
int *iseq_indices = ALLOCA_N(int, table->size);
unsigned int i;
for (i=0; i<table->size; i++) {
iseq_indices[i] = ibf_dump_iseq(dump, table->entries[i].iseq);
}
const ibf_offset_t offset = ibf_dump_pos(dump);
for (i=0; i<table->size; i++) {
ibf_dump_write_small_value(dump, iseq_indices[i]);
ibf_dump_write_small_value(dump, table->entries[i].type);
ibf_dump_write_small_value(dump, table->entries[i].start);
ibf_dump_write_small_value(dump, table->entries[i].end);
ibf_dump_write_small_value(dump, table->entries[i].cont);
ibf_dump_write_small_value(dump, table->entries[i].sp);
}
return offset;
}
else {
return ibf_dump_pos(dump);
}
}
static struct iseq_catch_table *
ibf_load_catch_table(const struct ibf_load *load, ibf_offset_t catch_table_offset, unsigned int size)
{
if (size) {
struct iseq_catch_table *table = ruby_xmalloc(iseq_catch_table_bytes(size));
table->size = size;
ibf_offset_t reading_pos = catch_table_offset;
unsigned int i;
for (i=0; i<table->size; i++) {
int iseq_index = (int)ibf_load_small_value(load, &reading_pos);
table->entries[i].type = (enum rb_catch_type)ibf_load_small_value(load, &reading_pos);
table->entries[i].start = (unsigned int)ibf_load_small_value(load, &reading_pos);
table->entries[i].end = (unsigned int)ibf_load_small_value(load, &reading_pos);
table->entries[i].cont = (unsigned int)ibf_load_small_value(load, &reading_pos);
table->entries[i].sp = (unsigned int)ibf_load_small_value(load, &reading_pos);
table->entries[i].iseq = ibf_load_iseq(load, (const rb_iseq_t *)(VALUE)iseq_index);
}
return table;
}
else {
return NULL;
}
}
static ibf_offset_t
ibf_dump_ci_entries(struct ibf_dump *dump, const rb_iseq_t *iseq)
{
const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
const unsigned int ci_size = body->ci_size;
const struct rb_call_data *cds = body->call_data;
ibf_offset_t offset = ibf_dump_pos(dump);
unsigned int i;
for (i = 0; i < ci_size; i++) {
const struct rb_callinfo *ci = cds[i].ci;
if (ci != NULL) {
ibf_dump_write_small_value(dump, ibf_dump_id(dump, vm_ci_mid(ci)));
ibf_dump_write_small_value(dump, vm_ci_flag(ci));
ibf_dump_write_small_value(dump, vm_ci_argc(ci));
const struct rb_callinfo_kwarg *kwarg = vm_ci_kwarg(ci);
if (kwarg) {
int len = kwarg->keyword_len;
ibf_dump_write_small_value(dump, len);
for (int j=0; j<len; j++) {
VALUE keyword = ibf_dump_object(dump, kwarg->keywords[j]);
ibf_dump_write_small_value(dump, keyword);
}
}
else {
ibf_dump_write_small_value(dump, 0);
}
}
else {
// TODO: truncate NULL ci from call_data.
ibf_dump_write_small_value(dump, (VALUE)-1);
}
}
return offset;
}
struct outer_variable_pair {
ID id;
VALUE name;
VALUE val;
};
struct outer_variable_list {
size_t num;
struct outer_variable_pair pairs[1];
};
static enum rb_id_table_iterator_result
store_outer_variable(ID id, VALUE val, void *dump)
{
struct outer_variable_list *ovlist = dump;
struct outer_variable_pair *pair = &ovlist->pairs[ovlist->num++];
pair->id = id;
pair->name = rb_id2str(id);
pair->val = val;
return ID_TABLE_CONTINUE;
}
static int
outer_variable_cmp(const void *a, const void *b, void *arg)
{
const struct outer_variable_pair *ap = (const struct outer_variable_pair *)a;
const struct outer_variable_pair *bp = (const struct outer_variable_pair *)b;
return rb_str_cmp(ap->name, bp->name);
}
static ibf_offset_t
ibf_dump_outer_variables(struct ibf_dump *dump, const rb_iseq_t *iseq)
{
struct rb_id_table * ovs = ISEQ_BODY(iseq)->outer_variables;
ibf_offset_t offset = ibf_dump_pos(dump);
size_t size = ovs ? rb_id_table_size(ovs) : 0;
ibf_dump_write_small_value(dump, (VALUE)size);
if (size > 0) {
VALUE buff;
size_t buffsize =
rb_size_mul_add_or_raise(sizeof(struct outer_variable_pair), size,
offsetof(struct outer_variable_list, pairs),
rb_eArgError);
struct outer_variable_list *ovlist = RB_ALLOCV(buff, buffsize);
ovlist->num = 0;
rb_id_table_foreach(ovs, store_outer_variable, ovlist);
ruby_qsort(ovlist->pairs, size, sizeof(struct outer_variable_pair), outer_variable_cmp, NULL);
for (size_t i = 0; i < size; ++i) {
ID id = ovlist->pairs[i].id;
ID val = ovlist->pairs[i].val;
ibf_dump_write_small_value(dump, ibf_dump_id(dump, id));
ibf_dump_write_small_value(dump, val);
}
}
return offset;
}
/* note that we dump out rb_call_info but load back rb_call_data */
static void
ibf_load_ci_entries(const struct ibf_load *load,
ibf_offset_t ci_entries_offset,
unsigned int ci_size,
struct rb_call_data **cd_ptr)
{
ibf_offset_t reading_pos = ci_entries_offset;
unsigned int i;
struct rb_call_data *cds = ZALLOC_N(struct rb_call_data, ci_size);
*cd_ptr = cds;
for (i = 0; i < ci_size; i++) {
VALUE mid_index = ibf_load_small_value(load, &reading_pos);
if (mid_index != (VALUE)-1) {
ID mid = ibf_load_id(load, mid_index);
unsigned int flag = (unsigned int)ibf_load_small_value(load, &reading_pos);
unsigned int argc = (unsigned int)ibf_load_small_value(load, &reading_pos);
struct rb_callinfo_kwarg *kwarg = NULL;
int kwlen = (int)ibf_load_small_value(load, &reading_pos);
if (kwlen > 0) {
kwarg = rb_xmalloc_mul_add(kwlen, sizeof(VALUE), sizeof(struct rb_callinfo_kwarg));
kwarg->references = 0;
kwarg->keyword_len = kwlen;
for (int j=0; j<kwlen; j++) {
VALUE keyword = ibf_load_small_value(load, &reading_pos);
kwarg->keywords[j] = ibf_load_object(load, keyword);
}
}
cds[i].ci = vm_ci_new(mid, flag, argc, kwarg);
RB_OBJ_WRITTEN(load->iseq, Qundef, cds[i].ci);
cds[i].cc = vm_cc_empty();
}
else {
// NULL ci
cds[i].ci = NULL;
cds[i].cc = NULL;
}
}
}
static struct rb_id_table *
ibf_load_outer_variables(const struct ibf_load * load, ibf_offset_t outer_variables_offset)
{
ibf_offset_t reading_pos = outer_variables_offset;
struct rb_id_table *tbl = NULL;
size_t table_size = (size_t)ibf_load_small_value(load, &reading_pos);
if (table_size > 0) {
tbl = rb_id_table_create(table_size);
}
for (size_t i = 0; i < table_size; i++) {
ID key = ibf_load_id(load, (ID)ibf_load_small_value(load, &reading_pos));
VALUE value = ibf_load_small_value(load, &reading_pos);
if (!key) key = rb_make_temporary_id(i);
rb_id_table_insert(tbl, key, value);
}
return tbl;
}
static ibf_offset_t
ibf_dump_iseq_each(struct ibf_dump *dump, const rb_iseq_t *iseq)
{
assert(dump->current_buffer == &dump->global_buffer);
unsigned int *positions;
const struct rb_iseq_constant_body *body = ISEQ_BODY(iseq);
const VALUE location_pathobj_index = ibf_dump_object(dump, body->location.pathobj); /* TODO: freeze */
const VALUE location_base_label_index = ibf_dump_object(dump, body->location.base_label);
const VALUE location_label_index = ibf_dump_object(dump, body->location.label);
#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
ibf_offset_t iseq_start = ibf_dump_pos(dump);
struct ibf_dump_buffer *saved_buffer = dump->current_buffer;
struct ibf_dump_buffer buffer;
buffer.str = rb_str_new(0, 0);
buffer.obj_table = ibf_dump_object_table_new();
dump->current_buffer = &buffer;
#endif
const ibf_offset_t bytecode_offset = ibf_dump_code(dump, iseq);
const ibf_offset_t bytecode_size = ibf_dump_pos(dump) - bytecode_offset;
const ibf_offset_t param_opt_table_offset = ibf_dump_param_opt_table(dump, iseq);
const ibf_offset_t param_keyword_offset = ibf_dump_param_keyword(dump, iseq);
const ibf_offset_t insns_info_body_offset = ibf_dump_insns_info_body(dump, iseq);
positions = rb_iseq_insns_info_decode_positions(ISEQ_BODY(iseq));
const ibf_offset_t insns_info_positions_offset = ibf_dump_insns_info_positions(dump, positions, body->insns_info.size);
ruby_xfree(positions);
const ibf_offset_t local_table_offset = ibf_dump_local_table(dump, iseq);
const unsigned int catch_table_size = body->catch_table ? body->catch_table->size : 0;
const ibf_offset_t catch_table_offset = ibf_dump_catch_table(dump, iseq);
const int parent_iseq_index = ibf_dump_iseq(dump, ISEQ_BODY(iseq)->parent_iseq);
const int local_iseq_index = ibf_dump_iseq(dump, ISEQ_BODY(iseq)->local_iseq);
const int mandatory_only_iseq_index = ibf_dump_iseq(dump, ISEQ_BODY(iseq)->mandatory_only_iseq);
const ibf_offset_t ci_entries_offset = ibf_dump_ci_entries(dump, iseq);
const ibf_offset_t outer_variables_offset = ibf_dump_outer_variables(dump, iseq);
#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
ibf_offset_t local_obj_list_offset;
unsigned int local_obj_list_size;
ibf_dump_object_list(dump, &local_obj_list_offset, &local_obj_list_size);
#endif
ibf_offset_t body_offset = ibf_dump_pos(dump);
/* dump the constant body */
unsigned int param_flags =
(body->param.flags.has_lead << 0) |
(body->param.flags.has_opt << 1) |
(body->param.flags.has_rest << 2) |
(body->param.flags.has_post << 3) |
(body->param.flags.has_kw << 4) |
(body->param.flags.has_kwrest << 5) |
(body->param.flags.has_block << 6) |
(body->param.flags.ambiguous_param0 << 7) |
(body->param.flags.accepts_no_kwarg << 8) |
(body->param.flags.ruby2_keywords << 9);
#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
# define IBF_BODY_OFFSET(x) (x)
#else
# define IBF_BODY_OFFSET(x) (body_offset - (x))
#endif
ibf_dump_write_small_value(dump, body->type);
ibf_dump_write_small_value(dump, body->iseq_size);
ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(bytecode_offset));
ibf_dump_write_small_value(dump, bytecode_size);
ibf_dump_write_small_value(dump, param_flags);
ibf_dump_write_small_value(dump, body->param.size);
ibf_dump_write_small_value(dump, body->param.lead_num);
ibf_dump_write_small_value(dump, body->param.opt_num);
ibf_dump_write_small_value(dump, body->param.rest_start);
ibf_dump_write_small_value(dump, body->param.post_start);
ibf_dump_write_small_value(dump, body->param.post_num);
ibf_dump_write_small_value(dump, body->param.block_start);
ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(param_opt_table_offset));
ibf_dump_write_small_value(dump, param_keyword_offset);
ibf_dump_write_small_value(dump, location_pathobj_index);
ibf_dump_write_small_value(dump, location_base_label_index);
ibf_dump_write_small_value(dump, location_label_index);
ibf_dump_write_small_value(dump, body->location.first_lineno);
ibf_dump_write_small_value(dump, body->location.node_id);
ibf_dump_write_small_value(dump, body->location.code_location.beg_pos.lineno);
ibf_dump_write_small_value(dump, body->location.code_location.beg_pos.column);
ibf_dump_write_small_value(dump, body->location.code_location.end_pos.lineno);
ibf_dump_write_small_value(dump, body->location.code_location.end_pos.column);
ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(insns_info_body_offset));
ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(insns_info_positions_offset));
ibf_dump_write_small_value(dump, body->insns_info.size);
ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(local_table_offset));
ibf_dump_write_small_value(dump, catch_table_size);
ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(catch_table_offset));
ibf_dump_write_small_value(dump, parent_iseq_index);
ibf_dump_write_small_value(dump, local_iseq_index);
ibf_dump_write_small_value(dump, mandatory_only_iseq_index);
ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(ci_entries_offset));
ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(outer_variables_offset));
ibf_dump_write_small_value(dump, body->variable.flip_count);
ibf_dump_write_small_value(dump, body->local_table_size);
ibf_dump_write_small_value(dump, body->ivc_size);
ibf_dump_write_small_value(dump, body->icvarc_size);
ibf_dump_write_small_value(dump, body->ise_size);
ibf_dump_write_small_value(dump, body->ic_size);
ibf_dump_write_small_value(dump, body->ci_size);
ibf_dump_write_small_value(dump, body->stack_max);
ibf_dump_write_small_value(dump, body->builtin_attrs);
#undef IBF_BODY_OFFSET
#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
ibf_offset_t iseq_length_bytes = ibf_dump_pos(dump);
dump->current_buffer = saved_buffer;
ibf_dump_write(dump, RSTRING_PTR(buffer.str), iseq_length_bytes);
ibf_offset_t offset = ibf_dump_pos(dump);
ibf_dump_write_small_value(dump, iseq_start);
ibf_dump_write_small_value(dump, iseq_length_bytes);
ibf_dump_write_small_value(dump, body_offset);
ibf_dump_write_small_value(dump, local_obj_list_offset);
ibf_dump_write_small_value(dump, local_obj_list_size);
st_free_table(buffer.obj_table); // TODO: this leaks in case of exception
return offset;
#else
return body_offset;
#endif
}
static VALUE
ibf_load_location_str(const struct ibf_load *load, VALUE str_index)
{
VALUE str = ibf_load_object(load, str_index);
if (str != Qnil) {
str = rb_fstring(str);
}
return str;
}
static void
ibf_load_iseq_each(struct ibf_load *load, rb_iseq_t *iseq, ibf_offset_t offset)
{
struct rb_iseq_constant_body *load_body = ISEQ_BODY(iseq) = rb_iseq_constant_body_alloc();
ibf_offset_t reading_pos = offset;
#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
struct ibf_load_buffer *saved_buffer = load->current_buffer;
load->current_buffer = &load->global_buffer;
const ibf_offset_t iseq_start = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
const ibf_offset_t iseq_length_bytes = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
const ibf_offset_t body_offset = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
struct ibf_load_buffer buffer;
buffer.buff = load->global_buffer.buff + iseq_start;
buffer.size = iseq_length_bytes;
buffer.obj_list_offset = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
buffer.obj_list_size = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
buffer.obj_list = pinned_list_new(buffer.obj_list_size);
load->current_buffer = &buffer;
reading_pos = body_offset;
#endif
#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
# define IBF_BODY_OFFSET(x) (x)
#else
# define IBF_BODY_OFFSET(x) (offset - (x))
#endif
const unsigned int type = (unsigned int)ibf_load_small_value(load, &reading_pos);
const unsigned int iseq_size = (unsigned int)ibf_load_small_value(load, &reading_pos);
const ibf_offset_t bytecode_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
const ibf_offset_t bytecode_size = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
const unsigned int param_flags = (unsigned int)ibf_load_small_value(load, &reading_pos);
const unsigned int param_size = (unsigned int)ibf_load_small_value(load, &reading_pos);
const int param_lead_num = (int)ibf_load_small_value(load, &reading_pos);
const int param_opt_num = (int)ibf_load_small_value(load, &reading_pos);
const int param_rest_start = (int)ibf_load_small_value(load, &reading_pos);
const int param_post_start = (int)ibf_load_small_value(load, &reading_pos);
const int param_post_num = (int)ibf_load_small_value(load, &reading_pos);
const int param_block_start = (int)ibf_load_small_value(load, &reading_pos);
const ibf_offset_t param_opt_table_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
const ibf_offset_t param_keyword_offset = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
const VALUE location_pathobj_index = ibf_load_small_value(load, &reading_pos);
const VALUE location_base_label_index = ibf_load_small_value(load, &reading_pos);
const VALUE location_label_index = ibf_load_small_value(load, &reading_pos);
const int location_first_lineno = (int)ibf_load_small_value(load, &reading_pos);
const int location_node_id = (int)ibf_load_small_value(load, &reading_pos);
const int location_code_location_beg_pos_lineno = (int)ibf_load_small_value(load, &reading_pos);
const int location_code_location_beg_pos_column = (int)ibf_load_small_value(load, &reading_pos);
const int location_code_location_end_pos_lineno = (int)ibf_load_small_value(load, &reading_pos);
const int location_code_location_end_pos_column = (int)ibf_load_small_value(load, &reading_pos);
const ibf_offset_t insns_info_body_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
const ibf_offset_t insns_info_positions_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
const unsigned int insns_info_size = (unsigned int)ibf_load_small_value(load, &reading_pos);
const ibf_offset_t local_table_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
const unsigned int catch_table_size = (unsigned int)ibf_load_small_value(load, &reading_pos);
const ibf_offset_t catch_table_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
const int parent_iseq_index = (int)ibf_load_small_value(load, &reading_pos);
const int local_iseq_index = (int)ibf_load_small_value(load, &reading_pos);
const int mandatory_only_iseq_index = (int)ibf_load_small_value(load, &reading_pos);
const ibf_offset_t ci_entries_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
const ibf_offset_t outer_variables_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
const rb_snum_t variable_flip_count = (rb_snum_t)ibf_load_small_value(load, &reading_pos);
const unsigned int local_table_size = (unsigned int)ibf_load_small_value(load, &reading_pos);
const unsigned int ivc_size = (unsigned int)ibf_load_small_value(load, &reading_pos);
const unsigned int icvarc_size = (unsigned int)ibf_load_small_value(load, &reading_pos);
const unsigned int ise_size = (unsigned int)ibf_load_small_value(load, &reading_pos);
const unsigned int ic_size = (unsigned int)ibf_load_small_value(load, &reading_pos);
const unsigned int ci_size = (unsigned int)ibf_load_small_value(load, &reading_pos);
const unsigned int stack_max = (unsigned int)ibf_load_small_value(load, &reading_pos);
const unsigned int builtin_attrs = (unsigned int)ibf_load_small_value(load, &reading_pos);
// setup fname and dummy frame
VALUE path = ibf_load_object(load, location_pathobj_index);
{
VALUE realpath = Qnil;
if (RB_TYPE_P(path, T_STRING)) {
realpath = path = rb_fstring(path);
}
else if (RB_TYPE_P(path, T_ARRAY)) {
VALUE pathobj = path;
if (RARRAY_LEN(pathobj) != 2) {
rb_raise(rb_eRuntimeError, "path object size mismatch");
}
path = rb_fstring(RARRAY_AREF(pathobj, 0));
realpath = RARRAY_AREF(pathobj, 1);
if (!NIL_P(realpath)) {
if (!RB_TYPE_P(realpath, T_STRING)) {
rb_raise(rb_eArgError, "unexpected realpath %"PRIxVALUE
"(%x), path=%+"PRIsVALUE,
realpath, TYPE(realpath), path);
}
realpath = rb_fstring(realpath);
}
}
else {
rb_raise(rb_eRuntimeError, "unexpected path object");
}
rb_iseq_pathobj_set(iseq, path, realpath);
}
// push dummy frame
rb_execution_context_t *ec = GET_EC();
VALUE dummy_frame = rb_vm_push_frame_fname(ec, path);
#undef IBF_BODY_OFFSET
load_body->type = type;
load_body->stack_max = stack_max;
load_body->param.flags.has_lead = (param_flags >> 0) & 1;
load_body->param.flags.has_opt = (param_flags >> 1) & 1;
load_body->param.flags.has_rest = (param_flags >> 2) & 1;
load_body->param.flags.has_post = (param_flags >> 3) & 1;
load_body->param.flags.has_kw = FALSE;
load_body->param.flags.has_kwrest = (param_flags >> 5) & 1;
load_body->param.flags.has_block = (param_flags >> 6) & 1;
load_body->param.flags.ambiguous_param0 = (param_flags >> 7) & 1;
load_body->param.flags.accepts_no_kwarg = (param_flags >> 8) & 1;
load_body->param.flags.ruby2_keywords = (param_flags >> 9) & 1;
load_body->param.size = param_size;
load_body->param.lead_num = param_lead_num;
load_body->param.opt_num = param_opt_num;
load_body->param.rest_start = param_rest_start;
load_body->param.post_start = param_post_start;
load_body->param.post_num = param_post_num;
load_body->param.block_start = param_block_start;
load_body->local_table_size = local_table_size;
load_body->ci_size = ci_size;
load_body->insns_info.size = insns_info_size;
ISEQ_COVERAGE_SET(iseq, Qnil);
ISEQ_ORIGINAL_ISEQ_CLEAR(iseq);
load_body->variable.flip_count = variable_flip_count;
load_body->variable.script_lines = Qnil;
load_body->location.first_lineno = location_first_lineno;
load_body->location.node_id = location_node_id;
load_body->location.code_location.beg_pos.lineno = location_code_location_beg_pos_lineno;
load_body->location.code_location.beg_pos.column = location_code_location_beg_pos_column;
load_body->location.code_location.end_pos.lineno = location_code_location_end_pos_lineno;
load_body->location.code_location.end_pos.column = location_code_location_end_pos_column;
load_body->builtin_attrs = builtin_attrs;
load_body->ivc_size = ivc_size;
load_body->icvarc_size = icvarc_size;
load_body->ise_size = ise_size;
load_body->ic_size = ic_size;
if (ISEQ_IS_SIZE(load_body)) {
load_body->is_entries = ZALLOC_N(union iseq_inline_storage_entry, ISEQ_IS_SIZE(load_body));
}
else {
load_body->is_entries = NULL;
}
ibf_load_ci_entries(load, ci_entries_offset, ci_size, &load_body->call_data);
load_body->outer_variables = ibf_load_outer_variables(load, outer_variables_offset);
load_body->param.opt_table = ibf_load_param_opt_table(load, param_opt_table_offset, param_opt_num);
load_body->param.keyword = ibf_load_param_keyword(load, param_keyword_offset);
load_body->param.flags.has_kw = (param_flags >> 4) & 1;
load_body->insns_info.body = ibf_load_insns_info_body(load, insns_info_body_offset, insns_info_size);
load_body->insns_info.positions = ibf_load_insns_info_positions(load, insns_info_positions_offset, insns_info_size);
load_body->local_table = ibf_load_local_table(load, local_table_offset, local_table_size);
load_body->catch_table = ibf_load_catch_table(load, catch_table_offset, catch_table_size);
load_body->parent_iseq = ibf_load_iseq(load, (const rb_iseq_t *)(VALUE)parent_iseq_index);
load_body->local_iseq = ibf_load_iseq(load, (const rb_iseq_t *)(VALUE)local_iseq_index);
load_body->mandatory_only_iseq = ibf_load_iseq(load, (const rb_iseq_t *)(VALUE)mandatory_only_iseq_index);
ibf_load_code(load, iseq, bytecode_offset, bytecode_size, iseq_size);
#if VM_INSN_INFO_TABLE_IMPL == 2
rb_iseq_insns_info_encode_positions(iseq);
#endif
rb_iseq_translate_threaded_code(iseq);
#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
load->current_buffer = &load->global_buffer;
#endif
RB_OBJ_WRITE(iseq, &load_body->location.base_label, ibf_load_location_str(load, location_base_label_index));
RB_OBJ_WRITE(iseq, &load_body->location.label, ibf_load_location_str(load, location_label_index));
#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
load->current_buffer = saved_buffer;
#endif
verify_call_cache(iseq);
RB_GC_GUARD(dummy_frame);
rb_vm_pop_frame_no_int(ec);
}
struct ibf_dump_iseq_list_arg
{
struct ibf_dump *dump;
VALUE offset_list;
};
static int
ibf_dump_iseq_list_i(st_data_t key, st_data_t val, st_data_t ptr)
{
const rb_iseq_t *iseq = (const rb_iseq_t *)key;
struct ibf_dump_iseq_list_arg *args = (struct ibf_dump_iseq_list_arg *)ptr;
ibf_offset_t offset = ibf_dump_iseq_each(args->dump, iseq);
rb_ary_push(args->offset_list, UINT2NUM(offset));
return ST_CONTINUE;
}
static void
ibf_dump_iseq_list(struct ibf_dump *dump, struct ibf_header *header)
{
VALUE offset_list = rb_ary_hidden_new(dump->iseq_table->num_entries);
struct ibf_dump_iseq_list_arg args;
args.dump = dump;
args.offset_list = offset_list;
st_foreach(dump->iseq_table, ibf_dump_iseq_list_i, (st_data_t)&args);
st_index_t i;
st_index_t size = dump->iseq_table->num_entries;
ibf_offset_t *offsets = ALLOCA_N(ibf_offset_t, size);
for (i = 0; i < size; i++) {
offsets[i] = NUM2UINT(RARRAY_AREF(offset_list, i));
}
ibf_dump_align(dump, sizeof(ibf_offset_t));
header->iseq_list_offset = ibf_dump_write(dump, offsets, sizeof(ibf_offset_t) * size);
header->iseq_list_size = (unsigned int)size;
}
#define IBF_OBJECT_INTERNAL FL_PROMOTED0
/*
* Binary format
* - ibf_object_header
* - ibf_object_xxx (xxx is type)
*/
struct ibf_object_header {
unsigned int type: 5;
unsigned int special_const: 1;
unsigned int frozen: 1;
unsigned int internal: 1;
};
enum ibf_object_class_index {
IBF_OBJECT_CLASS_OBJECT,
IBF_OBJECT_CLASS_ARRAY,
IBF_OBJECT_CLASS_STANDARD_ERROR,
IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_ERROR,
IBF_OBJECT_CLASS_TYPE_ERROR,
IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_KEY_ERROR,
};
struct ibf_object_regexp {
long srcstr;
char option;
};
struct ibf_object_hash {
long len;
long keyval[FLEX_ARY_LEN];
};
struct ibf_object_struct_range {
long class_index;
long len;
long beg;
long end;
int excl;
};
struct ibf_object_bignum {
ssize_t slen;
BDIGIT digits[FLEX_ARY_LEN];
};
enum ibf_object_data_type {
IBF_OBJECT_DATA_ENCODING,
};
struct ibf_object_complex_rational {
long a, b;
};
struct ibf_object_symbol {
long str;
};
#define IBF_ALIGNED_OFFSET(align, offset) /* offset > 0 */ \
((((offset) - 1) / (align) + 1) * (align))
#define IBF_OBJBODY(type, offset) (const type *)\
ibf_load_check_offset(load, IBF_ALIGNED_OFFSET(RUBY_ALIGNOF(type), offset))
static const void *
ibf_load_check_offset(const struct ibf_load *load, size_t offset)
{
if (offset >= load->current_buffer->size) {
rb_raise(rb_eIndexError, "object offset out of range: %"PRIdSIZE, offset);
}
return load->current_buffer->buff + offset;
}
NORETURN(static void ibf_dump_object_unsupported(struct ibf_dump *dump, VALUE obj));
static void
ibf_dump_object_unsupported(struct ibf_dump *dump, VALUE obj)
{
char buff[0x100];
rb_raw_obj_info(buff, sizeof(buff), obj);
rb_raise(rb_eNotImpError, "ibf_dump_object_unsupported: %s", buff);
}
NORETURN(static VALUE ibf_load_object_unsupported(const struct ibf_load *load, const struct ibf_object_header *header, ibf_offset_t offset));
static VALUE
ibf_load_object_unsupported(const struct ibf_load *load, const struct ibf_object_header *header, ibf_offset_t offset)
{
rb_raise(rb_eArgError, "unsupported");
UNREACHABLE_RETURN(Qnil);
}
static void
ibf_dump_object_class(struct ibf_dump *dump, VALUE obj)
{
enum ibf_object_class_index cindex;
if (obj == rb_cObject) {
cindex = IBF_OBJECT_CLASS_OBJECT;
}
else if (obj == rb_cArray) {
cindex = IBF_OBJECT_CLASS_ARRAY;
}
else if (obj == rb_eStandardError) {
cindex = IBF_OBJECT_CLASS_STANDARD_ERROR;
}
else if (obj == rb_eNoMatchingPatternError) {
cindex = IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_ERROR;
}
else if (obj == rb_eTypeError) {
cindex = IBF_OBJECT_CLASS_TYPE_ERROR;
}
else if (obj == rb_eNoMatchingPatternKeyError) {
cindex = IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_KEY_ERROR;
}
else {
rb_obj_info_dump(obj);
rb_p(obj);
rb_bug("unsupported class");
}
ibf_dump_write_small_value(dump, (VALUE)cindex);
}
static VALUE
ibf_load_object_class(const struct ibf_load *load, const struct ibf_object_header *header, ibf_offset_t offset)
{
enum ibf_object_class_index cindex = (enum ibf_object_class_index)ibf_load_small_value(load, &offset);
switch (cindex) {
case IBF_OBJECT_CLASS_OBJECT:
return rb_cObject;
case IBF_OBJECT_CLASS_ARRAY:
return rb_cArray;
case IBF_OBJECT_CLASS_STANDARD_ERROR:
return rb_eStandardError;
case IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_ERROR:
return rb_eNoMatchingPatternError;
case IBF_OBJECT_CLASS_TYPE_ERROR:
return rb_eTypeError;
case IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_KEY_ERROR:
return rb_eNoMatchingPatternKeyError;
}
rb_raise(rb_eArgError, "ibf_load_object_class: unknown class (%d)", (int)cindex);
}
static void
ibf_dump_object_float(struct ibf_dump *dump, VALUE obj)
{
double dbl = RFLOAT_VALUE(obj);
(void)IBF_W(&dbl, double, 1);
}
static VALUE
ibf_load_object_float(const struct ibf_load *load, const struct ibf_object_header *header, ibf_offset_t offset)
{
const double *dblp = IBF_OBJBODY(double, offset);
return DBL2NUM(*dblp);
}
static void
ibf_dump_object_string(struct ibf_dump *dump, VALUE obj)
{
long encindex = (long)rb_enc_get_index(obj);
long len = RSTRING_LEN(obj);
const char *ptr = RSTRING_PTR(obj);
if (encindex > RUBY_ENCINDEX_BUILTIN_MAX) {
rb_encoding *enc = rb_enc_from_index((int)encindex);
const char *enc_name = rb_enc_name(enc);
encindex = RUBY_ENCINDEX_BUILTIN_MAX + ibf_dump_object(dump, rb_str_new2(enc_name));
}
ibf_dump_write_small_value(dump, encindex);
ibf_dump_write_small_value(dump, len);
IBF_WP(ptr, char, len);
}
static VALUE
ibf_load_object_string(const struct ibf_load *load, const struct ibf_object_header *header, ibf_offset_t offset)
{
ibf_offset_t reading_pos = offset;
int encindex = (int)ibf_load_small_value(load, &reading_pos);
const long len = (long)ibf_load_small_value(load, &reading_pos);
const char *ptr = load->current_buffer->buff + reading_pos;
if (encindex > RUBY_ENCINDEX_BUILTIN_MAX) {
VALUE enc_name_str = ibf_load_object(load, encindex - RUBY_ENCINDEX_BUILTIN_MAX);
encindex = rb_enc_find_index(RSTRING_PTR(enc_name_str));
}
VALUE str;
if (header->frozen && !header->internal) {
str = rb_enc_interned_str(ptr, len, rb_enc_from_index(encindex));
}
else {
str = rb_enc_str_new(ptr, len, rb_enc_from_index(encindex));
if (header->internal) rb_obj_hide(str);
if (header->frozen) str = rb_fstring(str);
}
return str;
}
static void
ibf_dump_object_regexp(struct ibf_dump *dump, VALUE obj)
{
VALUE srcstr = RREGEXP_SRC(obj);
struct ibf_object_regexp regexp;
regexp.option = (char)rb_reg_options(obj);
regexp.srcstr = (long)ibf_dump_object(dump, srcstr);
ibf_dump_write_byte(dump, (unsigned char)regexp.option);
ibf_dump_write_small_value(dump, regexp.srcstr);
}
static VALUE
ibf_load_object_regexp(const struct ibf_load *load, const struct ibf_object_header *header, ibf_offset_t offset)
{
struct ibf_object_regexp regexp;
regexp.option = ibf_load_byte(load, &offset);
regexp.srcstr = ibf_load_small_value(load, &offset);
VALUE srcstr = ibf_load_object(load, regexp.srcstr);
VALUE reg = rb_reg_compile(srcstr, (int)regexp.option, NULL, 0);
if (header->internal) rb_obj_hide(reg);
if (header->frozen) rb_obj_freeze(reg);
return reg;
}
static void
ibf_dump_object_array(struct ibf_dump *dump, VALUE obj)
{
long i, len = RARRAY_LEN(obj);
ibf_dump_write_small_value(dump, len);
for (i=0; i<len; i++) {
long index = (long)ibf_dump_object(dump, RARRAY_AREF(obj, i));
ibf_dump_write_small_value(dump, index);
}
}
static VALUE
ibf_load_object_array(const struct ibf_load *load, const struct ibf_object_header *header, ibf_offset_t offset)
{
ibf_offset_t reading_pos = offset;
const long len = (long)ibf_load_small_value(load, &reading_pos);
VALUE ary = header->internal ? rb_ary_hidden_new(len) : rb_ary_new_capa(len);
int i;
for (i=0; i<len; i++) {
const VALUE index = ibf_load_small_value(load, &reading_pos);
rb_ary_push(ary, ibf_load_object(load, index));
}
if (header->frozen) rb_obj_freeze(ary);
return ary;
}
static int
ibf_dump_object_hash_i(st_data_t key, st_data_t val, st_data_t ptr)
{
struct ibf_dump *dump = (struct ibf_dump *)ptr;
VALUE key_index = ibf_dump_object(dump, (VALUE)key);
VALUE val_index = ibf_dump_object(dump, (VALUE)val);
ibf_dump_write_small_value(dump, key_index);
ibf_dump_write_small_value(dump, val_index);
return ST_CONTINUE;
}
static void
ibf_dump_object_hash(struct ibf_dump *dump, VALUE obj)
{
long len = RHASH_SIZE(obj);
ibf_dump_write_small_value(dump, (VALUE)len);
if (len > 0) rb_hash_foreach(obj, ibf_dump_object_hash_i, (VALUE)dump);
}
static VALUE
ibf_load_object_hash(const struct ibf_load *load, const struct ibf_object_header *header, ibf_offset_t offset)
{
long len = (long)ibf_load_small_value(load, &offset);
VALUE obj = rb_hash_new_with_size(len);
int i;
for (i = 0; i < len; i++) {
VALUE key_index = ibf_load_small_value(load, &offset);
VALUE val_index = ibf_load_small_value(load, &offset);
VALUE key = ibf_load_object(load, key_index);
VALUE val = ibf_load_object(load, val_index);
rb_hash_aset(obj, key, val);
}
rb_hash_rehash(obj);
if (header->internal) rb_obj_hide(obj);
if (header->frozen) rb_obj_freeze(obj);
return obj;
}
static void
ibf_dump_object_struct(struct ibf_dump *dump, VALUE obj)
{
if (rb_obj_is_kind_of(obj, rb_cRange)) {
struct ibf_object_struct_range range;
VALUE beg, end;
IBF_ZERO(range);
range.len = 3;
range.class_index = 0;
rb_range_values(obj, &beg, &end, &range.excl);
range.beg = (long)ibf_dump_object(dump, beg);
range.end = (long)ibf_dump_object(dump, end);
IBF_W_ALIGN(struct ibf_object_struct_range);
IBF_WV(range);
}
else {
rb_raise(rb_eNotImpError, "ibf_dump_object_struct: unsupported class %"PRIsVALUE,
rb_class_name(CLASS_OF(obj)));
}
}
static VALUE
ibf_load_object_struct(const struct ibf_load *load, const struct ibf_object_header *header, ibf_offset_t offset)
{
const struct ibf_object_struct_range *range = IBF_OBJBODY(struct ibf_object_struct_range, offset);
VALUE beg = ibf_load_object(load, range->beg);
VALUE end = ibf_load_object(load, range->end);
VALUE obj = rb_range_new(beg, end, range->excl);
if (header->internal) rb_obj_hide(obj);
if (header->frozen) rb_obj_freeze(obj);
return obj;
}
static void
ibf_dump_object_bignum(struct ibf_dump *dump, VALUE obj)
{
ssize_t len = BIGNUM_LEN(obj);
ssize_t slen = BIGNUM_SIGN(obj) > 0 ? len : len * -1;
BDIGIT *d = BIGNUM_DIGITS(obj);
(void)IBF_W(&slen, ssize_t, 1);
IBF_WP(d, BDIGIT, len);
}
static VALUE
ibf_load_object_bignum(const struct ibf_load *load, const struct ibf_object_header *header, ibf_offset_t offset)
{
const struct ibf_object_bignum *bignum = IBF_OBJBODY(struct ibf_object_bignum, offset);
int sign = bignum->slen > 0;
ssize_t len = sign > 0 ? bignum->slen : -1 * bignum->slen;
const int big_unpack_flags = /* c.f. rb_big_unpack() */
INTEGER_PACK_LSWORD_FIRST |
INTEGER_PACK_NATIVE_BYTE_ORDER;
VALUE obj = rb_integer_unpack(bignum->digits, len, sizeof(BDIGIT), 0,
big_unpack_flags |
(sign == 0 ? INTEGER_PACK_NEGATIVE : 0));
if (header->internal) rb_obj_hide(obj);
if (header->frozen) rb_obj_freeze(obj);
return obj;
}
static void
ibf_dump_object_data(struct ibf_dump *dump, VALUE obj)
{
if (rb_data_is_encoding(obj)) {
rb_encoding *enc = rb_to_encoding(obj);
const char *name = rb_enc_name(enc);
long len = strlen(name) + 1;
long data[2];
data[0] = IBF_OBJECT_DATA_ENCODING;
data[1] = len;
(void)IBF_W(data, long, 2);
IBF_WP(name, char, len);
}
else {
ibf_dump_object_unsupported(dump, obj);
}
}
static VALUE
ibf_load_object_data(const struct ibf_load *load, const struct ibf_object_header *header, ibf_offset_t offset)
{
const long *body = IBF_OBJBODY(long, offset);
const enum ibf_object_data_type type = (enum ibf_object_data_type)body[0];
/* const long len = body[1]; */
const char *data = (const char *)&body[2];
switch (type) {
case IBF_OBJECT_DATA_ENCODING:
{
VALUE encobj = rb_enc_from_encoding(rb_enc_find(data));
return encobj;
}
}
return ibf_load_object_unsupported(load, header, offset);
}
static void
ibf_dump_object_complex_rational(struct ibf_dump *dump, VALUE obj)
{
long data[2];
data[0] = (long)ibf_dump_object(dump, RCOMPLEX(obj)->real);
data[1] = (long)ibf_dump_object(dump, RCOMPLEX(obj)->imag);
(void)IBF_W(data, long, 2);
}
static VALUE
ibf_load_object_complex_rational(const struct ibf_load *load, const struct ibf_object_header *header, ibf_offset_t offset)
{
const struct ibf_object_complex_rational *nums = IBF_OBJBODY(struct ibf_object_complex_rational, offset);
VALUE a = ibf_load_object(load, nums->a);
VALUE b = ibf_load_object(load, nums->b);
VALUE obj = header->type == T_COMPLEX ?
rb_complex_new(a, b) : rb_rational_new(a, b);
if (header->internal) rb_obj_hide(obj);
if (header->frozen) rb_obj_freeze(obj);
return obj;
}
static void
ibf_dump_object_symbol(struct ibf_dump *dump, VALUE obj)
{
ibf_dump_object_string(dump, rb_sym2str(obj));
}
static VALUE
ibf_load_object_symbol(const struct ibf_load *load, const struct ibf_object_header *header, ibf_offset_t offset)
{
ibf_offset_t reading_pos = offset;
int encindex = (int)ibf_load_small_value(load, &reading_pos);
const long len = (long)ibf_load_small_value(load, &reading_pos);
const char *ptr = load->current_buffer->buff + reading_pos;
if (encindex > RUBY_ENCINDEX_BUILTIN_MAX) {
VALUE enc_name_str = ibf_load_object(load, encindex - RUBY_ENCINDEX_BUILTIN_MAX);
encindex = rb_enc_find_index(RSTRING_PTR(enc_name_str));
}
ID id = rb_intern3(ptr, len, rb_enc_from_index(encindex));
return ID2SYM(id);
}
typedef void (*ibf_dump_object_function)(struct ibf_dump *dump, VALUE obj);
static const ibf_dump_object_function dump_object_functions[RUBY_T_MASK+1] = {
ibf_dump_object_unsupported, /* T_NONE */
ibf_dump_object_unsupported, /* T_OBJECT */
ibf_dump_object_class, /* T_CLASS */
ibf_dump_object_unsupported, /* T_MODULE */
ibf_dump_object_float, /* T_FLOAT */
ibf_dump_object_string, /* T_STRING */
ibf_dump_object_regexp, /* T_REGEXP */
ibf_dump_object_array, /* T_ARRAY */
ibf_dump_object_hash, /* T_HASH */
ibf_dump_object_struct, /* T_STRUCT */
ibf_dump_object_bignum, /* T_BIGNUM */
ibf_dump_object_unsupported, /* T_FILE */
ibf_dump_object_data, /* T_DATA */
ibf_dump_object_unsupported, /* T_MATCH */
ibf_dump_object_complex_rational, /* T_COMPLEX */
ibf_dump_object_complex_rational, /* T_RATIONAL */
ibf_dump_object_unsupported, /* 0x10 */
ibf_dump_object_unsupported, /* 0x11 T_NIL */
ibf_dump_object_unsupported, /* 0x12 T_TRUE */
ibf_dump_object_unsupported, /* 0x13 T_FALSE */
ibf_dump_object_symbol, /* 0x14 T_SYMBOL */
ibf_dump_object_unsupported, /* T_FIXNUM */
ibf_dump_object_unsupported, /* T_UNDEF */
ibf_dump_object_unsupported, /* 0x17 */
ibf_dump_object_unsupported, /* 0x18 */
ibf_dump_object_unsupported, /* 0x19 */
ibf_dump_object_unsupported, /* T_IMEMO 0x1a */
ibf_dump_object_unsupported, /* T_NODE 0x1b */
ibf_dump_object_unsupported, /* T_ICLASS 0x1c */
ibf_dump_object_unsupported, /* T_ZOMBIE 0x1d */
ibf_dump_object_unsupported, /* 0x1e */
ibf_dump_object_unsupported, /* 0x1f */
};
static void
ibf_dump_object_object_header(struct ibf_dump *dump, const struct ibf_object_header header)
{
unsigned char byte =
(header.type << 0) |
(header.special_const << 5) |
(header.frozen << 6) |
(header.internal << 7);
IBF_WV(byte);
}
static struct ibf_object_header
ibf_load_object_object_header(const struct ibf_load *load, ibf_offset_t *offset)
{
unsigned char byte = ibf_load_byte(load, offset);
struct ibf_object_header header;
header.type = (byte >> 0) & 0x1f;
header.special_const = (byte >> 5) & 0x01;
header.frozen = (byte >> 6) & 0x01;
header.internal = (byte >> 7) & 0x01;
return header;
}
static ibf_offset_t
ibf_dump_object_object(struct ibf_dump *dump, VALUE obj)
{
struct ibf_object_header obj_header;
ibf_offset_t current_offset;
IBF_ZERO(obj_header);
obj_header.type = TYPE(obj);
IBF_W_ALIGN(ibf_offset_t);
current_offset = ibf_dump_pos(dump);
if (SPECIAL_CONST_P(obj) &&
! (SYMBOL_P(obj) ||
RB_FLOAT_TYPE_P(obj))) {
obj_header.special_const = TRUE;
obj_header.frozen = TRUE;
obj_header.internal = TRUE;
ibf_dump_object_object_header(dump, obj_header);
ibf_dump_write_small_value(dump, obj);
}
else {
obj_header.internal = SPECIAL_CONST_P(obj) ? FALSE : (RBASIC_CLASS(obj) == 0) ? TRUE : FALSE;
obj_header.special_const = FALSE;
obj_header.frozen = FL_TEST(obj, FL_FREEZE) ? TRUE : FALSE;
ibf_dump_object_object_header(dump, obj_header);
(*dump_object_functions[obj_header.type])(dump, obj);
}
return current_offset;
}
typedef VALUE (*ibf_load_object_function)(const struct ibf_load *load, const struct ibf_object_header *header, ibf_offset_t offset);
static const ibf_load_object_function load_object_functions[RUBY_T_MASK+1] = {
ibf_load_object_unsupported, /* T_NONE */
ibf_load_object_unsupported, /* T_OBJECT */
ibf_load_object_class, /* T_CLASS */
ibf_load_object_unsupported, /* T_MODULE */
ibf_load_object_float, /* T_FLOAT */
ibf_load_object_string, /* T_STRING */
ibf_load_object_regexp, /* T_REGEXP */
ibf_load_object_array, /* T_ARRAY */
ibf_load_object_hash, /* T_HASH */
ibf_load_object_struct, /* T_STRUCT */
ibf_load_object_bignum, /* T_BIGNUM */
ibf_load_object_unsupported, /* T_FILE */
ibf_load_object_data, /* T_DATA */
ibf_load_object_unsupported, /* T_MATCH */
ibf_load_object_complex_rational, /* T_COMPLEX */
ibf_load_object_complex_rational, /* T_RATIONAL */
ibf_load_object_unsupported, /* 0x10 */
ibf_load_object_unsupported, /* T_NIL */
ibf_load_object_unsupported, /* T_TRUE */
ibf_load_object_unsupported, /* T_FALSE */
ibf_load_object_symbol,
ibf_load_object_unsupported, /* T_FIXNUM */
ibf_load_object_unsupported, /* T_UNDEF */
ibf_load_object_unsupported, /* 0x17 */
ibf_load_object_unsupported, /* 0x18 */
ibf_load_object_unsupported, /* 0x19 */
ibf_load_object_unsupported, /* T_IMEMO 0x1a */
ibf_load_object_unsupported, /* T_NODE 0x1b */
ibf_load_object_unsupported, /* T_ICLASS 0x1c */
ibf_load_object_unsupported, /* T_ZOMBIE 0x1d */
ibf_load_object_unsupported, /* 0x1e */
ibf_load_object_unsupported, /* 0x1f */
};
static VALUE
ibf_load_object(const struct ibf_load *load, VALUE object_index)
{
if (object_index == 0) {
return Qnil;
}
else {
VALUE obj = pinned_list_fetch(load->current_buffer->obj_list, (long)object_index);
if (!obj) {
ibf_offset_t *offsets = (ibf_offset_t *)(load->current_buffer->obj_list_offset + load->current_buffer->buff);
ibf_offset_t offset = offsets[object_index];
const struct ibf_object_header header = ibf_load_object_object_header(load, &offset);
#if IBF_ISEQ_DEBUG
fprintf(stderr, "ibf_load_object: list=%#x offsets=%p offset=%#x\n",
load->current_buffer->obj_list_offset, (void *)offsets, offset);
fprintf(stderr, "ibf_load_object: type=%#x special=%d frozen=%d internal=%d\n",
header.type, header.special_const, header.frozen, header.internal);
#endif
if (offset >= load->current_buffer->size) {
rb_raise(rb_eIndexError, "object offset out of range: %u", offset);
}
if (header.special_const) {
ibf_offset_t reading_pos = offset;
obj = ibf_load_small_value(load, &reading_pos);
}
else {
obj = (*load_object_functions[header.type])(load, &header, offset);
}
pinned_list_store(load->current_buffer->obj_list, (long)object_index, obj);
}
#if IBF_ISEQ_DEBUG
fprintf(stderr, "ibf_load_object: index=%#"PRIxVALUE" obj=%#"PRIxVALUE"\n",
object_index, obj);
#endif
return obj;
}
}
struct ibf_dump_object_list_arg
{
struct ibf_dump *dump;
VALUE offset_list;
};
static int
ibf_dump_object_list_i(st_data_t key, st_data_t val, st_data_t ptr)
{
VALUE obj = (VALUE)key;
struct ibf_dump_object_list_arg *args = (struct ibf_dump_object_list_arg *)ptr;
ibf_offset_t offset = ibf_dump_object_object(args->dump, obj);
rb_ary_push(args->offset_list, UINT2NUM(offset));
return ST_CONTINUE;
}
static void
ibf_dump_object_list(struct ibf_dump *dump, ibf_offset_t *obj_list_offset, unsigned int *obj_list_size)
{
st_table *obj_table = dump->current_buffer->obj_table;
VALUE offset_list = rb_ary_hidden_new(obj_table->num_entries);
struct ibf_dump_object_list_arg args;
args.dump = dump;
args.offset_list = offset_list;
st_foreach(obj_table, ibf_dump_object_list_i, (st_data_t)&args);
IBF_W_ALIGN(ibf_offset_t);
*obj_list_offset = ibf_dump_pos(dump);
st_index_t size = obj_table->num_entries;
st_index_t i;
for (i=0; i<size; i++) {
ibf_offset_t offset = NUM2UINT(RARRAY_AREF(offset_list, i));
IBF_WV(offset);
}
*obj_list_size = (unsigned int)size;
}
static void
ibf_dump_mark(void *ptr)
{
struct ibf_dump *dump = (struct ibf_dump *)ptr;
rb_gc_mark(dump->global_buffer.str);
rb_mark_set(dump->global_buffer.obj_table);
rb_mark_set(dump->iseq_table);
}
static void
ibf_dump_free(void *ptr)
{
struct ibf_dump *dump = (struct ibf_dump *)ptr;
if (dump->global_buffer.obj_table) {
st_free_table(dump->global_buffer.obj_table);
dump->global_buffer.obj_table = 0;
}
if (dump->iseq_table) {
st_free_table(dump->iseq_table);
dump->iseq_table = 0;
}
}
static size_t
ibf_dump_memsize(const void *ptr)
{
struct ibf_dump *dump = (struct ibf_dump *)ptr;
size_t size = 0;
if (dump->iseq_table) size += st_memsize(dump->iseq_table);
if (dump->global_buffer.obj_table) size += st_memsize(dump->global_buffer.obj_table);
return size;
}
static const rb_data_type_t ibf_dump_type = {
"ibf_dump",
{ibf_dump_mark, ibf_dump_free, ibf_dump_memsize,},
0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_EMBEDDABLE
};
static void
ibf_dump_setup(struct ibf_dump *dump, VALUE dumper_obj)
{
dump->global_buffer.obj_table = NULL; // GC may run before a value is assigned
dump->iseq_table = NULL;
RB_OBJ_WRITE(dumper_obj, &dump->global_buffer.str, rb_str_new(0, 0));
dump->global_buffer.obj_table = ibf_dump_object_table_new();
dump->iseq_table = st_init_numtable(); /* need free */
dump->current_buffer = &dump->global_buffer;
}
VALUE
rb_iseq_ibf_dump(const rb_iseq_t *iseq, VALUE opt)
{
struct ibf_dump *dump;
struct ibf_header header = {{0}};
VALUE dump_obj;
VALUE str;
if (ISEQ_BODY(iseq)->parent_iseq != NULL ||
ISEQ_BODY(iseq)->local_iseq != iseq) {
rb_raise(rb_eRuntimeError, "should be top of iseq");
}
if (RTEST(ISEQ_COVERAGE(iseq))) {
rb_raise(rb_eRuntimeError, "should not compile with coverage");
}
dump_obj = TypedData_Make_Struct(0, struct ibf_dump, &ibf_dump_type, dump);
ibf_dump_setup(dump, dump_obj);
ibf_dump_write(dump, &header, sizeof(header));
ibf_dump_iseq(dump, iseq);
header.magic[0] = 'Y'; /* YARB */
header.magic[1] = 'A';
header.magic[2] = 'R';
header.magic[3] = 'B';
header.major_version = IBF_MAJOR_VERSION;
header.minor_version = IBF_MINOR_VERSION;
header.endian = IBF_ENDIAN_MARK;
header.wordsize = (uint8_t)SIZEOF_VALUE;
ibf_dump_iseq_list(dump, &header);
ibf_dump_object_list(dump, &header.global_object_list_offset, &header.global_object_list_size);
header.size = ibf_dump_pos(dump);
if (RTEST(opt)) {
VALUE opt_str = opt;
const char *ptr = StringValuePtr(opt_str);
header.extra_size = RSTRING_LENINT(opt_str);
ibf_dump_write(dump, ptr, header.extra_size);
}
else {
header.extra_size = 0;
}
ibf_dump_overwrite(dump, &header, sizeof(header), 0);
str = dump->global_buffer.str;
RB_GC_GUARD(dump_obj);
return str;
}
static const ibf_offset_t *
ibf_iseq_list(const struct ibf_load *load)
{
return (const ibf_offset_t *)(load->global_buffer.buff + load->header->iseq_list_offset);
}
void
rb_ibf_load_iseq_complete(rb_iseq_t *iseq)
{
struct ibf_load *load = RTYPEDDATA_DATA(iseq->aux.loader.obj);
rb_iseq_t *prev_src_iseq = load->iseq;
ibf_offset_t offset = ibf_iseq_list(load)[iseq->aux.loader.index];
load->iseq = iseq;
#if IBF_ISEQ_DEBUG
fprintf(stderr, "rb_ibf_load_iseq_complete: index=%#x offset=%#x size=%#x\n",
iseq->aux.loader.index, offset,
load->header->size);
#endif
ibf_load_iseq_each(load, iseq, offset);
ISEQ_COMPILE_DATA_CLEAR(iseq);
FL_UNSET((VALUE)iseq, ISEQ_NOT_LOADED_YET);
rb_iseq_init_trace(iseq);
load->iseq = prev_src_iseq;
}
#if USE_LAZY_LOAD
const rb_iseq_t *
rb_iseq_complete(const rb_iseq_t *iseq)
{
rb_ibf_load_iseq_complete((rb_iseq_t *)iseq);
return iseq;
}
#endif
static rb_iseq_t *
ibf_load_iseq(const struct ibf_load *load, const rb_iseq_t *index_iseq)
{
int iseq_index = (int)(VALUE)index_iseq;
#if IBF_ISEQ_DEBUG
fprintf(stderr, "ibf_load_iseq: index_iseq=%p iseq_list=%p\n",
(void *)index_iseq, (void *)load->iseq_list);
#endif
if (iseq_index == -1) {
return NULL;
}
else {
VALUE iseqv = pinned_list_fetch(load->iseq_list, iseq_index);
#if IBF_ISEQ_DEBUG
fprintf(stderr, "ibf_load_iseq: iseqv=%p\n", (void *)iseqv);
#endif
if (iseqv) {
return (rb_iseq_t *)iseqv;
}
else {
rb_iseq_t *iseq = iseq_imemo_alloc();
#if IBF_ISEQ_DEBUG
fprintf(stderr, "ibf_load_iseq: new iseq=%p\n", (void *)iseq);
#endif
FL_SET((VALUE)iseq, ISEQ_NOT_LOADED_YET);
iseq->aux.loader.obj = load->loader_obj;
iseq->aux.loader.index = iseq_index;
#if IBF_ISEQ_DEBUG
fprintf(stderr, "ibf_load_iseq: iseq=%p loader_obj=%p index=%d\n",
(void *)iseq, (void *)load->loader_obj, iseq_index);
#endif
pinned_list_store(load->iseq_list, iseq_index, (VALUE)iseq);
if (!USE_LAZY_LOAD || GET_VM()->builtin_function_table) {
#if IBF_ISEQ_DEBUG
fprintf(stderr, "ibf_load_iseq: loading iseq=%p\n", (void *)iseq);
#endif
rb_ibf_load_iseq_complete(iseq);
}
#if IBF_ISEQ_DEBUG
fprintf(stderr, "ibf_load_iseq: iseq=%p loaded %p\n",
(void *)iseq, (void *)load->iseq);
#endif
return iseq;
}
}
}
static void
ibf_load_setup_bytes(struct ibf_load *load, VALUE loader_obj, const char *bytes, size_t size)
{
struct ibf_header *header = (struct ibf_header *)bytes;
load->loader_obj = loader_obj;
load->global_buffer.buff = bytes;
load->header = header;
load->global_buffer.size = header->size;
load->global_buffer.obj_list_offset = header->global_object_list_offset;
load->global_buffer.obj_list_size = header->global_object_list_size;
RB_OBJ_WRITE(loader_obj, &load->iseq_list, pinned_list_new(header->iseq_list_size));
RB_OBJ_WRITE(loader_obj, &load->global_buffer.obj_list, pinned_list_new(load->global_buffer.obj_list_size));
load->iseq = NULL;
load->current_buffer = &load->global_buffer;
if (size < header->size) {
rb_raise(rb_eRuntimeError, "broken binary format");
}
if (strncmp(header->magic, "YARB", 4) != 0) {
rb_raise(rb_eRuntimeError, "unknown binary format");
}
if (header->major_version != IBF_MAJOR_VERSION ||
header->minor_version != IBF_MINOR_VERSION) {
rb_raise(rb_eRuntimeError, "unmatched version file (%u.%u for %u.%u)",
header->major_version, header->minor_version, IBF_MAJOR_VERSION, IBF_MINOR_VERSION);
}
if (header->endian != IBF_ENDIAN_MARK) {
rb_raise(rb_eRuntimeError, "unmatched endian: %c", header->endian);
}
if (header->wordsize != SIZEOF_VALUE) {
rb_raise(rb_eRuntimeError, "unmatched word size: %d", header->wordsize);
}
if (header->iseq_list_offset % RUBY_ALIGNOF(ibf_offset_t)) {
rb_raise(rb_eArgError, "unaligned iseq list offset: %u",
header->iseq_list_offset);
}
if (load->global_buffer.obj_list_offset % RUBY_ALIGNOF(ibf_offset_t)) {
rb_raise(rb_eArgError, "unaligned object list offset: %u",
load->global_buffer.obj_list_offset);
}
}
static void
ibf_load_setup(struct ibf_load *load, VALUE loader_obj, VALUE str)
{
if (RSTRING_LENINT(str) < (int)sizeof(struct ibf_header)) {
rb_raise(rb_eRuntimeError, "broken binary format");
}
if (USE_LAZY_LOAD) {
str = rb_str_new(RSTRING_PTR(str), RSTRING_LEN(str));
}
ibf_load_setup_bytes(load, loader_obj, StringValuePtr(str), RSTRING_LEN(str));
RB_OBJ_WRITE(loader_obj, &load->str, str);
}
static void
ibf_loader_mark(void *ptr)
{
struct ibf_load *load = (struct ibf_load *)ptr;
rb_gc_mark(load->str);
rb_gc_mark(load->iseq_list);
rb_gc_mark(load->global_buffer.obj_list);
}
static void
ibf_loader_free(void *ptr)
{
struct ibf_load *load = (struct ibf_load *)ptr;
ruby_xfree(load);
}
static size_t
ibf_loader_memsize(const void *ptr)
{
return sizeof(struct ibf_load);
}
static const rb_data_type_t ibf_load_type = {
"ibf_loader",
{ibf_loader_mark, ibf_loader_free, ibf_loader_memsize,},
0, 0, RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FREE_IMMEDIATELY
};
const rb_iseq_t *
rb_iseq_ibf_load(VALUE str)
{
struct ibf_load *load;
rb_iseq_t *iseq;
VALUE loader_obj = TypedData_Make_Struct(0, struct ibf_load, &ibf_load_type, load);
ibf_load_setup(load, loader_obj, str);
iseq = ibf_load_iseq(load, 0);
RB_GC_GUARD(loader_obj);
return iseq;
}
const rb_iseq_t *
rb_iseq_ibf_load_bytes(const char *bytes, size_t size)
{
struct ibf_load *load;
rb_iseq_t *iseq;
VALUE loader_obj = TypedData_Make_Struct(0, struct ibf_load, &ibf_load_type, load);
ibf_load_setup_bytes(load, loader_obj, bytes, size);
iseq = ibf_load_iseq(load, 0);
RB_GC_GUARD(loader_obj);
return iseq;
}
VALUE
rb_iseq_ibf_load_extra_data(VALUE str)
{
struct ibf_load *load;
VALUE loader_obj = TypedData_Make_Struct(0, struct ibf_load, &ibf_load_type, load);
VALUE extra_str;
ibf_load_setup(load, loader_obj, str);
extra_str = rb_str_new(load->global_buffer.buff + load->header->size, load->header->extra_size);
RB_GC_GUARD(loader_obj);
return extra_str;
}
#include "prism_compile.c"
|