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
|
/*
* Copyright (C) 2023 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "WasmIPIntGenerator.h"
#if ENABLE(WEBASSEMBLY)
#include "BytecodeGeneratorBaseInlines.h"
#include "BytecodeStructs.h"
#include "InstructionStream.h"
#include "JSCJSValueInlines.h"
#include "Label.h"
#include "WasmCallingConvention.h"
#include "WasmContext.h"
#include "WasmFunctionIPIntMetadataGenerator.h"
#include "WasmFunctionParser.h"
#include "WasmGeneratorTraits.h"
#include <variant>
#include <wtf/Assertions.h>
#include <wtf/CompletionHandler.h>
#include <wtf/RefPtr.h>
/*
* WebAssembly in-place interpreter metadata generator
*
* docs by Daniel Liu <daniel_liu4@apple.com / danlliu@umich.edu>; 2023 intern project
*
* 1. Why Metadata?
* ----------------
*
* WebAssembly's bytecode format isn't always the easiest to interpret by itself: jumps would require parsing
* through many bytes to find their target, constants are stored in LEB128, and a myriad of other reasons.
* For IPInt, we design metadata to act as "supporting information" for the interpreter, allowing it to quickly
* find important values such as constants, indices, and branch targets.
*
* FIXME: We should consider not aligning on Apple ARM64 cores since they don't typically have a penatly for unaligned loads/stores.
*
* 2. Metadata Structure
* ---------------------
*
* Metadata is kept in a vector of UInt8 (bytes). We handle metadata in "metadata entries", which are groups of
* 8 metadata bytes. We keep metadata aligned to 8B to improve access times. Sometimes, this results in higher
* memory overhead; however, these cases are relatively sparse. Each instruction pushes a certain number of
* entries to the metadata vector.
*
* 3. Metadata for Instructions
* ----------------------------
*
* block (0x02): 1 entry; 8B PC of next instruction
* loop (0x03): 1 entry; 8B PC of next instruction
* if (0x04): 2 entries; 4B new PC, 4B new MC for `else`, 8B new PC for `if`
* else (0x05): 1 entry; 4B new PC, 4B new MC for `end`
* end (0x0b): If exiting the function: ceil((# return values + 2) / 8) entries; 2B for total entry size, 1B / value returned
* br (0x0c): 2 entries; 4B new PC, 4B new MC, 2B number of values to pop, 2B arity, 4B PC after br
* br_if (0x0d): 2 entries; same as br
* br_table (0x0e): 1 + 2n entries for n branches: 8B number of targets; n br metadata entries
* local.get (0x20): 1 entry; 4B index of local, 4B size of instruction
* local.set (0x21): 1 entry; 4B index of local, 4B size of instruction
* local.tee (0x22): 2 entries because of how FunctionParser works
* global.get (0x23): 1 entry; 4B index of global, 4B size of instruction
* global.set (0x24): 1 entry; 4B index of global, 4B size of instruction
* table.get (0x23): 1 entry; 4B index of table, 4B size of instruction
* table.set (0x24): 1 entry; 4B index of table, 4B size of instruction
* mem load (0x28 - 0x35): 1 entry; 4B memarg, 4B size of instruction
* mem store (0x28 - 0x35): 1 entry; 4B memarg, 4B size of instruction
* i32.const (0x41): 1 entry; 4B value, 4B size of instruction
* i64.const (0x42): 2 entries; 8B value, 8B size of instruction
*
* i32, i64, f32, and f64 operations (besides the ones shown above) do not require metadata
*
*/
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN
namespace JSC { namespace Wasm {
using ErrorType = String;
using PartialResult = Expected<void, ErrorType>;
using UnexpectedResult = Unexpected<ErrorType>;
struct Value { };
// ControlBlock
struct IPIntLocation {
uint32_t pc;
uint32_t mc;
};
struct IPIntControlType {
friend class IPIntGenerator;
IPIntControlType()
{
}
IPIntControlType(BlockSignature signature, uint32_t stackSize, BlockType blockType, CatchKind catchKind = CatchKind::Catch)
: m_signature(signature)
, m_blockType(blockType)
, m_catchKind(catchKind)
, m_stackSize(stackSize)
{ }
static bool isIf(const IPIntControlType& control) { return control.blockType() == BlockType::If; }
static bool isTry(const IPIntControlType& control) { return control.blockType() == BlockType::Try; }
static bool isTryTable(const IPIntControlType& control) { return control.blockType() == BlockType::TryTable; }
static bool isAnyCatch(const IPIntControlType& control) { return control.blockType() == BlockType::Catch; }
static bool isTopLevel(const IPIntControlType& control) { return control.blockType() == BlockType::TopLevel; }
static bool isLoop(const IPIntControlType& control) { return control.blockType() == BlockType::Loop; }
static bool isBlock(const IPIntControlType& control) { return control.blockType() == BlockType::Block; }
static bool isCatch(const IPIntControlType& control)
{
if (control.blockType() != BlockType::Catch)
return false;
return control.catchKind() == CatchKind::Catch;
}
void dump(PrintStream&) const
{ }
BlockType blockType() const { return m_blockType; }
CatchKind catchKind() const { return m_catchKind; }
BlockSignature signature() const { return m_signature; }
unsigned stackSize() const { return m_stackSize; }
Type branchTargetType(unsigned i) const
{
ASSERT(i < branchTargetArity());
if (blockType() == BlockType::Loop)
return m_signature.m_signature->argumentType(i);
return m_signature.m_signature->returnType(i);
}
unsigned branchTargetArity() const
{
return isLoop(*this)
? m_signature.m_signature->argumentCount()
: m_signature.m_signature->returnCount();
}
private:
BlockSignature m_signature;
BlockType m_blockType;
CatchKind m_catchKind;
bool isElse { false };
int32_t m_pendingOffset { -1 };
uint32_t m_index { 0 };
uint32_t m_pc { 0 }; // where am i?
uint32_t m_mc { 0 };
uint32_t m_pcEnd { 0 };
uint32_t m_stackSize { 0 };
uint32_t m_tryDepth { 0 };
Vector<IPIntLocation> m_catchesAwaitingFixup;
struct TryTableTarget {
CatchKind type;
uint32_t tag;
const TypeDefinition* exceptionSignature;
ControlRef target;
};
Vector<TryTableTarget> m_tryTableTargets;
};
class IPIntGenerator {
public:
IPIntGenerator(ModuleInformation&, FunctionCodeIndex, const TypeDefinition&, std::span<const uint8_t>);
static constexpr bool shouldFuseBranchCompare = false;
using ControlType = IPIntControlType;
using ExpressionType = Value;
using CallType = CallLinkInfo::CallType;
using ResultList = Vector<Value, 8>;
using ArgumentList = Vector<Value, 8>;
using ExpressionList = Vector<Value, 1>;
using ControlEntry = FunctionParser<IPIntGenerator>::ControlEntry;
using ControlStack = FunctionParser<IPIntGenerator>::ControlStack;
using Stack = FunctionParser<IPIntGenerator>::Stack;
using TypedExpression = FunctionParser<IPIntGenerator>::TypedExpression;
using CatchHandler = FunctionParser<IPIntGenerator>::CatchHandler;
static ExpressionType emptyExpression() { return { }; };
PartialResult WARN_UNUSED_RETURN addDrop(ExpressionType);
template <typename ...Args>
NEVER_INLINE UnexpectedResult WARN_UNUSED_RETURN fail(Args... args) const
{
using namespace FailureHelper; // See ADL comment in WasmParser.h.
return UnexpectedResult(makeString("WebAssembly.Module failed compiling: "_s, makeString(args)...));
}
#define WASM_COMPILE_FAIL_IF(condition, ...) do { \
if (UNLIKELY(condition)) \
return fail(__VA_ARGS__); \
} while (0)
std::unique_ptr<FunctionIPIntMetadataGenerator> finalize();
PartialResult WARN_UNUSED_RETURN addArguments(const TypeDefinition&);
PartialResult WARN_UNUSED_RETURN addLocal(Type, uint32_t);
Value addConstant(Type, uint64_t);
// SIMD
bool usesSIMD() { return m_usesSIMD; }
void notifyFunctionUsesSIMD() { ASSERT(Options::useWasmSIMD()); m_usesSIMD = true; }
PartialResult WARN_UNUSED_RETURN addSIMDLoad(ExpressionType, uint32_t, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addSIMDStore(ExpressionType, ExpressionType, uint32_t);
PartialResult WARN_UNUSED_RETURN addSIMDSplat(SIMDLane, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addSIMDShuffle(v128_t, ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addSIMDShift(SIMDLaneOperation, SIMDInfo, ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addSIMDExtmul(SIMDLaneOperation, SIMDInfo, ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addSIMDLoadSplat(SIMDLaneOperation, ExpressionType, uint32_t, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addSIMDLoadLane(SIMDLaneOperation, ExpressionType, ExpressionType, uint32_t, uint8_t, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addSIMDStoreLane(SIMDLaneOperation, ExpressionType, ExpressionType, uint32_t, uint8_t);
PartialResult WARN_UNUSED_RETURN addSIMDLoadExtend(SIMDLaneOperation, ExpressionType, uint32_t, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addSIMDLoadPad(SIMDLaneOperation, ExpressionType, uint32_t, ExpressionType&);
ExpressionType addConstant(v128_t);
// SIMD generated
PartialResult WARN_UNUSED_RETURN addExtractLane(SIMDInfo, uint8_t, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addReplaceLane(SIMDInfo, uint8_t, ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addSIMDI_V(SIMDLaneOperation, SIMDInfo, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addSIMDV_V(SIMDLaneOperation, SIMDInfo, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addSIMDBitwiseSelect(ExpressionType, ExpressionType, ExpressionType, ExpressionType&);
#if ENABLE(B3_JIT)
PartialResult WARN_UNUSED_RETURN addSIMDRelOp(SIMDLaneOperation, SIMDInfo, ExpressionType, ExpressionType, B3::Air::Arg, ExpressionType&);
#endif
PartialResult WARN_UNUSED_RETURN addSIMDV_VV(SIMDLaneOperation, SIMDInfo, ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addSIMDRelaxedFMA(SIMDLaneOperation, SIMDInfo, ExpressionType, ExpressionType, ExpressionType, ExpressionType&);
// References
PartialResult WARN_UNUSED_RETURN addRefIsNull(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addRefFunc(FunctionSpaceIndex, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addRefAsNonNull(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addRefEq(ExpressionType, ExpressionType, ExpressionType&);
// Tables
PartialResult WARN_UNUSED_RETURN addTableGet(unsigned, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addTableSet(unsigned, ExpressionType, ExpressionType);
PartialResult WARN_UNUSED_RETURN addTableInit(unsigned, unsigned, ExpressionType, ExpressionType, ExpressionType);
PartialResult WARN_UNUSED_RETURN addElemDrop(unsigned);
PartialResult WARN_UNUSED_RETURN addTableSize(unsigned, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addTableGrow(unsigned, ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addTableFill(unsigned, ExpressionType, ExpressionType, ExpressionType);
PartialResult WARN_UNUSED_RETURN addTableCopy(unsigned, unsigned, ExpressionType, ExpressionType, ExpressionType);
// Locals
PartialResult WARN_UNUSED_RETURN getLocal(uint32_t index, ExpressionType&);
PartialResult WARN_UNUSED_RETURN setLocal(uint32_t, ExpressionType);
PartialResult WARN_UNUSED_RETURN teeLocal(uint32_t, ExpressionType, ExpressionType& result);
// Globals
PartialResult WARN_UNUSED_RETURN getGlobal(uint32_t, ExpressionType&);
PartialResult WARN_UNUSED_RETURN setGlobal(uint32_t, ExpressionType);
// Memory
PartialResult WARN_UNUSED_RETURN load(LoadOpType, ExpressionType, ExpressionType&, uint32_t);
PartialResult WARN_UNUSED_RETURN store(StoreOpType, ExpressionType, ExpressionType, uint32_t);
PartialResult WARN_UNUSED_RETURN addGrowMemory(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addCurrentMemory(ExpressionType&);
PartialResult WARN_UNUSED_RETURN addMemoryFill(ExpressionType, ExpressionType, ExpressionType);
PartialResult WARN_UNUSED_RETURN addMemoryCopy(ExpressionType, ExpressionType, ExpressionType);
PartialResult WARN_UNUSED_RETURN addMemoryInit(unsigned, ExpressionType, ExpressionType, ExpressionType);
PartialResult WARN_UNUSED_RETURN addDataDrop(unsigned);
// Atomics
PartialResult WARN_UNUSED_RETURN atomicLoad(ExtAtomicOpType, Type, ExpressionType, ExpressionType&, uint32_t);
PartialResult WARN_UNUSED_RETURN atomicStore(ExtAtomicOpType, Type, ExpressionType, ExpressionType, uint32_t);
PartialResult WARN_UNUSED_RETURN atomicBinaryRMW(ExtAtomicOpType, Type, ExpressionType, ExpressionType, ExpressionType&, uint32_t);
PartialResult WARN_UNUSED_RETURN atomicCompareExchange(ExtAtomicOpType, Type, ExpressionType, ExpressionType, ExpressionType, ExpressionType&, uint32_t);
PartialResult WARN_UNUSED_RETURN atomicWait(ExtAtomicOpType, ExpressionType, ExpressionType, ExpressionType, ExpressionType&, uint32_t);
PartialResult WARN_UNUSED_RETURN atomicNotify(ExtAtomicOpType, ExpressionType, ExpressionType, ExpressionType&, uint32_t);
PartialResult WARN_UNUSED_RETURN atomicFence(ExtAtomicOpType, uint8_t);
// Saturated truncation
PartialResult WARN_UNUSED_RETURN truncSaturated(Ext1OpType, ExpressionType, ExpressionType&, Type, Type);
// GC
PartialResult WARN_UNUSED_RETURN addRefI31(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI31GetS(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI31GetU(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addArrayNew(uint32_t, ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addArrayNewDefault(uint32_t, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addArrayNewData(uint32_t, uint32_t, ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addArrayNewElem(uint32_t, uint32_t, ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addArrayNewFixed(uint32_t, ArgumentList&, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addArrayGet(ExtGCOpType, uint32_t, ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addArraySet(uint32_t, ExpressionType, ExpressionType, ExpressionType);
PartialResult WARN_UNUSED_RETURN addArrayLen(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addArrayFill(uint32_t, ExpressionType, ExpressionType, ExpressionType, ExpressionType);
PartialResult WARN_UNUSED_RETURN addArrayCopy(uint32_t, ExpressionType, ExpressionType, uint32_t, ExpressionType, ExpressionType, ExpressionType);
PartialResult WARN_UNUSED_RETURN addArrayInitElem(uint32_t, ExpressionType, ExpressionType, uint32_t, ExpressionType, ExpressionType);
PartialResult WARN_UNUSED_RETURN addArrayInitData(uint32_t, ExpressionType, ExpressionType, uint32_t, ExpressionType, ExpressionType);
PartialResult WARN_UNUSED_RETURN addStructNew(uint32_t, ArgumentList&, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addStructNewDefault(uint32_t, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addStructGet(ExtGCOpType, ExpressionType, const StructType&, uint32_t, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addStructSet(ExpressionType, const StructType&, uint32_t, ExpressionType);
PartialResult WARN_UNUSED_RETURN addRefTest(ExpressionType, bool, int32_t, bool, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addRefCast(ExpressionType, bool, int32_t, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addAnyConvertExtern(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addExternConvertAny(ExpressionType, ExpressionType&);
// Basic operators
PartialResult WARN_UNUSED_RETURN addI32DivS(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI32RemS(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI32DivU(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI32RemU(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI64DivS(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI64RemS(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI64DivU(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI64RemU(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI32Ctz(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI32Popcnt(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI64Popcnt(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF64Nearest(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF32Nearest(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF64Trunc(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF32Trunc(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI32TruncSF64(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI32TruncSF32(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI32TruncUF64(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI32TruncUF32(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI64TruncSF64(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI64TruncSF32(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI64TruncUF64(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI64TruncUF32(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF32Ceil(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI32Mul(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI32Sub(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF64Le(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF32DemoteF64(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF64Ne(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF64Lt(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF32Min(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF32Max(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF64Min(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF64Max(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF64Mul(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF32Div(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI32Clz(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF32Copysign(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF32ReinterpretI32(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF32Ne(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF64Gt(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF32Sqrt(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF64Ge(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI64GtS(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI64GtU(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF64Div(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF32Add(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI32LeU(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI32LeS(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI64Ne(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI64Clz(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF32Neg(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI32And(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI32LtU(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI64Rotr(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF64Abs(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI32LtS(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI32Eq(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF64Copysign(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF32ConvertSI64(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI64Rotl(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF32Lt(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF64ConvertSI32(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF64Eq(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF32Le(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF32Ge(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI32ShrU(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF32ConvertUI32(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI32ShrS(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI32GeU(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF64Ceil(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI32GeS(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI32Shl(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF64Floor(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI32Xor(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF32Abs(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF32Mul(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI64Sub(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI32ReinterpretF32(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI32Add(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF64Sub(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI32Or(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI64LtU(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI64LtS(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF64ConvertSI64(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI64Xor(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI64GeU(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI64Mul(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF32Sub(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF64PromoteF32(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF64Add(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI64GeS(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI64ExtendUI32(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI32Ne(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF64ReinterpretI64(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF32Eq(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI64Eq(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF32Floor(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF32ConvertSI32(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI64And(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI64Or(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI64Ctz(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI32Eqz(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI64Eqz(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI64ReinterpretF64(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF64ConvertUI32(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF32ConvertUI64(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF64ConvertUI64(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI64ShrS(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI64ShrU(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF64Sqrt(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI64Shl(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF32Gt(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI32WrapI64(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI32Rotl(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI32Rotr(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI32GtU(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI64ExtendSI32(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI32Extend8S(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI32Extend16S(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI64Extend8S(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI64Extend16S(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI64Extend32S(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI32GtS(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addF64Neg(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI64LeU(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI64LeS(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addI64Add(ExpressionType, ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addSelect(ExpressionType, ExpressionType, ExpressionType, ExpressionType&);
// Control flow
ControlType WARN_UNUSED_RETURN addTopLevel(BlockSignature);
PartialResult WARN_UNUSED_RETURN addBlock(BlockSignature, Stack&, ControlType&, Stack&);
PartialResult WARN_UNUSED_RETURN addLoop(BlockSignature, Stack&, ControlType&, Stack&, uint32_t);
PartialResult WARN_UNUSED_RETURN addIf(ExpressionType, BlockSignature, Stack&, ControlType&, Stack&);
PartialResult WARN_UNUSED_RETURN addElse(ControlType&, Stack&);
PartialResult WARN_UNUSED_RETURN addElseToUnreachable(ControlType&);
PartialResult WARN_UNUSED_RETURN addTry(BlockSignature, Stack&, ControlType&, Stack&);
PartialResult WARN_UNUSED_RETURN addTryTable(BlockSignature, Stack& enclosingStack, const Vector<CatchHandler>& targets, ControlType& result, Stack& newStack);
PartialResult WARN_UNUSED_RETURN addCatch(unsigned, const TypeDefinition&, Stack&, ControlType&, ResultList&);
PartialResult WARN_UNUSED_RETURN addCatchToUnreachable(unsigned, const TypeDefinition&, ControlType&, ResultList&);
PartialResult WARN_UNUSED_RETURN addCatchAll(Stack&, ControlType&);
PartialResult WARN_UNUSED_RETURN addCatchAllToUnreachable(ControlType&);
PartialResult WARN_UNUSED_RETURN addDelegate(ControlType&, ControlType&);
PartialResult WARN_UNUSED_RETURN addDelegateToUnreachable(ControlType&, ControlType&);
PartialResult WARN_UNUSED_RETURN addThrow(unsigned, ArgumentList&, Stack&);
PartialResult WARN_UNUSED_RETURN addRethrow(unsigned, ControlType&);
PartialResult WARN_UNUSED_RETURN addThrowRef(ExpressionType, Stack&);
PartialResult WARN_UNUSED_RETURN addReturn(const ControlType&, const Stack&);
PartialResult WARN_UNUSED_RETURN addBranch(ControlType&, ExpressionType, const Stack&);
PartialResult WARN_UNUSED_RETURN addBranchNull(ControlType&, ExpressionType, Stack&, bool, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addBranchCast(ControlType&, ExpressionType, Stack&, bool, int32_t, bool);
PartialResult WARN_UNUSED_RETURN addSwitch(ExpressionType, const Vector<ControlType*>&, ControlType&, const Stack&);
PartialResult WARN_UNUSED_RETURN endBlock(ControlEntry&, Stack&);
void endTryTable(ControlType& data);
PartialResult WARN_UNUSED_RETURN addEndToUnreachable(ControlEntry&, Stack&);
PartialResult WARN_UNUSED_RETURN endTopLevel(BlockSignature, const Stack&);
// Fused comparison stubs (TODO: make use of these for better codegen)
PartialResult WARN_UNUSED_RETURN addFusedBranchCompare(OpType, ControlType&, ExpressionType, const Stack&) { RELEASE_ASSERT_NOT_REACHED(); }
PartialResult WARN_UNUSED_RETURN addFusedBranchCompare(OpType, ControlType&, ExpressionType, ExpressionType, const Stack&) { RELEASE_ASSERT_NOT_REACHED(); }
PartialResult WARN_UNUSED_RETURN addFusedIfCompare(OpType, ExpressionType, BlockSignature, Stack&, ControlType&, Stack&) { RELEASE_ASSERT_NOT_REACHED(); }
PartialResult WARN_UNUSED_RETURN addFusedIfCompare(OpType, ExpressionType, ExpressionType, BlockSignature, Stack&, ControlType&, Stack&) { RELEASE_ASSERT_NOT_REACHED(); }
// Calls
PartialResult WARN_UNUSED_RETURN addCall(FunctionSpaceIndex, const TypeDefinition&, ArgumentList&, ResultList&, CallType = CallType::Call);
PartialResult WARN_UNUSED_RETURN addCallIndirect(unsigned, const TypeDefinition&, ArgumentList&, ResultList&, CallType = CallType::Call);
PartialResult WARN_UNUSED_RETURN addCallRef(const TypeDefinition&, ArgumentList&, ResultList&, CallType = CallType::Call);
PartialResult WARN_UNUSED_RETURN addUnreachable();
PartialResult WARN_UNUSED_RETURN addCrash();
inline void assertAboutStackSize(bool condition)
{
// There's a few cases that we only want to assert our stack contents if SIMD isn't enabled.
// Since IPInt doesn't support SIMD, we don't update the stack size correctly, but this is
// not an issue because the code never gets run.
ASSERT_UNUSED(condition, m_usesSIMD || condition);
}
void setParser(FunctionParser<IPIntGenerator>* parser) { m_parser = parser; };
size_t getCurrentInstructionLength()
{
return m_parser->offset() - m_parser->currentOpcodeStartingOffset();
}
void addCallCommonData(const FunctionSignature&, const CallInformation&);
void addTailCallCommonData(const FunctionSignature&);
void didFinishParsingLocals()
{
m_metadata->m_bytecodeOffset = m_parser->offset();
}
void didPopValueFromStack(ExpressionType, ASCIILiteral) { }
void willParseOpcode() { }
void willParseExtendedOpcode() { }
void didParseOpcode()
{
if (!m_parser->unreachableBlocks())
assertAboutStackSize(m_parser->getStackHeightInValues() == m_stackSize.value());
}
void dump(const ControlStack&, const Stack*);
void convertTryToCatch(ControlType& tryBlock, CatchKind);
ALWAYS_INLINE void changeStackSize(int delta)
{
m_stackSize += delta;
if (delta > 0)
m_maxStackSize = std::max(m_maxStackSize, m_stackSize.value());
}
void coalesceControlFlow(bool force = false);
void resolveEntryTarget(unsigned, IPIntLocation);
void resolveExitTarget(unsigned, IPIntLocation);
void tryToResolveEntryTarget(uint32_t index, IPIntLocation loc, uint8_t*)
{
m_controlStructuresAwaitingCoalescing[index].m_awaitingEntryTarget.append(loc);
}
void tryToResolveExitTarget(uint32_t index, IPIntLocation loc, uint8_t*)
{
m_controlStructuresAwaitingCoalescing[index].m_awaitingExitTarget.append(loc);
}
void tryToResolveBranchTarget(ControlType& targetBlock, IPIntLocation loc, uint8_t* metadata)
{
if (ControlType::isTopLevel(targetBlock)) {
m_jumpLocationsAwaitingEnd.append(loc);
return;
}
auto index = targetBlock.m_index;
auto& target = m_controlStructuresAwaitingCoalescing[index];
if (target.isLoop) {
ASSERT(target.m_entryResolved);
IPInt::BlockMetadata md = { static_cast<int32_t>(target.m_entryTarget.pc - loc.pc), static_cast<int32_t>(target.m_entryTarget.mc - loc.mc) };
WRITE_TO_METADATA(metadata + loc.mc, md, IPInt::BlockMetadata);
} else {
ASSERT(!target.m_exitResolved);
target.m_awaitingBranchTarget.append(loc);
}
}
ALWAYS_INLINE const CallInformation& cachedCallInformationFor(const FunctionSignature& signature)
{
if (m_cachedSignature != &signature) {
m_cachedSignature = &signature;
m_cachedCallBytecode.shrink(0);
m_cachedCallInformation = wasmCallingConvention().callInformationFor(signature, CallRole::Caller);
}
return m_cachedCallInformation;
}
static constexpr bool tierSupportsSIMD = false;
static constexpr bool validateFunctionBodySize = true;
private:
Checked<uint32_t> m_stackSize { 0 };
uint32_t m_maxStackSize { 0 };
Checked<uint32_t> m_tryDepth { 0 };
uint32_t m_maxTryDepth { 0 };
FunctionParser<IPIntGenerator>* m_parser { nullptr };
ModuleInformation& m_info;
const FunctionCodeIndex m_functionIndex;
std::unique_ptr<FunctionIPIntMetadataGenerator> m_metadata;
struct ControlStructureAwaitingCoalescing {
Vector<IPIntLocation, 16> m_awaitingEntryTarget { };
Vector<IPIntLocation, 16> m_awaitingBranchTarget { };
Vector<IPIntLocation, 16> m_awaitingExitTarget { };
IPIntLocation m_entryTarget { 0, 0 }; // where do we go when entering normally?
IPIntLocation m_exitTarget { 0, 0 }; // where do we go when leaving?
uint32_t startPC { 0 };
bool isLoop { false };
bool m_entryResolved { false };
bool m_exitResolved { false };
};
Vector<ControlStructureAwaitingCoalescing, 16> m_controlStructuresAwaitingCoalescing;
struct QueuedCoalesceRequest {
size_t index;
bool isEntry;
};
Vector<QueuedCoalesceRequest, 16> m_coalesceQueue;
// if this is 0, all our control structures have been coalesced and we can clean up the vector
unsigned m_coalesceDebt { 0 };
// exit loations can still be unresolved when the ControlType* dies, so we put them here
Vector<IPIntLocation> m_exitHandlersAwaitingCoalescing;
// all jumps that go to the top level and return
Vector<IPIntLocation> m_jumpLocationsAwaitingEnd;
inline uint32_t curPC() { return m_parser->currentOpcodeStartingOffset() - m_metadata->m_bytecodeOffset; }
inline uint32_t nextPC() { return m_parser->offset() - m_metadata->m_bytecodeOffset; }
inline uint32_t curMC() { return m_metadata->m_metadata.size(); }
// FIXME: If rethrow is not used in practice we should consider just reparsing the function to update the SP offsets.
Vector<uint32_t> m_catchSPMetadataOffsets;
CallInformation m_cachedCallInformation { };
const FunctionSignature* m_cachedSignature { nullptr };
Vector<uint8_t, 16> m_cachedCallBytecode;
bool m_usesRethrow { false };
bool m_usesSIMD { false };
};
// use if (true) to avoid warnings.
#define IPINT_UNIMPLEMENTED { if (true) { CRASH(); } return { }; }
IPIntGenerator::IPIntGenerator(ModuleInformation& info, FunctionCodeIndex functionIndex, const TypeDefinition&, std::span<const uint8_t> bytecode)
: m_info(info)
, m_functionIndex(functionIndex)
, m_metadata(WTF::makeUnique<FunctionIPIntMetadataGenerator>(functionIndex, bytecode))
{
m_metadata->m_callees = FixedBitVector(m_info.internalFunctionCount());
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addDrop(ExpressionType)
{
changeStackSize(-1);
return { };
}
Value IPIntGenerator::addConstant(Type type, uint64_t value)
{
changeStackSize(1);
m_metadata->addLEB128ConstantAndLengthForType(type, value, getCurrentInstructionLength());
return { };
}
// SIMD
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addSIMDLoad(ExpressionType, uint32_t, ExpressionType&) IPINT_UNIMPLEMENTED
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addSIMDStore(ExpressionType, ExpressionType, uint32_t) IPINT_UNIMPLEMENTED
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addSIMDSplat(SIMDLane, ExpressionType, ExpressionType&) IPINT_UNIMPLEMENTED
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addSIMDShuffle(v128_t, ExpressionType, ExpressionType, ExpressionType&) IPINT_UNIMPLEMENTED
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addSIMDShift(SIMDLaneOperation, SIMDInfo, ExpressionType, ExpressionType, ExpressionType&) IPINT_UNIMPLEMENTED
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addSIMDExtmul(SIMDLaneOperation, SIMDInfo, ExpressionType, ExpressionType, ExpressionType&) IPINT_UNIMPLEMENTED
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addSIMDLoadSplat(SIMDLaneOperation, ExpressionType, uint32_t, ExpressionType&) IPINT_UNIMPLEMENTED
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addSIMDLoadLane(SIMDLaneOperation, ExpressionType, ExpressionType, uint32_t, uint8_t, ExpressionType&) IPINT_UNIMPLEMENTED
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addSIMDStoreLane(SIMDLaneOperation, ExpressionType, ExpressionType, uint32_t, uint8_t) IPINT_UNIMPLEMENTED
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addSIMDLoadExtend(SIMDLaneOperation, ExpressionType, uint32_t, ExpressionType&) IPINT_UNIMPLEMENTED
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addSIMDLoadPad(SIMDLaneOperation, ExpressionType, uint32_t, ExpressionType&) IPINT_UNIMPLEMENTED
IPIntGenerator::ExpressionType IPIntGenerator::addConstant(v128_t)
{
changeStackSize(1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addExtractLane(SIMDInfo, uint8_t, ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addReplaceLane(SIMDInfo, uint8_t, ExpressionType, ExpressionType, ExpressionType&) IPINT_UNIMPLEMENTED
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addSIMDI_V(SIMDLaneOperation, SIMDInfo, ExpressionType, ExpressionType&) IPINT_UNIMPLEMENTED
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addSIMDV_V(SIMDLaneOperation, SIMDInfo, ExpressionType, ExpressionType&) IPINT_UNIMPLEMENTED
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addSIMDBitwiseSelect(ExpressionType, ExpressionType, ExpressionType, ExpressionType&) IPINT_UNIMPLEMENTED
#if ENABLE(B3_JIT)
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addSIMDRelOp(SIMDLaneOperation, SIMDInfo, ExpressionType, ExpressionType, B3::Air::Arg, ExpressionType&) IPINT_UNIMPLEMENTED
#endif
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addSIMDV_VV(SIMDLaneOperation, SIMDInfo, ExpressionType, ExpressionType, ExpressionType&) IPINT_UNIMPLEMENTED
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addSIMDRelaxedFMA(SIMDLaneOperation, SIMDInfo, ExpressionType, ExpressionType, ExpressionType, ExpressionType&) IPINT_UNIMPLEMENTED
// References
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addRefIsNull(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addRefFunc(FunctionSpaceIndex index, ExpressionType&)
{
changeStackSize(1);
m_metadata->addLEB128ConstantInt32AndLength(index, getCurrentInstructionLength());
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addRefAsNonNull(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addRefEq(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
// Tables
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addTableGet(unsigned index, ExpressionType, ExpressionType&)
{
m_metadata->addLEB128ConstantInt32AndLength(index, getCurrentInstructionLength());
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addTableSet(unsigned index, ExpressionType, ExpressionType)
{
changeStackSize(-2);
m_metadata->addLEB128ConstantInt32AndLength(index, getCurrentInstructionLength());
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addTableInit(unsigned elementIndex, unsigned tableIndex, ExpressionType, ExpressionType, ExpressionType)
{
changeStackSize(-3);
IPInt::TableInitMetadata table {
.elementIndex = safeCast<uint32_t>(elementIndex),
.tableIndex = safeCast<uint32_t>(tableIndex),
.instructionLength = { .length = safeCast<uint8_t>(getCurrentInstructionLength()) }
};
m_metadata->appendMetadata(table);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addElemDrop(unsigned elementIndex)
{
m_metadata->addLEB128ConstantInt32AndLength(elementIndex, getCurrentInstructionLength());
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addTableSize(unsigned tableIndex, ExpressionType&)
{
changeStackSize(1);
m_metadata->addLEB128ConstantInt32AndLength(tableIndex, getCurrentInstructionLength());
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addTableGrow(unsigned tableIndex, ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
IPInt::TableGrowMetadata table {
.tableIndex = safeCast<uint32_t>(tableIndex),
.instructionLength = { .length = safeCast<uint8_t>(getCurrentInstructionLength()) }
};
m_metadata->appendMetadata(table);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addTableFill(unsigned tableIndex, ExpressionType, ExpressionType, ExpressionType)
{
changeStackSize(-3);
IPInt::TableFillMetadata table {
.tableIndex = safeCast<uint32_t>(tableIndex),
.instructionLength = { .length = safeCast<uint8_t>(getCurrentInstructionLength()) }
};
m_metadata->appendMetadata(table);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addTableCopy(unsigned dstTableIndex, unsigned srcTableIndex, ExpressionType, ExpressionType, ExpressionType)
{
changeStackSize(-3);
IPInt::TableCopyMetadata table {
.dstTableIndex = safeCast<uint32_t>(dstTableIndex),
.srcTableIndex = safeCast<uint32_t>(srcTableIndex),
.instructionLength = { .length = safeCast<uint8_t>(getCurrentInstructionLength()) }
};
m_metadata->appendMetadata(table);
return { };
}
// Locals and Globals
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addArguments(const TypeDefinition &signature)
{
auto sig = signature.as<FunctionSignature>();
CallInformation callCC = wasmCallingConvention().callInformationFor(*sig, CallRole::Callee);
auto numArgs = sig->argumentCount();
m_metadata->m_numLocals += numArgs;
m_metadata->m_numArguments = numArgs;
m_metadata->m_argumINTBytecode.reserveInitialCapacity(sig->argumentCount() + 1);
constexpr static int NUM_ARGUMINT_GPRS = 8;
constexpr static int NUM_ARGUMINT_FPRS = 8;
ASSERT_UNUSED(NUM_ARGUMINT_GPRS, wasmCallingConvention().jsrArgs.size() <= NUM_ARGUMINT_GPRS);
ASSERT_UNUSED(NUM_ARGUMINT_FPRS, wasmCallingConvention().fprArgs.size() <= NUM_ARGUMINT_FPRS);
// 0x00 - 0x07: GPR 0-7
// 0x08 - 0x0f: FPR 0-3
// 0x10: stack
// 0x11: end
for (size_t i = 0; i < numArgs; ++i) {
auto loc = callCC.params[i].location;
if (loc.isGPR()) {
#if USE(JSVALUE64)
ASSERT_UNUSED(NUM_ARGUMINT_GPRS, GPRInfo::toArgumentIndex(loc.jsr().gpr()) < NUM_ARGUMINT_GPRS);
m_metadata->m_argumINTBytecode.append(static_cast<uint8_t>(IPInt::ArgumINTBytecode::ArgGPR) + GPRInfo::toArgumentIndex(loc.jsr().gpr()));
#elif USE(JSVALUE32_64)
ASSERT_UNUSED(NUM_ARGUMINT_GPRS, GPRInfo::toArgumentIndex(loc.jsr().payloadGPR()) < NUM_ARGUMINT_GPRS);
ASSERT_UNUSED(NUM_ARGUMINT_GPRS, GPRInfo::toArgumentIndex(loc.jsr().tagGPR()) < NUM_ARGUMINT_GPRS);
m_metadata->m_argumINTBytecode.append(static_cast<uint8_t>(IPInt::ArgumINTBytecode::ArgGPR) + GPRInfo::toArgumentIndex(loc.jsr().gpr(WhichValueWord::PayloadWord)));
#endif
} else if (loc.isFPR()) {
ASSERT_UNUSED(NUM_ARGUMINT_FPRS, FPRInfo::toArgumentIndex(loc.fpr()) < NUM_ARGUMINT_FPRS);
m_metadata->m_argumINTBytecode.append(static_cast<uint8_t>(IPInt::ArgumINTBytecode::RegFPR) + FPRInfo::toArgumentIndex(loc.fpr()));
} else if (loc.isStack()) {
m_metadata->m_argumINTBytecode.append(static_cast<uint8_t>(IPInt::ArgumINTBytecode::Stack));
}
}
m_metadata->m_argumINTBytecode.append(static_cast<uint8_t>(IPInt::ArgumINTBytecode::End));
m_metadata->addReturnData(*sig, callCC);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addLocal(Type localType, uint32_t count)
{
// push 0x00 or 0xff (for bit hacks) to the metadata depending on if we have a primitive or a reference
if (isRefType(localType)) {
for (unsigned i = 0; i < count; ++i)
m_metadata->m_argumINTBytecode.append(0xff);
} else {
for (unsigned i = 0; i < count; ++i)
m_metadata->m_argumINTBytecode.append(0);
}
m_metadata->m_numLocals += count;
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::getLocal(uint32_t, ExpressionType&)
{
// Local indices are usually very small, so we decode them on the fly
// instead of generating metadata.
changeStackSize(1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::setLocal(uint32_t, ExpressionType)
{
// Local indices are usually very small, so we decode them on the fly
// instead of generating metadata.
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::teeLocal(uint32_t, ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::getGlobal(uint32_t index, ExpressionType&)
{
changeStackSize(1);
const Wasm::GlobalInformation& global = m_info.globals[index];
IPInt::GlobalMetadata mdGlobal {
.index = index,
.instructionLength = { .length = safeCast<uint8_t>(getCurrentInstructionLength()) },
.bindingMode = safeCast<uint8_t>(global.bindingMode),
.isRef = safeCast<uint8_t>(isRefType(m_info.globals[index].type))
};
m_metadata->appendMetadata(mdGlobal);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::setGlobal(uint32_t index, ExpressionType)
{
changeStackSize(-1);
const Wasm::GlobalInformation& global = m_info.globals[index];
IPInt::GlobalMetadata mdGlobal {
.index = index,
.instructionLength = { .length = safeCast<uint8_t>(getCurrentInstructionLength()) },
.bindingMode = safeCast<uint8_t>(global.bindingMode),
.isRef = safeCast<uint8_t>(isRefType(m_info.globals[index].type))
};
m_metadata->appendMetadata(mdGlobal);
return { };
}
// Loads and Stores
PartialResult WARN_UNUSED_RETURN IPIntGenerator::load(LoadOpType, ExpressionType, ExpressionType&, uint32_t offset)
{
m_metadata->addLEB128ConstantInt32AndLength(offset, getCurrentInstructionLength());
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::store(StoreOpType, ExpressionType, ExpressionType, uint32_t offset)
{
changeStackSize(-2);
m_metadata->addLEB128ConstantInt32AndLength(offset, getCurrentInstructionLength());
return { };
}
// Memories
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addGrowMemory(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addCurrentMemory(ExpressionType&)
{
changeStackSize(1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addMemoryFill(ExpressionType, ExpressionType, ExpressionType)
{
changeStackSize(-3);
m_metadata->addLength(getCurrentInstructionLength());
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addMemoryCopy(ExpressionType, ExpressionType, ExpressionType)
{
changeStackSize(-3);
m_metadata->addLength(getCurrentInstructionLength());
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addMemoryInit(unsigned dataIndex, ExpressionType, ExpressionType, ExpressionType)
{
changeStackSize(-3);
m_metadata->addLEB128ConstantInt32AndLength(dataIndex, getCurrentInstructionLength());
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addDataDrop(unsigned dataIndex)
{
m_metadata->addLEB128ConstantInt32AndLength(dataIndex, getCurrentInstructionLength());
return { };
}
// Atomics
PartialResult WARN_UNUSED_RETURN IPIntGenerator::atomicLoad(ExtAtomicOpType, Type, ExpressionType, ExpressionType&, uint32_t offset)
{
m_metadata->addLEB128ConstantInt32AndLength(offset, getCurrentInstructionLength());
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::atomicStore(ExtAtomicOpType, Type, ExpressionType, ExpressionType, uint32_t offset)
{
changeStackSize(-2);
m_metadata->addLEB128ConstantInt32AndLength(offset, getCurrentInstructionLength());
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::atomicBinaryRMW(ExtAtomicOpType, Type, ExpressionType, ExpressionType, ExpressionType&, uint32_t offset)
{
changeStackSize(-1);
m_metadata->addLEB128ConstantInt32AndLength(offset, getCurrentInstructionLength());
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::atomicCompareExchange(ExtAtomicOpType, Type, ExpressionType, ExpressionType, ExpressionType, ExpressionType&, uint32_t offset)
{
changeStackSize(-2);
m_metadata->addLEB128ConstantInt32AndLength(offset, getCurrentInstructionLength());
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::atomicWait(ExtAtomicOpType, ExpressionType, ExpressionType, ExpressionType, ExpressionType&, uint32_t offset)
{
changeStackSize(-2);
m_metadata->addLEB128ConstantInt32AndLength(offset, getCurrentInstructionLength());
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::atomicNotify(ExtAtomicOpType, ExpressionType, ExpressionType, ExpressionType&, uint32_t offset)
{
changeStackSize(-1);
m_metadata->addLEB128ConstantInt32AndLength(offset, getCurrentInstructionLength());
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::atomicFence(ExtAtomicOpType, uint8_t)
{
m_metadata->addLength(getCurrentInstructionLength());
return { };
}
// GC
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addRefI31(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI31GetS(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI31GetU(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addArrayNew(uint32_t index, ExpressionType, ExpressionType, ExpressionType&)
{
m_metadata->appendMetadata<IPInt::ArrayNewMetadata>({
static_cast<Wasm::TypeIndex>(index),
static_cast<uint8_t>(getCurrentInstructionLength())
});
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addArrayNewData(uint32_t index, uint32_t dataSegmentIndex, ExpressionType, ExpressionType, ExpressionType&)
{
m_metadata->appendMetadata<IPInt::ArrayNewDataMetadata>({
static_cast<Wasm::TypeIndex>(index),
dataSegmentIndex,
static_cast<uint8_t>(getCurrentInstructionLength())
});
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addArrayNewElem(uint32_t index, uint32_t elemSegmentIndex, ExpressionType, ExpressionType, ExpressionType&)
{
m_metadata->appendMetadata<IPInt::ArrayNewElemMetadata>({
static_cast<Wasm::TypeIndex>(index),
elemSegmentIndex,
static_cast<uint8_t>(getCurrentInstructionLength())
});
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addArrayNewFixed(uint32_t index, ArgumentList& args, ExpressionType&)
{
m_metadata->appendMetadata<IPInt::ArrayNewFixedMetadata>({
static_cast<Wasm::TypeIndex>(index),
static_cast<uint32_t>(args.size()),
static_cast<uint8_t>(getCurrentInstructionLength())
});
changeStackSize(-args.size() + 1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addArrayNewDefault(uint32_t index, ExpressionType, ExpressionType&)
{
m_metadata->appendMetadata<IPInt::ArrayNewMetadata>({
static_cast<Wasm::TypeIndex>(index),
static_cast<uint8_t>(getCurrentInstructionLength())
});
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addArrayGet(ExtGCOpType, uint32_t index, ExpressionType, ExpressionType, ExpressionType&)
{
m_metadata->appendMetadata<IPInt::ArrayGetSetMetadata>({
static_cast<Wasm::TypeIndex>(index),
static_cast<uint8_t>(getCurrentInstructionLength())
});
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addArraySet(uint32_t index, ExpressionType, ExpressionType, ExpressionType)
{
m_metadata->appendMetadata<IPInt::ArrayGetSetMetadata>({
static_cast<Wasm::TypeIndex>(index),
static_cast<uint8_t>(getCurrentInstructionLength())
});
changeStackSize(-3);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addArrayLen(ExpressionType, ExpressionType&)
{
// no metadata
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addArrayFill(uint32_t, ExpressionType, ExpressionType, ExpressionType, ExpressionType)
{
changeStackSize(-4);
m_metadata->appendMetadata<IPInt::ArrayFillMetadata>({
static_cast<uint8_t>(getCurrentInstructionLength())
});
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addArrayCopy(uint32_t, ExpressionType, ExpressionType, uint32_t, ExpressionType, ExpressionType, ExpressionType)
{
changeStackSize(-5);
m_metadata->appendMetadata<IPInt::ArrayCopyMetadata>({
static_cast<uint8_t>(getCurrentInstructionLength())
});
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addArrayInitElem(uint32_t, ExpressionType, ExpressionType, uint32_t elemSegmentIndex, ExpressionType, ExpressionType)
{
changeStackSize(-4);
m_metadata->appendMetadata<IPInt::ArrayInitDataMetadata>({
elemSegmentIndex,
static_cast<uint8_t>(getCurrentInstructionLength())
});
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addArrayInitData(uint32_t, ExpressionType, ExpressionType, uint32_t dataSegmentIndex, ExpressionType, ExpressionType)
{
changeStackSize(-4);
m_metadata->appendMetadata<IPInt::ArrayInitDataMetadata>({
dataSegmentIndex,
static_cast<uint8_t>(getCurrentInstructionLength())
});
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addStructNew(uint32_t index, ArgumentList&, ExpressionType&)
{
const StructType& type = *m_info.typeSignatures[index]->expand().as<StructType>();
m_metadata->appendMetadata<IPInt::StructNewMetadata>({
static_cast<Wasm::TypeIndex>(index),
static_cast<uint16_t>(type.fieldCount()),
static_cast<uint8_t>(getCurrentInstructionLength())
});
changeStackSize(-type.fieldCount() + 1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addStructNewDefault(uint32_t index, ExpressionType&)
{
m_metadata->appendMetadata<IPInt::StructNewDefaultMetadata>({
static_cast<Wasm::TypeIndex>(index),
static_cast<uint8_t>(getCurrentInstructionLength())
});
changeStackSize(1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addStructGet(ExtGCOpType, ExpressionType, const StructType&, uint32_t fieldIndex, ExpressionType&)
{
m_metadata->appendMetadata<IPInt::StructGetSetMetadata>({
fieldIndex,
static_cast<uint8_t>(getCurrentInstructionLength())
});
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addStructSet(ExpressionType, const StructType&, uint32_t fieldIndex, ExpressionType)
{
m_metadata->appendMetadata<IPInt::StructGetSetMetadata>({
fieldIndex,
static_cast<uint8_t>(getCurrentInstructionLength())
});
changeStackSize(-2);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addRefTest(ExpressionType, bool, int32_t heapType, bool, ExpressionType&)
{
m_metadata->appendMetadata<IPInt::RefTestCastMetadata>({
heapType,
static_cast<uint8_t>(getCurrentInstructionLength())
});
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addRefCast(ExpressionType, bool, int32_t heapType, ExpressionType&)
{
m_metadata->appendMetadata<IPInt::RefTestCastMetadata>({
heapType,
static_cast<uint8_t>(getCurrentInstructionLength())
});
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addAnyConvertExtern(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addExternConvertAny(ExpressionType, ExpressionType&)
{
return { };
}
// Integer Arithmetic
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI32Add(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI64Add(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI32Sub(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI64Sub(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI32Mul(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI64Mul(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI32DivS(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI32DivU(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI64DivS(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI64DivU(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI32RemS(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI32RemU(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI64RemS(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI64RemU(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
// Bitwise Operations
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI32And(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI64And(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI32Xor(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI64Xor(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI32Or(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI64Or(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI32Shl(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI32ShrU(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI32ShrS(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI64Shl(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI64ShrU(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI64ShrS(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI32Rotl(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI64Rotl(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI32Rotr(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI64Rotr(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI32Popcnt(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI64Popcnt(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI32Clz(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI64Clz(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI32Ctz(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI64Ctz(ExpressionType, ExpressionType&)
{
return { };
}
// Floating-Point Arithmetic
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF32Add(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF64Add(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF32Sub(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF64Sub(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF32Mul(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF64Mul(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF32Div(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF64Div(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
// Other Floating-Point Instructions
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF32Min(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF32Max(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF64Min(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF64Max(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF32Nearest(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF64Nearest(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF32Floor(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF64Floor(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF32Ceil(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF64Ceil(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF32Copysign(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF64Copysign(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF32Sqrt(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF64Sqrt(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF32Neg(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF64Neg(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF32Abs(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF64Abs(ExpressionType, ExpressionType&)
{
return { };
}
// Integer Comparisons
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI32Eq(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI32Ne(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI32LtS(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI32LtU(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI32LeS(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI32LeU(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI32GtS(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI32GtU(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI32GeU(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI32GeS(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI32Eqz(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI64Eq(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI64Ne(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI64GtS(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI64GtU(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI64GeS(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI64GeU(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI64LtS(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI64LtU(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI64LeS(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI64LeU(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI64Eqz(ExpressionType, ExpressionType&)
{
return { };
}
// Floating-Point Comparisons
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF32Eq(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF32Ne(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF32Lt(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF32Le(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF32Gt(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF32Ge(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF64Eq(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF64Ne(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF64Lt(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF64Le(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF64Gt(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF64Ge(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
// Integer Extension
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI64ExtendSI32(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI64ExtendUI32(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI32Extend8S(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI32Extend16S(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI64Extend8S(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI64Extend16S(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI64Extend32S(ExpressionType, ExpressionType&)
{
return { };
}
// Truncation
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF64Trunc(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF32Trunc(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI32TruncSF64(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI32TruncSF32(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI32TruncUF64(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI32TruncUF32(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI64TruncSF64(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI64TruncSF32(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI64TruncUF64(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI64TruncUF32(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::truncSaturated(Ext1OpType, ExpressionType, ExpressionType&, Type, Type)
{
m_metadata->addLength(getCurrentInstructionLength());
return { };
}
// Conversions
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI32WrapI64(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF32DemoteF64(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF64PromoteF32(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF32ReinterpretI32(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI32ReinterpretF32(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF64ReinterpretI64(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addI64ReinterpretF64(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF32ConvertSI32(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF32ConvertUI32(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF32ConvertSI64(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF32ConvertUI64(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF64ConvertSI32(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF64ConvertUI32(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF64ConvertSI64(ExpressionType, ExpressionType&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addF64ConvertUI64(ExpressionType, ExpressionType&)
{
return { };
}
// Control Flow Blocks
void IPIntGenerator::coalesceControlFlow(bool force)
{
// Peek at the next opcode
IPIntLocation here = { nextPC(), curMC() };
if (!force) {
if (m_parser->offset() >= m_parser->source().size())
return;
uint8_t nextOpcode = m_parser->source()[m_parser->offset()];
if (nextOpcode == Block || nextOpcode == End)
return;
} else
here = { curPC(), curMC() };
// There's something useful after us. Resolve everything here.
for (auto& entry : m_coalesceQueue) {
if (entry.isEntry)
resolveEntryTarget(entry.index, here);
else
resolveExitTarget(entry.index, here);
}
m_coalesceQueue.shrink(0);
if (!m_coalesceDebt)
m_controlStructuresAwaitingCoalescing.shrink(0);
for (auto& src : m_exitHandlersAwaitingCoalescing) {
IPInt::BlockMetadata md = { static_cast<int32_t>(here.pc - src.pc), static_cast<int32_t>(here.mc - src.mc) };
WRITE_TO_METADATA(m_metadata->m_metadata.data() + src.mc, md, IPInt::BlockMetadata);
}
m_exitHandlersAwaitingCoalescing.shrink(0);
}
void IPIntGenerator::resolveEntryTarget(unsigned index, IPIntLocation loc)
{
auto& control = m_controlStructuresAwaitingCoalescing[index];
ASSERT(!control.m_entryResolved);
for (auto& src : control.m_awaitingEntryTarget) {
// write delta PC and delta MC
IPInt::BlockMetadata md = { static_cast<int32_t>(loc.pc - src.pc), static_cast<int32_t>(loc.mc - src.mc) };
WRITE_TO_METADATA(m_metadata->m_metadata.data() + src.mc, md, IPInt::BlockMetadata);
}
if (control.isLoop) {
for (auto& src : control.m_awaitingBranchTarget) {
IPInt::BlockMetadata md = { static_cast<int32_t>(loc.pc - src.pc), static_cast<int32_t>(loc.mc - src.mc) };
WRITE_TO_METADATA(m_metadata->m_metadata.data() + src.mc, md, IPInt::BlockMetadata);
}
control.m_awaitingBranchTarget.clear();
}
control.m_awaitingEntryTarget.clear();
control.m_entryResolved = true;
control.m_entryTarget = loc;
}
void IPIntGenerator::resolveExitTarget(unsigned index, IPIntLocation loc)
{
auto& control = m_controlStructuresAwaitingCoalescing[index];
ASSERT(!control.m_exitResolved);
for (auto& src : control.m_awaitingExitTarget) {
// write delta PC and delta MC
IPInt::BlockMetadata md = { static_cast<int32_t>(loc.pc - src.pc), static_cast<int32_t>(loc.mc - src.mc) };
WRITE_TO_METADATA(m_metadata->m_metadata.data() + src.mc, md, IPInt::BlockMetadata);
}
if (!control.isLoop) {
for (auto& src : control.m_awaitingBranchTarget) {
IPInt::BlockMetadata md = { static_cast<int32_t>(loc.pc - src.pc), static_cast<int32_t>(loc.mc - src.mc) };
WRITE_TO_METADATA(m_metadata->m_metadata.data() + src.mc, md, IPInt::BlockMetadata);
}
control.m_awaitingBranchTarget.clear();
}
control.m_awaitingExitTarget.clear();
control.m_exitResolved = true;
control.m_exitTarget = loc;
}
IPIntGenerator::ControlType WARN_UNUSED_RETURN IPIntGenerator::addTopLevel(BlockSignature signature)
{
return ControlType(signature, 0, BlockType::TopLevel);
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addSelect(ExpressionType, ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-2);
m_metadata->addLength(getCurrentInstructionLength());
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addBlock(BlockSignature signature, Stack& oldStack, ControlType& block, Stack& newStack)
{
splitStack(signature, oldStack, newStack);
block = ControlType(signature, m_stackSize.value() - newStack.size(), BlockType::Block);
block.m_index = m_controlStructuresAwaitingCoalescing.size();
block.m_pc = curPC();
block.m_mc = curMC();
block.m_pendingOffset = curMC();
// Register to be coalesced if possible!
m_coalesceQueue.append(QueuedCoalesceRequest { m_controlStructuresAwaitingCoalescing.size(), true });
m_controlStructuresAwaitingCoalescing.append(ControlStructureAwaitingCoalescing {
.startPC = block.m_pc,
.isLoop = false
});
++m_coalesceDebt;
IPIntLocation here = { curPC(), curMC() };
m_metadata->addBlankSpace<IPInt::BlockMetadata>();
tryToResolveEntryTarget(block.m_index, here, m_metadata->m_metadata.data());
coalesceControlFlow();
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addLoop(BlockSignature signature, Stack& oldStack, ControlType& block, Stack& newStack, uint32_t loopIndex)
{
splitStack(signature, oldStack, newStack);
block = ControlType(signature, m_stackSize.value() - newStack.size(), BlockType::Loop);
block.m_index = m_controlStructuresAwaitingCoalescing.size();
block.m_pendingOffset = -1; // no need to update!
block.m_pc = curPC();
// Register to be coalesced if possible!
m_controlStructuresAwaitingCoalescing.append(ControlStructureAwaitingCoalescing {
.m_entryTarget = { curPC(), curMC() },
.startPC = block.m_pc,
.isLoop = true,
.m_entryResolved = true,
});
++m_coalesceDebt;
IPInt::InstructionLengthMetadata md { static_cast<uint8_t>(getCurrentInstructionLength()) };
m_metadata->appendMetadata(md);
// Loop OSR
assertAboutStackSize(m_parser->getStackHeightInValues() + newStack.size() == m_stackSize.value());
unsigned numOSREntryDataValues = m_stackSize.value();
// Note the +1: we do this to avoid having 0 as a key in the map, since the current map can't handle 0 as a key
m_metadata->tierUpCounter().add(m_parser->currentOpcodeStartingOffset() - m_metadata->m_bytecodeOffset + 1, IPIntTierUpCounter::OSREntryData { loopIndex, numOSREntryDataValues, m_tryDepth });
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addIf(ExpressionType, BlockSignature signature, Stack& oldStack, ControlType& block, Stack& newStack)
{
splitStack(signature, oldStack, newStack);
changeStackSize(-1);
block = ControlType(signature, m_stackSize.value() - newStack.size(), BlockType::If);
block.m_index = m_controlStructuresAwaitingCoalescing.size();
block.m_pc = curPC();
block.m_mc = curMC();
block.m_pendingOffset = m_metadata->m_metadata.size();
m_coalesceQueue.append(QueuedCoalesceRequest { m_controlStructuresAwaitingCoalescing.size(), true });
m_controlStructuresAwaitingCoalescing.append(ControlStructureAwaitingCoalescing {
.startPC = block.m_pc,
.isLoop = false
});
++m_coalesceDebt;
IPInt::IfMetadata mdIf {
.elseDeltaPC = 0xbeef,
.elseDeltaMC = 0xbeef,
.instructionLength = { .length = safeCast<uint8_t>(getCurrentInstructionLength()) }
};
m_metadata->appendMetadata(mdIf);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addElse(ControlType& block, Stack&)
{
return addElseToUnreachable(block);
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addElseToUnreachable(ControlType& block)
{
auto blockSignature = block.signature();
const FunctionSignature& signature = *blockSignature.m_signature;
m_stackSize = block.stackSize();
changeStackSize(signature.argumentCount());
auto ifIndex = block.m_index;
auto mdIf = reinterpret_cast<IPInt::IfMetadata*>(m_metadata->m_metadata.data() + block.m_pendingOffset);
// delta PC
mdIf->elseDeltaPC = nextPC() - block.m_pc;
// delta MC
if (m_parser->currentOpcode() == OpType::End) {
// Edge case: if ... end with no else
mdIf->elseDeltaMC = curMC() - block.m_mc;
block = ControlType(block.signature(), block.stackSize(), BlockType::Block);
block.m_index = ifIndex;
block.m_pendingOffset = -1;
block.isElse = true;
return { };
}
// New MC, normal case
mdIf->elseDeltaMC = safeCast<uint32_t>(curMC() + sizeof(IPInt::BlockMetadata)) - block.m_mc;
block = ControlType(block.signature(), block.stackSize(), BlockType::Block);
block.m_index = ifIndex;
block.m_pc = curPC();
block.m_mc = curMC();
block.m_pendingOffset = curMC();
block.isElse = true;
m_metadata->addBlankSpace<IPInt::BlockMetadata>();
return { };
}
// Exception Handling
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addTry(BlockSignature signature, Stack& oldStack, ControlType& block, Stack& newStack)
{
m_tryDepth++;
m_maxTryDepth = std::max(m_maxTryDepth, m_tryDepth.value());
splitStack(signature, oldStack, newStack);
block = ControlType(signature, m_stackSize.value() - newStack.size(), BlockType::Try);
block.m_index = m_controlStructuresAwaitingCoalescing.size();
block.m_tryDepth = m_tryDepth;
block.m_pc = curPC();
block.m_mc = curMC();
m_coalesceQueue.append(QueuedCoalesceRequest { m_controlStructuresAwaitingCoalescing.size(), true });
m_controlStructuresAwaitingCoalescing.append(ControlStructureAwaitingCoalescing {
.startPC = block.m_pc,
.isLoop = false
});
++m_coalesceDebt;
// FIXME: Should this participate the same skipping that block does?
// The upside is that we skip a bunch of sequential try/block instructions.
// The downside is that try needs more metadata.
// It's not clear that code would want to have many nested try blocks
// though.
m_metadata->addLength(getCurrentInstructionLength());
coalesceControlFlow();
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addTryTable(BlockSignature signature, Stack& enclosingStack, const Vector<CatchHandler>& targets, ControlType& result, Stack& newStack)
{
splitStack(signature, enclosingStack, newStack);
result = ControlType(signature, m_stackSize.value() - newStack.size(), BlockType::TryTable);
result.m_tryTableTargets.reserveInitialCapacity(targets.size());
result.m_index = m_controlStructuresAwaitingCoalescing.size();
result.m_pc = curPC();
result.m_mc = curMC();
result.m_pendingOffset = curMC();
m_coalesceQueue.append(QueuedCoalesceRequest { m_controlStructuresAwaitingCoalescing.size(), true });
m_controlStructuresAwaitingCoalescing.append(ControlStructureAwaitingCoalescing {
.startPC = result.m_pc,
.isLoop = false
});
++m_coalesceDebt;
IPIntLocation here = { curPC(), curMC() };
m_metadata->addBlankSpace<IPInt::BlockMetadata>();
tryToResolveEntryTarget(result.m_index, here, m_metadata->m_metadata.data());
result.m_tryTableTargets.appendUsingFunctor(targets.size(),
[&](unsigned i) -> ControlType::TryTableTarget {
auto& target = targets[i];
return {
target.type,
target.tag,
target.exceptionSignature,
target.target
};
}
);
// append all the branch data first
for (auto& target : targets) {
auto entry = m_parser->resolveControlRef(target.target).controlData;
// stack size at destination is (locals) + (everything below target) + (things we push)
m_metadata->appendMetadata<IPInt::CatchMetadata>({
static_cast<uint32_t>(entry.stackSize() + entry.branchTargetArity() + roundUpToMultipleOf<2>(m_metadata->m_numLocals))
});
IPIntLocation here = { curPC(), curMC() };
m_metadata->appendMetadata<IPInt::BlockMetadata>({
.deltaPC = 0xbeef, .deltaMC = 0xbeef
});
tryToResolveBranchTarget(entry, here, m_metadata->m_metadata.data());
}
coalesceControlFlow();
return { };
}
void IPIntGenerator::convertTryToCatch(ControlType& tryBlock, CatchKind catchKind)
{
ASSERT(ControlType::isTry(tryBlock));
ControlType catchBlock = ControlType(tryBlock.signature(), tryBlock.stackSize(), BlockType::Catch, catchKind);
catchBlock.m_pc = tryBlock.m_pc;
catchBlock.m_pcEnd = m_parser->currentOpcodeStartingOffset() - m_metadata->m_bytecodeOffset;
catchBlock.m_tryDepth = tryBlock.m_tryDepth;
catchBlock.m_index = tryBlock.m_index;
catchBlock.m_mc = tryBlock.m_mc;
tryBlock = WTFMove(catchBlock);
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addCatch(unsigned exceptionIndex, const TypeDefinition& exceptionSignature, Stack&, ControlType& block, ResultList& results)
{
return addCatchToUnreachable(exceptionIndex, exceptionSignature, block, results);
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addCatchToUnreachable(unsigned exceptionIndex, const TypeDefinition& exceptionSignature, ControlType& block, ResultList& results)
{
if (ControlType::isTry(block))
convertTryToCatch(block, CatchKind::Catch);
const FunctionSignature& signature = *exceptionSignature.as<FunctionSignature>();
for (unsigned i = 0; i < signature.argumentCount(); i++)
results.append(Value { });
assertAboutStackSize(block.stackSize() == m_parser->getControlEntryStackHeightInValues());
m_stackSize = block.stackSize();
changeStackSize(signature.argumentCount());
// FIXME: If this is actually unreachable we shouldn't need metadata.
block.m_catchesAwaitingFixup.append({ curPC(), curMC() });
m_metadata->addBlankSpace<IPInt::BlockMetadata>();
m_metadata->m_exceptionHandlers.append({
HandlerType::Catch,
static_cast<uint32_t>(block.m_pc),
static_cast<uint32_t>(block.m_pcEnd),
static_cast<uint32_t>(m_parser->offset() - m_metadata->m_bytecodeOffset),
static_cast<uint32_t>(m_metadata->m_metadata.size()),
m_tryDepth,
exceptionIndex
});
uint32_t stackSizeInV128 = m_stackSize.value() + roundUpToMultipleOf<2>(m_metadata->m_numLocals);
IPInt::CatchMetadata mdCatch {
.stackSizeInV128 = stackSizeInV128
};
m_metadata->appendMetadata(mdCatch);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addCatchAll(Stack&, ControlType& block)
{
return addCatchAllToUnreachable(block);
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addCatchAllToUnreachable(ControlType& block)
{
UNUSED_PARAM(block);
if (ControlType::isTry(block))
convertTryToCatch(block, CatchKind::CatchAll);
else
block.m_catchKind = CatchKind::CatchAll;
assertAboutStackSize(block.stackSize() == m_parser->getControlEntryStackHeightInValues());
m_stackSize = block.stackSize();
// FIXME: If this is actually unreachable we shouldn't need metadata.
block.m_catchesAwaitingFixup.append({ curPC(), curMC() });
m_metadata->addBlankSpace(sizeof(IPInt::BlockMetadata));
m_metadata->m_exceptionHandlers.append({
HandlerType::CatchAll,
static_cast<uint32_t>(block.m_pc),
static_cast<uint32_t>(block.m_pcEnd),
static_cast<uint32_t>(m_parser->offset() - m_metadata->m_bytecodeOffset),
static_cast<uint32_t>(m_metadata->m_metadata.size()),
m_tryDepth,
0
});
uint32_t stackSizeInV128 = m_stackSize.value() + roundUpToMultipleOf<2>(m_metadata->m_numLocals);
IPInt::CatchMetadata mdCatch {
.stackSizeInV128 = stackSizeInV128
};
m_metadata->appendMetadata(mdCatch);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addDelegate(ControlType& target, ControlType& data)
{
return addDelegateToUnreachable(target, data);
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addDelegateToUnreachable(ControlType& target, ControlType& data)
{
UNUSED_PARAM(target);
UNUSED_PARAM(data);
data.m_pcEnd = curPC();
// FIXME: If this is actually unreachable we shouldn't need metadata.
data.m_catchesAwaitingFixup.append({ curPC(), curMC() });
m_metadata->addBlankSpace<IPInt::BlockMetadata>();
ASSERT(ControlType::isTry(target) || ControlType::isTopLevel(target));
unsigned targetDepth = ControlType::isTry(target) ? target.m_tryDepth : 0;
m_metadata->m_exceptionHandlers.append({
HandlerType::Delegate,
static_cast<uint32_t>(data.m_pc),
static_cast<uint32_t>(data.m_pcEnd),
static_cast<uint32_t>(curPC()),
static_cast<uint32_t>(curMC()),
m_tryDepth,
targetDepth
});
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addThrow(unsigned exceptionIndex, ArgumentList&, Stack&)
{
IPInt::ThrowMetadata mdThrow {
.exceptionIndex = safeCast<uint32_t>(exceptionIndex)
};
m_metadata->appendMetadata(mdThrow);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addRethrow(unsigned, ControlType& catchBlock)
{
m_usesRethrow = true;
IPInt::RethrowMetadata mdRethrow {
.tryDepth = catchBlock.m_tryDepth
};
auto size = m_metadata->m_metadata.size();
m_metadata->addBlankSpace(sizeof(mdRethrow));
WRITE_TO_METADATA(m_metadata->m_metadata.data() + size, mdRethrow, IPInt::RethrowMetadata);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addThrowRef(ExpressionType, Stack&)
{
changeStackSize(-1);
return { };
}
// Control Flow Branches
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addReturn(const ControlType&, const Stack&)
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addBranch(ControlType& block, ExpressionType, const Stack&)
{
if (m_parser->currentOpcode() == OpType::BrIf)
changeStackSize(-1);
IPIntLocation here = { curPC(), curMC() };
IPInt::BranchMetadata branch {
.target = {
.block = { .deltaPC = 0xbeef, .deltaMC = 0xbeef },
.toPop = safeCast<uint16_t>(m_stackSize - block.stackSize() - block.branchTargetArity()),
.toKeep = safeCast<uint16_t>(block.branchTargetArity()),
},
.instructionLength = { .length = safeCast<uint8_t>(getCurrentInstructionLength()) }
};
m_metadata->appendMetadata(branch);
tryToResolveBranchTarget(block, here, m_metadata->m_metadata.data());
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addBranchNull(ControlType& block, ExpressionType, Stack&, bool shouldNegate, ExpressionType&)
{
// We don't need shouldNegate in the metadata since it's in the opcode
IPIntLocation here = { curPC(), curMC() };
unsigned toPop = m_stackSize - block.stackSize() - block.branchTargetArity();
// if we branch_on_null, we'll pop the null first
if (!shouldNegate)
toPop -= 1;
IPInt::BranchMetadata branch {
.target = {
.block = { .deltaPC = 0xbeef, .deltaMC = 0xbeef },
.toPop = safeCast<uint16_t>(toPop),
.toKeep = safeCast<uint16_t>(block.branchTargetArity()),
},
.instructionLength = { .length = safeCast<uint8_t>(getCurrentInstructionLength()) }
};
m_metadata->appendMetadata(branch);
tryToResolveBranchTarget(block, here, m_metadata->m_metadata.data());
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addBranchCast(ControlType& block, ExpressionType, Stack&, bool, int32_t heapType, bool)
{
m_metadata->appendMetadata<IPInt::RefTestCastMetadata>({
heapType,
0
});
IPIntLocation here = { curPC(), curMC() };
m_metadata->appendMetadata<IPInt::BranchMetadata>({
.target = {
.block = { .deltaPC = 0xbeef, .deltaMC = 0xbeef },
.toPop = safeCast<uint16_t>(m_stackSize - block.stackSize() - block.branchTargetArity()),
.toKeep = safeCast<uint16_t>(block.branchTargetArity()),
},
.instructionLength = { .length = safeCast<uint8_t>(getCurrentInstructionLength()) }
});
tryToResolveBranchTarget(block, here, m_metadata->m_metadata.data());
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addSwitch(ExpressionType, const Vector<ControlType*>& jumps, ControlType& defaultJump, const Stack&)
{
changeStackSize(-1);
IPInt::SwitchMetadata mdSwitch {
.size = safeCast<uint32_t>(jumps.size() + 1),
.target = { }
};
m_metadata->appendMetadata(mdSwitch);
for (auto* block : jumps) {
IPInt::BranchTargetMetadata target {
.block = { .deltaPC = 0xbeef, .deltaMC = 0xbeef },
.toPop = safeCast<uint16_t>(m_stackSize - block->stackSize() - block->branchTargetArity()),
.toKeep = safeCast<uint16_t>(block->branchTargetArity())
};
IPIntLocation here = { curPC(), curMC() };
m_metadata->appendMetadata(target);
tryToResolveBranchTarget(*block, here, m_metadata->m_metadata.data());
}
IPInt::BranchTargetMetadata defaultTarget {
.block = { .deltaPC = 0xbeef, .deltaMC = 0xbeef },
.toPop = safeCast<uint16_t>(m_stackSize - defaultJump.stackSize() - defaultJump.branchTargetArity()),
.toKeep = safeCast<uint16_t>(defaultJump.branchTargetArity())
};
IPIntLocation here = { curPC(), curMC() };
m_metadata->appendMetadata(defaultTarget);
tryToResolveBranchTarget(defaultJump, here, m_metadata->m_metadata.data());
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::endBlock(ControlEntry& entry, Stack& stack)
{
return addEndToUnreachable(entry, stack);
}
void IPIntGenerator::endTryTable(ControlType& data)
{
auto targets = data.m_tryTableTargets;
unsigned i = 0;
for (auto& target : targets) {
HandlerType targetType;
switch (target.type) {
case CatchKind::Catch:
targetType = HandlerType::TryTableCatch;
break;
case CatchKind::CatchRef:
targetType = HandlerType::TryTableCatchRef;
break;
case CatchKind::CatchAll:
targetType = HandlerType::TryTableCatchAll;
break;
case CatchKind::CatchAllRef:
targetType = HandlerType::TryTableCatchAllRef;
break;
}
auto entry = m_parser->resolveControlRef(target.target).controlData;
m_metadata->m_exceptionHandlers.append({
targetType,
data.m_pc,
curPC(),
// index into the array of try_table targets
data.m_pc, // PC will be fixed up relative to the try_table's PC
static_cast<unsigned>(data.m_mc
+ sizeof(IPInt::BlockMetadata)
+ i * (sizeof(IPInt::CatchMetadata) + sizeof(IPInt::BlockMetadata))),
m_tryDepth,
target.tag
});
++i;
}
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addEndToUnreachable(ControlEntry& entry, Stack&)
{
auto blockSignature = entry.controlData.signature();
const auto& signature = *blockSignature.m_signature;
for (unsigned i = 0; i < signature.returnCount(); i ++)
entry.enclosedExpressionStack.constructAndAppend(signature.returnType(i), Value { });
auto block = entry.controlData;
m_stackSize = block.stackSize();
changeStackSize(signature.returnCount());
if (ControlType::isTry(block) || ControlType::isAnyCatch(block)) {
--m_tryDepth;
m_exitHandlersAwaitingCoalescing.appendVector(block.m_catchesAwaitingFixup);
}
if (ControlType::isTryTable(block))
endTryTable(block);
if (ControlType::isTopLevel(block)) {
// Hit the end
m_exitHandlersAwaitingCoalescing.appendVector(m_jumpLocationsAwaitingEnd);
coalesceControlFlow(true);
// Metadata = round up 8 bytes, one for each
m_metadata->m_bytecode = m_metadata->m_bytecode.first(m_parser->offset());
return { };
}
if (ControlType::isIf(block)) {
m_exitHandlersAwaitingCoalescing.append({ block.m_pc, block.m_mc });
} else if (ControlType::isBlock(block)) {
if (block.isElse) {
// if it's not an if ... end, coalesce
if (block.m_pendingOffset != -1)
m_exitHandlersAwaitingCoalescing.append({ block.m_pc, block.m_mc });
m_coalesceQueue.append({ static_cast<unsigned>(block.m_index), false });
--m_coalesceDebt;
} else {
// block
m_coalesceQueue.append({ static_cast<unsigned>(block.m_index), false });
--m_coalesceDebt;
}
} else if (ControlType::isLoop(block)) {
m_coalesceQueue.append({ static_cast<unsigned>(block.m_index), false });
--m_coalesceDebt;
} else if (ControlType::isTryTable(block)) {
m_coalesceQueue.append({ static_cast<unsigned>(block.m_index), false });
--m_coalesceDebt;
} else if (ControlType::isTry(block) || ControlType::isAnyCatch(block)) {
m_coalesceQueue.append({ static_cast<unsigned>(block.m_index), false });
--m_coalesceDebt;
}
// mark pending exit targets to be resolved
// any pending branch targets must be blocks because a loop would've been resolved. if it's loop, end then there's nobody
// asking for the target
coalesceControlFlow();
return { };
}
auto IPIntGenerator::endTopLevel(BlockSignature signature, const Stack& expressionStack) -> PartialResult
{
if (m_usesSIMD)
m_info.markUsesSIMD(m_metadata->functionIndex());
RELEASE_ASSERT(expressionStack.size() == signature.m_signature->returnCount());
m_info.doneSeeingFunction(m_metadata->m_functionIndex);
return { };
}
// Calls
void IPIntGenerator::addCallCommonData(const FunctionSignature&, const CallInformation& callConvention)
{
// CallCommonData payload is the same for the same CallInformation.
// We use previously generated payload if we hit the cache!
if (!m_cachedCallBytecode.isEmpty()) {
size_t size = m_metadata->m_metadata.size();
m_metadata->addBlankSpace(m_cachedCallBytecode.size());
memcpy(m_metadata->m_metadata.data() + size, m_cachedCallBytecode.data(), m_cachedCallBytecode.size());
return;
}
uint16_t stackArgs = 0;
constexpr static int NUM_MINT_CALL_GPRS = 8;
constexpr static int NUM_MINT_CALL_FPRS = 8;
ASSERT_UNUSED(NUM_MINT_CALL_GPRS, wasmCallingConvention().jsrArgs.size() <= NUM_MINT_CALL_GPRS);
ASSERT_UNUSED(NUM_MINT_CALL_FPRS, wasmCallingConvention().fprArgs.size() <= NUM_MINT_CALL_FPRS);
auto toSpan = [&](auto& metadata) {
auto start = std::bit_cast<const uint8_t*>(&metadata);
return std::span { start, start + sizeof(metadata) };
};
m_cachedCallBytecode.append(static_cast<uint8_t>(IPInt::CallArgumentBytecode::Call));
m_cachedCallBytecode.appendUsingFunctor(callConvention.params.size(),
[&](unsigned index) -> uint8_t {
auto loc = callConvention.params[index].location;
if (loc.isGPR()) {
#if USE(JSVALUE64)
ASSERT_UNUSED(NUM_MINT_CALL_GPRS, GPRInfo::toArgumentIndex(loc.jsr().gpr()) < NUM_MINT_CALL_GPRS);
return static_cast<uint8_t>(IPInt::CallArgumentBytecode::ArgumentGPR) + GPRInfo::toArgumentIndex(loc.jsr().gpr());
#elif USE(JSVALUE32_64)
ASSERT_UNUSED(NUM_MINT_CALL_GPRS, GPRInfo::toArgumentIndex(loc.jsr().payloadGPR()) < NUM_MINT_CALL_GPRS);
ASSERT_UNUSED(NUM_MINT_CALL_GPRS, GPRInfo::toArgumentIndex(loc.jsr().tagGPR()) < NUM_MINT_CALL_GPRS);
return static_cast<uint8_t>(IPInt::CallArgumentBytecode::ArgumentGPR) + GPRInfo::toArgumentIndex(loc.jsr().gpr(WhichValueWord::PayloadWord));
#endif
}
if (loc.isFPR()) {
ASSERT_UNUSED(NUM_MINT_CALL_FPRS, FPRInfo::toArgumentIndex(loc.fpr()) < NUM_MINT_CALL_FPRS);
return static_cast<uint8_t>(IPInt::CallArgumentBytecode::ArgumentFPR) + FPRInfo::toArgumentIndex(loc.fpr());
}
if (loc.isStackArgument()) {
if (stackArgs++ & 1)
return static_cast<uint8_t>(IPInt::CallArgumentBytecode::ArgumentStackUnaligned);
return static_cast<uint8_t>(IPInt::CallArgumentBytecode::ArgumentStackAligned);
}
RELEASE_ASSERT_NOT_REACHED();
return 0;
});
if (stackArgs & 1) {
++stackArgs;
m_cachedCallBytecode.append(static_cast<uint8_t>(IPInt::CallArgumentBytecode::StackAlign));
}
for (unsigned i = stackArgs; i < callConvention.numberOfStackValues; i += 2)
m_cachedCallBytecode.append(static_cast<uint8_t>(IPInt::CallArgumentBytecode::StackAlign));
m_cachedCallBytecode.reverse();
IPInt::CallReturnMetadata commonReturn {
.stackFrameSize = static_cast<uint32_t>(callConvention.headerAndArgumentStackSizeInBytes),
.resultBytecode = { }
};
m_cachedCallBytecode.append(toSpan(commonReturn));
constexpr static int NUM_MINT_RET_GPRS = 8;
constexpr static int NUM_MINT_RET_FPRS = 8;
ASSERT_UNUSED(NUM_MINT_RET_GPRS, wasmCallingConvention().jsrArgs.size() <= NUM_MINT_RET_GPRS);
ASSERT_UNUSED(NUM_MINT_RET_FPRS, wasmCallingConvention().fprArgs.size() <= NUM_MINT_RET_FPRS);
bool hasSeenStackArgument = false;
uint32_t firstStackArgumentSPOffset = 0;
size_t indexForSPOffset = m_cachedCallBytecode.size();
m_cachedCallBytecode.append(toSpan(firstStackArgumentSPOffset));
m_cachedCallBytecode.appendUsingFunctor(callConvention.results.size(),
[&](unsigned index) -> uint8_t {
auto loc = callConvention.results[index].location;
if (loc.isGPR()) {
ASSERT_UNUSED(NUM_MINT_RET_GPRS, GPRInfo::toArgumentIndex(loc.jsr().payloadGPR()) < NUM_MINT_RET_GPRS);
#if USE(JSVALUE64)
return static_cast<uint8_t>(IPInt::CallResultBytecode::ResultGPR) + GPRInfo::toArgumentIndex(loc.jsr().gpr());
#elif USE(JSVALUE32_64)
return static_cast<uint8_t>(IPInt::CallResultBytecode::ResultGPR) + GPRInfo::toArgumentIndex(loc.jsr().gpr(WhichValueWord::PayloadWord));
#endif
}
if (loc.isFPR()) {
ASSERT_UNUSED(NUM_MINT_RET_FPRS, FPRInfo::toArgumentIndex(loc.fpr()) < NUM_MINT_RET_FPRS);
return static_cast<uint8_t>(IPInt::CallResultBytecode::ResultFPR) + FPRInfo::toArgumentIndex(loc.fpr());
}
if (loc.isStackArgument()) {
if (!hasSeenStackArgument) {
hasSeenStackArgument = true;
// If our first argument starts further down the frame, we need to push a bunch of empty values
// If our first stack argument is in an "odd" slot, we need to skip one slot.
firstStackArgumentSPOffset = loc.offsetFromSP();
}
return static_cast<uint8_t>(IPInt::CallResultBytecode::ResultStack);
}
RELEASE_ASSERT_NOT_REACHED();
return 0;
});
m_cachedCallBytecode.append(static_cast<uint8_t>(IPInt::CallResultBytecode::End));
for (uint8_t v : toSpan(firstStackArgumentSPOffset))
m_cachedCallBytecode[indexForSPOffset++] = v;
size_t size = m_metadata->m_metadata.size();
m_metadata->addBlankSpace(m_cachedCallBytecode.size());
memcpy(m_metadata->m_metadata.data() + size, m_cachedCallBytecode.data(), m_cachedCallBytecode.size());
}
void IPIntGenerator::addTailCallCommonData(const FunctionSignature& signature)
{
auto& callConvention = cachedCallInformationFor(signature);
uint16_t stackArgs = 0;
constexpr static int NUM_MINT_CALL_GPRS = 8;
constexpr static int NUM_MINT_CALL_FPRS = 8;
ASSERT_UNUSED(NUM_MINT_CALL_GPRS, wasmCallingConvention().jsrArgs.size() <= NUM_MINT_CALL_GPRS);
ASSERT_UNUSED(NUM_MINT_CALL_FPRS, wasmCallingConvention().fprArgs.size() <= NUM_MINT_CALL_FPRS);
Vector<uint8_t, 16> mINTBytecode;
mINTBytecode.append(static_cast<uint8_t>(IPInt::CallArgumentBytecode::TailCall));
mINTBytecode.appendUsingFunctor(callConvention.params.size(),
[&](unsigned index) -> uint8_t {
auto loc = callConvention.params[index].location;
if (loc.isGPR()) {
#if USE(JSVALUE64)
ASSERT_UNUSED(NUM_MINT_CALL_GPRS, GPRInfo::toArgumentIndex(loc.jsr().gpr()) < NUM_MINT_CALL_GPRS);
return static_cast<uint8_t>(IPInt::CallArgumentBytecode::ArgumentGPR) + GPRInfo::toArgumentIndex(loc.jsr().gpr());
#elif USE(JSVALUE32_64)
ASSERT_UNUSED(NUM_MINT_CALL_GPRS, GPRInfo::toArgumentIndex(loc.jsr().payloadGPR()) < NUM_MINT_CALL_GPRS);
ASSERT_UNUSED(NUM_MINT_CALL_GPRS, GPRInfo::toArgumentIndex(loc.jsr().tagGPR()) < NUM_MINT_CALL_GPRS);
return static_cast<uint8_t>(IPInt::CallArgumentBytecode::ArgumentGPR) + GPRInfo::toArgumentIndex(loc.jsr().gpr(WhichValueWord::PayloadWord));
#endif
}
if (loc.isFPR()) {
ASSERT_UNUSED(NUM_MINT_CALL_FPRS, FPRInfo::toArgumentIndex(loc.fpr()) < NUM_MINT_CALL_FPRS);
return static_cast<uint8_t>(IPInt::CallArgumentBytecode::ArgumentFPR) + FPRInfo::toArgumentIndex(loc.fpr());
}
if (loc.isStackArgument()) {
if (stackArgs++ & 1)
return static_cast<uint8_t>(IPInt::CallArgumentBytecode::TailArgumentStackUnaligned);
return static_cast<uint8_t>(IPInt::CallArgumentBytecode::TailArgumentStackAligned);
}
RELEASE_ASSERT_NOT_REACHED();
return 0;
});
if (stackArgs & 1) {
++stackArgs;
mINTBytecode.append(static_cast<uint8_t>(IPInt::CallArgumentBytecode::TailStackAlign));
}
for (unsigned i = stackArgs, limit = WTF::roundUpToMultipleOf(stackAlignmentRegisters(), callConvention.numberOfStackValues); i < limit; i += stackAlignmentRegisters())
mINTBytecode.append(static_cast<uint8_t>(IPInt::CallArgumentBytecode::TailStackAlign));
auto size = m_metadata->m_metadata.size();
m_metadata->addBlankSpace(mINTBytecode.size());
std::ranges::reverse_copy(mINTBytecode, m_metadata->m_metadata.data() + size);
uint32_t numStackValues = WTF::roundUpToMultipleOf(stackAlignmentRegisters(), callConvention.numberOfStackValues);
// each stack value is 8B, so to calculate stack size in V128, we need to divide by two
if (m_stackSize + numStackValues / 2 > m_maxStackSize)
m_maxStackSize = m_stackSize + numStackValues / 2;
ASSERT(!(numStackValues % 2));
m_metadata->appendMetadata(numStackValues);
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addCall(FunctionSpaceIndex index, const TypeDefinition& type, ArgumentList&, ResultList& results, CallType callType)
{
const FunctionSignature& signature = *type.as<FunctionSignature>();
if (callType == CallType::TailCall) {
// on a tail call, we need to:
// roll back to old SP, shift SP to accommodate arguments
// put arguments into registers / sp (reutilize mINT)
// jump to entrypoint
changeStackSize(-signature.argumentCount());
const auto& callingConvention = wasmCallingConvention();
m_metadata->setTailCall(index, m_info.isImportedFunctionFromFunctionIndexSpace(index));
const TypeIndex callerTypeIndex = m_info.internalFunctionTypeIndices[m_functionIndex];
const TypeDefinition& callerTypeDefinition = TypeInformation::get(callerTypeIndex).expand();
uint32_t callerStackArgs = WTF::roundUpToMultipleOf(stackAlignmentRegisters(), callingConvention.numberOfStackValues(*callerTypeDefinition.as<FunctionSignature>()));
IPInt::TailCallMetadata functionIndexMetadata {
.length = safeCast<uint8_t>(getCurrentInstructionLength()),
.functionIndex = index,
.callerStackArgSize = static_cast<int32_t>(callerStackArgs * sizeof(Register)),
.argumentBytecode = { }
};
m_metadata->appendMetadata(functionIndexMetadata);
addTailCallCommonData(signature);
return { };
}
auto& callConvention = cachedCallInformationFor(signature);
results.appendUsingFunctor(signature.returnCount(), [](unsigned) { return Value { }; });
changeStackSize(signature.returnCount() - signature.argumentCount());
if (!m_info.isImportedFunctionFromFunctionIndexSpace(index))
m_metadata->m_callees.testAndSet(index - m_info.importFunctionCount());
IPInt::CallMetadata functionIndexMetadata {
.length = safeCast<uint8_t>(getCurrentInstructionLength()),
.functionIndex = index,
.signature = {
static_cast<uint32_t>(callConvention.headerAndArgumentStackSizeInBytes),
static_cast<uint16_t>(signature.returnCount() > signature.argumentCount() ? signature.returnCount() - signature.argumentCount() : 0),
static_cast<uint16_t>(signature.argumentCount())
},
.argumentBytecode = { }
};
m_metadata->appendMetadata(functionIndexMetadata);
addCallCommonData(signature, callConvention);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addCallIndirect(unsigned tableIndex, const TypeDefinition& originalSignature, ArgumentList&, ResultList& results, CallType callType)
{
const FunctionSignature& signature = *originalSignature.expand().as<FunctionSignature>();
if (callType == CallType::TailCall) {
const unsigned callIndex = 1;
changeStackSize(-signature.argumentCount() - callIndex);
m_metadata->setTailCallClobbersInstance();
// on a tail call, we need to:
// roll back to old SP, shift SP to accommodate arguments
// put arguments into registers / sp (reutilize mINT)
// jump to entrypoint
const auto& callingConvention = wasmCallingConvention();
const TypeIndex callerTypeIndex = m_info.internalFunctionTypeIndices[m_functionIndex];
const TypeDefinition& callerTypeDefinition = TypeInformation::get(callerTypeIndex).expand();
uint32_t callerStackArgs = WTF::roundUpToMultipleOf(stackAlignmentRegisters(), callingConvention.numberOfStackValues(*callerTypeDefinition.as<FunctionSignature>()));
IPInt::TailCallIndirectMetadata functionIndexMetadata {
.length = safeCast<uint8_t>(getCurrentInstructionLength()),
.tableIndex = tableIndex,
.typeIndex = m_metadata->addSignature(originalSignature),
.callerStackArgSize = static_cast<int32_t>(callerStackArgs * sizeof(Register)),
.argumentBytecode = { }
};
m_metadata->appendMetadata(functionIndexMetadata);
addTailCallCommonData(signature);
return { };
}
auto& callConvention = cachedCallInformationFor(signature);
results.appendUsingFunctor(signature.returnCount(), [](unsigned) { return Value { }; });
const unsigned callIndex = 1;
changeStackSize(signature.returnCount() - signature.argumentCount() - callIndex);
IPInt::CallIndirectMetadata functionIndexMetadata {
.length = safeCast<uint8_t>(getCurrentInstructionLength()),
.tableIndex = tableIndex,
.typeIndex = m_metadata->addSignature(originalSignature),
.signature = {
static_cast<uint32_t>(callConvention.headerAndArgumentStackSizeInBytes),
static_cast<uint16_t>(signature.returnCount() > signature.argumentCount() ? signature.returnCount() - signature.argumentCount() : 0),
static_cast<uint16_t>(signature.argumentCount())
},
.argumentBytecode = { }
};
m_metadata->appendMetadata(functionIndexMetadata);
addCallCommonData(signature, callConvention);
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addCallRef(const TypeDefinition& originalSignature, ArgumentList&, ResultList& results, CallType callType)
{
const FunctionSignature& signature = *originalSignature.expand().as<FunctionSignature>();
if (callType == CallType::TailCall) {
const unsigned callIndex = 1;
changeStackSize(-signature.argumentCount() - callIndex);
m_metadata->setTailCallClobbersInstance();
// on a tail call, we need to:
// roll back to old SP, shift SP to accommodate arguments
// put arguments into registers / sp (reutilize mINT)
// jump to entrypoint
const auto& callingConvention = wasmCallingConvention();
const TypeIndex callerTypeIndex = m_info.internalFunctionTypeIndices[m_functionIndex];
const TypeDefinition& callerTypeDefinition = TypeInformation::get(callerTypeIndex).expand();
uint32_t callerStackArgs = WTF::roundUpToMultipleOf(stackAlignmentRegisters(), callingConvention.numberOfStackValues(*callerTypeDefinition.as<FunctionSignature>()));
IPInt::TailCallRefMetadata callMetadata {
.length = safeCast<uint8_t>(getCurrentInstructionLength()),
.typeIndex = m_metadata->addSignature(originalSignature),
.callerStackArgSize = static_cast<int32_t>(callerStackArgs * sizeof(Register)),
.argumentBytecode = { }
};
m_metadata->appendMetadata(callMetadata);
addTailCallCommonData(signature);
return { };
}
auto& callConvention = cachedCallInformationFor(signature);
results.appendUsingFunctor(signature.returnCount(), [](unsigned) { return Value { }; });
const unsigned callRef = 1;
changeStackSize(signature.returnCount() - signature.argumentCount() - callRef);
IPInt::CallRefMetadata callMetadata {
.length = safeCast<uint8_t>(getCurrentInstructionLength()),
.typeIndex = m_metadata->addSignature(originalSignature),
.signature = {
static_cast<uint32_t>(callConvention.headerAndArgumentStackSizeInBytes),
static_cast<uint16_t>(signature.returnCount() > signature.argumentCount() ? signature.returnCount() - signature.argumentCount() : 0),
static_cast<uint16_t>(signature.argumentCount())
},
.argumentBytecode = { }
};
m_metadata->appendMetadata(callMetadata);
addCallCommonData(signature, callConvention);
return { };
}
// Traps
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addUnreachable()
{
return { };
}
PartialResult WARN_UNUSED_RETURN IPIntGenerator::addCrash()
{
return { };
}
// Finalize
std::unique_ptr<FunctionIPIntMetadataGenerator> IPIntGenerator::finalize()
{
if (m_usesRethrow) {
m_metadata->m_numAlignedRethrowSlots = roundUpToMultipleOf<2>(m_maxTryDepth);
for (uint32_t catchSPOffset : m_catchSPMetadataOffsets)
*reinterpret_cast_ptr<uint32_t*>(m_metadata->m_metadata.data() + catchSPOffset) += m_metadata->m_numAlignedRethrowSlots;
}
// Pad the metadata to an even number since we will allocate the rounded up size
if (m_metadata->m_numLocals % 2)
m_metadata->m_argumINTBytecode.append(0);
m_metadata->m_maxFrameSizeInV128 = roundUpToMultipleOf<2>(m_metadata->m_numLocals) / 2;
m_metadata->m_maxFrameSizeInV128 += m_metadata->m_numAlignedRethrowSlots / 2;
m_metadata->m_maxFrameSizeInV128 += m_maxStackSize;
return WTFMove(m_metadata);
}
Expected<std::unique_ptr<FunctionIPIntMetadataGenerator>, String> parseAndCompileMetadata(std::span<const uint8_t> function, const TypeDefinition& signature, ModuleInformation& info, FunctionCodeIndex functionIndex)
{
IPIntGenerator generator(info, functionIndex, signature, function);
FunctionParser<IPIntGenerator> parser(generator, function, signature, info);
WASM_FAIL_IF_HELPER_FAILS(parser.parse());
return generator.finalize();
}
void IPIntGenerator::dump(const ControlStack&, const Stack*)
{
dataLogLn("PC: ", m_parser->currentOpcodeStartingOffset() - m_metadata->m_bytecodeOffset, " MC: ", m_metadata->m_metadata.size());
}
} } // namespace JSC::Wasm
WTF_ALLOW_UNSAFE_BUFFER_USAGE_END
#endif // ENABLE(WEBASSEMBLY)
|