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
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* vim: set ts=8 sw=4 et tw=78:
*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Communicator client code, released
* March 31, 1998.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/*
* JS bytecode generation.
*/
#include "jsstddef.h"
#ifdef HAVE_MEMORY_H
#include <memory.h>
#endif
#include <string.h>
#include "jstypes.h"
#include "jsarena.h" /* Added by JSIFY */
#include "jsutil.h" /* Added by JSIFY */
#include "jsbit.h"
#include "jsprf.h"
#include "jsapi.h"
#include "jsatom.h"
#include "jscntxt.h"
#include "jsconfig.h"
#include "jsemit.h"
#include "jsfun.h"
#include "jsnum.h"
#include "jsopcode.h"
#include "jsparse.h"
#include "jsregexp.h"
#include "jsscan.h"
#include "jsscope.h"
#include "jsscript.h"
/* Allocation chunk counts, must be powers of two in general. */
#define BYTECODE_CHUNK 256 /* code allocation increment */
#define SRCNOTE_CHUNK 64 /* initial srcnote allocation increment */
#define TRYNOTE_CHUNK 64 /* trynote allocation increment */
/* Macros to compute byte sizes from typed element counts. */
#define BYTECODE_SIZE(n) ((n) * sizeof(jsbytecode))
#define SRCNOTE_SIZE(n) ((n) * sizeof(jssrcnote))
#define TRYNOTE_SIZE(n) ((n) * sizeof(JSTryNote))
JS_FRIEND_API(JSBool)
js_InitCodeGenerator(JSContext *cx, JSCodeGenerator *cg,
JSArenaPool *codePool, JSArenaPool *notePool,
const char *filename, uintN lineno,
JSPrincipals *principals)
{
memset(cg, 0, sizeof *cg);
TREE_CONTEXT_INIT(&cg->treeContext);
cg->treeContext.flags |= TCF_COMPILING;
cg->codePool = codePool;
cg->notePool = notePool;
cg->codeMark = JS_ARENA_MARK(codePool);
cg->noteMark = JS_ARENA_MARK(notePool);
cg->tempMark = JS_ARENA_MARK(&cx->tempPool);
cg->current = &cg->main;
cg->filename = filename;
cg->firstLine = cg->prolog.currentLine = cg->main.currentLine = lineno;
cg->principals = principals;
ATOM_LIST_INIT(&cg->atomList);
cg->prolog.noteMask = cg->main.noteMask = SRCNOTE_CHUNK - 1;
ATOM_LIST_INIT(&cg->constList);
return JS_TRUE;
}
JS_FRIEND_API(void)
js_FinishCodeGenerator(JSContext *cx, JSCodeGenerator *cg)
{
TREE_CONTEXT_FINISH(&cg->treeContext);
JS_ARENA_RELEASE(cg->codePool, cg->codeMark);
JS_ARENA_RELEASE(cg->notePool, cg->noteMark);
JS_ARENA_RELEASE(&cx->tempPool, cg->tempMark);
}
static ptrdiff_t
EmitCheck(JSContext *cx, JSCodeGenerator *cg, JSOp op, ptrdiff_t delta)
{
jsbytecode *base, *limit, *next;
ptrdiff_t offset, length;
size_t incr, size;
base = CG_BASE(cg);
next = CG_NEXT(cg);
limit = CG_LIMIT(cg);
offset = PTRDIFF(next, base, jsbytecode);
if (next + delta > limit) {
length = offset + delta;
length = (length <= BYTECODE_CHUNK)
? BYTECODE_CHUNK
: JS_BIT(JS_CeilingLog2(length));
incr = BYTECODE_SIZE(length);
if (!base) {
JS_ARENA_ALLOCATE_CAST(base, jsbytecode *, cg->codePool, incr);
} else {
size = BYTECODE_SIZE(PTRDIFF(limit, base, jsbytecode));
incr -= size;
JS_ARENA_GROW_CAST(base, jsbytecode *, cg->codePool, size, incr);
}
if (!base) {
JS_ReportOutOfMemory(cx);
return -1;
}
CG_BASE(cg) = base;
CG_LIMIT(cg) = base + length;
CG_NEXT(cg) = base + offset;
}
return offset;
}
static void
UpdateDepth(JSContext *cx, JSCodeGenerator *cg, ptrdiff_t target)
{
jsbytecode *pc;
const JSCodeSpec *cs;
intN nuses;
pc = CG_CODE(cg, target);
cs = &js_CodeSpec[pc[0]];
nuses = cs->nuses;
if (nuses < 0)
nuses = 2 + GET_ARGC(pc); /* stack: fun, this, [argc arguments] */
cg->stackDepth -= nuses;
JS_ASSERT(cg->stackDepth >= 0);
if (cg->stackDepth < 0) {
char numBuf[12];
JS_snprintf(numBuf, sizeof numBuf, "%d", target);
JS_ReportErrorFlagsAndNumber(cx, JSREPORT_WARNING,
js_GetErrorMessage, NULL,
JSMSG_STACK_UNDERFLOW,
cg->filename ? cg->filename : "stdin",
numBuf);
}
cg->stackDepth += cs->ndefs;
if ((uintN)cg->stackDepth > cg->maxStackDepth)
cg->maxStackDepth = cg->stackDepth;
}
ptrdiff_t
js_Emit1(JSContext *cx, JSCodeGenerator *cg, JSOp op)
{
ptrdiff_t offset = EmitCheck(cx, cg, op, 1);
if (offset >= 0) {
*CG_NEXT(cg)++ = (jsbytecode)op;
UpdateDepth(cx, cg, offset);
}
return offset;
}
ptrdiff_t
js_Emit2(JSContext *cx, JSCodeGenerator *cg, JSOp op, jsbytecode op1)
{
ptrdiff_t offset = EmitCheck(cx, cg, op, 2);
if (offset >= 0) {
jsbytecode *next = CG_NEXT(cg);
next[0] = (jsbytecode)op;
next[1] = op1;
CG_NEXT(cg) = next + 2;
UpdateDepth(cx, cg, offset);
}
return offset;
}
ptrdiff_t
js_Emit3(JSContext *cx, JSCodeGenerator *cg, JSOp op, jsbytecode op1,
jsbytecode op2)
{
ptrdiff_t offset = EmitCheck(cx, cg, op, 3);
if (offset >= 0) {
jsbytecode *next = CG_NEXT(cg);
next[0] = (jsbytecode)op;
next[1] = op1;
next[2] = op2;
CG_NEXT(cg) = next + 3;
UpdateDepth(cx, cg, offset);
}
return offset;
}
ptrdiff_t
js_EmitN(JSContext *cx, JSCodeGenerator *cg, JSOp op, size_t extra)
{
ptrdiff_t length = 1 + (ptrdiff_t)extra;
ptrdiff_t offset = EmitCheck(cx, cg, op, length);
if (offset >= 0) {
jsbytecode *next = CG_NEXT(cg);
*next = (jsbytecode)op;
memset(next + 1, 0, BYTECODE_SIZE(extra));
CG_NEXT(cg) = next + length;
UpdateDepth(cx, cg, offset);
}
return offset;
}
/* XXX too many "... statement" L10N gaffes below -- fix via js.msg! */
const char js_with_statement_str[] = "with statement";
const char js_finally_block_str[] = "finally block";
const char js_script_str[] = "script";
static const char *statementName[] = {
"label statement", /* LABEL */
"if statement", /* IF */
"else statement", /* ELSE */
"switch statement", /* SWITCH */
"block", /* BLOCK */
js_with_statement_str, /* WITH */
"catch block", /* CATCH */
"try block", /* TRY */
js_finally_block_str, /* FINALLY */
js_finally_block_str, /* SUBROUTINE */
"do loop", /* DO_LOOP */
"for loop", /* FOR_LOOP */
"for/in loop", /* FOR_IN_LOOP */
"while loop", /* WHILE_LOOP */
};
static const char *
StatementName(JSCodeGenerator *cg)
{
if (!cg->treeContext.topStmt)
return js_script_str;
return statementName[cg->treeContext.topStmt->type];
}
static void
ReportStatementTooLarge(JSContext *cx, JSCodeGenerator *cg)
{
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_NEED_DIET,
StatementName(cg));
}
/**
Span-dependent instructions in JS bytecode consist of the jump (JOF_JUMP)
and switch (JOF_LOOKUPSWITCH, JOF_TABLESWITCH) format opcodes, subdivided
into unconditional (gotos and gosubs), and conditional jumps or branches
(which pop a value, test it, and jump depending on its value). Most jumps
have just one immediate operand, a signed offset from the jump opcode's pc
to the target bytecode. The lookup and table switch opcodes may contain
many jump offsets.
Mozilla bug #80981 (http://bugzilla.mozilla.org/show_bug.cgi?id=80981) was
fixed by adding extended "X" counterparts to the opcodes/formats (NB: X is
suffixed to prefer JSOP_ORX thereby avoiding a JSOP_XOR name collision for
the extended form of the JSOP_OR branch opcode). The unextended or short
formats have 16-bit signed immediate offset operands, the extended or long
formats have 32-bit signed immediates. The span-dependency problem consists
of selecting as few long instructions as possible, or about as few -- since
jumps can span other jumps, extending one jump may cause another to need to
be extended.
Most JS scripts are short, so need no extended jumps. We optimize for this
case by generating short jumps until we know a long jump is needed. After
that point, we keep generating short jumps, but each jump's 16-bit immediate
offset operand is actually an unsigned index into cg->spanDeps, an array of
JSSpanDep structs. Each struct tells the top offset in the script of the
opcode, the "before" offset of the jump (which will be the same as top for
simplex jumps, but which will index further into the bytecode array for a
non-initial jump offset in a lookup or table switch), the after "offset"
adjusted during span-dependent instruction selection (initially the same
value as the "before" offset), and the jump target (more below).
Since we generate cg->spanDeps lazily, from within js_SetJumpOffset, we must
ensure that all bytecode generated so far can be inspected to discover where
the jump offset immediate operands lie within CG_CODE(cg). But the bonus is
that we generate span-dependency records sorted by their offsets, so we can
binary-search when trying to find a JSSpanDep for a given bytecode offset,
or the nearest JSSpanDep at or above a given pc.
To avoid limiting scripts to 64K jumps, if the cg->spanDeps index overflows
65534, we store SPANDEP_INDEX_HUGE in the jump's immediate operand. This
tells us that we need to binary-search for the cg->spanDeps entry by the
jump opcode's bytecode offset (sd->before).
Jump targets need to be maintained in a data structure that lets us look
up an already-known target by its address (jumps may have a common target),
and that also lets us update the addresses (script-relative, a.k.a. absolute
offsets) of targets that come after a jump target (for when a jump below
that target needs to be extended). We use an AVL tree, implemented using
recursion, but with some tricky optimizations to its height-balancing code
(see http://www.cmcrossroads.com/bradapp/ftp/src/libs/C++/AvlTrees.html).
A final wrinkle: backpatch chains are linked by jump-to-jump offsets with
positive sign, even though they link "backward" (i.e., toward lower bytecode
address). We don't want to waste space and search time in the AVL tree for
such temporary backpatch deltas, so we use a single-bit wildcard scheme to
tag true JSJumpTarget pointers and encode untagged, signed (positive) deltas
in JSSpanDep.target pointers, depending on whether the JSSpanDep has a known
target, or is still awaiting backpatching.
Note that backpatch chains would present a problem for BuildSpanDepTable,
which inspects bytecode to build cg->spanDeps on demand, when the first
short jump offset overflows. To solve this temporary problem, we emit a
proxy bytecode (JSOP_BACKPATCH; JSOP_BACKPATCH_POP for branch ops) whose
nuses/ndefs counts help keep the stack balanced, but whose opcode format
distinguishes its backpatch delta immediate operand from a normal jump
offset.
*/
static int
BalanceJumpTargets(JSJumpTarget **jtp)
{
JSJumpTarget *jt, *jt2, *root;
int dir, otherDir, heightChanged;
JSBool doubleRotate;
jt = *jtp;
JS_ASSERT(jt->balance != 0);
if (jt->balance < -1) {
dir = JT_RIGHT;
doubleRotate = (jt->kids[JT_LEFT]->balance > 0);
} else if (jt->balance > 1) {
dir = JT_LEFT;
doubleRotate = (jt->kids[JT_RIGHT]->balance < 0);
} else {
return 0;
}
otherDir = JT_OTHER_DIR(dir);
if (doubleRotate) {
jt2 = jt->kids[otherDir];
*jtp = root = jt2->kids[dir];
jt->kids[otherDir] = root->kids[dir];
root->kids[dir] = jt;
jt2->kids[dir] = root->kids[otherDir];
root->kids[otherDir] = jt2;
heightChanged = 1;
root->kids[JT_LEFT]->balance = -JS_MAX(root->balance, 0);
root->kids[JT_RIGHT]->balance = -JS_MIN(root->balance, 0);
root->balance = 0;
} else {
*jtp = root = jt->kids[otherDir];
jt->kids[otherDir] = root->kids[dir];
root->kids[dir] = jt;
heightChanged = (root->balance != 0);
jt->balance = -((dir == JT_LEFT) ? --root->balance : ++root->balance);
}
return heightChanged;
}
typedef struct AddJumpTargetArgs {
JSContext *cx;
JSCodeGenerator *cg;
ptrdiff_t offset;
JSJumpTarget *node;
} AddJumpTargetArgs;
static int
AddJumpTarget(AddJumpTargetArgs *args, JSJumpTarget **jtp)
{
JSJumpTarget *jt;
int balanceDelta;
jt = *jtp;
if (!jt) {
JSCodeGenerator *cg = args->cg;
jt = cg->jtFreeList;
if (jt) {
cg->jtFreeList = jt->kids[JT_LEFT];
} else {
JS_ARENA_ALLOCATE_CAST(jt, JSJumpTarget *, &args->cx->tempPool,
sizeof *jt);
if (!jt) {
JS_ReportOutOfMemory(args->cx);
return 0;
}
}
jt->offset = args->offset;
jt->balance = 0;
jt->kids[JT_LEFT] = jt->kids[JT_RIGHT] = NULL;
cg->numJumpTargets++;
args->node = jt;
*jtp = jt;
return 1;
}
if (jt->offset == args->offset) {
args->node = jt;
return 0;
}
if (args->offset < jt->offset)
balanceDelta = -AddJumpTarget(args, &jt->kids[JT_LEFT]);
else
balanceDelta = AddJumpTarget(args, &jt->kids[JT_RIGHT]);
if (!args->node)
return 0;
jt->balance += balanceDelta;
return (balanceDelta && jt->balance)
? 1 - BalanceJumpTargets(jtp)
: 0;
}
#ifdef DEBUG_brendan
static int AVLCheck(JSJumpTarget *jt)
{
int lh, rh;
if (!jt) return 0;
JS_ASSERT(-1 <= jt->balance && jt->balance <= 1);
lh = AVLCheck(jt->kids[JT_LEFT]);
rh = AVLCheck(jt->kids[JT_RIGHT]);
JS_ASSERT(jt->balance == rh - lh);
return 1 + JS_MAX(lh, rh);
}
#endif
static JSBool
SetSpanDepTarget(JSContext *cx, JSCodeGenerator *cg, JSSpanDep *sd,
ptrdiff_t off)
{
AddJumpTargetArgs args;
if (off < JUMPX_OFFSET_MIN || JUMPX_OFFSET_MAX < off) {
ReportStatementTooLarge(cx, cg);
return JS_FALSE;
}
args.cx = cx;
args.cg = cg;
args.offset = sd->top + off;
args.node = NULL;
AddJumpTarget(&args, &cg->jumpTargets);
if (!args.node)
return JS_FALSE;
#ifdef DEBUG_brendan
AVLCheck(cg->jumpTargets);
#endif
SD_SET_TARGET(sd, args.node);
return JS_TRUE;
}
#define SPANDEPS_MIN 256
#define SPANDEPS_SIZE(n) ((n) * sizeof(JSSpanDep))
#define SPANDEPS_SIZE_MIN SPANDEPS_SIZE(SPANDEPS_MIN)
static JSBool
AddSpanDep(JSContext *cx, JSCodeGenerator *cg, jsbytecode *pc, jsbytecode *pc2,
ptrdiff_t off)
{
uintN index;
JSSpanDep *sdbase, *sd;
size_t size;
index = cg->numSpanDeps;
if (index + 1 == 0) {
ReportStatementTooLarge(cx, cg);
return JS_FALSE;
}
if ((index & (index - 1)) == 0 &&
(!(sdbase = cg->spanDeps) || index >= SPANDEPS_MIN)) {
if (!sdbase) {
size = SPANDEPS_SIZE_MIN;
JS_ARENA_ALLOCATE_CAST(sdbase, JSSpanDep *, &cx->tempPool, size);
} else {
size = SPANDEPS_SIZE(index);
JS_ARENA_GROW_CAST(sdbase, JSSpanDep *, &cx->tempPool, size, size);
}
if (!sdbase)
return JS_FALSE;
cg->spanDeps = sdbase;
}
cg->numSpanDeps = index + 1;
sd = cg->spanDeps + index;
sd->top = PTRDIFF(pc, CG_BASE(cg), jsbytecode);
sd->offset = sd->before = PTRDIFF(pc2, CG_BASE(cg), jsbytecode);
if (js_CodeSpec[*pc].format & JOF_BACKPATCH) {
/* Jump offset will be backpatched if off is a non-zero "bpdelta". */
if (off != 0) {
JS_ASSERT(off >= 1 + JUMP_OFFSET_LEN);
if (off > BPDELTA_MAX) {
ReportStatementTooLarge(cx, cg);
return JS_FALSE;
}
}
SD_SET_BPDELTA(sd, off);
} else if (off == 0) {
/* Jump offset will be patched directly, without backpatch chaining. */
SD_SET_TARGET(sd, NULL);
} else {
/* The jump offset in off is non-zero, therefore it's already known. */
if (!SetSpanDepTarget(cx, cg, sd, off))
return JS_FALSE;
}
if (index > SPANDEP_INDEX_MAX)
index = SPANDEP_INDEX_HUGE;
SET_SPANDEP_INDEX(pc2, index);
return JS_TRUE;
}
static JSBool
BuildSpanDepTable(JSContext *cx, JSCodeGenerator *cg)
{
jsbytecode *pc, *end;
JSOp op;
const JSCodeSpec *cs;
ptrdiff_t len, off;
pc = CG_BASE(cg) + cg->spanDepTodo;
end = CG_NEXT(cg);
while (pc < end) {
op = (JSOp)*pc;
cs = &js_CodeSpec[op];
len = (ptrdiff_t)cs->length;
switch (cs->format & JOF_TYPEMASK) {
case JOF_JUMP:
off = GET_JUMP_OFFSET(pc);
if (!AddSpanDep(cx, cg, pc, pc, off))
return JS_FALSE;
break;
case JOF_TABLESWITCH:
{
jsbytecode *pc2;
jsint i, low, high;
pc2 = pc;
off = GET_JUMP_OFFSET(pc2);
if (!AddSpanDep(cx, cg, pc, pc2, off))
return JS_FALSE;
pc2 += JUMP_OFFSET_LEN;
low = GET_JUMP_OFFSET(pc2);
pc2 += JUMP_OFFSET_LEN;
high = GET_JUMP_OFFSET(pc2);
pc2 += JUMP_OFFSET_LEN;
for (i = low; i <= high; i++) {
off = GET_JUMP_OFFSET(pc2);
if (!AddSpanDep(cx, cg, pc, pc2, off))
return JS_FALSE;
pc2 += JUMP_OFFSET_LEN;
}
len = 1 + pc2 - pc;
break;
}
case JOF_LOOKUPSWITCH:
{
jsbytecode *pc2;
jsint npairs;
pc2 = pc;
off = GET_JUMP_OFFSET(pc2);
if (!AddSpanDep(cx, cg, pc, pc2, off))
return JS_FALSE;
pc2 += JUMP_OFFSET_LEN;
npairs = (jsint) GET_ATOM_INDEX(pc2);
pc2 += ATOM_INDEX_LEN;
while (npairs) {
pc2 += ATOM_INDEX_LEN;
off = GET_JUMP_OFFSET(pc2);
if (!AddSpanDep(cx, cg, pc, pc2, off))
return JS_FALSE;
pc2 += JUMP_OFFSET_LEN;
npairs--;
}
len = 1 + pc2 - pc;
break;
}
}
JS_ASSERT(len > 0);
pc += len;
}
return JS_TRUE;
}
static JSSpanDep *
GetSpanDep(JSCodeGenerator *cg, jsbytecode *pc)
{
uintN index;
ptrdiff_t offset;
int lo, hi, mid;
JSSpanDep *sd;
index = GET_SPANDEP_INDEX(pc);
if (index != SPANDEP_INDEX_HUGE)
return cg->spanDeps + index;
offset = PTRDIFF(pc, CG_BASE(cg), jsbytecode);
lo = 0;
hi = cg->numSpanDeps - 1;
while (lo <= hi) {
mid = (lo + hi) / 2;
sd = cg->spanDeps + mid;
if (sd->before == offset)
return sd;
if (sd->before < offset)
lo = mid + 1;
else
hi = mid - 1;
}
JS_ASSERT(0);
return NULL;
}
static JSBool
SetBackPatchDelta(JSContext *cx, JSCodeGenerator *cg, jsbytecode *pc,
ptrdiff_t delta)
{
JSSpanDep *sd;
JS_ASSERT(delta >= 1 + JUMP_OFFSET_LEN);
if (!cg->spanDeps && delta < JUMP_OFFSET_MAX) {
SET_JUMP_OFFSET(pc, delta);
return JS_TRUE;
}
if (delta > BPDELTA_MAX) {
ReportStatementTooLarge(cx, cg);
return JS_FALSE;
}
if (!cg->spanDeps && !BuildSpanDepTable(cx, cg))
return JS_FALSE;
sd = GetSpanDep(cg, pc);
JS_ASSERT(SD_GET_BPDELTA(sd) == 0);
SD_SET_BPDELTA(sd, delta);
return JS_TRUE;
}
static void
UpdateJumpTargets(JSJumpTarget *jt, ptrdiff_t pivot, ptrdiff_t delta)
{
if (jt->offset > pivot) {
jt->offset += delta;
if (jt->kids[JT_LEFT])
UpdateJumpTargets(jt->kids[JT_LEFT], pivot, delta);
}
if (jt->kids[JT_RIGHT])
UpdateJumpTargets(jt->kids[JT_RIGHT], pivot, delta);
}
static JSSpanDep *
FindNearestSpanDep(JSCodeGenerator *cg, ptrdiff_t offset, int lo,
JSSpanDep *guard)
{
int num, hi, mid;
JSSpanDep *sdbase, *sd;
num = cg->numSpanDeps;
JS_ASSERT(num > 0);
hi = num - 1;
sdbase = cg->spanDeps;
while (lo <= hi) {
mid = (lo + hi) / 2;
sd = sdbase + mid;
if (sd->before == offset)
return sd;
if (sd->before < offset)
lo = mid + 1;
else
hi = mid - 1;
}
if (lo == num)
return guard;
sd = sdbase + lo;
JS_ASSERT(sd->before >= offset && (lo == 0 || sd[-1].before < offset));
return sd;
}
static void
FreeJumpTargets(JSCodeGenerator *cg, JSJumpTarget *jt)
{
if (jt->kids[JT_LEFT])
FreeJumpTargets(cg, jt->kids[JT_LEFT]);
if (jt->kids[JT_RIGHT])
FreeJumpTargets(cg, jt->kids[JT_RIGHT]);
jt->kids[JT_LEFT] = cg->jtFreeList;
cg->jtFreeList = jt;
}
static JSBool
OptimizeSpanDeps(JSContext *cx, JSCodeGenerator *cg)
{
jsbytecode *pc, *oldpc, *base, *limit, *next;
JSSpanDep *sd, *sd2, *sdbase, *sdlimit, *sdtop, guard;
ptrdiff_t offset, growth, delta, top, pivot, span, length, target;
JSBool done;
JSOp op;
uint32 type;
size_t size, incr;
jssrcnote *sn, *snlimit;
JSSrcNoteSpec *spec;
uintN i, n, noteIndex;
JSTryNote *tn, *tnlimit;
#ifdef DEBUG_brendan
int passes = 0;
#endif
base = CG_BASE(cg);
sdbase = cg->spanDeps;
sdlimit = sdbase + cg->numSpanDeps;
offset = CG_OFFSET(cg);
growth = 0;
do {
done = JS_TRUE;
delta = 0;
top = pivot = -1;
sdtop = NULL;
pc = NULL;
op = JSOP_NOP;
type = 0;
#ifdef DEBUG_brendan
passes++;
#endif
for (sd = sdbase; sd < sdlimit; sd++) {
JS_ASSERT(JT_HAS_TAG(sd->target));
sd->offset += delta;
if (sd->top != top) {
sdtop = sd;
top = sd->top;
JS_ASSERT(top == sd->before);
pivot = sd->offset;
pc = base + top;
op = (JSOp) *pc;
type = (js_CodeSpec[op].format & JOF_TYPEMASK);
if (JOF_TYPE_IS_EXTENDED_JUMP(type)) {
/*
* We already extended all the jump offset operands for
* the opcode at sd->top. Jumps and branches have only
* one jump offset operand, but switches have many, all
* of which are adjacent in cg->spanDeps.
*/
continue;
}
JS_ASSERT(type == JOF_JUMP ||
type == JOF_TABLESWITCH ||
type == JOF_LOOKUPSWITCH);
}
if (!JOF_TYPE_IS_EXTENDED_JUMP(type)) {
span = SD_SPAN(sd, pivot);
if (span < JUMP_OFFSET_MIN || JUMP_OFFSET_MAX < span) {
ptrdiff_t deltaFromTop = 0;
done = JS_FALSE;
switch (op) {
case JSOP_GOTO: op = JSOP_GOTOX; break;
case JSOP_IFEQ: op = JSOP_IFEQX; break;
case JSOP_IFNE: op = JSOP_IFNEX; break;
case JSOP_OR: op = JSOP_ORX; break;
case JSOP_AND: op = JSOP_ANDX; break;
case JSOP_GOSUB: op = JSOP_GOSUBX; break;
case JSOP_CASE: op = JSOP_CASEX; break;
case JSOP_DEFAULT: op = JSOP_DEFAULTX; break;
case JSOP_TABLESWITCH: op = JSOP_TABLESWITCHX; break;
case JSOP_LOOKUPSWITCH: op = JSOP_LOOKUPSWITCHX; break;
default:
ReportStatementTooLarge(cx, cg);
return JS_FALSE;
}
*pc = (jsbytecode) op;
for (sd2 = sdtop; sd2 < sdlimit && sd2->top == top; sd2++) {
if (sd2 <= sd) {
/*
* sd2->offset already includes delta as it stood
* before we entered this loop, but it must also
* include the delta relative to top due to all the
* extended jump offset immediates for the opcode
* starting at top, which we extend in this loop.
*
* If there is only one extended jump offset, then
* sd2->offset won't change and this for loop will
* iterate once only.
*/
sd2->offset += deltaFromTop;
deltaFromTop += JUMPX_OFFSET_LEN - JUMP_OFFSET_LEN;
} else {
/*
* sd2 comes after sd, and won't be revisited by
* the outer for loop, so we have to increase its
* offset by delta, not merely by deltaFromTop.
*/
sd2->offset += delta;
}
delta += JUMPX_OFFSET_LEN - JUMP_OFFSET_LEN;
UpdateJumpTargets(cg->jumpTargets, sd2->offset,
JUMPX_OFFSET_LEN - JUMP_OFFSET_LEN);
}
sd = sd2 - 1;
}
}
}
growth += delta;
} while (!done);
if (growth) {
#ifdef DEBUG_brendan
printf("%s:%u: %u/%u jumps extended in %d passes (%d=%d+%d)\n",
cg->filename ? cg->filename : "stdin", cg->firstLine,
growth / (JUMPX_OFFSET_LEN - JUMP_OFFSET_LEN), cg->numSpanDeps,
passes, offset + growth, offset, growth);
#endif
/*
* Ensure that we have room for the extended jumps, but don't round up
* to a power of two -- we're done generating code, so we cut to fit.
*/
limit = CG_LIMIT(cg);
length = offset + growth;
next = base + length;
if (next > limit) {
JS_ASSERT(length > BYTECODE_CHUNK);
size = BYTECODE_SIZE(PTRDIFF(limit, base, jsbytecode));
incr = BYTECODE_SIZE(length) - size;
JS_ARENA_GROW_CAST(base, jsbytecode *, cg->codePool, size, incr);
if (!base) {
JS_ReportOutOfMemory(cx);
return JS_FALSE;
}
CG_BASE(cg) = base;
CG_LIMIT(cg) = next = base + length;
}
CG_NEXT(cg) = next;
/*
* Set up a fake span dependency record to guard the end of the code
* being generated. This guard record is returned as a fencepost by
* FindNearestSpanDep if there is no real spandep at or above a given
* unextended code offset.
*/
guard.top = -1;
guard.offset = offset + growth;
guard.before = offset;
guard.target = NULL;
}
/*
* Now work backwards through the span dependencies, copying chunks of
* bytecode between each extended jump toward the end of the grown code
* space, and restoring immediate offset operands for all jump bytecodes.
* The first chunk of bytecodes, starting at base and ending at the first
* extended jump offset (NB: this chunk includes the operation bytecode
* just before that immediate jump offset), doesn't need to be copied.
*/
JS_ASSERT(sd == sdlimit);
top = -1;
while (--sd >= sdbase) {
if (sd->top != top) {
top = sd->top;
op = (JSOp) base[top];
type = (js_CodeSpec[op].format & JOF_TYPEMASK);
for (sd2 = sd - 1; sd2 >= sdbase && sd2->top == top; sd2--)
continue;
sd2++;
pivot = sd2->offset;
JS_ASSERT(top == sd2->before);
}
oldpc = base + sd->before;
span = SD_SPAN(sd, pivot);
/*
* If this jump didn't need to be extended, restore its span immediate
* offset operand now, overwriting the index of sd within cg->spanDeps
* that was stored temporarily after *pc when BuildSpanDepTable ran.
*
* Note that span might fit in 16 bits even for an extended jump op,
* if the op has multiple span operands, not all of which overflowed
* (e.g. JSOP_LOOKUPSWITCH or JSOP_TABLESWITCH where some cases are in
* range for a short jump, but others are not).
*/
if (!JOF_TYPE_IS_EXTENDED_JUMP(type)) {
JS_ASSERT(JUMP_OFFSET_MIN <= span && span <= JUMP_OFFSET_MAX);
SET_JUMP_OFFSET(oldpc, span);
continue;
}
/*
* Set up parameters needed to copy the next run of bytecode starting
* at offset (which is a cursor into the unextended, original bytecode
* vector), down to sd->before (a cursor of the same scale as offset,
* it's the index of the original jump pc). Reuse delta to count the
* nominal number of bytes to copy.
*/
pc = base + sd->offset;
delta = offset - sd->before;
JS_ASSERT(delta >= 1 + JUMP_OFFSET_LEN);
/*
* Don't bother copying the jump offset we're about to reset, but do
* copy the bytecode at oldpc (which comes just before its immediate
* jump offset operand), on the next iteration through the loop, by
* including it in offset's new value.
*/
offset = sd->before + 1;
size = BYTECODE_SIZE(delta - (1 + JUMP_OFFSET_LEN));
if (size) {
memmove(pc + 1 + JUMPX_OFFSET_LEN,
oldpc + 1 + JUMP_OFFSET_LEN,
size);
}
SET_JUMPX_OFFSET(pc, span);
}
if (growth) {
/*
* Fix source note deltas. Don't hardwire the delta fixup adjustment,
* even though currently it must be JUMPX_OFFSET_LEN - JUMP_OFFSET_LEN
* at each sd that moved. The future may bring different offset sizes
* for span-dependent instruction operands. However, we fix only main
* notes here, not prolog notes -- we know that prolog opcodes are not
* span-dependent, and aren't likely ever to be.
*/
offset = growth = 0;
sd = sdbase;
for (sn = cg->main.notes, snlimit = sn + cg->main.noteCount;
sn < snlimit;
sn = SN_NEXT(sn)) {
/*
* Recall that the offset of a given note includes its delta, and
* tells the offset of the annotated bytecode from the main entry
* point of the script.
*/
offset += SN_DELTA(sn);
while (sd < sdlimit && sd->before < offset) {
/*
* To compute the delta to add to sn, we need to look at the
* spandep after sd, whose offset - (before + growth) tells by
* how many bytes sd's instruction grew.
*/
sd2 = sd + 1;
if (sd2 == sdlimit)
sd2 = &guard;
delta = sd2->offset - (sd2->before + growth);
if (delta > 0) {
JS_ASSERT(delta == JUMPX_OFFSET_LEN - JUMP_OFFSET_LEN);
sn = js_AddToSrcNoteDelta(cx, cg, sn, delta);
if (!sn)
return JS_FALSE;
snlimit = cg->main.notes + cg->main.noteCount;
growth += delta;
}
sd++;
}
/*
* If sn has span-dependent offset operands, check whether each
* covers further span-dependencies, and increase those operands
* accordingly. Some source notes measure offset not from the
* annotated pc, but from that pc plus some small bias. NB: we
* assume that spec->offsetBias can't itself span span-dependent
* instructions!
*/
spec = &js_SrcNoteSpec[SN_TYPE(sn)];
if (spec->isSpanDep) {
pivot = offset + spec->offsetBias;
n = spec->arity;
for (i = 0; i < n; i++) {
span = js_GetSrcNoteOffset(sn, i);
if (span == 0)
continue;
target = pivot + span * spec->isSpanDep;
sd2 = FindNearestSpanDep(cg, target,
(target >= pivot)
? sd - sdbase
: 0,
&guard);
/*
* Increase target by sd2's before-vs-after offset delta,
* which is absolute (i.e., relative to start of script,
* as is target). Recompute the span by subtracting its
* adjusted pivot from target.
*/
target += sd2->offset - sd2->before;
span = target - (pivot + growth);
span *= spec->isSpanDep;
noteIndex = sn - cg->main.notes;
if (!js_SetSrcNoteOffset(cx, cg, noteIndex, i, span))
return JS_FALSE;
sn = cg->main.notes + noteIndex;
snlimit = cg->main.notes + cg->main.noteCount;
}
}
}
cg->main.lastNoteOffset += growth;
/*
* Fix try/catch notes (O(numTryNotes * log2(numSpanDeps)), but it's
* not clear how we can beat that).
*/
for (tn = cg->tryBase, tnlimit = cg->tryNext; tn < tnlimit; tn++) {
/*
* First, look for the nearest span dependency at/above tn->start.
* There may not be any such spandep, in which case the guard will
* be returned.
*/
offset = tn->start;
sd = FindNearestSpanDep(cg, offset, 0, &guard);
delta = sd->offset - sd->before;
tn->start = offset + delta;
/*
* Next, find the nearest spandep at/above tn->start + tn->length.
* Use its delta minus tn->start's delta to increase tn->length.
*/
length = tn->length;
sd2 = FindNearestSpanDep(cg, offset + length, sd - sdbase, &guard);
if (sd2 != sd)
tn->length = length + sd2->offset - sd2->before - delta;
/*
* Finally, adjust tn->catchStart upward only if it is non-zero,
* and provided there are spandeps below it that grew.
*/
offset = tn->catchStart;
if (offset != 0) {
sd = FindNearestSpanDep(cg, offset, sd2 - sdbase, &guard);
tn->catchStart = offset + sd->offset - sd->before;
}
}
}
#ifdef DEBUG_brendan
{
uintN bigspans = 0;
top = -1;
for (sd = sdbase; sd < sdlimit; sd++) {
offset = sd->offset;
/* NB: sd->top cursors into the original, unextended bytecode vector. */
if (sd->top != top) {
JS_ASSERT(top == -1 ||
!JOF_TYPE_IS_EXTENDED_JUMP(type) ||
bigspans != 0);
bigspans = 0;
top = sd->top;
JS_ASSERT(top == sd->before);
op = (JSOp) base[offset];
type = (js_CodeSpec[op].format & JOF_TYPEMASK);
JS_ASSERT(type == JOF_JUMP ||
type == JOF_JUMPX ||
type == JOF_TABLESWITCH ||
type == JOF_TABLESWITCHX ||
type == JOF_LOOKUPSWITCH ||
type == JOF_LOOKUPSWITCHX);
pivot = offset;
}
pc = base + offset;
if (JOF_TYPE_IS_EXTENDED_JUMP(type)) {
span = GET_JUMPX_OFFSET(pc);
if (span < JUMP_OFFSET_MIN || JUMP_OFFSET_MAX < span) {
bigspans++;
} else {
JS_ASSERT(type == JOF_TABLESWITCHX ||
type == JOF_LOOKUPSWITCHX);
}
} else {
span = GET_JUMP_OFFSET(pc);
}
JS_ASSERT(SD_SPAN(sd, pivot) == span);
}
JS_ASSERT(!JOF_TYPE_IS_EXTENDED_JUMP(type) || bigspans != 0);
}
#endif
/*
* Reset so we optimize at most once -- cg may be used for further code
* generation of successive, independent, top-level statements. No jump
* can span top-level statements, because JS lacks goto.
*/
size = SPANDEPS_SIZE(JS_BIT(JS_CeilingLog2(cg->numSpanDeps)));
JS_ArenaFreeAllocation(&cx->tempPool, cg->spanDeps,
JS_MAX(size, SPANDEPS_SIZE_MIN));
cg->spanDeps = NULL;
FreeJumpTargets(cg, cg->jumpTargets);
cg->jumpTargets = NULL;
cg->numSpanDeps = cg->numJumpTargets = 0;
cg->spanDepTodo = CG_OFFSET(cg);
return JS_TRUE;
}
static JSBool
EmitJump(JSContext *cx, JSCodeGenerator *cg, JSOp op, ptrdiff_t off)
{
JSBool extend;
ptrdiff_t jmp;
jsbytecode *pc;
extend = off < JUMP_OFFSET_MIN || JUMP_OFFSET_MAX < off;
if (extend && !cg->spanDeps && !BuildSpanDepTable(cx, cg))
return JS_FALSE;
jmp = js_Emit3(cx, cg, op, JUMP_OFFSET_HI(off), JUMP_OFFSET_LO(off));
if (jmp >= 0 && (extend || cg->spanDeps)) {
pc = CG_CODE(cg, jmp);
if (!AddSpanDep(cx, cg, pc, pc, off))
return JS_FALSE;
}
return jmp;
}
static ptrdiff_t
GetJumpOffset(JSCodeGenerator *cg, jsbytecode *pc)
{
JSSpanDep *sd;
JSJumpTarget *jt;
ptrdiff_t top;
if (!cg->spanDeps)
return GET_JUMP_OFFSET(pc);
sd = GetSpanDep(cg, pc);
jt = sd->target;
if (!JT_HAS_TAG(jt))
return JT_TO_BPDELTA(jt);
top = sd->top;
while (--sd >= cg->spanDeps && sd->top == top)
continue;
sd++;
return JT_CLR_TAG(jt)->offset - sd->offset;
}
JSBool
js_SetJumpOffset(JSContext *cx, JSCodeGenerator *cg, jsbytecode *pc,
ptrdiff_t off)
{
if (!cg->spanDeps) {
if (JUMP_OFFSET_MIN <= off && off <= JUMP_OFFSET_MAX) {
SET_JUMP_OFFSET(pc, off);
return JS_TRUE;
}
if (!BuildSpanDepTable(cx, cg))
return JS_FALSE;
}
return SetSpanDepTarget(cx, cg, GetSpanDep(cg, pc), off);
}
JSBool
js_InStatement(JSTreeContext *tc, JSStmtType type)
{
JSStmtInfo *stmt;
for (stmt = tc->topStmt; stmt; stmt = stmt->down) {
if (stmt->type == type)
return JS_TRUE;
}
return JS_FALSE;
}
JSBool
js_IsGlobalReference(JSTreeContext *tc, JSAtom *atom, JSBool *loopyp)
{
JSStmtInfo *stmt;
JSObject *obj;
JSScope *scope;
*loopyp = JS_FALSE;
for (stmt = tc->topStmt; stmt; stmt = stmt->down) {
if (stmt->type == STMT_WITH)
return JS_FALSE;
if (STMT_IS_LOOP(stmt)) {
*loopyp = JS_TRUE;
continue;
}
if (stmt->flags & SIF_SCOPE) {
obj = ATOM_TO_OBJECT(stmt->atom);
JS_ASSERT(LOCKED_OBJ_GET_CLASS(obj) == &js_BlockClass);
scope = OBJ_SCOPE(obj);
if (SCOPE_GET_PROPERTY(scope, ATOM_TO_JSID(atom)))
return JS_FALSE;
}
}
return JS_TRUE;
}
void
js_PushStatement(JSTreeContext *tc, JSStmtInfo *stmt, JSStmtType type,
ptrdiff_t top)
{
stmt->type = type;
stmt->flags = 0;
SET_STATEMENT_TOP(stmt, top);
stmt->atom = NULL;
stmt->down = tc->topStmt;
tc->topStmt = stmt;
if (STMT_LINKS_SCOPE(stmt)) {
stmt->downScope = tc->topScopeStmt;
tc->topScopeStmt = stmt;
} else {
stmt->downScope = NULL;
}
}
void
js_PushBlockScope(JSTreeContext *tc, JSStmtInfo *stmt, JSAtom *blockAtom,
ptrdiff_t top)
{
JSObject *blockObj;
js_PushStatement(tc, stmt, STMT_BLOCK, top);
stmt->flags |= SIF_SCOPE;
blockObj = ATOM_TO_OBJECT(blockAtom);
blockObj->slots[JSSLOT_PARENT] = OBJECT_TO_JSVAL(tc->blockChain);
stmt->downScope = tc->topScopeStmt;
tc->topScopeStmt = stmt;
tc->blockChain = blockObj;
stmt->atom = blockAtom;
}
/*
* Emit a backpatch op with offset pointing to the previous jump of this type,
* so that we can walk back up the chain fixing up the op and jump offset.
*/
static ptrdiff_t
EmitBackPatchOp(JSContext *cx, JSCodeGenerator *cg, JSOp op, ptrdiff_t *lastp)
{
ptrdiff_t offset, delta;
offset = CG_OFFSET(cg);
delta = offset - *lastp;
*lastp = offset;
JS_ASSERT(delta > 0);
return EmitJump(cx, cg, op, delta);
}
/*
* Macro to emit a bytecode followed by a uint16 immediate operand stored in
* big-endian order, used for arg and var numbers as well as for atomIndexes.
* NB: We use cx and cg from our caller's lexical environment, and return
* false on error.
*/
#define EMIT_UINT16_IMM_OP(op, i) \
JS_BEGIN_MACRO \
if (js_Emit3(cx, cg, op, UINT16_HI(i), UINT16_LO(i)) < 0) \
return JS_FALSE; \
JS_END_MACRO
/* Emit additional bytecode(s) for non-local jumps. */
static JSBool
EmitNonLocalJumpFixup(JSContext *cx, JSCodeGenerator *cg, JSStmtInfo *toStmt,
JSOp *returnop)
{
intN depth;
JSStmtInfo *stmt;
ptrdiff_t jmp;
/*
* Return from within a try block that has a finally clause must be split
* into two ops: JSOP_SETRVAL, to pop the r.v. and store it in fp->rval;
* and JSOP_RETRVAL, which makes control flow go back to the caller, who
* picks up fp->rval as usual. Otherwise, the stack will be unbalanced
* when executing the finally clause.
*
* We mutate *returnop once only if we find an enclosing try-block (viz,
* STMT_FINALLY) to ensure that we emit just one JSOP_SETRVAL before one
* or more JSOP_GOSUBs and other fixup opcodes emitted by this function.
* Our caller (the TOK_RETURN case of js_EmitTree) then emits *returnop.
* The fixup opcodes and gosubs must interleave in the proper order, from
* inner statement to outer, so that finally clauses run at the correct
* stack depth.
*/
if (returnop) {
JS_ASSERT(*returnop == JSOP_RETURN);
for (stmt = cg->treeContext.topStmt; stmt != toStmt;
stmt = stmt->down) {
if (stmt->type == STMT_FINALLY ||
((cg->treeContext.flags & TCF_FUN_HEAVYWEIGHT) &&
STMT_MAYBE_SCOPE(stmt))) {
if (js_Emit1(cx, cg, JSOP_SETRVAL) < 0)
return JS_FALSE;
*returnop = JSOP_RETRVAL;
break;
}
}
/*
* If there are no try-with-finally blocks open around this return
* statement, we can generate a return forthwith and skip generating
* any fixup code.
*/
if (*returnop == JSOP_RETURN)
return JS_TRUE;
}
/*
* The non-local jump fixup we emit will unbalance cg->stackDepth, because
* the fixup replicates balanced code such as JSOP_LEAVEWITH emitted at the
* end of a with statement, so we save cg->stackDepth here and restore it
* just before a successful return.
*/
depth = cg->stackDepth;
for (stmt = cg->treeContext.topStmt; stmt != toStmt; stmt = stmt->down) {
switch (stmt->type) {
case STMT_FINALLY:
if (js_NewSrcNote(cx, cg, SRC_HIDDEN) < 0)
return JS_FALSE;
jmp = EmitBackPatchOp(cx, cg, JSOP_BACKPATCH, &GOSUBS(*stmt));
if (jmp < 0)
return JS_FALSE;
break;
case STMT_WITH:
/* There's a With object on the stack that we need to pop. */
if (js_NewSrcNote(cx, cg, SRC_HIDDEN) < 0)
return JS_FALSE;
if (js_Emit1(cx, cg, JSOP_LEAVEWITH) < 0)
return JS_FALSE;
break;
case STMT_FOR_IN_LOOP:
/*
* The iterator and the object being iterated need to be popped.
*/
if (js_NewSrcNote(cx, cg, SRC_HIDDEN) < 0)
return JS_FALSE;
if (js_Emit1(cx, cg, JSOP_ENDITER) < 0)
return JS_FALSE;
break;
case STMT_SUBROUTINE:
/*
* There's a [exception or hole, retsub pc-index] pair on the
* stack that we need to pop.
*/
if (js_NewSrcNote(cx, cg, SRC_HIDDEN) < 0)
return JS_FALSE;
if (js_Emit1(cx, cg, JSOP_POP2) < 0)
return JS_FALSE;
break;
default:;
}
if (stmt->flags & SIF_SCOPE) {
uintN i;
/* There is a Block object with locals on the stack to pop. */
if (js_NewSrcNote(cx, cg, SRC_HIDDEN) < 0)
return JS_FALSE;
i = OBJ_BLOCK_COUNT(cx, ATOM_TO_OBJECT(stmt->atom));
EMIT_UINT16_IMM_OP(JSOP_LEAVEBLOCK, i);
}
}
cg->stackDepth = depth;
return JS_TRUE;
}
static ptrdiff_t
EmitGoto(JSContext *cx, JSCodeGenerator *cg, JSStmtInfo *toStmt,
ptrdiff_t *lastp, JSAtomListElement *label, JSSrcNoteType noteType)
{
intN index;
if (!EmitNonLocalJumpFixup(cx, cg, toStmt, NULL))
return -1;
if (label)
index = js_NewSrcNote2(cx, cg, noteType, (ptrdiff_t) ALE_INDEX(label));
else if (noteType != SRC_NULL)
index = js_NewSrcNote(cx, cg, noteType);
else
index = 0;
if (index < 0)
return -1;
return EmitBackPatchOp(cx, cg, JSOP_BACKPATCH, lastp);
}
static JSBool
BackPatch(JSContext *cx, JSCodeGenerator *cg, ptrdiff_t last,
jsbytecode *target, jsbytecode op)
{
jsbytecode *pc, *stop;
ptrdiff_t delta, span;
pc = CG_CODE(cg, last);
stop = CG_CODE(cg, -1);
while (pc != stop) {
delta = GetJumpOffset(cg, pc);
span = PTRDIFF(target, pc, jsbytecode);
CHECK_AND_SET_JUMP_OFFSET(cx, cg, pc, span);
/*
* Set *pc after jump offset in case bpdelta didn't overflow, but span
* does (if so, CHECK_AND_SET_JUMP_OFFSET might call BuildSpanDepTable
* and need to see the JSOP_BACKPATCH* op at *pc).
*/
*pc = op;
pc -= delta;
}
return JS_TRUE;
}
void
js_PopStatement(JSTreeContext *tc)
{
JSStmtInfo *stmt;
JSObject *blockObj;
stmt = tc->topStmt;
tc->topStmt = stmt->down;
if (STMT_LINKS_SCOPE(stmt)) {
tc->topScopeStmt = stmt->downScope;
if (stmt->flags & SIF_SCOPE) {
blockObj = ATOM_TO_OBJECT(stmt->atom);
tc->blockChain = JSVAL_TO_OBJECT(blockObj->slots[JSSLOT_PARENT]);
}
}
}
JSBool
js_PopStatementCG(JSContext *cx, JSCodeGenerator *cg)
{
JSStmtInfo *stmt;
stmt = cg->treeContext.topStmt;
if (!STMT_IS_TRYING(stmt) &&
(!BackPatch(cx, cg, stmt->breaks, CG_NEXT(cg), JSOP_GOTO) ||
!BackPatch(cx, cg, stmt->continues, CG_CODE(cg, stmt->update),
JSOP_GOTO))) {
return JS_FALSE;
}
js_PopStatement(&cg->treeContext);
return JS_TRUE;
}
JSBool
js_DefineCompileTimeConstant(JSContext *cx, JSCodeGenerator *cg, JSAtom *atom,
JSParseNode *pn)
{
jsdouble dval;
jsint ival;
JSAtom *valueAtom;
JSAtomListElement *ale;
/* XXX just do numbers for now */
if (pn->pn_type == TOK_NUMBER) {
dval = pn->pn_dval;
valueAtom = (JSDOUBLE_IS_INT(dval, ival) && INT_FITS_IN_JSVAL(ival))
? js_AtomizeInt(cx, ival, 0)
: js_AtomizeDouble(cx, dval, 0);
if (!valueAtom)
return JS_FALSE;
ale = js_IndexAtom(cx, atom, &cg->constList);
if (!ale)
return JS_FALSE;
ALE_SET_VALUE(ale, ATOM_KEY(valueAtom));
}
return JS_TRUE;
}
JSStmtInfo *
js_LexicalLookup(JSTreeContext *tc, JSAtom *atom, jsint *slotp, JSBool letdecl)
{
JSStmtInfo *stmt;
JSObject *obj;
JSScope *scope;
JSScopeProperty *sprop;
jsval v;
for (stmt = tc->topScopeStmt; stmt; stmt = stmt->downScope) {
if (stmt->type == STMT_WITH) {
/* Ignore with statements enclosing a single let declaration. */
if (letdecl)
continue;
break;
}
/* Skip "maybe scope" statements that don't contain let bindings. */
if (!(stmt->flags & SIF_SCOPE))
continue;
obj = ATOM_TO_OBJECT(stmt->atom);
JS_ASSERT(LOCKED_OBJ_GET_CLASS(obj) == &js_BlockClass);
scope = OBJ_SCOPE(obj);
sprop = SCOPE_GET_PROPERTY(scope, ATOM_TO_JSID(atom));
if (sprop) {
JS_ASSERT(sprop->flags & SPROP_HAS_SHORTID);
if (slotp) {
/*
* Use LOCKED_OBJ_GET_SLOT since we know obj is single-
* threaded and owned by this compiler activation.
*/
v = LOCKED_OBJ_GET_SLOT(obj, JSSLOT_BLOCK_DEPTH);
JS_ASSERT(JSVAL_IS_INT(v) && JSVAL_TO_INT(v) >= 0);
*slotp = JSVAL_TO_INT(v) + sprop->shortid;
}
return stmt;
}
}
if (slotp)
*slotp = -1;
return stmt;
}
JSBool
js_LookupCompileTimeConstant(JSContext *cx, JSCodeGenerator *cg, JSAtom *atom,
jsval *vp)
{
JSBool ok;
JSStackFrame *fp;
JSStmtInfo *stmt;
jsint slot;
JSAtomListElement *ale;
JSObject *obj, *pobj;
JSProperty *prop;
uintN attrs;
/*
* fp chases cg down the stack, but only until we reach the outermost cg.
* This enables propagating consts from top-level into switch cases in a
* function compiled along with the top-level script. All stack frames
* with matching code generators should be flagged with JSFRAME_COMPILING;
* we check sanity here.
*/
*vp = JSVAL_VOID;
ok = JS_TRUE;
fp = cx->fp;
do {
JS_ASSERT(fp->flags & JSFRAME_COMPILING);
obj = fp->varobj;
if (obj == fp->scopeChain) {
/* XXX this will need revising when 'let const' is added. */
stmt = js_LexicalLookup(&cg->treeContext, atom, &slot, JS_FALSE);
if (stmt)
return JS_TRUE;
ATOM_LIST_SEARCH(ale, &cg->constList, atom);
if (ale) {
*vp = ALE_VALUE(ale);
return JS_TRUE;
}
/*
* Try looking in the variable object for a direct property that
* is readonly and permanent. We know such a property can't be
* shadowed by another property on obj's prototype chain, or a
* with object or catch variable; nor can prop's value be changed,
* nor can prop be deleted.
*/
prop = NULL;
if (OBJ_GET_CLASS(cx, obj) == &js_FunctionClass) {
ok = js_LookupHiddenProperty(cx, obj, ATOM_TO_JSID(atom),
&pobj, &prop);
if (!ok)
break;
if (prop) {
#ifdef DEBUG
JSScopeProperty *sprop = (JSScopeProperty *)prop;
/*
* Any hidden property must be a formal arg or local var,
* which will shadow a global const of the same name.
*/
JS_ASSERT(sprop->getter == js_GetArgument ||
sprop->getter == js_GetLocalVariable);
#endif
OBJ_DROP_PROPERTY(cx, pobj, prop);
break;
}
}
ok = OBJ_LOOKUP_PROPERTY(cx, obj, ATOM_TO_JSID(atom), &pobj, &prop);
if (ok) {
if (pobj == obj &&
(fp->flags & (JSFRAME_EVAL | JSFRAME_COMPILE_N_GO))) {
/*
* We're compiling code that will be executed immediately,
* not re-executed against a different scope chain and/or
* variable object. Therefore we can get constant values
* from our variable object here.
*/
ok = OBJ_GET_ATTRIBUTES(cx, obj, ATOM_TO_JSID(atom), prop,
&attrs);
if (ok && !(~attrs & (JSPROP_READONLY | JSPROP_PERMANENT)))
ok = OBJ_GET_PROPERTY(cx, obj, ATOM_TO_JSID(atom), vp);
}
if (prop)
OBJ_DROP_PROPERTY(cx, pobj, prop);
}
if (!ok || prop)
break;
}
fp = fp->down;
} while ((cg = cg->parent) != NULL);
return ok;
}
/*
* Allocate an index invariant for all activations of the code being compiled
* in cg, that can be used to store and fetch a reference to a cloned RegExp
* object that shares the same JSRegExp private data created for the object
* literal in pn->pn_atom. We need clones to hold lastIndex and other direct
* properties that should not be shared among threads sharing a precompiled
* function or script.
*
* If the code being compiled is function code, allocate a reserved slot in
* the cloned function object that shares its precompiled script with other
* cloned function objects and with the compiler-created clone-parent. There
* are fun->nregexps such reserved slots in each function object cloned from
* fun->object. NB: during compilation, funobj slots must never be allocated,
* because js_AllocSlot could hand out one of the slots that should be given
* to a regexp clone.
*
* If the code being compiled is global code, reserve the fp->vars slot at
* ALE_INDEX(ale), by ensuring that cg->treeContext.numGlobalVars is at least
* one more than this index. For global code, fp->vars is parallel to the
* global script->atomMap.vector array, but possibly shorter for the common
* case (where var declarations and regexp literals cluster toward the front
* of the script or function body).
*
* Global variable name literals in script->atomMap have fast-global slot
* numbers (stored as int-tagged jsvals) in the corresponding fp->vars array
* element. The atomIndex for a regexp object literal thus also addresses an
* fp->vars element that is not used by any optimized global variable, so we
* use that GC-scanned element to keep the regexp object clone alive, as well
* as to lazily create and find it at run-time for the JSOP_REGEXP bytecode.
*
* In no case can cx->fp->varobj be a Call object here, because that implies
* we are compiling eval code, in which case (cx->fp->flags & JSFRAME_EVAL)
* is true, and js_GetToken will have already selected JSOP_OBJECT instead of
* JSOP_REGEXP, to avoid all this RegExp object cloning business.
*
* Why clone regexp objects? ECMA specifies that when a regular expression
* literal is scanned, a RegExp object is created. In the spec, compilation
* and execution happen indivisibly, but in this implementation and many of
* its embeddings, code is precompiled early and re-executed in multiple
* threads, or using multiple global objects, or both, for efficiency.
*
* In such cases, naively following ECMA leads to wrongful sharing of RegExp
* objects, which makes for collisions on the lastIndex property (especially
* for global regexps) and on any ad-hoc properties. Also, __proto__ and
* __parent__ refer to the pre-compilation prototype and global objects, a
* pigeon-hole problem for instanceof tests.
*/
static JSBool
IndexRegExpClone(JSContext *cx, JSParseNode *pn, JSAtomListElement *ale,
JSCodeGenerator *cg)
{
JSObject *varobj, *reobj;
JSClass *clasp;
JSFunction *fun;
JSRegExp *re;
uint16 *countPtr;
uintN cloneIndex;
JS_ASSERT(!(cx->fp->flags & (JSFRAME_EVAL | JSFRAME_COMPILE_N_GO)));
varobj = cx->fp->varobj;
clasp = OBJ_GET_CLASS(cx, varobj);
if (clasp == &js_FunctionClass) {
fun = (JSFunction *) JS_GetPrivate(cx, varobj);
countPtr = &fun->u.i.nregexps;
cloneIndex = *countPtr;
} else {
JS_ASSERT(clasp != &js_CallClass);
countPtr = &cg->treeContext.numGlobalVars;
cloneIndex = ALE_INDEX(ale);
}
if ((cloneIndex + 1) >> 16) {
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
JSMSG_NEED_DIET, js_script_str);
return JS_FALSE;
}
if (cloneIndex >= *countPtr)
*countPtr = cloneIndex + 1;
reobj = ATOM_TO_OBJECT(pn->pn_atom);
JS_ASSERT(OBJ_GET_CLASS(cx, reobj) == &js_RegExpClass);
re = (JSRegExp *) JS_GetPrivate(cx, reobj);
re->cloneIndex = cloneIndex;
return JS_TRUE;
}
/*
* Emit a bytecode and its 2-byte constant (atom) index immediate operand.
* If the atomIndex requires more than 2 bytes, emit a prefix op whose 24-bit
* immediate operand indexes the atom in script->atomMap.
*
* If op has JOF_NAME mode, emit JSOP_FINDNAME to find and push the object in
* the scope chain in which the literal name was found, followed by the name
* as a string. This enables us to use the JOF_ELEM counterpart to op.
*
* Otherwise, if op has JOF_PROP mode, emit JSOP_LITERAL before op, to push
* the atom's value key. For JOF_PROP ops, the object being operated on has
* already been pushed, and JSOP_LITERAL will push the id, leaving the stack
* in the proper state for a JOF_ELEM counterpart.
*
* Otherwise, emit JSOP_LITOPX to push the atom index, then perform a special
* dispatch on op, but getting op's atom index from the stack instead of from
* an unsigned 16-bit immediate operand.
*/
static JSBool
EmitAtomIndexOp(JSContext *cx, JSOp op, jsatomid atomIndex, JSCodeGenerator *cg)
{
uint32 mode;
JSOp prefixOp;
ptrdiff_t off;
jsbytecode *pc;
if (atomIndex >= JS_BIT(16)) {
mode = (js_CodeSpec[op].format & JOF_MODEMASK);
if (op != JSOP_SETNAME) {
prefixOp = ((mode != JOF_NAME && mode != JOF_PROP) ||
#if JS_HAS_XML_SUPPORT
op == JSOP_GETMETHOD ||
op == JSOP_SETMETHOD ||
#endif
op == JSOP_SETCONST)
? JSOP_LITOPX
: (mode == JOF_NAME)
? JSOP_FINDNAME
: JSOP_LITERAL;
off = js_EmitN(cx, cg, prefixOp, 3);
if (off < 0)
return JS_FALSE;
pc = CG_CODE(cg, off);
SET_LITERAL_INDEX(pc, atomIndex);
}
switch (op) {
case JSOP_DECNAME: op = JSOP_DECELEM; break;
case JSOP_DECPROP: op = JSOP_DECELEM; break;
case JSOP_DELNAME: op = JSOP_DELELEM; break;
case JSOP_DELPROP: op = JSOP_DELELEM; break;
case JSOP_FORNAME: op = JSOP_FORELEM; break;
case JSOP_FORPROP: op = JSOP_FORELEM; break;
case JSOP_GETPROP: op = JSOP_GETELEM; break;
case JSOP_GETXPROP: op = JSOP_GETXELEM; break;
case JSOP_IMPORTPROP: op = JSOP_IMPORTELEM; break;
case JSOP_INCNAME: op = JSOP_INCELEM; break;
case JSOP_INCPROP: op = JSOP_INCELEM; break;
case JSOP_INITPROP: op = JSOP_INITELEM; break;
case JSOP_NAME: op = JSOP_GETELEM; break;
case JSOP_NAMEDEC: op = JSOP_ELEMDEC; break;
case JSOP_NAMEINC: op = JSOP_ELEMINC; break;
case JSOP_PROPDEC: op = JSOP_ELEMDEC; break;
case JSOP_PROPINC: op = JSOP_ELEMINC; break;
case JSOP_BINDNAME: return JS_TRUE;
case JSOP_SETNAME: op = JSOP_SETELEM; break;
case JSOP_SETPROP: op = JSOP_SETELEM; break;
#if JS_HAS_EXPORT_IMPORT
case JSOP_EXPORTNAME:
ReportStatementTooLarge(cx, cg);
return JS_FALSE;
#endif
default:
#if JS_HAS_XML_SUPPORT
JS_ASSERT(mode == 0 || op == JSOP_SETCONST ||
op == JSOP_GETMETHOD || op == JSOP_SETMETHOD);
#else
JS_ASSERT(mode == 0 || op == JSOP_SETCONST);
#endif
break;
}
return js_Emit1(cx, cg, op) >= 0;
}
EMIT_UINT16_IMM_OP(op, atomIndex);
return JS_TRUE;
}
/*
* Slight sugar for EmitAtomIndexOp, again accessing cx and cg from the macro
* caller's lexical environment, and embedding a false return on error.
* XXXbe hey, who checks for fun->nvars and fun->nargs overflow?!
*/
#define EMIT_ATOM_INDEX_OP(op, atomIndex) \
JS_BEGIN_MACRO \
if (!EmitAtomIndexOp(cx, op, atomIndex, cg)) \
return JS_FALSE; \
JS_END_MACRO
static JSBool
EmitAtomOp(JSContext *cx, JSParseNode *pn, JSOp op, JSCodeGenerator *cg)
{
JSAtomListElement *ale;
ale = js_IndexAtom(cx, pn->pn_atom, &cg->atomList);
if (!ale)
return JS_FALSE;
if (op == JSOP_REGEXP && !IndexRegExpClone(cx, pn, ale, cg))
return JS_FALSE;
return EmitAtomIndexOp(cx, op, ALE_INDEX(ale), cg);
}
/*
* This routine tries to optimize name gets and sets to stack slot loads and
* stores, given the variables object and scope chain in cx's top frame, the
* compile-time context in tc, and a TOK_NAME node pn. It returns false on
* error, true on success.
*
* The caller can inspect pn->pn_slot for a non-negative slot number to tell
* whether optimization occurred, in which case BindNameToSlot also updated
* pn->pn_op. If pn->pn_slot is still -1 on return, pn->pn_op nevertheless
* may have been optimized, e.g., from JSOP_NAME to JSOP_ARGUMENTS. Whether
* or not pn->pn_op was modified, if this function finds an argument or local
* variable name, pn->pn_attrs will contain the property's attributes after a
* successful return.
*
* NB: if you add more opcodes specialized from JSOP_NAME, etc., don't forget
* to update the TOK_FOR (for-in) and TOK_ASSIGN (op=, e.g. +=) special cases
* in js_EmitTree.
*/
static JSBool
BindNameToSlot(JSContext *cx, JSTreeContext *tc, JSParseNode *pn,
JSBool letdecl)
{
JSAtom *atom;
JSStmtInfo *stmt;
jsint slot;
JSOp op;
JSStackFrame *fp;
JSObject *obj, *pobj;
JSClass *clasp;
JSBool optimizeGlobals;
JSPropertyOp getter;
uintN attrs;
JSAtomListElement *ale;
JSProperty *prop;
JSScopeProperty *sprop;
JS_ASSERT(pn->pn_type == TOK_NAME);
if (pn->pn_slot >= 0 || pn->pn_op == JSOP_ARGUMENTS)
return JS_TRUE;
/* QNAME references can never be optimized to use arg/var storage. */
if (pn->pn_op == JSOP_QNAMEPART)
return JS_TRUE;
/*
* We can't optimize if we are compiling a with statement and its body,
* or we're in a catch block whose exception variable has the same name
* as this node. FIXME: we should be able to optimize catch vars to be
* block-locals.
*/
atom = pn->pn_atom;
stmt = js_LexicalLookup(tc, atom, &slot, letdecl);
if (stmt) {
if (stmt->type == STMT_WITH)
return JS_TRUE;
JS_ASSERT(stmt->flags & SIF_SCOPE);
JS_ASSERT(slot >= 0);
op = pn->pn_op;
switch (op) {
case JSOP_NAME: op = JSOP_GETLOCAL; break;
case JSOP_SETNAME: op = JSOP_SETLOCAL; break;
case JSOP_INCNAME: op = JSOP_INCLOCAL; break;
case JSOP_NAMEINC: op = JSOP_LOCALINC; break;
case JSOP_DECNAME: op = JSOP_DECLOCAL; break;
case JSOP_NAMEDEC: op = JSOP_LOCALDEC; break;
case JSOP_FORNAME: op = JSOP_FORLOCAL; break;
case JSOP_DELNAME: op = JSOP_FALSE; break;
default: JS_ASSERT(0);
}
if (op != pn->pn_op) {
pn->pn_op = op;
pn->pn_slot = slot;
}
return JS_TRUE;
}
/*
* A Script object can be used to split an eval into a compile step done
* at construction time, and an execute step done separately, possibly in
* a different scope altogether. We therefore cannot do any name-to-slot
* optimizations, but must lookup names at runtime. Note that script_exec
* ensures that its caller's frame has a Call object, so arg and var name
* lookups will succeed.
*/
fp = cx->fp;
if (fp->flags & JSFRAME_SCRIPT_OBJECT)
return JS_TRUE;
/*
* We can't optimize if var and closure (a local function not in a larger
* expression and not at top-level within another's body) collide.
* XXX suboptimal: keep track of colliding names and deoptimize only those
*/
if (tc->flags & TCF_FUN_CLOSURE_VS_VAR)
return JS_TRUE;
/*
* We can't optimize if we're not compiling a function body, whether via
* eval, or directly when compiling a function statement or expression.
*/
obj = fp->varobj;
clasp = OBJ_GET_CLASS(cx, obj);
if (clasp != &js_FunctionClass && clasp != &js_CallClass) {
/* Check for an eval or debugger frame. */
if (fp->flags & JSFRAME_SPECIAL)
return JS_TRUE;
/*
* Optimize global variable accesses if there are at least 100 uses
* in unambiguous contexts, or failing that, if least half of all the
* uses of global vars/consts/functions are in loops.
*/
optimizeGlobals = (tc->globalUses >= 100 ||
(tc->loopyGlobalUses &&
tc->loopyGlobalUses >= tc->globalUses / 2));
if (!optimizeGlobals)
return JS_TRUE;
} else {
optimizeGlobals = JS_FALSE;
}
/*
* We can't optimize if we are in an eval called inside a with statement.
*/
if (fp->scopeChain != obj)
return JS_TRUE;
op = pn->pn_op;
getter = NULL;
#ifdef __GNUC__
attrs = slot = 0; /* quell GCC overwarning */
#endif
if (optimizeGlobals) {
/*
* We are optimizing global variables, and there is no pre-existing
* global property named atom. If atom was declared via const or var,
* optimize pn to access fp->vars using the appropriate JOF_QVAR op.
*/
ATOM_LIST_SEARCH(ale, &tc->decls, atom);
if (!ale) {
/* Use precedes declaration, or name is never declared. */
return JS_TRUE;
}
attrs = (ALE_JSOP(ale) == JSOP_DEFCONST)
? JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT
: JSPROP_ENUMERATE | JSPROP_PERMANENT;
/* Index atom so we can map fast global number to name. */
JS_ASSERT(tc->flags & TCF_COMPILING);
ale = js_IndexAtom(cx, atom, &((JSCodeGenerator *) tc)->atomList);
if (!ale)
return JS_FALSE;
/* Defend against tc->numGlobalVars 16-bit overflow. */
slot = ALE_INDEX(ale);
if ((slot + 1) >> 16)
return JS_TRUE;
if ((uint16)(slot + 1) > tc->numGlobalVars)
tc->numGlobalVars = (uint16)(slot + 1);
} else {
/*
* We may be able to optimize name to stack slot. Look for an argument
* or variable property in the function, or its call object, not found
* in any prototype object. Rewrite pn_op and update pn accordingly.
* NB: We know that JSOP_DELNAME on an argument or variable evaluates
* to false, due to JSPROP_PERMANENT.
*/
if (!js_LookupHiddenProperty(cx, obj, ATOM_TO_JSID(atom), &pobj, &prop))
return JS_FALSE;
sprop = (JSScopeProperty *) prop;
if (sprop) {
if (pobj == obj) {
getter = sprop->getter;
attrs = sprop->attrs;
slot = (sprop->flags & SPROP_HAS_SHORTID) ? sprop->shortid : -1;
}
OBJ_DROP_PROPERTY(cx, pobj, prop);
}
}
if (optimizeGlobals || getter) {
if (optimizeGlobals) {
switch (op) {
case JSOP_NAME: op = JSOP_GETGVAR; break;
case JSOP_SETNAME: op = JSOP_SETGVAR; break;
case JSOP_SETCONST: /* NB: no change */ break;
case JSOP_INCNAME: op = JSOP_INCGVAR; break;
case JSOP_NAMEINC: op = JSOP_GVARINC; break;
case JSOP_DECNAME: op = JSOP_DECGVAR; break;
case JSOP_NAMEDEC: op = JSOP_GVARDEC; break;
case JSOP_FORNAME: /* NB: no change */ break;
case JSOP_DELNAME: /* NB: no change */ break;
default: JS_ASSERT(0);
}
} else if (getter == js_GetLocalVariable ||
getter == js_GetCallVariable) {
switch (op) {
case JSOP_NAME: op = JSOP_GETVAR; break;
case JSOP_SETNAME: op = JSOP_SETVAR; break;
case JSOP_SETCONST: op = JSOP_SETVAR; break;
case JSOP_INCNAME: op = JSOP_INCVAR; break;
case JSOP_NAMEINC: op = JSOP_VARINC; break;
case JSOP_DECNAME: op = JSOP_DECVAR; break;
case JSOP_NAMEDEC: op = JSOP_VARDEC; break;
case JSOP_FORNAME: op = JSOP_FORVAR; break;
case JSOP_DELNAME: op = JSOP_FALSE; break;
default: JS_ASSERT(0);
}
} else if (getter == js_GetArgument ||
(getter == js_CallClass.getProperty &&
fp->fun && (uintN) slot < fp->fun->nargs)) {
switch (op) {
case JSOP_NAME: op = JSOP_GETARG; break;
case JSOP_SETNAME: op = JSOP_SETARG; break;
case JSOP_INCNAME: op = JSOP_INCARG; break;
case JSOP_NAMEINC: op = JSOP_ARGINC; break;
case JSOP_DECNAME: op = JSOP_DECARG; break;
case JSOP_NAMEDEC: op = JSOP_ARGDEC; break;
case JSOP_FORNAME: op = JSOP_FORARG; break;
case JSOP_DELNAME: op = JSOP_FALSE; break;
default: JS_ASSERT(0);
}
}
if (op != pn->pn_op) {
pn->pn_op = op;
pn->pn_slot = slot;
}
pn->pn_attrs = attrs;
}
if (pn->pn_slot < 0) {
/*
* We couldn't optimize pn, so it's not a global or local slot name.
* Now we must check for the predefined arguments variable. It may be
* overridden by assignment, in which case the function is heavyweight
* and the interpreter will look up 'arguments' in the function's call
* object.
*/
if (pn->pn_op == JSOP_NAME &&
atom == cx->runtime->atomState.argumentsAtom) {
pn->pn_op = JSOP_ARGUMENTS;
return JS_TRUE;
}
tc->flags |= TCF_FUN_USES_NONLOCALS;
}
return JS_TRUE;
}
/*
* If pn contains a useful expression, return true with *answer set to true.
* If pn contains a useless expression, return true with *answer set to false.
* Return false on error.
*
* The caller should initialize *answer to false and invoke this function on
* an expression statement or similar subtree to decide whether the tree could
* produce code that has any side effects. For an expression statement, we
* define useless code as code with no side effects, because the main effect,
* the value left on the stack after the code executes, will be discarded by a
* pop bytecode.
*/
static JSBool
CheckSideEffects(JSContext *cx, JSTreeContext *tc, JSParseNode *pn,
JSBool *answer)
{
JSBool ok;
JSFunction *fun;
JSParseNode *pn2;
ok = JS_TRUE;
if (!pn || *answer)
return ok;
switch (pn->pn_arity) {
case PN_FUNC:
/*
* A named function is presumed useful: we can't yet know that it is
* not called. The side effects are the creation of a scope object
* to parent this function object, and the binding of the function's
* name in that scope object. See comments at case JSOP_NAMEDFUNOBJ:
* in jsinterp.c.
*/
fun = (JSFunction *) JS_GetPrivate(cx, ATOM_TO_OBJECT(pn->pn_funAtom));
if (fun->atom)
*answer = JS_TRUE;
break;
case PN_LIST:
if (pn->pn_type == TOK_NEW ||
pn->pn_type == TOK_LP ||
pn->pn_type == TOK_LB ||
pn->pn_type == TOK_RB ||
pn->pn_type == TOK_RC) {
/*
* All invocation operations (construct: TOK_NEW, call: TOK_LP)
* are presumed to be useful, because they may have side effects
* even if their main effect (their return value) is discarded.
*
* TOK_LB binary trees of 3 or more nodes are flattened into lists
* to avoid too much recursion. All such lists must be presumed
* to be useful because each index operation could invoke a getter
* (the JSOP_ARGUMENTS special case below, in the PN_BINARY case,
* does not apply here: arguments[i][j] might invoke a getter).
*
* Array and object initializers (TOK_RB and TOK_RC lists) must be
* considered useful, because they are sugar for constructor calls
* (to Array and Object, respectively).
*/
*answer = JS_TRUE;
} else {
for (pn2 = pn->pn_head; pn2; pn2 = pn2->pn_next)
ok &= CheckSideEffects(cx, tc, pn2, answer);
}
break;
case PN_TERNARY:
ok = CheckSideEffects(cx, tc, pn->pn_kid1, answer) &&
CheckSideEffects(cx, tc, pn->pn_kid2, answer) &&
CheckSideEffects(cx, tc, pn->pn_kid3, answer);
break;
case PN_BINARY:
if (pn->pn_type == TOK_ASSIGN) {
/*
* Assignment is presumed to be useful, even if the next operation
* is another assignment overwriting this one's ostensible effect,
* because the left operand may be a property with a setter that
* has side effects.
*
* The only exception is assignment of a useless value to a const
* declared in the function currently being compiled.
*/
pn2 = pn->pn_left;
if (pn2->pn_type != TOK_NAME) {
*answer = JS_TRUE;
} else {
if (!BindNameToSlot(cx, tc, pn2, JS_FALSE))
return JS_FALSE;
if (!CheckSideEffects(cx, tc, pn->pn_right, answer))
return JS_FALSE;
if (!*answer &&
(pn2->pn_slot < 0 || !(pn2->pn_attrs & JSPROP_READONLY))) {
*answer = JS_TRUE;
}
}
} else {
if (pn->pn_type == TOK_LB) {
pn2 = pn->pn_left;
if (pn2->pn_type == TOK_NAME &&
!BindNameToSlot(cx, tc, pn2, JS_FALSE)) {
return JS_FALSE;
}
if (pn2->pn_op != JSOP_ARGUMENTS) {
/*
* Any indexed property reference could call a getter with
* side effects, except for arguments[i] where arguments is
* unambiguous.
*/
*answer = JS_TRUE;
}
}
ok = CheckSideEffects(cx, tc, pn->pn_left, answer) &&
CheckSideEffects(cx, tc, pn->pn_right, answer);
}
break;
case PN_UNARY:
if (pn->pn_type == TOK_INC || pn->pn_type == TOK_DEC ||
pn->pn_type == TOK_THROW ||
#if JS_HAS_GENERATORS
pn->pn_type == TOK_YIELD ||
#endif
pn->pn_type == TOK_DEFSHARP) {
/* All these operations have effects that we must commit. */
*answer = JS_TRUE;
} else if (pn->pn_type == TOK_DELETE) {
pn2 = pn->pn_kid;
switch (pn2->pn_type) {
case TOK_NAME:
case TOK_DOT:
#if JS_HAS_XML_SUPPORT
case TOK_DBLDOT:
#endif
#if JS_HAS_LVALUE_RETURN
case TOK_LP:
#endif
case TOK_LB:
/* All these delete addressing modes have effects too. */
*answer = JS_TRUE;
break;
default:
ok = CheckSideEffects(cx, tc, pn2, answer);
break;
}
} else {
ok = CheckSideEffects(cx, tc, pn->pn_kid, answer);
}
break;
case PN_NAME:
/*
* Take care to avoid trying to bind a label name (labels, both for
* statements and property values in object initialisers, have pn_op
* defaulted to JSOP_NOP).
*/
if (pn->pn_type == TOK_NAME && pn->pn_op != JSOP_NOP) {
if (!BindNameToSlot(cx, tc, pn, JS_FALSE))
return JS_FALSE;
if (pn->pn_slot < 0 && pn->pn_op != JSOP_ARGUMENTS) {
/*
* Not an argument or local variable use, so this expression
* could invoke a getter that has side effects.
*/
*answer = JS_TRUE;
}
}
pn2 = pn->pn_expr;
if (pn->pn_type == TOK_DOT) {
if (pn2->pn_type == TOK_NAME &&
!BindNameToSlot(cx, tc, pn2, JS_FALSE)) {
return JS_FALSE;
}
if (!(pn2->pn_op == JSOP_ARGUMENTS &&
pn->pn_atom == cx->runtime->atomState.lengthAtom)) {
/*
* Any dotted property reference could call a getter, except
* for arguments.length where arguments is unambiguous.
*/
*answer = JS_TRUE;
}
}
ok = CheckSideEffects(cx, tc, pn2, answer);
break;
case PN_NULLARY:
if (pn->pn_type == TOK_DEBUGGER)
*answer = JS_TRUE;
break;
}
return ok;
}
/*
* Secret handshake with js_EmitTree's TOK_LP/TOK_NEW case logic, to flag all
* uses of JSOP_GETMETHOD that implicitly qualify the method property's name
* with a function:: prefix. All other JSOP_GETMETHOD and JSOP_SETMETHOD uses
* must be explicit, so we need a distinct source note (SRC_METHODBASE rather
* than SRC_PCBASE) for round-tripping through the beloved decompiler.
*/
#define JSPROP_IMPLICIT_FUNCTION_NAMESPACE 0x100
static jssrcnote
SrcNoteForPropOp(JSParseNode *pn, JSOp op)
{
return ((op == JSOP_GETMETHOD &&
!(pn->pn_attrs & JSPROP_IMPLICIT_FUNCTION_NAMESPACE)) ||
op == JSOP_SETMETHOD)
? SRC_METHODBASE
: SRC_PCBASE;
}
static JSBool
EmitPropOp(JSContext *cx, JSParseNode *pn, JSOp op, JSCodeGenerator *cg)
{
JSParseNode *pn2, *pndot, *pnup, *pndown;
ptrdiff_t top;
pn2 = pn->pn_expr;
if (op == JSOP_GETPROP &&
pn->pn_type == TOK_DOT &&
pn2->pn_type == TOK_NAME) {
/* Try to optimize arguments.length into JSOP_ARGCNT. */
if (!BindNameToSlot(cx, &cg->treeContext, pn2, JS_FALSE))
return JS_FALSE;
if (pn2->pn_op == JSOP_ARGUMENTS &&
pn->pn_atom == cx->runtime->atomState.lengthAtom) {
return js_Emit1(cx, cg, JSOP_ARGCNT) >= 0;
}
}
/*
* If the object operand is also a dotted property reference, reverse the
* list linked via pn_expr temporarily so we can iterate over it from the
* bottom up (reversing again as we go), to avoid excessive recursion.
*/
if (pn2->pn_type == TOK_DOT) {
pndot = pn2;
pnup = NULL;
top = CG_OFFSET(cg);
for (;;) {
/* Reverse pndot->pn_expr to point up, not down. */
pndot->pn_offset = top;
pndown = pndot->pn_expr;
pndot->pn_expr = pnup;
if (pndown->pn_type != TOK_DOT)
break;
pnup = pndot;
pndot = pndown;
}
/* pndown is a primary expression, not a dotted property reference. */
if (!js_EmitTree(cx, cg, pndown))
return JS_FALSE;
do {
/* Walk back up the list, emitting annotated name ops. */
if (js_NewSrcNote2(cx, cg, SrcNoteForPropOp(pndot, pndot->pn_op),
CG_OFFSET(cg) - pndown->pn_offset) < 0) {
return JS_FALSE;
}
if (!EmitAtomOp(cx, pndot, pndot->pn_op, cg))
return JS_FALSE;
/* Reverse the pn_expr link again. */
pnup = pndot->pn_expr;
pndot->pn_expr = pndown;
pndown = pndot;
} while ((pndot = pnup) != NULL);
} else {
if (!js_EmitTree(cx, cg, pn2))
return JS_FALSE;
}
if (js_NewSrcNote2(cx, cg, SrcNoteForPropOp(pn, op),
CG_OFFSET(cg) - pn2->pn_offset) < 0) {
return JS_FALSE;
}
if (!pn->pn_atom) {
JS_ASSERT(op == JSOP_IMPORTALL);
if (js_Emit1(cx, cg, op) < 0)
return JS_FALSE;
} else {
if (!EmitAtomOp(cx, pn, op, cg))
return JS_FALSE;
}
return JS_TRUE;
}
static JSBool
EmitElemOp(JSContext *cx, JSParseNode *pn, JSOp op, JSCodeGenerator *cg)
{
ptrdiff_t top;
JSParseNode *left, *right, *next, ltmp, rtmp;
jsint slot;
top = CG_OFFSET(cg);
if (pn->pn_arity == PN_LIST) {
/* Left-associative operator chain to avoid too much recursion. */
JS_ASSERT(pn->pn_op == JSOP_GETELEM || pn->pn_op == JSOP_IMPORTELEM);
JS_ASSERT(pn->pn_count >= 3);
left = pn->pn_head;
right = PN_LAST(pn);
next = left->pn_next;
JS_ASSERT(next != right);
/*
* Try to optimize arguments[0][j]... into JSOP_ARGSUB<0> followed by
* one or more index expression and JSOP_GETELEM op pairs.
*/
if (left->pn_type == TOK_NAME && next->pn_type == TOK_NUMBER) {
if (!BindNameToSlot(cx, &cg->treeContext, left, JS_FALSE))
return JS_FALSE;
if (left->pn_op == JSOP_ARGUMENTS &&
JSDOUBLE_IS_INT(next->pn_dval, slot) &&
(jsuint)slot < JS_BIT(16)) {
left->pn_offset = next->pn_offset = top;
EMIT_UINT16_IMM_OP(JSOP_ARGSUB, (jsatomid)slot);
left = next;
next = left->pn_next;
}
}
/*
* Check whether we generated JSOP_ARGSUB, just above, and have only
* one more index expression to emit. Given arguments[0][j], we must
* skip the while loop altogether, falling through to emit code for j
* (in the subtree referenced by right), followed by the annotated op,
* at the bottom of this function.
*/
JS_ASSERT(next != right || pn->pn_count == 3);
if (left == pn->pn_head) {
if (!js_EmitTree(cx, cg, left))
return JS_FALSE;
}
while (next != right) {
if (!js_EmitTree(cx, cg, next))
return JS_FALSE;
if (js_NewSrcNote2(cx, cg, SRC_PCBASE, CG_OFFSET(cg) - top) < 0)
return JS_FALSE;
if (js_Emit1(cx, cg, JSOP_GETELEM) < 0)
return JS_FALSE;
next = next->pn_next;
}
} else {
if (pn->pn_arity == PN_NAME) {
/*
* Set left and right so pn appears to be a TOK_LB node, instead
* of a TOK_DOT node. See the TOK_FOR/IN case in js_EmitTree, and
* EmitDestructuringOps nearer below. In the destructuring case,
* the base expression (pn_expr) of the name may be null, which
* means we have to emit a JSOP_BINDNAME.
*/
left = pn->pn_expr;
if (!left) {
left = <mp;
left->pn_type = TOK_OBJECT;
left->pn_op = JSOP_BINDNAME;
left->pn_arity = PN_NULLARY;
left->pn_pos = pn->pn_pos;
left->pn_atom = pn->pn_atom;
}
right = &rtmp;
right->pn_type = TOK_STRING;
JS_ASSERT(ATOM_IS_STRING(pn->pn_atom));
right->pn_op = js_IsIdentifier(ATOM_TO_STRING(pn->pn_atom))
? JSOP_QNAMEPART
: JSOP_STRING;
right->pn_arity = PN_NULLARY;
right->pn_pos = pn->pn_pos;
right->pn_atom = pn->pn_atom;
} else {
JS_ASSERT(pn->pn_arity == PN_BINARY);
left = pn->pn_left;
right = pn->pn_right;
}
/* Try to optimize arguments[0] (e.g.) into JSOP_ARGSUB<0>. */
if (op == JSOP_GETELEM &&
left->pn_type == TOK_NAME &&
right->pn_type == TOK_NUMBER) {
if (!BindNameToSlot(cx, &cg->treeContext, left, JS_FALSE))
return JS_FALSE;
if (left->pn_op == JSOP_ARGUMENTS &&
JSDOUBLE_IS_INT(right->pn_dval, slot) &&
(jsuint)slot < JS_BIT(16)) {
left->pn_offset = right->pn_offset = top;
EMIT_UINT16_IMM_OP(JSOP_ARGSUB, (jsatomid)slot);
return JS_TRUE;
}
}
if (!js_EmitTree(cx, cg, left))
return JS_FALSE;
}
/* The right side of the descendant operator is implicitly quoted. */
JS_ASSERT(op != JSOP_DESCENDANTS || right->pn_type != TOK_STRING ||
right->pn_op == JSOP_QNAMEPART);
if (!js_EmitTree(cx, cg, right))
return JS_FALSE;
if (js_NewSrcNote2(cx, cg, SRC_PCBASE, CG_OFFSET(cg) - top) < 0)
return JS_FALSE;
return js_Emit1(cx, cg, op) >= 0;
}
static JSBool
EmitNumberOp(JSContext *cx, jsdouble dval, JSCodeGenerator *cg)
{
jsint ival;
jsatomid atomIndex;
ptrdiff_t off;
jsbytecode *pc;
JSAtom *atom;
JSAtomListElement *ale;
if (JSDOUBLE_IS_INT(dval, ival) && INT_FITS_IN_JSVAL(ival)) {
if (ival == 0)
return js_Emit1(cx, cg, JSOP_ZERO) >= 0;
if (ival == 1)
return js_Emit1(cx, cg, JSOP_ONE) >= 0;
atomIndex = (jsatomid)ival;
if (atomIndex < JS_BIT(16)) {
EMIT_UINT16_IMM_OP(JSOP_UINT16, atomIndex);
return JS_TRUE;
}
if (atomIndex < JS_BIT(24)) {
off = js_EmitN(cx, cg, JSOP_UINT24, 3);
if (off < 0)
return JS_FALSE;
pc = CG_CODE(cg, off);
SET_LITERAL_INDEX(pc, atomIndex);
return JS_TRUE;
}
atom = js_AtomizeInt(cx, ival, 0);
} else {
atom = js_AtomizeDouble(cx, dval, 0);
}
if (!atom)
return JS_FALSE;
ale = js_IndexAtom(cx, atom, &cg->atomList);
if (!ale)
return JS_FALSE;
return EmitAtomIndexOp(cx, JSOP_NUMBER, ALE_INDEX(ale), cg);
}
static JSBool
EmitSwitch(JSContext *cx, JSCodeGenerator *cg, JSParseNode *pn,
JSStmtInfo *stmtInfo)
{
JSOp switchOp;
JSBool ok, hasDefault, constPropagated;
ptrdiff_t top, off, defaultOffset;
JSParseNode *pn2, *pn3, *pn4;
uint32 caseCount, tableLength;
JSParseNode **table;
jsdouble d;
jsint i, low, high;
jsval v;
JSAtom *atom;
JSAtomListElement *ale;
intN noteIndex;
size_t switchSize, tableSize;
jsbytecode *pc, *savepc;
#if JS_HAS_BLOCK_SCOPE
JSObject *obj;
jsint count;
#endif
/* Try for most optimal, fall back if not dense ints, and per ECMAv2. */
switchOp = JSOP_TABLESWITCH;
ok = JS_TRUE;
hasDefault = constPropagated = JS_FALSE;
defaultOffset = -1;
/*
* If the switch contains let variables scoped by its body, model the
* resulting block on the stack first, before emitting the discriminant's
* bytecode (in case the discriminant contains a stack-model dependency
* such as a let expression).
*/
pn2 = pn->pn_right;
#if JS_HAS_BLOCK_SCOPE
if (pn2->pn_type == TOK_LEXICALSCOPE) {
atom = pn2->pn_atom;
obj = ATOM_TO_OBJECT(atom);
OBJ_SET_BLOCK_DEPTH(cx, obj, cg->stackDepth);
/*
* Push the body's block scope before discriminant code-gen for proper
* static block scope linkage in case the discriminant contains a let
* expression. The block's locals must lie under the discriminant on
* the stack so that case-dispatch bytecodes can find the discriminant
* on top of stack.
*/
js_PushBlockScope(&cg->treeContext, stmtInfo, atom, -1);
stmtInfo->type = STMT_SWITCH;
count = OBJ_BLOCK_COUNT(cx, obj);
cg->stackDepth += count;
if ((uintN)cg->stackDepth > cg->maxStackDepth)
cg->maxStackDepth = cg->stackDepth;
/* Emit JSOP_ENTERBLOCK before code to evaluate the discriminant. */
ale = js_IndexAtom(cx, atom, &cg->atomList);
if (!ale)
return JS_FALSE;
EMIT_ATOM_INDEX_OP(JSOP_ENTERBLOCK, ALE_INDEX(ale));
/*
* Pop the switch's statement info around discriminant code-gen. Note
* how this leaves cg->treeContext.blockChain referencing the switch's
* block scope object, which is necessary for correct block parenting
* in the case where the discriminant contains a let expression.
*/
cg->treeContext.topStmt = stmtInfo->down;
cg->treeContext.topScopeStmt = stmtInfo->downScope;
}
#ifdef __GNUC__
else {
atom = NULL;
count = -1;
}
#endif
#endif
/*
* Emit code for the discriminant first (or nearly first, in the case of a
* switch whose body is a block scope).
*/
if (!js_EmitTree(cx, cg, pn->pn_left))
return JS_FALSE;
/* Switch bytecodes run from here till end of final case. */
top = CG_OFFSET(cg);
#if !JS_HAS_BLOCK_SCOPE
js_PushStatement(&cg->treeContext, stmtInfo, STMT_SWITCH, top);
#else
if (pn2->pn_type == TOK_LC) {
js_PushStatement(&cg->treeContext, stmtInfo, STMT_SWITCH, top);
} else {
/* Re-push the switch's statement info record. */
cg->treeContext.topStmt = cg->treeContext.topScopeStmt = stmtInfo;
/* Set the statement info record's idea of top. */
stmtInfo->update = top;
/* Advance pn2 to refer to the switch case list. */
pn2 = pn2->pn_expr;
}
#endif
caseCount = pn2->pn_count;
tableLength = 0;
table = NULL;
if (caseCount == 0 ||
(caseCount == 1 &&
(hasDefault = (pn2->pn_head->pn_type == TOK_DEFAULT)))) {
caseCount = 0;
low = 0;
high = -1;
} else {
#define INTMAP_LENGTH 256
jsbitmap intmap_space[INTMAP_LENGTH];
jsbitmap *intmap = NULL;
int32 intmap_bitlen = 0;
low = JSVAL_INT_MAX;
high = JSVAL_INT_MIN;
for (pn3 = pn2->pn_head; pn3; pn3 = pn3->pn_next) {
if (pn3->pn_type == TOK_DEFAULT) {
hasDefault = JS_TRUE;
caseCount--; /* one of the "cases" was the default */
continue;
}
JS_ASSERT(pn3->pn_type == TOK_CASE);
if (switchOp == JSOP_CONDSWITCH)
continue;
pn4 = pn3->pn_left;
switch (pn4->pn_type) {
case TOK_NUMBER:
d = pn4->pn_dval;
if (JSDOUBLE_IS_INT(d, i) && INT_FITS_IN_JSVAL(i)) {
pn3->pn_val = INT_TO_JSVAL(i);
} else {
atom = js_AtomizeDouble(cx, d, 0);
if (!atom) {
ok = JS_FALSE;
goto release;
}
pn3->pn_val = ATOM_KEY(atom);
}
break;
case TOK_STRING:
pn3->pn_val = ATOM_KEY(pn4->pn_atom);
break;
case TOK_NAME:
if (!pn4->pn_expr) {
ok = js_LookupCompileTimeConstant(cx, cg, pn4->pn_atom, &v);
if (!ok)
goto release;
if (!JSVAL_IS_VOID(v)) {
pn3->pn_val = v;
constPropagated = JS_TRUE;
break;
}
}
/* FALL THROUGH */
case TOK_PRIMARY:
if (pn4->pn_op == JSOP_TRUE) {
pn3->pn_val = JSVAL_TRUE;
break;
}
if (pn4->pn_op == JSOP_FALSE) {
pn3->pn_val = JSVAL_FALSE;
break;
}
/* FALL THROUGH */
default:
switchOp = JSOP_CONDSWITCH;
continue;
}
JS_ASSERT(JSVAL_IS_NUMBER(pn3->pn_val) ||
JSVAL_IS_STRING(pn3->pn_val) ||
JSVAL_IS_BOOLEAN(pn3->pn_val));
if (switchOp != JSOP_TABLESWITCH)
continue;
if (!JSVAL_IS_INT(pn3->pn_val)) {
switchOp = JSOP_LOOKUPSWITCH;
continue;
}
i = JSVAL_TO_INT(pn3->pn_val);
if ((jsuint)(i + (jsint)JS_BIT(15)) >= (jsuint)JS_BIT(16)) {
switchOp = JSOP_LOOKUPSWITCH;
continue;
}
if (i < low)
low = i;
if (high < i)
high = i;
/*
* Check for duplicates, which require a JSOP_LOOKUPSWITCH.
* We bias i by 65536 if it's negative, and hope that's a rare
* case (because it requires a malloc'd bitmap).
*/
if (i < 0)
i += JS_BIT(16);
if (i >= intmap_bitlen) {
if (!intmap &&
i < (INTMAP_LENGTH << JS_BITS_PER_WORD_LOG2)) {
intmap = intmap_space;
intmap_bitlen = INTMAP_LENGTH << JS_BITS_PER_WORD_LOG2;
} else {
/* Just grab 8K for the worst-case bitmap. */
intmap_bitlen = JS_BIT(16);
intmap = (jsbitmap *)
JS_malloc(cx,
(JS_BIT(16) >> JS_BITS_PER_WORD_LOG2)
* sizeof(jsbitmap));
if (!intmap) {
JS_ReportOutOfMemory(cx);
return JS_FALSE;
}
}
memset(intmap, 0, intmap_bitlen >> JS_BITS_PER_BYTE_LOG2);
}
if (JS_TEST_BIT(intmap, i)) {
switchOp = JSOP_LOOKUPSWITCH;
continue;
}
JS_SET_BIT(intmap, i);
}
release:
if (intmap && intmap != intmap_space)
JS_free(cx, intmap);
if (!ok)
return JS_FALSE;
/*
* Compute table length and select lookup instead if overlarge or
* more than half-sparse.
*/
if (switchOp == JSOP_TABLESWITCH) {
tableLength = (uint32)(high - low + 1);
if (tableLength >= JS_BIT(16) || tableLength > 2 * caseCount)
switchOp = JSOP_LOOKUPSWITCH;
} else if (switchOp == JSOP_LOOKUPSWITCH) {
/*
* Lookup switch supports only atom indexes below 64K limit.
* Conservatively estimate the maximum possible index during
* switch generation and use conditional switch if it exceeds
* the limit.
*/
if (caseCount + cg->atomList.count > JS_BIT(16))
switchOp = JSOP_CONDSWITCH;
}
}
/*
* Emit a note with two offsets: first tells total switch code length,
* second tells offset to first JSOP_CASE if condswitch.
*/
noteIndex = js_NewSrcNote3(cx, cg, SRC_SWITCH, 0, 0);
if (noteIndex < 0)
return JS_FALSE;
if (switchOp == JSOP_CONDSWITCH) {
/*
* 0 bytes of immediate for unoptimized ECMAv2 switch.
*/
switchSize = 0;
} else if (switchOp == JSOP_TABLESWITCH) {
/*
* 3 offsets (len, low, high) before the table, 1 per entry.
*/
switchSize = (size_t)(JUMP_OFFSET_LEN * (3 + tableLength));
} else {
/*
* JSOP_LOOKUPSWITCH:
* 1 offset (len) and 1 atom index (npairs) before the table,
* 1 atom index and 1 jump offset per entry.
*/
switchSize = (size_t)(JUMP_OFFSET_LEN + ATOM_INDEX_LEN +
(ATOM_INDEX_LEN + JUMP_OFFSET_LEN) * caseCount);
}
/*
* Emit switchOp followed by switchSize bytes of jump or lookup table.
*
* If switchOp is JSOP_LOOKUPSWITCH or JSOP_TABLESWITCH, it is crucial
* to emit the immediate operand(s) by which bytecode readers such as
* BuildSpanDepTable discover the length of the switch opcode *before*
* calling js_SetJumpOffset (which may call BuildSpanDepTable). It's
* also important to zero all unknown jump offset immediate operands,
* so they can be converted to span dependencies with null targets to
* be computed later (js_EmitN zeros switchSize bytes after switchOp).
*/
if (js_EmitN(cx, cg, switchOp, switchSize) < 0)
return JS_FALSE;
off = -1;
if (switchOp == JSOP_CONDSWITCH) {
intN caseNoteIndex = -1;
JSBool beforeCases = JS_TRUE;
/* Emit code for evaluating cases and jumping to case statements. */
for (pn3 = pn2->pn_head; pn3; pn3 = pn3->pn_next) {
pn4 = pn3->pn_left;
if (pn4 && !js_EmitTree(cx, cg, pn4))
return JS_FALSE;
if (caseNoteIndex >= 0) {
/* off is the previous JSOP_CASE's bytecode offset. */
if (!js_SetSrcNoteOffset(cx, cg, (uintN)caseNoteIndex, 0,
CG_OFFSET(cg) - off)) {
return JS_FALSE;
}
}
if (!pn4) {
JS_ASSERT(pn3->pn_type == TOK_DEFAULT);
continue;
}
caseNoteIndex = js_NewSrcNote2(cx, cg, SRC_PCDELTA, 0);
if (caseNoteIndex < 0)
return JS_FALSE;
off = EmitJump(cx, cg, JSOP_CASE, 0);
if (off < 0)
return JS_FALSE;
pn3->pn_offset = off;
if (beforeCases) {
uintN noteCount, noteCountDelta;
/* Switch note's second offset is to first JSOP_CASE. */
noteCount = CG_NOTE_COUNT(cg);
if (!js_SetSrcNoteOffset(cx, cg, (uintN)noteIndex, 1,
off - top)) {
return JS_FALSE;
}
noteCountDelta = CG_NOTE_COUNT(cg) - noteCount;
if (noteCountDelta != 0)
caseNoteIndex += noteCountDelta;
beforeCases = JS_FALSE;
}
}
/*
* If we didn't have an explicit default (which could fall in between
* cases, preventing us from fusing this js_SetSrcNoteOffset with the
* call in the loop above), link the last case to the implicit default
* for the decompiler.
*/
if (!hasDefault &&
caseNoteIndex >= 0 &&
!js_SetSrcNoteOffset(cx, cg, (uintN)caseNoteIndex, 0,
CG_OFFSET(cg) - off)) {
return JS_FALSE;
}
/* Emit default even if no explicit default statement. */
defaultOffset = EmitJump(cx, cg, JSOP_DEFAULT, 0);
if (defaultOffset < 0)
return JS_FALSE;
} else {
pc = CG_CODE(cg, top + JUMP_OFFSET_LEN);
if (switchOp == JSOP_TABLESWITCH) {
/* Fill in switch bounds, which we know fit in 16-bit offsets. */
SET_JUMP_OFFSET(pc, low);
pc += JUMP_OFFSET_LEN;
SET_JUMP_OFFSET(pc, high);
pc += JUMP_OFFSET_LEN;
/*
* Use malloc to avoid arena bloat for programs with many switches.
* We free table if non-null at label out, so all control flow must
* exit this function through goto out or goto bad.
*/
if (tableLength != 0) {
tableSize = (size_t)tableLength * sizeof *table;
table = (JSParseNode **) JS_malloc(cx, tableSize);
if (!table)
return JS_FALSE;
memset(table, 0, tableSize);
for (pn3 = pn2->pn_head; pn3; pn3 = pn3->pn_next) {
if (pn3->pn_type == TOK_DEFAULT)
continue;
i = JSVAL_TO_INT(pn3->pn_val);
i -= low;
JS_ASSERT((uint32)i < tableLength);
table[i] = pn3;
}
}
} else {
JS_ASSERT(switchOp == JSOP_LOOKUPSWITCH);
/* Fill in the number of cases. */
SET_ATOM_INDEX(pc, caseCount);
pc += ATOM_INDEX_LEN;
}
/*
* After this point, all control flow involving JSOP_TABLESWITCH
* must set ok and goto out to exit this function. To keep things
* simple, all switchOp cases exit that way.
*/
if (constPropagated) {
/*
* Skip switchOp, as we are not setting jump offsets in the two
* for loops below. We'll restore CG_NEXT(cg) from savepc after,
* unless there was an error.
*/
savepc = CG_NEXT(cg);
CG_NEXT(cg) = pc + 1;
if (switchOp == JSOP_TABLESWITCH) {
for (i = 0; i < (jsint)tableLength; i++) {
pn3 = table[i];
if (pn3 &&
(pn4 = pn3->pn_left) != NULL &&
pn4->pn_type == TOK_NAME) {
/* Note a propagated constant with the const's name. */
JS_ASSERT(!pn4->pn_expr);
ale = js_IndexAtom(cx, pn4->pn_atom, &cg->atomList);
if (!ale)
goto bad;
CG_NEXT(cg) = pc;
if (js_NewSrcNote2(cx, cg, SRC_LABEL, (ptrdiff_t)
ALE_INDEX(ale)) < 0) {
goto bad;
}
}
pc += JUMP_OFFSET_LEN;
}
} else {
for (pn3 = pn2->pn_head; pn3; pn3 = pn3->pn_next) {
pn4 = pn3->pn_left;
if (pn4 && pn4->pn_type == TOK_NAME) {
/* Note a propagated constant with the const's name. */
JS_ASSERT(!pn4->pn_expr);
ale = js_IndexAtom(cx, pn4->pn_atom, &cg->atomList);
if (!ale)
goto bad;
CG_NEXT(cg) = pc;
if (js_NewSrcNote2(cx, cg, SRC_LABEL, (ptrdiff_t)
ALE_INDEX(ale)) < 0) {
goto bad;
}
}
pc += ATOM_INDEX_LEN + JUMP_OFFSET_LEN;
}
}
CG_NEXT(cg) = savepc;
}
}
/* Emit code for each case's statements, copying pn_offset up to pn3. */
for (pn3 = pn2->pn_head; pn3; pn3 = pn3->pn_next) {
if (switchOp == JSOP_CONDSWITCH && pn3->pn_type != TOK_DEFAULT)
CHECK_AND_SET_JUMP_OFFSET_AT(cx, cg, pn3->pn_offset);
pn4 = pn3->pn_right;
ok = js_EmitTree(cx, cg, pn4);
if (!ok)
goto out;
pn3->pn_offset = pn4->pn_offset;
if (pn3->pn_type == TOK_DEFAULT)
off = pn3->pn_offset - top;
}
if (!hasDefault) {
/* If no default case, offset for default is to end of switch. */
off = CG_OFFSET(cg) - top;
}
/* We better have set "off" by now. */
JS_ASSERT(off != -1);
/* Set the default offset (to end of switch if no default). */
if (switchOp == JSOP_CONDSWITCH) {
pc = NULL;
JS_ASSERT(defaultOffset != -1);
ok = js_SetJumpOffset(cx, cg, CG_CODE(cg, defaultOffset),
off - (defaultOffset - top));
if (!ok)
goto out;
} else {
pc = CG_CODE(cg, top);
ok = js_SetJumpOffset(cx, cg, pc, off);
if (!ok)
goto out;
pc += JUMP_OFFSET_LEN;
}
/* Set the SRC_SWITCH note's offset operand to tell end of switch. */
off = CG_OFFSET(cg) - top;
ok = js_SetSrcNoteOffset(cx, cg, (uintN)noteIndex, 0, off);
if (!ok)
goto out;
if (switchOp == JSOP_TABLESWITCH) {
/* Skip over the already-initialized switch bounds. */
pc += 2 * JUMP_OFFSET_LEN;
/* Fill in the jump table, if there is one. */
for (i = 0; i < (jsint)tableLength; i++) {
pn3 = table[i];
off = pn3 ? pn3->pn_offset - top : 0;
ok = js_SetJumpOffset(cx, cg, pc, off);
if (!ok)
goto out;
pc += JUMP_OFFSET_LEN;
}
} else if (switchOp == JSOP_LOOKUPSWITCH) {
/* Skip over the already-initialized number of cases. */
pc += ATOM_INDEX_LEN;
for (pn3 = pn2->pn_head; pn3; pn3 = pn3->pn_next) {
if (pn3->pn_type == TOK_DEFAULT)
continue;
atom = js_AtomizeValue(cx, pn3->pn_val, 0);
if (!atom)
goto bad;
ale = js_IndexAtom(cx, atom, &cg->atomList);
if (!ale)
goto bad;
SET_ATOM_INDEX(pc, ALE_INDEX(ale));
pc += ATOM_INDEX_LEN;
off = pn3->pn_offset - top;
ok = js_SetJumpOffset(cx, cg, pc, off);
if (!ok)
goto out;
pc += JUMP_OFFSET_LEN;
}
}
out:
if (table)
JS_free(cx, table);
if (ok) {
ok = js_PopStatementCG(cx, cg);
#if JS_HAS_BLOCK_SCOPE
if (ok && pn->pn_right->pn_type == TOK_LEXICALSCOPE) {
EMIT_UINT16_IMM_OP(JSOP_LEAVEBLOCK, count);
cg->stackDepth -= count;
}
#endif
}
return ok;
bad:
ok = JS_FALSE;
goto out;
}
JSBool
js_EmitFunctionBytecode(JSContext *cx, JSCodeGenerator *cg, JSParseNode *body)
{
if (!js_AllocTryNotes(cx, cg))
return JS_FALSE;
if (cg->treeContext.flags & TCF_FUN_IS_GENERATOR) {
/* JSOP_GENERATOR must be the first instruction. */
CG_SWITCH_TO_PROLOG(cg);
JS_ASSERT(CG_NEXT(cg) == CG_BASE(cg));
if (js_Emit1(cx, cg, JSOP_GENERATOR) < 0)
return JS_FALSE;
CG_SWITCH_TO_MAIN(cg);
}
return js_EmitTree(cx, cg, body) &&
js_Emit1(cx, cg, JSOP_STOP) >= 0;
}
JSBool
js_EmitFunctionBody(JSContext *cx, JSCodeGenerator *cg, JSParseNode *body,
JSFunction *fun)
{
JSStackFrame *fp, frame;
JSObject *funobj;
JSBool ok;
fp = cx->fp;
funobj = fun->object;
JS_ASSERT(!fp || (fp->fun != fun && fp->varobj != funobj &&
fp->scopeChain != funobj));
memset(&frame, 0, sizeof frame);
frame.fun = fun;
frame.varobj = frame.scopeChain = funobj;
frame.down = fp;
frame.flags = JS_HAS_COMPILE_N_GO_OPTION(cx)
? JSFRAME_COMPILING | JSFRAME_COMPILE_N_GO
: JSFRAME_COMPILING;
cx->fp = &frame;
ok = js_EmitFunctionBytecode(cx, cg, body);
cx->fp = fp;
if (!ok)
return JS_FALSE;
if (!js_NewScriptFromCG(cx, cg, fun))
return JS_FALSE;
JS_ASSERT(FUN_INTERPRETED(fun));
return JS_TRUE;
}
/* A macro for inlining at the top of js_EmitTree (whence it came). */
#define UPDATE_LINE_NUMBER_NOTES(cx, cg, pn) \
JS_BEGIN_MACRO \
uintN line_ = (pn)->pn_pos.begin.lineno; \
uintN delta_ = line_ - CG_CURRENT_LINE(cg); \
if (delta_ != 0) { \
/* \
* Encode any change in the current source line number by using \
* either several SRC_NEWLINE notes or just one SRC_SETLINE note, \
* whichever consumes less space. \
* \
* NB: We handle backward line number deltas (possible with for \
* loops where the update part is emitted after the body, but its \
* line number is <= any line number in the body) here by letting \
* unsigned delta_ wrap to a very large number, which triggers a \
* SRC_SETLINE. \
*/ \
CG_CURRENT_LINE(cg) = line_; \
if (delta_ >= (uintN)(2 + ((line_ > SN_3BYTE_OFFSET_MASK)<<1))) { \
if (js_NewSrcNote2(cx, cg, SRC_SETLINE, (ptrdiff_t)line_) < 0)\
return JS_FALSE; \
} else { \
do { \
if (js_NewSrcNote(cx, cg, SRC_NEWLINE) < 0) \
return JS_FALSE; \
} while (--delta_ != 0); \
} \
} \
JS_END_MACRO
/* A function, so that we avoid macro-bloating all the other callsites. */
static JSBool
UpdateLineNumberNotes(JSContext *cx, JSCodeGenerator *cg, JSParseNode *pn)
{
UPDATE_LINE_NUMBER_NOTES(cx, cg, pn);
return JS_TRUE;
}
static JSBool
MaybeEmitVarDecl(JSContext *cx, JSCodeGenerator *cg, JSOp prologOp,
JSParseNode *pn, jsatomid *result)
{
jsatomid atomIndex;
JSAtomListElement *ale;
if (pn->pn_slot >= 0) {
atomIndex = (jsatomid) pn->pn_slot;
} else {
ale = js_IndexAtom(cx, pn->pn_atom, &cg->atomList);
if (!ale)
return JS_FALSE;
atomIndex = ALE_INDEX(ale);
}
if ((js_CodeSpec[pn->pn_op].format & JOF_TYPEMASK) == JOF_CONST &&
(!(cg->treeContext.flags & TCF_IN_FUNCTION) ||
(cg->treeContext.flags & TCF_FUN_HEAVYWEIGHT))) {
/* Emit a prolog bytecode to predefine the variable. */
CG_SWITCH_TO_PROLOG(cg);
if (!UpdateLineNumberNotes(cx, cg, pn))
return JS_FALSE;
EMIT_ATOM_INDEX_OP(prologOp, atomIndex);
CG_SWITCH_TO_MAIN(cg);
}
if (result)
*result = atomIndex;
return JS_TRUE;
}
#if JS_HAS_DESTRUCTURING
typedef JSBool
(*DestructuringDeclEmitter)(JSContext *cx, JSCodeGenerator *cg, JSOp prologOp,
JSParseNode *pn);
static JSBool
EmitDestructuringDecl(JSContext *cx, JSCodeGenerator *cg, JSOp prologOp,
JSParseNode *pn)
{
JS_ASSERT(pn->pn_type == TOK_NAME);
if (!BindNameToSlot(cx, &cg->treeContext, pn, prologOp == JSOP_NOP))
return JS_FALSE;
JS_ASSERT(pn->pn_op != JSOP_ARGUMENTS);
return MaybeEmitVarDecl(cx, cg, prologOp, pn, NULL);
}
static JSBool
EmitDestructuringDecls(JSContext *cx, JSCodeGenerator *cg, JSOp prologOp,
JSParseNode *pn)
{
JSParseNode *pn2, *pn3;
DestructuringDeclEmitter emitter;
if (pn->pn_type == TOK_RB) {
for (pn2 = pn->pn_head; pn2; pn2 = pn2->pn_next) {
if (pn2->pn_type == TOK_COMMA)
continue;
emitter = (pn2->pn_type == TOK_NAME)
? EmitDestructuringDecl
: EmitDestructuringDecls;
if (!emitter(cx, cg, prologOp, pn2))
return JS_FALSE;
}
} else {
JS_ASSERT(pn->pn_type == TOK_RC);
for (pn2 = pn->pn_head; pn2; pn2 = pn2->pn_next) {
pn3 = pn2->pn_right;
emitter = (pn3->pn_type == TOK_NAME)
? EmitDestructuringDecl
: EmitDestructuringDecls;
if (!emitter(cx, cg, prologOp, pn3))
return JS_FALSE;
}
}
return JS_TRUE;
}
static JSBool
EmitDestructuringOpsHelper(JSContext *cx, JSCodeGenerator *cg, JSParseNode *pn);
static JSBool
EmitDestructuringLHS(JSContext *cx, JSCodeGenerator *cg, JSParseNode *pn,
JSBool wantpop)
{
jsuint slot;
/* Skip any parenthesization. */
while (pn->pn_type == TOK_RP)
pn = pn->pn_kid;
/*
* Now emit the lvalue opcode sequence. If the lvalue is a nested
* destructuring initialiser-form, call ourselves to handle it, then
* pop the matched value. Otherwise emit an lvalue bytecode sequence
* ending with a JSOP_ENUMELEM or equivalent op.
*/
if (pn->pn_type == TOK_RB || pn->pn_type == TOK_RC) {
if (!EmitDestructuringOpsHelper(cx, cg, pn))
return JS_FALSE;
if (wantpop && js_Emit1(cx, cg, JSOP_POP) < 0)
return JS_FALSE;
} else {
if (pn->pn_type == TOK_NAME &&
!BindNameToSlot(cx, &cg->treeContext, pn, JS_FALSE)) {
return JS_FALSE;
}
switch (pn->pn_op) {
case JSOP_SETNAME:
/*
* NB: pn is a PN_NAME node, not a PN_BINARY. Nevertheless,
* we want to emit JSOP_ENUMELEM, which has format JOF_ELEM.
* So here and for JSOP_ENUMCONSTELEM, we use EmitElemOp.
*/
if (!EmitElemOp(cx, pn, JSOP_ENUMELEM, cg))
return JS_FALSE;
break;
case JSOP_SETCONST:
if (!EmitElemOp(cx, pn, JSOP_ENUMCONSTELEM, cg))
return JS_FALSE;
break;
case JSOP_SETLOCAL:
if (wantpop) {
slot = (jsuint) pn->pn_slot;
EMIT_UINT16_IMM_OP(JSOP_SETLOCALPOP, slot);
break;
}
/* FALL THROUGH */
case JSOP_SETARG:
case JSOP_SETVAR:
case JSOP_SETGVAR:
slot = (jsuint) pn->pn_slot;
EMIT_UINT16_IMM_OP(pn->pn_op, slot);
if (wantpop && js_Emit1(cx, cg, JSOP_POP) < 0)
return JS_FALSE;
break;
default:
#if JS_HAS_LVALUE_RETURN || JS_HAS_XML_SUPPORT
{
ptrdiff_t top;
top = CG_OFFSET(cg);
if (!js_EmitTree(cx, cg, pn))
return JS_FALSE;
if (js_NewSrcNote2(cx, cg, SRC_PCBASE, CG_OFFSET(cg) - top) < 0)
return JS_FALSE;
if (js_Emit1(cx, cg, JSOP_ENUMELEM) < 0)
return JS_FALSE;
break;
}
#endif
case JSOP_ENUMELEM:
JS_ASSERT(0);
}
}
return JS_TRUE;
}
/*
* Recursive helper for EmitDestructuringOps.
*
* Given a value to destructure on the stack, walk over an object or array
* initialiser at pn, emitting bytecodes to match property values and store
* them in the lvalues identified by the matched property names.
*/
static JSBool
EmitDestructuringOpsHelper(JSContext *cx, JSCodeGenerator *cg, JSParseNode *pn)
{
jsuint index;
JSParseNode *pn2, *pn3;
JSBool doElemOp;
#ifdef DEBUG
intN stackDepth = cg->stackDepth;
JS_ASSERT(stackDepth != 0);
JS_ASSERT(pn->pn_arity == PN_LIST);
JS_ASSERT(pn->pn_type == TOK_RB || pn->pn_type == TOK_RC);
#endif
if (pn->pn_count == 0) {
/* Emit a DUP;POP sequence for the decompiler. */
return js_Emit1(cx, cg, JSOP_DUP) >= 0 &&
js_Emit1(cx, cg, JSOP_POP) >= 0;
}
index = 0;
for (pn2 = pn->pn_head; pn2; pn2 = pn2->pn_next) {
/*
* Duplicate the value being destructured to use as a reference base.
*/
if (js_Emit1(cx, cg, JSOP_DUP) < 0)
return JS_FALSE;
/*
* Now push the property name currently being matched, which is either
* the array initialiser's current index, or the current property name
* "label" on the left of a colon in the object initialiser. Set pn3
* to the lvalue node, which is in the value-initializing position.
*/
doElemOp = JS_TRUE;
if (pn->pn_type == TOK_RB) {
if (!EmitNumberOp(cx, index, cg))
return JS_FALSE;
pn3 = pn2;
} else {
JS_ASSERT(pn->pn_type == TOK_RC);
JS_ASSERT(pn2->pn_type == TOK_COLON);
pn3 = pn2->pn_left;
if (pn3->pn_type == TOK_NUMBER) {
/*
* If we are emitting an object destructuring initialiser,
* annotate the index op with SRC_INITPROP so we know we are
* not decompiling an array initialiser.
*/
if (js_NewSrcNote(cx, cg, SRC_INITPROP) < 0)
return JS_FALSE;
if (!EmitNumberOp(cx, pn3->pn_dval, cg))
return JS_FALSE;
} else {
JS_ASSERT(pn3->pn_type == TOK_STRING ||
pn3->pn_type == TOK_NAME);
if (!EmitAtomOp(cx, pn3, JSOP_GETPROP, cg))
return JS_FALSE;
doElemOp = JS_FALSE;
}
pn3 = pn2->pn_right;
}
if (doElemOp) {
/*
* Ok, get the value of the matching property name. This leaves
* that value on top of the value being destructured, so the stack
* is one deeper than when we started.
*/
if (js_Emit1(cx, cg, JSOP_GETELEM) < 0)
return JS_FALSE;
JS_ASSERT(cg->stackDepth == stackDepth + 1);
}
/* Nullary comma node makes a hole in the array destructurer. */
if (pn3->pn_type == TOK_COMMA && pn3->pn_arity == PN_NULLARY) {
JS_ASSERT(pn->pn_type == TOK_RB);
JS_ASSERT(pn2 == pn3);
if (js_Emit1(cx, cg, JSOP_POP) < 0)
return JS_FALSE;
} else {
if (!EmitDestructuringLHS(cx, cg, pn3, JS_TRUE))
return JS_FALSE;
}
JS_ASSERT(cg->stackDepth == stackDepth);
++index;
}
return JS_TRUE;
}
static ptrdiff_t
OpToDeclType(JSOp op)
{
switch (op) {
case JSOP_NOP:
return SRC_DECL_LET;
case JSOP_DEFCONST:
return SRC_DECL_CONST;
case JSOP_DEFVAR:
return SRC_DECL_VAR;
default:
return SRC_DECL_NONE;
}
}
static JSBool
EmitDestructuringOps(JSContext *cx, JSCodeGenerator *cg, JSOp declOp,
JSParseNode *pn)
{
/*
* If we're called from a variable declaration, help the decompiler by
* annotating the first JSOP_DUP that EmitDestructuringOpsHelper emits.
* If the destructuring initialiser is empty, our helper will emit a
* JSOP_DUP followed by a JSOP_POP for the decompiler.
*/
if (js_NewSrcNote2(cx, cg, SRC_DESTRUCT, OpToDeclType(declOp)) < 0)
return JS_FALSE;
/*
* Call our recursive helper to emit the destructuring assignments and
* related stack manipulations.
*/
return EmitDestructuringOpsHelper(cx, cg, pn);
}
static JSBool
EmitGroupAssignment(JSContext *cx, JSCodeGenerator *cg, JSOp declOp,
JSParseNode *lhs, JSParseNode *rhs)
{
jsuint depth, limit, slot;
JSParseNode *pn;
depth = limit = (uintN) cg->stackDepth;
for (pn = rhs->pn_head; pn; pn = pn->pn_next) {
if (limit == JS_BIT(16)) {
js_ReportCompileErrorNumber(cx, rhs,
JSREPORT_PN | JSREPORT_ERROR,
JSMSG_ARRAY_INIT_TOO_BIG);
return JS_FALSE;
}
if (pn->pn_type == TOK_COMMA) {
if (js_Emit1(cx, cg, JSOP_PUSH) < 0)
return JS_FALSE;
} else {
JS_ASSERT(pn->pn_type != TOK_DEFSHARP);
if (!js_EmitTree(cx, cg, pn))
return JS_FALSE;
}
++limit;
}
if (js_NewSrcNote2(cx, cg, SRC_GROUPASSIGN, OpToDeclType(declOp)) < 0)
return JS_FALSE;
slot = depth;
for (pn = lhs->pn_head; pn; pn = pn->pn_next) {
if (slot < limit) {
EMIT_UINT16_IMM_OP(JSOP_GETLOCAL, slot);
} else {
if (js_Emit1(cx, cg, JSOP_PUSH) < 0)
return JS_FALSE;
}
if (pn->pn_type == TOK_COMMA && pn->pn_arity == PN_NULLARY) {
if (js_Emit1(cx, cg, JSOP_POP) < 0)
return JS_FALSE;
} else {
if (!EmitDestructuringLHS(cx, cg, pn, pn->pn_next != NULL))
return JS_FALSE;
}
++slot;
}
EMIT_UINT16_IMM_OP(JSOP_SETSP, (jsatomid)depth);
cg->stackDepth = (uintN) depth;
return JS_TRUE;
}
/*
* Helper called with pop out param initialized to a JSOP_POP* opcode. If we
* can emit a group assignment sequence, which results in 0 stack depth delta,
* we set *pop to JSOP_NOP so callers can veto emitting pn followed by a pop.
*/
static JSBool
MaybeEmitGroupAssignment(JSContext *cx, JSCodeGenerator *cg, JSOp declOp,
JSParseNode *pn, JSOp *pop)
{
JSParseNode *lhs, *rhs;
JS_ASSERT(pn->pn_type == TOK_ASSIGN);
JS_ASSERT(*pop == JSOP_POP || *pop == JSOP_POPV);
lhs = pn->pn_left;
rhs = pn->pn_right;
if (lhs->pn_type == TOK_RB && rhs->pn_type == TOK_RB &&
lhs->pn_count <= rhs->pn_count &&
(rhs->pn_count == 0 ||
rhs->pn_head->pn_type != TOK_DEFSHARP)) {
if (!EmitGroupAssignment(cx, cg, declOp, lhs, rhs))
return JS_FALSE;
*pop = JSOP_NOP;
}
return JS_TRUE;
}
#endif /* JS_HAS_DESTRUCTURING */
static JSBool
EmitVariables(JSContext *cx, JSCodeGenerator *cg, JSParseNode *pn,
JSBool inLetHead, ptrdiff_t *headNoteIndex)
{
JSTreeContext *tc;
JSBool let, forInVar;
#if JS_HAS_BLOCK_SCOPE
JSBool forInLet, popScope;
JSStmtInfo *stmt, *scopeStmt;
#endif
ptrdiff_t off, noteIndex, tmp;
JSParseNode *pn2, *pn3;
JSOp op;
jsatomid atomIndex;
uintN oldflags;
/* Default in case of JS_HAS_BLOCK_SCOPE early return, below. */
*headNoteIndex = -1;
/*
* Let blocks and expressions have a parenthesized head in which the new
* scope is not yet open. Initializer evaluation uses the parent node's
* lexical scope. If popScope is true below, then we hide the top lexical
* block from any calls to BindNameToSlot hiding in pn2->pn_expr so that
* it won't find any names in the new let block.
*
* The same goes for let declarations in the head of any kind of for loop.
* Unlike a let declaration 'let x = i' within a block, where x is hoisted
* to the start of the block, a 'for (let x = i...) ...' loop evaluates i
* in the containing scope, and puts x in the loop body's scope.
*/
tc = &cg->treeContext;
let = (pn->pn_op == JSOP_NOP);
forInVar = (pn->pn_extra & PNX_FORINVAR) != 0;
#if JS_HAS_BLOCK_SCOPE
forInLet = let && forInVar;
popScope = (inLetHead || (let && (tc->flags & TCF_IN_FOR_INIT)));
JS_ASSERT(!popScope || let);
#endif
off = noteIndex = -1;
for (pn2 = pn->pn_head; ; pn2 = pn2->pn_next) {
#if JS_HAS_DESTRUCTURING
if (pn2->pn_type != TOK_NAME) {
if (pn2->pn_type == TOK_RB || pn2->pn_type == TOK_RC) {
/*
* Emit variable binding ops, but not destructuring ops.
* The parser (see Variables, jsparse.c) has ensured that
* our caller will be the TOK_FOR/TOK_IN case in js_EmitTree,
* and that case will emit the destructuring code only after
* emitting an enumerating opcode and a branch that tests
* whether the enumeration ended.
*/
JS_ASSERT(forInVar);
JS_ASSERT(pn->pn_count == 1);
if (!EmitDestructuringDecls(cx, cg, pn->pn_op, pn2))
return JS_FALSE;
break;
}
/*
* A destructuring initialiser assignment preceded by var is
* always evaluated promptly, even if it is to the left of 'in'
* in a for-in loop. As with 'for (var x = i in o)...', this
* will cause the entire 'var [a, b] = i' to be hoisted out of
* the head of the loop.
*/
JS_ASSERT(pn2->pn_type == TOK_ASSIGN);
if (pn->pn_count == 1 && !forInLet) {
/*
* If this is the only destructuring assignment in the list,
* try to optimize to a group assignment. If we're in a let
* head, pass JSOP_POP rather than the pseudo-prolog JSOP_NOP
* in pn->pn_op, to suppress a second (and misplaced) 'let'.
*/
JS_ASSERT(noteIndex < 0 && !pn2->pn_next);
op = JSOP_POP;
if (!MaybeEmitGroupAssignment(cx, cg,
inLetHead ? JSOP_POP : pn->pn_op,
pn2, &op)) {
return JS_FALSE;
}
if (op == JSOP_NOP) {
pn->pn_extra = (pn->pn_extra & ~PNX_POPVAR) | PNX_GROUPINIT;
break;
}
}
pn3 = pn2->pn_left;
if (!EmitDestructuringDecls(cx, cg, pn->pn_op, pn3))
return JS_FALSE;
#if JS_HAS_BLOCK_SCOPE
/*
* If this is a 'for (let [x, y] = i in o) ...' let declaration,
* throw away i if it is a useless expression.
*/
if (forInLet) {
JSBool useful = JS_FALSE;
JS_ASSERT(pn->pn_count == 1);
if (!CheckSideEffects(cx, tc, pn2->pn_right, &useful))
return JS_FALSE;
if (!useful)
return JS_TRUE;
}
#endif
if (!js_EmitTree(cx, cg, pn2->pn_right))
return JS_FALSE;
#if JS_HAS_BLOCK_SCOPE
/*
* The expression i in 'for (let [x, y] = i in o) ...', which is
* pn2->pn_right above, appears to have side effects. We've just
* emitted code to evaluate i, but we must not destructure i yet.
* Let the TOK_FOR: code in js_EmitTree do the destructuring to
* emit the right combination of source notes and bytecode for the
* decompiler.
*
* This has the effect of hoisting the evaluation of i out of the
* for-in loop, without hoisting the let variables, which must of
* course be scoped by the loop. Set PNX_POPVAR to cause JSOP_POP
* to be emitted, just before returning from this function.
*/
if (forInVar) {
pn->pn_extra |= PNX_POPVAR;
if (forInLet)
break;
}
#endif
/*
* Veto pn->pn_op if inLetHead to avoid emitting a SRC_DESTRUCT
* that's redundant with respect to the SRC_DECL/SRC_DECL_LET that
* we will emit at the bottom of this function.
*/
if (!EmitDestructuringOps(cx, cg,
inLetHead ? JSOP_POP : pn->pn_op,
pn3)) {
return JS_FALSE;
}
goto emit_note_pop;
}
#else
JS_ASSERT(pn2->pn_type == TOK_NAME);
#endif
if (!BindNameToSlot(cx, &cg->treeContext, pn2, let))
return JS_FALSE;
JS_ASSERT(pn2->pn_slot >= 0 || !let);
op = pn2->pn_op;
if (op == JSOP_ARGUMENTS) {
/* JSOP_ARGUMENTS => no initializer */
JS_ASSERT(!pn2->pn_expr && !let);
pn3 = NULL;
#ifdef __GNUC__
atomIndex = 0; /* quell GCC overwarning */
#endif
} else {
if (!MaybeEmitVarDecl(cx, cg, pn->pn_op, pn2, &atomIndex))
return JS_FALSE;
pn3 = pn2->pn_expr;
if (pn3) {
#if JS_HAS_BLOCK_SCOPE
/*
* If this is a 'for (let x = i in o) ...' let declaration,
* throw away i if it is a useless expression.
*/
if (forInLet) {
JSBool useful = JS_FALSE;
JS_ASSERT(pn->pn_count == 1);
if (!CheckSideEffects(cx, tc, pn3, &useful))
return JS_FALSE;
if (!useful)
return JS_TRUE;
}
#endif
if (op == JSOP_SETNAME) {
JS_ASSERT(!let);
EMIT_ATOM_INDEX_OP(JSOP_BINDNAME, atomIndex);
}
if (pn->pn_op == JSOP_DEFCONST &&
!js_DefineCompileTimeConstant(cx, cg, pn2->pn_atom,
pn3)) {
return JS_FALSE;
}
#if JS_HAS_BLOCK_SCOPE
/* Evaluate expr in the outer lexical scope if requested. */
if (popScope) {
stmt = tc->topStmt;
scopeStmt = tc->topScopeStmt;
tc->topStmt = stmt->down;
tc->topScopeStmt = scopeStmt->downScope;
}
#ifdef __GNUC__
else {
stmt = scopeStmt = NULL; /* quell GCC overwarning */
}
#endif
#endif
oldflags = cg->treeContext.flags;
cg->treeContext.flags &= ~TCF_IN_FOR_INIT;
if (!js_EmitTree(cx, cg, pn3))
return JS_FALSE;
cg->treeContext.flags |= oldflags & TCF_IN_FOR_INIT;
#if JS_HAS_BLOCK_SCOPE
if (popScope) {
tc->topStmt = stmt;
tc->topScopeStmt = scopeStmt;
}
#endif
}
}
/*
* 'for (var x in o) ...' and 'for (var x = i in o) ...' call the
* TOK_VAR case, but only the initialized case (a strange one that
* falls out of ECMA-262's grammar) wants to run past this point.
* Both cases must conditionally emit a JSOP_DEFVAR, above. Note
* that the parser error-checks to ensure that pn->pn_count is 1.
*
* 'for (let x = i in o) ...' must evaluate i before the loop, and
* subject it to useless expression elimination. The variable list
* in pn is a single let declaration if pn_op == JSOP_NOP. We test
* the let local in order to break early in this case, as well as in
* the 'for (var x in o)' case.
*
* XXX Narcissus keeps track of variable declarations in the node
* for the script being compiled, so there's no need to share any
* conditional prolog code generation there. We could do likewise,
* but it's a big change, requiring extra allocation, so probably
* not worth the trouble for SpiderMonkey.
*/
JS_ASSERT(pn3 == pn2->pn_expr);
if (forInVar && (!pn3 || let)) {
JS_ASSERT(pn->pn_count == 1);
break;
}
if (pn2 == pn->pn_head &&
!inLetHead &&
js_NewSrcNote2(cx, cg, SRC_DECL,
(pn->pn_op == JSOP_DEFCONST)
? SRC_DECL_CONST
: (pn->pn_op == JSOP_DEFVAR)
? SRC_DECL_VAR
: SRC_DECL_LET) < 0) {
return JS_FALSE;
}
if (op == JSOP_ARGUMENTS) {
if (js_Emit1(cx, cg, op) < 0)
return JS_FALSE;
} else if (pn2->pn_slot >= 0) {
EMIT_UINT16_IMM_OP(op, atomIndex);
} else {
EMIT_ATOM_INDEX_OP(op, atomIndex);
}
#if JS_HAS_DESTRUCTURING
emit_note_pop:
#endif
tmp = CG_OFFSET(cg);
if (noteIndex >= 0) {
if (!js_SetSrcNoteOffset(cx, cg, (uintN)noteIndex, 0, tmp-off))
return JS_FALSE;
}
if (!pn2->pn_next)
break;
off = tmp;
noteIndex = js_NewSrcNote2(cx, cg, SRC_PCDELTA, 0);
if (noteIndex < 0 || js_Emit1(cx, cg, JSOP_POP) < 0)
return JS_FALSE;
}
/* If this is a let head, emit and return a srcnote on the pop. */
if (inLetHead) {
*headNoteIndex = js_NewSrcNote(cx, cg, SRC_DECL);
if (*headNoteIndex < 0)
return JS_FALSE;
if (!(pn->pn_extra & PNX_POPVAR))
return js_Emit1(cx, cg, JSOP_NOP) >= 0;
}
return !(pn->pn_extra & PNX_POPVAR) || js_Emit1(cx, cg, JSOP_POP) >= 0;
}
#if defined DEBUG_brendan || defined DEBUG_mrbkap
static JSBool
GettableNoteForNextOp(JSCodeGenerator *cg)
{
ptrdiff_t offset, target;
jssrcnote *sn, *end;
offset = 0;
target = CG_OFFSET(cg);
for (sn = CG_NOTES(cg), end = sn + CG_NOTE_COUNT(cg); sn < end;
sn = SN_NEXT(sn)) {
if (offset == target && SN_IS_GETTABLE(sn))
return JS_TRUE;
offset += SN_DELTA(sn);
}
return JS_FALSE;
}
#endif
JSBool
js_EmitTree(JSContext *cx, JSCodeGenerator *cg, JSParseNode *pn)
{
JSBool ok, useful, wantval;
JSStmtInfo *stmt, stmtInfo;
ptrdiff_t top, off, tmp, beq, jmp;
JSParseNode *pn2, *pn3;
JSAtom *atom;
JSAtomListElement *ale;
jsatomid atomIndex;
ptrdiff_t noteIndex;
JSSrcNoteType noteType;
jsbytecode *pc;
JSOp op;
JSTokenType type;
uint32 argc;
int stackDummy;
if (!JS_CHECK_STACK_SIZE(cx, stackDummy)) {
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_OVER_RECURSED);
return JS_FALSE;
}
ok = JS_TRUE;
cg->emitLevel++;
pn->pn_offset = top = CG_OFFSET(cg);
/* Emit notes to tell the current bytecode's source line number. */
UPDATE_LINE_NUMBER_NOTES(cx, cg, pn);
switch (pn->pn_type) {
case TOK_FUNCTION:
{
void *cg2mark;
JSCodeGenerator *cg2;
JSFunction *fun;
#if JS_HAS_XML_SUPPORT
if (pn->pn_arity == PN_NULLARY) {
if (js_Emit1(cx, cg, JSOP_GETFUNNS) < 0)
return JS_FALSE;
break;
}
#endif
/* Generate code for the function's body. */
cg2mark = JS_ARENA_MARK(&cx->tempPool);
JS_ARENA_ALLOCATE_TYPE(cg2, JSCodeGenerator, &cx->tempPool);
if (!cg2) {
JS_ReportOutOfMemory(cx);
return JS_FALSE;
}
if (!js_InitCodeGenerator(cx, cg2, cg->codePool, cg->notePool,
cg->filename, pn->pn_pos.begin.lineno,
cg->principals)) {
return JS_FALSE;
}
cg2->treeContext.flags = (uint16) (pn->pn_flags | TCF_IN_FUNCTION);
cg2->treeContext.tryCount = pn->pn_tryCount;
cg2->parent = cg;
fun = (JSFunction *) JS_GetPrivate(cx, ATOM_TO_OBJECT(pn->pn_funAtom));
if (!js_EmitFunctionBody(cx, cg2, pn->pn_body, fun))
return JS_FALSE;
/*
* We need an activation object if an inner peeks out, or if such
* inner-peeking caused one of our inners to become heavyweight.
*/
if (cg2->treeContext.flags &
(TCF_FUN_USES_NONLOCALS | TCF_FUN_HEAVYWEIGHT)) {
cg->treeContext.flags |= TCF_FUN_HEAVYWEIGHT;
}
js_FinishCodeGenerator(cx, cg2);
JS_ARENA_RELEASE(&cx->tempPool, cg2mark);
/* Make the function object a literal in the outer script's pool. */
ale = js_IndexAtom(cx, pn->pn_funAtom, &cg->atomList);
if (!ale)
return JS_FALSE;
atomIndex = ALE_INDEX(ale);
/* Emit a bytecode pointing to the closure object in its immediate. */
if (pn->pn_op != JSOP_NOP) {
EMIT_ATOM_INDEX_OP(pn->pn_op, atomIndex);
break;
}
/* Top-level named functions need a nop for decompilation. */
noteIndex = js_NewSrcNote2(cx, cg, SRC_FUNCDEF, (ptrdiff_t)atomIndex);
if (noteIndex < 0 ||
js_Emit1(cx, cg, JSOP_NOP) < 0) {
return JS_FALSE;
}
/*
* Top-levels also need a prolog op to predefine their names in the
* variable object, or if local, to fill their stack slots.
*/
CG_SWITCH_TO_PROLOG(cg);
if (cg->treeContext.flags & TCF_IN_FUNCTION) {
JSObject *obj, *pobj;
JSProperty *prop;
JSScopeProperty *sprop;
uintN slot;
obj = OBJ_GET_PARENT(cx, fun->object);
if (!js_LookupHiddenProperty(cx, obj, ATOM_TO_JSID(fun->atom),
&pobj, &prop)) {
return JS_FALSE;
}
JS_ASSERT(prop && pobj == obj);
sprop = (JSScopeProperty *) prop;
JS_ASSERT(sprop->getter == js_GetLocalVariable);
slot = sprop->shortid;
OBJ_DROP_PROPERTY(cx, pobj, prop);
/*
* If this local function is declared in a body block induced by
* let declarations, reparent fun->object to the compiler-created
* body block object so that JSOP_DEFLOCALFUN can clone that block
* into the runtime scope chain.
*/
stmt = cg->treeContext.topStmt;
if (stmt && stmt->type == STMT_BLOCK &&
stmt->down && stmt->down->type == STMT_BLOCK &&
(stmt->down->flags & SIF_SCOPE)) {
obj = ATOM_TO_OBJECT(stmt->down->atom);
JS_ASSERT(LOCKED_OBJ_GET_CLASS(obj) == &js_BlockClass);
OBJ_SET_PARENT(cx, fun->object, obj);
}
if (atomIndex >= JS_BIT(16)) {
/*
* Lots of literals in the outer function, so we have to emit
* [JSOP_LITOPX, atomIndex, JSOP_DEFLOCALFUN, var slot].
*/
off = js_EmitN(cx, cg, JSOP_LITOPX, 3);
if (off < 0)
return JS_FALSE;
pc = CG_CODE(cg, off);
SET_LITERAL_INDEX(pc, atomIndex);
EMIT_UINT16_IMM_OP(JSOP_DEFLOCALFUN, slot);
} else {
/* Emit [JSOP_DEFLOCALFUN, var slot, atomIndex]. */
off = js_EmitN(cx, cg, JSOP_DEFLOCALFUN,
VARNO_LEN + ATOM_INDEX_LEN);
if (off < 0)
return JS_FALSE;
pc = CG_CODE(cg, off);
SET_VARNO(pc, slot);
pc += VARNO_LEN;
SET_ATOM_INDEX(pc, atomIndex);
}
} else {
JS_ASSERT(!cg->treeContext.topStmt);
EMIT_ATOM_INDEX_OP(JSOP_DEFFUN, atomIndex);
}
CG_SWITCH_TO_MAIN(cg);
break;
}
#if JS_HAS_EXPORT_IMPORT
case TOK_EXPORT:
pn2 = pn->pn_head;
if (pn2->pn_type == TOK_STAR) {
/*
* 'export *' must have no other elements in the list (what would
* be the point?).
*/
if (js_Emit1(cx, cg, JSOP_EXPORTALL) < 0)
return JS_FALSE;
} else {
/*
* If not 'export *', the list consists of NAME nodes identifying
* properties of the variables object to flag as exported.
*/
do {
ale = js_IndexAtom(cx, pn2->pn_atom, &cg->atomList);
if (!ale)
return JS_FALSE;
EMIT_ATOM_INDEX_OP(JSOP_EXPORTNAME, ALE_INDEX(ale));
} while ((pn2 = pn2->pn_next) != NULL);
}
break;
case TOK_IMPORT:
for (pn2 = pn->pn_head; pn2; pn2 = pn2->pn_next) {
/*
* Each subtree on an import list is rooted by a DOT or LB node.
* A DOT may have a null pn_atom member, in which case pn_op must
* be JSOP_IMPORTALL -- see EmitPropOp above.
*/
if (!js_EmitTree(cx, cg, pn2))
return JS_FALSE;
}
break;
#endif /* JS_HAS_EXPORT_IMPORT */
case TOK_IF:
/* Initialize so we can detect else-if chains and avoid recursion. */
stmtInfo.type = STMT_IF;
beq = jmp = -1;
noteIndex = -1;
if_again:
/* Emit code for the condition before pushing stmtInfo. */
if (!js_EmitTree(cx, cg, pn->pn_kid1))
return JS_FALSE;
top = CG_OFFSET(cg);
if (stmtInfo.type == STMT_IF) {
js_PushStatement(&cg->treeContext, &stmtInfo, STMT_IF, top);
} else {
/*
* We came here from the goto further below that detects else-if
* chains, so we must mutate stmtInfo back into a STMT_IF record.
* Also (see below for why) we need a note offset for SRC_IF_ELSE
* to help the decompiler. Actually, we need two offsets, one for
* decompiling any else clause and the second for decompiling an
* else-if chain without bracing, overindenting, or incorrectly
* scoping let declarations.
*/
JS_ASSERT(stmtInfo.type == STMT_ELSE);
stmtInfo.type = STMT_IF;
stmtInfo.update = top;
if (!js_SetSrcNoteOffset(cx, cg, noteIndex, 0, jmp - beq))
return JS_FALSE;
if (!js_SetSrcNoteOffset(cx, cg, noteIndex, 1, top - jmp))
return JS_FALSE;
}
/* Emit an annotated branch-if-false around the then part. */
pn3 = pn->pn_kid3;
noteIndex = js_NewSrcNote(cx, cg, pn3 ? SRC_IF_ELSE : SRC_IF);
if (noteIndex < 0)
return JS_FALSE;
beq = EmitJump(cx, cg, JSOP_IFEQ, 0);
if (beq < 0)
return JS_FALSE;
/* Emit code for the then and optional else parts. */
if (!js_EmitTree(cx, cg, pn->pn_kid2))
return JS_FALSE;
if (pn3) {
/* Modify stmtInfo so we know we're in the else part. */
stmtInfo.type = STMT_ELSE;
/*
* Emit a JSOP_BACKPATCH op to jump from the end of our then part
* around the else part. The js_PopStatementCG call at the bottom
* of this switch case will fix up the backpatch chain linked from
* stmtInfo.breaks.
*/
jmp = EmitGoto(cx, cg, &stmtInfo, &stmtInfo.breaks, NULL, SRC_NULL);
if (jmp < 0)
return JS_FALSE;
/* Ensure the branch-if-false comes here, then emit the else. */
CHECK_AND_SET_JUMP_OFFSET_AT(cx, cg, beq);
if (pn3->pn_type == TOK_IF) {
pn = pn3;
goto if_again;
}
if (!js_EmitTree(cx, cg, pn3))
return JS_FALSE;
/*
* Annotate SRC_IF_ELSE with the offset from branch to jump, for
* the decompiler's benefit. We can't just "back up" from the pc
* of the else clause, because we don't know whether an extended
* jump was required to leap from the end of the then clause over
* the else clause.
*/
if (!js_SetSrcNoteOffset(cx, cg, noteIndex, 0, jmp - beq))
return JS_FALSE;
} else {
/* No else part, fixup the branch-if-false to come here. */
CHECK_AND_SET_JUMP_OFFSET_AT(cx, cg, beq);
}
ok = js_PopStatementCG(cx, cg);
break;
case TOK_SWITCH:
/* Out of line to avoid bloating js_EmitTree's stack frame size. */
ok = EmitSwitch(cx, cg, pn, &stmtInfo);
break;
case TOK_WHILE:
js_PushStatement(&cg->treeContext, &stmtInfo, STMT_WHILE_LOOP, top);
if (!js_EmitTree(cx, cg, pn->pn_left))
return JS_FALSE;
noteIndex = js_NewSrcNote(cx, cg, SRC_WHILE);
if (noteIndex < 0)
return JS_FALSE;
beq = EmitJump(cx, cg, JSOP_IFEQ, 0);
if (beq < 0)
return JS_FALSE;
if (!js_EmitTree(cx, cg, pn->pn_right))
return JS_FALSE;
jmp = EmitJump(cx, cg, JSOP_GOTO, top - CG_OFFSET(cg));
if (jmp < 0)
return JS_FALSE;
CHECK_AND_SET_JUMP_OFFSET_AT(cx, cg, beq);
if (!js_SetSrcNoteOffset(cx, cg, noteIndex, 0, jmp - beq))
return JS_FALSE;
ok = js_PopStatementCG(cx, cg);
break;
case TOK_DO:
/* Emit an annotated nop so we know to decompile a 'do' keyword. */
if (js_NewSrcNote(cx, cg, SRC_WHILE) < 0 ||
js_Emit1(cx, cg, JSOP_NOP) < 0) {
return JS_FALSE;
}
/* Compile the loop body. */
top = CG_OFFSET(cg);
js_PushStatement(&cg->treeContext, &stmtInfo, STMT_DO_LOOP, top);
if (!js_EmitTree(cx, cg, pn->pn_left))
return JS_FALSE;
/* Set loop and enclosing label update offsets, for continue. */
stmt = &stmtInfo;
do {
stmt->update = CG_OFFSET(cg);
} while ((stmt = stmt->down) != NULL && stmt->type == STMT_LABEL);
/* Compile the loop condition, now that continues know where to go. */
if (!js_EmitTree(cx, cg, pn->pn_right))
return JS_FALSE;
/*
* No source note needed, because JSOP_IFNE is used only for do-while.
* If we ever use JSOP_IFNE for other purposes, we can still avoid yet
* another note here, by storing (jmp - top) in the SRC_WHILE note's
* offset, and fetching that delta in order to decompile recursively.
*/
if (EmitJump(cx, cg, JSOP_IFNE, top - CG_OFFSET(cg)) < 0)
return JS_FALSE;
ok = js_PopStatementCG(cx, cg);
break;
case TOK_FOR:
beq = 0; /* suppress gcc warnings */
pn2 = pn->pn_left;
js_PushStatement(&cg->treeContext, &stmtInfo, STMT_FOR_LOOP, top);
if (pn2->pn_type == TOK_IN) {
JSBool emitIFEQ;
/* Set stmtInfo type for later testing. */
stmtInfo.type = STMT_FOR_IN_LOOP;
noteIndex = -1;
/*
* If the left part is 'var x', emit code to define x if necessary
* using a prolog opcode, but do not emit a pop. If the left part
* is 'var x = i', emit prolog code to define x if necessary; then
* emit code to evaluate i, assign the result to x, and pop the
* result off the stack.
*
* All the logic to do this is implemented in the outer switch's
* TOK_VAR case, conditioned on pn_extra flags set by the parser.
*
* In the 'for (var x = i in o) ...' case, the js_EmitTree(...pn3)
* called here will generate the proper note for the assignment
* op that sets x = i, hoisting the initialized var declaration
* out of the loop: 'var x = i; for (x in o) ...'.
*
* In the 'for (var x in o) ...' case, nothing but the prolog op
* (if needed) should be generated here, we must emit the note
* just before the JSOP_FOR* opcode in the switch on pn3->pn_type
* a bit below, so nothing is hoisted: 'for (var x in o) ...'.
*
* A 'for (let x = i in o)' loop must not be hoisted, since in
* this form the let variable is scoped by the loop body (but not
* the head). The initializer expression i must be evaluated for
* any side effects. So we hoist only i in the let case.
*/
pn3 = pn2->pn_left;
type = pn3->pn_type;
cg->treeContext.flags |= TCF_IN_FOR_INIT;
if (TOKEN_TYPE_IS_DECL(type) && !js_EmitTree(cx, cg, pn3))
return JS_FALSE;
cg->treeContext.flags &= ~TCF_IN_FOR_INIT;
/* Emit a push to allocate the iterator. */
if (js_Emit1(cx, cg, JSOP_STARTITER) < 0)
return JS_FALSE;
/* Compile the object expression to the right of 'in'. */
if (!js_EmitTree(cx, cg, pn2->pn_right))
return JS_FALSE;
/*
* Emit a bytecode to convert top of stack value to the iterator
* object depending on the loop variant (for-in, for-each-in, or
* destructuring for-in).
*/
#if JS_HAS_DESTRUCTURING
JS_ASSERT(pn->pn_op == JSOP_FORIN ||
pn->pn_op == JSOP_FOREACHKEYVAL ||
pn->pn_op == JSOP_FOREACH);
#else
JS_ASSERT(pn->pn_op == JSOP_FORIN || pn->pn_op == JSOP_FOREACH);
#endif
if (js_Emit1(cx, cg, pn->pn_op) < 0)
return JS_FALSE;
top = CG_OFFSET(cg);
SET_STATEMENT_TOP(&stmtInfo, top);
/*
* Compile a JSOP_FOR* bytecode based on the left hand side.
*
* Initialize op to JSOP_SETNAME in case of |for ([a, b] in o)...|
* or similar, to signify assignment, rather than declaration, to
* the decompiler. EmitDestructuringOps takes a prolog bytecode
* parameter and emits the appropriate source note, defaulting to
* assignment, so JSOP_SETNAME is not critical here; many similar
* ops could be used -- just not JSOP_NOP (which means 'let').
*/
emitIFEQ = JS_TRUE;
op = JSOP_SETNAME;
switch (type) {
#if JS_HAS_BLOCK_SCOPE
case TOK_LET:
#endif
case TOK_VAR:
JS_ASSERT(pn3->pn_arity == PN_LIST && pn3->pn_count == 1);
pn3 = pn3->pn_head;
#if JS_HAS_DESTRUCTURING
if (pn3->pn_type == TOK_ASSIGN) {
pn3 = pn3->pn_left;
JS_ASSERT(pn3->pn_type == TOK_RB || pn3->pn_type == TOK_RC);
}
if (pn3->pn_type == TOK_RB || pn3->pn_type == TOK_RC) {
op = pn2->pn_left->pn_op;
goto destructuring_for;
}
#else
JS_ASSERT(pn3->pn_type == TOK_NAME);
#endif
/*
* Always annotate JSOP_FORLOCAL if given input of the form
* 'for (let x in * o)' -- the decompiler must not hoist the
* 'let x' out of the loop head, or x will be bound in the
* wrong scope. Likewise, but in this case only for the sake
* of higher decompilation fidelity only, do not hoist 'var x'
* when given 'for (var x in o)'. But 'for (var x = i in o)'
* requires hoisting in order to preserve the initializer i.
* The decompiler can only handle so much!
*/
if ((
#if JS_HAS_BLOCK_SCOPE
type == TOK_LET ||
#endif
!pn3->pn_expr) &&
js_NewSrcNote2(cx, cg, SRC_DECL,
type == TOK_VAR
? SRC_DECL_VAR
: SRC_DECL_LET) < 0) {
return JS_FALSE;
}
/* FALL THROUGH */
case TOK_NAME:
if (pn3->pn_slot >= 0) {
op = pn3->pn_op;
switch (op) {
case JSOP_GETARG: /* FALL THROUGH */
case JSOP_SETARG: op = JSOP_FORARG; break;
case JSOP_GETVAR: /* FALL THROUGH */
case JSOP_SETVAR: op = JSOP_FORVAR; break;
case JSOP_GETGVAR: /* FALL THROUGH */
case JSOP_SETGVAR: op = JSOP_FORNAME; break;
case JSOP_GETLOCAL: /* FALL THROUGH */
case JSOP_SETLOCAL: op = JSOP_FORLOCAL; break;
default: JS_ASSERT(0);
}
} else {
pn3->pn_op = JSOP_FORNAME;
if (!BindNameToSlot(cx, &cg->treeContext, pn3, JS_FALSE))
return JS_FALSE;
op = pn3->pn_op;
}
if (pn3->pn_slot >= 0) {
if (pn3->pn_attrs & JSPROP_READONLY) {
JS_ASSERT(op == JSOP_FORVAR);
op = JSOP_GETVAR;
}
atomIndex = (jsatomid) pn3->pn_slot;
EMIT_UINT16_IMM_OP(op, atomIndex);
} else {
if (!EmitAtomOp(cx, pn3, op, cg))
return JS_FALSE;
}
break;
case TOK_DOT:
useful = JS_FALSE;
if (!CheckSideEffects(cx, &cg->treeContext, pn3->pn_expr,
&useful)) {
return JS_FALSE;
}
if (!useful) {
if (!EmitPropOp(cx, pn3, JSOP_FORPROP, cg))
return JS_FALSE;
break;
}
/* FALL THROUGH */
#if JS_HAS_DESTRUCTURING
case TOK_RB:
case TOK_RC:
destructuring_for:
#endif
#if JS_HAS_XML_SUPPORT
case TOK_UNARYOP:
#endif
#if JS_HAS_LVALUE_RETURN
case TOK_LP:
#endif
case TOK_LB:
/*
* We separate the first/next bytecode from the enumerator
* variable binding to avoid any side-effects in the index
* expression (e.g., for (x[i++] in {}) should not bind x[i]
* or increment i at all).
*/
emitIFEQ = JS_FALSE;
if (!js_Emit1(cx, cg, JSOP_FORELEM))
return JS_FALSE;
/*
* Emit a SRC_WHILE note with offset telling the distance to
* the loop-closing jump (we can't reckon from the branch at
* the top of the loop, because the loop-closing jump might
* need to be an extended jump, independent of whether the
* branch is short or long).
*/
noteIndex = js_NewSrcNote(cx, cg, SRC_WHILE);
if (noteIndex < 0)
return JS_FALSE;
beq = EmitJump(cx, cg, JSOP_IFEQ, 0);
if (beq < 0)
return JS_FALSE;
#if JS_HAS_DESTRUCTURING
if (pn3->pn_type == TOK_RB || pn3->pn_type == TOK_RC) {
if (!EmitDestructuringOps(cx, cg, op, pn3))
return JS_FALSE;
if (js_Emit1(cx, cg, JSOP_POP) < 0)
return JS_FALSE;
break;
}
#endif
#if JS_HAS_LVALUE_RETURN
if (pn3->pn_type == TOK_LP) {
JS_ASSERT(pn3->pn_op == JSOP_SETCALL);
if (!js_EmitTree(cx, cg, pn3))
return JS_FALSE;
if (!js_Emit1(cx, cg, JSOP_ENUMELEM))
return JS_FALSE;
break;
}
#endif
#if JS_HAS_XML_SUPPORT
if (pn3->pn_type == TOK_UNARYOP) {
JS_ASSERT(pn3->pn_op == JSOP_BINDXMLNAME);
if (!js_EmitTree(cx, cg, pn3))
return JS_FALSE;
if (!js_Emit1(cx, cg, JSOP_ENUMELEM))
return JS_FALSE;
break;
}
#endif
/* Now that we're safely past the IFEQ, commit side effects. */
if (!EmitElemOp(cx, pn3, JSOP_ENUMELEM, cg))
return JS_FALSE;
break;
default:
JS_ASSERT(0);
}
if (emitIFEQ) {
/* Annotate so the decompiler can find the loop-closing jump. */
noteIndex = js_NewSrcNote(cx, cg, SRC_WHILE);
if (noteIndex < 0)
return JS_FALSE;
/* Pop and test the loop condition generated by JSOP_FOR*. */
beq = EmitJump(cx, cg, JSOP_IFEQ, 0);
if (beq < 0)
return JS_FALSE;
}
} else {
op = JSOP_POP;
if (!pn2->pn_kid1) {
/* No initializer: emit an annotated nop for the decompiler. */
op = JSOP_NOP;
} else {
cg->treeContext.flags |= TCF_IN_FOR_INIT;
#if JS_HAS_DESTRUCTURING
pn3 = pn2->pn_kid1;
if (pn3->pn_type == TOK_ASSIGN &&
!MaybeEmitGroupAssignment(cx, cg, op, pn3, &op)) {
return JS_FALSE;
}
#endif
if (op == JSOP_POP) {
if (!js_EmitTree(cx, cg, pn3))
return JS_FALSE;
if (TOKEN_TYPE_IS_DECL(pn3->pn_type)) {
/*
* Check whether a destructuring-initialized var decl
* was optimized to a group assignment. If so, we do
* not need to emit a pop below, so switch to a nop,
* just for the decompiler.
*/
JS_ASSERT(pn3->pn_arity == PN_LIST);
if (pn3->pn_extra & PNX_GROUPINIT)
op = JSOP_NOP;
}
}
cg->treeContext.flags &= ~TCF_IN_FOR_INIT;
}
noteIndex = js_NewSrcNote(cx, cg, SRC_FOR);
if (noteIndex < 0 ||
js_Emit1(cx, cg, op) < 0) {
return JS_FALSE;
}
top = CG_OFFSET(cg);
SET_STATEMENT_TOP(&stmtInfo, top);
if (!pn2->pn_kid2) {
/* No loop condition: flag this fact in the source notes. */
if (!js_SetSrcNoteOffset(cx, cg, (uintN)noteIndex, 0, 0))
return JS_FALSE;
} else {
if (!js_EmitTree(cx, cg, pn2->pn_kid2))
return JS_FALSE;
if (!js_SetSrcNoteOffset(cx, cg, (uintN)noteIndex, 0,
CG_OFFSET(cg) - top)) {
return JS_FALSE;
}
beq = EmitJump(cx, cg, JSOP_IFEQ, 0);
if (beq < 0)
return JS_FALSE;
}
/* Set pn3 (used below) here to avoid spurious gcc warnings. */
pn3 = pn2->pn_kid3;
}
/* Emit code for the loop body. */
if (!js_EmitTree(cx, cg, pn->pn_right))
return JS_FALSE;
if (pn2->pn_type != TOK_IN) {
/* Set the second note offset so we can find the update part. */
JS_ASSERT(noteIndex != -1);
if (!js_SetSrcNoteOffset(cx, cg, (uintN)noteIndex, 1,
CG_OFFSET(cg) - top)) {
return JS_FALSE;
}
if (pn3) {
/* Set loop and enclosing "update" offsets, for continue. */
stmt = &stmtInfo;
do {
stmt->update = CG_OFFSET(cg);
} while ((stmt = stmt->down) != NULL &&
stmt->type == STMT_LABEL);
op = JSOP_POP;
#if JS_HAS_DESTRUCTURING
if (pn3->pn_type == TOK_ASSIGN &&
!MaybeEmitGroupAssignment(cx, cg, op, pn3, &op)) {
return JS_FALSE;
}
#endif
if (op == JSOP_POP) {
if (!js_EmitTree(cx, cg, pn3))
return JS_FALSE;
if (js_Emit1(cx, cg, op) < 0)
return JS_FALSE;
}
/* Restore the absolute line number for source note readers. */
off = (ptrdiff_t) pn->pn_pos.end.lineno;
if (CG_CURRENT_LINE(cg) != (uintN) off) {
if (js_NewSrcNote2(cx, cg, SRC_SETLINE, off) < 0)
return JS_FALSE;
CG_CURRENT_LINE(cg) = (uintN) off;
}
}
/* The third note offset helps us find the loop-closing jump. */
if (!js_SetSrcNoteOffset(cx, cg, (uintN)noteIndex, 2,
CG_OFFSET(cg) - top)) {
return JS_FALSE;
}
}
/* Emit the loop-closing jump and fixup all jump offsets. */
jmp = EmitJump(cx, cg, JSOP_GOTO, top - CG_OFFSET(cg));
if (jmp < 0)
return JS_FALSE;
if (beq > 0)
CHECK_AND_SET_JUMP_OFFSET_AT(cx, cg, beq);
if (pn2->pn_type == TOK_IN) {
/* Set the SRC_WHILE note offset so we can find the closing jump. */
JS_ASSERT(noteIndex != -1);
if (!js_SetSrcNoteOffset(cx, cg, (uintN)noteIndex, 0, jmp - beq))
return JS_FALSE;
}
/* Now fixup all breaks and continues (before for/in's JSOP_ENDITER). */
if (!js_PopStatementCG(cx, cg))
return JS_FALSE;
if (pn2->pn_type == TOK_IN) {
if (js_Emit1(cx, cg, JSOP_ENDITER) < 0)
return JS_FALSE;
}
break;
case TOK_BREAK:
stmt = cg->treeContext.topStmt;
atom = pn->pn_atom;
if (atom) {
ale = js_IndexAtom(cx, atom, &cg->atomList);
if (!ale)
return JS_FALSE;
while (stmt->type != STMT_LABEL || stmt->atom != atom)
stmt = stmt->down;
noteType = SRC_BREAK2LABEL;
} else {
ale = NULL;
while (!STMT_IS_LOOP(stmt) && stmt->type != STMT_SWITCH)
stmt = stmt->down;
noteType = SRC_NULL;
}
if (EmitGoto(cx, cg, stmt, &stmt->breaks, ale, noteType) < 0)
return JS_FALSE;
break;
case TOK_CONTINUE:
stmt = cg->treeContext.topStmt;
atom = pn->pn_atom;
if (atom) {
/* Find the loop statement enclosed by the matching label. */
JSStmtInfo *loop = NULL;
ale = js_IndexAtom(cx, atom, &cg->atomList);
if (!ale)
return JS_FALSE;
while (stmt->type != STMT_LABEL || stmt->atom != atom) {
if (STMT_IS_LOOP(stmt))
loop = stmt;
stmt = stmt->down;
}
stmt = loop;
noteType = SRC_CONT2LABEL;
} else {
ale = NULL;
while (!STMT_IS_LOOP(stmt))
stmt = stmt->down;
noteType = SRC_CONTINUE;
}
if (EmitGoto(cx, cg, stmt, &stmt->continues, ale, noteType) < 0)
return JS_FALSE;
break;
case TOK_WITH:
if (!js_EmitTree(cx, cg, pn->pn_left))
return JS_FALSE;
js_PushStatement(&cg->treeContext, &stmtInfo, STMT_WITH, CG_OFFSET(cg));
if (js_Emit1(cx, cg, JSOP_ENTERWITH) < 0)
return JS_FALSE;
if (!js_EmitTree(cx, cg, pn->pn_right))
return JS_FALSE;
if (js_Emit1(cx, cg, JSOP_LEAVEWITH) < 0)
return JS_FALSE;
ok = js_PopStatementCG(cx, cg);
break;
case TOK_TRY:
{
ptrdiff_t start, end, catchJump, catchStart, finallyCatch;
intN depth;
JSParseNode *lastCatch;
catchJump = catchStart = finallyCatch = -1;
/*
* Push stmtInfo to track jumps-over-catches and gosubs-to-finally
* for later fixup.
*
* When a finally block is 'active' (STMT_FINALLY on the treeContext),
* non-local jumps (including jumps-over-catches) result in a GOSUB
* being written into the bytecode stream and fixed-up later (c.f.
* EmitBackPatchOp and BackPatch).
*/
js_PushStatement(&cg->treeContext, &stmtInfo,
pn->pn_kid3 ? STMT_FINALLY : STMT_TRY,
CG_OFFSET(cg));
/*
* About JSOP_SETSP: an exception can be thrown while the stack is in
* an unbalanced state, and this imbalance causes problems with things
* like function invocation later on.
*
* To fix this, we compute the 'balanced' stack depth upon try entry,
* and then restore the stack to this depth when we hit the first catch
* or finally block. We can't just zero the stack, because things like
* for/in and with that are active upon entry to the block keep state
* variables on the stack.
*/
depth = cg->stackDepth;
/* Mark try location for decompilation, then emit try block. */
if (js_Emit1(cx, cg, JSOP_TRY) < 0)
return JS_FALSE;
start = CG_OFFSET(cg);
if (!js_EmitTree(cx, cg, pn->pn_kid1))
return JS_FALSE;
/* GOSUB to finally, if present. */
if (pn->pn_kid3) {
if (js_NewSrcNote(cx, cg, SRC_HIDDEN) < 0)
return JS_FALSE;
jmp = EmitBackPatchOp(cx, cg, JSOP_BACKPATCH, &GOSUBS(stmtInfo));
if (jmp < 0)
return JS_FALSE;
/* JSOP_RETSUB pops the return pc-index, balancing the stack. */
cg->stackDepth = depth;
}
/* Emit (hidden) jump over catch and/or finally. */
if (js_NewSrcNote(cx, cg, SRC_HIDDEN) < 0)
return JS_FALSE;
jmp = EmitBackPatchOp(cx, cg, JSOP_BACKPATCH, &catchJump);
if (jmp < 0)
return JS_FALSE;
end = CG_OFFSET(cg);
/* If this try has a catch block, emit it. */
pn2 = pn->pn_kid2;
lastCatch = NULL;
if (pn2) {
jsint count = 0; /* previous catch block's population */
catchStart = end;
/*
* The emitted code for a catch block looks like:
*
* [throwing] only if 2nd+ catch block
* [leaveblock] only if 2nd+ catch block
* enterblock with SRC_CATCH
* exception
* [dup] only if catchguard
* setlocalpop <slot> or destructuring code
* [< catchguard code >] if there's a catchguard
* [ifeq <offset to next catch block>] " "
* [pop] only if catchguard
* < catch block contents >
* leaveblock
* goto <end of catch blocks> non-local; finally applies
*
* If there's no catch block without a catchguard, the last
* <offset to next catch block> points to rethrow code. This
* code will [gosub] to the finally code if appropriate, and is
* also used for the catch-all trynote for capturing exceptions
* thrown from catch{} blocks.
*/
for (pn3 = pn2->pn_head; pn3; pn3 = pn3->pn_next) {
ptrdiff_t guardJump, catchNote;
guardJump = GUARDJUMP(stmtInfo);
if (guardJump == -1) {
/* Set stack to original depth (see SETSP comment above). */
EMIT_UINT16_IMM_OP(JSOP_SETSP, (jsatomid)depth);
cg->stackDepth = depth;
} else {
/* Fix up and clean up previous catch block. */
CHECK_AND_SET_JUMP_OFFSET_AT(cx, cg, guardJump);
/*
* Account for the pushed exception object that we still
* have after the jumping from the previous guard.
*/
JS_ASSERT(cg->stackDepth == depth);
cg->stackDepth = depth + 1;
/*
* Move exception back to cx->exception to prepare for
* the next catch. We hide [throwing] from the decompiler
* since it compensates for the hidden JSOP_DUP at the
* start of the previous guarded catch.
*/
if (js_NewSrcNote(cx, cg, SRC_HIDDEN) < 0 ||
js_Emit1(cx, cg, JSOP_THROWING) < 0) {
return JS_FALSE;
}
/*
* Emit an unbalanced [leaveblock] for the previous catch,
* whose block object count is saved below.
*/
if (js_NewSrcNote(cx, cg, SRC_HIDDEN) < 0)
return JS_FALSE;
JS_ASSERT(count >= 0);
EMIT_UINT16_IMM_OP(JSOP_LEAVEBLOCK, count);
}
/*
* Annotate the JSOP_ENTERBLOCK that's about to be generated
* by the call to js_EmitTree immediately below. Save this
* source note's index in stmtInfo for use by the TOK_CATCH:
* case, where the length of the catch guard is set as the
* note's offset.
*/
catchNote = js_NewSrcNote2(cx, cg, SRC_CATCH, 0);
if (catchNote < 0)
return JS_FALSE;
CATCHNOTE(stmtInfo) = catchNote;
/*
* Emit the lexical scope and catch body. Save the catch's
* block object population via count, for use when targeting
* guardJump at the next catch (the guard mismatch case).
*/
JS_ASSERT(pn3->pn_type == TOK_LEXICALSCOPE);
count = OBJ_BLOCK_COUNT(cx, ATOM_TO_OBJECT(pn3->pn_atom));
if (!js_EmitTree(cx, cg, pn3))
return JS_FALSE;
/* gosub <finally>, if required */
if (pn->pn_kid3) {
jmp = EmitBackPatchOp(cx, cg, JSOP_BACKPATCH,
&GOSUBS(stmtInfo));
if (jmp < 0)
return JS_FALSE;
JS_ASSERT(cg->stackDepth == depth);
}
/*
* Jump over the remaining catch blocks. This will get fixed
* up to jump to after catch/finally.
*/
if (js_NewSrcNote(cx, cg, SRC_HIDDEN) < 0)
return JS_FALSE;
jmp = EmitBackPatchOp(cx, cg, JSOP_BACKPATCH, &catchJump);
if (jmp < 0)
return JS_FALSE;
/*
* Save a pointer to the last catch node to handle try-finally
* and try-catch(guard)-finally special cases.
*/
lastCatch = pn3->pn_expr;
}
}
/*
* Last catch guard jumps to the rethrow code sequence if none of the
* guards match. Target guardJump at the beginning of the rethrow
* sequence, just in case a guard expression throws and leaves the
* stack unbalanced.
*/
if (lastCatch && lastCatch->pn_kid2) {
CHECK_AND_SET_JUMP_OFFSET_AT(cx, cg, GUARDJUMP(stmtInfo));
/* Sync the stack to take into account pushed exception. */
JS_ASSERT(cg->stackDepth == depth);
cg->stackDepth = depth + 1;
/*
* Rethrow the exception, delegating executing of finally if any
* to the exception handler.
*/
if (js_NewSrcNote(cx, cg, SRC_HIDDEN) < 0 ||
js_Emit1(cx, cg, JSOP_THROW) < 0) {
return JS_FALSE;
}
}
JS_ASSERT(cg->stackDepth == depth);
/* Emit finally handler if any. */
if (pn->pn_kid3) {
/*
* We emit [setsp][gosub] to call try-finally when an exception is
* thrown from try or try-catch blocks. The [gosub] and [retsub]
* opcodes will take care of stacking and rethrowing any exception
* pending across the finally.
*/
finallyCatch = CG_OFFSET(cg);
EMIT_UINT16_IMM_OP(JSOP_SETSP, (jsatomid)depth);
jmp = EmitBackPatchOp(cx, cg, JSOP_BACKPATCH,
&GOSUBS(stmtInfo));
if (jmp < 0)
return JS_FALSE;
JS_ASSERT(cg->stackDepth == depth);
JS_ASSERT((uintN)depth <= cg->maxStackDepth);
/*
* Fix up the gosubs that might have been emitted before non-local
* jumps to the finally code.
*/
if (!BackPatch(cx, cg, GOSUBS(stmtInfo), CG_NEXT(cg), JSOP_GOSUB))
return JS_FALSE;
/*
* The stack budget must be balanced at this point. All [gosub]
* calls emitted before this point will push two stack slots, one
* for the pending exception (or JSVAL_HOLE if there is no pending
* exception) and one for the [retsub] pc-index.
*/
JS_ASSERT(cg->stackDepth == depth);
cg->stackDepth += 2;
if ((uintN)cg->stackDepth > cg->maxStackDepth)
cg->maxStackDepth = cg->stackDepth;
/* Now indicate that we're emitting a subroutine body. */
stmtInfo.type = STMT_SUBROUTINE;
if (!UpdateLineNumberNotes(cx, cg, pn->pn_kid3))
return JS_FALSE;
if (js_Emit1(cx, cg, JSOP_FINALLY) < 0 ||
!js_EmitTree(cx, cg, pn->pn_kid3) ||
js_Emit1(cx, cg, JSOP_RETSUB) < 0) {
return JS_FALSE;
}
/* Restore stack depth budget to its balanced state. */
JS_ASSERT(cg->stackDepth == depth + 2);
cg->stackDepth = depth;
}
if (!js_PopStatementCG(cx, cg))
return JS_FALSE;
if (js_NewSrcNote(cx, cg, SRC_ENDBRACE) < 0 ||
js_Emit1(cx, cg, JSOP_NOP) < 0) {
return JS_FALSE;
}
/* Fix up the end-of-try/catch jumps to come here. */
if (!BackPatch(cx, cg, catchJump, CG_NEXT(cg), JSOP_GOTO))
return JS_FALSE;
/*
* Add the try note last, to let post-order give us the right ordering
* (first to last for a given nesting level, inner to outer by level).
*/
if (pn->pn_kid2) {
JS_ASSERT(end != -1 && catchStart != -1);
if (!js_NewTryNote(cx, cg, start, end, catchStart))
return JS_FALSE;
}
/*
* If we've got a finally, mark try+catch region with additional
* trynote to catch exceptions (re)thrown from a catch block or
* for the try{}finally{} case.
*/
if (pn->pn_kid3) {
JS_ASSERT(finallyCatch != -1);
if (!js_NewTryNote(cx, cg, start, finallyCatch, finallyCatch))
return JS_FALSE;
}
break;
}
case TOK_CATCH:
{
ptrdiff_t catchStart, guardJump;
/*
* Morph STMT_BLOCK to STMT_CATCH, note the block entry code offset,
* and save the block object atom.
*/
stmt = cg->treeContext.topStmt;
JS_ASSERT(stmt->type == STMT_BLOCK && (stmt->flags & SIF_SCOPE));
stmt->type = STMT_CATCH;
catchStart = stmt->update;
atom = stmt->atom;
/* Go up one statement info record to the TRY or FINALLY record. */
stmt = stmt->down;
JS_ASSERT(stmt->type == STMT_TRY || stmt->type == STMT_FINALLY);
/* Pick up the pending exception and bind it to the catch variable. */
if (js_Emit1(cx, cg, JSOP_EXCEPTION) < 0)
return JS_FALSE;
/*
* Dup the exception object if there is a guard for rethrowing to use
* it later when rethrowing or in other catches.
*/
if (pn->pn_kid2) {
if (js_NewSrcNote(cx, cg, SRC_HIDDEN) < 0 ||
js_Emit1(cx, cg, JSOP_DUP) < 0) {
return JS_FALSE;
}
}
pn2 = pn->pn_kid1;
switch (pn2->pn_type) {
#if JS_HAS_DESTRUCTURING
case TOK_RB:
case TOK_RC:
if (!EmitDestructuringOps(cx, cg, JSOP_NOP, pn2))
return JS_FALSE;
if (js_Emit1(cx, cg, JSOP_POP) < 0)
return JS_FALSE;
break;
#endif
case TOK_NAME:
/* Inline BindNameToSlot, adding block depth to pn2->pn_slot. */
pn2->pn_slot += OBJ_BLOCK_DEPTH(cx, ATOM_TO_OBJECT(atom));
EMIT_UINT16_IMM_OP(JSOP_SETLOCALPOP, pn2->pn_slot);
break;
default:
JS_ASSERT(0);
}
/* Emit the guard expression, if there is one. */
if (pn->pn_kid2) {
if (!js_EmitTree(cx, cg, pn->pn_kid2))
return JS_FALSE;
if (!js_SetSrcNoteOffset(cx, cg, CATCHNOTE(*stmt), 0,
CG_OFFSET(cg) - catchStart)) {
return JS_FALSE;
}
/* ifeq <next block> */
guardJump = EmitJump(cx, cg, JSOP_IFEQ, 0);
if (guardJump < 0)
return JS_FALSE;
GUARDJUMP(*stmt) = guardJump;
/* Pop duplicated exception object as we no longer need it. */
if (js_NewSrcNote(cx, cg, SRC_HIDDEN) < 0 ||
js_Emit1(cx, cg, JSOP_POP) < 0) {
return JS_FALSE;
}
}
/* Emit the catch body. */
if (!js_EmitTree(cx, cg, pn->pn_kid3))
return JS_FALSE;
/*
* Annotate the JSOP_LEAVEBLOCK that will be emitted as we unwind via
* our TOK_LEXICALSCOPE parent, so the decompiler knows to pop.
*/
off = cg->stackDepth;
if (js_NewSrcNote2(cx, cg, SRC_CATCH, off) < 0)
return JS_FALSE;
break;
}
case TOK_VAR:
if (!EmitVariables(cx, cg, pn, JS_FALSE, ¬eIndex))
return JS_FALSE;
break;
case TOK_RETURN:
/* Push a return value */
pn2 = pn->pn_kid;
if (pn2) {
if (!js_EmitTree(cx, cg, pn2))
return JS_FALSE;
} else {
if (js_Emit1(cx, cg, JSOP_PUSH) < 0)
return JS_FALSE;
}
/*
* EmitNonLocalJumpFixup mutates op to JSOP_RETRVAL after emitting a
* JSOP_SETRVAL if there are open try blocks having finally clauses.
* We can't simply transfer control flow to our caller in that case,
* because we must gosub to those clauses from inner to outer, with
* the correct stack pointer (i.e., after popping any with, for/in,
* etc., slots nested inside the finally's try).
*/
op = JSOP_RETURN;
if (!EmitNonLocalJumpFixup(cx, cg, NULL, &op))
return JS_FALSE;
if (js_Emit1(cx, cg, op) < 0)
return JS_FALSE;
break;
#if JS_HAS_GENERATORS
case TOK_YIELD:
if (pn->pn_kid) {
if (!js_EmitTree(cx, cg, pn->pn_kid))
return JS_FALSE;
} else {
if (js_Emit1(cx, cg, JSOP_PUSH) < 0)
return JS_FALSE;
}
if (js_Emit1(cx, cg, JSOP_YIELD) < 0)
return JS_FALSE;
break;
#endif
case TOK_LC:
#if JS_HAS_XML_SUPPORT
if (pn->pn_arity == PN_UNARY) {
if (!js_EmitTree(cx, cg, pn->pn_kid))
return JS_FALSE;
if (js_Emit1(cx, cg, pn->pn_op) < 0)
return JS_FALSE;
break;
}
#endif
JS_ASSERT(pn->pn_arity == PN_LIST);
noteIndex = -1;
tmp = CG_OFFSET(cg);
if (pn->pn_extra & PNX_NEEDBRACES) {
noteIndex = js_NewSrcNote2(cx, cg, SRC_BRACE, 0);
if (noteIndex < 0 || js_Emit1(cx, cg, JSOP_NOP) < 0)
return JS_FALSE;
}
js_PushStatement(&cg->treeContext, &stmtInfo, STMT_BLOCK, top);
for (pn2 = pn->pn_head; pn2; pn2 = pn2->pn_next) {
if (!js_EmitTree(cx, cg, pn2))
return JS_FALSE;
}
if (noteIndex >= 0 &&
!js_SetSrcNoteOffset(cx, cg, (uintN)noteIndex, 0,
CG_OFFSET(cg) - tmp)) {
return JS_FALSE;
}
ok = js_PopStatementCG(cx, cg);
break;
case TOK_BODY:
JS_ASSERT(pn->pn_arity == PN_LIST);
js_PushStatement(&cg->treeContext, &stmtInfo, STMT_BODY, top);
for (pn2 = pn->pn_head; pn2; pn2 = pn2->pn_next) {
if (!js_EmitTree(cx, cg, pn2))
return JS_FALSE;
}
ok = js_PopStatementCG(cx, cg);
break;
case TOK_SEMI:
pn2 = pn->pn_kid;
if (pn2) {
/*
* Top-level or called-from-a-native JS_Execute/EvaluateScript,
* debugger, and eval frames may need the value of the ultimate
* expression statement as the script's result, despite the fact
* that it appears useless to the compiler.
*/
useful = wantval = !cx->fp->fun ||
!FUN_INTERPRETED(cx->fp->fun) ||
(cx->fp->flags & JSFRAME_SPECIAL);
if (!useful) {
if (!CheckSideEffects(cx, &cg->treeContext, pn2, &useful))
return JS_FALSE;
}
/*
* Don't eliminate apparently useless expressions if they are
* labeled expression statements. The tc->topStmt->update test
* catches the case where we are nesting in js_EmitTree for a
* labeled compound statement.
*/
if (!useful &&
(!cg->treeContext.topStmt ||
cg->treeContext.topStmt->type != STMT_LABEL ||
cg->treeContext.topStmt->update < CG_OFFSET(cg))) {
CG_CURRENT_LINE(cg) = pn2->pn_pos.begin.lineno;
if (!js_ReportCompileErrorNumber(cx, cg,
JSREPORT_CG |
JSREPORT_WARNING |
JSREPORT_STRICT,
JSMSG_USELESS_EXPR)) {
return JS_FALSE;
}
} else {
op = wantval ? JSOP_POPV : JSOP_POP;
#if JS_HAS_DESTRUCTURING
if (!wantval &&
pn2->pn_type == TOK_ASSIGN &&
!MaybeEmitGroupAssignment(cx, cg, op, pn2, &op)) {
return JS_FALSE;
}
#endif
if (op != JSOP_NOP) {
if (!js_EmitTree(cx, cg, pn2))
return JS_FALSE;
if (js_Emit1(cx, cg, op) < 0)
return JS_FALSE;
}
}
}
break;
case TOK_COLON:
/* Emit an annotated nop so we know to decompile a label. */
atom = pn->pn_atom;
ale = js_IndexAtom(cx, atom, &cg->atomList);
if (!ale)
return JS_FALSE;
pn2 = pn->pn_expr;
noteType = (pn2->pn_type == TOK_LC ||
(pn2->pn_type == TOK_LEXICALSCOPE &&
pn2->pn_expr->pn_type == TOK_LC))
? SRC_LABELBRACE
: SRC_LABEL;
noteIndex = js_NewSrcNote2(cx, cg, noteType,
(ptrdiff_t) ALE_INDEX(ale));
if (noteIndex < 0 ||
js_Emit1(cx, cg, JSOP_NOP) < 0) {
return JS_FALSE;
}
/* Emit code for the labeled statement. */
js_PushStatement(&cg->treeContext, &stmtInfo, STMT_LABEL,
CG_OFFSET(cg));
stmtInfo.atom = atom;
if (!js_EmitTree(cx, cg, pn2))
return JS_FALSE;
if (!js_PopStatementCG(cx, cg))
return JS_FALSE;
/* If the statement was compound, emit a note for the end brace. */
if (noteType == SRC_LABELBRACE) {
if (js_NewSrcNote(cx, cg, SRC_ENDBRACE) < 0 ||
js_Emit1(cx, cg, JSOP_NOP) < 0) {
return JS_FALSE;
}
}
break;
case TOK_COMMA:
/*
* Emit SRC_PCDELTA notes on each JSOP_POP between comma operands.
* These notes help the decompiler bracket the bytecodes generated
* from each sub-expression that follows a comma.
*/
off = noteIndex = -1;
for (pn2 = pn->pn_head; ; pn2 = pn2->pn_next) {
if (!js_EmitTree(cx, cg, pn2))
return JS_FALSE;
tmp = CG_OFFSET(cg);
if (noteIndex >= 0) {
if (!js_SetSrcNoteOffset(cx, cg, (uintN)noteIndex, 0, tmp-off))
return JS_FALSE;
}
if (!pn2->pn_next)
break;
off = tmp;
noteIndex = js_NewSrcNote2(cx, cg, SRC_PCDELTA, 0);
if (noteIndex < 0 ||
js_Emit1(cx, cg, JSOP_POP) < 0) {
return JS_FALSE;
}
}
break;
case TOK_ASSIGN:
/*
* Check left operand type and generate specialized code for it.
* Specialize to avoid ECMA "reference type" values on the operand
* stack, which impose pervasive runtime "GetValue" costs.
*/
pn2 = pn->pn_left;
JS_ASSERT(pn2->pn_type != TOK_RP);
atomIndex = (jsatomid) -1;
switch (pn2->pn_type) {
case TOK_NAME:
if (!BindNameToSlot(cx, &cg->treeContext, pn2, JS_FALSE))
return JS_FALSE;
if (pn2->pn_slot >= 0) {
atomIndex = (jsatomid) pn2->pn_slot;
} else {
ale = js_IndexAtom(cx, pn2->pn_atom, &cg->atomList);
if (!ale)
return JS_FALSE;
atomIndex = ALE_INDEX(ale);
EMIT_ATOM_INDEX_OP(JSOP_BINDNAME, atomIndex);
}
break;
case TOK_DOT:
if (!js_EmitTree(cx, cg, pn2->pn_expr))
return JS_FALSE;
ale = js_IndexAtom(cx, pn2->pn_atom, &cg->atomList);
if (!ale)
return JS_FALSE;
atomIndex = ALE_INDEX(ale);
break;
case TOK_LB:
JS_ASSERT(pn2->pn_arity == PN_BINARY);
if (!js_EmitTree(cx, cg, pn2->pn_left))
return JS_FALSE;
if (!js_EmitTree(cx, cg, pn2->pn_right))
return JS_FALSE;
break;
#if JS_HAS_DESTRUCTURING
case TOK_RB:
case TOK_RC:
break;
#endif
#if JS_HAS_LVALUE_RETURN
case TOK_LP:
if (!js_EmitTree(cx, cg, pn2))
return JS_FALSE;
break;
#endif
#if JS_HAS_XML_SUPPORT
case TOK_UNARYOP:
JS_ASSERT(pn2->pn_op == JSOP_SETXMLNAME);
if (!js_EmitTree(cx, cg, pn2->pn_kid))
return JS_FALSE;
if (js_Emit1(cx, cg, JSOP_BINDXMLNAME) < 0)
return JS_FALSE;
break;
#endif
default:
JS_ASSERT(0);
}
op = pn->pn_op;
#if JS_HAS_GETTER_SETTER
if (op == JSOP_GETTER || op == JSOP_SETTER) {
/* We'll emit these prefix bytecodes after emitting the r.h.s. */
if (atomIndex != (jsatomid) -1 && atomIndex >= JS_BIT(16)) {
ReportStatementTooLarge(cx, cg);
return JS_FALSE;
}
} else
#endif
/* If += or similar, dup the left operand and get its value. */
if (op != JSOP_NOP) {
switch (pn2->pn_type) {
case TOK_NAME:
if (pn2->pn_op != JSOP_SETNAME) {
EMIT_UINT16_IMM_OP((pn2->pn_op == JSOP_SETGVAR)
? JSOP_GETGVAR
: (pn2->pn_op == JSOP_SETARG)
? JSOP_GETARG
: (pn2->pn_op == JSOP_SETLOCAL)
? JSOP_GETLOCAL
: JSOP_GETVAR,
atomIndex);
break;
}
/* FALL THROUGH */
case TOK_DOT:
if (js_Emit1(cx, cg, JSOP_DUP) < 0)
return JS_FALSE;
EMIT_ATOM_INDEX_OP((pn2->pn_type == TOK_NAME)
? JSOP_GETXPROP
: JSOP_GETPROP,
atomIndex);
break;
case TOK_LB:
#if JS_HAS_LVALUE_RETURN
case TOK_LP:
#endif
#if JS_HAS_XML_SUPPORT
case TOK_UNARYOP:
#endif
if (js_Emit1(cx, cg, JSOP_DUP2) < 0)
return JS_FALSE;
if (js_Emit1(cx, cg, JSOP_GETELEM) < 0)
return JS_FALSE;
break;
default:;
}
}
/* Now emit the right operand (it may affect the namespace). */
if (!js_EmitTree(cx, cg, pn->pn_right))
return JS_FALSE;
/* If += etc., emit the binary operator with a decompiler note. */
if (op != JSOP_NOP) {
/*
* Take care to avoid SRC_ASSIGNOP if the left-hand side is a
* const declared in a function (i.e., with non-negative pn_slot
* and JSPROP_READONLY in pn_attrs), as in this case (just a bit
* further below) we will avoid emitting the assignment op.
*/
if (pn2->pn_type != TOK_NAME ||
pn2->pn_slot < 0 ||
!(pn2->pn_attrs & JSPROP_READONLY)) {
if (js_NewSrcNote(cx, cg, SRC_ASSIGNOP) < 0)
return JS_FALSE;
}
if (js_Emit1(cx, cg, op) < 0)
return JS_FALSE;
}
/* Left parts such as a.b.c and a[b].c need a decompiler note. */
if (pn2->pn_type != TOK_NAME &&
#if JS_HAS_DESTRUCTURING
pn2->pn_type != TOK_RB &&
pn2->pn_type != TOK_RC &&
#endif
js_NewSrcNote2(cx, cg, SrcNoteForPropOp(pn2, pn2->pn_op),
CG_OFFSET(cg) - top) < 0) {
return JS_FALSE;
}
/* Finally, emit the specialized assignment bytecode. */
switch (pn2->pn_type) {
case TOK_NAME:
if (pn2->pn_slot < 0 || !(pn2->pn_attrs & JSPROP_READONLY)) {
if (pn2->pn_slot >= 0) {
EMIT_UINT16_IMM_OP(pn2->pn_op, atomIndex);
} else {
case TOK_DOT:
EMIT_ATOM_INDEX_OP(pn2->pn_op, atomIndex);
}
}
break;
case TOK_LB:
#if JS_HAS_LVALUE_RETURN
case TOK_LP:
#endif
if (js_Emit1(cx, cg, JSOP_SETELEM) < 0)
return JS_FALSE;
break;
#if JS_HAS_DESTRUCTURING
case TOK_RB:
case TOK_RC:
if (!EmitDestructuringOps(cx, cg, JSOP_SETNAME, pn2))
return JS_FALSE;
break;
#endif
#if JS_HAS_XML_SUPPORT
case TOK_UNARYOP:
if (js_Emit1(cx, cg, JSOP_SETXMLNAME) < 0)
return JS_FALSE;
break;
#endif
default:
JS_ASSERT(0);
}
break;
case TOK_HOOK:
/* Emit the condition, then branch if false to the else part. */
if (!js_EmitTree(cx, cg, pn->pn_kid1))
return JS_FALSE;
noteIndex = js_NewSrcNote(cx, cg, SRC_COND);
if (noteIndex < 0)
return JS_FALSE;
beq = EmitJump(cx, cg, JSOP_IFEQ, 0);
if (beq < 0 || !js_EmitTree(cx, cg, pn->pn_kid2))
return JS_FALSE;
/* Jump around else, fixup the branch, emit else, fixup jump. */
jmp = EmitJump(cx, cg, JSOP_GOTO, 0);
if (jmp < 0)
return JS_FALSE;
CHECK_AND_SET_JUMP_OFFSET_AT(cx, cg, beq);
/*
* Because each branch pushes a single value, but our stack budgeting
* analysis ignores branches, we now have to adjust cg->stackDepth to
* ignore the value pushed by the first branch. Execution will follow
* only one path, so we must decrement cg->stackDepth.
*
* Failing to do this will foil code, such as the try/catch/finally
* exception handling code generator, that samples cg->stackDepth for
* use at runtime (JSOP_SETSP), or in let expression and block code
* generation, which must use the stack depth to compute local stack
* indexes correctly.
*/
JS_ASSERT(cg->stackDepth > 0);
cg->stackDepth--;
if (!js_EmitTree(cx, cg, pn->pn_kid3))
return JS_FALSE;
CHECK_AND_SET_JUMP_OFFSET_AT(cx, cg, jmp);
if (!js_SetSrcNoteOffset(cx, cg, noteIndex, 0, jmp - beq))
return JS_FALSE;
break;
case TOK_OR:
case TOK_AND:
/*
* JSOP_OR converts the operand on the stack to boolean, and if true,
* leaves the original operand value on the stack and jumps; otherwise
* it pops and falls into the next bytecode, which evaluates the right
* operand. The jump goes around the right operand evaluation.
*
* JSOP_AND converts the operand on the stack to boolean, and if false,
* leaves the original operand value on the stack and jumps; otherwise
* it pops and falls into the right operand's bytecode.
*
* Avoid tail recursion for long ||...|| expressions and long &&...&&
* expressions or long mixtures of ||'s and &&'s that can easily blow
* the stack, by forward-linking and then backpatching all the JSOP_OR
* and JSOP_AND bytecodes' immediate jump-offset operands.
*/
pn3 = pn;
if (!js_EmitTree(cx, cg, pn->pn_left))
return JS_FALSE;
top = EmitJump(cx, cg, JSOP_BACKPATCH_POP, 0);
if (top < 0)
return JS_FALSE;
jmp = top;
pn2 = pn->pn_right;
while (pn2->pn_type == TOK_OR || pn2->pn_type == TOK_AND) {
pn = pn2;
if (!js_EmitTree(cx, cg, pn->pn_left))
return JS_FALSE;
off = EmitJump(cx, cg, JSOP_BACKPATCH_POP, 0);
if (off < 0)
return JS_FALSE;
if (!SetBackPatchDelta(cx, cg, CG_CODE(cg, jmp), off - jmp))
return JS_FALSE;
jmp = off;
pn2 = pn->pn_right;
}
if (!js_EmitTree(cx, cg, pn2))
return JS_FALSE;
off = CG_OFFSET(cg);
do {
pc = CG_CODE(cg, top);
tmp = GetJumpOffset(cg, pc);
CHECK_AND_SET_JUMP_OFFSET(cx, cg, pc, off - top);
*pc = pn3->pn_op;
top += tmp;
} while ((pn3 = pn3->pn_right) != pn2);
break;
case TOK_BITOR:
case TOK_BITXOR:
case TOK_BITAND:
case TOK_EQOP:
case TOK_RELOP:
case TOK_IN:
case TOK_INSTANCEOF:
case TOK_SHOP:
case TOK_PLUS:
case TOK_MINUS:
case TOK_STAR:
case TOK_DIVOP:
if (pn->pn_arity == PN_LIST) {
/* Left-associative operator chain: avoid too much recursion. */
pn2 = pn->pn_head;
if (!js_EmitTree(cx, cg, pn2))
return JS_FALSE;
op = pn->pn_op;
while ((pn2 = pn2->pn_next) != NULL) {
if (!js_EmitTree(cx, cg, pn2))
return JS_FALSE;
if (js_Emit1(cx, cg, op) < 0)
return JS_FALSE;
}
} else {
#if JS_HAS_XML_SUPPORT
uintN oldflags;
case TOK_DBLCOLON:
if (pn->pn_arity == PN_NAME) {
if (!js_EmitTree(cx, cg, pn->pn_expr))
return JS_FALSE;
if (!EmitAtomOp(cx, pn, pn->pn_op, cg))
return JS_FALSE;
break;
}
/*
* Binary :: has a right operand that brackets arbitrary code,
* possibly including a let (a = b) ... expression. We must clear
* TCF_IN_FOR_INIT to avoid mis-compiling such beasts.
*/
oldflags = cg->treeContext.flags;
cg->treeContext.flags &= ~TCF_IN_FOR_INIT;
#endif
/* Binary operators that evaluate both operands unconditionally. */
if (!js_EmitTree(cx, cg, pn->pn_left))
return JS_FALSE;
if (!js_EmitTree(cx, cg, pn->pn_right))
return JS_FALSE;
#if JS_HAS_XML_SUPPORT
cg->treeContext.flags |= oldflags & TCF_IN_FOR_INIT;
#endif
if (js_Emit1(cx, cg, pn->pn_op) < 0)
return JS_FALSE;
}
break;
case TOK_THROW:
#if JS_HAS_XML_SUPPORT
case TOK_AT:
case TOK_DEFAULT:
JS_ASSERT(pn->pn_arity == PN_UNARY);
/* FALL THROUGH */
#endif
case TOK_UNARYOP:
{
uintN oldflags;
/* Unary op, including unary +/-. */
pn2 = pn->pn_kid;
op = pn->pn_op;
if (op == JSOP_TYPEOF) {
for (pn3 = pn2; pn3->pn_type == TOK_RP; pn3 = pn3->pn_kid)
continue;
if (pn3->pn_type != TOK_NAME)
op = JSOP_TYPEOFEXPR;
}
oldflags = cg->treeContext.flags;
cg->treeContext.flags &= ~TCF_IN_FOR_INIT;
if (!js_EmitTree(cx, cg, pn2))
return JS_FALSE;
cg->treeContext.flags |= oldflags & TCF_IN_FOR_INIT;
#if JS_HAS_XML_SUPPORT
if (op == JSOP_XMLNAME &&
js_NewSrcNote2(cx, cg, SRC_PCBASE,
CG_OFFSET(cg) - pn2->pn_offset) < 0) {
return JS_FALSE;
}
#endif
if (js_Emit1(cx, cg, op) < 0)
return JS_FALSE;
break;
}
case TOK_INC:
case TOK_DEC:
{
intN depth;
/* Emit lvalue-specialized code for ++/-- operators. */
pn2 = pn->pn_kid;
JS_ASSERT(pn2->pn_type != TOK_RP);
op = pn->pn_op;
depth = cg->stackDepth;
switch (pn2->pn_type) {
case TOK_NAME:
pn2->pn_op = op;
if (!BindNameToSlot(cx, &cg->treeContext, pn2, JS_FALSE))
return JS_FALSE;
op = pn2->pn_op;
if (pn2->pn_slot >= 0) {
if (pn2->pn_attrs & JSPROP_READONLY) {
/* Incrementing a declared const: just get its value. */
op = ((js_CodeSpec[op].format & JOF_TYPEMASK) == JOF_CONST)
? JSOP_GETGVAR
: JSOP_GETVAR;
}
atomIndex = (jsatomid) pn2->pn_slot;
EMIT_UINT16_IMM_OP(op, atomIndex);
} else {
if (!EmitAtomOp(cx, pn2, op, cg))
return JS_FALSE;
}
break;
case TOK_DOT:
if (!EmitPropOp(cx, pn2, op, cg))
return JS_FALSE;
++depth;
break;
case TOK_LB:
if (!EmitElemOp(cx, pn2, op, cg))
return JS_FALSE;
depth += 2;
break;
#if JS_HAS_LVALUE_RETURN
case TOK_LP:
if (!js_EmitTree(cx, cg, pn2))
return JS_FALSE;
depth = cg->stackDepth;
if (js_NewSrcNote2(cx, cg, SRC_PCBASE,
CG_OFFSET(cg) - pn2->pn_offset) < 0) {
return JS_FALSE;
}
if (js_Emit1(cx, cg, op) < 0)
return JS_FALSE;
break;
#endif
#if JS_HAS_XML_SUPPORT
case TOK_UNARYOP:
JS_ASSERT(pn2->pn_op == JSOP_SETXMLNAME);
if (!js_EmitTree(cx, cg, pn2->pn_kid))
return JS_FALSE;
if (js_Emit1(cx, cg, JSOP_BINDXMLNAME) < 0)
return JS_FALSE;
depth = cg->stackDepth;
if (js_Emit1(cx, cg, op) < 0)
return JS_FALSE;
break;
#endif
default:
JS_ASSERT(0);
}
/*
* Allocate another stack slot for GC protection in case the initial
* value being post-incremented or -decremented is not a number, but
* converts to a jsdouble. In the TOK_NAME cases, op has 0 operand
* uses and 1 definition, so we don't need an extra stack slot -- we
* can use the one allocated for the def.
*/
if (pn2->pn_type != TOK_NAME &&
(js_CodeSpec[op].format & JOF_POST) &&
(uintN)depth == cg->maxStackDepth) {
++cg->maxStackDepth;
}
break;
}
case TOK_DELETE:
/*
* Under ECMA 3, deleting a non-reference returns true -- but alas we
* must evaluate the operand if it appears it might have side effects.
*/
pn2 = pn->pn_kid;
switch (pn2->pn_type) {
case TOK_NAME:
pn2->pn_op = JSOP_DELNAME;
if (!BindNameToSlot(cx, &cg->treeContext, pn2, JS_FALSE))
return JS_FALSE;
op = pn2->pn_op;
if (op == JSOP_FALSE) {
if (js_Emit1(cx, cg, op) < 0)
return JS_FALSE;
} else {
if (!EmitAtomOp(cx, pn2, op, cg))
return JS_FALSE;
}
break;
case TOK_DOT:
if (!EmitPropOp(cx, pn2, JSOP_DELPROP, cg))
return JS_FALSE;
break;
#if JS_HAS_XML_SUPPORT
case TOK_DBLDOT:
if (!EmitElemOp(cx, pn2, JSOP_DELDESC, cg))
return JS_FALSE;
break;
#endif
#if JS_HAS_LVALUE_RETURN
case TOK_LP:
if (pn2->pn_op != JSOP_SETCALL) {
JS_ASSERT(pn2->pn_op == JSOP_CALL || pn2->pn_op == JSOP_EVAL);
pn2->pn_op = JSOP_SETCALL;
}
top = CG_OFFSET(cg);
if (!js_EmitTree(cx, cg, pn2))
return JS_FALSE;
if (js_NewSrcNote2(cx, cg, SRC_PCBASE, CG_OFFSET(cg) - top) < 0)
return JS_FALSE;
if (js_Emit1(cx, cg, JSOP_DELELEM) < 0)
return JS_FALSE;
break;
#endif
case TOK_LB:
if (!EmitElemOp(cx, pn2, JSOP_DELELEM, cg))
return JS_FALSE;
break;
default:
/*
* If useless, just emit JSOP_TRUE; otherwise convert delete foo()
* to foo(), true (a comma expression, requiring SRC_PCDELTA).
*/
useful = JS_FALSE;
if (!CheckSideEffects(cx, &cg->treeContext, pn2, &useful))
return JS_FALSE;
if (!useful) {
off = noteIndex = -1;
} else {
if (!js_EmitTree(cx, cg, pn2))
return JS_FALSE;
off = CG_OFFSET(cg);
noteIndex = js_NewSrcNote2(cx, cg, SRC_PCDELTA, 0);
if (noteIndex < 0 || js_Emit1(cx, cg, JSOP_POP) < 0)
return JS_FALSE;
}
if (js_Emit1(cx, cg, JSOP_TRUE) < 0)
return JS_FALSE;
if (noteIndex >= 0) {
tmp = CG_OFFSET(cg);
if (!js_SetSrcNoteOffset(cx, cg, (uintN)noteIndex, 0, tmp-off))
return JS_FALSE;
}
}
break;
#if JS_HAS_XML_SUPPORT
case TOK_FILTER:
if (!js_EmitTree(cx, cg, pn->pn_left))
return JS_FALSE;
jmp = js_Emit3(cx, cg, JSOP_FILTER, 0, 0);
if (jmp < 0)
return JS_FALSE;
if (!js_EmitTree(cx, cg, pn->pn_right))
return JS_FALSE;
if (js_Emit1(cx, cg, JSOP_ENDFILTER) < 0)
return JS_FALSE;
CHECK_AND_SET_JUMP_OFFSET_AT(cx, cg, jmp);
break;
#endif
case TOK_DOT:
/*
* Pop a stack operand, convert it to object, get a property named by
* this bytecode's immediate-indexed atom operand, and push its value
* (not a reference to it). This bytecode sets the virtual machine's
* "obj" register to the left operand's ToObject conversion result,
* for use by JSOP_PUSHOBJ.
*/
ok = EmitPropOp(cx, pn, pn->pn_op, cg);
break;
case TOK_LB:
#if JS_HAS_XML_SUPPORT
case TOK_DBLDOT:
#endif
/*
* Pop two operands, convert the left one to object and the right one
* to property name (atom or tagged int), get the named property, and
* push its value. Set the "obj" register to the result of ToObject
* on the left operand.
*/
ok = EmitElemOp(cx, pn, pn->pn_op, cg);
break;
case TOK_NEW:
case TOK_LP:
{
uintN oldflags;
/*
* Emit function call or operator new (constructor call) code.
* First, emit code for the left operand to evaluate the callable or
* constructable object expression.
*
* For E4X, if this expression is a dotted member reference, select
* JSOP_GETMETHOD instead of JSOP_GETPROP. ECMA-357 separates XML
* method lookup from the normal property id lookup done for native
* objects.
*/
pn2 = pn->pn_head;
#if JS_HAS_XML_SUPPORT
if (pn2->pn_type == TOK_DOT && pn2->pn_op != JSOP_GETMETHOD) {
JS_ASSERT(pn2->pn_op == JSOP_GETPROP);
pn2->pn_op = JSOP_GETMETHOD;
pn2->pn_attrs |= JSPROP_IMPLICIT_FUNCTION_NAMESPACE;
}
#endif
if (!js_EmitTree(cx, cg, pn2))
return JS_FALSE;
/*
* Push the virtual machine's "obj" register, which was set by a
* name, property, or element get (or set) bytecode.
*/
if (js_Emit1(cx, cg, JSOP_PUSHOBJ) < 0)
return JS_FALSE;
/* Remember start of callable-object bytecode for decompilation hint. */
off = top;
/*
* Emit code for each argument in order, then emit the JSOP_*CALL or
* JSOP_NEW bytecode with a two-byte immediate telling how many args
* were pushed on the operand stack.
*/
oldflags = cg->treeContext.flags;
cg->treeContext.flags &= ~TCF_IN_FOR_INIT;
for (pn2 = pn2->pn_next; pn2; pn2 = pn2->pn_next) {
if (!js_EmitTree(cx, cg, pn2))
return JS_FALSE;
}
cg->treeContext.flags |= oldflags & TCF_IN_FOR_INIT;
if (js_NewSrcNote2(cx, cg, SRC_PCBASE, CG_OFFSET(cg) - off) < 0)
return JS_FALSE;
argc = pn->pn_count - 1;
if (js_Emit3(cx, cg, pn->pn_op, ARGC_HI(argc), ARGC_LO(argc)) < 0)
return JS_FALSE;
break;
}
case TOK_LEXICALSCOPE:
{
JSObject *obj;
jsint count;
atom = pn->pn_atom;
obj = ATOM_TO_OBJECT(atom);
js_PushBlockScope(&cg->treeContext, &stmtInfo, atom, CG_OFFSET(cg));
OBJ_SET_BLOCK_DEPTH(cx, obj, cg->stackDepth);
count = OBJ_BLOCK_COUNT(cx, obj);
cg->stackDepth += count;
if ((uintN)cg->stackDepth > cg->maxStackDepth)
cg->maxStackDepth = cg->stackDepth;
/*
* If this lexical scope is not for a catch block, let block or let
* expression, or any kind of for loop (where the scope starts in the
* head after the first part if for (;;), else in the body if for-in);
* and if our container is top-level but not a function body, or else
* a block statement; then emit a SRC_BRACE note. All other container
* statements get braces by default from the decompiler.
*/
noteIndex = -1;
type = pn->pn_expr->pn_type;
if (type != TOK_CATCH && type != TOK_LET && type != TOK_FOR &&
(!(stmt = stmtInfo.down)
? !(cg->treeContext.flags & TCF_IN_FUNCTION)
: stmt->type == STMT_BLOCK)) {
#if defined DEBUG_brendan || defined DEBUG_mrbkap
/* There must be no source note already output for the next op. */
JS_ASSERT(CG_NOTE_COUNT(cg) == 0 ||
CG_LAST_NOTE_OFFSET(cg) != CG_OFFSET(cg) ||
!GettableNoteForNextOp(cg));
#endif
noteIndex = js_NewSrcNote2(cx, cg, SRC_BRACE, 0);
if (noteIndex < 0)
return JS_FALSE;
}
ale = js_IndexAtom(cx, atom, &cg->atomList);
if (!ale)
return JS_FALSE;
JS_ASSERT(CG_OFFSET(cg) == top);
EMIT_ATOM_INDEX_OP(JSOP_ENTERBLOCK, ALE_INDEX(ale));
if (!js_EmitTree(cx, cg, pn->pn_expr))
return JS_FALSE;
op = pn->pn_op;
if (op == JSOP_LEAVEBLOCKEXPR) {
if (js_NewSrcNote2(cx, cg, SRC_PCBASE, CG_OFFSET(cg) - top) < 0)
return JS_FALSE;
} else {
if (noteIndex >= 0 &&
!js_SetSrcNoteOffset(cx, cg, (uintN)noteIndex, 0,
CG_OFFSET(cg) - top)) {
return JS_FALSE;
}
}
/* Emit the JSOP_LEAVEBLOCK or JSOP_LEAVEBLOCKEXPR opcode. */
EMIT_UINT16_IMM_OP(op, count);
cg->stackDepth -= count;
ok = js_PopStatementCG(cx, cg);
break;
}
#if JS_HAS_BLOCK_SCOPE
case TOK_LET:
/* Let statements have their variable declarations on the left. */
if (pn->pn_arity == PN_BINARY) {
pn2 = pn->pn_right;
pn = pn->pn_left;
} else {
pn2 = NULL;
}
/* Non-null pn2 means that pn is the variable list from a let head. */
JS_ASSERT(pn->pn_arity == PN_LIST);
if (!EmitVariables(cx, cg, pn, pn2 != NULL, ¬eIndex))
return JS_FALSE;
/* Thus non-null pn2 is the body of the let block or expression. */
tmp = CG_OFFSET(cg);
if (pn2 && !js_EmitTree(cx, cg, pn2))
return JS_FALSE;
if (noteIndex >= 0 &&
!js_SetSrcNoteOffset(cx, cg, (uintN)noteIndex, 0,
CG_OFFSET(cg) - tmp)) {
return JS_FALSE;
}
break;
#endif /* JS_HAS_BLOCK_SCOPE */
#if JS_HAS_GENERATORS
case TOK_ARRAYPUSH:
/*
* The array object's stack index is in cg->arrayCompSlot. See below
* under the array initialiser code generator for array comprehension
* special casing.
*/
if (!js_EmitTree(cx, cg, pn->pn_kid))
return JS_FALSE;
EMIT_UINT16_IMM_OP(pn->pn_op, cg->arrayCompSlot);
break;
#endif
case TOK_RB:
#if JS_HAS_GENERATORS
case TOK_ARRAYCOMP:
#endif
/*
* Emit code for [a, b, c] of the form:
* t = new Array; t[0] = a; t[1] = b; t[2] = c; t;
* but use a stack slot for t and avoid dup'ing and popping it via
* the JSOP_NEWINIT and JSOP_INITELEM bytecodes.
*/
ale = js_IndexAtom(cx, CLASS_ATOM(cx, Array), &cg->atomList);
if (!ale)
return JS_FALSE;
EMIT_ATOM_INDEX_OP(JSOP_NAME, ALE_INDEX(ale));
if (js_Emit1(cx, cg, JSOP_PUSHOBJ) < 0)
return JS_FALSE;
if (js_Emit1(cx, cg, JSOP_NEWINIT) < 0)
return JS_FALSE;
pn2 = pn->pn_head;
#if JS_HAS_SHARP_VARS
if (pn2 && pn2->pn_type == TOK_DEFSHARP) {
EMIT_UINT16_IMM_OP(JSOP_DEFSHARP, (jsatomid)pn2->pn_num);
pn2 = pn2->pn_next;
}
#endif
#if JS_HAS_GENERATORS
if (pn->pn_type == TOK_ARRAYCOMP) {
uintN saveSlot;
/*
* Pass the new array's stack index to the TOK_ARRAYPUSH case by
* storing it in pn->pn_extra, then simply traverse the TOK_FOR
* node and its kids under pn2 to generate this comprehension.
*/
JS_ASSERT(cg->stackDepth > 0);
saveSlot = cg->arrayCompSlot;
cg->arrayCompSlot = (uint32) (cg->stackDepth - 1);
if (!js_EmitTree(cx, cg, pn2))
return JS_FALSE;
cg->arrayCompSlot = saveSlot;
/* Emit the usual op needed for decompilation. */
if (js_Emit1(cx, cg, JSOP_ENDINIT) < 0)
return JS_FALSE;
break;
}
#endif /* JS_HAS_GENERATORS */
for (atomIndex = 0; pn2; atomIndex++, pn2 = pn2->pn_next) {
if (!EmitNumberOp(cx, atomIndex, cg))
return JS_FALSE;
/* FIXME 260106: holes in a sparse initializer are void-filled. */
if (pn2->pn_type == TOK_COMMA) {
if (js_Emit1(cx, cg, JSOP_PUSH) < 0)
return JS_FALSE;
} else {
if (!js_EmitTree(cx, cg, pn2))
return JS_FALSE;
}
if (js_Emit1(cx, cg, JSOP_INITELEM) < 0)
return JS_FALSE;
}
if (pn->pn_extra & PNX_ENDCOMMA) {
/* Emit a source note so we know to decompile an extra comma. */
if (js_NewSrcNote(cx, cg, SRC_CONTINUE) < 0)
return JS_FALSE;
}
/* Emit an op for sharp array cleanup and decompilation. */
if (js_Emit1(cx, cg, JSOP_ENDINIT) < 0)
return JS_FALSE;
break;
case TOK_RC:
/*
* Emit code for {p:a, '%q':b, 2:c} of the form:
* t = new Object; t.p = a; t['%q'] = b; t[2] = c; t;
* but use a stack slot for t and avoid dup'ing and popping it via
* the JSOP_NEWINIT and JSOP_INITELEM bytecodes.
*/
ale = js_IndexAtom(cx, CLASS_ATOM(cx, Object), &cg->atomList);
if (!ale)
return JS_FALSE;
EMIT_ATOM_INDEX_OP(JSOP_NAME, ALE_INDEX(ale));
if (js_Emit1(cx, cg, JSOP_PUSHOBJ) < 0)
return JS_FALSE;
if (js_Emit1(cx, cg, JSOP_NEWINIT) < 0)
return JS_FALSE;
pn2 = pn->pn_head;
#if JS_HAS_SHARP_VARS
if (pn2 && pn2->pn_type == TOK_DEFSHARP) {
EMIT_UINT16_IMM_OP(JSOP_DEFSHARP, (jsatomid)pn2->pn_num);
pn2 = pn2->pn_next;
}
#endif
for (; pn2; pn2 = pn2->pn_next) {
/* Emit an index for t[2], else map an atom for t.p or t['%q']. */
pn3 = pn2->pn_left;
switch (pn3->pn_type) {
case TOK_NUMBER:
if (!EmitNumberOp(cx, pn3->pn_dval, cg))
return JS_FALSE;
break;
case TOK_NAME:
case TOK_STRING:
ale = js_IndexAtom(cx, pn3->pn_atom, &cg->atomList);
if (!ale)
return JS_FALSE;
break;
default:
JS_ASSERT(0);
}
/* Emit code for the property initializer. */
if (!js_EmitTree(cx, cg, pn2->pn_right))
return JS_FALSE;
#if JS_HAS_GETTER_SETTER
op = pn2->pn_op;
if (op == JSOP_GETTER || op == JSOP_SETTER) {
if (pn3->pn_type != TOK_NUMBER &&
ALE_INDEX(ale) >= JS_BIT(16)) {
ReportStatementTooLarge(cx, cg);
return JS_FALSE;
}
if (js_Emit1(cx, cg, op) < 0)
return JS_FALSE;
}
#endif
/* Annotate JSOP_INITELEM so we decompile 2:c and not just c. */
if (pn3->pn_type == TOK_NUMBER) {
if (js_NewSrcNote(cx, cg, SRC_INITPROP) < 0)
return JS_FALSE;
if (js_Emit1(cx, cg, JSOP_INITELEM) < 0)
return JS_FALSE;
} else {
EMIT_ATOM_INDEX_OP(JSOP_INITPROP, ALE_INDEX(ale));
}
}
/* Emit an op for sharpArray cleanup and decompilation. */
if (js_Emit1(cx, cg, JSOP_ENDINIT) < 0)
return JS_FALSE;
break;
#if JS_HAS_SHARP_VARS
case TOK_DEFSHARP:
if (!js_EmitTree(cx, cg, pn->pn_kid))
return JS_FALSE;
EMIT_UINT16_IMM_OP(JSOP_DEFSHARP, (jsatomid) pn->pn_num);
break;
case TOK_USESHARP:
EMIT_UINT16_IMM_OP(JSOP_USESHARP, (jsatomid) pn->pn_num);
break;
#endif /* JS_HAS_SHARP_VARS */
case TOK_RP:
{
uintN oldflags;
/*
* The node for (e) has e as its kid, enabling users who want to nest
* assignment expressions in conditions to avoid the error correction
* done by Condition (from x = y to x == y) by double-parenthesizing.
*/
oldflags = cg->treeContext.flags;
cg->treeContext.flags &= ~TCF_IN_FOR_INIT;
if (!js_EmitTree(cx, cg, pn->pn_kid))
return JS_FALSE;
cg->treeContext.flags |= oldflags & TCF_IN_FOR_INIT;
if (js_Emit1(cx, cg, JSOP_GROUP) < 0)
return JS_FALSE;
break;
}
case TOK_NAME:
if (!BindNameToSlot(cx, &cg->treeContext, pn, JS_FALSE))
return JS_FALSE;
op = pn->pn_op;
if (op == JSOP_ARGUMENTS) {
if (js_Emit1(cx, cg, op) < 0)
return JS_FALSE;
break;
}
if (pn->pn_slot >= 0) {
atomIndex = (jsatomid) pn->pn_slot;
EMIT_UINT16_IMM_OP(op, atomIndex);
break;
}
/* FALL THROUGH */
#if JS_HAS_XML_SUPPORT
case TOK_XMLATTR:
case TOK_XMLSPACE:
case TOK_XMLTEXT:
case TOK_XMLCDATA:
case TOK_XMLCOMMENT:
#endif
case TOK_STRING:
case TOK_OBJECT:
/*
* The scanner and parser associate JSOP_NAME with TOK_NAME, although
* other bytecodes may result instead (JSOP_BINDNAME/JSOP_SETNAME,
* JSOP_FORNAME, etc.). Among JSOP_*NAME* variants, only JSOP_NAME
* may generate the first operand of a call or new expression, so only
* it sets the "obj" virtual machine register to the object along the
* scope chain in which the name was found.
*
* Token types for STRING and OBJECT have corresponding bytecode ops
* in pn_op and emit the same format as NAME, so they share this code.
*/
ok = EmitAtomOp(cx, pn, pn->pn_op, cg);
break;
case TOK_NUMBER:
ok = EmitNumberOp(cx, pn->pn_dval, cg);
break;
#if JS_HAS_XML_SUPPORT
case TOK_ANYNAME:
#endif
case TOK_PRIMARY:
if (js_Emit1(cx, cg, pn->pn_op) < 0)
return JS_FALSE;
break;
#if JS_HAS_DEBUGGER_KEYWORD
case TOK_DEBUGGER:
if (js_Emit1(cx, cg, JSOP_DEBUGGER) < 0)
return JS_FALSE;
break;
#endif /* JS_HAS_DEBUGGER_KEYWORD */
#if JS_HAS_XML_SUPPORT
case TOK_XMLELEM:
case TOK_XMLLIST:
if (pn->pn_op == JSOP_XMLOBJECT) {
ok = EmitAtomOp(cx, pn, pn->pn_op, cg);
break;
}
JS_ASSERT(pn->pn_type == TOK_XMLLIST || pn->pn_count != 0);
switch (pn->pn_head ? pn->pn_head->pn_type : TOK_XMLLIST) {
case TOK_XMLETAGO:
JS_ASSERT(0);
/* FALL THROUGH */
case TOK_XMLPTAGC:
case TOK_XMLSTAGO:
break;
default:
if (js_Emit1(cx, cg, JSOP_STARTXML) < 0)
return JS_FALSE;
}
for (pn2 = pn->pn_head; pn2; pn2 = pn2->pn_next) {
if (pn2->pn_type == TOK_LC &&
js_Emit1(cx, cg, JSOP_STARTXMLEXPR) < 0) {
return JS_FALSE;
}
if (!js_EmitTree(cx, cg, pn2))
return JS_FALSE;
if (pn2 != pn->pn_head && js_Emit1(cx, cg, JSOP_ADD) < 0)
return JS_FALSE;
}
if (pn->pn_extra & PNX_XMLROOT) {
if (pn->pn_count == 0) {
JS_ASSERT(pn->pn_type == TOK_XMLLIST);
atom = cx->runtime->atomState.emptyAtom;
ale = js_IndexAtom(cx, atom, &cg->atomList);
if (!ale)
return JS_FALSE;
EMIT_ATOM_INDEX_OP(JSOP_STRING, ALE_INDEX(ale));
}
if (js_Emit1(cx, cg, pn->pn_op) < 0)
return JS_FALSE;
}
#ifdef DEBUG
else
JS_ASSERT(pn->pn_count != 0);
#endif
break;
case TOK_XMLPTAGC:
if (pn->pn_op == JSOP_XMLOBJECT) {
ok = EmitAtomOp(cx, pn, pn->pn_op, cg);
break;
}
/* FALL THROUGH */
case TOK_XMLSTAGO:
case TOK_XMLETAGO:
{
uint32 i;
if (js_Emit1(cx, cg, JSOP_STARTXML) < 0)
return JS_FALSE;
ale = js_IndexAtom(cx,
(pn->pn_type == TOK_XMLETAGO)
? cx->runtime->atomState.etagoAtom
: cx->runtime->atomState.stagoAtom,
&cg->atomList);
if (!ale)
return JS_FALSE;
EMIT_ATOM_INDEX_OP(JSOP_STRING, ALE_INDEX(ale));
JS_ASSERT(pn->pn_count != 0);
pn2 = pn->pn_head;
if (pn2->pn_type == TOK_LC && js_Emit1(cx, cg, JSOP_STARTXMLEXPR) < 0)
return JS_FALSE;
if (!js_EmitTree(cx, cg, pn2))
return JS_FALSE;
if (js_Emit1(cx, cg, JSOP_ADD) < 0)
return JS_FALSE;
for (pn2 = pn2->pn_next, i = 0; pn2; pn2 = pn2->pn_next, i++) {
if (pn2->pn_type == TOK_LC &&
js_Emit1(cx, cg, JSOP_STARTXMLEXPR) < 0) {
return JS_FALSE;
}
if (!js_EmitTree(cx, cg, pn2))
return JS_FALSE;
if ((i & 1) && pn2->pn_type == TOK_LC) {
if (js_Emit1(cx, cg, JSOP_TOATTRVAL) < 0)
return JS_FALSE;
}
if (js_Emit1(cx, cg,
(i & 1) ? JSOP_ADDATTRVAL : JSOP_ADDATTRNAME) < 0) {
return JS_FALSE;
}
}
ale = js_IndexAtom(cx,
(pn->pn_type == TOK_XMLPTAGC)
? cx->runtime->atomState.ptagcAtom
: cx->runtime->atomState.tagcAtom,
&cg->atomList);
if (!ale)
return JS_FALSE;
EMIT_ATOM_INDEX_OP(JSOP_STRING, ALE_INDEX(ale));
if (js_Emit1(cx, cg, JSOP_ADD) < 0)
return JS_FALSE;
if ((pn->pn_extra & PNX_XMLROOT) && js_Emit1(cx, cg, pn->pn_op) < 0)
return JS_FALSE;
break;
}
case TOK_XMLNAME:
if (pn->pn_arity == PN_LIST) {
JS_ASSERT(pn->pn_count != 0);
for (pn2 = pn->pn_head; pn2; pn2 = pn2->pn_next) {
if (!js_EmitTree(cx, cg, pn2))
return JS_FALSE;
if (pn2 != pn->pn_head && js_Emit1(cx, cg, JSOP_ADD) < 0)
return JS_FALSE;
}
} else {
JS_ASSERT(pn->pn_arity == PN_NULLARY);
ok = EmitAtomOp(cx, pn, pn->pn_op, cg);
}
break;
case TOK_XMLPI:
ale = js_IndexAtom(cx, pn->pn_atom2, &cg->atomList);
if (!ale)
return JS_FALSE;
if (!EmitAtomIndexOp(cx, JSOP_QNAMEPART, ALE_INDEX(ale), cg))
return JS_FALSE;
if (!EmitAtomOp(cx, pn, JSOP_XMLPI, cg))
return JS_FALSE;
break;
#endif /* JS_HAS_XML_SUPPORT */
default:
JS_ASSERT(0);
}
if (ok && --cg->emitLevel == 0 && cg->spanDeps)
ok = OptimizeSpanDeps(cx, cg);
return ok;
}
/* XXX get rid of offsetBias, it's used only by SRC_FOR and SRC_DECL */
JS_FRIEND_DATA(JSSrcNoteSpec) js_SrcNoteSpec[] = {
{"null", 0, 0, 0},
{"if", 0, 0, 0},
{"if-else", 2, 0, 1},
{"while", 1, 0, 1},
{"for", 3, 1, 1},
{"continue", 0, 0, 0},
{"decl", 1, 1, 1},
{"pcdelta", 1, 0, 1},
{"assignop", 0, 0, 0},
{"cond", 1, 0, 1},
{"brace", 1, 0, 1},
{"hidden", 0, 0, 0},
{"pcbase", 1, 0, -1},
{"label", 1, 0, 0},
{"labelbrace", 1, 0, 0},
{"endbrace", 0, 0, 0},
{"break2label", 1, 0, 0},
{"cont2label", 1, 0, 0},
{"switch", 2, 0, 1},
{"funcdef", 1, 0, 0},
{"catch", 1, 0, 1},
{"extended", -1, 0, 0},
{"newline", 0, 0, 0},
{"setline", 1, 0, 0},
{"xdelta", 0, 0, 0},
};
static intN
AllocSrcNote(JSContext *cx, JSCodeGenerator *cg)
{
intN index;
JSArenaPool *pool;
size_t size;
index = CG_NOTE_COUNT(cg);
if (((uintN)index & CG_NOTE_MASK(cg)) == 0) {
pool = cg->notePool;
size = SRCNOTE_SIZE(CG_NOTE_MASK(cg) + 1);
if (!CG_NOTES(cg)) {
/* Allocate the first note array lazily; leave noteMask alone. */
JS_ARENA_ALLOCATE_CAST(CG_NOTES(cg), jssrcnote *, pool, size);
} else {
/* Grow by doubling note array size; update noteMask on success. */
JS_ARENA_GROW_CAST(CG_NOTES(cg), jssrcnote *, pool, size, size);
if (CG_NOTES(cg))
CG_NOTE_MASK(cg) = (CG_NOTE_MASK(cg) << 1) | 1;
}
if (!CG_NOTES(cg)) {
JS_ReportOutOfMemory(cx);
return -1;
}
}
CG_NOTE_COUNT(cg) = index + 1;
return index;
}
intN
js_NewSrcNote(JSContext *cx, JSCodeGenerator *cg, JSSrcNoteType type)
{
intN index, n;
jssrcnote *sn;
ptrdiff_t offset, delta, xdelta;
/*
* Claim a note slot in CG_NOTES(cg) by growing it if necessary and then
* incrementing CG_NOTE_COUNT(cg).
*/
index = AllocSrcNote(cx, cg);
if (index < 0)
return -1;
sn = &CG_NOTES(cg)[index];
/*
* Compute delta from the last annotated bytecode's offset. If it's too
* big to fit in sn, allocate one or more xdelta notes and reset sn.
*/
offset = CG_OFFSET(cg);
delta = offset - CG_LAST_NOTE_OFFSET(cg);
CG_LAST_NOTE_OFFSET(cg) = offset;
if (delta >= SN_DELTA_LIMIT) {
do {
xdelta = JS_MIN(delta, SN_XDELTA_MASK);
SN_MAKE_XDELTA(sn, xdelta);
delta -= xdelta;
index = AllocSrcNote(cx, cg);
if (index < 0)
return -1;
sn = &CG_NOTES(cg)[index];
} while (delta >= SN_DELTA_LIMIT);
}
/*
* Initialize type and delta, then allocate the minimum number of notes
* needed for type's arity. Usually, we won't need more, but if an offset
* does take two bytes, js_SetSrcNoteOffset will grow CG_NOTES(cg).
*/
SN_MAKE_NOTE(sn, type, delta);
for (n = (intN)js_SrcNoteSpec[type].arity; n > 0; n--) {
if (js_NewSrcNote(cx, cg, SRC_NULL) < 0)
return -1;
}
return index;
}
intN
js_NewSrcNote2(JSContext *cx, JSCodeGenerator *cg, JSSrcNoteType type,
ptrdiff_t offset)
{
intN index;
index = js_NewSrcNote(cx, cg, type);
if (index >= 0) {
if (!js_SetSrcNoteOffset(cx, cg, index, 0, offset))
return -1;
}
return index;
}
intN
js_NewSrcNote3(JSContext *cx, JSCodeGenerator *cg, JSSrcNoteType type,
ptrdiff_t offset1, ptrdiff_t offset2)
{
intN index;
index = js_NewSrcNote(cx, cg, type);
if (index >= 0) {
if (!js_SetSrcNoteOffset(cx, cg, index, 0, offset1))
return -1;
if (!js_SetSrcNoteOffset(cx, cg, index, 1, offset2))
return -1;
}
return index;
}
static JSBool
GrowSrcNotes(JSContext *cx, JSCodeGenerator *cg)
{
JSArenaPool *pool;
size_t size;
/* Grow by doubling note array size; update noteMask on success. */
pool = cg->notePool;
size = SRCNOTE_SIZE(CG_NOTE_MASK(cg) + 1);
JS_ARENA_GROW_CAST(CG_NOTES(cg), jssrcnote *, pool, size, size);
if (!CG_NOTES(cg)) {
JS_ReportOutOfMemory(cx);
return JS_FALSE;
}
CG_NOTE_MASK(cg) = (CG_NOTE_MASK(cg) << 1) | 1;
return JS_TRUE;
}
jssrcnote *
js_AddToSrcNoteDelta(JSContext *cx, JSCodeGenerator *cg, jssrcnote *sn,
ptrdiff_t delta)
{
ptrdiff_t base, limit, newdelta, diff;
intN index;
/*
* Called only from OptimizeSpanDeps and js_FinishTakingSrcNotes to add to
* main script note deltas, and only by a small positive amount.
*/
JS_ASSERT(cg->current == &cg->main);
JS_ASSERT((unsigned) delta < (unsigned) SN_XDELTA_LIMIT);
base = SN_DELTA(sn);
limit = SN_IS_XDELTA(sn) ? SN_XDELTA_LIMIT : SN_DELTA_LIMIT;
newdelta = base + delta;
if (newdelta < limit) {
SN_SET_DELTA(sn, newdelta);
} else {
index = sn - cg->main.notes;
if ((cg->main.noteCount & cg->main.noteMask) == 0) {
if (!GrowSrcNotes(cx, cg))
return NULL;
sn = cg->main.notes + index;
}
diff = cg->main.noteCount - index;
cg->main.noteCount++;
memmove(sn + 1, sn, SRCNOTE_SIZE(diff));
SN_MAKE_XDELTA(sn, delta);
sn++;
}
return sn;
}
JS_FRIEND_API(uintN)
js_SrcNoteLength(jssrcnote *sn)
{
uintN arity;
jssrcnote *base;
arity = (intN)js_SrcNoteSpec[SN_TYPE(sn)].arity;
for (base = sn++; arity; sn++, arity--) {
if (*sn & SN_3BYTE_OFFSET_FLAG)
sn += 2;
}
return sn - base;
}
JS_FRIEND_API(ptrdiff_t)
js_GetSrcNoteOffset(jssrcnote *sn, uintN which)
{
/* Find the offset numbered which (i.e., skip exactly which offsets). */
JS_ASSERT(SN_TYPE(sn) != SRC_XDELTA);
JS_ASSERT(which < js_SrcNoteSpec[SN_TYPE(sn)].arity);
for (sn++; which; sn++, which--) {
if (*sn & SN_3BYTE_OFFSET_FLAG)
sn += 2;
}
if (*sn & SN_3BYTE_OFFSET_FLAG) {
return (ptrdiff_t)(((uint32)(sn[0] & SN_3BYTE_OFFSET_MASK) << 16)
| (sn[1] << 8)
| sn[2]);
}
return (ptrdiff_t)*sn;
}
JSBool
js_SetSrcNoteOffset(JSContext *cx, JSCodeGenerator *cg, uintN index,
uintN which, ptrdiff_t offset)
{
jssrcnote *sn;
ptrdiff_t diff;
if ((jsuword)offset >= (jsuword)((ptrdiff_t)SN_3BYTE_OFFSET_FLAG << 16)) {
ReportStatementTooLarge(cx, cg);
return JS_FALSE;
}
/* Find the offset numbered which (i.e., skip exactly which offsets). */
sn = &CG_NOTES(cg)[index];
JS_ASSERT(SN_TYPE(sn) != SRC_XDELTA);
JS_ASSERT(which < js_SrcNoteSpec[SN_TYPE(sn)].arity);
for (sn++; which; sn++, which--) {
if (*sn & SN_3BYTE_OFFSET_FLAG)
sn += 2;
}
/* See if the new offset requires three bytes. */
if (offset > (ptrdiff_t)SN_3BYTE_OFFSET_MASK) {
/* Maybe this offset was already set to a three-byte value. */
if (!(*sn & SN_3BYTE_OFFSET_FLAG)) {
/* Losing, need to insert another two bytes for this offset. */
index = PTRDIFF(sn, CG_NOTES(cg), jssrcnote);
/*
* Simultaneously test to see if the source note array must grow to
* accomodate either the first or second byte of additional storage
* required by this 3-byte offset.
*/
if (((CG_NOTE_COUNT(cg) + 1) & CG_NOTE_MASK(cg)) <= 1) {
if (!GrowSrcNotes(cx, cg))
return JS_FALSE;
sn = CG_NOTES(cg) + index;
}
CG_NOTE_COUNT(cg) += 2;
diff = CG_NOTE_COUNT(cg) - (index + 3);
JS_ASSERT(diff >= 0);
if (diff > 0)
memmove(sn + 3, sn + 1, SRCNOTE_SIZE(diff));
}
*sn++ = (jssrcnote)(SN_3BYTE_OFFSET_FLAG | (offset >> 16));
*sn++ = (jssrcnote)(offset >> 8);
}
*sn = (jssrcnote)offset;
return JS_TRUE;
}
#ifdef DEBUG_notme
#define DEBUG_srcnotesize
#endif
#ifdef DEBUG_srcnotesize
#define NBINS 10
static uint32 hist[NBINS];
void DumpSrcNoteSizeHist()
{
static FILE *fp;
int i, n;
if (!fp) {
fp = fopen("/tmp/srcnotes.hist", "w");
if (!fp)
return;
setvbuf(fp, NULL, _IONBF, 0);
}
fprintf(fp, "SrcNote size histogram:\n");
for (i = 0; i < NBINS; i++) {
fprintf(fp, "%4u %4u ", JS_BIT(i), hist[i]);
for (n = (int) JS_HOWMANY(hist[i], 10); n > 0; --n)
fputc('*', fp);
fputc('\n', fp);
}
fputc('\n', fp);
}
#endif
/*
* Fill in the storage at notes with prolog and main srcnotes; the space at
* notes was allocated using the CG_COUNT_FINAL_SRCNOTES macro from jsemit.h.
* SO DON'T CHANGE THIS FUNCTION WITHOUT AT LEAST CHECKING WHETHER jsemit.h's
* CG_COUNT_FINAL_SRCNOTES MACRO NEEDS CORRESPONDING CHANGES!
*/
JSBool
js_FinishTakingSrcNotes(JSContext *cx, JSCodeGenerator *cg, jssrcnote *notes)
{
uintN prologCount, mainCount, totalCount;
ptrdiff_t offset, delta;
jssrcnote *sn;
JS_ASSERT(cg->current == &cg->main);
prologCount = cg->prolog.noteCount;
if (prologCount && cg->prolog.currentLine != cg->firstLine) {
CG_SWITCH_TO_PROLOG(cg);
if (js_NewSrcNote2(cx, cg, SRC_SETLINE, (ptrdiff_t)cg->firstLine) < 0)
return JS_FALSE;
prologCount = cg->prolog.noteCount;
CG_SWITCH_TO_MAIN(cg);
} else {
/*
* Either no prolog srcnotes, or no line number change over prolog.
* We don't need a SRC_SETLINE, but we may need to adjust the offset
* of the first main note, by adding to its delta and possibly even
* prepending SRC_XDELTA notes to it to account for prolog bytecodes
* that came at and after the last annotated bytecode.
*/
offset = CG_PROLOG_OFFSET(cg) - cg->prolog.lastNoteOffset;
JS_ASSERT(offset >= 0);
if (offset > 0 && cg->main.noteCount != 0) {
/* NB: Use as much of the first main note's delta as we can. */
sn = cg->main.notes;
delta = SN_IS_XDELTA(sn)
? SN_XDELTA_MASK - (*sn & SN_XDELTA_MASK)
: SN_DELTA_MASK - (*sn & SN_DELTA_MASK);
if (offset < delta)
delta = offset;
for (;;) {
if (!js_AddToSrcNoteDelta(cx, cg, sn, delta))
return JS_FALSE;
offset -= delta;
if (offset == 0)
break;
delta = JS_MIN(offset, SN_XDELTA_MASK);
sn = cg->main.notes;
}
}
}
mainCount = cg->main.noteCount;
totalCount = prologCount + mainCount;
if (prologCount)
memcpy(notes, cg->prolog.notes, SRCNOTE_SIZE(prologCount));
memcpy(notes + prologCount, cg->main.notes, SRCNOTE_SIZE(mainCount));
SN_MAKE_TERMINATOR(¬es[totalCount]);
#ifdef DEBUG_notme
{ int bin = JS_CeilingLog2(totalCount);
if (bin >= NBINS)
bin = NBINS - 1;
++hist[bin];
}
#endif
return JS_TRUE;
}
JSBool
js_AllocTryNotes(JSContext *cx, JSCodeGenerator *cg)
{
size_t size, incr;
ptrdiff_t delta;
size = TRYNOTE_SIZE(cg->treeContext.tryCount);
if (size <= cg->tryNoteSpace)
return JS_TRUE;
/*
* Allocate trynotes from cx->tempPool.
* XXX Too much growing and we bloat, as other tempPool allocators block
* in-place growth, and we never recycle old free space in an arena.
* YYY But once we consume an entire arena, we'll realloc it, letting the
* malloc heap recycle old space, while still freeing _en masse_ via the
* arena pool.
*/
if (!cg->tryBase) {
size = JS_ROUNDUP(size, TRYNOTE_SIZE(TRYNOTE_CHUNK));
JS_ARENA_ALLOCATE_CAST(cg->tryBase, JSTryNote *, &cx->tempPool, size);
if (!cg->tryBase)
return JS_FALSE;
cg->tryNoteSpace = size;
cg->tryNext = cg->tryBase;
} else {
delta = PTRDIFF((char *)cg->tryNext, (char *)cg->tryBase, char);
incr = size - cg->tryNoteSpace;
incr = JS_ROUNDUP(incr, TRYNOTE_SIZE(TRYNOTE_CHUNK));
size = cg->tryNoteSpace;
JS_ARENA_GROW_CAST(cg->tryBase, JSTryNote *, &cx->tempPool, size, incr);
if (!cg->tryBase)
return JS_FALSE;
cg->tryNoteSpace = size + incr;
cg->tryNext = (JSTryNote *)((char *)cg->tryBase + delta);
}
return JS_TRUE;
}
JSTryNote *
js_NewTryNote(JSContext *cx, JSCodeGenerator *cg, ptrdiff_t start,
ptrdiff_t end, ptrdiff_t catchStart)
{
JSTryNote *tn;
JS_ASSERT(cg->tryBase <= cg->tryNext);
JS_ASSERT(catchStart >= 0);
tn = cg->tryNext++;
tn->start = start;
tn->length = end - start;
tn->catchStart = catchStart;
return tn;
}
void
js_FinishTakingTryNotes(JSContext *cx, JSCodeGenerator *cg, JSTryNote *notes)
{
uintN count;
count = PTRDIFF(cg->tryNext, cg->tryBase, JSTryNote);
if (!count)
return;
memcpy(notes, cg->tryBase, TRYNOTE_SIZE(count));
notes[count].start = 0;
notes[count].length = CG_OFFSET(cg);
notes[count].catchStart = 0;
}
|