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
|
#include "prism.h"
#include "version.h"
/**
* This compiler defines its own concept of the location of a node. We do this
* because we want to pair line information with node identifier so that we can
* have reproducible parses.
*/
typedef struct {
/** This is the line number of a node. */
int32_t line;
/** This is a unique identifier for the node. */
uint32_t node_id;
} pm_node_location_t;
/******************************************************************************/
/* These macros operate on pm_node_location_t structs as opposed to NODE*s. */
/******************************************************************************/
#define PUSH_ADJUST(seq, location, label) \
ADD_ELEM((seq), (LINK_ELEMENT *) new_adjust_body(iseq, (label), (int) (location).line))
#define PUSH_ADJUST_RESTORE(seq, label) \
ADD_ELEM((seq), (LINK_ELEMENT *) new_adjust_body(iseq, (label), -1))
#define PUSH_INSN(seq, location, insn) \
ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_body(iseq, (int) (location).line, (int) (location).node_id, BIN(insn), 0))
#define PUSH_INSN1(seq, location, insn, op1) \
ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_body(iseq, (int) (location).line, (int) (location).node_id, BIN(insn), 1, (VALUE)(op1)))
#define PUSH_INSN2(seq, location, insn, op1, op2) \
ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_body(iseq, (int) (location).line, (int) (location).node_id, BIN(insn), 2, (VALUE)(op1), (VALUE)(op2)))
#define PUSH_INSN3(seq, location, insn, op1, op2, op3) \
ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_body(iseq, (int) (location).line, (int) (location).node_id, BIN(insn), 3, (VALUE)(op1), (VALUE)(op2), (VALUE)(op3)))
#define PUSH_INSNL(seq, location, insn, label) \
(PUSH_INSN1(seq, location, insn, label), LABEL_REF(label))
#define PUSH_LABEL(seq, label) \
ADD_ELEM((seq), (LINK_ELEMENT *) (label))
#define PUSH_SEND_R(seq, location, id, argc, block, flag, keywords) \
ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_send(iseq, (int) (location).line, (int) (location).node_id, (id), (VALUE)(argc), (block), (VALUE)(flag), (keywords)))
#define PUSH_SEND(seq, location, id, argc) \
PUSH_SEND_R((seq), location, (id), (argc), NULL, (VALUE)INT2FIX(0), NULL)
#define PUSH_SEND_WITH_FLAG(seq, location, id, argc, flag) \
PUSH_SEND_R((seq), location, (id), (argc), NULL, (VALUE)(flag), NULL)
#define PUSH_SEND_WITH_BLOCK(seq, location, id, argc, block) \
PUSH_SEND_R((seq), location, (id), (argc), (block), (VALUE)INT2FIX(0), NULL)
#define PUSH_CALL(seq, location, id, argc) \
PUSH_SEND_R((seq), location, (id), (argc), NULL, (VALUE)INT2FIX(VM_CALL_FCALL), NULL)
#define PUSH_CALL_WITH_BLOCK(seq, location, id, argc, block) \
PUSH_SEND_R((seq), location, (id), (argc), (block), (VALUE)INT2FIX(VM_CALL_FCALL), NULL)
#define PUSH_TRACE(seq, event) \
ADD_ELEM((seq), (LINK_ELEMENT *) new_trace_body(iseq, (event), 0))
#define PUSH_CATCH_ENTRY(type, ls, le, iseqv, lc) \
ADD_CATCH_ENTRY((type), (ls), (le), (iseqv), (lc))
#define PUSH_SEQ(seq1, seq2) \
APPEND_LIST((seq1), (seq2))
#define PUSH_SYNTHETIC_PUTNIL(seq, iseq) \
do { \
int lineno = ISEQ_COMPILE_DATA(iseq)->last_line; \
if (lineno == 0) lineno = FIX2INT(rb_iseq_first_lineno(iseq)); \
ADD_SYNTHETIC_INSN(seq, lineno, -1, putnil); \
} while (0)
/******************************************************************************/
/* These functions compile getlocal/setlocal instructions but operate on */
/* prism locations instead of NODEs. */
/******************************************************************************/
static void
pm_iseq_add_getlocal(rb_iseq_t *iseq, LINK_ANCHOR *const seq, int line, int node_id, int idx, int level)
{
if (iseq_local_block_param_p(iseq, idx, level)) {
ADD_ELEM(seq, (LINK_ELEMENT *) new_insn_body(iseq, line, node_id, BIN(getblockparam), 2, INT2FIX((idx) + VM_ENV_DATA_SIZE - 1), INT2FIX(level)));
}
else {
ADD_ELEM(seq, (LINK_ELEMENT *) new_insn_body(iseq, line, node_id, BIN(getlocal), 2, 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
pm_iseq_add_setlocal(rb_iseq_t *iseq, LINK_ANCHOR *const seq, int line, int node_id, int idx, int level)
{
if (iseq_local_block_param_p(iseq, idx, level)) {
ADD_ELEM(seq, (LINK_ELEMENT *) new_insn_body(iseq, line, node_id, BIN(setblockparam), 2, INT2FIX((idx) + VM_ENV_DATA_SIZE - 1), INT2FIX(level)));
}
else {
ADD_ELEM(seq, (LINK_ELEMENT *) new_insn_body(iseq, line, node_id, BIN(setlocal), 2, INT2FIX((idx) + VM_ENV_DATA_SIZE - 1), INT2FIX(level)));
}
if (level > 0) access_outer_variables(iseq, level, iseq_lvar_id(iseq, idx, level), Qtrue);
}
#define PUSH_GETLOCAL(seq, location, idx, level) \
pm_iseq_add_getlocal(iseq, (seq), (int) (location).line, (int) (location).node_id, (idx), (level))
#define PUSH_SETLOCAL(seq, location, idx, level) \
pm_iseq_add_setlocal(iseq, (seq), (int) (location).line, (int) (location).node_id, (idx), (level))
/******************************************************************************/
/* These are helper macros for the compiler. */
/******************************************************************************/
#define OLD_ISEQ NEW_ISEQ
#undef NEW_ISEQ
#define NEW_ISEQ(node, name, type, line_no) \
pm_new_child_iseq(iseq, (node), rb_fstring(name), 0, (type), (line_no))
#define OLD_CHILD_ISEQ NEW_CHILD_ISEQ
#undef NEW_CHILD_ISEQ
#define NEW_CHILD_ISEQ(node, name, type, line_no) \
pm_new_child_iseq(iseq, (node), rb_fstring(name), iseq, (type), (line_no))
#define PM_COMPILE(node) \
pm_compile_node(iseq, (node), ret, popped, scope_node)
#define PM_COMPILE_INTO_ANCHOR(_ret, node) \
pm_compile_node(iseq, (node), _ret, popped, scope_node)
#define PM_COMPILE_POPPED(node) \
pm_compile_node(iseq, (node), ret, true, scope_node)
#define PM_COMPILE_NOT_POPPED(node) \
pm_compile_node(iseq, (node), ret, false, scope_node)
#define PM_NODE_START_LOCATION(parser, node) \
((pm_node_location_t) { .line = pm_newline_list_line(&(parser)->newline_list, ((const pm_node_t *) (node))->location.start, (parser)->start_line), .node_id = ((const pm_node_t *) (node))->node_id })
#define PM_NODE_END_LOCATION(parser, node) \
((pm_node_location_t) { .line = pm_newline_list_line(&(parser)->newline_list, ((const pm_node_t *) (node))->location.end, (parser)->start_line), .node_id = ((const pm_node_t *) (node))->node_id })
#define PM_LOCATION_START_LOCATION(parser, location, id) \
((pm_node_location_t) { .line = pm_newline_list_line(&(parser)->newline_list, (location)->start, (parser)->start_line), .node_id = id })
#define PM_NODE_START_LINE_COLUMN(parser, node) \
pm_newline_list_line_column(&(parser)->newline_list, ((const pm_node_t *) (node))->location.start, (parser)->start_line)
#define PM_NODE_END_LINE_COLUMN(parser, node) \
pm_newline_list_line_column(&(parser)->newline_list, ((const pm_node_t *) (node))->location.end, (parser)->start_line)
#define PM_LOCATION_START_LINE_COLUMN(parser, location) \
pm_newline_list_line_column(&(parser)->newline_list, (location)->start, (parser)->start_line)
static int
pm_node_line_number(const pm_parser_t *parser, const pm_node_t *node)
{
return (int) pm_newline_list_line(&parser->newline_list, node->location.start, parser->start_line);
}
static int
pm_location_line_number(const pm_parser_t *parser, const pm_location_t *location) {
return (int) pm_newline_list_line(&parser->newline_list, location->start, parser->start_line);
}
/**
* Parse the value of a pm_integer_t into a Ruby Integer.
*/
static VALUE
parse_integer_value(const pm_integer_t *integer)
{
VALUE result;
if (integer->values == NULL) {
result = UINT2NUM(integer->value);
}
else {
VALUE string = rb_str_new(NULL, integer->length * 8);
unsigned char *bytes = (unsigned char *) RSTRING_PTR(string);
size_t offset = integer->length * 8;
for (size_t value_index = 0; value_index < integer->length; value_index++) {
uint32_t value = integer->values[value_index];
for (int index = 0; index < 8; index++) {
int byte = (value >> (4 * index)) & 0xf;
bytes[--offset] = byte < 10 ? byte + '0' : byte - 10 + 'a';
}
}
result = rb_funcall(string, rb_intern("to_i"), 1, UINT2NUM(16));
}
if (integer->negative) {
result = rb_funcall(result, rb_intern("-@"), 0);
}
return result;
}
/**
* Convert the value of an integer node into a Ruby Integer.
*/
static inline VALUE
parse_integer(const pm_integer_node_t *node)
{
return parse_integer_value(&node->value);
}
/**
* Convert the value of a float node into a Ruby Float.
*/
static VALUE
parse_float(const pm_float_node_t *node)
{
return DBL2NUM(node->value);
}
/**
* Convert the value of a rational node into a Ruby Rational. Rational nodes can
* either be wrapping an integer node or a float node. If it's an integer node,
* we can reuse our parsing. If it's not, then we'll parse the numerator and
* then parse the denominator and create the rational from those two values.
*/
static VALUE
parse_rational(const pm_rational_node_t *node)
{
VALUE numerator = parse_integer_value(&node->numerator);
VALUE denominator = parse_integer_value(&node->denominator);
return rb_rational_new(numerator, denominator);
}
/**
* Convert the value of an imaginary node into a Ruby Complex. Imaginary nodes
* can be wrapping an integer node, a float node, or a rational node. In all
* cases we will reuse parsing functions seen above to get the inner value, and
* then convert into an imaginary with rb_complex_raw.
*/
static VALUE
parse_imaginary(const pm_imaginary_node_t *node)
{
VALUE imaginary_part;
switch (PM_NODE_TYPE(node->numeric)) {
case PM_FLOAT_NODE: {
imaginary_part = parse_float((const pm_float_node_t *) node->numeric);
break;
}
case PM_INTEGER_NODE: {
imaginary_part = parse_integer((const pm_integer_node_t *) node->numeric);
break;
}
case PM_RATIONAL_NODE: {
imaginary_part = parse_rational((const pm_rational_node_t *) node->numeric);
break;
}
default:
rb_bug("Unexpected numeric type on imaginary number %s\n", pm_node_type_to_str(PM_NODE_TYPE(node->numeric)));
}
return rb_complex_raw(INT2FIX(0), imaginary_part);
}
static inline VALUE
parse_string(const pm_scope_node_t *scope_node, const pm_string_t *string)
{
return rb_enc_str_new((const char *) pm_string_source(string), pm_string_length(string), scope_node->encoding);
}
/**
* Certain strings can have their encoding differ from the parser's encoding due
* to bytes or escape sequences that have the top bit set. This function handles
* creating those strings based on the flags set on the owning node.
*/
static inline VALUE
parse_string_encoded(const pm_node_t *node, const pm_string_t *string, rb_encoding *default_encoding)
{
rb_encoding *encoding;
if (node->flags & PM_ENCODING_FLAGS_FORCED_BINARY_ENCODING) {
encoding = rb_ascii8bit_encoding();
}
else if (node->flags & PM_ENCODING_FLAGS_FORCED_UTF8_ENCODING) {
encoding = rb_utf8_encoding();
}
else {
encoding = default_encoding;
}
return rb_enc_str_new((const char *) pm_string_source(string), pm_string_length(string), encoding);
}
static inline VALUE
parse_static_literal_string(rb_iseq_t *iseq, const pm_scope_node_t *scope_node, const pm_node_t *node, const pm_string_t *string)
{
rb_encoding *encoding;
if (node->flags & PM_STRING_FLAGS_FORCED_BINARY_ENCODING) {
encoding = rb_ascii8bit_encoding();
}
else if (node->flags & PM_STRING_FLAGS_FORCED_UTF8_ENCODING) {
encoding = rb_utf8_encoding();
}
else {
encoding = scope_node->encoding;
}
VALUE value = rb_enc_literal_str((const char *) pm_string_source(string), pm_string_length(string), encoding);
rb_enc_str_coderange(value);
if (ISEQ_COMPILE_DATA(iseq)->option->debug_frozen_string_literal || RTEST(ruby_debug)) {
int line_number = pm_node_line_number(scope_node->parser, node);
value = rb_str_with_debug_created_info(value, rb_iseq_path(iseq), line_number);
}
return value;
}
static inline ID
parse_string_symbol(const pm_scope_node_t *scope_node, const pm_symbol_node_t *symbol)
{
rb_encoding *encoding;
if (symbol->base.flags & PM_SYMBOL_FLAGS_FORCED_UTF8_ENCODING) {
encoding = rb_utf8_encoding();
}
else if (symbol->base.flags & PM_SYMBOL_FLAGS_FORCED_BINARY_ENCODING) {
encoding = rb_ascii8bit_encoding();
}
else if (symbol->base.flags & PM_SYMBOL_FLAGS_FORCED_US_ASCII_ENCODING) {
encoding = rb_usascii_encoding();
}
else {
encoding = scope_node->encoding;
}
return rb_intern3((const char *) pm_string_source(&symbol->unescaped), pm_string_length(&symbol->unescaped), encoding);
}
static int
pm_optimizable_range_item_p(const pm_node_t *node)
{
return (!node || PM_NODE_TYPE_P(node, PM_INTEGER_NODE) || PM_NODE_TYPE_P(node, PM_NIL_NODE));
}
/** Raise an error corresponding to the invalid regular expression. */
static VALUE
parse_regexp_error(rb_iseq_t *iseq, int32_t line_number, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
VALUE error = rb_syntax_error_append(Qnil, rb_iseq_path(iseq), line_number, -1, NULL, "%" PRIsVALUE, args);
va_end(args);
rb_exc_raise(error);
}
static VALUE
parse_regexp_string_part(rb_iseq_t *iseq, const pm_scope_node_t *scope_node, const pm_node_t *node, const pm_string_t *unescaped, rb_encoding *implicit_regexp_encoding, rb_encoding *explicit_regexp_encoding)
{
// If we were passed an explicit regexp encoding, then we need to double
// check that it's okay here for this fragment of the string.
rb_encoding *encoding;
if (explicit_regexp_encoding != NULL) {
encoding = explicit_regexp_encoding;
}
else if (node->flags & PM_STRING_FLAGS_FORCED_BINARY_ENCODING) {
encoding = rb_ascii8bit_encoding();
}
else if (node->flags & PM_STRING_FLAGS_FORCED_UTF8_ENCODING) {
encoding = rb_utf8_encoding();
}
else {
encoding = implicit_regexp_encoding;
}
VALUE string = rb_enc_str_new((const char *) pm_string_source(unescaped), pm_string_length(unescaped), encoding);
VALUE error = rb_reg_check_preprocess(string);
if (error != Qnil) parse_regexp_error(iseq, pm_node_line_number(scope_node->parser, node), "%" PRIsVALUE, rb_obj_as_string(error));
return string;
}
static VALUE
pm_static_literal_concat(rb_iseq_t *iseq, const pm_node_list_t *nodes, const pm_scope_node_t *scope_node, rb_encoding *implicit_regexp_encoding, rb_encoding *explicit_regexp_encoding, bool top)
{
VALUE current = Qnil;
for (size_t index = 0; index < nodes->size; index++) {
const pm_node_t *part = nodes->nodes[index];
VALUE string;
switch (PM_NODE_TYPE(part)) {
case PM_STRING_NODE:
if (implicit_regexp_encoding != NULL) {
if (top) {
string = parse_regexp_string_part(iseq, scope_node, part, &((const pm_string_node_t *) part)->unescaped, implicit_regexp_encoding, explicit_regexp_encoding);
}
else {
string = parse_string_encoded(part, &((const pm_string_node_t *) part)->unescaped, scope_node->encoding);
VALUE error = rb_reg_check_preprocess(string);
if (error != Qnil) parse_regexp_error(iseq, pm_node_line_number(scope_node->parser, part), "%" PRIsVALUE, rb_obj_as_string(error));
}
}
else {
string = parse_string_encoded(part, &((const pm_string_node_t *) part)->unescaped, scope_node->encoding);
}
break;
case PM_INTERPOLATED_STRING_NODE:
string = pm_static_literal_concat(iseq, &((const pm_interpolated_string_node_t *) part)->parts, scope_node, implicit_regexp_encoding, explicit_regexp_encoding, false);
break;
case PM_EMBEDDED_STATEMENTS_NODE: {
const pm_embedded_statements_node_t *cast = (const pm_embedded_statements_node_t *) part;
string = pm_static_literal_concat(iseq, &cast->statements->body, scope_node, implicit_regexp_encoding, explicit_regexp_encoding, false);
break;
}
default:
RUBY_ASSERT(false && "unexpected node type in pm_static_literal_concat");
return Qnil;
}
if (current != Qnil) {
current = rb_str_concat(current, string);
}
else {
current = string;
}
}
return top ? rb_fstring(current) : current;
}
#define RE_OPTION_ENCODING_SHIFT 8
#define RE_OPTION_ENCODING(encoding) (((encoding) & 0xFF) << RE_OPTION_ENCODING_SHIFT)
#define ARG_ENCODING_NONE 32
#define ARG_ENCODING_FIXED 16
#define ENC_ASCII8BIT 1
#define ENC_EUC_JP 2
#define ENC_Windows_31J 3
#define ENC_UTF8 4
/**
* Check the prism flags of a regular expression-like node and return the flags
* that are expected by the CRuby VM.
*/
static int
parse_regexp_flags(const pm_node_t *node)
{
int flags = 0;
// Check "no encoding" first so that flags don't get clobbered
// We're calling `rb_char_to_option_kcode` in this case so that
// we don't need to have access to `ARG_ENCODING_NONE`
if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_ASCII_8BIT)) {
flags |= ARG_ENCODING_NONE;
}
if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_EUC_JP)) {
flags |= (ARG_ENCODING_FIXED | RE_OPTION_ENCODING(ENC_EUC_JP));
}
if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_WINDOWS_31J)) {
flags |= (ARG_ENCODING_FIXED | RE_OPTION_ENCODING(ENC_Windows_31J));
}
if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_UTF_8)) {
flags |= (ARG_ENCODING_FIXED | RE_OPTION_ENCODING(ENC_UTF8));
}
if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_IGNORE_CASE)) {
flags |= ONIG_OPTION_IGNORECASE;
}
if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_MULTI_LINE)) {
flags |= ONIG_OPTION_MULTILINE;
}
if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_EXTENDED)) {
flags |= ONIG_OPTION_EXTEND;
}
return flags;
}
#undef RE_OPTION_ENCODING_SHIFT
#undef RE_OPTION_ENCODING
#undef ARG_ENCODING_FIXED
#undef ARG_ENCODING_NONE
#undef ENC_ASCII8BIT
#undef ENC_EUC_JP
#undef ENC_Windows_31J
#undef ENC_UTF8
static rb_encoding *
parse_regexp_encoding(const pm_scope_node_t *scope_node, const pm_node_t *node)
{
if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_FORCED_BINARY_ENCODING) || PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_ASCII_8BIT)) {
return rb_ascii8bit_encoding();
}
else if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_UTF_8)) {
return rb_utf8_encoding();
}
else if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_EUC_JP)) {
return rb_enc_get_from_index(ENCINDEX_EUC_JP);
}
else if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_WINDOWS_31J)) {
return rb_enc_get_from_index(ENCINDEX_Windows_31J);
}
else {
return NULL;
}
}
static VALUE
parse_regexp(rb_iseq_t *iseq, const pm_scope_node_t *scope_node, const pm_node_t *node, VALUE string)
{
VALUE errinfo = rb_errinfo();
int32_t line_number = pm_node_line_number(scope_node->parser, node);
VALUE regexp = rb_reg_compile(string, parse_regexp_flags(node), (const char *) pm_string_source(&scope_node->parser->filepath), line_number);
if (NIL_P(regexp)) {
VALUE message = rb_attr_get(rb_errinfo(), idMesg);
rb_set_errinfo(errinfo);
parse_regexp_error(iseq, line_number, "%" PRIsVALUE, message);
return Qnil;
}
rb_obj_freeze(regexp);
return regexp;
}
static inline VALUE
parse_regexp_literal(rb_iseq_t *iseq, const pm_scope_node_t *scope_node, const pm_node_t *node, const pm_string_t *unescaped)
{
rb_encoding *regexp_encoding = parse_regexp_encoding(scope_node, node);
if (regexp_encoding == NULL) regexp_encoding = scope_node->encoding;
VALUE string = rb_enc_str_new((const char *) pm_string_source(unescaped), pm_string_length(unescaped), regexp_encoding);
return parse_regexp(iseq, scope_node, node, string);
}
static inline VALUE
parse_regexp_concat(rb_iseq_t *iseq, const pm_scope_node_t *scope_node, const pm_node_t *node, const pm_node_list_t *parts)
{
rb_encoding *explicit_regexp_encoding = parse_regexp_encoding(scope_node, node);
rb_encoding *implicit_regexp_encoding = explicit_regexp_encoding != NULL ? explicit_regexp_encoding : scope_node->encoding;
VALUE string = pm_static_literal_concat(iseq, parts, scope_node, implicit_regexp_encoding, explicit_regexp_encoding, false);
return parse_regexp(iseq, scope_node, node, string);
}
static void pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node);
static int
pm_interpolated_node_compile(rb_iseq_t *iseq, const pm_node_list_t *parts, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node, rb_encoding *implicit_regexp_encoding, rb_encoding *explicit_regexp_encoding, bool mutable_result, bool frozen_result)
{
int stack_size = 0;
size_t parts_size = parts->size;
bool interpolated = false;
if (parts_size > 0) {
VALUE current_string = Qnil;
pm_node_location_t current_location = *node_location;
for (size_t index = 0; index < parts_size; index++) {
const pm_node_t *part = parts->nodes[index];
if (PM_NODE_TYPE_P(part, PM_STRING_NODE)) {
const pm_string_node_t *string_node = (const pm_string_node_t *) part;
VALUE string_value;
if (implicit_regexp_encoding == NULL) {
string_value = parse_string_encoded(part, &string_node->unescaped, scope_node->encoding);
}
else {
string_value = parse_regexp_string_part(iseq, scope_node, (const pm_node_t *) string_node, &string_node->unescaped, implicit_regexp_encoding, explicit_regexp_encoding);
}
if (RTEST(current_string)) {
current_string = rb_str_concat(current_string, string_value);
}
else {
current_string = string_value;
if (index != 0) current_location = PM_NODE_END_LOCATION(scope_node->parser, part);
}
}
else {
interpolated = true;
if (
PM_NODE_TYPE_P(part, PM_EMBEDDED_STATEMENTS_NODE) &&
((const pm_embedded_statements_node_t *) part)->statements != NULL &&
((const pm_embedded_statements_node_t *) part)->statements->body.size == 1 &&
PM_NODE_TYPE_P(((const pm_embedded_statements_node_t *) part)->statements->body.nodes[0], PM_STRING_NODE)
) {
const pm_string_node_t *string_node = (const pm_string_node_t *) ((const pm_embedded_statements_node_t *) part)->statements->body.nodes[0];
VALUE string_value;
if (implicit_regexp_encoding == NULL) {
string_value = parse_string_encoded(part, &string_node->unescaped, scope_node->encoding);
}
else {
string_value = parse_regexp_string_part(iseq, scope_node, (const pm_node_t *) string_node, &string_node->unescaped, implicit_regexp_encoding, explicit_regexp_encoding);
}
if (RTEST(current_string)) {
current_string = rb_str_concat(current_string, string_value);
}
else {
current_string = string_value;
current_location = PM_NODE_START_LOCATION(scope_node->parser, part);
}
}
else {
if (!RTEST(current_string)) {
rb_encoding *encoding;
if (implicit_regexp_encoding != NULL) {
if (explicit_regexp_encoding != NULL) {
encoding = explicit_regexp_encoding;
}
else if (scope_node->parser->encoding == PM_ENCODING_US_ASCII_ENTRY) {
encoding = rb_ascii8bit_encoding();
}
else {
encoding = implicit_regexp_encoding;
}
}
else {
encoding = scope_node->encoding;
}
if (parts_size == 1) {
current_string = rb_enc_str_new(NULL, 0, encoding);
}
}
if (RTEST(current_string)) {
VALUE operand = rb_fstring(current_string);
PUSH_INSN1(ret, current_location, putobject, operand);
stack_size++;
}
PM_COMPILE_NOT_POPPED(part);
const pm_node_location_t current_location = PM_NODE_START_LOCATION(scope_node->parser, part);
PUSH_INSN(ret, current_location, dup);
{
const struct rb_callinfo *callinfo = new_callinfo(iseq, idTo_s, 0, VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE, NULL, FALSE);
PUSH_INSN1(ret, current_location, objtostring, callinfo);
}
PUSH_INSN(ret, current_location, anytostring);
current_string = Qnil;
stack_size++;
}
}
}
if (RTEST(current_string)) {
current_string = rb_fstring(current_string);
if (stack_size == 0) {
if (frozen_result) {
PUSH_INSN1(ret, current_location, putobject, current_string);
} else if (mutable_result || interpolated) {
PUSH_INSN1(ret, current_location, putstring, current_string);
} else {
PUSH_INSN1(ret, current_location, putchilledstring, current_string);
}
} else {
PUSH_INSN1(ret, current_location, putobject, current_string);
}
current_string = Qnil;
stack_size++;
}
}
else {
PUSH_INSN(ret, *node_location, putnil);
}
return stack_size;
}
static void
pm_compile_regexp_dynamic(rb_iseq_t *iseq, const pm_node_t *node, const pm_node_list_t *parts, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
{
rb_encoding *explicit_regexp_encoding = parse_regexp_encoding(scope_node, node);
rb_encoding *implicit_regexp_encoding = explicit_regexp_encoding != NULL ? explicit_regexp_encoding : scope_node->encoding;
int length = pm_interpolated_node_compile(iseq, parts, node_location, ret, popped, scope_node, implicit_regexp_encoding, explicit_regexp_encoding, false, false);
PUSH_INSN2(ret, *node_location, toregexp, INT2FIX(parse_regexp_flags(node) & 0xFF), INT2FIX(length));
}
static VALUE
pm_source_file_value(const pm_source_file_node_t *node, const pm_scope_node_t *scope_node)
{
const pm_string_t *filepath = &node->filepath;
size_t length = pm_string_length(filepath);
if (length > 0) {
rb_encoding *filepath_encoding = scope_node->filepath_encoding != NULL ? scope_node->filepath_encoding : rb_utf8_encoding();
return rb_enc_interned_str((const char *) pm_string_source(filepath), length, filepath_encoding);
}
else {
return rb_fstring_lit("<compiled>");
}
}
/**
* Return a static literal string, optionally with attached debugging
* information.
*/
static VALUE
pm_static_literal_string(rb_iseq_t *iseq, VALUE string, int line_number)
{
if (ISEQ_COMPILE_DATA(iseq)->option->debug_frozen_string_literal || RTEST(ruby_debug)) {
return rb_str_with_debug_created_info(string, rb_iseq_path(iseq), line_number);
}
else {
return rb_fstring(string);
}
}
/**
* Certain nodes can be compiled literally. This function returns the literal
* value described by the given node. For example, an array node with all static
* literal values can be compiled into a literal array.
*/
static VALUE
pm_static_literal_value(rb_iseq_t *iseq, const pm_node_t *node, const pm_scope_node_t *scope_node)
{
// Every node that comes into this function should already be marked as
// static literal. If it's not, then we have a bug somewhere.
RUBY_ASSERT(PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL));
switch (PM_NODE_TYPE(node)) {
case PM_ARRAY_NODE: {
const pm_array_node_t *cast = (const pm_array_node_t *) node;
const pm_node_list_t *elements = &cast->elements;
VALUE value = rb_ary_hidden_new(elements->size);
for (size_t index = 0; index < elements->size; index++) {
rb_ary_push(value, pm_static_literal_value(iseq, elements->nodes[index], scope_node));
}
OBJ_FREEZE(value);
return value;
}
case PM_FALSE_NODE:
return Qfalse;
case PM_FLOAT_NODE:
return parse_float((const pm_float_node_t *) node);
case PM_HASH_NODE: {
const pm_hash_node_t *cast = (const pm_hash_node_t *) node;
const pm_node_list_t *elements = &cast->elements;
VALUE array = rb_ary_hidden_new(elements->size * 2);
for (size_t index = 0; index < elements->size; index++) {
RUBY_ASSERT(PM_NODE_TYPE_P(elements->nodes[index], PM_ASSOC_NODE));
const pm_assoc_node_t *cast = (const pm_assoc_node_t *) elements->nodes[index];
VALUE pair[2] = { pm_static_literal_value(iseq, cast->key, scope_node), pm_static_literal_value(iseq, cast->value, scope_node) };
rb_ary_cat(array, pair, 2);
}
VALUE value = rb_hash_new_with_size(elements->size);
rb_hash_bulk_insert(RARRAY_LEN(array), RARRAY_CONST_PTR(array), value);
value = rb_obj_hide(value);
OBJ_FREEZE(value);
return value;
}
case PM_IMAGINARY_NODE:
return parse_imaginary((const pm_imaginary_node_t *) node);
case PM_INTEGER_NODE:
return parse_integer((const pm_integer_node_t *) node);
case PM_INTERPOLATED_MATCH_LAST_LINE_NODE: {
const pm_interpolated_match_last_line_node_t *cast = (const pm_interpolated_match_last_line_node_t *) node;
return parse_regexp_concat(iseq, scope_node, (const pm_node_t *) cast, &cast->parts);
}
case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE: {
const pm_interpolated_regular_expression_node_t *cast = (const pm_interpolated_regular_expression_node_t *) node;
return parse_regexp_concat(iseq, scope_node, (const pm_node_t *) cast, &cast->parts);
}
case PM_INTERPOLATED_STRING_NODE: {
VALUE string = pm_static_literal_concat(iseq, &((const pm_interpolated_string_node_t *) node)->parts, scope_node, NULL, NULL, false);
int line_number = pm_node_line_number(scope_node->parser, node);
return pm_static_literal_string(iseq, string, line_number);
}
case PM_INTERPOLATED_SYMBOL_NODE: {
const pm_interpolated_symbol_node_t *cast = (const pm_interpolated_symbol_node_t *) node;
VALUE string = pm_static_literal_concat(iseq, &cast->parts, scope_node, NULL, NULL, true);
return ID2SYM(rb_intern_str(string));
}
case PM_MATCH_LAST_LINE_NODE: {
const pm_match_last_line_node_t *cast = (const pm_match_last_line_node_t *) node;
return parse_regexp_literal(iseq, scope_node, (const pm_node_t *) cast, &cast->unescaped);
}
case PM_NIL_NODE:
return Qnil;
case PM_RATIONAL_NODE:
return parse_rational((const pm_rational_node_t *) node);
case PM_REGULAR_EXPRESSION_NODE: {
const pm_regular_expression_node_t *cast = (const pm_regular_expression_node_t *) node;
return parse_regexp_literal(iseq, scope_node, (const pm_node_t *) cast, &cast->unescaped);
}
case PM_SOURCE_ENCODING_NODE:
return rb_enc_from_encoding(scope_node->encoding);
case PM_SOURCE_FILE_NODE: {
const pm_source_file_node_t *cast = (const pm_source_file_node_t *) node;
return pm_source_file_value(cast, scope_node);
}
case PM_SOURCE_LINE_NODE:
return INT2FIX(pm_node_line_number(scope_node->parser, node));
case PM_STRING_NODE: {
const pm_string_node_t *cast = (const pm_string_node_t *) node;
return parse_static_literal_string(iseq, scope_node, node, &cast->unescaped);
}
case PM_SYMBOL_NODE:
return ID2SYM(parse_string_symbol(scope_node, (const pm_symbol_node_t *) node));
case PM_TRUE_NODE:
return Qtrue;
default:
rb_bug("Don't have a literal value for node type %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
return Qfalse;
}
}
/**
* A helper for converting a pm_location_t into a rb_code_location_t.
*/
static rb_code_location_t
pm_code_location(const pm_scope_node_t *scope_node, const pm_node_t *node)
{
const pm_line_column_t start_location = PM_NODE_START_LINE_COLUMN(scope_node->parser, node);
const pm_line_column_t end_location = PM_NODE_END_LINE_COLUMN(scope_node->parser, node);
return (rb_code_location_t) {
.beg_pos = { .lineno = start_location.line, .column = start_location.column },
.end_pos = { .lineno = end_location.line, .column = end_location.column }
};
}
/**
* A macro for determining if we should go through the work of adding branch
* coverage to the current iseq. We check this manually each time because we
* want to avoid the overhead of creating rb_code_location_t objects.
*/
#define PM_BRANCH_COVERAGE_P(iseq) (ISEQ_COVERAGE(iseq) && ISEQ_BRANCH_COVERAGE(iseq))
static void
pm_compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const pm_node_t *cond,
LABEL *then_label, LABEL *else_label, bool popped, pm_scope_node_t *scope_node);
static void
pm_compile_logical(rb_iseq_t *iseq, LINK_ANCHOR *const ret, pm_node_t *cond, LABEL *then_label, LABEL *else_label, bool popped, pm_scope_node_t *scope_node)
{
const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, cond);
DECL_ANCHOR(seq);
LABEL *label = NEW_LABEL(location.line);
if (!then_label) then_label = label;
else if (!else_label) else_label = label;
pm_compile_branch_condition(iseq, seq, cond, then_label, else_label, popped, scope_node);
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;
}
if (!label->refcnt) {
if (popped) PUSH_INSN(ret, location, putnil);
}
else {
PUSH_LABEL(seq, label);
}
PUSH_SEQ(ret, seq);
return;
}
static void
pm_compile_flip_flop_bound(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
{
const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
if (PM_NODE_TYPE_P(node, PM_INTEGER_NODE)) {
PM_COMPILE_NOT_POPPED(node);
VALUE operand = ID2SYM(rb_intern("$."));
PUSH_INSN1(ret, location, getglobal, operand);
PUSH_SEND(ret, location, idEq, INT2FIX(1));
if (popped) PUSH_INSN(ret, location, pop);
}
else {
PM_COMPILE(node);
}
}
static void
pm_compile_flip_flop(const pm_flip_flop_node_t *flip_flop_node, LABEL *else_label, LABEL *then_label, rb_iseq_t *iseq, const int lineno, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
{
const pm_node_location_t location = { .line = lineno, .node_id = -1 };
LABEL *lend = NEW_LABEL(location.line);
int again = !(flip_flop_node->base.flags & PM_RANGE_FLAGS_EXCLUDE_END);
rb_num_t count = ISEQ_FLIP_CNT_INCREMENT(ISEQ_BODY(iseq)->local_iseq) + VM_SVAR_FLIPFLOP_START;
VALUE key = INT2FIX(count);
PUSH_INSN2(ret, location, getspecial, key, INT2FIX(0));
PUSH_INSNL(ret, location, branchif, lend);
if (flip_flop_node->left) {
pm_compile_flip_flop_bound(iseq, flip_flop_node->left, ret, popped, scope_node);
}
else {
PUSH_INSN(ret, location, putnil);
}
PUSH_INSNL(ret, location, branchunless, else_label);
PUSH_INSN1(ret, location, putobject, Qtrue);
PUSH_INSN1(ret, location, setspecial, key);
if (!again) {
PUSH_INSNL(ret, location, jump, then_label);
}
PUSH_LABEL(ret, lend);
if (flip_flop_node->right) {
pm_compile_flip_flop_bound(iseq, flip_flop_node->right, ret, popped, scope_node);
}
else {
PUSH_INSN(ret, location, putnil);
}
PUSH_INSNL(ret, location, branchunless, then_label);
PUSH_INSN1(ret, location, putobject, Qfalse);
PUSH_INSN1(ret, location, setspecial, key);
PUSH_INSNL(ret, location, jump, then_label);
}
static void pm_compile_defined_expr(rb_iseq_t *iseq, const pm_node_t *node, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node, bool in_condition);
static void
pm_compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const pm_node_t *cond, LABEL *then_label, LABEL *else_label, bool popped, pm_scope_node_t *scope_node)
{
const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, cond);
again:
switch (PM_NODE_TYPE(cond)) {
case PM_AND_NODE: {
const pm_and_node_t *cast = (const pm_and_node_t *) cond;
pm_compile_logical(iseq, ret, cast->left, NULL, else_label, popped, scope_node);
cond = cast->right;
goto again;
}
case PM_OR_NODE: {
const pm_or_node_t *cast = (const pm_or_node_t *) cond;
pm_compile_logical(iseq, ret, cast->left, then_label, NULL, popped, scope_node);
cond = cast->right;
goto again;
}
case PM_FALSE_NODE:
case PM_NIL_NODE:
PUSH_INSNL(ret, location, jump, else_label);
return;
case PM_FLOAT_NODE:
case PM_IMAGINARY_NODE:
case PM_INTEGER_NODE:
case PM_LAMBDA_NODE:
case PM_RATIONAL_NODE:
case PM_REGULAR_EXPRESSION_NODE:
case PM_STRING_NODE:
case PM_SYMBOL_NODE:
case PM_TRUE_NODE:
PUSH_INSNL(ret, location, jump, then_label);
return;
case PM_FLIP_FLOP_NODE:
pm_compile_flip_flop((const pm_flip_flop_node_t *) cond, else_label, then_label, iseq, location.line, ret, popped, scope_node);
return;
case PM_DEFINED_NODE: {
const pm_defined_node_t *cast = (const pm_defined_node_t *) cond;
pm_compile_defined_expr(iseq, cast->value, &location, ret, popped, scope_node, true);
break;
}
default: {
DECL_ANCHOR(cond_seq);
pm_compile_node(iseq, cond, cond_seq, false, scope_node);
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])) {
PUSH_INSNL(ret, location, jump, then_label);
// maybe unreachable
return;
}
else {
PUSH_INSNL(ret, location, jump, else_label);
return;
}
}
}
PUSH_SEQ(ret, cond_seq);
break;
}
}
PUSH_INSNL(ret, location, branchunless, else_label);
PUSH_INSNL(ret, location, jump, then_label);
}
/**
* Compile an if or unless node.
*/
static void
pm_compile_conditional(rb_iseq_t *iseq, const pm_node_location_t *node_location, pm_node_type_t type, const pm_node_t *node, const pm_statements_node_t *statements, const pm_node_t *subsequent, const pm_node_t *predicate, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
{
const pm_node_location_t location = *node_location;
LABEL *then_label = NEW_LABEL(location.line);
LABEL *else_label = NEW_LABEL(location.line);
LABEL *end_label = NULL;
DECL_ANCHOR(cond_seq);
pm_compile_branch_condition(iseq, cond_seq, predicate, then_label, else_label, false, scope_node);
PUSH_SEQ(ret, cond_seq);
rb_code_location_t conditional_location = { 0 };
VALUE branches = Qfalse;
if (then_label->refcnt && else_label->refcnt && PM_BRANCH_COVERAGE_P(iseq)) {
conditional_location = pm_code_location(scope_node, node);
branches = decl_branch_base(iseq, PTR2NUM(node), &conditional_location, type == PM_IF_NODE ? "if" : "unless");
}
if (then_label->refcnt) {
PUSH_LABEL(ret, then_label);
DECL_ANCHOR(then_seq);
if (statements != NULL) {
pm_compile_node(iseq, (const pm_node_t *) statements, then_seq, popped, scope_node);
}
else if (!popped) {
PUSH_SYNTHETIC_PUTNIL(then_seq, iseq);
}
if (else_label->refcnt) {
// Establish branch coverage for the then block.
if (PM_BRANCH_COVERAGE_P(iseq)) {
rb_code_location_t branch_location;
if (statements != NULL) {
branch_location = pm_code_location(scope_node, (const pm_node_t *) statements);
} else if (type == PM_IF_NODE) {
pm_line_column_t predicate_end = PM_NODE_END_LINE_COLUMN(scope_node->parser, predicate);
branch_location = (rb_code_location_t) {
.beg_pos = { .lineno = predicate_end.line, .column = predicate_end.column },
.end_pos = { .lineno = predicate_end.line, .column = predicate_end.column }
};
} else {
branch_location = conditional_location;
}
add_trace_branch_coverage(iseq, ret, &branch_location, branch_location.beg_pos.column, 0, type == PM_IF_NODE ? "then" : "else", branches);
}
end_label = NEW_LABEL(location.line);
PUSH_INSNL(then_seq, location, jump, end_label);
if (!popped) PUSH_INSN(then_seq, location, pop);
}
PUSH_SEQ(ret, then_seq);
}
if (else_label->refcnt) {
PUSH_LABEL(ret, else_label);
DECL_ANCHOR(else_seq);
if (subsequent != NULL) {
pm_compile_node(iseq, subsequent, else_seq, popped, scope_node);
}
else if (!popped) {
PUSH_SYNTHETIC_PUTNIL(else_seq, iseq);
}
// Establish branch coverage for the else block.
if (then_label->refcnt && PM_BRANCH_COVERAGE_P(iseq)) {
rb_code_location_t branch_location;
if (subsequent == NULL) {
branch_location = conditional_location;
} else if (PM_NODE_TYPE_P(subsequent, PM_ELSE_NODE)) {
const pm_else_node_t *else_node = (const pm_else_node_t *) subsequent;
branch_location = pm_code_location(scope_node, else_node->statements != NULL ? ((const pm_node_t *) else_node->statements) : (const pm_node_t *) else_node);
} else {
branch_location = pm_code_location(scope_node, (const pm_node_t *) subsequent);
}
add_trace_branch_coverage(iseq, ret, &branch_location, branch_location.beg_pos.column, 1, type == PM_IF_NODE ? "else" : "then", branches);
}
PUSH_SEQ(ret, else_seq);
}
if (end_label) {
PUSH_LABEL(ret, end_label);
}
return;
}
/**
* Compile a while or until loop.
*/
static void
pm_compile_loop(rb_iseq_t *iseq, const pm_node_location_t *node_location, pm_node_flags_t flags, enum pm_node_type type, const pm_node_t *node, const pm_statements_node_t *statements, const pm_node_t *predicate, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
{
const pm_node_location_t location = *node_location;
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;
LABEL *next_label = ISEQ_COMPILE_DATA(iseq)->start_label = NEW_LABEL(location.line); /* next */
LABEL *redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label = NEW_LABEL(location.line); /* redo */
LABEL *break_label = ISEQ_COMPILE_DATA(iseq)->end_label = NEW_LABEL(location.line); /* break */
LABEL *end_label = NEW_LABEL(location.line);
LABEL *adjust_label = NEW_LABEL(location.line);
LABEL *next_catch_label = NEW_LABEL(location.line);
LABEL *tmp_label = NULL;
// We're pushing onto the ensure stack because breaks need to break out of
// this loop and not break into the ensure statements within the same
// lexical scope.
struct iseq_compile_data_ensure_node_stack enl;
push_ensure_entry(iseq, &enl, NULL, NULL);
// begin; end while true
if (flags & PM_LOOP_FLAGS_BEGIN_MODIFIER) {
tmp_label = NEW_LABEL(location.line);
PUSH_INSNL(ret, location, jump, tmp_label);
}
else {
// while true; end
PUSH_INSNL(ret, location, jump, next_label);
}
PUSH_LABEL(ret, adjust_label);
PUSH_INSN(ret, location, putnil);
PUSH_LABEL(ret, next_catch_label);
PUSH_INSN(ret, location, pop);
PUSH_INSNL(ret, location, jump, next_label);
if (tmp_label) PUSH_LABEL(ret, tmp_label);
PUSH_LABEL(ret, redo_label);
// Establish branch coverage for the loop.
if (PM_BRANCH_COVERAGE_P(iseq)) {
rb_code_location_t loop_location = pm_code_location(scope_node, node);
VALUE branches = decl_branch_base(iseq, PTR2NUM(node), &loop_location, type == PM_WHILE_NODE ? "while" : "until");
rb_code_location_t branch_location = statements != NULL ? pm_code_location(scope_node, (const pm_node_t *) statements) : loop_location;
add_trace_branch_coverage(iseq, ret, &branch_location, branch_location.beg_pos.column, 0, "body", branches);
}
if (statements != NULL) PM_COMPILE_POPPED((const pm_node_t *) statements);
PUSH_LABEL(ret, next_label);
if (type == PM_WHILE_NODE) {
pm_compile_branch_condition(iseq, ret, predicate, redo_label, end_label, popped, scope_node);
}
else if (type == PM_UNTIL_NODE) {
pm_compile_branch_condition(iseq, ret, predicate, end_label, redo_label, popped, scope_node);
}
PUSH_LABEL(ret, end_label);
PUSH_ADJUST_RESTORE(ret, adjust_label);
PUSH_INSN(ret, location, putnil);
PUSH_LABEL(ret, break_label);
if (popped) PUSH_INSN(ret, location, pop);
PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, redo_label, break_label, NULL, break_label);
PUSH_CATCH_ENTRY(CATCH_TYPE_NEXT, redo_label, break_label, NULL, next_catch_label);
PUSH_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)->ensure_node_stack = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack->prev;
return;
}
// This recurses through scopes and finds the local index at any scope level
// It also takes a pointer to depth, and increments depth appropriately
// according to the depth of the local.
static pm_local_index_t
pm_lookup_local_index(rb_iseq_t *iseq, const pm_scope_node_t *scope_node, pm_constant_id_t constant_id, int start_depth)
{
pm_local_index_t lindex = { 0 };
st_data_t local_index;
int level;
for (level = 0; level < start_depth; level++) {
scope_node = scope_node->previous;
}
while (!st_lookup(scope_node->index_lookup_table, constant_id, &local_index)) {
level++;
if (scope_node->previous) {
scope_node = scope_node->previous;
}
else {
// We have recursed up all scope nodes
// and have not found the local yet
rb_bug("Local with constant_id %u does not exist", (unsigned int) constant_id);
}
}
lindex.level = level;
lindex.index = scope_node->local_table_for_iseq_size - (int) local_index;
return lindex;
}
// This returns the CRuby ID which maps to the pm_constant_id_t
//
// Constant_ids in prism are indexes of the constants in prism's constant pool.
// We add a constants mapping on the scope_node which is a mapping from
// these constant_id indexes to the CRuby IDs that they represent.
// This helper method allows easy access to those IDs
static ID
pm_constant_id_lookup(const pm_scope_node_t *scope_node, pm_constant_id_t constant_id)
{
if (constant_id < 1 || constant_id > scope_node->parser->constant_pool.size) {
rb_bug("constant_id out of range: %u", (unsigned int)constant_id);
}
return scope_node->constants[constant_id - 1];
}
static rb_iseq_t *
pm_new_child_iseq(rb_iseq_t *iseq, pm_scope_node_t *node, VALUE name, const rb_iseq_t *parent, enum rb_iseq_type type, int line_no)
{
debugs("[new_child_iseq]> ---------------------------------------\n");
int isolated_depth = ISEQ_COMPILE_DATA(iseq)->isolated_depth;
int error_state;
rb_iseq_t *ret_iseq = pm_iseq_new_with_opt(node, name,
rb_iseq_path(iseq), rb_iseq_realpath(iseq),
line_no, parent,
isolated_depth ? isolated_depth + 1 : 0,
type, ISEQ_COMPILE_DATA(iseq)->option, &error_state);
if (error_state) {
pm_scope_node_destroy(node);
RUBY_ASSERT(ret_iseq == NULL);
rb_jump_tag(error_state);
}
debugs("[new_child_iseq]< ---------------------------------------\n");
return ret_iseq;
}
static int
pm_compile_class_path(rb_iseq_t *iseq, const pm_node_t *node, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
{
if (PM_NODE_TYPE_P(node, PM_CONSTANT_PATH_NODE)) {
const pm_node_t *parent = ((const pm_constant_path_node_t *) node)->parent;
if (parent) {
/* Bar::Foo */
PM_COMPILE(parent);
return VM_DEFINECLASS_FLAG_SCOPED;
}
else {
/* toplevel class ::Foo */
PUSH_INSN1(ret, *node_location, putobject, rb_cObject);
return VM_DEFINECLASS_FLAG_SCOPED;
}
}
else {
/* class at cbase Foo */
PUSH_INSN1(ret, *node_location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
return 0;
}
}
/**
* Compile either a call and write node or a call or write node. These look like
* method calls that are followed by a ||= or &&= operator.
*/
static void
pm_compile_call_and_or_write_node(rb_iseq_t *iseq, bool and_node, const pm_node_t *receiver, const pm_node_t *value, pm_constant_id_t write_name, pm_constant_id_t read_name, bool safe_nav, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
{
const pm_node_location_t location = *node_location;
LABEL *lfin = NEW_LABEL(location.line);
LABEL *lcfin = NEW_LABEL(location.line);
LABEL *lskip = NULL;
int flag = PM_NODE_TYPE_P(receiver, PM_SELF_NODE) ? VM_CALL_FCALL : 0;
ID id_read_name = pm_constant_id_lookup(scope_node, read_name);
PM_COMPILE_NOT_POPPED(receiver);
if (safe_nav) {
lskip = NEW_LABEL(location.line);
PUSH_INSN(ret, location, dup);
PUSH_INSNL(ret, location, branchnil, lskip);
}
PUSH_INSN(ret, location, dup);
PUSH_SEND_WITH_FLAG(ret, location, id_read_name, INT2FIX(0), INT2FIX(flag));
if (!popped) PUSH_INSN(ret, location, dup);
if (and_node) {
PUSH_INSNL(ret, location, branchunless, lcfin);
}
else {
PUSH_INSNL(ret, location, branchif, lcfin);
}
if (!popped) PUSH_INSN(ret, location, pop);
PM_COMPILE_NOT_POPPED(value);
if (!popped) {
PUSH_INSN(ret, location, swap);
PUSH_INSN1(ret, location, topn, INT2FIX(1));
}
ID id_write_name = pm_constant_id_lookup(scope_node, write_name);
PUSH_SEND_WITH_FLAG(ret, location, id_write_name, INT2FIX(1), INT2FIX(flag));
PUSH_INSNL(ret, location, jump, lfin);
PUSH_LABEL(ret, lcfin);
if (!popped) PUSH_INSN(ret, location, swap);
PUSH_LABEL(ret, lfin);
if (lskip && popped) PUSH_LABEL(ret, lskip);
PUSH_INSN(ret, location, pop);
if (lskip && !popped) PUSH_LABEL(ret, lskip);
}
static void pm_compile_shareable_constant_value(rb_iseq_t *iseq, const pm_node_t *node, const pm_node_flags_t shareability, VALUE path, LINK_ANCHOR *const ret, pm_scope_node_t *scope_node, bool top);
/**
* This function compiles a hash onto the stack. It is used to compile hash
* literals and keyword arguments. It is assumed that if we get here that the
* contents of the hash are not popped.
*/
static void
pm_compile_hash_elements(rb_iseq_t *iseq, const pm_node_t *node, const pm_node_list_t *elements, const pm_node_flags_t shareability, VALUE path, bool argument, LINK_ANCHOR *const ret, pm_scope_node_t *scope_node)
{
const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
// If this element is not popped, then we need to create the hash on the
// stack. Neighboring plain assoc nodes should be grouped together (either
// by newhash or hash merge). Double splat nodes should be merged using the
// merge_kwd method call.
const int max_stack_length = 0x100;
const unsigned int min_tmp_hash_length = 0x800;
int stack_length = 0;
bool first_chunk = true;
// This is an optimization wherein we keep track of whether or not the
// previous element was a static literal. If it was, then we do not attempt
// to check if we have a subhash that can be optimized. If it was not, then
// we do check.
bool static_literal = false;
DECL_ANCHOR(anchor);
// Convert pushed elements to a hash, and merge if needed.
#define FLUSH_CHUNK \
if (stack_length) { \
if (first_chunk) { \
PUSH_SEQ(ret, anchor); \
PUSH_INSN1(ret, location, newhash, INT2FIX(stack_length)); \
first_chunk = false; \
} \
else { \
PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE)); \
PUSH_INSN(ret, location, swap); \
PUSH_SEQ(ret, anchor); \
PUSH_SEND(ret, location, id_core_hash_merge_ptr, INT2FIX(stack_length + 1)); \
} \
INIT_ANCHOR(anchor); \
stack_length = 0; \
}
for (size_t index = 0; index < elements->size; index++) {
const pm_node_t *element = elements->nodes[index];
switch (PM_NODE_TYPE(element)) {
case PM_ASSOC_NODE: {
// Pre-allocation check (this branch can be omitted).
if (
(shareability == 0) &&
PM_NODE_FLAG_P(element, PM_NODE_FLAG_STATIC_LITERAL) && (
(!static_literal && ((index + min_tmp_hash_length) < elements->size)) ||
(first_chunk && stack_length == 0)
)
) {
// Count the elements that are statically-known.
size_t count = 1;
while (index + count < elements->size && PM_NODE_FLAG_P(elements->nodes[index + count], PM_NODE_FLAG_STATIC_LITERAL)) count++;
if ((first_chunk && stack_length == 0) || count >= min_tmp_hash_length) {
// The subsequence of elements in this hash is long enough
// to merit its own hash.
VALUE ary = rb_ary_hidden_new(count);
// Create a hidden hash.
for (size_t tmp_end = index + count; index < tmp_end; index++) {
const pm_assoc_node_t *assoc = (const pm_assoc_node_t *) elements->nodes[index];
VALUE elem[2] = {
pm_static_literal_value(iseq, assoc->key, scope_node),
pm_static_literal_value(iseq, assoc->value, scope_node)
};
rb_ary_cat(ary, elem, 2);
}
index --;
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) {
PUSH_INSN1(ret, location, duphash, hash);
first_chunk = false;
}
else {
PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
PUSH_INSN(ret, location, swap);
PUSH_INSN1(ret, location, putobject, hash);
PUSH_SEND(ret, location, id_core_hash_merge_kwd, INT2FIX(2));
}
break;
}
else {
static_literal = true;
}
}
else {
static_literal = false;
}
// If this is a plain assoc node, then we can compile it directly
// and then add the total number of values on the stack.
if (shareability == 0) {
pm_compile_node(iseq, element, anchor, false, scope_node);
}
else {
const pm_assoc_node_t *assoc = (const pm_assoc_node_t *) element;
pm_compile_shareable_constant_value(iseq, assoc->key, shareability, path, ret, scope_node, false);
pm_compile_shareable_constant_value(iseq, assoc->value, shareability, path, ret, scope_node, false);
}
if ((stack_length += 2) >= max_stack_length) FLUSH_CHUNK;
break;
}
case PM_ASSOC_SPLAT_NODE: {
FLUSH_CHUNK;
const pm_assoc_splat_node_t *assoc_splat = (const pm_assoc_splat_node_t *) element;
bool empty_hash = assoc_splat->value != NULL && (
(PM_NODE_TYPE_P(assoc_splat->value, PM_HASH_NODE) && ((const pm_hash_node_t *) assoc_splat->value)->elements.size == 0) ||
PM_NODE_TYPE_P(assoc_splat->value, PM_NIL_NODE)
);
bool first_element = first_chunk && stack_length == 0;
bool last_element = index == elements->size - 1;
bool only_element = first_element && last_element;
if (empty_hash) {
if (only_element && argument) {
// **{} appears at the only keyword argument in method call,
// so it won't be modified.
//
// This is only done for method calls and not for literal
// hashes, because literal hashes should always result in a
// new hash.
PUSH_INSN(ret, location, putnil);
}
else if (first_element) {
// **{} appears as the first keyword argument, so it may be
// modified. We need to create a fresh hash object.
PUSH_INSN1(ret, location, 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_element && argument) {
// ** is only keyword argument in the method call. Use it
// 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.
if (shareability == 0) {
PM_COMPILE_NOT_POPPED(element);
}
else {
pm_compile_shareable_constant_value(iseq, element, shareability, path, ret, scope_node, false);
}
}
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).
PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
if (first_element) {
PUSH_INSN1(ret, location, newhash, INT2FIX(0));
}
else {
PUSH_INSN(ret, location, swap);
}
if (shareability == 0) {
PM_COMPILE_NOT_POPPED(element);
}
else {
pm_compile_shareable_constant_value(iseq, element, shareability, path, ret, scope_node, false);
}
PUSH_SEND(ret, location, id_core_hash_merge_kwd, INT2FIX(2));
}
}
first_chunk = false;
static_literal = false;
break;
}
default:
RUBY_ASSERT("Invalid node type for hash" && false);
break;
}
}
FLUSH_CHUNK;
#undef FLUSH_CHUNK
}
#define SPLATARRAY_FALSE 0
#define SPLATARRAY_TRUE 1
#define DUP_SINGLE_KW_SPLAT 2
// This is details. Users should call pm_setup_args() instead.
static int
pm_setup_args_core(const pm_arguments_node_t *arguments_node, const pm_node_t *block, int *flags, const bool has_regular_blockarg, struct rb_callinfo_kwarg **kw_arg, int *dup_rest, rb_iseq_t *iseq, LINK_ANCHOR *const ret, pm_scope_node_t *scope_node, const pm_node_location_t *node_location)
{
const pm_node_location_t location = *node_location;
int orig_argc = 0;
bool has_splat = false;
bool has_keyword_splat = false;
if (arguments_node == NULL) {
if (*flags & VM_CALL_FCALL) {
*flags |= VM_CALL_VCALL;
}
}
else {
const pm_node_list_t *arguments = &arguments_node->arguments;
has_keyword_splat = PM_NODE_FLAG_P(arguments_node, PM_ARGUMENTS_NODE_FLAGS_CONTAINS_KEYWORD_SPLAT);
// We count the number of elements post the splat node that are not keyword elements to
// eventually pass as an argument to newarray
int post_splat_counter = 0;
const pm_node_t *argument;
PM_NODE_LIST_FOREACH(arguments, index, argument) {
switch (PM_NODE_TYPE(argument)) {
// A keyword hash node contains all keyword arguments as AssocNodes and AssocSplatNodes
case PM_KEYWORD_HASH_NODE: {
const pm_keyword_hash_node_t *keyword_arg = (const pm_keyword_hash_node_t *) argument;
const pm_node_list_t *elements = &keyword_arg->elements;
if (has_keyword_splat || has_splat) {
*flags |= VM_CALL_KW_SPLAT;
has_keyword_splat = true;
if (elements->size > 1 || !(elements->size == 1 && PM_NODE_TYPE_P(elements->nodes[0], PM_ASSOC_SPLAT_NODE))) {
// A new hash will be created for the keyword arguments
// in this case, so mark the method as passing mutable
// keyword splat.
*flags |= VM_CALL_KW_SPLAT_MUT;
pm_compile_hash_elements(iseq, argument, elements, 0, Qundef, true, ret, scope_node);
}
else if (*dup_rest & DUP_SINGLE_KW_SPLAT) {
*flags |= VM_CALL_KW_SPLAT_MUT;
PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
PUSH_INSN1(ret, location, newhash, INT2FIX(0));
pm_compile_hash_elements(iseq, argument, elements, 0, Qundef, true, ret, scope_node);
PUSH_SEND(ret, location, id_core_hash_merge_kwd, INT2FIX(2));
}
else {
pm_compile_hash_elements(iseq, argument, elements, 0, Qundef, true, ret, scope_node);
}
}
else {
// We need to first figure out if all elements of the
// KeywordHashNode are AssocNodes with symbol keys.
if (PM_NODE_FLAG_P(keyword_arg, PM_KEYWORD_HASH_NODE_FLAGS_SYMBOL_KEYS)) {
// If they are all symbol keys then we can pass them as
// keyword arguments. The first thing we need to do is
// deduplicate. We'll do this using the combination of a
// Ruby hash and a Ruby array.
VALUE stored_indices = rb_hash_new();
VALUE keyword_indices = rb_ary_new_capa(elements->size);
size_t size = 0;
for (size_t element_index = 0; element_index < elements->size; element_index++) {
const pm_assoc_node_t *assoc = (const pm_assoc_node_t *) elements->nodes[element_index];
// Retrieve the stored index from the hash for this
// keyword.
VALUE keyword = pm_static_literal_value(iseq, assoc->key, scope_node);
VALUE stored_index = rb_hash_aref(stored_indices, keyword);
// If this keyword was already seen in the hash,
// then mark the array at that index as false and
// decrement the keyword size.
if (!NIL_P(stored_index)) {
rb_ary_store(keyword_indices, NUM2LONG(stored_index), Qfalse);
size--;
}
// Store (and possibly overwrite) the index for this
// keyword in the hash, mark the array at that index
// as true, and increment the keyword size.
rb_hash_aset(stored_indices, keyword, ULONG2NUM(element_index));
rb_ary_store(keyword_indices, (long) element_index, Qtrue);
size++;
}
*kw_arg = rb_xmalloc_mul_add(size, sizeof(VALUE), sizeof(struct rb_callinfo_kwarg));
*flags |= VM_CALL_KWARG;
VALUE *keywords = (*kw_arg)->keywords;
(*kw_arg)->references = 0;
(*kw_arg)->keyword_len = (int) size;
size_t keyword_index = 0;
for (size_t element_index = 0; element_index < elements->size; element_index++) {
const pm_assoc_node_t *assoc = (const pm_assoc_node_t *) elements->nodes[element_index];
bool popped = true;
if (rb_ary_entry(keyword_indices, (long) element_index) == Qtrue) {
keywords[keyword_index++] = pm_static_literal_value(iseq, assoc->key, scope_node);
popped = false;
}
PM_COMPILE(assoc->value);
}
RUBY_ASSERT(keyword_index == size);
}
else {
// If they aren't all symbol keys then we need to
// construct a new hash and pass that as an argument.
orig_argc++;
*flags |= VM_CALL_KW_SPLAT;
size_t size = elements->size;
if (size > 1) {
// A new hash will be created for the keyword
// arguments in this case, so mark the method as
// passing mutable keyword splat.
*flags |= VM_CALL_KW_SPLAT_MUT;
}
for (size_t element_index = 0; element_index < size; element_index++) {
const pm_assoc_node_t *assoc = (const pm_assoc_node_t *) elements->nodes[element_index];
PM_COMPILE_NOT_POPPED(assoc->key);
PM_COMPILE_NOT_POPPED(assoc->value);
}
PUSH_INSN1(ret, location, newhash, INT2FIX(size * 2));
}
}
break;
}
case PM_SPLAT_NODE: {
*flags |= VM_CALL_ARGS_SPLAT;
const pm_splat_node_t *splat_node = (const pm_splat_node_t *) argument;
if (splat_node->expression) {
PM_COMPILE_NOT_POPPED(splat_node->expression);
}
else {
pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_MULT, 0);
PUSH_GETLOCAL(ret, location, index.index, index.level);
}
bool first_splat = !has_splat;
if (first_splat) {
// If this is the first splat array seen and it's not the
// last parameter, we want splatarray to dup it.
//
// foo(a, *b, c)
// ^^
if (index + 1 < arguments->size || has_regular_blockarg) {
PUSH_INSN1(ret, location, splatarray, (*dup_rest & SPLATARRAY_TRUE) ? Qtrue : Qfalse);
if (*dup_rest & SPLATARRAY_TRUE) *dup_rest &= ~SPLATARRAY_TRUE;
}
// If this is the first spalt array seen and it's the last
// parameter, we don't want splatarray to dup it.
//
// foo(a, *b)
// ^^
else {
PUSH_INSN1(ret, location, splatarray, Qfalse);
}
}
else {
// If this is not the first splat array seen and it is also
// the last parameter, we don't want splatarray to dup it
// and we need to concat the array.
//
// foo(a, *b, *c)
// ^^
PUSH_INSN(ret, location, concattoarray);
}
has_splat = true;
post_splat_counter = 0;
break;
}
case PM_FORWARDING_ARGUMENTS_NODE: { // not counted in argc return value
iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
if (ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->param.flags.forwardable) {
*flags |= VM_CALL_FORWARDING;
pm_local_index_t mult_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_DOT3, 0);
PUSH_GETLOCAL(ret, location, mult_local.index, mult_local.level);
break;
}
if (has_splat) {
// If we already have a splat, we're concatenating to existing array
orig_argc += 1;
} else {
orig_argc += 2;
}
*flags |= VM_CALL_ARGS_SPLAT | VM_CALL_ARGS_BLOCKARG | VM_CALL_KW_SPLAT;
// Forwarding arguments nodes are treated as foo(*, **, &)
// So foo(...) equals foo(*, **, &) and as such the local
// table for this method is known in advance
//
// Push the *
pm_local_index_t mult_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_MULT, 0);
PUSH_GETLOCAL(ret, location, mult_local.index, mult_local.level);
if (has_splat) {
// If we already have a splat, we need to concatenate arrays
PUSH_INSN(ret, location, concattoarray);
} else {
PUSH_INSN1(ret, location, splatarray, Qfalse);
}
// Push the **
pm_local_index_t pow_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_POW, 0);
PUSH_GETLOCAL(ret, location, pow_local.index, pow_local.level);
// Push the &
pm_local_index_t and_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_AND, 0);
PUSH_INSN2(ret, location, getblockparamproxy, INT2FIX(and_local.index + VM_ENV_DATA_SIZE - 1), INT2FIX(and_local.level));
break;
}
default: {
post_splat_counter++;
PM_COMPILE_NOT_POPPED(argument);
// If we have a splat and we've seen a splat, we need to process
// everything after the splat.
if (has_splat) {
// Stack items are turned into an array and concatenated in
// the following cases:
//
// If the next node is a splat:
//
// foo(*a, b, *c)
//
// If the next node is a kwarg or kwarg splat:
//
// foo(*a, b, c: :d)
// foo(*a, b, **c)
//
// If the next node is a forwarding argument:
//
// foo(*a, b, ...)
//
// If the next node is NULL (we have hit the end):
//
// foo(*a, b)
if (index == arguments->size - 1) {
RUBY_ASSERT(post_splat_counter > 0);
PUSH_INSN1(ret, location, pushtoarray, INT2FIX(post_splat_counter));
}
else {
pm_node_t *next_arg = arguments->nodes[index + 1];
switch (PM_NODE_TYPE(next_arg)) {
// A keyword hash node contains all keyword arguments as AssocNodes and AssocSplatNodes
case PM_KEYWORD_HASH_NODE: {
PUSH_INSN1(ret, location, newarray, INT2FIX(post_splat_counter));
PUSH_INSN(ret, location, concatarray);
break;
}
case PM_SPLAT_NODE: {
PUSH_INSN1(ret, location, newarray, INT2FIX(post_splat_counter));
PUSH_INSN(ret, location, concatarray);
break;
}
case PM_FORWARDING_ARGUMENTS_NODE: {
PUSH_INSN1(ret, location, pushtoarray, INT2FIX(post_splat_counter));
break;
}
default:
break;
}
}
}
else {
orig_argc++;
}
}
}
}
}
if (has_splat) orig_argc++;
if (has_keyword_splat) orig_argc++;
return orig_argc;
}
/**
* True if the given kind of node could potentially mutate the array that is
* being splatted in a set of call arguments.
*/
static inline bool
pm_setup_args_dup_rest_p(const pm_node_t *node)
{
switch (PM_NODE_TYPE(node)) {
case PM_BACK_REFERENCE_READ_NODE:
case PM_CLASS_VARIABLE_READ_NODE:
case PM_CONSTANT_READ_NODE:
case PM_FALSE_NODE:
case PM_FLOAT_NODE:
case PM_GLOBAL_VARIABLE_READ_NODE:
case PM_IMAGINARY_NODE:
case PM_INSTANCE_VARIABLE_READ_NODE:
case PM_INTEGER_NODE:
case PM_LAMBDA_NODE:
case PM_LOCAL_VARIABLE_READ_NODE:
case PM_NIL_NODE:
case PM_NUMBERED_REFERENCE_READ_NODE:
case PM_RATIONAL_NODE:
case PM_REGULAR_EXPRESSION_NODE:
case PM_SELF_NODE:
case PM_STRING_NODE:
case PM_SYMBOL_NODE:
case PM_TRUE_NODE:
return false;
case PM_CONSTANT_PATH_NODE: {
const pm_constant_path_node_t *cast = (const pm_constant_path_node_t *) node;
if (cast->parent != NULL) {
return pm_setup_args_dup_rest_p(cast->parent);
}
return false;
}
case PM_IMPLICIT_NODE:
return pm_setup_args_dup_rest_p(((const pm_implicit_node_t *) node)->value);
default:
return true;
}
}
/**
* Compile the argument parts of a call.
*/
static int
pm_setup_args(const pm_arguments_node_t *arguments_node, const pm_node_t *block, int *flags, struct rb_callinfo_kwarg **kw_arg, rb_iseq_t *iseq, LINK_ANCHOR *const ret, pm_scope_node_t *scope_node, const pm_node_location_t *node_location)
{
int dup_rest = SPLATARRAY_TRUE;
const pm_node_list_t *arguments;
size_t arguments_size;
// Calls like foo(1, *f, **hash) that use splat and kwsplat could be
// eligible for eliding duping the rest array (dup_reset=false).
if (
arguments_node != NULL &&
(arguments = &arguments_node->arguments, arguments_size = arguments->size) >= 2 &&
PM_NODE_FLAG_P(arguments_node, PM_ARGUMENTS_NODE_FLAGS_CONTAINS_SPLAT) &&
!PM_NODE_FLAG_P(arguments_node, PM_ARGUMENTS_NODE_FLAGS_CONTAINS_MULTIPLE_SPLATS) &&
PM_NODE_TYPE_P(arguments->nodes[arguments_size - 1], PM_KEYWORD_HASH_NODE)
) {
// Start by assuming that dup_rest=false, then check each element of the
// hash to ensure we don't need to flip it back to true (in case one of
// the elements could potentially mutate the array).
dup_rest = SPLATARRAY_FALSE;
const pm_keyword_hash_node_t *keyword_hash = (const pm_keyword_hash_node_t *) arguments->nodes[arguments_size - 1];
const pm_node_list_t *elements = &keyword_hash->elements;
for (size_t index = 0; dup_rest == SPLATARRAY_FALSE && index < elements->size; index++) {
const pm_node_t *element = elements->nodes[index];
switch (PM_NODE_TYPE(element)) {
case PM_ASSOC_NODE: {
const pm_assoc_node_t *assoc = (const pm_assoc_node_t *) element;
if (pm_setup_args_dup_rest_p(assoc->key) || pm_setup_args_dup_rest_p(assoc->value)) dup_rest = SPLATARRAY_TRUE;
break;
}
case PM_ASSOC_SPLAT_NODE: {
const pm_assoc_splat_node_t *assoc = (const pm_assoc_splat_node_t *) element;
if (assoc->value != NULL && pm_setup_args_dup_rest_p(assoc->value)) dup_rest = SPLATARRAY_TRUE;
break;
}
default:
break;
}
}
}
int initial_dup_rest = dup_rest;
int argc;
if (block && PM_NODE_TYPE_P(block, PM_BLOCK_ARGUMENT_NODE)) {
// We compile the `&block_arg` expression first and stitch it later
// since the nature of the expression influences whether splat should
// duplicate the array.
bool regular_block_arg = true;
const pm_node_t *block_expr = ((const pm_block_argument_node_t *)block)->expression;
if (block_expr && pm_setup_args_dup_rest_p(block_expr)) {
dup_rest = SPLATARRAY_TRUE | DUP_SINGLE_KW_SPLAT;
initial_dup_rest = dup_rest;
}
DECL_ANCHOR(block_arg);
pm_compile_node(iseq, block, block_arg, false, scope_node);
*flags |= VM_CALL_ARGS_BLOCKARG;
if (LIST_INSN_SIZE_ONE(block_arg)) {
LINK_ELEMENT *elem = FIRST_ELEMENT(block_arg);
if (IS_INSN(elem)) {
INSN *iobj = (INSN *) elem;
if (iobj->insn_id == BIN(getblockparam)) {
iobj->insn_id = BIN(getblockparamproxy);
}
// Allow splat without duplication for simple one-instruction
// block arguments like `&arg`. It is known that this
// optimization can be too aggressive in some cases. See
// [Bug #16504].
regular_block_arg = false;
}
}
argc = pm_setup_args_core(arguments_node, block, flags, regular_block_arg, kw_arg, &dup_rest, iseq, ret, scope_node, node_location);
PUSH_SEQ(ret, block_arg);
}
else {
argc = pm_setup_args_core(arguments_node, block, flags, false, kw_arg, &dup_rest, iseq, ret, scope_node, node_location);
}
// If the dup_rest flag was consumed while compiling the arguments (which
// effectively means we found the splat node), then it would have changed
// during the call to pm_setup_args_core. In this case, we want to add the
// VM_CALL_ARGS_SPLAT_MUT flag.
if (*flags & VM_CALL_ARGS_SPLAT && dup_rest != initial_dup_rest) {
*flags |= VM_CALL_ARGS_SPLAT_MUT;
}
return argc;
}
/**
* Compile an index operator write node, which is a node that is writing a value
* using the [] and []= methods. It looks like:
*
* foo[bar] += baz
*
* This breaks down to caching the receiver and arguments on the stack, calling
* the [] method, calling the operator method with the result of the [] method,
* and then calling the []= method with the result of the operator method.
*/
static void
pm_compile_index_operator_write_node(rb_iseq_t *iseq, const pm_index_operator_write_node_t *node, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
{
const pm_node_location_t location = *node_location;
if (!popped) PUSH_INSN(ret, location, putnil);
PM_COMPILE_NOT_POPPED(node->receiver);
int boff = (node->block == NULL ? 0 : 1);
int flag = PM_NODE_TYPE_P(node->receiver, PM_SELF_NODE) ? VM_CALL_FCALL : 0;
struct rb_callinfo_kwarg *keywords = NULL;
int argc = pm_setup_args(node->arguments, (const pm_node_t *) node->block, &flag, &keywords, iseq, ret, scope_node, node_location);
if ((argc > 0 || boff) && (flag & VM_CALL_KW_SPLAT)) {
if (boff) {
PUSH_INSN(ret, location, splatkw);
}
else {
PUSH_INSN(ret, location, dup);
PUSH_INSN(ret, location, splatkw);
PUSH_INSN(ret, location, pop);
}
}
int dup_argn = argc + 1 + boff;
int keyword_len = 0;
if (keywords) {
keyword_len = keywords->keyword_len;
dup_argn += keyword_len;
}
PUSH_INSN1(ret, location, dupn, INT2FIX(dup_argn));
PUSH_SEND_R(ret, location, idAREF, INT2FIX(argc), NULL, INT2FIX(flag & ~(VM_CALL_ARGS_SPLAT_MUT | VM_CALL_KW_SPLAT_MUT)), keywords);
PM_COMPILE_NOT_POPPED(node->value);
ID id_operator = pm_constant_id_lookup(scope_node, node->binary_operator);
PUSH_SEND(ret, location, id_operator, INT2FIX(1));
if (!popped) {
PUSH_INSN1(ret, location, setn, INT2FIX(dup_argn + 1));
}
if (flag & VM_CALL_ARGS_SPLAT) {
if (flag & VM_CALL_KW_SPLAT) {
PUSH_INSN1(ret, location, topn, INT2FIX(2 + boff));
if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
PUSH_INSN1(ret, location, splatarray, Qtrue);
flag |= VM_CALL_ARGS_SPLAT_MUT;
}
PUSH_INSN(ret, location, swap);
PUSH_INSN1(ret, location, pushtoarray, INT2FIX(1));
PUSH_INSN1(ret, location, setn, INT2FIX(2 + boff));
PUSH_INSN(ret, location, pop);
}
else {
if (boff > 0) {
PUSH_INSN1(ret, location, dupn, INT2FIX(3));
PUSH_INSN(ret, location, swap);
PUSH_INSN(ret, location, pop);
}
if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
PUSH_INSN(ret, location, swap);
PUSH_INSN1(ret, location, splatarray, Qtrue);
PUSH_INSN(ret, location, swap);
flag |= VM_CALL_ARGS_SPLAT_MUT;
}
PUSH_INSN1(ret, location, pushtoarray, INT2FIX(1));
if (boff > 0) {
PUSH_INSN1(ret, location, setn, INT2FIX(3));
PUSH_INSN(ret, location, pop);
PUSH_INSN(ret, location, pop);
}
}
PUSH_SEND_R(ret, location, idASET, INT2FIX(argc), NULL, INT2FIX(flag), keywords);
}
else if (flag & VM_CALL_KW_SPLAT) {
if (boff > 0) {
PUSH_INSN1(ret, location, topn, INT2FIX(2));
PUSH_INSN(ret, location, swap);
PUSH_INSN1(ret, location, setn, INT2FIX(3));
PUSH_INSN(ret, location, pop);
}
PUSH_INSN(ret, location, swap);
PUSH_SEND_R(ret, location, idASET, INT2FIX(argc + 1), NULL, INT2FIX(flag), keywords);
}
else if (keyword_len) {
PUSH_INSN(ret, location, dup);
PUSH_INSN1(ret, location, opt_reverse, INT2FIX(keyword_len + boff + 2));
PUSH_INSN1(ret, location, opt_reverse, INT2FIX(keyword_len + boff + 1));
PUSH_INSN(ret, location, pop);
PUSH_SEND_R(ret, location, idASET, INT2FIX(argc + 1), NULL, INT2FIX(flag), keywords);
}
else {
if (boff > 0) {
PUSH_INSN(ret, location, swap);
}
PUSH_SEND_R(ret, location, idASET, INT2FIX(argc + 1), NULL, INT2FIX(flag), keywords);
}
PUSH_INSN(ret, location, pop);
}
/**
* Compile an index control flow write node, which is a node that is writing a
* value using the [] and []= methods and the &&= and ||= operators. It looks
* like:
*
* foo[bar] ||= baz
*
* This breaks down to caching the receiver and arguments on the stack, calling
* the [] method, checking the result and then changing control flow based on
* it. If the value would result in a write, then the value is written using the
* []= method.
*/
static void
pm_compile_index_control_flow_write_node(rb_iseq_t *iseq, const pm_node_t *node, const pm_node_t *receiver, const pm_arguments_node_t *arguments, const pm_block_argument_node_t *block, const pm_node_t *value, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
{
const pm_node_location_t location = *node_location;
if (!popped) PUSH_INSN(ret, location, putnil);
PM_COMPILE_NOT_POPPED(receiver);
int boff = (block == NULL ? 0 : 1);
int flag = PM_NODE_TYPE_P(receiver, PM_SELF_NODE) ? VM_CALL_FCALL : 0;
struct rb_callinfo_kwarg *keywords = NULL;
int argc = pm_setup_args(arguments, (const pm_node_t *) block, &flag, &keywords, iseq, ret, scope_node, node_location);
if ((argc > 0 || boff) && (flag & VM_CALL_KW_SPLAT)) {
if (boff) {
PUSH_INSN(ret, location, splatkw);
}
else {
PUSH_INSN(ret, location, dup);
PUSH_INSN(ret, location, splatkw);
PUSH_INSN(ret, location, pop);
}
}
int dup_argn = argc + 1 + boff;
int keyword_len = 0;
if (keywords) {
keyword_len = keywords->keyword_len;
dup_argn += keyword_len;
}
PUSH_INSN1(ret, location, dupn, INT2FIX(dup_argn));
PUSH_SEND_R(ret, location, idAREF, INT2FIX(argc), NULL, INT2FIX(flag & ~(VM_CALL_ARGS_SPLAT_MUT | VM_CALL_KW_SPLAT_MUT)), keywords);
LABEL *label = NEW_LABEL(location.line);
LABEL *lfin = NEW_LABEL(location.line);
PUSH_INSN(ret, location, dup);
if (PM_NODE_TYPE_P(node, PM_INDEX_AND_WRITE_NODE)) {
PUSH_INSNL(ret, location, branchunless, label);
}
else {
PUSH_INSNL(ret, location, branchif, label);
}
PUSH_INSN(ret, location, pop);
PM_COMPILE_NOT_POPPED(value);
if (!popped) {
PUSH_INSN1(ret, location, setn, INT2FIX(dup_argn + 1));
}
if (flag & VM_CALL_ARGS_SPLAT) {
if (flag & VM_CALL_KW_SPLAT) {
PUSH_INSN1(ret, location, topn, INT2FIX(2 + boff));
if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
PUSH_INSN1(ret, location, splatarray, Qtrue);
flag |= VM_CALL_ARGS_SPLAT_MUT;
}
PUSH_INSN(ret, location, swap);
PUSH_INSN1(ret, location, pushtoarray, INT2FIX(1));
PUSH_INSN1(ret, location, setn, INT2FIX(2 + boff));
PUSH_INSN(ret, location, pop);
}
else {
if (boff > 0) {
PUSH_INSN1(ret, location, dupn, INT2FIX(3));
PUSH_INSN(ret, location, swap);
PUSH_INSN(ret, location, pop);
}
if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
PUSH_INSN(ret, location, swap);
PUSH_INSN1(ret, location, splatarray, Qtrue);
PUSH_INSN(ret, location, swap);
flag |= VM_CALL_ARGS_SPLAT_MUT;
}
PUSH_INSN1(ret, location, pushtoarray, INT2FIX(1));
if (boff > 0) {
PUSH_INSN1(ret, location, setn, INT2FIX(3));
PUSH_INSN(ret, location, pop);
PUSH_INSN(ret, location, pop);
}
}
PUSH_SEND_R(ret, location, idASET, INT2FIX(argc), NULL, INT2FIX(flag), keywords);
}
else if (flag & VM_CALL_KW_SPLAT) {
if (boff > 0) {
PUSH_INSN1(ret, location, topn, INT2FIX(2));
PUSH_INSN(ret, location, swap);
PUSH_INSN1(ret, location, setn, INT2FIX(3));
PUSH_INSN(ret, location, pop);
}
PUSH_INSN(ret, location, swap);
PUSH_SEND_R(ret, location, idASET, INT2FIX(argc + 1), NULL, INT2FIX(flag), keywords);
}
else if (keyword_len) {
PUSH_INSN1(ret, location, opt_reverse, INT2FIX(keyword_len + boff + 1));
PUSH_INSN1(ret, location, opt_reverse, INT2FIX(keyword_len + boff + 0));
PUSH_SEND_R(ret, location, idASET, INT2FIX(argc + 1), NULL, INT2FIX(flag), keywords);
}
else {
if (boff > 0) {
PUSH_INSN(ret, location, swap);
}
PUSH_SEND_R(ret, location, idASET, INT2FIX(argc + 1), NULL, INT2FIX(flag), keywords);
}
PUSH_INSN(ret, location, pop);
PUSH_INSNL(ret, location, jump, lfin);
PUSH_LABEL(ret, label);
if (!popped) {
PUSH_INSN1(ret, location, setn, INT2FIX(dup_argn + 1));
}
PUSH_INSN1(ret, location, adjuststack, INT2FIX(dup_argn + 1));
PUSH_LABEL(ret, lfin);
}
// When we compile a pattern matching expression, we use the stack as a scratch
// space to store lots of different values (consider it like we have a pattern
// matching function and we need space for a bunch of different local
// variables). The "base index" refers to the index on the stack where we
// started compiling the pattern matching expression. These offsets from that
// base index indicate the location of the various locals we need.
#define PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE 0
#define PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING 1
#define PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P 2
#define PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_MATCHEE 3
#define PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_KEY 4
// A forward declaration because this is the recursive function that handles
// compiling a pattern. It can be reentered by nesting patterns, as in the case
// of arrays or hashes.
static int pm_compile_pattern(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t *node, LINK_ANCHOR *const ret, LABEL *matched_label, LABEL *unmatched_label, bool in_single_pattern, bool in_alternation_pattern, bool use_deconstructed_cache, unsigned int base_index);
/**
* This function generates the code to set up the error string and error_p
* locals depending on whether or not the pattern matched.
*/
static int
pm_compile_pattern_generic_error(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t *node, LINK_ANCHOR *const ret, VALUE message, unsigned int base_index)
{
const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
LABEL *match_succeeded_label = NEW_LABEL(location.line);
PUSH_INSN(ret, location, dup);
PUSH_INSNL(ret, location, branchif, match_succeeded_label);
PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
PUSH_INSN1(ret, location, putobject, message);
PUSH_INSN1(ret, location, topn, INT2FIX(3));
PUSH_SEND(ret, location, id_core_sprintf, INT2FIX(2));
PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
PUSH_INSN1(ret, location, putobject, Qfalse);
PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
PUSH_INSN(ret, location, pop);
PUSH_INSN(ret, location, pop);
PUSH_LABEL(ret, match_succeeded_label);
return COMPILE_OK;
}
/**
* This function generates the code to set up the error string and error_p
* locals depending on whether or not the pattern matched when the value needs
* to match a specific deconstructed length.
*/
static int
pm_compile_pattern_length_error(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t *node, LINK_ANCHOR *const ret, VALUE message, VALUE length, unsigned int base_index)
{
const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
LABEL *match_succeeded_label = NEW_LABEL(location.line);
PUSH_INSN(ret, location, dup);
PUSH_INSNL(ret, location, branchif, match_succeeded_label);
PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
PUSH_INSN1(ret, location, putobject, message);
PUSH_INSN1(ret, location, topn, INT2FIX(3));
PUSH_INSN(ret, location, dup);
PUSH_SEND(ret, location, idLength, INT2FIX(0));
PUSH_INSN1(ret, location, putobject, length);
PUSH_SEND(ret, location, id_core_sprintf, INT2FIX(4));
PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
PUSH_INSN1(ret, location, putobject, Qfalse);
PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
PUSH_INSN(ret, location, pop);
PUSH_INSN(ret, location, pop);
PUSH_LABEL(ret, match_succeeded_label);
return COMPILE_OK;
}
/**
* This function generates the code to set up the error string and error_p
* locals depending on whether or not the pattern matched when the value needs
* to pass a specific #=== method call.
*/
static int
pm_compile_pattern_eqq_error(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t *node, LINK_ANCHOR *const ret, unsigned int base_index)
{
const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
LABEL *match_succeeded_label = NEW_LABEL(location.line);
PUSH_INSN(ret, location, dup);
PUSH_INSNL(ret, location, branchif, match_succeeded_label);
PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
VALUE operand = rb_fstring_lit("%p === %p does not return true");
PUSH_INSN1(ret, location, putobject, operand);
PUSH_INSN1(ret, location, topn, INT2FIX(3));
PUSH_INSN1(ret, location, topn, INT2FIX(5));
PUSH_SEND(ret, location, id_core_sprintf, INT2FIX(3));
PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
PUSH_INSN1(ret, location, putobject, Qfalse);
PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
PUSH_INSN(ret, location, pop);
PUSH_INSN(ret, location, pop);
PUSH_LABEL(ret, match_succeeded_label);
PUSH_INSN1(ret, location, setn, INT2FIX(2));
PUSH_INSN(ret, location, pop);
PUSH_INSN(ret, location, pop);
return COMPILE_OK;
}
/**
* This is a variation on compiling a pattern matching expression that is used
* to have the pattern matching instructions fall through to immediately after
* the pattern if it passes. Otherwise it jumps to the given unmatched_label
* label.
*/
static int
pm_compile_pattern_match(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t *node, LINK_ANCHOR *const ret, LABEL *unmatched_label, bool in_single_pattern, bool in_alternation_pattern, bool use_deconstructed_cache, unsigned int base_index)
{
LABEL *matched_label = NEW_LABEL(pm_node_line_number(scope_node->parser, node));
CHECK(pm_compile_pattern(iseq, scope_node, node, ret, matched_label, unmatched_label, in_single_pattern, in_alternation_pattern, use_deconstructed_cache, base_index));
PUSH_LABEL(ret, matched_label);
return COMPILE_OK;
}
/**
* This function compiles in the code necessary to call #deconstruct on the
* value to match against. It raises appropriate errors if the method does not
* exist or if it returns the wrong type.
*/
static int
pm_compile_pattern_deconstruct(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t *node, LINK_ANCHOR *const ret, LABEL *deconstruct_label, LABEL *match_failed_label, LABEL *deconstructed_label, LABEL *type_error_label, bool in_single_pattern, bool use_deconstructed_cache, unsigned int base_index)
{
const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
if (use_deconstructed_cache) {
PUSH_INSN1(ret, location, topn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE));
PUSH_INSNL(ret, location, branchnil, deconstruct_label);
PUSH_INSN1(ret, location, topn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE));
PUSH_INSNL(ret, location, branchunless, match_failed_label);
PUSH_INSN(ret, location, pop);
PUSH_INSN1(ret, location, topn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE - 1));
PUSH_INSNL(ret, location, jump, deconstructed_label);
}
else {
PUSH_INSNL(ret, location, jump, deconstruct_label);
}
PUSH_LABEL(ret, deconstruct_label);
PUSH_INSN(ret, location, dup);
VALUE operand = ID2SYM(rb_intern("deconstruct"));
PUSH_INSN1(ret, location, putobject, operand);
PUSH_SEND(ret, location, idRespond_to, INT2FIX(1));
if (use_deconstructed_cache) {
PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE + 1));
}
if (in_single_pattern) {
CHECK(pm_compile_pattern_generic_error(iseq, scope_node, node, ret, rb_fstring_lit("%p does not respond to #deconstruct"), base_index + 1));
}
PUSH_INSNL(ret, location, branchunless, match_failed_label);
PUSH_SEND(ret, location, rb_intern("deconstruct"), INT2FIX(0));
if (use_deconstructed_cache) {
PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE));
}
PUSH_INSN(ret, location, dup);
PUSH_INSN1(ret, location, checktype, INT2FIX(T_ARRAY));
PUSH_INSNL(ret, location, branchunless, type_error_label);
PUSH_LABEL(ret, deconstructed_label);
return COMPILE_OK;
}
/**
* This function compiles in the code necessary to match against the optional
* constant path that is attached to an array, find, or hash pattern.
*/
static int
pm_compile_pattern_constant(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t *node, LINK_ANCHOR *const ret, LABEL *match_failed_label, bool in_single_pattern, unsigned int base_index)
{
const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
PUSH_INSN(ret, location, dup);
PM_COMPILE_NOT_POPPED(node);
if (in_single_pattern) {
PUSH_INSN1(ret, location, dupn, INT2FIX(2));
}
PUSH_INSN1(ret, location, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_CASE));
if (in_single_pattern) {
CHECK(pm_compile_pattern_eqq_error(iseq, scope_node, node, ret, base_index + 3));
}
PUSH_INSNL(ret, location, branchunless, match_failed_label);
return COMPILE_OK;
}
/**
* When matching fails, an appropriate error must be raised. This function is
* responsible for compiling in those error raising instructions.
*/
static void
pm_compile_pattern_error_handler(rb_iseq_t *iseq, const pm_scope_node_t *scope_node, const pm_node_t *node, LINK_ANCHOR *const ret, LABEL *done_label, bool popped)
{
const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
LABEL *key_error_label = NEW_LABEL(location.line);
LABEL *cleanup_label = NEW_LABEL(location.line);
struct rb_callinfo_kwarg *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"));
PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
PUSH_INSN1(ret, location, topn, INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
PUSH_INSNL(ret, location, branchif, key_error_label);
PUSH_INSN1(ret, location, putobject, rb_eNoMatchingPatternError);
PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
{
VALUE operand = rb_fstring_lit("%p: %s");
PUSH_INSN1(ret, location, putobject, operand);
}
PUSH_INSN1(ret, location, topn, INT2FIX(4));
PUSH_INSN1(ret, location, topn, INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 6));
PUSH_SEND(ret, location, id_core_sprintf, INT2FIX(3));
PUSH_SEND(ret, location, id_core_raise, INT2FIX(2));
PUSH_INSNL(ret, location, jump, cleanup_label);
PUSH_LABEL(ret, key_error_label);
PUSH_INSN1(ret, location, putobject, rb_eNoMatchingPatternKeyError);
PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
{
VALUE operand = rb_fstring_lit("%p: %s");
PUSH_INSN1(ret, location, putobject, operand);
}
PUSH_INSN1(ret, location, topn, INT2FIX(4));
PUSH_INSN1(ret, location, topn, INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 6));
PUSH_SEND(ret, location, id_core_sprintf, INT2FIX(3));
PUSH_INSN1(ret, location, topn, INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_MATCHEE + 4));
PUSH_INSN1(ret, location, topn, INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_KEY + 5));
PUSH_SEND_R(ret, location, rb_intern("new"), INT2FIX(1), NULL, INT2FIX(VM_CALL_KWARG), kw_arg);
PUSH_SEND(ret, location, id_core_raise, INT2FIX(1));
PUSH_LABEL(ret, cleanup_label);
PUSH_INSN1(ret, location, adjuststack, INT2FIX(7));
if (!popped) PUSH_INSN(ret, location, putnil);
PUSH_INSNL(ret, location, jump, done_label);
PUSH_INSN1(ret, location, dupn, INT2FIX(5));
if (popped) PUSH_INSN(ret, location, putnil);
}
/**
* Compile a pattern matching expression.
*/
static int
pm_compile_pattern(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t *node, LINK_ANCHOR *const ret, LABEL *matched_label, LABEL *unmatched_label, bool in_single_pattern, bool in_alternation_pattern, bool use_deconstructed_cache, unsigned int base_index)
{
const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
switch (PM_NODE_TYPE(node)) {
case PM_ARRAY_PATTERN_NODE: {
// Array patterns in pattern matching are triggered by using commas in
// a pattern or wrapping it in braces. They are represented by a
// ArrayPatternNode. This looks like:
//
// foo => [1, 2, 3]
//
// It can optionally have a splat in the middle of it, which can
// optionally have a name attached.
const pm_array_pattern_node_t *cast = (const pm_array_pattern_node_t *) node;
const size_t requireds_size = cast->requireds.size;
const size_t posts_size = cast->posts.size;
const size_t minimum_size = requireds_size + posts_size;
bool rest_named = false;
bool use_rest_size = false;
if (cast->rest != NULL) {
rest_named = (PM_NODE_TYPE_P(cast->rest, PM_SPLAT_NODE) && ((const pm_splat_node_t *) cast->rest)->expression != NULL);
use_rest_size = (rest_named || (!rest_named && posts_size > 0));
}
LABEL *match_failed_label = NEW_LABEL(location.line);
LABEL *type_error_label = NEW_LABEL(location.line);
LABEL *deconstruct_label = NEW_LABEL(location.line);
LABEL *deconstructed_label = NEW_LABEL(location.line);
if (use_rest_size) {
PUSH_INSN1(ret, location, putobject, INT2FIX(0));
PUSH_INSN(ret, location, swap);
base_index++;
}
if (cast->constant != NULL) {
CHECK(pm_compile_pattern_constant(iseq, scope_node, cast->constant, ret, match_failed_label, in_single_pattern, base_index));
}
CHECK(pm_compile_pattern_deconstruct(iseq, scope_node, node, ret, deconstruct_label, match_failed_label, deconstructed_label, type_error_label, in_single_pattern, use_deconstructed_cache, base_index));
PUSH_INSN(ret, location, dup);
PUSH_SEND(ret, location, idLength, INT2FIX(0));
PUSH_INSN1(ret, location, putobject, INT2FIX(minimum_size));
PUSH_SEND(ret, location, cast->rest == NULL ? idEq : idGE, INT2FIX(1));
if (in_single_pattern) {
VALUE message = cast->rest == NULL ? rb_fstring_lit("%p length mismatch (given %p, expected %p)") : rb_fstring_lit("%p length mismatch (given %p, expected %p+)");
CHECK(pm_compile_pattern_length_error(iseq, scope_node, node, ret, message, INT2FIX(minimum_size), base_index + 1));
}
PUSH_INSNL(ret, location, branchunless, match_failed_label);
for (size_t index = 0; index < requireds_size; index++) {
const pm_node_t *required = cast->requireds.nodes[index];
PUSH_INSN(ret, location, dup);
PUSH_INSN1(ret, location, putobject, INT2FIX(index));
PUSH_SEND(ret, location, idAREF, INT2FIX(1));
CHECK(pm_compile_pattern_match(iseq, scope_node, required, ret, match_failed_label, in_single_pattern, in_alternation_pattern, false, base_index + 1));
}
if (cast->rest != NULL) {
if (rest_named) {
PUSH_INSN(ret, location, dup);
PUSH_INSN1(ret, location, putobject, INT2FIX(requireds_size));
PUSH_INSN1(ret, location, topn, INT2FIX(1));
PUSH_SEND(ret, location, idLength, INT2FIX(0));
PUSH_INSN1(ret, location, putobject, INT2FIX(minimum_size));
PUSH_SEND(ret, location, idMINUS, INT2FIX(1));
PUSH_INSN1(ret, location, setn, INT2FIX(4));
PUSH_SEND(ret, location, idAREF, INT2FIX(2));
CHECK(pm_compile_pattern_match(iseq, scope_node, ((const pm_splat_node_t *) cast->rest)->expression, ret, match_failed_label, in_single_pattern, in_alternation_pattern, false, base_index + 1));
}
else if (posts_size > 0) {
PUSH_INSN(ret, location, dup);
PUSH_SEND(ret, location, idLength, INT2FIX(0));
PUSH_INSN1(ret, location, putobject, INT2FIX(minimum_size));
PUSH_SEND(ret, location, idMINUS, INT2FIX(1));
PUSH_INSN1(ret, location, setn, INT2FIX(2));
PUSH_INSN(ret, location, pop);
}
}
for (size_t index = 0; index < posts_size; index++) {
const pm_node_t *post = cast->posts.nodes[index];
PUSH_INSN(ret, location, dup);
PUSH_INSN1(ret, location, putobject, INT2FIX(requireds_size + index));
PUSH_INSN1(ret, location, topn, INT2FIX(3));
PUSH_SEND(ret, location, idPLUS, INT2FIX(1));
PUSH_SEND(ret, location, idAREF, INT2FIX(1));
CHECK(pm_compile_pattern_match(iseq, scope_node, post, ret, match_failed_label, in_single_pattern, in_alternation_pattern, false, base_index + 1));
}
PUSH_INSN(ret, location, pop);
if (use_rest_size) {
PUSH_INSN(ret, location, pop);
}
PUSH_INSNL(ret, location, jump, matched_label);
PUSH_INSN(ret, location, putnil);
if (use_rest_size) {
PUSH_INSN(ret, location, putnil);
}
PUSH_LABEL(ret, type_error_label);
PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
PUSH_INSN1(ret, location, putobject, rb_eTypeError);
{
VALUE operand = rb_fstring_lit("deconstruct must return Array");
PUSH_INSN1(ret, location, putobject, operand);
}
PUSH_SEND(ret, location, id_core_raise, INT2FIX(2));
PUSH_INSN(ret, location, pop);
PUSH_LABEL(ret, match_failed_label);
PUSH_INSN(ret, location, pop);
if (use_rest_size) {
PUSH_INSN(ret, location, pop);
}
PUSH_INSNL(ret, location, jump, unmatched_label);
break;
}
case PM_FIND_PATTERN_NODE: {
// Find patterns in pattern matching are triggered by using commas in
// a pattern or wrapping it in braces and using a splat on both the left
// and right side of the pattern. This looks like:
//
// foo => [*, 1, 2, 3, *]
//
// There can be any number of requireds in the middle. The splats on
// both sides can optionally have names attached.
const pm_find_pattern_node_t *cast = (const pm_find_pattern_node_t *) node;
const size_t size = cast->requireds.size;
LABEL *match_failed_label = NEW_LABEL(location.line);
LABEL *type_error_label = NEW_LABEL(location.line);
LABEL *deconstruct_label = NEW_LABEL(location.line);
LABEL *deconstructed_label = NEW_LABEL(location.line);
if (cast->constant) {
CHECK(pm_compile_pattern_constant(iseq, scope_node, cast->constant, ret, match_failed_label, in_single_pattern, base_index));
}
CHECK(pm_compile_pattern_deconstruct(iseq, scope_node, node, ret, deconstruct_label, match_failed_label, deconstructed_label, type_error_label, in_single_pattern, use_deconstructed_cache, base_index));
PUSH_INSN(ret, location, dup);
PUSH_SEND(ret, location, idLength, INT2FIX(0));
PUSH_INSN1(ret, location, putobject, INT2FIX(size));
PUSH_SEND(ret, location, idGE, INT2FIX(1));
if (in_single_pattern) {
CHECK(pm_compile_pattern_length_error(iseq, scope_node, node, ret, rb_fstring_lit("%p length mismatch (given %p, expected %p+)"), INT2FIX(size), base_index + 1));
}
PUSH_INSNL(ret, location, branchunless, match_failed_label);
{
LABEL *while_begin_label = NEW_LABEL(location.line);
LABEL *next_loop_label = NEW_LABEL(location.line);
LABEL *find_succeeded_label = NEW_LABEL(location.line);
LABEL *find_failed_label = NEW_LABEL(location.line);
PUSH_INSN(ret, location, dup);
PUSH_SEND(ret, location, idLength, INT2FIX(0));
PUSH_INSN(ret, location, dup);
PUSH_INSN1(ret, location, putobject, INT2FIX(size));
PUSH_SEND(ret, location, idMINUS, INT2FIX(1));
PUSH_INSN1(ret, location, putobject, INT2FIX(0));
PUSH_LABEL(ret, while_begin_label);
PUSH_INSN(ret, location, dup);
PUSH_INSN1(ret, location, topn, INT2FIX(2));
PUSH_SEND(ret, location, idLE, INT2FIX(1));
PUSH_INSNL(ret, location, branchunless, find_failed_label);
for (size_t index = 0; index < size; index++) {
PUSH_INSN1(ret, location, topn, INT2FIX(3));
PUSH_INSN1(ret, location, topn, INT2FIX(1));
if (index != 0) {
PUSH_INSN1(ret, location, putobject, INT2FIX(index));
PUSH_SEND(ret, location, idPLUS, INT2FIX(1));
}
PUSH_SEND(ret, location, idAREF, INT2FIX(1));
CHECK(pm_compile_pattern_match(iseq, scope_node, cast->requireds.nodes[index], ret, next_loop_label, in_single_pattern, in_alternation_pattern, false, base_index + 4));
}
const pm_splat_node_t *left = cast->left;
if (left->expression != NULL) {
PUSH_INSN1(ret, location, topn, INT2FIX(3));
PUSH_INSN1(ret, location, putobject, INT2FIX(0));
PUSH_INSN1(ret, location, topn, INT2FIX(2));
PUSH_SEND(ret, location, idAREF, INT2FIX(2));
CHECK(pm_compile_pattern_match(iseq, scope_node, left->expression, ret, find_failed_label, in_single_pattern, in_alternation_pattern, false, base_index + 4));
}
RUBY_ASSERT(PM_NODE_TYPE_P(cast->right, PM_SPLAT_NODE));
const pm_splat_node_t *right = (const pm_splat_node_t *) cast->right;
if (right->expression != NULL) {
PUSH_INSN1(ret, location, topn, INT2FIX(3));
PUSH_INSN1(ret, location, topn, INT2FIX(1));
PUSH_INSN1(ret, location, putobject, INT2FIX(size));
PUSH_SEND(ret, location, idPLUS, INT2FIX(1));
PUSH_INSN1(ret, location, topn, INT2FIX(3));
PUSH_SEND(ret, location, idAREF, INT2FIX(2));
pm_compile_pattern_match(iseq, scope_node, right->expression, ret, find_failed_label, in_single_pattern, in_alternation_pattern, false, base_index + 4);
}
PUSH_INSNL(ret, location, jump, find_succeeded_label);
PUSH_LABEL(ret, next_loop_label);
PUSH_INSN1(ret, location, putobject, INT2FIX(1));
PUSH_SEND(ret, location, idPLUS, INT2FIX(1));
PUSH_INSNL(ret, location, jump, while_begin_label);
PUSH_LABEL(ret, find_failed_label);
PUSH_INSN1(ret, location, adjuststack, INT2FIX(3));
if (in_single_pattern) {
PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
{
VALUE operand = rb_fstring_lit("%p does not match to find pattern");
PUSH_INSN1(ret, location, putobject, operand);
}
PUSH_INSN1(ret, location, topn, INT2FIX(2));
PUSH_SEND(ret, location, id_core_sprintf, INT2FIX(2));
PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
PUSH_INSN1(ret, location, putobject, Qfalse);
PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
PUSH_INSN(ret, location, pop);
PUSH_INSN(ret, location, pop);
}
PUSH_INSNL(ret, location, jump, match_failed_label);
PUSH_INSN1(ret, location, dupn, INT2FIX(3));
PUSH_LABEL(ret, find_succeeded_label);
PUSH_INSN1(ret, location, adjuststack, INT2FIX(3));
}
PUSH_INSN(ret, location, pop);
PUSH_INSNL(ret, location, jump, matched_label);
PUSH_INSN(ret, location, putnil);
PUSH_LABEL(ret, type_error_label);
PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
PUSH_INSN1(ret, location, putobject, rb_eTypeError);
{
VALUE operand = rb_fstring_lit("deconstruct must return Array");
PUSH_INSN1(ret, location, putobject, operand);
}
PUSH_SEND(ret, location, id_core_raise, INT2FIX(2));
PUSH_INSN(ret, location, pop);
PUSH_LABEL(ret, match_failed_label);
PUSH_INSN(ret, location, pop);
PUSH_INSNL(ret, location, jump, unmatched_label);
break;
}
case PM_HASH_PATTERN_NODE: {
// Hash patterns in pattern matching are triggered by using labels and
// values in a pattern or by using the ** operator. They are represented
// by the HashPatternNode. This looks like:
//
// foo => { a: 1, b: 2, **bar }
//
// It can optionally have an assoc splat in the middle of it, which can
// optionally have a name.
const pm_hash_pattern_node_t *cast = (const pm_hash_pattern_node_t *) node;
// We don't consider it a "rest" parameter if it's a ** that is unnamed.
bool has_rest = cast->rest != NULL && !(PM_NODE_TYPE_P(cast->rest, PM_ASSOC_SPLAT_NODE) && ((const pm_assoc_splat_node_t *) cast->rest)->value == NULL);
bool has_keys = cast->elements.size > 0 || cast->rest != NULL;
LABEL *match_failed_label = NEW_LABEL(location.line);
LABEL *type_error_label = NEW_LABEL(location.line);
VALUE keys = Qnil;
if (has_keys && !has_rest) {
keys = rb_ary_new_capa(cast->elements.size);
for (size_t index = 0; index < cast->elements.size; index++) {
const pm_node_t *element = cast->elements.nodes[index];
RUBY_ASSERT(PM_NODE_TYPE_P(element, PM_ASSOC_NODE));
const pm_node_t *key = ((const pm_assoc_node_t *) element)->key;
RUBY_ASSERT(PM_NODE_TYPE_P(key, PM_SYMBOL_NODE));
VALUE symbol = ID2SYM(parse_string_symbol(scope_node, (const pm_symbol_node_t *) key));
rb_ary_push(keys, symbol);
}
}
if (cast->constant) {
CHECK(pm_compile_pattern_constant(iseq, scope_node, cast->constant, ret, match_failed_label, in_single_pattern, base_index));
}
PUSH_INSN(ret, location, dup);
{
VALUE operand = ID2SYM(rb_intern("deconstruct_keys"));
PUSH_INSN1(ret, location, putobject, operand);
}
PUSH_SEND(ret, location, idRespond_to, INT2FIX(1));
if (in_single_pattern) {
CHECK(pm_compile_pattern_generic_error(iseq, scope_node, node, ret, rb_fstring_lit("%p does not respond to #deconstruct_keys"), base_index + 1));
}
PUSH_INSNL(ret, location, branchunless, match_failed_label);
if (NIL_P(keys)) {
PUSH_INSN(ret, location, putnil);
}
else {
PUSH_INSN1(ret, location, duparray, keys);
RB_OBJ_WRITTEN(iseq, Qundef, rb_obj_hide(keys));
}
PUSH_SEND(ret, location, rb_intern("deconstruct_keys"), INT2FIX(1));
PUSH_INSN(ret, location, dup);
PUSH_INSN1(ret, location, checktype, INT2FIX(T_HASH));
PUSH_INSNL(ret, location, branchunless, type_error_label);
if (has_rest) {
PUSH_SEND(ret, location, rb_intern("dup"), INT2FIX(0));
}
if (has_keys) {
DECL_ANCHOR(match_values);
for (size_t index = 0; index < cast->elements.size; index++) {
const pm_node_t *element = cast->elements.nodes[index];
RUBY_ASSERT(PM_NODE_TYPE_P(element, PM_ASSOC_NODE));
const pm_assoc_node_t *assoc = (const pm_assoc_node_t *) element;
const pm_node_t *key = assoc->key;
RUBY_ASSERT(PM_NODE_TYPE_P(key, PM_SYMBOL_NODE));
VALUE symbol = ID2SYM(parse_string_symbol(scope_node, (const pm_symbol_node_t *) key));
PUSH_INSN(ret, location, dup);
PUSH_INSN1(ret, location, putobject, symbol);
PUSH_SEND(ret, location, rb_intern("key?"), INT2FIX(1));
if (in_single_pattern) {
LABEL *match_succeeded_label = NEW_LABEL(location.line);
PUSH_INSN(ret, location, dup);
PUSH_INSNL(ret, location, branchif, match_succeeded_label);
{
VALUE operand = rb_str_freeze(rb_sprintf("key not found: %+"PRIsVALUE, symbol));
PUSH_INSN1(ret, location, putobject, operand);
}
PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 2));
PUSH_INSN1(ret, location, putobject, Qtrue);
PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 3));
PUSH_INSN1(ret, location, topn, INT2FIX(3));
PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_MATCHEE + 4));
PUSH_INSN1(ret, location, putobject, symbol);
PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_KEY + 5));
PUSH_INSN1(ret, location, adjuststack, INT2FIX(4));
PUSH_LABEL(ret, match_succeeded_label);
}
PUSH_INSNL(ret, location, branchunless, match_failed_label);
PUSH_INSN(match_values, location, dup);
PUSH_INSN1(match_values, location, putobject, symbol);
PUSH_SEND(match_values, location, has_rest ? rb_intern("delete") : idAREF, INT2FIX(1));
const pm_node_t *value = assoc->value;
if (PM_NODE_TYPE_P(value, PM_IMPLICIT_NODE)) {
value = ((const pm_implicit_node_t *) value)->value;
}
CHECK(pm_compile_pattern_match(iseq, scope_node, value, match_values, match_failed_label, in_single_pattern, in_alternation_pattern, false, base_index + 1));
}
PUSH_SEQ(ret, match_values);
}
else {
PUSH_INSN(ret, location, dup);
PUSH_SEND(ret, location, idEmptyP, INT2FIX(0));
if (in_single_pattern) {
CHECK(pm_compile_pattern_generic_error(iseq, scope_node, node, ret, rb_fstring_lit("%p is not empty"), base_index + 1));
}
PUSH_INSNL(ret, location, branchunless, match_failed_label);
}
if (has_rest) {
switch (PM_NODE_TYPE(cast->rest)) {
case PM_NO_KEYWORDS_PARAMETER_NODE: {
PUSH_INSN(ret, location, dup);
PUSH_SEND(ret, location, idEmptyP, INT2FIX(0));
if (in_single_pattern) {
pm_compile_pattern_generic_error(iseq, scope_node, node, ret, rb_fstring_lit("rest of %p is not empty"), base_index + 1);
}
PUSH_INSNL(ret, location, branchunless, match_failed_label);
break;
}
case PM_ASSOC_SPLAT_NODE: {
const pm_assoc_splat_node_t *splat = (const pm_assoc_splat_node_t *) cast->rest;
PUSH_INSN(ret, location, dup);
pm_compile_pattern_match(iseq, scope_node, splat->value, ret, match_failed_label, in_single_pattern, in_alternation_pattern, false, base_index + 1);
break;
}
default:
rb_bug("unreachable");
break;
}
}
PUSH_INSN(ret, location, pop);
PUSH_INSNL(ret, location, jump, matched_label);
PUSH_INSN(ret, location, putnil);
PUSH_LABEL(ret, type_error_label);
PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
PUSH_INSN1(ret, location, putobject, rb_eTypeError);
{
VALUE operand = rb_fstring_lit("deconstruct_keys must return Hash");
PUSH_INSN1(ret, location, putobject, operand);
}
PUSH_SEND(ret, location, id_core_raise, INT2FIX(2));
PUSH_INSN(ret, location, pop);
PUSH_LABEL(ret, match_failed_label);
PUSH_INSN(ret, location, pop);
PUSH_INSNL(ret, location, jump, unmatched_label);
break;
}
case PM_CAPTURE_PATTERN_NODE: {
// Capture patterns allow you to pattern match against an element in a
// pattern and also capture the value into a local variable. This looks
// like:
//
// [1] => [Integer => foo]
//
// In this case the `Integer => foo` will be represented by a
// CapturePatternNode, which has both a value (the pattern to match
// against) and a target (the place to write the variable into).
const pm_capture_pattern_node_t *cast = (const pm_capture_pattern_node_t *) node;
LABEL *match_failed_label = NEW_LABEL(location.line);
PUSH_INSN(ret, location, dup);
CHECK(pm_compile_pattern_match(iseq, scope_node, cast->value, ret, match_failed_label, in_single_pattern, in_alternation_pattern, use_deconstructed_cache, base_index + 1));
CHECK(pm_compile_pattern(iseq, scope_node, (const pm_node_t *) cast->target, ret, matched_label, match_failed_label, in_single_pattern, in_alternation_pattern, false, base_index));
PUSH_INSN(ret, location, putnil);
PUSH_LABEL(ret, match_failed_label);
PUSH_INSN(ret, location, pop);
PUSH_INSNL(ret, location, jump, unmatched_label);
break;
}
case PM_LOCAL_VARIABLE_TARGET_NODE: {
// Local variables can be targeted by placing identifiers in the place
// of a pattern. For example, foo in bar. This results in the value
// being matched being written to that local variable.
const pm_local_variable_target_node_t *cast = (const pm_local_variable_target_node_t *) node;
pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, cast->name, cast->depth);
// If this local variable is being written from within an alternation
// pattern, then it cannot actually be added to the local table since
// it's ambiguous which value should be used. So instead we indicate
// this with a compile error.
if (in_alternation_pattern) {
ID id = pm_constant_id_lookup(scope_node, cast->name);
const char *name = rb_id2name(id);
if (name && strlen(name) > 0 && name[0] != '_') {
COMPILE_ERROR(iseq, location.line, "illegal variable in alternative pattern (%"PRIsVALUE")", rb_id2str(id));
return COMPILE_NG;
}
}
PUSH_SETLOCAL(ret, location, index.index, index.level);
PUSH_INSNL(ret, location, jump, matched_label);
break;
}
case PM_ALTERNATION_PATTERN_NODE: {
// Alternation patterns allow you to specify multiple patterns in a
// single expression using the | operator.
const pm_alternation_pattern_node_t *cast = (const pm_alternation_pattern_node_t *) node;
LABEL *matched_left_label = NEW_LABEL(location.line);
LABEL *unmatched_left_label = NEW_LABEL(location.line);
// First, we're going to attempt to match against the left pattern. If
// that pattern matches, then we'll skip matching the right pattern.
PUSH_INSN(ret, location, dup);
CHECK(pm_compile_pattern(iseq, scope_node, cast->left, ret, matched_left_label, unmatched_left_label, in_single_pattern, true, use_deconstructed_cache, base_index + 1));
// If we get here, then we matched on the left pattern. In this case we
// should pop out the duplicate value that we preemptively added to
// match against the right pattern and then jump to the match label.
PUSH_LABEL(ret, matched_left_label);
PUSH_INSN(ret, location, pop);
PUSH_INSNL(ret, location, jump, matched_label);
PUSH_INSN(ret, location, putnil);
// If we get here, then we didn't match on the left pattern. In this
// case we attempt to match against the right pattern.
PUSH_LABEL(ret, unmatched_left_label);
CHECK(pm_compile_pattern(iseq, scope_node, cast->right, ret, matched_label, unmatched_label, in_single_pattern, true, use_deconstructed_cache, base_index));
break;
}
case PM_PARENTHESES_NODE:
// Parentheses are allowed to wrap expressions in pattern matching and
// they do nothing since they can only wrap individual expressions and
// not groups. In this case we'll recurse back into this same function
// with the body of the parentheses.
return pm_compile_pattern(iseq, scope_node, ((const pm_parentheses_node_t *) node)->body, ret, matched_label, unmatched_label, in_single_pattern, in_alternation_pattern, use_deconstructed_cache, base_index);
case PM_PINNED_EXPRESSION_NODE:
// Pinned expressions are a way to match against the value of an
// expression that should be evaluated at runtime. This looks like:
// foo in ^(bar). To compile these, we compile the expression as if it
// were a literal value by falling through to the literal case.
node = ((const pm_pinned_expression_node_t *) node)->expression;
/* fallthrough */
case PM_ARRAY_NODE:
case PM_CLASS_VARIABLE_READ_NODE:
case PM_CONSTANT_PATH_NODE:
case PM_CONSTANT_READ_NODE:
case PM_FALSE_NODE:
case PM_FLOAT_NODE:
case PM_GLOBAL_VARIABLE_READ_NODE:
case PM_IMAGINARY_NODE:
case PM_INSTANCE_VARIABLE_READ_NODE:
case PM_IT_LOCAL_VARIABLE_READ_NODE:
case PM_INTEGER_NODE:
case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE:
case PM_INTERPOLATED_STRING_NODE:
case PM_INTERPOLATED_SYMBOL_NODE:
case PM_INTERPOLATED_X_STRING_NODE:
case PM_LAMBDA_NODE:
case PM_LOCAL_VARIABLE_READ_NODE:
case PM_NIL_NODE:
case PM_SOURCE_ENCODING_NODE:
case PM_SOURCE_FILE_NODE:
case PM_SOURCE_LINE_NODE:
case PM_RANGE_NODE:
case PM_RATIONAL_NODE:
case PM_REGULAR_EXPRESSION_NODE:
case PM_SELF_NODE:
case PM_STRING_NODE:
case PM_SYMBOL_NODE:
case PM_TRUE_NODE:
case PM_X_STRING_NODE: {
// These nodes are all simple patterns, which means we'll use the
// checkmatch instruction to match against them, which is effectively a
// VM-level === operator.
PM_COMPILE_NOT_POPPED(node);
if (in_single_pattern) {
PUSH_INSN1(ret, location, dupn, INT2FIX(2));
}
PUSH_INSN1(ret, location, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_CASE));
if (in_single_pattern) {
pm_compile_pattern_eqq_error(iseq, scope_node, node, ret, base_index + 2);
}
PUSH_INSNL(ret, location, branchif, matched_label);
PUSH_INSNL(ret, location, jump, unmatched_label);
break;
}
case PM_PINNED_VARIABLE_NODE: {
// Pinned variables are a way to match against the value of a variable
// without it looking like you're trying to write to the variable. This
// looks like: foo in ^@bar. To compile these, we compile the variable
// that they hold.
const pm_pinned_variable_node_t *cast = (const pm_pinned_variable_node_t *) node;
CHECK(pm_compile_pattern(iseq, scope_node, cast->variable, ret, matched_label, unmatched_label, in_single_pattern, in_alternation_pattern, true, base_index));
break;
}
case PM_IF_NODE:
case PM_UNLESS_NODE: {
// If and unless nodes can show up here as guards on `in` clauses. This
// looks like:
//
// case foo
// in bar if baz?
// qux
// end
//
// Because we know they're in the modifier form and they can't have any
// variation on this pattern, we compile them differently (more simply)
// here than we would in the normal compilation path.
const pm_node_t *predicate;
const pm_node_t *statement;
if (PM_NODE_TYPE_P(node, PM_IF_NODE)) {
const pm_if_node_t *cast = (const pm_if_node_t *) node;
predicate = cast->predicate;
RUBY_ASSERT(cast->statements != NULL && cast->statements->body.size == 1);
statement = cast->statements->body.nodes[0];
}
else {
const pm_unless_node_t *cast = (const pm_unless_node_t *) node;
predicate = cast->predicate;
RUBY_ASSERT(cast->statements != NULL && cast->statements->body.size == 1);
statement = cast->statements->body.nodes[0];
}
CHECK(pm_compile_pattern_match(iseq, scope_node, statement, ret, unmatched_label, in_single_pattern, in_alternation_pattern, use_deconstructed_cache, base_index));
PM_COMPILE_NOT_POPPED(predicate);
if (in_single_pattern) {
LABEL *match_succeeded_label = NEW_LABEL(location.line);
PUSH_INSN(ret, location, dup);
if (PM_NODE_TYPE_P(node, PM_IF_NODE)) {
PUSH_INSNL(ret, location, branchif, match_succeeded_label);
}
else {
PUSH_INSNL(ret, location, branchunless, match_succeeded_label);
}
{
VALUE operand = rb_fstring_lit("guard clause does not return true");
PUSH_INSN1(ret, location, putobject, operand);
}
PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
PUSH_INSN1(ret, location, putobject, Qfalse);
PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
PUSH_INSN(ret, location, pop);
PUSH_INSN(ret, location, pop);
PUSH_LABEL(ret, match_succeeded_label);
}
if (PM_NODE_TYPE_P(node, PM_IF_NODE)) {
PUSH_INSNL(ret, location, branchunless, unmatched_label);
}
else {
PUSH_INSNL(ret, location, branchif, unmatched_label);
}
PUSH_INSNL(ret, location, jump, matched_label);
break;
}
default:
// If we get here, then we have a node type that should not be in this
// position. This would be a bug in the parser, because a different node
// type should never have been created in this position in the tree.
rb_bug("Unexpected node type in pattern matching expression: %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
break;
}
return COMPILE_OK;
}
#undef PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE
#undef PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING
#undef PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P
#undef PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_MATCHEE
#undef PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_KEY
// Generate a scope node from the given node.
void
pm_scope_node_init(const pm_node_t *node, pm_scope_node_t *scope, pm_scope_node_t *previous)
{
// This is very important, otherwise the scope node could be seen as having
// certain flags set that _should not_ be set.
memset(scope, 0, sizeof(pm_scope_node_t));
scope->base.type = PM_SCOPE_NODE;
scope->base.location.start = node->location.start;
scope->base.location.end = node->location.end;
scope->previous = previous;
scope->ast_node = (pm_node_t *) node;
if (previous) {
scope->parser = previous->parser;
scope->encoding = previous->encoding;
scope->filepath_encoding = previous->filepath_encoding;
scope->constants = previous->constants;
scope->coverage_enabled = previous->coverage_enabled;
scope->script_lines = previous->script_lines;
}
switch (PM_NODE_TYPE(node)) {
case PM_BLOCK_NODE: {
const pm_block_node_t *cast = (const pm_block_node_t *) node;
scope->body = cast->body;
scope->locals = cast->locals;
scope->parameters = cast->parameters;
break;
}
case PM_CLASS_NODE: {
const pm_class_node_t *cast = (const pm_class_node_t *) node;
scope->body = cast->body;
scope->locals = cast->locals;
break;
}
case PM_DEF_NODE: {
const pm_def_node_t *cast = (const pm_def_node_t *) node;
scope->parameters = (pm_node_t *) cast->parameters;
scope->body = cast->body;
scope->locals = cast->locals;
break;
}
case PM_ENSURE_NODE: {
const pm_ensure_node_t *cast = (const pm_ensure_node_t *) node;
scope->body = (pm_node_t *) node;
if (cast->statements != NULL) {
scope->base.location.start = cast->statements->base.location.start;
scope->base.location.end = cast->statements->base.location.end;
}
break;
}
case PM_FOR_NODE: {
const pm_for_node_t *cast = (const pm_for_node_t *) node;
scope->body = (pm_node_t *) cast->statements;
break;
}
case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE: {
RUBY_ASSERT(node->flags & PM_REGULAR_EXPRESSION_FLAGS_ONCE);
scope->body = (pm_node_t *) node;
break;
}
case PM_LAMBDA_NODE: {
const pm_lambda_node_t *cast = (const pm_lambda_node_t *) node;
scope->parameters = cast->parameters;
scope->body = cast->body;
scope->locals = cast->locals;
if (cast->parameters != NULL) {
scope->base.location.start = cast->parameters->location.start;
}
else {
scope->base.location.start = cast->operator_loc.end;
}
break;
}
case PM_MODULE_NODE: {
const pm_module_node_t *cast = (const pm_module_node_t *) node;
scope->body = cast->body;
scope->locals = cast->locals;
break;
}
case PM_POST_EXECUTION_NODE: {
const pm_post_execution_node_t *cast = (const pm_post_execution_node_t *) node;
scope->body = (pm_node_t *) cast->statements;
break;
}
case PM_PROGRAM_NODE: {
const pm_program_node_t *cast = (const pm_program_node_t *) node;
scope->body = (pm_node_t *) cast->statements;
scope->locals = cast->locals;
break;
}
case PM_RESCUE_NODE: {
const pm_rescue_node_t *cast = (const pm_rescue_node_t *) node;
scope->body = (pm_node_t *) cast->statements;
break;
}
case PM_RESCUE_MODIFIER_NODE: {
const pm_rescue_modifier_node_t *cast = (const pm_rescue_modifier_node_t *) node;
scope->body = (pm_node_t *) cast->rescue_expression;
break;
}
case PM_SINGLETON_CLASS_NODE: {
const pm_singleton_class_node_t *cast = (const pm_singleton_class_node_t *) node;
scope->body = cast->body;
scope->locals = cast->locals;
break;
}
case PM_STATEMENTS_NODE: {
const pm_statements_node_t *cast = (const pm_statements_node_t *) node;
scope->body = (pm_node_t *) cast;
break;
}
default:
rb_bug("unreachable");
break;
}
}
void
pm_scope_node_destroy(pm_scope_node_t *scope_node)
{
if (scope_node->index_lookup_table) {
st_free_table(scope_node->index_lookup_table);
}
}
/**
* 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", "invokesuperforward" appears instead of "send".
*/
static void
pm_compile_retry_end_label(rb_iseq_t *iseq, LINK_ANCHOR *const ret, LABEL *retry_end_l)
{
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 (!IS_INSN_ID(iobj, send) && !IS_INSN_ID(iobj, invokesuper) && !IS_INSN_ID(iobj, sendforward) && !IS_INSN_ID(iobj, invokesuperforward)) {
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;
}
}
static const char *
pm_iseq_builtin_function_name(const pm_scope_node_t *scope_node, const pm_node_t *receiver, ID method_id)
{
const char *name = rb_id2name(method_id);
static const char prefix[] = "__builtin_";
const size_t prefix_len = sizeof(prefix) - 1;
if (receiver == NULL) {
if (UNLIKELY(strncmp(prefix, name, prefix_len) == 0)) {
// __builtin_foo
return &name[prefix_len];
}
}
else if (PM_NODE_TYPE_P(receiver, PM_CALL_NODE)) {
if (PM_NODE_FLAG_P(receiver, PM_CALL_NODE_FLAGS_VARIABLE_CALL)) {
const pm_call_node_t *cast = (const pm_call_node_t *) receiver;
if (pm_constant_id_lookup(scope_node, cast->name) == rb_intern_const("__builtin")) {
// __builtin.foo
return name;
}
}
}
else if (PM_NODE_TYPE_P(receiver, PM_CONSTANT_READ_NODE)) {
const pm_constant_read_node_t *cast = (const pm_constant_read_node_t *) receiver;
if (pm_constant_id_lookup(scope_node, cast->name) == rb_intern_const("Primitive")) {
// Primitive.foo
return name;
}
}
return NULL;
}
// Compile Primitive.attr! :leaf, ...
static int
pm_compile_builtin_attr(rb_iseq_t *iseq, const pm_scope_node_t *scope_node, const pm_arguments_node_t *arguments, const pm_node_location_t *node_location)
{
if (arguments == NULL) {
COMPILE_ERROR(iseq, node_location->line, "attr!: no argument");
return COMPILE_NG;
}
const pm_node_t *argument;
PM_NODE_LIST_FOREACH(&arguments->arguments, index, argument) {
if (!PM_NODE_TYPE_P(argument, PM_SYMBOL_NODE)) {
COMPILE_ERROR(iseq, node_location->line, "non symbol argument to attr!: %s", pm_node_type_to_str(PM_NODE_TYPE(argument)));
return COMPILE_NG;
}
VALUE symbol = pm_static_literal_value(iseq, argument, scope_node);
VALUE string = rb_sym2str(symbol);
if (strcmp(RSTRING_PTR(string), "leaf") == 0) {
ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_LEAF;
}
else if (strcmp(RSTRING_PTR(string), "inline_block") == 0) {
ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_INLINE_BLOCK;
}
else if (strcmp(RSTRING_PTR(string), "use_block") == 0) {
iseq_set_use_block(iseq);
}
else if (strcmp(RSTRING_PTR(string), "c_trace") == 0) {
// Let the iseq act like a C method in backtraces
ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_C_TRACE;
}
else {
COMPILE_ERROR(iseq, node_location->line, "unknown argument to attr!: %s", RSTRING_PTR(string));
return COMPILE_NG;
}
}
return COMPILE_OK;
}
static int
pm_compile_builtin_arg(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const pm_scope_node_t *scope_node, const pm_arguments_node_t *arguments, const pm_node_location_t *node_location, int popped)
{
if (arguments == NULL) {
COMPILE_ERROR(iseq, node_location->line, "arg!: no argument");
return COMPILE_NG;
}
if (arguments->arguments.size != 1) {
COMPILE_ERROR(iseq, node_location->line, "arg!: too many argument");
return COMPILE_NG;
}
const pm_node_t *argument = arguments->arguments.nodes[0];
if (!PM_NODE_TYPE_P(argument, PM_SYMBOL_NODE)) {
COMPILE_ERROR(iseq, node_location->line, "non symbol argument to arg!: %s", pm_node_type_to_str(PM_NODE_TYPE(argument)));
return COMPILE_NG;
}
if (!popped) {
ID name = parse_string_symbol(scope_node, ((const pm_symbol_node_t *) argument));
int index = ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->local_table_size - get_local_var_idx(iseq, name);
debugs("id: %s idx: %d\n", rb_id2name(name), index);
PUSH_GETLOCAL(ret, *node_location, index, get_lvar_level(iseq));
}
return COMPILE_OK;
}
static int
pm_compile_builtin_mandatory_only_method(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_call_node_t *call_node, const pm_node_location_t *node_location)
{
const pm_node_t *ast_node = scope_node->ast_node;
if (!PM_NODE_TYPE_P(ast_node, PM_DEF_NODE)) {
rb_bug("mandatory_only?: not in method definition");
return COMPILE_NG;
}
const pm_def_node_t *def_node = (const pm_def_node_t *) ast_node;
const pm_parameters_node_t *parameters_node = def_node->parameters;
if (parameters_node == NULL) {
rb_bug("mandatory_only?: in method definition with no parameters");
return COMPILE_NG;
}
const pm_node_t *body_node = def_node->body;
if (body_node == NULL || !PM_NODE_TYPE_P(body_node, PM_STATEMENTS_NODE) || (((const pm_statements_node_t *) body_node)->body.size != 1) || !PM_NODE_TYPE_P(((const pm_statements_node_t *) body_node)->body.nodes[0], PM_IF_NODE)) {
rb_bug("mandatory_only?: not in method definition with plain statements");
return COMPILE_NG;
}
const pm_if_node_t *if_node = (const pm_if_node_t *) ((const pm_statements_node_t *) body_node)->body.nodes[0];
if (if_node->predicate != ((const pm_node_t *) call_node)) {
rb_bug("mandatory_only?: can't find mandatory node");
return COMPILE_NG;
}
pm_parameters_node_t parameters = {
.base = parameters_node->base,
.requireds = parameters_node->requireds
};
const pm_def_node_t def = {
.base = def_node->base,
.name = def_node->name,
.receiver = def_node->receiver,
.parameters = ¶meters,
.body = (pm_node_t *) if_node->statements,
.locals = {
.ids = def_node->locals.ids,
.size = parameters_node->requireds.size,
.capacity = def_node->locals.capacity
}
};
pm_scope_node_t next_scope_node;
pm_scope_node_init(&def.base, &next_scope_node, scope_node);
int error_state;
const rb_iseq_t *mandatory_only_iseq = pm_iseq_new_with_opt(
&next_scope_node,
rb_iseq_base_label(iseq),
rb_iseq_path(iseq),
rb_iseq_realpath(iseq),
node_location->line,
NULL,
0,
ISEQ_TYPE_METHOD,
ISEQ_COMPILE_DATA(iseq)->option,
&error_state
);
RB_OBJ_WRITE(iseq, &ISEQ_BODY(iseq)->mandatory_only_iseq, (VALUE)mandatory_only_iseq);
if (error_state) {
RUBY_ASSERT(ISEQ_BODY(iseq)->mandatory_only_iseq == NULL);
rb_jump_tag(error_state);
}
pm_scope_node_destroy(&next_scope_node);
return COMPILE_OK;
}
static int
pm_compile_builtin_function_call(rb_iseq_t *iseq, LINK_ANCHOR *const ret, pm_scope_node_t *scope_node, const pm_call_node_t *call_node, const pm_node_location_t *node_location, int popped, const rb_iseq_t *parent_block, const char *builtin_func)
{
const pm_arguments_node_t *arguments = call_node->arguments;
if (parent_block != NULL) {
COMPILE_ERROR(iseq, node_location->line, "should not call builtins here.");
return COMPILE_NG;
}
#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
return COMPILE_OK;
}
else if (strcmp("attr!", builtin_func) == 0) {
return pm_compile_builtin_attr(iseq, scope_node, arguments, node_location);
}
else if (strcmp("arg!", builtin_func) == 0) {
return pm_compile_builtin_arg(iseq, ret, scope_node, arguments, node_location, 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");
}
PUSH_INSN1(ret, *node_location, putobject, Qfalse);
return pm_compile_builtin_mandatory_only_method(iseq, scope_node, call_node, node_location);
}
else if (1) {
rb_bug("can't find builtin function:%s", builtin_func);
}
else {
COMPILE_ERROR(iseq, node_location->line, "can't find builtin function:%s", builtin_func);
return COMPILE_NG;
}
int inline_index = node_location->line;
snprintf(inline_func, sizeof(inline_func), BUILTIN_INLINE_PREFIX "%d", inline_index);
builtin_func = inline_func;
arguments = NULL;
goto retry;
}
if (cconst) {
typedef VALUE(*builtin_func0)(void *, VALUE);
VALUE const_val = (*(builtin_func0)(uintptr_t)bf->func_ptr)(NULL, Qnil);
PUSH_INSN1(ret, *node_location, putobject, const_val);
return COMPILE_OK;
}
// fprintf(stderr, "func_name:%s -> %p\n", builtin_func, bf->func_ptr);
DECL_ANCHOR(args_seq);
int flags = 0;
struct rb_callinfo_kwarg *keywords = NULL;
int argc = pm_setup_args(arguments, call_node->block, &flags, &keywords, iseq, args_seq, scope_node, node_location);
if (argc != bf->argc) {
COMPILE_ERROR(iseq, node_location->line, "argc is not match for builtin function:%s (expect %d but %d)", builtin_func, bf->argc, argc);
return COMPILE_NG;
}
unsigned int start_index;
if (delegate_call_p(iseq, argc, args_seq, &start_index)) {
PUSH_INSN2(ret, *node_location, opt_invokebuiltin_delegate, bf, INT2FIX(start_index));
}
else {
PUSH_SEQ(ret, args_seq);
PUSH_INSN1(ret, *node_location, invokebuiltin, bf);
}
if (popped) PUSH_INSN(ret, *node_location, pop);
return COMPILE_OK;
}
/**
* Compile a call node into the given iseq.
*/
static void
pm_compile_call(rb_iseq_t *iseq, const pm_call_node_t *call_node, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node, ID method_id, LABEL *start)
{
const pm_location_t *message_loc = &call_node->message_loc;
if (message_loc->start == NULL) message_loc = &call_node->base.location;
const pm_node_location_t location = PM_LOCATION_START_LOCATION(scope_node->parser, message_loc, call_node->base.node_id);
LABEL *else_label = NEW_LABEL(location.line);
LABEL *end_label = NEW_LABEL(location.line);
LABEL *retry_end_l = NEW_LABEL(location.line);
VALUE branches = Qfalse;
rb_code_location_t code_location = { 0 };
int node_id = location.node_id;
if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION)) {
if (PM_BRANCH_COVERAGE_P(iseq)) {
const uint8_t *cursors[3] = {
call_node->closing_loc.end,
call_node->arguments == NULL ? NULL : call_node->arguments->base.location.end,
call_node->message_loc.end
};
const uint8_t *end_cursor = cursors[0];
end_cursor = (end_cursor == NULL || cursors[1] == NULL) ? cursors[1] : (end_cursor > cursors[1] ? end_cursor : cursors[1]);
end_cursor = (end_cursor == NULL || cursors[2] == NULL) ? cursors[2] : (end_cursor > cursors[2] ? end_cursor : cursors[2]);
if (!end_cursor) end_cursor = call_node->closing_loc.end;
const pm_line_column_t start_location = PM_NODE_START_LINE_COLUMN(scope_node->parser, call_node);
const pm_line_column_t end_location = pm_newline_list_line_column(&scope_node->parser->newline_list, end_cursor, scope_node->parser->start_line);
code_location = (rb_code_location_t) {
.beg_pos = { .lineno = start_location.line, .column = start_location.column },
.end_pos = { .lineno = end_location.line, .column = end_location.column }
};
branches = decl_branch_base(iseq, PTR2NUM(call_node), &code_location, "&.");
}
PUSH_INSN(ret, location, dup);
PUSH_INSNL(ret, location, branchnil, else_label);
add_trace_branch_coverage(iseq, ret, &code_location, node_id, 0, "then", branches);
}
int flags = 0;
struct rb_callinfo_kwarg *kw_arg = NULL;
int orig_argc = pm_setup_args(call_node->arguments, call_node->block, &flags, &kw_arg, iseq, ret, scope_node, &location);
const rb_iseq_t *previous_block = ISEQ_COMPILE_DATA(iseq)->current_block;
const rb_iseq_t *block_iseq = NULL;
if (call_node->block != NULL && PM_NODE_TYPE_P(call_node->block, PM_BLOCK_NODE)) {
// Scope associated with the block
pm_scope_node_t next_scope_node;
pm_scope_node_init(call_node->block, &next_scope_node, scope_node);
block_iseq = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, pm_node_line_number(scope_node->parser, call_node->block));
pm_scope_node_destroy(&next_scope_node);
ISEQ_COMPILE_DATA(iseq)->current_block = block_iseq;
}
else {
if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_VARIABLE_CALL)) {
flags |= VM_CALL_VCALL;
}
if (!flags) {
flags |= VM_CALL_ARGS_SIMPLE;
}
}
if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_IGNORE_VISIBILITY)) {
flags |= VM_CALL_FCALL;
}
if (!popped && PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_ATTRIBUTE_WRITE)) {
if (flags & VM_CALL_ARGS_BLOCKARG) {
PUSH_INSN1(ret, location, topn, INT2FIX(1));
if (flags & VM_CALL_ARGS_SPLAT) {
PUSH_INSN1(ret, location, putobject, INT2FIX(-1));
PUSH_SEND_WITH_FLAG(ret, location, idAREF, INT2FIX(1), INT2FIX(0));
}
PUSH_INSN1(ret, location, setn, INT2FIX(orig_argc + 3));
PUSH_INSN(ret, location, pop);
}
else if (flags & VM_CALL_ARGS_SPLAT) {
PUSH_INSN(ret, location, dup);
PUSH_INSN1(ret, location, putobject, INT2FIX(-1));
PUSH_SEND_WITH_FLAG(ret, location, idAREF, INT2FIX(1), INT2FIX(0));
PUSH_INSN1(ret, location, setn, INT2FIX(orig_argc + 2));
PUSH_INSN(ret, location, pop);
}
else {
PUSH_INSN1(ret, location, setn, INT2FIX(orig_argc + 1));
}
}
if ((flags & VM_CALL_KW_SPLAT) && (flags & VM_CALL_ARGS_BLOCKARG) && !(flags & VM_CALL_KW_SPLAT_MUT)) {
PUSH_INSN(ret, location, splatkw);
}
PUSH_SEND_R(ret, location, method_id, INT2FIX(orig_argc), block_iseq, INT2FIX(flags), kw_arg);
if (block_iseq && ISEQ_BODY(block_iseq)->catch_table) {
pm_compile_retry_end_label(iseq, ret, retry_end_l);
PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, start, retry_end_l, block_iseq, retry_end_l);
}
if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION)) {
PUSH_INSNL(ret, location, jump, end_label);
PUSH_LABEL(ret, else_label);
add_trace_branch_coverage(iseq, ret, &code_location, node_id, 1, "else", branches);
PUSH_LABEL(ret, end_label);
}
if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_ATTRIBUTE_WRITE) && !popped) {
PUSH_INSN(ret, location, pop);
}
if (popped) PUSH_INSN(ret, location, pop);
ISEQ_COMPILE_DATA(iseq)->current_block = previous_block;
}
/**
* Compile and return the VALUE associated with the given back reference read
* node.
*/
static inline VALUE
pm_compile_back_reference_ref(const pm_back_reference_read_node_t *node)
{
const char *type = (const char *) (node->base.location.start + 1);
// Since a back reference is `$<char>`, Ruby represents the ID as an
// rb_intern on the value after the `$`.
return INT2FIX(rb_intern2(type, 1)) << 1 | 1;
}
/**
* Compile and return the VALUE associated with the given numbered reference
* read node.
*/
static inline VALUE
pm_compile_numbered_reference_ref(const pm_numbered_reference_read_node_t *node)
{
return INT2FIX(node->number << 1);
}
static void
pm_compile_defined_expr0(rb_iseq_t *iseq, const pm_node_t *node, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node, bool in_condition, LABEL **lfinish, bool explicit_receiver)
{
#define PUSH_VAL(type) (in_condition ? Qtrue : rb_iseq_defined_string(type))
// in_condition is the same as compile.c's needstr
enum defined_type dtype = DEFINED_NOT_DEFINED;
const pm_node_location_t location = *node_location;
switch (PM_NODE_TYPE(node)) {
/* DEFINED_NIL ****************************************************************/
case PM_NIL_NODE:
// defined?(nil)
// ^^^
dtype = DEFINED_NIL;
break;
/* DEFINED_IVAR ***************************************************************/
case PM_INSTANCE_VARIABLE_READ_NODE: {
// defined?(@a)
// ^^
const pm_instance_variable_read_node_t *cast = (const pm_instance_variable_read_node_t *) node;
ID name = pm_constant_id_lookup(scope_node, cast->name);
PUSH_INSN3(ret, location, definedivar, ID2SYM(name), get_ivar_ic_value(iseq, name), PUSH_VAL(DEFINED_IVAR));
return;
}
/* DEFINED_LVAR ***************************************************************/
case PM_LOCAL_VARIABLE_READ_NODE:
// a = 1; defined?(a)
// ^
case PM_IT_LOCAL_VARIABLE_READ_NODE:
// 1.then { defined?(it) }
// ^^
dtype = DEFINED_LVAR;
break;
/* DEFINED_GVAR ***************************************************************/
case PM_GLOBAL_VARIABLE_READ_NODE: {
// defined?($a)
// ^^
const pm_global_variable_read_node_t *cast = (const pm_global_variable_read_node_t *) node;
ID name = pm_constant_id_lookup(scope_node, cast->name);
PUSH_INSN(ret, location, putnil);
PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_GVAR), ID2SYM(name), PUSH_VAL(DEFINED_GVAR));
return;
}
/* DEFINED_CVAR ***************************************************************/
case PM_CLASS_VARIABLE_READ_NODE: {
// defined?(@@a)
// ^^^
const pm_class_variable_read_node_t *cast = (const pm_class_variable_read_node_t *) node;
ID name = pm_constant_id_lookup(scope_node, cast->name);
PUSH_INSN(ret, location, putnil);
PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_CVAR), ID2SYM(name), PUSH_VAL(DEFINED_CVAR));
return;
}
/* DEFINED_CONST **************************************************************/
case PM_CONSTANT_READ_NODE: {
// defined?(A)
// ^
const pm_constant_read_node_t *cast = (const pm_constant_read_node_t *) node;
ID name = pm_constant_id_lookup(scope_node, cast->name);
PUSH_INSN(ret, location, putnil);
PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_CONST), ID2SYM(name), PUSH_VAL(DEFINED_CONST));
return;
}
/* DEFINED_YIELD **************************************************************/
case PM_YIELD_NODE:
// defined?(yield)
// ^^^^^
iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
PUSH_INSN(ret, location, putnil);
PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_YIELD), 0, PUSH_VAL(DEFINED_YIELD));
return;
/* DEFINED_ZSUPER *************************************************************/
case PM_SUPER_NODE: {
// defined?(super 1, 2)
// ^^^^^^^^^^
const pm_super_node_t *cast = (const pm_super_node_t *) node;
if (cast->block != NULL && !PM_NODE_TYPE_P(cast->block, PM_BLOCK_ARGUMENT_NODE)) {
dtype = DEFINED_EXPR;
break;
}
PUSH_INSN(ret, location, putnil);
PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_ZSUPER), 0, PUSH_VAL(DEFINED_ZSUPER));
return;
}
case PM_FORWARDING_SUPER_NODE: {
// defined?(super)
// ^^^^^
const pm_forwarding_super_node_t *cast = (const pm_forwarding_super_node_t *) node;
if (cast->block != NULL) {
dtype = DEFINED_EXPR;
break;
}
PUSH_INSN(ret, location, putnil);
PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_ZSUPER), 0, PUSH_VAL(DEFINED_ZSUPER));
return;
}
/* DEFINED_SELF ***************************************************************/
case PM_SELF_NODE:
// defined?(self)
// ^^^^
dtype = DEFINED_SELF;
break;
/* DEFINED_TRUE ***************************************************************/
case PM_TRUE_NODE:
// defined?(true)
// ^^^^
dtype = DEFINED_TRUE;
break;
/* DEFINED_FALSE **************************************************************/
case PM_FALSE_NODE:
// defined?(false)
// ^^^^^
dtype = DEFINED_FALSE;
break;
/* DEFINED_ASGN ***************************************************************/
case PM_CALL_AND_WRITE_NODE:
// defined?(a.a &&= 1)
// ^^^^^^^^^
case PM_CALL_OPERATOR_WRITE_NODE:
// defined?(a.a += 1)
// ^^^^^^^^
case PM_CALL_OR_WRITE_NODE:
// defined?(a.a ||= 1)
// ^^^^^^^^^
case PM_CLASS_VARIABLE_AND_WRITE_NODE:
// defined?(@@a &&= 1)
// ^^^^^^^^^
case PM_CLASS_VARIABLE_OPERATOR_WRITE_NODE:
// defined?(@@a += 1)
// ^^^^^^^^
case PM_CLASS_VARIABLE_OR_WRITE_NODE:
// defined?(@@a ||= 1)
// ^^^^^^^^^
case PM_CLASS_VARIABLE_WRITE_NODE:
// defined?(@@a = 1)
// ^^^^^^^
case PM_CONSTANT_AND_WRITE_NODE:
// defined?(A &&= 1)
// ^^^^^^^
case PM_CONSTANT_OPERATOR_WRITE_NODE:
// defined?(A += 1)
// ^^^^^^
case PM_CONSTANT_OR_WRITE_NODE:
// defined?(A ||= 1)
// ^^^^^^^
case PM_CONSTANT_PATH_AND_WRITE_NODE:
// defined?(A::A &&= 1)
// ^^^^^^^^^^
case PM_CONSTANT_PATH_OPERATOR_WRITE_NODE:
// defined?(A::A += 1)
// ^^^^^^^^^
case PM_CONSTANT_PATH_OR_WRITE_NODE:
// defined?(A::A ||= 1)
// ^^^^^^^^^^
case PM_CONSTANT_PATH_WRITE_NODE:
// defined?(A::A = 1)
// ^^^^^^^^
case PM_CONSTANT_WRITE_NODE:
// defined?(A = 1)
// ^^^^^
case PM_GLOBAL_VARIABLE_AND_WRITE_NODE:
// defined?($a &&= 1)
// ^^^^^^^^
case PM_GLOBAL_VARIABLE_OPERATOR_WRITE_NODE:
// defined?($a += 1)
// ^^^^^^^
case PM_GLOBAL_VARIABLE_OR_WRITE_NODE:
// defined?($a ||= 1)
// ^^^^^^^^
case PM_GLOBAL_VARIABLE_WRITE_NODE:
// defined?($a = 1)
// ^^^^^^
case PM_INDEX_AND_WRITE_NODE:
// defined?(a[1] &&= 1)
// ^^^^^^^^^^
case PM_INDEX_OPERATOR_WRITE_NODE:
// defined?(a[1] += 1)
// ^^^^^^^^^
case PM_INDEX_OR_WRITE_NODE:
// defined?(a[1] ||= 1)
// ^^^^^^^^^^
case PM_INSTANCE_VARIABLE_AND_WRITE_NODE:
// defined?(@a &&= 1)
// ^^^^^^^^
case PM_INSTANCE_VARIABLE_OPERATOR_WRITE_NODE:
// defined?(@a += 1)
// ^^^^^^^
case PM_INSTANCE_VARIABLE_OR_WRITE_NODE:
// defined?(@a ||= 1)
// ^^^^^^^^
case PM_INSTANCE_VARIABLE_WRITE_NODE:
// defined?(@a = 1)
// ^^^^^^
case PM_LOCAL_VARIABLE_AND_WRITE_NODE:
// defined?(a &&= 1)
// ^^^^^^^
case PM_LOCAL_VARIABLE_OPERATOR_WRITE_NODE:
// defined?(a += 1)
// ^^^^^^
case PM_LOCAL_VARIABLE_OR_WRITE_NODE:
// defined?(a ||= 1)
// ^^^^^^^
case PM_LOCAL_VARIABLE_WRITE_NODE:
// defined?(a = 1)
// ^^^^^
case PM_MULTI_WRITE_NODE:
// defined?((a, = 1))
// ^^^^^^
dtype = DEFINED_ASGN;
break;
/* DEFINED_EXPR ***************************************************************/
case PM_ALIAS_GLOBAL_VARIABLE_NODE:
// defined?((alias $a $b))
// ^^^^^^^^^^^
case PM_ALIAS_METHOD_NODE:
// defined?((alias a b))
// ^^^^^^^^^
case PM_AND_NODE:
// defined?(a and b)
// ^^^^^^^
case PM_BREAK_NODE:
// defined?(break 1)
// ^^^^^^^
case PM_CASE_MATCH_NODE:
// defined?(case 1; in 1; end)
// ^^^^^^^^^^^^^^^^^
case PM_CASE_NODE:
// defined?(case 1; when 1; end)
// ^^^^^^^^^^^^^^^^^^^
case PM_CLASS_NODE:
// defined?(class Foo; end)
// ^^^^^^^^^^^^^^
case PM_DEF_NODE:
// defined?(def a() end)
// ^^^^^^^^^^^
case PM_DEFINED_NODE:
// defined?(defined?(a))
// ^^^^^^^^^^^
case PM_FLIP_FLOP_NODE:
// defined?(not (a .. b))
// ^^^^^^
case PM_FLOAT_NODE:
// defined?(1.0)
// ^^^
case PM_FOR_NODE:
// defined?(for a in 1 do end)
// ^^^^^^^^^^^^^^^^^
case PM_IF_NODE:
// defined?(if a then end)
// ^^^^^^^^^^^^^
case PM_IMAGINARY_NODE:
// defined?(1i)
// ^^
case PM_INTEGER_NODE:
// defined?(1)
// ^
case PM_INTERPOLATED_MATCH_LAST_LINE_NODE:
// defined?(not /#{1}/)
// ^^^^^^
case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE:
// defined?(/#{1}/)
// ^^^^^^
case PM_INTERPOLATED_STRING_NODE:
// defined?("#{1}")
// ^^^^^^
case PM_INTERPOLATED_SYMBOL_NODE:
// defined?(:"#{1}")
// ^^^^^^^
case PM_INTERPOLATED_X_STRING_NODE:
// defined?(`#{1}`)
// ^^^^^^
case PM_LAMBDA_NODE:
// defined?(-> {})
// ^^^^^
case PM_MATCH_LAST_LINE_NODE:
// defined?(not //)
// ^^^^^^
case PM_MATCH_PREDICATE_NODE:
// defined?(1 in 1)
// ^^^^^^
case PM_MATCH_REQUIRED_NODE:
// defined?(1 => 1)
// ^^^^^^
case PM_MATCH_WRITE_NODE:
// defined?(/(?<a>)/ =~ "")
// ^^^^^^^^^^^^^^
case PM_MODULE_NODE:
// defined?(module A end)
// ^^^^^^^^^^^^
case PM_NEXT_NODE:
// defined?(next 1)
// ^^^^^^
case PM_OR_NODE:
// defined?(a or b)
// ^^^^^^
case PM_POST_EXECUTION_NODE:
// defined?((END {}))
// ^^^^^^^^
case PM_RANGE_NODE:
// defined?(1..1)
// ^^^^
case PM_RATIONAL_NODE:
// defined?(1r)
// ^^
case PM_REDO_NODE:
// defined?(redo)
// ^^^^
case PM_REGULAR_EXPRESSION_NODE:
// defined?(//)
// ^^
case PM_RESCUE_MODIFIER_NODE:
// defined?(a rescue b)
// ^^^^^^^^^^
case PM_RETRY_NODE:
// defined?(retry)
// ^^^^^
case PM_RETURN_NODE:
// defined?(return)
// ^^^^^^
case PM_SINGLETON_CLASS_NODE:
// defined?(class << self; end)
// ^^^^^^^^^^^^^^^^^^
case PM_SOURCE_ENCODING_NODE:
// defined?(__ENCODING__)
// ^^^^^^^^^^^^
case PM_SOURCE_FILE_NODE:
// defined?(__FILE__)
// ^^^^^^^^
case PM_SOURCE_LINE_NODE:
// defined?(__LINE__)
// ^^^^^^^^
case PM_STRING_NODE:
// defined?("")
// ^^
case PM_SYMBOL_NODE:
// defined?(:a)
// ^^
case PM_UNDEF_NODE:
// defined?((undef a))
// ^^^^^^^
case PM_UNLESS_NODE:
// defined?(unless a then end)
// ^^^^^^^^^^^^^^^^^
case PM_UNTIL_NODE:
// defined?(until a do end)
// ^^^^^^^^^^^^^^
case PM_WHILE_NODE:
// defined?(while a do end)
// ^^^^^^^^^^^^^^
case PM_X_STRING_NODE:
// defined?(``)
// ^^
dtype = DEFINED_EXPR;
break;
/* DEFINED_REF ****************************************************************/
case PM_BACK_REFERENCE_READ_NODE: {
// defined?($+)
// ^^
const pm_back_reference_read_node_t *cast = (const pm_back_reference_read_node_t *) node;
VALUE ref = pm_compile_back_reference_ref(cast);
PUSH_INSN(ret, location, putnil);
PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_REF), ref, PUSH_VAL(DEFINED_GVAR));
return;
}
case PM_NUMBERED_REFERENCE_READ_NODE: {
// defined?($1)
// ^^
const pm_numbered_reference_read_node_t *cast = (const pm_numbered_reference_read_node_t *) node;
VALUE ref = pm_compile_numbered_reference_ref(cast);
PUSH_INSN(ret, location, putnil);
PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_REF), ref, PUSH_VAL(DEFINED_GVAR));
return;
}
/* DEFINED_CONST_FROM *********************************************************/
case PM_CONSTANT_PATH_NODE: {
// defined?(A::A)
// ^^^^
const pm_constant_path_node_t *cast = (const pm_constant_path_node_t *) node;
ID name = pm_constant_id_lookup(scope_node, cast->name);
if (cast->parent != NULL) {
if (!lfinish[1]) lfinish[1] = NEW_LABEL(location.line);
pm_compile_defined_expr0(iseq, cast->parent, node_location, ret, popped, scope_node, true, lfinish, false);
PUSH_INSNL(ret, location, branchunless, lfinish[1]);
PM_COMPILE(cast->parent);
}
else {
PUSH_INSN1(ret, location, putobject, rb_cObject);
}
PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_CONST_FROM), ID2SYM(name), PUSH_VAL(DEFINED_CONST));
return;
}
/* Containers *****************************************************************/
case PM_BEGIN_NODE: {
// defined?(begin end)
// ^^^^^^^^^
const pm_begin_node_t *cast = (const pm_begin_node_t *) node;
if (cast->rescue_clause == NULL && cast->ensure_clause == NULL && cast->else_clause == NULL) {
if (cast->statements == NULL) {
// If we have empty statements, then we want to return "nil".
dtype = DEFINED_NIL;
}
else if (cast->statements->body.size == 1) {
// If we have a begin node that is wrapping a single statement
// then we want to recurse down to that statement and compile
// it.
pm_compile_defined_expr0(iseq, cast->statements->body.nodes[0], node_location, ret, popped, scope_node, in_condition, lfinish, false);
return;
}
else {
// Otherwise, we have a begin wrapping multiple statements, in
// which case this is defined as "expression".
dtype = DEFINED_EXPR;
}
} else {
// If we have any of the other clauses besides the main begin/end,
// this is defined as "expression".
dtype = DEFINED_EXPR;
}
break;
}
case PM_PARENTHESES_NODE: {
// defined?(())
// ^^
const pm_parentheses_node_t *cast = (const pm_parentheses_node_t *) node;
if (cast->body == NULL) {
// If we have empty parentheses, then we want to return "nil".
dtype = DEFINED_NIL;
}
else if (PM_NODE_TYPE_P(cast->body, PM_STATEMENTS_NODE) && ((const pm_statements_node_t *) cast->body)->body.size == 1) {
// If we have a parentheses node that is wrapping a single statement
// then we want to recurse down to that statement and compile it.
pm_compile_defined_expr0(iseq, ((const pm_statements_node_t *) cast->body)->body.nodes[0], node_location, ret, popped, scope_node, in_condition, lfinish, explicit_receiver);
return;
}
else {
// Otherwise, we have parentheses wrapping multiple statements, in
// which case this is defined as "expression".
dtype = DEFINED_EXPR;
}
break;
}
case PM_ARRAY_NODE: {
// defined?([])
// ^^
const pm_array_node_t *cast = (const pm_array_node_t *) node;
if (cast->elements.size > 0 && !lfinish[1]) {
lfinish[1] = NEW_LABEL(location.line);
}
for (size_t index = 0; index < cast->elements.size; index++) {
pm_compile_defined_expr0(iseq, cast->elements.nodes[index], node_location, ret, popped, scope_node, true, lfinish, false);
PUSH_INSNL(ret, location, branchunless, lfinish[1]);
}
dtype = DEFINED_EXPR;
break;
}
case PM_HASH_NODE:
// defined?({ a: 1 })
// ^^^^^^^^
case PM_KEYWORD_HASH_NODE: {
// defined?(a(a: 1))
// ^^^^
const pm_node_list_t *elements;
if (PM_NODE_TYPE_P(node, PM_HASH_NODE)) {
elements = &((const pm_hash_node_t *) node)->elements;
}
else {
elements = &((const pm_keyword_hash_node_t *) node)->elements;
}
if (elements->size > 0 && !lfinish[1]) {
lfinish[1] = NEW_LABEL(location.line);
}
for (size_t index = 0; index < elements->size; index++) {
pm_compile_defined_expr0(iseq, elements->nodes[index], node_location, ret, popped, scope_node, true, lfinish, false);
PUSH_INSNL(ret, location, branchunless, lfinish[1]);
}
dtype = DEFINED_EXPR;
break;
}
case PM_ASSOC_NODE: {
// defined?({ a: 1 })
// ^^^^
const pm_assoc_node_t *cast = (const pm_assoc_node_t *) node;
pm_compile_defined_expr0(iseq, cast->key, node_location, ret, popped, scope_node, true, lfinish, false);
PUSH_INSNL(ret, location, branchunless, lfinish[1]);
pm_compile_defined_expr0(iseq, cast->value, node_location, ret, popped, scope_node, true, lfinish, false);
return;
}
case PM_ASSOC_SPLAT_NODE: {
// defined?({ **a })
// ^^^^
const pm_assoc_splat_node_t *cast = (const pm_assoc_splat_node_t *) node;
if (cast->value == NULL) {
dtype = DEFINED_EXPR;
break;
}
pm_compile_defined_expr0(iseq, cast->value, node_location, ret, popped, scope_node, true, lfinish, false);
return;
}
case PM_IMPLICIT_NODE: {
// defined?({ a: })
// ^^
const pm_implicit_node_t *cast = (const pm_implicit_node_t *) node;
pm_compile_defined_expr0(iseq, cast->value, node_location, ret, popped, scope_node, in_condition, lfinish, explicit_receiver);
return;
}
case PM_CALL_NODE: {
#define BLOCK_P(cast) ((cast)->block != NULL && PM_NODE_TYPE_P((cast)->block, PM_BLOCK_NODE))
// defined?(a(1, 2, 3))
// ^^^^^^^^^^
const pm_call_node_t *cast = ((const pm_call_node_t *) node);
if (cast->block != NULL && PM_NODE_TYPE_P(cast->block, PM_BLOCK_NODE)) {
dtype = DEFINED_EXPR;
break;
}
if (cast->receiver || cast->arguments || (cast->block && PM_NODE_TYPE_P(cast->block, PM_BLOCK_ARGUMENT_NODE))) {
if (!lfinish[1]) lfinish[1] = NEW_LABEL(location.line);
if (!lfinish[2]) lfinish[2] = NEW_LABEL(location.line);
}
if (cast->arguments) {
pm_compile_defined_expr0(iseq, (const pm_node_t *) cast->arguments, node_location, ret, popped, scope_node, true, lfinish, false);
PUSH_INSNL(ret, location, branchunless, lfinish[1]);
}
if (cast->block && PM_NODE_TYPE_P(cast->block, PM_BLOCK_ARGUMENT_NODE)) {
pm_compile_defined_expr0(iseq, cast->block, node_location, ret, popped, scope_node, true, lfinish, false);
PUSH_INSNL(ret, location, branchunless, lfinish[1]);
}
if (cast->receiver) {
if (PM_NODE_TYPE_P(cast->receiver, PM_CALL_NODE) && !BLOCK_P((const pm_call_node_t *) cast->receiver)) {
// Special behavior here where we chain calls together. This is
// the only path that sets explicit_receiver to true.
pm_compile_defined_expr0(iseq, cast->receiver, node_location, ret, popped, scope_node, true, lfinish, true);
PUSH_INSNL(ret, location, branchunless, lfinish[2]);
const pm_call_node_t *receiver = (const pm_call_node_t *) cast->receiver;
ID method_id = pm_constant_id_lookup(scope_node, receiver->name);
pm_compile_call(iseq, receiver, ret, popped, scope_node, method_id, NULL);
}
else {
pm_compile_defined_expr0(iseq, cast->receiver, node_location, ret, popped, scope_node, true, lfinish, false);
PUSH_INSNL(ret, location, branchunless, lfinish[1]);
PM_COMPILE(cast->receiver);
}
ID method_id = pm_constant_id_lookup(scope_node, cast->name);
if (explicit_receiver) PUSH_INSN(ret, location, dup);
PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_METHOD), rb_id2sym(method_id), PUSH_VAL(DEFINED_METHOD));
}
else {
ID method_id = pm_constant_id_lookup(scope_node, cast->name);
PUSH_INSN(ret, location, putself);
if (explicit_receiver) PUSH_INSN(ret, location, dup);
PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_FUNC), rb_id2sym(method_id), PUSH_VAL(DEFINED_METHOD));
}
return;
}
case PM_ARGUMENTS_NODE: {
// defined?(a(1, 2, 3))
// ^^^^^^^
const pm_arguments_node_t *cast = (const pm_arguments_node_t *) node;
for (size_t index = 0; index < cast->arguments.size; index++) {
pm_compile_defined_expr0(iseq, cast->arguments.nodes[index], node_location, ret, popped, scope_node, in_condition, lfinish, explicit_receiver);
if (!lfinish[1]) {
lfinish[1] = NEW_LABEL(location.line);
}
PUSH_INSNL(ret, location, branchunless, lfinish[1]);
}
dtype = DEFINED_TRUE;
break;
}
case PM_BLOCK_ARGUMENT_NODE:
// defined?(a(&b))
// ^^
dtype = DEFINED_EXPR;
break;
case PM_FORWARDING_ARGUMENTS_NODE:
// def a(...) = defined?(a(...))
// ^^^
dtype = DEFINED_EXPR;
break;
case PM_SPLAT_NODE: {
// def a(*) = defined?(a(*))
// ^
const pm_splat_node_t *cast = (const pm_splat_node_t *) node;
if (cast->expression == NULL) {
dtype = DEFINED_EXPR;
break;
}
pm_compile_defined_expr0(iseq, cast->expression, node_location, ret, popped, scope_node, in_condition, lfinish, false);
if (!lfinish[1]) lfinish[1] = NEW_LABEL(location.line);
PUSH_INSNL(ret, location, branchunless, lfinish[1]);
dtype = DEFINED_EXPR;
break;
}
case PM_SHAREABLE_CONSTANT_NODE:
// # shareable_constant_value: literal
// defined?(A = 1)
// ^^^^^
pm_compile_defined_expr0(iseq, ((const pm_shareable_constant_node_t *) node)->write, node_location, ret, popped, scope_node, in_condition, lfinish, explicit_receiver);
return;
/* Unreachable (parameters) ***************************************************/
case PM_BLOCK_LOCAL_VARIABLE_NODE:
case PM_BLOCK_PARAMETER_NODE:
case PM_BLOCK_PARAMETERS_NODE:
case PM_FORWARDING_PARAMETER_NODE:
case PM_IMPLICIT_REST_NODE:
case PM_IT_PARAMETERS_NODE:
case PM_PARAMETERS_NODE:
case PM_KEYWORD_REST_PARAMETER_NODE:
case PM_NO_KEYWORDS_PARAMETER_NODE:
case PM_NUMBERED_PARAMETERS_NODE:
case PM_OPTIONAL_KEYWORD_PARAMETER_NODE:
case PM_OPTIONAL_PARAMETER_NODE:
case PM_REQUIRED_KEYWORD_PARAMETER_NODE:
case PM_REQUIRED_PARAMETER_NODE:
case PM_REST_PARAMETER_NODE:
/* Unreachable (pattern matching) *********************************************/
case PM_ALTERNATION_PATTERN_NODE:
case PM_ARRAY_PATTERN_NODE:
case PM_CAPTURE_PATTERN_NODE:
case PM_FIND_PATTERN_NODE:
case PM_HASH_PATTERN_NODE:
case PM_PINNED_EXPRESSION_NODE:
case PM_PINNED_VARIABLE_NODE:
/* Unreachable (indirect writes) **********************************************/
case PM_CALL_TARGET_NODE:
case PM_CLASS_VARIABLE_TARGET_NODE:
case PM_CONSTANT_PATH_TARGET_NODE:
case PM_CONSTANT_TARGET_NODE:
case PM_GLOBAL_VARIABLE_TARGET_NODE:
case PM_INDEX_TARGET_NODE:
case PM_INSTANCE_VARIABLE_TARGET_NODE:
case PM_LOCAL_VARIABLE_TARGET_NODE:
case PM_MULTI_TARGET_NODE:
/* Unreachable (clauses) ******************************************************/
case PM_ELSE_NODE:
case PM_ENSURE_NODE:
case PM_IN_NODE:
case PM_RESCUE_NODE:
case PM_WHEN_NODE:
/* Unreachable (miscellaneous) ************************************************/
case PM_BLOCK_NODE:
case PM_EMBEDDED_STATEMENTS_NODE:
case PM_EMBEDDED_VARIABLE_NODE:
case PM_MISSING_NODE:
case PM_PRE_EXECUTION_NODE:
case PM_PROGRAM_NODE:
case PM_SCOPE_NODE:
case PM_STATEMENTS_NODE:
rb_bug("Unreachable node in defined?: %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
}
RUBY_ASSERT(dtype != DEFINED_NOT_DEFINED);
PUSH_INSN1(ret, location, putobject, PUSH_VAL(dtype));
#undef PUSH_VAL
}
static void
pm_defined_expr(rb_iseq_t *iseq, const pm_node_t *node, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node, bool in_condition, LABEL **lfinish, bool explicit_receiver)
{
LINK_ELEMENT *lcur = ret->last;
pm_compile_defined_expr0(iseq, node, node_location, ret, popped, scope_node, in_condition, lfinish, false);
if (lfinish[1]) {
LABEL *lstart = NEW_LABEL(node_location->line);
LABEL *lend = NEW_LABEL(node_location->line);
struct rb_iseq_new_with_callback_callback_func *ifunc =
rb_iseq_new_with_callback_new_callback(build_defined_rescue_iseq, NULL);
const rb_iseq_t *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);
PUSH_LABEL(ret, lend);
PUSH_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue, lfinish[1]);
}
}
static void
pm_compile_defined_expr(rb_iseq_t *iseq, const pm_node_t *node, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node, bool in_condition)
{
LABEL *lfinish[3];
LINK_ELEMENT *last = ret->last;
lfinish[0] = NEW_LABEL(node_location->line);
lfinish[1] = 0;
lfinish[2] = 0;
if (!popped) {
pm_defined_expr(iseq, node, node_location, ret, popped, scope_node, in_condition, lfinish, false);
}
if (lfinish[1]) {
ELEM_INSERT_NEXT(last, &new_insn_body(iseq, node_location->line, node_location->node_id, BIN(putnil), 0)->link);
PUSH_INSN(ret, *node_location, swap);
if (lfinish[2]) PUSH_LABEL(ret, lfinish[2]);
PUSH_INSN(ret, *node_location, pop);
PUSH_LABEL(ret, lfinish[1]);
}
PUSH_LABEL(ret, lfinish[0]);
}
// This is exactly the same as add_ensure_iseq, except it compiled
// the node as a Prism node, and not a CRuby node
static void
pm_add_ensure_iseq(LINK_ANCHOR *const ret, rb_iseq_t *iseq, int is_return, pm_scope_node_t *scope_node)
{
RUBY_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);
while (enlp) {
if (enlp->erange != NULL) {
DECL_ANCHOR(ensure_part);
LABEL *lstart = NEW_LABEL(0);
LABEL *lend = NEW_LABEL(0);
add_ensure_range(iseq, enlp->erange, lstart, lend);
ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enlp->prev;
PUSH_LABEL(ensure_part, lstart);
bool popped = true;
PM_COMPILE_INTO_ANCHOR(ensure_part, (const pm_node_t *) enlp->ensure_node);
PUSH_LABEL(ensure_part, lend);
PUSH_SEQ(ensure, ensure_part);
}
else {
if (!is_return) {
break;
}
}
enlp = enlp->prev;
}
ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = prev_enlp;
PUSH_SEQ(ret, ensure);
}
struct pm_local_table_insert_ctx {
pm_scope_node_t *scope_node;
rb_ast_id_table_t *local_table_for_iseq;
int local_index;
};
static int
pm_local_table_insert_func(st_data_t *key, st_data_t *value, st_data_t arg, int existing)
{
if (!existing) {
pm_constant_id_t constant_id = (pm_constant_id_t) *key;
struct pm_local_table_insert_ctx * ctx = (struct pm_local_table_insert_ctx *) arg;
pm_scope_node_t *scope_node = ctx->scope_node;
rb_ast_id_table_t *local_table_for_iseq = ctx->local_table_for_iseq;
int local_index = ctx->local_index;
ID local = pm_constant_id_lookup(scope_node, constant_id);
local_table_for_iseq->ids[local_index] = local;
*value = (st_data_t)local_index;
ctx->local_index++;
}
return ST_CONTINUE;
}
/**
* Insert a local into the local table for the iseq. This is used to create the
* local table in the correct order while compiling the scope. The locals being
* inserted are regular named locals, as opposed to special forwarding locals.
*/
static void
pm_insert_local_index(pm_constant_id_t constant_id, int local_index, st_table *index_lookup_table, rb_ast_id_table_t *local_table_for_iseq, pm_scope_node_t *scope_node)
{
RUBY_ASSERT((constant_id & PM_SPECIAL_CONSTANT_FLAG) == 0);
ID local = pm_constant_id_lookup(scope_node, constant_id);
local_table_for_iseq->ids[local_index] = local;
st_insert(index_lookup_table, (st_data_t) constant_id, (st_data_t) local_index);
}
/**
* Insert a local into the local table for the iseq that is a special forwarding
* local variable.
*/
static void
pm_insert_local_special(ID local_name, int local_index, st_table *index_lookup_table, rb_ast_id_table_t *local_table_for_iseq)
{
local_table_for_iseq->ids[local_index] = local_name;
st_insert(index_lookup_table, (st_data_t) (local_name | PM_SPECIAL_CONSTANT_FLAG), (st_data_t) local_index);
}
/**
* Compile the locals of a multi target node that is used as a positional
* parameter in a method, block, or lambda definition. Note that this doesn't
* actually add any instructions to the iseq. Instead, it adds locals to the
* local and index lookup tables and increments the local index as necessary.
*/
static int
pm_compile_destructured_param_locals(const pm_multi_target_node_t *node, st_table *index_lookup_table, rb_ast_id_table_t *local_table_for_iseq, pm_scope_node_t *scope_node, int local_index)
{
for (size_t index = 0; index < node->lefts.size; index++) {
const pm_node_t *left = node->lefts.nodes[index];
if (PM_NODE_TYPE_P(left, PM_REQUIRED_PARAMETER_NODE)) {
if (!PM_NODE_FLAG_P(left, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
pm_insert_local_index(((const pm_required_parameter_node_t *) left)->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
local_index++;
}
}
else {
RUBY_ASSERT(PM_NODE_TYPE_P(left, PM_MULTI_TARGET_NODE));
local_index = pm_compile_destructured_param_locals((const pm_multi_target_node_t *) left, index_lookup_table, local_table_for_iseq, scope_node, local_index);
}
}
if (node->rest != NULL && PM_NODE_TYPE_P(node->rest, PM_SPLAT_NODE)) {
const pm_splat_node_t *rest = (const pm_splat_node_t *) node->rest;
if (rest->expression != NULL) {
RUBY_ASSERT(PM_NODE_TYPE_P(rest->expression, PM_REQUIRED_PARAMETER_NODE));
if (!PM_NODE_FLAG_P(rest->expression, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
pm_insert_local_index(((const pm_required_parameter_node_t *) rest->expression)->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
local_index++;
}
}
}
for (size_t index = 0; index < node->rights.size; index++) {
const pm_node_t *right = node->rights.nodes[index];
if (PM_NODE_TYPE_P(right, PM_REQUIRED_PARAMETER_NODE)) {
if (!PM_NODE_FLAG_P(right, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
pm_insert_local_index(((const pm_required_parameter_node_t *) right)->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
local_index++;
}
}
else {
RUBY_ASSERT(PM_NODE_TYPE_P(right, PM_MULTI_TARGET_NODE));
local_index = pm_compile_destructured_param_locals((const pm_multi_target_node_t *) right, index_lookup_table, local_table_for_iseq, scope_node, local_index);
}
}
return local_index;
}
/**
* Compile a required parameter node that is part of a destructure that is used
* as a positional parameter in a method, block, or lambda definition.
*/
static inline void
pm_compile_destructured_param_write(rb_iseq_t *iseq, const pm_required_parameter_node_t *node, LINK_ANCHOR *const ret, const pm_scope_node_t *scope_node)
{
const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, node->name, 0);
PUSH_SETLOCAL(ret, location, index.index, index.level);
}
/**
* Compile a multi target node that is used as a positional parameter in a
* method, block, or lambda definition. Note that this is effectively the same
* as a multi write, but with the added context that all of the targets
* contained in the write are required parameter nodes. With this context, we
* know they won't have any parent expressions so we build a separate code path
* for this simplified case.
*/
static void
pm_compile_destructured_param_writes(rb_iseq_t *iseq, const pm_multi_target_node_t *node, LINK_ANCHOR *const ret, const pm_scope_node_t *scope_node)
{
const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
bool has_rest = (node->rest && PM_NODE_TYPE_P(node->rest, PM_SPLAT_NODE) && (((const pm_splat_node_t *) node->rest)->expression) != NULL);
bool has_rights = node->rights.size > 0;
int flag = (has_rest || has_rights) ? 1 : 0;
PUSH_INSN2(ret, location, expandarray, INT2FIX(node->lefts.size), INT2FIX(flag));
for (size_t index = 0; index < node->lefts.size; index++) {
const pm_node_t *left = node->lefts.nodes[index];
if (PM_NODE_TYPE_P(left, PM_REQUIRED_PARAMETER_NODE)) {
pm_compile_destructured_param_write(iseq, (const pm_required_parameter_node_t *) left, ret, scope_node);
}
else {
RUBY_ASSERT(PM_NODE_TYPE_P(left, PM_MULTI_TARGET_NODE));
pm_compile_destructured_param_writes(iseq, (const pm_multi_target_node_t *) left, ret, scope_node);
}
}
if (has_rest) {
if (has_rights) {
PUSH_INSN2(ret, location, expandarray, INT2FIX(node->rights.size), INT2FIX(3));
}
const pm_node_t *rest = ((const pm_splat_node_t *) node->rest)->expression;
RUBY_ASSERT(PM_NODE_TYPE_P(rest, PM_REQUIRED_PARAMETER_NODE));
pm_compile_destructured_param_write(iseq, (const pm_required_parameter_node_t *) rest, ret, scope_node);
}
if (has_rights) {
if (!has_rest) {
PUSH_INSN2(ret, location, expandarray, INT2FIX(node->rights.size), INT2FIX(2));
}
for (size_t index = 0; index < node->rights.size; index++) {
const pm_node_t *right = node->rights.nodes[index];
if (PM_NODE_TYPE_P(right, PM_REQUIRED_PARAMETER_NODE)) {
pm_compile_destructured_param_write(iseq, (const pm_required_parameter_node_t *) right, ret, scope_node);
}
else {
RUBY_ASSERT(PM_NODE_TYPE_P(right, PM_MULTI_TARGET_NODE));
pm_compile_destructured_param_writes(iseq, (const pm_multi_target_node_t *) right, ret, scope_node);
}
}
}
}
/**
* This is a node in the multi target state linked list. It tracks the
* information for a particular target that necessarily has a parent expression.
*/
typedef struct pm_multi_target_state_node {
// The pointer to the topn instruction that will need to be modified after
// we know the total stack size of all of the targets.
INSN *topn;
// The index of the stack from the base of the entire multi target at which
// the parent expression is located.
size_t stack_index;
// The number of slots in the stack that this node occupies.
size_t stack_size;
// The position of the node in the list of targets.
size_t position;
// A pointer to the next node in this linked list.
struct pm_multi_target_state_node *next;
} pm_multi_target_state_node_t;
/**
* As we're compiling a multi target, we need to track additional information
* whenever there is a parent expression on the left hand side of the target.
* This is because we need to go back and tell the expression where to fetch its
* parent expression from the stack. We use a linked list of nodes to track this
* information.
*/
typedef struct {
// The total number of slots in the stack that this multi target occupies.
size_t stack_size;
// The position of the current node being compiled. This is forwarded to
// nodes when they are allocated.
size_t position;
// A pointer to the head of this linked list.
pm_multi_target_state_node_t *head;
// A pointer to the tail of this linked list.
pm_multi_target_state_node_t *tail;
} pm_multi_target_state_t;
/**
* Push a new state node onto the multi target state.
*/
static void
pm_multi_target_state_push(pm_multi_target_state_t *state, INSN *topn, size_t stack_size)
{
pm_multi_target_state_node_t *node = ALLOC(pm_multi_target_state_node_t);
node->topn = topn;
node->stack_index = state->stack_size + 1;
node->stack_size = stack_size;
node->position = state->position;
node->next = NULL;
if (state->head == NULL) {
state->head = node;
state->tail = node;
}
else {
state->tail->next = node;
state->tail = node;
}
state->stack_size += stack_size;
}
/**
* Walk through a multi target state's linked list and update the topn
* instructions that were inserted into the write sequence to make sure they can
* correctly retrieve their parent expressions.
*/
static void
pm_multi_target_state_update(pm_multi_target_state_t *state)
{
// If nothing was ever pushed onto the stack, then we don't need to do any
// kind of updates.
if (state->stack_size == 0) return;
pm_multi_target_state_node_t *current = state->head;
pm_multi_target_state_node_t *previous;
while (current != NULL) {
VALUE offset = INT2FIX(state->stack_size - current->stack_index + current->position);
current->topn->operands[0] = offset;
// stack_size will be > 1 in the case that we compiled an index target
// and it had arguments. In this case, we use multiple topn instructions
// to grab up all of the arguments as well, so those offsets need to be
// updated as well.
if (current->stack_size > 1) {
INSN *insn = current->topn;
for (size_t index = 1; index < current->stack_size; index += 1) {
LINK_ELEMENT *element = get_next_insn(insn);
RUBY_ASSERT(IS_INSN(element));
insn = (INSN *) element;
RUBY_ASSERT(insn->insn_id == BIN(topn));
insn->operands[0] = offset;
}
}
previous = current;
current = current->next;
xfree(previous);
}
}
static void
pm_compile_multi_target_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const parents, LINK_ANCHOR *const writes, LINK_ANCHOR *const cleanup, pm_scope_node_t *scope_node, pm_multi_target_state_t *state);
/**
* A target node represents an indirect write to a variable or a method call to
* a method ending in =. Compiling one of these nodes requires three sequences:
*
* * The first is to compile retrieving the parent expression if there is one.
* This could be the object that owns a constant or the receiver of a method
* call.
* * The second is to compile the writes to the targets. This could be writing
* to variables, or it could be performing method calls.
* * The third is to compile any cleanup that needs to happen, i.e., popping the
* appropriate number of values off the stack.
*
* When there is a parent expression and this target is part of a multi write, a
* topn instruction will be inserted into the write sequence. This is to move
* the parent expression to the top of the stack so that it can be used as the
* receiver of the method call or the owner of the constant. To facilitate this,
* we return a pointer to the topn instruction that was used to be later
* modified with the correct offset.
*
* These nodes can appear in a couple of places, but most commonly:
*
* * For loops - the index variable is a target node
* * Rescue clauses - the exception reference variable is a target node
* * Multi writes - the left hand side contains a list of target nodes
*
* For the comments with examples within this function, we'll use for loops as
* the containing node.
*/
static void
pm_compile_target_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const parents, LINK_ANCHOR *const writes, LINK_ANCHOR *const cleanup, pm_scope_node_t *scope_node, pm_multi_target_state_t *state)
{
const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
switch (PM_NODE_TYPE(node)) {
case PM_LOCAL_VARIABLE_TARGET_NODE: {
// Local variable targets have no parent expression, so they only need
// to compile the write.
//
// for i in []; end
//
const pm_local_variable_target_node_t *cast = (const pm_local_variable_target_node_t *) node;
pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, cast->name, cast->depth);
PUSH_SETLOCAL(writes, location, index.index, index.level);
break;
}
case PM_CLASS_VARIABLE_TARGET_NODE: {
// Class variable targets have no parent expression, so they only need
// to compile the write.
//
// for @@i in []; end
//
const pm_class_variable_target_node_t *cast = (const pm_class_variable_target_node_t *) node;
ID name = pm_constant_id_lookup(scope_node, cast->name);
VALUE operand = ID2SYM(name);
PUSH_INSN2(writes, location, setclassvariable, operand, get_cvar_ic_value(iseq, name));
break;
}
case PM_CONSTANT_TARGET_NODE: {
// Constant targets have no parent expression, so they only need to
// compile the write.
//
// for I in []; end
//
const pm_constant_target_node_t *cast = (const pm_constant_target_node_t *) node;
ID name = pm_constant_id_lookup(scope_node, cast->name);
VALUE operand = ID2SYM(name);
PUSH_INSN1(writes, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
PUSH_INSN1(writes, location, setconstant, operand);
break;
}
case PM_GLOBAL_VARIABLE_TARGET_NODE: {
// Global variable targets have no parent expression, so they only need
// to compile the write.
//
// for $i in []; end
//
const pm_global_variable_target_node_t *cast = (const pm_global_variable_target_node_t *) node;
ID name = pm_constant_id_lookup(scope_node, cast->name);
VALUE operand = ID2SYM(name);
PUSH_INSN1(writes, location, setglobal, operand);
break;
}
case PM_INSTANCE_VARIABLE_TARGET_NODE: {
// Instance variable targets have no parent expression, so they only
// need to compile the write.
//
// for @i in []; end
//
const pm_instance_variable_target_node_t *cast = (const pm_instance_variable_target_node_t *) node;
ID name = pm_constant_id_lookup(scope_node, cast->name);
VALUE operand = ID2SYM(name);
PUSH_INSN2(writes, location, setinstancevariable, operand, get_ivar_ic_value(iseq, name));
break;
}
case PM_CONSTANT_PATH_TARGET_NODE: {
// Constant path targets have a parent expression that is the object
// that owns the constant. This needs to be compiled first into the
// parents sequence. If no parent is found, then it represents using the
// unary :: operator to indicate a top-level constant. In that case we
// need to push Object onto the stack.
//
// for I::J in []; end
//
const pm_constant_path_target_node_t *cast = (const pm_constant_path_target_node_t *) node;
ID name = pm_constant_id_lookup(scope_node, cast->name);
if (cast->parent != NULL) {
pm_compile_node(iseq, cast->parent, parents, false, scope_node);
}
else {
PUSH_INSN1(parents, location, putobject, rb_cObject);
}
if (state == NULL) {
PUSH_INSN(writes, location, swap);
}
else {
PUSH_INSN1(writes, location, topn, INT2FIX(1));
pm_multi_target_state_push(state, (INSN *) LAST_ELEMENT(writes), 1);
}
VALUE operand = ID2SYM(name);
PUSH_INSN1(writes, location, setconstant, operand);
if (state != NULL) {
PUSH_INSN(cleanup, location, pop);
}
break;
}
case PM_CALL_TARGET_NODE: {
// Call targets have a parent expression that is the receiver of the
// method being called. This needs to be compiled first into the parents
// sequence. These nodes cannot have arguments, so the method call is
// compiled with a single argument which represents the value being
// written.
//
// for i.j in []; end
//
const pm_call_target_node_t *cast = (const pm_call_target_node_t *) node;
ID method_id = pm_constant_id_lookup(scope_node, cast->name);
pm_compile_node(iseq, cast->receiver, parents, false, scope_node);
LABEL *safe_label = NULL;
if (PM_NODE_FLAG_P(cast, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION)) {
safe_label = NEW_LABEL(location.line);
PUSH_INSN(parents, location, dup);
PUSH_INSNL(parents, location, branchnil, safe_label);
}
if (state != NULL) {
PUSH_INSN1(writes, location, topn, INT2FIX(1));
pm_multi_target_state_push(state, (INSN *) LAST_ELEMENT(writes), 1);
PUSH_INSN(writes, location, swap);
}
int flags = VM_CALL_ARGS_SIMPLE;
if (PM_NODE_FLAG_P(cast, PM_CALL_NODE_FLAGS_IGNORE_VISIBILITY)) flags |= VM_CALL_FCALL;
PUSH_SEND_WITH_FLAG(writes, location, method_id, INT2FIX(1), INT2FIX(flags));
if (safe_label != NULL && state == NULL) PUSH_LABEL(writes, safe_label);
PUSH_INSN(writes, location, pop);
if (safe_label != NULL && state != NULL) PUSH_LABEL(writes, safe_label);
if (state != NULL) {
PUSH_INSN(cleanup, location, pop);
}
break;
}
case PM_INDEX_TARGET_NODE: {
// Index targets have a parent expression that is the receiver of the
// method being called and any additional arguments that are being
// passed along with the value being written. The receiver and arguments
// both need to be on the stack. Note that this is even more complicated
// by the fact that these nodes can hold a block using the unary &
// operator.
//
// for i[:j] in []; end
//
const pm_index_target_node_t *cast = (const pm_index_target_node_t *) node;
pm_compile_node(iseq, cast->receiver, parents, false, scope_node);
int flags = 0;
struct rb_callinfo_kwarg *kwargs = NULL;
int argc = pm_setup_args(cast->arguments, (const pm_node_t *) cast->block, &flags, &kwargs, iseq, parents, scope_node, &location);
if (state != NULL) {
PUSH_INSN1(writes, location, topn, INT2FIX(argc + 1));
pm_multi_target_state_push(state, (INSN *) LAST_ELEMENT(writes), argc + 1);
if (argc == 0) {
PUSH_INSN(writes, location, swap);
}
else {
for (int index = 0; index < argc; index++) {
PUSH_INSN1(writes, location, topn, INT2FIX(argc + 1));
}
PUSH_INSN1(writes, location, topn, INT2FIX(argc + 1));
}
}
// The argc that we're going to pass to the send instruction is the
// number of arguments + 1 for the value being written. If there's a
// splat, then we need to insert newarray and concatarray instructions
// after the arguments have been written.
int ci_argc = argc + 1;
if (flags & VM_CALL_ARGS_SPLAT) {
ci_argc--;
PUSH_INSN1(writes, location, newarray, INT2FIX(1));
PUSH_INSN(writes, location, concatarray);
}
PUSH_SEND_R(writes, location, idASET, INT2NUM(ci_argc), NULL, INT2FIX(flags), kwargs);
PUSH_INSN(writes, location, pop);
if (state != NULL) {
if (argc != 0) {
PUSH_INSN(writes, location, pop);
}
for (int index = 0; index < argc + 1; index++) {
PUSH_INSN(cleanup, location, pop);
}
}
break;
}
case PM_MULTI_TARGET_NODE: {
// Multi target nodes represent a set of writes to multiple variables.
// The parent expressions are the combined set of the parent expressions
// of its inner target nodes.
//
// for i, j in []; end
//
size_t before_position;
if (state != NULL) {
before_position = state->position;
state->position--;
}
pm_compile_multi_target_node(iseq, node, parents, writes, cleanup, scope_node, state);
if (state != NULL) state->position = before_position;
break;
}
case PM_SPLAT_NODE: {
// Splat nodes capture all values into an array. They can be used
// as targets in assignments or for loops.
//
// for *x in []; end
//
const pm_splat_node_t *cast = (const pm_splat_node_t *) node;
if (cast->expression != NULL) {
pm_compile_target_node(iseq, cast->expression, parents, writes, cleanup, scope_node, state);
}
break;
}
default:
rb_bug("Unexpected node type: %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
break;
}
}
/**
* Compile a multi target or multi write node. It returns the number of values
* on the stack that correspond to the parent expressions of the various
* targets.
*/
static void
pm_compile_multi_target_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const parents, LINK_ANCHOR *const writes, LINK_ANCHOR *const cleanup, pm_scope_node_t *scope_node, pm_multi_target_state_t *state)
{
const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
const pm_node_list_t *lefts;
const pm_node_t *rest;
const pm_node_list_t *rights;
switch (PM_NODE_TYPE(node)) {
case PM_MULTI_TARGET_NODE: {
const pm_multi_target_node_t *cast = (const pm_multi_target_node_t *) node;
lefts = &cast->lefts;
rest = cast->rest;
rights = &cast->rights;
break;
}
case PM_MULTI_WRITE_NODE: {
const pm_multi_write_node_t *cast = (const pm_multi_write_node_t *) node;
lefts = &cast->lefts;
rest = cast->rest;
rights = &cast->rights;
break;
}
default:
rb_bug("Unsupported node %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
break;
}
bool has_rest = (rest != NULL) && PM_NODE_TYPE_P(rest, PM_SPLAT_NODE) && ((const pm_splat_node_t *) rest)->expression != NULL;
bool has_posts = rights->size > 0;
// The first instruction in the writes sequence is going to spread the
// top value of the stack onto the number of values that we're going to
// write.
PUSH_INSN2(writes, location, expandarray, INT2FIX(lefts->size), INT2FIX((has_rest || has_posts) ? 1 : 0));
// We need to keep track of some additional state information as we're
// going through the targets because we will need to revisit them once
// we know how many values are being pushed onto the stack.
pm_multi_target_state_t target_state = { 0 };
if (state == NULL) state = &target_state;
size_t base_position = state->position;
size_t splat_position = (has_rest || has_posts) ? 1 : 0;
// Next, we'll iterate through all of the leading targets.
for (size_t index = 0; index < lefts->size; index++) {
const pm_node_t *target = lefts->nodes[index];
state->position = lefts->size - index + splat_position + base_position;
pm_compile_target_node(iseq, target, parents, writes, cleanup, scope_node, state);
}
// Next, we'll compile the rest target if there is one.
if (has_rest) {
const pm_node_t *target = ((const pm_splat_node_t *) rest)->expression;
state->position = 1 + rights->size + base_position;
if (has_posts) {
PUSH_INSN2(writes, location, expandarray, INT2FIX(rights->size), INT2FIX(3));
}
pm_compile_target_node(iseq, target, parents, writes, cleanup, scope_node, state);
}
// Finally, we'll compile the trailing targets.
if (has_posts) {
if (!has_rest && rest != NULL) {
PUSH_INSN2(writes, location, expandarray, INT2FIX(rights->size), INT2FIX(2));
}
for (size_t index = 0; index < rights->size; index++) {
const pm_node_t *target = rights->nodes[index];
state->position = rights->size - index + base_position;
pm_compile_target_node(iseq, target, parents, writes, cleanup, scope_node, state);
}
}
}
/**
* When compiling a for loop, we need to write the iteration variable to
* whatever expression exists in the index slot. This function performs that
* compilation.
*/
static void
pm_compile_for_node_index(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, pm_scope_node_t *scope_node)
{
const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
switch (PM_NODE_TYPE(node)) {
case PM_LOCAL_VARIABLE_TARGET_NODE: {
// For local variables, all we have to do is retrieve the value and then
// compile the index node.
PUSH_GETLOCAL(ret, location, 1, 0);
pm_compile_target_node(iseq, node, ret, ret, ret, scope_node, NULL);
break;
}
case PM_CLASS_VARIABLE_TARGET_NODE:
case PM_CONSTANT_TARGET_NODE:
case PM_GLOBAL_VARIABLE_TARGET_NODE:
case PM_INSTANCE_VARIABLE_TARGET_NODE:
case PM_CONSTANT_PATH_TARGET_NODE:
case PM_CALL_TARGET_NODE:
case PM_INDEX_TARGET_NODE: {
// For other targets, we need to potentially compile the parent or
// owning expression of this target, then retrieve the value, expand it,
// and then compile the necessary writes.
DECL_ANCHOR(writes);
DECL_ANCHOR(cleanup);
pm_multi_target_state_t state = { 0 };
state.position = 1;
pm_compile_target_node(iseq, node, ret, writes, cleanup, scope_node, &state);
PUSH_GETLOCAL(ret, location, 1, 0);
PUSH_INSN2(ret, location, expandarray, INT2FIX(1), INT2FIX(0));
PUSH_SEQ(ret, writes);
PUSH_SEQ(ret, cleanup);
pm_multi_target_state_update(&state);
break;
}
case PM_SPLAT_NODE:
case PM_MULTI_TARGET_NODE: {
DECL_ANCHOR(writes);
DECL_ANCHOR(cleanup);
pm_compile_target_node(iseq, node, ret, writes, cleanup, scope_node, NULL);
LABEL *not_single = NEW_LABEL(location.line);
LABEL *not_ary = NEW_LABEL(location.line);
// When there are multiple targets, we'll do a bunch of work to convert
// the value into an array before we expand it. Effectively we're trying
// to accomplish:
//
// (args.length == 1 && Array.try_convert(args[0])) || args
//
PUSH_GETLOCAL(ret, location, 1, 0);
PUSH_INSN(ret, location, dup);
PUSH_CALL(ret, location, idLength, INT2FIX(0));
PUSH_INSN1(ret, location, putobject, INT2FIX(1));
PUSH_CALL(ret, location, idEq, INT2FIX(1));
PUSH_INSNL(ret, location, branchunless, not_single);
PUSH_INSN(ret, location, dup);
PUSH_INSN1(ret, location, putobject, INT2FIX(0));
PUSH_CALL(ret, location, idAREF, INT2FIX(1));
PUSH_INSN1(ret, location, putobject, rb_cArray);
PUSH_INSN(ret, location, swap);
PUSH_CALL(ret, location, rb_intern("try_convert"), INT2FIX(1));
PUSH_INSN(ret, location, dup);
PUSH_INSNL(ret, location, branchunless, not_ary);
PUSH_INSN(ret, location, swap);
PUSH_LABEL(ret, not_ary);
PUSH_INSN(ret, location, pop);
PUSH_LABEL(ret, not_single);
if (PM_NODE_TYPE_P(node, PM_SPLAT_NODE)) {
const pm_splat_node_t *cast = (const pm_splat_node_t *) node;
PUSH_INSN2(ret, location, expandarray, INT2FIX(0), INT2FIX(cast->expression == NULL ? 0 : 1));
}
PUSH_SEQ(ret, writes);
PUSH_SEQ(ret, cleanup);
break;
}
default:
rb_bug("Unexpected node type for index in for node: %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
break;
}
}
static void
pm_compile_rescue(rb_iseq_t *iseq, const pm_begin_node_t *cast, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
{
const pm_parser_t *parser = scope_node->parser;
LABEL *lstart = NEW_LABEL(node_location->line);
LABEL *lend = NEW_LABEL(node_location->line);
LABEL *lcont = NEW_LABEL(node_location->line);
pm_scope_node_t rescue_scope_node;
pm_scope_node_init((const pm_node_t *) cast->rescue_clause, &rescue_scope_node, scope_node);
rb_iseq_t *rescue_iseq = NEW_CHILD_ISEQ(
&rescue_scope_node,
rb_str_concat(rb_str_new2("rescue in "), ISEQ_BODY(iseq)->location.label),
ISEQ_TYPE_RESCUE,
pm_node_line_number(parser, (const pm_node_t *) cast->rescue_clause)
);
pm_scope_node_destroy(&rescue_scope_node);
lstart->rescued = LABEL_RESCUE_BEG;
lend->rescued = LABEL_RESCUE_END;
PUSH_LABEL(ret, lstart);
bool prev_in_rescue = ISEQ_COMPILE_DATA(iseq)->in_rescue;
ISEQ_COMPILE_DATA(iseq)->in_rescue = true;
if (cast->statements != NULL) {
PM_COMPILE_NOT_POPPED((const pm_node_t *) cast->statements);
}
else {
const pm_node_location_t location = PM_NODE_START_LOCATION(parser, cast->rescue_clause);
PUSH_INSN(ret, location, putnil);
}
ISEQ_COMPILE_DATA(iseq)->in_rescue = prev_in_rescue;
PUSH_LABEL(ret, lend);
if (cast->else_clause != NULL) {
if (!popped) PUSH_INSN(ret, *node_location, pop);
PM_COMPILE((const pm_node_t *) cast->else_clause);
}
PUSH_INSN(ret, *node_location, nop);
PUSH_LABEL(ret, lcont);
if (popped) PUSH_INSN(ret, *node_location, pop);
PUSH_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue_iseq, lcont);
PUSH_CATCH_ENTRY(CATCH_TYPE_RETRY, lend, lcont, NULL, lstart);
}
static void
pm_compile_ensure(rb_iseq_t *iseq, const pm_begin_node_t *cast, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
{
const pm_parser_t *parser = scope_node->parser;
const pm_statements_node_t *statements = cast->ensure_clause->statements;
pm_node_location_t location;
if (statements != NULL) {
location = PM_NODE_START_LOCATION(parser, statements);
}
else {
location = *node_location;
}
LABEL *lstart = NEW_LABEL(location.line);
LABEL *lend = NEW_LABEL(location.line);
LABEL *lcont = NEW_LABEL(location.line);
struct ensure_range er;
struct iseq_compile_data_ensure_node_stack enl;
struct ensure_range *erange;
DECL_ANCHOR(ensr);
if (statements != NULL) {
pm_compile_node(iseq, (const pm_node_t *) statements, ensr, true, scope_node);
}
LINK_ELEMENT *last = ensr->last;
bool 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, (void *) cast->ensure_clause);
PUSH_LABEL(ret, lstart);
if (cast->rescue_clause != NULL) {
pm_compile_rescue(iseq, cast, node_location, ret, popped | last_leave, scope_node);
}
else if (cast->statements != NULL) {
pm_compile_node(iseq, (const pm_node_t *) cast->statements, ret, popped | last_leave, scope_node);
}
else if (!(popped | last_leave)) {
PUSH_SYNTHETIC_PUTNIL(ret, iseq);
}
PUSH_LABEL(ret, lend);
PUSH_SEQ(ret, ensr);
if (!popped && last_leave) PUSH_INSN(ret, *node_location, putnil);
PUSH_LABEL(ret, lcont);
if (last_leave) PUSH_INSN(ret, *node_location, pop);
pm_scope_node_t next_scope_node;
pm_scope_node_init((const pm_node_t *) cast->ensure_clause, &next_scope_node, scope_node);
rb_iseq_t *child_iseq = NEW_CHILD_ISEQ(
&next_scope_node,
rb_str_concat(rb_str_new2("ensure in "), ISEQ_BODY(iseq)->location.label),
ISEQ_TYPE_ENSURE,
location.line
);
pm_scope_node_destroy(&next_scope_node);
erange = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack->erange;
if (lstart->link.next != &lend->link) {
while (erange) {
PUSH_CATCH_ENTRY(CATCH_TYPE_ENSURE, erange->begin, erange->end, child_iseq, lcont);
erange = erange->next;
}
}
ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enl.prev;
}
/**
* Returns true if the given call node can use the opt_str_uminus or
* opt_str_freeze instructions as an optimization with the current iseq options.
*/
static inline bool
pm_opt_str_freeze_p(const rb_iseq_t *iseq, const pm_call_node_t *node)
{
return (
!PM_NODE_FLAG_P(node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION) &&
node->receiver != NULL &&
PM_NODE_TYPE_P(node->receiver, PM_STRING_NODE) &&
node->arguments == NULL &&
node->block == NULL &&
ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction
);
}
/**
* Returns true if the given call node can use the opt_aref_with optimization
* with the current iseq options.
*/
static inline bool
pm_opt_aref_with_p(const rb_iseq_t *iseq, const pm_call_node_t *node)
{
return (
!PM_NODE_FLAG_P(node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION) &&
node->arguments != NULL &&
PM_NODE_TYPE_P((const pm_node_t *) node->arguments, PM_ARGUMENTS_NODE) &&
((const pm_arguments_node_t *) node->arguments)->arguments.size == 1 &&
PM_NODE_TYPE_P(((const pm_arguments_node_t *) node->arguments)->arguments.nodes[0], PM_STRING_NODE) &&
node->block == NULL &&
!PM_NODE_FLAG_P(((const pm_arguments_node_t *) node->arguments)->arguments.nodes[0], PM_STRING_FLAGS_FROZEN) &&
ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction
);
}
/**
* Returns true if the given call node can use the opt_aset_with optimization
* with the current iseq options.
*/
static inline bool
pm_opt_aset_with_p(const rb_iseq_t *iseq, const pm_call_node_t *node)
{
return (
!PM_NODE_FLAG_P(node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION) &&
node->arguments != NULL &&
PM_NODE_TYPE_P((const pm_node_t *) node->arguments, PM_ARGUMENTS_NODE) &&
((const pm_arguments_node_t *) node->arguments)->arguments.size == 2 &&
PM_NODE_TYPE_P(((const pm_arguments_node_t *) node->arguments)->arguments.nodes[0], PM_STRING_NODE) &&
node->block == NULL &&
!PM_NODE_FLAG_P(((const pm_arguments_node_t *) node->arguments)->arguments.nodes[0], PM_STRING_FLAGS_FROZEN) &&
ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction
);
}
/**
* Compile the instructions necessary to read a constant, based on the options
* of the current iseq.
*/
static void
pm_compile_constant_read(rb_iseq_t *iseq, VALUE name, const pm_location_t *name_loc, uint32_t node_id, LINK_ANCHOR *const ret, const pm_scope_node_t *scope_node)
{
const pm_node_location_t location = PM_LOCATION_START_LOCATION(scope_node->parser, name_loc, node_id);
if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache) {
ISEQ_BODY(iseq)->ic_size++;
VALUE segments = rb_ary_new_from_args(1, name);
PUSH_INSN1(ret, location, opt_getconstant_path, segments);
}
else {
PUSH_INSN(ret, location, putnil);
PUSH_INSN1(ret, location, putobject, Qtrue);
PUSH_INSN1(ret, location, getconstant, name);
}
}
/**
* Returns a Ruby array of the parts of the constant path node if it is constant
* reads all of the way down. If it isn't, then Qnil is returned.
*/
static VALUE
pm_constant_path_parts(const pm_node_t *node, const pm_scope_node_t *scope_node)
{
VALUE parts = rb_ary_new();
while (true) {
switch (PM_NODE_TYPE(node)) {
case PM_CONSTANT_READ_NODE: {
const pm_constant_read_node_t *cast = (const pm_constant_read_node_t *) node;
VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
rb_ary_unshift(parts, name);
return parts;
}
case PM_CONSTANT_PATH_NODE: {
const pm_constant_path_node_t *cast = (const pm_constant_path_node_t *) node;
VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
rb_ary_unshift(parts, name);
if (cast->parent == NULL) {
rb_ary_unshift(parts, ID2SYM(idNULL));
return parts;
}
node = cast->parent;
break;
}
default:
return Qnil;
}
}
}
/**
* Compile a constant path into two sequences of instructions, one for the
* owning expression if there is one (prefix) and one for the constant reads
* (body).
*/
static void
pm_compile_constant_path(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const prefix, LINK_ANCHOR *const body, bool popped, pm_scope_node_t *scope_node)
{
const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
switch (PM_NODE_TYPE(node)) {
case PM_CONSTANT_READ_NODE: {
const pm_constant_read_node_t *cast = (const pm_constant_read_node_t *) node;
VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
PUSH_INSN1(body, location, putobject, Qtrue);
PUSH_INSN1(body, location, getconstant, name);
break;
}
case PM_CONSTANT_PATH_NODE: {
const pm_constant_path_node_t *cast = (const pm_constant_path_node_t *) node;
VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
if (cast->parent == NULL) {
PUSH_INSN(body, location, pop);
PUSH_INSN1(body, location, putobject, rb_cObject);
PUSH_INSN1(body, location, putobject, Qtrue);
PUSH_INSN1(body, location, getconstant, name);
}
else {
pm_compile_constant_path(iseq, cast->parent, prefix, body, false, scope_node);
PUSH_INSN1(body, location, putobject, Qfalse);
PUSH_INSN1(body, location, getconstant, name);
}
break;
}
default:
PM_COMPILE_INTO_ANCHOR(prefix, node);
break;
}
}
/**
* Return the object that will be pushed onto the stack for the given node.
*/
static VALUE
pm_compile_shareable_constant_literal(rb_iseq_t *iseq, const pm_node_t *node, const pm_scope_node_t *scope_node)
{
switch (PM_NODE_TYPE(node)) {
case PM_TRUE_NODE:
case PM_FALSE_NODE:
case PM_NIL_NODE:
case PM_SYMBOL_NODE:
case PM_REGULAR_EXPRESSION_NODE:
case PM_SOURCE_LINE_NODE:
case PM_INTEGER_NODE:
case PM_FLOAT_NODE:
case PM_RATIONAL_NODE:
case PM_IMAGINARY_NODE:
case PM_SOURCE_ENCODING_NODE:
return pm_static_literal_value(iseq, node, scope_node);
case PM_STRING_NODE:
return parse_static_literal_string(iseq, scope_node, node, &((const pm_string_node_t *) node)->unescaped);
case PM_SOURCE_FILE_NODE:
return pm_source_file_value((const pm_source_file_node_t *) node, scope_node);
case PM_ARRAY_NODE: {
const pm_array_node_t *cast = (const pm_array_node_t *) node;
VALUE result = rb_ary_new_capa(cast->elements.size);
for (size_t index = 0; index < cast->elements.size; index++) {
VALUE element = pm_compile_shareable_constant_literal(iseq, cast->elements.nodes[index], scope_node);
if (element == Qundef) return Qundef;
rb_ary_push(result, element);
}
return rb_ractor_make_shareable(result);
}
case PM_HASH_NODE: {
const pm_hash_node_t *cast = (const pm_hash_node_t *) node;
VALUE result = rb_hash_new_capa(cast->elements.size);
for (size_t index = 0; index < cast->elements.size; index++) {
const pm_node_t *element = cast->elements.nodes[index];
if (!PM_NODE_TYPE_P(element, PM_ASSOC_NODE)) return Qundef;
const pm_assoc_node_t *assoc = (const pm_assoc_node_t *) element;
VALUE key = pm_compile_shareable_constant_literal(iseq, assoc->key, scope_node);
if (key == Qundef) return Qundef;
VALUE value = pm_compile_shareable_constant_literal(iseq, assoc->value, scope_node);
if (value == Qundef) return Qundef;
rb_hash_aset(result, key, value);
}
return rb_ractor_make_shareable(result);
}
default:
return Qundef;
}
}
/**
* Compile the instructions for pushing the value that will be written to a
* shared constant.
*/
static void
pm_compile_shareable_constant_value(rb_iseq_t *iseq, const pm_node_t *node, const pm_node_flags_t shareability, VALUE path, LINK_ANCHOR *const ret, pm_scope_node_t *scope_node, bool top)
{
VALUE literal = pm_compile_shareable_constant_literal(iseq, node, scope_node);
if (literal != Qundef) {
const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
PUSH_INSN1(ret, location, putobject, literal);
return;
}
const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
switch (PM_NODE_TYPE(node)) {
case PM_ARRAY_NODE: {
const pm_array_node_t *cast = (const pm_array_node_t *) node;
if (top) {
PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
}
for (size_t index = 0; index < cast->elements.size; index++) {
pm_compile_shareable_constant_value(iseq, cast->elements.nodes[index], shareability, path, ret, scope_node, false);
}
PUSH_INSN1(ret, location, newarray, INT2FIX(cast->elements.size));
if (top) {
ID method_id = (shareability & PM_SHAREABLE_CONSTANT_NODE_FLAGS_EXPERIMENTAL_COPY) ? rb_intern("make_shareable_copy") : rb_intern("make_shareable");
PUSH_SEND_WITH_FLAG(ret, location, method_id, INT2FIX(1), INT2FIX(VM_CALL_ARGS_SIMPLE));
}
return;
}
case PM_HASH_NODE: {
const pm_hash_node_t *cast = (const pm_hash_node_t *) node;
if (top) {
PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
}
pm_compile_hash_elements(iseq, (const pm_node_t *) cast, &cast->elements, shareability, path, false, ret, scope_node);
if (top) {
ID method_id = (shareability & PM_SHAREABLE_CONSTANT_NODE_FLAGS_EXPERIMENTAL_COPY) ? rb_intern("make_shareable_copy") : rb_intern("make_shareable");
PUSH_SEND_WITH_FLAG(ret, location, method_id, INT2FIX(1), INT2FIX(VM_CALL_ARGS_SIMPLE));
}
return;
}
default: {
DECL_ANCHOR(value_seq);
pm_compile_node(iseq, node, value_seq, false, scope_node);
if (PM_NODE_TYPE_P(node, PM_INTERPOLATED_STRING_NODE)) {
PUSH_SEND_WITH_FLAG(value_seq, location, idUMinus, INT2FIX(0), INT2FIX(VM_CALL_ARGS_SIMPLE));
}
if (shareability & PM_SHAREABLE_CONSTANT_NODE_FLAGS_LITERAL) {
PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
PUSH_SEQ(ret, value_seq);
PUSH_INSN1(ret, location, putobject, path);
PUSH_SEND_WITH_FLAG(ret, location, rb_intern("ensure_shareable"), INT2FIX(2), INT2FIX(VM_CALL_ARGS_SIMPLE));
}
else if (shareability & PM_SHAREABLE_CONSTANT_NODE_FLAGS_EXPERIMENTAL_COPY) {
if (top) PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
PUSH_SEQ(ret, value_seq);
if (top) PUSH_SEND_WITH_FLAG(ret, location, rb_intern("make_shareable_copy"), INT2FIX(1), INT2FIX(VM_CALL_ARGS_SIMPLE));
}
else if (shareability & PM_SHAREABLE_CONSTANT_NODE_FLAGS_EXPERIMENTAL_EVERYTHING) {
if (top) PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
PUSH_SEQ(ret, value_seq);
if (top) PUSH_SEND_WITH_FLAG(ret, location, rb_intern("make_shareable"), INT2FIX(1), INT2FIX(VM_CALL_ARGS_SIMPLE));
}
break;
}
}
}
/**
* Compile a constant write node, either in the context of a ractor pragma or
* not.
*/
static void
pm_compile_constant_write_node(rb_iseq_t *iseq, const pm_constant_write_node_t *node, const pm_node_flags_t shareability, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
{
const pm_node_location_t location = *node_location;
ID name_id = pm_constant_id_lookup(scope_node, node->name);
if (shareability != 0) {
pm_compile_shareable_constant_value(iseq, node->value, shareability, rb_id2str(name_id), ret, scope_node, true);
}
else {
PM_COMPILE_NOT_POPPED(node->value);
}
if (!popped) PUSH_INSN(ret, location, dup);
PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
VALUE operand = ID2SYM(name_id);
PUSH_INSN1(ret, location, setconstant, operand);
}
/**
* Compile a constant and write node, either in the context of a ractor pragma
* or not.
*/
static void
pm_compile_constant_and_write_node(rb_iseq_t *iseq, const pm_constant_and_write_node_t *node, const pm_node_flags_t shareability, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
{
const pm_node_location_t location = *node_location;
VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, node->name));
LABEL *end_label = NEW_LABEL(location.line);
pm_compile_constant_read(iseq, name, &node->name_loc, location.node_id, ret, scope_node);
if (!popped) PUSH_INSN(ret, location, dup);
PUSH_INSNL(ret, location, branchunless, end_label);
if (!popped) PUSH_INSN(ret, location, pop);
if (shareability != 0) {
pm_compile_shareable_constant_value(iseq, node->value, shareability, name, ret, scope_node, true);
}
else {
PM_COMPILE_NOT_POPPED(node->value);
}
if (!popped) PUSH_INSN(ret, location, dup);
PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
PUSH_INSN1(ret, location, setconstant, name);
PUSH_LABEL(ret, end_label);
}
/**
* Compile a constant or write node, either in the context of a ractor pragma or
* not.
*/
static void
pm_compile_constant_or_write_node(rb_iseq_t *iseq, const pm_constant_or_write_node_t *node, const pm_node_flags_t shareability, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
{
const pm_node_location_t location = *node_location;
VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, node->name));
LABEL *set_label = NEW_LABEL(location.line);
LABEL *end_label = NEW_LABEL(location.line);
PUSH_INSN(ret, location, putnil);
PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_CONST), name, Qtrue);
PUSH_INSNL(ret, location, branchunless, set_label);
pm_compile_constant_read(iseq, name, &node->name_loc, location.node_id, ret, scope_node);
if (!popped) PUSH_INSN(ret, location, dup);
PUSH_INSNL(ret, location, branchif, end_label);
if (!popped) PUSH_INSN(ret, location, pop);
PUSH_LABEL(ret, set_label);
if (shareability != 0) {
pm_compile_shareable_constant_value(iseq, node->value, shareability, name, ret, scope_node, true);
}
else {
PM_COMPILE_NOT_POPPED(node->value);
}
if (!popped) PUSH_INSN(ret, location, dup);
PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
PUSH_INSN1(ret, location, setconstant, name);
PUSH_LABEL(ret, end_label);
}
/**
* Compile a constant operator write node, either in the context of a ractor
* pragma or not.
*/
static void
pm_compile_constant_operator_write_node(rb_iseq_t *iseq, const pm_constant_operator_write_node_t *node, const pm_node_flags_t shareability, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
{
const pm_node_location_t location = *node_location;
VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, node->name));
ID method_id = pm_constant_id_lookup(scope_node, node->binary_operator);
pm_compile_constant_read(iseq, name, &node->name_loc, location.node_id, ret, scope_node);
if (shareability != 0) {
pm_compile_shareable_constant_value(iseq, node->value, shareability, name, ret, scope_node, true);
}
else {
PM_COMPILE_NOT_POPPED(node->value);
}
PUSH_SEND_WITH_FLAG(ret, location, method_id, INT2NUM(1), INT2FIX(VM_CALL_ARGS_SIMPLE));
if (!popped) PUSH_INSN(ret, location, dup);
PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
PUSH_INSN1(ret, location, setconstant, name);
}
/**
* Creates a string that is used in ractor error messages to describe the
* constant path being written.
*/
static VALUE
pm_constant_path_path(const pm_constant_path_node_t *node, const pm_scope_node_t *scope_node)
{
VALUE parts = rb_ary_new();
rb_ary_push(parts, rb_id2str(pm_constant_id_lookup(scope_node, node->name)));
const pm_node_t *current = node->parent;
while (current != NULL && PM_NODE_TYPE_P(current, PM_CONSTANT_PATH_NODE)) {
const pm_constant_path_node_t *cast = (const pm_constant_path_node_t *) current;
rb_ary_unshift(parts, rb_id2str(pm_constant_id_lookup(scope_node, cast->name)));
current = cast->parent;
}
if (current == NULL) {
rb_ary_unshift(parts, rb_id2str(idNULL));
}
else if (PM_NODE_TYPE_P(current, PM_CONSTANT_READ_NODE)) {
rb_ary_unshift(parts, rb_id2str(pm_constant_id_lookup(scope_node, ((const pm_constant_read_node_t *) current)->name)));
}
else {
rb_ary_unshift(parts, rb_str_new_cstr("..."));
}
return rb_ary_join(parts, rb_str_new_cstr("::"));
}
/**
* Compile a constant path write node, either in the context of a ractor pragma
* or not.
*/
static void
pm_compile_constant_path_write_node(rb_iseq_t *iseq, const pm_constant_path_write_node_t *node, const pm_node_flags_t shareability, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
{
const pm_node_location_t location = *node_location;
const pm_constant_path_node_t *target = node->target;
VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, target->name));
if (target->parent) {
PM_COMPILE_NOT_POPPED((const pm_node_t *) target->parent);
}
else {
PUSH_INSN1(ret, location, putobject, rb_cObject);
}
if (shareability != 0) {
pm_compile_shareable_constant_value(iseq, node->value, shareability, pm_constant_path_path(node->target, scope_node), ret, scope_node, true);
}
else {
PM_COMPILE_NOT_POPPED(node->value);
}
if (!popped) {
PUSH_INSN(ret, location, swap);
PUSH_INSN1(ret, location, topn, INT2FIX(1));
}
PUSH_INSN(ret, location, swap);
PUSH_INSN1(ret, location, setconstant, name);
}
/**
* Compile a constant path and write node, either in the context of a ractor
* pragma or not.
*/
static void
pm_compile_constant_path_and_write_node(rb_iseq_t *iseq, const pm_constant_path_and_write_node_t *node, const pm_node_flags_t shareability, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
{
const pm_node_location_t location = *node_location;
const pm_constant_path_node_t *target = node->target;
VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, target->name));
LABEL *lfin = NEW_LABEL(location.line);
if (target->parent) {
PM_COMPILE_NOT_POPPED(target->parent);
}
else {
PUSH_INSN1(ret, location, putobject, rb_cObject);
}
PUSH_INSN(ret, location, dup);
PUSH_INSN1(ret, location, putobject, Qtrue);
PUSH_INSN1(ret, location, getconstant, name);
if (!popped) PUSH_INSN(ret, location, dup);
PUSH_INSNL(ret, location, branchunless, lfin);
if (!popped) PUSH_INSN(ret, location, pop);
if (shareability != 0) {
pm_compile_shareable_constant_value(iseq, node->value, shareability, pm_constant_path_path(node->target, scope_node), ret, scope_node, true);
}
else {
PM_COMPILE_NOT_POPPED(node->value);
}
if (popped) {
PUSH_INSN1(ret, location, topn, INT2FIX(1));
}
else {
PUSH_INSN1(ret, location, dupn, INT2FIX(2));
PUSH_INSN(ret, location, swap);
}
PUSH_INSN1(ret, location, setconstant, name);
PUSH_LABEL(ret, lfin);
if (!popped) PUSH_INSN(ret, location, swap);
PUSH_INSN(ret, location, pop);
}
/**
* Compile a constant path or write node, either in the context of a ractor
* pragma or not.
*/
static void
pm_compile_constant_path_or_write_node(rb_iseq_t *iseq, const pm_constant_path_or_write_node_t *node, const pm_node_flags_t shareability, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
{
const pm_node_location_t location = *node_location;
const pm_constant_path_node_t *target = node->target;
VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, target->name));
LABEL *lassign = NEW_LABEL(location.line);
LABEL *lfin = NEW_LABEL(location.line);
if (target->parent) {
PM_COMPILE_NOT_POPPED(target->parent);
}
else {
PUSH_INSN1(ret, location, putobject, rb_cObject);
}
PUSH_INSN(ret, location, dup);
PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_CONST_FROM), name, Qtrue);
PUSH_INSNL(ret, location, branchunless, lassign);
PUSH_INSN(ret, location, dup);
PUSH_INSN1(ret, location, putobject, Qtrue);
PUSH_INSN1(ret, location, getconstant, name);
if (!popped) PUSH_INSN(ret, location, dup);
PUSH_INSNL(ret, location, branchif, lfin);
if (!popped) PUSH_INSN(ret, location, pop);
PUSH_LABEL(ret, lassign);
if (shareability != 0) {
pm_compile_shareable_constant_value(iseq, node->value, shareability, pm_constant_path_path(node->target, scope_node), ret, scope_node, true);
}
else {
PM_COMPILE_NOT_POPPED(node->value);
}
if (popped) {
PUSH_INSN1(ret, location, topn, INT2FIX(1));
}
else {
PUSH_INSN1(ret, location, dupn, INT2FIX(2));
PUSH_INSN(ret, location, swap);
}
PUSH_INSN1(ret, location, setconstant, name);
PUSH_LABEL(ret, lfin);
if (!popped) PUSH_INSN(ret, location, swap);
PUSH_INSN(ret, location, pop);
}
/**
* Compile a constant path operator write node, either in the context of a
* ractor pragma or not.
*/
static void
pm_compile_constant_path_operator_write_node(rb_iseq_t *iseq, const pm_constant_path_operator_write_node_t *node, const pm_node_flags_t shareability, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
{
const pm_node_location_t location = *node_location;
const pm_constant_path_node_t *target = node->target;
ID method_id = pm_constant_id_lookup(scope_node, node->binary_operator);
VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, target->name));
if (target->parent) {
PM_COMPILE_NOT_POPPED(target->parent);
}
else {
PUSH_INSN1(ret, location, putobject, rb_cObject);
}
PUSH_INSN(ret, location, dup);
PUSH_INSN1(ret, location, putobject, Qtrue);
PUSH_INSN1(ret, location, getconstant, name);
if (shareability != 0) {
pm_compile_shareable_constant_value(iseq, node->value, shareability, pm_constant_path_path(node->target, scope_node), ret, scope_node, true);
}
else {
PM_COMPILE_NOT_POPPED(node->value);
}
PUSH_CALL(ret, location, method_id, INT2FIX(1));
PUSH_INSN(ret, location, swap);
if (!popped) {
PUSH_INSN1(ret, location, topn, INT2FIX(1));
PUSH_INSN(ret, location, swap);
}
PUSH_INSN1(ret, location, setconstant, name);
}
/**
* Many nodes in Prism can be marked as a static literal, which means slightly
* different things depending on which node it is. Occasionally we need to omit
* container nodes from static literal checks, which is where this macro comes
* in.
*/
#define PM_CONTAINER_P(node) (PM_NODE_TYPE_P(node, PM_ARRAY_NODE) || PM_NODE_TYPE_P(node, PM_HASH_NODE) || PM_NODE_TYPE_P(node, PM_RANGE_NODE))
/**
* Compile a scope node, which is a special kind of node that represents a new
* lexical scope, attached to a node in the AST.
*/
static inline void
pm_compile_scope_node(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped)
{
const pm_node_location_t location = *node_location;
struct rb_iseq_constant_body *body = ISEQ_BODY(iseq);
pm_constant_id_list_t *locals = &scope_node->locals;
pm_parameters_node_t *parameters_node = NULL;
pm_node_list_t *keywords_list = NULL;
pm_node_list_t *optionals_list = NULL;
pm_node_list_t *posts_list = NULL;
pm_node_list_t *requireds_list = NULL;
pm_node_list_t *block_locals = NULL;
bool trailing_comma = false;
if (PM_NODE_TYPE_P(scope_node->ast_node, PM_CLASS_NODE) || PM_NODE_TYPE_P(scope_node->ast_node, PM_MODULE_NODE)) {
PUSH_TRACE(ret, RUBY_EVENT_CLASS);
}
if (scope_node->parameters != NULL) {
switch (PM_NODE_TYPE(scope_node->parameters)) {
case PM_BLOCK_PARAMETERS_NODE: {
pm_block_parameters_node_t *cast = (pm_block_parameters_node_t *) scope_node->parameters;
parameters_node = cast->parameters;
block_locals = &cast->locals;
if (parameters_node) {
if (parameters_node->rest && PM_NODE_TYPE_P(parameters_node->rest, PM_IMPLICIT_REST_NODE)) {
trailing_comma = true;
}
}
break;
}
case PM_PARAMETERS_NODE: {
parameters_node = (pm_parameters_node_t *) scope_node->parameters;
break;
}
case PM_NUMBERED_PARAMETERS_NODE: {
uint32_t maximum = ((const pm_numbered_parameters_node_t *) scope_node->parameters)->maximum;
body->param.lead_num = maximum;
body->param.flags.ambiguous_param0 = maximum == 1;
break;
}
case PM_IT_PARAMETERS_NODE:
body->param.lead_num = 1;
body->param.flags.ambiguous_param0 = true;
break;
default:
rb_bug("Unexpected node type for parameters: %s", pm_node_type_to_str(PM_NODE_TYPE(scope_node->parameters)));
}
}
struct rb_iseq_param_keyword *keyword = NULL;
if (parameters_node) {
optionals_list = ¶meters_node->optionals;
requireds_list = ¶meters_node->requireds;
keywords_list = ¶meters_node->keywords;
posts_list = ¶meters_node->posts;
}
else if (scope_node->parameters && (PM_NODE_TYPE_P(scope_node->parameters, PM_NUMBERED_PARAMETERS_NODE) || PM_NODE_TYPE_P(scope_node->parameters, PM_IT_PARAMETERS_NODE))) {
body->param.opt_num = 0;
}
else {
body->param.lead_num = 0;
body->param.opt_num = 0;
}
//********STEP 1**********
// Goal: calculate the table size for the locals, accounting for
// hidden variables and multi target nodes
size_t locals_size = locals->size;
// Index lookup table buffer size is only the number of the locals
st_table *index_lookup_table = st_init_numtable();
int table_size = (int) locals_size;
// For nodes have a hidden iteration variable. We add that to the local
// table size here.
if (PM_NODE_TYPE_P(scope_node->ast_node, PM_FOR_NODE)) table_size++;
if (keywords_list && keywords_list->size) {
table_size++;
}
if (requireds_list) {
for (size_t i = 0; i < requireds_list->size; i++) {
// For each MultiTargetNode, we're going to have one
// additional anonymous local not represented in the locals table
// We want to account for this in our table size
pm_node_t *required = requireds_list->nodes[i];
if (PM_NODE_TYPE_P(required, PM_MULTI_TARGET_NODE)) {
table_size++;
}
else if (PM_NODE_TYPE_P(required, PM_REQUIRED_PARAMETER_NODE)) {
if (PM_NODE_FLAG_P(required, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
table_size++;
}
}
}
}
// If we have the `it` implicit local variable, we need to account for
// it in the local table size.
if (scope_node->parameters != NULL && PM_NODE_TYPE_P(scope_node->parameters, PM_IT_PARAMETERS_NODE)) {
table_size++;
}
// Ensure there is enough room in the local table for any
// parameters that have been repeated
// ex: def underscore_parameters(_, _ = 1, _ = 2); _; end
// ^^^^^^^^^^^^
if (optionals_list && optionals_list->size) {
for (size_t i = 0; i < optionals_list->size; i++) {
pm_node_t * node = optionals_list->nodes[i];
if (PM_NODE_FLAG_P(node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
table_size++;
}
}
}
// If we have an anonymous "rest" node, we'll need to increase the local
// table size to take it in to account.
// def m(foo, *, bar)
// ^
if (parameters_node) {
if (parameters_node->rest) {
if (!(PM_NODE_TYPE_P(parameters_node->rest, PM_IMPLICIT_REST_NODE))) {
if (!((const pm_rest_parameter_node_t *) parameters_node->rest)->name || PM_NODE_FLAG_P(parameters_node->rest, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
table_size++;
}
}
}
// def foo(_, **_); _; end
// ^^^
if (parameters_node->keyword_rest) {
// def foo(...); end
// ^^^
// When we have a `...` as the keyword_rest, it's a forwarding_parameter_node and
// we need to leave space for 4 locals: *, **, &, ...
if (PM_NODE_TYPE_P(parameters_node->keyword_rest, PM_FORWARDING_PARAMETER_NODE)) {
// Only optimize specifically methods like this: `foo(...)`
if (requireds_list->size == 0 && optionals_list->size == 0 && keywords_list->size == 0) {
ISEQ_BODY(iseq)->param.flags.use_block = TRUE;
ISEQ_BODY(iseq)->param.flags.forwardable = TRUE;
table_size += 1;
}
else {
table_size += 4;
}
}
else {
const pm_keyword_rest_parameter_node_t *kw_rest = (const pm_keyword_rest_parameter_node_t *) parameters_node->keyword_rest;
// If it's anonymous or repeated, then we need to allocate stack space
if (!kw_rest->name || PM_NODE_FLAG_P(kw_rest, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
table_size++;
}
}
}
}
if (posts_list) {
for (size_t i = 0; i < posts_list->size; i++) {
// For each MultiTargetNode, we're going to have one
// additional anonymous local not represented in the locals table
// We want to account for this in our table size
pm_node_t *required = posts_list->nodes[i];
if (PM_NODE_TYPE_P(required, PM_MULTI_TARGET_NODE) || PM_NODE_FLAG_P(required, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
table_size++;
}
}
}
if (keywords_list && keywords_list->size) {
for (size_t i = 0; i < keywords_list->size; i++) {
pm_node_t *keyword_parameter_node = keywords_list->nodes[i];
if (PM_NODE_FLAG_P(keyword_parameter_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
table_size++;
}
}
}
if (parameters_node && parameters_node->block) {
const pm_block_parameter_node_t *block_node = (const pm_block_parameter_node_t *) parameters_node->block;
if (PM_NODE_FLAG_P(block_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER) || !block_node->name) {
table_size++;
}
}
// We can create local_table_for_iseq with the correct size
VALUE idtmp = 0;
rb_ast_id_table_t *local_table_for_iseq = ALLOCV(idtmp, sizeof(rb_ast_id_table_t) + table_size * sizeof(ID));
local_table_for_iseq->size = table_size;
//********END OF STEP 1**********
//********STEP 2**********
// Goal: populate iv index table as well as local table, keeping the
// layout of the local table consistent with the layout of the
// stack when calling the method
//
// Do a first pass on all of the parameters, setting their values in
// the local_table_for_iseq, _except_ for Multis who get a hidden
// variable in this step, and will get their names inserted in step 3
// local_index is a cursor that keeps track of the current
// index into local_table_for_iseq. The local table is actually a list,
// and the order of that list must match the order of the items pushed
// on the stack. We need to take in to account things pushed on the
// stack that _might not have a name_ (for example array destructuring).
// This index helps us know which item we're dealing with and also give
// those anonymous items temporary names (as below)
int local_index = 0;
// Here we figure out local table indices and insert them in to the
// index lookup table and local tables.
//
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
// ^^^^^^^^^^^^^
if (requireds_list && requireds_list->size) {
for (size_t i = 0; i < requireds_list->size; i++, local_index++) {
ID local;
// For each MultiTargetNode, we're going to have one additional
// anonymous local not represented in the locals table. We want
// to account for this in our table size.
pm_node_t *required = requireds_list->nodes[i];
switch (PM_NODE_TYPE(required)) {
case PM_MULTI_TARGET_NODE: {
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
// ^^^^^^^^^^
local = rb_make_temporary_id(local_index);
local_table_for_iseq->ids[local_index] = local;
break;
}
case PM_REQUIRED_PARAMETER_NODE: {
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
// ^
const pm_required_parameter_node_t *param = (const pm_required_parameter_node_t *) required;
if (PM_NODE_FLAG_P(required, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
ID local = pm_constant_id_lookup(scope_node, param->name);
local_table_for_iseq->ids[local_index] = local;
}
else {
pm_insert_local_index(param->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
}
break;
}
default:
rb_bug("Unsupported node in requireds in parameters %s", pm_node_type_to_str(PM_NODE_TYPE(required)));
}
}
body->param.lead_num = (int) requireds_list->size;
body->param.flags.has_lead = true;
}
if (scope_node->parameters != NULL && PM_NODE_TYPE_P(scope_node->parameters, PM_IT_PARAMETERS_NODE)) {
ID local = rb_make_temporary_id(local_index);
local_table_for_iseq->ids[local_index++] = local;
}
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
// ^^^^^
if (optionals_list && optionals_list->size) {
body->param.opt_num = (int) optionals_list->size;
body->param.flags.has_opt = true;
for (size_t i = 0; i < optionals_list->size; i++, local_index++) {
pm_node_t * node = optionals_list->nodes[i];
pm_constant_id_t name = ((const pm_optional_parameter_node_t *) node)->name;
if (PM_NODE_FLAG_P(node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
ID local = pm_constant_id_lookup(scope_node, name);
local_table_for_iseq->ids[local_index] = local;
}
else {
pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
}
}
}
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
// ^^
if (parameters_node && parameters_node->rest) {
body->param.rest_start = local_index;
// If there's a trailing comma, we'll have an implicit rest node,
// and we don't want it to impact the rest variables on param
if (!(PM_NODE_TYPE_P(parameters_node->rest, PM_IMPLICIT_REST_NODE))) {
body->param.flags.has_rest = true;
RUBY_ASSERT(body->param.rest_start != -1);
pm_constant_id_t name = ((const pm_rest_parameter_node_t *) parameters_node->rest)->name;
if (name) {
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
// ^^
if (PM_NODE_FLAG_P(parameters_node->rest, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
ID local = pm_constant_id_lookup(scope_node, name);
local_table_for_iseq->ids[local_index] = local;
}
else {
pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
}
}
else {
// def foo(a, (b, *c, d), e = 1, *, g, (h, *i, j), k:, l: 1, **m, &n)
// ^
body->param.flags.anon_rest = true;
pm_insert_local_special(idMULT, local_index, index_lookup_table, local_table_for_iseq);
}
local_index++;
}
}
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
// ^^^^^^^^^^^^^
if (posts_list && posts_list->size) {
body->param.post_num = (int) posts_list->size;
body->param.post_start = local_index;
body->param.flags.has_post = true;
for (size_t i = 0; i < posts_list->size; i++, local_index++) {
ID local;
// For each MultiTargetNode, we're going to have one additional
// anonymous local not represented in the locals table. We want
// to account for this in our table size.
const pm_node_t *post_node = posts_list->nodes[i];
switch (PM_NODE_TYPE(post_node)) {
case PM_MULTI_TARGET_NODE: {
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
// ^^^^^^^^^^
local = rb_make_temporary_id(local_index);
local_table_for_iseq->ids[local_index] = local;
break;
}
case PM_REQUIRED_PARAMETER_NODE: {
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
// ^
const pm_required_parameter_node_t *param = (const pm_required_parameter_node_t *) post_node;
if (PM_NODE_FLAG_P(param, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
ID local = pm_constant_id_lookup(scope_node, param->name);
local_table_for_iseq->ids[local_index] = local;
}
else {
pm_insert_local_index(param->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
}
break;
}
default:
rb_bug("Unsupported node in posts in parameters %s", pm_node_type_to_str(PM_NODE_TYPE(post_node)));
}
}
}
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
// ^^^^^^^^
// Keywords create an internal variable on the parse tree
if (keywords_list && keywords_list->size) {
keyword = ZALLOC_N(struct rb_iseq_param_keyword, 1);
keyword->num = (int) keywords_list->size;
const VALUE default_values = rb_ary_hidden_new(1);
const VALUE complex_mark = rb_str_tmp_new(0);
for (size_t i = 0; i < keywords_list->size; i++) {
pm_node_t *keyword_parameter_node = keywords_list->nodes[i];
pm_constant_id_t name;
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
// ^^
if (PM_NODE_TYPE_P(keyword_parameter_node, PM_REQUIRED_KEYWORD_PARAMETER_NODE)) {
name = ((const pm_required_keyword_parameter_node_t *) keyword_parameter_node)->name;
keyword->required_num++;
ID local = pm_constant_id_lookup(scope_node, name);
if (PM_NODE_FLAG_P(keyword_parameter_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
local_table_for_iseq->ids[local_index] = local;
}
else {
pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
}
local_index++;
}
}
for (size_t i = 0; i < keywords_list->size; i++) {
pm_node_t *keyword_parameter_node = keywords_list->nodes[i];
pm_constant_id_t name;
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
// ^^^^
if (PM_NODE_TYPE_P(keyword_parameter_node, PM_OPTIONAL_KEYWORD_PARAMETER_NODE)) {
const pm_optional_keyword_parameter_node_t *cast = ((const pm_optional_keyword_parameter_node_t *) keyword_parameter_node);
pm_node_t *value = cast->value;
name = cast->name;
if (PM_NODE_FLAG_P(value, PM_NODE_FLAG_STATIC_LITERAL) && !PM_CONTAINER_P(value)) {
rb_ary_push(default_values, pm_static_literal_value(iseq, value, scope_node));
}
else {
rb_ary_push(default_values, complex_mark);
}
ID local = pm_constant_id_lookup(scope_node, name);
if (PM_NODE_FLAG_P(keyword_parameter_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
local_table_for_iseq->ids[local_index] = local;
}
else {
pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
}
local_index++;
}
}
if (RARRAY_LEN(default_values)) {
VALUE *dvs = ALLOC_N(VALUE, RARRAY_LEN(default_values));
for (int i = 0; i < RARRAY_LEN(default_values); i++) {
VALUE dv = RARRAY_AREF(default_values, i);
if (dv == complex_mark) dv = Qundef;
RB_OBJ_WRITE(iseq, &dvs[i], dv);
}
keyword->default_values = dvs;
}
// Hidden local for keyword arguments
keyword->bits_start = local_index;
ID local = rb_make_temporary_id(local_index);
local_table_for_iseq->ids[local_index] = local;
local_index++;
body->param.keyword = keyword;
body->param.flags.has_kw = true;
}
if (body->type == ISEQ_TYPE_BLOCK && local_index == 1 && requireds_list && requireds_list->size == 1 && !trailing_comma) {
body->param.flags.ambiguous_param0 = true;
}
if (parameters_node) {
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
// ^^^
if (parameters_node->keyword_rest) {
switch (PM_NODE_TYPE(parameters_node->keyword_rest)) {
case PM_NO_KEYWORDS_PARAMETER_NODE: {
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **nil, &n)
// ^^^^^
body->param.flags.accepts_no_kwarg = true;
break;
}
case PM_KEYWORD_REST_PARAMETER_NODE: {
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
// ^^^
const pm_keyword_rest_parameter_node_t *kw_rest_node = (const pm_keyword_rest_parameter_node_t *) parameters_node->keyword_rest;
if (!body->param.flags.has_kw) {
body->param.keyword = keyword = ZALLOC_N(struct rb_iseq_param_keyword, 1);
}
keyword->rest_start = local_index;
body->param.flags.has_kwrest = true;
pm_constant_id_t constant_id = kw_rest_node->name;
if (constant_id) {
if (PM_NODE_FLAG_P(kw_rest_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
ID local = pm_constant_id_lookup(scope_node, constant_id);
local_table_for_iseq->ids[local_index] = local;
}
else {
pm_insert_local_index(constant_id, local_index, index_lookup_table, local_table_for_iseq, scope_node);
}
}
else {
body->param.flags.anon_kwrest = true;
pm_insert_local_special(idPow, local_index, index_lookup_table, local_table_for_iseq);
}
local_index++;
break;
}
case PM_FORWARDING_PARAMETER_NODE: {
// def foo(...)
// ^^^
if (!ISEQ_BODY(iseq)->param.flags.forwardable) {
// Add the anonymous *
body->param.rest_start = local_index;
body->param.flags.has_rest = true;
body->param.flags.anon_rest = true;
pm_insert_local_special(idMULT, local_index++, index_lookup_table, local_table_for_iseq);
// Add the anonymous **
RUBY_ASSERT(!body->param.flags.has_kw);
body->param.flags.has_kw = false;
body->param.flags.has_kwrest = true;
body->param.flags.anon_kwrest = true;
body->param.keyword = keyword = ZALLOC_N(struct rb_iseq_param_keyword, 1);
keyword->rest_start = local_index;
pm_insert_local_special(idPow, local_index++, index_lookup_table, local_table_for_iseq);
// Add the anonymous &
body->param.block_start = local_index;
body->param.flags.has_block = true;
pm_insert_local_special(idAnd, local_index++, index_lookup_table, local_table_for_iseq);
}
// Add the ...
pm_insert_local_special(idDot3, local_index++, index_lookup_table, local_table_for_iseq);
break;
}
default:
rb_bug("node type %s not expected as keyword_rest", pm_node_type_to_str(PM_NODE_TYPE(parameters_node->keyword_rest)));
}
}
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
// ^^
if (parameters_node->block) {
body->param.block_start = local_index;
body->param.flags.has_block = true;
iseq_set_use_block(iseq);
pm_constant_id_t name = ((const pm_block_parameter_node_t *) parameters_node->block)->name;
if (name) {
if (PM_NODE_FLAG_P(parameters_node->block, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
ID local = pm_constant_id_lookup(scope_node, name);
local_table_for_iseq->ids[local_index] = local;
}
else {
pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
}
}
else {
pm_insert_local_special(idAnd, local_index, index_lookup_table, local_table_for_iseq);
}
local_index++;
}
}
//********END OF STEP 2**********
// The local table is now consistent with expected
// stack layout
// If there's only one required element in the parameters
// CRuby needs to recognize it as an ambiguous parameter
//********STEP 3**********
// Goal: fill in the names of the parameters in MultiTargetNodes
//
// Go through requireds again to set the multis
if (requireds_list && requireds_list->size) {
for (size_t i = 0; i < requireds_list->size; i++) {
// For each MultiTargetNode, we're going to have one
// additional anonymous local not represented in the locals table
// We want to account for this in our table size
const pm_node_t *required = requireds_list->nodes[i];
if (PM_NODE_TYPE_P(required, PM_MULTI_TARGET_NODE)) {
local_index = pm_compile_destructured_param_locals((const pm_multi_target_node_t *) required, index_lookup_table, local_table_for_iseq, scope_node, local_index);
}
}
}
// Go through posts again to set the multis
if (posts_list && posts_list->size) {
for (size_t i = 0; i < posts_list->size; i++) {
// For each MultiTargetNode, we're going to have one
// additional anonymous local not represented in the locals table
// We want to account for this in our table size
const pm_node_t *post = posts_list->nodes[i];
if (PM_NODE_TYPE_P(post, PM_MULTI_TARGET_NODE)) {
local_index = pm_compile_destructured_param_locals((const pm_multi_target_node_t *) post, index_lookup_table, local_table_for_iseq, scope_node, local_index);
}
}
}
// Set any anonymous locals for the for node
if (PM_NODE_TYPE_P(scope_node->ast_node, PM_FOR_NODE)) {
if (PM_NODE_TYPE_P(((const pm_for_node_t *) scope_node->ast_node)->index, PM_LOCAL_VARIABLE_TARGET_NODE)) {
body->param.lead_num++;
}
else {
body->param.rest_start = local_index;
body->param.flags.has_rest = true;
}
ID local = rb_make_temporary_id(local_index);
local_table_for_iseq->ids[local_index] = local;
local_index++;
}
// Fill in any NumberedParameters, if they exist
if (scope_node->parameters && PM_NODE_TYPE_P(scope_node->parameters, PM_NUMBERED_PARAMETERS_NODE)) {
int maximum = ((const pm_numbered_parameters_node_t *) scope_node->parameters)->maximum;
RUBY_ASSERT(0 < maximum && maximum <= 9);
for (int i = 0; i < maximum; i++, local_index++) {
const uint8_t param_name[] = { '_', '1' + i };
pm_constant_id_t constant_id = pm_constant_pool_find(&scope_node->parser->constant_pool, param_name, 2);
RUBY_ASSERT(constant_id && "parser should fill in any gaps in numbered parameters");
pm_insert_local_index(constant_id, local_index, index_lookup_table, local_table_for_iseq, scope_node);
}
body->param.lead_num = maximum;
body->param.flags.has_lead = true;
}
//********END OF STEP 3**********
//********STEP 4**********
// Goal: fill in the method body locals
// To be explicit, these are the non-parameter locals
// We fill in the block_locals, if they exist
// lambda { |x; y| y }
// ^
if (block_locals && block_locals->size) {
for (size_t i = 0; i < block_locals->size; i++, local_index++) {
pm_constant_id_t constant_id = ((const pm_block_local_variable_node_t *) block_locals->nodes[i])->name;
pm_insert_local_index(constant_id, local_index, index_lookup_table, local_table_for_iseq, scope_node);
}
}
// Fill in any locals we missed
if (scope_node->locals.size) {
for (size_t i = 0; i < scope_node->locals.size; i++) {
pm_constant_id_t constant_id = locals->ids[i];
if (constant_id) {
struct pm_local_table_insert_ctx ctx;
ctx.scope_node = scope_node;
ctx.local_table_for_iseq = local_table_for_iseq;
ctx.local_index = local_index;
st_update(index_lookup_table, (st_data_t)constant_id, pm_local_table_insert_func, (st_data_t)&ctx);
local_index = ctx.local_index;
}
}
}
//********END OF STEP 4**********
// We set the index_lookup_table on the scope node so we can
// refer to the parameters correctly
if (scope_node->index_lookup_table) {
st_free_table(scope_node->index_lookup_table);
}
scope_node->index_lookup_table = index_lookup_table;
iseq_calc_param_size(iseq);
if (ISEQ_BODY(iseq)->param.flags.forwardable) {
// We're treating `...` as a parameter so that frame
// pushing won't clobber it.
ISEQ_BODY(iseq)->param.size += 1;
}
// FIXME: args?
iseq_set_local_table(iseq, local_table_for_iseq, 0);
scope_node->local_table_for_iseq_size = local_table_for_iseq->size;
if (keyword != NULL) {
size_t keyword_start_index = keyword->bits_start - keyword->num;
keyword->table = (ID *)&ISEQ_BODY(iseq)->local_table[keyword_start_index];
}
//********STEP 5************
// Goal: compile anything that needed to be compiled
if (optionals_list && optionals_list->size) {
LABEL **opt_table = (LABEL **) ALLOC_N(VALUE, optionals_list->size + 1);
LABEL *label;
// TODO: Should we make an api for NEW_LABEL where you can pass
// a pointer to the label it should fill out? We already
// have a list of labels allocated above so it seems wasteful
// to do the copies.
for (size_t i = 0; i < optionals_list->size; i++) {
label = NEW_LABEL(location.line);
opt_table[i] = label;
PUSH_LABEL(ret, label);
pm_node_t *optional_node = optionals_list->nodes[i];
PM_COMPILE_NOT_POPPED(optional_node);
}
// Set the last label
label = NEW_LABEL(location.line);
opt_table[optionals_list->size] = label;
PUSH_LABEL(ret, label);
body->param.opt_table = (const VALUE *) opt_table;
}
if (keywords_list && keywords_list->size) {
size_t optional_index = 0;
for (size_t i = 0; i < keywords_list->size; i++) {
pm_node_t *keyword_parameter_node = keywords_list->nodes[i];
pm_constant_id_t name;
switch (PM_NODE_TYPE(keyword_parameter_node)) {
case PM_OPTIONAL_KEYWORD_PARAMETER_NODE: {
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
// ^^^^
const pm_optional_keyword_parameter_node_t *cast = ((const pm_optional_keyword_parameter_node_t *) keyword_parameter_node);
pm_node_t *value = cast->value;
name = cast->name;
if (!PM_NODE_FLAG_P(value, PM_NODE_FLAG_STATIC_LITERAL) || PM_CONTAINER_P(value)) {
LABEL *end_label = NEW_LABEL(location.line);
pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, name, 0);
int kw_bits_idx = table_size - body->param.keyword->bits_start;
PUSH_INSN2(ret, location, checkkeyword, INT2FIX(kw_bits_idx + VM_ENV_DATA_SIZE - 1), INT2FIX(optional_index));
PUSH_INSNL(ret, location, branchif, end_label);
PM_COMPILE(value);
PUSH_SETLOCAL(ret, location, index.index, index.level);
PUSH_LABEL(ret, end_label);
}
optional_index++;
break;
}
case PM_REQUIRED_KEYWORD_PARAMETER_NODE:
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
// ^^
break;
default:
rb_bug("Unexpected keyword parameter node type %s", pm_node_type_to_str(PM_NODE_TYPE(keyword_parameter_node)));
}
}
}
if (requireds_list && requireds_list->size) {
for (size_t i = 0; i < requireds_list->size; i++) {
// For each MultiTargetNode, we're going to have one additional
// anonymous local not represented in the locals table. We want
// to account for this in our table size.
const pm_node_t *required = requireds_list->nodes[i];
if (PM_NODE_TYPE_P(required, PM_MULTI_TARGET_NODE)) {
PUSH_GETLOCAL(ret, location, table_size - (int)i, 0);
pm_compile_destructured_param_writes(iseq, (const pm_multi_target_node_t *) required, ret, scope_node);
}
}
}
if (posts_list && posts_list->size) {
for (size_t i = 0; i < posts_list->size; i++) {
// For each MultiTargetNode, we're going to have one additional
// anonymous local not represented in the locals table. We want
// to account for this in our table size.
const pm_node_t *post = posts_list->nodes[i];
if (PM_NODE_TYPE_P(post, PM_MULTI_TARGET_NODE)) {
PUSH_GETLOCAL(ret, location, table_size - body->param.post_start - (int) i, 0);
pm_compile_destructured_param_writes(iseq, (const pm_multi_target_node_t *) post, ret, scope_node);
}
}
}
switch (body->type) {
case ISEQ_TYPE_PLAIN: {
RUBY_ASSERT(PM_NODE_TYPE_P(scope_node->ast_node, PM_INTERPOLATED_REGULAR_EXPRESSION_NODE));
const pm_interpolated_regular_expression_node_t *cast = (const pm_interpolated_regular_expression_node_t *) scope_node->ast_node;
pm_compile_regexp_dynamic(iseq, (const pm_node_t *) cast, &cast->parts, &location, ret, popped, scope_node);
break;
}
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);
const pm_node_location_t block_location = { .line = body->location.first_lineno, .node_id = scope_node->ast_node->node_id };
start->rescued = LABEL_RESCUE_BEG;
end->rescued = LABEL_RESCUE_END;
// For nodes automatically assign the iteration variable to whatever
// index variable. We need to handle that write here because it has
// to happen in the context of the block. Note that this happens
// before the B_CALL tracepoint event.
if (PM_NODE_TYPE_P(scope_node->ast_node, PM_FOR_NODE)) {
pm_compile_for_node_index(iseq, ((const pm_for_node_t *) scope_node->ast_node)->index, ret, scope_node);
}
PUSH_TRACE(ret, RUBY_EVENT_B_CALL);
PUSH_INSN(ret, block_location, nop);
PUSH_LABEL(ret, start);
if (scope_node->body != NULL) {
switch (PM_NODE_TYPE(scope_node->ast_node)) {
case PM_POST_EXECUTION_NODE: {
const pm_post_execution_node_t *cast = (const pm_post_execution_node_t *) scope_node->ast_node;
PUSH_INSN1(ret, block_location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
// We create another ScopeNode from the statements within the PostExecutionNode
pm_scope_node_t next_scope_node;
pm_scope_node_init((const pm_node_t *) cast->statements, &next_scope_node, scope_node);
const rb_iseq_t *block = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(body->parent_iseq), ISEQ_TYPE_BLOCK, location.line);
pm_scope_node_destroy(&next_scope_node);
PUSH_CALL_WITH_BLOCK(ret, block_location, id_core_set_postexe, INT2FIX(0), block);
break;
}
case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE: {
const pm_interpolated_regular_expression_node_t *cast = (const pm_interpolated_regular_expression_node_t *) scope_node->ast_node;
pm_compile_regexp_dynamic(iseq, (const pm_node_t *) cast, &cast->parts, &location, ret, popped, scope_node);
break;
}
default:
pm_compile_node(iseq, scope_node->body, ret, popped, scope_node);
break;
}
}
else {
PUSH_INSN(ret, block_location, putnil);
}
PUSH_LABEL(ret, end);
PUSH_TRACE(ret, RUBY_EVENT_B_RETURN);
ISEQ_COMPILE_DATA(iseq)->last_line = body->location.code_location.end_pos.lineno;
/* wide range catch handler must put at last */
PUSH_CATCH_ENTRY(CATCH_TYPE_REDO, start, end, NULL, start);
PUSH_CATCH_ENTRY(CATCH_TYPE_NEXT, start, end, NULL, end);
break;
}
case ISEQ_TYPE_ENSURE: {
const pm_node_location_t statements_location = (scope_node->body != NULL ? PM_NODE_START_LOCATION(scope_node->parser, scope_node->body) : location);
iseq_set_exception_local_table(iseq);
if (scope_node->body != NULL) {
PM_COMPILE_POPPED((const pm_node_t *) scope_node->body);
}
PUSH_GETLOCAL(ret, statements_location, 1, 0);
PUSH_INSN1(ret, statements_location, throw, INT2FIX(0));
return;
}
case ISEQ_TYPE_METHOD: {
ISEQ_COMPILE_DATA(iseq)->root_node = (const void *) scope_node->body;
PUSH_TRACE(ret, RUBY_EVENT_CALL);
if (scope_node->body) {
PM_COMPILE((const pm_node_t *) scope_node->body);
}
else {
PUSH_INSN(ret, location, putnil);
}
ISEQ_COMPILE_DATA(iseq)->root_node = (const void *) scope_node->body;
PUSH_TRACE(ret, RUBY_EVENT_RETURN);
ISEQ_COMPILE_DATA(iseq)->last_line = body->location.code_location.end_pos.lineno;
break;
}
case ISEQ_TYPE_RESCUE: {
iseq_set_exception_local_table(iseq);
if (PM_NODE_TYPE_P(scope_node->ast_node, PM_RESCUE_MODIFIER_NODE)) {
LABEL *lab = NEW_LABEL(location.line);
LABEL *rescue_end = NEW_LABEL(location.line);
PUSH_GETLOCAL(ret, location, LVAR_ERRINFO, 0);
PUSH_INSN1(ret, location, putobject, rb_eStandardError);
PUSH_INSN1(ret, location, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
PUSH_INSNL(ret, location, branchif, lab);
PUSH_INSNL(ret, location, jump, rescue_end);
PUSH_LABEL(ret, lab);
PUSH_TRACE(ret, RUBY_EVENT_RESCUE);
PM_COMPILE((const pm_node_t *) scope_node->body);
PUSH_INSN(ret, location, leave);
PUSH_LABEL(ret, rescue_end);
PUSH_GETLOCAL(ret, location, LVAR_ERRINFO, 0);
}
else {
PM_COMPILE((const pm_node_t *) scope_node->ast_node);
}
PUSH_INSN1(ret, location, throw, INT2FIX(0));
return;
}
default:
if (scope_node->body) {
PM_COMPILE((const pm_node_t *) scope_node->body);
}
else {
PUSH_INSN(ret, location, putnil);
}
break;
}
if (PM_NODE_TYPE_P(scope_node->ast_node, PM_CLASS_NODE) || PM_NODE_TYPE_P(scope_node->ast_node, PM_MODULE_NODE)) {
const pm_node_location_t end_location = PM_NODE_END_LOCATION(scope_node->parser, scope_node->ast_node);
PUSH_TRACE(ret, RUBY_EVENT_END);
ISEQ_COMPILE_DATA(iseq)->last_line = end_location.line;
}
if (!PM_NODE_TYPE_P(scope_node->ast_node, PM_ENSURE_NODE)) {
const pm_node_location_t location = { .line = ISEQ_COMPILE_DATA(iseq)->last_line, .node_id = scope_node->ast_node->node_id };
PUSH_INSN(ret, location, leave);
}
}
static inline void
pm_compile_alias_global_variable_node(rb_iseq_t *iseq, const pm_alias_global_variable_node_t *node, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
{
// alias $foo $bar
// ^^^^^^^^^^^^^^^
PUSH_INSN1(ret, *location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
{
const pm_location_t *name_loc = &node->new_name->location;
VALUE operand = ID2SYM(rb_intern3((const char *) name_loc->start, name_loc->end - name_loc->start, scope_node->encoding));
PUSH_INSN1(ret, *location, putobject, operand);
}
{
const pm_location_t *name_loc = &node->old_name->location;
VALUE operand = ID2SYM(rb_intern3((const char *) name_loc->start, name_loc->end - name_loc->start, scope_node->encoding));
PUSH_INSN1(ret, *location, putobject, operand);
}
PUSH_SEND(ret, *location, id_core_set_variable_alias, INT2FIX(2));
if (popped) PUSH_INSN(ret, *location, pop);
}
static inline void
pm_compile_alias_method_node(rb_iseq_t *iseq, const pm_alias_method_node_t *node, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
{
PUSH_INSN1(ret, *location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
PUSH_INSN1(ret, *location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CBASE));
PM_COMPILE_NOT_POPPED(node->new_name);
PM_COMPILE_NOT_POPPED(node->old_name);
PUSH_SEND(ret, *location, id_core_set_method_alias, INT2FIX(3));
if (popped) PUSH_INSN(ret, *location, pop);
}
static inline void
pm_compile_and_node(rb_iseq_t *iseq, const pm_and_node_t *node, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
{
LABEL *end_label = NEW_LABEL(location->line);
PM_COMPILE_NOT_POPPED(node->left);
if (!popped) PUSH_INSN(ret, *location, dup);
PUSH_INSNL(ret, *location, branchunless, end_label);
if (!popped) PUSH_INSN(ret, *location, pop);
PM_COMPILE(node->right);
PUSH_LABEL(ret, end_label);
}
static inline void
pm_compile_array_node(rb_iseq_t *iseq, const pm_node_t *node, const pm_node_list_t *elements, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
{
// If every node in the array is static, then we can compile the entire
// array now instead of later.
if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
// We're only going to compile this node if it's not popped. If it
// is popped, then we know we don't need to do anything since it's
// statically known.
if (!popped) {
if (elements->size) {
VALUE value = pm_static_literal_value(iseq, node, scope_node);
PUSH_INSN1(ret, *location, duparray, value);
}
else {
PUSH_INSN1(ret, *location, newarray, INT2FIX(0));
}
}
return;
}
// Here since we know there are possible side-effects inside the
// array contents, we're going to build it entirely at runtime.
// We'll do this by pushing all of the elements onto the stack and
// then combining them with newarray.
//
// If this array is popped, then this serves only to ensure we enact
// all side-effects (like method calls) that are contained within
// the array contents.
//
// We treat all sequences of non-splat elements as their
// own arrays, followed by a newarray, and then continually
// concat the arrays with the SplatNode nodes.
const int max_new_array_size = 0x100;
const unsigned int min_tmp_array_size = 0x40;
int new_array_size = 0;
bool first_chunk = true;
// This is an optimization wherein we keep track of whether or not
// the previous element was a static literal. If it was, then we do
// not attempt to check if we have a subarray that can be optimized.
// If it was not, then we do check.
bool static_literal = false;
// Either create a new array, or push to the existing array.
#define FLUSH_CHUNK \
if (new_array_size) { \
if (first_chunk) PUSH_INSN1(ret, *location, newarray, INT2FIX(new_array_size)); \
else PUSH_INSN1(ret, *location, pushtoarray, INT2FIX(new_array_size)); \
first_chunk = false; \
new_array_size = 0; \
}
for (size_t index = 0; index < elements->size; index++) {
const pm_node_t *element = elements->nodes[index];
if (PM_NODE_TYPE_P(element, PM_SPLAT_NODE)) {
FLUSH_CHUNK;
const pm_splat_node_t *splat_element = (const pm_splat_node_t *) element;
if (splat_element->expression) {
PM_COMPILE_NOT_POPPED(splat_element->expression);
}
else {
pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_MULT, 0);
PUSH_GETLOCAL(ret, *location, index.index, index.level);
}
if (first_chunk) {
// If this is the first element of the array then we
// need to splatarray the elements into the list.
PUSH_INSN1(ret, *location, splatarray, Qtrue);
first_chunk = false;
}
else {
PUSH_INSN(ret, *location, concattoarray);
}
static_literal = false;
}
else if (PM_NODE_TYPE_P(element, PM_KEYWORD_HASH_NODE)) {
if (new_array_size == 0 && first_chunk) {
PUSH_INSN1(ret, *location, newarray, INT2FIX(0));
first_chunk = false;
}
else {
FLUSH_CHUNK;
}
// If we get here, then this is the last element of the
// array/arguments, because it cannot be followed by
// anything else without a syntax error. This looks like:
//
// [foo, bar, baz: qux]
// ^^^^^^^^
//
// [foo, bar, **baz]
// ^^^^^
//
const pm_keyword_hash_node_t *keyword_hash = (const pm_keyword_hash_node_t *) element;
pm_compile_hash_elements(iseq, element, &keyword_hash->elements, 0, Qundef, false, ret, scope_node);
// This boolean controls the manner in which we push the
// hash onto the array. If it's all keyword splats, then we
// can use the very specialized pushtoarraykwsplat
// instruction to check if it's empty before we push it.
size_t splats = 0;
while (splats < keyword_hash->elements.size && PM_NODE_TYPE_P(keyword_hash->elements.nodes[splats], PM_ASSOC_SPLAT_NODE)) splats++;
if (keyword_hash->elements.size == splats) {
PUSH_INSN(ret, *location, pushtoarraykwsplat);
}
else {
new_array_size++;
}
}
else if (
PM_NODE_FLAG_P(element, PM_NODE_FLAG_STATIC_LITERAL) &&
!PM_CONTAINER_P(element) &&
!static_literal &&
((index + min_tmp_array_size) < elements->size)
) {
// If we have a static literal, then there's the potential
// to group a bunch of them together with a literal array
// and then concat them together.
size_t right_index = index + 1;
while (
right_index < elements->size &&
PM_NODE_FLAG_P(elements->nodes[right_index], PM_NODE_FLAG_STATIC_LITERAL) &&
!PM_CONTAINER_P(elements->nodes[right_index])
) right_index++;
size_t tmp_array_size = right_index - index;
if (tmp_array_size >= min_tmp_array_size) {
VALUE tmp_array = rb_ary_hidden_new(tmp_array_size);
// Create the temporary array.
for (; tmp_array_size; tmp_array_size--)
rb_ary_push(tmp_array, pm_static_literal_value(iseq, elements->nodes[index++], scope_node));
index--; // about to be incremented by for loop
OBJ_FREEZE(tmp_array);
// Emit the optimized code.
FLUSH_CHUNK;
if (first_chunk) {
PUSH_INSN1(ret, *location, duparray, tmp_array);
first_chunk = false;
}
else {
PUSH_INSN1(ret, *location, putobject, tmp_array);
PUSH_INSN(ret, *location, concattoarray);
}
}
else {
PM_COMPILE_NOT_POPPED(element);
if (++new_array_size >= max_new_array_size) FLUSH_CHUNK;
static_literal = true;
}
} else {
PM_COMPILE_NOT_POPPED(element);
if (++new_array_size >= max_new_array_size) FLUSH_CHUNK;
static_literal = false;
}
}
FLUSH_CHUNK;
if (popped) PUSH_INSN(ret, *location, pop);
#undef FLUSH_CHUNK
}
static inline void
pm_compile_break_node(rb_iseq_t *iseq, const pm_break_node_t *node, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_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);
PUSH_LABEL(ret, splabel);
PUSH_ADJUST(ret, *location, ISEQ_COMPILE_DATA(iseq)->redo_label);
if (node->arguments != NULL) {
PM_COMPILE_NOT_POPPED((const pm_node_t *) node->arguments);
}
else {
PUSH_INSN(ret, *location, putnil);
}
pm_add_ensure_iseq(ret, iseq, 0, scope_node);
PUSH_INSNL(ret, *location, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
PUSH_ADJUST_RESTORE(ret, splabel);
if (!popped) PUSH_INSN(ret, *location, 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(iseq, location->line, "Invalid break");
return;
}
else {
ip = ISEQ_BODY(ip)->parent_iseq;
continue;
}
/* escape from block */
if (node->arguments != NULL) {
PM_COMPILE_NOT_POPPED((const pm_node_t *) node->arguments);
}
else {
PUSH_INSN(ret, *location, putnil);
}
PUSH_INSN1(ret, *location, throw, INT2FIX(throw_flag | TAG_BREAK));
if (popped) PUSH_INSN(ret, *location, pop);
return;
}
COMPILE_ERROR(iseq, location->line, "Invalid break");
}
}
static inline void
pm_compile_call_node(rb_iseq_t *iseq, const pm_call_node_t *node, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
{
ID method_id = pm_constant_id_lookup(scope_node, node->name);
const pm_location_t *message_loc = &node->message_loc;
if (message_loc->start == NULL) message_loc = &node->base.location;
const pm_node_location_t location = PM_LOCATION_START_LOCATION(scope_node->parser, message_loc, node->base.node_id);
const char *builtin_func;
if (UNLIKELY(iseq_has_builtin_function_table(iseq)) && (builtin_func = pm_iseq_builtin_function_name(scope_node, node->receiver, method_id)) != NULL) {
pm_compile_builtin_function_call(iseq, ret, scope_node, node, &location, popped, ISEQ_COMPILE_DATA(iseq)->current_block, builtin_func);
return;
}
LABEL *start = NEW_LABEL(location.line);
if (node->block) PUSH_LABEL(ret, start);
switch (method_id) {
case idUMinus: {
if (pm_opt_str_freeze_p(iseq, node)) {
VALUE value = parse_static_literal_string(iseq, scope_node, node->receiver, &((const pm_string_node_t * ) node->receiver)->unescaped);
const struct rb_callinfo *callinfo = new_callinfo(iseq, idUMinus, 0, 0, NULL, FALSE);
PUSH_INSN2(ret, location, opt_str_uminus, value, callinfo);
if (popped) PUSH_INSN(ret, location, pop);
return;
}
break;
}
case idFreeze: {
if (pm_opt_str_freeze_p(iseq, node)) {
VALUE value = parse_static_literal_string(iseq, scope_node, node->receiver, &((const pm_string_node_t * ) node->receiver)->unescaped);
const struct rb_callinfo *callinfo = new_callinfo(iseq, idFreeze, 0, 0, NULL, FALSE);
PUSH_INSN2(ret, location, opt_str_freeze, value, callinfo);
if (popped) PUSH_INSN(ret, location, pop);
return;
}
break;
}
case idAREF: {
if (pm_opt_aref_with_p(iseq, node)) {
const pm_string_node_t *string = (const pm_string_node_t *) ((const pm_arguments_node_t *) node->arguments)->arguments.nodes[0];
VALUE value = parse_static_literal_string(iseq, scope_node, (const pm_node_t *) string, &string->unescaped);
PM_COMPILE_NOT_POPPED(node->receiver);
const struct rb_callinfo *callinfo = new_callinfo(iseq, idAREF, 1, 0, NULL, FALSE);
PUSH_INSN2(ret, location, opt_aref_with, value, callinfo);
if (popped) {
PUSH_INSN(ret, location, pop);
}
return;
}
break;
}
case idASET: {
if (pm_opt_aset_with_p(iseq, node)) {
const pm_string_node_t *string = (const pm_string_node_t *) ((const pm_arguments_node_t *) node->arguments)->arguments.nodes[0];
VALUE value = parse_static_literal_string(iseq, scope_node, (const pm_node_t *) string, &string->unescaped);
PM_COMPILE_NOT_POPPED(node->receiver);
PM_COMPILE_NOT_POPPED(((const pm_arguments_node_t *) node->arguments)->arguments.nodes[1]);
if (!popped) {
PUSH_INSN(ret, location, swap);
PUSH_INSN1(ret, location, topn, INT2FIX(1));
}
const struct rb_callinfo *callinfo = new_callinfo(iseq, idASET, 2, 0, NULL, FALSE);
PUSH_INSN2(ret, location, opt_aset_with, value, callinfo);
PUSH_INSN(ret, location, pop);
return;
}
break;
}
}
if (PM_NODE_FLAG_P(node, PM_CALL_NODE_FLAGS_ATTRIBUTE_WRITE) && !popped) {
PUSH_INSN(ret, location, putnil);
}
if (node->receiver == NULL) {
PUSH_INSN(ret, location, putself);
}
else {
if (method_id == idCall && PM_NODE_TYPE_P(node->receiver, PM_LOCAL_VARIABLE_READ_NODE)) {
const pm_local_variable_read_node_t *read_node_cast = (const pm_local_variable_read_node_t *) node->receiver;
uint32_t node_id = node->receiver->node_id;
int idx, level;
if (iseq_block_param_id_p(iseq, pm_constant_id_lookup(scope_node, read_node_cast->name), &idx, &level)) {
ADD_ELEM(ret, (LINK_ELEMENT *) new_insn_body(iseq, location.line, node_id, BIN(getblockparamproxy), 2, INT2FIX((idx) + VM_ENV_DATA_SIZE - 1), INT2FIX(level)));
}
else {
PM_COMPILE_NOT_POPPED(node->receiver);
}
}
else {
PM_COMPILE_NOT_POPPED(node->receiver);
}
}
pm_compile_call(iseq, node, ret, popped, scope_node, method_id, start);
return;
}
static inline void
pm_compile_call_operator_write_node(rb_iseq_t *iseq, const pm_call_operator_write_node_t *node, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
{
int flag = 0;
if (PM_NODE_FLAG_P(node, PM_CALL_NODE_FLAGS_IGNORE_VISIBILITY)) {
flag = VM_CALL_FCALL;
}
PM_COMPILE_NOT_POPPED(node->receiver);
LABEL *safe_label = NULL;
if (PM_NODE_FLAG_P(node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION)) {
safe_label = NEW_LABEL(location->line);
PUSH_INSN(ret, *location, dup);
PUSH_INSNL(ret, *location, branchnil, safe_label);
}
PUSH_INSN(ret, *location, dup);
ID id_read_name = pm_constant_id_lookup(scope_node, node->read_name);
PUSH_SEND_WITH_FLAG(ret, *location, id_read_name, INT2FIX(0), INT2FIX(flag));
PM_COMPILE_NOT_POPPED(node->value);
ID id_operator = pm_constant_id_lookup(scope_node, node->binary_operator);
PUSH_SEND(ret, *location, id_operator, INT2FIX(1));
if (!popped) {
PUSH_INSN(ret, *location, swap);
PUSH_INSN1(ret, *location, topn, INT2FIX(1));
}
ID id_write_name = pm_constant_id_lookup(scope_node, node->write_name);
PUSH_SEND_WITH_FLAG(ret, *location, id_write_name, INT2FIX(1), INT2FIX(flag));
if (safe_label != NULL && popped) PUSH_LABEL(ret, safe_label);
PUSH_INSN(ret, *location, pop);
if (safe_label != NULL && !popped) PUSH_LABEL(ret, safe_label);
}
/**
* When we're compiling a case node, it's possible that we can speed it up using
* a dispatch hash, which will allow us to jump directly to the correct when
* clause body based on a hash lookup of the value. This can only happen when
* the conditions are literals that can be compiled into a hash key.
*
* This function accepts a dispatch hash and the condition of a when clause. It
* is responsible for compiling the condition into a hash key and then adding it
* to the dispatch hash.
*
* If the value can be successfully compiled into the hash, then this function
* returns the dispatch hash with the new key added. If the value cannot be
* compiled into the hash, then this function returns Qundef. In the case of
* Qundef, this function is signaling that the caller should abandon the
* optimization entirely.
*/
static VALUE
pm_compile_case_node_dispatch(rb_iseq_t *iseq, VALUE dispatch, const pm_node_t *node, LABEL *label, const pm_scope_node_t *scope_node)
{
VALUE key = Qundef;
switch (PM_NODE_TYPE(node)) {
case PM_FLOAT_NODE: {
key = pm_static_literal_value(iseq, node, scope_node);
double intptr;
if (modf(RFLOAT_VALUE(key), &intptr) == 0.0) {
key = (FIXABLE(intptr) ? LONG2FIX((long) intptr) : rb_dbl2big(intptr));
}
break;
}
case PM_FALSE_NODE:
case PM_INTEGER_NODE:
case PM_NIL_NODE:
case PM_SOURCE_FILE_NODE:
case PM_SOURCE_LINE_NODE:
case PM_SYMBOL_NODE:
case PM_TRUE_NODE:
key = pm_static_literal_value(iseq, node, scope_node);
break;
case PM_STRING_NODE: {
const pm_string_node_t *cast = (const pm_string_node_t *) node;
key = parse_static_literal_string(iseq, scope_node, node, &cast->unescaped);
break;
}
default:
return Qundef;
}
if (NIL_P(rb_hash_lookup(dispatch, key))) {
rb_hash_aset(dispatch, key, ((VALUE) label) | 1);
}
return dispatch;
}
/**
* Compile a case node, representing a case statement with when clauses.
*/
static inline void
pm_compile_case_node(rb_iseq_t *iseq, const pm_case_node_t *cast, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
{
const pm_parser_t *parser = scope_node->parser;
const pm_node_location_t location = *node_location;
const pm_node_list_t *conditions = &cast->conditions;
// This is the anchor that we will compile the conditions of the various
// `when` nodes into. If a match is found, they will need to jump into
// the body_seq anchor to the correct spot.
DECL_ANCHOR(cond_seq);
// This is the anchor that we will compile the bodies of the various
// `when` nodes into. We'll make sure that the clauses that are compiled
// jump into the correct spots within this anchor.
DECL_ANCHOR(body_seq);
// This is the label where all of the when clauses will jump to if they
// have matched and are done executing their bodies.
LABEL *end_label = NEW_LABEL(location.line);
// If we have a predicate on this case statement, then it's going to
// compare all of the various when clauses to the predicate. If we
// don't, then it's basically an if-elsif-else chain.
if (cast->predicate == NULL) {
// Establish branch coverage for the case node.
VALUE branches = Qfalse;
rb_code_location_t case_location = { 0 };
int branch_id = 0;
if (PM_BRANCH_COVERAGE_P(iseq)) {
case_location = pm_code_location(scope_node, (const pm_node_t *) cast);
branches = decl_branch_base(iseq, PTR2NUM(cast), &case_location, "case");
}
// Loop through each clauses in the case node and compile each of
// the conditions within them into cond_seq. If they match, they
// should jump into their respective bodies in body_seq.
for (size_t clause_index = 0; clause_index < conditions->size; clause_index++) {
const pm_when_node_t *clause = (const pm_when_node_t *) conditions->nodes[clause_index];
const pm_node_list_t *conditions = &clause->conditions;
int clause_lineno = pm_node_line_number(parser, (const pm_node_t *) clause);
LABEL *label = NEW_LABEL(clause_lineno);
PUSH_LABEL(body_seq, label);
// Establish branch coverage for the when clause.
if (PM_BRANCH_COVERAGE_P(iseq)) {
rb_code_location_t branch_location = pm_code_location(scope_node, clause->statements != NULL ? ((const pm_node_t *) clause->statements) : ((const pm_node_t *) clause));
add_trace_branch_coverage(iseq, body_seq, &branch_location, branch_location.beg_pos.column, branch_id++, "when", branches);
}
if (clause->statements != NULL) {
pm_compile_node(iseq, (const pm_node_t *) clause->statements, body_seq, popped, scope_node);
}
else if (!popped) {
PUSH_SYNTHETIC_PUTNIL(body_seq, iseq);
}
PUSH_INSNL(body_seq, location, jump, end_label);
// Compile each of the conditions for the when clause into the
// cond_seq. Each one should have a unique condition and should
// jump to the subsequent one if it doesn't match.
for (size_t condition_index = 0; condition_index < conditions->size; condition_index++) {
const pm_node_t *condition = conditions->nodes[condition_index];
if (PM_NODE_TYPE_P(condition, PM_SPLAT_NODE)) {
pm_node_location_t cond_location = PM_NODE_START_LOCATION(parser, condition);
PUSH_INSN(cond_seq, cond_location, putnil);
pm_compile_node(iseq, condition, cond_seq, false, scope_node);
PUSH_INSN1(cond_seq, cond_location, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_WHEN | VM_CHECKMATCH_ARRAY));
PUSH_INSNL(cond_seq, cond_location, branchif, label);
}
else {
LABEL *next_label = NEW_LABEL(pm_node_line_number(parser, condition));
pm_compile_branch_condition(iseq, cond_seq, condition, label, next_label, false, scope_node);
PUSH_LABEL(cond_seq, next_label);
}
}
}
// Establish branch coverage for the else clause (implicit or
// explicit).
if (PM_BRANCH_COVERAGE_P(iseq)) {
rb_code_location_t branch_location;
if (cast->else_clause == NULL) {
branch_location = case_location;
} else if (cast->else_clause->statements == NULL) {
branch_location = pm_code_location(scope_node, (const pm_node_t *) cast->else_clause);
} else {
branch_location = pm_code_location(scope_node, (const pm_node_t *) cast->else_clause->statements);
}
add_trace_branch_coverage(iseq, cond_seq, &branch_location, branch_location.beg_pos.column, branch_id, "else", branches);
}
// Compile the else clause if there is one.
if (cast->else_clause != NULL) {
pm_compile_node(iseq, (const pm_node_t *) cast->else_clause, cond_seq, popped, scope_node);
}
else if (!popped) {
PUSH_SYNTHETIC_PUTNIL(cond_seq, iseq);
}
// Finally, jump to the end label if none of the other conditions
// have matched.
PUSH_INSNL(cond_seq, location, jump, end_label);
PUSH_SEQ(ret, cond_seq);
}
else {
// Establish branch coverage for the case node.
VALUE branches = Qfalse;
rb_code_location_t case_location = { 0 };
int branch_id = 0;
if (PM_BRANCH_COVERAGE_P(iseq)) {
case_location = pm_code_location(scope_node, (const pm_node_t *) cast);
branches = decl_branch_base(iseq, PTR2NUM(cast), &case_location, "case");
}
// This is the label where everything will fall into if none of the
// conditions matched.
LABEL *else_label = NEW_LABEL(location.line);
// It's possible for us to speed up the case node by using a
// dispatch hash. This is a hash that maps the conditions of the
// various when clauses to the labels of their bodies. If we can
// compile the conditions into a hash key, then we can use a hash
// lookup to jump directly to the correct when clause body.
VALUE dispatch = Qundef;
if (ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
dispatch = rb_hash_new();
RHASH_TBL_RAW(dispatch)->type = &cdhash_type;
}
// We're going to loop through each of the conditions in the case
// node and compile each of their contents into both the cond_seq
// and the body_seq. Each condition will use its own label to jump
// from its conditions into its body.
//
// Note that none of the code in the loop below should be adding
// anything to ret, as we're going to be laying out the entire case
// node instructions later.
for (size_t clause_index = 0; clause_index < conditions->size; clause_index++) {
const pm_when_node_t *clause = (const pm_when_node_t *) conditions->nodes[clause_index];
pm_node_location_t clause_location = PM_NODE_START_LOCATION(parser, (const pm_node_t *) clause);
const pm_node_list_t *conditions = &clause->conditions;
LABEL *label = NEW_LABEL(clause_location.line);
// Compile each of the conditions for the when clause into the
// cond_seq. Each one should have a unique comparison that then
// jumps into the body if it matches.
for (size_t condition_index = 0; condition_index < conditions->size; condition_index++) {
const pm_node_t *condition = conditions->nodes[condition_index];
const pm_node_location_t condition_location = PM_NODE_START_LOCATION(parser, condition);
// If we haven't already abandoned the optimization, then
// we're going to try to compile the condition into the
// dispatch hash.
if (dispatch != Qundef) {
dispatch = pm_compile_case_node_dispatch(iseq, dispatch, condition, label, scope_node);
}
if (PM_NODE_TYPE_P(condition, PM_SPLAT_NODE)) {
PUSH_INSN(cond_seq, condition_location, dup);
pm_compile_node(iseq, condition, cond_seq, false, scope_node);
PUSH_INSN1(cond_seq, condition_location, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_CASE | VM_CHECKMATCH_ARRAY));
}
else {
if (PM_NODE_TYPE_P(condition, PM_STRING_NODE)) {
const pm_string_node_t *string = (const pm_string_node_t *) condition;
VALUE value = parse_static_literal_string(iseq, scope_node, condition, &string->unescaped);
PUSH_INSN1(cond_seq, condition_location, putobject, value);
}
else {
pm_compile_node(iseq, condition, cond_seq, false, scope_node);
}
PUSH_INSN1(cond_seq, condition_location, topn, INT2FIX(1));
PUSH_SEND_WITH_FLAG(cond_seq, condition_location, idEqq, INT2NUM(1), INT2FIX(VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE));
}
PUSH_INSNL(cond_seq, condition_location, branchif, label);
}
// Now, add the label to the body and compile the body of the
// when clause. This involves popping the predicate, compiling
// the statements to be executed, and then compiling a jump to
// the end of the case node.
PUSH_LABEL(body_seq, label);
PUSH_INSN(body_seq, clause_location, pop);
// Establish branch coverage for the when clause.
if (PM_BRANCH_COVERAGE_P(iseq)) {
rb_code_location_t branch_location = pm_code_location(scope_node, clause->statements != NULL ? ((const pm_node_t *) clause->statements) : ((const pm_node_t *) clause));
add_trace_branch_coverage(iseq, body_seq, &branch_location, branch_location.beg_pos.column, branch_id++, "when", branches);
}
if (clause->statements != NULL) {
pm_compile_node(iseq, (const pm_node_t *) clause->statements, body_seq, popped, scope_node);
}
else if (!popped) {
PUSH_SYNTHETIC_PUTNIL(body_seq, iseq);
}
PUSH_INSNL(body_seq, clause_location, jump, end_label);
}
// Now that we have compiled the conditions and the bodies of the
// various when clauses, we can compile the predicate, lay out the
// conditions, compile the fallback subsequent if there is one, and
// finally put in the bodies of the when clauses.
PM_COMPILE_NOT_POPPED(cast->predicate);
// If we have a dispatch hash, then we'll use it here to create the
// optimization.
if (dispatch != Qundef) {
PUSH_INSN(ret, location, dup);
PUSH_INSN2(ret, location, opt_case_dispatch, dispatch, else_label);
LABEL_REF(else_label);
}
PUSH_SEQ(ret, cond_seq);
// Compile either the explicit else clause or an implicit else
// clause.
PUSH_LABEL(ret, else_label);
if (cast->else_clause != NULL) {
pm_node_location_t else_location = PM_NODE_START_LOCATION(parser, cast->else_clause->statements != NULL ? ((const pm_node_t *) cast->else_clause->statements) : ((const pm_node_t *) cast->else_clause));
PUSH_INSN(ret, else_location, pop);
// Establish branch coverage for the else clause.
if (PM_BRANCH_COVERAGE_P(iseq)) {
rb_code_location_t branch_location = pm_code_location(scope_node, cast->else_clause->statements != NULL ? ((const pm_node_t *) cast->else_clause->statements) : ((const pm_node_t *) cast->else_clause));
add_trace_branch_coverage(iseq, ret, &branch_location, branch_location.beg_pos.column, branch_id, "else", branches);
}
PM_COMPILE((const pm_node_t *) cast->else_clause);
PUSH_INSNL(ret, else_location, jump, end_label);
}
else {
PUSH_INSN(ret, location, pop);
// Establish branch coverage for the implicit else clause.
if (PM_BRANCH_COVERAGE_P(iseq)) {
add_trace_branch_coverage(iseq, ret, &case_location, case_location.beg_pos.column, branch_id, "else", branches);
}
if (!popped) PUSH_INSN(ret, location, putnil);
PUSH_INSNL(ret, location, jump, end_label);
}
}
PUSH_SEQ(ret, body_seq);
PUSH_LABEL(ret, end_label);
}
static inline void
pm_compile_case_match_node(rb_iseq_t *iseq, const pm_case_match_node_t *node, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
{
// This is the anchor that we will compile the bodies of the various
// `in` nodes into. We'll make sure that the patterns that are compiled
// jump into the correct spots within this anchor.
DECL_ANCHOR(body_seq);
// This is the anchor that we will compile the patterns of the various
// `in` nodes into. If a match is found, they will need to jump into the
// body_seq anchor to the correct spot.
DECL_ANCHOR(cond_seq);
// This label is used to indicate the end of the entire node. It is
// jumped to after the entire stack is cleaned up.
LABEL *end_label = NEW_LABEL(location->line);
// This label is used as the fallback for the case match. If no match is
// found, then we jump to this label. This is either an `else` clause or
// an error handler.
LABEL *else_label = NEW_LABEL(location->line);
// We're going to use this to uniquely identify each branch so that we
// can track coverage information.
rb_code_location_t case_location = { 0 };
VALUE branches = Qfalse;
int branch_id = 0;
if (PM_BRANCH_COVERAGE_P(iseq)) {
case_location = pm_code_location(scope_node, (const pm_node_t *) node);
branches = decl_branch_base(iseq, PTR2NUM(node), &case_location, "case");
}
// If there is only one pattern, then the behavior changes a bit. It
// effectively gets treated as a match required node (this is how it is
// represented in the other parser).
bool in_single_pattern = node->else_clause == NULL && node->conditions.size == 1;
// First, we're going to push a bunch of stuff onto the stack that is
// going to serve as our scratch space.
if (in_single_pattern) {
PUSH_INSN(ret, *location, putnil); // key error key
PUSH_INSN(ret, *location, putnil); // key error matchee
PUSH_INSN1(ret, *location, putobject, Qfalse); // key error?
PUSH_INSN(ret, *location, putnil); // error string
}
// Now we're going to compile the value to match against.
PUSH_INSN(ret, *location, putnil); // deconstruct cache
PM_COMPILE_NOT_POPPED(node->predicate);
// Next, we'll loop through every in clause and compile its body into
// the body_seq anchor and its pattern into the cond_seq anchor. We'll
// make sure the pattern knows how to jump correctly into the body if it
// finds a match.
for (size_t index = 0; index < node->conditions.size; index++) {
const pm_node_t *condition = node->conditions.nodes[index];
RUBY_ASSERT(PM_NODE_TYPE_P(condition, PM_IN_NODE));
const pm_in_node_t *in_node = (const pm_in_node_t *) condition;
const pm_node_location_t in_location = PM_NODE_START_LOCATION(scope_node->parser, in_node);
const pm_node_location_t pattern_location = PM_NODE_START_LOCATION(scope_node->parser, in_node->pattern);
if (branch_id) {
PUSH_INSN(body_seq, in_location, putnil);
}
LABEL *body_label = NEW_LABEL(in_location.line);
PUSH_LABEL(body_seq, body_label);
PUSH_INSN1(body_seq, in_location, adjuststack, INT2FIX(in_single_pattern ? 6 : 2));
// Establish branch coverage for the in clause.
if (PM_BRANCH_COVERAGE_P(iseq)) {
rb_code_location_t branch_location = pm_code_location(scope_node, in_node->statements != NULL ? ((const pm_node_t *) in_node->statements) : ((const pm_node_t *) in_node));
add_trace_branch_coverage(iseq, body_seq, &branch_location, branch_location.beg_pos.column, branch_id++, "in", branches);
}
if (in_node->statements != NULL) {
PM_COMPILE_INTO_ANCHOR(body_seq, (const pm_node_t *) in_node->statements);
}
else if (!popped) {
PUSH_SYNTHETIC_PUTNIL(body_seq, iseq);
}
PUSH_INSNL(body_seq, in_location, jump, end_label);
LABEL *next_pattern_label = NEW_LABEL(pattern_location.line);
PUSH_INSN(cond_seq, pattern_location, dup);
pm_compile_pattern(iseq, scope_node, in_node->pattern, cond_seq, body_label, next_pattern_label, in_single_pattern, false, true, 2);
PUSH_LABEL(cond_seq, next_pattern_label);
LABEL_UNREMOVABLE(next_pattern_label);
}
if (node->else_clause != NULL) {
// If we have an `else` clause, then this becomes our fallback (and
// there is no need to compile in code to potentially raise an
// error).
const pm_else_node_t *else_node = node->else_clause;
PUSH_LABEL(cond_seq, else_label);
PUSH_INSN(cond_seq, *location, pop);
PUSH_INSN(cond_seq, *location, pop);
// Establish branch coverage for the else clause.
if (PM_BRANCH_COVERAGE_P(iseq)) {
rb_code_location_t branch_location = pm_code_location(scope_node, else_node->statements != NULL ? ((const pm_node_t *) else_node->statements) : ((const pm_node_t *) else_node));
add_trace_branch_coverage(iseq, cond_seq, &branch_location, branch_location.beg_pos.column, branch_id, "else", branches);
}
PM_COMPILE_INTO_ANCHOR(cond_seq, (const pm_node_t *) else_node);
PUSH_INSNL(cond_seq, *location, jump, end_label);
PUSH_INSN(cond_seq, *location, putnil);
if (popped) PUSH_INSN(cond_seq, *location, putnil);
}
else {
// Otherwise, if we do not have an `else` clause, we will compile in
// the code to handle raising an appropriate error.
PUSH_LABEL(cond_seq, else_label);
// Establish branch coverage for the implicit else clause.
add_trace_branch_coverage(iseq, cond_seq, &case_location, case_location.beg_pos.column, branch_id, "else", branches);
if (in_single_pattern) {
pm_compile_pattern_error_handler(iseq, scope_node, (const pm_node_t *) node, cond_seq, end_label, popped);
}
else {
PUSH_INSN1(cond_seq, *location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
PUSH_INSN1(cond_seq, *location, putobject, rb_eNoMatchingPatternError);
PUSH_INSN1(cond_seq, *location, topn, INT2FIX(2));
PUSH_SEND(cond_seq, *location, id_core_raise, INT2FIX(2));
PUSH_INSN1(cond_seq, *location, adjuststack, INT2FIX(3));
if (!popped) PUSH_INSN(cond_seq, *location, putnil);
PUSH_INSNL(cond_seq, *location, jump, end_label);
PUSH_INSN1(cond_seq, *location, dupn, INT2FIX(1));
if (popped) PUSH_INSN(cond_seq, *location, putnil);
}
}
// At the end of all of this compilation, we will add the code for the
// conditions first, then the various bodies, then mark the end of the
// entire sequence with the end label.
PUSH_SEQ(ret, cond_seq);
PUSH_SEQ(ret, body_seq);
PUSH_LABEL(ret, end_label);
}
static inline void
pm_compile_forwarding_super_node(rb_iseq_t *iseq, const pm_forwarding_super_node_t *node, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
{
const rb_iseq_t *block = NULL;
const rb_iseq_t *previous_block = NULL;
LABEL *retry_label = NULL;
LABEL *retry_end_l = NULL;
if (node->block != NULL) {
previous_block = ISEQ_COMPILE_DATA(iseq)->current_block;
ISEQ_COMPILE_DATA(iseq)->current_block = NULL;
retry_label = NEW_LABEL(location->line);
retry_end_l = NEW_LABEL(location->line);
PUSH_LABEL(ret, retry_label);
}
else {
iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
}
PUSH_INSN(ret, *location, putself);
int flag = VM_CALL_ZSUPER | VM_CALL_SUPER | VM_CALL_FCALL;
if (node->block != NULL) {
pm_scope_node_t next_scope_node;
pm_scope_node_init((const pm_node_t *) node->block, &next_scope_node, scope_node);
ISEQ_COMPILE_DATA(iseq)->current_block = block = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, location->line);
pm_scope_node_destroy(&next_scope_node);
RB_OBJ_WRITTEN(iseq, Qundef, (VALUE) block);
}
DECL_ANCHOR(args);
struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
const rb_iseq_t *local_iseq = body->local_iseq;
const struct rb_iseq_constant_body *const local_body = ISEQ_BODY(local_iseq);
int argc = 0;
int depth = get_lvar_level(iseq);
if (ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->param.flags.forwardable) {
flag |= VM_CALL_FORWARDING;
pm_local_index_t mult_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_DOT3, 0);
PUSH_GETLOCAL(ret, *location, mult_local.index, mult_local.level);
const struct rb_callinfo *callinfo = new_callinfo(iseq, 0, 0, flag, NULL, block != NULL);
PUSH_INSN2(ret, *location, invokesuperforward, callinfo, block);
if (popped) PUSH_INSN(ret, *location, pop);
if (node->block) {
ISEQ_COMPILE_DATA(iseq)->current_block = previous_block;
}
return;
}
if (local_body->param.flags.has_lead) {
/* required arguments */
for (int i = 0; i < local_body->param.lead_num; i++) {
int idx = local_body->local_table_size - i;
PUSH_GETLOCAL(args, *location, idx, depth);
}
argc += local_body->param.lead_num;
}
if (local_body->param.flags.has_opt) {
/* optional arguments */
for (int j = 0; j < local_body->param.opt_num; j++) {
int idx = local_body->local_table_size - (argc + j);
PUSH_GETLOCAL(args, *location, idx, depth);
}
argc += local_body->param.opt_num;
}
if (local_body->param.flags.has_rest) {
/* rest argument */
int idx = local_body->local_table_size - local_body->param.rest_start;
PUSH_GETLOCAL(args, *location, idx, depth);
PUSH_INSN1(args, *location, 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;
int j = 0;
for (; j < post_len; j++) {
int idx = local_body->local_table_size - (post_start + j);
PUSH_GETLOCAL(args, *location, idx, depth);
}
if (local_body->param.flags.has_rest) {
// argc remains unchanged from rest branch
PUSH_INSN1(args, *location, newarray, INT2FIX(j));
PUSH_INSN(args, *location, concatarray);
}
else {
argc = post_len + post_start;
}
}
const struct rb_iseq_param_keyword *const local_keyword = local_body->param.keyword;
if (local_body->param.flags.has_kw) {
int local_size = local_body->local_table_size;
argc++;
PUSH_INSN1(args, *location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
if (local_body->param.flags.has_kwrest) {
int idx = local_body->local_table_size - local_keyword->rest_start;
PUSH_GETLOCAL(args, *location, idx, depth);
RUBY_ASSERT(local_keyword->num > 0);
PUSH_SEND(args, *location, rb_intern("dup"), INT2FIX(0));
}
else {
PUSH_INSN1(args, *location, newhash, INT2FIX(0));
}
int i = 0;
for (; i < local_keyword->num; ++i) {
ID id = local_keyword->table[i];
int idx = local_size - get_local_var_idx(local_iseq, id);
{
VALUE operand = ID2SYM(id);
PUSH_INSN1(args, *location, putobject, operand);
}
PUSH_GETLOCAL(args, *location, idx, depth);
}
PUSH_SEND(args, *location, 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_keyword->rest_start;
PUSH_GETLOCAL(args, *location, idx, depth);
argc++;
flag |= VM_CALL_KW_SPLAT;
}
PUSH_SEQ(ret, args);
{
const struct rb_callinfo *callinfo = new_callinfo(iseq, 0, argc, flag, NULL, block != NULL);
PUSH_INSN2(ret, *location, invokesuper, callinfo, block);
}
if (node->block != NULL) {
pm_compile_retry_end_label(iseq, ret, retry_end_l);
PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, block, retry_end_l);
ISEQ_COMPILE_DATA(iseq)->current_block = previous_block;
}
if (popped) PUSH_INSN(ret, *location, pop);
}
static inline void
pm_compile_match_required_node(rb_iseq_t *iseq, const pm_match_required_node_t *node, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
{
LABEL *matched_label = NEW_LABEL(location->line);
LABEL *unmatched_label = NEW_LABEL(location->line);
LABEL *done_label = NEW_LABEL(location->line);
// First, we're going to push a bunch of stuff onto the stack that is
// going to serve as our scratch space.
PUSH_INSN(ret, *location, putnil); // key error key
PUSH_INSN(ret, *location, putnil); // key error matchee
PUSH_INSN1(ret, *location, putobject, Qfalse); // key error?
PUSH_INSN(ret, *location, putnil); // error string
PUSH_INSN(ret, *location, putnil); // deconstruct cache
// Next we're going to compile the value expression such that it's on
// the stack.
PM_COMPILE_NOT_POPPED(node->value);
// Here we'll dup it so that it can be used for comparison, but also be
// used for error handling.
PUSH_INSN(ret, *location, dup);
// Next we'll compile the pattern. We indicate to the pm_compile_pattern
// function that this is the only pattern that will be matched against
// through the in_single_pattern parameter. We also indicate that the
// value to compare against is 2 slots from the top of the stack (the
// base_index parameter).
pm_compile_pattern(iseq, scope_node, node->pattern, ret, matched_label, unmatched_label, true, false, true, 2);
// If the pattern did not match the value, then we're going to compile
// in our error handler code. This will determine which error to raise
// and raise it.
PUSH_LABEL(ret, unmatched_label);
pm_compile_pattern_error_handler(iseq, scope_node, (const pm_node_t *) node, ret, done_label, popped);
// If the pattern did match, we'll clean up the values we've pushed onto
// the stack and then push nil onto the stack if it's not popped.
PUSH_LABEL(ret, matched_label);
PUSH_INSN1(ret, *location, adjuststack, INT2FIX(6));
if (!popped) PUSH_INSN(ret, *location, putnil);
PUSH_INSNL(ret, *location, jump, done_label);
PUSH_LABEL(ret, done_label);
}
static inline void
pm_compile_match_write_node(rb_iseq_t *iseq, const pm_match_write_node_t *node, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
{
LABEL *fail_label = NEW_LABEL(location->line);
LABEL *end_label = NEW_LABEL(location->line);
// First, we'll compile the call so that all of its instructions are
// present. Then we'll compile all of the local variable targets.
PM_COMPILE_NOT_POPPED((const pm_node_t *) node->call);
// Now, check if the match was successful. If it was, then we'll
// continue on and assign local variables. Otherwise we'll skip over the
// assignment code.
{
VALUE operand = rb_id2sym(idBACKREF);
PUSH_INSN1(ret, *location, getglobal, operand);
}
PUSH_INSN(ret, *location, dup);
PUSH_INSNL(ret, *location, branchunless, fail_label);
// If there's only a single local variable target, we can skip some of
// the bookkeeping, so we'll put a special branch here.
size_t targets_count = node->targets.size;
if (targets_count == 1) {
const pm_node_t *target = node->targets.nodes[0];
RUBY_ASSERT(PM_NODE_TYPE_P(target, PM_LOCAL_VARIABLE_TARGET_NODE));
const pm_local_variable_target_node_t *local_target = (const pm_local_variable_target_node_t *) target;
pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, local_target->name, local_target->depth);
{
VALUE operand = rb_id2sym(pm_constant_id_lookup(scope_node, local_target->name));
PUSH_INSN1(ret, *location, putobject, operand);
}
PUSH_SEND(ret, *location, idAREF, INT2FIX(1));
PUSH_LABEL(ret, fail_label);
PUSH_SETLOCAL(ret, *location, index.index, index.level);
if (popped) PUSH_INSN(ret, *location, pop);
return;
}
DECL_ANCHOR(fail_anchor);
// Otherwise there is more than one local variable target, so we'll need
// to do some bookkeeping.
for (size_t targets_index = 0; targets_index < targets_count; targets_index++) {
const pm_node_t *target = node->targets.nodes[targets_index];
RUBY_ASSERT(PM_NODE_TYPE_P(target, PM_LOCAL_VARIABLE_TARGET_NODE));
const pm_local_variable_target_node_t *local_target = (const pm_local_variable_target_node_t *) target;
pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, local_target->name, local_target->depth);
if (((size_t) targets_index) < (targets_count - 1)) {
PUSH_INSN(ret, *location, dup);
}
{
VALUE operand = rb_id2sym(pm_constant_id_lookup(scope_node, local_target->name));
PUSH_INSN1(ret, *location, putobject, operand);
}
PUSH_SEND(ret, *location, idAREF, INT2FIX(1));
PUSH_SETLOCAL(ret, *location, index.index, index.level);
PUSH_INSN(fail_anchor, *location, putnil);
PUSH_SETLOCAL(fail_anchor, *location, index.index, index.level);
}
// Since we matched successfully, now we'll jump to the end.
PUSH_INSNL(ret, *location, jump, end_label);
// In the case that the match failed, we'll loop through each local
// variable target and set all of them to `nil`.
PUSH_LABEL(ret, fail_label);
PUSH_INSN(ret, *location, pop);
PUSH_SEQ(ret, fail_anchor);
// Finally, we can push the end label for either case.
PUSH_LABEL(ret, end_label);
if (popped) PUSH_INSN(ret, *location, pop);
}
static inline void
pm_compile_next_node(rb_iseq_t *iseq, const pm_next_node_t *node, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
{
if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
LABEL *splabel = NEW_LABEL(0);
PUSH_LABEL(ret, splabel);
if (node->arguments) {
PM_COMPILE_NOT_POPPED((const pm_node_t *) node->arguments);
}
else {
PUSH_INSN(ret, *location, putnil);
}
pm_add_ensure_iseq(ret, iseq, 0, scope_node);
PUSH_ADJUST(ret, *location, ISEQ_COMPILE_DATA(iseq)->redo_label);
PUSH_INSNL(ret, *location, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
PUSH_ADJUST_RESTORE(ret, splabel);
if (!popped) PUSH_INSN(ret, *location, putnil);
}
else if (ISEQ_COMPILE_DATA(iseq)->end_label && can_add_ensure_iseq(iseq)) {
LABEL *splabel = NEW_LABEL(0);
PUSH_LABEL(ret, splabel);
PUSH_ADJUST(ret, *location, ISEQ_COMPILE_DATA(iseq)->start_label);
if (node->arguments != NULL) {
PM_COMPILE_NOT_POPPED((const pm_node_t *) node->arguments);
}
else {
PUSH_INSN(ret, *location, putnil);
}
pm_add_ensure_iseq(ret, iseq, 0, scope_node);
PUSH_INSNL(ret, *location, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
PUSH_ADJUST_RESTORE(ret, splabel);
splabel->unremovable = FALSE;
if (!popped) PUSH_INSN(ret, *location, putnil);
}
else {
const rb_iseq_t *ip = iseq;
unsigned long throw_flag = 0;
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(iseq, location->line, "Invalid next");
return;
}
ip = ISEQ_BODY(ip)->parent_iseq;
}
if (ip != 0) {
if (node->arguments) {
PM_COMPILE_NOT_POPPED((const pm_node_t *) node->arguments);
}
else {
PUSH_INSN(ret, *location, putnil);
}
PUSH_INSN1(ret, *location, throw, INT2FIX(throw_flag | TAG_NEXT));
if (popped) PUSH_INSN(ret, *location, pop);
}
else {
COMPILE_ERROR(iseq, location->line, "Invalid next");
}
}
}
static inline void
pm_compile_redo_node(rb_iseq_t *iseq, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
{
if (ISEQ_COMPILE_DATA(iseq)->redo_label && can_add_ensure_iseq(iseq)) {
LABEL *splabel = NEW_LABEL(0);
PUSH_LABEL(ret, splabel);
PUSH_ADJUST(ret, *location, ISEQ_COMPILE_DATA(iseq)->redo_label);
pm_add_ensure_iseq(ret, iseq, 0, scope_node);
PUSH_INSNL(ret, *location, jump, ISEQ_COMPILE_DATA(iseq)->redo_label);
PUSH_ADJUST_RESTORE(ret, splabel);
if (!popped) PUSH_INSN(ret, *location, 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);
PUSH_LABEL(ret, splabel);
pm_add_ensure_iseq(ret, iseq, 0, scope_node);
PUSH_ADJUST(ret, *location, ISEQ_COMPILE_DATA(iseq)->start_label);
PUSH_INSNL(ret, *location, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
PUSH_ADJUST_RESTORE(ret, splabel);
if (!popped) PUSH_INSN(ret, *location, 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(iseq, location->line, "Invalid redo");
return;
}
ip = ISEQ_BODY(ip)->parent_iseq;
}
if (ip != 0) {
PUSH_INSN(ret, *location, putnil);
PUSH_INSN1(ret, *location, throw, INT2FIX(VM_THROW_NO_ESCAPE_FLAG | TAG_REDO));
if (popped) PUSH_INSN(ret, *location, pop);
}
else {
COMPILE_ERROR(iseq, location->line, "Invalid redo");
}
}
}
static inline void
pm_compile_rescue_node(rb_iseq_t *iseq, const pm_rescue_node_t *node, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
{
iseq_set_exception_local_table(iseq);
// First, establish the labels that we need to be able to jump to within
// this compilation block.
LABEL *exception_match_label = NEW_LABEL(location->line);
LABEL *rescue_end_label = NEW_LABEL(location->line);
// Next, compile each of the exceptions that we're going to be
// handling. For each one, we'll add instructions to check if the
// exception matches the raised one, and if it does then jump to the
// exception_match_label label. Otherwise it will fall through to the
// subsequent check. If there are no exceptions, we'll only check
// StandardError.
const pm_node_list_t *exceptions = &node->exceptions;
if (exceptions->size > 0) {
for (size_t index = 0; index < exceptions->size; index++) {
PUSH_GETLOCAL(ret, *location, LVAR_ERRINFO, 0);
PM_COMPILE(exceptions->nodes[index]);
int checkmatch_flags = VM_CHECKMATCH_TYPE_RESCUE;
if (PM_NODE_TYPE_P(exceptions->nodes[index], PM_SPLAT_NODE)) {
checkmatch_flags |= VM_CHECKMATCH_ARRAY;
}
PUSH_INSN1(ret, *location, checkmatch, INT2FIX(checkmatch_flags));
PUSH_INSNL(ret, *location, branchif, exception_match_label);
}
}
else {
PUSH_GETLOCAL(ret, *location, LVAR_ERRINFO, 0);
PUSH_INSN1(ret, *location, putobject, rb_eStandardError);
PUSH_INSN1(ret, *location, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
PUSH_INSNL(ret, *location, branchif, exception_match_label);
}
// If none of the exceptions that we are matching against matched, then
// we'll jump straight to the rescue_end_label label.
PUSH_INSNL(ret, *location, jump, rescue_end_label);
// Here we have the exception_match_label, which is where the
// control-flow goes in the case that one of the exceptions matched.
// Here we will compile the instructions to handle the exception.
PUSH_LABEL(ret, exception_match_label);
PUSH_TRACE(ret, RUBY_EVENT_RESCUE);
// If we have a reference to the exception, then we'll compile the write
// into the instruction sequence. This can look quite different
// depending on the kind of write being performed.
if (node->reference) {
DECL_ANCHOR(writes);
DECL_ANCHOR(cleanup);
pm_compile_target_node(iseq, node->reference, ret, writes, cleanup, scope_node, NULL);
PUSH_GETLOCAL(ret, *location, LVAR_ERRINFO, 0);
PUSH_SEQ(ret, writes);
PUSH_SEQ(ret, cleanup);
}
// If we have statements to execute, we'll compile them here. Otherwise
// we'll push nil onto the stack.
if (node->statements != NULL) {
// We'll temporarily remove the end_label location from the iseq
// when compiling the statements so that next/redo statements
// inside the body will throw to the correct place instead of
// jumping straight to the end of this iseq
LABEL *prev_end = ISEQ_COMPILE_DATA(iseq)->end_label;
ISEQ_COMPILE_DATA(iseq)->end_label = NULL;
PM_COMPILE((const pm_node_t *) node->statements);
// Now restore the end_label
ISEQ_COMPILE_DATA(iseq)->end_label = prev_end;
}
else {
PUSH_INSN(ret, *location, putnil);
}
PUSH_INSN(ret, *location, leave);
// Here we'll insert the rescue_end_label label, which is jumped to if
// none of the exceptions matched. It will cause the control-flow to
// either jump to the next rescue clause or it will fall through to the
// subsequent instruction returning the raised error.
PUSH_LABEL(ret, rescue_end_label);
if (node->subsequent != NULL) {
PM_COMPILE((const pm_node_t *) node->subsequent);
}
else {
PUSH_GETLOCAL(ret, *location, 1, 0);
}
}
static inline void
pm_compile_return_node(rb_iseq_t *iseq, const pm_return_node_t *node, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
{
const pm_arguments_node_t *arguments = node->arguments;
enum rb_iseq_type type = ISEQ_BODY(iseq)->type;
LABEL *splabel = 0;
const rb_iseq_t *parent_iseq = iseq;
enum rb_iseq_type parent_type = ISEQ_BODY(parent_iseq)->type;
while (parent_type == ISEQ_TYPE_RESCUE || parent_type == ISEQ_TYPE_ENSURE) {
if (!(parent_iseq = ISEQ_BODY(parent_iseq)->parent_iseq)) break;
parent_type = ISEQ_BODY(parent_iseq)->type;
}
switch (parent_type) {
case ISEQ_TYPE_TOP:
case ISEQ_TYPE_MAIN:
if (arguments) {
rb_warn("argument of top-level return is ignored");
}
if (parent_iseq == iseq) {
type = ISEQ_TYPE_METHOD;
}
break;
default:
break;
}
if (type == ISEQ_TYPE_METHOD) {
splabel = NEW_LABEL(0);
PUSH_LABEL(ret, splabel);
PUSH_ADJUST(ret, *location, 0);
}
if (arguments != NULL) {
PM_COMPILE_NOT_POPPED((const pm_node_t *) arguments);
}
else {
PUSH_INSN(ret, *location, putnil);
}
if (type == ISEQ_TYPE_METHOD && can_add_ensure_iseq(iseq)) {
pm_add_ensure_iseq(ret, iseq, 1, scope_node);
PUSH_TRACE(ret, RUBY_EVENT_RETURN);
PUSH_INSN(ret, *location, leave);
PUSH_ADJUST_RESTORE(ret, splabel);
if (!popped) PUSH_INSN(ret, *location, putnil);
}
else {
PUSH_INSN1(ret, *location, throw, INT2FIX(TAG_RETURN));
if (popped) PUSH_INSN(ret, *location, pop);
}
}
static inline void
pm_compile_super_node(rb_iseq_t *iseq, const pm_super_node_t *node, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
{
DECL_ANCHOR(args);
LABEL *retry_label = NEW_LABEL(location->line);
LABEL *retry_end_l = NEW_LABEL(location->line);
const rb_iseq_t *previous_block = ISEQ_COMPILE_DATA(iseq)->current_block;
const rb_iseq_t *current_block;
ISEQ_COMPILE_DATA(iseq)->current_block = current_block = NULL;
PUSH_LABEL(ret, retry_label);
PUSH_INSN(ret, *location, putself);
int flags = 0;
struct rb_callinfo_kwarg *keywords = NULL;
int argc = pm_setup_args(node->arguments, node->block, &flags, &keywords, iseq, ret, scope_node, location);
bool is_forwardable = (node->arguments != NULL) && PM_NODE_FLAG_P(node->arguments, PM_ARGUMENTS_NODE_FLAGS_CONTAINS_FORWARDING);
flags |= VM_CALL_SUPER | VM_CALL_FCALL;
if (node->block && PM_NODE_TYPE_P(node->block, PM_BLOCK_NODE)) {
pm_scope_node_t next_scope_node;
pm_scope_node_init(node->block, &next_scope_node, scope_node);
ISEQ_COMPILE_DATA(iseq)->current_block = current_block = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, location->line);
pm_scope_node_destroy(&next_scope_node);
}
if (!node->block) {
iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
}
if ((flags & VM_CALL_ARGS_BLOCKARG) && (flags & VM_CALL_KW_SPLAT) && !(flags & VM_CALL_KW_SPLAT_MUT)) {
PUSH_INSN(args, *location, splatkw);
}
PUSH_SEQ(ret, args);
if (is_forwardable && ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->param.flags.forwardable) {
flags |= VM_CALL_FORWARDING;
{
const struct rb_callinfo *callinfo = new_callinfo(iseq, 0, argc, flags, keywords, current_block != NULL);
PUSH_INSN2(ret, *location, invokesuperforward, callinfo, current_block);
}
}
else {
{
const struct rb_callinfo *callinfo = new_callinfo(iseq, 0, argc, flags, keywords, current_block != NULL);
PUSH_INSN2(ret, *location, invokesuper, callinfo, current_block);
}
}
pm_compile_retry_end_label(iseq, ret, retry_end_l);
if (popped) PUSH_INSN(ret, *location, pop);
ISEQ_COMPILE_DATA(iseq)->current_block = previous_block;
PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, current_block, retry_end_l);
}
static inline void
pm_compile_yield_node(rb_iseq_t *iseq, const pm_yield_node_t *node, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
{
switch (ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->type) {
case ISEQ_TYPE_TOP:
case ISEQ_TYPE_MAIN:
case ISEQ_TYPE_CLASS:
COMPILE_ERROR(iseq, location->line, "Invalid yield");
return;
default: /* valid */;
}
int argc = 0;
int flags = 0;
struct rb_callinfo_kwarg *keywords = NULL;
if (node->arguments) {
argc = pm_setup_args(node->arguments, NULL, &flags, &keywords, iseq, ret, scope_node, location);
}
const struct rb_callinfo *callinfo = new_callinfo(iseq, 0, argc, flags, keywords, FALSE);
PUSH_INSN1(ret, *location, invokeblock, callinfo);
iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
if (popped) PUSH_INSN(ret, *location, pop);
int level = 0;
for (const rb_iseq_t *tmp_iseq = iseq; 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);
}
/**
* Compiles a prism node into instruction sequences.
*
* iseq - The current instruction sequence object (used for locals)
* node - The prism node to compile
* ret - The linked list of instructions to append instructions onto
* popped - True if compiling something with no side effects, so instructions don't
* need to be added
* scope_node - Stores parser and local information
*/
static void
pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
{
const pm_parser_t *parser = scope_node->parser;
const pm_node_location_t location = PM_NODE_START_LOCATION(parser, node);
int lineno = (int) location.line;
if (PM_NODE_TYPE_P(node, PM_BEGIN_NODE) && (((const pm_begin_node_t *) node)->statements == NULL) && (((const pm_begin_node_t *) node)->rescue_clause != NULL)) {
// If this node is a begin node and it has empty statements and also
// has a rescue clause, then the other parser considers it as
// starting on the same line as the rescue, as opposed to the
// location of the begin keyword. We replicate that behavior here.
lineno = (int) PM_NODE_START_LINE_COLUMN(parser, ((const pm_begin_node_t *) node)->rescue_clause).line;
}
if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_NEWLINE) && ISEQ_COMPILE_DATA(iseq)->last_line != lineno) {
// If this node has the newline flag set and it is on a new line
// from the previous nodes that have been compiled for this ISEQ,
// then we need to emit a newline event.
int event = RUBY_EVENT_LINE;
ISEQ_COMPILE_DATA(iseq)->last_line = lineno;
if (lineno > 0 && ISEQ_COVERAGE(iseq) && ISEQ_LINE_COVERAGE(iseq)) {
event |= RUBY_EVENT_COVERAGE_LINE;
}
PUSH_TRACE(ret, event);
}
switch (PM_NODE_TYPE(node)) {
case PM_ALIAS_GLOBAL_VARIABLE_NODE:
// alias $foo $bar
// ^^^^^^^^^^^^^^^
pm_compile_alias_global_variable_node(iseq, (const pm_alias_global_variable_node_t *) node, &location, ret, popped, scope_node);
return;
case PM_ALIAS_METHOD_NODE:
// alias foo bar
// ^^^^^^^^^^^^^
pm_compile_alias_method_node(iseq, (const pm_alias_method_node_t *) node, &location, ret, popped, scope_node);
return;
case PM_AND_NODE:
// a and b
// ^^^^^^^
pm_compile_and_node(iseq, (const pm_and_node_t *) node, &location, ret, popped, scope_node);
return;
case PM_ARGUMENTS_NODE: {
// break foo
// ^^^
//
// These are ArgumentsNodes that are not compiled directly by their
// parent call nodes, used in the cases of NextNodes, ReturnNodes, and
// BreakNodes. They can create an array like ArrayNode.
const pm_arguments_node_t *cast = (const pm_arguments_node_t *) node;
const pm_node_list_t *elements = &cast->arguments;
if (elements->size == 1) {
// If we are only returning a single element through one of the jump
// nodes, then we will only compile that node directly.
PM_COMPILE(elements->nodes[0]);
}
else {
pm_compile_array_node(iseq, (const pm_node_t *) cast, elements, &location, ret, popped, scope_node);
}
return;
}
case PM_ARRAY_NODE: {
// [foo, bar, baz]
// ^^^^^^^^^^^^^^^
const pm_array_node_t *cast = (const pm_array_node_t *) node;
pm_compile_array_node(iseq, (const pm_node_t *) cast, &cast->elements, &location, ret, popped, scope_node);
return;
}
case PM_ASSOC_NODE: {
// { foo: 1 }
// ^^^^^^
//
// foo(bar: 1)
// ^^^^^^
const pm_assoc_node_t *cast = (const pm_assoc_node_t *) node;
PM_COMPILE(cast->key);
PM_COMPILE(cast->value);
return;
}
case PM_ASSOC_SPLAT_NODE: {
// { **foo }
// ^^^^^
//
// def foo(**); bar(**); end
// ^^
const pm_assoc_splat_node_t *cast = (const pm_assoc_splat_node_t *) node;
if (cast->value != NULL) {
PM_COMPILE(cast->value);
}
else if (!popped) {
pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_POW, 0);
PUSH_GETLOCAL(ret, location, index.index, index.level);
}
return;
}
case PM_BACK_REFERENCE_READ_NODE: {
// $+
// ^^
if (!popped) {
const pm_back_reference_read_node_t *cast = (const pm_back_reference_read_node_t *) node;
VALUE backref = pm_compile_back_reference_ref(cast);
PUSH_INSN2(ret, location, getspecial, INT2FIX(1), backref);
}
return;
}
case PM_BEGIN_NODE: {
// begin end
// ^^^^^^^^^
const pm_begin_node_t *cast = (const pm_begin_node_t *) node;
if (cast->ensure_clause) {
// Compiling the ensure clause will compile the rescue clause (if
// there is one), which will compile the begin statements.
pm_compile_ensure(iseq, cast, &location, ret, popped, scope_node);
}
else if (cast->rescue_clause) {
// Compiling rescue will compile begin statements (if applicable).
pm_compile_rescue(iseq, cast, &location, ret, popped, scope_node);
}
else {
// If there is neither ensure or rescue, the just compile the
// statements.
if (cast->statements != NULL) {
PM_COMPILE((const pm_node_t *) cast->statements);
}
else if (!popped) {
PUSH_SYNTHETIC_PUTNIL(ret, iseq);
}
}
return;
}
case PM_BLOCK_ARGUMENT_NODE: {
// foo(&bar)
// ^^^^
const pm_block_argument_node_t *cast = (const pm_block_argument_node_t *) node;
if (cast->expression != NULL) {
PM_COMPILE(cast->expression);
}
else {
// If there's no expression, this must be block forwarding.
pm_local_index_t local_index = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_AND, 0);
PUSH_INSN2(ret, location, getblockparamproxy, INT2FIX(local_index.index + VM_ENV_DATA_SIZE - 1), INT2FIX(local_index.level));
}
return;
}
case PM_BREAK_NODE:
// break
// ^^^^^
//
// break foo
// ^^^^^^^^^
pm_compile_break_node(iseq, (const pm_break_node_t *) node, &location, ret, popped, scope_node);
return;
case PM_CALL_NODE:
// foo
// ^^^
//
// foo.bar
// ^^^^^^^
//
// foo.bar() {}
// ^^^^^^^^^^^^
pm_compile_call_node(iseq, (const pm_call_node_t *) node, ret, popped, scope_node);
return;
case PM_CALL_AND_WRITE_NODE: {
// foo.bar &&= baz
// ^^^^^^^^^^^^^^^
const pm_call_and_write_node_t *cast = (const pm_call_and_write_node_t *) node;
pm_compile_call_and_or_write_node(iseq, true, cast->receiver, cast->value, cast->write_name, cast->read_name, PM_NODE_FLAG_P(cast, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION), &location, ret, popped, scope_node);
return;
}
case PM_CALL_OR_WRITE_NODE: {
// foo.bar ||= baz
// ^^^^^^^^^^^^^^^
const pm_call_or_write_node_t *cast = (const pm_call_or_write_node_t *) node;
pm_compile_call_and_or_write_node(iseq, false, cast->receiver, cast->value, cast->write_name, cast->read_name, PM_NODE_FLAG_P(cast, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION), &location, ret, popped, scope_node);
return;
}
case PM_CALL_OPERATOR_WRITE_NODE:
// foo.bar += baz
// ^^^^^^^^^^^^^^^
//
// Call operator writes occur when you have a call node on the left-hand
// side of a write operator that is not `=`. As an example,
// `foo.bar *= 1`. This breaks down to caching the receiver on the
// stack and then performing three method calls, one to read the value,
// one to compute the result, and one to write the result back to the
// receiver.
pm_compile_call_operator_write_node(iseq, (const pm_call_operator_write_node_t *) node, &location, ret, popped, scope_node);
return;
case PM_CASE_NODE:
// case foo; when bar; end
// ^^^^^^^^^^^^^^^^^^^^^^^
pm_compile_case_node(iseq, (const pm_case_node_t *) node, &location, ret, popped, scope_node);
return;
case PM_CASE_MATCH_NODE:
// case foo; in bar; end
// ^^^^^^^^^^^^^^^^^^^^^
//
// If you use the `case` keyword to create a case match node, it will
// match against all of the `in` clauses until it finds one that
// matches. If it doesn't find one, it can optionally fall back to an
// `else` clause. If none is present and a match wasn't found, it will
// raise an appropriate error.
pm_compile_case_match_node(iseq, (const pm_case_match_node_t *) node, &location, ret, popped, scope_node);
return;
case PM_CLASS_NODE: {
// class Foo; end
// ^^^^^^^^^^^^^^
const pm_class_node_t *cast = (const pm_class_node_t *) node;
ID class_id = pm_constant_id_lookup(scope_node, cast->name);
VALUE class_name = rb_str_freeze(rb_sprintf("<class:%"PRIsVALUE">", rb_id2str(class_id)));
pm_scope_node_t next_scope_node;
pm_scope_node_init((const pm_node_t *) cast, &next_scope_node, scope_node);
const rb_iseq_t *class_iseq = NEW_CHILD_ISEQ(&next_scope_node, class_name, ISEQ_TYPE_CLASS, location.line);
pm_scope_node_destroy(&next_scope_node);
// TODO: Once we merge constant path nodes correctly, fix this flag
const int flags = VM_DEFINECLASS_TYPE_CLASS |
(cast->superclass ? VM_DEFINECLASS_FLAG_HAS_SUPERCLASS : 0) |
pm_compile_class_path(iseq, cast->constant_path, &location, ret, false, scope_node);
if (cast->superclass) {
PM_COMPILE_NOT_POPPED(cast->superclass);
}
else {
PUSH_INSN(ret, location, putnil);
}
{
VALUE operand = ID2SYM(class_id);
PUSH_INSN3(ret, location, defineclass, operand, class_iseq, INT2FIX(flags));
}
RB_OBJ_WRITTEN(iseq, Qundef, (VALUE)class_iseq);
if (popped) PUSH_INSN(ret, location, pop);
return;
}
case PM_CLASS_VARIABLE_AND_WRITE_NODE: {
// @@foo &&= bar
// ^^^^^^^^^^^^^
const pm_class_variable_and_write_node_t *cast = (const pm_class_variable_and_write_node_t *) node;
LABEL *end_label = NEW_LABEL(location.line);
ID name_id = pm_constant_id_lookup(scope_node, cast->name);
VALUE name = ID2SYM(name_id);
PUSH_INSN2(ret, location, getclassvariable, name, get_cvar_ic_value(iseq, name_id));
if (!popped) PUSH_INSN(ret, location, dup);
PUSH_INSNL(ret, location, branchunless, end_label);
if (!popped) PUSH_INSN(ret, location, pop);
PM_COMPILE_NOT_POPPED(cast->value);
if (!popped) PUSH_INSN(ret, location, dup);
PUSH_INSN2(ret, location, setclassvariable, name, get_cvar_ic_value(iseq, name_id));
PUSH_LABEL(ret, end_label);
return;
}
case PM_CLASS_VARIABLE_OPERATOR_WRITE_NODE: {
// @@foo += bar
// ^^^^^^^^^^^^
const pm_class_variable_operator_write_node_t *cast = (const pm_class_variable_operator_write_node_t *) node;
ID name_id = pm_constant_id_lookup(scope_node, cast->name);
VALUE name = ID2SYM(name_id);
PUSH_INSN2(ret, location, getclassvariable, name, get_cvar_ic_value(iseq, name_id));
PM_COMPILE_NOT_POPPED(cast->value);
ID method_id = pm_constant_id_lookup(scope_node, cast->binary_operator);
int flags = VM_CALL_ARGS_SIMPLE;
PUSH_SEND_WITH_FLAG(ret, location, method_id, INT2NUM(1), INT2FIX(flags));
if (!popped) PUSH_INSN(ret, location, dup);
PUSH_INSN2(ret, location, setclassvariable, name, get_cvar_ic_value(iseq, name_id));
return;
}
case PM_CLASS_VARIABLE_OR_WRITE_NODE: {
// @@foo ||= bar
// ^^^^^^^^^^^^^
const pm_class_variable_or_write_node_t *cast = (const pm_class_variable_or_write_node_t *) node;
LABEL *end_label = NEW_LABEL(location.line);
LABEL *start_label = NEW_LABEL(location.line);
ID name_id = pm_constant_id_lookup(scope_node, cast->name);
VALUE name = ID2SYM(name_id);
PUSH_INSN(ret, location, putnil);
PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_CVAR), name, Qtrue);
PUSH_INSNL(ret, location, branchunless, start_label);
PUSH_INSN2(ret, location, getclassvariable, name, get_cvar_ic_value(iseq, name_id));
if (!popped) PUSH_INSN(ret, location, dup);
PUSH_INSNL(ret, location, branchif, end_label);
if (!popped) PUSH_INSN(ret, location, pop);
PUSH_LABEL(ret, start_label);
PM_COMPILE_NOT_POPPED(cast->value);
if (!popped) PUSH_INSN(ret, location, dup);
PUSH_INSN2(ret, location, setclassvariable, name, get_cvar_ic_value(iseq, name_id));
PUSH_LABEL(ret, end_label);
return;
}
case PM_CLASS_VARIABLE_READ_NODE: {
// @@foo
// ^^^^^
if (!popped) {
const pm_class_variable_read_node_t *cast = (const pm_class_variable_read_node_t *) node;
ID name = pm_constant_id_lookup(scope_node, cast->name);
PUSH_INSN2(ret, location, getclassvariable, ID2SYM(name), get_cvar_ic_value(iseq, name));
}
return;
}
case PM_CLASS_VARIABLE_WRITE_NODE: {
// @@foo = 1
// ^^^^^^^^^
const pm_class_variable_write_node_t *cast = (const pm_class_variable_write_node_t *) node;
PM_COMPILE_NOT_POPPED(cast->value);
if (!popped) PUSH_INSN(ret, location, dup);
ID name = pm_constant_id_lookup(scope_node, cast->name);
PUSH_INSN2(ret, location, setclassvariable, ID2SYM(name), get_cvar_ic_value(iseq, name));
return;
}
case PM_CONSTANT_PATH_NODE: {
// Foo::Bar
// ^^^^^^^^
VALUE parts;
if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache && ((parts = pm_constant_path_parts(node, scope_node)) != Qnil)) {
ISEQ_BODY(iseq)->ic_size++;
PUSH_INSN1(ret, location, opt_getconstant_path, parts);
}
else {
DECL_ANCHOR(prefix);
DECL_ANCHOR(body);
pm_compile_constant_path(iseq, node, prefix, body, popped, scope_node);
if (LIST_INSN_SIZE_ZERO(prefix)) {
PUSH_INSN(ret, location, putnil);
}
else {
PUSH_SEQ(ret, prefix);
}
PUSH_SEQ(ret, body);
}
if (popped) PUSH_INSN(ret, location, pop);
return;
}
case PM_CONSTANT_PATH_AND_WRITE_NODE: {
// Foo::Bar &&= baz
// ^^^^^^^^^^^^^^^^
const pm_constant_path_and_write_node_t *cast = (const pm_constant_path_and_write_node_t *) node;
pm_compile_constant_path_and_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
return;
}
case PM_CONSTANT_PATH_OR_WRITE_NODE: {
// Foo::Bar ||= baz
// ^^^^^^^^^^^^^^^^
const pm_constant_path_or_write_node_t *cast = (const pm_constant_path_or_write_node_t *) node;
pm_compile_constant_path_or_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
return;
}
case PM_CONSTANT_PATH_OPERATOR_WRITE_NODE: {
// Foo::Bar += baz
// ^^^^^^^^^^^^^^^
const pm_constant_path_operator_write_node_t *cast = (const pm_constant_path_operator_write_node_t *) node;
pm_compile_constant_path_operator_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
return;
}
case PM_CONSTANT_PATH_WRITE_NODE: {
// Foo::Bar = 1
// ^^^^^^^^^^^^
const pm_constant_path_write_node_t *cast = (const pm_constant_path_write_node_t *) node;
pm_compile_constant_path_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
return;
}
case PM_CONSTANT_READ_NODE: {
// Foo
// ^^^
const pm_constant_read_node_t *cast = (const pm_constant_read_node_t *) node;
VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
pm_compile_constant_read(iseq, name, &cast->base.location, location.node_id, ret, scope_node);
if (popped) PUSH_INSN(ret, location, pop);
return;
}
case PM_CONSTANT_AND_WRITE_NODE: {
// Foo &&= bar
// ^^^^^^^^^^^
const pm_constant_and_write_node_t *cast = (const pm_constant_and_write_node_t *) node;
pm_compile_constant_and_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
return;
}
case PM_CONSTANT_OR_WRITE_NODE: {
// Foo ||= bar
// ^^^^^^^^^^^
const pm_constant_or_write_node_t *cast = (const pm_constant_or_write_node_t *) node;
pm_compile_constant_or_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
return;
}
case PM_CONSTANT_OPERATOR_WRITE_NODE: {
// Foo += bar
// ^^^^^^^^^^
const pm_constant_operator_write_node_t *cast = (const pm_constant_operator_write_node_t *) node;
pm_compile_constant_operator_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
return;
}
case PM_CONSTANT_WRITE_NODE: {
// Foo = 1
// ^^^^^^^
const pm_constant_write_node_t *cast = (const pm_constant_write_node_t *) node;
pm_compile_constant_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
return;
}
case PM_DEF_NODE: {
// def foo; end
// ^^^^^^^^^^^^
//
// def self.foo; end
// ^^^^^^^^^^^^^^^^^
const pm_def_node_t *cast = (const pm_def_node_t *) node;
ID method_name = pm_constant_id_lookup(scope_node, cast->name);
pm_scope_node_t next_scope_node;
pm_scope_node_init((const pm_node_t *) cast, &next_scope_node, scope_node);
rb_iseq_t *method_iseq = NEW_ISEQ(&next_scope_node, rb_id2str(method_name), ISEQ_TYPE_METHOD, location.line);
pm_scope_node_destroy(&next_scope_node);
if (cast->receiver) {
PM_COMPILE_NOT_POPPED(cast->receiver);
PUSH_INSN2(ret, location, definesmethod, ID2SYM(method_name), method_iseq);
}
else {
PUSH_INSN2(ret, location, definemethod, ID2SYM(method_name), method_iseq);
}
RB_OBJ_WRITTEN(iseq, Qundef, (VALUE) method_iseq);
if (!popped) {
PUSH_INSN1(ret, location, putobject, ID2SYM(method_name));
}
return;
}
case PM_DEFINED_NODE: {
// defined?(a)
// ^^^^^^^^^^^
const pm_defined_node_t *cast = (const pm_defined_node_t *) node;
pm_compile_defined_expr(iseq, cast->value, &location, ret, popped, scope_node, false);
return;
}
case PM_EMBEDDED_STATEMENTS_NODE: {
// "foo #{bar}"
// ^^^^^^
const pm_embedded_statements_node_t *cast = (const pm_embedded_statements_node_t *) node;
if (cast->statements != NULL) {
PM_COMPILE((const pm_node_t *) (cast->statements));
}
else {
PUSH_SYNTHETIC_PUTNIL(ret, iseq);
}
if (popped) PUSH_INSN(ret, location, pop);
return;
}
case PM_EMBEDDED_VARIABLE_NODE: {
// "foo #@bar"
// ^^^^^
const pm_embedded_variable_node_t *cast = (const pm_embedded_variable_node_t *) node;
PM_COMPILE(cast->variable);
return;
}
case PM_FALSE_NODE: {
// false
// ^^^^^
if (!popped) {
PUSH_INSN1(ret, location, putobject, Qfalse);
}
return;
}
case PM_ENSURE_NODE: {
const pm_ensure_node_t *cast = (const pm_ensure_node_t *) node;
if (cast->statements != NULL) {
PM_COMPILE((const pm_node_t *) cast->statements);
}
return;
}
case PM_ELSE_NODE: {
// if foo then bar else baz end
// ^^^^^^^^^^^^
const pm_else_node_t *cast = (const pm_else_node_t *) node;
if (cast->statements != NULL) {
PM_COMPILE((const pm_node_t *) cast->statements);
}
else if (!popped) {
PUSH_SYNTHETIC_PUTNIL(ret, iseq);
}
return;
}
case PM_FLIP_FLOP_NODE: {
// if foo .. bar; end
// ^^^^^^^^^^
const pm_flip_flop_node_t *cast = (const pm_flip_flop_node_t *) node;
LABEL *final_label = NEW_LABEL(location.line);
LABEL *then_label = NEW_LABEL(location.line);
LABEL *else_label = NEW_LABEL(location.line);
pm_compile_flip_flop(cast, else_label, then_label, iseq, location.line, ret, popped, scope_node);
PUSH_LABEL(ret, then_label);
PUSH_INSN1(ret, location, putobject, Qtrue);
PUSH_INSNL(ret, location, jump, final_label);
PUSH_LABEL(ret, else_label);
PUSH_INSN1(ret, location, putobject, Qfalse);
PUSH_LABEL(ret, final_label);
return;
}
case PM_FLOAT_NODE: {
// 1.0
// ^^^
if (!popped) {
VALUE operand = parse_float((const pm_float_node_t *) node);
PUSH_INSN1(ret, location, putobject, operand);
}
return;
}
case PM_FOR_NODE: {
// for foo in bar do end
// ^^^^^^^^^^^^^^^^^^^^^
const pm_for_node_t *cast = (const pm_for_node_t *) node;
LABEL *retry_label = NEW_LABEL(location.line);
LABEL *retry_end_l = NEW_LABEL(location.line);
// First, compile the collection that we're going to be iterating over.
PUSH_LABEL(ret, retry_label);
PM_COMPILE_NOT_POPPED(cast->collection);
// Next, create the new scope that is going to contain the block that
// will be passed to the each method.
pm_scope_node_t next_scope_node;
pm_scope_node_init((const pm_node_t *) cast, &next_scope_node, scope_node);
const rb_iseq_t *child_iseq = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, location.line);
pm_scope_node_destroy(&next_scope_node);
const rb_iseq_t *prev_block = ISEQ_COMPILE_DATA(iseq)->current_block;
ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq;
// Now, create the method call to each that will be used to iterate over
// the collection, and pass the newly created iseq as the block.
PUSH_SEND_WITH_BLOCK(ret, location, idEach, INT2FIX(0), child_iseq);
pm_compile_retry_end_label(iseq, ret, retry_end_l);
if (popped) PUSH_INSN(ret, location, pop);
ISEQ_COMPILE_DATA(iseq)->current_block = prev_block;
PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, child_iseq, retry_end_l);
return;
}
case PM_FORWARDING_ARGUMENTS_NODE:
rb_bug("Cannot compile a ForwardingArgumentsNode directly\n");
return;
case PM_FORWARDING_SUPER_NODE:
// super
// ^^^^^
//
// super {}
// ^^^^^^^^
pm_compile_forwarding_super_node(iseq, (const pm_forwarding_super_node_t *) node, &location, ret, popped, scope_node);
return;
case PM_GLOBAL_VARIABLE_AND_WRITE_NODE: {
// $foo &&= bar
// ^^^^^^^^^^^^
const pm_global_variable_and_write_node_t *cast = (const pm_global_variable_and_write_node_t *) node;
LABEL *end_label = NEW_LABEL(location.line);
VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
PUSH_INSN1(ret, location, getglobal, name);
if (!popped) PUSH_INSN(ret, location, dup);
PUSH_INSNL(ret, location, branchunless, end_label);
if (!popped) PUSH_INSN(ret, location, pop);
PM_COMPILE_NOT_POPPED(cast->value);
if (!popped) PUSH_INSN(ret, location, dup);
PUSH_INSN1(ret, location, setglobal, name);
PUSH_LABEL(ret, end_label);
return;
}
case PM_GLOBAL_VARIABLE_OPERATOR_WRITE_NODE: {
// $foo += bar
// ^^^^^^^^^^^
const pm_global_variable_operator_write_node_t *cast = (const pm_global_variable_operator_write_node_t *) node;
VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
PUSH_INSN1(ret, location, getglobal, name);
PM_COMPILE_NOT_POPPED(cast->value);
ID method_id = pm_constant_id_lookup(scope_node, cast->binary_operator);
int flags = VM_CALL_ARGS_SIMPLE;
PUSH_SEND_WITH_FLAG(ret, location, method_id, INT2NUM(1), INT2FIX(flags));
if (!popped) PUSH_INSN(ret, location, dup);
PUSH_INSN1(ret, location, setglobal, name);
return;
}
case PM_GLOBAL_VARIABLE_OR_WRITE_NODE: {
// $foo ||= bar
// ^^^^^^^^^^^^
const pm_global_variable_or_write_node_t *cast = (const pm_global_variable_or_write_node_t *) node;
LABEL *set_label = NEW_LABEL(location.line);
LABEL *end_label = NEW_LABEL(location.line);
PUSH_INSN(ret, location, putnil);
VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_GVAR), name, Qtrue);
PUSH_INSNL(ret, location, branchunless, set_label);
PUSH_INSN1(ret, location, getglobal, name);
if (!popped) PUSH_INSN(ret, location, dup);
PUSH_INSNL(ret, location, branchif, end_label);
if (!popped) PUSH_INSN(ret, location, pop);
PUSH_LABEL(ret, set_label);
PM_COMPILE_NOT_POPPED(cast->value);
if (!popped) PUSH_INSN(ret, location, dup);
PUSH_INSN1(ret, location, setglobal, name);
PUSH_LABEL(ret, end_label);
return;
}
case PM_GLOBAL_VARIABLE_READ_NODE: {
// $foo
// ^^^^
const pm_global_variable_read_node_t *cast = (const pm_global_variable_read_node_t *) node;
VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
PUSH_INSN1(ret, location, getglobal, name);
if (popped) PUSH_INSN(ret, location, pop);
return;
}
case PM_GLOBAL_VARIABLE_WRITE_NODE: {
// $foo = 1
// ^^^^^^^^
const pm_global_variable_write_node_t *cast = (const pm_global_variable_write_node_t *) node;
PM_COMPILE_NOT_POPPED(cast->value);
if (!popped) PUSH_INSN(ret, location, dup);
ID name = pm_constant_id_lookup(scope_node, cast->name);
PUSH_INSN1(ret, location, setglobal, ID2SYM(name));
return;
}
case PM_HASH_NODE: {
// {}
// ^^
//
// If every node in the hash is static, then we can compile the entire
// hash now instead of later.
if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
// We're only going to compile this node if it's not popped. If it
// is popped, then we know we don't need to do anything since it's
// statically known.
if (!popped) {
const pm_hash_node_t *cast = (const pm_hash_node_t *) node;
if (cast->elements.size == 0) {
PUSH_INSN1(ret, location, newhash, INT2FIX(0));
}
else {
VALUE value = pm_static_literal_value(iseq, node, scope_node);
PUSH_INSN1(ret, location, duphash, value);
RB_OBJ_WRITTEN(iseq, Qundef, value);
}
}
}
else {
// Here since we know there are possible side-effects inside the
// hash contents, we're going to build it entirely at runtime. We'll
// do this by pushing all of the key-value pairs onto the stack and
// then combining them with newhash.
//
// If this hash is popped, then this serves only to ensure we enact
// all side-effects (like method calls) that are contained within
// the hash contents.
const pm_hash_node_t *cast = (const pm_hash_node_t *) node;
const pm_node_list_t *elements = &cast->elements;
if (popped) {
// If this hash is popped, then we can iterate through each
// element and compile it. The result of each compilation will
// only include the side effects of the element itself.
for (size_t index = 0; index < elements->size; index++) {
PM_COMPILE_POPPED(elements->nodes[index]);
}
}
else {
pm_compile_hash_elements(iseq, node, elements, 0, Qundef, false, ret, scope_node);
}
}
return;
}
case PM_IF_NODE: {
// if foo then bar end
// ^^^^^^^^^^^^^^^^^^^
//
// bar if foo
// ^^^^^^^^^^
//
// foo ? bar : baz
// ^^^^^^^^^^^^^^^
const pm_if_node_t *cast = (const pm_if_node_t *) node;
pm_compile_conditional(iseq, &location, PM_IF_NODE, (const pm_node_t *) cast, cast->statements, cast->subsequent, cast->predicate, ret, popped, scope_node);
return;
}
case PM_IMAGINARY_NODE: {
// 1i
// ^^
if (!popped) {
VALUE operand = parse_imaginary((const pm_imaginary_node_t *) node);
PUSH_INSN1(ret, location, putobject, operand);
}
return;
}
case PM_IMPLICIT_NODE: {
// Implicit nodes mark places in the syntax tree where explicit syntax
// was omitted, but implied. For example,
//
// { foo: }
//
// In this case a method call/local variable read is implied by virtue
// of the missing value. To compile these nodes, we simply compile the
// value that is implied, which is helpfully supplied by the parser.
const pm_implicit_node_t *cast = (const pm_implicit_node_t *) node;
PM_COMPILE(cast->value);
return;
}
case PM_IN_NODE: {
// In nodes are handled by the case match node directly, so we should
// never end up hitting them through this path.
rb_bug("Should not ever enter an in node directly");
return;
}
case PM_INDEX_OPERATOR_WRITE_NODE: {
// foo[bar] += baz
// ^^^^^^^^^^^^^^^
const pm_index_operator_write_node_t *cast = (const pm_index_operator_write_node_t *) node;
pm_compile_index_operator_write_node(iseq, cast, &location, ret, popped, scope_node);
return;
}
case PM_INDEX_AND_WRITE_NODE: {
// foo[bar] &&= baz
// ^^^^^^^^^^^^^^^^
const pm_index_and_write_node_t *cast = (const pm_index_and_write_node_t *) node;
pm_compile_index_control_flow_write_node(iseq, node, cast->receiver, cast->arguments, cast->block, cast->value, &location, ret, popped, scope_node);
return;
}
case PM_INDEX_OR_WRITE_NODE: {
// foo[bar] ||= baz
// ^^^^^^^^^^^^^^^^
const pm_index_or_write_node_t *cast = (const pm_index_or_write_node_t *) node;
pm_compile_index_control_flow_write_node(iseq, node, cast->receiver, cast->arguments, cast->block, cast->value, &location, ret, popped, scope_node);
return;
}
case PM_INSTANCE_VARIABLE_AND_WRITE_NODE: {
// @foo &&= bar
// ^^^^^^^^^^^^
const pm_instance_variable_and_write_node_t *cast = (const pm_instance_variable_and_write_node_t *) node;
LABEL *end_label = NEW_LABEL(location.line);
ID name_id = pm_constant_id_lookup(scope_node, cast->name);
VALUE name = ID2SYM(name_id);
PUSH_INSN2(ret, location, getinstancevariable, name, get_ivar_ic_value(iseq, name_id));
if (!popped) PUSH_INSN(ret, location, dup);
PUSH_INSNL(ret, location, branchunless, end_label);
if (!popped) PUSH_INSN(ret, location, pop);
PM_COMPILE_NOT_POPPED(cast->value);
if (!popped) PUSH_INSN(ret, location, dup);
PUSH_INSN2(ret, location, setinstancevariable, name, get_ivar_ic_value(iseq, name_id));
PUSH_LABEL(ret, end_label);
return;
}
case PM_INSTANCE_VARIABLE_OPERATOR_WRITE_NODE: {
// @foo += bar
// ^^^^^^^^^^^
const pm_instance_variable_operator_write_node_t *cast = (const pm_instance_variable_operator_write_node_t *) node;
ID name_id = pm_constant_id_lookup(scope_node, cast->name);
VALUE name = ID2SYM(name_id);
PUSH_INSN2(ret, location, getinstancevariable, name, get_ivar_ic_value(iseq, name_id));
PM_COMPILE_NOT_POPPED(cast->value);
ID method_id = pm_constant_id_lookup(scope_node, cast->binary_operator);
int flags = VM_CALL_ARGS_SIMPLE;
PUSH_SEND_WITH_FLAG(ret, location, method_id, INT2NUM(1), INT2FIX(flags));
if (!popped) PUSH_INSN(ret, location, dup);
PUSH_INSN2(ret, location, setinstancevariable, name, get_ivar_ic_value(iseq, name_id));
return;
}
case PM_INSTANCE_VARIABLE_OR_WRITE_NODE: {
// @foo ||= bar
// ^^^^^^^^^^^^
const pm_instance_variable_or_write_node_t *cast = (const pm_instance_variable_or_write_node_t *) node;
LABEL *end_label = NEW_LABEL(location.line);
ID name_id = pm_constant_id_lookup(scope_node, cast->name);
VALUE name = ID2SYM(name_id);
PUSH_INSN2(ret, location, getinstancevariable, name, get_ivar_ic_value(iseq, name_id));
if (!popped) PUSH_INSN(ret, location, dup);
PUSH_INSNL(ret, location, branchif, end_label);
if (!popped) PUSH_INSN(ret, location, pop);
PM_COMPILE_NOT_POPPED(cast->value);
if (!popped) PUSH_INSN(ret, location, dup);
PUSH_INSN2(ret, location, setinstancevariable, name, get_ivar_ic_value(iseq, name_id));
PUSH_LABEL(ret, end_label);
return;
}
case PM_INSTANCE_VARIABLE_READ_NODE: {
// @foo
// ^^^^
if (!popped) {
const pm_instance_variable_read_node_t *cast = (const pm_instance_variable_read_node_t *) node;
ID name = pm_constant_id_lookup(scope_node, cast->name);
PUSH_INSN2(ret, location, getinstancevariable, ID2SYM(name), get_ivar_ic_value(iseq, name));
}
return;
}
case PM_INSTANCE_VARIABLE_WRITE_NODE: {
// @foo = 1
// ^^^^^^^^
const pm_instance_variable_write_node_t *cast = (const pm_instance_variable_write_node_t *) node;
PM_COMPILE_NOT_POPPED(cast->value);
if (!popped) PUSH_INSN(ret, location, dup);
ID name = pm_constant_id_lookup(scope_node, cast->name);
PUSH_INSN2(ret, location, setinstancevariable, ID2SYM(name), get_ivar_ic_value(iseq, name));
return;
}
case PM_INTEGER_NODE: {
// 1
// ^
if (!popped) {
VALUE operand = parse_integer((const pm_integer_node_t *) node);
PUSH_INSN1(ret, location, putobject, operand);
}
return;
}
case PM_INTERPOLATED_MATCH_LAST_LINE_NODE: {
// if /foo #{bar}/ then end
// ^^^^^^^^^^^^
if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
if (!popped) {
VALUE regexp = pm_static_literal_value(iseq, node, scope_node);
PUSH_INSN1(ret, location, putobject, regexp);
}
}
else {
pm_compile_regexp_dynamic(iseq, node, &((const pm_interpolated_match_last_line_node_t *) node)->parts, &location, ret, popped, scope_node);
}
PUSH_INSN1(ret, location, getglobal, rb_id2sym(idLASTLINE));
PUSH_SEND(ret, location, idEqTilde, INT2NUM(1));
if (popped) PUSH_INSN(ret, location, pop);
return;
}
case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE: {
// /foo #{bar}/
// ^^^^^^^^^^^^
if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_ONCE)) {
const rb_iseq_t *prevblock = ISEQ_COMPILE_DATA(iseq)->current_block;
const rb_iseq_t *block_iseq = NULL;
int ise_index = ISEQ_BODY(iseq)->ise_size++;
pm_scope_node_t next_scope_node;
pm_scope_node_init(node, &next_scope_node, scope_node);
block_iseq = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_PLAIN, location.line);
pm_scope_node_destroy(&next_scope_node);
ISEQ_COMPILE_DATA(iseq)->current_block = block_iseq;
PUSH_INSN2(ret, location, once, block_iseq, INT2FIX(ise_index));
ISEQ_COMPILE_DATA(iseq)->current_block = prevblock;
if (popped) PUSH_INSN(ret, location, pop);
return;
}
if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
if (!popped) {
VALUE regexp = pm_static_literal_value(iseq, node, scope_node);
PUSH_INSN1(ret, location, putobject, regexp);
}
}
else {
pm_compile_regexp_dynamic(iseq, node, &((const pm_interpolated_regular_expression_node_t *) node)->parts, &location, ret, popped, scope_node);
if (popped) PUSH_INSN(ret, location, pop);
}
return;
}
case PM_INTERPOLATED_STRING_NODE: {
// "foo #{bar}"
// ^^^^^^^^^^^^
if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
if (!popped) {
VALUE string = pm_static_literal_value(iseq, node, scope_node);
if (PM_NODE_FLAG_P(node, PM_INTERPOLATED_STRING_NODE_FLAGS_FROZEN)) {
PUSH_INSN1(ret, location, putobject, string);
}
else if (PM_NODE_FLAG_P(node, PM_INTERPOLATED_STRING_NODE_FLAGS_MUTABLE)) {
PUSH_INSN1(ret, location, putstring, string);
}
else {
PUSH_INSN1(ret, location, putchilledstring, string);
}
}
}
else {
const pm_interpolated_string_node_t *cast = (const pm_interpolated_string_node_t *) node;
int length = pm_interpolated_node_compile(iseq, &cast->parts, &location, ret, popped, scope_node, NULL, NULL, PM_NODE_FLAG_P(cast, PM_INTERPOLATED_STRING_NODE_FLAGS_MUTABLE), PM_NODE_FLAG_P(cast, PM_INTERPOLATED_STRING_NODE_FLAGS_FROZEN));
if (length > 1) PUSH_INSN1(ret, location, concatstrings, INT2FIX(length));
if (popped) PUSH_INSN(ret, location, pop);
}
return;
}
case PM_INTERPOLATED_SYMBOL_NODE: {
// :"foo #{bar}"
// ^^^^^^^^^^^^^
const pm_interpolated_symbol_node_t *cast = (const pm_interpolated_symbol_node_t *) node;
int length = pm_interpolated_node_compile(iseq, &cast->parts, &location, ret, popped, scope_node, NULL, NULL, false, false);
if (length > 1) {
PUSH_INSN1(ret, location, concatstrings, INT2FIX(length));
}
if (!popped) {
PUSH_INSN(ret, location, intern);
}
else {
PUSH_INSN(ret, location, pop);
}
return;
}
case PM_INTERPOLATED_X_STRING_NODE: {
// `foo #{bar}`
// ^^^^^^^^^^^^
const pm_interpolated_x_string_node_t *cast = (const pm_interpolated_x_string_node_t *) node;
PUSH_INSN(ret, location, putself);
int length = pm_interpolated_node_compile(iseq, &cast->parts, &location, ret, false, scope_node, NULL, NULL, false, false);
if (length > 1) PUSH_INSN1(ret, location, concatstrings, INT2FIX(length));
PUSH_SEND_WITH_FLAG(ret, location, idBackquote, INT2NUM(1), INT2FIX(VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE));
if (popped) PUSH_INSN(ret, location, pop);
return;
}
case PM_IT_LOCAL_VARIABLE_READ_NODE: {
// -> { it }
// ^^
if (!popped) {
pm_scope_node_t *current_scope_node = scope_node;
int level = 0;
while (current_scope_node) {
if (current_scope_node->parameters && PM_NODE_TYPE_P(current_scope_node->parameters, PM_IT_PARAMETERS_NODE)) {
PUSH_GETLOCAL(ret, location, current_scope_node->local_table_for_iseq_size, level);
return;
}
current_scope_node = current_scope_node->previous;
level++;
}
rb_bug("Local `it` does not exist");
}
return;
}
case PM_KEYWORD_HASH_NODE: {
// foo(bar: baz)
// ^^^^^^^^
const pm_keyword_hash_node_t *cast = (const pm_keyword_hash_node_t *) node;
const pm_node_list_t *elements = &cast->elements;
const pm_node_t *element;
PM_NODE_LIST_FOREACH(elements, index, element) {
PM_COMPILE(element);
}
if (!popped) PUSH_INSN1(ret, location, newhash, INT2FIX(elements->size * 2));
return;
}
case PM_LAMBDA_NODE: {
// -> {}
// ^^^^^
const pm_lambda_node_t *cast = (const pm_lambda_node_t *) node;
pm_scope_node_t next_scope_node;
pm_scope_node_init(node, &next_scope_node, scope_node);
int opening_lineno = pm_location_line_number(parser, &cast->opening_loc);
const rb_iseq_t *block = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, opening_lineno);
pm_scope_node_destroy(&next_scope_node);
VALUE argc = INT2FIX(0);
PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
PUSH_CALL_WITH_BLOCK(ret, location, idLambda, argc, block);
RB_OBJ_WRITTEN(iseq, Qundef, (VALUE) block);
if (popped) PUSH_INSN(ret, location, pop);
return;
}
case PM_LOCAL_VARIABLE_AND_WRITE_NODE: {
// foo &&= bar
// ^^^^^^^^^^^
const pm_local_variable_and_write_node_t *cast = (const pm_local_variable_and_write_node_t *) node;
LABEL *end_label = NEW_LABEL(location.line);
pm_local_index_t local_index = pm_lookup_local_index(iseq, scope_node, cast->name, cast->depth);
PUSH_GETLOCAL(ret, location, local_index.index, local_index.level);
if (!popped) PUSH_INSN(ret, location, dup);
PUSH_INSNL(ret, location, branchunless, end_label);
if (!popped) PUSH_INSN(ret, location, pop);
PM_COMPILE_NOT_POPPED(cast->value);
if (!popped) PUSH_INSN(ret, location, dup);
PUSH_SETLOCAL(ret, location, local_index.index, local_index.level);
PUSH_LABEL(ret, end_label);
return;
}
case PM_LOCAL_VARIABLE_OPERATOR_WRITE_NODE: {
// foo += bar
// ^^^^^^^^^^
const pm_local_variable_operator_write_node_t *cast = (const pm_local_variable_operator_write_node_t *) node;
pm_local_index_t local_index = pm_lookup_local_index(iseq, scope_node, cast->name, cast->depth);
PUSH_GETLOCAL(ret, location, local_index.index, local_index.level);
PM_COMPILE_NOT_POPPED(cast->value);
ID method_id = pm_constant_id_lookup(scope_node, cast->binary_operator);
PUSH_SEND_WITH_FLAG(ret, location, method_id, INT2NUM(1), INT2FIX(VM_CALL_ARGS_SIMPLE));
if (!popped) PUSH_INSN(ret, location, dup);
PUSH_SETLOCAL(ret, location, local_index.index, local_index.level);
return;
}
case PM_LOCAL_VARIABLE_OR_WRITE_NODE: {
// foo ||= bar
// ^^^^^^^^^^^
const pm_local_variable_or_write_node_t *cast = (const pm_local_variable_or_write_node_t *) node;
LABEL *set_label = NEW_LABEL(location.line);
LABEL *end_label = NEW_LABEL(location.line);
PUSH_INSN1(ret, location, putobject, Qtrue);
PUSH_INSNL(ret, location, branchunless, set_label);
pm_local_index_t local_index = pm_lookup_local_index(iseq, scope_node, cast->name, cast->depth);
PUSH_GETLOCAL(ret, location, local_index.index, local_index.level);
if (!popped) PUSH_INSN(ret, location, dup);
PUSH_INSNL(ret, location, branchif, end_label);
if (!popped) PUSH_INSN(ret, location, pop);
PUSH_LABEL(ret, set_label);
PM_COMPILE_NOT_POPPED(cast->value);
if (!popped) PUSH_INSN(ret, location, dup);
PUSH_SETLOCAL(ret, location, local_index.index, local_index.level);
PUSH_LABEL(ret, end_label);
return;
}
case PM_LOCAL_VARIABLE_READ_NODE: {
// foo
// ^^^
if (!popped) {
const pm_local_variable_read_node_t *cast = (const pm_local_variable_read_node_t *) node;
pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, cast->name, cast->depth);
PUSH_GETLOCAL(ret, location, index.index, index.level);
}
return;
}
case PM_LOCAL_VARIABLE_WRITE_NODE: {
// foo = 1
// ^^^^^^^
const pm_local_variable_write_node_t *cast = (const pm_local_variable_write_node_t *) node;
PM_COMPILE_NOT_POPPED(cast->value);
if (!popped) PUSH_INSN(ret, location, dup);
pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, cast->name, cast->depth);
PUSH_SETLOCAL(ret, location, index.index, index.level);
return;
}
case PM_MATCH_LAST_LINE_NODE: {
// if /foo/ then end
// ^^^^^
VALUE regexp = pm_static_literal_value(iseq, node, scope_node);
PUSH_INSN1(ret, location, putobject, regexp);
PUSH_INSN2(ret, location, getspecial, INT2FIX(0), INT2FIX(0));
PUSH_SEND(ret, location, idEqTilde, INT2NUM(1));
if (popped) PUSH_INSN(ret, location, pop);
return;
}
case PM_MATCH_PREDICATE_NODE: {
// foo in bar
// ^^^^^^^^^^
const pm_match_predicate_node_t *cast = (const pm_match_predicate_node_t *) node;
// First, allocate some stack space for the cached return value of any
// calls to #deconstruct.
PUSH_INSN(ret, location, putnil);
// Next, compile the expression that we're going to match against.
PM_COMPILE_NOT_POPPED(cast->value);
PUSH_INSN(ret, location, dup);
// Now compile the pattern that is going to be used to match against the
// expression.
LABEL *matched_label = NEW_LABEL(location.line);
LABEL *unmatched_label = NEW_LABEL(location.line);
LABEL *done_label = NEW_LABEL(location.line);
pm_compile_pattern(iseq, scope_node, cast->pattern, ret, matched_label, unmatched_label, false, false, true, 2);
// If the pattern did not match, then compile the necessary instructions
// to handle pushing false onto the stack, then jump to the end.
PUSH_LABEL(ret, unmatched_label);
PUSH_INSN(ret, location, pop);
PUSH_INSN(ret, location, pop);
if (!popped) PUSH_INSN1(ret, location, putobject, Qfalse);
PUSH_INSNL(ret, location, jump, done_label);
PUSH_INSN(ret, location, putnil);
// If the pattern did match, then compile the necessary instructions to
// handle pushing true onto the stack, then jump to the end.
PUSH_LABEL(ret, matched_label);
PUSH_INSN1(ret, location, adjuststack, INT2FIX(2));
if (!popped) PUSH_INSN1(ret, location, putobject, Qtrue);
PUSH_INSNL(ret, location, jump, done_label);
PUSH_LABEL(ret, done_label);
return;
}
case PM_MATCH_REQUIRED_NODE:
// foo => bar
// ^^^^^^^^^^
//
// A match required node represents pattern matching against a single
// pattern using the => operator. For example,
//
// foo => bar
//
// This is somewhat analogous to compiling a case match statement with a
// single pattern. In both cases, if the pattern fails it should
// immediately raise an error.
pm_compile_match_required_node(iseq, (const pm_match_required_node_t *) node, &location, ret, popped, scope_node);
return;
case PM_MATCH_WRITE_NODE:
// /(?<foo>foo)/ =~ bar
// ^^^^^^^^^^^^^^^^^^^^
//
// Match write nodes are specialized call nodes that have a regular
// expression with valid named capture groups on the left, the =~
// operator, and some value on the right. The nodes themselves simply
// wrap the call with the local variable targets that will be written
// when the call is executed.
pm_compile_match_write_node(iseq, (const pm_match_write_node_t *) node, &location, ret, popped, scope_node);
return;
case PM_MISSING_NODE:
rb_bug("A pm_missing_node_t should not exist in prism's AST.");
return;
case PM_MODULE_NODE: {
// module Foo; end
// ^^^^^^^^^^^^^^^
const pm_module_node_t *cast = (const pm_module_node_t *) node;
ID module_id = pm_constant_id_lookup(scope_node, cast->name);
VALUE module_name = rb_str_freeze(rb_sprintf("<module:%"PRIsVALUE">", rb_id2str(module_id)));
pm_scope_node_t next_scope_node;
pm_scope_node_init((const pm_node_t *) cast, &next_scope_node, scope_node);
const rb_iseq_t *module_iseq = NEW_CHILD_ISEQ(&next_scope_node, module_name, ISEQ_TYPE_CLASS, location.line);
pm_scope_node_destroy(&next_scope_node);
const int flags = VM_DEFINECLASS_TYPE_MODULE | pm_compile_class_path(iseq, cast->constant_path, &location, ret, false, scope_node);
PUSH_INSN(ret, location, putnil);
PUSH_INSN3(ret, location, defineclass, ID2SYM(module_id), module_iseq, INT2FIX(flags));
RB_OBJ_WRITTEN(iseq, Qundef, (VALUE) module_iseq);
if (popped) PUSH_INSN(ret, location, pop);
return;
}
case PM_REQUIRED_PARAMETER_NODE: {
// def foo(bar); end
// ^^^
const pm_required_parameter_node_t *cast = (const pm_required_parameter_node_t *) node;
pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, cast->name, 0);
PUSH_SETLOCAL(ret, location, index.index, index.level);
return;
}
case PM_MULTI_WRITE_NODE: {
// foo, bar = baz
// ^^^^^^^^^^^^^^
//
// A multi write node represents writing to multiple values using an =
// operator. Importantly these nodes are only parsed when the left-hand
// side of the operator has multiple targets. The right-hand side of the
// operator having multiple targets represents an implicit array
// instead.
const pm_multi_write_node_t *cast = (const pm_multi_write_node_t *) node;
DECL_ANCHOR(writes);
DECL_ANCHOR(cleanup);
pm_multi_target_state_t state = { 0 };
state.position = popped ? 0 : 1;
pm_compile_multi_target_node(iseq, node, ret, writes, cleanup, scope_node, &state);
PM_COMPILE_NOT_POPPED(cast->value);
if (!popped) PUSH_INSN(ret, location, dup);
PUSH_SEQ(ret, writes);
if (!popped && state.stack_size >= 1) {
// Make sure the value on the right-hand side of the = operator is
// being returned before we pop the parent expressions.
PUSH_INSN1(ret, location, setn, INT2FIX(state.stack_size));
}
// Now, we need to go back and modify the topn instructions in order to
// ensure they can correctly retrieve the parent expressions.
pm_multi_target_state_update(&state);
PUSH_SEQ(ret, cleanup);
return;
}
case PM_NEXT_NODE:
// next
// ^^^^
//
// next foo
// ^^^^^^^^
pm_compile_next_node(iseq, (const pm_next_node_t *) node, &location, ret, popped, scope_node);
return;
case PM_NIL_NODE: {
// nil
// ^^^
if (!popped) {
PUSH_INSN(ret, location, putnil);
}
return;
}
case PM_NO_KEYWORDS_PARAMETER_NODE: {
// def foo(**nil); end
// ^^^^^
ISEQ_BODY(iseq)->param.flags.accepts_no_kwarg = TRUE;
return;
}
case PM_NUMBERED_REFERENCE_READ_NODE: {
// $1
// ^^
if (!popped) {
const pm_numbered_reference_read_node_t *cast = (const pm_numbered_reference_read_node_t *) node;
if (cast->number != 0) {
VALUE ref = pm_compile_numbered_reference_ref(cast);
PUSH_INSN2(ret, location, getspecial, INT2FIX(1), ref);
}
else {
PUSH_INSN(ret, location, putnil);
}
}
return;
}
case PM_OR_NODE: {
// a or b
// ^^^^^^
const pm_or_node_t *cast = (const pm_or_node_t *) node;
LABEL *end_label = NEW_LABEL(location.line);
PM_COMPILE_NOT_POPPED(cast->left);
if (!popped) PUSH_INSN(ret, location, dup);
PUSH_INSNL(ret, location, branchif, end_label);
if (!popped) PUSH_INSN(ret, location, pop);
PM_COMPILE(cast->right);
PUSH_LABEL(ret, end_label);
return;
}
case PM_OPTIONAL_PARAMETER_NODE: {
// def foo(bar = 1); end
// ^^^^^^^
const pm_optional_parameter_node_t *cast = (const pm_optional_parameter_node_t *) node;
PM_COMPILE_NOT_POPPED(cast->value);
pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, cast->name, 0);
PUSH_SETLOCAL(ret, location, index.index, index.level);
return;
}
case PM_PARENTHESES_NODE: {
// ()
// ^^
//
// (1)
// ^^^
const pm_parentheses_node_t *cast = (const pm_parentheses_node_t *) node;
if (cast->body != NULL) {
PM_COMPILE(cast->body);
}
else if (!popped) {
PUSH_INSN(ret, location, putnil);
}
return;
}
case PM_PRE_EXECUTION_NODE: {
// BEGIN {}
// ^^^^^^^^
const pm_pre_execution_node_t *cast = (const pm_pre_execution_node_t *) node;
LINK_ANCHOR *outer_pre = scope_node->pre_execution_anchor;
RUBY_ASSERT(outer_pre != NULL);
// BEGIN{} nodes can be nested, so here we're going to do the same thing
// that we did for the top-level compilation where we create two
// anchors and then join them in the correct order into the resulting
// anchor.
DECL_ANCHOR(inner_pre);
scope_node->pre_execution_anchor = inner_pre;
DECL_ANCHOR(inner_body);
if (cast->statements != NULL) {
const pm_node_list_t *body = &cast->statements->body;
for (size_t index = 0; index < body->size; index++) {
pm_compile_node(iseq, body->nodes[index], inner_body, true, scope_node);
}
}
if (!popped) {
PUSH_INSN(inner_body, location, putnil);
}
// Now that everything has been compiled, join both anchors together
// into the correct outer pre execution anchor, and reset the value so
// that subsequent BEGIN{} nodes can be compiled correctly.
PUSH_SEQ(outer_pre, inner_pre);
PUSH_SEQ(outer_pre, inner_body);
scope_node->pre_execution_anchor = outer_pre;
return;
}
case PM_POST_EXECUTION_NODE: {
// END {}
// ^^^^^^
const rb_iseq_t *child_iseq;
const rb_iseq_t *prevblock = ISEQ_COMPILE_DATA(iseq)->current_block;
pm_scope_node_t next_scope_node;
pm_scope_node_init(node, &next_scope_node, scope_node);
child_iseq = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, lineno);
pm_scope_node_destroy(&next_scope_node);
ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq;
int is_index = ISEQ_BODY(iseq)->ise_size++;
PUSH_INSN2(ret, location, once, child_iseq, INT2FIX(is_index));
RB_OBJ_WRITTEN(iseq, Qundef, (VALUE) child_iseq);
if (popped) PUSH_INSN(ret, location, pop);
ISEQ_COMPILE_DATA(iseq)->current_block = prevblock;
return;
}
case PM_RANGE_NODE: {
// 0..5
// ^^^^
const pm_range_node_t *cast = (const pm_range_node_t *) node;
bool exclude_end = PM_NODE_FLAG_P(cast, PM_RANGE_FLAGS_EXCLUDE_END);
if (pm_optimizable_range_item_p(cast->left) && pm_optimizable_range_item_p(cast->right)) {
if (!popped) {
const pm_node_t *left = cast->left;
const pm_node_t *right = cast->right;
VALUE val = rb_range_new(
(left && PM_NODE_TYPE_P(left, PM_INTEGER_NODE)) ? parse_integer((const pm_integer_node_t *) left) : Qnil,
(right && PM_NODE_TYPE_P(right, PM_INTEGER_NODE)) ? parse_integer((const pm_integer_node_t *) right) : Qnil,
exclude_end
);
PUSH_INSN1(ret, location, putobject, val);
}
}
else {
if (cast->left != NULL) {
PM_COMPILE(cast->left);
}
else if (!popped) {
PUSH_INSN(ret, location, putnil);
}
if (cast->right != NULL) {
PM_COMPILE(cast->right);
}
else if (!popped) {
PUSH_INSN(ret, location, putnil);
}
if (!popped) {
PUSH_INSN1(ret, location, newrange, INT2FIX(exclude_end ? 1 : 0));
}
}
return;
}
case PM_RATIONAL_NODE: {
// 1r
// ^^
if (!popped) {
PUSH_INSN1(ret, location, putobject, parse_rational((const pm_rational_node_t *) node));
}
return;
}
case PM_REDO_NODE:
// redo
// ^^^^
pm_compile_redo_node(iseq, &location, ret, popped, scope_node);
return;
case PM_REGULAR_EXPRESSION_NODE: {
// /foo/
// ^^^^^
if (!popped) {
VALUE regexp = pm_static_literal_value(iseq, node, scope_node);
PUSH_INSN1(ret, location, putobject, regexp);
}
return;
}
case PM_RESCUE_NODE:
// begin; rescue; end
// ^^^^^^^
pm_compile_rescue_node(iseq, (const pm_rescue_node_t *) node, &location, ret, popped, scope_node);
return;
case PM_RESCUE_MODIFIER_NODE: {
// foo rescue bar
// ^^^^^^^^^^^^^^
const pm_rescue_modifier_node_t *cast = (const pm_rescue_modifier_node_t *) node;
pm_scope_node_t rescue_scope_node;
pm_scope_node_init((const pm_node_t *) cast, &rescue_scope_node, scope_node);
rb_iseq_t *rescue_iseq = NEW_CHILD_ISEQ(
&rescue_scope_node,
rb_str_concat(rb_str_new2("rescue in "), ISEQ_BODY(iseq)->location.label),
ISEQ_TYPE_RESCUE,
pm_node_line_number(parser, cast->rescue_expression)
);
pm_scope_node_destroy(&rescue_scope_node);
LABEL *lstart = NEW_LABEL(location.line);
LABEL *lend = NEW_LABEL(location.line);
LABEL *lcont = NEW_LABEL(location.line);
lstart->rescued = LABEL_RESCUE_BEG;
lend->rescued = LABEL_RESCUE_END;
PUSH_LABEL(ret, lstart);
PM_COMPILE_NOT_POPPED(cast->expression);
PUSH_LABEL(ret, lend);
PUSH_INSN(ret, location, nop);
PUSH_LABEL(ret, lcont);
if (popped) PUSH_INSN(ret, location, pop);
PUSH_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue_iseq, lcont);
PUSH_CATCH_ENTRY(CATCH_TYPE_RETRY, lend, lcont, NULL, lstart);
return;
}
case PM_RETURN_NODE:
// return
// ^^^^^^
//
// return 1
// ^^^^^^^^
pm_compile_return_node(iseq, (const pm_return_node_t *) node, &location, ret, popped, scope_node);
return;
case PM_RETRY_NODE: {
// retry
// ^^^^^
if (ISEQ_BODY(iseq)->type == ISEQ_TYPE_RESCUE) {
PUSH_INSN(ret, location, putnil);
PUSH_INSN1(ret, location, throw, INT2FIX(TAG_RETRY));
if (popped) PUSH_INSN(ret, location, pop);
}
else {
COMPILE_ERROR(iseq, location.line, "Invalid retry");
return;
}
return;
}
case PM_SCOPE_NODE:
pm_compile_scope_node(iseq, (pm_scope_node_t *) node, &location, ret, popped);
return;
case PM_SELF_NODE: {
// self
// ^^^^
if (!popped) {
PUSH_INSN(ret, location, putself);
}
return;
}
case PM_SHAREABLE_CONSTANT_NODE: {
// A value that is being written to a constant that is being marked as
// shared depending on the current lexical context.
const pm_shareable_constant_node_t *cast = (const pm_shareable_constant_node_t *) node;
pm_node_flags_t shareability = (cast->base.flags & (PM_SHAREABLE_CONSTANT_NODE_FLAGS_LITERAL | PM_SHAREABLE_CONSTANT_NODE_FLAGS_EXPERIMENTAL_EVERYTHING | PM_SHAREABLE_CONSTANT_NODE_FLAGS_EXPERIMENTAL_COPY));
switch (PM_NODE_TYPE(cast->write)) {
case PM_CONSTANT_WRITE_NODE:
pm_compile_constant_write_node(iseq, (const pm_constant_write_node_t *) cast->write, shareability, &location, ret, popped, scope_node);
break;
case PM_CONSTANT_AND_WRITE_NODE:
pm_compile_constant_and_write_node(iseq, (const pm_constant_and_write_node_t *) cast->write, shareability, &location, ret, popped, scope_node);
break;
case PM_CONSTANT_OR_WRITE_NODE:
pm_compile_constant_or_write_node(iseq, (const pm_constant_or_write_node_t *) cast->write, shareability, &location, ret, popped, scope_node);
break;
case PM_CONSTANT_OPERATOR_WRITE_NODE:
pm_compile_constant_operator_write_node(iseq, (const pm_constant_operator_write_node_t *) cast->write, shareability, &location, ret, popped, scope_node);
break;
case PM_CONSTANT_PATH_WRITE_NODE:
pm_compile_constant_path_write_node(iseq, (const pm_constant_path_write_node_t *) cast->write, shareability, &location, ret, popped, scope_node);
break;
case PM_CONSTANT_PATH_AND_WRITE_NODE:
pm_compile_constant_path_and_write_node(iseq, (const pm_constant_path_and_write_node_t *) cast->write, shareability, &location, ret, popped, scope_node);
break;
case PM_CONSTANT_PATH_OR_WRITE_NODE:
pm_compile_constant_path_or_write_node(iseq, (const pm_constant_path_or_write_node_t *) cast->write, shareability, &location, ret, popped, scope_node);
break;
case PM_CONSTANT_PATH_OPERATOR_WRITE_NODE:
pm_compile_constant_path_operator_write_node(iseq, (const pm_constant_path_operator_write_node_t *) cast->write, shareability, &location, ret, popped, scope_node);
break;
default:
rb_bug("Unexpected node type for shareable constant write: %s", pm_node_type_to_str(PM_NODE_TYPE(cast->write)));
break;
}
return;
}
case PM_SINGLETON_CLASS_NODE: {
// class << self; end
// ^^^^^^^^^^^^^^^^^^
const pm_singleton_class_node_t *cast = (const pm_singleton_class_node_t *) node;
pm_scope_node_t next_scope_node;
pm_scope_node_init((const pm_node_t *) cast, &next_scope_node, scope_node);
const rb_iseq_t *child_iseq = NEW_ISEQ(&next_scope_node, rb_fstring_lit("singleton class"), ISEQ_TYPE_CLASS, location.line);
pm_scope_node_destroy(&next_scope_node);
PM_COMPILE_NOT_POPPED(cast->expression);
PUSH_INSN(ret, location, putnil);
ID singletonclass;
CONST_ID(singletonclass, "singletonclass");
PUSH_INSN3(ret, location, defineclass, ID2SYM(singletonclass), child_iseq, INT2FIX(VM_DEFINECLASS_TYPE_SINGLETON_CLASS));
if (popped) PUSH_INSN(ret, location, pop);
RB_OBJ_WRITTEN(iseq, Qundef, (VALUE) child_iseq);
return;
}
case PM_SOURCE_ENCODING_NODE: {
// __ENCODING__
// ^^^^^^^^^^^^
if (!popped) {
VALUE value = pm_static_literal_value(iseq, node, scope_node);
PUSH_INSN1(ret, location, putobject, value);
}
return;
}
case PM_SOURCE_FILE_NODE: {
// __FILE__
// ^^^^^^^^
if (!popped) {
const pm_source_file_node_t *cast = (const pm_source_file_node_t *) node;
VALUE string = pm_source_file_value(cast, scope_node);
if (PM_NODE_FLAG_P(cast, PM_STRING_FLAGS_FROZEN)) {
PUSH_INSN1(ret, location, putobject, string);
}
else if (PM_NODE_FLAG_P(cast, PM_STRING_FLAGS_MUTABLE)) {
PUSH_INSN1(ret, location, putstring, string);
}
else {
PUSH_INSN1(ret, location, putchilledstring, string);
}
}
return;
}
case PM_SOURCE_LINE_NODE: {
// __LINE__
// ^^^^^^^^
if (!popped) {
VALUE value = pm_static_literal_value(iseq, node, scope_node);
PUSH_INSN1(ret, location, putobject, value);
}
return;
}
case PM_SPLAT_NODE: {
// foo(*bar)
// ^^^^
const pm_splat_node_t *cast = (const pm_splat_node_t *) node;
if (cast->expression) {
PM_COMPILE(cast->expression);
}
if (!popped) {
PUSH_INSN1(ret, location, splatarray, Qtrue);
}
return;
}
case PM_STATEMENTS_NODE: {
// A list of statements.
const pm_statements_node_t *cast = (const pm_statements_node_t *) node;
const pm_node_list_t *body = &cast->body;
if (body->size > 0) {
for (size_t index = 0; index < body->size - 1; index++) {
PM_COMPILE_POPPED(body->nodes[index]);
}
PM_COMPILE(body->nodes[body->size - 1]);
}
else {
PUSH_INSN(ret, location, putnil);
}
return;
}
case PM_STRING_NODE: {
// "foo"
// ^^^^^
if (!popped) {
const pm_string_node_t *cast = (const pm_string_node_t *) node;
VALUE value = parse_static_literal_string(iseq, scope_node, node, &cast->unescaped);
if (PM_NODE_FLAG_P(node, PM_STRING_FLAGS_FROZEN)) {
PUSH_INSN1(ret, location, putobject, value);
}
else if (PM_NODE_FLAG_P(node, PM_STRING_FLAGS_MUTABLE)) {
PUSH_INSN1(ret, location, putstring, value);
}
else {
PUSH_INSN1(ret, location, putchilledstring, value);
}
}
return;
}
case PM_SUPER_NODE:
// super()
// super(foo)
// super(...)
pm_compile_super_node(iseq, (const pm_super_node_t *) node, &location, ret, popped, scope_node);
return;
case PM_SYMBOL_NODE: {
// :foo
// ^^^^
if (!popped) {
VALUE value = pm_static_literal_value(iseq, node, scope_node);
PUSH_INSN1(ret, location, putobject, value);
}
return;
}
case PM_TRUE_NODE: {
// true
// ^^^^
if (!popped) {
PUSH_INSN1(ret, location, putobject, Qtrue);
}
return;
}
case PM_UNDEF_NODE: {
// undef foo
// ^^^^^^^^^
const pm_undef_node_t *cast = (const pm_undef_node_t *) node;
const pm_node_list_t *names = &cast->names;
for (size_t index = 0; index < names->size; index++) {
PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CBASE));
PM_COMPILE_NOT_POPPED(names->nodes[index]);
PUSH_SEND(ret, location, id_core_undef_method, INT2NUM(2));
if (index < names->size - 1) {
PUSH_INSN(ret, location, pop);
}
}
if (popped) PUSH_INSN(ret, location, pop);
return;
}
case PM_UNLESS_NODE: {
// unless foo; bar end
// ^^^^^^^^^^^^^^^^^^^
//
// bar unless foo
// ^^^^^^^^^^^^^^
const pm_unless_node_t *cast = (const pm_unless_node_t *) node;
const pm_statements_node_t *statements = NULL;
if (cast->else_clause != NULL) {
statements = ((const pm_else_node_t *) cast->else_clause)->statements;
}
pm_compile_conditional(iseq, &location, PM_UNLESS_NODE, (const pm_node_t *) cast, statements, (const pm_node_t *) cast->statements, cast->predicate, ret, popped, scope_node);
return;
}
case PM_UNTIL_NODE: {
// until foo; bar end
// ^^^^^^^^^^^^^^^^^
//
// bar until foo
// ^^^^^^^^^^^^^
const pm_until_node_t *cast = (const pm_until_node_t *) node;
pm_compile_loop(iseq, &location, cast->base.flags, PM_UNTIL_NODE, (const pm_node_t *) cast, cast->statements, cast->predicate, ret, popped, scope_node);
return;
}
case PM_WHILE_NODE: {
// while foo; bar end
// ^^^^^^^^^^^^^^^^^^
//
// bar while foo
// ^^^^^^^^^^^^^
const pm_while_node_t *cast = (const pm_while_node_t *) node;
pm_compile_loop(iseq, &location, cast->base.flags, PM_WHILE_NODE, (const pm_node_t *) cast, cast->statements, cast->predicate, ret, popped, scope_node);
return;
}
case PM_X_STRING_NODE: {
// `foo`
// ^^^^^
const pm_x_string_node_t *cast = (const pm_x_string_node_t *) node;
VALUE value = parse_static_literal_string(iseq, scope_node, node, &cast->unescaped);
PUSH_INSN(ret, location, putself);
PUSH_INSN1(ret, location, putobject, value);
PUSH_SEND_WITH_FLAG(ret, location, idBackquote, INT2NUM(1), INT2FIX(VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE));
if (popped) PUSH_INSN(ret, location, pop);
return;
}
case PM_YIELD_NODE:
// yield
// ^^^^^
//
// yield 1
// ^^^^^^^
pm_compile_yield_node(iseq, (const pm_yield_node_t *) node, &location, ret, popped, scope_node);
return;
default:
rb_raise(rb_eNotImpError, "node type %s not implemented", pm_node_type_to_str(PM_NODE_TYPE(node)));
return;
}
}
#undef PM_CONTAINER_P
/** True if the given iseq can have pre execution blocks. */
static inline bool
pm_iseq_pre_execution_p(rb_iseq_t *iseq)
{
switch (ISEQ_BODY(iseq)->type) {
case ISEQ_TYPE_TOP:
case ISEQ_TYPE_EVAL:
case ISEQ_TYPE_MAIN:
return true;
default:
return false;
}
}
/**
* This is the main entry-point into the prism compiler. It accepts the iseq
* that it should be compiling instruction into and a pointer to the scope node
* that it should be compiling. It returns the established instruction sequence.
* Note that this function could raise Ruby errors if it encounters compilation
* errors or if there is a bug in the compiler.
*/
VALUE
pm_iseq_compile_node(rb_iseq_t *iseq, pm_scope_node_t *node)
{
DECL_ANCHOR(ret);
if (pm_iseq_pre_execution_p(iseq)) {
// Because these ISEQs can have BEGIN{}, we're going to create two
// anchors to compile them, a "pre" and a "body". We'll mark the "pre"
// on the scope node so that when BEGIN{} is found, its contents will be
// added to the "pre" anchor.
DECL_ANCHOR(pre);
node->pre_execution_anchor = pre;
// Now we'll compile the body as normal. We won't compile directly into
// the "ret" anchor yet because we want to add the "pre" anchor to the
// beginning of the "ret" anchor first.
DECL_ANCHOR(body);
pm_compile_node(iseq, (const pm_node_t *) node, body, false, node);
// Now we'll join both anchors together so that the content is in the
// correct order.
PUSH_SEQ(ret, pre);
PUSH_SEQ(ret, body);
}
else {
// In other circumstances, we can just compile the node directly into
// the "ret" anchor.
pm_compile_node(iseq, (const pm_node_t *) node, ret, false, node);
}
CHECK(iseq_setup_insn(iseq, ret));
return iseq_setup(iseq, ret);
}
/**
* Free the internal memory associated with a pm_parse_result_t struct.
* Importantly this does not free the struct itself.
*/
void
pm_parse_result_free(pm_parse_result_t *result)
{
if (result->node.ast_node != NULL) {
pm_node_destroy(&result->parser, result->node.ast_node);
}
if (result->parsed) {
xfree(result->node.constants);
pm_scope_node_destroy(&result->node);
}
pm_parser_free(&result->parser);
pm_string_free(&result->input);
pm_options_free(&result->options);
}
/** An error that is going to be formatted into the output. */
typedef struct {
/** A pointer to the diagnostic that was generated during parsing. */
pm_diagnostic_t *error;
/** The start line of the diagnostic message. */
int32_t line;
/** The column start of the diagnostic message. */
uint32_t column_start;
/** The column end of the diagnostic message. */
uint32_t column_end;
} pm_parse_error_t;
/** The format that will be used to format the errors into the output. */
typedef struct {
/** The prefix that will be used for line numbers. */
const char *number_prefix;
/** The prefix that will be used for blank lines. */
const char *blank_prefix;
/** The divider that will be used between sections of source code. */
const char *divider;
/** The length of the blank prefix. */
size_t blank_prefix_length;
/** The length of the divider. */
size_t divider_length;
} pm_parse_error_format_t;
#define PM_COLOR_BOLD "\033[1m"
#define PM_COLOR_GRAY "\033[2m"
#define PM_COLOR_RED "\033[1;31m"
#define PM_COLOR_RESET "\033[m"
#define PM_ERROR_TRUNCATE 30
static inline pm_parse_error_t *
pm_parse_errors_format_sort(const pm_parser_t *parser, const pm_list_t *error_list, const pm_newline_list_t *newline_list) {
pm_parse_error_t *errors = xcalloc(error_list->size, sizeof(pm_parse_error_t));
if (errors == NULL) return NULL;
int32_t start_line = parser->start_line;
for (pm_diagnostic_t *error = (pm_diagnostic_t *) error_list->head; error != NULL; error = (pm_diagnostic_t *) error->node.next) {
pm_line_column_t start = pm_newline_list_line_column(newline_list, error->location.start, start_line);
pm_line_column_t end = pm_newline_list_line_column(newline_list, error->location.end, start_line);
// We're going to insert this error into the array in sorted order. We
// do this by finding the first error that has a line number greater
// than the current error and then inserting the current error before
// that one.
size_t index = 0;
while (
(index < error_list->size) &&
(errors[index].error != NULL) &&
(
(errors[index].line < start.line) ||
((errors[index].line == start.line) && (errors[index].column_start < start.column))
)
) index++;
// Now we're going to shift all of the errors after this one down one
// index to make room for the new error.
if (index + 1 < error_list->size) {
memmove(&errors[index + 1], &errors[index], sizeof(pm_parse_error_t) * (error_list->size - index - 1));
}
// Finally, we'll insert the error into the array.
uint32_t column_end;
if (start.line == end.line) {
column_end = end.column;
} else {
column_end = (uint32_t) (newline_list->offsets[start.line - start_line + 1] - newline_list->offsets[start.line - start_line] - 1);
}
// Ensure we have at least one column of error.
if (start.column == column_end) column_end++;
errors[index] = (pm_parse_error_t) {
.error = error,
.line = start.line,
.column_start = start.column,
.column_end = column_end
};
}
return errors;
}
/* Append a literal string to the buffer. */
#define pm_buffer_append_literal(buffer, str) pm_buffer_append_string(buffer, str, rb_strlen_lit(str))
static inline void
pm_parse_errors_format_line(const pm_parser_t *parser, const pm_newline_list_t *newline_list, const char *number_prefix, int32_t line, uint32_t column_start, uint32_t column_end, pm_buffer_t *buffer) {
int32_t line_delta = line - parser->start_line;
assert(line_delta >= 0);
size_t index = (size_t) line_delta;
assert(index < newline_list->size);
const uint8_t *start = &parser->start[newline_list->offsets[index]];
const uint8_t *end;
if (index >= newline_list->size - 1) {
end = parser->end;
} else {
end = &parser->start[newline_list->offsets[index + 1]];
}
pm_buffer_append_format(buffer, number_prefix, line);
// Here we determine if we should truncate the end of the line.
bool truncate_end = false;
if ((column_end != 0) && ((end - (start + column_end)) >= PM_ERROR_TRUNCATE)) {
end = start + column_end + PM_ERROR_TRUNCATE;
truncate_end = true;
}
// Here we determine if we should truncate the start of the line.
if (column_start >= PM_ERROR_TRUNCATE) {
pm_buffer_append_string(buffer, "... ", 4);
start += column_start;
}
pm_buffer_append_string(buffer, (const char *) start, (size_t) (end - start));
if (truncate_end) {
pm_buffer_append_string(buffer, " ...\n", 5);
} else if (end == parser->end && end[-1] != '\n') {
pm_buffer_append_string(buffer, "\n", 1);
}
}
/**
* Format the errors on the parser into the given buffer.
*/
static void
pm_parse_errors_format(const pm_parser_t *parser, const pm_list_t *error_list, pm_buffer_t *buffer, int highlight, bool inline_messages) {
assert(error_list->size != 0);
// First, we're going to sort all of the errors by line number using an
// insertion sort into a newly allocated array.
const int32_t start_line = parser->start_line;
const pm_newline_list_t *newline_list = &parser->newline_list;
pm_parse_error_t *errors = pm_parse_errors_format_sort(parser, error_list, newline_list);
if (errors == NULL) return;
// Now we're going to determine how we're going to format line numbers and
// blank lines based on the maximum number of digits in the line numbers
// that are going to be displaid.
pm_parse_error_format_t error_format;
int32_t first_line_number = errors[0].line;
int32_t last_line_number = errors[error_list->size - 1].line;
// If we have a maximum line number that is negative, then we're going to
// use the absolute value for comparison but multiple by 10 to additionally
// have a column for the negative sign.
if (first_line_number < 0) first_line_number = (-first_line_number) * 10;
if (last_line_number < 0) last_line_number = (-last_line_number) * 10;
int32_t max_line_number = first_line_number > last_line_number ? first_line_number : last_line_number;
if (max_line_number < 10) {
if (highlight > 0) {
error_format = (pm_parse_error_format_t) {
.number_prefix = PM_COLOR_GRAY "%1" PRIi32 " | " PM_COLOR_RESET,
.blank_prefix = PM_COLOR_GRAY " | " PM_COLOR_RESET,
.divider = PM_COLOR_GRAY " ~~~~~" PM_COLOR_RESET "\n"
};
} else {
error_format = (pm_parse_error_format_t) {
.number_prefix = "%1" PRIi32 " | ",
.blank_prefix = " | ",
.divider = " ~~~~~\n"
};
}
} else if (max_line_number < 100) {
if (highlight > 0) {
error_format = (pm_parse_error_format_t) {
.number_prefix = PM_COLOR_GRAY "%2" PRIi32 " | " PM_COLOR_RESET,
.blank_prefix = PM_COLOR_GRAY " | " PM_COLOR_RESET,
.divider = PM_COLOR_GRAY " ~~~~~~" PM_COLOR_RESET "\n"
};
} else {
error_format = (pm_parse_error_format_t) {
.number_prefix = "%2" PRIi32 " | ",
.blank_prefix = " | ",
.divider = " ~~~~~~\n"
};
}
} else if (max_line_number < 1000) {
if (highlight > 0) {
error_format = (pm_parse_error_format_t) {
.number_prefix = PM_COLOR_GRAY "%3" PRIi32 " | " PM_COLOR_RESET,
.blank_prefix = PM_COLOR_GRAY " | " PM_COLOR_RESET,
.divider = PM_COLOR_GRAY " ~~~~~~~" PM_COLOR_RESET "\n"
};
} else {
error_format = (pm_parse_error_format_t) {
.number_prefix = "%3" PRIi32 " | ",
.blank_prefix = " | ",
.divider = " ~~~~~~~\n"
};
}
} else if (max_line_number < 10000) {
if (highlight > 0) {
error_format = (pm_parse_error_format_t) {
.number_prefix = PM_COLOR_GRAY "%4" PRIi32 " | " PM_COLOR_RESET,
.blank_prefix = PM_COLOR_GRAY " | " PM_COLOR_RESET,
.divider = PM_COLOR_GRAY " ~~~~~~~~" PM_COLOR_RESET "\n"
};
} else {
error_format = (pm_parse_error_format_t) {
.number_prefix = "%4" PRIi32 " | ",
.blank_prefix = " | ",
.divider = " ~~~~~~~~\n"
};
}
} else {
if (highlight > 0) {
error_format = (pm_parse_error_format_t) {
.number_prefix = PM_COLOR_GRAY "%5" PRIi32 " | " PM_COLOR_RESET,
.blank_prefix = PM_COLOR_GRAY " | " PM_COLOR_RESET,
.divider = PM_COLOR_GRAY " ~~~~~~~~" PM_COLOR_RESET "\n"
};
} else {
error_format = (pm_parse_error_format_t) {
.number_prefix = "%5" PRIi32 " | ",
.blank_prefix = " | ",
.divider = " ~~~~~~~~\n"
};
}
}
error_format.blank_prefix_length = strlen(error_format.blank_prefix);
error_format.divider_length = strlen(error_format.divider);
// Now we're going to iterate through every error in our error list and
// display it. While we're iterating, we will display some padding lines of
// the source before the error to give some context. We'll be careful not to
// display the same line twice in case the errors are close enough in the
// source.
int32_t last_line = parser->start_line - 1;
uint32_t last_column_start = 0;
const pm_encoding_t *encoding = parser->encoding;
for (size_t index = 0; index < error_list->size; index++) {
pm_parse_error_t *error = &errors[index];
// Here we determine how many lines of padding of the source to display,
// based on the difference from the last line that was displaid.
if (error->line - last_line > 1) {
if (error->line - last_line > 2) {
if ((index != 0) && (error->line - last_line > 3)) {
pm_buffer_append_string(buffer, error_format.divider, error_format.divider_length);
}
pm_buffer_append_string(buffer, " ", 2);
pm_parse_errors_format_line(parser, newline_list, error_format.number_prefix, error->line - 2, 0, 0, buffer);
}
pm_buffer_append_string(buffer, " ", 2);
pm_parse_errors_format_line(parser, newline_list, error_format.number_prefix, error->line - 1, 0, 0, buffer);
}
// If this is the first error or we're on a new line, then we'll display
// the line that has the error in it.
if ((index == 0) || (error->line != last_line)) {
if (highlight > 1) {
pm_buffer_append_literal(buffer, PM_COLOR_RED "> " PM_COLOR_RESET);
} else if (highlight > 0) {
pm_buffer_append_literal(buffer, PM_COLOR_BOLD "> " PM_COLOR_RESET);
} else {
pm_buffer_append_literal(buffer, "> ");
}
last_column_start = error->column_start;
// Find the maximum column end of all the errors on this line.
uint32_t column_end = error->column_end;
for (size_t next_index = index + 1; next_index < error_list->size; next_index++) {
if (errors[next_index].line != error->line) break;
if (errors[next_index].column_end > column_end) column_end = errors[next_index].column_end;
}
pm_parse_errors_format_line(parser, newline_list, error_format.number_prefix, error->line, error->column_start, column_end, buffer);
}
const uint8_t *start = &parser->start[newline_list->offsets[error->line - start_line]];
if (start == parser->end) pm_buffer_append_byte(buffer, '\n');
// Now we'll display the actual error message. We'll do this by first
// putting the prefix to the line, then a bunch of blank spaces
// depending on the column, then as many carets as we need to display
// the width of the error, then the error message itself.
//
// Note that this doesn't take into account the width of the actual
// character when displaid in the terminal. For some east-asian
// languages or emoji, this means it can be thrown off pretty badly. We
// will need to solve this eventually.
pm_buffer_append_string(buffer, " ", 2);
pm_buffer_append_string(buffer, error_format.blank_prefix, error_format.blank_prefix_length);
size_t column = 0;
if (last_column_start >= PM_ERROR_TRUNCATE) {
pm_buffer_append_string(buffer, " ", 4);
column = last_column_start;
}
while (column < error->column_start) {
pm_buffer_append_byte(buffer, ' ');
size_t char_width = encoding->char_width(start + column, parser->end - (start + column));
column += (char_width == 0 ? 1 : char_width);
}
if (highlight > 1) pm_buffer_append_literal(buffer, PM_COLOR_RED);
else if (highlight > 0) pm_buffer_append_literal(buffer, PM_COLOR_BOLD);
pm_buffer_append_byte(buffer, '^');
size_t char_width = encoding->char_width(start + column, parser->end - (start + column));
column += (char_width == 0 ? 1 : char_width);
while (column < error->column_end) {
pm_buffer_append_byte(buffer, '~');
size_t char_width = encoding->char_width(start + column, parser->end - (start + column));
column += (char_width == 0 ? 1 : char_width);
}
if (highlight > 0) pm_buffer_append_literal(buffer, PM_COLOR_RESET);
if (inline_messages) {
pm_buffer_append_byte(buffer, ' ');
assert(error->error != NULL);
const char *message = error->error->message;
pm_buffer_append_string(buffer, message, strlen(message));
}
pm_buffer_append_byte(buffer, '\n');
// Here we determine how many lines of padding to display after the
// error, depending on where the next error is in source.
last_line = error->line;
int32_t next_line;
if (index == error_list->size - 1) {
next_line = (((int32_t) newline_list->size) + parser->start_line);
// If the file ends with a newline, subtract one from our "next_line"
// so that we don't output an extra line at the end of the file
if ((parser->start + newline_list->offsets[newline_list->size - 1]) == parser->end) {
next_line--;
}
}
else {
next_line = errors[index + 1].line;
}
if (next_line - last_line > 1) {
pm_buffer_append_string(buffer, " ", 2);
pm_parse_errors_format_line(parser, newline_list, error_format.number_prefix, ++last_line, 0, 0, buffer);
}
if (next_line - last_line > 1) {
pm_buffer_append_string(buffer, " ", 2);
pm_parse_errors_format_line(parser, newline_list, error_format.number_prefix, ++last_line, 0, 0, buffer);
}
}
// Finally, we'll free the array of errors that we allocated.
xfree(errors);
}
#undef PM_ERROR_TRUNCATE
#undef PM_COLOR_GRAY
#undef PM_COLOR_RED
#undef PM_COLOR_RESET
/**
* Check if the given source slice is valid UTF-8. The location represents the
* location of the error, but the slice of the source will include the content
* of all of the lines that the error touches, so we need to check those parts
* as well.
*/
static bool
pm_parse_process_error_utf8_p(const pm_parser_t *parser, const pm_location_t *location)
{
const size_t start_line = pm_newline_list_line_column(&parser->newline_list, location->start, 1).line;
const size_t end_line = pm_newline_list_line_column(&parser->newline_list, location->end, 1).line;
const uint8_t *start = parser->start + parser->newline_list.offsets[start_line - 1];
const uint8_t *end = ((end_line == parser->newline_list.size) ? parser->end : (parser->start + parser->newline_list.offsets[end_line]));
size_t width;
while (start < end) {
if ((width = pm_encoding_utf_8_char_width(start, end - start)) == 0) return false;
start += width;
}
return true;
}
/**
* Generate an error object from the given parser that contains as much
* information as possible about the errors that were encountered.
*/
static VALUE
pm_parse_process_error(const pm_parse_result_t *result)
{
const pm_parser_t *parser = &result->parser;
const pm_diagnostic_t *head = (const pm_diagnostic_t *) parser->error_list.head;
bool valid_utf8 = true;
pm_buffer_t buffer = { 0 };
const pm_string_t *filepath = &parser->filepath;
int highlight = rb_stderr_tty_p();
if (highlight) {
const char *no_color = getenv("NO_COLOR");
highlight = (no_color == NULL || no_color[0] == '\0') ? 2 : 1;
}
for (const pm_diagnostic_t *error = head; error != NULL; error = (const pm_diagnostic_t *) error->node.next) {
switch (error->level) {
case PM_ERROR_LEVEL_SYNTAX:
// It is implicitly assumed that the error messages will be
// encodeable as UTF-8. Because of this, we can't include source
// examples that contain invalid byte sequences. So if any source
// examples include invalid UTF-8 byte sequences, we will skip
// showing source examples entirely.
if (valid_utf8 && !pm_parse_process_error_utf8_p(parser, &error->location)) {
valid_utf8 = false;
}
break;
case PM_ERROR_LEVEL_ARGUMENT: {
// Any errors with the level PM_ERROR_LEVEL_ARGUMENT take over as
// the only argument that gets raised. This is to allow priority
// messages that should be handled before anything else.
int32_t line_number = (int32_t) pm_location_line_number(parser, &error->location);
pm_buffer_append_format(
&buffer,
"%.*s:%" PRIi32 ": %s",
(int) pm_string_length(filepath),
pm_string_source(filepath),
line_number,
error->message
);
if (pm_parse_process_error_utf8_p(parser, &error->location)) {
pm_buffer_append_byte(&buffer, '\n');
pm_list_node_t *list_node = (pm_list_node_t *) error;
pm_list_t error_list = { .size = 1, .head = list_node, .tail = list_node };
pm_parse_errors_format(parser, &error_list, &buffer, highlight, false);
}
VALUE value = rb_exc_new(rb_eArgError, pm_buffer_value(&buffer), pm_buffer_length(&buffer));
pm_buffer_free(&buffer);
return value;
}
case PM_ERROR_LEVEL_LOAD: {
// Load errors are much simpler, because they don't include any of
// the source in them. We create the error directly from the
// message.
VALUE message = rb_enc_str_new_cstr(error->message, rb_locale_encoding());
VALUE value = rb_exc_new3(rb_eLoadError, message);
rb_ivar_set(value, rb_intern_const("@path"), Qnil);
return value;
}
}
}
pm_buffer_append_format(
&buffer,
"%.*s:%" PRIi32 ": syntax error%s found\n",
(int) pm_string_length(filepath),
pm_string_source(filepath),
(int32_t) pm_location_line_number(parser, &head->location),
(parser->error_list.size > 1) ? "s" : ""
);
if (valid_utf8) {
pm_parse_errors_format(parser, &parser->error_list, &buffer, highlight, true);
}
else {
for (const pm_diagnostic_t *error = head; error != NULL; error = (const pm_diagnostic_t *) error->node.next) {
if (error != head) pm_buffer_append_byte(&buffer, '\n');
pm_buffer_append_format(&buffer, "%.*s:%" PRIi32 ": %s", (int) pm_string_length(filepath), pm_string_source(filepath), (int32_t) pm_location_line_number(parser, &error->location), error->message);
}
}
VALUE message = rb_enc_str_new(pm_buffer_value(&buffer), pm_buffer_length(&buffer), result->node.encoding);
VALUE error = rb_exc_new_str(rb_eSyntaxError, message);
rb_encoding *filepath_encoding = result->node.filepath_encoding != NULL ? result->node.filepath_encoding : rb_utf8_encoding();
VALUE path = rb_enc_str_new((const char *) pm_string_source(filepath), pm_string_length(filepath), filepath_encoding);
rb_ivar_set(error, rb_intern_const("@path"), path);
pm_buffer_free(&buffer);
return error;
}
/**
* Parse the parse result and raise a Ruby error if there are any syntax errors.
* It returns an error if one should be raised. It is assumed that the parse
* result object is zeroed out.
*/
static VALUE
pm_parse_process(pm_parse_result_t *result, pm_node_t *node, VALUE *script_lines)
{
pm_parser_t *parser = &result->parser;
// First, set up the scope node so that the AST node is attached and can be
// freed regardless of whether or we return an error.
pm_scope_node_t *scope_node = &result->node;
rb_encoding *filepath_encoding = scope_node->filepath_encoding;
int coverage_enabled = scope_node->coverage_enabled;
pm_scope_node_init(node, scope_node, NULL);
scope_node->filepath_encoding = filepath_encoding;
scope_node->encoding = rb_enc_find(parser->encoding->name);
if (!scope_node->encoding) rb_bug("Encoding not found %s!", parser->encoding->name);
scope_node->coverage_enabled = coverage_enabled;
// If RubyVM.keep_script_lines is set to true, then we need to create that
// array of script lines here.
if (script_lines != NULL) {
*script_lines = rb_ary_new_capa(parser->newline_list.size);
for (size_t index = 0; index < parser->newline_list.size; index++) {
size_t offset = parser->newline_list.offsets[index];
size_t length = index == parser->newline_list.size - 1 ? ((size_t) (parser->end - (parser->start + offset))) : (parser->newline_list.offsets[index + 1] - offset);
rb_ary_push(*script_lines, rb_enc_str_new((const char *) parser->start + offset, length, scope_node->encoding));
}
scope_node->script_lines = script_lines;
}
// Emit all of the various warnings from the parse.
const pm_diagnostic_t *warning;
const char *warning_filepath = (const char *) pm_string_source(&parser->filepath);
for (warning = (const pm_diagnostic_t *) parser->warning_list.head; warning != NULL; warning = (const pm_diagnostic_t *) warning->node.next) {
int line = pm_location_line_number(parser, &warning->location);
if (warning->level == PM_WARNING_LEVEL_VERBOSE) {
rb_enc_compile_warning(scope_node->encoding, warning_filepath, line, "%s", warning->message);
}
else {
rb_enc_compile_warn(scope_node->encoding, warning_filepath, line, "%s", warning->message);
}
}
// If there are errors, raise an appropriate error and free the result.
if (parser->error_list.size > 0) {
VALUE error = pm_parse_process_error(result);
// TODO: We need to set the backtrace.
// rb_funcallv(error, rb_intern("set_backtrace"), 1, &path);
return error;
}
// Now set up the constant pool and intern all of the various constants into
// their corresponding IDs.
scope_node->parser = parser;
scope_node->constants = xcalloc(parser->constant_pool.size, sizeof(ID));
for (uint32_t index = 0; index < parser->constant_pool.size; index++) {
pm_constant_t *constant = &parser->constant_pool.constants[index];
scope_node->constants[index] = rb_intern3((const char *) constant->start, constant->length, scope_node->encoding);
}
scope_node->index_lookup_table = st_init_numtable();
pm_constant_id_list_t *locals = &scope_node->locals;
for (size_t index = 0; index < locals->size; index++) {
st_insert(scope_node->index_lookup_table, locals->ids[index], index);
}
// If we got here, this is a success and we can return Qnil to indicate that
// no error should be raised.
result->parsed = true;
return Qnil;
}
/**
* Set the frozen_string_literal option based on the default value used by the
* CRuby compiler.
*/
static void
pm_options_frozen_string_literal_init(pm_options_t *options)
{
int frozen_string_literal = rb_iseq_opt_frozen_string_literal();
switch (frozen_string_literal) {
case ISEQ_FROZEN_STRING_LITERAL_UNSET:
break;
case ISEQ_FROZEN_STRING_LITERAL_DISABLED:
pm_options_frozen_string_literal_set(options, false);
break;
case ISEQ_FROZEN_STRING_LITERAL_ENABLED:
pm_options_frozen_string_literal_set(options, true);
break;
default:
rb_bug("pm_options_frozen_string_literal_init: invalid frozen_string_literal=%d", frozen_string_literal);
break;
}
}
/**
* Returns an array of ruby String objects that represent the lines of the
* source file that the given parser parsed.
*/
static inline VALUE
pm_parse_file_script_lines(const pm_scope_node_t *scope_node, const pm_parser_t *parser)
{
const pm_newline_list_t *newline_list = &parser->newline_list;
const char *start = (const char *) parser->start;
const char *end = (const char *) parser->end;
// If we end exactly on a newline, then there's no need to push on a final
// segment. If we don't, then we need to push on the last offset up to the
// end of the string.
size_t last_offset = newline_list->offsets[newline_list->size - 1];
bool last_push = start + last_offset != end;
// Create the ruby strings that represent the lines of the source.
VALUE lines = rb_ary_new_capa(newline_list->size - (last_push ? 0 : 1));
for (size_t index = 0; index < newline_list->size - 1; index++) {
size_t offset = newline_list->offsets[index];
size_t length = newline_list->offsets[index + 1] - offset;
rb_ary_push(lines, rb_enc_str_new(start + offset, length, scope_node->encoding));
}
// Push on the last line if we need to.
if (last_push) {
rb_ary_push(lines, rb_enc_str_new(start + last_offset, end - (start + last_offset), scope_node->encoding));
}
return lines;
}
// This is essentially pm_string_mapped_init(), preferring to memory map the
// file, with additional handling for files that require blocking to properly
// read (e.g. pipes).
static pm_string_init_result_t
pm_read_file(pm_string_t *string, const char *filepath)
{
#ifdef _WIN32
// Open the file for reading.
int length = MultiByteToWideChar(CP_UTF8, 0, filepath, -1, NULL, 0);
if (length == 0) return PM_STRING_INIT_ERROR_GENERIC;
WCHAR *wfilepath = xmalloc(sizeof(WCHAR) * ((size_t) length));
if ((wfilepath == NULL) || (MultiByteToWideChar(CP_UTF8, 0, filepath, -1, wfilepath, length) == 0)) {
xfree(wfilepath);
return PM_STRING_INIT_ERROR_GENERIC;
}
HANDLE file = CreateFileW(wfilepath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
if (file == INVALID_HANDLE_VALUE) {
pm_string_init_result_t result = PM_STRING_INIT_ERROR_GENERIC;
if (GetLastError() == ERROR_ACCESS_DENIED) {
DWORD attributes = GetFileAttributesW(wfilepath);
if ((attributes != INVALID_FILE_ATTRIBUTES) && (attributes & FILE_ATTRIBUTE_DIRECTORY)) {
result = PM_STRING_INIT_ERROR_DIRECTORY;
}
}
xfree(wfilepath);
return result;
}
// Get the file size.
DWORD file_size = GetFileSize(file, NULL);
if (file_size == INVALID_FILE_SIZE) {
CloseHandle(file);
xfree(wfilepath);
return PM_STRING_INIT_ERROR_GENERIC;
}
// If the file is empty, then we don't need to do anything else, we'll set
// the source to a constant empty string and return.
if (file_size == 0) {
CloseHandle(file);
xfree(wfilepath);
const uint8_t source[] = "";
*string = (pm_string_t) { .type = PM_STRING_CONSTANT, .source = source, .length = 0 };
return PM_STRING_INIT_SUCCESS;
}
// Create a mapping of the file.
HANDLE mapping = CreateFileMapping(file, NULL, PAGE_READONLY, 0, 0, NULL);
if (mapping == NULL) {
CloseHandle(file);
xfree(wfilepath);
return PM_STRING_INIT_ERROR_GENERIC;
}
// Map the file into memory.
uint8_t *source = (uint8_t *) MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0);
CloseHandle(mapping);
CloseHandle(file);
xfree(wfilepath);
if (source == NULL) {
return PM_STRING_INIT_ERROR_GENERIC;
}
*string = (pm_string_t) { .type = PM_STRING_MAPPED, .source = source, .length = (size_t) file_size };
return PM_STRING_INIT_SUCCESS;
#elif defined(_POSIX_MAPPED_FILES)
// Open the file for reading
const int open_mode = O_RDONLY | O_NONBLOCK;
int fd = open(filepath, open_mode);
if (fd == -1) {
return PM_STRING_INIT_ERROR_GENERIC;
}
// Stat the file to get the file size
struct stat sb;
if (fstat(fd, &sb) == -1) {
close(fd);
return PM_STRING_INIT_ERROR_GENERIC;
}
// Ensure it is a file and not a directory
if (S_ISDIR(sb.st_mode)) {
close(fd);
return PM_STRING_INIT_ERROR_DIRECTORY;
}
// We need to wait for data first before reading from pipes and character
// devices. To not block the entire VM, we need to release the GVL while
// reading. Use IO#read to do this and let the GC handle closing the FD.
if (S_ISFIFO(sb.st_mode) || S_ISCHR(sb.st_mode)) {
VALUE io = rb_io_fdopen((int) fd, open_mode, filepath);
rb_io_wait(io, RB_INT2NUM(RUBY_IO_READABLE), Qnil);
VALUE contents = rb_funcall(io, rb_intern("read"), 0);
if (!RB_TYPE_P(contents, T_STRING)) {
return PM_STRING_INIT_ERROR_GENERIC;
}
long len = RSTRING_LEN(contents);
if (len < 0) {
return PM_STRING_INIT_ERROR_GENERIC;
}
size_t length = (size_t) len;
uint8_t *source = malloc(length);
memcpy(source, RSTRING_PTR(contents), length);
*string = (pm_string_t) { .type = PM_STRING_OWNED, .source = source, .length = length };
return PM_STRING_INIT_SUCCESS;
}
// mmap the file descriptor to virtually get the contents
size_t size = (size_t) sb.st_size;
uint8_t *source = NULL;
if (size == 0) {
close(fd);
const uint8_t source[] = "";
*string = (pm_string_t) { .type = PM_STRING_CONSTANT, .source = source, .length = 0 };
return PM_STRING_INIT_SUCCESS;
}
source = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
if (source == MAP_FAILED) {
close(fd);
return PM_STRING_INIT_ERROR_GENERIC;
}
close(fd);
*string = (pm_string_t) { .type = PM_STRING_MAPPED, .source = source, .length = size };
return PM_STRING_INIT_SUCCESS;
#else
return pm_string_file_init(string, filepath);
#endif
}
/**
* Attempt to load the file into memory. Return a Ruby error if the file cannot
* be read.
*/
VALUE
pm_load_file(pm_parse_result_t *result, VALUE filepath, bool load_error)
{
pm_string_init_result_t init_result = pm_read_file(&result->input, RSTRING_PTR(filepath));
if (init_result == PM_STRING_INIT_SUCCESS) {
pm_options_frozen_string_literal_init(&result->options);
return Qnil;
}
int err;
if (init_result == PM_STRING_INIT_ERROR_DIRECTORY) {
err = EISDIR;
} else {
#ifdef _WIN32
err = rb_w32_map_errno(GetLastError());
#else
err = errno;
#endif
}
VALUE error;
if (load_error) {
VALUE message = rb_str_buf_new_cstr(strerror(err));
rb_str_cat2(message, " -- ");
rb_str_append(message, filepath);
error = rb_exc_new3(rb_eLoadError, message);
rb_ivar_set(error, rb_intern_const("@path"), filepath);
} else {
error = rb_syserr_new(err, RSTRING_PTR(filepath));
RB_GC_GUARD(filepath);
}
return error;
}
/**
* Parse the given filepath and store the resulting scope node in the given
* parse result struct. It returns a Ruby error if the file cannot be read or
* if it cannot be parsed properly. It is assumed that the parse result object
* is zeroed out.
*/
VALUE
pm_parse_file(pm_parse_result_t *result, VALUE filepath, VALUE *script_lines)
{
result->node.filepath_encoding = rb_enc_get(filepath);
pm_options_filepath_set(&result->options, RSTRING_PTR(filepath));
RB_GC_GUARD(filepath);
pm_options_version_for_current_ruby_set(&result->options);
pm_parser_init(&result->parser, pm_string_source(&result->input), pm_string_length(&result->input), &result->options);
pm_node_t *node = pm_parse(&result->parser);
VALUE error = pm_parse_process(result, node, script_lines);
// If we're parsing a filepath, then we need to potentially support the
// SCRIPT_LINES__ constant, which can be a hash that has an array of lines
// of every read file.
ID id_script_lines = rb_intern("SCRIPT_LINES__");
if (rb_const_defined_at(rb_cObject, id_script_lines)) {
VALUE constant_script_lines = rb_const_get_at(rb_cObject, id_script_lines);
if (RB_TYPE_P(constant_script_lines, T_HASH)) {
rb_hash_aset(constant_script_lines, filepath, pm_parse_file_script_lines(&result->node, &result->parser));
}
}
return error;
}
/**
* Load and then parse the given filepath. It returns a Ruby error if the file
* cannot be read or if it cannot be parsed properly.
*/
VALUE
pm_load_parse_file(pm_parse_result_t *result, VALUE filepath, VALUE *script_lines)
{
VALUE error = pm_load_file(result, filepath, false);
if (NIL_P(error)) {
error = pm_parse_file(result, filepath, script_lines);
}
return error;
}
/**
* Parse the given source that corresponds to the given filepath and store the
* resulting scope node in the given parse result struct. It is assumed that the
* parse result object is zeroed out. If the string fails to parse, then a Ruby
* error is returned.
*/
VALUE
pm_parse_string(pm_parse_result_t *result, VALUE source, VALUE filepath, VALUE *script_lines)
{
rb_encoding *encoding = rb_enc_get(source);
if (!rb_enc_asciicompat(encoding)) {
return rb_exc_new_cstr(rb_eArgError, "invalid source encoding");
}
pm_options_frozen_string_literal_init(&result->options);
pm_string_constant_init(&result->input, RSTRING_PTR(source), RSTRING_LEN(source));
pm_options_encoding_set(&result->options, rb_enc_name(encoding));
result->node.filepath_encoding = rb_enc_get(filepath);
pm_options_filepath_set(&result->options, RSTRING_PTR(filepath));
RB_GC_GUARD(filepath);
pm_options_version_for_current_ruby_set(&result->options);
pm_parser_init(&result->parser, pm_string_source(&result->input), pm_string_length(&result->input), &result->options);
pm_node_t *node = pm_parse(&result->parser);
return pm_parse_process(result, node, script_lines);
}
struct rb_stdin_wrapper {
VALUE rb_stdin;
int eof_seen;
};
static int
pm_parse_stdin_eof(void *stream)
{
struct rb_stdin_wrapper * wrapped_stdin = (struct rb_stdin_wrapper *)stream;
return wrapped_stdin->eof_seen;
}
/**
* An implementation of fgets that is suitable for use with Ruby IO objects.
*/
static char *
pm_parse_stdin_fgets(char *string, int size, void *stream)
{
RUBY_ASSERT(size > 0);
struct rb_stdin_wrapper * wrapped_stdin = (struct rb_stdin_wrapper *)stream;
VALUE line = rb_funcall(wrapped_stdin->rb_stdin, rb_intern("gets"), 1, INT2FIX(size - 1));
if (NIL_P(line)) {
return NULL;
}
const char *cstr = RSTRING_PTR(line);
long length = RSTRING_LEN(line);
memcpy(string, cstr, length);
string[length] = '\0';
// We're reading strings from stdin via gets. We'll assume that if the
// string is smaller than the requested length, and doesn't end with a
// newline, that we hit EOF.
if (length < (size - 1) && string[length - 1] != '\n') {
wrapped_stdin->eof_seen = 1;
}
return string;
}
// We need access to this function when we're done parsing stdin.
void rb_reset_argf_lineno(long n);
/**
* Parse the source off STDIN and store the resulting scope node in the given
* parse result struct. It is assumed that the parse result object is zeroed
* out. If the stream fails to parse, then a Ruby error is returned.
*/
VALUE
pm_parse_stdin(pm_parse_result_t *result)
{
pm_options_frozen_string_literal_init(&result->options);
struct rb_stdin_wrapper wrapped_stdin = {
rb_stdin,
0
};
pm_buffer_t buffer;
pm_node_t *node = pm_parse_stream(&result->parser, &buffer, (void *) &wrapped_stdin, pm_parse_stdin_fgets, pm_parse_stdin_eof, &result->options);
// Copy the allocated buffer contents into the input string so that it gets
// freed. At this point we've handed over ownership, so we don't need to
// free the buffer itself.
pm_string_owned_init(&result->input, (uint8_t *) pm_buffer_value(&buffer), pm_buffer_length(&buffer));
// When we're done parsing, we reset $. because we don't want the fact that
// we went through an IO object to be visible to the user.
rb_reset_argf_lineno(0);
return pm_parse_process(result, node, NULL);
}
#define PM_VERSION_FOR_RELEASE(major, minor) PM_VERSION_FOR_RELEASE_IMPL(major, minor)
#define PM_VERSION_FOR_RELEASE_IMPL(major, minor) PM_OPTIONS_VERSION_CRUBY_##major##_##minor
void pm_options_version_for_current_ruby_set(pm_options_t *options) {
options->version = PM_VERSION_FOR_RELEASE(RUBY_VERSION_MAJOR, RUBY_VERSION_MINOR);
}
#undef NEW_ISEQ
#define NEW_ISEQ OLD_ISEQ
#undef NEW_CHILD_ISEQ
#define NEW_CHILD_ISEQ OLD_CHILD_ISEQ
|