1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042 8043 8044 8045 8046 8047 8048 8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059 8060 8061 8062 8063 8064 8065 8066 8067 8068 8069 8070 8071 8072 8073 8074 8075 8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130 8131 8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153 8154 8155 8156 8157 8158 8159 8160 8161 8162 8163 8164 8165 8166 8167 8168 8169 8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180 8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200 8201 8202 8203 8204 8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217 8218 8219 8220 8221 8222 8223 8224 8225 8226 8227 8228 8229 8230 8231 8232 8233 8234 8235 8236 8237 8238 8239 8240 8241 8242 8243 8244 8245 8246 8247 8248 8249 8250 8251 8252 8253 8254 8255 8256 8257 8258 8259 8260 8261 8262 8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293 8294 8295 8296 8297 8298 8299 8300 8301 8302 8303 8304 8305 8306 8307 8308 8309 8310 8311 8312 8313 8314 8315 8316 8317 8318 8319 8320 8321 8322 8323 8324 8325 8326 8327 8328 8329 8330 8331 8332 8333 8334 8335 8336 8337 8338 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351 8352 8353 8354 8355 8356 8357 8358 8359 8360 8361 8362 8363 8364 8365 8366 8367 8368 8369 8370 8371 8372 8373 8374 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8390 8391 8392 8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431 8432 8433 8434 8435 8436 8437 8438 8439 8440 8441 8442 8443 8444 8445 8446 8447 8448 8449 8450 8451 8452 8453 8454 8455 8456 8457 8458 8459 8460 8461 8462 8463 8464 8465 8466 8467 8468 8469 8470 8471 8472 8473 8474 8475 8476 8477 8478 8479 8480 8481 8482 8483 8484 8485 8486 8487 8488 8489 8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503 8504 8505 8506 8507 8508 8509 8510 8511 8512 8513 8514 8515 8516 8517 8518 8519 8520 8521 8522 8523 8524 8525 8526 8527 8528 8529 8530 8531 8532 8533 8534 8535 8536 8537 8538 8539 8540 8541 8542 8543 8544 8545 8546 8547 8548 8549 8550 8551 8552 8553 8554 8555 8556 8557 8558 8559 8560 8561 8562 8563 8564 8565 8566 8567 8568 8569 8570 8571 8572 8573 8574 8575 8576 8577 8578 8579 8580 8581 8582 8583 8584 8585 8586 8587 8588 8589 8590 8591 8592 8593 8594 8595 8596 8597 8598 8599 8600 8601 8602 8603 8604 8605 8606 8607 8608 8609 8610 8611 8612 8613 8614 8615 8616 8617 8618 8619 8620 8621 8622 8623 8624 8625 8626 8627 8628 8629 8630 8631 8632 8633 8634 8635 8636 8637 8638 8639 8640 8641 8642 8643 8644 8645 8646 8647 8648 8649 8650 8651 8652 8653 8654 8655 8656 8657 8658 8659 8660 8661 8662 8663 8664 8665 8666 8667 8668 8669 8670 8671 8672 8673 8674 8675 8676 8677 8678 8679 8680 8681 8682 8683 8684 8685 8686 8687 8688 8689 8690 8691 8692 8693 8694 8695 8696 8697 8698 8699 8700 8701 8702 8703 8704 8705 8706 8707 8708 8709 8710 8711 8712 8713 8714 8715 8716 8717 8718 8719 8720 8721 8722 8723 8724 8725 8726 8727 8728 8729 8730 8731 8732 8733 8734 8735 8736 8737 8738 8739 8740 8741 8742 8743 8744 8745 8746 8747 8748 8749 8750 8751 8752 8753 8754 8755 8756 8757 8758 8759 8760 8761 8762 8763 8764 8765 8766 8767 8768 8769 8770 8771 8772 8773 8774 8775 8776 8777 8778 8779 8780 8781 8782 8783 8784 8785 8786 8787 8788 8789 8790 8791 8792 8793 8794 8795 8796 8797 8798 8799 8800 8801 8802 8803 8804 8805 8806 8807 8808 8809 8810 8811 8812 8813 8814 8815 8816 8817 8818 8819 8820 8821 8822 8823 8824 8825 8826 8827 8828 8829 8830 8831 8832 8833 8834 8835 8836 8837 8838 8839 8840 8841 8842 8843 8844 8845 8846 8847 8848 8849 8850 8851 8852 8853 8854 8855 8856 8857 8858 8859 8860 8861 8862 8863 8864 8865 8866 8867 8868 8869 8870 8871 8872 8873 8874 8875 8876 8877 8878 8879 8880 8881 8882 8883 8884 8885 8886 8887 8888 8889 8890 8891 8892 8893 8894 8895 8896 8897 8898 8899 8900 8901 8902 8903 8904 8905 8906 8907 8908 8909 8910 8911 8912 8913 8914 8915 8916 8917 8918 8919 8920 8921 8922 8923 8924 8925 8926 8927 8928 8929 8930 8931 8932 8933 8934 8935 8936 8937 8938 8939 8940 8941 8942 8943 8944 8945 8946 8947 8948 8949 8950 8951 8952 8953 8954 8955 8956 8957 8958 8959 8960 8961 8962 8963 8964 8965 8966 8967 8968 8969 8970 8971 8972 8973 8974 8975 8976 8977 8978 8979 8980 8981 8982 8983 8984 8985 8986 8987 8988 8989 8990 8991 8992 8993 8994 8995 8996 8997 8998 8999 9000 9001 9002 9003 9004 9005 9006 9007 9008 9009 9010 9011 9012 9013 9014 9015 9016 9017 9018 9019 9020 9021 9022 9023 9024 9025 9026 9027 9028 9029 9030 9031 9032 9033 9034 9035 9036 9037 9038 9039 9040 9041 9042 9043 9044 9045 9046 9047 9048 9049 9050 9051 9052 9053 9054 9055 9056 9057 9058 9059 9060 9061 9062 9063 9064 9065 9066 9067 9068 9069 9070 9071 9072 9073 9074 9075 9076 9077 9078 9079 9080 9081 9082 9083 9084 9085 9086 9087 9088 9089 9090 9091 9092 9093 9094 9095 9096 9097 9098 9099 9100 9101 9102 9103 9104 9105 9106 9107 9108 9109 9110 9111 9112 9113 9114 9115 9116 9117 9118 9119 9120 9121 9122 9123 9124 9125 9126 9127 9128 9129 9130 9131 9132 9133 9134 9135 9136 9137 9138 9139 9140 9141 9142 9143 9144 9145 9146 9147 9148 9149 9150 9151 9152 9153 9154 9155 9156 9157 9158 9159 9160 9161 9162 9163 9164 9165 9166 9167 9168 9169 9170 9171 9172 9173 9174 9175 9176 9177 9178 9179 9180 9181 9182 9183 9184 9185 9186 9187 9188 9189 9190 9191 9192 9193 9194 9195 9196 9197 9198 9199 9200 9201 9202 9203 9204 9205 9206 9207 9208 9209 9210 9211 9212 9213 9214 9215 9216 9217 9218 9219 9220 9221 9222 9223 9224 9225 9226 9227 9228 9229 9230 9231 9232 9233 9234 9235 9236 9237 9238 9239 9240 9241 9242 9243 9244 9245 9246 9247 9248 9249 9250 9251 9252 9253 9254 9255 9256 9257 9258 9259 9260 9261 9262 9263 9264 9265 9266 9267 9268 9269 9270 9271 9272 9273 9274 9275 9276 9277 9278 9279 9280 9281 9282 9283 9284 9285 9286 9287 9288 9289 9290 9291 9292 9293 9294 9295 9296 9297 9298 9299 9300 9301 9302 9303 9304 9305 9306 9307 9308 9309 9310 9311 9312 9313 9314 9315 9316 9317 9318 9319 9320 9321 9322 9323 9324 9325 9326 9327 9328 9329 9330 9331 9332 9333 9334 9335 9336 9337 9338 9339 9340 9341 9342 9343 9344 9345 9346 9347 9348 9349 9350 9351 9352 9353 9354 9355 9356 9357 9358 9359 9360 9361 9362 9363 9364 9365 9366 9367 9368 9369 9370 9371 9372 9373 9374 9375 9376 9377 9378 9379 9380 9381 9382 9383 9384 9385 9386 9387 9388 9389 9390 9391 9392 9393 9394 9395 9396 9397 9398 9399 9400 9401 9402 9403 9404 9405 9406 9407 9408 9409 9410 9411 9412 9413 9414 9415 9416 9417 9418 9419 9420 9421 9422 9423 9424 9425 9426 9427 9428 9429 9430 9431 9432 9433 9434 9435 9436 9437 9438 9439 9440 9441 9442 9443 9444 9445 9446 9447 9448 9449 9450 9451 9452 9453 9454 9455 9456 9457 9458 9459 9460 9461 9462 9463 9464 9465 9466 9467 9468 9469 9470 9471 9472 9473 9474 9475 9476 9477 9478 9479 9480 9481 9482 9483 9484 9485 9486 9487 9488 9489 9490 9491 9492 9493 9494 9495 9496 9497 9498 9499 9500 9501 9502 9503 9504 9505 9506 9507 9508 9509 9510 9511 9512 9513 9514 9515 9516 9517 9518 9519 9520 9521 9522 9523 9524 9525 9526 9527 9528 9529 9530 9531 9532 9533 9534 9535 9536 9537 9538 9539 9540 9541 9542 9543 9544 9545 9546 9547 9548 9549 9550 9551 9552 9553 9554 9555 9556 9557 9558 9559 9560 9561 9562 9563 9564 9565 9566 9567 9568 9569 9570 9571 9572 9573 9574 9575 9576 9577 9578 9579 9580 9581 9582 9583 9584 9585 9586 9587 9588 9589 9590 9591 9592 9593 9594 9595 9596 9597 9598 9599 9600 9601 9602 9603 9604 9605 9606 9607 9608 9609 9610 9611 9612 9613 9614 9615 9616 9617 9618 9619 9620 9621 9622 9623 9624 9625 9626 9627 9628 9629 9630 9631 9632 9633 9634 9635 9636 9637 9638 9639 9640 9641 9642 9643 9644 9645 9646 9647 9648 9649 9650 9651 9652 9653 9654 9655 9656 9657 9658 9659 9660 9661 9662 9663 9664 9665 9666 9667 9668 9669 9670 9671 9672 9673 9674 9675 9676 9677 9678 9679 9680 9681 9682 9683 9684 9685 9686 9687 9688 9689 9690 9691 9692 9693 9694 9695 9696 9697 9698 9699 9700 9701 9702 9703 9704 9705 9706 9707 9708 9709 9710 9711 9712 9713 9714 9715 9716 9717 9718 9719 9720 9721 9722 9723 9724 9725 9726 9727 9728 9729 9730 9731 9732 9733 9734 9735 9736 9737 9738 9739 9740 9741 9742 9743 9744 9745 9746 9747 9748 9749 9750 9751 9752 9753 9754 9755 9756 9757 9758 9759 9760 9761 9762 9763 9764 9765 9766 9767 9768 9769 9770 9771 9772 9773 9774 9775 9776 9777 9778 9779 9780 9781 9782 9783 9784 9785 9786 9787 9788 9789 9790 9791 9792 9793 9794 9795 9796 9797 9798 9799 9800 9801 9802 9803 9804 9805 9806 9807 9808 9809 9810 9811 9812 9813 9814 9815 9816 9817 9818 9819 9820 9821 9822 9823 9824 9825 9826 9827 9828 9829 9830 9831 9832 9833 9834 9835 9836 9837 9838 9839 9840 9841 9842 9843 9844 9845 9846 9847 9848 9849 9850 9851 9852 9853 9854 9855 9856 9857 9858 9859 9860 9861 9862 9863 9864 9865 9866 9867 9868 9869 9870 9871 9872 9873 9874 9875 9876 9877 9878 9879 9880 9881 9882 9883 9884 9885 9886 9887 9888 9889 9890 9891 9892 9893 9894 9895 9896 9897 9898 9899 9900 9901 9902 9903 9904 9905 9906 9907 9908 9909 9910 9911 9912 9913 9914 9915 9916 9917 9918 9919 9920 9921 9922 9923 9924 9925 9926 9927 9928 9929 9930 9931 9932 9933 9934 9935 9936 9937 9938 9939 9940 9941 9942 9943 9944 9945 9946 9947 9948 9949 9950 9951 9952 9953 9954 9955 9956 9957 9958 9959 9960 9961 9962 9963 9964 9965 9966 9967 9968 9969 9970 9971 9972 9973 9974 9975 9976 9977 9978 9979 9980 9981 9982 9983 9984 9985 9986 9987 9988 9989 9990 9991 9992 9993 9994 9995 9996 9997 9998 9999 10000 10001 10002 10003 10004 10005 10006 10007 10008 10009 10010 10011 10012 10013 10014 10015 10016 10017 10018 10019 10020 10021 10022 10023 10024 10025 10026 10027 10028 10029 10030 10031 10032 10033 10034 10035 10036 10037 10038 10039 10040 10041 10042 10043 10044 10045 10046 10047 10048 10049 10050 10051 10052 10053 10054 10055 10056 10057 10058 10059 10060 10061 10062 10063 10064 10065 10066 10067 10068 10069 10070 10071 10072 10073 10074 10075 10076 10077 10078 10079 10080 10081 10082 10083 10084 10085 10086 10087 10088 10089 10090 10091 10092 10093 10094 10095 10096 10097 10098 10099 10100 10101 10102 10103 10104 10105 10106 10107 10108 10109 10110 10111 10112 10113 10114 10115 10116 10117 10118 10119 10120 10121 10122 10123 10124 10125 10126 10127 10128 10129 10130 10131 10132 10133 10134 10135 10136 10137 10138 10139 10140 10141 10142 10143 10144 10145 10146 10147 10148 10149 10150 10151 10152 10153 10154 10155 10156 10157 10158 10159 10160 10161 10162 10163 10164 10165 10166 10167 10168 10169 10170 10171 10172 10173 10174 10175 10176 10177 10178 10179 10180 10181 10182 10183 10184 10185 10186 10187 10188 10189 10190 10191 10192 10193 10194 10195 10196 10197 10198 10199 10200 10201 10202 10203 10204 10205 10206 10207 10208 10209 10210 10211 10212 10213 10214 10215 10216 10217 10218 10219 10220 10221 10222 10223 10224 10225 10226 10227 10228 10229 10230 10231 10232 10233 10234 10235 10236 10237 10238 10239 10240 10241 10242 10243 10244 10245 10246 10247 10248 10249 10250 10251 10252 10253 10254 10255 10256 10257 10258 10259 10260 10261 10262 10263 10264 10265 10266 10267 10268 10269 10270 10271 10272 10273 10274 10275 10276 10277 10278 10279 10280 10281 10282 10283 10284 10285 10286 10287 10288 10289 10290 10291 10292 10293 10294 10295 10296 10297 10298 10299 10300 10301 10302 10303 10304 10305 10306 10307 10308 10309 10310 10311 10312 10313 10314 10315 10316 10317 10318 10319 10320 10321 10322 10323 10324 10325 10326 10327 10328 10329 10330 10331 10332 10333 10334 10335 10336 10337 10338 10339 10340 10341 10342 10343 10344 10345 10346 10347 10348 10349 10350 10351 10352 10353 10354 10355 10356 10357 10358 10359 10360 10361 10362 10363 10364 10365 10366 10367 10368 10369 10370 10371 10372 10373 10374 10375 10376 10377 10378 10379 10380 10381 10382 10383 10384 10385 10386 10387 10388 10389 10390 10391 10392 10393 10394 10395 10396 10397 10398 10399 10400 10401 10402 10403 10404 10405 10406 10407 10408 10409 10410 10411 10412 10413 10414 10415 10416 10417 10418 10419 10420 10421 10422 10423 10424 10425 10426 10427 10428 10429 10430 10431 10432 10433 10434 10435 10436 10437 10438 10439 10440 10441 10442 10443 10444 10445 10446 10447 10448 10449 10450 10451 10452 10453 10454 10455 10456 10457 10458 10459 10460 10461 10462 10463 10464 10465 10466 10467 10468 10469 10470 10471 10472 10473 10474 10475 10476 10477 10478 10479 10480 10481 10482 10483 10484 10485 10486 10487 10488 10489 10490 10491 10492 10493 10494 10495 10496 10497 10498 10499 10500 10501 10502 10503 10504 10505 10506 10507 10508 10509 10510 10511 10512 10513 10514 10515 10516 10517 10518 10519 10520 10521 10522 10523 10524 10525 10526 10527 10528 10529 10530 10531 10532 10533 10534 10535 10536 10537 10538 10539 10540 10541 10542 10543 10544 10545 10546 10547 10548 10549 10550 10551 10552 10553 10554 10555 10556 10557 10558 10559 10560 10561 10562 10563 10564 10565 10566 10567 10568 10569 10570 10571 10572 10573 10574 10575 10576 10577 10578 10579 10580 10581 10582 10583 10584 10585 10586 10587 10588 10589 10590 10591 10592 10593 10594 10595 10596 10597 10598 10599 10600 10601 10602 10603 10604 10605 10606 10607 10608 10609 10610 10611 10612 10613 10614 10615 10616 10617 10618 10619 10620 10621 10622 10623 10624 10625 10626 10627 10628 10629 10630 10631 10632 10633 10634 10635 10636 10637 10638 10639 10640 10641 10642 10643 10644 10645 10646 10647 10648 10649 10650 10651 10652 10653 10654 10655 10656 10657 10658 10659 10660 10661 10662 10663 10664 10665 10666 10667 10668 10669 10670 10671 10672 10673 10674 10675 10676 10677 10678 10679 10680 10681 10682 10683 10684 10685 10686 10687 10688 10689 10690 10691 10692 10693 10694 10695 10696 10697 10698 10699 10700 10701 10702 10703 10704 10705 10706 10707 10708 10709 10710 10711 10712 10713 10714 10715 10716 10717 10718 10719 10720 10721 10722 10723 10724 10725 10726 10727 10728 10729 10730 10731 10732 10733 10734 10735 10736 10737 10738 10739 10740 10741 10742 10743 10744 10745 10746 10747 10748 10749 10750 10751 10752 10753 10754 10755 10756 10757 10758 10759 10760 10761 10762 10763 10764 10765 10766 10767 10768 10769 10770 10771 10772 10773 10774 10775 10776 10777 10778 10779 10780 10781 10782 10783 10784 10785 10786 10787 10788 10789 10790 10791 10792 10793 10794 10795 10796 10797 10798 10799 10800 10801 10802 10803 10804 10805 10806 10807 10808 10809 10810 10811 10812 10813 10814 10815 10816 10817 10818 10819 10820 10821 10822 10823 10824 10825 10826 10827 10828 10829 10830 10831 10832 10833 10834 10835 10836 10837 10838 10839 10840 10841 10842 10843 10844 10845 10846 10847 10848 10849 10850 10851 10852 10853 10854 10855 10856 10857 10858 10859 10860 10861 10862 10863 10864 10865 10866 10867 10868 10869 10870 10871 10872 10873 10874 10875 10876 10877 10878 10879 10880 10881 10882 10883 10884 10885 10886 10887 10888 10889 10890 10891 10892 10893 10894 10895 10896 10897 10898 10899 10900 10901 10902 10903 10904 10905 10906 10907 10908 10909 10910 10911 10912 10913 10914 10915 10916 10917 10918 10919 10920 10921 10922 10923 10924 10925 10926 10927 10928 10929 10930 10931 10932 10933 10934 10935 10936 10937 10938 10939 10940 10941 10942 10943 10944 10945 10946 10947 10948 10949 10950 10951 10952 10953 10954 10955 10956 10957 10958 10959 10960 10961 10962 10963 10964 10965 10966 10967 10968 10969 10970 10971 10972 10973 10974 10975 10976 10977 10978 10979 10980 10981 10982 10983 10984 10985 10986 10987 10988 10989 10990 10991
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* vim: set ts=4 sw=4 et tw=99:
*
* ***** 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 SpiderMonkey JavaScript 1.9 code, released
* May 28, 2008.
*
* The Initial Developer of the Original Code is
* Brendan Eich <brendan@mozilla.org>
*
* Contributor(s):
* Andreas Gal <gal@mozilla.com>
* Mike Shaver <shaver@mozilla.org>
* David Anderson <danderson@mozilla.com>
*
* 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 ***** */
#include "jsstddef.h" // always first
#include "jsbit.h" // low-level (NSPR-based) headers next
#include "jsprf.h"
#include <math.h> // standard headers next
#ifdef _MSC_VER
#include <malloc.h>
#define alloca _alloca
#endif
#ifdef SOLARIS
#include <alloca.h>
#endif
#include <limits.h>
#include "nanojit/nanojit.h"
#include "jsapi.h" // higher-level library and API headers
#include "jsarray.h"
#include "jsbool.h"
#include "jscntxt.h"
#include "jsdbgapi.h"
#include "jsemit.h"
#include "jsfun.h"
#include "jsinterp.h"
#include "jsiter.h"
#include "jsobj.h"
#include "jsopcode.h"
#include "jsregexp.h"
#include "jsscope.h"
#include "jsscript.h"
#include "jsdate.h"
#include "jsstaticcheck.h"
#include "jstracer.h"
#include "jsxml.h"
#include "jsautooplen.h" // generated headers last
#include "imacros.c.out"
#if JS_HAS_XML_SUPPORT
#define ABORT_IF_XML(v) \
JS_BEGIN_MACRO \
if (!JSVAL_IS_PRIMITIVE(v) && OBJECT_IS_XML(BOGUS_CX, JSVAL_TO_OBJECT(v)))\
ABORT_TRACE("xml detected"); \
JS_END_MACRO
#else
#define ABORT_IF_XML(cx, v) ((void) 0)
#endif
/* Never use JSVAL_IS_BOOLEAN because it restricts the value (true, false) and
the type. What you want to use is JSVAL_TAG(x) == JSVAL_BOOLEAN and then
handle the undefined case properly (bug 457363). */
#undef JSVAL_IS_BOOLEAN
#define JSVAL_IS_BOOLEAN(x) JS_STATIC_ASSERT(0)
/* Use a fake tag to represent boxed values, borrowing from the integer tag
range since we only use JSVAL_INT to indicate integers. */
#define JSVAL_BOXED 3
/* Another fake jsval tag, used to distinguish null from object values. */
#define JSVAL_TNULL 5
/* A last fake jsval tag distinguishing functions from non-function objects. */
#define JSVAL_TFUN 7
/* Map to translate a type tag into a printable representation. */
static const char typeChar[] = "OIDXSNBF";
static const char tagChar[] = "OIDISIBI";
/* Blacklist parameters. */
/* Number of iterations of a loop where we start tracing. That is, we don't
start tracing until the beginning of the HOTLOOP-th iteration. */
#define HOTLOOP 2
/* Attempt recording this many times before blacklisting permanently. */
#define BL_ATTEMPTS 2
/* Skip this many future hits before allowing recording again after blacklisting. */
#define BL_BACKOFF 32
/* Number of times we wait to exit on a side exit before we try to extend the tree. */
#define HOTEXIT 1
/* Number of times we try to extend the tree along a side exit. */
#define MAXEXIT 3
/* Maximum number of peer trees allowed. */
#define MAXPEERS 9
/* Max call depths for inlining. */
#define MAX_CALLDEPTH 10
/* Max native stack size. */
#define MAX_NATIVE_STACK_SLOTS 1024
/* Max call stack size. */
#define MAX_CALL_STACK_ENTRIES 64
/* Max global object size. */
#define MAX_GLOBAL_SLOTS 4096
/* Max memory needed to rebuild the interpreter stack when falling off trace. */
#define MAX_INTERP_STACK_BYTES \
(MAX_NATIVE_STACK_SLOTS * sizeof(jsval) + \
MAX_CALL_STACK_ENTRIES * sizeof(JSInlineFrame) + \
sizeof(JSInlineFrame)) /* possibly slow native frame at top of stack */
/* Max number of branches per tree. */
#define MAX_BRANCHES 32
#define CHECK_STATUS(expr) \
JS_BEGIN_MACRO \
JSRecordingStatus _status = (expr); \
if (_status != JSRS_CONTINUE) \
return _status; \
JS_END_MACRO
#ifdef JS_JIT_SPEW
#define debug_only_a(x) if (js_verboseAbort || js_verboseDebug ) { x; }
#define ABORT_TRACE_RV(msg, value) \
JS_BEGIN_MACRO \
debug_only_a(fprintf(stdout, "abort: %d: %s\n", __LINE__, (msg));) \
return (value); \
JS_END_MACRO
#else
#define debug_only_a(x)
#define ABORT_TRACE_RV(msg, value) return (value)
#endif
#define ABORT_TRACE(msg) ABORT_TRACE_RV(msg, JSRS_STOP)
#define ABORT_TRACE_ERROR(msg) ABORT_TRACE_RV(msg, JSRS_ERROR)
#ifdef JS_JIT_SPEW
struct __jitstats {
#define JITSTAT(x) uint64 x;
#include "jitstats.tbl"
#undef JITSTAT
} jitstats = { 0LL, };
JS_STATIC_ASSERT(sizeof(jitstats) % sizeof(uint64) == 0);
enum jitstat_ids {
#define JITSTAT(x) STAT ## x ## ID,
#include "jitstats.tbl"
#undef JITSTAT
STAT_IDS_TOTAL
};
static JSPropertySpec jitstats_props[] = {
#define JITSTAT(x) { #x, STAT ## x ## ID, JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT },
#include "jitstats.tbl"
#undef JITSTAT
{ 0 }
};
static JSBool
jitstats_getProperty(JSContext *cx, JSObject *obj, jsid id, jsval *vp)
{
int index = -1;
if (JSVAL_IS_STRING(id)) {
JSString* str = JSVAL_TO_STRING(id);
if (strcmp(JS_GetStringBytes(str), "HOTLOOP") == 0) {
*vp = INT_TO_JSVAL(HOTLOOP);
return JS_TRUE;
}
}
if (JSVAL_IS_INT(id))
index = JSVAL_TO_INT(id);
uint64 result = 0;
switch (index) {
#define JITSTAT(x) case STAT ## x ## ID: result = jitstats.x; break;
#include "jitstats.tbl"
#undef JITSTAT
default:
*vp = JSVAL_VOID;
return JS_TRUE;
}
if (result < JSVAL_INT_MAX) {
*vp = INT_TO_JSVAL(result);
return JS_TRUE;
}
char retstr[64];
JS_snprintf(retstr, sizeof retstr, "%llu", result);
*vp = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, retstr));
return JS_TRUE;
}
JSClass jitstats_class = {
"jitstats",
JSCLASS_HAS_PRIVATE,
JS_PropertyStub, JS_PropertyStub,
jitstats_getProperty, JS_PropertyStub,
JS_EnumerateStub, JS_ResolveStub,
JS_ConvertStub, JS_FinalizeStub,
JSCLASS_NO_OPTIONAL_MEMBERS
};
void
js_InitJITStatsClass(JSContext *cx, JSObject *glob)
{
JS_InitClass(cx, glob, NULL, &jitstats_class, NULL, 0, jitstats_props, NULL, NULL, NULL);
}
#define AUDIT(x) (jitstats.x++)
#else
#define AUDIT(x) ((void)0)
#endif /* JS_JIT_SPEW */
#define INS_CONST(c) addName(lir->insImm(c), #c)
#define INS_CONSTPTR(p) addName(lir->insImmPtr(p), #p)
#define INS_CONSTFUNPTR(p) addName(lir->insImmPtr(JS_FUNC_TO_DATA_PTR(void*, p)), #p)
#define INS_CONSTWORD(v) addName(lir->insImmPtr((void *) v), #v)
using namespace avmplus;
using namespace nanojit;
static GC gc = GC();
static avmplus::AvmCore s_core = avmplus::AvmCore();
static avmplus::AvmCore* core = &s_core;
#ifdef JS_JIT_SPEW
void
js_DumpPeerStability(JSTraceMonitor* tm, const void* ip, JSObject* globalObj, uint32 globalShape, uint32 argc);
#endif
/* We really need a better way to configure the JIT. Shaver, where is my fancy JIT object? */
static bool did_we_check_processor_features = false;
#ifdef JS_JIT_SPEW
bool js_verboseDebug = getenv("TRACEMONKEY") && strstr(getenv("TRACEMONKEY"), "verbose");
bool js_verboseStats = js_verboseDebug ||
(getenv("TRACEMONKEY") && strstr(getenv("TRACEMONKEY"), "stats"));
bool js_verboseAbort = getenv("TRACEMONKEY") && strstr(getenv("TRACEMONKEY"), "abort");
#endif
/* The entire VM shares one oracle. Collisions and concurrent updates are tolerated and worst
case cause performance regressions. */
static Oracle oracle;
Tracker::Tracker()
{
pagelist = 0;
}
Tracker::~Tracker()
{
clear();
}
jsuword
Tracker::getPageBase(const void* v) const
{
return jsuword(v) & ~jsuword(NJ_PAGE_SIZE-1);
}
struct Tracker::Page*
Tracker::findPage(const void* v) const
{
jsuword base = getPageBase(v);
struct Tracker::Page* p = pagelist;
while (p) {
if (p->base == base) {
return p;
}
p = p->next;
}
return 0;
}
struct Tracker::Page*
Tracker::addPage(const void* v) {
jsuword base = getPageBase(v);
struct Tracker::Page* p = (struct Tracker::Page*)
GC::Alloc(sizeof(*p) - sizeof(p->map) + (NJ_PAGE_SIZE >> 2) * sizeof(LIns*));
p->base = base;
p->next = pagelist;
pagelist = p;
return p;
}
void
Tracker::clear()
{
while (pagelist) {
Page* p = pagelist;
pagelist = pagelist->next;
GC::Free(p);
}
}
bool
Tracker::has(const void *v) const
{
return get(v) != NULL;
}
#if defined NANOJIT_64BIT
#define PAGEMASK 0x7ff
#else
#define PAGEMASK 0xfff
#endif
LIns*
Tracker::get(const void* v) const
{
struct Tracker::Page* p = findPage(v);
if (!p)
return NULL;
return p->map[(jsuword(v) & PAGEMASK) >> 2];
}
void
Tracker::set(const void* v, LIns* i)
{
struct Tracker::Page* p = findPage(v);
if (!p)
p = addPage(v);
p->map[(jsuword(v) & PAGEMASK) >> 2] = i;
}
static inline jsuint argSlots(JSStackFrame* fp)
{
return JS_MAX(fp->argc, fp->fun->nargs);
}
static inline bool isNumber(jsval v)
{
return JSVAL_IS_INT(v) || JSVAL_IS_DOUBLE(v);
}
static inline jsdouble asNumber(jsval v)
{
JS_ASSERT(isNumber(v));
if (JSVAL_IS_DOUBLE(v))
return *JSVAL_TO_DOUBLE(v);
return (jsdouble)JSVAL_TO_INT(v);
}
static inline bool isInt32(jsval v)
{
if (!isNumber(v))
return false;
jsdouble d = asNumber(v);
jsint i;
return JSDOUBLE_IS_INT(d, i);
}
static inline jsint asInt32(jsval v)
{
JS_ASSERT(isNumber(v));
if (JSVAL_IS_INT(v))
return JSVAL_TO_INT(v);
#ifdef DEBUG
jsint i;
JS_ASSERT(JSDOUBLE_IS_INT(*JSVAL_TO_DOUBLE(v), i));
#endif
return jsint(*JSVAL_TO_DOUBLE(v));
}
/* Return JSVAL_DOUBLE for all numbers (int and double) and the tag otherwise. */
static inline uint8 getPromotedType(jsval v)
{
if (JSVAL_IS_INT(v))
return JSVAL_DOUBLE;
if (JSVAL_IS_OBJECT(v)) {
if (JSVAL_IS_NULL(v))
return JSVAL_TNULL;
if (HAS_FUNCTION_CLASS(JSVAL_TO_OBJECT(v)))
return JSVAL_TFUN;
return JSVAL_OBJECT;
}
return uint8(JSVAL_TAG(v));
}
/* Return JSVAL_INT for all whole numbers that fit into signed 32-bit and the tag otherwise. */
static inline uint8 getCoercedType(jsval v)
{
if (isInt32(v))
return JSVAL_INT;
if (JSVAL_IS_OBJECT(v)) {
if (JSVAL_IS_NULL(v))
return JSVAL_TNULL;
if (HAS_FUNCTION_CLASS(JSVAL_TO_OBJECT(v)))
return JSVAL_TFUN;
return JSVAL_OBJECT;
}
return uint8(JSVAL_TAG(v));
}
/*
* Constant seed and accumulate step borrowed from the DJB hash.
*/
#define ORACLE_MASK (ORACLE_SIZE - 1)
#define FRAGMENT_TABLE_MASK (FRAGMENT_TABLE_SIZE - 1)
#define HASH_SEED 5381
static inline void
hash_accum(uintptr_t& h, uintptr_t i, uintptr_t mask)
{
h = ((h << 5) + h + (mask & i)) & mask;
}
JS_REQUIRES_STACK static inline int
stackSlotHash(JSContext* cx, unsigned slot)
{
uintptr_t h = HASH_SEED;
hash_accum(h, uintptr_t(cx->fp->script), ORACLE_MASK);
hash_accum(h, uintptr_t(cx->fp->regs->pc), ORACLE_MASK);
hash_accum(h, uintptr_t(slot), ORACLE_MASK);
return int(h);
}
JS_REQUIRES_STACK static inline int
globalSlotHash(JSContext* cx, unsigned slot)
{
uintptr_t h = HASH_SEED;
JSStackFrame* fp = cx->fp;
while (fp->down)
fp = fp->down;
hash_accum(h, uintptr_t(fp->script), ORACLE_MASK);
hash_accum(h, uintptr_t(OBJ_SHAPE(JS_GetGlobalForObject(cx, fp->scopeChain))),
ORACLE_MASK);
hash_accum(h, uintptr_t(slot), ORACLE_MASK);
return int(h);
}
Oracle::Oracle()
{
/* Grow the oracle bitsets to their (fixed) size here, once. */
_stackDontDemote.set(&gc, ORACLE_SIZE-1);
_globalDontDemote.set(&gc, ORACLE_SIZE-1);
clear();
}
/* Tell the oracle that a certain global variable should not be demoted. */
JS_REQUIRES_STACK void
Oracle::markGlobalSlotUndemotable(JSContext* cx, unsigned slot)
{
_globalDontDemote.set(&gc, globalSlotHash(cx, slot));
}
/* Consult with the oracle whether we shouldn't demote a certain global variable. */
JS_REQUIRES_STACK bool
Oracle::isGlobalSlotUndemotable(JSContext* cx, unsigned slot) const
{
return _globalDontDemote.get(globalSlotHash(cx, slot));
}
/* Tell the oracle that a certain slot at a certain bytecode location should not be demoted. */
JS_REQUIRES_STACK void
Oracle::markStackSlotUndemotable(JSContext* cx, unsigned slot)
{
_stackDontDemote.set(&gc, stackSlotHash(cx, slot));
}
/* Consult with the oracle whether we shouldn't demote a certain slot. */
JS_REQUIRES_STACK bool
Oracle::isStackSlotUndemotable(JSContext* cx, unsigned slot) const
{
return _stackDontDemote.get(stackSlotHash(cx, slot));
}
void
Oracle::clearDemotability()
{
_stackDontDemote.reset();
_globalDontDemote.reset();
}
struct PCHashEntry : public JSDHashEntryStub {
size_t count;
};
#define PC_HASH_COUNT 1024
static void
js_Blacklist(jsbytecode* pc)
{
JS_ASSERT(*pc == JSOP_LOOP || *pc == JSOP_NOP);
*pc = JSOP_NOP;
}
static void
js_Backoff(JSContext *cx, jsbytecode* pc, Fragment* tree=NULL)
{
JSDHashTable *table = &JS_TRACE_MONITOR(cx).recordAttempts;
if (table->ops) {
PCHashEntry *entry = (PCHashEntry *)
JS_DHashTableOperate(table, pc, JS_DHASH_ADD);
if (entry) {
if (!entry->key) {
entry->key = pc;
JS_ASSERT(entry->count == 0);
}
JS_ASSERT(JS_DHASH_ENTRY_IS_LIVE(&(entry->hdr)));
if (entry->count++ > (BL_ATTEMPTS * MAXPEERS)) {
entry->count = 0;
js_Blacklist(pc);
return;
}
}
}
if (tree) {
tree->hits() -= BL_BACKOFF;
/*
* In case there is no entry or no table (due to OOM) or some
* serious imbalance in the recording-attempt distribution on a
* multitree, give each tree another chance to blacklist here as
* well.
*/
if (++tree->recordAttempts > BL_ATTEMPTS)
js_Blacklist(pc);
}
}
static void
js_resetRecordingAttempts(JSContext *cx, jsbytecode* pc)
{
JSDHashTable *table = &JS_TRACE_MONITOR(cx).recordAttempts;
if (table->ops) {
PCHashEntry *entry = (PCHashEntry *)
JS_DHashTableOperate(table, pc, JS_DHASH_LOOKUP);
if (JS_DHASH_ENTRY_IS_FREE(&(entry->hdr)))
return;
JS_ASSERT(JS_DHASH_ENTRY_IS_LIVE(&(entry->hdr)));
entry->count = 0;
}
}
static inline size_t
fragmentHash(const void *ip, JSObject* globalObj, uint32 globalShape, uint32 argc)
{
uintptr_t h = HASH_SEED;
hash_accum(h, uintptr_t(ip), FRAGMENT_TABLE_MASK);
hash_accum(h, uintptr_t(globalObj), FRAGMENT_TABLE_MASK);
hash_accum(h, uintptr_t(globalShape), FRAGMENT_TABLE_MASK);
hash_accum(h, uintptr_t(argc), FRAGMENT_TABLE_MASK);
return size_t(h);
}
/*
* argc is cx->fp->argc at the trace loop header, i.e., the number of arguments
* pushed for the innermost JS frame. This is required as part of the fragment
* key because the fragment will write those arguments back to the interpreter
* stack when it exits, using its typemap, which implicitly incorporates a given
* value of argc. Without this feature, a fragment could be called as an inner
* tree with two different values of argc, and entry type checking or exit
* frame synthesis could crash.
*/
struct VMFragment : public Fragment
{
VMFragment(const void* _ip, JSObject* _globalObj, uint32 _globalShape, uint32 _argc) :
Fragment(_ip),
next(NULL),
globalObj(_globalObj),
globalShape(_globalShape),
argc(_argc)
{}
VMFragment* next;
JSObject* globalObj;
uint32 globalShape;
uint32 argc;
};
static VMFragment*
getVMFragment(JSTraceMonitor* tm, const void *ip, JSObject* globalObj, uint32 globalShape,
uint32 argc)
{
size_t h = fragmentHash(ip, globalObj, globalShape, argc);
VMFragment* vf = tm->vmfragments[h];
while (vf &&
! (vf->globalObj == globalObj &&
vf->globalShape == globalShape &&
vf->ip == ip &&
vf->argc == argc)) {
vf = vf->next;
}
return vf;
}
static VMFragment*
getLoop(JSTraceMonitor* tm, const void *ip, JSObject* globalObj, uint32 globalShape,
uint32 argc)
{
return getVMFragment(tm, ip, globalObj, globalShape, argc);
}
static Fragment*
getAnchor(JSTraceMonitor* tm, const void *ip, JSObject* globalObj, uint32 globalShape,
uint32 argc)
{
VMFragment *f = new (&gc) VMFragment(ip, globalObj, globalShape, argc);
JS_ASSERT(f);
Fragment *p = getVMFragment(tm, ip, globalObj, globalShape, argc);
if (p) {
f->first = p;
/* append at the end of the peer list */
Fragment* next;
while ((next = p->peer) != NULL)
p = next;
p->peer = f;
} else {
/* this is the first fragment */
f->first = f;
size_t h = fragmentHash(ip, globalObj, globalShape, argc);
f->next = tm->vmfragments[h];
tm->vmfragments[h] = f;
}
f->anchor = f;
f->root = f;
f->kind = LoopTrace;
return f;
}
#ifdef DEBUG
static void
ensureTreeIsUnique(JSTraceMonitor* tm, VMFragment* f, TreeInfo* ti)
{
JS_ASSERT(f->root == f);
/*
* Check for duplicate entry type maps. This is always wrong and hints at
* trace explosion since we are trying to stabilize something without
* properly connecting peer edges.
*/
TreeInfo* ti_other;
for (Fragment* peer = getLoop(tm, f->ip, f->globalObj, f->globalShape, f->argc);
peer != NULL;
peer = peer->peer) {
if (!peer->code() || peer == f)
continue;
ti_other = (TreeInfo*)peer->vmprivate;
JS_ASSERT(ti_other);
JS_ASSERT(!ti->typeMap.matches(ti_other->typeMap));
}
}
#endif
static void
js_AttemptCompilation(JSContext *cx, JSTraceMonitor* tm, JSObject* globalObj, jsbytecode* pc,
uint32 argc)
{
/*
* If we already permanently blacklisted the location, undo that.
*/
JS_ASSERT(*(jsbytecode*)pc == JSOP_NOP || *(jsbytecode*)pc == JSOP_LOOP);
*(jsbytecode*)pc = JSOP_LOOP;
js_resetRecordingAttempts(cx, pc);
/*
* Breath new live into all peer fragments at the designated loop header.
*/
Fragment* f = (VMFragment*)getLoop(tm, pc, globalObj, OBJ_SHAPE(globalObj),
argc);
if (!f) {
/*
* If the global object's shape changed, we can't easily find the
* corresponding loop header via a hash table lookup. In this
* we simply bail here and hope that the fragment has another
* outstanding compilation attempt. This case is extremely rare.
*/
return;
}
JS_ASSERT(f->root == f);
f = f->first;
while (f) {
JS_ASSERT(f->root == f);
--f->recordAttempts;
f->hits() = HOTLOOP;
f = f->peer;
}
}
JS_DEFINE_CALLINFO_1(static, DOUBLE, i2f, INT32, 1, 1)
JS_DEFINE_CALLINFO_1(static, DOUBLE, u2f, UINT32, 1, 1)
static bool isi2f(LInsp i)
{
if (i->isop(LIR_i2f))
return true;
if (nanojit::AvmCore::config.soft_float &&
i->isop(LIR_qjoin) &&
i->oprnd1()->isop(LIR_call) &&
i->oprnd2()->isop(LIR_callh))
{
if (i->oprnd1()->callInfo() == &i2f_ci)
return true;
}
return false;
}
static bool isu2f(LInsp i)
{
if (i->isop(LIR_u2f))
return true;
if (nanojit::AvmCore::config.soft_float &&
i->isop(LIR_qjoin) &&
i->oprnd1()->isop(LIR_call) &&
i->oprnd2()->isop(LIR_callh))
{
if (i->oprnd1()->callInfo() == &u2f_ci)
return true;
}
return false;
}
static LInsp iu2fArg(LInsp i)
{
if (nanojit::AvmCore::config.soft_float &&
i->isop(LIR_qjoin))
{
return i->oprnd1()->arg(0);
}
return i->oprnd1();
}
static LIns* demote(LirWriter *out, LInsp i)
{
if (i->isCall())
return callArgN(i, 0);
if (isi2f(i) || isu2f(i))
return iu2fArg(i);
if (i->isconst())
return i;
AvmAssert(i->isconstq());
double cf = i->constvalf();
int32_t ci = cf > 0x7fffffff ? uint32_t(cf) : int32_t(cf);
return out->insImm(ci);
}
static bool isPromoteInt(LIns* i)
{
if (isi2f(i) || i->isconst())
return true;
if (!i->isconstq())
return false;
jsdouble d = i->constvalf();
return d == jsdouble(jsint(d)) && !JSDOUBLE_IS_NEGZERO(d);
}
static bool isPromoteUint(LIns* i)
{
if (isu2f(i) || i->isconst())
return true;
if (!i->isconstq())
return false;
jsdouble d = i->constvalf();
return d == jsdouble(jsuint(d)) && !JSDOUBLE_IS_NEGZERO(d);
}
static bool isPromote(LIns* i)
{
return isPromoteInt(i) || isPromoteUint(i);
}
static bool isconst(LIns* i, int32_t c)
{
return i->isconst() && i->constval() == c;
}
static bool overflowSafe(LIns* i)
{
LIns* c;
return (i->isop(LIR_and) && ((c = i->oprnd2())->isconst()) &&
((c->constval() & 0xc0000000) == 0)) ||
(i->isop(LIR_rsh) && ((c = i->oprnd2())->isconst()) &&
((c->constval() > 0)));
}
/* soft float support */
JS_DEFINE_CALLINFO_1(static, DOUBLE, fneg, DOUBLE, 1, 1)
JS_DEFINE_CALLINFO_2(static, INT32, fcmpeq, DOUBLE, DOUBLE, 1, 1)
JS_DEFINE_CALLINFO_2(static, INT32, fcmplt, DOUBLE, DOUBLE, 1, 1)
JS_DEFINE_CALLINFO_2(static, INT32, fcmple, DOUBLE, DOUBLE, 1, 1)
JS_DEFINE_CALLINFO_2(static, INT32, fcmpgt, DOUBLE, DOUBLE, 1, 1)
JS_DEFINE_CALLINFO_2(static, INT32, fcmpge, DOUBLE, DOUBLE, 1, 1)
JS_DEFINE_CALLINFO_2(static, DOUBLE, fmul, DOUBLE, DOUBLE, 1, 1)
JS_DEFINE_CALLINFO_2(static, DOUBLE, fadd, DOUBLE, DOUBLE, 1, 1)
JS_DEFINE_CALLINFO_2(static, DOUBLE, fdiv, DOUBLE, DOUBLE, 1, 1)
JS_DEFINE_CALLINFO_2(static, DOUBLE, fsub, DOUBLE, DOUBLE, 1, 1)
jsdouble FASTCALL
fneg(jsdouble x)
{
return -x;
}
jsdouble FASTCALL
i2f(int32 i)
{
return i;
}
jsdouble FASTCALL
u2f(jsuint u)
{
return u;
}
int32 FASTCALL
fcmpeq(jsdouble x, jsdouble y)
{
return x==y;
}
int32 FASTCALL
fcmplt(jsdouble x, jsdouble y)
{
return x < y;
}
int32 FASTCALL
fcmple(jsdouble x, jsdouble y)
{
return x <= y;
}
int32 FASTCALL
fcmpgt(jsdouble x, jsdouble y)
{
return x > y;
}
int32 FASTCALL
fcmpge(jsdouble x, jsdouble y)
{
return x >= y;
}
jsdouble FASTCALL
fmul(jsdouble x, jsdouble y)
{
return x * y;
}
jsdouble FASTCALL
fadd(jsdouble x, jsdouble y)
{
return x + y;
}
jsdouble FASTCALL
fdiv(jsdouble x, jsdouble y)
{
return x / y;
}
jsdouble FASTCALL
fsub(jsdouble x, jsdouble y)
{
return x - y;
}
class SoftFloatFilter: public LirWriter
{
public:
SoftFloatFilter(LirWriter* out):
LirWriter(out)
{
}
LInsp quadCall(const CallInfo *ci, LInsp args[]) {
LInsp qlo, qhi;
qlo = out->insCall(ci, args);
qhi = out->ins1(LIR_callh, qlo);
return out->qjoin(qlo, qhi);
}
LInsp ins1(LOpcode v, LInsp s0)
{
if (v == LIR_fneg)
return quadCall(&fneg_ci, &s0);
if (v == LIR_i2f)
return quadCall(&i2f_ci, &s0);
if (v == LIR_u2f)
return quadCall(&u2f_ci, &s0);
return out->ins1(v, s0);
}
LInsp ins2(LOpcode v, LInsp s0, LInsp s1)
{
LInsp args[2];
LInsp bv;
// change the numeric value and order of these LIR opcodes and die
if (LIR_fadd <= v && v <= LIR_fdiv) {
static const CallInfo *fmap[] = { &fadd_ci, &fsub_ci, &fmul_ci, &fdiv_ci };
args[0] = s1;
args[1] = s0;
return quadCall(fmap[v - LIR_fadd], args);
}
if (LIR_feq <= v && v <= LIR_fge) {
static const CallInfo *fmap[] = { &fcmpeq_ci, &fcmplt_ci, &fcmpgt_ci, &fcmple_ci, &fcmpge_ci };
args[0] = s1;
args[1] = s0;
bv = out->insCall(fmap[v - LIR_feq], args);
return out->ins2(LIR_eq, bv, out->insImm(1));
}
return out->ins2(v, s0, s1);
}
LInsp insCall(const CallInfo *ci, LInsp args[])
{
// if the return type is ARGSIZE_F, we have
// to do a quadCall ( qjoin(call,callh) )
if ((ci->_argtypes & 3) == ARGSIZE_F)
return quadCall(ci, args);
return out->insCall(ci, args);
}
};
class FuncFilter: public LirWriter
{
public:
FuncFilter(LirWriter* out):
LirWriter(out)
{
}
LInsp ins2(LOpcode v, LInsp s0, LInsp s1)
{
if (s0 == s1 && v == LIR_feq) {
if (isPromote(s0)) {
// double(int) and double(uint) cannot be nan
return insImm(1);
}
if (s0->isop(LIR_fmul) || s0->isop(LIR_fsub) || s0->isop(LIR_fadd)) {
LInsp lhs = s0->oprnd1();
LInsp rhs = s0->oprnd2();
if (isPromote(lhs) && isPromote(rhs)) {
// add/sub/mul promoted ints can't be nan
return insImm(1);
}
}
} else if (LIR_feq <= v && v <= LIR_fge) {
if (isPromoteInt(s0) && isPromoteInt(s1)) {
// demote fcmp to cmp
v = LOpcode(v + (LIR_eq - LIR_feq));
return out->ins2(v, demote(out, s0), demote(out, s1));
} else if (isPromoteUint(s0) && isPromoteUint(s1)) {
// uint compare
v = LOpcode(v + (LIR_eq - LIR_feq));
if (v != LIR_eq)
v = LOpcode(v + (LIR_ult - LIR_lt)); // cmp -> ucmp
return out->ins2(v, demote(out, s0), demote(out, s1));
}
} else if (v == LIR_or &&
s0->isop(LIR_lsh) && isconst(s0->oprnd2(), 16) &&
s1->isop(LIR_and) && isconst(s1->oprnd2(), 0xffff)) {
LIns* msw = s0->oprnd1();
LIns* lsw = s1->oprnd1();
LIns* x;
LIns* y;
if (lsw->isop(LIR_add) &&
lsw->oprnd1()->isop(LIR_and) &&
lsw->oprnd2()->isop(LIR_and) &&
isconst(lsw->oprnd1()->oprnd2(), 0xffff) &&
isconst(lsw->oprnd2()->oprnd2(), 0xffff) &&
msw->isop(LIR_add) &&
msw->oprnd1()->isop(LIR_add) &&
msw->oprnd2()->isop(LIR_rsh) &&
msw->oprnd1()->oprnd1()->isop(LIR_rsh) &&
msw->oprnd1()->oprnd2()->isop(LIR_rsh) &&
isconst(msw->oprnd2()->oprnd2(), 16) &&
isconst(msw->oprnd1()->oprnd1()->oprnd2(), 16) &&
isconst(msw->oprnd1()->oprnd2()->oprnd2(), 16) &&
(x = lsw->oprnd1()->oprnd1()) == msw->oprnd1()->oprnd1()->oprnd1() &&
(y = lsw->oprnd2()->oprnd1()) == msw->oprnd1()->oprnd2()->oprnd1() &&
lsw == msw->oprnd2()->oprnd1()) {
return out->ins2(LIR_add, x, y);
}
}
#ifdef NANOJIT_ARM
else if (v == LIR_lsh ||
v == LIR_rsh ||
v == LIR_ush)
{
// needed on ARM -- arm doesn't mask shifts to 31 like x86 does
if (s1->isconst())
s1->setimm16(s1->constval() & 31);
else
s1 = out->ins2(LIR_and, s1, out->insImm(31));
return out->ins2(v, s0, s1);
}
#endif
return out->ins2(v, s0, s1);
}
LInsp insCall(const CallInfo *ci, LInsp args[])
{
if (ci == &js_DoubleToUint32_ci) {
LInsp s0 = args[0];
if (s0->isconstq())
return out->insImm(js_DoubleToECMAUint32(s0->constvalf()));
if (isi2f(s0) || isu2f(s0))
return iu2fArg(s0);
} else if (ci == &js_DoubleToInt32_ci) {
LInsp s0 = args[0];
if (s0->isconstq())
return out->insImm(js_DoubleToECMAInt32(s0->constvalf()));
if (s0->isop(LIR_fadd) || s0->isop(LIR_fsub)) {
LInsp lhs = s0->oprnd1();
LInsp rhs = s0->oprnd2();
if (isPromote(lhs) && isPromote(rhs)) {
LOpcode op = LOpcode(s0->opcode() & ~LIR64);
return out->ins2(op, demote(out, lhs), demote(out, rhs));
}
}
if (isi2f(s0) || isu2f(s0))
return iu2fArg(s0);
// XXX ARM -- check for qjoin(call(UnboxDouble),call(UnboxDouble))
if (s0->isCall()) {
const CallInfo* ci2 = s0->callInfo();
if (ci2 == &js_UnboxDouble_ci) {
LIns* args2[] = { callArgN(s0, 0) };
return out->insCall(&js_UnboxInt32_ci, args2);
} else if (ci2 == &js_StringToNumber_ci) {
// callArgN's ordering is that as seen by the builtin, not as stored in
// args here. True story!
LIns* args2[] = { callArgN(s0, 1), callArgN(s0, 0) };
return out->insCall(&js_StringToInt32_ci, args2);
} else if (ci2 == &js_String_p_charCodeAt0_ci) {
// Use a fast path builtin for a charCodeAt that converts to an int right away.
LIns* args2[] = { callArgN(s0, 0) };
return out->insCall(&js_String_p_charCodeAt0_int_ci, args2);
} else if (ci2 == &js_String_p_charCodeAt_ci) {
LIns* idx = callArgN(s0, 1);
// If the index is not already an integer, force it to be an integer.
idx = isPromote(idx)
? demote(out, idx)
: out->insCall(&js_DoubleToInt32_ci, &idx);
LIns* args2[] = { idx, callArgN(s0, 0) };
return out->insCall(&js_String_p_charCodeAt_int_ci, args2);
}
}
} else if (ci == &js_BoxDouble_ci) {
LInsp s0 = args[0];
JS_ASSERT(s0->isQuad());
if (isi2f(s0)) {
LIns* args2[] = { iu2fArg(s0), args[1] };
return out->insCall(&js_BoxInt32_ci, args2);
}
if (s0->isCall() && s0->callInfo() == &js_UnboxDouble_ci)
return callArgN(s0, 0);
}
return out->insCall(ci, args);
}
};
/* In debug mode vpname contains a textual description of the type of the
slot during the forall iteration over all slots. If JS_JIT_SPEW is not
defined, vpnum is set to a very large integer to catch invalid uses of
it. Non-debug code should never use vpnum. */
#ifdef JS_JIT_SPEW
#define DEF_VPNAME const char* vpname; unsigned vpnum
#define SET_VPNAME(name) do { vpname = name; vpnum = 0; } while(0)
#define INC_VPNUM() do { ++vpnum; } while(0)
#else
#define DEF_VPNAME do {} while (0)
#define vpname ""
#define vpnum 0x40000000
#define SET_VPNAME(name) ((void)0)
#define INC_VPNUM() ((void)0)
#endif
/* Iterate over all interned global variables. */
#define FORALL_GLOBAL_SLOTS(cx, ngslots, gslots, code) \
JS_BEGIN_MACRO \
DEF_VPNAME; \
JSObject* globalObj = JS_GetGlobalForObject(cx, cx->fp->scopeChain); \
unsigned n; \
jsval* vp; \
SET_VPNAME("global"); \
for (n = 0; n < ngslots; ++n) { \
vp = &STOBJ_GET_SLOT(globalObj, gslots[n]); \
{ code; } \
INC_VPNUM(); \
} \
JS_END_MACRO
/* Iterate over all slots in the frame, consisting of args, vars, and stack
(except for the top-level frame which does not have args or vars. */
#define FORALL_FRAME_SLOTS(fp, depth, code) \
JS_BEGIN_MACRO \
jsval* vp; \
jsval* vpstop; \
if (fp->callee) { \
if (depth == 0) { \
SET_VPNAME("callee"); \
vp = &fp->argv[-2]; \
{ code; } \
SET_VPNAME("this"); \
vp = &fp->argv[-1]; \
{ code; } \
SET_VPNAME("argv"); \
vp = &fp->argv[0]; vpstop = &fp->argv[argSlots(fp)]; \
while (vp < vpstop) { code; ++vp; INC_VPNUM(); } \
} \
SET_VPNAME("vars"); \
vp = fp->slots; vpstop = &fp->slots[fp->script->nfixed]; \
while (vp < vpstop) { code; ++vp; INC_VPNUM(); } \
} \
SET_VPNAME("stack"); \
vp = StackBase(fp); vpstop = fp->regs->sp; \
while (vp < vpstop) { code; ++vp; INC_VPNUM(); } \
if (fsp < fspstop - 1) { \
JSStackFrame* fp2 = fsp[1]; \
int missing = fp2->fun->nargs - fp2->argc; \
if (missing > 0) { \
SET_VPNAME("missing"); \
vp = fp->regs->sp; \
vpstop = vp + missing; \
while (vp < vpstop) { code; ++vp; INC_VPNUM(); } \
} \
} \
JS_END_MACRO
/* Iterate over all slots in each pending frame. */
#define FORALL_SLOTS_IN_PENDING_FRAMES(cx, callDepth, code) \
JS_BEGIN_MACRO \
DEF_VPNAME; \
unsigned n; \
JSStackFrame* currentFrame = cx->fp; \
JSStackFrame* entryFrame; \
JSStackFrame* fp = currentFrame; \
for (n = 0; n < callDepth; ++n) { fp = fp->down; } \
entryFrame = fp; \
unsigned frames = callDepth+1; \
JSStackFrame** fstack = \
(JSStackFrame**) alloca(frames * sizeof (JSStackFrame*)); \
JSStackFrame** fspstop = &fstack[frames]; \
JSStackFrame** fsp = fspstop-1; \
fp = currentFrame; \
for (;; fp = fp->down) { *fsp-- = fp; if (fp == entryFrame) break; } \
unsigned depth; \
for (depth = 0, fsp = fstack; fsp < fspstop; ++fsp, ++depth) { \
fp = *fsp; \
FORALL_FRAME_SLOTS(fp, depth, code); \
} \
JS_END_MACRO
#define FORALL_SLOTS(cx, ngslots, gslots, callDepth, code) \
JS_BEGIN_MACRO \
FORALL_SLOTS_IN_PENDING_FRAMES(cx, callDepth, code); \
FORALL_GLOBAL_SLOTS(cx, ngslots, gslots, code); \
JS_END_MACRO
/* Calculate the total number of native frame slots we need from this frame
all the way back to the entry frame, including the current stack usage. */
JS_REQUIRES_STACK unsigned
js_NativeStackSlots(JSContext *cx, unsigned callDepth)
{
JSStackFrame* fp = cx->fp;
unsigned slots = 0;
#if defined _DEBUG
unsigned int origCallDepth = callDepth;
#endif
for (;;) {
unsigned operands = fp->regs->sp - StackBase(fp);
slots += operands;
if (fp->callee)
slots += fp->script->nfixed;
if (callDepth-- == 0) {
if (fp->callee)
slots += 2/*callee,this*/ + argSlots(fp);
#if defined _DEBUG
unsigned int m = 0;
FORALL_SLOTS_IN_PENDING_FRAMES(cx, origCallDepth, m++);
JS_ASSERT(m == slots);
#endif
return slots;
}
JSStackFrame* fp2 = fp;
fp = fp->down;
int missing = fp2->fun->nargs - fp2->argc;
if (missing > 0)
slots += missing;
}
JS_NOT_REACHED("js_NativeStackSlots");
}
/*
* Capture the type map for the selected slots of the global object and currently pending
* stack frames.
*/
JS_REQUIRES_STACK void
TypeMap::captureTypes(JSContext* cx, SlotList& slots, unsigned callDepth)
{
unsigned ngslots = slots.length();
uint16* gslots = slots.data();
setLength(js_NativeStackSlots(cx, callDepth) + ngslots);
uint8* map = data();
uint8* m = map;
FORALL_SLOTS_IN_PENDING_FRAMES(cx, callDepth,
uint8 type = getCoercedType(*vp);
if ((type == JSVAL_INT) && oracle.isStackSlotUndemotable(cx, unsigned(m - map)))
type = JSVAL_DOUBLE;
JS_ASSERT(type != JSVAL_BOXED);
debug_only_v(printf("capture stack type %s%d: %d=%c\n", vpname, vpnum, type, typeChar[type]);)
JS_ASSERT(uintptr_t(m - map) < length());
*m++ = type;
);
FORALL_GLOBAL_SLOTS(cx, ngslots, gslots,
uint8 type = getCoercedType(*vp);
if ((type == JSVAL_INT) && oracle.isGlobalSlotUndemotable(cx, gslots[n]))
type = JSVAL_DOUBLE;
JS_ASSERT(type != JSVAL_BOXED);
debug_only_v(printf("capture global type %s%d: %d=%c\n", vpname, vpnum, type, typeChar[type]);)
JS_ASSERT(uintptr_t(m - map) < length());
*m++ = type;
);
JS_ASSERT(uintptr_t(m - map) == length());
}
JS_REQUIRES_STACK void
TypeMap::captureMissingGlobalTypes(JSContext* cx, SlotList& slots, unsigned stackSlots)
{
unsigned oldSlots = length() - stackSlots;
int diff = slots.length() - oldSlots;
JS_ASSERT(diff >= 0);
unsigned ngslots = slots.length();
uint16* gslots = slots.data();
setLength(length() + diff);
uint8* map = data() + stackSlots;
uint8* m = map;
FORALL_GLOBAL_SLOTS(cx, ngslots, gslots,
if (n >= oldSlots) {
uint8 type = getCoercedType(*vp);
if ((type == JSVAL_INT) && oracle.isGlobalSlotUndemotable(cx, gslots[n]))
type = JSVAL_DOUBLE;
JS_ASSERT(type != JSVAL_BOXED);
debug_only_v(printf("capture global type %s%d: %d=%c\n", vpname, vpnum, type, typeChar[type]);)
*m = type;
JS_ASSERT((m > map + oldSlots) || (*m == type));
}
m++;
);
}
/* Compare this type map to another one and see whether they match. */
bool
TypeMap::matches(TypeMap& other) const
{
if (length() != other.length())
return false;
return !memcmp(data(), other.data(), length());
}
/* Use the provided storage area to create a new type map that contains the partial type map
with the rest of it filled up from the complete type map. */
static void
mergeTypeMaps(uint8** partial, unsigned* plength, uint8* complete, unsigned clength, uint8* mem)
{
unsigned l = *plength;
JS_ASSERT(l < clength);
memcpy(mem, *partial, l * sizeof(uint8));
memcpy(mem + l, complete + l, (clength - l) * sizeof(uint8));
*partial = mem;
*plength = clength;
}
/* Specializes a tree to any missing globals, including any dependent trees. */
static JS_REQUIRES_STACK void
specializeTreesToMissingGlobals(JSContext* cx, TreeInfo* root)
{
TreeInfo* ti = root;
ti->typeMap.captureMissingGlobalTypes(cx, *ti->globalSlots, ti->nStackTypes);
JS_ASSERT(ti->globalSlots->length() == ti->typeMap.length() - ti->nStackTypes);
for (unsigned i = 0; i < root->dependentTrees.length(); i++) {
ti = (TreeInfo*)root->dependentTrees.data()[i]->vmprivate;
/* ti can be NULL if we hit the recording tree in emitTreeCall; this is harmless. */
if (ti && ti->nGlobalTypes() < ti->globalSlots->length())
specializeTreesToMissingGlobals(cx, ti);
}
for (unsigned i = 0; i < root->linkedTrees.length(); i++) {
ti = (TreeInfo*)root->linkedTrees.data()[i]->vmprivate;
if (ti && ti->nGlobalTypes() < ti->globalSlots->length())
specializeTreesToMissingGlobals(cx, ti);
}
}
static void
js_TrashTree(JSContext* cx, Fragment* f);
JS_REQUIRES_STACK
TraceRecorder::TraceRecorder(JSContext* cx, VMSideExit* _anchor, Fragment* _fragment,
TreeInfo* ti, unsigned stackSlots, unsigned ngslots, uint8* typeMap,
VMSideExit* innermostNestedGuard, jsbytecode* outer, uint32 outerArgc)
{
JS_ASSERT(!_fragment->vmprivate && ti && cx->fp->regs->pc == (jsbytecode*)_fragment->ip);
/* Reset the fragment state we care about in case we got a recycled fragment. */
_fragment->lastIns = NULL;
this->cx = cx;
this->traceMonitor = &JS_TRACE_MONITOR(cx);
this->globalObj = JS_GetGlobalForObject(cx, cx->fp->scopeChain);
this->lexicalBlock = cx->fp->blockChain;
this->anchor = _anchor;
this->fragment = _fragment;
this->lirbuf = _fragment->lirbuf;
this->treeInfo = ti;
this->callDepth = _anchor ? _anchor->calldepth : 0;
this->atoms = FrameAtomBase(cx, cx->fp);
this->deepAborted = false;
this->trashSelf = false;
this->global_dslots = this->globalObj->dslots;
this->loop = true; /* default assumption is we are compiling a loop */
this->wasRootFragment = _fragment == _fragment->root;
this->outer = outer;
this->outerArgc = outerArgc;
this->pendingTraceableNative = NULL;
this->newobj_ins = NULL;
this->generatedTraceableNative = new JSTraceableNative();
JS_ASSERT(generatedTraceableNative);
debug_only_v(printf("recording starting from %s:%u@%u\n",
ti->treeFileName, ti->treeLineNumber, ti->treePCOffset);)
debug_only_v(printf("globalObj=%p, shape=%d\n", (void*)this->globalObj, OBJ_SHAPE(this->globalObj));)
lir = lir_buf_writer = new (&gc) LirBufWriter(lirbuf);
debug_only_v(lir = verbose_filter = new (&gc) VerboseWriter(&gc, lir, lirbuf->names);)
if (nanojit::AvmCore::config.soft_float)
lir = float_filter = new (&gc) SoftFloatFilter(lir);
else
float_filter = 0;
lir = cse_filter = new (&gc) CseFilter(lir, &gc);
lir = expr_filter = new (&gc) ExprFilter(lir);
lir = func_filter = new (&gc) FuncFilter(lir);
lir->ins0(LIR_start);
if (!nanojit::AvmCore::config.tree_opt || fragment->root == fragment)
lirbuf->state = addName(lir->insParam(0, 0), "state");
lirbuf->sp = addName(lir->insLoad(LIR_ldp, lirbuf->state, (int)offsetof(InterpState, sp)), "sp");
lirbuf->rp = addName(lir->insLoad(LIR_ldp, lirbuf->state, offsetof(InterpState, rp)), "rp");
cx_ins = addName(lir->insLoad(LIR_ldp, lirbuf->state, offsetof(InterpState, cx)), "cx");
eos_ins = addName(lir->insLoad(LIR_ldp, lirbuf->state, offsetof(InterpState, eos)), "eos");
eor_ins = addName(lir->insLoad(LIR_ldp, lirbuf->state, offsetof(InterpState, eor)), "eor");
/* If we came from exit, we might not have enough global types. */
if (ti->globalSlots->length() > ti->nGlobalTypes())
specializeTreesToMissingGlobals(cx, ti);
/* read into registers all values on the stack and all globals we know so far */
import(treeInfo, lirbuf->sp, stackSlots, ngslots, callDepth, typeMap);
if (fragment == fragment->root) {
/*
* We poll the operation callback request flag. It is updated asynchronously whenever
* the callback is to be invoked.
*/
LIns* x = lir->insLoadi(cx_ins, offsetof(JSContext, operationCallbackFlag));
guard(true, lir->ins_eq0(x), snapshot(TIMEOUT_EXIT));
}
/* If we are attached to a tree call guard, make sure the guard the inner tree exited from
is what we expect it to be. */
if (_anchor && _anchor->exitType == NESTED_EXIT) {
LIns* nested_ins = addName(lir->insLoad(LIR_ldp, lirbuf->state,
offsetof(InterpState, lastTreeExitGuard)),
"lastTreeExitGuard");
guard(true, lir->ins2(LIR_eq, nested_ins, INS_CONSTPTR(innermostNestedGuard)), NESTED_EXIT);
}
}
TreeInfo::~TreeInfo()
{
UnstableExit* temp;
while (unstableExits) {
temp = unstableExits->next;
delete unstableExits;
unstableExits = temp;
}
}
TraceRecorder::~TraceRecorder()
{
JS_ASSERT(nextRecorderToAbort == NULL);
JS_ASSERT(treeInfo && (fragment || wasDeepAborted()));
#ifdef DEBUG
TraceRecorder* tr = JS_TRACE_MONITOR(cx).abortStack;
while (tr != NULL)
{
JS_ASSERT(this != tr);
tr = tr->nextRecorderToAbort;
}
#endif
if (fragment) {
if (wasRootFragment && !fragment->root->code()) {
JS_ASSERT(!fragment->root->vmprivate);
delete treeInfo;
}
if (trashSelf)
js_TrashTree(cx, fragment->root);
for (unsigned int i = 0; i < whichTreesToTrash.length(); i++)
js_TrashTree(cx, whichTreesToTrash.get(i));
} else if (wasRootFragment) {
delete treeInfo;
}
#ifdef DEBUG
delete verbose_filter;
#endif
delete cse_filter;
delete expr_filter;
delete func_filter;
delete float_filter;
delete lir_buf_writer;
delete generatedTraceableNative;
}
void TraceRecorder::removeFragmentoReferences()
{
fragment = NULL;
}
void TraceRecorder::deepAbort()
{
debug_only_v(printf("deep abort");)
deepAborted = true;
}
/* Add debug information to a LIR instruction as we emit it. */
inline LIns*
TraceRecorder::addName(LIns* ins, const char* name)
{
#ifdef JS_JIT_SPEW
if (js_verboseDebug)
lirbuf->names->addName(ins, name);
#endif
return ins;
}
/* Determine the current call depth (starting with the entry frame.) */
unsigned
TraceRecorder::getCallDepth() const
{
return callDepth;
}
/* Determine the offset in the native global frame for a jsval we track */
ptrdiff_t
TraceRecorder::nativeGlobalOffset(jsval* p) const
{
JS_ASSERT(isGlobal(p));
if (size_t(p - globalObj->fslots) < JS_INITIAL_NSLOTS)
return sizeof(InterpState) + size_t(p - globalObj->fslots) * sizeof(double);
return sizeof(InterpState) + ((p - globalObj->dslots) + JS_INITIAL_NSLOTS) * sizeof(double);
}
/* Determine whether a value is a global stack slot */
bool
TraceRecorder::isGlobal(jsval* p) const
{
return ((size_t(p - globalObj->fslots) < JS_INITIAL_NSLOTS) ||
(size_t(p - globalObj->dslots) < (STOBJ_NSLOTS(globalObj) - JS_INITIAL_NSLOTS)));
}
/* Determine the offset in the native stack for a jsval we track */
JS_REQUIRES_STACK ptrdiff_t
TraceRecorder::nativeStackOffset(jsval* p) const
{
#ifdef DEBUG
size_t slow_offset = 0;
FORALL_SLOTS_IN_PENDING_FRAMES(cx, callDepth,
if (vp == p) goto done;
slow_offset += sizeof(double)
);
/*
* If it's not in a pending frame, it must be on the stack of the current frame above
* sp but below fp->slots + script->nslots.
*/
JS_ASSERT(size_t(p - cx->fp->slots) < cx->fp->script->nslots);
slow_offset += size_t(p - cx->fp->regs->sp) * sizeof(double);
done:
#define RETURN(offset) { JS_ASSERT((offset) == slow_offset); return offset; }
#else
#define RETURN(offset) { return offset; }
#endif
size_t offset = 0;
JSStackFrame* currentFrame = cx->fp;
JSStackFrame* entryFrame;
JSStackFrame* fp = currentFrame;
for (unsigned n = 0; n < callDepth; ++n) { fp = fp->down; }
entryFrame = fp;
unsigned frames = callDepth+1;
JSStackFrame** fstack = (JSStackFrame **)alloca(frames * sizeof (JSStackFrame *));
JSStackFrame** fspstop = &fstack[frames];
JSStackFrame** fsp = fspstop-1;
fp = currentFrame;
for (;; fp = fp->down) { *fsp-- = fp; if (fp == entryFrame) break; }
for (fsp = fstack; fsp < fspstop; ++fsp) {
fp = *fsp;
if (fp->callee) {
if (fsp == fstack) {
if (size_t(p - &fp->argv[-2]) < size_t(2/*callee,this*/ + argSlots(fp)))
RETURN(offset + size_t(p - &fp->argv[-2]) * sizeof(double));
offset += (2/*callee,this*/ + argSlots(fp)) * sizeof(double);
}
if (size_t(p - &fp->slots[0]) < fp->script->nfixed)
RETURN(offset + size_t(p - &fp->slots[0]) * sizeof(double));
offset += fp->script->nfixed * sizeof(double);
}
jsval* spbase = StackBase(fp);
if (size_t(p - spbase) < size_t(fp->regs->sp - spbase))
RETURN(offset + size_t(p - spbase) * sizeof(double));
offset += size_t(fp->regs->sp - spbase) * sizeof(double);
if (fsp < fspstop - 1) {
JSStackFrame* fp2 = fsp[1];
int missing = fp2->fun->nargs - fp2->argc;
if (missing > 0) {
if (size_t(p - fp->regs->sp) < size_t(missing))
RETURN(offset + size_t(p - fp->regs->sp) * sizeof(double));
offset += size_t(missing) * sizeof(double);
}
}
}
/*
* If it's not in a pending frame, it must be on the stack of the current frame above
* sp but below fp->slots + script->nslots.
*/
JS_ASSERT(size_t(p - currentFrame->slots) < currentFrame->script->nslots);
offset += size_t(p - currentFrame->regs->sp) * sizeof(double);
RETURN(offset);
#undef RETURN
}
/* Track the maximum number of native frame slots we need during
execution. */
void
TraceRecorder::trackNativeStackUse(unsigned slots)
{
if (slots > treeInfo->maxNativeStackSlots)
treeInfo->maxNativeStackSlots = slots;
}
/* Unbox a jsval into a slot. Slots are wide enough to hold double values directly (instead of
storing a pointer to them). We now assert instead of type checking, the caller must ensure the
types are compatible. */
static void
ValueToNative(JSContext* cx, jsval v, uint8 type, double* slot)
{
unsigned tag = JSVAL_TAG(v);
switch (type) {
case JSVAL_OBJECT:
JS_ASSERT(tag == JSVAL_OBJECT);
JS_ASSERT(!JSVAL_IS_NULL(v) && !HAS_FUNCTION_CLASS(JSVAL_TO_OBJECT(v)));
*(JSObject**)slot = JSVAL_TO_OBJECT(v);
debug_only_v(printf("object<%p:%s> ", (void*)JSVAL_TO_OBJECT(v),
JSVAL_IS_NULL(v)
? "null"
: STOBJ_GET_CLASS(JSVAL_TO_OBJECT(v))->name);)
return;
case JSVAL_INT:
jsint i;
if (JSVAL_IS_INT(v))
*(jsint*)slot = JSVAL_TO_INT(v);
else if ((tag == JSVAL_DOUBLE) && JSDOUBLE_IS_INT(*JSVAL_TO_DOUBLE(v), i))
*(jsint*)slot = i;
else
JS_ASSERT(JSVAL_IS_INT(v));
debug_only_v(printf("int<%d> ", *(jsint*)slot);)
return;
case JSVAL_DOUBLE:
jsdouble d;
if (JSVAL_IS_INT(v))
d = JSVAL_TO_INT(v);
else
d = *JSVAL_TO_DOUBLE(v);
JS_ASSERT(JSVAL_IS_INT(v) || JSVAL_IS_DOUBLE(v));
*(jsdouble*)slot = d;
debug_only_v(printf("double<%g> ", d);)
return;
case JSVAL_BOXED:
JS_NOT_REACHED("found boxed type in an entry type map");
return;
case JSVAL_STRING:
JS_ASSERT(tag == JSVAL_STRING);
*(JSString**)slot = JSVAL_TO_STRING(v);
debug_only_v(printf("string<%p> ", (void*)(*(JSString**)slot));)
return;
case JSVAL_TNULL:
JS_ASSERT(tag == JSVAL_OBJECT);
*(JSObject**)slot = NULL;
debug_only_v(printf("null ");)
return;
case JSVAL_BOOLEAN:
/* Watch out for pseudo-booleans. */
JS_ASSERT(tag == JSVAL_BOOLEAN);
*(JSBool*)slot = JSVAL_TO_PSEUDO_BOOLEAN(v);
debug_only_v(printf("boolean<%d> ", *(JSBool*)slot);)
return;
case JSVAL_TFUN: {
JS_ASSERT(tag == JSVAL_OBJECT);
JSObject* obj = JSVAL_TO_OBJECT(v);
*(JSObject**)slot = obj;
#ifdef DEBUG
JSFunction* fun = GET_FUNCTION_PRIVATE(cx, obj);
debug_only_v(printf("function<%p:%s> ", (void*) obj,
fun->atom
? JS_GetStringBytes(ATOM_TO_STRING(fun->atom))
: "unnamed");)
#endif
return;
}
}
JS_NOT_REACHED("unexpected type");
}
/* We maintain an emergency pool of doubles so we can recover safely if a trace runs
out of memory (doubles or objects). */
static jsval
AllocateDoubleFromReservedPool(JSContext* cx)
{
JSTraceMonitor* tm = &JS_TRACE_MONITOR(cx);
JS_ASSERT(tm->reservedDoublePoolPtr > tm->reservedDoublePool);
return *--tm->reservedDoublePoolPtr;
}
static bool
js_ReplenishReservedPool(JSContext* cx, JSTraceMonitor* tm)
{
/* We should not be called with a full pool. */
JS_ASSERT((size_t) (tm->reservedDoublePoolPtr - tm->reservedDoublePool) <
MAX_NATIVE_STACK_SLOTS);
/*
* When the GC runs in js_NewDoubleInRootedValue, it resets
* tm->reservedDoublePoolPtr back to tm->reservedDoublePool.
*/
JSRuntime* rt = cx->runtime;
uintN gcNumber = rt->gcNumber;
uintN lastgcNumber = gcNumber;
jsval* ptr = tm->reservedDoublePoolPtr;
while (ptr < tm->reservedDoublePool + MAX_NATIVE_STACK_SLOTS) {
if (!js_NewDoubleInRootedValue(cx, 0.0, ptr))
goto oom;
/* Check if the last call to js_NewDoubleInRootedValue GC'd. */
if (rt->gcNumber != lastgcNumber) {
lastgcNumber = rt->gcNumber;
JS_ASSERT(tm->reservedDoublePoolPtr == tm->reservedDoublePool);
ptr = tm->reservedDoublePool;
/*
* Have we GC'd more than once? We're probably running really
* low on memory, bail now.
*/
if (uintN(rt->gcNumber - gcNumber) > uintN(1))
goto oom;
continue;
}
++ptr;
}
tm->reservedDoublePoolPtr = ptr;
return true;
oom:
/*
* Already massive GC pressure, no need to hold doubles back.
* We won't run any native code anyway.
*/
tm->reservedDoublePoolPtr = tm->reservedDoublePool;
return false;
}
/* Box a value from the native stack back into the jsval format. Integers
that are too large to fit into a jsval are automatically boxed into
heap-allocated doubles. */
static void
NativeToValue(JSContext* cx, jsval& v, uint8 type, double* slot)
{
jsint i;
jsdouble d;
switch (type) {
case JSVAL_OBJECT:
v = OBJECT_TO_JSVAL(*(JSObject**)slot);
JS_ASSERT(JSVAL_TAG(v) == JSVAL_OBJECT); /* if this fails the pointer was not aligned */
JS_ASSERT(v != JSVAL_ERROR_COOKIE); /* don't leak JSVAL_ERROR_COOKIE */
debug_only_v(printf("object<%p:%s> ", (void*)JSVAL_TO_OBJECT(v),
JSVAL_IS_NULL(v)
? "null"
: STOBJ_GET_CLASS(JSVAL_TO_OBJECT(v))->name);)
break;
case JSVAL_INT:
i = *(jsint*)slot;
debug_only_v(printf("int<%d> ", i);)
store_int:
if (INT_FITS_IN_JSVAL(i)) {
v = INT_TO_JSVAL(i);
break;
}
d = (jsdouble)i;
goto store_double;
case JSVAL_DOUBLE:
d = *slot;
debug_only_v(printf("double<%g> ", d);)
if (JSDOUBLE_IS_INT(d, i))
goto store_int;
store_double: {
/* Its not safe to trigger the GC here, so use an emergency heap if we are out of
double boxes. */
if (cx->doubleFreeList) {
#ifdef DEBUG
JSBool ok =
#endif
js_NewDoubleInRootedValue(cx, d, &v);
JS_ASSERT(ok);
return;
}
v = AllocateDoubleFromReservedPool(cx);
JS_ASSERT(JSVAL_IS_DOUBLE(v) && *JSVAL_TO_DOUBLE(v) == 0.0);
*JSVAL_TO_DOUBLE(v) = d;
return;
}
case JSVAL_BOXED:
v = *(jsval*)slot;
JS_ASSERT(v != JSVAL_ERROR_COOKIE); /* don't leak JSVAL_ERROR_COOKIE */
debug_only_v(printf("box<%lx> ", v));
break;
case JSVAL_STRING:
v = STRING_TO_JSVAL(*(JSString**)slot);
JS_ASSERT(JSVAL_TAG(v) == JSVAL_STRING); /* if this fails the pointer was not aligned */
debug_only_v(printf("string<%p> ", *(JSString**)slot);)
break;
case JSVAL_TNULL:
JS_ASSERT(*(JSObject**)slot == NULL);
v = JSVAL_NULL;
debug_only_v(printf("null<%p> ", *(JSObject**)slot));
break;
case JSVAL_BOOLEAN:
/* Watch out for pseudo-booleans. */
v = PSEUDO_BOOLEAN_TO_JSVAL(*(JSBool*)slot);
debug_only_v(printf("boolean<%d> ", *(JSBool*)slot);)
break;
case JSVAL_TFUN: {
JS_ASSERT(HAS_FUNCTION_CLASS(*(JSObject**)slot));
v = OBJECT_TO_JSVAL(*(JSObject**)slot);
#ifdef DEBUG
JSFunction* fun = GET_FUNCTION_PRIVATE(cx, JSVAL_TO_OBJECT(v));
debug_only_v(printf("function<%p:%s> ", (void*)JSVAL_TO_OBJECT(v),
fun->atom
? JS_GetStringBytes(ATOM_TO_STRING(fun->atom))
: "unnamed");)
#endif
break;
}
}
}
/* Attempt to unbox the given list of interned globals onto the native global frame. */
static JS_REQUIRES_STACK void
BuildNativeGlobalFrame(JSContext* cx, unsigned ngslots, uint16* gslots, uint8* mp, double* np)
{
debug_only_v(printf("global: ");)
FORALL_GLOBAL_SLOTS(cx, ngslots, gslots,
ValueToNative(cx, *vp, *mp, np + gslots[n]);
++mp;
);
debug_only_v(printf("\n");)
}
/* Attempt to unbox the given JS frame onto a native frame. */
static JS_REQUIRES_STACK void
BuildNativeStackFrame(JSContext* cx, unsigned callDepth, uint8* mp, double* np)
{
debug_only_v(printf("stack: ");)
FORALL_SLOTS_IN_PENDING_FRAMES(cx, callDepth,
debug_only_v(printf("%s%u=", vpname, vpnum);)
ValueToNative(cx, *vp, *mp, np);
++mp; ++np;
);
debug_only_v(printf("\n");)
}
/* Box the given native frame into a JS frame. This is infallible. */
static JS_REQUIRES_STACK int
FlushNativeGlobalFrame(JSContext* cx, unsigned ngslots, uint16* gslots, uint8* mp, double* np)
{
uint8* mp_base = mp;
FORALL_GLOBAL_SLOTS(cx, ngslots, gslots,
debug_only_v(printf("%s%u=", vpname, vpnum);)
NativeToValue(cx, *vp, *mp, np + gslots[n]);
++mp;
);
debug_only_v(printf("\n");)
return mp - mp_base;
}
/*
* Returns the number of values on the native stack, excluding the innermost
* frame. This walks all FrameInfos on the native frame stack and sums the
* slot usage of each frame.
*/
static int32
StackDepthFromCallStack(InterpState* state, uint32 callDepth)
{
int32 nativeStackFramePos = 0;
// Duplicate native stack layout computation: see VisitFrameSlots header comment.
for (FrameInfo** fip = state->callstackBase; fip < state->rp + callDepth; fip++)
nativeStackFramePos += (*fip)->callerHeight;
return nativeStackFramePos;
}
/*
* Generic function to read upvars on trace.
* T Traits type parameter. Must provide static functions:
* interp_get(fp, slot) Read the value out of an interpreter frame.
* native_slot(argc, slot) Return the position of the desired value in the on-trace
* stack frame (with position 0 being callee).
*
* level Static level of the function containing the upvar definition.
* slot Identifies the value to get. The meaning is defined by the traits type.
* callDepth Call depth of current point relative to trace entry
*/
template<typename T>
uint32 JS_INLINE
js_GetUpvarOnTrace(JSContext* cx, uint32 level, int32 slot, uint32 callDepth, double* result)
{
InterpState* state = cx->interpState;
FrameInfo** fip = state->rp + callDepth;
/*
* First search the FrameInfo call stack for an entry containing our
* upvar, namely one with level == upvarLevel. The first FrameInfo is a
* transition from the entry frame to some callee. However, it is not
* known (from looking at the FrameInfo) whether the entry frame had a
* callee. Rather than special-case this or insert more logic into the
* loop, instead just stop before that FrameInfo (i.e. |> base| instead of
* |>= base|), and let the code after the loop handle it.
*/
int32 stackOffset = StackDepthFromCallStack(state, callDepth);
while (--fip > state->callstackBase) {
FrameInfo* fi = *fip;
/*
* The loop starts aligned to the top of the stack, so move down to the
* first meaningful callee. Then read the callee directly from the frame.
*/
stackOffset -= fi->callerHeight;
JSObject* callee = *(JSObject**)(&state->stackBase[stackOffset]);
JSFunction* fun = GET_FUNCTION_PRIVATE(cx, callee);
uintN calleeLevel = fun->u.i.script->staticLevel;
if (calleeLevel == level) {
/*
* Now find the upvar's value in the native stack. stackOffset is
* the offset of the start of the activation record corresponding
* to *fip in the native stack.
*/
uint32 native_slot = T::native_slot(fi->callerArgc, slot);
*result = state->stackBase[stackOffset + native_slot];
return fi->get_typemap()[native_slot];
}
}
// Next search the trace entry frame, which is not in the FrameInfo stack.
if (state->outermostTree->script->staticLevel == level) {
uint32 argc = ((VMFragment*) state->outermostTree->fragment)->argc;
uint32 native_slot = T::native_slot(argc, slot);
*result = state->stackBase[native_slot];
return state->callstackBase[0]->get_typemap()[native_slot];
}
/*
* If we did not find the upvar in the frames for the active traces,
* then we simply get the value from the interpreter state.
*/
JS_ASSERT(level < JS_DISPLAY_SIZE);
JSStackFrame* fp = cx->display[level];
jsval v = T::interp_get(fp, slot);
uint8 type = getCoercedType(v);
ValueToNative(cx, v, type, result);
return type;
}
// For this traits type, 'slot' is the argument index, which may be -2 for callee.
struct UpvarArgTraits {
static jsval interp_get(JSStackFrame* fp, int32 slot) {
return fp->argv[slot];
}
static uint32 native_slot(uint32 argc, int32 slot) {
return 2 /*callee,this*/ + slot;
}
};
uint32 JS_FASTCALL
js_GetUpvarArgOnTrace(JSContext* cx, uint32 staticLevel, int32 slot, uint32 callDepth, double* result)
{
return js_GetUpvarOnTrace<UpvarArgTraits>(cx, staticLevel, slot, callDepth, result);
}
// For this traits type, 'slot' is an index into the local slots array.
struct UpvarVarTraits {
static jsval interp_get(JSStackFrame* fp, int32 slot) {
return fp->slots[slot];
}
static uint32 native_slot(uint32 argc, int32 slot) {
return 2 /*callee,this*/ + argc + slot;
}
};
uint32 JS_FASTCALL
js_GetUpvarVarOnTrace(JSContext* cx, uint32 staticLevel, int32 slot, uint32 callDepth, double* result)
{
return js_GetUpvarOnTrace<UpvarVarTraits>(cx, staticLevel, slot, callDepth, result);
}
/*
* For this traits type, 'slot' is an index into the stack area (within slots, after nfixed)
* of a frame with no function. (On trace, the top-level frame is the only one that can have
* no function.)
*/
struct UpvarStackTraits {
static jsval interp_get(JSStackFrame* fp, int32 slot) {
return fp->slots[slot + fp->script->nfixed];
}
static uint32 native_slot(uint32 argc, int32 slot) {
/*
* Locals are not imported by the tracer when the frame has no function, so
* we do not add fp->script->nfixed.
*/
JS_ASSERT(argc == 0);
return slot;
}
};
uint32 JS_FASTCALL
js_GetUpvarStackOnTrace(JSContext* cx, uint32 upvarLevel, int32 slot, uint32 callDepth, double* result)
{
return js_GetUpvarOnTrace<UpvarStackTraits>(cx, upvarLevel, slot, callDepth, result);
}
/**
* Box the given native stack frame into the virtual machine stack. This
* is infallible.
*
* @param callDepth the distance between the entry frame into our trace and
* cx->fp when we make this call. If this is not called as a
* result of a nested exit, callDepth is 0.
* @param mp pointer to an array of type tags (JSVAL_INT, etc.) that indicate
* what the types of the things on the stack are.
* @param np pointer to the native stack. We want to copy values from here to
* the JS stack as needed.
* @param stopFrame if non-null, this frame and everything above it should not
* be restored.
* @return the number of things we popped off of np.
*/
static JS_REQUIRES_STACK int
FlushNativeStackFrame(JSContext* cx, unsigned callDepth, uint8* mp, double* np,
JSStackFrame* stopFrame)
{
jsval* stopAt = stopFrame ? &stopFrame->argv[-2] : NULL;
uint8* mp_base = mp;
/* Root all string and object references first (we don't need to call the GC for this). */
FORALL_SLOTS_IN_PENDING_FRAMES(cx, callDepth,
if (vp == stopAt) goto skip;
debug_only_v(printf("%s%u=", vpname, vpnum);)
NativeToValue(cx, *vp, *mp, np);
++mp; ++np
);
skip:
// Restore thisp from the now-restored argv[-1] in each pending frame.
// Keep in mind that we didn't restore frames at stopFrame and above!
// Scope to keep |fp| from leaking into the macros we're using.
{
unsigned n = callDepth+1; // +1 to make sure we restore the entry frame
JSStackFrame* fp = cx->fp;
if (stopFrame) {
for (; fp != stopFrame; fp = fp->down) {
JS_ASSERT(n != 0);
--n;
}
// Skip over stopFrame itself.
JS_ASSERT(n != 0);
--n;
fp = fp->down;
}
for (; n != 0; fp = fp->down) {
--n;
if (fp->callee) {
/*
* We might return from trace with a different callee object, but it still
* has to be the same JSFunction (FIXME: bug 471425, eliminate fp->callee).
*/
JS_ASSERT(JSVAL_IS_OBJECT(fp->argv[-1]));
JS_ASSERT(HAS_FUNCTION_CLASS(JSVAL_TO_OBJECT(fp->argv[-2])));
JS_ASSERT(GET_FUNCTION_PRIVATE(cx, JSVAL_TO_OBJECT(fp->argv[-2])) ==
GET_FUNCTION_PRIVATE(cx, fp->callee));
JS_ASSERT(GET_FUNCTION_PRIVATE(cx, fp->callee) == fp->fun);
fp->callee = JSVAL_TO_OBJECT(fp->argv[-2]);
/*
* SynthesizeFrame sets scopeChain to NULL, because we can't calculate the
* correct scope chain until we have the final callee. Calculate the real
* scope object here.
*/
if (!fp->scopeChain) {
fp->scopeChain = OBJ_GET_PARENT(cx, fp->callee);
if (fp->fun->flags & JSFUN_HEAVYWEIGHT) {
/*
* Set hookData to null because the failure case for js_GetCallObject
* involves it calling the debugger hook.
*
* Allocating the Call object must not fail, so use an object
* previously reserved by js_ExecuteTree if needed.
*/
void* hookData = ((JSInlineFrame*)fp)->hookData;
((JSInlineFrame*)fp)->hookData = NULL;
JS_ASSERT(!JS_TRACE_MONITOR(cx).useReservedObjects);
JS_TRACE_MONITOR(cx).useReservedObjects = JS_TRUE;
#ifdef DEBUG
JSObject *obj =
#endif
js_GetCallObject(cx, fp);
JS_ASSERT(obj);
JS_TRACE_MONITOR(cx).useReservedObjects = JS_FALSE;
((JSInlineFrame*)fp)->hookData = hookData;
}
}
fp->thisp = JSVAL_TO_OBJECT(fp->argv[-1]);
if (fp->flags & JSFRAME_CONSTRUCTING) // constructors always compute 'this'
fp->flags |= JSFRAME_COMPUTED_THIS;
}
}
}
debug_only_v(printf("\n");)
return mp - mp_base;
}
/* Emit load instructions onto the trace that read the initial stack state. */
JS_REQUIRES_STACK void
TraceRecorder::import(LIns* base, ptrdiff_t offset, jsval* p, uint8 t,
const char *prefix, uintN index, JSStackFrame *fp)
{
LIns* ins;
if (t == JSVAL_INT) { /* demoted */
JS_ASSERT(isInt32(*p));
/* Ok, we have a valid demotion attempt pending, so insert an integer
read and promote it to double since all arithmetic operations expect
to see doubles on entry. The first op to use this slot will emit a
f2i cast which will cancel out the i2f we insert here. */
ins = lir->insLoadi(base, offset);
ins = lir->ins1(LIR_i2f, ins);
} else {
JS_ASSERT_IF(t != JSVAL_BOXED, isNumber(*p) == (t == JSVAL_DOUBLE));
if (t == JSVAL_DOUBLE) {
ins = lir->insLoad(LIR_ldq, base, offset);
} else if (t == JSVAL_BOOLEAN) {
ins = lir->insLoad(LIR_ld, base, offset);
} else {
ins = lir->insLoad(LIR_ldp, base, offset);
}
}
checkForGlobalObjectReallocation();
tracker.set(p, ins);
#ifdef DEBUG
char name[64];
JS_ASSERT(strlen(prefix) < 10);
void* mark = NULL;
jsuword* localNames = NULL;
const char* funName = NULL;
if (*prefix == 'a' || *prefix == 'v') {
mark = JS_ARENA_MARK(&cx->tempPool);
if (fp->fun->hasLocalNames())
localNames = js_GetLocalNameArray(cx, fp->fun, &cx->tempPool);
funName = fp->fun->atom ? js_AtomToPrintableString(cx, fp->fun->atom) : "<anonymous>";
}
if (!strcmp(prefix, "argv")) {
if (index < fp->fun->nargs) {
JSAtom *atom = JS_LOCAL_NAME_TO_ATOM(localNames[index]);
JS_snprintf(name, sizeof name, "$%s.%s", funName, js_AtomToPrintableString(cx, atom));
} else {
JS_snprintf(name, sizeof name, "$%s.<arg%d>", funName, index);
}
} else if (!strcmp(prefix, "vars")) {
JSAtom *atom = JS_LOCAL_NAME_TO_ATOM(localNames[fp->fun->nargs + index]);
JS_snprintf(name, sizeof name, "$%s.%s", funName, js_AtomToPrintableString(cx, atom));
} else {
JS_snprintf(name, sizeof name, "$%s%d", prefix, index);
}
if (mark)
JS_ARENA_RELEASE(&cx->tempPool, mark);
addName(ins, name);
static const char* typestr[] = {
"object", "int", "double", "boxed", "string", "null", "boolean", "function"
};
debug_only_v(printf("import vp=%p name=%s type=%s flags=%d\n",
(void*)p, name, typestr[t & 7], t >> 3);)
#endif
}
JS_REQUIRES_STACK void
TraceRecorder::import(TreeInfo* treeInfo, LIns* sp, unsigned stackSlots, unsigned ngslots,
unsigned callDepth, uint8* typeMap)
{
/* If we get a partial list that doesn't have all the types (i.e. recording from a side
exit that was recorded but we added more global slots later), merge the missing types
from the entry type map. This is safe because at the loop edge we verify that we
have compatible types for all globals (entry type and loop edge type match). While
a different trace of the tree might have had a guard with a different type map for
these slots we just filled in here (the guard we continue from didn't know about them),
since we didn't take that particular guard the only way we could have ended up here
is if that other trace had at its end a compatible type distribution with the entry
map. Since thats exactly what we used to fill in the types our current side exit
didn't provide, this is always safe to do. */
uint8* globalTypeMap = typeMap + stackSlots;
unsigned length = treeInfo->nGlobalTypes();
/*
* This is potentially the typemap of the side exit and thus shorter than the tree's
* global type map.
*/
if (ngslots < length) {
mergeTypeMaps(&globalTypeMap/*out param*/, &ngslots/*out param*/,
treeInfo->globalTypeMap(), length,
(uint8*)alloca(sizeof(uint8) * length));
}
JS_ASSERT(ngslots == treeInfo->nGlobalTypes());
/*
* Check whether there are any values on the stack we have to unbox and do that first
* before we waste any time fetching the state from the stack.
*/
ptrdiff_t offset = -treeInfo->nativeStackBase;
uint8* m = typeMap;
FORALL_SLOTS_IN_PENDING_FRAMES(cx, callDepth,
if (*m == JSVAL_BOXED) {
import(sp, offset, vp, JSVAL_BOXED, "boxed", vpnum, cx->fp);
LIns* vp_ins = get(vp);
unbox_jsval(*vp, vp_ins, copy(anchor));
set(vp, vp_ins);
}
m++; offset += sizeof(double);
);
/*
* The first time we compile a tree this will be empty as we add entries lazily.
*/
uint16* gslots = treeInfo->globalSlots->data();
m = globalTypeMap;
FORALL_GLOBAL_SLOTS(cx, ngslots, gslots,
JS_ASSERT(*m != JSVAL_BOXED);
import(lirbuf->state, nativeGlobalOffset(vp), vp, *m, vpname, vpnum, NULL);
m++;
);
offset = -treeInfo->nativeStackBase;
m = typeMap;
FORALL_SLOTS_IN_PENDING_FRAMES(cx, callDepth,
if (*m != JSVAL_BOXED)
import(sp, offset, vp, *m, vpname, vpnum, fp);
m++; offset += sizeof(double);
);
}
JS_REQUIRES_STACK bool
TraceRecorder::isValidSlot(JSScope* scope, JSScopeProperty* sprop)
{
uint32 setflags = (js_CodeSpec[*cx->fp->regs->pc].format & (JOF_SET | JOF_INCDEC | JOF_FOR));
if (setflags) {
if (!SPROP_HAS_STUB_SETTER(sprop))
ABORT_TRACE_RV("non-stub setter", false);
if (sprop->attrs & JSPROP_READONLY)
ABORT_TRACE_RV("writing to a read-only property", false);
}
/* This check applies even when setflags == 0. */
if (setflags != JOF_SET && !SPROP_HAS_STUB_GETTER(sprop))
ABORT_TRACE_RV("non-stub getter", false);
if (!SPROP_HAS_VALID_SLOT(sprop, scope))
ABORT_TRACE_RV("slotless obj property", false);
return true;
}
/* Lazily import a global slot if we don't already have it in the tracker. */
JS_REQUIRES_STACK bool
TraceRecorder::lazilyImportGlobalSlot(unsigned slot)
{
if (slot != uint16(slot)) /* we use a table of 16-bit ints, bail out if that's not enough */
return false;
/*
* If the global object grows too large, alloca in js_ExecuteTree might fail, so
* abort tracing on global objects with unreasonably many slots.
*/
if (STOBJ_NSLOTS(globalObj) > MAX_GLOBAL_SLOTS)
return false;
jsval* vp = &STOBJ_GET_SLOT(globalObj, slot);
if (known(vp))
return true; /* we already have it */
unsigned index = treeInfo->globalSlots->length();
/* Add the slot to the list of interned global slots. */
JS_ASSERT(treeInfo->nGlobalTypes() == treeInfo->globalSlots->length());
treeInfo->globalSlots->add(slot);
uint8 type = getCoercedType(*vp);
if ((type == JSVAL_INT) && oracle.isGlobalSlotUndemotable(cx, slot))
type = JSVAL_DOUBLE;
treeInfo->typeMap.add(type);
import(lirbuf->state, sizeof(struct InterpState) + slot*sizeof(double),
vp, type, "global", index, NULL);
specializeTreesToMissingGlobals(cx, treeInfo);
return true;
}
/* Write back a value onto the stack or global frames. */
LIns*
TraceRecorder::writeBack(LIns* i, LIns* base, ptrdiff_t offset)
{
/* Sink all type casts targeting the stack into the side exit by simply storing the original
(uncasted) value. Each guard generates the side exit map based on the types of the
last stores to every stack location, so its safe to not perform them on-trace. */
if (isPromoteInt(i))
i = ::demote(lir, i);
return lir->insStorei(i, base, offset);
}
/* Update the tracker, then issue a write back store. */
JS_REQUIRES_STACK void
TraceRecorder::set(jsval* p, LIns* i, bool initializing)
{
JS_ASSERT(i != NULL);
JS_ASSERT(initializing || known(p));
checkForGlobalObjectReallocation();
tracker.set(p, i);
/* If we are writing to this location for the first time, calculate the offset into the
native frame manually, otherwise just look up the last load or store associated with
the same source address (p) and use the same offset/base. */
LIns* x = nativeFrameTracker.get(p);
if (!x) {
if (isGlobal(p))
x = writeBack(i, lirbuf->state, nativeGlobalOffset(p));
else
x = writeBack(i, lirbuf->sp, -treeInfo->nativeStackBase + nativeStackOffset(p));
nativeFrameTracker.set(p, x);
} else {
#define ASSERT_VALID_CACHE_HIT(base, offset) \
JS_ASSERT(base == lirbuf->sp || base == lirbuf->state); \
JS_ASSERT(offset == ((base == lirbuf->sp) \
? -treeInfo->nativeStackBase + nativeStackOffset(p) \
: nativeGlobalOffset(p))); \
if (x->isop(LIR_st) || x->isop(LIR_stq)) {
ASSERT_VALID_CACHE_HIT(x->oprnd2(), x->oprnd3()->constval());
writeBack(i, x->oprnd2(), x->oprnd3()->constval());
} else {
JS_ASSERT(x->isop(LIR_sti) || x->isop(LIR_stqi));
ASSERT_VALID_CACHE_HIT(x->oprnd2(), x->immdisp());
writeBack(i, x->oprnd2(), x->immdisp());
}
}
#undef ASSERT_VALID_CACHE_HIT
}
JS_REQUIRES_STACK LIns*
TraceRecorder::get(jsval* p)
{
checkForGlobalObjectReallocation();
return tracker.get(p);
}
JS_REQUIRES_STACK bool
TraceRecorder::known(jsval* p)
{
checkForGlobalObjectReallocation();
return tracker.has(p);
}
/*
* The dslots of the global object are sometimes reallocated by the interpreter.
* This function check for that condition and re-maps the entries of the tracker
* accordingly.
*/
JS_REQUIRES_STACK void
TraceRecorder::checkForGlobalObjectReallocation()
{
if (global_dslots != globalObj->dslots) {
debug_only_v(printf("globalObj->dslots relocated, updating tracker\n");)
jsval* src = global_dslots;
jsval* dst = globalObj->dslots;
jsuint length = globalObj->dslots[-1] - JS_INITIAL_NSLOTS;
LIns** map = (LIns**)alloca(sizeof(LIns*) * length);
for (jsuint n = 0; n < length; ++n) {
map[n] = tracker.get(src);
tracker.set(src++, NULL);
}
for (jsuint n = 0; n < length; ++n)
tracker.set(dst++, map[n]);
global_dslots = globalObj->dslots;
}
}
/* Determine whether the current branch is a loop edge (taken or not taken). */
static JS_REQUIRES_STACK bool
js_IsLoopEdge(jsbytecode* pc, jsbytecode* header)
{
switch (*pc) {
case JSOP_IFEQ:
case JSOP_IFNE:
return ((pc + GET_JUMP_OFFSET(pc)) == header);
case JSOP_IFEQX:
case JSOP_IFNEX:
return ((pc + GET_JUMPX_OFFSET(pc)) == header);
default:
JS_ASSERT((*pc == JSOP_AND) || (*pc == JSOP_ANDX) ||
(*pc == JSOP_OR) || (*pc == JSOP_ORX));
}
return false;
}
/*
* Promote slots if necessary to match the called tree's type map. This function is
* infallible and must only be called if we are certain that it is possible to
* reconcile the types for each slot in the inner and outer trees.
*/
JS_REQUIRES_STACK void
TraceRecorder::adjustCallerTypes(Fragment* f)
{
uint16* gslots = treeInfo->globalSlots->data();
unsigned ngslots = treeInfo->globalSlots->length();
JS_ASSERT(ngslots == treeInfo->nGlobalTypes());
TreeInfo* ti = (TreeInfo*)f->vmprivate;
uint8* map = ti->globalTypeMap();
uint8* m = map;
FORALL_GLOBAL_SLOTS(cx, ngslots, gslots,
LIns* i = get(vp);
bool isPromote = isPromoteInt(i);
if (isPromote && *m == JSVAL_DOUBLE)
lir->insStorei(get(vp), lirbuf->state, nativeGlobalOffset(vp));
JS_ASSERT(!(!isPromote && *m == JSVAL_INT));
++m;
);
JS_ASSERT(unsigned(m - map) == ti->nGlobalTypes());
map = ti->stackTypeMap();
m = map;
FORALL_SLOTS_IN_PENDING_FRAMES(cx, 0,
LIns* i = get(vp);
bool isPromote = isPromoteInt(i);
if (isPromote && *m == JSVAL_DOUBLE) {
lir->insStorei(get(vp), lirbuf->sp,
-treeInfo->nativeStackBase + nativeStackOffset(vp));
/* Aggressively undo speculation so the inner tree will compile if this fails. */
oracle.markStackSlotUndemotable(cx, unsigned(m - map));
}
JS_ASSERT(!(!isPromote && *m == JSVAL_INT));
++m;
);
JS_ASSERT(unsigned(m - map) == ti->nStackTypes);
JS_ASSERT(f == f->root);
}
JS_REQUIRES_STACK uint8
TraceRecorder::determineSlotType(jsval* vp)
{
uint8 m;
LIns* i = get(vp);
if (isNumber(*vp)) {
m = isPromoteInt(i) ? JSVAL_INT : JSVAL_DOUBLE;
} else if (JSVAL_IS_OBJECT(*vp)) {
if (JSVAL_IS_NULL(*vp))
m = JSVAL_TNULL;
else if (HAS_FUNCTION_CLASS(JSVAL_TO_OBJECT(*vp)))
m = JSVAL_TFUN;
else
m = JSVAL_OBJECT;
} else {
m = JSVAL_TAG(*vp);
}
JS_ASSERT((m != JSVAL_INT) || isInt32(*vp));
return m;
}
JS_REQUIRES_STACK VMSideExit*
TraceRecorder::snapshot(ExitType exitType)
{
JSStackFrame* fp = cx->fp;
JSFrameRegs* regs = fp->regs;
jsbytecode* pc = regs->pc;
/* Check for a return-value opcode that needs to restart at the next instruction. */
const JSCodeSpec& cs = js_CodeSpec[*pc];
/*
* When calling a _FAIL native, make the snapshot's pc point to the next
* instruction after the CALL or APPLY. Even on failure, a _FAIL native must not
* be called again from the interpreter.
*/
bool resumeAfter = (pendingTraceableNative &&
JSTN_ERRTYPE(pendingTraceableNative) == FAIL_STATUS);
if (resumeAfter) {
JS_ASSERT(*pc == JSOP_CALL || *pc == JSOP_APPLY || *pc == JSOP_NEW);
pc += cs.length;
regs->pc = pc;
MUST_FLOW_THROUGH("restore_pc");
}
/* Generate the entry map for the (possibly advanced) pc and stash it in the trace. */
unsigned stackSlots = js_NativeStackSlots(cx, callDepth);
/* It's sufficient to track the native stack use here since all stores above the
stack watermark defined by guards are killed. */
trackNativeStackUse(stackSlots + 1);
/* Capture the type map into a temporary location. */
unsigned ngslots = treeInfo->globalSlots->length();
unsigned typemap_size = (stackSlots + ngslots) * sizeof(uint8);
void *mark = JS_ARENA_MARK(&cx->tempPool);
uint8* typemap;
JS_ARENA_ALLOCATE_CAST(typemap, uint8*, &cx->tempPool, typemap_size);
uint8* m = typemap;
/* Determine the type of a store by looking at the current type of the actual value the
interpreter is using. For numbers we have to check what kind of store we used last
(integer or double) to figure out what the side exit show reflect in its typemap. */
FORALL_SLOTS(cx, ngslots, treeInfo->globalSlots->data(), callDepth,
*m++ = determineSlotType(vp);
);
JS_ASSERT(unsigned(m - typemap) == ngslots + stackSlots);
/*
* If we are currently executing a traceable native or we are attaching a second trace
* to it, the value on top of the stack is boxed. Make a note of this in the typemap.
*/
if (pendingTraceableNative && (pendingTraceableNative->flags & JSTN_UNBOX_AFTER))
typemap[stackSlots - 1] = JSVAL_BOXED;
/* Now restore the the original pc (after which early returns are ok). */
if (resumeAfter) {
MUST_FLOW_LABEL(restore_pc);
regs->pc = pc - cs.length;
} else {
/* If we take a snapshot on a goto, advance to the target address. This avoids inner
trees returning on a break goto, which the outer recorder then would confuse with
a break in the outer tree. */
if (*pc == JSOP_GOTO)
pc += GET_JUMP_OFFSET(pc);
else if (*pc == JSOP_GOTOX)
pc += GET_JUMPX_OFFSET(pc);
}
/*
* Check if we already have a matching side exit; if so we can return that
* side exit instead of creating a new one.
*/
VMSideExit** exits = treeInfo->sideExits.data();
unsigned nexits = treeInfo->sideExits.length();
if (exitType == LOOP_EXIT) {
for (unsigned n = 0; n < nexits; ++n) {
VMSideExit* e = exits[n];
if (e->pc == pc && e->imacpc == fp->imacpc &&
ngslots == e->numGlobalSlots &&
!memcmp(getFullTypeMap(exits[n]), typemap, typemap_size)) {
AUDIT(mergedLoopExits);
JS_ARENA_RELEASE(&cx->tempPool, mark);
return e;
}
}
}
if (sizeof(VMSideExit) + (stackSlots + ngslots) * sizeof(uint8) >= MAX_SKIP_BYTES) {
/*
* ::snapshot() is infallible in the sense that callers don't
* expect errors; but this is a trace-aborting error condition. So
* mangle the request to consume zero slots, and mark the tree as
* to-be-trashed. This should be safe as the trace will be aborted
* before assembly or execution due to the call to
* trackNativeStackUse above.
*/
stackSlots = 0;
ngslots = 0;
typemap_size = 0;
trashSelf = true;
}
/* We couldn't find a matching side exit, so create a new one. */
LIns* data = lir->skip(sizeof(VMSideExit) + (stackSlots + ngslots) * sizeof(uint8));
VMSideExit* exit = (VMSideExit*) data->payload();
/* Setup side exit structure. */
memset(exit, 0, sizeof(VMSideExit));
exit->from = fragment;
exit->calldepth = callDepth;
exit->numGlobalSlots = ngslots;
exit->numStackSlots = stackSlots;
exit->numStackSlotsBelowCurrentFrame = cx->fp->callee
? nativeStackOffset(&cx->fp->argv[-2])/sizeof(double)
: 0;
exit->exitType = exitType;
exit->block = fp->blockChain;
exit->pc = pc;
exit->imacpc = fp->imacpc;
exit->sp_adj = (stackSlots * sizeof(double)) - treeInfo->nativeStackBase;
exit->rp_adj = exit->calldepth * sizeof(FrameInfo*);
exit->nativeCalleeWord = 0;
memcpy(getFullTypeMap(exit), typemap, typemap_size);
JS_ARENA_RELEASE(&cx->tempPool, mark);
return exit;
}
JS_REQUIRES_STACK LIns*
TraceRecorder::createGuardRecord(VMSideExit* exit)
{
LIns* guardRec = lir->skip(sizeof(GuardRecord));
GuardRecord* gr = (GuardRecord*) guardRec->payload();
memset(gr, 0, sizeof(GuardRecord));
gr->exit = exit;
exit->addGuard(gr);
return guardRec;
}
/*
* Emit a guard for condition (cond), expecting to evaluate to boolean result
* (expected) and using the supplied side exit if the conditon doesn't hold.
*/
JS_REQUIRES_STACK void
TraceRecorder::guard(bool expected, LIns* cond, VMSideExit* exit)
{
LIns* guardRec = createGuardRecord(exit);
/*
* BIG FAT WARNING: If compilation fails we don't reset the lirbuf, so it's
* safe to keep references to the side exits here. If we ever start
* rewinding those lirbufs, we have to make sure we purge the side exits
* that then no longer will be in valid memory.
*/
if (exit->exitType == LOOP_EXIT)
treeInfo->sideExits.add(exit);
if (!cond->isCond()) {
expected = !expected;
cond = lir->ins_eq0(cond);
}
LIns* guardIns =
lir->insGuard(expected ? LIR_xf : LIR_xt, cond, guardRec);
if (guardIns) {
debug_only_v(printf(" SideExit=%p exitType=%d\n", (void*)exit, exit->exitType);)
} else {
debug_only_v(printf(" redundant guard, eliminated\n");)
}
}
JS_REQUIRES_STACK VMSideExit*
TraceRecorder::copy(VMSideExit* copy)
{
size_t typemap_size = copy->numGlobalSlots + copy->numStackSlots;
LIns* data = lir->skip(sizeof(VMSideExit) +
typemap_size * sizeof(uint8));
VMSideExit* exit = (VMSideExit*) data->payload();
/* Copy side exit structure. */
memcpy(exit, copy, sizeof(VMSideExit) + typemap_size * sizeof(uint8));
exit->guards = NULL;
exit->from = fragment;
exit->target = NULL;
/*
* BIG FAT WARNING: If compilation fails we don't reset the lirbuf, so it's
* safe to keep references to the side exits here. If we ever start
* rewinding those lirbufs, we have to make sure we purge the side exits
* that then no longer will be in valid memory.
*/
if (exit->exitType == LOOP_EXIT)
treeInfo->sideExits.add(exit);
return exit;
}
/* Emit a guard for condition (cond), expecting to evaluate to boolean result (expected)
and generate a side exit with type exitType to jump to if the condition does not hold. */
JS_REQUIRES_STACK void
TraceRecorder::guard(bool expected, LIns* cond, ExitType exitType)
{
guard(expected, cond, snapshot(exitType));
}
/* Try to match the type of a slot to type t. checkType is used to verify that the type of
* values flowing into the loop edge is compatible with the type we expect in the loop header.
*
* @param v Value.
* @param t Typemap entry for value.
* @param stage_val Outparam for set() address.
* @param stage_ins Outparam for set() instruction.
* @param stage_count Outparam for set() buffer count.
* @return True if types are compatible, false otherwise.
*/
JS_REQUIRES_STACK bool
TraceRecorder::checkType(jsval& v, uint8 t, jsval*& stage_val, LIns*& stage_ins,
unsigned& stage_count)
{
if (t == JSVAL_INT) { /* initially all whole numbers cause the slot to be demoted */
debug_only_v(printf("checkType(tag=1, t=%d, isnum=%d, i2f=%d) stage_count=%d\n",
t,
isNumber(v),
isPromoteInt(get(&v)),
stage_count);)
if (!isNumber(v))
return false; /* not a number? type mismatch */
LIns* i = get(&v);
/* This is always a type mismatch, we can't close a double to an int. */
if (!isPromoteInt(i))
return false;
/* Looks good, slot is an int32, the last instruction should be promotable. */
JS_ASSERT(isInt32(v) && isPromoteInt(i));
/* Overwrite the value in this slot with the argument promoted back to an integer. */
stage_val = &v;
stage_ins = f2i(i);
stage_count++;
return true;
}
if (t == JSVAL_DOUBLE) {
debug_only_v(printf("checkType(tag=2, t=%d, isnum=%d, promote=%d) stage_count=%d\n",
t,
isNumber(v),
isPromoteInt(get(&v)),
stage_count);)
if (!isNumber(v))
return false; /* not a number? type mismatch */
LIns* i = get(&v);
/* We sink i2f conversions into the side exit, but at the loop edge we have to make
sure we promote back to double if at loop entry we want a double. */
if (isPromoteInt(i)) {
stage_val = &v;
stage_ins = lir->ins1(LIR_i2f, i);
stage_count++;
}
return true;
}
if (t == JSVAL_TNULL)
return JSVAL_IS_NULL(v);
if (t == JSVAL_TFUN)
return !JSVAL_IS_PRIMITIVE(v) && HAS_FUNCTION_CLASS(JSVAL_TO_OBJECT(v));
if (t == JSVAL_OBJECT)
return !JSVAL_IS_PRIMITIVE(v) && !HAS_FUNCTION_CLASS(JSVAL_TO_OBJECT(v));
/* for non-number types we expect a precise match of the type */
uint8 vt = getCoercedType(v);
#ifdef DEBUG
if (vt != t) {
debug_only_v(printf("Type mismatch: val %c, map %c ", typeChar[vt],
typeChar[t]);)
}
#endif
debug_only_v(printf("checkType(vt=%d, t=%d) stage_count=%d\n",
(int) vt, t, stage_count);)
return vt == t;
}
/**
* Make sure that the current values in the given stack frame and all stack frames
* up and including entryFrame are type-compatible with the entry map.
*
* @param root_peer First fragment in peer list.
* @param stable_peer Outparam for first type stable peer.
* @param demote True if stability was achieved through demotion.
* @return True if type stable, false otherwise.
*/
JS_REQUIRES_STACK bool
TraceRecorder::deduceTypeStability(Fragment* root_peer, Fragment** stable_peer, bool& demote)
{
uint8* m;
uint8* typemap;
unsigned ngslots = treeInfo->globalSlots->length();
uint16* gslots = treeInfo->globalSlots->data();
JS_ASSERT(ngslots == treeInfo->nGlobalTypes());
if (stable_peer)
*stable_peer = NULL;
/*
* Rather than calculate all of this stuff twice, it gets cached locally. The "stage" buffers
* are for calls to set() that will change the exit types.
*/
bool success;
unsigned stage_count;
jsval** stage_vals = (jsval**)alloca(sizeof(jsval*) * (treeInfo->typeMap.length()));
LIns** stage_ins = (LIns**)alloca(sizeof(LIns*) * (treeInfo->typeMap.length()));
/* First run through and see if we can close ourselves - best case! */
stage_count = 0;
success = false;
debug_only_v(printf("Checking type stability against self=%p\n", (void*)fragment);)
m = typemap = treeInfo->globalTypeMap();
FORALL_GLOBAL_SLOTS(cx, ngslots, gslots,
debug_only_v(printf("%s%d ", vpname, vpnum);)
if (!checkType(*vp, *m, stage_vals[stage_count], stage_ins[stage_count], stage_count)) {
/* If the failure was an int->double, tell the oracle. */
if (*m == JSVAL_INT && isNumber(*vp) && !isPromoteInt(get(vp))) {
oracle.markGlobalSlotUndemotable(cx, gslots[n]);
demote = true;
} else {
goto checktype_fail_1;
}
}
++m;
);
m = typemap = treeInfo->stackTypeMap();
FORALL_SLOTS_IN_PENDING_FRAMES(cx, 0,
debug_only_v(printf("%s%d ", vpname, vpnum);)
if (!checkType(*vp, *m, stage_vals[stage_count], stage_ins[stage_count], stage_count)) {
if (*m == JSVAL_INT && isNumber(*vp) && !isPromoteInt(get(vp))) {
oracle.markStackSlotUndemotable(cx, unsigned(m - typemap));
demote = true;
} else {
goto checktype_fail_1;
}
}
++m;
);
success = true;
checktype_fail_1:
/* If we got a success and we don't need to recompile, we should just close here. */
if (success && !demote) {
for (unsigned i = 0; i < stage_count; i++)
set(stage_vals[i], stage_ins[i]);
return true;
/* If we need to trash, don't bother checking peers. */
} else if (trashSelf) {
return false;
}
demote = false;
/* At this point the tree is about to be incomplete, so let's see if we can connect to any
* peer fragment that is type stable.
*/
Fragment* f;
TreeInfo* ti;
for (f = root_peer; f != NULL; f = f->peer) {
debug_only_v(printf("Checking type stability against peer=%p (code=%p)\n", (void*)f, f->code());)
if (!f->code())
continue;
ti = (TreeInfo*)f->vmprivate;
/* Don't allow varying stack depths */
if ((ti->nStackTypes != treeInfo->nStackTypes) ||
(ti->typeMap.length() != treeInfo->typeMap.length()) ||
(ti->globalSlots->length() != treeInfo->globalSlots->length()))
continue;
stage_count = 0;
success = false;
m = ti->globalTypeMap();
FORALL_GLOBAL_SLOTS(cx, treeInfo->globalSlots->length(), treeInfo->globalSlots->data(),
if (!checkType(*vp, *m, stage_vals[stage_count], stage_ins[stage_count], stage_count))
goto checktype_fail_2;
++m;
);
m = ti->stackTypeMap();
FORALL_SLOTS_IN_PENDING_FRAMES(cx, 0,
if (!checkType(*vp, *m, stage_vals[stage_count], stage_ins[stage_count], stage_count))
goto checktype_fail_2;
++m;
);
success = true;
checktype_fail_2:
if (success) {
/*
* There was a successful match. We don't care about restoring the saved staging, but
* we do need to clear the original undemote list.
*/
for (unsigned i = 0; i < stage_count; i++)
set(stage_vals[i], stage_ins[i]);
if (stable_peer)
*stable_peer = f;
demote = false;
return false;
}
}
/*
* If this is a loop trace and it would be stable with demotions, build an undemote list
* and return true. Our caller should sniff this and trash the tree, recording a new one
* that will assumedly stabilize.
*/
if (demote && fragment->kind == LoopTrace) {
typemap = m = treeInfo->globalTypeMap();
FORALL_GLOBAL_SLOTS(cx, treeInfo->globalSlots->length(), treeInfo->globalSlots->data(),
if (*m == JSVAL_INT) {
JS_ASSERT(isNumber(*vp));
if (!isPromoteInt(get(vp)))
oracle.markGlobalSlotUndemotable(cx, gslots[n]);
} else if (*m == JSVAL_DOUBLE) {
JS_ASSERT(isNumber(*vp));
oracle.markGlobalSlotUndemotable(cx, gslots[n]);
} else {
JS_ASSERT(*m == JSVAL_TAG(*vp));
}
m++;
);
typemap = m = treeInfo->stackTypeMap();
FORALL_SLOTS_IN_PENDING_FRAMES(cx, 0,
if (*m == JSVAL_INT) {
JS_ASSERT(isNumber(*vp));
if (!isPromoteInt(get(vp)))
oracle.markStackSlotUndemotable(cx, unsigned(m - typemap));
} else if (*m == JSVAL_DOUBLE) {
JS_ASSERT(isNumber(*vp));
oracle.markStackSlotUndemotable(cx, unsigned(m - typemap));
} else {
JS_ASSERT((*m == JSVAL_TNULL)
? JSVAL_IS_NULL(*vp)
: *m == JSVAL_TFUN
? !JSVAL_IS_PRIMITIVE(*vp) && HAS_FUNCTION_CLASS(JSVAL_TO_OBJECT(*vp))
: *m == JSVAL_TAG(*vp));
}
m++;
);
return true;
} else {
demote = false;
}
return false;
}
static JS_REQUIRES_STACK void
FlushJITCache(JSContext* cx)
{
if (!TRACING_ENABLED(cx))
return;
JSTraceMonitor* tm = &JS_TRACE_MONITOR(cx);
debug_only_v(printf("Flushing cache.\n");)
if (tm->recorder)
js_AbortRecording(cx, "flush cache");
TraceRecorder* tr;
while ((tr = tm->abortStack) != NULL) {
tr->removeFragmentoReferences();
tr->deepAbort();
tr->popAbortStack();
}
Fragmento* fragmento = tm->fragmento;
if (fragmento) {
if (tm->prohibitFlush) {
debug_only_v(printf("Deferring fragmento flush due to deep bail.\n");)
tm->needFlush = JS_TRUE;
return;
}
fragmento->clearFrags();
#ifdef DEBUG
JS_ASSERT(fragmento->labels);
fragmento->labels->clear();
#endif
tm->lirbuf->rewind();
for (size_t i = 0; i < FRAGMENT_TABLE_SIZE; ++i) {
VMFragment* f = tm->vmfragments[i];
while (f) {
VMFragment* next = f->next;
fragmento->clearFragment(f);
f = next;
}
tm->vmfragments[i] = NULL;
}
for (size_t i = 0; i < MONITOR_N_GLOBAL_STATES; ++i) {
tm->globalStates[i].globalShape = -1;
tm->globalStates[i].globalSlots->clear();
}
}
tm->needFlush = JS_FALSE;
}
/* Compile the current fragment. */
JS_REQUIRES_STACK void
TraceRecorder::compile(JSTraceMonitor* tm)
{
if (tm->needFlush) {
FlushJITCache(cx);
return;
}
Fragmento* fragmento = tm->fragmento;
if (treeInfo->maxNativeStackSlots >= MAX_NATIVE_STACK_SLOTS) {
debug_only_v(printf("Blacklist: excessive stack use.\n"));
js_Blacklist((jsbytecode*) fragment->root->ip);
return;
}
if (anchor && anchor->exitType != CASE_EXIT)
++treeInfo->branchCount;
if (lirbuf->outOMem()) {
fragmento->assm()->setError(nanojit::OutOMem);
return;
}
::compile(fragmento->assm(), fragment);
if (fragmento->assm()->error() == nanojit::OutOMem)
return;
if (fragmento->assm()->error() != nanojit::None) {
debug_only_v(printf("Blacklisted: error during compilation\n");)
js_Blacklist((jsbytecode*) fragment->root->ip);
return;
}
js_resetRecordingAttempts(cx, (jsbytecode*) fragment->ip);
js_resetRecordingAttempts(cx, (jsbytecode*) fragment->root->ip);
if (anchor) {
#ifdef NANOJIT_IA32
if (anchor->exitType == CASE_EXIT)
fragmento->assm()->patch(anchor, anchor->switchInfo);
else
#endif
fragmento->assm()->patch(anchor);
}
JS_ASSERT(fragment->code());
JS_ASSERT(!fragment->vmprivate);
if (fragment == fragment->root)
fragment->vmprivate = treeInfo;
/* :TODO: windows support */
#if defined DEBUG && !defined WIN32
const char* filename = cx->fp->script->filename;
char* label = (char*)malloc((filename ? strlen(filename) : 7) + 16);
sprintf(label, "%s:%u", filename ? filename : "<stdin>",
js_FramePCToLineNumber(cx, cx->fp));
fragmento->labels->add(fragment, sizeof(Fragment), 0, label);
free(label);
#endif
AUDIT(traceCompleted);
}
static bool
js_JoinPeersIfCompatible(Fragmento* frago, Fragment* stableFrag, TreeInfo* stableTree,
VMSideExit* exit)
{
JS_ASSERT(exit->numStackSlots == stableTree->nStackTypes);
/* Must have a matching type unstable exit. */
if ((exit->numGlobalSlots + exit->numStackSlots != stableTree->typeMap.length()) ||
memcmp(getFullTypeMap(exit), stableTree->typeMap.data(), stableTree->typeMap.length())) {
return false;
}
exit->target = stableFrag;
frago->assm()->patch(exit);
stableTree->dependentTrees.addUnique(exit->from->root);
((TreeInfo*)exit->from->root->vmprivate)->linkedTrees.addUnique(stableFrag);
return true;
}
/* Complete and compile a trace and link it to the existing tree if appropriate. */
JS_REQUIRES_STACK void
TraceRecorder::closeLoop(JSTraceMonitor* tm, bool& demote)
{
/*
* We should have arrived back at the loop header, and hence we don't want to be in an imacro
* here and the opcode should be either JSOP_LOOP, or in case this loop was blacklisted in the
* meantime JSOP_NOP.
*/
JS_ASSERT((*cx->fp->regs->pc == JSOP_LOOP || *cx->fp->regs->pc == JSOP_NOP) && !cx->fp->imacpc);
bool stable;
Fragment* peer;
VMFragment* peer_root;
Fragmento* fragmento = tm->fragmento;
if (callDepth != 0) {
debug_only_v(printf("Blacklisted: stack depth mismatch, possible recursion.\n");)
js_Blacklist((jsbytecode*) fragment->root->ip);
trashSelf = true;
return;
}
VMSideExit* exit = snapshot(UNSTABLE_LOOP_EXIT);
JS_ASSERT(exit->numStackSlots == treeInfo->nStackTypes);
VMFragment* root = (VMFragment*)fragment->root;
peer_root = getLoop(traceMonitor, root->ip, root->globalObj, root->globalShape, root->argc);
JS_ASSERT(peer_root != NULL);
stable = deduceTypeStability(peer_root, &peer, demote);
#if DEBUG
if (!stable)
AUDIT(unstableLoopVariable);
#endif
if (trashSelf) {
debug_only_v(printf("Trashing tree from type instability.\n");)
return;
}
if (stable && demote) {
JS_ASSERT(fragment->kind == LoopTrace);
return;
}
if (!stable) {
fragment->lastIns = lir->insGuard(LIR_x, lir->insImm(1), createGuardRecord(exit));
/*
* If we didn't find a type stable peer, we compile the loop anyway and
* hope it becomes stable later.
*/
if (!peer) {
/*
* If such a fragment does not exist, let's compile the loop ahead
* of time anyway. Later, if the loop becomes type stable, we will
* connect these two fragments together.
*/
debug_only_v(printf("Trace has unstable loop variable with no stable peer, "
"compiling anyway.\n");)
UnstableExit* uexit = new UnstableExit;
uexit->fragment = fragment;
uexit->exit = exit;
uexit->next = treeInfo->unstableExits;
treeInfo->unstableExits = uexit;
} else {
JS_ASSERT(peer->code());
exit->target = peer;
debug_only_v(printf("Joining type-unstable trace to target fragment %p.\n", (void*)peer);)
stable = true;
((TreeInfo*)peer->vmprivate)->dependentTrees.addUnique(fragment->root);
treeInfo->linkedTrees.addUnique(peer);
}
} else {
exit->target = fragment->root;
fragment->lastIns = lir->insGuard(LIR_loop, lir->insImm(1), createGuardRecord(exit));
}
compile(tm);
if (fragmento->assm()->error() != nanojit::None)
return;
joinEdgesToEntry(fragmento, peer_root);
debug_only_v(printf("updating specializations on dependent and linked trees\n"))
if (fragment->root->vmprivate)
specializeTreesToMissingGlobals(cx, (TreeInfo*)fragment->root->vmprivate);
/*
* If this is a newly formed tree, and the outer tree has not been compiled yet, we
* should try to compile the outer tree again.
*/
if (outer)
js_AttemptCompilation(cx, tm, globalObj, outer, outerArgc);
debug_only_v(printf("recording completed at %s:%u@%u via closeLoop\n",
cx->fp->script->filename,
js_FramePCToLineNumber(cx, cx->fp),
FramePCOffset(cx->fp));)
}
JS_REQUIRES_STACK void
TraceRecorder::joinEdgesToEntry(Fragmento* fragmento, VMFragment* peer_root)
{
if (fragment->kind == LoopTrace) {
TreeInfo* ti;
Fragment* peer;
uint8* t1, *t2;
UnstableExit* uexit, **unext;
uint32* stackDemotes = (uint32*)alloca(sizeof(uint32) * treeInfo->nStackTypes);
uint32* globalDemotes = (uint32*)alloca(sizeof(uint32) * treeInfo->nGlobalTypes());
for (peer = peer_root; peer != NULL; peer = peer->peer) {
if (!peer->code())
continue;
ti = (TreeInfo*)peer->vmprivate;
uexit = ti->unstableExits;
unext = &ti->unstableExits;
while (uexit != NULL) {
bool remove = js_JoinPeersIfCompatible(fragmento, fragment, treeInfo, uexit->exit);
JS_ASSERT(!remove || fragment != peer);
debug_only_v(if (remove) {
printf("Joining type-stable trace to target exit %p->%p.\n",
(void*)uexit->fragment, (void*)uexit->exit); });
if (!remove) {
/* See if this exit contains mismatch demotions, which imply trashing a tree.
This is actually faster than trashing the original tree as soon as the
instability is detected, since we could have compiled a fairly stable
tree that ran faster with integers. */
unsigned stackCount = 0;
unsigned globalCount = 0;
t1 = treeInfo->stackTypeMap();
t2 = getStackTypeMap(uexit->exit);
for (unsigned i = 0; i < uexit->exit->numStackSlots; i++) {
if (t2[i] == JSVAL_INT && t1[i] == JSVAL_DOUBLE) {
stackDemotes[stackCount++] = i;
} else if (t2[i] != t1[i]) {
stackCount = 0;
break;
}
}
t1 = treeInfo->globalTypeMap();
t2 = getGlobalTypeMap(uexit->exit);
for (unsigned i = 0; i < uexit->exit->numGlobalSlots; i++) {
if (t2[i] == JSVAL_INT && t1[i] == JSVAL_DOUBLE) {
globalDemotes[globalCount++] = i;
} else if (t2[i] != t1[i]) {
globalCount = 0;
stackCount = 0;
break;
}
}
if (stackCount || globalCount) {
for (unsigned i = 0; i < stackCount; i++)
oracle.markStackSlotUndemotable(cx, stackDemotes[i]);
for (unsigned i = 0; i < globalCount; i++)
oracle.markGlobalSlotUndemotable(cx, ti->globalSlots->data()[globalDemotes[i]]);
JS_ASSERT(peer == uexit->fragment->root);
if (fragment == peer)
trashSelf = true;
else
whichTreesToTrash.addUnique(uexit->fragment->root);
break;
}
}
if (remove) {
*unext = uexit->next;
delete uexit;
uexit = *unext;
} else {
unext = &uexit->next;
uexit = uexit->next;
}
}
}
}
debug_only_v(js_DumpPeerStability(traceMonitor, peer_root->ip, peer_root->globalObj,
peer_root->globalShape, peer_root->argc);)
}
/* Emit an always-exit guard and compile the tree (used for break statements. */
JS_REQUIRES_STACK void
TraceRecorder::endLoop(JSTraceMonitor* tm)
{
if (callDepth != 0) {
debug_only_v(printf("Blacklisted: stack depth mismatch, possible recursion.\n");)
js_Blacklist((jsbytecode*) fragment->root->ip);
trashSelf = true;
return;
}
fragment->lastIns =
lir->insGuard(LIR_x, lir->insImm(1), createGuardRecord(snapshot(LOOP_EXIT)));
compile(tm);
if (tm->fragmento->assm()->error() != nanojit::None)
return;
VMFragment* root = (VMFragment*)fragment->root;
joinEdgesToEntry(tm->fragmento, getLoop(tm, root->ip, root->globalObj, root->globalShape, root->argc));
/* Note: this must always be done, in case we added new globals on trace and haven't yet
propagated those to linked and dependent trees. */
debug_only_v(printf("updating specializations on dependent and linked trees\n"))
if (fragment->root->vmprivate)
specializeTreesToMissingGlobals(cx, (TreeInfo*)fragment->root->vmprivate);
/*
* If this is a newly formed tree, and the outer tree has not been compiled yet, we
* should try to compile the outer tree again.
*/
if (outer)
js_AttemptCompilation(cx, tm, globalObj, outer, outerArgc);
debug_only_v(printf("recording completed at %s:%u@%u via endLoop\n",
cx->fp->script->filename,
js_FramePCToLineNumber(cx, cx->fp),
FramePCOffset(cx->fp));)
}
/* Emit code to adjust the stack to match the inner tree's stack expectations. */
JS_REQUIRES_STACK void
TraceRecorder::prepareTreeCall(Fragment* inner)
{
TreeInfo* ti = (TreeInfo*)inner->vmprivate;
inner_sp_ins = lirbuf->sp;
/* The inner tree expects to be called from the current frame. If the outer tree (this
trace) is currently inside a function inlining code (calldepth > 0), we have to advance
the native stack pointer such that we match what the inner trace expects to see. We
move it back when we come out of the inner tree call. */
if (callDepth > 0) {
/* Calculate the amount we have to lift the native stack pointer by to compensate for
any outer frames that the inner tree doesn't expect but the outer tree has. */
ptrdiff_t sp_adj = nativeStackOffset(&cx->fp->argv[-2]);
/* Calculate the amount we have to lift the call stack by */
ptrdiff_t rp_adj = callDepth * sizeof(FrameInfo*);
/* Guard that we have enough stack space for the tree we are trying to call on top
of the new value for sp. */
debug_only_v(printf("sp_adj=%d outer=%d inner=%d\n",
sp_adj, treeInfo->nativeStackBase, ti->nativeStackBase));
LIns* sp_top = lir->ins2i(LIR_piadd, lirbuf->sp,
- treeInfo->nativeStackBase /* rebase sp to beginning of outer tree's stack */
+ sp_adj /* adjust for stack in outer frame inner tree can't see */
+ ti->maxNativeStackSlots * sizeof(double)); /* plus the inner tree's stack */
guard(true, lir->ins2(LIR_lt, sp_top, eos_ins), OOM_EXIT);
/* Guard that we have enough call stack space. */
LIns* rp_top = lir->ins2i(LIR_piadd, lirbuf->rp, rp_adj +
ti->maxCallDepth * sizeof(FrameInfo*));
guard(true, lir->ins2(LIR_lt, rp_top, eor_ins), OOM_EXIT);
/* We have enough space, so adjust sp and rp to their new level. */
lir->insStorei(inner_sp_ins = lir->ins2i(LIR_piadd, lirbuf->sp,
- treeInfo->nativeStackBase /* rebase sp to beginning of outer tree's stack */
+ sp_adj /* adjust for stack in outer frame inner tree can't see */
+ ti->nativeStackBase), /* plus the inner tree's stack base */
lirbuf->state, offsetof(InterpState, sp));
lir->insStorei(lir->ins2i(LIR_piadd, lirbuf->rp, rp_adj),
lirbuf->state, offsetof(InterpState, rp));
}
}
/* Record a call to an inner tree. */
JS_REQUIRES_STACK void
TraceRecorder::emitTreeCall(Fragment* inner, VMSideExit* exit)
{
TreeInfo* ti = (TreeInfo*)inner->vmprivate;
/* Invoke the inner tree. */
LIns* args[] = { INS_CONSTPTR(inner), lirbuf->state }; /* reverse order */
LIns* ret = lir->insCall(&js_CallTree_ci, args);
/* Read back all registers, in case the called tree changed any of them. */
JS_ASSERT(!memchr(getGlobalTypeMap(exit), JSVAL_BOXED, exit->numGlobalSlots) &&
!memchr(getStackTypeMap(exit), JSVAL_BOXED, exit->numStackSlots));
/* bug 502604 - It is illegal to extend from the outer typemap without first extending from the
* inner. Make a new typemap here.
*/
TypeMap fullMap;
fullMap.add(getStackTypeMap(exit), exit->numStackSlots);
fullMap.add(getGlobalTypeMap(exit), exit->numGlobalSlots);
TreeInfo* innerTree = (TreeInfo*)exit->from->root->vmprivate;
if (exit->numGlobalSlots < innerTree->nGlobalTypes()) {
fullMap.add(innerTree->globalTypeMap() + exit->numGlobalSlots,
innerTree->nGlobalTypes() - exit->numGlobalSlots);
}
import(ti, inner_sp_ins, exit->numStackSlots, fullMap.length() - exit->numStackSlots,
exit->calldepth, fullMap.data());
/* Restore sp and rp to their original values (we still have them in a register). */
if (callDepth > 0) {
lir->insStorei(lirbuf->sp, lirbuf->state, offsetof(InterpState, sp));
lir->insStorei(lirbuf->rp, lirbuf->state, offsetof(InterpState, rp));
}
/*
* Guard that we come out of the inner tree along the same side exit we came out when
* we called the inner tree at recording time.
*/
guard(true, lir->ins2(LIR_eq, ret, INS_CONSTPTR(exit)), NESTED_EXIT);
/* Register us as a dependent tree of the inner tree. */
((TreeInfo*)inner->vmprivate)->dependentTrees.addUnique(fragment->root);
treeInfo->linkedTrees.addUnique(inner);
}
/* Add a if/if-else control-flow merge point to the list of known merge points. */
JS_REQUIRES_STACK void
TraceRecorder::trackCfgMerges(jsbytecode* pc)
{
/* If we hit the beginning of an if/if-else, then keep track of the merge point after it. */
JS_ASSERT((*pc == JSOP_IFEQ) || (*pc == JSOP_IFEQX));
jssrcnote* sn = js_GetSrcNote(cx->fp->script, pc);
if (sn != NULL) {
if (SN_TYPE(sn) == SRC_IF) {
cfgMerges.add((*pc == JSOP_IFEQ)
? pc + GET_JUMP_OFFSET(pc)
: pc + GET_JUMPX_OFFSET(pc));
} else if (SN_TYPE(sn) == SRC_IF_ELSE)
cfgMerges.add(pc + js_GetSrcNoteOffset(sn, 0));
}
}
/* Invert the direction of the guard if this is a loop edge that is not
taken (thin loop). */
JS_REQUIRES_STACK void
TraceRecorder::emitIf(jsbytecode* pc, bool cond, LIns* x)
{
ExitType exitType;
if (js_IsLoopEdge(pc, (jsbytecode*)fragment->root->ip)) {
exitType = LOOP_EXIT;
/*
* If we are about to walk out of the loop, generate code for the inverse loop
* condition, pretending we recorded the case that stays on trace.
*/
if ((*pc == JSOP_IFEQ || *pc == JSOP_IFEQX) == cond) {
JS_ASSERT(*pc == JSOP_IFNE || *pc == JSOP_IFNEX || *pc == JSOP_IFEQ || *pc == JSOP_IFEQX);
debug_only_v(printf("Walking out of the loop, terminating it anyway.\n");)
cond = !cond;
}
/*
* Conditional guards do not have to be emitted if the condition is constant. We
* make a note whether the loop condition is true or false here, so we later know
* whether to emit a loop edge or a loop end.
*/
if (x->isconst()) {
loop = (x->constval() == cond);
return;
}
} else {
exitType = BRANCH_EXIT;
}
if (!x->isconst())
guard(cond, x, exitType);
}
/* Emit code for a fused IFEQ/IFNE. */
JS_REQUIRES_STACK void
TraceRecorder::fuseIf(jsbytecode* pc, bool cond, LIns* x)
{
if (*pc == JSOP_IFEQ || *pc == JSOP_IFNE) {
emitIf(pc, cond, x);
if (*pc == JSOP_IFEQ)
trackCfgMerges(pc);
}
}
/* Check whether we have reached the end of the trace. */
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::checkTraceEnd(jsbytecode *pc)
{
if (js_IsLoopEdge(pc, (jsbytecode*)fragment->root->ip)) {
/*
* If we compile a loop, the trace should have a zero stack balance at the loop
* edge. Currently we are parked on a comparison op or IFNE/IFEQ, so advance
* pc to the loop header and adjust the stack pointer and pretend we have
* reached the loop header.
*/
if (loop) {
JS_ASSERT(!cx->fp->imacpc && (pc == cx->fp->regs->pc || pc == cx->fp->regs->pc + 1));
bool fused = pc != cx->fp->regs->pc;
JSFrameRegs orig = *cx->fp->regs;
cx->fp->regs->pc = (jsbytecode*)fragment->root->ip;
cx->fp->regs->sp -= fused ? 2 : 1;
bool demote = false;
closeLoop(traceMonitor, demote);
*cx->fp->regs = orig;
/*
* If compiling this loop generated new oracle information which will likely
* lead to a different compilation result, immediately trigger another
* compiler run. This is guaranteed to converge since the oracle only
* accumulates adverse information but never drops it (except when we
* flush it during garbage collection.)
*/
if (demote)
js_AttemptCompilation(cx, traceMonitor, globalObj, outer, outerArgc);
} else {
endLoop(traceMonitor);
}
return JSRS_STOP;
}
return JSRS_CONTINUE;
}
bool
TraceRecorder::hasMethod(JSObject* obj, jsid id)
{
if (!obj)
return false;
JSObject* pobj;
JSProperty* prop;
int protoIndex = OBJ_LOOKUP_PROPERTY(cx, obj, id, &pobj, &prop);
if (protoIndex < 0 || !prop)
return false;
bool found = false;
if (OBJ_IS_NATIVE(pobj)) {
JSScope* scope = OBJ_SCOPE(pobj);
JSScopeProperty* sprop = (JSScopeProperty*) prop;
if (SPROP_HAS_STUB_GETTER(sprop) &&
SPROP_HAS_VALID_SLOT(sprop, scope)) {
jsval v = LOCKED_OBJ_GET_SLOT(pobj, sprop->slot);
if (VALUE_IS_FUNCTION(cx, v)) {
found = true;
if (!SCOPE_IS_BRANDED(scope)) {
js_MakeScopeShapeUnique(cx, scope);
SCOPE_SET_BRANDED(scope);
}
}
}
}
OBJ_DROP_PROPERTY(cx, pobj, prop);
return found;
}
JS_REQUIRES_STACK bool
TraceRecorder::hasIteratorMethod(JSObject* obj)
{
JS_ASSERT(cx->fp->regs->sp + 2 <= cx->fp->slots + cx->fp->script->nslots);
return hasMethod(obj, ATOM_TO_JSID(cx->runtime->atomState.iteratorAtom));
}
int
nanojit::StackFilter::getTop(LInsp guard)
{
VMSideExit* e = (VMSideExit*)guard->record()->exit;
if (sp == lirbuf->sp)
return e->sp_adj;
JS_ASSERT(sp == lirbuf->rp);
return e->rp_adj;
}
#if defined NJ_VERBOSE
void
nanojit::LirNameMap::formatGuard(LIns *i, char *out)
{
VMSideExit *x;
x = (VMSideExit *)i->record()->exit;
sprintf(out,
"%s: %s %s -> pc=%p imacpc=%p sp%+ld rp%+ld",
formatRef(i),
lirNames[i->opcode()],
i->oprnd1()->isCond() ? formatRef(i->oprnd1()) : "",
(void *)x->pc,
(void *)x->imacpc,
(long int)x->sp_adj,
(long int)x->rp_adj);
}
#endif
void
nanojit::Fragment::onDestroy()
{
delete (TreeInfo *)vmprivate;
}
static JS_REQUIRES_STACK bool
js_DeleteRecorder(JSContext* cx)
{
JSTraceMonitor* tm = &JS_TRACE_MONITOR(cx);
/* Aborting and completing a trace end up here. */
delete tm->recorder;
tm->recorder = NULL;
/*
* If we ran out of memory, flush the code cache.
*/
if (JS_TRACE_MONITOR(cx).fragmento->assm()->error() == OutOMem ||
js_OverfullFragmento(tm, tm->fragmento)) {
FlushJITCache(cx);
return false;
}
return true;
}
/**
* Checks whether the shape of the global object has changed.
*/
static JS_REQUIRES_STACK bool
CheckGlobalObjectShape(JSContext* cx, JSTraceMonitor* tm, JSObject* globalObj,
uint32 *shape=NULL, SlotList** slots=NULL)
{
if (tm->needFlush) {
FlushJITCache(cx);
return false;
}
if (STOBJ_NSLOTS(globalObj) > MAX_GLOBAL_SLOTS)
return false;
uint32 globalShape = OBJ_SHAPE(globalObj);
if (tm->recorder) {
VMFragment* root = (VMFragment*)tm->recorder->getFragment()->root;
TreeInfo* ti = tm->recorder->getTreeInfo();
/* Check the global shape matches the recorder's treeinfo's shape. */
if (globalObj != root->globalObj || globalShape != root->globalShape) {
AUDIT(globalShapeMismatchAtEntry);
debug_only_v(printf("Global object/shape mismatch (%p/%u vs. %p/%u), flushing cache.\n",
(void*)globalObj, globalShape, (void*)root->globalObj,
root->globalShape);)
js_Backoff(cx, (jsbytecode*) root->ip);
FlushJITCache(cx);
return false;
}
if (shape)
*shape = globalShape;
if (slots)
*slots = ti->globalSlots;
return true;
}
/* No recorder, search for a tracked global-state (or allocate one). */
for (size_t i = 0; i < MONITOR_N_GLOBAL_STATES; ++i) {
GlobalState &state = tm->globalStates[i];
if (state.globalShape == uint32(-1)) {
state.globalObj = globalObj;
state.globalShape = globalShape;
JS_ASSERT(state.globalSlots);
JS_ASSERT(state.globalSlots->length() == 0);
}
if (state.globalObj == globalObj && state.globalShape == globalShape) {
if (shape)
*shape = globalShape;
if (slots)
*slots = state.globalSlots;
return true;
}
}
/* No currently-tracked-global found and no room to allocate, abort. */
AUDIT(globalShapeMismatchAtEntry);
debug_only_v(printf("No global slotlist for global shape %u, flushing cache.\n",
globalShape));
FlushJITCache(cx);
return false;
}
static JS_REQUIRES_STACK bool
js_StartRecorder(JSContext* cx, VMSideExit* anchor, Fragment* f, TreeInfo* ti,
unsigned stackSlots, unsigned ngslots, uint8* typeMap,
VMSideExit* expectedInnerExit, jsbytecode* outer, uint32 outerArgc)
{
JSTraceMonitor* tm = &JS_TRACE_MONITOR(cx);
if (JS_TRACE_MONITOR(cx).needFlush) {
FlushJITCache(cx);
return false;
}
JS_ASSERT(f->root != f || !cx->fp->imacpc);
/* start recording if no exception during construction */
tm->recorder = new (&gc) TraceRecorder(cx, anchor, f, ti,
stackSlots, ngslots, typeMap,
expectedInnerExit, outer, outerArgc);
if (cx->throwing) {
js_AbortRecording(cx, "setting up recorder failed");
return false;
}
/* clear any leftover error state */
tm->fragmento->assm()->setError(None);
return true;
}
static void
js_TrashTree(JSContext* cx, Fragment* f)
{
JS_ASSERT((!f->code()) == (!f->vmprivate));
JS_ASSERT(f == f->root);
if (!f->code())
return;
AUDIT(treesTrashed);
debug_only_v(printf("Trashing tree info.\n");)
Fragmento* fragmento = JS_TRACE_MONITOR(cx).fragmento;
TreeInfo* ti = (TreeInfo*)f->vmprivate;
f->vmprivate = NULL;
f->releaseCode(fragmento);
Fragment** data = ti->dependentTrees.data();
unsigned length = ti->dependentTrees.length();
for (unsigned n = 0; n < length; ++n)
js_TrashTree(cx, data[n]);
data = ti->linkedTrees.data();
length = ti->linkedTrees.length();
for (unsigned n = 0; n < length; ++n)
js_TrashTree(cx, data[n]);
delete ti;
JS_ASSERT(!f->code() && !f->vmprivate);
}
static int
js_SynthesizeFrame(JSContext* cx, const FrameInfo& fi)
{
VOUCH_DOES_NOT_REQUIRE_STACK();
JS_ASSERT(HAS_FUNCTION_CLASS(fi.callee));
JSFunction* fun = GET_FUNCTION_PRIVATE(cx, fi.callee);
JS_ASSERT(FUN_INTERPRETED(fun));
/* Assert that we have a correct sp distance from cx->fp->slots in fi. */
JSStackFrame* fp = cx->fp;
JS_ASSERT_IF(!fi.imacpc,
js_ReconstructStackDepth(cx, fp->script, fi.pc)
== uintN(fi.spdist - fp->script->nfixed));
uintN nframeslots = JS_HOWMANY(sizeof(JSInlineFrame), sizeof(jsval));
JSScript* script = fun->u.i.script;
size_t nbytes = (nframeslots + script->nslots) * sizeof(jsval);
/* Code duplicated from inline_call: case in js_Interpret (FIXME). */
JSArena* a = cx->stackPool.current;
void* newmark = (void*) a->avail;
uintN argc = fi.get_argc();
jsval* vp = fp->slots + fi.spdist - (2 + argc);
uintN missing = 0;
jsval* newsp;
if (fun->nargs > argc) {
const JSFrameRegs& regs = *fp->regs;
newsp = vp + 2 + fun->nargs;
JS_ASSERT(newsp > regs.sp);
if ((jsuword) newsp <= a->limit) {
if ((jsuword) newsp > a->avail)
a->avail = (jsuword) newsp;
jsval* argsp = newsp;
do {
*--argsp = JSVAL_VOID;
} while (argsp != regs.sp);
missing = 0;
} else {
missing = fun->nargs - argc;
nbytes += (2 + fun->nargs) * sizeof(jsval);
}
}
/* Allocate the inline frame with its vars and operands. */
if (a->avail + nbytes <= a->limit) {
newsp = (jsval *) a->avail;
a->avail += nbytes;
JS_ASSERT(missing == 0);
} else {
/* This allocation is infallible: js_ExecuteTree reserved enough stack. */
JS_ARENA_ALLOCATE_CAST(newsp, jsval *, &cx->stackPool, nbytes);
JS_ASSERT(newsp);
/*
* Move args if the missing ones overflow arena a, then push
* undefined for the missing args.
*/
if (missing) {
memcpy(newsp, vp, (2 + argc) * sizeof(jsval));
vp = newsp;
newsp = vp + 2 + argc;
do {
*newsp++ = JSVAL_VOID;
} while (--missing != 0);
}
}
/* Claim space for the stack frame and initialize it. */
JSInlineFrame* newifp = (JSInlineFrame *) newsp;
newsp += nframeslots;
newifp->frame.callobj = NULL;
newifp->frame.argsobj = NULL;
newifp->frame.varobj = NULL;
newifp->frame.script = script;
newifp->frame.callee = fi.callee; // Roll with a potentially stale callee for now.
newifp->frame.fun = fun;
bool constructing = fi.is_constructing();
newifp->frame.argc = argc;
newifp->callerRegs.pc = fi.pc;
newifp->callerRegs.sp = fp->slots + fi.spdist;
fp->imacpc = fi.imacpc;
#ifdef DEBUG
if (fi.block != fp->blockChain) {
for (JSObject* obj = fi.block; obj != fp->blockChain; obj = STOBJ_GET_PARENT(obj))
JS_ASSERT(obj);
}
#endif
fp->blockChain = fi.block;
newifp->frame.argv = newifp->callerRegs.sp - argc;
JS_ASSERT(newifp->frame.argv);
#ifdef DEBUG
// Initialize argv[-1] to a known-bogus value so we'll catch it if
// someone forgets to initialize it later.
newifp->frame.argv[-1] = JSVAL_HOLE;
#endif
JS_ASSERT(newifp->frame.argv >= StackBase(fp) + 2);
newifp->frame.rval = JSVAL_VOID;
newifp->frame.down = fp;
newifp->frame.annotation = NULL;
newifp->frame.scopeChain = NULL; // will be updated in FlushNativeStackFrame
newifp->frame.sharpDepth = 0;
newifp->frame.sharpArray = NULL;
newifp->frame.flags = constructing ? JSFRAME_CONSTRUCTING : 0;
newifp->frame.dormantNext = NULL;
newifp->frame.xmlNamespace = NULL;
newifp->frame.blockChain = NULL;
newifp->mark = newmark;
newifp->frame.thisp = NULL; // will be updated in FlushNativeStackFrame
newifp->frame.regs = fp->regs;
newifp->frame.regs->pc = script->code;
newifp->frame.regs->sp = newsp + script->nfixed;
newifp->frame.imacpc = NULL;
newifp->frame.slots = newsp;
if (script->staticLevel < JS_DISPLAY_SIZE) {
JSStackFrame **disp = &cx->display[script->staticLevel];
newifp->frame.displaySave = *disp;
*disp = &newifp->frame;
}
/*
* Note that fp->script is still the caller's script; set the callee
* inline frame's idea of caller version from its version.
*/
newifp->callerVersion = (JSVersion) fp->script->version;
// After this paragraph, fp and cx->fp point to the newly synthesized frame.
fp->regs = &newifp->callerRegs;
fp = cx->fp = &newifp->frame;
/*
* If there's a call hook, invoke it to compute the hookData used by
* debuggers that cooperate with the interpreter.
*/
JSInterpreterHook hook = cx->debugHooks->callHook;
if (hook) {
newifp->hookData = hook(cx, fp, JS_TRUE, 0, cx->debugHooks->callHookData);
} else {
newifp->hookData = NULL;
}
// FIXME? we must count stack slots from caller's operand stack up to (but not including)
// callee's, including missing arguments. Could we shift everything down to the caller's
// fp->slots (where vars start) and avoid some of the complexity?
return (fi.spdist - fp->down->script->nfixed) +
((fun->nargs > fp->argc) ? fun->nargs - fp->argc : 0) +
script->nfixed;
}
static void
SynthesizeSlowNativeFrame(JSContext *cx, VMSideExit *exit)
{
void *mark;
JSInlineFrame *ifp;
/* This allocation is infallible: js_ExecuteTree reserved enough stack. */
mark = JS_ARENA_MARK(&cx->stackPool);
JS_ARENA_ALLOCATE_CAST(ifp, JSInlineFrame *, &cx->stackPool, sizeof(JSInlineFrame));
JS_ASSERT(ifp);
JSStackFrame *fp = &ifp->frame;
fp->regs = NULL;
fp->imacpc = NULL;
fp->slots = NULL;
fp->callobj = NULL;
fp->argsobj = NULL;
fp->varobj = cx->fp->varobj;
fp->callee = exit->nativeCallee();
fp->script = NULL;
fp->fun = GET_FUNCTION_PRIVATE(cx, fp->callee);
// fp->thisp is really a jsval, so reinterpret_cast here, not JSVAL_TO_OBJECT.
fp->thisp = (JSObject *) cx->nativeVp[1];
fp->argc = cx->nativeVpLen - 2;
fp->argv = cx->nativeVp + 2;
fp->rval = JSVAL_VOID;
fp->down = cx->fp;
fp->annotation = NULL;
JS_ASSERT(cx->fp->scopeChain);
fp->scopeChain = cx->fp->scopeChain;
fp->blockChain = NULL;
fp->sharpDepth = 0;
fp->sharpArray = NULL;
fp->flags = exit->constructing() ? JSFRAME_CONSTRUCTING : 0;
fp->dormantNext = NULL;
fp->xmlNamespace = NULL;
fp->displaySave = NULL;
ifp->mark = mark;
cx->fp = fp;
}
JS_REQUIRES_STACK bool
js_RecordTree(JSContext* cx, JSTraceMonitor* tm, Fragment* f, jsbytecode* outer,
uint32 outerArgc, JSObject* globalObj, uint32 globalShape,
SlotList* globalSlots, uint32 argc)
{
JS_ASSERT(f->root == f);
/* Make sure the global type map didn't change on us. */
if (!CheckGlobalObjectShape(cx, tm, globalObj)) {
js_Backoff(cx, (jsbytecode*) f->root->ip);
return false;
}
AUDIT(recorderStarted);
/* Try to find an unused peer fragment, or allocate a new one. */
while (f->code() && f->peer)
f = f->peer;
if (f->code())
f = getAnchor(&JS_TRACE_MONITOR(cx), f->root->ip, globalObj, globalShape, argc);
if (!f) {
FlushJITCache(cx);
return false;
}
f->root = f;
f->lirbuf = tm->lirbuf;
if (f->lirbuf->outOMem() || js_OverfullFragmento(tm, tm->fragmento)) {
js_Backoff(cx, (jsbytecode*) f->root->ip);
FlushJITCache(cx);
debug_only_v(printf("Out of memory recording new tree, flushing cache.\n");)
return false;
}
JS_ASSERT(!f->code() && !f->vmprivate);
/* setup the VM-private treeInfo structure for this fragment */
TreeInfo* ti = new (&gc) TreeInfo(f, globalSlots);
/* capture the coerced type of each active slot in the type map */
ti->typeMap.captureTypes(cx, *globalSlots, 0/*callDepth*/);
ti->nStackTypes = ti->typeMap.length() - globalSlots->length();
#ifdef DEBUG
ensureTreeIsUnique(tm, (VMFragment*)f, ti);
ti->treeFileName = cx->fp->script->filename;
ti->treeLineNumber = js_FramePCToLineNumber(cx, cx->fp);
ti->treePCOffset = FramePCOffset(cx->fp);
#endif
/* determine the native frame layout at the entry point */
unsigned entryNativeStackSlots = ti->nStackTypes;
JS_ASSERT(entryNativeStackSlots == js_NativeStackSlots(cx, 0/*callDepth*/));
ti->nativeStackBase = (entryNativeStackSlots -
(cx->fp->regs->sp - StackBase(cx->fp))) * sizeof(double);
ti->maxNativeStackSlots = entryNativeStackSlots;
ti->maxCallDepth = 0;
ti->script = cx->fp->script;
/* recording primary trace */
if (!js_StartRecorder(cx, NULL, f, ti,
ti->nStackTypes,
ti->globalSlots->length(),
ti->typeMap.data(), NULL, outer, outerArgc)) {
return false;
}
return true;
}
JS_REQUIRES_STACK static inline void
markSlotUndemotable(JSContext* cx, TreeInfo* ti, unsigned slot)
{
if (slot < ti->nStackTypes) {
oracle.markStackSlotUndemotable(cx, slot);
return;
}
uint16* gslots = ti->globalSlots->data();
oracle.markGlobalSlotUndemotable(cx, gslots[slot - ti->nStackTypes]);
}
JS_REQUIRES_STACK static inline bool
isSlotUndemotable(JSContext* cx, TreeInfo* ti, unsigned slot)
{
if (slot < ti->nStackTypes)
return oracle.isStackSlotUndemotable(cx, slot);
uint16* gslots = ti->globalSlots->data();
return oracle.isGlobalSlotUndemotable(cx, gslots[slot - ti->nStackTypes]);
}
JS_REQUIRES_STACK static bool
js_AttemptToStabilizeTree(JSContext* cx, VMSideExit* exit, jsbytecode* outer, uint32 outerArgc)
{
JSTraceMonitor* tm = &JS_TRACE_MONITOR(cx);
if (tm->needFlush) {
FlushJITCache(cx);
return false;
}
VMFragment* from = (VMFragment*)exit->from->root;
TreeInfo* from_ti = (TreeInfo*)from->vmprivate;
JS_ASSERT(exit->from->root->code());
/*
* The loop edge exit might not know about all types since the tree could have
* been further specialized since it was recorded. Fill in the missing types
* from the entry type map.
*/
uint8* m = getFullTypeMap(exit);
unsigned ngslots = exit->numGlobalSlots;
if (ngslots < from_ti->nGlobalTypes()) {
uint32 partial = exit->numStackSlots + exit->numGlobalSlots;
m = (uint8*)alloca(from_ti->typeMap.length());
memcpy(m, getFullTypeMap(exit), partial);
memcpy(m + partial, from_ti->globalTypeMap() + exit->numGlobalSlots,
from_ti->nGlobalTypes() - exit->numGlobalSlots);
ngslots = from_ti->nGlobalTypes();
}
JS_ASSERT(exit->numStackSlots + ngslots == from_ti->typeMap.length());
/*
* If we see any doubles along the loop edge, mark those slots undemotable
* since we know now for a fact that they can contain doubles.
*/
for (unsigned i = 0; i < from_ti->typeMap.length(); i++) {
if (m[i] == JSVAL_DOUBLE)
markSlotUndemotable(cx, from_ti, i);
}
bool bound = false;
for (Fragment* f = from->first; f != NULL; f = f->peer) {
if (!f->code())
continue;
TreeInfo* ti = (TreeInfo*)f->vmprivate;
JS_ASSERT(exit->numStackSlots == ti->nStackTypes);
/* Check the minimum number of slots that need to be compared. */
unsigned checkSlots = JS_MIN(from_ti->typeMap.length(), ti->typeMap.length());
uint8* m2 = ti->typeMap.data();
/* Analyze the exit typemap against the peer typemap.
* Two conditions are important:
* 1) Typemaps are identical: these peers can be attached.
* 2) Typemaps do not match, but only contain I->D mismatches.
* In this case, the original tree must be trashed because it
* will never connect to any peer.
*/
bool matched = true;
bool undemote = false;
for (uint32 i = 0; i < checkSlots; i++) {
/* If the types are equal we're okay. */
if (m[i] == m2[i])
continue;
matched = false;
/* If there's an I->D that cannot be resolved, flag it.
* Otherwise, break and go to the next peer.
*/
if (m[i] == JSVAL_INT && m2[i] == JSVAL_DOUBLE && isSlotUndemotable(cx, ti, i)) {
undemote = true;
} else {
undemote = false;
break;
}
}
if (matched) {
JS_ASSERT(from_ti->globalSlots == ti->globalSlots);
JS_ASSERT(from_ti->nStackTypes == ti->nStackTypes);
/* Capture missing globals on both trees and link the fragments together. */
if (from != f) {
ti->dependentTrees.addUnique(from);
from_ti->linkedTrees.addUnique(f);
}
if (ti->nGlobalTypes() < ti->globalSlots->length())
specializeTreesToMissingGlobals(cx, ti);
exit->target = f;
tm->fragmento->assm()->patch(exit);
/* Now erase this exit from the unstable exit list. */
UnstableExit** tail = &from_ti->unstableExits;
for (UnstableExit* uexit = from_ti->unstableExits; uexit != NULL; uexit = uexit->next) {
if (uexit->exit == exit) {
*tail = uexit->next;
delete uexit;
bound = true;
break;
}
tail = &uexit->next;
}
JS_ASSERT(bound);
debug_only_v(js_DumpPeerStability(tm, f->ip, from->globalObj, from->globalShape, from->argc);)
break;
} else if (undemote) {
/* The original tree is unconnectable, so trash it. */
js_TrashTree(cx, f);
/* We shouldn't attempt to record now, since we'll hit a duplicate. */
return false;
}
}
if (bound)
return false;
VMFragment* root = (VMFragment*)from->root;
return js_RecordTree(cx, tm, from->first, outer, outerArgc, root->globalObj,
root->globalShape, from_ti->globalSlots, cx->fp->argc);
}
static JS_REQUIRES_STACK bool
js_AttemptToExtendTree(JSContext* cx, VMSideExit* anchor, VMSideExit* exitedFrom, jsbytecode* outer)
{
JSTraceMonitor* tm = &JS_TRACE_MONITOR(cx);
if (tm->needFlush) {
FlushJITCache(cx);
return false;
}
Fragment* f = anchor->from->root;
JS_ASSERT(f->vmprivate);
TreeInfo* ti = (TreeInfo*)f->vmprivate;
/* Don't grow trees above a certain size to avoid code explosion due to tail duplication. */
if (ti->branchCount >= MAX_BRANCHES)
return false;
Fragment* c;
if (!(c = anchor->target)) {
c = JS_TRACE_MONITOR(cx).fragmento->createBranch(anchor, cx->fp->regs->pc);
c->spawnedFrom = anchor;
c->parent = f;
anchor->target = c;
c->root = f;
}
/*
* If we are recycling a fragment, it might have a different ip so reset it here. This
* can happen when attaching a branch to a NESTED_EXIT, which might extend along separate paths
* (i.e. after the loop edge, and after a return statement).
*/
c->ip = cx->fp->regs->pc;
debug_only_v(printf("trying to attach another branch to the tree (hits = %d)\n", c->hits());)
int32_t& hits = c->hits();
if (outer || (hits++ >= HOTEXIT && hits <= HOTEXIT+MAXEXIT)) {
/* start tracing secondary trace from this point */
c->lirbuf = f->lirbuf;
unsigned stackSlots;
unsigned ngslots;
uint8* typeMap;
TypeMap fullMap;
if (exitedFrom == NULL) {
/* If we are coming straight from a simple side exit, just use that exit's type map
as starting point. */
ngslots = anchor->numGlobalSlots;
stackSlots = anchor->numStackSlots;
typeMap = getFullTypeMap(anchor);
} else {
/* If we side-exited on a loop exit and continue on a nesting guard, the nesting
guard (anchor) has the type information for everything below the current scope,
and the actual guard we exited from has the types for everything in the current
scope (and whatever it inlined). We have to merge those maps here. */
VMSideExit* e1 = anchor;
VMSideExit* e2 = exitedFrom;
fullMap.add(getStackTypeMap(e1), e1->numStackSlotsBelowCurrentFrame);
fullMap.add(getStackTypeMap(e2), e2->numStackSlots);
stackSlots = fullMap.length();
fullMap.add(getGlobalTypeMap(e2), e2->numGlobalSlots);
if (e2->numGlobalSlots < e1->numGlobalSlots) {
/* Watch out for an extremely rare case (bug 502714). The sequence of events is:
*
* 1) Inner tree compiles not knowing about global X (which has type A).
* 2) Inner tree learns about global X and specializes it to a different type
* (type B).
* 3) Outer tree records inner tree with global X as type A, exiting as B.
* 4) Outer tree now has a nesting guard with typeof(X)=B.
* 5) Inner tree takes its original exit that does not know about X.
*
* In this case, the nesting guard fails, and now it is illegal to use the nested
* typemap entry for X. The correct entry is in the inner guard's TreeInfo,
* analogous to the solution for bug 476653.
*/
TreeInfo* innerTree = (TreeInfo*)e2->from->root->vmprivate;
unsigned slots = e2->numGlobalSlots;
if (innerTree->nGlobalTypes() > slots) {
unsigned addSlots = JS_MIN(innerTree->nGlobalTypes() - slots,
e1->numGlobalSlots - slots);
fullMap.add(innerTree->globalTypeMap() + e2->numGlobalSlots, addSlots);
slots += addSlots;
}
if (slots < e1->numGlobalSlots)
fullMap.add(getGlobalTypeMap(e1) + slots, e1->numGlobalSlots - slots);
JS_ASSERT(slots == e1->numGlobalSlots);
}
ngslots = e1->numGlobalSlots;
typeMap = fullMap.data();
}
JS_ASSERT(ngslots >= anchor->numGlobalSlots);
return js_StartRecorder(cx, anchor, c, (TreeInfo*)f->vmprivate, stackSlots,
ngslots, typeMap, exitedFrom, outer, cx->fp->argc);
}
return false;
}
static JS_REQUIRES_STACK VMSideExit*
js_ExecuteTree(JSContext* cx, Fragment* f, uintN& inlineCallCount,
VMSideExit** innermostNestedGuardp);
JS_REQUIRES_STACK bool
js_RecordLoopEdge(JSContext* cx, TraceRecorder* r, uintN& inlineCallCount)
{
#ifdef JS_THREADSAFE
if (OBJ_SCOPE(JS_GetGlobalForObject(cx, cx->fp->scopeChain))->title.ownercx != cx) {
js_AbortRecording(cx, "Global object not owned by this context");
return false; /* we stay away from shared global objects */
}
#endif
JSTraceMonitor* tm = &JS_TRACE_MONITOR(cx);
/* Process needFlush and deep abort requests. */
if (tm->needFlush) {
FlushJITCache(cx);
return false;
}
if (r->wasDeepAborted()) {
js_AbortRecording(cx, "deep abort requested");
return false;
}
JS_ASSERT(r->getFragment() && !r->getFragment()->lastIns);
VMFragment* root = (VMFragment*)r->getFragment()->root;
/* Does this branch go to an inner loop? */
Fragment* first = getLoop(&JS_TRACE_MONITOR(cx), cx->fp->regs->pc,
root->globalObj, root->globalShape, cx->fp->argc);
if (!first) {
/* Not an inner loop we can call, abort trace. */
AUDIT(returnToDifferentLoopHeader);
JS_ASSERT(!cx->fp->imacpc);
debug_only_v(printf("loop edge to %d, header %d\n",
cx->fp->regs->pc - cx->fp->script->code,
(jsbytecode*)r->getFragment()->root->ip - cx->fp->script->code));
js_AbortRecording(cx, "Loop edge does not return to header");
return false;
}
/* Make sure inner tree call will not run into an out-of-memory condition. */
if (tm->reservedDoublePoolPtr < (tm->reservedDoublePool + MAX_NATIVE_STACK_SLOTS) &&
!js_ReplenishReservedPool(cx, tm)) {
js_AbortRecording(cx, "Couldn't call inner tree (out of memory)");
return false;
}
/* Make sure the shape of the global object still matches (this might flush
the JIT cache). */
JSObject* globalObj = JS_GetGlobalForObject(cx, cx->fp->scopeChain);
uint32 globalShape = -1;
SlotList* globalSlots = NULL;
if (!CheckGlobalObjectShape(cx, tm, globalObj, &globalShape, &globalSlots))
return false;
debug_only_v(printf("Looking for type-compatible peer (%s:%d@%d)\n",
cx->fp->script->filename,
js_FramePCToLineNumber(cx, cx->fp),
FramePCOffset(cx->fp));)
// Find a matching inner tree. If none can be found, compile one.
Fragment* f = r->findNestedCompatiblePeer(first);
if (!f || !f->code()) {
AUDIT(noCompatInnerTrees);
VMFragment* outerFragment = (VMFragment*) tm->recorder->getFragment()->root;
jsbytecode* outer = (jsbytecode*) outerFragment->ip;
uint32 outerArgc = outerFragment->argc;
uint32 argc = cx->fp->argc;
js_AbortRecording(cx, "No compatible inner tree");
// Find an empty fragment we can recycle, or allocate a new one.
for (f = first; f != NULL; f = f->peer) {
if (!f->code())
break;
}
if (!f || f->code()) {
f = getAnchor(tm, cx->fp->regs->pc, globalObj, globalShape, argc);
if (!f) {
FlushJITCache(cx);
return false;
}
}
return js_RecordTree(cx, tm, f, outer, outerArgc, globalObj, globalShape, globalSlots, argc);
}
r->adjustCallerTypes(f);
r->prepareTreeCall(f);
VMSideExit* innermostNestedGuard = NULL;
VMSideExit* lr = js_ExecuteTree(cx, f, inlineCallCount, &innermostNestedGuard);
if (!lr || r->wasDeepAborted()) {
if (!lr)
js_AbortRecording(cx, "Couldn't call inner tree");
return false;
}
VMFragment* outerFragment = (VMFragment*) tm->recorder->getFragment()->root;
jsbytecode* outer = (jsbytecode*) outerFragment->ip;
switch (lr->exitType) {
case LOOP_EXIT:
/* If the inner tree exited on an unknown loop exit, grow the tree around it. */
if (innermostNestedGuard) {
js_AbortRecording(cx, "Inner tree took different side exit, abort current "
"recording and grow nesting tree");
return js_AttemptToExtendTree(cx, innermostNestedGuard, lr, outer);
}
/* emit a call to the inner tree and continue recording the outer tree trace */
r->emitTreeCall(f, lr);
return true;
case UNSTABLE_LOOP_EXIT:
/* abort recording so the inner loop can become type stable. */
js_AbortRecording(cx, "Inner tree is trying to stabilize, abort outer recording");
return js_AttemptToStabilizeTree(cx, lr, outer, outerFragment->argc);
case BRANCH_EXIT:
/* abort recording the outer tree, extend the inner tree */
js_AbortRecording(cx, "Inner tree is trying to grow, abort outer recording");
return js_AttemptToExtendTree(cx, lr, NULL, outer);
default:
debug_only_v(printf("exit_type=%d\n", lr->exitType);)
js_AbortRecording(cx, "Inner tree not suitable for calling");
return false;
}
}
static bool
js_IsEntryTypeCompatible(jsval* vp, uint8* m)
{
unsigned tag = JSVAL_TAG(*vp);
debug_only_v(printf("%c/%c ", tagChar[tag], typeChar[*m]);)
switch (*m) {
case JSVAL_OBJECT:
if (tag == JSVAL_OBJECT && !JSVAL_IS_NULL(*vp) &&
!HAS_FUNCTION_CLASS(JSVAL_TO_OBJECT(*vp))) {
return true;
}
debug_only_v(printf("object != tag%u ", tag);)
return false;
case JSVAL_INT:
jsint i;
if (JSVAL_IS_INT(*vp))
return true;
if ((tag == JSVAL_DOUBLE) && JSDOUBLE_IS_INT(*JSVAL_TO_DOUBLE(*vp), i))
return true;
debug_only_v(printf("int != tag%u(value=%lu) ", tag, (unsigned long)*vp);)
return false;
case JSVAL_DOUBLE:
if (JSVAL_IS_INT(*vp) || tag == JSVAL_DOUBLE)
return true;
debug_only_v(printf("double != tag%u ", tag);)
return false;
case JSVAL_BOXED:
JS_NOT_REACHED("shouldn't see boxed type in entry");
return false;
case JSVAL_STRING:
if (tag == JSVAL_STRING)
return true;
debug_only_v(printf("string != tag%u ", tag);)
return false;
case JSVAL_TNULL:
if (JSVAL_IS_NULL(*vp))
return true;
debug_only_v(printf("null != tag%u ", tag);)
return false;
case JSVAL_BOOLEAN:
if (tag == JSVAL_BOOLEAN)
return true;
debug_only_v(printf("bool != tag%u ", tag);)
return false;
default:
JS_ASSERT(*m == JSVAL_TFUN);
if (tag == JSVAL_OBJECT && !JSVAL_IS_NULL(*vp) &&
HAS_FUNCTION_CLASS(JSVAL_TO_OBJECT(*vp))) {
return true;
}
debug_only_v(printf("fun != tag%u ", tag);)
return false;
}
}
JS_REQUIRES_STACK Fragment*
TraceRecorder::findNestedCompatiblePeer(Fragment* f)
{
JSTraceMonitor* tm;
tm = &JS_TRACE_MONITOR(cx);
unsigned int ngslots = treeInfo->globalSlots->length();
uint16* gslots = treeInfo->globalSlots->data();
TreeInfo* ti;
for (; f != NULL; f = f->peer) {
if (!f->code())
continue;
ti = (TreeInfo*)f->vmprivate;
debug_only_v(printf("checking nested types %p: ", (void*)f);)
if (ngslots > ti->nGlobalTypes())
specializeTreesToMissingGlobals(cx, ti);
uint8* typemap = ti->typeMap.data();
/*
* Determine whether the typemap of the inner tree matches the outer tree's
* current state. If the inner tree expects an integer, but the outer tree
* doesn't guarantee an integer for that slot, we mark the slot undemotable
* and mismatch here. This will force a new tree to be compiled that accepts
* a double for the slot. If the inner tree expects a double, but the outer
* tree has an integer, we can proceed, but we mark the location undemotable.
*/
bool ok = true;
uint8* m = typemap;
FORALL_SLOTS_IN_PENDING_FRAMES(cx, 0,
debug_only_v(printf("%s%d=", vpname, vpnum);)
if (!js_IsEntryTypeCompatible(vp, m)) {
ok = false;
} else if (!isPromoteInt(get(vp)) && *m == JSVAL_INT) {
oracle.markStackSlotUndemotable(cx, unsigned(m - typemap));
ok = false;
} else if (JSVAL_IS_INT(*vp) && *m == JSVAL_DOUBLE) {
oracle.markStackSlotUndemotable(cx, unsigned(m - typemap));
}
m++;
);
FORALL_GLOBAL_SLOTS(cx, ngslots, gslots,
debug_only_v(printf("%s%d=", vpname, vpnum);)
if (!js_IsEntryTypeCompatible(vp, m)) {
ok = false;
} else if (!isPromoteInt(get(vp)) && *m == JSVAL_INT) {
oracle.markGlobalSlotUndemotable(cx, gslots[n]);
ok = false;
} else if (JSVAL_IS_INT(*vp) && *m == JSVAL_DOUBLE) {
oracle.markGlobalSlotUndemotable(cx, gslots[n]);
}
m++;
);
JS_ASSERT(unsigned(m - ti->typeMap.data()) == ti->typeMap.length());
debug_only_v(printf(" %s\n", ok ? "match" : "");)
if (ok)
return f;
}
return NULL;
}
/**
* Check if types are usable for trace execution.
*
* @param cx Context.
* @param ti Tree info of peer we're testing.
* @return True if compatible (with or without demotions), false otherwise.
*/
static JS_REQUIRES_STACK bool
js_CheckEntryTypes(JSContext* cx, TreeInfo* ti)
{
unsigned int ngslots = ti->globalSlots->length();
uint16* gslots = ti->globalSlots->data();
JS_ASSERT(ti->nStackTypes == js_NativeStackSlots(cx, 0));
if (ngslots > ti->nGlobalTypes())
specializeTreesToMissingGlobals(cx, ti);
uint8* m = ti->typeMap.data();
JS_ASSERT(ti->typeMap.length() == js_NativeStackSlots(cx, 0) + ngslots);
JS_ASSERT(ti->typeMap.length() == ti->nStackTypes + ngslots);
JS_ASSERT(ti->nGlobalTypes() == ngslots);
FORALL_SLOTS(cx, ngslots, gslots, 0,
debug_only_v(printf("%s%d=", vpname, vpnum);)
JS_ASSERT(*m != 0xCD);
if (!js_IsEntryTypeCompatible(vp, m))
goto check_fail;
m++;
);
JS_ASSERT(unsigned(m - ti->typeMap.data()) == ti->typeMap.length());
debug_only_v(printf("\n");)
return true;
check_fail:
debug_only_v(printf("\n");)
return false;
}
/**
* Find an acceptable entry tree given a PC.
*
* @param cx Context.
* @param f First peer fragment.
* @param nodemote If true, will try to find a peer that does not require demotion.
* @out count Number of fragments consulted.
*/
static JS_REQUIRES_STACK Fragment*
js_FindVMCompatiblePeer(JSContext* cx, Fragment* f, uintN& count)
{
count = 0;
for (; f != NULL; f = f->peer) {
if (f->vmprivate == NULL)
continue;
debug_only_v(printf("checking vm types %p (ip: %p): ", (void*)f, f->ip);)
if (js_CheckEntryTypes(cx, (TreeInfo*)f->vmprivate))
return f;
++count;
}
return NULL;
}
static void
LeaveTree(InterpState&, VMSideExit* lr);
/**
* Executes a tree.
*/
static JS_REQUIRES_STACK VMSideExit*
js_ExecuteTree(JSContext* cx, Fragment* f, uintN& inlineCallCount,
VMSideExit** innermostNestedGuardp)
{
JS_ASSERT(f->root == f && f->code() && f->vmprivate);
JSTraceMonitor* tm = &JS_TRACE_MONITOR(cx);
JSObject* globalObj = JS_GetGlobalForObject(cx, cx->fp->scopeChain);
TreeInfo* ti = (TreeInfo*)f->vmprivate;
unsigned ngslots = ti->globalSlots->length();
uint16* gslots = ti->globalSlots->data();
unsigned globalFrameSize = STOBJ_NSLOTS(globalObj);
/* Make sure the global object is sane. */
JS_ASSERT(!ngslots || (OBJ_SHAPE(JS_GetGlobalForObject(cx, cx->fp->scopeChain)) ==
((VMFragment*)f)->globalShape));
/* Make sure our caller replenished the double pool. */
JS_ASSERT(tm->reservedDoublePoolPtr >= tm->reservedDoublePool + MAX_NATIVE_STACK_SLOTS);
/* Reserve objects and stack space now, to make leaving the tree infallible. */
if (!js_ReserveObjects(cx, MAX_CALL_STACK_ENTRIES))
return NULL;
#ifdef DEBUG
uintN savedProhibitFlush = JS_TRACE_MONITOR(cx).prohibitFlush;
#endif
/* Setup the interpreter state block, which is followed by the native global frame. */
InterpState* state = (InterpState*)alloca(sizeof(InterpState) + (globalFrameSize+1)*sizeof(double));
state->cx = cx;
state->inlineCallCountp = &inlineCallCount;
state->innermostNestedGuardp = innermostNestedGuardp;
state->outermostTree = ti;
state->lastTreeExitGuard = NULL;
state->lastTreeCallGuard = NULL;
state->rpAtLastTreeCall = NULL;
state->builtinStatus = 0;
/* Setup the native global frame. */
double* global = (double*)(state+1);
/* Setup the native stack frame. */
double stack_buffer[MAX_NATIVE_STACK_SLOTS];
state->stackBase = stack_buffer;
state->sp = stack_buffer + (ti->nativeStackBase/sizeof(double));
state->eos = stack_buffer + MAX_NATIVE_STACK_SLOTS;
/* Setup the native call stack frame. */
FrameInfo* callstack_buffer[MAX_CALL_STACK_ENTRIES];
state->callstackBase = callstack_buffer;
state->rp = callstack_buffer;
state->eor = callstack_buffer + MAX_CALL_STACK_ENTRIES;
void *reserve;
state->stackMark = JS_ARENA_MARK(&cx->stackPool);
JS_ARENA_ALLOCATE(reserve, &cx->stackPool, MAX_INTERP_STACK_BYTES);
if (!reserve)
return NULL;
#ifdef DEBUG
memset(stack_buffer, 0xCD, sizeof(stack_buffer));
memset(global, 0xCD, (globalFrameSize+1)*sizeof(double));
JS_ASSERT(globalFrameSize <= MAX_GLOBAL_SLOTS);
#endif
debug_only(*(uint64*)&global[globalFrameSize] = 0xdeadbeefdeadbeefLL;)
debug_only_v(printf("entering trace at %s:%u@%u, native stack slots: %u code: %p\n",
cx->fp->script->filename,
js_FramePCToLineNumber(cx, cx->fp),
FramePCOffset(cx->fp),
ti->maxNativeStackSlots,
f->code());)
JS_ASSERT(ti->nGlobalTypes() == ngslots);
if (ngslots)
BuildNativeGlobalFrame(cx, ngslots, gslots, ti->globalTypeMap(), global);
BuildNativeStackFrame(cx, 0/*callDepth*/, ti->typeMap.data(), stack_buffer);
union { NIns *code; GuardRecord* (FASTCALL *func)(InterpState*, Fragment*); } u;
u.code = f->code();
#ifdef EXECUTE_TREE_TIMER
state->startTime = rdtsc();
#endif
JS_ASSERT(!tm->tracecx);
tm->tracecx = cx;
state->prev = cx->interpState;
cx->interpState = state;
debug_only(fflush(NULL);)
GuardRecord* rec;
#if defined(JS_NO_FASTCALL) && defined(NANOJIT_IA32)
SIMULATE_FASTCALL(rec, state, NULL, u.func);
#else
rec = u.func(state, NULL);
#endif
VMSideExit* lr = (VMSideExit*)rec->exit;
AUDIT(traceTriggered);
cx->interpState = state->prev;
JS_ASSERT(lr->exitType != LOOP_EXIT || !lr->calldepth);
tm->tracecx = NULL;
LeaveTree(*state, lr);
JS_ASSERT(JS_TRACE_MONITOR(cx).prohibitFlush == savedProhibitFlush);
return state->innermost;
}
static JS_FORCES_STACK void
LeaveTree(InterpState& state, VMSideExit* lr)
{
VOUCH_DOES_NOT_REQUIRE_STACK();
JSContext* cx = state.cx;
FrameInfo** callstack = state.callstackBase;
double* stack = state.stackBase;
/* Except if we find that this is a nested bailout, the guard the call returned is the
one we have to use to adjust pc and sp. */
VMSideExit* innermost = lr;
/* While executing a tree we do not update state.sp and state.rp even if they grow. Instead,
guards tell us by how much sp and rp should be incremented in case of a side exit. When
calling a nested tree, however, we actively adjust sp and rp. If we have such frames
from outer trees on the stack, then rp will have been adjusted. Before we can process
the stack of the frames of the tree we directly exited from, we have to first work our
way through the outer frames and generate interpreter frames for them. Once the call
stack (rp) is empty, we can process the final frames (which again are not directly
visible and only the guard we exited on will tells us about). */
FrameInfo** rp = (FrameInfo**)state.rp;
if (lr->exitType == NESTED_EXIT) {
VMSideExit* nested = state.lastTreeCallGuard;
if (!nested) {
/* If lastTreeCallGuard is not set in state, we only have a single level of
nesting in this exit, so lr itself is the innermost and outermost nested
guard, and hence we set nested to lr. The calldepth of the innermost guard
is not added to state.rp, so we do it here manually. For a nesting depth
greater than 1 the CallTree builtin already added the innermost guard's
calldepth to state.rpAtLastTreeCall. */
nested = lr;
rp += lr->calldepth;
} else {
/* During unwinding state.rp gets overwritten at every step and we restore
it here to its state at the innermost nested guard. The builtin already
added the calldepth of that innermost guard to rpAtLastTreeCall. */
rp = (FrameInfo**)state.rpAtLastTreeCall;
}
innermost = state.lastTreeExitGuard;
if (state.innermostNestedGuardp)
*state.innermostNestedGuardp = nested;
JS_ASSERT(nested);
JS_ASSERT(nested->exitType == NESTED_EXIT);
JS_ASSERT(state.lastTreeExitGuard);
JS_ASSERT(state.lastTreeExitGuard->exitType != NESTED_EXIT);
}
int32_t bs = state.builtinStatus;
bool bailed = innermost->exitType == STATUS_EXIT && (bs & JSBUILTIN_BAILED);
if (bailed) {
/*
* Deep-bail case.
*
* A _FAIL native already called LeaveTree. We already reconstructed
* the interpreter stack, in pre-call state, with pc pointing to the
* CALL/APPLY op, for correctness. Then we continued in native code.
*
* First, if we just returned from a slow native, pop its stack frame.
*/
if (!cx->fp->script) {
JSStackFrame *fp = cx->fp;
JS_ASSERT(FUN_SLOW_NATIVE(GET_FUNCTION_PRIVATE(cx, fp->callee)));
JS_ASSERT(fp->regs == NULL);
JS_ASSERT(fp->down->regs != &((JSInlineFrame *) fp)->callerRegs);
cx->fp = fp->down;
JS_ARENA_RELEASE(&cx->stackPool, ((JSInlineFrame *) fp)->mark);
}
JS_ASSERT(cx->fp->script);
if (!(bs & JSBUILTIN_ERROR)) {
/*
* The native succeeded (no exception or error). After it returned, the
* trace stored the return value (at the top of the native stack) and
* then immediately flunked the guard on state->builtinStatus.
*
* Now LeaveTree has been called again from the tail of
* js_ExecuteTree. We are about to return to the interpreter. Adjust
* the top stack frame to resume on the next op.
*/
JS_ASSERT(*cx->fp->regs->pc == JSOP_CALL ||
*cx->fp->regs->pc == JSOP_APPLY ||
*cx->fp->regs->pc == JSOP_NEW);
uintN argc = GET_ARGC(cx->fp->regs->pc);
cx->fp->regs->pc += JSOP_CALL_LENGTH;
cx->fp->regs->sp -= argc + 1;
JS_ASSERT_IF(!cx->fp->imacpc,
cx->fp->slots + cx->fp->script->nfixed +
js_ReconstructStackDepth(cx, cx->fp->script, cx->fp->regs->pc) ==
cx->fp->regs->sp);
/*
* The return value was not available when we reconstructed the stack,
* but we have it now. Box it.
*/
uint8* typeMap = getStackTypeMap(innermost);
/*
* If there's a tree call around the point that we deep exited at,
* then state.sp and state.rp were restored to their original
* values before the tree call and sp might be less than deepBailSp,
* which we sampled when we were told to deep bail.
*/
JS_ASSERT(state.deepBailSp >= state.stackBase && state.sp <= state.deepBailSp);
NativeToValue(cx,
cx->fp->regs->sp[-1],
typeMap[innermost->numStackSlots - 1],
(jsdouble *) state.deepBailSp + innermost->sp_adj / sizeof(jsdouble) - 1);
}
JSTraceMonitor* tm = &JS_TRACE_MONITOR(cx);
if (tm->prohibitFlush && --tm->prohibitFlush == 0 && tm->needFlush)
FlushJITCache(cx);
return;
}
JS_ARENA_RELEASE(&cx->stackPool, state.stackMark);
while (callstack < rp) {
/* Synthesize a stack frame and write out the values in it using the type map pointer
on the native call stack. */
js_SynthesizeFrame(cx, **callstack);
int slots = FlushNativeStackFrame(cx, 1/*callDepth*/, (uint8*)(*callstack+1), stack, cx->fp);
#ifdef DEBUG
JSStackFrame* fp = cx->fp;
debug_only_v(printf("synthesized deep frame for %s:%u@%u, slots=%d\n",
fp->script->filename,
js_FramePCToLineNumber(cx, fp),
FramePCOffset(fp),
slots);)
#endif
/* Keep track of the additional frames we put on the interpreter stack and the native
stack slots we consumed. */
++*state.inlineCallCountp;
++callstack;
stack += slots;
}
/* We already synthesized the frames around the innermost guard. Here we just deal
with additional frames inside the tree we are bailing out from. */
JS_ASSERT(rp == callstack);
unsigned calldepth = innermost->calldepth;
unsigned calldepth_slots = 0;
for (unsigned n = 0; n < calldepth; ++n) {
calldepth_slots += js_SynthesizeFrame(cx, *callstack[n]);
++*state.inlineCallCountp;
#ifdef DEBUG
JSStackFrame* fp = cx->fp;
debug_only_v(printf("synthesized shallow frame for %s:%u@%u\n",
fp->script->filename, js_FramePCToLineNumber(cx, fp),
FramePCOffset(fp));)
#endif
}
/* Adjust sp and pc relative to the tree we exited from (not the tree we entered into).
These are our final values for sp and pc since js_SynthesizeFrame has already taken
care of all frames in between. But first we recover fp->blockChain, which comes from
the side exit struct. */
JSStackFrame* fp = cx->fp;
fp->blockChain = innermost->block;
/* If we are not exiting from an inlined frame the state->sp is spbase, otherwise spbase
is whatever slots frames around us consume. */
fp->regs->pc = innermost->pc;
fp->imacpc = innermost->imacpc;
fp->regs->sp = StackBase(fp) + (innermost->sp_adj / sizeof(double)) - calldepth_slots;
JS_ASSERT_IF(!fp->imacpc,
fp->slots + fp->script->nfixed +
js_ReconstructStackDepth(cx, fp->script, fp->regs->pc) == fp->regs->sp);
#ifdef EXECUTE_TREE_TIMER
uint64 cycles = rdtsc() - state.startTime;
#elif defined(JS_JIT_SPEW)
uint64 cycles = 0;
#endif
debug_only_v(printf("leaving trace at %s:%u@%u, op=%s, lr=%p, exitType=%d, sp=%d, "
"calldepth=%d, cycles=%llu\n",
fp->script->filename,
js_FramePCToLineNumber(cx, fp),
FramePCOffset(fp),
js_CodeName[fp->imacpc ? *fp->imacpc : *fp->regs->pc],
(void*)lr,
lr->exitType,
fp->regs->sp - StackBase(fp),
calldepth,
cycles));
/* If this trace is part of a tree, later branches might have added additional globals for
which we don't have any type information available in the side exit. We merge in this
information from the entry type-map. See also comment in the constructor of TraceRecorder
why this is always safe to do. */
TreeInfo* outermostTree = state.outermostTree;
uint16* gslots = outermostTree->globalSlots->data();
unsigned ngslots = outermostTree->globalSlots->length();
JS_ASSERT(ngslots == outermostTree->nGlobalTypes());
uint8* globalTypeMap;
/* Are there enough globals? This is the ideal fast path. */
if (innermost->numGlobalSlots == ngslots) {
globalTypeMap = getGlobalTypeMap(innermost);
/* Otherwise, merge the typemap of the innermost entry and exit together. This should always
work because it is invalid for nested trees or linked trees to have incompatible types.
Thus, whenever a new global type is lazily added into a tree, all dependent and linked
trees are immediately specialized (see bug 476653). */
} else {
TreeInfo* ti = (TreeInfo*)innermost->from->root->vmprivate;
JS_ASSERT(ti->nGlobalTypes() == ngslots);
JS_ASSERT(ti->nGlobalTypes() > innermost->numGlobalSlots);
globalTypeMap = (uint8*)alloca(ngslots * sizeof(uint8));
memcpy(globalTypeMap, getGlobalTypeMap(innermost), innermost->numGlobalSlots);
memcpy(globalTypeMap + innermost->numGlobalSlots,
ti->globalTypeMap() + innermost->numGlobalSlots,
ti->nGlobalTypes() - innermost->numGlobalSlots);
}
/* write back native stack frame */
#ifdef DEBUG
int slots =
#endif
FlushNativeStackFrame(cx, innermost->calldepth,
getStackTypeMap(innermost),
stack, NULL);
JS_ASSERT(unsigned(slots) == innermost->numStackSlots);
if (innermost->nativeCalleeWord)
SynthesizeSlowNativeFrame(cx, innermost);
/* write back interned globals */
double* global = (double*)(&state + 1);
FlushNativeGlobalFrame(cx, ngslots, gslots, globalTypeMap, global);
JS_ASSERT(*(uint64*)&global[STOBJ_NSLOTS(JS_GetGlobalForObject(cx, cx->fp->scopeChain))] ==
0xdeadbeefdeadbeefLL);
cx->nativeVp = NULL;
#ifdef DEBUG
// Verify that our state restoration worked.
for (JSStackFrame* fp = cx->fp; fp; fp = fp->down) {
JS_ASSERT_IF(fp->callee, JSVAL_IS_OBJECT(fp->argv[-1]));
}
#endif
#ifdef JS_JIT_SPEW
if (innermost->exitType != TIMEOUT_EXIT)
AUDIT(sideExitIntoInterpreter);
else
AUDIT(timeoutIntoInterpreter);
#endif
state.innermost = innermost;
}
JS_REQUIRES_STACK bool
js_MonitorLoopEdge(JSContext* cx, uintN& inlineCallCount)
{
JSTraceMonitor* tm = &JS_TRACE_MONITOR(cx);
/* Is the recorder currently active? */
if (tm->recorder) {
jsbytecode* innerLoopHeaderPC = cx->fp->regs->pc;
if (js_RecordLoopEdge(cx, tm->recorder, inlineCallCount))
return true;
/*
* js_RecordLoopEdge will invoke an inner tree if we have a matching one. If we
* arrive here, that tree didn't run to completion and instead we mis-matched
* or the inner tree took a side exit other than the loop exit. We are thus
* no longer guaranteed to be parked on the same loop header js_MonitorLoopEdge
* was called for. In fact, this might not even be a loop header at all. Hence
* if the program counter no longer hovers over the inner loop header, return to
* the interpreter and do not attempt to trigger or record a new tree at this
* location.
*/
if (innerLoopHeaderPC != cx->fp->regs->pc)
return false;
}
JS_ASSERT(!tm->recorder);
/* Check the pool of reserved doubles (this might trigger a GC). */
if (tm->reservedDoublePoolPtr < (tm->reservedDoublePool + MAX_NATIVE_STACK_SLOTS) &&
!js_ReplenishReservedPool(cx, tm)) {
return false; /* Out of memory, don't try to record now. */
}
/* Make sure the shape of the global object still matches (this might flush the JIT cache). */
JSObject* globalObj = JS_GetGlobalForObject(cx, cx->fp->scopeChain);
uint32 globalShape = -1;
SlotList* globalSlots = NULL;
if (!CheckGlobalObjectShape(cx, tm, globalObj, &globalShape, &globalSlots)) {
js_Backoff(cx, cx->fp->regs->pc);
return false;
}
/* Do not enter the JIT code with a pending operation callback. */
if (cx->operationCallbackFlag)
return false;
jsbytecode* pc = cx->fp->regs->pc;
uint32 argc = cx->fp->argc;
Fragment* f = getLoop(tm, pc, globalObj, globalShape, argc);
if (!f)
f = getAnchor(tm, pc, globalObj, globalShape, argc);
if (!f) {
FlushJITCache(cx);
return false;
}
/* If we have no code in the anchor and no peers, we definitively won't be able to
activate any trees so, start compiling. */
if (!f->code() && !f->peer) {
record:
if (++f->hits() < HOTLOOP)
return false;
/* We can give RecordTree the root peer. If that peer is already taken, it will
walk the peer list and find us a free slot or allocate a new tree if needed. */
return js_RecordTree(cx, tm, f->first, NULL, 0, globalObj, globalShape,
globalSlots, argc);
}
debug_only_v(printf("Looking for compat peer %d@%d, from %p (ip: %p)\n",
js_FramePCToLineNumber(cx, cx->fp),
FramePCOffset(cx->fp), (void*)f, f->ip);)
uintN count;
Fragment* match = js_FindVMCompatiblePeer(cx, f, count);
if (!match) {
if (count < MAXPEERS)
goto record;
/* If we hit the max peers ceiling, don't try to lookup fragments all the time. Thats
expensive. This must be a rather type-unstable loop. */
debug_only_v(printf("Blacklisted: too many peer trees.\n");)
js_Blacklist((jsbytecode*) f->root->ip);
return false;
}
VMSideExit* lr = NULL;
VMSideExit* innermostNestedGuard = NULL;
lr = js_ExecuteTree(cx, match, inlineCallCount, &innermostNestedGuard);
if (!lr)
return false;
/* If we exit on a branch, or on a tree call guard, try to grow the inner tree (in case
of a branch exit), or the tree nested around the tree we exited from (in case of the
tree call guard). */
switch (lr->exitType) {
case UNSTABLE_LOOP_EXIT:
return js_AttemptToStabilizeTree(cx, lr, NULL, NULL);
case BRANCH_EXIT:
case CASE_EXIT:
return js_AttemptToExtendTree(cx, lr, NULL, NULL);
case LOOP_EXIT:
if (innermostNestedGuard)
return js_AttemptToExtendTree(cx, innermostNestedGuard, lr, NULL);
return false;
default:
/* No, this was an unusual exit (i.e. out of memory/GC), so just resume interpretation. */
return false;
}
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::monitorRecording(JSContext* cx, TraceRecorder* tr, JSOp op)
{
/* Process needFlush and deepAbort() requests now. */
if (JS_TRACE_MONITOR(cx).needFlush) {
FlushJITCache(cx);
return JSRS_STOP;
}
if (tr->wasDeepAborted()) {
js_AbortRecording(cx, "deep abort requested");
return JSRS_STOP;
}
JS_ASSERT(!tr->fragment->lastIns);
/*
* Clear one-shot state used to communicate between record_JSOP_CALL and post-
* opcode-case-guts record hook (record_NativeCallComplete).
*/
tr->pendingTraceableNative = NULL;
tr->newobj_ins = NULL;
debug_only_v(js_Disassemble1(cx, cx->fp->script, cx->fp->regs->pc,
cx->fp->imacpc ? 0 : cx->fp->regs->pc - cx->fp->script->code,
!cx->fp->imacpc, stdout);)
/* If op is not a break or a return from a loop, continue recording and follow the
trace. We check for imacro-calling bytecodes inside each switch case to resolve
the if (JSOP_IS_IMACOP(x)) conditions at compile time. */
JSRecordingStatus status;
#ifdef DEBUG
bool wasInImacro = (cx->fp->imacpc != NULL);
#endif
switch (op) {
default:
status = JSRS_ERROR;
goto stop_recording;
# define OPDEF(x,val,name,token,length,nuses,ndefs,prec,format) \
case x: \
status = tr->record_##x(); \
if (JSOP_IS_IMACOP(x)) \
goto imacro; \
break;
# include "jsopcode.tbl"
# undef OPDEF
}
JS_ASSERT(status != JSRS_IMACRO);
JS_ASSERT_IF(!wasInImacro, cx->fp->imacpc == NULL);
/* Process deepAbort() requests now. */
if (tr->wasDeepAborted()) {
js_AbortRecording(cx, "deep abort requested");
return JSRS_STOP;
}
if (JS_TRACE_MONITOR(cx).fragmento->assm()->error()) {
js_AbortRecording(cx, "error during recording");
return JSRS_STOP;
}
if (tr->lirbuf->outOMem() ||
js_OverfullFragmento(&JS_TRACE_MONITOR(cx), JS_TRACE_MONITOR(cx).fragmento)) {
js_AbortRecording(cx, "no more LIR memory");
FlushJITCache(cx);
return JSRS_STOP;
}
imacro:
if (!STATUS_ABORTS_RECORDING(status))
return status;
stop_recording:
/* If we recorded the end of the trace, destroy the recorder now. */
if (tr->fragment->lastIns) {
js_DeleteRecorder(cx);
return status;
}
/* Looks like we encountered an error condition. Abort recording. */
js_AbortRecording(cx, js_CodeName[op]);
return status;
}
JS_REQUIRES_STACK void
js_AbortRecording(JSContext* cx, const char* reason)
{
JSTraceMonitor* tm = &JS_TRACE_MONITOR(cx);
JS_ASSERT(tm->recorder != NULL);
AUDIT(recorderAborted);
/* Abort the trace and blacklist its starting point. */
Fragment* f = tm->recorder->getFragment();
/*
* If the recorder already had its fragment disposed, or we actually finished
* recording and this recorder merely is passing through the deep abort state
* to the next recorder on the stack, just destroy the recorder. There is
* nothing to abort.
*/
if (!f || f->lastIns) {
js_DeleteRecorder(cx);
return;
}
JS_ASSERT(!f->vmprivate);
#ifdef DEBUG
TreeInfo* ti = tm->recorder->getTreeInfo();
debug_only_a(printf("Abort recording of tree %s:%d@%d at %s:%d@%d: %s.\n",
ti->treeFileName,
ti->treeLineNumber,
ti->treePCOffset,
cx->fp->script->filename,
js_FramePCToLineNumber(cx, cx->fp),
FramePCOffset(cx->fp),
reason);)
#endif
js_Backoff(cx, (jsbytecode*) f->root->ip, f->root);
/*
* If js_DeleteRecorder flushed the code cache, we can't rely on f any more.
*/
if (!js_DeleteRecorder(cx))
return;
/*
* If this is the primary trace and we didn't succeed compiling, trash the
* TreeInfo object.
*/
if (!f->code() && (f->root == f))
js_TrashTree(cx, f);
}
#if defined NANOJIT_IA32
static bool
js_CheckForSSE2()
{
int features = 0;
#if defined _MSC_VER
__asm
{
pushad
mov eax, 1
cpuid
mov features, edx
popad
}
#elif defined __GNUC__
asm("xchg %%esi, %%ebx\n" /* we can't clobber ebx on gcc (PIC register) */
"mov $0x01, %%eax\n"
"cpuid\n"
"mov %%edx, %0\n"
"xchg %%esi, %%ebx\n"
: "=m" (features)
: /* We have no inputs */
: "%eax", "%esi", "%ecx", "%edx"
);
#elif defined __SUNPRO_C || defined __SUNPRO_CC
asm("push %%ebx\n"
"mov $0x01, %%eax\n"
"cpuid\n"
"pop %%ebx\n"
: "=d" (features)
: /* We have no inputs */
: "%eax", "%ecx"
);
#endif
return (features & (1<<26)) != 0;
}
#endif
#if defined(NANOJIT_ARM)
#if defined(_MSC_VER) && defined(WINCE)
// these come in from jswince.asm
extern "C" int js_arm_try_armv6t2_op();
extern "C" int js_arm_try_vfp_op();
static bool
js_arm_check_armv6t2() {
bool ret = false;
__try {
js_arm_try_armv6t2_op();
ret = true;
} __except(GetExceptionCode() == EXCEPTION_ILLEGAL_INSTRUCTION) {
ret = false;
}
return ret;
}
static bool
js_arm_check_vfp() {
bool ret = false;
__try {
js_arm_try_vfp_op();
ret = true;
} __except(GetExceptionCode() == EXCEPTION_ILLEGAL_INSTRUCTION) {
ret = false;
}
return ret;
}
#elif defined(__GNUC__) && defined(AVMPLUS_LINUX)
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <string.h>
#include <elf.h>
static bool arm_has_v7 = false;
static bool arm_has_v6 = false;
static bool arm_has_vfp = false;
static bool arm_has_neon = false;
static bool arm_has_iwmmxt = false;
static bool arm_tests_initialized = false;
static void
arm_read_auxv() {
int fd;
Elf32_auxv_t aux;
fd = open("/proc/self/auxv", O_RDONLY);
if (fd > 0) {
while (read(fd, &aux, sizeof(Elf32_auxv_t))) {
if (aux.a_type == AT_HWCAP) {
uint32_t hwcap = aux.a_un.a_val;
if (getenv("ARM_FORCE_HWCAP"))
hwcap = strtoul(getenv("ARM_FORCE_HWCAP"), NULL, 0);
// hardcode these values to avoid depending on specific versions
// of the hwcap header, e.g. HWCAP_NEON
arm_has_vfp = (hwcap & 64) != 0;
arm_has_iwmmxt = (hwcap & 512) != 0;
// this flag is only present on kernel 2.6.29
arm_has_neon = (hwcap & 4096) != 0;
} else if (aux.a_type == AT_PLATFORM) {
const char *plat = (const char*) aux.a_un.a_val;
if (getenv("ARM_FORCE_PLATFORM"))
plat = getenv("ARM_FORCE_PLATFORM");
if (strncmp(plat, "v7l", 3) == 0) {
arm_has_v7 = true;
arm_has_v6 = true;
} else if (strncmp(plat, "v6l", 3) == 0) {
arm_has_v6 = true;
}
}
}
close (fd);
// if we don't have 2.6.29, we have to do this hack; set
// the env var to trust HWCAP.
if (!getenv("ARM_TRUST_HWCAP") && arm_has_v7)
arm_has_neon = true;
}
arm_tests_initialized = true;
}
static bool
js_arm_check_armv6t2() {
if (!arm_tests_initialized)
arm_read_auxv();
return arm_has_v7;
}
static bool
js_arm_check_vfp() {
if (!arm_tests_initialized)
arm_read_auxv();
return arm_has_vfp;
}
#else
#warning Not sure how to check for armv6t2 and vfp on your platform, assuming neither present.
static bool
js_arm_check_armv6t2() { return false; }
static bool
js_arm_check_vfp() { return false; }
#endif
#endif /* NANOJIT_ARM */
#define K *1024
#define M K K
#define G K M
void
js_SetMaxCodeCacheBytes(JSContext* cx, uint32 bytes)
{
JSTraceMonitor* tm = &JS_THREAD_DATA(cx)->traceMonitor;
JS_ASSERT(tm->fragmento && tm->reFragmento);
if (bytes > 1 G)
bytes = 1 G;
if (bytes < 128 K)
bytes = 128 K;
tm->maxCodeCacheBytes = bytes;
}
void
js_InitJIT(JSTraceMonitor *tm)
{
if (!did_we_check_processor_features) {
#if defined NANOJIT_IA32
avmplus::AvmCore::config.use_cmov =
avmplus::AvmCore::config.sse2 = js_CheckForSSE2();
#endif
#if defined NANOJIT_ARM
avmplus::AvmCore::config.vfp = js_arm_check_vfp();
avmplus::AvmCore::config.soft_float = !avmplus::AvmCore::config.vfp;
avmplus::AvmCore::config.v6t2 = js_arm_check_armv6t2();
#endif
did_we_check_processor_features = true;
}
/*
* Set the default size for the code cache to 16MB.
*/
tm->maxCodeCacheBytes = 16 M;
if (!tm->recordAttempts.ops) {
JS_DHashTableInit(&tm->recordAttempts, JS_DHashGetStubOps(),
NULL, sizeof(PCHashEntry),
JS_DHASH_DEFAULT_CAPACITY(PC_HASH_COUNT));
}
if (!tm->fragmento) {
JS_ASSERT(!tm->reservedDoublePool);
Fragmento* fragmento = new (&gc) Fragmento(core, 32);
verbose_only(fragmento->labels = new (&gc) LabelMap(core, NULL);)
tm->fragmento = fragmento;
tm->lirbuf = new (&gc) LirBuffer(fragmento, NULL);
#ifdef DEBUG
tm->lirbuf->names = new (&gc) LirNameMap(&gc, NULL, tm->fragmento->labels);
#endif
for (size_t i = 0; i < MONITOR_N_GLOBAL_STATES; ++i) {
tm->globalStates[i].globalShape = -1;
JS_ASSERT(!tm->globalStates[i].globalSlots);
tm->globalStates[i].globalSlots = new (&gc) SlotList();
}
tm->reservedDoublePoolPtr = tm->reservedDoublePool = new jsval[MAX_NATIVE_STACK_SLOTS];
memset(tm->vmfragments, 0, sizeof(tm->vmfragments));
}
if (!tm->reFragmento) {
Fragmento* fragmento = new (&gc) Fragmento(core, 32);
verbose_only(fragmento->labels = new (&gc) LabelMap(core, NULL);)
tm->reFragmento = fragmento;
tm->reLirBuf = new (&gc) LirBuffer(fragmento, NULL);
}
#if !defined XP_WIN
debug_only(memset(&jitstats, 0, sizeof(jitstats)));
#endif
}
void
js_FinishJIT(JSTraceMonitor *tm)
{
#ifdef JS_JIT_SPEW
if (js_verboseStats && jitstats.recorderStarted) {
printf("recorder: started(%llu), aborted(%llu), completed(%llu), different header(%llu), "
"trees trashed(%llu), slot promoted(%llu), unstable loop variable(%llu), "
"breaks(%llu), returns(%llu), unstableInnerCalls(%llu)\n",
jitstats.recorderStarted, jitstats.recorderAborted, jitstats.traceCompleted,
jitstats.returnToDifferentLoopHeader, jitstats.treesTrashed, jitstats.slotPromoted,
jitstats.unstableLoopVariable, jitstats.breakLoopExits, jitstats.returnLoopExits,
jitstats.noCompatInnerTrees);
printf("monitor: triggered(%llu), exits(%llu), type mismatch(%llu), "
"global mismatch(%llu)\n", jitstats.traceTriggered, jitstats.sideExitIntoInterpreter,
jitstats.typeMapMismatchAtEntry, jitstats.globalShapeMismatchAtEntry);
}
#endif
if (tm->fragmento != NULL) {
JS_ASSERT(tm->reservedDoublePool);
verbose_only(delete tm->fragmento->labels;)
#ifdef DEBUG
delete tm->lirbuf->names;
tm->lirbuf->names = NULL;
#endif
delete tm->lirbuf;
tm->lirbuf = NULL;
if (tm->recordAttempts.ops)
JS_DHashTableFinish(&tm->recordAttempts);
for (size_t i = 0; i < FRAGMENT_TABLE_SIZE; ++i) {
VMFragment* f = tm->vmfragments[i];
while(f) {
VMFragment* next = f->next;
tm->fragmento->clearFragment(f);
f = next;
}
tm->vmfragments[i] = NULL;
}
delete tm->fragmento;
tm->fragmento = NULL;
for (size_t i = 0; i < MONITOR_N_GLOBAL_STATES; ++i) {
JS_ASSERT(tm->globalStates[i].globalSlots);
delete tm->globalStates[i].globalSlots;
}
delete[] tm->reservedDoublePool;
tm->reservedDoublePool = tm->reservedDoublePoolPtr = NULL;
}
if (tm->reFragmento != NULL) {
delete tm->reLirBuf;
verbose_only(delete tm->reFragmento->labels;)
delete tm->reFragmento;
}
}
void
TraceRecorder::pushAbortStack()
{
JSTraceMonitor* tm = &JS_TRACE_MONITOR(cx);
JS_ASSERT(tm->abortStack != this);
nextRecorderToAbort = tm->abortStack;
tm->abortStack = this;
}
void
TraceRecorder::popAbortStack()
{
JSTraceMonitor* tm = &JS_TRACE_MONITOR(cx);
JS_ASSERT(tm->abortStack == this);
tm->abortStack = nextRecorderToAbort;
nextRecorderToAbort = NULL;
}
void
js_PurgeJITOracle()
{
oracle.clear();
}
static JSDHashOperator
js_PurgeScriptRecordingAttempts(JSDHashTable *table,
JSDHashEntryHdr *hdr,
uint32 number, void *arg)
{
PCHashEntry *e = (PCHashEntry *)hdr;
JSScript *script = (JSScript *)arg;
jsbytecode *pc = (jsbytecode *)e->key;
if (JS_UPTRDIFF(pc, script->code) < script->length)
return JS_DHASH_REMOVE;
return JS_DHASH_NEXT;
}
/*
* Call 'action' for each root fragment created for 'script'.
*/
template<typename FragmentAction>
static void
js_IterateScriptFragments(JSContext* cx, JSScript* script, FragmentAction action)
{
JSTraceMonitor* tm = &JS_TRACE_MONITOR(cx);
for (size_t i = 0; i < FRAGMENT_TABLE_SIZE; ++i) {
for (VMFragment **f = &(tm->vmfragments[i]); *f; ) {
VMFragment* frag = *f;
/* Disable future use of any script-associated VMFragment.*/
if (JS_UPTRDIFF(frag->ip, script->code) < script->length) {
JS_ASSERT(frag->root == frag);
VMFragment* next = frag->next;
if (action(cx, tm, frag)) {
debug_only_v(printf("Disconnecting VMFragment %p "
"with ip %p, in range [%p,%p).\n",
(void*)frag, frag->ip, script->code,
script->code + script->length));
*f = next;
} else {
f = &((*f)->next);
}
} else {
f = &((*f)->next);
}
}
}
}
static bool
trashTreeAction(JSContext* cx, JSTraceMonitor* tm, Fragment* frag)
{
for (Fragment *p = frag; p; p = p->peer)
js_TrashTree(cx, p);
return false;
}
static bool
clearFragmentAction(JSContext* cx, JSTraceMonitor* tm, Fragment* frag)
{
tm->fragmento->clearFragment(frag);
return true;
}
JS_REQUIRES_STACK void
js_PurgeScriptFragments(JSContext* cx, JSScript* script)
{
if (!TRACING_ENABLED(cx))
return;
debug_only_v(printf("Purging fragments for JSScript %p.\n", (void*)script);)
/*
* js_TrashTree trashes dependent trees recursively, so we must do all the trashing
* before clearing in order to avoid calling js_TrashTree with a deleted fragment.
*/
js_IterateScriptFragments(cx, script, trashTreeAction);
js_IterateScriptFragments(cx, script, clearFragmentAction);
JSTraceMonitor* tm = &JS_TRACE_MONITOR(cx);
JS_DHashTableEnumerate(&(tm->recordAttempts),
js_PurgeScriptRecordingAttempts, script);
}
bool
js_OverfullFragmento(JSTraceMonitor* tm, Fragmento *fragmento)
{
/*
* You might imagine the outOMem flag on the lirbuf is sufficient
* to model the notion of "running out of memory", but there are actually
* two separate issues involved:
*
* 1. The process truly running out of memory: malloc() or mmap()
* failed.
*
* 2. The limit we put on the "intended size" of the tracemonkey code
* cache, in pages, has been exceeded.
*
* Condition 1 doesn't happen very often, but we're obliged to try to
* safely shut down and signal the rest of spidermonkey when it
* does. Condition 2 happens quite regularly.
*
* Presently, the code in this file doesn't check the outOMem condition
* often enough, and frequently misuses the unchecked results of
* lirbuffer insertions on the asssumption that it will notice the
* outOMem flag "soon enough" when it returns to the monitorRecording
* function. This turns out to be a false assumption if we use outOMem
* to signal condition 2: we regularly provoke "passing our intended
* size" and regularly fail to notice it in time to prevent writing
* over the end of an artificially self-limited LIR buffer.
*
* To mitigate, though not completely solve, this problem, we're
* modeling the two forms of memory exhaustion *separately* for the
* time being: condition 1 is handled by the outOMem flag inside
* nanojit, and condition 2 is being handled independently *here*. So
* we construct our fragmentos to use all available memory they like,
* and only report outOMem to us when there is literally no OS memory
* left. Merely purging our cache when we hit our highwater mark is
* handled by the (few) callers of this function.
*
*/
jsuint maxsz = tm->maxCodeCacheBytes;
if (fragmento == tm->fragmento) {
if (tm->prohibitFlush)
return false;
} else {
/*
* At the time of making the code cache size configurable, we were using
* 16 MB for the main code cache and 1 MB for the regular expression code
* cache. We will stick to this 16:1 ratio here until we unify the two
* code caches.
*/
maxsz /= 16;
}
return (fragmento->cacheUsed() > maxsz);
}
JS_FORCES_STACK JS_FRIEND_API(void)
js_DeepBail(JSContext *cx)
{
JS_ASSERT(JS_ON_TRACE(cx));
/*
* Exactly one context on the current thread is on trace. Find out which
* one. (Most callers cannot guarantee that it's cx.)
*/
JSTraceMonitor *tm = &JS_TRACE_MONITOR(cx);
JSContext *tracecx = tm->tracecx;
/* It's a bug if a non-FAIL_STATUS builtin gets here. */
JS_ASSERT(tracecx->bailExit);
tm->tracecx = NULL;
tm->prohibitFlush++;
debug_only_v(printf("Deep bail.\n");)
LeaveTree(*tracecx->interpState, tracecx->bailExit);
tracecx->bailExit = NULL;
InterpState* state = tracecx->interpState;
state->builtinStatus |= JSBUILTIN_BAILED;
state->deepBailSp = state->sp;
}
JS_REQUIRES_STACK jsval&
TraceRecorder::argval(unsigned n) const
{
JS_ASSERT(n < cx->fp->fun->nargs);
return cx->fp->argv[n];
}
JS_REQUIRES_STACK jsval&
TraceRecorder::varval(unsigned n) const
{
JS_ASSERT(n < cx->fp->script->nslots);
return cx->fp->slots[n];
}
JS_REQUIRES_STACK jsval&
TraceRecorder::stackval(int n) const
{
jsval* sp = cx->fp->regs->sp;
return sp[n];
}
JS_REQUIRES_STACK LIns*
TraceRecorder::scopeChain() const
{
return lir->insLoad(LIR_ldp,
lir->insLoad(LIR_ldp, cx_ins, offsetof(JSContext, fp)),
offsetof(JSStackFrame, scopeChain));
}
static inline bool
FrameInRange(JSStackFrame* fp, JSStackFrame *target, unsigned callDepth)
{
while (fp != target) {
if (callDepth-- == 0)
return false;
if (!(fp = fp->down))
return false;
}
return true;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::activeCallOrGlobalSlot(JSObject* obj, jsval*& vp)
{
// Lookup a name in the scope chain, arriving at a property either in the
// global object or some call object's fp->slots, and import that property
// into the trace's native stack frame. This could theoretically do *lookup*
// through the property cache, but there is little performance to be gained
// by doing so since at trace-execution time the underlying object (call
// object or global object) will not be consulted at all: the jsval*
// returned from this function will map (in the tracker) to a LIns* directly
// defining a slot in the trace's native stack.
JS_ASSERT(obj != globalObj);
JSAtom* atom = atoms[GET_INDEX(cx->fp->regs->pc)];
JSObject* obj2;
JSProperty* prop;
if (!js_FindProperty(cx, ATOM_TO_JSID(atom), &obj, &obj2, &prop))
ABORT_TRACE_ERROR("error in js_FindProperty");
if (!prop)
ABORT_TRACE("failed to find name in non-global scope chain");
if (obj == globalObj) {
JSScopeProperty* sprop = (JSScopeProperty*) prop;
if (obj2 != obj) {
OBJ_DROP_PROPERTY(cx, obj2, prop);
ABORT_TRACE("prototype property");
}
if (!isValidSlot(OBJ_SCOPE(obj), sprop)) {
OBJ_DROP_PROPERTY(cx, obj2, prop);
return JSRS_STOP;
}
if (!lazilyImportGlobalSlot(sprop->slot)) {
OBJ_DROP_PROPERTY(cx, obj2, prop);
ABORT_TRACE("lazy import of global slot failed");
}
vp = &STOBJ_GET_SLOT(obj, sprop->slot);
OBJ_DROP_PROPERTY(cx, obj2, prop);
return JSRS_CONTINUE;
}
if (wasDeepAborted())
ABORT_TRACE("deep abort from property lookup");
if (obj == obj2 && OBJ_GET_CLASS(cx, obj) == &js_CallClass) {
JSStackFrame* cfp = (JSStackFrame*) JS_GetPrivate(cx, obj);
if (cfp && FrameInRange(cx->fp, cfp, callDepth)) {
JSScopeProperty* sprop = (JSScopeProperty*) prop;
uint32 setflags = (js_CodeSpec[*cx->fp->regs->pc].format & (JOF_SET | JOF_INCDEC | JOF_FOR));
if (setflags && (sprop->attrs & JSPROP_READONLY))
ABORT_TRACE("writing to a read-only property");
uintN slot = sprop->shortid;
vp = NULL;
if (sprop->getter == js_GetCallArg) {
JS_ASSERT(slot < cfp->fun->nargs);
vp = &cfp->argv[slot];
} else if (sprop->getter == js_GetCallVar) {
JS_ASSERT(slot < cfp->script->nslots);
vp = &cfp->slots[slot];
}
OBJ_DROP_PROPERTY(cx, obj2, prop);
if (!vp)
ABORT_TRACE("dynamic property of Call object");
return JSRS_CONTINUE;
}
}
OBJ_DROP_PROPERTY(cx, obj2, prop);
ABORT_TRACE("fp->scopeChain is not global or active call object");
}
JS_REQUIRES_STACK LIns*
TraceRecorder::arg(unsigned n)
{
return get(&argval(n));
}
JS_REQUIRES_STACK void
TraceRecorder::arg(unsigned n, LIns* i)
{
set(&argval(n), i);
}
JS_REQUIRES_STACK LIns*
TraceRecorder::var(unsigned n)
{
return get(&varval(n));
}
JS_REQUIRES_STACK void
TraceRecorder::var(unsigned n, LIns* i)
{
set(&varval(n), i);
}
JS_REQUIRES_STACK LIns*
TraceRecorder::stack(int n)
{
return get(&stackval(n));
}
JS_REQUIRES_STACK void
TraceRecorder::stack(int n, LIns* i)
{
set(&stackval(n), i, n >= 0);
}
JS_REQUIRES_STACK LIns*
TraceRecorder::alu(LOpcode v, jsdouble v0, jsdouble v1, LIns* s0, LIns* s1)
{
if (v == LIR_fadd || v == LIR_fsub) {
jsdouble r;
if (v == LIR_fadd)
r = v0 + v1;
else
r = v0 - v1;
/*
* Calculate the result of the addition for the current values. If the
* value is not within the integer range, don't even try to demote
* here.
*/
if (!JSDOUBLE_IS_NEGZERO(r) && (jsint(r) == r) && isPromoteInt(s0) && isPromoteInt(s1)) {
LIns* d0 = ::demote(lir, s0);
LIns* d1 = ::demote(lir, s1);
/*
* If the inputs are constant, generate an integer constant for
* this operation.
*/
if (d0->isconst() && d1->isconst())
return lir->ins1(LIR_i2f, lir->insImm(jsint(r)));
/*
* Speculatively generate code that will perform the addition over
* the integer inputs as an integer addition/subtraction and exit
* if that fails.
*/
v = (LOpcode)((int)v & ~LIR64);
LIns* result = lir->ins2(v, d0, d1);
if (!result->isconst() && (!overflowSafe(d0) || !overflowSafe(d1))) {
VMSideExit* exit = snapshot(OVERFLOW_EXIT);
lir->insGuard(LIR_xt, lir->ins1(LIR_ov, result), createGuardRecord(exit));
}
return lir->ins1(LIR_i2f, result);
}
/*
* The result doesn't fit into the integer domain, so either generate
* a floating point constant or a floating point operation.
*/
if (s0->isconst() && s1->isconst())
return lir->insImmf(r);
return lir->ins2(v, s0, s1);
}
return lir->ins2(v, s0, s1);
}
LIns*
TraceRecorder::f2i(LIns* f)
{
return lir->insCall(&js_DoubleToInt32_ci, &f);
}
JS_REQUIRES_STACK LIns*
TraceRecorder::makeNumberInt32(LIns* f)
{
JS_ASSERT(f->isQuad());
LIns* x;
if (!isPromote(f)) {
x = f2i(f);
guard(true, lir->ins2(LIR_feq, f, lir->ins1(LIR_i2f, x)), MISMATCH_EXIT);
} else {
x = ::demote(lir, f);
}
return x;
}
JS_REQUIRES_STACK LIns*
TraceRecorder::stringify(jsval& v)
{
LIns* v_ins = get(&v);
if (JSVAL_IS_STRING(v))
return v_ins;
LIns* args[] = { v_ins, cx_ins };
const CallInfo* ci;
if (JSVAL_IS_NUMBER(v)) {
ci = &js_NumberToString_ci;
} else if (JSVAL_TAG(v) == JSVAL_BOOLEAN) {
ci = &js_BooleanOrUndefinedToString_ci;
} else {
/*
* Callers must deal with non-primitive (non-null object) values by
* calling an imacro. We don't try to guess about which imacro, with
* what valueOf hint, here.
*/
JS_ASSERT(JSVAL_IS_NULL(v));
return INS_CONSTPTR(ATOM_TO_STRING(cx->runtime->atomState.nullAtom));
}
v_ins = lir->insCall(ci, args);
guard(false, lir->ins_eq0(v_ins), OOM_EXIT);
return v_ins;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::call_imacro(jsbytecode* imacro)
{
JSStackFrame* fp = cx->fp;
JSFrameRegs* regs = fp->regs;
// We can't nest imacros.
if (fp->imacpc)
return JSRS_STOP;
fp->imacpc = regs->pc;
regs->pc = imacro;
atoms = COMMON_ATOMS_START(&cx->runtime->atomState);
return JSRS_IMACRO;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::ifop()
{
jsval& v = stackval(-1);
LIns* v_ins = get(&v);
bool cond;
LIns* x;
if (JSVAL_IS_NULL(v)) {
cond = false;
x = lir->insImm(0);
} else if (!JSVAL_IS_PRIMITIVE(v)) {
cond = true;
x = lir->insImm(1);
} else if (JSVAL_TAG(v) == JSVAL_BOOLEAN) {
/* Test for boolean is true, negate later if we are testing for false. */
cond = JSVAL_TO_PSEUDO_BOOLEAN(v) == JS_TRUE;
x = lir->ins2i(LIR_eq, v_ins, 1);
} else if (isNumber(v)) {
jsdouble d = asNumber(v);
cond = !JSDOUBLE_IS_NaN(d) && d;
x = lir->ins2(LIR_and,
lir->ins2(LIR_feq, v_ins, v_ins),
lir->ins_eq0(lir->ins2(LIR_feq, v_ins, lir->insImmq(0))));
} else if (JSVAL_IS_STRING(v)) {
cond = JSSTRING_LENGTH(JSVAL_TO_STRING(v)) != 0;
x = lir->ins2(LIR_piand,
lir->insLoad(LIR_ldp,
v_ins,
(int)offsetof(JSString, length)),
INS_CONSTPTR(reinterpret_cast<void *>(JSSTRING_LENGTH_MASK)));
} else {
JS_NOT_REACHED("ifop");
return JSRS_STOP;
}
jsbytecode* pc = cx->fp->regs->pc;
emitIf(pc, cond, x);
return checkTraceEnd(pc);
}
#ifdef NANOJIT_IA32
/* Record LIR for a tableswitch or tableswitchx op. We record LIR only the
"first" time we hit the op. Later, when we start traces after exiting that
trace, we just patch. */
JS_REQUIRES_STACK LIns*
TraceRecorder::tableswitch()
{
jsval& v = stackval(-1);
if (!isNumber(v))
return NULL;
/* no need to guard if condition is constant */
LIns* v_ins = f2i(get(&v));
if (v_ins->isconst() || v_ins->isconstq())
return NULL;
jsbytecode* pc = cx->fp->regs->pc;
/* Starting a new trace after exiting a trace via switch. */
if (anchor &&
(anchor->exitType == CASE_EXIT || anchor->exitType == DEFAULT_EXIT) &&
fragment->ip == pc) {
return NULL;
}
/* Decode jsop. */
jsint low, high;
if (*pc == JSOP_TABLESWITCH) {
pc += JUMP_OFFSET_LEN;
low = GET_JUMP_OFFSET(pc);
pc += JUMP_OFFSET_LEN;
high = GET_JUMP_OFFSET(pc);
} else {
pc += JUMPX_OFFSET_LEN;
low = GET_JUMPX_OFFSET(pc);
pc += JUMPX_OFFSET_LEN;
high = GET_JUMPX_OFFSET(pc);
}
/* Really large tables won't fit in a page. This is a conservative check.
If it matters in practice we need to go off-page. */
if ((high + 1 - low) * sizeof(intptr_t*) + 128 > (unsigned) LARGEST_UNDERRUN_PROT) {
// This throws away the return value of switchop but it seems
// ok because switchop always returns true.
(void) switchop();
return NULL;
}
/* Generate switch LIR. */
LIns* si_ins = lir_buf_writer->skip(sizeof(SwitchInfo));
SwitchInfo* si = (SwitchInfo*) si_ins->payload();
si->count = high + 1 - low;
si->table = 0;
si->index = (uint32) -1;
LIns* diff = lir->ins2(LIR_sub, v_ins, lir->insImm(low));
LIns* cmp = lir->ins2(LIR_ult, diff, lir->insImm(si->count));
lir->insGuard(LIR_xf, cmp, createGuardRecord(snapshot(DEFAULT_EXIT)));
lir->insStore(diff, lir->insImmPtr(&si->index), lir->insImm(0));
VMSideExit* exit = snapshot(CASE_EXIT);
exit->switchInfo = si;
return lir->insGuard(LIR_xtbl, diff, createGuardRecord(exit));
}
#endif
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::switchop()
{
jsval& v = stackval(-1);
LIns* v_ins = get(&v);
/* no need to guard if condition is constant */
if (v_ins->isconst() || v_ins->isconstq())
return JSRS_CONTINUE;
if (isNumber(v)) {
jsdouble d = asNumber(v);
guard(true,
addName(lir->ins2(LIR_feq, v_ins, lir->insImmf(d)),
"guard(switch on numeric)"),
BRANCH_EXIT);
} else if (JSVAL_IS_STRING(v)) {
LIns* args[] = { v_ins, INS_CONSTPTR(JSVAL_TO_STRING(v)) };
guard(true,
addName(lir->ins_eq0(lir->ins_eq0(lir->insCall(&js_EqualStrings_ci, args))),
"guard(switch on string)"),
BRANCH_EXIT);
} else if (JSVAL_TAG(v) == JSVAL_BOOLEAN) {
guard(true,
addName(lir->ins2(LIR_eq, v_ins, lir->insImm(JSVAL_TO_PUBLIC_PSEUDO_BOOLEAN(v))),
"guard(switch on boolean)"),
BRANCH_EXIT);
} else {
ABORT_TRACE("switch on object or null");
}
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::inc(jsval& v, jsint incr, bool pre)
{
LIns* v_ins = get(&v);
CHECK_STATUS(inc(v, v_ins, incr, pre));
set(&v, v_ins);
return JSRS_CONTINUE;
}
/*
* On exit, v_ins is the incremented unboxed value, and the appropriate
* value (pre- or post-increment as described by pre) is stacked.
*/
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::inc(jsval& v, LIns*& v_ins, jsint incr, bool pre)
{
if (!isNumber(v))
ABORT_TRACE("can only inc numbers");
LIns* v_after = alu(LIR_fadd, asNumber(v), incr, v_ins, lir->insImmf(incr));
const JSCodeSpec& cs = js_CodeSpec[*cx->fp->regs->pc];
JS_ASSERT(cs.ndefs == 1);
stack(-cs.nuses, pre ? v_after : v_ins);
v_ins = v_after;
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::incProp(jsint incr, bool pre)
{
jsval& l = stackval(-1);
if (JSVAL_IS_PRIMITIVE(l))
ABORT_TRACE("incProp on primitive");
JSObject* obj = JSVAL_TO_OBJECT(l);
LIns* obj_ins = get(&l);
uint32 slot;
LIns* v_ins;
CHECK_STATUS(prop(obj, obj_ins, slot, v_ins));
if (slot == SPROP_INVALID_SLOT)
ABORT_TRACE("incProp on invalid slot");
jsval& v = STOBJ_GET_SLOT(obj, slot);
CHECK_STATUS(inc(v, v_ins, incr, pre));
box_jsval(v, v_ins);
LIns* dslots_ins = NULL;
stobj_set_slot(obj_ins, slot, dslots_ins, v_ins);
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::incElem(jsint incr, bool pre)
{
jsval& r = stackval(-1);
jsval& l = stackval(-2);
jsval* vp;
LIns* v_ins;
LIns* addr_ins;
if (!JSVAL_IS_OBJECT(l) || !JSVAL_IS_INT(r) ||
!guardDenseArray(JSVAL_TO_OBJECT(l), get(&l))) {
return JSRS_STOP;
}
CHECK_STATUS(denseArrayElement(l, r, vp, v_ins, addr_ins));
if (!addr_ins) // if we read a hole, abort
return JSRS_STOP;
CHECK_STATUS(inc(*vp, v_ins, incr, pre));
box_jsval(*vp, v_ins);
lir->insStorei(v_ins, addr_ins, 0);
return JSRS_CONTINUE;
}
static bool
evalCmp(LOpcode op, double l, double r)
{
bool cond;
switch (op) {
case LIR_feq:
cond = (l == r);
break;
case LIR_flt:
cond = l < r;
break;
case LIR_fgt:
cond = l > r;
break;
case LIR_fle:
cond = l <= r;
break;
case LIR_fge:
cond = l >= r;
break;
default:
JS_NOT_REACHED("unexpected comparison op");
return false;
}
return cond;
}
static bool
evalCmp(LOpcode op, JSString* l, JSString* r)
{
if (op == LIR_feq)
return js_EqualStrings(l, r);
return evalCmp(op, js_CompareStrings(l, r), 0);
}
JS_REQUIRES_STACK void
TraceRecorder::strictEquality(bool equal, bool cmpCase)
{
jsval& r = stackval(-1);
jsval& l = stackval(-2);
LIns* l_ins = get(&l);
LIns* r_ins = get(&r);
LIns* x;
bool cond;
uint8 ltag = getPromotedType(l);
if (ltag != getPromotedType(r)) {
cond = !equal;
x = lir->insImm(cond);
} else if (ltag == JSVAL_STRING) {
LIns* args[] = { r_ins, l_ins };
x = lir->ins2i(LIR_eq, lir->insCall(&js_EqualStrings_ci, args), equal);
cond = js_EqualStrings(JSVAL_TO_STRING(l), JSVAL_TO_STRING(r));
} else {
LOpcode op = (ltag != JSVAL_DOUBLE) ? LIR_eq : LIR_feq;
x = lir->ins2(op, l_ins, r_ins);
if (!equal)
x = lir->ins_eq0(x);
cond = (ltag == JSVAL_DOUBLE)
? asNumber(l) == asNumber(r)
: l == r;
}
cond = (cond == equal);
if (cmpCase) {
/* Only guard if the same path may not always be taken. */
if (!x->isconst())
guard(cond, x, BRANCH_EXIT);
return;
}
set(&l, x);
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::equality(bool negate, bool tryBranchAfterCond)
{
jsval& rval = stackval(-1);
jsval& lval = stackval(-2);
LIns* l_ins = get(&lval);
LIns* r_ins = get(&rval);
return equalityHelper(lval, rval, l_ins, r_ins, negate, tryBranchAfterCond, lval);
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::equalityHelper(jsval l, jsval r, LIns* l_ins, LIns* r_ins,
bool negate, bool tryBranchAfterCond,
jsval& rval)
{
bool fp = false;
bool cond;
LIns* args[] = { NULL, NULL };
/*
* The if chain below closely mirrors that found in 11.9.3, in general
* deviating from that ordering of ifs only to account for SpiderMonkey's
* conflation of booleans and undefined and for the possibility of
* confusing objects and null. Note carefully the spec-mandated recursion
* in the final else clause, which terminates because Number == T recurs
* only if T is Object, but that must recur again to convert Object to
* primitive, and ToPrimitive throws if the object cannot be converted to
* a primitive value (which would terminate recursion).
*/
if (getPromotedType(l) == getPromotedType(r)) {
if (JSVAL_TAG(l) == JSVAL_OBJECT || JSVAL_TAG(l) == JSVAL_BOOLEAN) {
cond = (l == r);
} else if (JSVAL_IS_STRING(l)) {
args[0] = r_ins, args[1] = l_ins;
l_ins = lir->insCall(&js_EqualStrings_ci, args);
r_ins = lir->insImm(1);
cond = js_EqualStrings(JSVAL_TO_STRING(l), JSVAL_TO_STRING(r));
} else {
JS_ASSERT(isNumber(l) && isNumber(r));
cond = (asNumber(l) == asNumber(r));
fp = true;
}
} else if (JSVAL_IS_NULL(l) && JSVAL_TAG(r) == JSVAL_BOOLEAN) {
l_ins = lir->insImm(JSVAL_TO_PSEUDO_BOOLEAN(JSVAL_VOID));
cond = (r == JSVAL_VOID);
} else if (JSVAL_TAG(l) == JSVAL_BOOLEAN && JSVAL_IS_NULL(r)) {
r_ins = lir->insImm(JSVAL_TO_PSEUDO_BOOLEAN(JSVAL_VOID));
cond = (l == JSVAL_VOID);
} else if (isNumber(l) && JSVAL_IS_STRING(r)) {
args[0] = r_ins, args[1] = cx_ins;
r_ins = lir->insCall(&js_StringToNumber_ci, args);
cond = (asNumber(l) == js_StringToNumber(cx, JSVAL_TO_STRING(r)));
fp = true;
} else if (JSVAL_IS_STRING(l) && isNumber(r)) {
args[0] = l_ins, args[1] = cx_ins;
l_ins = lir->insCall(&js_StringToNumber_ci, args);
cond = (js_StringToNumber(cx, JSVAL_TO_STRING(l)) == asNumber(r));
fp = true;
} else {
if (JSVAL_TAG(l) == JSVAL_BOOLEAN) {
bool isVoid = JSVAL_IS_VOID(l);
guard(isVoid,
lir->ins2(LIR_eq, l_ins, INS_CONST(JSVAL_TO_PSEUDO_BOOLEAN(JSVAL_VOID))),
BRANCH_EXIT);
if (!isVoid) {
args[0] = l_ins, args[1] = cx_ins;
l_ins = lir->insCall(&js_BooleanOrUndefinedToNumber_ci, args);
l = (l == JSVAL_VOID)
? DOUBLE_TO_JSVAL(cx->runtime->jsNaN)
: INT_TO_JSVAL(l == JSVAL_TRUE);
return equalityHelper(l, r, l_ins, r_ins, negate,
tryBranchAfterCond, rval);
}
} else if (JSVAL_TAG(r) == JSVAL_BOOLEAN) {
bool isVoid = JSVAL_IS_VOID(r);
guard(isVoid,
lir->ins2(LIR_eq, r_ins, INS_CONST(JSVAL_TO_PSEUDO_BOOLEAN(JSVAL_VOID))),
BRANCH_EXIT);
if (!isVoid) {
args[0] = r_ins, args[1] = cx_ins;
r_ins = lir->insCall(&js_BooleanOrUndefinedToNumber_ci, args);
r = (r == JSVAL_VOID)
? DOUBLE_TO_JSVAL(cx->runtime->jsNaN)
: INT_TO_JSVAL(r == JSVAL_TRUE);
return equalityHelper(l, r, l_ins, r_ins, negate,
tryBranchAfterCond, rval);
}
} else {
if ((JSVAL_IS_STRING(l) || isNumber(l)) && !JSVAL_IS_PRIMITIVE(r)) {
ABORT_IF_XML(r);
return call_imacro(equality_imacros.any_obj);
}
if (!JSVAL_IS_PRIMITIVE(l) && (JSVAL_IS_STRING(r) || isNumber(r))) {
ABORT_IF_XML(l);
return call_imacro(equality_imacros.obj_any);
}
}
l_ins = lir->insImm(0);
r_ins = lir->insImm(1);
cond = false;
}
/* If the operands aren't numbers, compare them as integers. */
LOpcode op = fp ? LIR_feq : LIR_eq;
LIns* x = lir->ins2(op, l_ins, r_ins);
if (negate) {
x = lir->ins_eq0(x);
cond = !cond;
}
jsbytecode* pc = cx->fp->regs->pc;
/*
* Don't guard if the same path is always taken. If it isn't, we have to
* fuse comparisons and the following branch, because the interpreter does
* that.
*/
if (tryBranchAfterCond)
fuseIf(pc + 1, cond, x);
/*
* There is no need to write out the result of this comparison if the trace
* ends on this operation.
*/
if (pc[1] == JSOP_IFNE || pc[1] == JSOP_IFEQ)
CHECK_STATUS(checkTraceEnd(pc + 1));
/*
* We update the stack after the guard. This is safe since the guard bails
* out at the comparison and the interpreter will therefore re-execute the
* comparison. This way the value of the condition doesn't have to be
* calculated and saved on the stack in most cases.
*/
set(&rval, x);
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::relational(LOpcode op, bool tryBranchAfterCond)
{
jsval& r = stackval(-1);
jsval& l = stackval(-2);
LIns* x = NULL;
bool cond;
LIns* l_ins = get(&l);
LIns* r_ins = get(&r);
bool fp = false;
jsdouble lnum, rnum;
/*
* 11.8.5 if either argument is an object with a function-valued valueOf
* property; if both arguments are objects with non-function-valued valueOf
* properties, abort.
*/
if (!JSVAL_IS_PRIMITIVE(l)) {
ABORT_IF_XML(l);
if (!JSVAL_IS_PRIMITIVE(r)) {
ABORT_IF_XML(r);
return call_imacro(binary_imacros.obj_obj);
}
return call_imacro(binary_imacros.obj_any);
}
if (!JSVAL_IS_PRIMITIVE(r)) {
ABORT_IF_XML(r);
return call_imacro(binary_imacros.any_obj);
}
/* 11.8.5 steps 3, 16-21. */
if (JSVAL_IS_STRING(l) && JSVAL_IS_STRING(r)) {
LIns* args[] = { r_ins, l_ins };
l_ins = lir->insCall(&js_CompareStrings_ci, args);
r_ins = lir->insImm(0);
cond = evalCmp(op, JSVAL_TO_STRING(l), JSVAL_TO_STRING(r));
goto do_comparison;
}
/* 11.8.5 steps 4-5. */
if (!JSVAL_IS_NUMBER(l)) {
LIns* args[] = { l_ins, cx_ins };
switch (JSVAL_TAG(l)) {
case JSVAL_BOOLEAN:
l_ins = lir->insCall(&js_BooleanOrUndefinedToNumber_ci, args);
break;
case JSVAL_STRING:
l_ins = lir->insCall(&js_StringToNumber_ci, args);
break;
case JSVAL_OBJECT:
if (JSVAL_IS_NULL(l)) {
l_ins = lir->insImmq(0);
break;
}
// FALL THROUGH
case JSVAL_INT:
case JSVAL_DOUBLE:
default:
JS_NOT_REACHED("JSVAL_IS_NUMBER if int/double, objects should "
"have been handled at start of method");
ABORT_TRACE("safety belt");
}
}
if (!JSVAL_IS_NUMBER(r)) {
LIns* args[] = { r_ins, cx_ins };
switch (JSVAL_TAG(r)) {
case JSVAL_BOOLEAN:
r_ins = lir->insCall(&js_BooleanOrUndefinedToNumber_ci, args);
break;
case JSVAL_STRING:
r_ins = lir->insCall(&js_StringToNumber_ci, args);
break;
case JSVAL_OBJECT:
if (JSVAL_IS_NULL(r)) {
r_ins = lir->insImmq(0);
break;
}
// FALL THROUGH
case JSVAL_INT:
case JSVAL_DOUBLE:
default:
JS_NOT_REACHED("JSVAL_IS_NUMBER if int/double, objects should "
"have been handled at start of method");
ABORT_TRACE("safety belt");
}
}
{
jsval tmp = JSVAL_NULL;
JSAutoTempValueRooter tvr(cx, 1, &tmp);
tmp = l;
lnum = js_ValueToNumber(cx, &tmp);
tmp = r;
rnum = js_ValueToNumber(cx, &tmp);
}
cond = evalCmp(op, lnum, rnum);
fp = true;
/* 11.8.5 steps 6-15. */
do_comparison:
/* If the result is not a number or it's not a quad, we must use an integer compare. */
if (!fp) {
JS_ASSERT(op >= LIR_feq && op <= LIR_fge);
op = LOpcode(op + (LIR_eq - LIR_feq));
}
x = lir->ins2(op, l_ins, r_ins);
jsbytecode* pc = cx->fp->regs->pc;
/*
* Don't guard if the same path is always taken. If it isn't, we have to
* fuse comparisons and the following branch, because the interpreter does
* that.
*/
if (tryBranchAfterCond)
fuseIf(pc + 1, cond, x);
/*
* There is no need to write out the result of this comparison if the trace
* ends on this operation.
*/
if (pc[1] == JSOP_IFNE || pc[1] == JSOP_IFEQ)
CHECK_STATUS(checkTraceEnd(pc + 1));
/*
* We update the stack after the guard. This is safe since the guard bails
* out at the comparison and the interpreter will therefore re-execute the
* comparison. This way the value of the condition doesn't have to be
* calculated and saved on the stack in most cases.
*/
set(&l, x);
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::unary(LOpcode op)
{
jsval& v = stackval(-1);
bool intop = !(op & LIR64);
if (isNumber(v)) {
LIns* a = get(&v);
if (intop)
a = f2i(a);
a = lir->ins1(op, a);
if (intop)
a = lir->ins1(LIR_i2f, a);
set(&v, a);
return JSRS_CONTINUE;
}
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::binary(LOpcode op)
{
jsval& r = stackval(-1);
jsval& l = stackval(-2);
if (!JSVAL_IS_PRIMITIVE(l)) {
ABORT_IF_XML(l);
if (!JSVAL_IS_PRIMITIVE(r)) {
ABORT_IF_XML(r);
return call_imacro(binary_imacros.obj_obj);
}
return call_imacro(binary_imacros.obj_any);
}
if (!JSVAL_IS_PRIMITIVE(r)) {
ABORT_IF_XML(r);
return call_imacro(binary_imacros.any_obj);
}
bool intop = !(op & LIR64);
LIns* a = get(&l);
LIns* b = get(&r);
bool leftIsNumber = isNumber(l);
jsdouble lnum = leftIsNumber ? asNumber(l) : 0;
bool rightIsNumber = isNumber(r);
jsdouble rnum = rightIsNumber ? asNumber(r) : 0;
if ((op >= LIR_sub && op <= LIR_ush) || // sub, mul, (callh), or, xor, (not,) lsh, rsh, ush
(op >= LIR_fsub && op <= LIR_fdiv)) { // fsub, fmul, fdiv
LIns* args[2];
if (JSVAL_IS_STRING(l)) {
args[0] = a;
args[1] = cx_ins;
a = lir->insCall(&js_StringToNumber_ci, args);
lnum = js_StringToNumber(cx, JSVAL_TO_STRING(l));
leftIsNumber = true;
}
if (JSVAL_IS_STRING(r)) {
args[0] = b;
args[1] = cx_ins;
b = lir->insCall(&js_StringToNumber_ci, args);
rnum = js_StringToNumber(cx, JSVAL_TO_STRING(r));
rightIsNumber = true;
}
}
if (JSVAL_TAG(l) == JSVAL_BOOLEAN) {
LIns* args[] = { a, cx_ins };
a = lir->insCall(&js_BooleanOrUndefinedToNumber_ci, args);
lnum = js_BooleanOrUndefinedToNumber(cx, JSVAL_TO_PSEUDO_BOOLEAN(l));
leftIsNumber = true;
}
if (JSVAL_TAG(r) == JSVAL_BOOLEAN) {
LIns* args[] = { b, cx_ins };
b = lir->insCall(&js_BooleanOrUndefinedToNumber_ci, args);
rnum = js_BooleanOrUndefinedToNumber(cx, JSVAL_TO_PSEUDO_BOOLEAN(r));
rightIsNumber = true;
}
if (leftIsNumber && rightIsNumber) {
if (intop) {
LIns *args[] = { a };
a = lir->insCall(op == LIR_ush ? &js_DoubleToUint32_ci : &js_DoubleToInt32_ci, args);
b = f2i(b);
}
a = alu(op, lnum, rnum, a, b);
if (intop)
a = lir->ins1(op == LIR_ush ? LIR_u2f : LIR_i2f, a);
set(&l, a);
return JSRS_CONTINUE;
}
return JSRS_STOP;
}
JS_STATIC_ASSERT(offsetof(JSObjectOps, objectMap) == 0);
bool
TraceRecorder::map_is_native(JSObjectMap* map, LIns* map_ins, LIns*& ops_ins, size_t op_offset)
{
JS_ASSERT(op_offset < sizeof(JSObjectOps));
JS_ASSERT(op_offset % sizeof(void *) == 0);
#define OP(ops) (*(void **) ((uint8 *) (ops) + op_offset))
void* ptr = OP(map->ops);
if (ptr != OP(&js_ObjectOps))
return false;
#undef OP
ops_ins = addName(lir->insLoad(LIR_ldp, map_ins, int(offsetof(JSObjectMap, ops))), "ops");
LIns* n = lir->insLoad(LIR_ldp, ops_ins, op_offset);
guard(true,
addName(lir->ins2(LIR_eq, n, INS_CONSTPTR(ptr)), "guard(native-map)"),
BRANCH_EXIT);
return true;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::test_property_cache(JSObject* obj, LIns* obj_ins, JSObject*& obj2, jsuword& pcval)
{
jsbytecode* pc = cx->fp->regs->pc;
JS_ASSERT(*pc != JSOP_INITPROP && *pc != JSOP_SETNAME && *pc != JSOP_SETPROP);
// Mimic the interpreter's special case for dense arrays by skipping up one
// hop along the proto chain when accessing a named (not indexed) property,
// typically to find Array.prototype methods.
JSObject* aobj = obj;
if (OBJ_IS_DENSE_ARRAY(cx, obj)) {
guardDenseArray(obj, obj_ins, BRANCH_EXIT);
aobj = OBJ_GET_PROTO(cx, obj);
obj_ins = stobj_get_fslot(obj_ins, JSSLOT_PROTO);
}
LIns* map_ins = lir->insLoad(LIR_ldp, obj_ins, (int)offsetof(JSObject, map));
LIns* ops_ins;
// Interpreter calls to PROPERTY_CACHE_TEST guard on native object ops
// which is required to use native objects (those whose maps are scopes),
// or even more narrow conditions required because the cache miss case
// will call a particular object-op (js_GetProperty, js_SetProperty).
//
// We parameterize using offsetof and guard on match against the hook at
// the given offset in js_ObjectOps. TraceRecorder::record_JSOP_SETPROP
// guards the js_SetProperty case.
uint32 format = js_CodeSpec[*pc].format;
uint32 mode = JOF_MODE(format);
// No need to guard native-ness of global object.
JS_ASSERT(OBJ_IS_NATIVE(globalObj));
if (aobj != globalObj) {
size_t op_offset = offsetof(JSObjectOps, objectMap);
if (mode == JOF_PROP || mode == JOF_VARPROP) {
JS_ASSERT(!(format & JOF_SET));
op_offset = offsetof(JSObjectOps, getProperty);
} else {
JS_ASSERT(mode == JOF_NAME);
}
if (!map_is_native(aobj->map, map_ins, ops_ins, op_offset))
ABORT_TRACE("non-native map");
}
JSAtom* atom;
JSPropCacheEntry* entry;
PROPERTY_CACHE_TEST(cx, pc, aobj, obj2, entry, atom);
if (!atom) {
// Null atom means that obj2 is locked and must now be unlocked.
JS_UNLOCK_OBJ(cx, obj2);
} else {
// Miss: pre-fill the cache for the interpreter, as well as for our needs.
jsid id = ATOM_TO_JSID(atom);
JSProperty* prop;
if (JOF_OPMODE(*pc) == JOF_NAME) {
JS_ASSERT(aobj == obj);
entry = js_FindPropertyHelper(cx, id, true, &obj, &obj2, &prop);
if (!entry)
ABORT_TRACE_ERROR("error in js_FindPropertyHelper");
if (entry == JS_NO_PROP_CACHE_FILL)
ABORT_TRACE("cannot cache name");
} else {
int protoIndex = js_LookupPropertyWithFlags(cx, aobj, id,
cx->resolveFlags,
&obj2, &prop);
if (protoIndex < 0)
ABORT_TRACE_ERROR("error in js_LookupPropertyWithFlags");
if (prop) {
if (!OBJ_IS_NATIVE(obj2)) {
OBJ_DROP_PROPERTY(cx, obj2, prop);
ABORT_TRACE("property found on non-native object");
}
entry = js_FillPropertyCache(cx, aobj, 0, protoIndex, obj2,
(JSScopeProperty*) prop, false);
JS_ASSERT(entry);
if (entry == JS_NO_PROP_CACHE_FILL)
entry = NULL;
}
}
if (!prop) {
// Propagate obj from js_FindPropertyHelper to record_JSOP_BINDNAME
// via our obj2 out-parameter. If we are recording JSOP_SETNAME and
// the global it's assigning does not yet exist, create it.
obj2 = obj;
// Use PCVAL_NULL to return "no such property" to our caller.
pcval = PCVAL_NULL;
return JSRS_CONTINUE;
}
OBJ_DROP_PROPERTY(cx, obj2, prop);
if (!entry)
ABORT_TRACE("failed to fill property cache");
}
if (wasDeepAborted())
ABORT_TRACE("deep abort from property lookup");
#ifdef JS_THREADSAFE
// There's a potential race in any JS_THREADSAFE embedding that's nuts
// enough to share mutable objects on the scope or proto chain, but we
// don't care about such insane embeddings. Anyway, the (scope, proto)
// entry->vcap coordinates must reach obj2 from aobj at this point.
JS_ASSERT(cx->requestDepth);
#endif
// Emit guard(s), common code for both hit and miss cases.
// Check for first-level cache hit and guard on kshape if possible.
// Otherwise guard on key object exact match.
if (PCVCAP_TAG(entry->vcap) <= 1) {
if (aobj != globalObj) {
LIns* shape_ins = addName(lir->insLoad(LIR_ld, map_ins, offsetof(JSScope, shape)),
"shape");
guard(true, addName(lir->ins2i(LIR_eq, shape_ins, entry->kshape), "guard(kshape)"),
BRANCH_EXIT);
}
} else {
#ifdef DEBUG
JSOp op = js_GetOpcode(cx, cx->fp->script, pc);
JSAtom *pcatom;
if (op == JSOP_LENGTH) {
pcatom = cx->runtime->atomState.lengthAtom;
} else {
ptrdiff_t pcoff = (JOF_TYPE(js_CodeSpec[op].format) == JOF_SLOTATOM) ? SLOTNO_LEN : 0;
GET_ATOM_FROM_BYTECODE(cx->fp->script, pc, pcoff, pcatom);
}
JS_ASSERT(entry->kpc == (jsbytecode *) pcatom);
JS_ASSERT(entry->kshape == jsuword(aobj));
#endif
if (aobj != globalObj && !obj_ins->isconstp()) {
guard(true, addName(lir->ins2i(LIR_eq, obj_ins, entry->kshape), "guard(kobj)"),
BRANCH_EXIT);
}
}
// For any hit that goes up the scope and/or proto chains, we will need to
// guard on the shape of the object containing the property.
if (PCVCAP_TAG(entry->vcap) >= 1) {
jsuword vcap = entry->vcap;
uint32 vshape = PCVCAP_SHAPE(vcap);
JS_ASSERT(OBJ_SHAPE(obj2) == vshape);
LIns* obj2_ins;
if (PCVCAP_TAG(entry->vcap) == 1) {
// Duplicate the special case in PROPERTY_CACHE_TEST.
obj2_ins = stobj_get_fslot(obj_ins, JSSLOT_PROTO);
guard(false, lir->ins_eq0(obj2_ins), BRANCH_EXIT);
} else {
obj2_ins = INS_CONSTPTR(obj2);
}
map_ins = lir->insLoad(LIR_ldp, obj2_ins, (int)offsetof(JSObject, map));
if (!map_is_native(obj2->map, map_ins, ops_ins))
ABORT_TRACE("non-native map");
LIns* shape_ins = addName(lir->insLoad(LIR_ld, map_ins, offsetof(JSScope, shape)),
"shape");
guard(true,
addName(lir->ins2i(LIR_eq, shape_ins, vshape), "guard(vshape)"),
BRANCH_EXIT);
}
pcval = entry->vword;
return JSRS_CONTINUE;
}
void
TraceRecorder::stobj_set_fslot(LIns *obj_ins, unsigned slot, LIns* v_ins, const char *name)
{
addName(lir->insStorei(v_ins, obj_ins, offsetof(JSObject, fslots) + slot * sizeof(jsval)),
name);
}
void
TraceRecorder::stobj_set_dslot(LIns *obj_ins, unsigned slot, LIns*& dslots_ins, LIns* v_ins,
const char *name)
{
if (!dslots_ins)
dslots_ins = lir->insLoad(LIR_ldp, obj_ins, offsetof(JSObject, dslots));
addName(lir->insStorei(v_ins, dslots_ins, slot * sizeof(jsval)), name);
}
void
TraceRecorder::stobj_set_slot(LIns* obj_ins, unsigned slot, LIns*& dslots_ins, LIns* v_ins)
{
if (slot < JS_INITIAL_NSLOTS) {
stobj_set_fslot(obj_ins, slot, v_ins, "set_slot(fslots)");
} else {
stobj_set_dslot(obj_ins, slot - JS_INITIAL_NSLOTS, dslots_ins, v_ins,
"set_slot(dslots)");
}
}
LIns*
TraceRecorder::stobj_get_fslot(LIns* obj_ins, unsigned slot)
{
JS_ASSERT(slot < JS_INITIAL_NSLOTS);
return lir->insLoad(LIR_ldp, obj_ins, offsetof(JSObject, fslots) + slot * sizeof(jsval));
}
LIns*
TraceRecorder::stobj_get_dslot(LIns* obj_ins, unsigned index, LIns*& dslots_ins)
{
if (!dslots_ins)
dslots_ins = lir->insLoad(LIR_ldp, obj_ins, offsetof(JSObject, dslots));
return lir->insLoad(LIR_ldp, dslots_ins, index * sizeof(jsval));
}
LIns*
TraceRecorder::stobj_get_slot(LIns* obj_ins, unsigned slot, LIns*& dslots_ins)
{
if (slot < JS_INITIAL_NSLOTS)
return stobj_get_fslot(obj_ins, slot);
return stobj_get_dslot(obj_ins, slot - JS_INITIAL_NSLOTS, dslots_ins);
}
JSRecordingStatus
TraceRecorder::native_set(LIns* obj_ins, JSScopeProperty* sprop, LIns*& dslots_ins, LIns* v_ins)
{
if (SPROP_HAS_STUB_SETTER(sprop) && sprop->slot != SPROP_INVALID_SLOT) {
stobj_set_slot(obj_ins, sprop->slot, dslots_ins, v_ins);
return JSRS_CONTINUE;
}
ABORT_TRACE("unallocated or non-stub sprop");
}
JSRecordingStatus
TraceRecorder::native_get(LIns* obj_ins, LIns* pobj_ins, JSScopeProperty* sprop,
LIns*& dslots_ins, LIns*& v_ins)
{
if (!SPROP_HAS_STUB_GETTER(sprop))
return JSRS_STOP;
if (sprop->slot != SPROP_INVALID_SLOT)
v_ins = stobj_get_slot(pobj_ins, sprop->slot, dslots_ins);
else
v_ins = INS_CONST(JSVAL_TO_PSEUDO_BOOLEAN(JSVAL_VOID));
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK void
TraceRecorder::box_jsval(jsval v, LIns*& v_ins)
{
if (isNumber(v)) {
LIns* args[] = { v_ins, cx_ins };
v_ins = lir->insCall(&js_BoxDouble_ci, args);
guard(false, lir->ins2(LIR_eq, v_ins, INS_CONST(JSVAL_ERROR_COOKIE)),
OOM_EXIT);
return;
}
switch (JSVAL_TAG(v)) {
case JSVAL_BOOLEAN:
v_ins = lir->ins2i(LIR_pior, lir->ins2i(LIR_pilsh, v_ins, JSVAL_TAGBITS), JSVAL_BOOLEAN);
return;
case JSVAL_OBJECT:
return;
default:
JS_ASSERT(JSVAL_TAG(v) == JSVAL_STRING);
v_ins = lir->ins2(LIR_pior, v_ins, INS_CONST(JSVAL_STRING));
return;
}
}
JS_REQUIRES_STACK void
TraceRecorder::unbox_jsval(jsval v, LIns*& v_ins, VMSideExit* exit)
{
if (isNumber(v)) {
// JSVAL_IS_NUMBER(v)
guard(false,
lir->ins_eq0(lir->ins2(LIR_pior,
lir->ins2(LIR_piand, v_ins, INS_CONST(JSVAL_INT)),
lir->ins2i(LIR_eq,
lir->ins2(LIR_piand, v_ins,
INS_CONST(JSVAL_TAGMASK)),
JSVAL_DOUBLE))),
exit);
LIns* args[] = { v_ins };
v_ins = lir->insCall(&js_UnboxDouble_ci, args);
return;
}
switch (JSVAL_TAG(v)) {
case JSVAL_BOOLEAN:
guard(true,
lir->ins2i(LIR_eq,
lir->ins2(LIR_piand, v_ins, INS_CONST(JSVAL_TAGMASK)),
JSVAL_BOOLEAN),
exit);
v_ins = lir->ins2i(LIR_ush, v_ins, JSVAL_TAGBITS);
return;
case JSVAL_OBJECT:
if (JSVAL_IS_NULL(v)) {
// JSVAL_NULL maps to type JSVAL_TNULL, so insist that v_ins == 0 here.
guard(true, lir->ins_eq0(v_ins), exit);
} else {
guard(false, lir->ins_eq0(v_ins), exit);
guard(true,
lir->ins2i(LIR_eq,
lir->ins2(LIR_piand, v_ins, INS_CONSTWORD(JSVAL_TAGMASK)),
JSVAL_OBJECT),
exit);
guard(HAS_FUNCTION_CLASS(JSVAL_TO_OBJECT(v)),
lir->ins2(LIR_eq,
lir->ins2(LIR_piand,
lir->insLoad(LIR_ldp, v_ins, offsetof(JSObject, classword)),
INS_CONSTWORD(~JSSLOT_CLASS_MASK_BITS)),
INS_CONSTPTR(&js_FunctionClass)),
exit);
}
return;
default:
JS_ASSERT(JSVAL_TAG(v) == JSVAL_STRING);
guard(true,
lir->ins2i(LIR_eq,
lir->ins2(LIR_piand, v_ins, INS_CONST(JSVAL_TAGMASK)),
JSVAL_STRING),
exit);
v_ins = lir->ins2(LIR_piand, v_ins, INS_CONST(~JSVAL_TAGMASK));
return;
}
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::getThis(LIns*& this_ins)
{
/*
* js_ComputeThisForFrame updates cx->fp->argv[-1], so sample it into 'original' first.
*/
jsval original = JSVAL_NULL;
if (cx->fp->callee) {
original = cx->fp->argv[-1];
if (!JSVAL_IS_PRIMITIVE(original) &&
guardClass(JSVAL_TO_OBJECT(original), get(&cx->fp->argv[-1]), &js_WithClass, snapshot(MISMATCH_EXIT))) {
ABORT_TRACE("can't trace getThis on With object");
}
}
JSObject* thisObj = js_ComputeThisForFrame(cx, cx->fp);
if (!thisObj)
ABORT_TRACE_ERROR("js_ComputeThisForName failed");
/*
* In global code, bake in the global object as 'this' object.
*/
if (!cx->fp->callee) {
JS_ASSERT(callDepth == 0);
this_ins = INS_CONSTPTR(thisObj);
/*
* We don't have argv[-1] in global code, so we don't update the tracker here.
*/
return JSRS_CONTINUE;
}
jsval& thisv = cx->fp->argv[-1];
JS_ASSERT(JSVAL_IS_OBJECT(thisv));
/*
* Traces type-specialize between null and objects, so if we currently see a null
* value in argv[-1], this trace will only match if we see null at runtime as well.
* Bake in the global object as 'this' object, updating the tracker as well. We
* can only detect this condition prior to calling js_ComputeThisForFrame, since it
* updates the interpreter's copy of argv[-1].
*/
JSClass* clasp = NULL;;
if (JSVAL_IS_NULL(original) ||
(((clasp = STOBJ_GET_CLASS(JSVAL_TO_OBJECT(original))) == &js_CallClass) ||
(clasp == &js_BlockClass))) {
if (clasp)
guardClass(JSVAL_TO_OBJECT(original), get(&thisv), clasp, snapshot(BRANCH_EXIT));
JS_ASSERT(!JSVAL_IS_PRIMITIVE(thisv));
if (thisObj != globalObj)
ABORT_TRACE("global object was wrapped while recording");
this_ins = INS_CONSTPTR(thisObj);
set(&thisv, this_ins);
return JSRS_CONTINUE;
}
this_ins = get(&thisv);
/*
* The only unwrapped object that needs to be wrapped that we can get here is the
* global object obtained throught the scope chain.
*/
JSObject* obj = js_GetWrappedObject(cx, JSVAL_TO_OBJECT(thisv));
JSObject* inner = obj;
OBJ_TO_INNER_OBJECT(cx, inner);
if (!obj)
return JSRS_ERROR;
JS_ASSERT(original == thisv ||
original == OBJECT_TO_JSVAL(inner) ||
original == OBJECT_TO_JSVAL(obj));
// If the returned this object is the unwrapped inner or outer object,
// then we need to use the wrapped outer object.
LIns* is_inner = lir->ins2(LIR_eq, this_ins, INS_CONSTPTR(inner));
LIns* is_outer = lir->ins2(LIR_eq, this_ins, INS_CONSTPTR(obj));
LIns* wrapper = INS_CONSTPTR(JSVAL_TO_OBJECT(thisv));
this_ins = lir->ins_choose(is_inner,
wrapper,
lir->ins_choose(is_outer,
wrapper,
this_ins));
return JSRS_CONTINUE;
}
LIns*
TraceRecorder::getStringLength(LIns* str_ins)
{
LIns* len_ins = lir->insLoad(LIR_ldp, str_ins, (int)offsetof(JSString, length));
LIns* masked_len_ins = lir->ins2(LIR_piand,
len_ins,
INS_CONSTWORD(JSSTRING_LENGTH_MASK));
return
lir->ins_choose(lir->ins_eq0(lir->ins2(LIR_piand,
len_ins,
INS_CONSTWORD(JSSTRFLAG_DEPENDENT))),
masked_len_ins,
lir->ins_choose(lir->ins_eq0(lir->ins2(LIR_piand,
len_ins,
INS_CONSTWORD(JSSTRFLAG_PREFIX))),
lir->ins2(LIR_piand,
len_ins,
INS_CONSTWORD(JSSTRDEP_LENGTH_MASK)),
masked_len_ins));
}
JS_REQUIRES_STACK bool
TraceRecorder::guardClass(JSObject* obj, LIns* obj_ins, JSClass* clasp, VMSideExit* exit)
{
bool cond = STOBJ_GET_CLASS(obj) == clasp;
LIns* class_ins = lir->insLoad(LIR_ldp, obj_ins, offsetof(JSObject, classword));
class_ins = lir->ins2(LIR_piand, class_ins, lir->insImm(~JSSLOT_CLASS_MASK_BITS));
char namebuf[32];
JS_snprintf(namebuf, sizeof namebuf, "guard(class is %s)", clasp->name);
guard(cond, addName(lir->ins2(LIR_eq, class_ins, INS_CONSTPTR(clasp)), namebuf), exit);
return cond;
}
JS_REQUIRES_STACK bool
TraceRecorder::guardDenseArray(JSObject* obj, LIns* obj_ins, ExitType exitType)
{
return guardClass(obj, obj_ins, &js_ArrayClass, snapshot(exitType));
}
JS_REQUIRES_STACK bool
TraceRecorder::guardHasPrototype(JSObject* obj, LIns* obj_ins,
JSObject** pobj, LIns** pobj_ins,
VMSideExit* exit)
{
*pobj = JSVAL_TO_OBJECT(obj->fslots[JSSLOT_PROTO]);
*pobj_ins = stobj_get_fslot(obj_ins, JSSLOT_PROTO);
bool cond = *pobj == NULL;
guard(cond, addName(lir->ins_eq0(*pobj_ins), "guard(proto-not-null)"), exit);
return !cond;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::guardPrototypeHasNoIndexedProperties(JSObject* obj, LIns* obj_ins, ExitType exitType)
{
/*
* Guard that no object along the prototype chain has any indexed properties which
* might become visible through holes in the array.
*/
VMSideExit* exit = snapshot(exitType);
if (js_PrototypeHasIndexedProperties(cx, obj))
return JSRS_STOP;
while (guardHasPrototype(obj, obj_ins, &obj, &obj_ins, exit)) {
LIns* map_ins = lir->insLoad(LIR_ldp, obj_ins, (int)offsetof(JSObject, map));
LIns* ops_ins;
if (!map_is_native(obj->map, map_ins, ops_ins))
ABORT_TRACE("non-native object involved along prototype chain");
LIns* shape_ins = addName(lir->insLoad(LIR_ld, map_ins, offsetof(JSScope, shape)),
"shape");
guard(true,
addName(lir->ins2i(LIR_eq, shape_ins, OBJ_SHAPE(obj)), "guard(shape)"),
exit);
}
return JSRS_CONTINUE;
}
JSRecordingStatus
TraceRecorder::guardNotGlobalObject(JSObject* obj, LIns* obj_ins)
{
if (obj == globalObj)
ABORT_TRACE("reference aliases global object");
guard(false, lir->ins2(LIR_eq, obj_ins, INS_CONSTPTR(globalObj)), MISMATCH_EXIT);
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK void
TraceRecorder::clearFrameSlotsFromCache()
{
/* Clear out all slots of this frame in the nativeFrameTracker. Different locations on the
VM stack might map to different locations on the native stack depending on the
number of arguments (i.e.) of the next call, so we have to make sure we map
those in to the cache with the right offsets. */
JSStackFrame* fp = cx->fp;
jsval* vp;
jsval* vpstop;
if (fp->callee) {
vp = &fp->argv[-2];
vpstop = &fp->argv[argSlots(fp)];
while (vp < vpstop)
nativeFrameTracker.set(vp++, (LIns*)0);
}
vp = &fp->slots[0];
vpstop = &fp->slots[fp->script->nslots];
while (vp < vpstop)
nativeFrameTracker.set(vp++, (LIns*)0);
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_EnterFrame()
{
JSStackFrame* fp = cx->fp;
if (++callDepth >= MAX_CALLDEPTH)
ABORT_TRACE("exceeded maximum call depth");
// FIXME: Allow and attempt to inline a single level of recursion until we compile
// recursive calls as independent trees (459301).
if (fp->script == fp->down->script && fp->down->down && fp->down->down->script == fp->script)
ABORT_TRACE("recursive call");
debug_only_v(printf("EnterFrame %s, callDepth=%d\n",
js_AtomToPrintableString(cx, cx->fp->fun->atom),
callDepth);)
debug_only_v(
js_Disassemble(cx, cx->fp->script, JS_TRUE, stdout);
printf("----\n");)
LIns* void_ins = INS_CONST(JSVAL_TO_PSEUDO_BOOLEAN(JSVAL_VOID));
jsval* vp = &fp->argv[fp->argc];
jsval* vpstop = vp + ptrdiff_t(fp->fun->nargs) - ptrdiff_t(fp->argc);
while (vp < vpstop) {
if (vp >= fp->down->regs->sp)
nativeFrameTracker.set(vp, (LIns*)0);
set(vp++, void_ins, true);
}
vp = &fp->slots[0];
vpstop = vp + fp->script->nfixed;
while (vp < vpstop)
set(vp++, void_ins, true);
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_LeaveFrame()
{
debug_only_v(
if (cx->fp->fun)
printf("LeaveFrame (back to %s), callDepth=%d\n",
js_AtomToPrintableString(cx, cx->fp->fun->atom),
callDepth);
);
if (callDepth-- <= 0)
ABORT_TRACE("returned out of a loop we started tracing");
// LeaveFrame gets called after the interpreter popped the frame and
// stored rval, so cx->fp not cx->fp->down, and -1 not 0.
atoms = FrameAtomBase(cx, cx->fp);
set(&stackval(-1), rval_ins, true);
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_PUSH()
{
stack(0, INS_CONST(JSVAL_TO_PSEUDO_BOOLEAN(JSVAL_VOID)));
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_POPV()
{
jsval& rval = stackval(-1);
LIns *rval_ins = get(&rval);
box_jsval(rval, rval_ins);
// Store it in cx->fp->rval. NB: Tricky dependencies. cx->fp is the right
// frame because POPV appears only in global and eval code and we don't
// trace JSOP_EVAL or leaving the frame where tracing started.
LIns *fp_ins = lir->insLoad(LIR_ldp, cx_ins, offsetof(JSContext, fp));
lir->insStorei(rval_ins, fp_ins, offsetof(JSStackFrame, rval));
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_ENTERWITH()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_LEAVEWITH()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_RETURN()
{
/* A return from callDepth 0 terminates the current loop. */
if (callDepth == 0) {
AUDIT(returnLoopExits);
endLoop(traceMonitor);
return JSRS_STOP;
}
/* If we inlined this function call, make the return value available to the caller code. */
jsval& rval = stackval(-1);
JSStackFrame *fp = cx->fp;
if ((cx->fp->flags & JSFRAME_CONSTRUCTING) && JSVAL_IS_PRIMITIVE(rval)) {
JS_ASSERT(OBJECT_TO_JSVAL(fp->thisp) == fp->argv[-1]);
rval_ins = get(&fp->argv[-1]);
} else {
rval_ins = get(&rval);
}
debug_only_v(printf("returning from %s\n", js_AtomToPrintableString(cx, cx->fp->fun->atom));)
clearFrameSlotsFromCache();
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_GOTO()
{
/*
* If we hit a break, end the loop and generate an always taken loop exit guard.
* For other downward gotos (like if/else) continue recording.
*/
jssrcnote* sn = js_GetSrcNote(cx->fp->script, cx->fp->regs->pc);
if (sn && SN_TYPE(sn) == SRC_BREAK) {
AUDIT(breakLoopExits);
endLoop(traceMonitor);
return JSRS_STOP;
}
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_IFEQ()
{
trackCfgMerges(cx->fp->regs->pc);
return ifop();
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_IFNE()
{
return ifop();
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_ARGUMENTS()
{
#if 1
ABORT_TRACE("can't trace arguments yet");
#else
LIns* args[] = { cx_ins };
LIns* a_ins = lir->insCall(&js_Arguments_ci, args);
guard(false, lir->ins_eq0(a_ins), OOM_EXIT);
stack(0, a_ins);
return JSRS_CONTINUE;
#endif
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_DUP()
{
stack(0, get(&stackval(-1)));
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_DUP2()
{
stack(0, get(&stackval(-2)));
stack(1, get(&stackval(-1)));
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_SWAP()
{
jsval& l = stackval(-2);
jsval& r = stackval(-1);
LIns* l_ins = get(&l);
LIns* r_ins = get(&r);
set(&r, l_ins);
set(&l, r_ins);
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_PICK()
{
jsval* sp = cx->fp->regs->sp;
jsint n = cx->fp->regs->pc[1];
JS_ASSERT(sp - (n+1) >= StackBase(cx->fp));
LIns* top = get(sp - (n+1));
for (jsint i = 0; i < n; ++i)
set(sp - (n+1) + i, get(sp - n + i));
set(&sp[-1], top);
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_SETCONST()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_BITOR()
{
return binary(LIR_or);
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_BITXOR()
{
return binary(LIR_xor);
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_BITAND()
{
return binary(LIR_and);
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_EQ()
{
return equality(false, true);
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_NE()
{
return equality(true, true);
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_LT()
{
return relational(LIR_flt, true);
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_LE()
{
return relational(LIR_fle, true);
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_GT()
{
return relational(LIR_fgt, true);
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_GE()
{
return relational(LIR_fge, true);
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_LSH()
{
return binary(LIR_lsh);
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_RSH()
{
return binary(LIR_rsh);
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_URSH()
{
return binary(LIR_ush);
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_ADD()
{
jsval& r = stackval(-1);
jsval& l = stackval(-2);
if (!JSVAL_IS_PRIMITIVE(l)) {
ABORT_IF_XML(l);
if (!JSVAL_IS_PRIMITIVE(r)) {
ABORT_IF_XML(r);
return call_imacro(add_imacros.obj_obj);
}
return call_imacro(add_imacros.obj_any);
}
if (!JSVAL_IS_PRIMITIVE(r)) {
ABORT_IF_XML(r);
return call_imacro(add_imacros.any_obj);
}
if (JSVAL_IS_STRING(l) || JSVAL_IS_STRING(r)) {
LIns* args[] = { stringify(r), stringify(l), cx_ins };
LIns* concat = lir->insCall(&js_ConcatStrings_ci, args);
guard(false, lir->ins_eq0(concat), OOM_EXIT);
set(&l, concat);
return JSRS_CONTINUE;
}
return binary(LIR_fadd);
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_SUB()
{
return binary(LIR_fsub);
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_MUL()
{
return binary(LIR_fmul);
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_DIV()
{
return binary(LIR_fdiv);
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_MOD()
{
jsval& r = stackval(-1);
jsval& l = stackval(-2);
if (!JSVAL_IS_PRIMITIVE(l)) {
ABORT_IF_XML(l);
if (!JSVAL_IS_PRIMITIVE(r)) {
ABORT_IF_XML(r);
return call_imacro(binary_imacros.obj_obj);
}
return call_imacro(binary_imacros.obj_any);
}
if (!JSVAL_IS_PRIMITIVE(r)) {
ABORT_IF_XML(r);
return call_imacro(binary_imacros.any_obj);
}
if (isNumber(l) && isNumber(r)) {
LIns* l_ins = get(&l);
LIns* r_ins = get(&r);
LIns* x;
/* We can't demote this in a filter since we need the actual values of l and r. */
if (isPromote(l_ins) && isPromote(r_ins) && asNumber(l) >= 0 && asNumber(r) > 0) {
LIns* args[] = { ::demote(lir, r_ins), ::demote(lir, l_ins) };
x = lir->insCall(&js_imod_ci, args);
guard(false, lir->ins2(LIR_eq, x, lir->insImm(-1)), BRANCH_EXIT);
x = lir->ins1(LIR_i2f, x);
} else {
LIns* args[] = { r_ins, l_ins };
x = lir->insCall(&js_dmod_ci, args);
}
set(&l, x);
return JSRS_CONTINUE;
}
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_NOT()
{
jsval& v = stackval(-1);
if (JSVAL_TAG(v) == JSVAL_BOOLEAN) {
set(&v, lir->ins_eq0(lir->ins2i(LIR_eq, get(&v), 1)));
return JSRS_CONTINUE;
}
if (isNumber(v)) {
LIns* v_ins = get(&v);
set(&v, lir->ins2(LIR_or, lir->ins2(LIR_feq, v_ins, lir->insImmq(0)),
lir->ins_eq0(lir->ins2(LIR_feq, v_ins, v_ins))));
return JSRS_CONTINUE;
}
if (JSVAL_TAG(v) == JSVAL_OBJECT) {
set(&v, lir->ins_eq0(get(&v)));
return JSRS_CONTINUE;
}
JS_ASSERT(JSVAL_IS_STRING(v));
set(&v, lir->ins_eq0(lir->ins2(LIR_piand,
lir->insLoad(LIR_ldp, get(&v), (int)offsetof(JSString, length)),
INS_CONSTPTR(reinterpret_cast<void *>(JSSTRING_LENGTH_MASK)))));
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_BITNOT()
{
return unary(LIR_not);
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_NEG()
{
jsval& v = stackval(-1);
if (!JSVAL_IS_PRIMITIVE(v)) {
ABORT_IF_XML(v);
return call_imacro(unary_imacros.sign);
}
if (isNumber(v)) {
LIns* a = get(&v);
/* If we're a promoted integer, we have to watch out for 0s since -0 is a double.
Only follow this path if we're not an integer that's 0 and we're not a double
that's zero.
*/
if (isPromoteInt(a) &&
(!JSVAL_IS_INT(v) || JSVAL_TO_INT(v) != 0) &&
(!JSVAL_IS_DOUBLE(v) || !JSDOUBLE_IS_NEGZERO(*JSVAL_TO_DOUBLE(v))) &&
-asNumber(v) == (int)-asNumber(v)) {
a = lir->ins1(LIR_neg, ::demote(lir, a));
if (!a->isconst()) {
VMSideExit* exit = snapshot(OVERFLOW_EXIT);
lir->insGuard(LIR_xt, lir->ins1(LIR_ov, a),
createGuardRecord(exit));
lir->insGuard(LIR_xt, lir->ins2(LIR_eq, a, lir->insImm(0)),
createGuardRecord(exit));
}
a = lir->ins1(LIR_i2f, a);
} else {
a = lir->ins1(LIR_fneg, a);
}
set(&v, a);
return JSRS_CONTINUE;
}
if (JSVAL_IS_NULL(v)) {
set(&v, lir->insImmf(-0.0));
return JSRS_CONTINUE;
}
JS_ASSERT(JSVAL_TAG(v) == JSVAL_STRING || JSVAL_TAG(v) == JSVAL_BOOLEAN);
LIns* args[] = { get(&v), cx_ins };
set(&v, lir->ins1(LIR_fneg,
lir->insCall(JSVAL_IS_STRING(v)
? &js_StringToNumber_ci
: &js_BooleanOrUndefinedToNumber_ci,
args)));
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_POS()
{
jsval& v = stackval(-1);
if (!JSVAL_IS_PRIMITIVE(v)) {
ABORT_IF_XML(v);
return call_imacro(unary_imacros.sign);
}
if (isNumber(v))
return JSRS_CONTINUE;
if (JSVAL_IS_NULL(v)) {
set(&v, lir->insImmq(0));
return JSRS_CONTINUE;
}
JS_ASSERT(JSVAL_TAG(v) == JSVAL_STRING || JSVAL_TAG(v) == JSVAL_BOOLEAN);
LIns* args[] = { get(&v), cx_ins };
set(&v, lir->insCall(JSVAL_IS_STRING(v)
? &js_StringToNumber_ci
: &js_BooleanOrUndefinedToNumber_ci,
args));
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_PRIMTOP()
{
// Either this opcode does nothing or we couldn't have traced here, because
// we'd have thrown an exception -- so do nothing if we actually hit this.
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_OBJTOP()
{
jsval& v = stackval(-1);
ABORT_IF_XML(v);
return JSRS_CONTINUE;
}
JSBool
js_Array(JSContext* cx, JSObject* obj, uintN argc, jsval* argv, jsval* rval);
JSBool
js_Object(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);
JSBool
js_Date(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);
JSBool
js_fun_apply(JSContext* cx, uintN argc, jsval* vp);
JSRecordingStatus
TraceRecorder::getClassPrototype(JSObject* ctor, LIns*& proto_ins)
{
jsval pval;
if (!OBJ_GET_PROPERTY(cx, ctor,
ATOM_TO_JSID(cx->runtime->atomState.classPrototypeAtom),
&pval)) {
ABORT_TRACE_ERROR("error getting prototype from constructor");
}
if (JSVAL_TAG(pval) != JSVAL_OBJECT)
ABORT_TRACE("got primitive prototype from constructor");
#ifdef DEBUG
JSBool ok, found;
uintN attrs;
ok = JS_GetPropertyAttributes(cx, ctor, js_class_prototype_str, &attrs, &found);
JS_ASSERT(ok);
JS_ASSERT(found);
JS_ASSERT((~attrs & (JSPROP_READONLY | JSPROP_PERMANENT)) == 0);
#endif
proto_ins = INS_CONSTPTR(JSVAL_TO_OBJECT(pval));
return JSRS_CONTINUE;
}
JSRecordingStatus
TraceRecorder::getClassPrototype(JSProtoKey key, LIns*& proto_ins)
{
JSObject* proto;
if (!js_GetClassPrototype(cx, globalObj, INT_TO_JSID(key), &proto))
ABORT_TRACE_ERROR("error in js_GetClassPrototype");
proto_ins = INS_CONSTPTR(proto);
return JSRS_CONTINUE;
}
#define IGNORE_NATIVE_CALL_COMPLETE_CALLBACK ((JSTraceableNative*)1)
JSRecordingStatus
TraceRecorder::newString(JSObject* ctor, uint32 argc, jsval* argv, jsval* rval)
{
JS_ASSERT(argc == 1);
if (!JSVAL_IS_PRIMITIVE(argv[0])) {
ABORT_IF_XML(argv[0]);
return call_imacro(new_imacros.String);
}
LIns* proto_ins;
CHECK_STATUS(getClassPrototype(ctor, proto_ins));
LIns* args[] = { stringify(argv[0]), proto_ins, cx_ins };
LIns* obj_ins = lir->insCall(&js_String_tn_ci, args);
guard(false, lir->ins_eq0(obj_ins), OOM_EXIT);
set(rval, obj_ins);
pendingTraceableNative = IGNORE_NATIVE_CALL_COMPLETE_CALLBACK;
return JSRS_CONTINUE;
}
JSRecordingStatus
TraceRecorder::newArray(JSObject* ctor, uint32 argc, jsval* argv, jsval* rval)
{
LIns *proto_ins;
CHECK_STATUS(getClassPrototype(ctor, proto_ins));
LIns *arr_ins;
if (argc == 0 || (argc == 1 && JSVAL_IS_NUMBER(argv[0]))) {
// arr_ins = js_NewEmptyArray(cx, Array.prototype)
LIns *args[] = { proto_ins, cx_ins };
arr_ins = lir->insCall(&js_NewEmptyArray_ci, args);
guard(false, lir->ins_eq0(arr_ins), OOM_EXIT);
if (argc == 1) {
// array_ins.fslots[JSSLOT_ARRAY_LENGTH] = length
lir->insStorei(f2i(get(argv)), // FIXME: is this 64-bit safe?
arr_ins,
offsetof(JSObject, fslots) + JSSLOT_ARRAY_LENGTH * sizeof(jsval));
}
} else {
// arr_ins = js_NewUninitializedArray(cx, Array.prototype, argc)
LIns *args[] = { INS_CONST(argc), proto_ins, cx_ins };
arr_ins = lir->insCall(&js_NewUninitializedArray_ci, args);
guard(false, lir->ins_eq0(arr_ins), OOM_EXIT);
// arr->dslots[i] = box_jsval(vp[i]); for i in 0..argc
LIns *dslots_ins = NULL;
for (uint32 i = 0; i < argc && !lirbuf->outOMem(); i++) {
LIns *elt_ins = get(argv + i);
box_jsval(argv[i], elt_ins);
stobj_set_dslot(arr_ins, i, dslots_ins, elt_ins, "set_array_elt");
}
if (argc > 0)
stobj_set_fslot(arr_ins, JSSLOT_ARRAY_COUNT, INS_CONST(argc), "set_array_count");
}
set(rval, arr_ins);
pendingTraceableNative = IGNORE_NATIVE_CALL_COMPLETE_CALLBACK;
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::emitNativeCall(JSTraceableNative* known, uintN argc, LIns* args[])
{
bool constructing = known->flags & JSTN_CONSTRUCTOR;
if (JSTN_ERRTYPE(known) == FAIL_STATUS) {
// This needs to capture the pre-call state of the stack. So do not set
// pendingTraceableNative before taking this snapshot.
JS_ASSERT(!pendingTraceableNative);
// Take snapshot for deep LeaveTree and store it in cx->bailExit.
// If we are calling a slow native, add information to the side exit
// for SynthesizeSlowNativeFrame.
VMSideExit* exit = snapshot(DEEP_BAIL_EXIT);
JSObject* funobj = JSVAL_TO_OBJECT(stackval(0 - (2 + argc)));
if (FUN_SLOW_NATIVE(GET_FUNCTION_PRIVATE(cx, funobj)))
exit->setNativeCallee(funobj, constructing);
lir->insStorei(INS_CONSTPTR(exit), cx_ins, offsetof(JSContext, bailExit));
// Tell nanojit not to discard or defer stack writes before this call.
LIns* guardRec = createGuardRecord(exit);
lir->insGuard(LIR_xbarrier, guardRec, guardRec);
}
LIns* res_ins = lir->insCall(known->builtin, args);
rval_ins = res_ins;
switch (JSTN_ERRTYPE(known)) {
case FAIL_NULL:
guard(false, lir->ins_eq0(res_ins), OOM_EXIT);
break;
case FAIL_NEG:
res_ins = lir->ins1(LIR_i2f, res_ins);
guard(false, lir->ins2(LIR_flt, res_ins, lir->insImmq(0)), OOM_EXIT);
break;
case FAIL_VOID:
guard(false, lir->ins2i(LIR_eq, res_ins, JSVAL_TO_PSEUDO_BOOLEAN(JSVAL_VOID)), OOM_EXIT);
break;
case FAIL_COOKIE:
guard(false, lir->ins2(LIR_eq, res_ins, INS_CONST(JSVAL_ERROR_COOKIE)), OOM_EXIT);
break;
default:;
}
set(&stackval(0 - (2 + argc)), res_ins);
/*
* The return value will be processed by NativeCallComplete since
* we have to know the actual return value type for calls that return
* jsval (like Array_p_pop).
*/
pendingTraceableNative = known;
return JSRS_CONTINUE;
}
/*
* Check whether we have a specialized implementation for this native invocation.
*/
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::callTraceableNative(JSFunction* fun, uintN argc, bool constructing)
{
JSTraceableNative* known = FUN_TRCINFO(fun);
JS_ASSERT(known && (JSFastNative)fun->u.n.native == known->native);
JSStackFrame* fp = cx->fp;
jsbytecode *pc = fp->regs->pc;
jsval& fval = stackval(0 - (2 + argc));
jsval& tval = stackval(0 - (1 + argc));
LIns* this_ins = get(&tval);
LIns* args[nanojit::MAXARGS];
do {
if (((known->flags & JSTN_CONSTRUCTOR) != 0) != constructing)
continue;
uintN knownargc = strlen(known->argtypes);
if (argc != knownargc)
continue;
intN prefixc = strlen(known->prefix);
JS_ASSERT(prefixc <= 3);
LIns** argp = &args[argc + prefixc - 1];
char argtype;
#if defined _DEBUG
memset(args, 0xCD, sizeof(args));
#endif
uintN i;
for (i = prefixc; i--; ) {
argtype = known->prefix[i];
if (argtype == 'C') {
*argp = cx_ins;
} else if (argtype == 'T') { /* this, as an object */
if (JSVAL_IS_PRIMITIVE(tval))
goto next_specialization;
*argp = this_ins;
} else if (argtype == 'S') { /* this, as a string */
if (!JSVAL_IS_STRING(tval))
goto next_specialization;
*argp = this_ins;
} else if (argtype == 'f') {
*argp = INS_CONSTPTR(JSVAL_TO_OBJECT(fval));
} else if (argtype == 'p') {
CHECK_STATUS(getClassPrototype(JSVAL_TO_OBJECT(fval), *argp));
} else if (argtype == 'R') {
*argp = INS_CONSTPTR(cx->runtime);
} else if (argtype == 'P') {
// FIXME: Set pc to imacpc when recording JSOP_CALL inside the
// JSOP_GETELEM imacro (bug 476559).
if (*pc == JSOP_CALL && fp->imacpc && *fp->imacpc == JSOP_GETELEM)
*argp = INS_CONSTPTR(fp->imacpc);
else
*argp = INS_CONSTPTR(pc);
} else if (argtype == 'D') { /* this, as a number */
if (!isNumber(tval))
goto next_specialization;
*argp = this_ins;
} else {
JS_NOT_REACHED("unknown prefix arg type");
}
argp--;
}
for (i = knownargc; i--; ) {
jsval& arg = stackval(0 - (i + 1));
*argp = get(&arg);
argtype = known->argtypes[i];
if (argtype == 'd' || argtype == 'i') {
if (!isNumber(arg))
goto next_specialization;
if (argtype == 'i')
*argp = f2i(*argp);
} else if (argtype == 'o') {
if (JSVAL_IS_PRIMITIVE(arg))
goto next_specialization;
} else if (argtype == 's') {
if (!JSVAL_IS_STRING(arg))
goto next_specialization;
} else if (argtype == 'r') {
if (!VALUE_IS_REGEXP(cx, arg))
goto next_specialization;
} else if (argtype == 'f') {
if (!VALUE_IS_FUNCTION(cx, arg))
goto next_specialization;
} else if (argtype == 'v') {
box_jsval(arg, *argp);
} else {
goto next_specialization;
}
argp--;
}
#if defined _DEBUG
JS_ASSERT(args[0] != (LIns *)0xcdcdcdcd);
#endif
return emitNativeCall(known, argc, args);
next_specialization:;
} while ((known++)->flags & JSTN_MORE);
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::callNative(uintN argc, JSOp mode)
{
LIns* args[5];
JS_ASSERT(mode == JSOP_CALL || mode == JSOP_NEW || mode == JSOP_APPLY);
jsval* vp = &stackval(0 - (2 + argc));
JSObject* funobj = JSVAL_TO_OBJECT(vp[0]);
JSFunction* fun = GET_FUNCTION_PRIVATE(cx, funobj);
if (fun->flags & JSFUN_TRACEABLE) {
JSRecordingStatus status;
if ((status = callTraceableNative(fun, argc, mode == JSOP_NEW)) != JSRS_STOP)
return status;
}
JSFastNative native = (JSFastNative)fun->u.n.native;
if (native == js_fun_apply || native == js_fun_call)
ABORT_TRACE("trying to call native apply or call");
// Allocate the vp vector and emit code to root it.
uintN vplen = 2 + JS_MAX(argc, FUN_MINARGS(fun)) + fun->u.n.extra;
if (!(fun->flags & JSFUN_FAST_NATIVE))
vplen++; // slow native return value slot
lir->insStorei(INS_CONST(vplen), cx_ins, offsetof(JSContext, nativeVpLen));
LIns* invokevp_ins = lir->insAlloc(vplen * sizeof(jsval));
lir->insStorei(invokevp_ins, cx_ins, offsetof(JSContext, nativeVp));
// vp[0] is the callee.
lir->insStorei(INS_CONSTWORD(OBJECT_TO_JSVAL(funobj)), invokevp_ins, 0);
// Calculate |this|.
LIns* this_ins;
if (mode == JSOP_NEW) {
JSClass* clasp = fun->u.n.clasp;
JS_ASSERT(clasp != &js_SlowArrayClass);
if (!clasp)
clasp = &js_ObjectClass;
JS_ASSERT(((jsuword) clasp & 3) == 0);
// Abort on |new Function|. js_NewInstance would allocate a regular-
// sized JSObject, not a Function-sized one. (The Function ctor would
// deep-bail anyway but let's not go there.)
if (clasp == &js_FunctionClass)
ABORT_TRACE("new Function");
if (clasp->getObjectOps)
ABORT_TRACE("new with non-native ops");
args[0] = INS_CONSTPTR(funobj);
args[1] = INS_CONSTPTR(clasp);
args[2] = cx_ins;
newobj_ins = lir->insCall(&js_NewInstance_ci, args);
guard(false, lir->ins_eq0(newobj_ins), OOM_EXIT);
this_ins = newobj_ins; // boxing an object is a no-op
} else if (JSFUN_BOUND_METHOD_TEST(fun->flags)) {
this_ins = INS_CONSTWORD(OBJECT_TO_JSVAL(OBJ_GET_PARENT(cx, funobj)));
} else {
this_ins = get(&vp[1]);
/*
* For fast natives, 'null' or primitives are fine as as 'this' value.
* For slow natives we have to ensure the object is substituted for the
* appropriate global object or boxed object value. JSOP_NEW allocates its
* own object so its guaranteed to have a valid 'this' value.
*/
if (!(fun->flags & JSFUN_FAST_NATIVE)) {
if (JSVAL_IS_NULL(vp[1])) {
JSObject* thisObj = js_ComputeThis(cx, JS_FALSE, vp + 2);
if (!thisObj)
ABORT_TRACE_ERROR("error in js_ComputeGlobalThis");
this_ins = INS_CONSTPTR(thisObj);
} else if (!JSVAL_IS_OBJECT(vp[1])) {
ABORT_TRACE("slow native(primitive, args)");
} else {
if (guardClass(JSVAL_TO_OBJECT(vp[1]), this_ins, &js_WithClass, snapshot(MISMATCH_EXIT)))
ABORT_TRACE("can't trace slow native invocation on With object");
this_ins = lir->ins_choose(lir->ins_eq0(stobj_get_fslot(this_ins, JSSLOT_PARENT)),
INS_CONSTPTR(globalObj),
this_ins);
}
}
box_jsval(vp[1], this_ins);
}
lir->insStorei(this_ins, invokevp_ins, 1 * sizeof(jsval));
// Populate argv.
for (uintN n = 2; n < 2 + argc; n++) {
LIns* i = get(&vp[n]);
box_jsval(vp[n], i);
lir->insStorei(i, invokevp_ins, n * sizeof(jsval));
// For a very long argument list we might run out of LIR space, so
// check inside the loop.
if (lirbuf->outOMem())
ABORT_TRACE("out of memory in argument list");
}
// Populate extra slots, including the return value slot for a slow native.
if (2 + argc < vplen) {
LIns* undef_ins = INS_CONSTWORD(JSVAL_VOID);
for (uintN n = 2 + argc; n < vplen; n++) {
lir->insStorei(undef_ins, invokevp_ins, n * sizeof(jsval));
if (lirbuf->outOMem())
ABORT_TRACE("out of memory in extra slots");
}
}
// Set up arguments for the JSNative or JSFastNative.
uint32 types;
if (fun->flags & JSFUN_FAST_NATIVE) {
if (mode == JSOP_NEW)
ABORT_TRACE("untraceable fast native constructor");
native_rval_ins = invokevp_ins;
args[0] = invokevp_ins;
args[1] = lir->insImm(argc);
args[2] = cx_ins;
types = ARGSIZE_LO | ARGSIZE_LO << 2 | ARGSIZE_LO << 4 | ARGSIZE_LO << 6;
} else {
native_rval_ins = lir->ins2i(LIR_piadd, invokevp_ins, int32_t((vplen - 1) * sizeof(jsval)));
args[0] = native_rval_ins;
args[1] = lir->ins2i(LIR_piadd, invokevp_ins, int32_t(2 * sizeof(jsval)));
args[2] = lir->insImm(argc);
args[3] = this_ins;
args[4] = cx_ins;
types = ARGSIZE_LO | ARGSIZE_LO << 2 | ARGSIZE_LO << 4 | ARGSIZE_LO << 6 |
ARGSIZE_LO << 8 | ARGSIZE_LO << 10;
}
// Generate CallInfo and a JSTraceableNative structure on the fly. Do not
// use JSTN_UNBOX_AFTER for mode JSOP_NEW because record_NativeCallComplete
// unboxes the result specially.
CallInfo* ci = (CallInfo*) lir->skip(sizeof(struct CallInfo))->payload();
ci->_address = uintptr_t(fun->u.n.native);
ci->_cse = ci->_fold = 0;
ci->_abi = ABI_CDECL;
ci->_argtypes = types;
#ifdef DEBUG
ci->_name = JS_GetFunctionName(fun);
#endif
// Generate a JSTraceableNative structure on the fly.
generatedTraceableNative->builtin = ci;
generatedTraceableNative->native = (JSFastNative)fun->u.n.native;
generatedTraceableNative->flags = FAIL_STATUS | ((mode == JSOP_NEW)
? JSTN_CONSTRUCTOR
: JSTN_UNBOX_AFTER);
generatedTraceableNative->prefix = generatedTraceableNative->argtypes = NULL;
// argc is the original argc here. It is used to calculate where to place
// the return value.
JSRecordingStatus status;
if ((status = emitNativeCall(generatedTraceableNative, argc, args)) != JSRS_CONTINUE)
return status;
// Unroot the vp.
lir->insStorei(INS_CONSTPTR(NULL), cx_ins, offsetof(JSContext, nativeVp));
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::functionCall(uintN argc, JSOp mode)
{
jsval& fval = stackval(0 - (2 + argc));
JS_ASSERT(&fval >= StackBase(cx->fp));
if (!VALUE_IS_FUNCTION(cx, fval))
ABORT_TRACE("callee is not a function");
jsval& tval = stackval(0 - (1 + argc));
/*
* If callee is not constant, it's a shapeless call and we have to guard
* explicitly that we will get this callee again at runtime.
*/
if (!get(&fval)->isconst())
CHECK_STATUS(guardCallee(fval));
/*
* Require that the callee be a function object, to avoid guarding on its
* class here. We know if the callee and this were pushed by JSOP_CALLNAME
* or JSOP_CALLPROP that callee is a *particular* function, since these hit
* the property cache and guard on the object (this) in which the callee
* was found. So it's sufficient to test here that the particular function
* is interpreted, not guard on that condition.
*
* Bytecode sequences that push shapeless callees must guard on the callee
* class being Function and the function being interpreted.
*/
JSFunction* fun = GET_FUNCTION_PRIVATE(cx, JSVAL_TO_OBJECT(fval));
if (FUN_INTERPRETED(fun)) {
if (mode == JSOP_NEW) {
LIns* args[] = { get(&fval), INS_CONSTPTR(&js_ObjectClass), cx_ins };
LIns* tv_ins = lir->insCall(&js_NewInstance_ci, args);
guard(false, lir->ins_eq0(tv_ins), OOM_EXIT);
set(&tval, tv_ins);
}
return interpretedFunctionCall(fval, fun, argc, mode == JSOP_NEW);
}
if (FUN_SLOW_NATIVE(fun)) {
JSNative native = fun->u.n.native;
jsval* argv = &tval + 1;
if (native == js_Array)
return newArray(JSVAL_TO_OBJECT(fval), argc, argv, &fval);
if (native == js_String && argc == 1) {
if (mode == JSOP_NEW)
return newString(JSVAL_TO_OBJECT(fval), 1, argv, &fval);
if (!JSVAL_IS_PRIMITIVE(argv[0])) {
ABORT_IF_XML(argv[0]);
return call_imacro(call_imacros.String);
}
set(&fval, stringify(argv[0]));
pendingTraceableNative = IGNORE_NATIVE_CALL_COMPLETE_CALLBACK;
return JSRS_CONTINUE;
}
}
return callNative(argc, mode);
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_NEW()
{
uintN argc = GET_ARGC(cx->fp->regs->pc);
cx->fp->assertValidStackDepth(argc + 2);
return functionCall(argc, JSOP_NEW);
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_DELNAME()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_DELPROP()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_DELELEM()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_TYPEOF()
{
jsval& r = stackval(-1);
LIns* type;
if (JSVAL_IS_STRING(r)) {
type = INS_CONSTPTR(ATOM_TO_STRING(cx->runtime->atomState.typeAtoms[JSTYPE_STRING]));
} else if (isNumber(r)) {
type = INS_CONSTPTR(ATOM_TO_STRING(cx->runtime->atomState.typeAtoms[JSTYPE_NUMBER]));
} else if (VALUE_IS_FUNCTION(cx, r)) {
type = INS_CONSTPTR(ATOM_TO_STRING(cx->runtime->atomState.typeAtoms[JSTYPE_FUNCTION]));
} else {
LIns* args[] = { get(&r), cx_ins };
if (JSVAL_TAG(r) == JSVAL_BOOLEAN) {
// We specialize identically for boolean and undefined. We must not have a hole here.
// Pass the unboxed type here, since TypeOfBoolean knows how to handle it.
JS_ASSERT(r == JSVAL_TRUE || r == JSVAL_FALSE || r == JSVAL_VOID);
type = lir->insCall(&js_TypeOfBoolean_ci, args);
} else {
JS_ASSERT(JSVAL_TAG(r) == JSVAL_OBJECT);
type = lir->insCall(&js_TypeOfObject_ci, args);
}
}
set(&r, type);
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_VOID()
{
stack(-1, INS_CONST(JSVAL_TO_PSEUDO_BOOLEAN(JSVAL_VOID)));
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_INCNAME()
{
return incName(1);
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_INCPROP()
{
return incProp(1);
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_INCELEM()
{
return incElem(1);
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_DECNAME()
{
return incName(-1);
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_DECPROP()
{
return incProp(-1);
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_DECELEM()
{
return incElem(-1);
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::incName(jsint incr, bool pre)
{
jsval* vp;
CHECK_STATUS(name(vp));
LIns* v_ins = get(vp);
CHECK_STATUS(inc(*vp, v_ins, incr, pre));
set(vp, v_ins);
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_NAMEINC()
{
return incName(1, false);
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_PROPINC()
{
return incProp(1, false);
}
// XXX consolidate with record_JSOP_GETELEM code...
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_ELEMINC()
{
return incElem(1, false);
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_NAMEDEC()
{
return incName(-1, false);
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_PROPDEC()
{
return incProp(-1, false);
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_ELEMDEC()
{
return incElem(-1, false);
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_GETPROP()
{
return getProp(stackval(-1));
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_SETPROP()
{
jsval& l = stackval(-2);
if (JSVAL_IS_PRIMITIVE(l))
ABORT_TRACE("primitive this for SETPROP");
JSObject* obj = JSVAL_TO_OBJECT(l);
if (obj->map->ops->setProperty != js_SetProperty)
ABORT_TRACE("non-native JSObjectOps::setProperty");
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_SetPropHit(JSPropCacheEntry* entry, JSScopeProperty* sprop)
{
if (entry == JS_NO_PROP_CACHE_FILL)
ABORT_TRACE("can't trace uncacheable property set");
if (PCVCAP_TAG(entry->vcap) >= 1)
ABORT_TRACE("can't trace inherited property set");
jsbytecode* pc = cx->fp->regs->pc;
JS_ASSERT(entry->kpc == pc);
jsval& r = stackval(-1);
jsval& l = stackval(-2);
JS_ASSERT(!JSVAL_IS_PRIMITIVE(l));
JSObject* obj = JSVAL_TO_OBJECT(l);
LIns* obj_ins = get(&l);
JSScope* scope = OBJ_SCOPE(obj);
JS_ASSERT(scope->object == obj);
JS_ASSERT(SCOPE_HAS_PROPERTY(scope, sprop));
if (!isValidSlot(scope, sprop))
return JSRS_STOP;
/*
* Setting a function-valued property might need to rebrand the object; we
* don't trace that case. There's no need to guard on that, though, because
* separating functions into the trace-time type JSVAL_TFUN will save the
* day!
*/
if (VALUE_IS_FUNCTION(cx, r))
ABORT_TRACE("can't trace function-valued property set");
if (obj == globalObj) {
JS_ASSERT(SPROP_HAS_VALID_SLOT(sprop, scope));
uint32 slot = sprop->slot;
if (!lazilyImportGlobalSlot(slot))
ABORT_TRACE("lazy import of global slot failed");
LIns* r_ins = get(&r);
set(&STOBJ_GET_SLOT(obj, slot), r_ins);
JS_ASSERT(*pc != JSOP_INITPROP);
if (pc[JSOP_SETPROP_LENGTH] != JSOP_POP)
set(&l, r_ins);
return JSRS_CONTINUE;
}
// The global object's shape is guarded at trace entry, all others need a guard here.
LIns* map_ins = lir->insLoad(LIR_ldp, obj_ins, (int)offsetof(JSObject, map));
LIns* ops_ins;
if (!map_is_native(obj->map, map_ins, ops_ins, offsetof(JSObjectOps, setProperty)))
ABORT_TRACE("non-native map");
LIns* shape_ins = addName(lir->insLoad(LIR_ld, map_ins, offsetof(JSScope, shape)), "shape");
guard(true, addName(lir->ins2i(LIR_eq, shape_ins, entry->kshape), "guard(kshape)"),
BRANCH_EXIT);
uint32 vshape = PCVCAP_SHAPE(entry->vcap);
if (entry->kshape != vshape) {
LIns *vshape_ins = lir->insLoad(LIR_ld,
lir->insLoad(LIR_ldp, cx_ins, offsetof(JSContext, runtime)),
offsetof(JSRuntime, protoHazardShape));
guard(true, addName(lir->ins2i(LIR_eq, vshape_ins, vshape), "guard(vshape)"),
MISMATCH_EXIT);
LIns* args[] = { INS_CONSTPTR(sprop), obj_ins, cx_ins };
LIns* ok_ins = lir->insCall(&js_AddProperty_ci, args);
guard(false, lir->ins_eq0(ok_ins), OOM_EXIT);
}
LIns* dslots_ins = NULL;
LIns* v_ins = get(&r);
LIns* boxed_ins = v_ins;
box_jsval(r, boxed_ins);
CHECK_STATUS(native_set(obj_ins, sprop, dslots_ins, boxed_ins));
if (*pc != JSOP_INITPROP && pc[JSOP_SETPROP_LENGTH] != JSOP_POP)
set(&l, v_ins);
return JSRS_CONTINUE;
}
/* Functions used by JSOP_GETELEM. */
static JSBool
GetProperty(JSContext *cx, uintN argc, jsval *vp)
{
jsval *argv;
jsid id;
JS_ASSERT_NOT_ON_TRACE(cx);
JS_ASSERT(cx->fp->imacpc && argc == 1);
argv = JS_ARGV(cx, vp);
JS_ASSERT(JSVAL_IS_STRING(argv[0]));
if (!js_ValueToStringId(cx, argv[0], &id))
return JS_FALSE;
argv[0] = ID_TO_VALUE(id);
return OBJ_GET_PROPERTY(cx, JS_THIS_OBJECT(cx, vp), id, &JS_RVAL(cx, vp));
}
static jsval FASTCALL
GetProperty_tn(JSContext *cx, jsbytecode *pc, JSObject *obj, JSString *name)
{
JSAutoTempIdRooter idr(cx);
JSAutoTempValueRooter tvr(cx);
if (!js_ValueToStringId(cx, STRING_TO_JSVAL(name), idr.addr()) ||
!OBJ_GET_PROPERTY(cx, obj, idr.id(), tvr.addr())) {
js_SetBuiltinError(cx);
*tvr.addr() = JSVAL_ERROR_COOKIE;
}
return tvr.value();
}
static JSBool
GetElement(JSContext *cx, uintN argc, jsval *vp)
{
jsval *argv;
jsid id;
JS_ASSERT_NOT_ON_TRACE(cx);
JS_ASSERT(cx->fp->imacpc && argc == 1);
argv = JS_ARGV(cx, vp);
JS_ASSERT(JSVAL_IS_NUMBER(argv[0]));
if (!JS_ValueToId(cx, argv[0], &id))
return JS_FALSE;
argv[0] = ID_TO_VALUE(id);
return OBJ_GET_PROPERTY(cx, JS_THIS_OBJECT(cx, vp), id, &JS_RVAL(cx, vp));
}
static jsval FASTCALL
GetElement_tn(JSContext* cx, jsbytecode *pc, JSObject* obj, int32 index)
{
JSAutoTempValueRooter tvr(cx);
JSAutoTempIdRooter idr(cx);
if (!js_Int32ToId(cx, index, idr.addr())) {
js_SetBuiltinError(cx);
return JSVAL_ERROR_COOKIE;
}
if (!OBJ_GET_PROPERTY(cx, obj, idr.id(), tvr.addr())) {
js_SetBuiltinError(cx);
*tvr.addr() = JSVAL_ERROR_COOKIE;
}
return tvr.value();
}
JS_DEFINE_TRCINFO_1(GetProperty,
(4, (static, JSVAL_FAIL, GetProperty_tn, CONTEXT, PC, THIS, STRING, 0, 0)))
JS_DEFINE_TRCINFO_1(GetElement,
(4, (extern, JSVAL_FAIL, GetElement_tn, CONTEXT, PC, THIS, INT32, 0, 0)))
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_GETELEM()
{
bool call = *cx->fp->regs->pc == JSOP_CALLELEM;
jsval& idx = stackval(-1);
jsval& lval = stackval(-2);
LIns* obj_ins = get(&lval);
LIns* idx_ins = get(&idx);
// Special case for array-like access of strings.
if (JSVAL_IS_STRING(lval) && isInt32(idx)) {
if (call)
ABORT_TRACE("JSOP_CALLELEM on a string");
int i = asInt32(idx);
if (size_t(i) >= JSSTRING_LENGTH(JSVAL_TO_STRING(lval)))
ABORT_TRACE("Invalid string index in JSOP_GETELEM");
idx_ins = makeNumberInt32(idx_ins);
LIns* args[] = { idx_ins, obj_ins, cx_ins };
LIns* unitstr_ins = lir->insCall(&js_String_getelem_ci, args);
guard(false, lir->ins_eq0(unitstr_ins), MISMATCH_EXIT);
set(&lval, unitstr_ins);
return JSRS_CONTINUE;
}
if (JSVAL_IS_PRIMITIVE(lval))
ABORT_TRACE("JSOP_GETLEM on a primitive");
ABORT_IF_XML(lval);
JSObject* obj = JSVAL_TO_OBJECT(lval);
jsval id;
LIns* v_ins;
/* Property access using a string name or something we have to stringify. */
if (!JSVAL_IS_INT(idx)) {
if (!JSVAL_IS_PRIMITIVE(idx))
ABORT_TRACE("non-primitive index");
// If index is not a string, turn it into a string.
if (!js_InternNonIntElementId(cx, obj, idx, &id))
ABORT_TRACE_ERROR("failed to intern non-int element id");
set(&idx, stringify(idx));
// Store the interned string to the stack to save the interpreter from redoing this work.
idx = ID_TO_VALUE(id);
// The object is not guaranteed to be a dense array at this point, so it might be the
// global object, which we have to guard against.
CHECK_STATUS(guardNotGlobalObject(obj, obj_ins));
return call_imacro(call ? callelem_imacros.callprop : getelem_imacros.getprop);
}
if (!guardDenseArray(obj, obj_ins, BRANCH_EXIT)) {
CHECK_STATUS(guardNotGlobalObject(obj, obj_ins));
return call_imacro(call ? callelem_imacros.callelem : getelem_imacros.getelem);
}
// Fast path for dense arrays accessed with a integer index.
jsval* vp;
LIns* addr_ins;
CHECK_STATUS(denseArrayElement(lval, idx, vp, v_ins, addr_ins));
set(&lval, v_ins);
if (call)
set(&idx, obj_ins);
return JSRS_CONTINUE;
}
/* Functions used by JSOP_SETELEM */
static JSBool
SetProperty(JSContext *cx, uintN argc, jsval *vp)
{
jsval *argv;
jsid id;
JS_ASSERT(argc == 2);
argv = JS_ARGV(cx, vp);
JS_ASSERT(JSVAL_IS_STRING(argv[0]));
if (!js_ValueToStringId(cx, argv[0], &id))
return JS_FALSE;
argv[0] = ID_TO_VALUE(id);
if (!OBJ_SET_PROPERTY(cx, JS_THIS_OBJECT(cx, vp), id, &argv[1]))
return JS_FALSE;
JS_SET_RVAL(cx, vp, JSVAL_VOID);
return JS_TRUE;
}
static JSBool FASTCALL
SetProperty_tn(JSContext* cx, JSObject* obj, JSString* idstr, jsval v)
{
JSAutoTempValueRooter tvr(cx, v);
JSAutoTempIdRooter idr(cx);
if (!js_ValueToStringId(cx, STRING_TO_JSVAL(idstr), idr.addr()) ||
!OBJ_SET_PROPERTY(cx, obj, idr.id(), tvr.addr())) {
js_SetBuiltinError(cx);
}
return JSVAL_TO_PSEUDO_BOOLEAN(JSVAL_VOID);
}
static JSBool
SetElement(JSContext *cx, uintN argc, jsval *vp)
{
jsval *argv;
jsid id;
JS_ASSERT(argc == 2);
argv = JS_ARGV(cx, vp);
JS_ASSERT(JSVAL_IS_NUMBER(argv[0]));
if (!JS_ValueToId(cx, argv[0], &id))
return JS_FALSE;
argv[0] = ID_TO_VALUE(id);
if (!OBJ_SET_PROPERTY(cx, JS_THIS_OBJECT(cx, vp), id, &argv[1]))
return JS_FALSE;
JS_SET_RVAL(cx, vp, JSVAL_VOID);
return JS_TRUE;
}
static JSBool FASTCALL
SetElement_tn(JSContext* cx, JSObject* obj, int32 index, jsval v)
{
JSAutoTempIdRooter idr(cx);
JSAutoTempValueRooter tvr(cx, v);
if (!js_Int32ToId(cx, index, idr.addr()) ||
!OBJ_SET_PROPERTY(cx, obj, idr.id(), tvr.addr())) {
js_SetBuiltinError(cx);
}
return JSVAL_TO_PSEUDO_BOOLEAN(JSVAL_VOID);
}
JS_DEFINE_TRCINFO_1(SetProperty,
(4, (extern, BOOL_FAIL, SetProperty_tn, CONTEXT, THIS, STRING, JSVAL, 0, 0)))
JS_DEFINE_TRCINFO_1(SetElement,
(4, (extern, BOOL_FAIL, SetElement_tn, CONTEXT, THIS, INT32, JSVAL, 0, 0)))
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_SETELEM()
{
jsval& v = stackval(-1);
jsval& idx = stackval(-2);
jsval& lval = stackval(-3);
/* no guards for type checks, trace specialized this already */
if (JSVAL_IS_PRIMITIVE(lval))
ABORT_TRACE("left JSOP_SETELEM operand is not an object");
ABORT_IF_XML(lval);
JSObject* obj = JSVAL_TO_OBJECT(lval);
LIns* obj_ins = get(&lval);
LIns* idx_ins = get(&idx);
LIns* v_ins = get(&v);
jsid id;
if (!JSVAL_IS_INT(idx)) {
if (!JSVAL_IS_PRIMITIVE(idx))
ABORT_TRACE("non-primitive index");
// If index is not a string, turn it into a string.
if (!js_InternNonIntElementId(cx, obj, idx, &id))
ABORT_TRACE_ERROR("failed to intern non-int element id");
set(&idx, stringify(idx));
// Store the interned string to the stack to save the interpreter from redoing this work.
idx = ID_TO_VALUE(id);
// The object is not guaranteed to be a dense array at this point, so it might be the
// global object, which we have to guard against.
CHECK_STATUS(guardNotGlobalObject(obj, obj_ins));
return call_imacro((*cx->fp->regs->pc == JSOP_INITELEM)
? initelem_imacros.initprop
: setelem_imacros.setprop);
}
if (JSVAL_TO_INT(idx) < 0 || !OBJ_IS_DENSE_ARRAY(cx, obj)) {
CHECK_STATUS(guardNotGlobalObject(obj, obj_ins));
return call_imacro((*cx->fp->regs->pc == JSOP_INITELEM)
? initelem_imacros.initelem
: setelem_imacros.setelem);
}
// Make sure the array is actually dense.
if (!guardDenseArray(obj, obj_ins, BRANCH_EXIT))
return JSRS_STOP;
// Fast path for dense arrays accessed with a non-negative integer index. In case the trace
// calculated the index using the FPU, force it to be an integer.
idx_ins = makeNumberInt32(idx_ins);
// Box the value so we can use one builtin instead of having to add one builtin for every
// storage type.
LIns* boxed_v_ins = v_ins;
box_jsval(v, boxed_v_ins);
LIns* args[] = { boxed_v_ins, idx_ins, obj_ins, cx_ins };
LIns* res_ins = lir->insCall(&js_Array_dense_setelem_ci, args);
guard(false, lir->ins_eq0(res_ins), MISMATCH_EXIT);
jsbytecode* pc = cx->fp->regs->pc;
if (*pc == JSOP_SETELEM && pc[JSOP_SETELEM_LENGTH] != JSOP_POP)
set(&lval, v_ins);
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_CALLNAME()
{
JSObject* obj = cx->fp->scopeChain;
if (obj != globalObj) {
jsval* vp;
CHECK_STATUS(activeCallOrGlobalSlot(obj, vp));
stack(0, get(vp));
stack(1, INS_CONSTPTR(globalObj));
return JSRS_CONTINUE;
}
LIns* obj_ins = scopeChain();
JSObject* obj2;
jsuword pcval;
CHECK_STATUS(test_property_cache(obj, obj_ins, obj2, pcval));
if (PCVAL_IS_NULL(pcval) || !PCVAL_IS_OBJECT(pcval))
ABORT_TRACE("callee is not an object");
JS_ASSERT(HAS_FUNCTION_CLASS(PCVAL_TO_OBJECT(pcval)));
stack(0, INS_CONSTPTR(PCVAL_TO_OBJECT(pcval)));
stack(1, obj_ins);
return JSRS_CONTINUE;
}
JS_DEFINE_CALLINFO_5(extern, UINT32, js_GetUpvarArgOnTrace, CONTEXT, UINT32, INT32, UINT32,
DOUBLEPTR, 0, 0)
JS_DEFINE_CALLINFO_5(extern, UINT32, js_GetUpvarVarOnTrace, CONTEXT, UINT32, INT32, UINT32,
DOUBLEPTR, 0, 0)
JS_DEFINE_CALLINFO_5(extern, UINT32, js_GetUpvarStackOnTrace, CONTEXT, UINT32, INT32, UINT32,
DOUBLEPTR, 0, 0)
/*
* Record LIR to get the given upvar. Return the LIR instruction for
* the upvar value. NULL is returned only on a can't-happen condition
* with an invalid typemap. The value of the upvar is returned as v.
*/
JS_REQUIRES_STACK LIns*
TraceRecorder::upvar(JSScript* script, JSUpvarArray* uva, uintN index, jsval& v)
{
/*
* Try to find the upvar in the current trace's tracker. For &vr to be
* the address of the jsval found in js_GetUpvar, we must initialize
* vr directly with the result, so it is a reference to the same location.
* It does not work to assign the result to v, because v is an already
* existing reference that points to something else.
*/
uint32 cookie = uva->vector[index];
jsval& vr = js_GetUpvar(cx, script->staticLevel, cookie);
v = vr;
LIns* upvar_ins = get(&vr);
if (upvar_ins) {
return upvar_ins;
}
/*
* The upvar is not in the current trace, so get the upvar value
* exactly as the interpreter does and unbox.
*/
uint32 level = script->staticLevel - UPVAR_FRAME_SKIP(cookie);
uint32 cookieSlot = UPVAR_FRAME_SLOT(cookie);
JSStackFrame* fp = cx->display[level];
const CallInfo* ci;
int32 slot;
if (!fp->fun) {
ci = &js_GetUpvarStackOnTrace_ci;
slot = cookieSlot;
} else if (cookieSlot < fp->fun->nargs) {
ci = &js_GetUpvarArgOnTrace_ci;
slot = cookieSlot;
} else if (cookieSlot == CALLEE_UPVAR_SLOT) {
ci = &js_GetUpvarArgOnTrace_ci;
slot = -2;
} else {
ci = &js_GetUpvarVarOnTrace_ci;
slot = cookieSlot - fp->fun->nargs;
}
LIns* outp = lir->insAlloc(sizeof(double));
LIns* args[] = {
outp,
INS_CONST(callDepth),
INS_CONST(slot),
INS_CONST(level),
cx_ins
};
LIns* call_ins = lir->insCall(ci, args);
uint8 type = getCoercedType(v);
guard(true,
addName(lir->ins2(LIR_eq, call_ins, lir->insImm(type)),
"guard(type-stable upvar)"),
BRANCH_EXIT);
LOpcode loadOp;
switch (type) {
case JSVAL_DOUBLE:
loadOp = LIR_ldq;
break;
case JSVAL_OBJECT:
case JSVAL_STRING:
case JSVAL_TFUN:
case JSVAL_TNULL:
loadOp = LIR_ldp;
break;
case JSVAL_INT:
case JSVAL_BOOLEAN:
loadOp = LIR_ld;
break;
case JSVAL_BOXED:
default:
JS_NOT_REACHED("found boxed type in an upvar type map entry");
return NULL;
}
LIns* result = lir->insLoad(loadOp, outp, lir->insImm(0));
if (type == JSVAL_INT)
result = lir->ins1(LIR_i2f, result);
return result;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_GETUPVAR()
{
uintN index = GET_UINT16(cx->fp->regs->pc);
JSScript *script = cx->fp->script;
JSUpvarArray* uva = JS_SCRIPT_UPVARS(script);
JS_ASSERT(index < uva->length);
jsval v;
LIns* upvar_ins = upvar(script, uva, index, v);
if (!upvar_ins)
return JSRS_STOP;
stack(0, upvar_ins);
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_CALLUPVAR()
{
CHECK_STATUS(record_JSOP_GETUPVAR());
stack(1, INS_CONSTPTR(NULL));
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_GETDSLOT()
{
JSObject* callee = cx->fp->callee;
LIns* callee_ins = get(&cx->fp->argv[-2]);
unsigned index = GET_UINT16(cx->fp->regs->pc);
LIns* dslots_ins = NULL;
LIns* v_ins = stobj_get_dslot(callee_ins, index, dslots_ins);
unbox_jsval(callee->dslots[index], v_ins, snapshot(BRANCH_EXIT));
stack(0, v_ins);
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_CALLDSLOT()
{
CHECK_STATUS(record_JSOP_GETDSLOT());
stack(1, INS_CONSTPTR(NULL));
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::guardCallee(jsval& callee)
{
JS_ASSERT(VALUE_IS_FUNCTION(cx, callee));
VMSideExit* branchExit = snapshot(BRANCH_EXIT);
JSObject* callee_obj = JSVAL_TO_OBJECT(callee);
LIns* callee_ins = get(&callee);
guard(true,
lir->ins2(LIR_eq,
lir->ins2(LIR_piand,
stobj_get_fslot(callee_ins, JSSLOT_PRIVATE),
INS_CONSTWORD(~JSVAL_INT)),
INS_CONSTPTR(OBJ_GET_PRIVATE(cx, callee_obj))),
branchExit);
guard(true,
lir->ins2(LIR_eq,
stobj_get_fslot(callee_ins, JSSLOT_PARENT),
INS_CONSTPTR(OBJ_GET_PARENT(cx, callee_obj))),
branchExit);
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::interpretedFunctionCall(jsval& fval, JSFunction* fun, uintN argc, bool constructing)
{
if (JS_GetGlobalForObject(cx, JSVAL_TO_OBJECT(fval)) != globalObj)
ABORT_TRACE("JSOP_CALL or JSOP_NEW crosses global scopes");
JSStackFrame* fp = cx->fp;
// TODO: track the copying via the tracker...
if (argc < fun->nargs &&
jsuword(fp->regs->sp + (fun->nargs - argc)) > cx->stackPool.current->limit) {
ABORT_TRACE("can't trace calls with too few args requiring argv move");
}
// Generate a type map for the outgoing frame and stash it in the LIR
unsigned stackSlots = js_NativeStackSlots(cx, 0/*callDepth*/);
if (sizeof(FrameInfo) + stackSlots * sizeof(uint8) > MAX_SKIP_BYTES)
ABORT_TRACE("interpreted function call requires saving too much stack");
LIns* data = lir->skip(sizeof(FrameInfo) + stackSlots * sizeof(uint8));
FrameInfo* fi = (FrameInfo*)data->payload();
uint8* typemap = (uint8 *)(fi + 1);
uint8* m = typemap;
/* Determine the type of a store by looking at the current type of the actual value the
interpreter is using. For numbers we have to check what kind of store we used last
(integer or double) to figure out what the side exit show reflect in its typemap. */
FORALL_SLOTS_IN_PENDING_FRAMES(cx, 0/*callDepth*/,
*m++ = determineSlotType(vp);
);
if (argc >= 0x8000)
ABORT_TRACE("too many arguments");
fi->callee = JSVAL_TO_OBJECT(fval);
fi->block = fp->blockChain;
fi->pc = fp->regs->pc;
fi->imacpc = fp->imacpc;
fi->spdist = fp->regs->sp - fp->slots;
fi->set_argc(argc, constructing);
fi->callerHeight = js_NativeStackSlots(cx, 0) - (2 + argc);
fi->callerArgc = fp->argc;
unsigned callDepth = getCallDepth();
if (callDepth >= treeInfo->maxCallDepth)
treeInfo->maxCallDepth = callDepth + 1;
lir->insStorei(INS_CONSTPTR(fi), lirbuf->rp, callDepth * sizeof(FrameInfo*));
atoms = fun->u.i.script->atomMap.vector;
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_CALL()
{
uintN argc = GET_ARGC(cx->fp->regs->pc);
cx->fp->assertValidStackDepth(argc + 2);
return functionCall(argc,
(cx->fp->imacpc && *cx->fp->imacpc == JSOP_APPLY)
? JSOP_APPLY
: JSOP_CALL);
}
static jsbytecode* apply_imacro_table[] = {
apply_imacros.apply0,
apply_imacros.apply1,
apply_imacros.apply2,
apply_imacros.apply3,
apply_imacros.apply4,
apply_imacros.apply5,
apply_imacros.apply6,
apply_imacros.apply7,
apply_imacros.apply8
};
static jsbytecode* call_imacro_table[] = {
apply_imacros.call0,
apply_imacros.call1,
apply_imacros.call2,
apply_imacros.call3,
apply_imacros.call4,
apply_imacros.call5,
apply_imacros.call6,
apply_imacros.call7,
apply_imacros.call8
};
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_APPLY()
{
JSStackFrame* fp = cx->fp;
jsbytecode *pc = fp->regs->pc;
uintN argc = GET_ARGC(pc);
cx->fp->assertValidStackDepth(argc + 2);
jsval* vp = fp->regs->sp - (argc + 2);
jsuint length = 0;
JSObject* aobj = NULL;
LIns* aobj_ins = NULL;
JS_ASSERT(!fp->imacpc);
if (!VALUE_IS_FUNCTION(cx, vp[0]))
return record_JSOP_CALL();
ABORT_IF_XML(vp[0]);
JSObject* obj = JSVAL_TO_OBJECT(vp[0]);
JSFunction* fun = GET_FUNCTION_PRIVATE(cx, obj);
if (FUN_INTERPRETED(fun))
return record_JSOP_CALL();
bool apply = (JSFastNative)fun->u.n.native == js_fun_apply;
if (!apply && (JSFastNative)fun->u.n.native != js_fun_call)
return record_JSOP_CALL();
/*
* We don't trace apply and call with a primitive 'this', which is the
* first positional parameter.
*/
if (argc > 0 && JSVAL_IS_PRIMITIVE(vp[2]))
return record_JSOP_CALL();
/*
* Guard on the identity of this, which is the function we are applying.
*/
if (!VALUE_IS_FUNCTION(cx, vp[1]))
ABORT_TRACE("callee is not a function");
CHECK_STATUS(guardCallee(vp[1]));
if (apply && argc >= 2) {
if (argc != 2)
ABORT_TRACE("apply with excess arguments");
if (JSVAL_IS_PRIMITIVE(vp[3]))
ABORT_TRACE("arguments parameter of apply is primitive");
aobj = JSVAL_TO_OBJECT(vp[3]);
aobj_ins = get(&vp[3]);
/*
* We expect a dense array for the arguments (the other
* frequent case is the arguments object, but that we
* don't trace at the moment).
*/
if (!guardDenseArray(aobj, aobj_ins))
ABORT_TRACE("arguments parameter of apply is not a dense array");
/*
* We trace only apply calls with a certain number of arguments.
*/
length = jsuint(aobj->fslots[JSSLOT_ARRAY_LENGTH]);
if (length >= JS_ARRAY_LENGTH(apply_imacro_table))
ABORT_TRACE("too many arguments to apply");
/*
* Make sure the array has the same length at runtime.
*/
guard(true,
lir->ins2i(LIR_eq,
stobj_get_fslot(aobj_ins, JSSLOT_ARRAY_LENGTH),
length),
BRANCH_EXIT);
return call_imacro(apply_imacro_table[length]);
}
if (argc >= JS_ARRAY_LENGTH(call_imacro_table))
ABORT_TRACE("too many arguments to call");
return call_imacro(call_imacro_table[argc]);
}
static JSBool FASTCALL
CatchStopIteration_tn(JSContext* cx, JSBool ok, jsval* vp)
{
if (!ok && cx->throwing && js_ValueIsStopIteration(cx->exception)) {
cx->throwing = JS_FALSE;
cx->exception = JSVAL_VOID;
*vp = JSVAL_HOLE;
return JS_TRUE;
}
return ok;
}
JS_DEFINE_TRCINFO_1(CatchStopIteration_tn,
(3, (static, BOOL, CatchStopIteration_tn, CONTEXT, BOOL, JSVALPTR, 0, 0)))
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_NativeCallComplete()
{
if (pendingTraceableNative == IGNORE_NATIVE_CALL_COMPLETE_CALLBACK)
return JSRS_CONTINUE;
jsbytecode* pc = cx->fp->regs->pc;
JS_ASSERT(pendingTraceableNative);
JS_ASSERT(*pc == JSOP_CALL || *pc == JSOP_APPLY || *pc == JSOP_NEW);
jsval& v = stackval(-1);
LIns* v_ins = get(&v);
/* At this point the generated code has already called the native function
and we can no longer fail back to the original pc location (JSOP_CALL)
because that would cause the interpreter to re-execute the native
function, which might have side effects.
Instead, the snapshot() call below sees that we are currently parked on
a traceable native's JSOP_CALL instruction, and it will advance the pc
to restore by the length of the current opcode. If the native's return
type is jsval, snapshot() will also indicate in the type map that the
element on top of the stack is a boxed value which doesn't need to be
boxed if the type guard generated by unbox_jsval() fails. */
if (JSTN_ERRTYPE(pendingTraceableNative) == FAIL_STATUS) {
// Keep cx->bailExit null when it's invalid.
lir->insStorei(INS_CONSTPTR(NULL), cx_ins, (int) offsetof(JSContext, bailExit));
LIns* status = lir->insLoad(LIR_ld, lirbuf->state, (int) offsetof(InterpState, builtinStatus));
if (pendingTraceableNative == generatedTraceableNative) {
LIns* ok_ins = v_ins;
/*
* Custom implementations of Iterator.next() throw a StopIteration exception.
* Catch and clear it and set the return value to JSVAL_HOLE in this case.
*/
if (uintptr_t(pc - nextiter_imacros.custom_iter_next) <
sizeof(nextiter_imacros.custom_iter_next)) {
LIns* args[] = { native_rval_ins, ok_ins, cx_ins }; /* reverse order */
ok_ins = lir->insCall(&CatchStopIteration_tn_ci, args);
}
/*
* If we run a generic traceable native, the return value is in the argument
* vector for native function calls. The actual return value of the native is a JSBool
* indicating the error status.
*/
v_ins = lir->insLoad(LIR_ld, native_rval_ins, 0);
if (*pc == JSOP_NEW) {
LIns* x = lir->ins_eq0(lir->ins2i(LIR_piand, v_ins, JSVAL_TAGMASK));
x = lir->ins_choose(x, v_ins, INS_CONST(0));
v_ins = lir->ins_choose(lir->ins_eq0(x), newobj_ins, x);
}
set(&v, v_ins);
/*
* If this is a generic traceable native invocation, propagate the boolean return
* value of the native into builtinStatus. If the return value (v_ins)
* is true, status' == status. Otherwise status' = status | JSBUILTIN_ERROR.
* We calculate (rval&1)^1, which is 1 if rval is JS_FALSE (error), and then
* shift that by 1 which is JSBUILTIN_ERROR.
*/
JS_STATIC_ASSERT((1 - JS_TRUE) << 1 == 0);
JS_STATIC_ASSERT((1 - JS_FALSE) << 1 == JSBUILTIN_ERROR);
status = lir->ins2(LIR_or,
status,
lir->ins2i(LIR_lsh,
lir->ins2i(LIR_xor,
lir->ins2i(LIR_and, ok_ins, 1),
1),
1));
lir->insStorei(status, lirbuf->state, (int) offsetof(InterpState, builtinStatus));
}
guard(true,
lir->ins_eq0(status),
STATUS_EXIT);
}
JSRecordingStatus ok = JSRS_CONTINUE;
if (pendingTraceableNative->flags & JSTN_UNBOX_AFTER) {
/*
* If we side exit on the unboxing code due to a type change, make sure that the boxed
* value is actually currently associated with that location, and that we are talking
* about the top of the stack here, which is where we expected boxed values.
*/
JS_ASSERT(&v == &cx->fp->regs->sp[-1] && get(&v) == v_ins);
unbox_jsval(v, v_ins, snapshot(BRANCH_EXIT));
set(&v, v_ins);
} else if (JSTN_ERRTYPE(pendingTraceableNative) == FAIL_NEG) {
/* Already added i2f in functionCall. */
JS_ASSERT(JSVAL_IS_NUMBER(v));
} else {
/* Convert the result to double if the builtin returns int32. */
if (JSVAL_IS_NUMBER(v) &&
(pendingTraceableNative->builtin->_argtypes & 3) == nanojit::ARGSIZE_LO) {
set(&v, lir->ins1(LIR_i2f, v_ins));
}
}
// We'll null pendingTraceableNative in monitorRecording, on the next op cycle.
// There must be a next op since the stack is non-empty.
return ok;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::name(jsval*& vp)
{
JSObject* obj = cx->fp->scopeChain;
if (obj != globalObj)
return activeCallOrGlobalSlot(obj, vp);
/* Can't use prop here, because we don't want unboxing from global slots. */
LIns* obj_ins = scopeChain();
uint32 slot;
JSObject* obj2;
jsuword pcval;
/*
* Property cache ensures that we are dealing with an existing property,
* and guards the shape for us.
*/
CHECK_STATUS(test_property_cache(obj, obj_ins, obj2, pcval));
/*
* Abort if property doesn't exist (interpreter will report an error.)
*/
if (PCVAL_IS_NULL(pcval))
ABORT_TRACE("named property not found");
/*
* Insist on obj being the directly addressed object.
*/
if (obj2 != obj)
ABORT_TRACE("name() hit prototype chain");
/* Don't trace getter or setter calls, our caller wants a direct slot. */
if (PCVAL_IS_SPROP(pcval)) {
JSScopeProperty* sprop = PCVAL_TO_SPROP(pcval);
if (!isValidSlot(OBJ_SCOPE(obj), sprop))
ABORT_TRACE("name() not accessing a valid slot");
slot = sprop->slot;
} else {
if (!PCVAL_IS_SLOT(pcval))
ABORT_TRACE("PCE is not a slot");
slot = PCVAL_TO_SLOT(pcval);
}
if (!lazilyImportGlobalSlot(slot))
ABORT_TRACE("lazy import of global slot failed");
vp = &STOBJ_GET_SLOT(obj, slot);
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::prop(JSObject* obj, LIns* obj_ins, uint32& slot, LIns*& v_ins)
{
/*
* Can't specialize to assert obj != global, must guard to avoid aliasing
* stale homes of stacked global variables.
*/
CHECK_STATUS(guardNotGlobalObject(obj, obj_ins));
/*
* Property cache ensures that we are dealing with an existing property,
* and guards the shape for us.
*/
JSObject* obj2;
jsuword pcval;
CHECK_STATUS(test_property_cache(obj, obj_ins, obj2, pcval));
/* Check for non-existent property reference, which results in undefined. */
const JSCodeSpec& cs = js_CodeSpec[*cx->fp->regs->pc];
if (PCVAL_IS_NULL(pcval)) {
/*
* We could specialize to guard on just JSClass.getProperty, but a mere
* class guard is simpler and slightly faster.
*/
if (OBJ_GET_CLASS(cx, obj)->getProperty != JS_PropertyStub) {
ABORT_TRACE("can't trace through access to undefined property if "
"JSClass.getProperty hook isn't stubbed");
}
guardClass(obj, obj_ins, OBJ_GET_CLASS(cx, obj), snapshot(MISMATCH_EXIT));
/*
* This trace will be valid as long as neither the object nor any object
* on its prototype chain changes shape.
*/
VMSideExit* exit = snapshot(BRANCH_EXIT);
do {
LIns* map_ins = lir->insLoad(LIR_ldp, obj_ins, (int)offsetof(JSObject, map));
LIns* ops_ins;
if (map_is_native(obj->map, map_ins, ops_ins)) {
LIns* shape_ins = addName(lir->insLoad(LIR_ld, map_ins, offsetof(JSScope, shape)),
"shape");
guard(true,
addName(lir->ins2i(LIR_eq, shape_ins, OBJ_SHAPE(obj)), "guard(shape)"),
exit);
} else if (!guardDenseArray(obj, obj_ins, BRANCH_EXIT))
ABORT_TRACE("non-native object involved in undefined property access");
} while (guardHasPrototype(obj, obj_ins, &obj, &obj_ins, exit));
v_ins = INS_CONST(JSVAL_TO_PSEUDO_BOOLEAN(JSVAL_VOID));
slot = SPROP_INVALID_SLOT;
return JSRS_CONTINUE;
}
/* Insist if setting on obj being the directly addressed object. */
uint32 setflags = (cs.format & (JOF_SET | JOF_INCDEC | JOF_FOR));
LIns* dslots_ins = NULL;
/* Don't trace getter or setter calls, our caller wants a direct slot. */
if (PCVAL_IS_SPROP(pcval)) {
JSScopeProperty* sprop = PCVAL_TO_SPROP(pcval);
if (setflags && !SPROP_HAS_STUB_SETTER(sprop))
ABORT_TRACE("non-stub setter");
if (setflags && (sprop->attrs & JSPROP_READONLY))
ABORT_TRACE("writing to a readonly property");
if (setflags != JOF_SET && !SPROP_HAS_STUB_GETTER(sprop)) {
// FIXME 450335: generalize this away from regexp built-in getters.
if (setflags == 0 &&
sprop->getter == js_RegExpClass.getProperty &&
sprop->shortid < 0) {
if (sprop->shortid == REGEXP_LAST_INDEX)
ABORT_TRACE("can't trace RegExp.lastIndex yet");
LIns* args[] = { INS_CONSTPTR(sprop), obj_ins, cx_ins };
v_ins = lir->insCall(&js_CallGetter_ci, args);
guard(false, lir->ins2(LIR_eq, v_ins, INS_CONST(JSVAL_ERROR_COOKIE)), OOM_EXIT);
/*
* BIG FAT WARNING: This snapshot cannot be a BRANCH_EXIT, since
* the value to the top of the stack is not the value we unbox.
*/
unbox_jsval((sprop->shortid == REGEXP_SOURCE) ? JSVAL_STRING : JSVAL_BOOLEAN,
v_ins,
snapshot(MISMATCH_EXIT));
return JSRS_CONTINUE;
}
if (setflags == 0 &&
sprop->getter == js_StringClass.getProperty &&
sprop->id == ATOM_KEY(cx->runtime->atomState.lengthAtom)) {
if (!guardClass(obj, obj_ins, &js_StringClass, snapshot(MISMATCH_EXIT)))
ABORT_TRACE("can't trace String.length on non-String objects");
LIns* str_ins = stobj_get_fslot(obj_ins, JSSLOT_PRIVATE);
str_ins = lir->ins2(LIR_piand, str_ins, INS_CONSTWORD(~JSVAL_TAGMASK));
v_ins = lir->ins1(LIR_i2f, getStringLength(str_ins));
return JSRS_CONTINUE;
}
ABORT_TRACE("non-stub getter");
}
if (!SPROP_HAS_VALID_SLOT(sprop, OBJ_SCOPE(obj2)))
ABORT_TRACE("no valid slot");
slot = sprop->slot;
} else {
if (!PCVAL_IS_SLOT(pcval))
ABORT_TRACE("PCE is not a slot");
slot = PCVAL_TO_SLOT(pcval);
}
if (obj2 != obj) {
if (setflags)
ABORT_TRACE("JOF_SET opcode hit prototype chain");
/*
* We're getting a proto-property. Walk up the prototype chain emitting
* proto slot loads, updating obj as we go, leaving obj set to obj2 with
* obj_ins the last proto-load.
*/
while (obj != obj2) {
obj_ins = stobj_get_slot(obj_ins, JSSLOT_PROTO, dslots_ins);
obj = STOBJ_GET_PROTO(obj);
}
}
v_ins = stobj_get_slot(obj_ins, slot, dslots_ins);
unbox_jsval(STOBJ_GET_SLOT(obj, slot), v_ins, snapshot(BRANCH_EXIT));
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::denseArrayElement(jsval& oval, jsval& ival, jsval*& vp, LIns*& v_ins,
LIns*& addr_ins)
{
JS_ASSERT(JSVAL_IS_OBJECT(oval) && JSVAL_IS_INT(ival));
JSObject* obj = JSVAL_TO_OBJECT(oval);
LIns* obj_ins = get(&oval);
jsint idx = JSVAL_TO_INT(ival);
LIns* idx_ins = makeNumberInt32(get(&ival));
VMSideExit* exit = snapshot(BRANCH_EXIT);
/* check that the index is within bounds */
LIns* dslots_ins = lir->insLoad(LIR_ldp, obj_ins, offsetof(JSObject, dslots));
jsuint capacity = js_DenseArrayCapacity(obj);
bool within = (jsuint(idx) < jsuint(obj->fslots[JSSLOT_ARRAY_LENGTH]) && jsuint(idx) < capacity);
if (!within) {
/* If idx < 0, stay on trace (and read value as undefined, since this is a dense array). */
LIns* br1 = NULL;
if (MAX_DSLOTS_LENGTH > JS_BITMASK(30) && !idx_ins->isconst()) {
JS_ASSERT(sizeof(jsval) == 8); // Only 64-bit machines support large enough arrays for this.
br1 = lir->insBranch(LIR_jt,
lir->ins2i(LIR_lt, idx_ins, 0),
NULL);
}
/* If not idx < length, stay on trace (and read value as undefined). */
LIns* br2 = lir->insBranch(LIR_jf,
lir->ins2(LIR_ult,
idx_ins,
stobj_get_fslot(obj_ins, JSSLOT_ARRAY_LENGTH)),
NULL);
/* If dslots is NULL, stay on trace (and read value as undefined). */
LIns* br3 = lir->insBranch(LIR_jt, lir->ins_eq0(dslots_ins), NULL);
/* If not idx < capacity, stay on trace (and read value as undefined). */
LIns* br4 = lir->insBranch(LIR_jf,
lir->ins2(LIR_ult,
idx_ins,
lir->insLoad(LIR_ldp,
dslots_ins,
-(int)sizeof(jsval))),
NULL);
lir->insGuard(LIR_x, lir->insImm(1), createGuardRecord(exit));
LIns* label = lir->ins0(LIR_label);
if (br1)
br1->target(label);
br2->target(label);
br3->target(label);
br4->target(label);
CHECK_STATUS(guardPrototypeHasNoIndexedProperties(obj, obj_ins, MISMATCH_EXIT));
// Return undefined and indicate that we didn't actually read this (addr_ins).
v_ins = lir->insImm(JSVAL_TO_PSEUDO_BOOLEAN(JSVAL_VOID));
addr_ins = NULL;
return JSRS_CONTINUE;
}
/* Guard against negative index */
if (MAX_DSLOTS_LENGTH > JS_BITMASK(30) && !idx_ins->isconst()) {
JS_ASSERT(sizeof(jsval) == 8); // Only 64-bit machines support large enough arrays for this.
guard(false,
lir->ins2i(LIR_lt, idx_ins, 0),
exit);
}
/* Guard array length */
guard(true,
lir->ins2(LIR_ult, idx_ins, stobj_get_fslot(obj_ins, JSSLOT_ARRAY_LENGTH)),
exit);
/* dslots must not be NULL */
guard(false,
lir->ins_eq0(dslots_ins),
exit);
/* Guard array capacity */
guard(true,
lir->ins2(LIR_ult,
idx_ins,
lir->insLoad(LIR_ldp, dslots_ins, 0 - (int)sizeof(jsval))),
exit);
/* Load the value and guard on its type to unbox it. */
vp = &obj->dslots[jsuint(idx)];
addr_ins = lir->ins2(LIR_piadd, dslots_ins,
lir->ins2i(LIR_pilsh, idx_ins, (sizeof(jsval) == 4) ? 2 : 3));
v_ins = lir->insLoad(LIR_ldp, addr_ins, 0);
unbox_jsval(*vp, v_ins, exit);
if (JSVAL_TAG(*vp) == JSVAL_BOOLEAN) {
/*
* If we read a hole from the array, convert it to undefined and guard that there
* are no indexed properties along the prototype chain.
*/
LIns* br = lir->insBranch(LIR_jf,
lir->ins2i(LIR_eq, v_ins, JSVAL_TO_PSEUDO_BOOLEAN(JSVAL_HOLE)),
NULL);
CHECK_STATUS(guardPrototypeHasNoIndexedProperties(obj, obj_ins, MISMATCH_EXIT));
br->target(lir->ins0(LIR_label));
/*
* Don't let the hole value escape. Turn it into an undefined.
*/
v_ins = lir->ins2i(LIR_and, v_ins, ~(JSVAL_HOLE_FLAG >> JSVAL_TAGBITS));
}
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::getProp(JSObject* obj, LIns* obj_ins)
{
uint32 slot;
LIns* v_ins;
CHECK_STATUS(prop(obj, obj_ins, slot, v_ins));
const JSCodeSpec& cs = js_CodeSpec[*cx->fp->regs->pc];
JS_ASSERT(cs.ndefs == 1);
stack(-cs.nuses, v_ins);
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::getProp(jsval& v)
{
if (JSVAL_IS_PRIMITIVE(v))
ABORT_TRACE("primitive lhs");
return getProp(JSVAL_TO_OBJECT(v), get(&v));
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_NAME()
{
jsval* vp;
CHECK_STATUS(name(vp));
stack(0, get(vp));
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_DOUBLE()
{
jsval v = jsval(atoms[GET_INDEX(cx->fp->regs->pc)]);
stack(0, lir->insImmf(*JSVAL_TO_DOUBLE(v)));
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_STRING()
{
JSAtom* atom = atoms[GET_INDEX(cx->fp->regs->pc)];
JS_ASSERT(ATOM_IS_STRING(atom));
stack(0, INS_CONSTPTR(ATOM_TO_STRING(atom)));
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_ZERO()
{
stack(0, lir->insImmq(0));
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_ONE()
{
stack(0, lir->insImmf(1));
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_NULL()
{
stack(0, INS_CONSTPTR(NULL));
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_THIS()
{
LIns* this_ins;
CHECK_STATUS(getThis(this_ins));
stack(0, this_ins);
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_FALSE()
{
stack(0, lir->insImm(0));
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_TRUE()
{
stack(0, lir->insImm(1));
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_OR()
{
return ifop();
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_AND()
{
return ifop();
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_TABLESWITCH()
{
#ifdef NANOJIT_IA32
/* Handle tableswitches specially -- prepare a jump table if needed. */
LIns* guardIns = tableswitch();
if (guardIns) {
fragment->lastIns = guardIns;
compile(&JS_TRACE_MONITOR(cx));
}
return JSRS_STOP;
#else
return switchop();
#endif
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_LOOKUPSWITCH()
{
return switchop();
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_STRICTEQ()
{
strictEquality(true, false);
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_STRICTNE()
{
strictEquality(false, false);
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_OBJECT()
{
JSStackFrame* fp = cx->fp;
JSScript* script = fp->script;
unsigned index = atoms - script->atomMap.vector + GET_INDEX(fp->regs->pc);
JSObject* obj;
JS_GET_SCRIPT_OBJECT(script, index, obj);
stack(0, INS_CONSTPTR(obj));
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_POP()
{
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_TRAP()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_GETARG()
{
stack(0, arg(GET_ARGNO(cx->fp->regs->pc)));
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_SETARG()
{
arg(GET_ARGNO(cx->fp->regs->pc), stack(-1));
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_GETLOCAL()
{
stack(0, var(GET_SLOTNO(cx->fp->regs->pc)));
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_SETLOCAL()
{
var(GET_SLOTNO(cx->fp->regs->pc), stack(-1));
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_UINT16()
{
stack(0, lir->insImmf(GET_UINT16(cx->fp->regs->pc)));
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_NEWINIT()
{
JSProtoKey key = JSProtoKey(GET_INT8(cx->fp->regs->pc));
LIns *proto_ins;
CHECK_STATUS(getClassPrototype(key, proto_ins));
LIns* args[] = { proto_ins, cx_ins };
const CallInfo *ci = (key == JSProto_Array) ? &js_NewEmptyArray_ci : &js_Object_tn_ci;
LIns* v_ins = lir->insCall(ci, args);
guard(false, lir->ins_eq0(v_ins), OOM_EXIT);
stack(0, v_ins);
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_ENDINIT()
{
#ifdef DEBUG
jsval& v = stackval(-1);
JS_ASSERT(!JSVAL_IS_PRIMITIVE(v));
#endif
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_INITPROP()
{
// All the action is in record_SetPropHit.
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_INITELEM()
{
return record_JSOP_SETELEM();
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_DEFSHARP()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_USESHARP()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_INCARG()
{
return inc(argval(GET_ARGNO(cx->fp->regs->pc)), 1);
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_INCLOCAL()
{
return inc(varval(GET_SLOTNO(cx->fp->regs->pc)), 1);
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_DECARG()
{
return inc(argval(GET_ARGNO(cx->fp->regs->pc)), -1);
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_DECLOCAL()
{
return inc(varval(GET_SLOTNO(cx->fp->regs->pc)), -1);
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_ARGINC()
{
return inc(argval(GET_ARGNO(cx->fp->regs->pc)), 1, false);
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_LOCALINC()
{
return inc(varval(GET_SLOTNO(cx->fp->regs->pc)), 1, false);
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_ARGDEC()
{
return inc(argval(GET_ARGNO(cx->fp->regs->pc)), -1, false);
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_LOCALDEC()
{
return inc(varval(GET_SLOTNO(cx->fp->regs->pc)), -1, false);
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_IMACOP()
{
JS_ASSERT(cx->fp->imacpc);
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_ITER()
{
jsval& v = stackval(-1);
if (JSVAL_IS_PRIMITIVE(v))
ABORT_TRACE("for-in on a primitive value");
ABORT_IF_XML(v);
jsuint flags = cx->fp->regs->pc[1];
if (hasIteratorMethod(JSVAL_TO_OBJECT(v))) {
if (flags == JSITER_ENUMERATE)
return call_imacro(iter_imacros.for_in);
if (flags == (JSITER_ENUMERATE | JSITER_FOREACH))
return call_imacro(iter_imacros.for_each);
} else {
if (flags == JSITER_ENUMERATE)
return call_imacro(iter_imacros.for_in_native);
if (flags == (JSITER_ENUMERATE | JSITER_FOREACH))
return call_imacro(iter_imacros.for_each_native);
}
ABORT_TRACE("unimplemented JSITER_* flags");
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_NEXTITER()
{
jsval& iterobj_val = stackval(-2);
if (JSVAL_IS_PRIMITIVE(iterobj_val))
ABORT_TRACE("for-in on a primitive value");
ABORT_IF_XML(iterobj_val);
JSObject* iterobj = JSVAL_TO_OBJECT(iterobj_val);
JSClass* clasp = STOBJ_GET_CLASS(iterobj);
LIns* iterobj_ins = get(&iterobj_val);
if (clasp == &js_IteratorClass || clasp == &js_GeneratorClass) {
guardClass(iterobj, iterobj_ins, clasp, snapshot(BRANCH_EXIT));
return call_imacro(nextiter_imacros.native_iter_next);
}
return call_imacro(nextiter_imacros.custom_iter_next);
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_ENDITER()
{
LIns* args[] = { stack(-2), cx_ins };
LIns* ok_ins = lir->insCall(&js_CloseIterator_ci, args);
guard(false, lir->ins_eq0(ok_ins), MISMATCH_EXIT);
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_FORNAME()
{
jsval* vp;
CHECK_STATUS(name(vp));
set(vp, stack(-1));
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_FORPROP()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_FORELEM()
{
return record_JSOP_DUP();
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_FORARG()
{
return record_JSOP_SETARG();
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_FORLOCAL()
{
return record_JSOP_SETLOCAL();
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_POPN()
{
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_BINDNAME()
{
JSStackFrame *fp = cx->fp;
JSObject *obj;
if (fp->fun) {
// We can't trace BINDNAME in functions that contain direct
// calls to eval, as they might add bindings which
// previously-traced references would have to see.
if (JSFUN_HEAVYWEIGHT_TEST(fp->fun->flags))
ABORT_TRACE("Can't trace JSOP_BINDNAME in heavyweight functions.");
// In non-heavyweight functions, we can safely skip the call
// object, if any.
obj = OBJ_GET_PARENT(cx, fp->callee);
} else {
obj = fp->scopeChain;
// In global code, fp->scopeChain can only contain blocks
// whose values are still on the stack. We never use BINDNAME
// to refer to these.
while (OBJ_GET_CLASS(cx, obj) == &js_BlockClass) {
// The block's values are still on the stack.
JS_ASSERT(OBJ_GET_PRIVATE(cx, obj) == fp);
obj = OBJ_GET_PARENT(cx, obj);
// Blocks always have parents.
JS_ASSERT(obj);
}
}
if (obj != globalObj)
ABORT_TRACE("JSOP_BINDNAME must return global object on trace");
// The trace is specialized to this global object. Furthermore,
// we know it is the sole 'global' object on the scope chain: we
// set globalObj to the scope chain element with no parent, and we
// reached it starting from the function closure or the current
// scopeChain, so there is nothing inner to it. So this must be
// the right base object.
stack(0, INS_CONSTPTR(globalObj));
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_SETNAME()
{
jsval& l = stackval(-2);
JS_ASSERT(!JSVAL_IS_PRIMITIVE(l));
/*
* Trace cases that are global code or in lightweight functions scoped by
* the global object only.
*/
JSObject* obj = JSVAL_TO_OBJECT(l);
if (obj != cx->fp->scopeChain || obj != globalObj)
ABORT_TRACE("JSOP_SETNAME left operand is not the global object");
// The rest of the work is in record_SetPropHit.
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_THROW()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_IN()
{
jsval& rval = stackval(-1);
jsval& lval = stackval(-2);
if (JSVAL_IS_PRIMITIVE(rval))
ABORT_TRACE("JSOP_IN on non-object right operand");
JSObject* obj = JSVAL_TO_OBJECT(rval);
LIns* obj_ins = get(&rval);
jsid id;
LIns* x;
if (JSVAL_IS_INT(lval)) {
id = INT_JSVAL_TO_JSID(lval);
LIns* args[] = { makeNumberInt32(get(&lval)), obj_ins, cx_ins };
x = lir->insCall(&js_HasNamedPropertyInt32_ci, args);
} else if (JSVAL_IS_STRING(lval)) {
if (!js_ValueToStringId(cx, lval, &id))
ABORT_TRACE_ERROR("left operand of JSOP_IN didn't convert to a string-id");
LIns* args[] = { get(&lval), obj_ins, cx_ins };
x = lir->insCall(&js_HasNamedProperty_ci, args);
} else {
ABORT_TRACE("string or integer expected");
}
guard(false, lir->ins2i(LIR_eq, x, JSVAL_TO_PSEUDO_BOOLEAN(JSVAL_VOID)), OOM_EXIT);
x = lir->ins2i(LIR_eq, x, 1);
JSObject* obj2;
JSProperty* prop;
if (!OBJ_LOOKUP_PROPERTY(cx, obj, id, &obj2, &prop))
ABORT_TRACE_ERROR("OBJ_LOOKUP_PROPERTY failed in JSOP_IN");
bool cond = prop != NULL;
if (prop)
OBJ_DROP_PROPERTY(cx, obj2, prop);
if (wasDeepAborted())
ABORT_TRACE("deep abort from property lookup");
/* The interpreter fuses comparisons and the following branch,
so we have to do that here as well. */
fuseIf(cx->fp->regs->pc + 1, cond, x);
/* We update the stack after the guard. This is safe since
the guard bails out at the comparison and the interpreter
will therefore re-execute the comparison. This way the
value of the condition doesn't have to be calculated and
saved on the stack in most cases. */
set(&lval, x);
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_INSTANCEOF()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_DEBUGGER()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_GOSUB()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_RETSUB()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_EXCEPTION()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_LINENO()
{
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_CONDSWITCH()
{
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_CASE()
{
strictEquality(true, true);
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_DEFAULT()
{
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_EVAL()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_ENUMELEM()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_GETTER()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_SETTER()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_DEFFUN()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_DEFFUN_FC()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_DEFCONST()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_DEFVAR()
{
return JSRS_STOP;
}
jsatomid
TraceRecorder::getFullIndex(ptrdiff_t pcoff)
{
jsatomid index = GET_INDEX(cx->fp->regs->pc + pcoff);
index += atoms - cx->fp->script->atomMap.vector;
return index;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_LAMBDA()
{
JSFunction* fun;
JS_GET_SCRIPT_FUNCTION(cx->fp->script, getFullIndex(), fun);
if (FUN_NULL_CLOSURE(fun) && OBJ_GET_PARENT(cx, FUN_OBJECT(fun)) == globalObj) {
LIns *proto_ins;
CHECK_STATUS(getClassPrototype(JSProto_Function, proto_ins));
LIns* args[] = { INS_CONSTPTR(globalObj), proto_ins, INS_CONSTPTR(fun), cx_ins };
LIns* x = lir->insCall(&js_NewNullClosure_ci, args);
stack(0, x);
return JSRS_CONTINUE;
}
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_LAMBDA_FC()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_CALLEE()
{
stack(0, get(&cx->fp->argv[-2]));
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_SETLOCALPOP()
{
var(GET_SLOTNO(cx->fp->regs->pc), stack(-1));
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_IFPRIMTOP()
{
// Traces are type-specialized, including null vs. object, so we need do
// nothing here. The upstream unbox_jsval called after valueOf or toString
// from an imacro (e.g.) will fork the trace for us, allowing us to just
// follow along mindlessly :-).
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_SETCALL()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_TRY()
{
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_FINALLY()
{
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_NOP()
{
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_ARGSUB()
{
JSStackFrame* fp = cx->fp;
if (!(fp->fun->flags & JSFUN_HEAVYWEIGHT)) {
uintN slot = GET_ARGNO(fp->regs->pc);
if (slot < fp->fun->nargs && slot < fp->argc && !fp->argsobj) {
stack(0, get(&cx->fp->argv[slot]));
return JSRS_CONTINUE;
}
}
ABORT_TRACE("can't trace JSOP_ARGSUB hard case");
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_ARGCNT()
{
if (!(cx->fp->fun->flags & JSFUN_HEAVYWEIGHT)) {
stack(0, lir->insImmf(cx->fp->argc));
return JSRS_CONTINUE;
}
ABORT_TRACE("can't trace heavyweight JSOP_ARGCNT");
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_DefLocalFunSetSlot(uint32 slot, JSObject* obj)
{
JSFunction* fun = GET_FUNCTION_PRIVATE(cx, obj);
if (FUN_NULL_CLOSURE(fun) && OBJ_GET_PARENT(cx, FUN_OBJECT(fun)) == globalObj) {
LIns *proto_ins;
CHECK_STATUS(getClassPrototype(JSProto_Function, proto_ins));
LIns* args[] = { INS_CONSTPTR(globalObj), proto_ins, INS_CONSTPTR(fun), cx_ins };
LIns* x = lir->insCall(&js_NewNullClosure_ci, args);
var(slot, x);
return JSRS_CONTINUE;
}
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_DEFLOCALFUN()
{
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_DEFLOCALFUN_FC()
{
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_GOTOX()
{
return record_JSOP_GOTO();
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_IFEQX()
{
return record_JSOP_IFEQ();
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_IFNEX()
{
return record_JSOP_IFNE();
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_ORX()
{
return record_JSOP_OR();
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_ANDX()
{
return record_JSOP_AND();
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_GOSUBX()
{
return record_JSOP_GOSUB();
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_CASEX()
{
strictEquality(true, true);
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_DEFAULTX()
{
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_TABLESWITCHX()
{
return record_JSOP_TABLESWITCH();
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_LOOKUPSWITCHX()
{
return switchop();
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_BACKPATCH()
{
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_BACKPATCH_POP()
{
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_THROWING()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_SETRVAL()
{
// If we implement this, we need to update JSOP_STOP.
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_RETRVAL()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_GETGVAR()
{
jsval slotval = cx->fp->slots[GET_SLOTNO(cx->fp->regs->pc)];
if (JSVAL_IS_NULL(slotval))
return JSRS_CONTINUE; // We will see JSOP_NAME from the interpreter's jump, so no-op here.
uint32 slot = JSVAL_TO_INT(slotval);
if (!lazilyImportGlobalSlot(slot))
ABORT_TRACE("lazy import of global slot failed");
stack(0, get(&STOBJ_GET_SLOT(globalObj, slot)));
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_SETGVAR()
{
jsval slotval = cx->fp->slots[GET_SLOTNO(cx->fp->regs->pc)];
if (JSVAL_IS_NULL(slotval))
return JSRS_CONTINUE; // We will see JSOP_NAME from the interpreter's jump, so no-op here.
uint32 slot = JSVAL_TO_INT(slotval);
if (!lazilyImportGlobalSlot(slot))
ABORT_TRACE("lazy import of global slot failed");
set(&STOBJ_GET_SLOT(globalObj, slot), stack(-1));
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_INCGVAR()
{
jsval slotval = cx->fp->slots[GET_SLOTNO(cx->fp->regs->pc)];
if (JSVAL_IS_NULL(slotval))
// We will see JSOP_INCNAME from the interpreter's jump, so no-op here.
return JSRS_CONTINUE;
uint32 slot = JSVAL_TO_INT(slotval);
if (!lazilyImportGlobalSlot(slot))
ABORT_TRACE("lazy import of global slot failed");
return inc(STOBJ_GET_SLOT(globalObj, slot), 1);
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_DECGVAR()
{
jsval slotval = cx->fp->slots[GET_SLOTNO(cx->fp->regs->pc)];
if (JSVAL_IS_NULL(slotval))
// We will see JSOP_INCNAME from the interpreter's jump, so no-op here.
return JSRS_CONTINUE;
uint32 slot = JSVAL_TO_INT(slotval);
if (!lazilyImportGlobalSlot(slot))
ABORT_TRACE("lazy import of global slot failed");
return inc(STOBJ_GET_SLOT(globalObj, slot), -1);
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_GVARINC()
{
jsval slotval = cx->fp->slots[GET_SLOTNO(cx->fp->regs->pc)];
if (JSVAL_IS_NULL(slotval))
// We will see JSOP_INCNAME from the interpreter's jump, so no-op here.
return JSRS_CONTINUE;
uint32 slot = JSVAL_TO_INT(slotval);
if (!lazilyImportGlobalSlot(slot))
ABORT_TRACE("lazy import of global slot failed");
return inc(STOBJ_GET_SLOT(globalObj, slot), 1, false);
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_GVARDEC()
{
jsval slotval = cx->fp->slots[GET_SLOTNO(cx->fp->regs->pc)];
if (JSVAL_IS_NULL(slotval))
// We will see JSOP_INCNAME from the interpreter's jump, so no-op here.
return JSRS_CONTINUE;
uint32 slot = JSVAL_TO_INT(slotval);
if (!lazilyImportGlobalSlot(slot))
ABORT_TRACE("lazy import of global slot failed");
return inc(STOBJ_GET_SLOT(globalObj, slot), -1, false);
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_REGEXP()
{
return JSRS_STOP;
}
// begin JS_HAS_XML_SUPPORT
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_DEFXMLNS()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_ANYNAME()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_QNAMEPART()
{
return record_JSOP_STRING();
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_QNAMECONST()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_QNAME()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_TOATTRNAME()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_TOATTRVAL()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_ADDATTRNAME()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_ADDATTRVAL()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_BINDXMLNAME()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_SETXMLNAME()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_XMLNAME()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_DESCENDANTS()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_FILTER()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_ENDFILTER()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_TOXML()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_TOXMLLIST()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_XMLTAGEXPR()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_XMLELTEXPR()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_XMLOBJECT()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_XMLCDATA()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_XMLCOMMENT()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_XMLPI()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_GETFUNNS()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_STARTXML()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_STARTXMLEXPR()
{
return JSRS_STOP;
}
// end JS_HAS_XML_SUPPORT
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_CALLPROP()
{
jsval& l = stackval(-1);
JSObject* obj;
LIns* obj_ins;
LIns* this_ins;
if (!JSVAL_IS_PRIMITIVE(l)) {
obj = JSVAL_TO_OBJECT(l);
obj_ins = get(&l);
this_ins = obj_ins; // |this| for subsequent call
} else {
jsint i;
debug_only(const char* protoname = NULL;)
if (JSVAL_IS_STRING(l)) {
i = JSProto_String;
debug_only(protoname = "String.prototype";)
} else if (JSVAL_IS_NUMBER(l)) {
i = JSProto_Number;
debug_only(protoname = "Number.prototype";)
} else if (JSVAL_TAG(l) == JSVAL_BOOLEAN) {
if (l == JSVAL_VOID)
ABORT_TRACE("callprop on void");
guard(false, lir->ins2i(LIR_eq, get(&l), JSVAL_TO_PSEUDO_BOOLEAN(JSVAL_VOID)), MISMATCH_EXIT);
i = JSProto_Boolean;
debug_only(protoname = "Boolean.prototype";)
} else {
JS_ASSERT(JSVAL_IS_NULL(l) || JSVAL_IS_VOID(l));
ABORT_TRACE("callprop on null or void");
}
if (!js_GetClassPrototype(cx, NULL, INT_TO_JSID(i), &obj))
ABORT_TRACE_ERROR("GetClassPrototype failed!");
obj_ins = INS_CONSTPTR(obj);
debug_only(obj_ins = addName(obj_ins, protoname);)
this_ins = get(&l); // use primitive as |this|
}
JSObject* obj2;
jsuword pcval;
CHECK_STATUS(test_property_cache(obj, obj_ins, obj2, pcval));
if (PCVAL_IS_NULL(pcval) || !PCVAL_IS_OBJECT(pcval))
ABORT_TRACE("callee is not an object");
JS_ASSERT(HAS_FUNCTION_CLASS(PCVAL_TO_OBJECT(pcval)));
if (JSVAL_IS_PRIMITIVE(l)) {
JSFunction* fun = GET_FUNCTION_PRIVATE(cx, PCVAL_TO_OBJECT(pcval));
if (!PRIMITIVE_THIS_TEST(fun, l))
ABORT_TRACE("callee does not accept primitive |this|");
}
stack(0, this_ins);
stack(-1, INS_CONSTPTR(PCVAL_TO_OBJECT(pcval)));
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_DELDESC()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_UINT24()
{
stack(0, lir->insImmf(GET_UINT24(cx->fp->regs->pc)));
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_INDEXBASE()
{
atoms += GET_INDEXBASE(cx->fp->regs->pc);
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_RESETBASE()
{
atoms = cx->fp->script->atomMap.vector;
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_RESETBASE0()
{
atoms = cx->fp->script->atomMap.vector;
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_CALLELEM()
{
return record_JSOP_GETELEM();
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_STOP()
{
JSStackFrame *fp = cx->fp;
if (fp->imacpc) {
// End of imacro, so return true to the interpreter immediately. The
// interpreter's JSOP_STOP case will return from the imacro, back to
// the pc after the calling op, still in the same JSStackFrame.
atoms = fp->script->atomMap.vector;
return JSRS_CONTINUE;
}
/*
* We know falling off the end of a constructor returns the new object that
* was passed in via fp->argv[-1], while falling off the end of a function
* returns undefined.
*
* NB: we do not support script rval (eval, API users who want the result
* of the last expression-statement, debugger API calls).
*/
if (fp->flags & JSFRAME_CONSTRUCTING) {
JS_ASSERT(OBJECT_TO_JSVAL(fp->thisp) == fp->argv[-1]);
rval_ins = get(&fp->argv[-1]);
} else {
rval_ins = INS_CONST(JSVAL_TO_PSEUDO_BOOLEAN(JSVAL_VOID));
}
clearFrameSlotsFromCache();
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_GETXPROP()
{
jsval& l = stackval(-1);
if (JSVAL_IS_PRIMITIVE(l))
ABORT_TRACE("primitive-this for GETXPROP?");
JSObject* obj = JSVAL_TO_OBJECT(l);
if (obj != cx->fp->scopeChain || obj != globalObj)
return JSRS_STOP;
jsval* vp;
CHECK_STATUS(name(vp));
stack(-1, get(vp));
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_CALLXMLNAME()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_TYPEOFEXPR()
{
return record_JSOP_TYPEOF();
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_ENTERBLOCK()
{
JSObject* obj;
JS_GET_SCRIPT_OBJECT(cx->fp->script, getFullIndex(0), obj);
LIns* void_ins = INS_CONST(JSVAL_TO_PSEUDO_BOOLEAN(JSVAL_VOID));
for (int i = 0, n = OBJ_BLOCK_COUNT(cx, obj); i < n; i++)
stack(i, void_ins);
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_LEAVEBLOCK()
{
/* We mustn't exit the lexical block we began recording in. */
if (cx->fp->blockChain != lexicalBlock)
return JSRS_CONTINUE;
else
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_GENERATOR()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_YIELD()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_ARRAYPUSH()
{
uint32_t slot = GET_UINT16(cx->fp->regs->pc);
JS_ASSERT(cx->fp->script->nfixed <= slot);
JS_ASSERT(cx->fp->slots + slot < cx->fp->regs->sp - 1);
jsval &arrayval = cx->fp->slots[slot];
JS_ASSERT(JSVAL_IS_OBJECT(arrayval));
JS_ASSERT(OBJ_IS_DENSE_ARRAY(cx, JSVAL_TO_OBJECT(arrayval)));
LIns *array_ins = get(&arrayval);
jsval &elt = stackval(-1);
LIns *elt_ins = get(&elt);
box_jsval(elt, elt_ins);
LIns *args[] = { elt_ins, array_ins, cx_ins };
LIns *ok_ins = lir->insCall(&js_ArrayCompPush_ci, args);
guard(false, lir->ins_eq0(ok_ins), OOM_EXIT);
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_ENUMCONSTELEM()
{
return JSRS_STOP;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_LEAVEBLOCKEXPR()
{
LIns* v_ins = stack(-1);
int n = -1 - GET_UINT16(cx->fp->regs->pc);
stack(n, v_ins);
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_GETTHISPROP()
{
LIns* this_ins;
CHECK_STATUS(getThis(this_ins));
/*
* It's safe to just use cx->fp->thisp here because getThis() returns JSRS_STOP if thisp
* is not available.
*/
CHECK_STATUS(getProp(cx->fp->thisp, this_ins));
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_GETARGPROP()
{
return getProp(argval(GET_ARGNO(cx->fp->regs->pc)));
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_GETLOCALPROP()
{
return getProp(varval(GET_SLOTNO(cx->fp->regs->pc)));
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_INDEXBASE1()
{
atoms += 1 << 16;
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_INDEXBASE2()
{
atoms += 2 << 16;
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_INDEXBASE3()
{
atoms += 3 << 16;
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_CALLGVAR()
{
jsval slotval = cx->fp->slots[GET_SLOTNO(cx->fp->regs->pc)];
if (JSVAL_IS_NULL(slotval))
// We will see JSOP_CALLNAME from the interpreter's jump, so no-op here.
return JSRS_CONTINUE;
uint32 slot = JSVAL_TO_INT(slotval);
if (!lazilyImportGlobalSlot(slot))
ABORT_TRACE("lazy import of global slot failed");
jsval& v = STOBJ_GET_SLOT(globalObj, slot);
stack(0, get(&v));
stack(1, INS_CONSTPTR(NULL));
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_CALLLOCAL()
{
uintN slot = GET_SLOTNO(cx->fp->regs->pc);
stack(0, var(slot));
stack(1, INS_CONSTPTR(NULL));
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_CALLARG()
{
uintN slot = GET_ARGNO(cx->fp->regs->pc);
stack(0, arg(slot));
stack(1, INS_CONSTPTR(NULL));
return JSRS_CONTINUE;
}
/* Functions for use with JSOP_CALLBUILTIN. */
static JSBool
ObjectToIterator(JSContext *cx, uintN argc, jsval *vp)
{
jsval *argv = JS_ARGV(cx, vp);
JS_ASSERT(JSVAL_IS_INT(argv[0]));
JS_SET_RVAL(cx, vp, JS_THIS(cx, vp));
return js_ValueToIterator(cx, JSVAL_TO_INT(argv[0]), &JS_RVAL(cx, vp));
}
static JSObject* FASTCALL
ObjectToIterator_tn(JSContext* cx, jsbytecode* pc, JSObject *obj, int32 flags)
{
jsval v = OBJECT_TO_JSVAL(obj);
JSBool ok = js_ValueToIterator(cx, flags, &v);
if (!ok) {
js_SetBuiltinError(cx);
return NULL;
}
return JSVAL_TO_OBJECT(v);
}
static JSBool
CallIteratorNext(JSContext *cx, uintN argc, jsval *vp)
{
return js_CallIteratorNext(cx, JS_THIS_OBJECT(cx, vp), &JS_RVAL(cx, vp));
}
static jsval FASTCALL
CallIteratorNext_tn(JSContext* cx, jsbytecode* pc, JSObject* iterobj)
{
JSAutoTempValueRooter tvr(cx);
JSBool ok = js_CallIteratorNext(cx, iterobj, tvr.addr());
if (!ok) {
js_SetBuiltinError(cx);
return JSVAL_ERROR_COOKIE;
}
return tvr.value();
}
JS_DEFINE_TRCINFO_1(ObjectToIterator,
(4, (static, OBJECT_FAIL, ObjectToIterator_tn, CONTEXT, PC, THIS, INT32, 0, 0)))
JS_DEFINE_TRCINFO_1(CallIteratorNext,
(3, (static, JSVAL_FAIL, CallIteratorNext_tn, CONTEXT, PC, THIS, 0, 0)))
static const struct BuiltinFunctionInfo {
JSTraceableNative *tn;
int nargs;
} builtinFunctionInfo[JSBUILTIN_LIMIT] = {
{ObjectToIterator_trcinfo, 1},
{CallIteratorNext_trcinfo, 0},
{GetProperty_trcinfo, 1},
{GetElement_trcinfo, 1},
{SetProperty_trcinfo, 2},
{SetElement_trcinfo, 2}
};
JSObject *
js_GetBuiltinFunction(JSContext *cx, uintN index)
{
JSRuntime *rt = cx->runtime;
JSObject *funobj = rt->builtinFunctions[index];
if (!funobj) {
/* Use NULL parent and atom. Builtin functions never escape to scripts. */
JS_ASSERT(index < JS_ARRAY_LENGTH(builtinFunctionInfo));
const BuiltinFunctionInfo *bfi = &builtinFunctionInfo[index];
JSFunction *fun = js_NewFunction(cx,
NULL,
JS_DATA_TO_FUNC_PTR(JSNative, bfi->tn),
bfi->nargs,
JSFUN_FAST_NATIVE | JSFUN_TRACEABLE,
NULL,
NULL);
if (fun) {
funobj = FUN_OBJECT(fun);
STOBJ_CLEAR_PROTO(funobj);
STOBJ_CLEAR_PARENT(funobj);
JS_LOCK_GC(rt);
if (!rt->builtinFunctions[index]) /* retest now that the lock is held */
rt->builtinFunctions[index] = funobj;
else
funobj = rt->builtinFunctions[index];
JS_UNLOCK_GC(rt);
}
}
return funobj;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_CALLBUILTIN()
{
JSObject *obj = js_GetBuiltinFunction(cx, GET_INDEX(cx->fp->regs->pc));
if (!obj)
ABORT_TRACE_ERROR("error in js_GetBuiltinFunction");
stack(0, get(&stackval(-1)));
stack(-1, INS_CONSTPTR(obj));
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_INT8()
{
stack(0, lir->insImmf(GET_INT8(cx->fp->regs->pc)));
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_INT32()
{
stack(0, lir->insImmf(GET_INT32(cx->fp->regs->pc)));
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_LENGTH()
{
jsval& l = stackval(-1);
if (JSVAL_IS_PRIMITIVE(l)) {
if (!JSVAL_IS_STRING(l))
ABORT_TRACE("non-string primitive JSOP_LENGTH unsupported");
set(&l, lir->ins1(LIR_i2f, getStringLength(get(&l))));
return JSRS_CONTINUE;
}
JSObject* obj = JSVAL_TO_OBJECT(l);
LIns* obj_ins = get(&l);
LIns* v_ins;
if (OBJ_IS_ARRAY(cx, obj)) {
if (OBJ_IS_DENSE_ARRAY(cx, obj)) {
if (!guardDenseArray(obj, obj_ins, BRANCH_EXIT)) {
JS_NOT_REACHED("OBJ_IS_DENSE_ARRAY but not?!?");
return JSRS_STOP;
}
} else {
if (!guardClass(obj, obj_ins, &js_SlowArrayClass, snapshot(BRANCH_EXIT)))
ABORT_TRACE("can't trace length property access on non-array");
}
v_ins = lir->ins1(LIR_i2f, stobj_get_fslot(obj_ins, JSSLOT_ARRAY_LENGTH));
} else {
if (!OBJ_IS_NATIVE(obj))
ABORT_TRACE("can't trace length property access on non-array, non-native object");
return getProp(obj, obj_ins);
}
set(&l, v_ins);
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_NEWARRAY()
{
LIns *proto_ins;
CHECK_STATUS(getClassPrototype(JSProto_Array, proto_ins));
uint32 len = GET_UINT16(cx->fp->regs->pc);
cx->fp->assertValidStackDepth(len);
LIns* args[] = { lir->insImm(len), proto_ins, cx_ins };
LIns* v_ins = lir->insCall(&js_NewUninitializedArray_ci, args);
guard(false, lir->ins_eq0(v_ins), OOM_EXIT);
LIns* dslots_ins = NULL;
uint32 count = 0;
for (uint32 i = 0; i < len; i++) {
jsval& v = stackval(int(i) - int(len));
if (v != JSVAL_HOLE)
count++;
LIns* elt_ins = get(&v);
box_jsval(v, elt_ins);
stobj_set_dslot(v_ins, i, dslots_ins, elt_ins, "set_array_elt");
}
if (count > 0)
stobj_set_fslot(v_ins, JSSLOT_ARRAY_COUNT, INS_CONST(count), "set_array_count");
stack(-int(len), v_ins);
return JSRS_CONTINUE;
}
JS_REQUIRES_STACK JSRecordingStatus
TraceRecorder::record_JSOP_HOLE()
{
stack(0, INS_CONST(JSVAL_TO_PSEUDO_BOOLEAN(JSVAL_HOLE)));
return JSRS_CONTINUE;
}
JSRecordingStatus
TraceRecorder::record_JSOP_LOOP()
{
return JSRS_CONTINUE;
}
#define DBG_STUB(OP) \
JS_REQUIRES_STACK JSRecordingStatus \
TraceRecorder::record_##OP() \
{ \
ABORT_TRACE("can't trace " #OP); \
}
DBG_STUB(JSOP_GETUPVAR_DBG)
DBG_STUB(JSOP_CALLUPVAR_DBG)
DBG_STUB(JSOP_DEFFUN_DBGFC)
DBG_STUB(JSOP_DEFLOCALFUN_DBGFC)
DBG_STUB(JSOP_LAMBDA_DBGFC)
#ifdef JS_JIT_SPEW
/* Prints information about entry typemaps and unstable exits for all peers at a PC */
void
js_DumpPeerStability(JSTraceMonitor* tm, const void* ip, JSObject* globalObj, uint32 globalShape,
uint32 argc)
{
Fragment* f;
TreeInfo* ti;
bool looped = false;
unsigned length = 0;
for (f = getLoop(tm, ip, globalObj, globalShape, argc); f != NULL; f = f->peer) {
if (!f->vmprivate)
continue;
printf("fragment %p:\nENTRY: ", (void*)f);
ti = (TreeInfo*)f->vmprivate;
if (looped)
JS_ASSERT(ti->nStackTypes == length);
for (unsigned i = 0; i < ti->nStackTypes; i++)
printf("S%d ", ti->stackTypeMap()[i]);
for (unsigned i = 0; i < ti->nGlobalTypes(); i++)
printf("G%d ", ti->globalTypeMap()[i]);
printf("\n");
UnstableExit* uexit = ti->unstableExits;
while (uexit != NULL) {
printf("EXIT: ");
uint8* m = getFullTypeMap(uexit->exit);
for (unsigned i = 0; i < uexit->exit->numStackSlots; i++)
printf("S%d ", m[i]);
for (unsigned i = 0; i < uexit->exit->numGlobalSlots; i++)
printf("G%d ", m[uexit->exit->numStackSlots + i]);
printf("\n");
uexit = uexit->next;
}
length = ti->nStackTypes;
looped = true;
}
}
#endif
#define UNUSED(n) \
JS_REQUIRES_STACK bool \
TraceRecorder::record_JSOP_UNUSED##n() { \
JS_NOT_REACHED("JSOP_UNUSED" # n); \
return false; \
}
|