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
|
/* pp.c
*
* Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
* 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
*
* You may distribute under the terms of either the GNU General Public
* License or the Artistic License, as specified in the README file.
*
*/
/*
* 'It's a big house this, and very peculiar. Always a bit more
* to discover, and no knowing what you'll find round a corner.
* And Elves, sir!' --Samwise Gamgee
*
* [p.225 of _The Lord of the Rings_, II/i: "Many Meetings"]
*/
/* This file contains general pp ("push/pop") functions that execute the
* opcodes that make up a perl program. A typical pp function expects to
* find its arguments on the stack, and usually pushes its results onto
* the stack, hence the 'pp' terminology. Each OP structure contains
* a pointer to the relevant pp_foo() function.
*/
#include "EXTERN.h"
#define PERL_IN_PP_C
#include "perl.h"
#include "keywords.h"
#include "invlist_inline.h"
#include "reentr.h"
#include "regcharclass.h"
/* variations on pp_null */
PP(pp_stub)
{
if (GIMME_V == G_SCALAR)
rpp_xpush_IMM(&PL_sv_undef);
return NORMAL;
}
/* Pushy stuff. */
PP(pp_padcv)
{
dTARGET;
assert(SvTYPE(TARG) == SVt_PVCV);
rpp_xpush_1(TARG);
return NORMAL;
}
PP(pp_introcv)
{
dTARGET;
SvPADSTALE_off(TARG);
return NORMAL;
}
PP(pp_clonecv)
{
dTARGET;
CV * const protocv = PadnamePROTOCV(
PadlistNAMESARRAY(CvPADLIST(find_runcv(NULL)))[ARGTARG]
);
assert(SvTYPE(TARG) == SVt_PVCV);
assert(protocv);
if (CvISXSUB(protocv)) { /* constant */
/* XXX Should we clone it here? */
/* If this changes to use SAVECLEARSV, we can move the SAVECLEARSV
to introcv and remove the SvPADSTALE_off. */
SAVEPADSVANDMORTALIZE(ARGTARG);
PAD_SVl(ARGTARG) = SvREFCNT_inc_simple_NN(protocv);
}
else {
if (CvROOT(protocv)) {
assert(CvCLONE(protocv));
assert(!CvCLONED(protocv));
}
cv_clone_into(protocv,(CV *)TARG);
SAVECLEARSV(PAD_SVl(ARGTARG));
}
return NORMAL;
}
/* Translations. */
/* In some cases this function inspects PL_op. If this function is called
for new op types, more bool parameters may need to be added in place of
the checks.
When noinit is true, the absence of a gv will cause a retval of undef.
This is unrelated to the cv-to-gv assignment case.
*/
static SV *
S_rv2gv(pTHX_ SV *sv, const bool vivify_sv, const bool strict,
const bool noinit)
{
if (!isGV(sv) || SvFAKE(sv)) SvGETMAGIC(sv);
if (SvROK(sv)) {
if (SvAMAGIC(sv)) {
sv = amagic_deref_call(sv, to_gv_amg);
}
wasref:
sv = SvRV(sv);
if (SvTYPE(sv) == SVt_PVIO) {
GV * const gv = MUTABLE_GV(sv_newmortal());
gv_init(gv, 0, "__ANONIO__", 10, 0);
GvIOp(gv) = MUTABLE_IO(sv);
SvREFCNT_inc_void_NN(sv);
sv = MUTABLE_SV(gv);
}
else if (!isGV_with_GP(sv)) {
die("Not a GLOB reference");
}
}
else {
if (!isGV_with_GP(sv)) {
if (!SvOK(sv)) {
/* If this is a 'my' scalar and flag is set then vivify
* NI-S 1999/05/07
*/
if (vivify_sv && sv != &PL_sv_undef) {
GV *gv;
HV *stash;
if (SvREADONLY(sv))
croak_no_modify();
gv = MUTABLE_GV(newSV_type(SVt_NULL));
stash = CopSTASH(PL_curcop);
if (SvTYPE(stash) != SVt_PVHV) stash = NULL;
if (cUNOP->op_targ) {
SV * const namesv = PAD_SV(cUNOP->op_targ);
gv_init_sv(gv, stash, namesv, 0);
}
else {
gv_init_pv(gv, stash, "__ANONIO__", 0);
}
sv_setrv_noinc_mg(sv, MUTABLE_SV(gv));
goto wasref;
}
if (PL_op->op_flags & OPf_REF || strict) {
die(PL_no_usym, "a symbol");
}
if (ckWARN(WARN_UNINITIALIZED))
report_uninit(sv);
return &PL_sv_undef;
}
if (noinit)
{
if (!(sv = MUTABLE_SV(gv_fetchsv_nomg(
sv, GV_ADDMG, SVt_PVGV
))))
return &PL_sv_undef;
}
else {
if (strict) {
die(
PL_no_symref_sv,
sv,
(SvPOKp(sv) && SvCUR(sv)>32 ? "..." : ""),
"a symbol"
);
}
if ((PL_op->op_private & (OPpLVAL_INTRO|OPpDONT_INIT_GV))
== OPpDONT_INIT_GV) {
/* We are the target of a coderef assignment. Return
the scalar unchanged, and let pp_sasssign deal with
things. */
return sv;
}
sv = MUTABLE_SV(gv_fetchsv_nomg(sv, GV_ADD, SVt_PVGV));
}
/* FAKE globs in the symbol table cause weird bugs (#77810) */
SvFAKE_off(sv);
}
}
if (SvFAKE(sv) && !(PL_op->op_private & OPpALLOW_FAKE)) {
SV *newsv = sv_mortalcopy_flags(sv, 0);
SvFAKE_off(newsv);
sv = newsv;
}
return sv;
}
PP(pp_rv2gv)
{
SV *sv = *PL_stack_sp;
sv = S_rv2gv(aTHX_
sv, PL_op->op_private & OPpDEREF,
PL_op->op_private & HINT_STRICT_REFS,
((PL_op->op_flags & OPf_SPECIAL) && !(PL_op->op_flags & OPf_MOD))
|| PL_op->op_type == OP_READLINE
);
if (PL_op->op_private & OPpLVAL_INTRO)
save_gp(MUTABLE_GV(sv), !(PL_op->op_flags & OPf_SPECIAL));
rpp_replace_1_1_NN(sv);
return NORMAL;
}
/* Helper function for pp_rv2sv and pp_rv2av/hv.
*
* Return a GV based on the value of sv, using symbolic references etc.
* On success: leaves argument on stack and returns gv.
* On failure: pops one item off stack;
* then unless (list context and not rv2sv), also pushes undef;
* then returns NULL.
*/
GV *
Perl_softref2xv(pTHX_ SV *const sv, const char *const what,
const svtype type)
{
GV *gv;
PERL_ARGS_ASSERT_SOFTREF2XV;
if (PL_op->op_private & HINT_STRICT_REFS) {
if (SvOK(sv))
die(PL_no_symref_sv, sv,
(SvPOKp(sv) && SvCUR(sv)>32 ? "..." : ""), what);
else
die(PL_no_usym, what);
}
if (!SvOK(sv)) {
if (
PL_op->op_flags & OPf_REF
)
die(PL_no_usym, what);
if (ckWARN(WARN_UNINITIALIZED))
report_uninit(sv);
if (type != SVt_PV && GIMME_V == G_LIST) {
rpp_popfree_1_NN();
return NULL;
}
rpp_replace_1_IMM_NN(&PL_sv_undef);
return NULL;
}
if ((PL_op->op_flags & OPf_SPECIAL) &&
!(PL_op->op_flags & OPf_MOD))
{
if (!(gv = gv_fetchsv_nomg(sv, GV_ADDMG, type)))
{
rpp_replace_1_IMM_NN(&PL_sv_undef);
return NULL;
}
}
else {
gv = gv_fetchsv_nomg(sv, GV_ADD, type);
}
return gv;
}
PP(pp_rv2sv)
{
SV *sv = *PL_stack_sp;
GV *gv = NULL;
SvGETMAGIC(sv);
if (SvROK(sv)) {
if (SvAMAGIC(sv)) {
sv = amagic_deref_call(sv, to_sv_amg);
}
sv = SvRV(sv);
if (SvTYPE(sv) >= SVt_PVAV)
DIE(aTHX_ "Not a SCALAR reference");
}
else {
gv = MUTABLE_GV(sv);
if (!isGV_with_GP(gv)) {
gv = Perl_softref2xv(aTHX_ sv, "a SCALAR", SVt_PV);
if (!gv)
return NORMAL;
}
sv = GvSVn(gv);
}
if (PL_op->op_flags & OPf_MOD) {
if (PL_op->op_private & OPpLVAL_INTRO) {
if (cUNOP->op_first->op_type == OP_NULL)
sv = save_scalar(MUTABLE_GV(*PL_stack_sp));
else if (gv)
sv = save_scalar(gv);
else
croak("%s", PL_no_localize_ref);
}
else if (PL_op->op_private & OPpDEREF)
sv = vivify_ref(sv, PL_op->op_private & OPpDEREF);
}
rpp_replace_1_1_NN(sv);
return NORMAL;
}
PP(pp_av2arylen)
{
AV * const av = MUTABLE_AV(*PL_stack_sp);
const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
if (lvalue) {
SV ** const svp = Perl_av_arylen_p(aTHX_ MUTABLE_AV(av));
if (!*svp) {
*svp = newSV_type(SVt_PVMG);
sv_magic(*svp, MUTABLE_SV(av), PERL_MAGIC_arylen, NULL, 0);
}
SV *sv_al = *svp; /* the temporary SV with arylen magic */
#ifdef PERL_RC_STACK
if (SvREFCNT(av) == 1) {
/* At this point there are two SVs pointing at each other,
* av and sv_al. av -> sv_al is strong (MGf_REFCOUNTED),
* while sv_al -> av is weak, to avoid a leaking loop.
*
* The only thing keeping av alive right now is the ref from
* the stack. We want to swap av and sv_al on the stack, but
* that would trigger freeing av. So keep the ref counts and
* just swap the strong/weak pointer settings.
*
* XXX perhaps this should be done even for SvREFCNT(av)>1 ?
*/
MAGIC *mg_av = mg_find((const SV *)av, PERL_MAGIC_arylen_p);
MAGIC *mg_al = mg_find(sv_al, PERL_MAGIC_arylen);
assert(mg_av);
assert(mg_al);
assert( mg_av->mg_flags & MGf_REFCOUNTED);
assert(!(mg_al->mg_flags & MGf_REFCOUNTED));
mg_av->mg_flags &= ~MGf_REFCOUNTED;
mg_al->mg_flags |= MGf_REFCOUNTED;
*PL_stack_sp = sv_al;
}
else
#endif
rpp_replace_1_1_NN(sv_al);
} else {
SV *sv = newSViv(AvFILL(MUTABLE_AV(av)));
rpp_popfree_1();
rpp_push_1_norc(sv);
}
return NORMAL;
}
PP(pp_pos)
{
SV *sv = *PL_stack_sp;
if (PL_op->op_flags & OPf_MOD || LVRET) {
SV * const ret = newSV_type_mortal(SVt_PVLV);/* Not TARG RT#67838 */
sv_magic(ret, NULL, PERL_MAGIC_pos, NULL, 0);
LvTYPE(ret) = '.';
LvTARG(ret) = SvREFCNT_inc_simple(sv);
rpp_replace_1_1_NN(ret); /* no SvSETMAGIC */
}
else {
const MAGIC * const mg = mg_find_mglob(sv);
if (mg && mg->mg_len != -1) {
STRLEN i = mg->mg_len;
if (PL_op->op_private & OPpTRUEBOOL)
rpp_replace_1_IMM_NN(i ? &PL_sv_yes : &PL_sv_zero);
else {
dTARGET;
if (mg->mg_flags & MGf_BYTES && DO_UTF8(sv))
i = sv_pos_b2u_flags(sv, i, SV_GMAGIC|SV_CONST_RETURN);
TARGu(i,1);
rpp_replace_1_1_NN(targ);
}
return NORMAL;
}
rpp_replace_1_IMM_NN(&PL_sv_undef);
}
return NORMAL;
}
PP(pp_rv2cv)
{
GV *gv;
HV *stash_unused;
const I32 flags = (PL_op->op_flags & OPf_SPECIAL)
? GV_ADDMG
: ((PL_op->op_private & (OPpLVAL_INTRO|OPpMAY_RETURN_CONSTANT))
== OPpMAY_RETURN_CONSTANT)
? GV_ADD|GV_NOEXPAND
: GV_ADD;
/* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
/* (But not in defined().) */
CV *cv = sv_2cv(*PL_stack_sp, &stash_unused, &gv, flags);
if (cv) NOOP;
else if ((flags == (GV_ADD|GV_NOEXPAND)) && gv && SvROK(gv)) {
cv = SvTYPE(SvRV(gv)) == SVt_PVCV
? CV_FROM_REF((SV *)gv)
: MUTABLE_CV(gv);
}
else
cv = MUTABLE_CV(&PL_sv_undef);
rpp_replace_1_1_NN(MUTABLE_SV(cv));
return NORMAL;
}
PP(pp_prototype)
{
CV *cv;
HV *stash;
GV *gv;
SV *ret = &PL_sv_undef;
SV *fn = *PL_stack_sp;
if (SvGMAGICAL(fn))
fn = sv_mortalcopy(fn);
if (SvPOK(fn) && SvCUR(fn) >= 7) {
const char * s = SvPVX_const(fn);
if (memBEGINs(s, SvCUR(fn), "CORE::")) {
const int code = keyword(s + 6, SvCUR(fn) - 6, 1);
if (!code)
DIE(aTHX_ "Can't find an opnumber for \"%" UTF8f "\"",
UTF8fARG(SvFLAGS(fn) & SVf_UTF8, SvCUR(fn)-6, s+6));
{
SV * const sv = core_prototype(NULL, s + 6, code, NULL);
if (sv) ret = sv;
}
goto set;
}
}
cv = sv_2cv(fn, &stash, &gv, 0);
if (cv && SvPOK(cv))
ret = newSVpvn_flags(
CvPROTO(cv), CvPROTOLEN(cv), SVs_TEMP | SvUTF8(cv)
);
set:
rpp_replace_1_1_NN(ret);
return NORMAL;
}
PP(pp_anoncode)
{
CV *cv = MUTABLE_CV(PAD_SV(PL_op->op_targ));
if (CvCLONE(cv))
cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
SV* sv = MUTABLE_SV(cv);
if (LIKELY(PL_op->op_flags & OPf_REF)) {
sv = refto(sv);
}
rpp_xpush_1(sv);
return NORMAL;
}
PP(pp_srefgen)
{
rpp_replace_1_1_NN(refto(*PL_stack_sp));
return NORMAL;
}
/* \( ... list ... ) */
PP(pp_refgen)
{
const U8 gimme = GIMME_V;
dMARK;
if (gimme == G_VOID)
rpp_popfree_to_NN(mark);
else if (gimme == G_SCALAR) {
if (++mark < PL_stack_sp) {
/* 2+ args on stack: free all except top one */
SV *topsv = *PL_stack_sp;
*PL_stack_sp = *mark;
*mark = topsv;
rpp_popfree_to_NN(mark);
}
else if (mark > PL_stack_sp) {
/* 0 args on stack */
rpp_xpush_IMM(&PL_sv_undef);
}
rpp_replace_1_1_NN(refto(*PL_stack_sp));
}
else {
/* G_LIST */
EXTEND_MORTAL(PL_stack_sp - MARK); /* refto() creates mortals */
while (++MARK <= PL_stack_sp) {
SV *sv = *MARK;
SV *rv = refto(sv);
#ifdef PERL_RC_STACK
SvREFCNT_dec(sv);
SvREFCNT_inc(rv);
#endif
*MARK = rv;
}
}
return NORMAL;
}
STATIC SV*
S_refto(pTHX_ SV *sv)
{
SV* rv;
PERL_ARGS_ASSERT_REFTO;
if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
if (LvTARGLEN(sv))
vivify_defelem(sv);
if (!(sv = LvTARG(sv)))
sv = &PL_sv_undef;
else
SvREFCNT_inc_void_NN(sv);
}
else if (SvTYPE(sv) == SVt_PVAV) {
if (!AvREAL((const AV *)sv) && AvREIFY((const AV *)sv))
av_reify(MUTABLE_AV(sv));
SvTEMP_off(sv);
SvREFCNT_inc_void_NN(sv);
}
else if (SvPADTMP(sv)) {
sv = newSVsv(sv);
}
else if (UNLIKELY(SvSMAGICAL(sv) && mg_find(sv, PERL_MAGIC_nonelem)))
sv_unmagic(SvREFCNT_inc_simple_NN(sv), PERL_MAGIC_nonelem);
else {
SvTEMP_off(sv);
SvREFCNT_inc_void_NN(sv);
}
rv = newSV_type_mortal(SVt_IV);
sv_setrv_noinc(rv, sv);
return rv;
}
PP(pp_ref)
{
SV * const sv = *PL_stack_sp;
SvGETMAGIC(sv);
if (!SvROK(sv)) {
rpp_replace_1_IMM_NN(&PL_sv_no);
return NORMAL;
}
/* op is in boolean context? */
if ( (PL_op->op_private & OPpTRUEBOOL)
|| ( (PL_op->op_private & OPpMAYBE_TRUEBOOL)
&& block_gimme() == G_VOID))
{
/* refs are always true - unless it's to an object blessed into a
* class with a false name, i.e. "0". So we have to check for
* that remote possibility. The following is is basically an
* unrolled SvTRUE(sv_reftype(rv)) */
SV * const rv = SvRV(sv);
if (SvOBJECT(rv)) {
HV *stash = SvSTASH(rv);
HEK *hek = HvNAME_HEK(stash);
if (hek) {
I32 len = HEK_LEN(hek);
/* bail out and do it the hard way? */
if (UNLIKELY(
len == HEf_SVKEY
|| (len == 1 && HEK_KEY(hek)[0] == '0')
))
goto do_sv_ref;
}
}
rpp_replace_1_IMM_NN(&PL_sv_yes);
return NORMAL;
}
do_sv_ref:
{
dTARGET;
sv_ref(TARG, SvRV(sv), TRUE);
rpp_replace_1_1_NN(TARG);
SvSETMAGIC(TARG);
return NORMAL;
}
}
PP(pp_bless)
{
HV *stash;
SV **sp = PL_stack_sp;
if (MAXARG == 1)
{
curstash:
stash = CopSTASH(PL_curcop);
if (SvTYPE(stash) != SVt_PVHV)
croak("Attempt to bless into a freed package");
}
else {
SV * const ssv = *sp--;
STRLEN len;
const char *ptr;
if (!ssv)
goto curstash;
SvGETMAGIC(ssv);
if (SvROK(ssv)) {
if (!SvAMAGIC(ssv)) {
frog:
croak("Attempt to bless into a reference");
}
/* SvAMAGIC is on here, but it only means potentially overloaded,
so after stringification: */
ptr = SvPV_nomg_const(ssv,len);
/* We need to check the flag again: */
if (!SvAMAGIC(ssv)) goto frog;
}
else ptr = SvPV_nomg_const(ssv,len);
if (len == 0)
ck_warner(packWARN(WARN_MISC),
"Explicit blessing to '' (assuming package main)");
stash = gv_stashpvn(ptr, len, GV_ADD|SvUTF8(ssv));
}
(void)sv_bless(*sp, stash);
if (PL_stack_sp > sp)
rpp_popfree_1();
return NORMAL;
}
PP(pp_gelem)
{
SV *sv = PL_stack_sp[0];
STRLEN len;
const char * const elem = SvPV_const(sv, len);
GV * const gv = MUTABLE_GV(PL_stack_sp[-1]);
SV * tmpRef = NULL;
sv = NULL;
if (elem) {
/* elem will always be NUL terminated. */
switch (*elem) {
case 'A':
if (memEQs(elem, len, "ARRAY"))
{
tmpRef = MUTABLE_SV(GvAV(gv));
if (tmpRef && !AvREAL((const AV *)tmpRef)
&& AvREIFY((const AV *)tmpRef))
av_reify(MUTABLE_AV(tmpRef));
}
break;
case 'C':
if (memEQs(elem, len, "CODE"))
tmpRef = MUTABLE_SV(GvCVu(gv));
break;
case 'F':
if (memEQs(elem, len, "FILEHANDLE")) {
tmpRef = MUTABLE_SV(GvIOp(gv));
}
else
if (memEQs(elem, len, "FORMAT"))
tmpRef = MUTABLE_SV(GvFORM(gv));
break;
case 'G':
if (memEQs(elem, len, "GLOB"))
tmpRef = MUTABLE_SV(gv);
break;
case 'H':
if (memEQs(elem, len, "HASH"))
tmpRef = MUTABLE_SV(GvHV(gv));
break;
case 'I':
if (memEQs(elem, len, "IO"))
tmpRef = MUTABLE_SV(GvIOp(gv));
break;
case 'N':
if (memEQs(elem, len, "NAME"))
sv = newSVhek(GvNAME_HEK(gv));
break;
case 'P':
if (memEQs(elem, len, "PACKAGE")) {
const HV * const stash = GvSTASH(gv);
const HEK * const hek = stash ? HvNAME_HEK(stash) : NULL;
sv = hek ? newSVhek(hek) : newSVpvs("__ANON__");
}
break;
case 'S':
if (memEQs(elem, len, "SCALAR"))
tmpRef = GvSVn(gv);
break;
}
}
if (tmpRef)
sv = newRV(tmpRef);
if (sv)
sv_2mortal(sv);
else
sv = &PL_sv_undef;
rpp_replace_2_1_NN(sv);
return NORMAL;
}
/* Pattern matching */
PP(pp_study)
{
SV *sv = *PL_stack_sp;
STRLEN len;
(void)SvPV(sv, len);
if (len == 0 || len > I32_MAX || !SvPOK(sv) || SvUTF8(sv) || SvVALID(sv)) {
/* Historically, study was skipped in these cases. */
rpp_replace_1_IMM_NN(&PL_sv_no);
return NORMAL;
}
/* Make study a no-op. It's no longer useful and its existence
complicates matters elsewhere. */
rpp_replace_1_IMM_NN(&PL_sv_yes);
return NORMAL;
}
/* also used for: pp_transr() */
PP_wrapped(pp_trans, ((PL_op->op_flags & OPf_STACKED) ? 1 : 0), 0)
{
dSP;
SV *sv;
if (PL_op->op_flags & OPf_STACKED)
sv = POPs;
else {
EXTEND(SP,1);
if (ARGTARG)
sv = PAD_SV(ARGTARG);
else {
sv = DEFSV;
}
}
if(PL_op->op_type == OP_TRANSR) {
STRLEN len;
const char * const pv = SvPV(sv,len);
SV * const newsv = newSVpvn_flags(pv, len, SVs_TEMP|SvUTF8(sv));
do_trans(newsv);
PUSHs(newsv);
}
else {
Size_t i = do_trans(sv);
mPUSHi((UV)i);
}
RETURN;
}
/* Lvalue operators. */
static size_t
S_do_chomp(pTHX_ SV *retval, SV *sv, bool chomping)
{
STRLEN len;
char *s;
size_t count = 0;
PERL_ARGS_ASSERT_DO_CHOMP;
if (chomping && (RsSNARF(PL_rs) || RsRECORD(PL_rs)))
return 0;
if (SvTYPE(sv) == SVt_PVAV) {
SSize_t i;
AV *const av = MUTABLE_AV(sv);
const SSize_t max = AvFILL(av);
for (i = 0; i <= max; i++) {
sv = MUTABLE_SV(av_fetch(av, i, FALSE));
if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
count += do_chomp(retval, sv, chomping);
}
return count;
}
else if (SvTYPE(sv) == SVt_PVHV) {
HV* const hv = MUTABLE_HV(sv);
HE* entry;
(void)hv_iterinit(hv);
while ((entry = hv_iternext(hv)))
count += do_chomp(retval, hv_iterval(hv,entry), chomping);
return count;
}
else if (SvREADONLY(sv)) {
croak_no_modify();
}
s = SvPV(sv, len);
if (chomping) {
if (s && len) {
void *free_me = NULL;
s += --len;
if (RsPARA(PL_rs)) {
if (*s != '\n')
goto nope_free_nothing;
++count;
while (len && s[-1] == '\n') {
--len;
--s;
++count;
}
}
else {
STRLEN rslen, rs_charlen;
const char *rsptr = SvPV_const(PL_rs, rslen);
rs_charlen = SvUTF8(PL_rs)
? sv_len_utf8(PL_rs)
: rslen;
if (SvUTF8(PL_rs) != SvUTF8(sv)) {
/* Assumption is that rs is shorter than the scalar. */
if (SvUTF8(PL_rs)) {
/* RS is utf8, scalar is 8 bit. */
if (! utf8_to_bytes_new_pv((const U8 **) &rsptr, &rslen,
&free_me))
{
/* Cannot downgrade, therefore cannot possibly
* match. */
goto nope_free_nothing;
}
}
else {
/* RS is 8 bit, scalar is utf8. */
rsptr = (char *) bytes_to_utf8_free_me((U8*) rsptr,
&rslen,
&free_me);
}
}
if (rslen == 1) {
if (*s != *rsptr)
goto nope_free_all;
++count;
}
else {
if (len < rslen - 1)
goto nope_free_all;
len -= rslen - 1;
s -= rslen - 1;
if (memNE(s, rsptr, rslen))
goto nope_free_all;
count += rs_charlen;
}
}
SvPV_force_nomg_nolen(sv);
SvCUR_set(sv, len);
*SvEND(sv) = '\0';
SvNIOK_off(sv);
SvSETMAGIC(sv);
nope_free_all:
Safefree(free_me);
nope_free_nothing: ;
}
} else {
if (len && (!SvPOK(sv) || SvIsCOW(sv)))
s = SvPV_force_nomg(sv, len);
if (DO_UTF8(sv)) {
if (s && len) {
char * const send = s + len;
char * const start = s;
s = (char *) utf8_hop_back((U8 *) send, -1, (U8 *) start);
if (is_utf8_string((U8*)s, send - s)) {
sv_setpvn(retval, s, send - s);
*s = '\0';
SvCUR_set(sv, s - start);
SvNIOK_off(sv);
SvUTF8_on(retval);
}
}
else
SvPVCLEAR(retval);
}
else if (s && len) {
s += --len;
sv_setpvn(retval, s, 1);
*s = '\0';
SvCUR_set(sv, len);
SvUTF8_off(sv);
SvNIOK_off(sv);
}
else
SvPVCLEAR(retval);
SvSETMAGIC(sv);
}
return count;
}
/* also used for: pp_schomp() */
PP(pp_schop)
{
dTARGET;
const bool chomping = PL_op->op_type == OP_SCHOMP;
const size_t count = do_chomp(TARG, *PL_stack_sp, chomping);
if (chomping)
sv_setiv(TARG, count);
SvSETMAGIC(TARG);
rpp_replace_1_1_NN(TARG);
return NORMAL;
}
/* also used for: pp_chomp() */
PP_wrapped(pp_chop, 0, 1)
{
dSP; dMARK; dTARGET; dORIGMARK;
const bool chomping = PL_op->op_type == OP_CHOMP;
size_t count = 0;
while (MARK < SP)
count += do_chomp(TARG, *++MARK, chomping);
if (chomping)
sv_setiv(TARG, count);
SP = ORIGMARK;
XPUSHTARG;
RETURN;
}
PP(pp_undef)
{
SV *sv;
if (!PL_op->op_private) {
rpp_xpush_IMM(&PL_sv_undef);
return NORMAL;
}
if (PL_op->op_private & OPpTARGET_MY) {
/* $lex = undef, or undef $lex */
SV** const padentry = &PAD_SVl(PL_op->op_targ);
sv = *padentry;
if (UNLIKELY((PL_op->op_flags & OPf_WANT) != OPf_WANT_VOID))
rpp_xpush_1(sv);
if ((PL_op->op_private & (OPpLVAL_INTRO|OPpPAD_STATE))
== OPpLVAL_INTRO)
{
save_clearsv(padentry);
}
} else {
sv = *PL_stack_sp;
if (!sv) {
/* sv is NULL when pp_undef is invoked like this:
* *myundef = \&CORE::undef; &myundef();
*/
*PL_stack_sp = &PL_sv_undef;
return NORMAL;
}
}
if (SvTHINKFIRST(sv))
sv_force_normal_flags(sv, SV_COW_DROP_PV|SV_IMMEDIATE_UNREF);
switch (SvTYPE(sv)) {
case SVt_NULL:
break;
case SVt_PVAV:
av_undef(MUTABLE_AV(sv));
break;
case SVt_PVHV:
hv_undef(MUTABLE_HV(sv));
break;
case SVt_PVCV:
if (cv_const_sv((const CV *)sv))
ck_warner(packWARN(WARN_MISC),
"Constant subroutine %" SVf " undefined",
SVfARG(CvANON((const CV *)sv)
? newSVpvs_flags("(anonymous)", SVs_TEMP)
: newSVhek_mortal(
CvNAMED(sv)
? CvNAME_HEK((CV *)sv)
: GvENAME_HEK(CvGV((const CV *)sv))
)
));
/* FALLTHROUGH */
case SVt_PVFM:
/* let user-undef'd sub keep its identity */
cv_undef_flags(MUTABLE_CV(sv), CV_UNDEF_KEEP_NAME);
break;
case SVt_PVGV:
assert(isGV_with_GP(sv));
assert(!SvFAKE(sv));
{
GP *gp;
HV *stash;
/* undef *Pkg::meth_name ... */
bool method_changed
= GvCVu((const GV *)sv) && (stash = GvSTASH((const GV *)sv))
&& HvHasENAME(stash);
/* undef *Foo:: */
if((stash = GvHV((const GV *)sv))) {
if(HvENAME_get(stash))
SvREFCNT_inc_simple_void_NN(sv_2mortal((SV *)stash));
else stash = NULL;
}
SvREFCNT_inc_simple_void_NN(sv_2mortal(sv));
gp_free(MUTABLE_GV(sv));
Newxz(gp, 1, GP);
GvGP_set(sv, gp_ref(gp));
#ifndef PERL_DONT_CREATE_GVSV
GvSV(sv) = newSV_type(SVt_NULL);
#endif
GvLINE(sv) = CopLINE(PL_curcop);
GvEGV(sv) = MUTABLE_GV(sv);
GvMULTI_on(sv);
if(stash)
mro_package_moved(NULL, stash, (const GV *)sv, 0);
stash = NULL;
/* undef *Foo::ISA */
if( strEQ(GvNAME((const GV *)sv), "ISA")
&& (stash = GvSTASH((const GV *)sv))
&& (method_changed || HvHasENAME(stash)) )
mro_isa_changed_in(stash);
else if(method_changed)
mro_method_changed_in(
GvSTASH((const GV *)sv)
);
break;
}
default:
if (SvTYPE(sv) >= SVt_PV && SvPVX_const(sv) && SvLEN(sv)
&& !(PL_op->op_private & OPpUNDEF_KEEP_PV)
) {
SvPV_free(sv);
SvPV_set(sv, NULL);
SvLEN_set(sv, 0);
}
SvOK_off(sv);
SvSETMAGIC(sv);
}
if (!(PL_op->op_private & OPpTARGET_MY)) {
if (LIKELY((PL_op->op_flags & OPf_WANT) == OPf_WANT_VOID))
rpp_popfree_1_NN();
else
rpp_replace_1_1_NN(&PL_sv_undef);
}
return NORMAL;
}
/* common "slow" code for pp_postinc and pp_postdec */
static OP *
S_postincdec_common(pTHX_ SV *sv, SV *targ)
{
const bool inc =
PL_op->op_type == OP_POSTINC || PL_op->op_type == OP_I_POSTINC;
if (SvROK(sv))
TARG = sv_newmortal();
sv_setsv(TARG, sv);
if (inc)
sv_inc_nomg(sv);
else
sv_dec_nomg(sv);
SvSETMAGIC(sv);
/* special case for undef: see thread at 2003-03/msg00536.html in archive */
if (inc && !SvOK(TARG))
sv_setiv(TARG, 0);
SvSETMAGIC(TARG);
rpp_replace_1_1_NN(TARG);
return NORMAL;
}
/* also used for: pp_i_postinc() */
PP(pp_postinc)
{
dTARGET;
SV *sv = *PL_stack_sp;
/* special-case sv being a simple integer */
if (LIKELY(((sv->sv_flags &
(SVf_THINKFIRST|SVs_GMG|SVf_IVisUV|
SVf_IOK|SVf_NOK|SVf_POK|SVp_NOK|SVp_POK|SVf_ROK))
== SVf_IOK))
&& SvIVX(sv) != IV_MAX)
{
IV iv = SvIVX(sv);
SvIV_set(sv, iv + 1);
TARGi(iv, 0); /* arg not GMG, so can't be tainted */
rpp_replace_1_1_NN(TARG);
return NORMAL;
}
return S_postincdec_common(aTHX_ sv, TARG);
}
/* also used for: pp_i_postdec() */
PP(pp_postdec)
{
dTARGET;
SV *sv = *PL_stack_sp;
/* special-case sv being a simple integer */
if (LIKELY(((sv->sv_flags &
(SVf_THINKFIRST|SVs_GMG|SVf_IVisUV|
SVf_IOK|SVf_NOK|SVf_POK|SVp_NOK|SVp_POK|SVf_ROK))
== SVf_IOK))
&& SvIVX(sv) != IV_MIN)
{
IV iv = SvIVX(sv);
SvIV_set(sv, iv - 1);
TARGi(iv, 0); /* arg not GMG, so can't be tainted */
rpp_replace_1_1_NN(TARG);
return NORMAL;
}
return S_postincdec_common(aTHX_ sv, TARG);
}
/* Ordinary operators. */
PP(pp_pow)
{
SV *targ = (PL_op->op_flags & OPf_STACKED)
? PL_stack_sp[-1]
: PAD_SV(PL_op->op_targ);
if (rpp_try_AMAGIC_2(pow_amg, AMGf_assign|AMGf_numeric))
return NORMAL;
SV *svr = PL_stack_sp[0];
SV *svl = PL_stack_sp[-1];
#ifdef PERL_PRESERVE_IVUV
bool is_int = 0;
/* For integer to integer power, we do the calculation by hand wherever
we're sure it is safe; otherwise we call pow() and try to convert to
integer afterwards. */
if (SvIV_please_nomg(svr) && SvIV_please_nomg(svl)) {
UV power;
bool baseuok;
UV baseuv;
if (SvUOK(svr)) {
power = SvUVX(svr);
} else {
const IV iv = SvIVX(svr);
if (iv >= 0) {
power = iv;
} else {
goto float_it; /* Can't do negative powers this way. */
}
}
baseuok = SvUOK(svl);
if (baseuok) {
baseuv = SvUVX(svl);
} else {
const IV iv = SvIVX(svl);
if (iv >= 0) {
baseuv = iv;
baseuok = TRUE; /* effectively it's a UV now */
} else {
baseuv = -iv; /* abs, baseuok == false records sign */
}
}
/* now we have integer ** positive integer. */
is_int = 1;
/* foo & (foo - 1) is zero only for a power of 2. */
if (!(baseuv & (baseuv - 1))) {
/* We are raising power-of-2 to a positive integer.
The logic here will work for any base (even non-integer
bases) but it can be less accurate than
pow (base,power) or exp (power * log (base)) when the
intermediate values start to spill out of the mantissa.
With powers of 2 we know this can't happen.
And powers of 2 are the favourite thing for perl
programmers to notice ** not doing what they mean. */
NV result = 1.0;
NV base = baseuok ? baseuv : -(NV)baseuv;
if (power & 1) {
result *= base;
}
while (power >>= 1) {
base *= base;
if (power & 1) {
result *= base;
}
}
TARGn(result, 1);
SvIV_please_nomg(svr);
goto ret;
} else {
unsigned int highbit = 8 * sizeof(UV);
unsigned int diff = 8 * sizeof(UV);
while (diff >>= 1) {
highbit -= diff;
if (baseuv >> highbit) {
highbit += diff;
}
}
/* we now have baseuv < 2 ** highbit */
if (power * highbit <= 8 * sizeof(UV)) {
/* result will definitely fit in UV, so use UV math
on same algorithm as above */
UV result = 1;
UV base = baseuv;
const bool odd_power = cBOOL(power & 1);
if (odd_power) {
result *= base;
}
while (power >>= 1) {
base *= base;
if (power & 1) {
result *= base;
}
}
if (baseuok || !odd_power)
/* answer is positive */
TARGu(result, 1);
else if (result <= ABS_IV_MIN)
/* answer negative, fits in IV */
TARGi(NEGATE_2IV(result), 1);
else
/* answer negative, doesn't fit */
TARGn(-(NV)result, 1);
goto ret;
}
}
}
float_it:
#endif
{
NV right = SvNV_nomg(svr);
NV left = SvNV_nomg(svl);
#if defined(USE_LONG_DOUBLE) && defined(HAS_AIX_POWL_NEG_BASE_BUG)
/*
We are building perl with long double support and are on an AIX OS
afflicted with a powl() function that wrongly returns NaNQ for any
negative base. This was reported to IBM as PMR #23047-379 on
03/06/2006. The problem exists in at least the following versions
of AIX and the libm fileset, and no doubt others as well:
AIX 4.3.3-ML10 bos.adt.libm 4.3.3.50
AIX 5.1.0-ML04 bos.adt.libm 5.1.0.29
AIX 5.2.0 bos.adt.libm 5.2.0.85
So, until IBM fixes powl(), we provide the following workaround to
handle the problem ourselves. Our logic is as follows: for
negative bases (left), we use fmod(right, 2) to check if the
exponent is an odd or even integer:
- if odd, powl(left, right) == -powl(-left, right)
- if even, powl(left, right) == powl(-left, right)
If the exponent is not an integer, the result is rightly NaNQ, so
we just return that (as NV_NAN).
*/
if (left < 0.0) {
NV mod2 = Perl_fmod( right, 2.0 );
if (mod2 == 1.0 || mod2 == -1.0) { /* odd integer */
TARGn(-Perl_pow(-left, right), 1);
} else if (mod2 == 0.0) { /* even integer */
TARGn(Perl_pow(-left, right), 1);
} else { /* fractional power */
TARGn(NV_NAN, 1);
}
} else {
TARGn(Perl_pow(left, right), 1);
}
#elif IVSIZE == 4 && defined(LONGDOUBLE_DOUBLEDOUBLE) && defined(USE_LONG_DOUBLE)
/*
Under these conditions, if a known libm bug exists, Perl_pow() could return
an incorrect value if the correct value is an integer in the range of around
25 or more bits. The error is always quite small, so we work around it by
rounding to the nearest integer value ... but only if is_int is true.
See https://github.com/Perl/perl5/issues/19625.
*/
if (is_int) {
TARGn(roundl(Perl_pow(left, right)), 1);
}
else
TARGn(Perl_pow(left, right), 1 );
#else
TARGn(Perl_pow(left, right), 1);
#endif /* HAS_AIX_POWL_NEG_BASE_BUG */
#ifdef PERL_PRESERVE_IVUV
if (is_int)
SvIV_please_nomg(svr);
#endif
}
ret:
rpp_replace_2_1_NN(targ);
return NORMAL;
}
PP(pp_multiply)
{
SV *targ = (PL_op->op_flags & OPf_STACKED)
? PL_stack_sp[-1]
: PAD_SV(PL_op->op_targ);
if (rpp_try_AMAGIC_2(mult_amg, AMGf_assign|AMGf_numeric))
return NORMAL;
SV *svr = PL_stack_sp[0];
SV *svl = PL_stack_sp[-1];
#ifdef PERL_PRESERVE_IVUV
/* special-case some simple common cases */
if (!((svl->sv_flags|svr->sv_flags) & (SVf_IVisUV|SVs_GMG))) {
IV il, ir;
U32 flags = (svl->sv_flags & svr->sv_flags);
if (flags & SVf_IOK) {
/* both args are simple IVs */
UV topl, topr;
il = SvIVX(svl);
ir = SvIVX(svr);
do_iv:
topl = ((UV)il) >> (UVSIZE * 4 - 1);
topr = ((UV)ir) >> (UVSIZE * 4 - 1);
/* if both are in a range that can't under/overflow, do a
* simple integer multiply: if the top halves(*) of both numbers
* are 00...00 or 11...11, then it's safe.
* (*) for 32-bits, the "top half" is the top 17 bits,
* for 64-bits, its 33 bits */
if (!(
((topl+1) | (topr+1))
& ( (((UV)1) << (UVSIZE * 4 + 1)) - 2) /* 11..110 */
)) {
TARGi(il * ir, 0); /* args not GMG, so can't be tainted */
goto ret;
}
goto generic;
}
else if (flags & SVf_NOK) {
/* both args are NVs */
NV nl = SvNVX(svl);
NV nr = SvNVX(svr);
NV result;
if (lossless_NV_to_IV(nl, &il) && lossless_NV_to_IV(nr, &ir)) {
/* nothing was lost by converting to IVs */
goto do_iv;
}
result = nl * nr;
# if defined(__sgi) && defined(USE_LONG_DOUBLE) && LONG_DOUBLEKIND == LONG_DOUBLE_IS_DOUBLEDOUBLE_128_BIT_BE_BE && NVSIZE == 16
if (Perl_isinf(result)) {
Zero((U8*)&result + 8, 8, U8);
}
# endif
TARGn(result, 0); /* args not GMG, so can't be tainted */
goto ret;
}
}
generic:
if (SvIV_please_nomg(svr)) {
/* Unless the left argument is integer in range we are going to have to
use NV maths. Hence only attempt to coerce the right argument if
we know the left is integer. */
/* Left operand is defined, so is it IV? */
if (SvIV_please_nomg(svl)) {
bool auvok = SvUOK(svl);
bool buvok = SvUOK(svr);
const UV topmask = (~ (UV)0) << (4 * sizeof (UV));
const UV botmask = ~((~ (UV)0) << (4 * sizeof (UV)));
UV alow;
UV ahigh;
UV blow;
UV bhigh;
if (auvok) {
alow = SvUVX(svl);
} else {
const IV aiv = SvIVX(svl);
if (aiv >= 0) {
alow = aiv;
auvok = TRUE; /* effectively it's a UV now */
} else {
/* abs, auvok == false records sign */
alow = NEGATE_2UV(aiv);
}
}
if (buvok) {
blow = SvUVX(svr);
} else {
const IV biv = SvIVX(svr);
if (biv >= 0) {
blow = biv;
buvok = TRUE; /* effectively it's a UV now */
} else {
/* abs, buvok == false records sign */
blow = NEGATE_2UV(biv);
}
}
/* If this does sign extension on unsigned it's time for plan B */
ahigh = alow >> (4 * sizeof (UV));
alow &= botmask;
bhigh = blow >> (4 * sizeof (UV));
blow &= botmask;
if (ahigh && bhigh) {
NOOP;
/* eg 32 bit is at least 0x10000 * 0x10000 == 0x100000000
which is overflow. Drop to NVs below. */
} else if (!ahigh && !bhigh) {
/* eg 32 bit is at most 0xFFFF * 0xFFFF == 0xFFFE0001
so the unsigned multiply cannot overflow. */
const UV product = alow * blow;
if (auvok == buvok) {
/* -ve * -ve or +ve * +ve gives a +ve result. */
TARGu(product, 1);
goto ret;
} else if (product <= ABS_IV_MIN) {
/* -ve result, which could overflow an IV */
TARGi(NEGATE_2IV(product), 1);
goto ret;
} /* else drop to NVs below. */
} else {
/* One operand is large, 1 small */
UV product_middle;
if (bhigh) {
/* swap the operands */
ahigh = bhigh;
bhigh = blow; /* bhigh now the temp var for the swap */
blow = alow;
alow = bhigh;
}
/* now, ((ahigh * blow) << half_UV_len) + (alow * blow)
multiplies can't overflow. shift can, add can, -ve can. */
product_middle = ahigh * blow;
if (!(product_middle & topmask)) {
/* OK, (ahigh * blow) won't lose bits when we shift it. */
UV product_low;
product_middle <<= (4 * sizeof (UV));
product_low = alow * blow;
/* as for pp_add, UV + something mustn't get smaller.
IIRC ANSI mandates this wrapping *behaviour* for
unsigned whatever the actual representation*/
product_low += product_middle;
if (product_low >= product_middle) {
/* didn't overflow */
if (auvok == buvok) {
/* -ve * -ve or +ve * +ve gives a +ve result. */
TARGu(product_low, 1);
goto ret;
} else if (product_low <= ABS_IV_MIN) {
/* -ve result, which could overflow an IV */
TARGi(NEGATE_2IV(product_low), 1);
goto ret;
} /* else drop to NVs below. */
}
} /* product_middle too large */
} /* ahigh && bhigh */
} /* SvIOK(svl) */
} /* SvIOK(svr) */
#endif
{
NV right = SvNV_nomg(svr);
NV left = SvNV_nomg(svl);
NV result = left * right;
#if defined(__sgi) && defined(USE_LONG_DOUBLE) && LONG_DOUBLEKIND == LONG_DOUBLE_IS_DOUBLEDOUBLE_128_BIT_BE_BE && NVSIZE == 16
if (Perl_isinf(result)) {
Zero((U8*)&result + 8, 8, U8);
}
#endif
TARGn(result, 1);
goto ret;
}
ret:
rpp_replace_2_1_NN(targ);
return NORMAL;
}
PP(pp_divide)
{
SV *targ = (PL_op->op_flags & OPf_STACKED)
? PL_stack_sp[-1]
: PAD_SV(PL_op->op_targ);
if (rpp_try_AMAGIC_2(div_amg, AMGf_assign|AMGf_numeric))
return NORMAL;
SV *svr = PL_stack_sp[0];
SV *svl = PL_stack_sp[-1];
/* Only try to do UV divide first
if ((SLOPPYDIVIDE is true) or
(PERL_PRESERVE_IVUV is true and one or both SV is a UV too large
to preserve))
The assumption is that it is better to use floating point divide
whenever possible, only doing integer divide first if we can't be sure.
If NV_PRESERVES_UV is true then we know at compile time that no UV
can be too large to preserve, so don't need to compile the code to
test the size of UVs. */
#if defined(SLOPPYDIVIDE) || (defined(PERL_PRESERVE_IVUV) && !defined(NV_PRESERVES_UV))
# define PERL_TRY_UV_DIVIDE
/* ensure that 20./5. == 4. */
#endif
#ifdef PERL_TRY_UV_DIVIDE
if (SvIV_please_nomg(svr) && SvIV_please_nomg(svl)) {
bool left_non_neg = SvUOK(svl);
bool right_non_neg = SvUOK(svr);
UV left;
UV right;
if (right_non_neg) {
right = SvUVX(svr);
}
else {
const IV biv = SvIVX(svr);
if (biv >= 0) {
right = biv;
right_non_neg = TRUE; /* effectively it's a UV now */
}
else {
right = NEGATE_2UV(biv);
}
}
/* historically undef()/0 gives a "Use of uninitialized value"
warning before dieing, hence this test goes here.
If it were immediately before the second SvIV_please, then
DIE() would be invoked before left was even inspected, so
no inspection would give no warning. */
if (right == 0)
DIE(aTHX_ "Illegal division by zero");
if (left_non_neg) {
left = SvUVX(svl);
}
else {
const IV aiv = SvIVX(svl);
if (aiv >= 0) {
left = aiv;
left_non_neg = TRUE; /* effectively it's a UV now */
}
else {
left = NEGATE_2UV(aiv);
}
}
if (left >= right
#ifdef SLOPPYDIVIDE
/* For sloppy divide we always attempt integer division. */
#else
/* Otherwise we only attempt it if either or both operands
would not be preserved by an NV. If both fit in NVs
we fall through to the NV divide code below. However,
as left >= right to ensure integer result here, we know that
we can skip the test on the right operand - right big
enough not to be preserved can't get here unless left is
also too big. */
&& (left > ((UV)1 << NV_PRESERVES_UV_BITS))
#endif
) {
/* Integer division can't overflow, but it can be imprecise. */
/* Modern compilers optimize division followed by
* modulo into a single div instruction */
const UV result = left / right;
if (left % right == 0) {
/* result is valid */
if (left_non_neg == right_non_neg) {
/* signs identical, result is positive. */
TARGu(result, 1);
goto ret;
}
/* 2s complement assumption */
if (result <= ABS_IV_MIN)
TARGi(NEGATE_2IV(result), 1);
else {
/* It's exact but too negative for IV. */
TARGn(-(NV)result, 1);
}
goto ret;
} /* tried integer divide but it was not an integer result */
} /* else (PERL_ABS(result) < 1.0) or (both UVs in range for NV) */
} /* one operand wasn't SvIOK */
#endif /* PERL_TRY_UV_DIVIDE */
{
NV right = SvNV_nomg(svr);
NV left = SvNV_nomg(svl);
#if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
if (! Perl_isnan(right) && right == 0.0)
#else
if (right == 0.0)
#endif
DIE(aTHX_ "Illegal division by zero");
TARGn(left / right, 1);
goto ret; /* redundant, but silence -Wunused-label */
}
ret:
rpp_replace_2_1_NN(targ);
return NORMAL;
}
PP(pp_modulo)
{
SV *targ = (PL_op->op_flags & OPf_STACKED)
? PL_stack_sp[-1]
: PAD_SV(PL_op->op_targ);
if (rpp_try_AMAGIC_2(modulo_amg, AMGf_assign|AMGf_numeric))
return NORMAL;
{
UV left = 0;
UV right = 0;
bool left_neg = FALSE;
bool right_neg = FALSE;
bool use_double = FALSE;
bool dright_valid = FALSE;
NV dright = 0.0;
NV dleft = 0.0;
SV * const svr = PL_stack_sp[0];
SV * const svl = PL_stack_sp[-1];
if (SvIV_please_nomg(svr)) {
right_neg = !SvUOK(svr);
if (!right_neg) {
right = SvUVX(svr);
} else {
const IV biv = SvIVX(svr);
if (biv >= 0) {
right = biv;
right_neg = FALSE; /* effectively it's a UV now */
} else {
right = NEGATE_2UV(biv);
}
}
}
else {
dright = SvNV_nomg(svr);
right_neg = dright < 0;
if (right_neg)
dright = -dright;
if (dright < UV_MAX_P1) {
right = U_V(dright);
dright_valid = TRUE; /* In case we need to use double below. */
} else {
use_double = TRUE;
}
}
/* At this point use_double is only true if right is out of range for
a UV. In range NV has been rounded down to nearest UV and
use_double false. */
if (!use_double && SvIV_please_nomg(svl)) {
left_neg = !SvUOK(svl);
if (!left_neg) {
left = SvUVX(svl);
} else {
const IV aiv = SvIVX(svl);
if (aiv >= 0) {
left = aiv;
left_neg = FALSE; /* effectively it's a UV now */
} else {
left = NEGATE_2UV(aiv);
}
}
}
else {
dleft = SvNV_nomg(svl);
left_neg = dleft < 0;
if (left_neg)
dleft = -dleft;
/* This should be exactly the 5.6 behaviour - if left and right are
both in range for UV then use U_V() rather than floor. */
if (!use_double) {
if (dleft < UV_MAX_P1) {
/* right was in range, so is dleft, so use UVs not double.
*/
left = U_V(dleft);
}
/* left is out of range for UV, right was in range, so promote
right (back) to double. */
else {
/* The +0.5 is used in 5.6 even though it is not strictly
consistent with the implicit +0 floor in the U_V()
inside the #if 1. */
dleft = Perl_floor(dleft + 0.5);
use_double = TRUE;
if (dright_valid)
dright = Perl_floor(dright + 0.5);
else
dright = right;
}
}
}
if (use_double) {
NV dans;
if (!dright)
DIE(aTHX_ "Illegal modulus zero");
dans = Perl_fmod(dleft, dright);
if ((left_neg != right_neg) && dans)
dans = dright - dans;
if (right_neg)
dans = -dans;
sv_setnv(TARG, dans);
}
else {
UV ans;
if (!right)
DIE(aTHX_ "Illegal modulus zero");
ans = left % right;
if ((left_neg != right_neg) && ans)
ans = right - ans;
if (right_neg) {
if (ans <= ABS_IV_MIN)
sv_setiv(TARG, NEGATE_2IV(ans));
else
sv_setnv(TARG, -(NV)ans);
}
else
sv_setuv(TARG, ans);
}
SvSETMAGIC(TARG);
rpp_replace_2_1_NN(targ);
return NORMAL;
}
}
PP_wrapped(pp_repeat,
/* two scalar args or one list */
((PL_op->op_private & OPpREPEAT_DOLIST) ? 0 : 2),
((PL_op->op_private & OPpREPEAT_DOLIST) ? 1 : 0))
{
dSP; dATARGET;
IV count;
SV *sv;
bool infnan = FALSE;
const U8 gimme = GIMME_V;
if (gimme == G_LIST && PL_op->op_private & OPpREPEAT_DOLIST) {
/* TODO: think of some way of doing list-repeat overloading ??? */
sv = POPs;
SvGETMAGIC(sv);
}
else {
if (UNLIKELY(PL_op->op_private & OPpREPEAT_DOLIST)) {
/* The parser saw this as a list repeat, and there
are probably several items on the stack. But we're
in scalar/void context, and there's no pp_list to save us
now. So drop the rest of the items -- robin@kitsite.com
*/
dMARK;
if (MARK + 1 < SP) {
MARK[1] = TOPm1s;
MARK[2] = TOPs;
}
else {
dTOPss;
ASSUME(MARK + 1 == SP);
MEXTEND(SP, 1);
PUSHs(sv);
MARK[1] = &PL_sv_undef;
}
SP = MARK + 2;
}
tryAMAGICbin_MG(repeat_amg, AMGf_assign);
sv = POPs;
}
if (SvIOKp(sv)) {
if (SvUOK(sv)) {
const UV uv = SvUV_nomg(sv);
if (uv > IV_MAX)
count = IV_MAX; /* The best we can do? */
else
count = uv;
} else {
count = SvIV_nomg(sv);
}
}
else if (SvNOKp(sv)) {
const NV nv = SvNV_nomg(sv);
infnan = Perl_isinfnan(nv);
if (UNLIKELY(infnan)) {
count = 0;
} else {
if (nv < 0.0)
count = -1; /* An arbitrary negative integer */
else
count = (IV)nv;
}
}
else
count = SvIV_nomg(sv);
if (infnan) {
ck_warner(packWARN(WARN_NUMERIC),
"Non-finite repeat count does nothing");
} else if (count < 0) {
count = 0;
ck_warner(packWARN(WARN_NUMERIC),
"Negative repeat count does nothing");
}
if (gimme == G_LIST && PL_op->op_private & OPpREPEAT_DOLIST) {
dMARK;
const SSize_t items = SP - MARK;
const U8 mod = PL_op->op_flags & OPf_MOD;
if (count > 1) {
SSize_t max;
if ( items > SSize_t_MAX / (SSize_t)sizeof(SV *) / count )
/* diag_listed_as: Out of memory during %s extend */
croak("Out of memory during list extend");
max = items * count;
MEXTEND(MARK, max);
while (SP > MARK) {
if (*SP) {
if (mod && SvPADTMP(*SP)) {
*SP = sv_mortalcopy(*SP);
}
SvTEMP_off((*SP));
}
SP--;
}
MARK++;
repeatcpy((char*)(MARK + items), (char*)MARK,
items * sizeof(const SV *), count - 1);
SP += max;
}
else if (count <= 0)
SP = MARK;
}
else { /* Note: mark already snarfed by pp_list */
SV * const tmpstr = POPs;
STRLEN len;
bool isutf;
if (TARG != tmpstr)
sv_setsv_nomg(TARG, tmpstr);
SvPV_force_nomg(TARG, len);
isutf = DO_UTF8(TARG);
if (count != 1) {
if (count < 1)
SvCUR_set(TARG, 0);
else {
STRLEN max;
if ( len > (MEM_SIZE_MAX-1) / (UV)count /* max would overflow */
)
/* diag_listed_as: Out of memory during %s extend */
croak("Out of memory during string extend");
max = (UV)count * len + 1;
SvGROW(TARG, max);
repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
SvCUR_set(TARG, SvCUR(TARG) * count);
}
*SvEND(TARG) = '\0';
}
if (isutf)
(void)SvPOK_only_UTF8(TARG);
else
(void)SvPOK_only(TARG);
PUSHTARG;
}
RETURN;
}
PP(pp_subtract)
{
bool useleft;
SV *targ = (PL_op->op_flags & OPf_STACKED)
? PL_stack_sp[-1]
: PAD_SV(PL_op->op_targ);
if (rpp_try_AMAGIC_2(subtr_amg, AMGf_assign|AMGf_numeric))
return NORMAL;
SV *svr = PL_stack_sp[0];
SV *svl = PL_stack_sp[-1];
#ifdef PERL_PRESERVE_IVUV
/* special-case some simple common cases */
if (!((svl->sv_flags|svr->sv_flags) & (SVf_IVisUV|SVs_GMG))) {
IV il, ir;
U32 flags = (svl->sv_flags & svr->sv_flags);
if (flags & SVf_IOK) {
/* both args are simple IVs */
UV topl, topr;
il = SvIVX(svl);
ir = SvIVX(svr);
do_iv:
topl = ((UV)il) >> (UVSIZE * 8 - 2);
topr = ((UV)ir) >> (UVSIZE * 8 - 2);
/* if both are in a range that can't under/overflow, do a
* simple integer subtract: if the top of both numbers
* are 00 or 11, then it's safe */
if (!( ((topl+1) | (topr+1)) & 2)) {
TARGi(il - ir, 0); /* args not GMG, so can't be tainted */
goto ret;
}
goto generic;
}
else if (flags & SVf_NOK) {
/* both args are NVs */
NV nl = SvNVX(svl);
NV nr = SvNVX(svr);
if (lossless_NV_to_IV(nl, &il) && lossless_NV_to_IV(nr, &ir)) {
/* nothing was lost by converting to IVs */
goto do_iv;
}
TARGn(nl - nr, 0); /* args not GMG, so can't be tainted */
goto ret;
}
}
generic:
useleft = USE_LEFT(svl);
/* See comments in pp_add (in pp_hot.c) about Overflow, and how
"bad things" happen if you rely on signed integers wrapping. */
if (SvIV_please_nomg(svr)) {
/* Unless the left argument is integer in range we are going to have to
use NV maths. Hence only attempt to coerce the right argument if
we know the left is integer. */
UV auv = 0;
bool auvok = FALSE;
bool a_valid = 0;
if (!useleft) {
auv = 0;
a_valid = auvok = 1;
/* left operand is undef, treat as zero. */
} else {
/* Left operand is defined, so is it IV? */
if (SvIV_please_nomg(svl)) {
if ((auvok = SvUOK(svl)))
auv = SvUVX(svl);
else {
const IV aiv = SvIVX(svl);
if (aiv >= 0) {
auv = aiv;
auvok = 1; /* Now acting as a sign flag. */
} else {
auv = NEGATE_2UV(aiv);
}
}
a_valid = 1;
}
}
if (a_valid) {
bool result_good = 0;
UV result;
UV buv;
bool buvok = SvUOK(svr);
if (buvok)
buv = SvUVX(svr);
else {
const IV biv = SvIVX(svr);
if (biv >= 0) {
buv = biv;
buvok = 1;
} else
buv = NEGATE_2UV(biv);
}
/* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
else "IV" now, independent of how it came in.
if a, b represents positive, A, B negative, a maps to -A etc
a - b => (a - b)
A - b => -(a + b)
a - B => (a + b)
A - B => -(a - b)
all UV maths. negate result if A negative.
subtract if signs same, add if signs differ. */
if (auvok ^ buvok) {
/* Signs differ. */
result = auv + buv;
if (result >= auv)
result_good = 1;
} else {
/* Signs same */
if (auv >= buv) {
result = auv - buv;
/* Must get smaller */
if (result <= auv)
result_good = 1;
} else {
result = buv - auv;
if (result <= buv) {
/* result really should be -(auv-buv). as its negation
of true value, need to swap our result flag */
auvok = !auvok;
result_good = 1;
}
}
}
if (result_good) {
if (auvok)
TARGu(result, 1);
else {
/* Negate result */
if (result <= ABS_IV_MIN)
TARGi(NEGATE_2IV(result), 1);
else {
/* result valid, but out of range for IV. */
TARGn(-(NV)result, 1);
}
}
goto ret;
} /* Overflow, drop through to NVs. */
}
}
#else
useleft = USE_LEFT(svl);
#endif
{
NV value = SvNV_nomg(svr);
if (!useleft) {
/* left operand is undef, treat as zero - value */
TARGn(-value, 1);
goto ret;
}
TARGn(SvNV_nomg(svl) - value, 1);
goto ret;
}
ret:
rpp_replace_2_1_NN(targ);
return NORMAL;
}
#define IV_BITS (IVSIZE * 8)
/* Taking the right operand of bitwise shift operators, returns an int
* indicating the shift amount clipped to the range [-IV_BITS, +IV_BITS].
*/
static int
S_shift_amount(pTHX_ SV *const svr)
{
const IV iv = SvIV_nomg(svr);
/* Note that [INT_MIN, INT_MAX] cannot be used as the clipping bound;
* INT_MIN will cause overflow in "shift = -shift;" in S_{iv,uv}_shift.
*/
if (SvIsUV(svr))
return SvUVX(svr) > IV_BITS ? IV_BITS : (int)SvUVX(svr);
return iv < -IV_BITS ? -IV_BITS : iv > IV_BITS ? IV_BITS : (int)iv;
}
static UV S_uv_shift(UV uv, int shift, bool left)
{
if (shift < 0) {
shift = -shift;
left = !left;
}
if (UNLIKELY(shift >= IV_BITS)) {
return 0;
}
return left ? uv << shift : uv >> shift;
}
static IV S_iv_shift(IV iv, int shift, bool left)
{
if (shift < 0) {
shift = -shift;
left = !left;
}
if (UNLIKELY(shift >= IV_BITS)) {
return iv < 0 && !left ? -1 : 0;
}
/* For left shifts, perl 5 has chosen to treat the value as unsigned for
* the purposes of shifting, then cast back to signed. This is very
* different from Raku:
*
* $ raku -e 'say -2 +< 5'
* -64
*
* $ ./perl -le 'print -2 << 5'
* 18446744073709551552
* */
if (left) {
return (IV) (((UV) iv) << shift);
}
/* Here is right shift */
return iv >> shift;
}
#define UV_LEFT_SHIFT(uv, shift) S_uv_shift(uv, shift, TRUE)
#define UV_RIGHT_SHIFT(uv, shift) S_uv_shift(uv, shift, FALSE)
#define IV_LEFT_SHIFT(iv, shift) S_iv_shift(iv, shift, TRUE)
#define IV_RIGHT_SHIFT(iv, shift) S_iv_shift(iv, shift, FALSE)
PP(pp_left_shift)
{
SV *targ = (PL_op->op_flags & OPf_STACKED)
? PL_stack_sp[-1]
: PAD_SV(PL_op->op_targ);
if (rpp_try_AMAGIC_2(lshift_amg, AMGf_assign|AMGf_numeric))
return NORMAL;
SV *svr = PL_stack_sp[0];
SV *svl = PL_stack_sp[-1];
{
const int shift = S_shift_amount(aTHX_ svr);
if (PL_op->op_private & OPpUSEINT) {
TARGi(IV_LEFT_SHIFT(SvIV_nomg(svl), shift), 1);
}
else {
TARGu(UV_LEFT_SHIFT(SvUV_nomg(svl), shift), 1);
}
rpp_replace_2_1_NN(targ);
return NORMAL;
}
}
PP(pp_right_shift)
{
SV *targ = (PL_op->op_flags & OPf_STACKED)
? PL_stack_sp[-1]
: PAD_SV(PL_op->op_targ);
if (rpp_try_AMAGIC_2(rshift_amg, AMGf_assign|AMGf_numeric))
return NORMAL;
SV *svr = PL_stack_sp[0];
SV *svl = PL_stack_sp[-1];
{
const int shift = S_shift_amount(aTHX_ svr);
if (PL_op->op_private & OPpUSEINT) {
TARGi(IV_RIGHT_SHIFT(SvIV_nomg(svl), shift), 1);
}
else {
TARGu(UV_RIGHT_SHIFT(SvUV_nomg(svl), shift), 1);
}
rpp_replace_2_1_NN(targ);
return NORMAL;
}
}
PP(pp_lt)
{
if (rpp_try_AMAGIC_2(lt_amg, AMGf_numeric))
return NORMAL;
SV *right = PL_stack_sp[0];
SV *left = PL_stack_sp[-1];
U32 flags_and = SvFLAGS(left) & SvFLAGS(right);
U32 flags_or = SvFLAGS(left) | SvFLAGS(right);
rpp_replace_2_IMM_NN(boolSV(
( (flags_and & SVf_IOK) && ((flags_or & SVf_IVisUV) ==0 ) )
? (SvIVX(left) < SvIVX(right))
: (flags_and & SVf_NOK)
? (SvNVX(left) < SvNVX(right))
: (do_ncmp(left, right) == -1)
));
return NORMAL;
}
PP(pp_gt)
{
if (rpp_try_AMAGIC_2(gt_amg, AMGf_numeric))
return NORMAL;
SV *right = PL_stack_sp[0];
SV *left = PL_stack_sp[-1];
U32 flags_and = SvFLAGS(left) & SvFLAGS(right);
U32 flags_or = SvFLAGS(left) | SvFLAGS(right);
rpp_replace_2_IMM_NN(boolSV(
( (flags_and & SVf_IOK) && ((flags_or & SVf_IVisUV) ==0 ) )
? (SvIVX(left) > SvIVX(right))
: (flags_and & SVf_NOK)
? (SvNVX(left) > SvNVX(right))
: (do_ncmp(left, right) == 1)
));
return NORMAL;
}
PP(pp_le)
{
if (rpp_try_AMAGIC_2(le_amg, AMGf_numeric))
return NORMAL;
SV *right = PL_stack_sp[0];
SV *left = PL_stack_sp[-1];
U32 flags_and = SvFLAGS(left) & SvFLAGS(right);
U32 flags_or = SvFLAGS(left) | SvFLAGS(right);
rpp_replace_2_IMM_NN(boolSV(
( (flags_and & SVf_IOK) && ((flags_or & SVf_IVisUV) ==0 ) )
? (SvIVX(left) <= SvIVX(right))
: (flags_and & SVf_NOK)
? (SvNVX(left) <= SvNVX(right))
: (do_ncmp(left, right) <= 0)
));
return NORMAL;
}
PP(pp_ge)
{
if (rpp_try_AMAGIC_2(ge_amg, AMGf_numeric))
return NORMAL;
SV *right = PL_stack_sp[0];
SV *left = PL_stack_sp[-1];
U32 flags_and = SvFLAGS(left) & SvFLAGS(right);
U32 flags_or = SvFLAGS(left) | SvFLAGS(right);
rpp_replace_2_IMM_NN(boolSV(
( (flags_and & SVf_IOK) && ((flags_or & SVf_IVisUV) ==0 ) )
? (SvIVX(left) >= SvIVX(right))
: (flags_and & SVf_NOK)
? (SvNVX(left) >= SvNVX(right))
: ( (do_ncmp(left, right) & 2) == 0)
));
return NORMAL;
}
PP(pp_ne)
{
if (rpp_try_AMAGIC_2(ne_amg, AMGf_numeric))
return NORMAL;
SV *right = PL_stack_sp[0];
SV *left = PL_stack_sp[-1];
U32 flags_and = SvFLAGS(left) & SvFLAGS(right);
U32 flags_or = SvFLAGS(left) | SvFLAGS(right);
rpp_replace_2_IMM_NN(boolSV(
( (flags_and & SVf_IOK) && ((flags_or & SVf_IVisUV) ==0 ) )
? (SvIVX(left) != SvIVX(right))
: (flags_and & SVf_NOK)
? (SvNVX(left) != SvNVX(right))
: (do_ncmp(left, right) != 0)
));
return NORMAL;
}
/* compare left and right SVs. Returns:
* -1: <
* 0: ==
* 1: >
* 2: left or right was a NaN
*/
I32
Perl_do_ncmp(pTHX_ SV* const left, SV * const right)
{
PERL_ARGS_ASSERT_DO_NCMP;
#ifdef PERL_PRESERVE_IVUV
/* Fortunately it seems NaN isn't IOK */
if (SvIV_please_nomg(right) && SvIV_please_nomg(left)) {
if (!SvUOK(left)) {
const IV leftiv = SvIVX(left);
if (!SvUOK(right)) {
/* ## IV <=> IV ## */
const IV rightiv = SvIVX(right);
return (leftiv > rightiv) - (leftiv < rightiv);
}
/* ## IV <=> UV ## */
if (leftiv < 0)
/* As (b) is a UV, it's >=0, so it must be < */
return -1;
{
const UV rightuv = SvUVX(right);
return ((UV)leftiv > rightuv) - ((UV)leftiv < rightuv);
}
}
if (SvUOK(right)) {
/* ## UV <=> UV ## */
const UV leftuv = SvUVX(left);
const UV rightuv = SvUVX(right);
return (leftuv > rightuv) - (leftuv < rightuv);
}
/* ## UV <=> IV ## */
{
const IV rightiv = SvIVX(right);
if (rightiv < 0)
/* As (a) is a UV, it's >=0, so it cannot be < */
return 1;
{
const UV leftuv = SvUVX(left);
return (leftuv > (UV)rightiv) - (leftuv < (UV)rightiv);
}
}
NOT_REACHED; /* NOTREACHED */
}
#endif
{
NV const rnv = SvNV_nomg(right);
NV const lnv = SvNV_nomg(left);
#if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
if (Perl_isnan(lnv) || Perl_isnan(rnv)) {
return 2;
}
return (lnv > rnv) - (lnv < rnv);
#else
if (lnv < rnv)
return -1;
if (lnv > rnv)
return 1;
if (lnv == rnv)
return 0;
return 2;
#endif
}
}
PP(pp_ncmp)
{
if (rpp_try_AMAGIC_2(ncmp_amg, AMGf_numeric))
return NORMAL;
SV *right = PL_stack_sp[0];
SV *left = PL_stack_sp[-1];
SV *targ;
I32 value = do_ncmp(left, right);
if (value == 2) {
targ = &PL_sv_undef;
}
else {
GETTARGET;
TARGi(value, 1);
}
rpp_replace_2_1_NN(targ);
return NORMAL;
}
/* also used for: pp_sge() pp_sgt() pp_slt() */
PP(pp_sle)
{
int amg_type = sle_amg;
int multiplier = 1;
int rhs = 1;
switch (PL_op->op_type) {
case OP_SLT:
amg_type = slt_amg;
/* cmp < 0 */
rhs = 0;
break;
case OP_SGT:
amg_type = sgt_amg;
/* cmp > 0 */
multiplier = -1;
rhs = 0;
break;
case OP_SGE:
amg_type = sge_amg;
/* cmp >= 0 */
multiplier = -1;
break;
}
if (rpp_try_AMAGIC_2(amg_type, 0))
return NORMAL;
SV *right = PL_stack_sp[0];
SV *left = PL_stack_sp[-1];
const int cmp =
#ifdef USE_LOCALE_COLLATE
(IN_LC_RUNTIME(LC_COLLATE))
? sv_cmp_locale_flags(left, right, 0)
:
#endif
sv_cmp_flags(left, right, 0);
rpp_replace_2_IMM_NN(boolSV(cmp * multiplier < rhs));
return NORMAL;
}
PP(pp_seq)
{
if (rpp_try_AMAGIC_2(seq_amg, 0))
return NORMAL;
SV *right = PL_stack_sp[0];
SV *left = PL_stack_sp[-1];
rpp_replace_2_IMM_NN(boolSV(sv_eq_flags(left, right, 0)));;
return NORMAL;
}
PP(pp_sne)
{
if (rpp_try_AMAGIC_2(sne_amg, 0))
return NORMAL;
SV *right = PL_stack_sp[0];
SV *left = PL_stack_sp[-1];
rpp_replace_2_IMM_NN(boolSV(!sv_eq_flags(left, right, 0)));
return NORMAL;
}
PP(pp_scmp)
{
dTARGET;
if (rpp_try_AMAGIC_2(scmp_amg, 0))
return NORMAL;
SV *right = PL_stack_sp[0];
SV *left = PL_stack_sp[-1];
const int cmp =
#ifdef USE_LOCALE_COLLATE
(IN_LC_RUNTIME(LC_COLLATE))
? sv_cmp_locale_flags(left, right, 0)
:
#endif
sv_cmp_flags(left, right, 0);
TARGi(cmp, 1);
rpp_replace_2_1_NN(targ);
return NORMAL;
}
PP(pp_bit_and)
{
SV *targ = (PL_op->op_flags & OPf_STACKED)
? PL_stack_sp[-1]
: PAD_SV(PL_op->op_targ);
if (rpp_try_AMAGIC_2(band_amg, AMGf_assign))
return NORMAL;
SV *right = PL_stack_sp[0];
SV *left = PL_stack_sp[-1];
{
if (SvNIOKp(left) || SvNIOKp(right)) {
const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
if (PL_op->op_private & OPpUSEINT) {
const IV i = SvIV_nomg(left) & SvIV_nomg(right);
TARGi(i, 1);
}
else {
const UV u = SvUV_nomg(left) & SvUV_nomg(right);
TARGu(u, 1);
}
if (left_ro_nonnum && left != TARG) SvNIOK_off(left);
if (right_ro_nonnum) SvNIOK_off(right);
}
else {
do_vop(PL_op->op_type, TARG, left, right);
SvSETMAGIC(targ);
}
}
rpp_replace_2_1_NN(targ);
return NORMAL;
}
PP(pp_nbit_and)
{
if (rpp_try_AMAGIC_2(band_amg, AMGf_assign|AMGf_numarg))
return NORMAL;
SV *targ = (PL_op->op_flags & OPf_STACKED)
? PL_stack_sp[-1]
: PAD_SV(PL_op->op_targ);
SV *right = PL_stack_sp[0];
SV *left = PL_stack_sp[-1];
{
if (PL_op->op_private & OPpUSEINT) {
const IV i = SvIV_nomg(left) & SvIV_nomg(right);
TARGi(i, 1);
}
else {
const UV u = SvUV_nomg(left) & SvUV_nomg(right);
TARGu(u, 1);
}
}
rpp_replace_2_1_NN(targ);
return NORMAL;
}
PP(pp_sbit_and)
{
if (rpp_try_AMAGIC_2(sband_amg, AMGf_assign))
return NORMAL;
SV *targ = (PL_op->op_flags & OPf_STACKED)
? PL_stack_sp[-1]
: PAD_SV(PL_op->op_targ);
SV *right = PL_stack_sp[0];
SV *left = PL_stack_sp[-1];
do_vop(OP_BIT_AND, targ, left, right);
SvSETMAGIC(targ);
rpp_replace_2_1_NN(targ);
return NORMAL;
}
/* also used for: pp_bit_xor() */
PP(pp_bit_or)
{
SV *targ = (PL_op->op_flags & OPf_STACKED)
? PL_stack_sp[-1]
: PAD_SV(PL_op->op_targ);
const int op_type = PL_op->op_type;
if (rpp_try_AMAGIC_2((op_type == OP_BIT_OR ? bor_amg : bxor_amg),
AMGf_assign))
return NORMAL;
SV *right = PL_stack_sp[0];
SV *left = PL_stack_sp[-1];
{
if (SvNIOKp(left) || SvNIOKp(right)) {
const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
if (PL_op->op_private & OPpUSEINT) {
const IV l = (USE_LEFT(left) ? SvIV_nomg(left) : 0);
const IV r = SvIV_nomg(right);
const IV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
TARGi(result, 1);
}
else {
const UV l = (USE_LEFT(left) ? SvUV_nomg(left) : 0);
const UV r = SvUV_nomg(right);
const UV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
TARGu(result, 1);
}
if (left_ro_nonnum && left != TARG) SvNIOK_off(left);
if (right_ro_nonnum) SvNIOK_off(right);
}
else {
do_vop(op_type, TARG, left, right);
SvSETMAGIC(targ);
}
rpp_replace_2_1_NN(targ);
return NORMAL;
}
}
/* also used for: pp_nbit_xor() */
PP(pp_nbit_or)
{
const int op_type = PL_op->op_type;
if (rpp_try_AMAGIC_2((op_type == OP_NBIT_OR ? bor_amg : bxor_amg),
AMGf_assign|AMGf_numarg))
return NORMAL;
SV *targ = (PL_op->op_flags & OPf_STACKED)
? PL_stack_sp[-1]
: PAD_SV(PL_op->op_targ);
SV *right = PL_stack_sp[0];
SV *left = PL_stack_sp[-1];
{
if (PL_op->op_private & OPpUSEINT) {
const IV l = (USE_LEFT(left) ? SvIV_nomg(left) : 0);
const IV r = SvIV_nomg(right);
const IV result = op_type == OP_NBIT_OR ? (l | r) : (l ^ r);
TARGi(result, 1);
}
else {
const UV l = (USE_LEFT(left) ? SvUV_nomg(left) : 0);
const UV r = SvUV_nomg(right);
const UV result = op_type == OP_NBIT_OR ? (l | r) : (l ^ r);
TARGu(result, 1);
}
}
rpp_replace_2_1_NN(targ);
return NORMAL;
}
/* also used for: pp_sbit_xor() */
PP(pp_sbit_or)
{
const int op_type = PL_op->op_type;
if (rpp_try_AMAGIC_2((op_type == OP_SBIT_OR ? sbor_amg : sbxor_amg),
AMGf_assign))
return NORMAL;
SV *targ = (PL_op->op_flags & OPf_STACKED)
? PL_stack_sp[-1]
: PAD_SV(PL_op->op_targ);
SV *right = PL_stack_sp[0];
SV *left = PL_stack_sp[-1];
do_vop(op_type == OP_SBIT_OR ? OP_BIT_OR : OP_BIT_XOR, targ,
left, right);
SvSETMAGIC(TARG);
rpp_replace_2_1_NN(targ);
return NORMAL;
}
PERL_STATIC_INLINE bool
S_negate_string(pTHX)
{
dTARGET;
STRLEN len;
const char *s;
SV * const sv = *PL_stack_sp;
assert(SvPOKp(sv));
if (SvNIOK(sv) || (!SvPOK(sv) && SvNIOKp(sv)))
return FALSE;
s = SvPV_nomg_const(sv, len);
if (isIDFIRST(*s)) {
if (LIKELY(TARG!=sv)) {
sv_setpvs(TARG, "-");
sv_catsv(TARG, sv);
} else {
sv_insert_flags(TARG, 0, 0, "-", 1, 0);
}
}
else if (*s == '+' || (*s == '-' && !looks_like_number(sv))) {
sv_setsv_nomg(TARG, sv);
*SvPV_force_nomg(TARG, len) = *s == '-' ? '+' : '-';
}
else return FALSE;
SvSETMAGIC(TARG);
if (LIKELY(targ != sv))
rpp_replace_1_1_NN(TARG);
return TRUE;
}
PP(pp_negate)
{
dTARGET;
if (rpp_try_AMAGIC_1(neg_amg, AMGf_numeric))
return NORMAL;
SV * const sv = *PL_stack_sp;
if (SvPOKp(sv) && S_negate_string(aTHX))
return NORMAL;
{
if (SvIOK(sv)) {
/* It's publicly an integer */
oops_its_an_int:
if (SvIsUV(sv)) {
if (SvUVX(sv) <= ABS_IV_MIN) {
TARGi(NEGATE_2IV(SvUVX(sv)), 1);
goto ret;
}
}
#ifdef PERL_PRESERVE_IVUV
else if (SvIVX(sv) < 0) {
TARGu(NEGATE_2UV(SvIVX(sv)), 1);
goto ret;
}
else {
TARGi(-SvIVX(sv), 1);
goto ret;
}
#else
else if (SvIVX(sv) != IV_MIN) {
TARGi(-SvIVX(sv), 1);
goto ret;
}
#endif
}
if (SvNIOKp(sv) && (SvNIOK(sv) || !SvPOK(sv)))
TARGn(-SvNV_nomg(sv), 1);
else if (SvPOKp(sv) && SvIV_please_nomg(sv))
goto oops_its_an_int;
else
TARGn(-SvNV_nomg(sv), 1);
}
ret:
if (LIKELY(targ != sv))
rpp_replace_1_1_NN(TARG);
return NORMAL;
}
PP(pp_not)
{
if (rpp_try_AMAGIC_1(not_amg, 0))
return NORMAL;
rpp_replace_1_IMM_NN(boolSV(!SvTRUE_nomg_NN(*PL_stack_sp)));
return NORMAL;
}
static void
S_scomplement(pTHX_ SV *targ, SV *sv)
{
U8 *tmps;
SSize_t anum;
STRLEN len;
sv_copypv_nomg(TARG, sv);
tmps = (U8*)SvPV_nomg(TARG, len);
if (SvUTF8(TARG)) {
if (len && ! utf8_to_bytes_overwrite(&tmps, &len)) {
croak(FATAL_ABOVE_FF_MSG, PL_op_desc[PL_op->op_type]);
}
SvCUR_set(TARG, len);
SvUTF8_off(TARG);
}
anum = len;
{
long *tmpl;
for ( ; anum && PTR2nat(tmps) % sizeof(long); anum--, tmps++)
*tmps = ~*tmps;
tmpl = (long*)tmps;
for ( ; anum >= (SSize_t)sizeof(long); anum -= (SSize_t)sizeof(long), tmpl++)
*tmpl = ~*tmpl;
tmps = (U8*)tmpl;
}
for ( ; anum > 0; anum--, tmps++)
*tmps = ~*tmps;
}
PP(pp_complement)
{
dTARGET;
if (rpp_try_AMAGIC_1(compl_amg, AMGf_numeric))
return NORMAL;
{
SV *sv = *PL_stack_sp;
if (SvNIOKp(sv)) {
if (PL_op->op_private & OPpUSEINT) {
const IV i = ~SvIV_nomg(sv);
TARGi(i, 1);
}
else {
const UV u = ~SvUV_nomg(sv);
TARGu(u, 1);
}
}
else {
S_scomplement(aTHX_ TARG, sv);
SvSETMAGIC(TARG);
}
rpp_replace_1_1_NN(TARG);
return NORMAL;
}
}
PP(pp_ncomplement)
{
if (rpp_try_AMAGIC_1(compl_amg, AMGf_numeric|AMGf_numarg))
return NORMAL;
dTARGET;
{
SV *sv = *PL_stack_sp;
if (PL_op->op_private & OPpUSEINT) {
const IV i = ~SvIV_nomg(sv);
TARGi(i, 1);
}
else {
const UV u = ~SvUV_nomg(sv);
TARGu(u, 1);
}
}
rpp_replace_1_1_NN(TARG);
return NORMAL;
}
PP(pp_scomplement)
{
if (rpp_try_AMAGIC_1(scompl_amg, AMGf_numeric))
return NORMAL;
dTARGET;
SV *sv = *PL_stack_sp;
S_scomplement(aTHX_ TARG, sv);
SvSETMAGIC(TARG);
rpp_replace_1_1_NN(TARG);
return NORMAL;
}
/* integer versions of some of the above */
PP(pp_i_multiply)
{
SV *targ = (PL_op->op_flags & OPf_STACKED)
? PL_stack_sp[-1]
: PAD_SV(PL_op->op_targ);
if (rpp_try_AMAGIC_2(mult_amg, AMGf_assign))
return NORMAL;
IV right = SvIV_nomg(PL_stack_sp[0]);
IV left = SvIV_nomg(PL_stack_sp[-1]);
TARGi((IV)((UV)left * (UV)right), 1);
rpp_replace_2_1_NN(targ);
return NORMAL;
}
PP(pp_i_divide)
{
SV *targ = (PL_op->op_flags & OPf_STACKED)
? PL_stack_sp[-1]
: PAD_SV(PL_op->op_targ);
if (rpp_try_AMAGIC_2(div_amg, AMGf_assign))
return NORMAL;
SV *right = PL_stack_sp[0];
SV *left = PL_stack_sp[-1];
{
IV value = SvIV_nomg(right);
if (value == 0)
DIE(aTHX_ "Illegal division by zero");
IV num = SvIV_nomg(left);
/* avoid FPE_INTOVF on some platforms when num is IV_MIN */
if (value == -1)
value = (IV)-(UV)num;
else
value = num / value;
TARGi(value, 1);
rpp_replace_2_1_NN(targ);
return NORMAL;
}
}
PP(pp_i_modulo)
{
SV *targ = (PL_op->op_flags & OPf_STACKED)
? PL_stack_sp[-1]
: PAD_SV(PL_op->op_targ);
if (rpp_try_AMAGIC_2(modulo_amg, AMGf_assign))
return NORMAL;
IV right = SvIV_nomg(PL_stack_sp[0]);
IV left = SvIV_nomg(PL_stack_sp[-1]);
{
if (!right)
DIE(aTHX_ "Illegal modulus zero");
/* avoid FPE_INTOVF on some platforms when left is IV_MIN */
if (right == -1)
TARGi(0, 1);
else
TARGi(left % right, 1);
}
rpp_replace_2_1_NN(targ);
return NORMAL;
}
PP(pp_i_add)
{
SV *targ = (PL_op->op_flags & OPf_STACKED)
? PL_stack_sp[-1]
: PAD_SV(PL_op->op_targ);
if (rpp_try_AMAGIC_2(add_amg, AMGf_assign))
return NORMAL;
IV right = SvIV_nomg(PL_stack_sp[0]);
SV *leftsv = PL_stack_sp[-1];
IV left = USE_LEFT(leftsv) ? SvIV_nomg(leftsv) : 0;
TARGi((IV)((UV)left + (UV)right), 1);
rpp_replace_2_1_NN(targ);
return NORMAL;
}
PP(pp_i_subtract)
{
SV *targ = (PL_op->op_flags & OPf_STACKED)
? PL_stack_sp[-1]
: PAD_SV(PL_op->op_targ);
if (rpp_try_AMAGIC_2(subtr_amg, AMGf_assign))
return NORMAL;
IV right = SvIV_nomg(PL_stack_sp[0]);
SV *leftsv = PL_stack_sp[-1];
IV left = USE_LEFT(leftsv) ? SvIV_nomg(leftsv) : 0;
TARGi((IV)((UV)left - (UV)right), 1);
rpp_replace_2_1_NN(targ);
return NORMAL;
}
PP(pp_i_lt)
{
if (rpp_try_AMAGIC_2(lt_amg, 0))
return NORMAL;
IV right = SvIV_nomg(PL_stack_sp[0]);
IV left = SvIV_nomg(PL_stack_sp[-1]);
rpp_replace_2_IMM_NN(boolSV(left < right));
return NORMAL;
}
PP(pp_i_gt)
{
if (rpp_try_AMAGIC_2(gt_amg, 0))
return NORMAL;
IV right = SvIV_nomg(PL_stack_sp[0]);
IV left = SvIV_nomg(PL_stack_sp[-1]);
rpp_replace_2_IMM_NN(boolSV(left > right));
return NORMAL;
}
PP(pp_i_le)
{
if (rpp_try_AMAGIC_2(le_amg, 0))
return NORMAL;
IV right = SvIV_nomg(PL_stack_sp[0]);
IV left = SvIV_nomg(PL_stack_sp[-1]);
rpp_replace_2_IMM_NN(boolSV(left <= right));
return NORMAL;
}
PP(pp_i_ge)
{
if (rpp_try_AMAGIC_2(ge_amg, 0))
return NORMAL;
IV right = SvIV_nomg(PL_stack_sp[0]);
IV left = SvIV_nomg(PL_stack_sp[-1]);
rpp_replace_2_IMM_NN(boolSV(left >= right));
return NORMAL;
}
PP(pp_i_eq)
{
if (rpp_try_AMAGIC_2(eq_amg, 0))
return NORMAL;
IV right = SvIV_nomg(PL_stack_sp[0]);
IV left = SvIV_nomg(PL_stack_sp[-1]);
rpp_replace_2_IMM_NN(boolSV(left == right));
return NORMAL;
}
PP(pp_i_ne)
{
if (rpp_try_AMAGIC_2(ne_amg, 0))
return NORMAL;
IV right = SvIV_nomg(PL_stack_sp[0]);
IV left = SvIV_nomg(PL_stack_sp[-1]);
rpp_replace_2_IMM_NN(boolSV(left != right));
return NORMAL;
}
PP(pp_i_ncmp)
{
dTARGET;
if (rpp_try_AMAGIC_2(ncmp_amg, 0))
return NORMAL;
IV right = SvIV_nomg(PL_stack_sp[0]);
IV left = SvIV_nomg(PL_stack_sp[-1]);
{
I32 value;
if (left > right)
value = 1;
else if (left < right)
value = -1;
else
value = 0;
TARGi(value, 1);
}
rpp_replace_2_1_NN(targ);
return NORMAL;
}
PP(pp_i_negate)
{
dTARGET;
if (rpp_try_AMAGIC_1(neg_amg, 0))
return NORMAL;
SV * const sv = *PL_stack_sp;
if (SvPOKp(sv) && S_negate_string(aTHX))
return NORMAL;
{
IV const i = SvIV_nomg(sv);
TARGi((IV)-(UV)i, 1);
if (LIKELY(targ != sv))
rpp_replace_1_1_NN(TARG);
return NORMAL;
}
}
/* High falutin' math. */
PP(pp_atan2)
{
dTARGET;
if (rpp_try_AMAGIC_2(atan2_amg, 0))
return NORMAL;
NV right = SvNV_nomg(PL_stack_sp[0]);
NV left = SvNV_nomg(PL_stack_sp[-1]);
TARGn(Perl_atan2(left, right), 1);
rpp_replace_2_1_NN(targ);
return NORMAL;
}
/* also used for: pp_cos() pp_exp() pp_log() pp_sqrt() */
PP(pp_sin)
{
dTARGET;
int amg_type = fallback_amg;
const char *neg_report = NULL;
const int op_type = PL_op->op_type;
switch (op_type) {
case OP_SIN: amg_type = sin_amg; break;
case OP_COS: amg_type = cos_amg; break;
case OP_EXP: amg_type = exp_amg; break;
case OP_LOG: amg_type = log_amg; neg_report = "log"; break;
case OP_SQRT: amg_type = sqrt_amg; neg_report = "sqrt"; break;
}
assert(amg_type != fallback_amg);
if (rpp_try_AMAGIC_1(amg_type, 0))
return NORMAL;
{
SV * const arg = *PL_stack_sp;
const NV value = SvNV_nomg(arg);
#ifdef NV_NAN
NV result = NV_NAN;
#else
NV result = 0.0;
#endif
if (neg_report) { /* log or sqrt */
if (
#if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
! Perl_isnan(value) &&
#endif
(op_type == OP_LOG ? (value <= 0.0) : (value < 0.0)))
{
char * mesg;
LC_NUMERIC_LOCK(0);
SET_NUMERIC_STANDARD();
mesg = form("Can't take %s of %" NVgf, neg_report, value);
LC_NUMERIC_UNLOCK;
/* diag_listed_as: Can't take log of %g */
DIE(aTHX_ "%s", mesg);
}
}
switch (op_type) {
default:
case OP_SIN: result = Perl_sin(value); break;
case OP_COS: result = Perl_cos(value); break;
case OP_EXP: result = Perl_exp(value); break;
case OP_LOG: result = Perl_log(value); break;
case OP_SQRT: result = Perl_sqrt(value); break;
}
TARGn(result, 1);
rpp_replace_1_1_NN(TARG);
return NORMAL;
}
}
/* Support Configure command-line overrides for rand() functions.
After 5.005, perhaps we should replace this by Configure support
for drand48(), random(), or rand(). For 5.005, though, maintain
compatibility by calling rand() but allow the user to override it.
See INSTALL for details. --Andy Dougherty 15 July 1998
*/
/* Now it's after 5.005, and Configure supports drand48() and random(),
in addition to rand(). So the overrides should not be needed any more.
--Jarkko Hietaniemi 27 September 1998
*/
PP_wrapped(pp_rand, MAXARG, 0)
{
if (!PL_srand_called) {
Rand_seed_t s;
if (PL_srand_override) {
/* env var PERL_RAND_SEED has been set so the user wants
* consistent srand() initialization. */
PERL_SRAND_OVERRIDE_GET(s);
} else {
/* Pseudo random initialization from context state and possible
* random devices */
s= (Rand_seed_t)seed();
}
(void)seedDrand01(s);
PL_srand_called = TRUE;
}
{
dSP;
NV value;
if (MAXARG < 1)
{
EXTEND(SP, 1);
value = 1.0;
}
else {
SV * const sv = POPs;
if(!sv)
value = 1.0;
else
value = SvNV(sv);
}
/* 1 of 2 things can be carried through SvNV, SP or TARG, SP was carried */
#if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
if (! Perl_isnan(value) && value == 0.0)
#else
if (value == 0.0)
#endif
value = 1.0;
{
dTARGET;
PUSHs(TARG);
PUTBACK;
value *= Drand01();
sv_setnv_mg(TARG, value);
}
}
return NORMAL;
}
PP_wrapped(pp_srand, MAXARG, 0)
{
dSP; dTARGET;
UV anum;
if (MAXARG >= 1 && (TOPs || POPs)) {
SV *top;
char *pv;
STRLEN len;
int flags;
top = POPs;
pv = SvPV(top, len);
flags = grok_number(pv, len, &anum);
if (!(flags & IS_NUMBER_IN_UV)) {
ck_warner_d(packWARN(WARN_OVERFLOW),
"Integer overflow in srand");
anum = UV_MAX;
}
}
else {
if (PL_srand_override) {
/* env var PERL_RAND_SEED has been set so the user wants
* consistent srand() initialization. */
PERL_SRAND_OVERRIDE_GET(anum);
} else {
anum = seed();
}
}
(void)seedDrand01((Rand_seed_t)anum);
PL_srand_called = TRUE;
if (anum)
XPUSHu(anum);
else {
/* Historically srand always returned true. We can avoid breaking
that like this: */
sv_setpvs(TARG, "0 but true");
XPUSHTARG;
}
RETURN;
}
PP(pp_int)
{
dTARGET;
if (rpp_try_AMAGIC_1(int_amg, AMGf_numeric))
return NORMAL;
{
SV * const sv = *PL_stack_sp;
const IV iv = SvIV_nomg(sv);
/* XXX it's arguable that compiler casting to IV might be subtly
different from modf (for numbers inside (IV_MIN,UV_MAX)) in which
else preferring IV has introduced a subtle behaviour change bug. OTOH
relying on floating point to be accurate is a bug. */
if (!SvOK(sv)) {
TARGu(0, 1);
}
else if (SvIOK(sv)) {
if (SvIsUV(sv))
TARGu(SvUVX(sv), 1);
else
TARGi(iv, 1);
}
else {
const NV value = SvNV_nomg(sv);
if (UNLIKELY(Perl_isinfnan(value)))
TARGn(value, 1);
else if (value >= 0.0) {
if (value < (NV)UV_MAX + 0.5) {
TARGu(U_V(value), 1);
} else {
TARGn(Perl_floor(value), 1);
}
}
else {
if (value > (NV)IV_MIN - 0.5) {
TARGi(I_V(value), 1);
} else {
TARGn(Perl_ceil(value), 1);
}
}
}
}
rpp_replace_1_1_NN(TARG);
return NORMAL;
}
PP(pp_abs)
{
dTARGET;
if (rpp_try_AMAGIC_1(abs_amg, AMGf_numeric))
return NORMAL;
{
SV * const sv = *PL_stack_sp;
/* This will cache the NV value if string isn't actually integer */
const IV iv = SvIV_nomg(sv);
UV uv;
if (!SvOK(sv)) {
uv = 0;
goto set_uv;
}
else if (SvIOK(sv)) {
/* IVX is precise */
if (SvIsUV(sv)) {
uv = SvUVX(sv); /* force it to be numeric only */
} else {
if (iv >= 0) {
uv = (UV)iv;
} else {
uv = NEGATE_2UV(iv);
}
}
set_uv:
TARGu(uv, 1);
} else{
const NV value = SvNV_nomg(sv);
TARGn(Perl_fabs(value), 1);
}
}
rpp_replace_1_1_NN(TARG);
return NORMAL;
}
/* also used for: pp_hex() */
PP(pp_oct)
{
dTARGET;
const char *tmps;
I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
STRLEN len;
NV result_nv;
UV result_uv;
SV* const sv = *PL_stack_sp;
tmps = (SvPV_const(sv, len));
if (DO_UTF8(sv)) {
/* If Unicode, try to downgrade
* If not possible, croak. */
SV* const tsv = sv_mortalcopy_flags(sv, SV_GMAGIC|SV_NOSTEAL);
SvUTF8_on(tsv);
(void)sv_utf8_downgrade(tsv, FALSE);
tmps = SvPV_const(tsv, len);
}
if (PL_op->op_type == OP_HEX)
goto hex;
while (*tmps && len && isSPACE(*tmps))
tmps++, len--;
if (*tmps == '0')
tmps++, len--;
if (isALPHA_FOLD_EQ(*tmps, 'x')) {
tmps++, len--;
flags |= PERL_SCAN_DISALLOW_PREFIX;
hex:
result_uv = grok_hex (tmps, &len, &flags, &result_nv);
}
else if (isALPHA_FOLD_EQ(*tmps, 'b')) {
tmps++, len--;
flags |= PERL_SCAN_DISALLOW_PREFIX;
result_uv = grok_bin (tmps, &len, &flags, &result_nv);
}
else {
if (isALPHA_FOLD_EQ(*tmps, 'o')) {
tmps++, len--;
}
result_uv = grok_oct (tmps, &len, &flags, &result_nv);
}
if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) {
TARGn(result_nv, 1);
}
else {
TARGu(result_uv, 1);
}
rpp_replace_1_1_NN(TARG);
return NORMAL;
}
/* String stuff. */
PP(pp_length)
{
dTARGET;
SV * const sv = *PL_stack_sp;
U32 in_bytes = IN_BYTES;
/* Simplest case shortcut:
* set svflags to just the SVf_POK|SVs_GMG|SVf_UTF8 from the SV,
* with the SVf_UTF8 flag inverted if under 'use bytes' (HINT_BYTES
* set)
*/
U32 svflags = (SvFLAGS(sv) ^ (in_bytes << 26)) & (SVf_POK|SVs_GMG|SVf_UTF8);
STATIC_ASSERT_STMT(SVf_UTF8 == (HINT_BYTES << 26));
if (LIKELY(svflags == SVf_POK))
goto simple_pv;
if (svflags & SVs_GMG)
mg_get(sv);
if (SvOK(sv)) {
STRLEN len;
if (!IN_BYTES) { /* reread to avoid using an C auto/register */
if ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8)) == SVf_POK)
goto simple_pv;
if ( SvPOK(sv) && (PL_op->op_private & OPpTRUEBOOL)) {
/* no need to convert from bytes to chars */
len = SvCUR(sv);
goto return_bool;
}
len = sv_len_utf8_nomg(sv);
}
else {
/* unrolled SvPV_nomg_const(sv,len) */
if (SvPOK_nog(sv)) {
simple_pv:
len = SvCUR(sv);
if (PL_op->op_private & OPpTRUEBOOL) {
return_bool:
rpp_replace_1_IMM_NN(len ? &PL_sv_yes : &PL_sv_zero);
return NORMAL;
}
}
else {
(void)sv_2pv_flags(sv, &len, 0|SV_CONST_RETURN);
}
}
TARGi((IV)(len), 1);
}
else {
if (!SvPADTMP(TARG)) {
/* OPpTARGET_MY: targ is var in '$lex = length()' */
sv_set_undef(TARG);
SvSETMAGIC(TARG);
}
else
targ = &PL_sv_undef;
}
rpp_replace_1_1_NN(TARG);
return NORMAL;
}
/* Returns false if substring is completely outside original string.
No length is indicated by len_iv = 0 and len_is_uv = 0. len_is_uv must
always be true for an explicit 0.
*/
bool
Perl_translate_substr_offsets( STRLEN curlen, IV pos1_iv,
bool pos1_is_uv, IV len_iv,
bool len_is_uv, STRLEN *posp,
STRLEN *lenp)
{
IV pos2_iv;
int pos2_is_uv;
PERL_ARGS_ASSERT_TRANSLATE_SUBSTR_OFFSETS;
if (!pos1_is_uv && pos1_iv < 0 && curlen) {
pos1_is_uv = curlen-1 > ~(UV)pos1_iv;
pos1_iv += curlen;
}
if ((pos1_is_uv || pos1_iv > 0) && (UV)pos1_iv > curlen)
return FALSE;
if (len_iv || len_is_uv) {
if (!len_is_uv && len_iv < 0) {
pos2_iv = curlen + len_iv;
if (curlen)
pos2_is_uv = curlen-1 > ~(UV)len_iv;
else
pos2_is_uv = 0;
} else { /* len_iv >= 0 */
if (!pos1_is_uv && pos1_iv < 0) {
pos2_iv = pos1_iv + len_iv;
pos2_is_uv = (UV)len_iv > (UV)IV_MAX;
} else {
if ((UV)len_iv > curlen-(UV)pos1_iv)
pos2_iv = curlen;
else
pos2_iv = pos1_iv+len_iv;
pos2_is_uv = 1;
}
}
}
else {
pos2_iv = curlen;
pos2_is_uv = 1;
}
if (!pos2_is_uv && pos2_iv < 0) {
if (!pos1_is_uv && pos1_iv < 0)
return FALSE;
pos2_iv = 0;
}
else if (!pos1_is_uv && pos1_iv < 0)
pos1_iv = 0;
if ((UV)pos2_iv < (UV)pos1_iv)
pos2_iv = pos1_iv;
if ((UV)pos2_iv > curlen)
pos2_iv = curlen;
/* pos1_iv and pos2_iv both in 0..curlen, so the cast is safe */
*posp = (STRLEN)( (UV)pos1_iv );
*lenp = (STRLEN)( (UV)pos2_iv - (UV)pos1_iv );
return TRUE;
}
PP_wrapped(pp_substr,
MAXARG3
+ ((PL_op->op_private & OPpSUBSTR_REPL_FIRST) ? 1 : 0),
0)
{
dSP; dTARGET;
SV *sv;
STRLEN curlen;
STRLEN utf8_curlen;
SV * pos_sv;
IV pos1_iv;
int pos1_is_uv;
SV * len_sv;
IV len_iv = 0;
int len_is_uv = 0;
I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
const bool rvalue = (GIMME_V != G_VOID);
const char *tmps;
SV *repl_sv = NULL;
const char *repl = NULL;
STRLEN repl_len;
int num_args = MAXARG3;
bool repl_need_utf8_upgrade = FALSE;
if (num_args > 2) {
if (num_args > 3) {
if(!(repl_sv = POPs)) num_args--;
}
if ((len_sv = POPs)) {
len_iv = SvIV(len_sv);
len_is_uv = len_iv == 0 || SvIOK_UV(len_sv);
}
else num_args--;
}
pos_sv = POPs;
pos1_iv = SvIV(pos_sv);
pos1_is_uv = SvIOK_UV(pos_sv);
sv = POPs;
if (PL_op->op_private & OPpSUBSTR_REPL_FIRST) {
assert(!repl_sv);
repl_sv = POPs;
}
if (lvalue && !repl_sv) {
SV * ret;
ret = newSV_type_mortal(SVt_PVLV); /* Not TARG RT#67838 */
sv_magic(ret, NULL, PERL_MAGIC_substr, NULL, 0);
LvTYPE(ret) = 'x';
LvTARG(ret) = SvREFCNT_inc_simple(sv);
LvTARGOFF(ret) =
pos1_is_uv || pos1_iv >= 0
? (STRLEN)(UV)pos1_iv
: (LvFLAGS(ret) |= LVf_NEG_OFF, (STRLEN)(UV)-pos1_iv);
LvTARGLEN(ret) =
len_is_uv || len_iv > 0
? (STRLEN)(UV)len_iv
: (LvFLAGS(ret) |= LVf_NEG_LEN, (STRLEN)(UV)-len_iv);
PUSHs(ret); /* avoid SvSETMAGIC here */
RETURN;
}
if (repl_sv) {
repl = SvPV_const(repl_sv, repl_len);
SvGETMAGIC(sv);
if (SvROK(sv))
ck_warner(packWARN(WARN_SUBSTR),
"Attempt to use reference as lvalue in substr");
tmps = SvPV_force_nomg(sv, curlen);
if (DO_UTF8(repl_sv) && repl_len) {
if (!DO_UTF8(sv)) {
/* Upgrade the dest, and recalculate tmps in case the buffer
* got reallocated; curlen may also have been changed */
sv_utf8_upgrade_nomg(sv);
tmps = SvPV_nomg(sv, curlen);
}
}
else if (DO_UTF8(sv))
repl_need_utf8_upgrade = TRUE;
}
else tmps = SvPV_const(sv, curlen);
if (DO_UTF8(sv)) {
utf8_curlen = sv_or_pv_len_utf8(sv, tmps, curlen);
if (utf8_curlen == curlen)
utf8_curlen = 0;
else
curlen = utf8_curlen;
}
else
utf8_curlen = 0;
{
STRLEN pos, len, byte_len, byte_pos;
if (!translate_substr_offsets(
curlen, pos1_iv, pos1_is_uv, len_iv, len_is_uv, &pos, &len
)) goto bound_fail;
byte_len = len;
byte_pos = utf8_curlen
? sv_or_pv_pos_u2b(sv, tmps, pos, &byte_len) : pos;
tmps += byte_pos;
if (rvalue) {
SvTAINTED_off(TARG); /* decontaminate */
SvUTF8_off(TARG); /* decontaminate */
sv_setpvn(TARG, tmps, byte_len);
#ifdef USE_LOCALE_COLLATE
sv_unmagic(TARG, PERL_MAGIC_collxfrm);
#endif
if (utf8_curlen)
SvUTF8_on(TARG);
}
if (repl) {
void * free_me = NULL;
if (repl_need_utf8_upgrade) {
repl = (char*)bytes_to_utf8_free_me(
(U8*)repl, &repl_len, &free_me);
}
/* The earlier SvPV_force_nomg(sv, curlen) should have ensured
* that sv is SvOK, even if it wasn't beforehand. */
assert(SvOK(sv));
sv_insert_flags(sv, byte_pos, byte_len, repl, repl_len, 0);
Safefree(free_me);
}
}
if (PL_op->op_private & OPpSUBSTR_REPL_FIRST)
SP++;
else if (rvalue) {
SvSETMAGIC(TARG);
PUSHs(TARG);
}
RETURN;
bound_fail:
if (repl)
croak("substr outside of string");
ck_warner(packWARN(WARN_SUBSTR), "substr outside of string");
RETPUSHUNDEF;
}
/* OP_SUBSTR_LEFT is a specialized version of OP_SUBSTR, where:
* the EXPR is a PADSV
* the OFFSET is a CONST zero
* the replacement pattern is a CONST ""
* it's definitely not in lvalue context (see the check in pp_substr)
* it definitely doesn't have OPpSUBSTR_REPL_FIRST set
* it may be an rvalue or in void context (may support TARGMY later)
*/
PP(pp_substr_left)
{
dTARGET;
STRLEN curlen;
STRLEN utf8_curlen = 0;
STRLEN byte_len = 0;
SV *sv = PL_stack_sp[-1];
const bool rvalue = (GIMME_V != G_VOID) || (PL_op->op_private & OPpTARGET_MY);
const bool do_chop = (MAXARG3 == 4);
const char *tmps;
if (do_chop) {
SvGETMAGIC(sv);
if (SvROK(sv))
ck_warner(packWARN(WARN_SUBSTR),
"Attempt to use reference as lvalue in substr");
tmps = SvPV_force_nomg(sv, curlen);
} else
tmps = SvPV_const(sv, curlen);
if (DO_UTF8(sv)) {
utf8_curlen = sv_or_pv_len_utf8(sv, tmps, curlen);
if (utf8_curlen == curlen)
utf8_curlen = 0;
else
curlen = utf8_curlen;
}
/* Inlined, simplified Perl_translate_substr_offsets */
if (curlen) {
const IV len_iv = SvIV(PL_stack_sp[0]);
const int len_is_uv = len_iv == 0 || SvIOK_UV(PL_stack_sp[0]);
if (!len_is_uv && len_iv < 0) { /* Negative length supplied */
const IV pos2_iv = curlen + len_iv;
if (!(curlen-1 > ~(UV)len_iv) && pos2_iv < 0) {
byte_len = 0;
} else if ((UV)pos2_iv > curlen) {
byte_len = (STRLEN)( (UV)curlen);
} else {
byte_len = (STRLEN)( (UV)pos2_iv );
}
} else if ((UV)len_iv <= curlen) { /* Non-negative length supplied */
byte_len = (STRLEN)( (UV)len_iv);
} else {
byte_len = curlen;
}
}
/* End of inlined, simplified Perl_translate_substr_offsets */
if (utf8_curlen) {
/* This could update byte_len, but the return value
will always be zero, which subsequent code has
assumed to be the case. */
sv_or_pv_pos_u2b(sv, tmps, 0, &byte_len);
}
if (rvalue) {
SvTAINTED_off(TARG); /* decontaminate */
SvUTF8_off(TARG); /* decontaminate */
sv_setpvn(TARG, tmps, byte_len);
#ifdef USE_LOCALE_COLLATE
sv_unmagic(TARG, PERL_MAGIC_collxfrm);
#endif
if (utf8_curlen)
SvUTF8_on(TARG);
}
if (do_chop) {
SvTAINT(sv);
sv_chop(sv, SvPVX(sv) + byte_len);
SvSETMAGIC(sv);
}
if (rvalue) {
SvSETMAGIC(TARG);
rpp_replace_2_1(TARG);
} else {
rpp_popfree_2();
}
return NORMAL;
}
PP_wrapped(pp_vec, 3, 0)
{
dSP;
const IV size = POPi;
SV* offsetsv = POPs;
SV * const src = POPs;
const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
SV * ret;
UV retuv;
STRLEN offset = 0;
char errflags = 0;
/* extract a STRLEN-ranged integer value from offsetsv into offset,
* or flag that its out of range */
{
IV iv = SvIV(offsetsv);
/* avoid a large UV being wrapped to a negative value */
if (SvIOK_UV(offsetsv) && SvUVX(offsetsv) > (UV)IV_MAX)
errflags = LVf_OUT_OF_RANGE;
else if (iv < 0)
errflags = (LVf_NEG_OFF|LVf_OUT_OF_RANGE);
#if PTRSIZE < IVSIZE
else if (iv > Size_t_MAX)
errflags = LVf_OUT_OF_RANGE;
#endif
else
offset = (STRLEN)iv;
}
retuv = errflags ? 0 : do_vecget(src, offset, size);
if (lvalue) { /* it's an lvalue! */
ret = newSV_type_mortal(SVt_PVLV); /* Not TARG RT#67838 */
sv_magic(ret, NULL, PERL_MAGIC_vec, NULL, 0);
LvTYPE(ret) = 'v';
LvTARG(ret) = SvREFCNT_inc_simple(src);
LvTARGOFF(ret) = offset;
LvTARGLEN(ret) = size;
LvFLAGS(ret) = errflags;
}
else {
dTARGET;
SvTAINTED_off(TARG); /* decontaminate */
ret = TARG;
}
sv_setuv(ret, retuv);
if (!lvalue)
SvSETMAGIC(ret);
PUSHs(ret);
RETURN;
}
/* also used for: pp_rindex() */
PP(pp_index)
{
SV *targ = (PL_op->op_flags & OPf_STACKED)
? PL_stack_sp[-1]
: PAD_SV(PL_op->op_targ);
SV *big;
SV *little;
SV *temp = NULL;
STRLEN biglen;
STRLEN llen = 0;
SSize_t offset = 0;
SSize_t retval;
const char *big_p;
const char *little_p;
bool big_utf8;
bool little_utf8;
const bool is_index = PL_op->op_type == OP_INDEX;
assert(MAXARG == 2 || MAXARG == 3);
bool threeargs = (MAXARG == 3);
if (MAXARG == 3 && !PL_stack_sp[0]) {
/* pp_coreargs pushes a NULL in order to flag that &CORE::index()
* was called with two args */
PL_stack_sp--;
threeargs = FALSE;
}
if (threeargs) {
offset = SvIV(*PL_stack_sp);
rpp_popfree_1_NN();
}
little = PL_stack_sp[0];
big = PL_stack_sp[-1];
big_p = SvPV_const(big, biglen);
little_p = SvPV_const(little, llen);
big_utf8 = DO_UTF8(big);
little_utf8 = DO_UTF8(little);
if (big_utf8 ^ little_utf8) {
/* One needs to be upgraded. */
if (little_utf8) {
/* Well, maybe instead we might be able to downgrade the small
string? */
void * free_little_p = NULL;
if (utf8_to_bytes_new_pv((const U8 **) &little_p, &llen,
&free_little_p))
{
little_utf8 = false;
/* Here 'little_p' is in byte form, and 'free_little_p' is
* non-NULL if the original wasn't, and 'little_p' is pointing
* to new memory. We create a new SV for use by the rest of
* the routine that contains the new byte string, and donate it
* to temp to ensure it will get free()d */
if (free_little_p) {
little = temp = newSV_type(SVt_NULL);
sv_usepvn(temp, (char *) little_p, llen);
little_p = SvPVX_const(little);
}
}
else {
/* When the large string is ISO-8859-1, and it's not possible to
convert the small string to ISO-8859-1, then there is no
way that it could be found anywhere by index. */
retval = -1;
goto push_result;
}
} else {
temp = newSVpvn(little_p, llen);
sv_utf8_upgrade(temp);
little = temp;
little_p = SvPV_const(little, llen);
}
}
if (SvGAMAGIC(big)) {
/* Life just becomes a lot easier if I use a temporary here.
Otherwise I need to avoid calls to sv_pos_u2b(), which (dangerously)
will trigger magic and overloading again, as will fbm_instr()
*/
big = newSVpvn_flags(big_p, biglen,
SVs_TEMP | (big_utf8 ? SVf_UTF8 : 0));
big_p = SvPVX(big);
}
if (SvGAMAGIC(little) || (is_index && !SvOK(little))) {
/* index && SvOK() is a hack. fbm_instr() calls SvPV_const, which will
warn on undef, and we've already triggered a warning with the
SvPV_const some lines above. We can't remove that, as we need to
call some SvPV to trigger overloading early and find out if the
string is UTF-8.
This is all getting too messy. The API isn't quite clean enough,
because data access has side effects.
*/
little = newSVpvn_flags(little_p, llen,
SVs_TEMP | (little_utf8 ? SVf_UTF8 : 0));
little_p = SvPVX(little);
}
if (!threeargs)
offset = is_index ? 0 : biglen;
else {
if (big_utf8 && offset > 0)
offset = sv_pos_u2b_flags(big, offset, 0, SV_CONST_RETURN);
if (!is_index)
offset += llen;
}
if (offset < 0)
offset = 0;
else if (offset > (SSize_t)biglen)
offset = biglen;
if (!(little_p = is_index
? fbm_instr((unsigned char*)big_p + offset,
(unsigned char*)big_p + biglen, little, 0)
: rninstr(big_p, big_p + offset,
little_p, little_p + llen)))
retval = -1;
else {
retval = little_p - big_p;
if (retval > 1 && big_utf8)
retval = sv_pos_b2u_flags(big, retval, SV_CONST_RETURN);
}
SvREFCNT_dec(temp);
push_result:
/* OPpTRUEBOOL indicates an '== -1' has been optimised away */
if (PL_op->op_private & OPpTRUEBOOL) {
SV *result = ((retval != -1) ^ cBOOL(PL_op->op_private & OPpINDEX_BOOLNEG))
? &PL_sv_yes : &PL_sv_no;
if (PL_op->op_private & OPpTARGET_MY)
/* $lex = (index() == -1) */
sv_setsv_mg(targ, result);
else
targ = result;
}
else
TARGi(retval, 1);
rpp_replace_2_1_NN(targ);
return NORMAL;
}
PP(pp_sprintf)
{
dMARK; dORIGMARK; dTARGET;
SvTAINTED_off(TARG);
do_sprintf(TARG, PL_stack_sp - MARK, MARK + 1);
TAINT_IF(SvTAINTED(TARG));
rpp_popfree_to_NN(ORIGMARK);
SvSETMAGIC(TARG);
rpp_push_1(TARG);
return NORMAL;
}
PP(pp_ord)
{
dTARGET;
SV *argsv = *PL_stack_sp;
STRLEN len;
const U8 *s = (U8*)SvPV_const(argsv, len);
UV cp;
if (UNLIKELY(len == 0)) {
cp = 0;
}
else if (DO_UTF8(argsv)) {
if (! utf8_to_uv(s, s + len, &cp, 0)) {
cp = 0;
}
}
else {
cp = (UV) (*s);
}
TARGu(cp, 1);
rpp_replace_1_1_NN(TARG);
return NORMAL;
}
PP(pp_chr)
{
dTARGET;
char *tmps;
UV value;
SV *top = *PL_stack_sp;
SvGETMAGIC(top);
if (UNLIKELY(SvAMAGIC(top)))
top = sv_2num(top);
if (UNLIKELY(isinfnansv(top)))
croak("Cannot chr %" NVgf, SvNV(top));
else {
if (!IN_BYTES /* under bytes, chr(-1) eq chr(0xff), etc. */
&& ((SvIOKp(top) && !SvIsUV(top) && SvIV_nomg(top) < 0)
||
((SvNOKp(top) || (SvOK(top) && !SvIsUV(top)))
&& SvNV_nomg(top) < 0.0)))
{
if (ckWARN(WARN_UTF8)) {
if (SvGMAGICAL(top)) {
SV *top2 = sv_mortalcopy_flags(top, SV_DO_COW_SVSETSV);
top = top2;
}
warner(packWARN(WARN_UTF8),
"Invalid negative number (%" SVf ") in chr", SVfARG(top));
}
value = UNICODE_REPLACEMENT;
} else {
value = SvUV_nomg(top);
}
}
SvUPGRADE(TARG,SVt_PV);
if (value > 255 && !IN_BYTES) {
SvGROW(TARG, (STRLEN)UVCHR_SKIP(value)+1);
tmps = (char*)uv_to_utf8((U8*)SvPVX(TARG), value);
SvCUR_set(TARG, tmps - SvPVX_const(TARG));
*tmps = '\0';
(void)SvPOK_only(TARG);
SvUTF8_on(TARG);
goto ret;
}
SvGROW(TARG,2);
SvCUR_set(TARG, 1);
tmps = SvPVX(TARG);
*tmps++ = (char)value;
*tmps = '\0';
(void)SvPOK_only(TARG);
ret:
SvSETMAGIC(TARG);
rpp_replace_1_1_NN(TARG);
return NORMAL;
}
PP(pp_crypt)
{
#ifdef HAS_CRYPT
dTARGET;
SV *right = PL_stack_sp[0];
SV *left = PL_stack_sp[-1];
STRLEN len;
const char *tmps = SvPV_const(left, len);
if (DO_UTF8(left)) {
/* If Unicode, try to downgrade.
* If not possible, croak.
* Yes, we made this up. */
SV* const tsv = newSVpvn_flags(tmps, len, SVf_UTF8|SVs_TEMP);
(void)sv_utf8_downgrade(tsv, FALSE);
tmps = SvPV_const(tsv, len);
}
# ifdef USE_ITHREADS
# ifdef HAS_CRYPT_R
if (!PL_reentrant_buffer->_crypt_struct_buffer) {
/* This should be threadsafe because in ithreads there is only
* one thread per interpreter. If this would not be true,
* we would need a mutex to protect this malloc. */
PL_reentrant_buffer->_crypt_struct_buffer =
(struct crypt_data *)safemalloc(sizeof(struct crypt_data));
# if defined(__GLIBC__) || defined(__EMX__)
if (PL_reentrant_buffer->_crypt_struct_buffer) {
PL_reentrant_buffer->_crypt_struct_buffer->initialized = 0;
}
# endif
}
# endif /* HAS_CRYPT_R */
# endif /* USE_ITHREADS */
sv_setpv(TARG, PerlProc_crypt(tmps, SvPV_nolen_const(right)));
SvUTF8_off(TARG);
SvSETMAGIC(TARG);
rpp_replace_2_1_NN(targ);
return NORMAL;
#else
DIE(aTHX_
"The crypt() function is unimplemented due to excessive paranoia.");
#endif
}
/* Generally UTF-8 and UTF-EBCDIC are indistinguishable at this level. So
* most comments below say UTF-8, when in fact they mean UTF-EBCDIC as well */
/* also used for: pp_lcfirst() */
PP_wrapped(pp_ucfirst, 1, 0)
{
/* Actually is both lcfirst() and ucfirst(). Only the first character
* changes. This means that possibly we can change in-place, ie., just
* take the source and change that one character and store it back, but not
* if read-only etc, or if the length changes */
dSP;
SV *source = TOPs;
STRLEN slen; /* slen is the byte length of the whole SV. */
STRLEN need;
SV *dest;
bool inplace; /* ? Convert first char only, in-place */
bool doing_utf8 = FALSE; /* ? using utf8 */
bool convert_source_to_utf8 = FALSE; /* ? need to convert */
const int op_type = PL_op->op_type;
const U8 *s;
U8 *d;
U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
STRLEN ulen; /* ulen is the byte length of the original Unicode character
* stored as UTF-8 at s. */
STRLEN tculen; /* tculen is the byte length of the freshly titlecased (or
* lowercased) character stored in tmpbuf. May be either
* UTF-8 or not, but in either case is the number of bytes */
bool remove_dot_above = FALSE;
s = (const U8*)SvPV_const(source, slen);
/* We may be able to get away with changing only the first character, in
* place, but not if read-only, etc. Later we may discover more reasons to
* not convert in-place. */
inplace = !SvREADONLY(source) && SvPADTMP(source);
#ifdef USE_LOCALE_CTYPE
if (IN_LC_RUNTIME(LC_CTYPE)) {
CHECK_AND_WARN_PROBLEMATIC_LOCALE_;
}
#endif
/* First calculate what the changed first character should be. This affects
* whether we can just swap it out, leaving the rest of the string unchanged,
* or even if have to convert the dest to UTF-8 when the source isn't */
if (! slen) { /* If empty */
need = 1; /* still need a trailing NUL */
ulen = 0;
*tmpbuf = '\0';
}
else if (DO_UTF8(source)) { /* Is the source utf8? */
doing_utf8 = TRUE;
ulen = UTF8SKIP(s);
if (op_type == OP_UCFIRST) {
#ifdef USE_LOCALE_CTYPE
_toTITLE_utf8_flags(s, s +slen, tmpbuf, &tculen, IN_LC_RUNTIME(LC_CTYPE));
#else
_toTITLE_utf8_flags(s, s +slen, tmpbuf, &tculen, 0);
#endif
}
else {
#ifdef USE_LOCALE_CTYPE
_toLOWER_utf8_flags(s, s + slen, tmpbuf, &tculen, IN_LC_RUNTIME(LC_CTYPE));
/* In turkic locales, lower casing an 'I' normally yields U+0131,
* LATIN SMALL LETTER DOTLESS I, but not if the grapheme also
* contains a COMBINING DOT ABOVE. Instead it is treated like
* LATIN CAPITAL LETTER I WITH DOT ABOVE lowercased to 'i'. The
* call to lowercase above has handled this. But SpecialCasing.txt
* says we are supposed to remove the COMBINING DOT ABOVE. We can
* tell if we have this situation if I ==> i in a turkic locale. */
if ( UNLIKELY(IN_UTF8_TURKIC_LOCALE)
&& IN_LC_RUNTIME(LC_CTYPE)
&& (UNLIKELY(*s == 'I' && tmpbuf[0] == 'i')))
{
/* Here, we know there was a COMBINING DOT ABOVE. We won't be
* able to handle this in-place. */
inplace = FALSE;
/* It seems likely that the DOT will immediately follow the
* 'I'. If so, we can remove it simply by indicating to the
* code below to start copying the source just beyond the DOT.
* We know its length is 2 */
if (LIKELY(memBEGINs(s + 1, s + slen, COMBINING_DOT_ABOVE_UTF8))) {
ulen += 2;
}
else { /* But if it doesn't follow immediately, set a flag for
the code below */
remove_dot_above = TRUE;
}
}
#else
PERL_UNUSED_VAR(remove_dot_above);
_toLOWER_utf8_flags(s, s + slen, tmpbuf, &tculen, 0);
#endif
}
/* we can't do in-place if the length changes. */
if (ulen != tculen) inplace = FALSE;
need = slen + 1 - ulen + tculen;
}
else { /* Non-zero length, non-UTF-8, Need to consider locale and if
* latin1 is treated as caseless. Note that a locale takes
* precedence */
ulen = 1; /* Original character is 1 byte */
tculen = 1; /* Most characters will require one byte, but this will
* need to be overridden for the tricky ones */
need = slen + 1;
#ifdef USE_LOCALE_CTYPE
if (IN_LC_RUNTIME(LC_CTYPE)) {
if ( UNLIKELY(IN_UTF8_TURKIC_LOCALE)
&& ( (op_type == OP_LCFIRST && UNLIKELY(*s == 'I'))
|| (op_type == OP_UCFIRST && UNLIKELY(*s == 'i'))))
{
if (*s == 'I') { /* lcfirst('I') */
tmpbuf[0] = UTF8_TWO_BYTE_HI(LATIN_SMALL_LETTER_DOTLESS_I);
tmpbuf[1] = UTF8_TWO_BYTE_LO(LATIN_SMALL_LETTER_DOTLESS_I);
}
else { /* ucfirst('i') */
tmpbuf[0] = UTF8_TWO_BYTE_HI(LATIN_CAPITAL_LETTER_I_WITH_DOT_ABOVE);
tmpbuf[1] = UTF8_TWO_BYTE_LO(LATIN_CAPITAL_LETTER_I_WITH_DOT_ABOVE);
}
tculen = 2;
inplace = FALSE;
doing_utf8 = TRUE;
convert_source_to_utf8 = TRUE;
need += variant_under_utf8_count(s, s + slen);
}
else if (op_type == OP_LCFIRST) {
/* For lc, there are no gotchas for UTF-8 locales (other than
* the turkish ones already handled above) */
*tmpbuf = toLOWER_LC(*s);
}
else { /* ucfirst */
/* But for uc, some characters require special handling */
if (IN_UTF8_CTYPE_LOCALE) {
goto do_uni_rules;
}
/* This would be a bug if any locales have upper and title case
* different */
*tmpbuf = (U8) toUPPER_LC(*s);
}
}
else
#endif
/* Here, not in locale. If not using Unicode rules, is a simple
* lower/upper, depending */
if (! IN_UNI_8_BIT) {
*tmpbuf = (op_type == OP_LCFIRST)
? toLOWER(*s)
: toUPPER(*s);
}
else if (op_type == OP_LCFIRST) {
/* lower case the first letter: no trickiness for any character */
*tmpbuf = toLOWER_LATIN1(*s);
}
else {
/* Here, is ucfirst non-UTF-8, not in locale (unless that locale is
* non-turkic UTF-8, which we treat as not in locale), and cased
* latin1 */
UV title_ord;
#ifdef USE_LOCALE_CTYPE
do_uni_rules:
#endif
title_ord = _to_upper_title_latin1(*s, tmpbuf, &tculen, 's');
if (tculen > 1) {
assert(tculen == 2);
/* If the result is an upper Latin1-range character, it can
* still be represented in one byte, which is its ordinal */
if (UTF8_IS_DOWNGRADEABLE_START(*tmpbuf)) {
*tmpbuf = (U8) title_ord;
tculen = 1;
}
else {
/* Otherwise it became more than one ASCII character (in
* the case of LATIN_SMALL_LETTER_SHARP_S) or changed to
* beyond Latin1, so the number of bytes changed, so can't
* replace just the first character in place. */
inplace = FALSE;
/* If the result won't fit in a byte, the entire result
* will have to be in UTF-8. Allocate enough space for the
* expanded first byte, and if UTF-8, the rest of the input
* string, some or all of which may also expand to two
* bytes, plus the terminating NUL. */
if (title_ord > 255) {
doing_utf8 = TRUE;
convert_source_to_utf8 = TRUE;
need = slen
+ variant_under_utf8_count(s, s + slen)
+ 1;
/* The (converted) UTF-8 and UTF-EBCDIC lengths of all
* characters whose title case is above 255 is
* 2. */
ulen = 2;
}
else { /* LATIN_SMALL_LETTER_SHARP_S expands by 1 byte */
need = slen + 1 + 1;
}
}
}
} /* End of use Unicode (Latin1) semantics */
} /* End of changing the case of the first character */
/* Here, have the first character's changed case stored in tmpbuf. Ready to
* generate the result */
if (inplace) {
/* We can convert in place. This means we change just the first
* character without disturbing the rest; no need to grow */
dest = source;
s = d = (U8*)SvPV_force_nomg(source, slen);
} else {
dTARGET;
dest = TARG;
/* Here, we can't convert in place; we earlier calculated how much
* space we will need, so grow to accommodate that */
SvUPGRADE(dest, SVt_PV);
d = (U8*)SvGROW(dest, need);
(void)SvPOK_only(dest);
SETs(dest);
}
if (doing_utf8) {
if (! inplace) {
if (! convert_source_to_utf8) {
/* Here both source and dest are in UTF-8, but have to create
* the entire output. We initialize the result to be the
* title/lower cased first character, and then append the rest
* of the string. */
sv_setpvn(dest, (char*)tmpbuf, tculen);
if (slen > ulen) {
/* But this boolean being set means we are in a turkic
* locale, and there is a DOT character that needs to be
* removed, and it isn't immediately after the current
* character. Keep concatenating characters to the output
* one at a time, until we find the DOT, which we simply
* skip */
if (UNLIKELY(remove_dot_above)) {
do {
Size_t this_len = UTF8SKIP(s + ulen);
sv_catpvn(dest, (char*)(s + ulen), this_len);
ulen += this_len;
if (memBEGINs(s + ulen, s + slen, COMBINING_DOT_ABOVE_UTF8)) {
ulen += 2;
break;
}
} while (s + ulen < s + slen);
}
/* The rest of the string can be concatenated unchanged,
* all at once */
sv_catpvn(dest, (char*)(s + ulen), slen - ulen);
}
}
else {
const U8 *const send = s + slen;
/* Here the dest needs to be in UTF-8, but the source isn't,
* except we earlier UTF-8'd the first character of the source
* into tmpbuf. First put that into dest, and then append the
* rest of the source, converting it to UTF-8 as we go. */
/* Assert tculen is 2 here because the only characters that
* get to this part of the code have 2-byte UTF-8 equivalents */
assert(tculen == 2);
*d++ = *tmpbuf;
*d++ = *(tmpbuf + 1);
s++; /* We have just processed the 1st char */
while (s < send) {
append_utf8_from_native_byte(*s, &d);
s++;
}
*d = '\0';
SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
}
SvUTF8_on(dest);
}
else { /* in-place UTF-8. Just overwrite the first character */
Copy(tmpbuf, d, tculen, U8);
SvCUR_set(dest, need - 1);
}
}
else { /* Neither source nor dest are, nor need to be UTF-8 */
if (slen) {
if (inplace) { /* in-place, only need to change the 1st char */
*d = *tmpbuf;
}
else { /* Not in-place */
/* Copy the case-changed character(s) from tmpbuf */
Copy(tmpbuf, d, tculen, U8);
d += tculen - 1; /* Code below expects d to point to final
* character stored */
}
}
else { /* empty source */
/* See bug #39028: Don't taint if empty */
*d = *s;
}
/* In a "use bytes" we don't treat the source as UTF-8, but, still want
* the destination to retain that flag */
if (DO_UTF8(source))
SvUTF8_on(dest);
if (!inplace) { /* Finish the rest of the string, unchanged */
/* This will copy the trailing NUL */
Copy(s + 1, d + 1, slen, U8);
SvCUR_set(dest, need - 1);
}
}
#ifdef USE_LOCALE_CTYPE
if (IN_LC_RUNTIME(LC_CTYPE)) {
TAINT;
SvTAINTED_on(dest);
}
#endif
if (dest != source && SvTAINTED(source))
SvTAINT(dest);
SvSETMAGIC(dest);
return NORMAL;
}
PP_wrapped(pp_uc, 1, 0)
{
dSP;
SV *source = TOPs;
STRLEN len;
STRLEN min;
SV *dest;
const U8 *s;
U8 *d;
SvGETMAGIC(source);
if ( SvPADTMP(source)
&& !SvREADONLY(source) && SvPOK(source)
&& !DO_UTF8(source)
&& (
#ifdef USE_LOCALE_CTYPE
(IN_LC_RUNTIME(LC_CTYPE))
? ! IN_UTF8_CTYPE_LOCALE
:
#endif
! IN_UNI_8_BIT))
{
/* We can convert in place. The reason we can't if in UNI_8_BIT is to
* make the loop tight, so we overwrite the source with the dest before
* looking at it, and we need to look at the original source
* afterwards. There would also need to be code added to handle
* switching to not in-place in midstream if we run into characters
* that change the length. Since being in locale overrides UNI_8_BIT,
* that latter becomes irrelevant in the above test; instead for
* locale, the size can't normally change, except if the locale is a
* UTF-8 one */
dest = source;
s = d = (U8*)SvPV_force_nomg(source, len);
min = len + 1;
} else {
dTARGET;
dest = TARG;
s = (const U8*)SvPV_nomg_const(source, len);
min = len + 1;
SvUPGRADE(dest, SVt_PV);
d = (U8*)SvGROW(dest, min);
(void)SvPOK_only(dest);
SETs(dest);
}
#ifdef USE_LOCALE_CTYPE
if (IN_LC_RUNTIME(LC_CTYPE)) {
CHECK_AND_WARN_PROBLEMATIC_LOCALE_;
}
#endif
/* Overloaded values may have toggled the UTF-8 flag on source, so we need
to check DO_UTF8 again here. */
if (DO_UTF8(source)) {
const U8 *const send = s + len;
U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
#define GREEK_CAPITAL_LETTER_IOTA 0x0399
#define COMBINING_GREEK_YPOGEGRAMMENI 0x0345
/* All occurrences of these are to be moved to follow any other marks.
* This is context-dependent. We may not be passed enough context to
* move the iota subscript beyond all of them, but we do the best we can
* with what we're given. The result is always better than if we
* hadn't done this. And, the problem would only arise if we are
* passed a character without all its combining marks, which would be
* the caller's mistake. The information this is based on comes from a
* comment in Unicode SpecialCasing.txt, (and the Standard's text
* itself) and so can't be checked properly to see if it ever gets
* revised. But the likelihood of it changing is remote */
bool in_iota_subscript = FALSE;
while (s < send) {
STRLEN u;
STRLEN ulen;
UV uv;
if (UNLIKELY(in_iota_subscript)) {
UV cp = utf8_to_uv_or_die(s, send, NULL);
if (! _invlist_contains_cp(PL_utf8_mark, cp)) {
/* A non-mark. Time to output the iota subscript */
*d++ = UTF8_TWO_BYTE_HI(GREEK_CAPITAL_LETTER_IOTA);
*d++ = UTF8_TWO_BYTE_LO(GREEK_CAPITAL_LETTER_IOTA);
in_iota_subscript = FALSE;
}
}
/* Then handle the current character. Get the changed case value
* and copy it to the output buffer */
u = UTF8SKIP(s);
#ifdef USE_LOCALE_CTYPE
uv = _toUPPER_utf8_flags(s, send, tmpbuf, &ulen, IN_LC_RUNTIME(LC_CTYPE));
#else
uv = _toUPPER_utf8_flags(s, send, tmpbuf, &ulen, 0);
#endif
if ( uv == GREEK_CAPITAL_LETTER_IOTA
&& utf8_to_uv_or_die(s, send, 0) ==
COMBINING_GREEK_YPOGEGRAMMENI)
{
in_iota_subscript = TRUE;
}
else {
if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
/* If the eventually required minimum size outgrows the
* available space, we need to grow. */
const UV o = d - (U8*)SvPVX_const(dest);
/* If someone uppercases one million U+03B0s we SvGROW()
* one million times. Or we could try guessing how much to
* allocate without allocating too much. But we can't
* really guess without examining the rest of the string.
* Such is life. See corresponding comment in lc code for
* another option */
d = o + (U8*) SvGROW(dest, min);
}
Copy(tmpbuf, d, ulen, U8);
d += ulen;
}
s += u;
}
if (in_iota_subscript) {
*d++ = UTF8_TWO_BYTE_HI(GREEK_CAPITAL_LETTER_IOTA);
*d++ = UTF8_TWO_BYTE_LO(GREEK_CAPITAL_LETTER_IOTA);
}
SvUTF8_on(dest);
*d = '\0';
SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
}
else { /* Not UTF-8 */
if (len) {
const U8 *const send = s + len;
/* Use locale casing if in locale; regular style if not treating
* latin1 as having case; otherwise the latin1 casing. Do the
* whole thing in a tight loop, for speed, */
#ifdef USE_LOCALE_CTYPE
if (IN_LC_RUNTIME(LC_CTYPE)) {
if (IN_UTF8_CTYPE_LOCALE) {
goto do_uni_rules;
}
for (; s < send; d++, s++)
*d = (U8) toUPPER_LC(*s);
}
else
#endif
if (! IN_UNI_8_BIT) {
for (; s < send; d++, s++) {
*d = toUPPER(*s);
}
}
else {
#ifdef USE_LOCALE_CTYPE
do_uni_rules:
#endif
for (; s < send; d++, s++) {
Size_t extra;
*d = toUPPER_LATIN1_MOD(*s);
if ( LIKELY(*d != LATIN_SMALL_LETTER_Y_WITH_DIAERESIS)
#ifdef USE_LOCALE_CTYPE
&& (LIKELY( ! IN_UTF8_TURKIC_LOCALE
|| ! IN_LC_RUNTIME(LC_CTYPE))
|| *s != 'i')
#endif
) {
continue;
}
/* The mainstream case is the tight loop above. To avoid
* extra tests in that, all three characters that always
* require special handling are mapped by the MOD to the
* one tested just above. Use the source to distinguish
* between those cases */
#if UNICODE_MAJOR_VERSION > 2 \
|| (UNICODE_MAJOR_VERSION == 2 && UNICODE_DOT_VERSION >= 1 \
&& UNICODE_DOT_DOT_VERSION >= 8)
if (*s == LATIN_SMALL_LETTER_SHARP_S) {
/* uc() of this requires 2 characters, but they are
* ASCII. If not enough room, grow the string */
if (SvLEN(dest) < ++min) {
const UV o = d - (U8*)SvPVX_const(dest);
d = o + (U8*) SvGROW(dest, min);
}
*d++ = 'S'; *d = 'S'; /* upper case is 'SS' */
continue; /* Back to the tight loop; still in ASCII */
}
#endif
/* The other special handling characters have their
* upper cases outside the latin1 range, hence need to be
* in UTF-8, so the whole result needs to be in UTF-8.
*
* So, here we are somewhere in the middle of processing a
* non-UTF-8 string, and realize that we will have to
* convert the whole thing to UTF-8. What to do? There
* are several possibilities. The simplest to code is to
* convert what we have so far, set a flag, and continue on
* in the loop. The flag would be tested each time through
* the loop, and if set, the next character would be
* converted to UTF-8 and stored. But, I (khw) didn't want
* to slow down the mainstream case at all for this fairly
* rare case, so I didn't want to add a test that didn't
* absolutely have to be there in the loop, besides the
* possibility that it would get too complicated for
* optimizers to deal with. Another possibility is to just
* give up, convert the source to UTF-8, and restart the
* function that way. Another possibility is to convert
* both what has already been processed and what is yet to
* come separately to UTF-8, then jump into the loop that
* handles UTF-8. But the most efficient time-wise of the
* ones I could think of is what follows, and turned out to
* not require much extra code.
*
* First, calculate the extra space needed for the
* remainder of the source needing to be in UTF-8. Except
* for the 'i' in Turkic locales, in UTF-8 strings, the
* uppercase of a character below 256 occupies the same
* number of bytes as the original. Therefore, the space
* needed is the that number plus the number of characters
* that become two bytes when converted to UTF-8, plus, in
* turkish locales, the number of 'i's. */
extra = send - s + variant_under_utf8_count(s, send);
#ifdef USE_LOCALE_CTYPE
if (UNLIKELY(*s == 'i')) { /* We wouldn't get an 'i' here
unless are in a Turkic
locale */
const U8 * s_peek = s;
do {
extra++;
s_peek = (U8 *) memchr(s_peek + 1, 'i',
send - (s_peek + 1));
} while (s_peek != NULL);
}
#endif
/* Convert what we have so far into UTF-8, telling the
* function that we know it should be converted, and to
* allow extra space for what we haven't processed yet.
*
* This may cause the string pointer to move, so need to
* save and re-find it. */
len = d - (U8*)SvPVX_const(dest);
SvCUR_set(dest, len);
len = sv_utf8_upgrade_flags_grow(dest,
SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
extra
+ 1 /* trailing NUL */ );
d = (U8*)SvPVX(dest) + len;
/* Now process the remainder of the source, simultaneously
* converting to upper and UTF-8.
*
* To avoid extra tests in the loop body, and since the
* loop is so simple, split out the rare Turkic case into
* its own loop */
#ifdef USE_LOCALE_CTYPE
if ( UNLIKELY(IN_UTF8_TURKIC_LOCALE)
&& UNLIKELY(IN_LC_RUNTIME(LC_CTYPE)))
{
for (; s < send; s++) {
if (*s == 'i') {
*d++ = UTF8_TWO_BYTE_HI(LATIN_CAPITAL_LETTER_I_WITH_DOT_ABOVE);
*d++ = UTF8_TWO_BYTE_LO(LATIN_CAPITAL_LETTER_I_WITH_DOT_ABOVE);
}
else {
(void) _to_upper_title_latin1(*s, d, &len, 'S');
d += len;
}
}
}
else
#endif
for (; s < send; s++) {
(void) _to_upper_title_latin1(*s, d, &len, 'S');
d += len;
}
/* Here have processed the whole source; no need to
* continue with the outer loop. Each character has been
* converted to upper case and converted to UTF-8. */
break;
} /* End of processing all latin1-style chars */
} /* End of processing all chars */
} /* End of source is not empty */
if (source != dest) {
*d = '\0'; /* Here d points to 1 after last char, add NUL */
SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
}
} /* End of isn't utf8 */
#ifdef USE_LOCALE_CTYPE
if (IN_LC_RUNTIME(LC_CTYPE)) {
TAINT;
SvTAINTED_on(dest);
}
#endif
if (dest != source && SvTAINTED(source))
SvTAINT(dest);
SvSETMAGIC(dest);
return NORMAL;
}
PP_wrapped(pp_lc, 1, 0)
{
dSP;
SV *source = TOPs;
STRLEN len;
STRLEN min;
SV *dest;
const U8 *s;
U8 *d;
bool has_turkic_I = FALSE;
SvGETMAGIC(source);
if ( SvPADTMP(source)
&& !SvREADONLY(source) && SvPOK(source)
&& !DO_UTF8(source)
#ifdef USE_LOCALE_CTYPE
&& ( LIKELY(! IN_LC_RUNTIME(LC_CTYPE))
|| LIKELY(! IN_UTF8_TURKIC_LOCALE))
#endif
) {
/* We can convert in place, as, outside of Turkic UTF-8 locales,
* lowercasing anything in the latin1 range (or else DO_UTF8 would have
* been on) doesn't lengthen it. */
dest = source;
s = d = (U8*)SvPV_force_nomg(source, len);
min = len + 1;
} else {
dTARGET;
dest = TARG;
s = (const U8*)SvPV_nomg_const(source, len);
min = len + 1;
SvUPGRADE(dest, SVt_PV);
d = (U8*)SvGROW(dest, min);
(void)SvPOK_only(dest);
SETs(dest);
}
#ifdef USE_LOCALE_CTYPE
if (IN_LC_RUNTIME(LC_CTYPE)) {
const U8 * next_I;
CHECK_AND_WARN_PROBLEMATIC_LOCALE_;
/* Lowercasing in a Turkic locale can cause non-UTF-8 to need to become
* UTF-8 for the single case of the character 'I' */
if ( UNLIKELY(IN_UTF8_TURKIC_LOCALE)
&& ! DO_UTF8(source)
&& (next_I = (U8 *) memchr(s, 'I', len)))
{
Size_t I_count = 0;
const U8 *const send = s + len;
do {
I_count++;
next_I = (U8 *) memchr(next_I + 1, 'I',
send - (next_I + 1));
} while (next_I != NULL);
/* Except for the 'I', in UTF-8 strings, the lower case of a
* character below 256 occupies the same number of bytes as the
* original. Therefore, the space needed is the original length
* plus I_count plus the number of characters that become two bytes
* when converted to UTF-8 */
sv_utf8_upgrade_flags_grow(dest, 0, len
+ I_count
+ variant_under_utf8_count(s, send)
+ 1 /* Trailing NUL */ );
d = (U8*)SvPVX(dest);
has_turkic_I = TRUE;
}
}
#else
PERL_UNUSED_VAR(has_turkic_I);
#endif
/* Overloaded values may have toggled the UTF-8 flag on source, so we need
to check DO_UTF8 again here. */
if (DO_UTF8(source)) {
const U8 *const send = s + len;
U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
bool remove_dot_above = FALSE;
while (s < send) {
const STRLEN u = UTF8SKIP(s);
STRLEN ulen;
#ifdef USE_LOCALE_CTYPE
_toLOWER_utf8_flags(s, send, tmpbuf, &ulen, IN_LC_RUNTIME(LC_CTYPE));
/* If we are in a Turkic locale, we have to do more work. As noted
* in the comments for lcfirst, there is a special case if a 'I'
* is in a grapheme with COMBINING DOT ABOVE UTF8. It turns into a
* 'i', and the DOT must be removed. We check for that situation,
* and set a flag if the DOT is there. Then each time through the
* loop, we have to see if we need to remove the next DOT above,
* and if so, do it. We know that there is a DOT because
* _toLOWER_utf8_flags() wouldn't have returned 'i' unless there
* was one in a proper position. */
if ( UNLIKELY(IN_UTF8_TURKIC_LOCALE)
&& IN_LC_RUNTIME(LC_CTYPE))
{
if ( UNLIKELY(remove_dot_above)
&& memBEGINs(tmpbuf, sizeof(tmpbuf), COMBINING_DOT_ABOVE_UTF8))
{
s += u;
remove_dot_above = FALSE;
continue;
}
else if (UNLIKELY(*s == 'I' && tmpbuf[0] == 'i')) {
remove_dot_above = TRUE;
}
}
#else
PERL_UNUSED_VAR(remove_dot_above);
_toLOWER_utf8_flags(s, send, tmpbuf, &ulen, 0);
#endif
/* Here is where we would do context-sensitive actions for the
* Greek final sigma. See the commit message for 86510fb15 for why
* there isn't any */
if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
/* If the eventually required minimum size outgrows the
* available space, we need to grow. */
const UV o = d - (U8*)SvPVX_const(dest);
/* If someone lowercases one million U+0130s we SvGROW() one
* million times. Or we could try guessing how much to
* allocate without allocating too much. Such is life.
* Another option would be to grow an extra byte or two more
* each time we need to grow, which would cut down the million
* to 500K, with little waste */
d = o + (U8*) SvGROW(dest, min);
}
/* Copy the newly lowercased letter to the output buffer we're
* building */
Copy(tmpbuf, d, ulen, U8);
d += ulen;
s += u;
} /* End of looping through the source string */
SvUTF8_on(dest);
*d = '\0';
SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
} else { /* 'source' not utf8 */
if (len) {
const U8 *const send = s + len;
/* Use locale casing if in locale; regular style if not treating
* latin1 as having case; otherwise the latin1 casing. Do the
* whole thing in a tight loop, for speed, */
#ifdef USE_LOCALE_CTYPE
if (IN_LC_RUNTIME(LC_CTYPE)) {
if (LIKELY( ! has_turkic_I)) {
for (; s < send; d++, s++)
*d = toLOWER_LC(*s);
}
else { /* This is the only case where lc() converts 'dest'
into UTF-8 from a non-UTF-8 'source' */
for (; s < send; s++) {
if (*s == 'I') {
*d++ = UTF8_TWO_BYTE_HI(LATIN_SMALL_LETTER_DOTLESS_I);
*d++ = UTF8_TWO_BYTE_LO(LATIN_SMALL_LETTER_DOTLESS_I);
}
else {
append_utf8_from_native_byte(toLOWER_LATIN1(*s), &d);
}
}
}
}
else
#endif
if (! IN_UNI_8_BIT) {
for (; s < send; d++, s++) {
*d = toLOWER(*s);
}
}
else {
for (; s < send; d++, s++) {
*d = toLOWER_LATIN1(*s);
}
}
}
if (source != dest) {
*d = '\0';
SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
}
}
#ifdef USE_LOCALE_CTYPE
if (IN_LC_RUNTIME(LC_CTYPE)) {
TAINT;
SvTAINTED_on(dest);
}
#endif
if (dest != source && SvTAINTED(source))
SvTAINT(dest);
SvSETMAGIC(dest);
return NORMAL;
}
PP(pp_quotemeta)
{
dTARGET;
SV * const sv = *PL_stack_sp;
STRLEN len;
const char *s = SvPV_const(sv,len);
SvUTF8_off(TARG); /* decontaminate */
if (len) {
char *d;
SvUPGRADE(TARG, SVt_PV);
SvGROW(TARG, (len * 2) + 1);
d = SvPVX(TARG);
if (DO_UTF8(sv)) {
while (len) {
STRLEN ulen = UTF8SKIP(s);
bool to_quote = FALSE;
if (UTF8_IS_INVARIANT(*s)) {
if (_isQUOTEMETA(*s)) {
to_quote = TRUE;
}
}
else if (UTF8_IS_NEXT_CHAR_DOWNGRADEABLE(s, s + len)) {
if (
#ifdef USE_LOCALE_CTYPE
/* In locale, we escape all non-ASCII Latin1 chars.
* Otherwise use the quoting rules */
IN_LC_RUNTIME(LC_CTYPE)
||
#endif
_isQUOTEMETA(EIGHT_BIT_UTF8_TO_NATIVE(*s, *(s + 1))))
{
to_quote = TRUE;
}
}
else if (is_QUOTEMETA_high(s)) {
to_quote = TRUE;
}
if (to_quote) {
*d++ = '\\';
}
if (ulen > len)
ulen = len;
len -= ulen;
while (ulen--)
*d++ = *s++;
}
SvUTF8_on(TARG);
}
else if (IN_UNI_8_BIT) {
while (len--) {
if (_isQUOTEMETA(*s))
*d++ = '\\';
*d++ = *s++;
}
}
else {
/* For non UNI_8_BIT (and hence in locale) just escape all \W
* including everything above ASCII */
while (len--) {
if (!isWORDCHAR_A(*s))
*d++ = '\\';
*d++ = *s++;
}
}
*d = '\0';
SvCUR_set(TARG, d - SvPVX_const(TARG));
(void)SvPOK_only_UTF8(TARG);
}
else
sv_setpvn(TARG, s, len);
SvSETMAGIC(TARG);
rpp_replace_1_1_NN(TARG);
return NORMAL;
}
PP_wrapped(pp_fc, 1, 0)
{
dTARGET;
dSP;
SV *source = TOPs;
STRLEN len;
STRLEN min;
SV *dest;
const U8 *s;
const U8 *send;
U8 *d;
U8 tmpbuf[UTF8_MAXBYTES_CASE + 1];
#if UNICODE_MAJOR_VERSION > 3 /* no multifolds in early Unicode */ \
|| (UNICODE_MAJOR_VERSION == 3 && ( UNICODE_DOT_VERSION > 0) \
|| UNICODE_DOT_DOT_VERSION > 0)
const bool full_folding = TRUE; /* This variable is here so we can easily
move to more generality later */
#else
const bool full_folding = FALSE;
#endif
const U8 flags = ( full_folding ? FOLD_FLAGS_FULL : 0 )
#ifdef USE_LOCALE_CTYPE
| ( IN_LC_RUNTIME(LC_CTYPE) ? FOLD_FLAGS_LOCALE : 0 )
#endif
;
/* This is a facsimile of pp_lc, but with a thousand bugs thanks to me.
* You are welcome(?) -Hugmeir
*/
SvGETMAGIC(source);
dest = TARG;
if (SvOK(source)) {
s = (const U8*)SvPV_nomg_const(source, len);
} else {
if (ckWARN(WARN_UNINITIALIZED))
report_uninit(source);
s = (const U8*)"";
len = 0;
}
min = len + 1;
SvUPGRADE(dest, SVt_PV);
d = (U8*)SvGROW(dest, min);
(void)SvPOK_only(dest);
SETs(dest);
send = s + len;
#ifdef USE_LOCALE_CTYPE
if ( IN_LC_RUNTIME(LC_CTYPE) ) { /* Under locale */
CHECK_AND_WARN_PROBLEMATIC_LOCALE_;
}
#endif
if (DO_UTF8(source)) { /* UTF-8 flagged string. */
while (s < send) {
const STRLEN u = UTF8SKIP(s);
STRLEN ulen;
_toFOLD_utf8_flags(s, send, tmpbuf, &ulen, flags);
if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
const UV o = d - (U8*)SvPVX_const(dest);
d = o + (U8*) SvGROW(dest, min);
}
Copy(tmpbuf, d, ulen, U8);
d += ulen;
s += u;
}
SvUTF8_on(dest);
} /* Unflagged string */
else if (len) {
#ifdef USE_LOCALE_CTYPE
if ( IN_LC_RUNTIME(LC_CTYPE) ) { /* Under locale */
if (IN_UTF8_CTYPE_LOCALE) {
goto do_uni_folding;
}
for (; s < send; d++, s++)
*d = (U8) toFOLD_LC(*s);
}
else
#endif
if ( !IN_UNI_8_BIT ) { /* Under nothing, or bytes */
for (; s < send; d++, s++)
*d = toFOLD(*s);
}
else {
#ifdef USE_LOCALE_CTYPE
do_uni_folding:
#endif
/* For ASCII and the Latin-1 range, there's potentially three
* troublesome folds:
* \x{DF} (\N{LATIN SMALL LETTER SHARP S}), which under full
* casefolding becomes 'ss';
* \x{B5} (\N{MICRO SIGN}), which under any fold becomes
* \x{3BC} (\N{GREEK SMALL LETTER MU})
* I only in Turkic locales, this folds to \x{131}
* \N{LATIN SMALL LETTER DOTLESS I}
* For the rest, the casefold is their lowercase. */
for (; s < send; d++, s++) {
if ( UNLIKELY(*s == MICRO_SIGN)
#ifdef USE_LOCALE_CTYPE
|| ( UNLIKELY(IN_UTF8_TURKIC_LOCALE)
&& UNLIKELY(IN_LC_RUNTIME(LC_CTYPE))
&& UNLIKELY(*s == 'I'))
#endif
) {
Size_t extra = send - s
+ variant_under_utf8_count(s, send);
/* \N{MICRO SIGN}'s casefold is \N{GREEK SMALL LETTER MU},
* and 'I' in Turkic locales is \N{LATIN SMALL LETTER
* DOTLESS I} both of which are outside of the latin-1
* range. There's a couple of ways to deal with this -- khw
* discusses them in pp_lc/uc, so go there :) What we do
* here is upgrade what we had already casefolded, then
* enter an inner loop that appends the rest of the
* characters as UTF-8.
*
* First we calculate the needed size of the upgraded dest
* beyond what's been processed already (the upgrade
* function figures that out). Except for the 'I' in
* Turkic locales, in UTF-8 strings, the fold case of a
* character below 256 occupies the same number of bytes as
* the original (even the Sharp S). Therefore, the space
* needed is the number of bytes remaining plus the number
* of characters that become two bytes when converted to
* UTF-8 plus, in turkish locales, the number of 'I's */
if (UNLIKELY(*s == 'I')) {
const U8 * s_peek = s;
do {
extra++;
s_peek = (U8 *) memchr(s_peek + 1, 'I',
send - (s_peek + 1));
} while (s_peek != NULL);
}
/* Growing may move things, so have to save and recalculate
* 'd' */
len = d - (U8*)SvPVX_const(dest);
SvCUR_set(dest, len);
len = sv_utf8_upgrade_flags_grow(dest,
SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
extra
+ 1 /* Trailing NUL */ );
d = (U8*)SvPVX(dest) + len;
if (*s == 'I') {
*d++ = UTF8_TWO_BYTE_HI(LATIN_SMALL_LETTER_DOTLESS_I);
*d++ = UTF8_TWO_BYTE_LO(LATIN_SMALL_LETTER_DOTLESS_I);
}
else {
*d++ = UTF8_TWO_BYTE_HI(GREEK_SMALL_LETTER_MU);
*d++ = UTF8_TWO_BYTE_LO(GREEK_SMALL_LETTER_MU);
}
s++;
for (; s < send; s++) {
STRLEN ulen;
_to_uni_fold_flags(*s, d, &ulen, flags);
d += ulen;
}
break;
}
else if ( UNLIKELY(*s == LATIN_SMALL_LETTER_SHARP_S)
&& full_folding)
{
/* Under full casefolding, LATIN SMALL LETTER SHARP S
* becomes "ss", which may require growing the SV. */
if (SvLEN(dest) < ++min) {
const UV o = d - (U8*)SvPVX_const(dest);
d = o + (U8*) SvGROW(dest, min);
}
*(d)++ = 's';
*d = 's';
}
else { /* Else, the fold is the lower case */
*d = toLOWER_LATIN1(*s);
}
}
}
}
*d = '\0';
SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
#ifdef USE_LOCALE_CTYPE
if (IN_LC_RUNTIME(LC_CTYPE)) {
TAINT;
SvTAINTED_on(dest);
}
#endif
if (SvTAINTED(source))
SvTAINT(dest);
SvSETMAGIC(dest);
RETURN;
}
/* Arrays. */
PP(pp_aslice)
{
dMARK; dORIGMARK;
AV *const av = MUTABLE_AV(*PL_stack_sp);
const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
if (SvTYPE(av) == SVt_PVAV) {
const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
bool can_preserve = FALSE;
if (localizing) {
can_preserve = SvCANEXISTDELETE(av);
}
if (lval && localizing) {
SV **svp;
SSize_t max = -1;
for (svp = MARK + 1; svp < PL_stack_sp; svp++) {
const SSize_t elem = SvIV(*svp);
if (elem > max)
max = elem;
}
if (max > AvMAX(av))
av_extend(av, max);
}
while (++MARK < PL_stack_sp) {
SV **svp;
SSize_t elem = SvIV(*MARK);
bool preeminent = TRUE;
if (localizing && can_preserve) {
/* If we can determine whether the element exist,
* Try to preserve the existenceness of a tied array
* element by using EXISTS and DELETE if possible.
* Fallback to FETCH and STORE otherwise. */
preeminent = av_exists(av, elem);
}
svp = av_fetch(av, elem, lval);
if (lval) {
if (!svp || !*svp)
DIE(aTHX_ PL_no_aelem, elem);
if (localizing) {
if (preeminent)
save_aelem(av, elem, svp);
else
SAVEADELETE(av, elem);
}
}
rpp_replace_at_NN(MARK, svp ? *svp : &PL_sv_undef);
}
}
rpp_context(ORIGMARK, GIMME_V, 1);
return NORMAL;
}
/* %ary[1,3,5] */
PP(pp_kvaslice)
{
dMARK; dORIGMARK;
/* leave av on stack for now to avoid leak on croak */
AV *const av = MUTABLE_AV(*PL_stack_sp);
I32 lval = (PL_op->op_flags & OPf_MOD);
SSize_t items = PL_stack_sp - MARK - 1;
if (PL_op->op_private & OPpMAYBE_LVSUB) {
const I32 flags = is_lvalue_sub();
if (flags) {
if (!(flags & OPpENTERSUB_INARGS))
/* diag_listed_as: Can't modify %s in %s */
croak("Can't modify index/value array slice in list assignment");
lval = flags;
}
}
rpp_extend(items);
MARK = ORIGMARK;
/* move av from old top-of-stack to new top-of-stack */
PL_stack_sp[items] = PL_stack_sp[0];
PL_stack_sp[0] = NULL;
/* spread the index SVs out to every second location */
SSize_t i = items;
while (i > 1) {
*(MARK+i*2-1) = *(MARK+i);
*(MARK+i*2) = NULL;
*(MARK+i) = NULL;
i--;
}
PL_stack_sp += items;
while (++MARK < PL_stack_sp) {
SV **svp;
svp = av_fetch(av, SvIV(*MARK), lval);
if (lval) {
if (!svp || !*svp || *svp == &PL_sv_undef) {
DIE(aTHX_ PL_no_aelem, SvIV(*MARK));
}
/* replace key SV with a copy */
SV *oldsv = *MARK;
SV *newsv = newSVsv(oldsv);
#ifdef PERL_RC_STACK
*MARK = newsv;
SvREFCNT_dec(oldsv);
#else
*MARK = sv_2mortal(newsv);
#endif
}
MARK++;
rpp_replace_at(MARK, svp ? *svp : &PL_sv_undef);
}
/* pop AV, then apply void/scalar/list context to stack above mark */
rpp_context(ORIGMARK, GIMME_V, 1);
return NORMAL;
}
PP_wrapped(pp_aeach, 1, 0)
{
dSP;
AV *array = MUTABLE_AV(POPs);
const U8 gimme = GIMME_V;
IV *iterp = Perl_av_iter_p(aTHX_ array);
const IV current = (*iterp)++;
if (current > av_top_index(array)) {
*iterp = 0;
if (gimme == G_SCALAR)
RETPUSHUNDEF;
else
RETURN;
}
EXTEND(SP, 2);
mPUSHi(current);
if (gimme == G_LIST) {
SV **const element = av_fetch(array, current, 0);
PUSHs(element ? *element : &PL_sv_undef);
}
RETURN;
}
/* also used for: pp_avalues()*/
PP_wrapped(pp_akeys, 1, 0)
{
dSP;
AV *array = MUTABLE_AV(POPs);
const U8 gimme = GIMME_V;
*Perl_av_iter_p(aTHX_ array) = 0;
if (gimme == G_SCALAR) {
dTARGET;
PUSHi(av_count(array));
}
else if (gimme == G_LIST) {
if (UNLIKELY(PL_op->op_private & OPpMAYBE_LVSUB)) {
const I32 flags = is_lvalue_sub();
if (flags && !(flags & OPpENTERSUB_INARGS))
/* diag_listed_as: Can't modify %s in %s */
croak(
"Can't modify keys on array in list assignment");
}
{
IV n = av_top_index(array);
IV i;
EXTEND(SP, n + 1);
if ( PL_op->op_type == OP_AKEYS
|| ( PL_op->op_type == OP_AVHVSWITCH
&& (PL_op->op_private & 3) + OP_AEACH == OP_AKEYS ))
{
for (i = 0; i <= n; i++) {
mPUSHi(i);
}
}
else {
for (i = 0; i <= n; i++) {
SV *const *const elem = Perl_av_fetch(aTHX_ array, i, 0);
PUSHs(elem ? *elem : &PL_sv_undef);
}
}
}
}
RETURN;
}
/* Associative arrays. */
PP_wrapped(pp_each, 1, 0)
{
dSP;
HV * hash = MUTABLE_HV(POPs);
HE *entry;
const U8 gimme = GIMME_V;
entry = hv_iternext(hash);
EXTEND(SP, 2);
if (entry) {
SV* const sv = hv_iterkeysv(entry);
PUSHs(sv);
if (gimme == G_LIST) {
SV *val;
val = hv_iterval(hash, entry);
PUSHs(val);
}
}
else if (gimme == G_SCALAR)
RETPUSHUNDEF;
RETURN;
}
STATIC OP *
S_do_delete_local(pTHX)
{
dSP;
const U8 gimme = GIMME_V;
const bool sliced = cBOOL(PL_op->op_private & OPpSLICE);
SV **unsliced_keysv = sliced ? NULL : sp--;
SV * const osv = POPs;
SV **mark = sliced ? PL_stack_base + POPMARK : unsliced_keysv-1;
dORIGMARK;
const bool tied = SvRMAGICAL(osv)
&& mg_find((const SV *)osv, PERL_MAGIC_tied);
const bool can_preserve = SvCANEXISTDELETE(osv);
const U32 type = SvTYPE(osv);
SV ** const end = sliced ? SP : unsliced_keysv;
if (type == SVt_PVHV) { /* hash element */
HV * const hv = MUTABLE_HV(osv);
while (++MARK <= end) {
SV * const keysv = *MARK;
SV *sv = NULL;
bool preeminent = TRUE;
if (can_preserve)
preeminent = hv_exists_ent(hv, keysv, 0);
if (tied) {
HE *he = hv_fetch_ent(hv, keysv, 1, 0);
if (he)
sv = HeVAL(he);
else
preeminent = FALSE;
}
else {
sv = hv_delete_ent(hv, keysv, 0, 0);
if (preeminent)
SvREFCNT_inc_simple_void(sv); /* De-mortalize */
}
if (preeminent) {
if (!sv) DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
save_helem_flags(hv, keysv, &sv, SAVEf_KEEPOLDELEM);
if (tied) {
*MARK = sv_mortalcopy(sv);
mg_clear(sv);
} else
*MARK = sv;
}
else {
SAVEHDELETE(hv, keysv);
*MARK = &PL_sv_undef;
}
}
}
else if (type == SVt_PVAV) { /* array element */
if (PL_op->op_flags & OPf_SPECIAL) {
AV * const av = MUTABLE_AV(osv);
while (++MARK <= end) {
SSize_t idx = SvIV(*MARK);
SV *sv = NULL;
bool preeminent = TRUE;
if (can_preserve)
preeminent = av_exists(av, idx);
if (tied) {
SV **svp = av_fetch(av, idx, 1);
if (svp)
sv = *svp;
else
preeminent = FALSE;
}
else {
sv = av_delete(av, idx, 0);
if (preeminent)
SvREFCNT_inc_simple_void(sv); /* De-mortalize */
}
if (preeminent) {
save_aelem_flags(av, idx, &sv, SAVEf_KEEPOLDELEM);
if (tied) {
*MARK = sv_mortalcopy(sv);
mg_clear(sv);
} else
*MARK = sv;
}
else {
SAVEADELETE(av, idx);
*MARK = &PL_sv_undef;
}
}
}
else
DIE(aTHX_ "panic: avhv_delete no longer supported");
}
else
DIE(aTHX_ "Not a HASH reference");
if (sliced) {
if (gimme == G_VOID)
SP = ORIGMARK;
else if (gimme == G_SCALAR) {
MARK = ORIGMARK;
if (SP > MARK)
*++MARK = *SP;
else
*++MARK = &PL_sv_undef;
SP = MARK;
}
}
else if (gimme != G_VOID)
PUSHs(*unsliced_keysv);
RETURN;
}
PP_wrapped(pp_delete,
((PL_op->op_private & (OPpSLICE|OPpKVSLICE)) ? 0 : 2),
((PL_op->op_private & (OPpSLICE|OPpKVSLICE)) ? 1 : 0))
{
dSP;
U8 gimme;
I32 discard;
if (PL_op->op_private & OPpLVAL_INTRO)
return do_delete_local();
gimme = GIMME_V;
discard = (gimme == G_VOID) ? G_DISCARD : 0;
if (PL_op->op_private & (OPpSLICE|OPpKVSLICE)) {
dMARK; dORIGMARK;
HV * const hv = MUTABLE_HV(POPs);
const U32 hvtype = SvTYPE(hv);
int skip = 0;
if (PL_op->op_private & OPpKVSLICE) {
SSize_t items = SP - MARK;
MEXTEND(SP,items);
while (items > 1) {
*(MARK+items*2-1) = *(MARK+items);
items--;
}
items = SP - MARK;
SP += items;
skip = 1;
}
if (hvtype == SVt_PVHV) { /* hash element */
while ((MARK += (1+skip)) <= SP) {
SV * const sv = hv_delete_ent(hv, *(MARK-skip), discard, 0);
*MARK = sv ? sv : &PL_sv_undef;
}
}
else if (hvtype == SVt_PVAV) { /* array element */
if (PL_op->op_flags & OPf_SPECIAL) {
while ((MARK += (1+skip)) <= SP) {
SV * const sv = av_delete(MUTABLE_AV(hv), SvIV(*(MARK-skip)), discard);
*MARK = sv ? sv : &PL_sv_undef;
}
}
}
else
DIE(aTHX_ "Not a HASH reference");
if (discard)
SP = ORIGMARK;
else if (gimme == G_SCALAR) {
MARK = ORIGMARK;
if (SP > MARK)
*++MARK = *SP;
else
*++MARK = &PL_sv_undef;
SP = MARK;
}
}
else {
SV *keysv = POPs;
HV * const hv = MUTABLE_HV(POPs);
SV *sv = NULL;
if (SvTYPE(hv) == SVt_PVHV)
sv = hv_delete_ent(hv, keysv, discard, 0);
else if (SvTYPE(hv) == SVt_PVAV) {
if (PL_op->op_flags & OPf_SPECIAL)
sv = av_delete(MUTABLE_AV(hv), SvIV(keysv), discard);
else
DIE(aTHX_ "panic: avhv_delete no longer supported");
}
else
DIE(aTHX_ "Not a HASH reference");
if (!sv)
sv = &PL_sv_undef;
if (!discard)
PUSHs(sv);
}
RETURN;
}
PP_wrapped(pp_exists, ((PL_op->op_private & OPpEXISTS_SUB) ? 1 : 2), 0)
{
dSP;
SV *tmpsv;
HV *hv;
if (UNLIKELY( PL_op->op_private & OPpEXISTS_SUB )) {
GV *gv;
SV * const sv = POPs;
CV * const cv = sv_2cv(sv, &hv, &gv, 0);
if (cv)
RETPUSHYES;
if (gv && isGV(gv) && GvCV(gv) && !GvCVGEN(gv))
RETPUSHYES;
RETPUSHNO;
}
tmpsv = POPs;
hv = MUTABLE_HV(POPs);
if (LIKELY( SvTYPE(hv) == SVt_PVHV )) {
if (hv_exists_ent(hv, tmpsv, 0))
RETPUSHYES;
}
else if (SvTYPE(hv) == SVt_PVAV) {
if (PL_op->op_flags & OPf_SPECIAL) { /* array element */
if (av_exists(MUTABLE_AV(hv), SvIV(tmpsv)))
RETPUSHYES;
}
}
else {
DIE(aTHX_ "Not a HASH reference");
}
RETPUSHNO;
}
/* OP_HELEMEXISTSOR is a LOGOP not currently available to pure Perl code, but
* is defined for use by the core for new features, optimisations, or XS
* modules.
*
* Constructing it consumes two optrees, the first of which must be an
* OP_HELEM.
*
* OP *o = newLOGOP(OP_HELEMEXISTSOR, 0, helemop, otherop);
*
* If the hash element exists (by the same rules as OP_EXISTS would find
* true) the op pushes it to the stack in the same way as a regular OP_HELEM
* and invokes op_next. If the element does not exist, then op_other is
* invoked instead. This is roughly equivalent to the perl code
*
* exists $hash{$key} ? $hash{$key} : OTHER
*
* Except that any expressions or side-effects involved in obtaining the HV
* or the key are only invoked once, and it is a little more efficient when
* run on regular (non-magical) HVs.
*
* Combined with the OPpHELEMEXISTSOR_DELETE flag in op_private, this
* additionally deletes the element if found.
*
* On a tied HV, the 'EXISTS' method will be run as expected. If the method
* returns true then either the 'FETCH' or 'DELETE' method will also be run
* as required.
*/
PP(pp_helemexistsor)
{
SV *keysv = PL_stack_sp[0];
HV *hv = MUTABLE_HV(PL_stack_sp[-1]);
bool is_delete = PL_op->op_private & OPpHELEMEXISTSOR_DELETE;
assert(SvTYPE(hv) == SVt_PVHV);
bool hv_is_magical = UNLIKELY(SvMAGICAL(hv));
SV *val = NULL;
/* For magical HVs we have to ensure we invoke the EXISTS method first.
* For regular HVs we can just skip this and use the "pointer or NULL"
* result of the real hv_* functions
*/
if(hv_is_magical && !hv_exists_ent(hv, keysv, 0))
goto other;
if(is_delete) {
val = hv_delete_ent(hv, keysv, 0, 0);
}
else {
HE *he = hv_fetch_ent(hv, keysv, 0, 0);
val = he ? HeVAL(he) : NULL;
/* A magical HV hasn't yet actually invoked the FETCH method. We must
* ask it to do so now
*/
if(hv_is_magical && val)
SvGETMAGIC(val);
}
if(!val) {
other:
rpp_popfree_2_NN();
return cLOGOP->op_other;
}
rpp_replace_2_1_NN(val);
return NORMAL;
}
/* @hash{'foo', 'bar'} */
PP(pp_hslice)
{
dMARK; dORIGMARK;
HV * const hv = MUTABLE_HV(*PL_stack_sp);
const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
bool can_preserve = FALSE;
if (localizing) {
if (SvCANEXISTDELETE(hv))
can_preserve = TRUE;
}
while (++MARK < PL_stack_sp) {
SV * const keysv = *MARK;
SV **svp;
HE *he;
bool preeminent = TRUE;
if (localizing && can_preserve) {
/* If we can determine whether the element exist,
* try to preserve the existenceness of a tied hash
* element by using EXISTS and DELETE if possible.
* Fallback to FETCH and STORE otherwise. */
preeminent = hv_exists_ent(hv, keysv, 0);
}
he = hv_fetch_ent(hv, keysv, lval, 0);
svp = he ? &HeVAL(he) : NULL;
if (lval) {
if (!svp || !*svp || *svp == &PL_sv_undef) {
DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
}
if (localizing) {
if (HvNAME_get(hv) && isGV_or_RVCV(*svp))
save_gp(MUTABLE_GV(*svp), !(PL_op->op_flags & OPf_SPECIAL));
else if (preeminent)
save_helem_flags(hv, keysv, svp,
(PL_op->op_flags & OPf_SPECIAL) ? 0 : SAVEf_SETMAGIC);
else
SAVEHDELETE(hv, keysv);
}
}
rpp_replace_at_NN(MARK, svp && *svp ? *svp : &PL_sv_undef);
}
rpp_context(ORIGMARK, GIMME_V, 1);
return NORMAL;
}
/* %hash{'foo', 'bar'} */
PP(pp_kvhslice)
{
dMARK; dORIGMARK;
/* leave hv on stack for now to avoid leak on croak */
HV * const hv = MUTABLE_HV(*PL_stack_sp);
I32 lval = (PL_op->op_flags & OPf_MOD);
SSize_t items = PL_stack_sp - MARK - 1;
if (PL_op->op_private & OPpMAYBE_LVSUB) {
const I32 flags = is_lvalue_sub();
if (flags) {
if (!(flags & OPpENTERSUB_INARGS))
/* diag_listed_as: Can't modify %s in %s */
croak("Can't modify key/value hash slice in %s assignment",
GIMME_V == G_LIST ? "list" : "scalar");
lval = flags;
}
}
rpp_extend(items);
MARK = ORIGMARK;
/* move hv from old top-of-stack to new top-of-stack */
PL_stack_sp[items] = PL_stack_sp[0];
PL_stack_sp[0] = NULL;
/* spread the key SVs out to every second location */
SSize_t i = items;
while (i > 1) {
*(MARK+i*2-1) = *(MARK+i);
*(MARK+i*2) = NULL;
*(MARK+i) = NULL;
i--;
}
PL_stack_sp += items;
while (++MARK < PL_stack_sp) {
SV * const keysv = *MARK;
SV **svp;
HE *he;
he = hv_fetch_ent(hv, keysv, lval, 0);
svp = he ? &HeVAL(he) : NULL;
if (lval) {
if (!svp || !*svp || *svp == &PL_sv_undef) {
DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
}
/* replace key SV with a copy */
SV *oldsv = *MARK;
SV *newsv = newSVsv(oldsv);
#ifdef PERL_RC_STACK
*MARK = newsv;
SvREFCNT_dec(oldsv);
#else
*MARK = sv_2mortal(newsv);
#endif
}
MARK++;
rpp_replace_at(MARK, (svp && *svp) ? *svp : &PL_sv_undef);
}
/* pop HV, then apply void/scalar/list context to stack above mark */
rpp_context(ORIGMARK, GIMME_V, 1);
return NORMAL;
}
/* List operators. */
PP(pp_list)
{
dMARK;
rpp_context(mark, GIMME_V, 0);
return NORMAL;
}
PP_wrapped(pp_lslice, 0, 2)
{
dSP;
SV ** const lastrelem = PL_stack_sp;
SV ** const lastlelem = PL_stack_base + POPMARK;
SV ** const firstlelem = PL_stack_base + POPMARK + 1;
SV ** const firstrelem = lastlelem + 1;
const U8 mod = PL_op->op_flags & OPf_MOD;
const SSize_t max = lastrelem - lastlelem;
SV **lelem;
if (GIMME_V != G_LIST) {
if (lastlelem < firstlelem) {
EXTEND(SP, 1);
*firstlelem = &PL_sv_undef;
}
else {
SSize_t ix = SvIV(*lastlelem);
if (ix < 0)
ix += max;
if (ix < 0 || ix >= max)
*firstlelem = &PL_sv_undef;
else
*firstlelem = firstrelem[ix];
}
SP = firstlelem;
RETURN;
}
if (max == 0) {
SP = firstlelem - 1;
RETURN;
}
for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
SSize_t ix = SvIV(*lelem);
if (ix < 0)
ix += max;
if (ix < 0 || ix >= max)
*lelem = &PL_sv_undef;
else {
if (!(*lelem = firstrelem[ix]))
*lelem = &PL_sv_undef;
else if (mod && SvPADTMP(*lelem)) {
*lelem = firstrelem[ix] = sv_mortalcopy(*lelem);
}
}
}
SP = lastlelem;
RETURN;
}
PP(pp_anonlist)
{
dMARK;
const SSize_t items = PL_stack_sp - MARK;
SV * const av = MUTABLE_SV(av_make(items, MARK+1));
/* attach new SV to stack before freeing everything else,
* so no leak on croak */
rpp_extend(1);
SV *sv = (PL_op->op_flags & OPf_SPECIAL) ? newRV_noinc(av) : (SV*)av;
rpp_push_1_norc(sv); /* this handles ref count and/or mortalising */
PL_stack_sp[0] = PL_stack_sp[-items];
PL_stack_sp[-items] = sv;
rpp_popfree_to_NN(PL_stack_sp - items);
return NORMAL;
}
/* When an anonlist or anonhash will (1) be empty and (2) return an RV
* pointing to the new AV/HV, the peephole optimizer can swap in this
* simpler function and op_null the originally associated PUSHMARK. */
PP(pp_emptyavhv)
{
OP * const op = PL_op;
SV * rv;
SV * const sv = MUTABLE_SV( newSV_type(
(op->op_private & OPpEMPTYAVHV_IS_HV) ?
SVt_PVHV :
SVt_PVAV ) );
/* Is it an assignment, just a stack push, or both?*/
if (op->op_private & OPpTARGET_MY) {
SV** const padentry = &PAD_SVl(op->op_targ);
rv = *padentry;
/* Since the op_targ is very likely to be an undef SVt_IV from
* a previous iteration, converting it to a live RV can
* typically be special-cased.*/
if (SvTYPE(rv) == SVt_IV && !SvOK(rv)) {
SvFLAGS(rv) = (SVt_IV | SVf_ROK);
SvRV_set(rv, sv);
} else {
sv_setrv_noinc_mg(rv, sv);
}
if ((op->op_private & (OPpLVAL_INTRO|OPpPAD_STATE)) == OPpLVAL_INTRO) {
save_clearsv(padentry);
}
if (GIMME_V == G_VOID) {
return NORMAL; /* skip extending and pushing */
}
rpp_xpush_1(rv);
} else {
/* Inlined newRV_noinc */
SV * refsv = newSV_type(SVt_IV);
SvRV_set(refsv, sv);
SvROK_on(refsv);
rpp_extend(1);
rpp_push_1_norc(refsv);
}
return NORMAL; /* skip extending and pushing */
}
/* return { list };
* without OPf_SPECIAL, return hash rather than hash ref */
PP(pp_anonhash)
{
dMARK; dORIGMARK;
HV* const hv = newHV();
SV* const retval = (PL_op->op_flags & OPf_SPECIAL)
? newRV_noinc(MUTABLE_SV(hv))
: MUTABLE_SV(hv);
/* + 1 because a lone scalar {FOO} counts as a {FOO => undef} pair */
const SSize_t pairs = (PL_stack_sp - MARK + 1) >> 1;
/* temporarily save the hv/hvref at the top of the stack to
* avoid possible premature free */
rpp_extend(1);
rpp_push_1_norc(retval);
MARK = ORIGMARK; /* in case stack was reallocated */
if (pairs == 0)
return NORMAL;
if (pairs > PERL_HASH_DEFAULT_HvMAX) {
hv_ksplit(hv, pairs);
}
while (++MARK < PL_stack_sp) {
SV *key = *MARK;
if (SvGMAGICAL(key))
key = sv_mortalcopy(key);
SV *val;
if (++MARK < PL_stack_sp)
{
SvGETMAGIC(*MARK);
val = newSVsv_flags(*MARK, SV_DO_COW_SVSETSV);
}
else
{
ck_warner(packWARN(WARN_MISC), "Odd number of elements in anonymous hash");
val = newSV_type(SVt_NULL);
}
(void)hv_store_ent(hv,key,val,0);
}
/* swap the HV (which is at the top of stack) with the first key
* (which is at the bottom of the stack frame), then free everything
* above it */
*PL_stack_sp = ORIGMARK[1];
ORIGMARK[1] = retval;
rpp_popfree_to_NN(ORIGMARK+1);
return NORMAL;
}
PP_wrapped(pp_splice, 0, 1)
{
dSP; dMARK; dORIGMARK;
int num_args = (SP - MARK);
AV *ary = MUTABLE_AV(*++MARK);
SV **src;
SV **dst;
SSize_t i;
SSize_t offset;
SSize_t length;
SSize_t newlen;
SSize_t after;
SSize_t diff;
const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
if (mg) {
return Perl_tied_method(aTHX_ SV_CONST(SPLICE), mark - 1, MUTABLE_SV(ary), mg,
GIMME_V | TIED_METHOD_ARGUMENTS_ON_STACK,
sp - mark);
}
if (SvREADONLY(ary))
croak_no_modify();
SP++;
if (++MARK < SP) {
offset = i = SvIV(*MARK);
if (offset < 0)
offset += AvFILLp(ary) + 1;
if (offset < 0)
DIE(aTHX_ PL_no_aelem, i);
if (++MARK < SP) {
length = SvIVx(*MARK++);
if (length < 0) {
length += AvFILLp(ary) - offset + 1;
if (length < 0)
length = 0;
}
}
else
length = AvMAX(ary) + 1; /* close enough to infinity */
}
else {
offset = 0;
length = AvMAX(ary) + 1;
}
if (offset > AvFILLp(ary) + 1) {
if (num_args > 2)
ck_warner(packWARN(WARN_MISC), "splice() offset past end of array" );
offset = AvFILLp(ary) + 1;
}
after = AvFILLp(ary) + 1 - (offset + length);
if (after < 0) { /* not that much array */
length += after; /* offset+length now in array */
after = 0;
if (!AvALLOC(ary))
av_extend(ary, 0);
}
/* At this point, MARK .. SP-1 is our new LIST */
newlen = SP - MARK;
diff = newlen - length;
if (newlen && !AvREAL(ary) && AvREIFY(ary))
av_reify(ary);
/* make new elements SVs now: avoid problems if they're from the array */
for (dst = MARK, i = newlen; i; i--) {
SV * const h = *dst;
*dst++ = newSVsv(h);
}
if (diff < 0) { /* shrinking the area */
SV **tmparyval = NULL;
if (newlen) {
Newx(tmparyval, newlen, SV*); /* so remember insertion */
Copy(MARK, tmparyval, newlen, SV*);
}
MARK = ORIGMARK + 1;
if (GIMME_V == G_LIST) { /* copy return vals to stack */
const bool real = cBOOL(AvREAL(ary));
MEXTEND(MARK, length);
if (real)
EXTEND_MORTAL(length);
for (i = 0, dst = MARK; i < length; i++) {
if ((*dst = AvARRAY(ary)[i+offset])) {
if (real)
sv_2mortal(*dst); /* free them eventually */
}
else
*dst = &PL_sv_undef;
dst++;
}
MARK += length - 1;
}
else {
*MARK = AvARRAY(ary)[offset+length-1];
if (AvREAL(ary)) {
sv_2mortal(*MARK);
for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
SvREFCNT_dec(*dst++); /* free them now */
}
if (!*MARK)
*MARK = &PL_sv_undef;
}
AvFILLp(ary) += diff;
/* pull up or down? */
if (offset < after) { /* easier to pull up */
if (offset) { /* esp. if nothing to pull */
src = &AvARRAY(ary)[offset-1];
dst = src - diff; /* diff is negative */
for (i = offset; i > 0; i--) /* can't trust Copy */
*dst-- = *src--;
}
dst = AvARRAY(ary);
AvARRAY(ary) = AvARRAY(ary) - diff; /* diff is negative */
AvMAX(ary) += diff;
}
else {
if (after) { /* anything to pull down? */
src = AvARRAY(ary) + offset + length;
dst = src + diff; /* diff is negative */
Move(src, dst, after, SV*);
}
dst = &AvARRAY(ary)[AvFILLp(ary)+1];
/* avoid later double free */
}
i = -diff;
while (i)
dst[--i] = NULL;
if (newlen) {
Copy( tmparyval, AvARRAY(ary) + offset, newlen, SV* );
Safefree(tmparyval);
}
}
else { /* no, expanding (or same) */
SV** tmparyval = NULL;
if (length) {
Newx(tmparyval, length, SV*); /* so remember deletion */
Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
}
if (diff > 0) { /* expanding */
/* push up or down? */
if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
if (offset) {
src = AvARRAY(ary);
dst = src - diff;
Move(src, dst, offset, SV*);
}
AvARRAY(ary) = AvARRAY(ary) - diff;/* diff is positive */
AvMAX(ary) += diff;
AvFILLp(ary) += diff;
}
else {
if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
av_extend(ary, AvFILLp(ary) + diff);
AvFILLp(ary) += diff;
if (after) {
dst = AvARRAY(ary) + AvFILLp(ary);
src = dst - diff;
for (i = after; i; i--) {
*dst-- = *src--;
}
}
}
}
if (newlen) {
Copy( MARK, AvARRAY(ary) + offset, newlen, SV* );
}
MARK = ORIGMARK + 1;
if (GIMME_V == G_LIST) { /* copy return vals to stack */
if (length) {
const bool real = cBOOL(AvREAL(ary));
if (real)
EXTEND_MORTAL(length);
for (i = 0, dst = MARK; i < length; i++) {
if ((*dst = tmparyval[i])) {
if (real)
sv_2mortal(*dst); /* free them eventually */
}
else *dst = &PL_sv_undef;
dst++;
}
}
MARK += length - 1;
}
else if (length--) {
*MARK = tmparyval[length];
if (AvREAL(ary)) {
sv_2mortal(*MARK);
while (length-- > 0)
SvREFCNT_dec(tmparyval[length]);
}
if (!*MARK)
*MARK = &PL_sv_undef;
}
else
*MARK = &PL_sv_undef;
Safefree(tmparyval);
}
if (SvMAGICAL(ary))
mg_set(MUTABLE_SV(ary));
SP = MARK;
RETURN;
}
PP(pp_push)
{
dMARK; dORIGMARK; dTARGET;
AV * const ary = MUTABLE_AV(*++MARK);
const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
if (mg) {
ENTER_with_name("call_PUSH");
SV *obj = SvTIED_obj(MUTABLE_SV(ary), mg);
#ifdef PERL_RC_STACK
/* keep ary alive as it's replaced on the stack with obj */
SAVEFREESV(MUTABLE_SV(ary));
SvREFCNT_inc_simple_void(obj);
#endif
*MARK-- = obj;
PUSHMARK(MARK);
call_sv(SV_CONST(PUSH),G_SCALAR|G_DISCARD|G_METHOD_NAMED);
LEAVE_with_name("call_PUSH");
}
else {
/* PL_delaymagic is restored by JMPENV_POP on dieing, so we
* only need to save locally, not on the save stack */
U16 old_delaymagic = PL_delaymagic;
if (SvREADONLY(ary) && MARK < PL_stack_sp)
croak_no_modify();
PL_delaymagic = DM_DELAY;
for (++MARK; MARK <= PL_stack_sp; MARK++) {
SV *sv;
if (*MARK) SvGETMAGIC(*MARK);
sv = newSV_type(SVt_NULL);
if (*MARK)
sv_setsv_nomg(sv, *MARK);
av_store(ary, AvFILLp(ary)+1, sv);
}
if (PL_delaymagic & DM_ARRAY_ISA)
mg_set(MUTABLE_SV(ary));
PL_delaymagic = old_delaymagic;
}
rpp_popfree_to_NN(ORIGMARK);
if ( (PL_op->op_flags & OPf_WANT) != G_VOID
|| (PL_op->op_private & OPpTARGET_MY))
{
TARGi(AvFILL(ary) + 1, 1);
if ((PL_op->op_flags & OPf_WANT) != G_VOID)
rpp_push_1(targ);
}
return NORMAL;
}
/* also used for: pp_pop()*/
PP_wrapped(pp_shift, (PL_op->op_flags & OPf_SPECIAL ? 0 : 1), 0)
{
dSP;
AV * const av = PL_op->op_flags & OPf_SPECIAL
? MUTABLE_AV(GvAVn(PL_defgv)) : MUTABLE_AV(POPs);
SV * const sv = PL_op->op_type == OP_SHIFT ? av_shift(av) : av_pop(av);
EXTEND(SP, 1);
assert (sv);
if (AvREAL(av))
(void)sv_2mortal(sv);
PUSHs(sv);
RETURN;
}
PP(pp_unshift)
{
dMARK; dORIGMARK; dTARGET;
AV *ary = MUTABLE_AV(*++MARK);
const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
if (mg) {
ENTER_with_name("call_UNSHIFT");
SV *obj = SvTIED_obj(MUTABLE_SV(ary), mg);
#ifdef PERL_RC_STACK
/* keep ary alive as it's replaced on the stack with obj */
SAVEFREESV(MUTABLE_SV(ary));
SvREFCNT_inc_simple_void(obj);
#endif
*MARK-- = obj;
PUSHMARK(MARK);
call_sv(SV_CONST(UNSHIFT),G_SCALAR|G_DISCARD|G_METHOD_NAMED);
LEAVE_with_name("call_UNSHIFT");
}
else {
/* PL_delaymagic is restored by JMPENV_POP on dieing, so we
* only need to save locally, not on the save stack */
U16 old_delaymagic = PL_delaymagic;
SSize_t i = 0;
/* unshift N undefs into the array */
av_unshift(ary, PL_stack_sp - MARK);
PL_delaymagic = DM_DELAY;
if (!SvMAGICAL(ary)) {
/* The av_unshift above means that many of the checks inside
* av_store are unnecessary. If ary does not have magic attached
* then a simple direct assignment is possible here. */
while (MARK < PL_stack_sp) {
SV * const sv = newSVsv(*++MARK);
assert( !SvTIED_mg((const SV *)ary, PERL_MAGIC_tied) );
assert( i >= 0 );
assert( !SvREADONLY(ary) );
assert( AvREAL(ary) || !AvREIFY(ary) );
assert( i <= AvMAX(ary) );
assert( i <= AvFILLp(ary) );
if (AvREAL(ary))
SvREFCNT_dec(AvARRAY(ary)[i]);
AvARRAY(ary)[i] = sv;
i++;
}
} else {
while (MARK < PL_stack_sp) {
SV * const sv = newSVsv(*++MARK);
(void)av_store(ary, i++, sv);
}
}
if (PL_delaymagic & DM_ARRAY_ISA)
mg_set(MUTABLE_SV(ary));
PL_delaymagic = old_delaymagic;
}
rpp_popfree_to_NN(ORIGMARK);
if ( (PL_op->op_flags & OPf_WANT) != G_VOID
|| (PL_op->op_private & OPpTARGET_MY))
{
TARGi(AvFILL(ary) + 1, 1);
if ((PL_op->op_flags & OPf_WANT) != G_VOID)
rpp_push_1(targ);
}
return NORMAL;
}
PP_wrapped(pp_reverse, 0, 1)
{
dSP; dMARK;
if (GIMME_V == G_LIST) {
if (PL_op->op_private & OPpREVERSE_INPLACE) {
AV *av;
/* See pp_sort() */
assert( MARK+1 == SP && *SP && SvTYPE(*SP) == SVt_PVAV);
(void)POPMARK; /* remove mark associated with ex-OP_AASSIGN */
av = MUTABLE_AV((*SP));
/* In-place reversing only happens in void context for the array
* assignment. We don't need to push anything on the stack. */
SP = MARK;
if (SvMAGICAL(av)) {
SSize_t i, j;
SV *tmp = sv_newmortal();
bool can_preserve = SvCANEXISTDELETE(av);
for (i = 0, j = av_top_index(av); i < j; ++i, --j) {
SV *begin, *end;
if (can_preserve) {
if (!av_exists(av, i)) {
if (av_exists(av, j)) {
SV *sv = av_delete(av, j, 0);
begin = *av_fetch(av, i, TRUE);
sv_setsv_mg(begin, sv);
}
continue;
}
else if (!av_exists(av, j)) {
SV *sv = av_delete(av, i, 0);
end = *av_fetch(av, j, TRUE);
sv_setsv_mg(end, sv);
continue;
}
}
begin = *av_fetch(av, i, TRUE);
end = *av_fetch(av, j, TRUE);
sv_setsv(tmp, begin);
sv_setsv_mg(begin, end);
sv_setsv_mg(end, tmp);
}
}
else {
SV **begin = AvARRAY(av);
if (begin) {
SV **end = begin + AvFILLp(av);
while (begin < end) {
SV * const tmp = *begin;
*begin++ = *end;
*end-- = tmp;
}
}
}
}
else {
SV **oldsp = SP;
MARK++;
while (MARK < SP) {
SV * const tmp = *MARK;
*MARK++ = *SP;
*SP-- = tmp;
}
/* safe as long as stack cannot get extended in the above */
SP = oldsp;
}
}
else { /* GIMME_V != G_LIST. Doing string reversal. */
dTARGET;
STRLEN len;
SvUTF8_off(TARG); /* decontaminate */
if (SP - MARK > 1) {
do_join(TARG, &PL_sv_no, MARK, SP);
SP = MARK + 1;
SETs(TARG);
} else {
SV * src_sv = NULL;
/* Determine the source SV and get TARG on the stack */
if (SP > MARK) {
src_sv = *SP;
SETs(TARG);
} else {
src_sv = DEFSV;
XPUSHs(TARG);
}
assert(src_sv);
assert(src_sv != TARG);
if (/* Fallback to sv_setsv_flags() + in-place reversal if: */
/* src_sv may need careful handling */
SvTYPE(src_sv) > SVt_PVMG || SvGMAGICAL(src_sv) ||
SvVOK(src_sv) ||
/* src_sv doesn't contain a valid string */
!(SvFLAGS(src_sv) & SVp_POK) ||
/* TARG may need careful handling */
SvTYPE(TARG) > SVt_PVMG ||
/* sv_setsv_flags() will swipe src_sv's buffer */
sv_can_swipe_pv_buf(src_sv)
) {
sv_setsv_flags(TARG, src_sv, SV_GMAGIC);
/* FALLTHROUGH */
} else { /* Source & destination buffers are distinct. By not
* calling sv_setsv_flags(), we can do a reverse copy
* in a single pass, rather than 2-3 passes. */
const char * src = SvPV_const(src_sv, len);
/* Prepare the TARG. */
if (SvTYPE(TARG) < SVt_PV) {
SvUPGRADE(TARG, SvTYPE(src_sv)); /* No buffer allocation here */
} else if(SvTHINKFIRST(TARG)) {
SV_CHECK_THINKFIRST_COW_DROP(TARG); /* Drops any buffer */
}
SvSETMAGIC(TARG);
SvGROW(TARG, len + 1);
SvCUR_set(TARG, len);
SvPOK_only(TARG);
*SvEND(TARG) = '\0';
if (SvTAINTED(src_sv))
SvTAINT(TARG);
/* Do the reverse copy */
if (DO_UTF8(src_sv)) {
SvUTF8_on(TARG);
const U8* s = (const U8*)src;
U8* dd = (U8*)(SvPVX(TARG) + len);
const U8* send = (const U8*)(s + len);
int bytes = 0;
while (s < send) {
bytes = UTF8SKIP(s);
if (bytes == 1) {
*--dd = *s++;
} else {
dd -= bytes;
U8* d2 = dd;
while (bytes-- > 0)
*d2++ = *s++;
}
}
} else {
char * outp= SvPVX(TARG);
const char *p = src + len;
while (p != src)
*outp++ = *--p;
}
RETURN;
}
}
/* Traditional in-place reversal routines */
SvSETMAGIC(TARG); /* remove any utf8 length magic */
char *up = SvPV_force(TARG, len);
if (len > 1) {
/* The traditional way, operate on the current byte buffer */
char *down;
if (DO_UTF8(TARG)) { /* first reverse each character */
U8* s = (U8*)SvPVX(TARG);
const U8* send = (U8*)(s + len);
while (s < send) {
if (UTF8_IS_INVARIANT(*s)) {
s++;
continue;
}
else {
if (!utf8_to_uvchr_buf(s, send, 0))
break;
up = (char*)s;
s += UTF8SKIP(s);
down = (char*)(s - 1);
/* reverse this character */
while (down > up) {
const char tmp = *up;
*up++ = *down;
*down-- = tmp;
}
}
}
up = SvPVX(TARG);
}
down = SvPVX(TARG) + len - 1;
while (down > up) {
const char tmp = *up;
*up++ = *down;
*down-- = tmp;
}
}
(void)SvPOK_only_UTF8(TARG);
}
RETURN;
}
PP_wrapped(pp_split,
( (PL_op->op_private & OPpSPLIT_ASSIGN)
&& (PL_op->op_flags & OPf_STACKED))
? 3 : 2,
0)
{
dSP; dTARG;
AV *ary = ( (PL_op->op_private & OPpSPLIT_ASSIGN) /* @a = split */
&& (PL_op->op_flags & OPf_STACKED)) /* @{expr} = split */
? (AV *)POPs : NULL;
IV limit = POPi; /* note, negative is forever */
SV * const sv = POPs;
STRLEN len;
const char *s = SvPV_const(sv, len);
const bool do_utf8 = DO_UTF8(sv);
const bool in_uni_8_bit = IN_UNI_8_BIT;
const char *strend = s + len;
PMOP *pm = cPMOP;
REGEXP *rx;
SV *dstr;
const char *m;
SSize_t iters = 0;
const STRLEN slen = do_utf8
? utf8_length((U8*)s, (U8*)strend)
: (STRLEN)(strend - s);
SSize_t maxiters = slen + 10;
I32 trailing_empty = 0;
const char *orig;
const IV origlimit = limit;
bool realarray = 0;
SSize_t base;
const U8 gimme = GIMME_V;
bool gimme_scalar;
I32 oldsave = PL_savestack_ix;
U32 flags = (do_utf8 ? SVf_UTF8 : 0) |
SVs_TEMP; /* Make mortal SVs by default */
MAGIC *mg = NULL;
rx = PM_GETRE(pm);
TAINT_IF(get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET &&
(RX_EXTFLAGS(rx) & (RXf_WHITE | RXf_SKIPWHITE)));
/* handle @ary = split(...) optimisation */
if (PL_op->op_private & OPpSPLIT_ASSIGN) {
realarray = 1;
if (!(PL_op->op_flags & OPf_STACKED)) {
if (PL_op->op_private & OPpSPLIT_LEX) {
if (PL_op->op_private & OPpLVAL_INTRO)
SAVECLEARSV(PAD_SVl(pm->op_pmreplrootu.op_pmtargetoff));
ary = (AV *)PAD_SVl(pm->op_pmreplrootu.op_pmtargetoff);
}
else {
GV *gv =
#ifdef USE_ITHREADS
MUTABLE_GV(PAD_SVl(pm->op_pmreplrootu.op_pmtargetoff));
#else
pm->op_pmreplrootu.op_pmtargetgv;
#endif
if (PL_op->op_private & OPpLVAL_INTRO)
ary = save_ary(gv);
else
ary = GvAVn(gv);
}
/* skip anything pushed by OPpLVAL_INTRO above */
oldsave = PL_savestack_ix;
}
/* Some defence against stack-not-refcounted bugs */
(void)sv_2mortal(SvREFCNT_inc_simple_NN(ary));
if ((mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied))) {
PUSHMARK(SP);
XPUSHs(SvTIED_obj(MUTABLE_SV(ary), mg));
} else {
flags &= ~SVs_TEMP; /* SVs will not be mortal */
}
}
base = SP - PL_stack_base;
orig = s;
if (RX_EXTFLAGS(rx) & RXf_SKIPWHITE) {
if (do_utf8) {
while (s < strend && isSPACE_utf8_safe(s, strend))
s += UTF8SKIP(s);
}
else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET) {
while (s < strend && isSPACE_LC(*s))
s++;
}
else if (in_uni_8_bit) {
while (s < strend && isSPACE_L1(*s))
s++;
}
else {
while (s < strend && isSPACE(*s))
s++;
}
}
gimme_scalar = gimme == G_SCALAR && !ary;
if (!limit)
limit = maxiters + 2;
if (RX_EXTFLAGS(rx) & RXf_WHITE) {
while (--limit) {
m = s;
/* this one uses 'm' and is a negative test */
if (do_utf8) {
while (m < strend && ! isSPACE_utf8_safe(m, strend) ) {
const int t = UTF8SKIP(m);
/* isSPACE_utf8_safe returns FALSE for malform utf8 */
if (strend - m < t)
m = strend;
else
m += t;
}
}
else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET)
{
while (m < strend && !isSPACE_LC(*m))
++m;
}
else if (in_uni_8_bit) {
while (m < strend && !isSPACE_L1(*m))
++m;
} else {
while (m < strend && !isSPACE(*m))
++m;
}
if (m >= strend)
break;
if (gimme_scalar) {
iters++;
if (m-s == 0)
trailing_empty++;
else
trailing_empty = 0;
} else {
dstr = newSVpvn_flags(s, m-s, flags);
XPUSHs(dstr);
}
/* skip the whitespace found last */
if (do_utf8)
s = m + UTF8SKIP(m);
else
s = m + 1;
/* this one uses 's' and is a positive test */
if (do_utf8) {
while (s < strend && isSPACE_utf8_safe(s, strend) )
s += UTF8SKIP(s);
}
else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET)
{
while (s < strend && isSPACE_LC(*s))
++s;
}
else if (in_uni_8_bit) {
while (s < strend && isSPACE_L1(*s))
++s;
} else {
while (s < strend && isSPACE(*s))
++s;
}
}
}
else if (RX_EXTFLAGS(rx) & RXf_START_ONLY) {
while (--limit) {
for (m = s; m < strend && *m != '\n'; m++)
;
m++;
if (m >= strend)
break;
if (gimme_scalar) {
iters++;
if (m-s == 0)
trailing_empty++;
else
trailing_empty = 0;
} else {
dstr = newSVpvn_flags(s, m-s, flags);
XPUSHs(dstr);
}
s = m;
}
}
else if (RX_EXTFLAGS(rx) & RXf_NULL && !(s >= strend)) {
/* This case boils down to deciding which is the smaller of:
* limit - effectively a number of characters
* slen - which already contains the number of characters in s
*
* The resulting number is the number of iters (for gimme_scalar)
* or the number of SVs to create (!gimme_scalar). */
/* setting it to -1 will trigger a panic in EXTEND() */
const SSize_t sslen = slen > SSize_t_MAX ? -1 : (SSize_t)slen;
const IV items = limit - 1;
if (sslen < items || items < 0) {
iters = slen -1;
limit = slen + 1;
/* Note: The same result is returned if the following block
* is removed, because of the "keep field after final delim?"
* adjustment, but having the following makes the "correct"
* behaviour more apparent. */
if (gimme_scalar) {
s = strend;
iters++;
}
} else {
iters = items;
}
if (!gimme_scalar) {
/*
Pre-extend the stack, either the number of bytes or
characters in the string or a limited amount, triggered by:
my ($x, $y) = split //, $str;
or
split //, $str, $i;
*/
EXTEND(SP, limit);
if (do_utf8) {
while (--limit) {
m = s;
s += UTF8SKIP(s);
dstr = newSVpvn_flags(m, s-m, flags);
PUSHs(dstr);
}
} else {
while (--limit) {
dstr = newSVpvn_flags(s, 1, flags);
PUSHs(dstr);
s++;
}
}
}
}
else if (do_utf8 == (RX_UTF8(rx) != 0) &&
(RX_EXTFLAGS(rx) & RXf_USE_INTUIT) && !RX_NPARENS(rx)
&& (RX_EXTFLAGS(rx) & RXf_CHECK_ALL)
&& !(RX_EXTFLAGS(rx) & RXf_IS_ANCHORED)) {
const int tail = (RX_EXTFLAGS(rx) & RXf_INTUIT_TAIL);
SV * const csv = CALLREG_INTUIT_STRING(rx);
len = RX_MINLENRET(rx);
if (len == 1 && !RX_UTF8(rx) && !tail) {
const char c = *SvPV_nolen_const(csv);
while (--limit) {
for (m = s; m < strend && *m != c; m++)
;
if (m >= strend)
break;
if (gimme_scalar) {
iters++;
if (m-s == 0)
trailing_empty++;
else
trailing_empty = 0;
} else {
dstr = newSVpvn_flags(s, m-s, flags);
XPUSHs(dstr);
}
/* The rx->minlen is in characters but we want to step
* s ahead by bytes. */
if (do_utf8)
s = (char*)utf8_hop_forward((U8*) m, len, (U8*) strend);
else
s = m + len; /* Fake \n at the end */
}
}
else {
const bool multiline = (RX_EXTFLAGS(rx) & RXf_PMf_MULTILINE) ? 1 : 0;
while (s < strend && --limit &&
(m = fbm_instr((unsigned char*)s, (unsigned char*)strend,
csv, multiline ? FBMrf_MULTILINE : 0)) )
{
if (gimme_scalar) {
iters++;
if (m-s == 0)
trailing_empty++;
else
trailing_empty = 0;
} else {
dstr = newSVpvn_flags(s, m-s, flags);
XPUSHs(dstr);
}
/* The rx->minlen is in characters but we want to step
* s ahead by bytes. */
if (do_utf8)
s = (char*)utf8_hop_forward((U8*)m, len, (U8 *) strend);
else
s = m + len; /* Fake \n at the end */
}
}
}
else {
maxiters += slen * RX_NPARENS(rx);
while (s < strend && --limit)
{
I32 rex_return;
PUTBACK;
rex_return = CALLREGEXEC(rx, (char*)s, (char*)strend, (char*)orig, 1,
sv, NULL, 0);
SPAGAIN;
if (rex_return == 0)
break;
TAINT_IF(RX_MATCH_TAINTED(rx));
/* we never pass the REXEC_COPY_STR flag, so it should
* never get copied */
assert(!RX_MATCH_COPIED(rx));
m = RX_OFFS_START(rx,0) + orig;
if (gimme_scalar) {
iters++;
if (m-s == 0)
trailing_empty++;
else
trailing_empty = 0;
} else {
dstr = newSVpvn_flags(s, m-s, flags);
XPUSHs(dstr);
}
if (RX_NPARENS(rx)) {
I32 i;
for (i = 1; i <= (I32)RX_NPARENS(rx); i++) {
s = orig + RX_OFFS_START(rx,i);
m = orig + RX_OFFS_END(rx,i);
/* japhy (07/27/01) -- the (m && s) test doesn't catch
parens that didn't match -- they should be set to
undef, not the empty string */
if (gimme_scalar) {
iters++;
if (m-s == 0)
trailing_empty++;
else
trailing_empty = 0;
} else {
if (m >= orig && s >= orig) {
dstr = newSVpvn_flags(s, m-s, flags);
}
else
dstr = &PL_sv_undef; /* undef, not "" */
XPUSHs(dstr);
}
}
}
s = RX_OFFS_END(rx,0) + orig;
}
}
if (!gimme_scalar) {
iters = (SP - PL_stack_base) - base;
}
if (iters > maxiters)
DIE(aTHX_ "Split loop");
/* keep field after final delim? */
if (s < strend || (iters && origlimit)) {
if (!gimme_scalar) {
const STRLEN l = strend - s;
dstr = newSVpvn_flags(s, l, flags);
XPUSHs(dstr);
}
iters++;
}
else if (!origlimit) {
if (gimme_scalar) {
iters -= trailing_empty;
} else {
while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0)) {
if (TOPs && !(flags & SVs_TEMP))
sv_2mortal(TOPs);
*SP-- = NULL;
iters--;
}
}
}
PUTBACK;
LEAVE_SCOPE(oldsave);
SPAGAIN;
if (realarray) {
if (!mg) {
PUTBACK;
if(AvREAL(ary)) {
if (av_count(ary) > 0)
av_clear(ary);
} else {
AvREAL_on(ary);
AvREIFY_off(ary);
if (AvMAX(ary) > -1) {
/* don't free mere refs */
Zero(AvARRAY(ary), AvMAX(ary), SV*);
}
}
if(AvMAX(ary) < iters)
av_extend(ary,iters);
SPAGAIN;
/* Need to copy the SV*s from the stack into ary */
Copy(SP + 1 - iters, AvARRAY(ary), iters, SV*);
AvFILLp(ary) = iters - 1;
if (SvSMAGICAL(ary)) {
PUTBACK;
mg_set(MUTABLE_SV(ary));
SPAGAIN;
}
if (gimme != G_LIST) {
/* SP points to the final SV* pushed to the stack. But the SV* */
/* are not going to be used from the stack. Point SP to below */
/* the first of these SV*. */
SP -= iters;
PUTBACK;
}
}
else {
PUTBACK;
av_extend(ary,iters);
av_clear(ary);
ENTER_with_name("call_PUSH");
call_sv(SV_CONST(PUSH),G_SCALAR|G_DISCARD|G_METHOD_NAMED);
LEAVE_with_name("call_PUSH");
SPAGAIN;
if (gimme == G_LIST) {
SSize_t i;
/* EXTEND should not be needed - we just popped them */
EXTEND_SKIP(SP, iters);
for (i=0; i < iters; i++) {
SV **svp = av_fetch(ary, i, FALSE);
PUSHs((svp) ? *svp : &PL_sv_undef);
}
RETURN;
}
}
}
if (gimme != G_LIST) {
GETTARGET;
XPUSHi(iters);
}
RETURN;
}
PP(pp_once)
{
SV *const sv = PAD_SVl(PL_op->op_targ);
if (SvPADSTALE(sv)) {
/* First time. */
SvPADSTALE_off(sv);
return cLOGOP->op_other;
}
return cLOGOP->op_next;
}
PP(pp_lock)
{
SV *sv = *PL_stack_sp;
SV *retsv = sv;
SvLOCK(sv);
if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV
|| SvTYPE(retsv) == SVt_PVCV) {
retsv = refto(retsv);
}
rpp_replace_1_1_NN(retsv);
return NORMAL;
}
/* used for: pp_padany(), pp_custom(); plus any system ops
* that aren't implemented on a particular platform */
PP(unimplemented_op)
{
const Optype op_type = PL_op->op_type;
/* Using OP_NAME() isn't going to be helpful here. Firstly, it doesn't cope
with out of range op numbers - it only "special" cases op_custom.
Secondly, as the three ops we "panic" on are padmy, mapstart and custom,
if we get here for a custom op then that means that the custom op didn't
have an implementation. Given that OP_NAME() looks up the custom op
by its op_ppaddr, likely it will return NULL, unless someone (unhelpfully)
registers &Perl_unimplemented_op as the address of their custom op.
NULL doesn't generate a useful error message. "custom" does. */
const char *const name = op_type >= OP_max
? "[out of range]" : PL_op_name[op_type];
if(OP_IS_SOCKET(op_type))
DIE(aTHX_ PL_no_sock_func, name);
DIE(aTHX_ "panic: unimplemented op %s (#%d) called", name, op_type);
}
static void
S_maybe_unwind_defav(pTHX)
{
if (CX_CUR()->cx_type & CXp_HASARGS) {
PERL_CONTEXT *cx = CX_CUR();
assert(CxHASARGS(cx));
cx_popsub_args(cx);
cx->cx_type &= ~CXp_HASARGS;
}
}
/* For sorting out arguments passed to a &CORE:: subroutine */
PP_wrapped(pp_coreargs, 0, 0)
{
dSP;
int opnum = SvIOK(cSVOP_sv) ? (int)SvUV(cSVOP_sv) : 0;
int defgv = PL_opargs[opnum] & OA_DEFGV ||opnum==OP_GLOB, whicharg = 0;
AV * const at_ = GvAV(PL_defgv);
SV **svp = at_ ? AvARRAY(at_) : NULL;
I32 minargs = 0, maxargs = 0, numargs = at_ ? AvFILLp(at_)+1 : 0;
I32 oa = opnum ? PL_opargs[opnum] >> OASHIFT : 0;
bool seen_question = 0;
const char *err = NULL;
const bool pushmark = PL_op->op_private & OPpCOREARGS_PUSHMARK;
/* Count how many args there are first, to get some idea how far to
extend the stack. */
while (oa) {
if ((oa & 7) == OA_LIST) { maxargs = I32_MAX; break; }
maxargs++;
if (oa & OA_OPTIONAL) seen_question = 1;
if (!seen_question) minargs++;
oa >>= 4;
}
if(numargs < minargs) err = "Not enough";
else if(numargs > maxargs) err = "Too many";
if (err)
/* diag_listed_as: Too many arguments for %s */
croak(
"%s arguments for %s", err,
opnum ? PL_op_desc[opnum] : SvPV_nolen_const(cSVOP_sv)
);
/* Reset the stack pointer. Without this, we end up returning our own
arguments in list context, in addition to the values we are supposed
to return. nextstate usually does this on sub entry, but we need
to run the next op with the caller's hints, so we cannot have a
nextstate. */
SP = PL_stack_base + CX_CUR()->blk_oldsp;
if(!maxargs) RETURN;
/* We do this here, rather than with a separate pushmark op, as it has
to come in between two things this function does (stack reset and
arg pushing). This seems the easiest way to do it. */
if (pushmark) {
PUSHMARK(SP);
}
EXTEND(SP, maxargs == I32_MAX ? numargs : maxargs);
PUTBACK; /* The code below can die in various places. */
oa = PL_opargs[opnum] >> OASHIFT;
for (; oa&&(numargs||!pushmark); (void)(numargs&&(++svp,--numargs))) {
whicharg++;
switch (oa & 7) {
case OA_SCALAR:
try_defsv:
if (!numargs && defgv && whicharg == minargs + 1) {
PUSHs(DEFSV);
}
else PUSHs(numargs ? svp && *svp ? *svp : &PL_sv_undef : NULL);
break;
case OA_LIST:
while (numargs--) {
PUSHs(svp && *svp ? *svp : &PL_sv_undef);
svp++;
}
RETURN;
case OA_AVREF:
if (!numargs) {
GV *gv;
if (CvUNIQUE(find_runcv_where(FIND_RUNCV_level_eq,1,NULL)))
gv = PL_argvgv;
else {
S_maybe_unwind_defav(aTHX);
gv = PL_defgv;
}
PUSHs((SV *)GvAVn(gv));
break;
}
if (!svp || !*svp || !SvROK(*svp)
|| SvTYPE(SvRV(*svp)) != SVt_PVAV)
DIE(aTHX_
/* diag_listed_as: Type of arg %d to &CORE::%s must be %s*/
"Type of arg %d to &CORE::%s must be array reference",
whicharg, PL_op_desc[opnum]
);
PUSHs(SvRV(*svp));
break;
case OA_HVREF:
if (!svp || !*svp || !SvROK(*svp)
|| ( SvTYPE(SvRV(*svp)) != SVt_PVHV
&& ( opnum == OP_DBMCLOSE || opnum == OP_DBMOPEN
|| SvTYPE(SvRV(*svp)) != SVt_PVAV )))
DIE(aTHX_
/* diag_listed_as: Type of arg %d to &CORE::%s must be %s*/
"Type of arg %d to &CORE::%s must be hash%s reference",
whicharg, PL_op_desc[opnum],
opnum == OP_DBMCLOSE || opnum == OP_DBMOPEN
? ""
: " or array"
);
PUSHs(SvRV(*svp));
break;
case OA_FILEREF:
if (!numargs) PUSHs(NULL);
else if(svp && *svp && SvROK(*svp) && isGV_with_GP(SvRV(*svp)))
/* no magic here, as the prototype will have added an extra
refgen and we just want what was there before that */
PUSHs(SvRV(*svp));
else {
const bool constr = PL_op->op_private & whicharg;
PUSHs(S_rv2gv(aTHX_
svp && *svp ? *svp : &PL_sv_undef,
constr, cBOOL(CopHINTS_get(PL_curcop) & HINT_STRICT_REFS),
!constr
));
}
break;
case OA_SCALARREF:
if (!numargs) goto try_defsv;
else {
const bool wantscalar =
PL_op->op_private & OPpCOREARGS_SCALARMOD;
if (!svp || !*svp || !SvROK(*svp)
/* We have to permit globrefs even for the \$ proto, as
*foo is indistinguishable from ${\*foo}, and the proto-
type permits the latter. */
|| SvTYPE(SvRV(*svp)) > (
wantscalar ? SVt_PVLV
: opnum == OP_LOCK || opnum == OP_UNDEF
? SVt_PVCV
: SVt_PVHV
)
)
DIE(aTHX_
"Type of arg %d to &CORE::%s must be %s",
whicharg, PL_op_name[opnum],
wantscalar
? "scalar reference"
: opnum == OP_LOCK || opnum == OP_UNDEF
? "reference to one of [$@%&*]"
: "reference to one of [$@%*]"
);
PUSHs(SvRV(*svp));
if (opnum == OP_UNDEF && SvRV(*svp) == (SV *)PL_defgv) {
/* Undo @_ localisation, so that sub exit does not undo
part of our undeffing. */
S_maybe_unwind_defav(aTHX);
}
}
break;
default:
DIE(aTHX_ "panic: unknown OA_*: %x", (unsigned)(oa&7));
}
oa = oa >> 4;
}
RETURN;
}
/* Implement CORE::keys(),values(),each().
*
* We won't know until run-time whether the arg is an array or hash,
* so this op calls
*
* pp_keys/pp_values/pp_each
* or
* pp_akeys/pp_avalues/pp_aeach
*
* as appropriate (or whatever pp function actually implements the OP_FOO
* functionality for each FOO).
*/
PP(pp_avhvswitch)
{
return PL_ppaddr[
(SvTYPE(*PL_stack_sp) == SVt_PVAV ? OP_AEACH : OP_EACH)
+ (PL_op->op_private & OPpAVHVSWITCH_MASK)
](aTHX);
}
PP(pp_runcv)
{
CV *cv;
if (PL_op->op_private & OPpOFFBYONE) {
cv = find_runcv_where(FIND_RUNCV_level_eq, 1, NULL);
}
else cv = find_runcv(NULL);
rpp_extend(1);
if (CvEVAL(cv))
rpp_push_IMM(&PL_sv_undef);
else
rpp_push_1_norc(newRV((SV *)cv));
return NORMAL;
}
static void
S_localise_aelem_lval(pTHX_ AV * const av, SV * const keysv,
const bool can_preserve)
{
const SSize_t ix = SvIV(keysv);
if (can_preserve ? av_exists(av, ix) : TRUE) {
SV ** const svp = av_fetch(av, ix, 1);
if (!svp || !*svp)
croak(PL_no_aelem, ix);
save_aelem(av, ix, svp);
}
else
SAVEADELETE(av, ix);
}
static void
S_localise_helem_lval(pTHX_ HV * const hv, SV * const keysv,
const bool can_preserve)
{
if (can_preserve ? hv_exists_ent(hv, keysv, 0) : TRUE) {
HE * const he = hv_fetch_ent(hv, keysv, 1, 0);
SV ** const svp = he ? &HeVAL(he) : NULL;
if (!svp || !*svp)
croak(PL_no_helem_sv, SVfARG(keysv));
save_helem_flags(hv, keysv, svp, 0);
}
else
SAVEHDELETE(hv, keysv);
}
static void
S_localise_gv_slot(pTHX_ GV *gv, U8 type)
{
if (type == OPpLVREF_SV) {
save_pushptrptr(gv, SvREFCNT_inc_simple(GvSV(gv)), SAVEt_GVSV);
GvSV(gv) = 0;
}
else if (type == OPpLVREF_AV)
/* XXX Inefficient, as it creates a new AV, which we are
about to clobber. */
save_ary(gv);
else {
assert(type == OPpLVREF_HV);
/* XXX Likewise inefficient. */
save_hash(gv);
}
}
PP(pp_refassign)
{
SV *key = NULL;
SV *left = NULL;
SSize_t extra = 0;
/* \$a[key] = ...; or \$h{key} = ...; */
if (PL_op->op_private & OPpLVREF_ELEM) {
key = PL_stack_sp[0];
extra++;
}
/* \X = ...; rather than \my X = ...; so X on stack */
if (PL_op->op_flags & OPf_STACKED) {
left = PL_stack_sp[-extra];
extra++;
}
SV *sv = PL_stack_sp[-extra];
const char *bad = NULL;
const U8 type = PL_op->op_private & OPpLVREF_TYPE;
if (!SvROK(sv)) DIE(aTHX_ "Assigned value is not a reference");
switch (type) {
case OPpLVREF_SV:
if (SvTYPE(SvRV(sv)) > SVt_PVLV)
bad = " SCALAR";
break;
case OPpLVREF_AV:
if (SvTYPE(SvRV(sv)) != SVt_PVAV)
bad = "n ARRAY";
break;
case OPpLVREF_HV:
if (SvTYPE(SvRV(sv)) != SVt_PVHV)
bad = " HASH";
break;
case OPpLVREF_CV:
if (SvTYPE(SvRV(sv)) != SVt_PVCV)
bad = " CODE";
}
if (bad)
/* diag_listed_as: Assigned value is not %s reference */
DIE(aTHX_ "Assigned value is not a%s reference", bad);
switch (left ? SvTYPE(left) : 0) {
case 0:
{
SV * const old = PAD_SV(ARGTARG);
PAD_SETSV(ARGTARG, SvREFCNT_inc_NN(SvRV(sv)));
SvREFCNT_dec(old);
if ((PL_op->op_private & (OPpLVAL_INTRO|OPpPAD_STATE))
== OPpLVAL_INTRO)
SAVECLEARSV(PAD_SVl(ARGTARG));
break;
}
case SVt_PVGV:
if (PL_op->op_private & OPpLVAL_INTRO) {
S_localise_gv_slot(aTHX_ (GV *)left, type);
}
gv_setref(left, sv);
SvSETMAGIC(left);
break;
case SVt_PVAV:
assert(key);
if (UNLIKELY(PL_op->op_private & OPpLVAL_INTRO)) {
S_localise_aelem_lval(aTHX_ (AV *)left, key,
SvCANEXISTDELETE(left));
}
av_store((AV *)left, SvIV(key), SvREFCNT_inc_simple_NN(SvRV(sv)));
break;
case SVt_PVHV:
if (UNLIKELY(PL_op->op_private & OPpLVAL_INTRO)) {
assert(key);
S_localise_helem_lval(aTHX_ (HV *)left, key,
SvCANEXISTDELETE(left));
}
(void)hv_store_ent((HV *)left, key, SvREFCNT_inc_simple_NN(SvRV(sv)), 0);
}
if (UNLIKELY(PL_op->op_flags & OPf_MOD)) {
/* e.g. f(\$x = \1); */
rpp_popfree_to_NN(PL_stack_sp - extra);
rpp_replace_at_norc(PL_stack_sp, newSVsv(sv));
/* XXX else can weak references go stale before they are read, e.g.,
in leavesub? */
}
else
rpp_popfree_to_NN(PL_stack_sp - (extra + cBOOL(GIMME_V == G_VOID)));
return NORMAL;
}
PP_wrapped(pp_lvref,
!!(PL_op->op_private & OPpLVREF_ELEM) + !!(PL_op->op_flags & OPf_STACKED),
0)
{
dSP;
SV * const ret = newSV_type_mortal(SVt_PVMG);
SV * const elem = PL_op->op_private & OPpLVREF_ELEM ? POPs : NULL;
SV * const arg = PL_op->op_flags & OPf_STACKED ? POPs : NULL;
MAGIC * const mg = sv_magicext(ret, arg, PERL_MAGIC_lvref,
&PL_vtbl_lvref, (char *)elem,
elem ? HEf_SVKEY : (I32)ARGTARG);
mg->mg_private = PL_op->op_private;
if (UNLIKELY(PL_op->op_private & OPpLVAL_INTRO)) {
if (elem) {
assert(arg);
{
const bool can_preserve = SvCANEXISTDELETE(arg);
if (SvTYPE(arg) == SVt_PVAV)
S_localise_aelem_lval(aTHX_ (AV *)arg, elem, can_preserve);
else
S_localise_helem_lval(aTHX_ (HV *)arg, elem, can_preserve);
}
}
else if (arg) {
S_localise_gv_slot(aTHX_ (GV *)arg,
PL_op->op_private & OPpLVREF_TYPE);
}
else if (!(PL_op->op_private & OPpPAD_STATE))
SAVECLEARSV(PAD_SVl(ARGTARG));
}
XPUSHs(ret);
RETURN;
}
PP_wrapped(pp_lvrefslice, 0, 1)
{
dSP; dMARK;
AV * const av = (AV *)POPs;
const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
bool can_preserve = FALSE;
if (UNLIKELY(localizing)) {
SV **svp;
can_preserve = SvCANEXISTDELETE(av);
if (SvTYPE(av) == SVt_PVAV) {
SSize_t max = -1;
for (svp = MARK + 1; svp <= SP; svp++) {
const SSize_t elem = SvIV(*svp);
if (elem > max)
max = elem;
}
if (max > AvMAX(av))
av_extend(av, max);
}
}
while (++MARK <= SP) {
SV * const elemsv = *MARK;
if (UNLIKELY(localizing)) {
if (SvTYPE(av) == SVt_PVAV)
S_localise_aelem_lval(aTHX_ av, elemsv, can_preserve);
else
S_localise_helem_lval(aTHX_ (HV *)av, elemsv, can_preserve);
}
*MARK = newSV_type_mortal(SVt_PVMG);
sv_magic(*MARK,(SV *)av,PERL_MAGIC_lvref,(char *)elemsv,HEf_SVKEY);
}
RETURN;
}
PP(pp_lvavref)
{
if (PL_op->op_flags & OPf_STACKED)
Perl_pp_rv2av(aTHX);
else
Perl_pp_padav(aTHX);
{
/* shift the return value up one and insert below it a special
* alias marker that aassign recognises */
rpp_extend(1);
PL_stack_sp[1] = PL_stack_sp[0];
PL_stack_sp[0] = NULL;
PL_stack_sp++;
return NORMAL;
}
}
PP(pp_anonconst)
{
SV *sv = *PL_stack_sp;
CV* constsub = newCONSTSUB(
SvTYPE(CopSTASH(PL_curcop))==SVt_PVHV ? CopSTASH(PL_curcop) : NULL,
NULL,
SvREFCNT_inc_simple_NN(sv)
);
SV* ret_sv = sv_2mortal((SV *)constsub);
/* Prior to Perl 5.38 anonconst ops always fed into srefgen.
5.38 redefined anonconst to create the reference without srefgen.
OPf_REF was added to the op. In case some XS code out there creates
anonconst the old way, we accommodate OPf_REF's absence here.
*/
if (LIKELY(PL_op->op_flags & OPf_REF)) {
ret_sv = refto(ret_sv);
}
rpp_replace_1_1_NN(ret_sv);
return NORMAL;
}
/* process one subroutine argument - typically when the sub has a signature:
* introduce PL_curpad[op_targ] and assign to it the value
* for $: (OPf_STACKED ? *sp : $_[N])
* for @/%: @_[N..$#_]
*
* It's equivalent to
* my $foo = $_[N];
* or
* my $foo = (value-on-stack)
* or
* my @foo = @_[N..$#_]
* etc
*/
PP_wrapped(pp_argelem,
!!( (PL_op->op_private & OPpARGELEM_MASK) == OPpARGELEM_SV
&& (PL_op->op_flags & OPf_STACKED)),
0)
{
dTARG;
SV *val;
SV ** padentry;
OP *o = PL_op;
AV *defav = GvAV(PL_defgv); /* @_ */
IV ix = PTR2IV(cUNOP_AUXo->op_aux);
IV argc;
/* do 'my $var, @var or %var' action */
padentry = &(PAD_SVl(o->op_targ));
save_clearsv(padentry);
targ = *padentry;
if ((o->op_private & OPpARGELEM_MASK) == OPpARGELEM_SV) {
if (o->op_flags & OPf_STACKED) {
dSP;
val = POPs;
PUTBACK;
}
else {
SV **svp;
/* should already have been checked */
assert(ix >= 0);
#if IVSIZE > PTRSIZE
assert(ix <= SSize_t_MAX);
#endif
svp = av_fetch(defav, ix, FALSE);
val = svp ? *svp : &PL_sv_undef;
}
/* $var = $val */
/* cargo-culted from pp_sassign */
assert(TAINTING_get || !TAINT_get);
if (UNLIKELY(TAINT_get) && !SvTAINTED(val))
TAINT_NOT;
SvSetMagicSV(targ, val);
return o->op_next;
}
/* must be AV or HV */
assert(!(o->op_flags & OPf_STACKED));
argc = ((IV)AvFILL(defav) + 1) - ix;
/* This is a copy of the relevant parts of pp_aassign().
*/
if ((o->op_private & OPpARGELEM_MASK) == OPpARGELEM_AV) {
IV i;
if (AvFILL((AV*)targ) > -1) {
/* target should usually be empty. If we get get
* here, someone's been doing some weird closure tricks.
* Make a copy of all args before clearing the array,
* to avoid the equivalent of @a = ($a[0]) prematurely freeing
* elements. See similar code in pp_aassign.
*/
for (i = 0; i < argc; i++) {
SV **svp = av_fetch(defav, ix + i, FALSE);
SV *newsv = newSVsv_flags(svp ? *svp : &PL_sv_undef,
(SV_DO_COW_SVSETSV|SV_NOSTEAL));
if (!av_store(defav, ix + i, newsv))
SvREFCNT_dec_NN(newsv);
}
av_clear((AV*)targ);
}
if (argc <= 0)
return o->op_next;
av_extend((AV*)targ, argc);
i = 0;
while (argc--) {
SV **svp = av_fetch(defav, ix + i, FALSE);
SV *val = svp ? *svp : &PL_sv_undef;
SV *tmpsv = newSVsv_flags(val, SV_GMAGIC|SV_DO_COW_SVSETSV);
av_store((AV*)targ, i++, tmpsv);
TAINT_NOT;
}
}
else {
IV i;
assert((o->op_private & OPpARGELEM_MASK) == OPpARGELEM_HV);
if (SvRMAGICAL(targ) || HvUSEDKEYS((HV*)targ)) {
/* see "target should usually be empty" comment above */
for (i = 0; i < argc; i++) {
SV **svp = av_fetch(defav, ix + i, FALSE);
SV *newsv = newSVsv_flags(svp ? *svp : &PL_sv_undef,
(SV_DO_COW_SVSETSV|SV_NOSTEAL));
if (!av_store(defav, ix + i, newsv))
SvREFCNT_dec_NN(newsv);
}
hv_clear((HV*)targ);
}
if (argc <= 0)
return o->op_next;
assert(argc % 2 == 0);
i = 0;
while (argc) {
SV **svp = av_fetch(defav, ix + i++, FALSE);
SV *key = svp ? *svp : &PL_sv_undef;
svp = av_fetch(defav, ix + i++, FALSE);
SV *val = svp ? *svp : &PL_sv_undef;
argc -= 2;
if (UNLIKELY(SvGMAGICAL(key)))
key = sv_mortalcopy(key);
SV *tmpsv = newSVsv_flags(val, SV_GMAGIC|SV_DO_COW_SVSETSV);
hv_store_ent((HV*)targ, key, tmpsv, 0);
TAINT_NOT;
}
}
return o->op_next;
}
/* Handle a default value for one subroutine argument (typically as part
* of a subroutine signature).
* It's equivalent to
* @_ > op_targ ? $_[op_targ] : result_of(op_other)
*
* Intended to be used where op_next is an OP_ARGELEM
*
* We abuse the op_targ field slightly: it's an index into @_ rather than
* into PL_curpad.
*/
PP(pp_argdefelem)
{
OP * const o = PL_op;
AV *defav = GvAV(PL_defgv); /* @_ */
IV ix = (IV)o->op_targ;
assert(ix >= 0);
#if IVSIZE > PTRSIZE
assert(ix <= SSize_t_MAX);
#endif
if (AvFILL(defav) < ix)
return cLOGOPo->op_other;
SV **svp = av_fetch(defav, ix, FALSE);
SV *val = svp ? *svp : &PL_sv_undef;
if ((PL_op->op_private & OPpARG_IF_UNDEF) && !SvOK(val))
return cLOGOPo->op_other;
if ((PL_op->op_private & OPpARG_IF_FALSE) && !SvTRUE(val))
return cLOGOPo->op_other;
rpp_xpush_1(val);
return NORMAL;
}
static SV *
S_find_runcv_name(void)
{
dTHX;
CV *cv;
GV *gv;
SV *sv;
cv = find_runcv(0);
if (!cv)
return &PL_sv_no;
gv = CvGV(cv);
if (!gv)
return &PL_sv_no;
sv = sv_newmortal();
gv_fullname4(sv, gv, NULL, TRUE);
return sv;
}
/* Check a sub's arguments - i.e. that it has the correct number of args
* (and anything else we might think of in future). Typically used with
* signatured subs.
*/
PP(pp_argcheck)
{
OP * const o = PL_op;
struct op_argcheck_aux *aux = (struct op_argcheck_aux *)cUNOP_AUXo->op_aux;
UV params = aux->params;
UV opt_params = aux->opt_params;
char slurpy = aux->slurpy;
AV *defav = GvAV(PL_defgv); /* @_ */
UV argc;
bool too_few;
assert(!SvMAGICAL(defav));
argc = (UV)(AvFILLp(defav) + 1);
too_few = (argc < (params - opt_params));
if (UNLIKELY(too_few || (!slurpy && argc > params)))
/* diag_listed_as: Too few arguments for subroutine '%s' (got %d; expected %d) */
/* diag_listed_as: Too few arguments for subroutine '%s' (got %d; expected at least %d) */
/* diag_listed_as: Too many arguments for subroutine '%s' (got %d; expected %d) */
/* diag_listed_as: Too many arguments for subroutine '%s' (got %d; expected at most %d)*/
Perl_croak_caller("Too %s arguments for subroutine '%" SVf "' (got %" UVuf "; expected %s%" UVuf ")",
too_few ? "few" : "many",
S_find_runcv_name(),
argc,
too_few ? (slurpy || opt_params ? "at least " : "") : (opt_params ? "at most " : ""),
too_few ? (params - opt_params) : params);
if (UNLIKELY(slurpy == '%' && argc > params && (argc - params) % 2))
/* diag_listed_as: Odd name/value argument for subroutine '%s' */
Perl_croak_caller("Odd name/value argument for subroutine '%" SVf "'",
S_find_runcv_name());
return NORMAL;
}
PP_wrapped(pp_isa, 2, 0)
{
dSP;
SV *left, *right;
right = POPs;
left = TOPs;
SETs(boolSV(sv_isa_sv(left, right)));
RETURN;
}
PP(pp_cmpchain_and)
{
SV *result = PL_stack_sp[0];
if (SvTRUE_NN(result)) {
rpp_popfree_1_NN();
return cLOGOP->op_other;
} else {
rpp_replace_2_1_NN(result);
return NORMAL;
}
}
PP(pp_cmpchain_dup)
{
SV *right = PL_stack_sp[0];
SV *left = PL_stack_sp[-1];
PL_stack_sp[-1] = right;
PL_stack_sp[0] = left;
rpp_xpush_1(right);
return NORMAL;
}
PP(pp_is_bool)
{
SV *arg = *PL_stack_sp;
SvGETMAGIC(arg);
rpp_replace_1_IMM_NN(boolSV(SvIsBOOL(arg)));
return NORMAL;
}
PP(pp_is_weak)
{
SV *arg = *PL_stack_sp;
SvGETMAGIC(arg);
rpp_replace_1_IMM_NN(boolSV(SvWEAKREF(arg)));
return NORMAL;
}
PP(pp_weaken)
{
sv_rvweaken(*PL_stack_sp);
rpp_popfree_1_NN();
return NORMAL;
}
PP(pp_unweaken)
{
sv_rvunweaken(*PL_stack_sp);
rpp_popfree_1_NN();
return NORMAL;
}
PP(pp_blessed)
{
SV *arg = *PL_stack_sp;
SV *rv, *ret;
SvGETMAGIC(arg);
if(!SvROK(arg) || !SvOBJECT((rv = SvRV(arg)))) {
ret = &PL_sv_undef;
goto ret;
}
if((PL_op->op_private & OPpTRUEBOOL) ||
((PL_op->op_private & OPpMAYBE_TRUEBOOL) && (block_gimme() == G_VOID))) {
/* We only care about the boolean truth, not the specific string value.
* We just have to check for the annoying cornercase of the package
* named "0" */
HV *stash = SvSTASH(rv);
HEK *hek = HvNAME_HEK(stash);
if(!hek)
goto fallback;
I32 len = HEK_LEN(hek);
if(UNLIKELY(len == HEf_SVKEY || (len == 1 && HEK_KEY(hek)[0] == '0')))
goto fallback;
ret = &PL_sv_yes;
goto ret;
}
else {
fallback:
ret = (sv_ref(NULL, rv, TRUE));
}
ret:
rpp_replace_1_1_NN(ret);
return NORMAL;
}
PP(pp_is_tainted)
{
SV *arg = *PL_stack_sp;
SvGETMAGIC(arg);
rpp_replace_1_IMM_NN(boolSV(SvTAINTED(arg)));
return NORMAL;
}
/*
* ex: set ts=8 sts=4 sw=4 et:
*/
|