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
|
/*
* Copyright (C) 2019-2024 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 "WasmLLIntGenerator.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 "WasmFunctionCodeBlockGenerator.h"
#include "WasmFunctionParser.h"
#include "WasmGeneratorTraits.h"
#include <variant>
#include <wtf/CompletionHandler.h>
#include <wtf/RefPtr.h>
#include <wtf/text/MakeString.h>
namespace JSC { namespace Wasm {
class LLIntGenerator : public BytecodeGeneratorBase<GeneratorTraits> {
public:
using ExpressionType = VirtualRegister;
using CallType = CallLinkInfo::CallType;
static constexpr bool shouldFuseBranchCompare = false;
static constexpr bool tierSupportsSIMD = false;
static constexpr bool validateFunctionBodySize = true;
struct ControlLoop {
Ref<Label> m_body;
};
struct ControlTopLevel {
};
struct ControlBlock {
};
struct ControlIf {
Ref<Label> m_alternate;
};
struct ControlTry {
Ref<Label> m_try;
unsigned m_tryDepth;
};
struct ControlType;
struct ControlTryTable {
struct TryTableTarget {
CatchKind type;
uint32_t tag;
const TypeDefinition* exceptionSignature;
RefPtr<Label> target;
unsigned targetStackSize;
};
using TargetList = Vector<TryTableTarget>;
Ref<Label> m_try;
unsigned m_tryDepth;
TargetList targets;
};
struct ControlCatch {
CatchKind m_kind;
Ref<Label> m_tryStart;
Ref<Label> m_tryEnd;
unsigned m_tryDepth;
VirtualRegister m_exception;
};
struct CatchRewriteInfo {
unsigned m_instructionOffset;
unsigned m_tryDepth;
};
struct ControlType : public std::variant<ControlLoop, ControlTopLevel, ControlBlock, ControlIf, ControlTry, ControlCatch, ControlTryTable> {
using Base = std::variant<ControlLoop, ControlTopLevel, ControlBlock, ControlIf, ControlTry, ControlCatch, ControlTryTable>;
ControlType()
: Base(ControlBlock { })
{
}
static ControlType topLevel(BlockSignature signature, unsigned stackSize, RefPtr<Label>&& continuation)
{
return ControlType(signature, stackSize, WTFMove(continuation), ControlTopLevel { });
}
static ControlType loop(BlockSignature signature, unsigned stackSize, Ref<Label>&& body, RefPtr<Label>&& continuation)
{
return ControlType(signature, stackSize - signature.m_signature->argumentCount(), WTFMove(continuation), ControlLoop { WTFMove(body) });
}
static ControlType block(BlockSignature signature, unsigned stackSize, RefPtr<Label>&& continuation)
{
return ControlType(signature, stackSize - signature.m_signature->argumentCount(), WTFMove(continuation), ControlBlock { });
}
static ControlType if_(BlockSignature signature, unsigned stackSize, Ref<Label>&& alternate, RefPtr<Label>&& continuation)
{
return ControlType(signature, stackSize - signature.m_signature->argumentCount(), WTFMove(continuation), ControlIf { WTFMove(alternate) });
}
static ControlType createTry(BlockSignature signature, unsigned stackSize, Ref<Label>&& tryLabel, RefPtr<Label>&& continuation, unsigned tryDepth)
{
return ControlType(signature, stackSize - signature.m_signature->argumentCount(), WTFMove(continuation), ControlTry { WTFMove(tryLabel), tryDepth });
}
static ControlType createTryTable(BlockSignature signature, unsigned stackSize, Ref<Label>&& tryLabel, RefPtr<Label>&& continuation, unsigned tryDepth, ControlTryTable::TargetList&& targets)
{
return ControlType(signature, stackSize - signature.m_signature->argumentCount(), WTFMove(continuation), ControlTryTable { WTFMove(tryLabel), tryDepth, WTFMove(targets) });
}
static bool isLoop(const ControlType& control) { return std::holds_alternative<ControlLoop>(control); }
static bool isTopLevel(const ControlType& control) { return std::holds_alternative<ControlTopLevel>(control); }
static bool isBlock(const ControlType& control) { return std::holds_alternative<ControlBlock>(control); }
static bool isIf(const ControlType& control) { return std::holds_alternative<ControlIf>(control); }
static bool isTry(const ControlType& control) { return std::holds_alternative<ControlTry>(control); }
static bool isTryTable(const ControlType& control) { return std::holds_alternative<ControlTryTable>(control); }
static bool isAnyCatch(const ControlType& control) { return std::holds_alternative<ControlCatch>(control); }
static bool isCatch(const ControlType& control)
{
if (!isAnyCatch(control))
return false;
ControlCatch catchData = std::get<ControlCatch>(control);
return catchData.m_kind == CatchKind::Catch;
}
static bool isCatchAll(const ControlType& control)
{
if (!isAnyCatch(control))
return false;
ControlCatch catchData = std::get<ControlCatch>(control);
return catchData.m_kind == CatchKind::CatchAll;
}
unsigned stackSize() const { return m_stackSize; }
BlockSignature signature() const { return m_signature; }
RefPtr<Label> targetLabelForBranch() const
{
if (isLoop(*this))
return std::get<ControlLoop>(*this).m_body.ptr();
return m_continuation;
}
FunctionArgCount branchTargetArity() const
{
if (isLoop(*this))
return m_signature.m_signature->argumentCount();
return m_signature.m_signature->returnCount();
}
Type branchTargetType(unsigned i) const
{
ASSERT(i < branchTargetArity());
if (isLoop(*this))
return m_signature.m_signature->argumentType(i);
return m_signature.m_signature->returnType(i);
}
void dump(PrintStream& out) const
{
if (isIf(*this))
out.print("If: ");
else if (isBlock(*this))
out.print("Block: ");
else if (isLoop(*this))
out.print("Loop: ");
else if (isTopLevel(*this))
out.print("TopLevel: ");
else if (isTry(*this))
out.print("Try: ");
else if (isCatch(*this))
out.print("Catch: ");
else if (isCatchAll(*this))
out.print("CatchAll: ");
out.print("stackSize:(", stackSize(), ") ");
}
void convertToCatch(Ref<Label> tryEnd, VirtualRegister exception)
{
ASSERT(isTry(*this));
auto& tryData = std::get<ControlTry>(*this);
auto tryStart = WTFMove(tryData.m_try);
auto tryDepth = WTFMove(tryData.m_tryDepth);
auto continuation = WTFMove(m_continuation);
*this = ControlType(m_signature, m_stackSize, WTFMove(continuation), ControlCatch { CatchKind::Catch, WTFMove(tryStart), WTFMove(tryEnd), WTFMove(tryDepth), exception });
}
BlockSignature m_signature;
unsigned m_stackSize;
RefPtr<Label> m_continuation;
private:
template<typename T>
ControlType(BlockSignature signature, unsigned stackSize, RefPtr<Label>&& continuation, T&& t)
: Base(std::forward<T>(t))
, m_signature(signature)
, m_stackSize(stackSize)
, m_continuation(WTFMove(continuation))
{
}
};
using ErrorType = String;
using PartialResult = Expected<void, ErrorType>;
using UnexpectedResult = Unexpected<ErrorType>;
using ControlEntry = FunctionParser<LLIntGenerator>::ControlEntry;
using ControlStack = FunctionParser<LLIntGenerator>::ControlStack;
using ResultList = FunctionParser<LLIntGenerator>::ResultList;
using ArgumentList = FunctionParser<LLIntGenerator>::ResultList;
using Stack = FunctionParser<LLIntGenerator>::Stack;
using TypedExpression = FunctionParser<LLIntGenerator>::TypedExpression;
using CatchHandler = FunctionParser<LLIntGenerator>::CatchHandler;
static ExpressionType emptyExpression() { return { }; };
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)...));
}
LLIntGenerator(ModuleInformation&, FunctionCodeIndex functionIndex, const TypeDefinition&);
std::unique_ptr<FunctionCodeBlockGenerator> finalize();
template<typename ExpressionListA, typename ExpressionListB>
void unifyValuesWithBlock(const ExpressionListA& destinations, const ExpressionListB& values)
{
ASSERT(destinations.size() <= values.size());
auto offset = values.size() - destinations.size();
for (size_t i = 0; i < destinations.size(); ++i) {
auto& src = values[offset + i];
auto& dst = destinations[i];
if (static_cast<VirtualRegister>(src) != static_cast<VirtualRegister>(dst))
WasmMov::emit(this, dst, src);
}
}
enum NoConsistencyCheckTag { NoConsistencyCheck };
ExpressionType push(NoConsistencyCheckTag)
{
m_maxStackSize = std::max(m_maxStackSize, ++m_stackSize);
return virtualRegisterForLocal(m_stackSize - 1);
}
ExpressionType push()
{
checkConsistency();
return push(NoConsistencyCheck);
}
void didPopValueFromStack(ExpressionType, ASCIILiteral) { --m_stackSize; }
bool usesSIMD() { return m_usesSIMD; }
void notifyFunctionUsesSIMD() { ASSERT(Options::useWasmSIMD()); m_usesSIMD = true; }
PartialResult WARN_UNUSED_RETURN addDrop(ExpressionType);
PartialResult WARN_UNUSED_RETURN addArguments(const TypeDefinition&);
PartialResult WARN_UNUSED_RETURN addLocal(Type, uint32_t);
ExpressionType addConstant(Type, int64_t);
ExpressionType addConstantWithoutPush(Type, int64_t);
// References
PartialResult WARN_UNUSED_RETURN addRefIsNull(ExpressionType value, ExpressionType& result);
PartialResult WARN_UNUSED_RETURN addRefFunc(FunctionSpaceIndex index, ExpressionType& result);
PartialResult WARN_UNUSED_RETURN addRefAsNonNull(ExpressionType, ExpressionType&);
PartialResult WARN_UNUSED_RETURN addRefEq(ExpressionType, ExpressionType, ExpressionType&);
// Tables
PartialResult WARN_UNUSED_RETURN addTableGet(unsigned, ExpressionType index, ExpressionType& result);
PartialResult WARN_UNUSED_RETURN addTableSet(unsigned, ExpressionType index, ExpressionType value);
PartialResult WARN_UNUSED_RETURN addTableInit(unsigned, unsigned, ExpressionType dstOffset, ExpressionType srcOffset, ExpressionType length);
PartialResult WARN_UNUSED_RETURN addElemDrop(unsigned);
PartialResult WARN_UNUSED_RETURN addTableSize(unsigned, ExpressionType& result);
PartialResult WARN_UNUSED_RETURN addTableGrow(unsigned, ExpressionType fill, ExpressionType delta, ExpressionType& result);
PartialResult WARN_UNUSED_RETURN addTableFill(unsigned, ExpressionType offset, ExpressionType fill, ExpressionType count);
PartialResult WARN_UNUSED_RETURN addTableCopy(unsigned, unsigned, ExpressionType dstOffset, ExpressionType srcOffset, ExpressionType length);
// Locals
PartialResult WARN_UNUSED_RETURN getLocal(uint32_t index, ExpressionType& result);
PartialResult WARN_UNUSED_RETURN setLocal(uint32_t index, ExpressionType value);
PartialResult WARN_UNUSED_RETURN teeLocal(uint32_t, ExpressionType, ExpressionType& result);
// Globals
PartialResult WARN_UNUSED_RETURN getGlobal(uint32_t index, ExpressionType& result);
PartialResult WARN_UNUSED_RETURN setGlobal(uint32_t index, ExpressionType value);
// Memory
PartialResult WARN_UNUSED_RETURN load(LoadOpType, ExpressionType pointer, ExpressionType& result, uint32_t offset);
PartialResult WARN_UNUSED_RETURN store(StoreOpType, ExpressionType pointer, ExpressionType value, uint32_t offset);
PartialResult WARN_UNUSED_RETURN addGrowMemory(ExpressionType delta, ExpressionType& result);
PartialResult WARN_UNUSED_RETURN addCurrentMemory(ExpressionType& result);
PartialResult WARN_UNUSED_RETURN addMemoryFill(ExpressionType dstAddress, ExpressionType targetValue, ExpressionType count);
PartialResult WARN_UNUSED_RETURN addMemoryCopy(ExpressionType dstAddress, ExpressionType srcAddress, ExpressionType count);
PartialResult WARN_UNUSED_RETURN addMemoryInit(unsigned, ExpressionType dstAddress, ExpressionType srcAddress, ExpressionType length);
PartialResult WARN_UNUSED_RETURN addDataDrop(unsigned);
// Atomics
PartialResult WARN_UNUSED_RETURN atomicLoad(ExtAtomicOpType, Type, ExpressionType pointer, ExpressionType& result, uint32_t offset);
PartialResult WARN_UNUSED_RETURN atomicStore(ExtAtomicOpType, Type, ExpressionType pointer, ExpressionType value, uint32_t offset);
PartialResult WARN_UNUSED_RETURN atomicBinaryRMW(ExtAtomicOpType, Type, ExpressionType pointer, ExpressionType value, ExpressionType& result, uint32_t offset);
PartialResult WARN_UNUSED_RETURN atomicCompareExchange(ExtAtomicOpType, Type, ExpressionType pointer, ExpressionType expected, ExpressionType value, ExpressionType& result, uint32_t offset);
PartialResult WARN_UNUSED_RETURN atomicWait(ExtAtomicOpType, ExpressionType pointer, ExpressionType value, ExpressionType timeout, ExpressionType& result, uint32_t offset);
PartialResult WARN_UNUSED_RETURN atomicNotify(ExtAtomicOpType, ExpressionType pointer, ExpressionType value, ExpressionType& result, uint32_t offset);
PartialResult WARN_UNUSED_RETURN atomicFence(ExtAtomicOpType, uint8_t flags);
// Saturated truncation.
PartialResult WARN_UNUSED_RETURN truncSaturated(Ext1OpType, ExpressionType operand, ExpressionType& result, Type, Type);
// GC
PartialResult WARN_UNUSED_RETURN addRefI31(ExpressionType value, ExpressionType& result);
PartialResult WARN_UNUSED_RETURN addI31GetS(ExpressionType ref, ExpressionType& result);
PartialResult WARN_UNUSED_RETURN addI31GetU(ExpressionType ref, ExpressionType& result);
PartialResult WARN_UNUSED_RETURN addArrayNew(uint32_t index, ExpressionType size, ExpressionType value, ExpressionType& result);
PartialResult WARN_UNUSED_RETURN addArrayNewDefault(uint32_t index, ExpressionType size, ExpressionType& result);
PartialResult WARN_UNUSED_RETURN addArrayNewFixed(uint32_t index, ArgumentList& args, ExpressionType& result);
PartialResult WARN_UNUSED_RETURN addArrayGet(ExtGCOpType arrayGetKind, uint32_t typeIndex, ExpressionType arrayref, ExpressionType index, ExpressionType& result);
PartialResult WARN_UNUSED_RETURN addArrayNewData(uint32_t typeIndex, uint32_t dataIndex, ExpressionType size, ExpressionType offset, ExpressionType& result);
PartialResult WARN_UNUSED_RETURN addArrayNewElem(uint32_t typeIndex, uint32_t elemSegmentIndex, ExpressionType size, ExpressionType offset, ExpressionType& result);
PartialResult WARN_UNUSED_RETURN addArraySet(uint32_t typeIndex, ExpressionType arrayref, ExpressionType index, ExpressionType value);
PartialResult WARN_UNUSED_RETURN addArrayLen(ExpressionType arrayref, ExpressionType& result);
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 index, ArgumentList& args, ExpressionType& result);
PartialResult WARN_UNUSED_RETURN addStructNewDefault(uint32_t index, ExpressionType& result);
PartialResult WARN_UNUSED_RETURN addStructGet(ExtGCOpType structGetKind, ExpressionType structReference, const StructType&, uint32_t fieldIndex, ExpressionType& result);
PartialResult WARN_UNUSED_RETURN addStructSet(ExpressionType structReference, const StructType&, uint32_t fieldIndex, ExpressionType value);
PartialResult WARN_UNUSED_RETURN addRefTest(ExpressionType reference, bool allowNull, int32_t heapType, bool shouldNegate, ExpressionType& result);
PartialResult WARN_UNUSED_RETURN addRefCast(ExpressionType reference, bool allowNull, int32_t heapType, ExpressionType& result);
PartialResult WARN_UNUSED_RETURN addAnyConvertExtern(ExpressionType reference, ExpressionType& result);
PartialResult WARN_UNUSED_RETURN addExternConvertAny(ExpressionType reference, ExpressionType& result);
// Basic operators
#define X(name, opcode, short, idx, ...) \
PartialResult WARN_UNUSED_RETURN add##name(ExpressionType arg, ExpressionType& result);
FOR_EACH_WASM_UNARY_OP(X)
#undef X
#define X(name, opcode, short, idx, ...) \
PartialResult WARN_UNUSED_RETURN add##name(ExpressionType left, ExpressionType right, ExpressionType& result);
FOR_EACH_WASM_BINARY_OP(X)
#undef X
PartialResult WARN_UNUSED_RETURN addSelect(ExpressionType condition, ExpressionType nonZero, ExpressionType zero, ExpressionType& result);
// Control flow
ControlType WARN_UNUSED_RETURN addTopLevel(BlockSignature);
PartialResult WARN_UNUSED_RETURN addBlock(BlockSignature, Stack& enclosingStack, ControlType& newBlock, Stack& newStack);
PartialResult WARN_UNUSED_RETURN addLoop(BlockSignature, Stack& enclosingStack, ControlType& block, Stack& newStack, uint32_t loopIndex);
PartialResult WARN_UNUSED_RETURN addIf(ExpressionType condition, BlockSignature, Stack& enclosingStack, ControlType& result, Stack& newStack);
PartialResult WARN_UNUSED_RETURN addElse(ControlType&, Stack&);
PartialResult WARN_UNUSED_RETURN addElseToUnreachable(ControlType&);
PartialResult WARN_UNUSED_RETURN addTry(BlockSignature, Stack& enclosingStack, ControlType& result, Stack& newStack);
PartialResult WARN_UNUSED_RETURN addTryTable(BlockSignature, Stack& enclosingStack, const Vector<CatchHandler>& targets, ControlType& result, Stack& newStack);
PartialResult WARN_UNUSED_RETURN addCatch(unsigned exceptionIndex, const TypeDefinition&, Stack&, ControlType&, ResultList&);
PartialResult WARN_UNUSED_RETURN addCatchToUnreachable(unsigned exceptionIndex, 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 exceptionIndex, ArgumentList& args, Stack&);
PartialResult WARN_UNUSED_RETURN addRethrow(unsigned, ControlType&);
PartialResult WARN_UNUSED_RETURN addThrowRef(ExpressionType exception, Stack&);
PartialResult WARN_UNUSED_RETURN addReturn(const ControlType&, Stack& returnValues);
PartialResult WARN_UNUSED_RETURN addBranch(ControlType&, ExpressionType condition, Stack& returnValues);
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 condition, const Vector<ControlType*>& targets, ControlType& defaultTargets, Stack& expressionStack);
PartialResult WARN_UNUSED_RETURN endBlock(ControlEntry&, Stack& expressionStack);
void endTryTable(ControlType&);
PartialResult WARN_UNUSED_RETURN addEndToUnreachable(ControlEntry&, Stack& expressionStack, bool unreachable = true);
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 calleeIndex, const TypeDefinition&, ArgumentList& args, ResultList& results, CallType = CallType::Call);
PartialResult WARN_UNUSED_RETURN addCallIndirect(unsigned tableIndex, const TypeDefinition&, ArgumentList& args, ResultList& results, CallType = CallType::Call);
PartialResult WARN_UNUSED_RETURN addCallRef(const TypeDefinition&, ArgumentList& args, ResultList& results, CallType = CallType::Call);
PartialResult WARN_UNUSED_RETURN addUnreachable();
PartialResult WARN_UNUSED_RETURN addCrash();
ALWAYS_INLINE void willParseOpcode() { }
ALWAYS_INLINE void willParseExtendedOpcode() { }
ALWAYS_INLINE void didParseOpcode() { }
void didFinishParsingLocals();
void setParser(FunctionParser<LLIntGenerator>* parser) { m_parser = parser; };
// We need this for autogenerated templates used by JS bytecodes.
void setUsesCheckpoints() const { UNREACHABLE_FOR_PLATFORM(); }
void dump(const ControlStack&, const Stack*);
private:
friend GenericLabel<Wasm::GeneratorTraits>;
struct LLIntCallInformation {
unsigned stackOffset;
unsigned numberOfStackArguments;
ResultList arguments;
CompletionHandler<void(ResultList&)> commitResults;
};
LLIntCallInformation callInformationForCaller(const FunctionSignature&);
Vector<VirtualRegister, 2> callInformationForCallee(const FunctionSignature&);
void linkSwitchTargets(Label&, unsigned location);
VirtualRegister virtualRegisterForWasmLocal(uint32_t index)
{
if (index < m_codeBlock->m_numArguments)
return m_normalizedArguments[index];
const auto& callingConvention = wasmCallingConvention();
const uint32_t gprCount = callingConvention.jsrArgs.size();
const uint32_t fprCount = callingConvention.fprArgs.size();
return virtualRegisterForLocal(index - m_codeBlock->m_numArguments + gprCount + fprCount + numberOfLLIntCalleeSaveRegisters + numberOfLLIntInternalRegisters);
}
ExpressionType jsNullConstant()
{
if (UNLIKELY(!m_jsNullConstant.isValid())) {
m_jsNullConstant = VirtualRegister(FirstConstantRegisterIndex + m_codeBlock->m_constants.size());
m_codeBlock->m_constants.append(JSValue::encode(jsNull()));
if (UNLIKELY(Options::dumpGeneratedWasmBytecodes()))
m_codeBlock->m_constantTypes.append(Types::Externref);
}
return m_jsNullConstant;
}
ExpressionType zeroConstant()
{
if (UNLIKELY(!m_zeroConstant.isValid())) {
m_zeroConstant = VirtualRegister(FirstConstantRegisterIndex + m_codeBlock->m_constants.size());
m_codeBlock->m_constants.append(0);
if (UNLIKELY(Options::dumpGeneratedWasmBytecodes()))
m_codeBlock->m_constantTypes.append(Types::I32);
}
return m_zeroConstant;
}
void getDropKeepCount(const ControlType& target, unsigned& startOffset, unsigned& drop, unsigned& keep)
{
startOffset = target.stackSize() + 1;
keep = target.branchTargetArity();
drop = m_stackSize - target.stackSize() - target.branchTargetArity();
}
void dropKeep(Stack& values, const ControlType& target, bool dropValues)
{
unsigned startOffset;
unsigned keep;
unsigned drop;
getDropKeepCount(target, startOffset, drop, keep);
if (dropValues)
values.shrink(keep);
if (!drop)
return;
if (keep)
WasmDropKeep::emit(this, startOffset, drop, keep);
}
template<typename Stack, typename Functor>
void walkExpressionStack(Stack& expressionStack, unsigned stackSize, const Functor& functor)
{
for (unsigned i = expressionStack.size(); i > 0; --i) {
VirtualRegister slot = virtualRegisterForLocal(stackSize - i);
functor(expressionStack[expressionStack.size() - i], slot);
}
}
template<typename Stack, typename Functor>
void walkExpressionStack(Stack& expressionStack, const Functor& functor)
{
walkExpressionStack(expressionStack, m_stackSize, functor);
}
template<typename Functor>
void walkExpressionStack(ControlEntry& entry, const Functor& functor)
{
unsigned stackSize = entry.controlData.stackSize();
walkExpressionStack(entry.enclosedExpressionStack, stackSize, functor);
}
void checkConsistency()
{
#if ASSERT_ENABLED
// The rules for locals and constants in the stack are:
// 1) Locals have to be materialized whenever a control entry is pushed to the control stack (i.e. every time we splitStack)
// NOTE: This is a trade-off so that set_local does not have to walk up the control stack looking for delayed get_locals
// 2) If the control entry is a loop, we also need to materialize constants in the newStack, since those slots will be written
// to from loop back edges
// 3) Both locals and constants have to be materialized before branches, since multiple branches might share the same target,
// we can't make any assumptions about the stack state at that point, so we materialize the stack.
for (ControlEntry& controlEntry : m_parser->controlStack()) {
walkExpressionStack(controlEntry, [&](VirtualRegister expression, VirtualRegister slot) {
ASSERT(expression == slot || expression.isConstant());
});
}
walkExpressionStack(m_parser->expressionStack(), [&](VirtualRegister expression, VirtualRegister slot) {
ASSERT(expression == slot || expression.isConstant() || expression.isArgument() || static_cast<unsigned>(expression.toLocal()) < m_codeBlock->m_numVars);
});
#endif // ASSERT_ENABLED
}
void materializeConstantsAndLocals(Stack& expressionStack, NoConsistencyCheckTag)
{
walkExpressionStack(expressionStack, [&](auto& expression, VirtualRegister slot) {
ASSERT(expression.value() == slot || expression.value().isConstant() || expression.value().isArgument() || static_cast<unsigned>(expression.value().toLocal()) < m_codeBlock->m_numVars);
if (expression.value() == slot)
return;
WasmMov::emit(this, slot, expression);
expression = TypedExpression { expression.type(), slot };
});
}
void materializeConstantsAndLocals(Stack& expressionStack)
{
if (expressionStack.isEmpty())
return;
checkConsistency();
materializeConstantsAndLocals(expressionStack, NoConsistencyCheck);
checkConsistency();
}
void splitStack(BlockSignature signature, Stack& enclosingStack, Stack& newStack)
{
JSC::Wasm::splitStack(signature, enclosingStack, newStack);
m_stackSize -= newStack.size();
checkConsistency();
walkExpressionStack(enclosingStack, [&](TypedExpression& expression, VirtualRegister slot) {
ASSERT(expression.value() == slot || expression.value().isConstant() || expression.value().isArgument() || static_cast<unsigned>(expression.value().toLocal()) < m_codeBlock->m_numVars);
if (expression.value() == slot || expression.value().isConstant())
return;
WasmMov::emit(this, slot, expression);
expression = TypedExpression { expression.type(), slot };
});
checkConsistency();
m_stackSize += newStack.size();
}
void finalizePreviousBlockForCatch(ControlType&, Stack&);
void addCallBuiltin(LLIntBuiltin, const ArgumentList args, ResultList& results);
struct SwitchEntry {
WasmInstructionStream::Offset offset;
int* jumpTarget;
};
struct CatchEntry {
unsigned tryStart;
unsigned tryEnd;
CatchKind kind;
unsigned m_tryDepth;
unsigned exceptionIndex;
};
struct ConstantMapHashTraits : HashTraits<EncodedJSValue> {
static constexpr bool emptyValueIsZero = true;
static void constructDeletedValue(EncodedJSValue& slot) { slot = JSValue::encode(jsNull()); }
static bool isDeletedValue(EncodedJSValue value) { return value == JSValue::encode(jsNull()); }
};
FunctionParser<LLIntGenerator>* m_parser { nullptr };
ModuleInformation& m_info;
const FunctionCodeIndex m_functionIndex;
Vector<VirtualRegister> m_normalizedArguments;
UncheckedKeyHashMap<Label*, Vector<SwitchEntry>> m_switches;
ExpressionType m_jsNullConstant;
ExpressionType m_zeroConstant;
ResultList m_uninitializedLocals;
UncheckedKeyHashMap<EncodedJSValue, VirtualRegister, WTF::IntHash<EncodedJSValue>, ConstantMapHashTraits> m_constantMap;
Vector<VirtualRegister, 2> m_results;
Checked<unsigned> m_stackSize { 0 };
Checked<unsigned> m_maxStackSize { 0 };
Checked<unsigned> m_tryDepth { 0 };
bool m_usesExceptions { false };
bool m_usesAtomics { false };
bool m_usesSIMD { false };
};
Expected<std::unique_ptr<FunctionCodeBlockGenerator>, String> parseAndCompileBytecode(std::span<const uint8_t> function, const TypeDefinition& signature, ModuleInformation& info, FunctionCodeIndex functionIndex)
{
LLIntGenerator llintGenerator(info, functionIndex, signature);
FunctionParser<LLIntGenerator> parser(llintGenerator, function, signature, info);
WASM_FAIL_IF_HELPER_FAILS(parser.parse());
return llintGenerator.finalize();
}
using Buffer = WasmInstructionStream::InstructionBuffer;
static ThreadSpecific<Buffer>* threadSpecificBufferPtr;
static ThreadSpecific<Buffer>& threadSpecificBuffer()
{
static std::once_flag flag;
std::call_once(
flag,
[] () {
threadSpecificBufferPtr = new ThreadSpecific<Buffer>();
});
return *threadSpecificBufferPtr;
}
LLIntGenerator::LLIntGenerator(ModuleInformation& info, FunctionCodeIndex functionIndex, const TypeDefinition&)
: BytecodeGeneratorBase(makeUnique<FunctionCodeBlockGenerator>(functionIndex), 0)
, m_info(info)
, m_functionIndex(functionIndex)
{
m_codeBlock->m_callees = FixedBitVector(m_info.internalFunctionCount());
{
auto& threadSpecific = threadSpecificBuffer();
Buffer buffer = WTFMove(*threadSpecific);
*threadSpecific = Buffer();
m_writer.setInstructionBuffer(WTFMove(buffer));
}
m_maxStackSize = m_stackSize = m_codeBlock->m_numVars = numberOfLLIntCalleeSaveRegisters + numberOfLLIntInternalRegisters;
WasmEnter::emit(this);
}
std::unique_ptr<FunctionCodeBlockGenerator> LLIntGenerator::finalize()
{
RELEASE_ASSERT(m_codeBlock);
size_t numCalleeLocals = WTF::roundUpToMultipleOf(stackAlignmentRegisters(), m_maxStackSize);
m_codeBlock->m_numCalleeLocals = numCalleeLocals;
RELEASE_ASSERT(numCalleeLocals == m_codeBlock->m_numCalleeLocals);
auto& threadSpecific = threadSpecificBuffer();
Buffer usedBuffer;
m_codeBlock->setInstructions(m_writer.finalize(usedBuffer));
size_t oldCapacity = usedBuffer.capacity();
usedBuffer.shrink(0);
RELEASE_ASSERT(usedBuffer.capacity() == oldCapacity);
*threadSpecific = WTFMove(usedBuffer);
return WTFMove(m_codeBlock);
}
// Generated from wasm.json
#include "WasmLLIntGeneratorInlines.h"
auto LLIntGenerator::callInformationForCaller(const FunctionSignature& signature) -> LLIntCallInformation
{
// This function sets up the stack layout for calls. The desired stack layout is:
// FPRn |
// ... v stack-growth towards lower memory
// FPR1
// FPR0
// ---
// GPRn
// ...
// GPR1
// GPR0
// ----
// stackN
// ...
// stack1
// stack0
// ---
// call frame header
// We need to allocate at least space for all GPRs and FPRs.
// Return value stack0 is at stackN - stackReturnValues
const auto initialStackSize = m_stackSize;
const auto& callingConvention = wasmCallingConvention();
const uint32_t gprCount = callingConvention.jsrArgs.size();
const uint32_t fprCount = callingConvention.fprArgs.size();
uint32_t stackResults = callingConvention.numberOfStackResults(signature);
uint32_t stackCountAligned = WTF::roundUpToMultipleOf(stackAlignmentRegisters(), std::max(callingConvention.numberOfStackArguments(signature), stackResults));
uint32_t gprIndex = 0;
uint32_t fprIndex = 0;
uint32_t stackIndex = 0;
m_stackSize = WTF::roundUpToMultipleOf(stackAlignmentRegisters(), m_stackSize.value());
// FIXME: we are allocating the extra space for the argument/return count in order to avoid interference, but we could do better
// NOTE: We increase arg count by 1 for the case of indirect calls
m_stackSize += std::max(signature.argumentCount() + 1, signature.returnCount()) + gprCount + fprCount + stackCountAligned + CallFrame::headerSizeInRegisters + 1;
m_stackSize = WTF::roundUpToMultipleOf(stackAlignmentRegisters(), m_stackSize.value());
if (m_maxStackSize < m_stackSize)
m_maxStackSize = m_stackSize;
ResultList arguments(signature.argumentCount());
ResultList temporaryResults(signature.returnCount());
const unsigned stackOffset = m_stackSize;
const unsigned base = stackOffset - CallFrame::headerSizeInRegisters - 1;
const uint32_t gprLimit = base - stackCountAligned - gprCount;
const uint32_t fprLimit = gprLimit - fprCount;
stackIndex = base;
gprIndex = base - stackCountAligned;
fprIndex = gprIndex - gprCount;
for (uint32_t i = 0; i < signature.argumentCount(); i++) {
switch (signature.argumentType(i).kind) {
case TypeKind::I32:
case TypeKind::I64:
case TypeKind::Exn:
case TypeKind::Externref:
case TypeKind::Funcref:
case TypeKind::RefNull:
case TypeKind::Ref:
if (gprIndex > gprLimit)
arguments[i] = virtualRegisterForLocal(--gprIndex);
else
arguments[i] = virtualRegisterForLocal(--stackIndex);
break;
case TypeKind::F32:
case TypeKind::F64:
case TypeKind::V128:
if (fprIndex > fprLimit)
arguments[i] = virtualRegisterForLocal(--fprIndex);
else
arguments[i] = virtualRegisterForLocal(--stackIndex);
break;
case TypeKind::Void:
case TypeKind::Func:
case TypeKind::Struct:
case TypeKind::Structref:
case TypeKind::Array:
case TypeKind::Arrayref:
case TypeKind::Eqref:
case TypeKind::Anyref:
case TypeKind::Nullexn:
case TypeKind::Nullref:
case TypeKind::Nullfuncref:
case TypeKind::Nullexternref:
case TypeKind::I31ref:
case TypeKind::Rec:
case TypeKind::Sub:
case TypeKind::Subfinal:
RELEASE_ASSERT_NOT_REACHED();
}
}
gprIndex = base - stackCountAligned;
stackIndex = gprIndex + stackResults;
fprIndex = gprIndex - gprCount;
for (uint32_t i = 0; i < signature.returnCount(); i++) {
switch (signature.returnType(i).kind) {
case TypeKind::I32:
case TypeKind::I64:
case TypeKind::Exn:
case TypeKind::Externref:
case TypeKind::Funcref:
case TypeKind::RefNull:
case TypeKind::Ref:
if (gprIndex > gprLimit)
temporaryResults[i] = virtualRegisterForLocal(--gprIndex);
else
temporaryResults[i] = virtualRegisterForLocal(--stackIndex);
break;
case TypeKind::F32:
case TypeKind::F64:
case TypeKind::V128:
if (fprIndex > fprLimit)
temporaryResults[i] = virtualRegisterForLocal(--fprIndex);
else
temporaryResults[i] = virtualRegisterForLocal(--stackIndex);
break;
case TypeKind::Void:
case TypeKind::Func:
case TypeKind::Struct:
case TypeKind::Structref:
case TypeKind::Array:
case TypeKind::Arrayref:
case TypeKind::Eqref:
case TypeKind::Anyref:
case TypeKind::Nullexn:
case TypeKind::Nullref:
case TypeKind::Nullfuncref:
case TypeKind::Nullexternref:
case TypeKind::I31ref:
case TypeKind::Rec:
case TypeKind::Sub:
case TypeKind::Subfinal:
RELEASE_ASSERT_NOT_REACHED();
}
}
m_stackSize = initialStackSize;
auto commitResults = [this, temporaryResults = WTFMove(temporaryResults)](ResultList& results) {
checkConsistency();
for (auto temporaryResult : temporaryResults) {
ExpressionType result = push(NoConsistencyCheck);
WasmMov::emit(this, result, temporaryResult);
results.append(result);
}
};
return LLIntCallInformation { stackOffset, stackCountAligned, WTFMove(arguments), WTFMove(commitResults) };
}
auto LLIntGenerator::callInformationForCallee(const FunctionSignature& signature) -> Vector<VirtualRegister, 2>
{
if (m_results.size())
return m_results;
m_results.reserveInitialCapacity(signature.returnCount());
const auto& callingConvention = wasmCallingConvention();
const uint32_t gprCount = callingConvention.jsrArgs.size();
const uint32_t fprCount = callingConvention.fprArgs.size();
uint32_t gprIndex = 0;
uint32_t fprIndex = gprCount;
const uint32_t maxGPRIndex = gprCount;
const uint32_t maxFPRIndex = maxGPRIndex + fprCount;
uint32_t stackResults = callingConvention.numberOfStackResults(signature);
uint32_t stackCountAligned = WTF::roundUpToMultipleOf(stackAlignmentRegisters(), std::max(callingConvention.numberOfStackArguments(signature), stackResults));
uint32_t stackIndex = 1 + stackCountAligned - stackResults;
for (uint32_t i = 0; i < signature.returnCount(); i++) {
switch (signature.returnType(i).kind) {
case TypeKind::I32:
case TypeKind::I64:
case TypeKind::Exn:
case TypeKind::Externref:
case TypeKind::Funcref:
case TypeKind::RefNull:
case TypeKind::Ref:
if (gprIndex < maxGPRIndex)
m_results.append(virtualRegisterForLocal(numberOfLLIntCalleeSaveRegisters + numberOfLLIntInternalRegisters + gprIndex++));
else
m_results.append(virtualRegisterForArgumentIncludingThis(stackIndex++));
break;
case TypeKind::F32:
case TypeKind::F64:
case TypeKind::V128:
if (fprIndex < maxFPRIndex)
m_results.append(virtualRegisterForLocal(numberOfLLIntCalleeSaveRegisters + numberOfLLIntInternalRegisters + fprIndex++));
else
m_results.append(virtualRegisterForArgumentIncludingThis(stackIndex++));
break;
case TypeKind::Void:
case TypeKind::Func:
case TypeKind::Struct:
case TypeKind::Structref:
case TypeKind::Array:
case TypeKind::Arrayref:
case TypeKind::Eqref:
case TypeKind::Anyref:
case TypeKind::Nullexn:
case TypeKind::Nullref:
case TypeKind::Nullfuncref:
case TypeKind::Nullexternref:
case TypeKind::I31ref:
case TypeKind::Rec:
case TypeKind::Sub:
case TypeKind::Subfinal:
RELEASE_ASSERT_NOT_REACHED();
}
}
return m_results;
}
auto LLIntGenerator::addDrop(ExpressionType) -> PartialResult
{
return { };
}
auto LLIntGenerator::addArguments(const TypeDefinition& signature) -> PartialResult
{
checkConsistency();
m_codeBlock->m_numArguments = signature.as<FunctionSignature>()->argumentCount();
m_normalizedArguments.resize(m_codeBlock->m_numArguments);
const auto& callingConvention = wasmCallingConvention();
const uint32_t gprCount = callingConvention.jsrArgs.size();
const uint32_t fprCount = callingConvention.fprArgs.size();
const uint32_t maxGPRIndex = gprCount;
const uint32_t maxFPRIndex = gprCount + fprCount;
uint32_t gprIndex = 0;
uint32_t fprIndex = maxGPRIndex;
uint32_t stackIndex = 1;
Vector<VirtualRegister, 32> registerArguments(gprCount + fprCount);
for (uint32_t i = 0; i < gprCount + fprCount; i++)
registerArguments[i] = push(NoConsistencyCheck);
const auto addArgument = [&](uint32_t index, uint32_t& count, uint32_t max) {
if (count < max)
m_normalizedArguments[index] = registerArguments[count++];
else
m_normalizedArguments[index] = virtualRegisterForArgumentIncludingThis(stackIndex++);
};
for (uint32_t i = 0; i < signature.as<FunctionSignature>()->argumentCount(); i++) {
switch (signature.as<FunctionSignature>()->argumentType(i).kind) {
case TypeKind::I32:
case TypeKind::I64:
case TypeKind::Exn:
case TypeKind::Externref:
case TypeKind::Funcref:
case TypeKind::RefNull:
case TypeKind::Ref:
addArgument(i, gprIndex, maxGPRIndex);
break;
case TypeKind::F32:
case TypeKind::F64:
case TypeKind::V128:
addArgument(i, fprIndex, maxFPRIndex);
break;
case TypeKind::Void:
case TypeKind::Func:
case TypeKind::Struct:
case TypeKind::Structref:
case TypeKind::Array:
case TypeKind::Arrayref:
case TypeKind::Eqref:
case TypeKind::Anyref:
case TypeKind::Nullexn:
case TypeKind::Nullref:
case TypeKind::Nullfuncref:
case TypeKind::Nullexternref:
case TypeKind::I31ref:
case TypeKind::Rec:
case TypeKind::Sub:
case TypeKind::Subfinal:
RELEASE_ASSERT_NOT_REACHED();
}
}
m_codeBlock->m_numVars += gprCount + fprCount;
return { };
}
auto LLIntGenerator::addLocal(Type type, uint32_t count) -> PartialResult
{
checkConsistency();
m_codeBlock->m_numVars += count;
// All ref-typed locals (funcref, externref, GC types) have to be
// initialized to the JS null value (not 0)
if (isRefType(type)) {
while (count--)
m_uninitializedLocals.append(push(NoConsistencyCheck));
} else
m_stackSize += count;
if (m_maxStackSize < m_stackSize)
m_maxStackSize = m_stackSize;
return { };
}
void LLIntGenerator::didFinishParsingLocals()
{
if (m_uninitializedLocals.isEmpty())
return;
auto null = jsNullConstant();
for (auto local : m_uninitializedLocals)
WasmMov::emit(this, local, null);
m_uninitializedLocals.clear();
}
auto LLIntGenerator::addConstantWithoutPush(Type type, int64_t value) -> ExpressionType
{
if (!value)
return zeroConstant();
if (value == JSValue::encode(jsNull()))
return jsNullConstant();
VirtualRegister source(FirstConstantRegisterIndex + m_codeBlock->m_constants.size());
auto result = m_constantMap.add(value, source);
if (!result.isNewEntry)
return result.iterator->value;
m_codeBlock->m_constants.append(value);
if (UNLIKELY(Options::dumpGeneratedWasmBytecodes()))
m_codeBlock->m_constantTypes.append(type);
return source;
}
auto LLIntGenerator::addConstant(Type type, int64_t value) -> ExpressionType
{
// leave a hole if we need to materialize the constant
push();
return addConstantWithoutPush(type, value);
}
auto LLIntGenerator::getLocal(uint32_t index, ExpressionType& result) -> PartialResult
{
// leave a hole if we need to materialize the local
push();
result = virtualRegisterForWasmLocal(index);
return { };
}
auto LLIntGenerator::setLocal(uint32_t index, ExpressionType value) -> PartialResult
{
VirtualRegister target = virtualRegisterForWasmLocal(index);
// If this local is currently on the stack we need to materialize it, otherwise it'll see the new value instead of the old one
walkExpressionStack(m_parser->expressionStack(), [&](TypedExpression& expression, VirtualRegister slot) {
if (expression.value() != target)
return;
WasmMov::emit(this, slot, expression);
expression = TypedExpression { expression.type(), slot };
});
WasmMov::emit(this, target, value);
return { };
}
auto LLIntGenerator::teeLocal(uint32_t index, ExpressionType value, ExpressionType& result) -> PartialResult
{
{
auto check = setLocal(index, value);
ASSERT_UNUSED(check, check);
}
{
auto check = getLocal(index, result);
ASSERT_UNUSED(check, check);
}
return { };
}
auto LLIntGenerator::getGlobal(uint32_t index, ExpressionType& result) -> PartialResult
{
const Wasm::GlobalInformation& global = m_info.globals[index];
result = push();
switch (global.bindingMode) {
case Wasm::GlobalInformation::BindingMode::EmbeddedInInstance:
WasmGetGlobal::emit(this, result, index);
break;
case Wasm::GlobalInformation::BindingMode::Portable:
WasmGetGlobalPortableBinding::emit(this, result, index);
break;
}
return { };
}
auto LLIntGenerator::setGlobal(uint32_t index, ExpressionType value) -> PartialResult
{
const Wasm::GlobalInformation& global = m_info.globals[index];
Type type = global.type;
switch (global.bindingMode) {
case Wasm::GlobalInformation::BindingMode::EmbeddedInInstance:
if (isRefType(type))
WasmSetGlobalRef::emit(this, index, value);
else
WasmSetGlobal::emit(this, index, value);
break;
case Wasm::GlobalInformation::BindingMode::Portable:
if (isRefType(type))
WasmSetGlobalRefPortableBinding::emit(this, index, value);
else
WasmSetGlobalPortableBinding::emit(this, index, value);
break;
}
return { };
}
auto LLIntGenerator::addLoop(BlockSignature signature, Stack& enclosingStack, ControlType& block, Stack& newStack, uint32_t loopIndex) -> PartialResult
{
splitStack(signature, enclosingStack, newStack);
materializeConstantsAndLocals(newStack, NoConsistencyCheck);
#if ASSERT_ENABLED
// We cannot yet call checkConsistency, since the arguments we are
// materializing for the loop are not neither in the expression
// nor the control stack, and it won't know what to do in this
// intermediate state. As a sanity check just verify that
// everything in newStack is a virtual register that is actually
// pointing to each stack position, which is what we should have
// after we split the stack and the previous call materializes
// constants and aliases if needed.
walkExpressionStack(newStack, [](VirtualRegister expression, VirtualRegister slot) {
ASSERT(expression == slot);
});
#endif
Ref<Label> body = newEmittedLabel();
Ref<Label> continuation = newLabel();
block = ControlType::loop(signature, m_stackSize, WTFMove(body), WTFMove(continuation));
Vector<VirtualRegister> osrEntryData;
for (uint32_t i = 0; i < m_codeBlock->m_numArguments; i++)
osrEntryData.append(m_normalizedArguments[i]);
const auto& callingConvention = wasmCallingConvention();
const uint32_t gprCount = callingConvention.jsrArgs.size();
const uint32_t fprCount = callingConvention.fprArgs.size();
for (uint32_t i = gprCount + fprCount + numberOfLLIntCalleeSaveRegisters + numberOfLLIntInternalRegisters; i < m_codeBlock->m_numVars; i++)
osrEntryData.append(virtualRegisterForLocal(i));
for (unsigned controlIndex = 0; controlIndex < m_parser->controlStack().size(); ++controlIndex) {
ControlType& data = m_parser->controlStack()[controlIndex].controlData;
Stack& expressionStack = m_parser->controlStack()[controlIndex].enclosedExpressionStack;
for (TypedExpression expression : expressionStack)
osrEntryData.append(expression);
if (ControlType::isAnyCatch(data))
osrEntryData.append(std::get<ControlCatch>(data).m_exception);
}
for (TypedExpression expression : enclosingStack)
osrEntryData.append(expression);
for (TypedExpression expression : newStack)
osrEntryData.append(expression);
WasmLoopHint::emit(this);
m_codeBlock->tierUpCounter().add(m_lastInstruction.offset(), LLIntTierUpCounter::OSREntryData { loopIndex, WTFMove(osrEntryData) });
return { };
}
auto LLIntGenerator::addTopLevel(BlockSignature signature) -> ControlType
{
return ControlType::topLevel(signature, m_stackSize, newLabel());
}
auto LLIntGenerator::addBlock(BlockSignature signature, Stack& enclosingStack, ControlType& newBlock, Stack& newStack) -> PartialResult
{
splitStack(signature, enclosingStack, newStack);
newBlock = ControlType::block(signature, m_stackSize, newLabel());
return { };
}
auto LLIntGenerator::addIf(ExpressionType condition, BlockSignature signature, Stack& enclosingStack, ControlType& result, Stack& newStack) -> PartialResult
{
Ref<Label> alternate = newLabel();
Ref<Label> continuation = newLabel();
splitStack(signature, enclosingStack, newStack);
WasmJfalse::emit(this, condition, alternate->bind(this));
result = ControlType::if_(signature, m_stackSize, WTFMove(alternate), WTFMove(continuation));
return { };
}
auto LLIntGenerator::addElse(ControlType& data, Stack& expressionStack) -> PartialResult
{
ASSERT(ControlType::isIf(data));
materializeConstantsAndLocals(expressionStack);
WasmJmp::emit(this, data.m_continuation->bind(this));
return addElseToUnreachable(data);
}
auto LLIntGenerator::addElseToUnreachable(ControlType& data) -> PartialResult
{
m_stackSize = data.stackSize() + data.m_signature.m_signature->argumentCount();
ControlIf& control = std::get<ControlIf>(data);
emitLabel(control.m_alternate.get());
data = ControlType::block(data.m_signature, m_stackSize, WTFMove(data.m_continuation));
return { };
}
auto LLIntGenerator::addTry(BlockSignature signature, Stack& enclosingStack, ControlType& result, Stack& newStack) -> PartialResult
{
m_usesExceptions = true;
++m_tryDepth;
splitStack(signature, enclosingStack, newStack);
Ref<Label> tryLabel = newEmittedLabel();
Ref<Label> continuation = newLabel();
result = ControlType::createTry(signature, m_stackSize, WTFMove(tryLabel), WTFMove(continuation), m_tryDepth);
return { };
}
auto LLIntGenerator::addTryTable(BlockSignature signature, Stack& enclosingStack, const Vector<CatchHandler>& targets, ControlType& result, Stack& newStack) -> PartialResult
{
m_usesExceptions = true;
++m_tryDepth;
splitStack(signature, enclosingStack, newStack);
auto targetList = targets.map(
[&](const auto& target) -> ControlTryTable::TryTableTarget {
auto& entry = m_parser->resolveControlRef(target.target).controlData;
return {
target.type,
target.tag,
target.exceptionSignature,
entry.targetLabelForBranch().get(),
entry.stackSize()
};
}
);
Ref<Label> tryLabel = newEmittedLabel();
Ref<Label> continuation = newLabel();
result = ControlType::createTryTable(signature, m_stackSize, WTFMove(tryLabel), WTFMove(continuation), m_tryDepth, WTFMove(targetList));
return { };
}
void LLIntGenerator::finalizePreviousBlockForCatch(ControlType& data, Stack& expressionStack)
{
if (!ControlType::isAnyCatch(data))
materializeConstantsAndLocals(expressionStack);
else {
checkConsistency();
VirtualRegister dst = virtualRegisterForLocal(data.stackSize());
for (TypedExpression& value : expressionStack) {
WasmMov::emit(this, dst, value);
value = TypedExpression { value.type(), dst };
dst -= 1;
}
}
WasmJmp::emit(this, data.m_continuation->bind(this));
}
auto LLIntGenerator::addCatch(unsigned exceptionIndex, const TypeDefinition& exceptionSignature, Stack& expressionStack, ControlType& data, ResultList& results) -> PartialResult
{
finalizePreviousBlockForCatch(data, expressionStack);
return addCatchToUnreachable(exceptionIndex, exceptionSignature, data, results);
}
auto LLIntGenerator::addCatchToUnreachable(unsigned exceptionIndex, const TypeDefinition& exceptionSignature, ControlType& data, ResultList& results) -> PartialResult
{
m_usesExceptions = true;
Ref<Label> catchLabel = newEmittedLabel();
m_stackSize = data.stackSize();
VirtualRegister exception = push();
if (ControlType::isTry(data))
data.convertToCatch(catchLabel, exception);
for (unsigned i = 0; i < exceptionSignature.as<FunctionSignature>()->argumentCount(); ++i)
results.append(push());
WasmCatch::emit(this, exceptionIndex, exception, exceptionSignature.as<FunctionSignature>()->argumentCount(), results.isEmpty() ? 0 : -results[0].offset());
for (unsigned i = 0; i < exceptionSignature.as<FunctionSignature>()->argumentCount(); ++i) {
VirtualRegister dst = results[i];
Type type = exceptionSignature.as<FunctionSignature>()->argumentType(i);
switch (type.kind) {
case Wasm::TypeKind::F32:
WasmF32ReinterpretI32::emit(this, dst, dst);
break;
case Wasm::TypeKind::F64:
WasmF64ReinterpretI64::emit(this, dst, dst);
break;
case Wasm::TypeKind::I32:
case Wasm::TypeKind::I64:
case Wasm::TypeKind::Externref:
case Wasm::TypeKind::Funcref:
case Wasm::TypeKind::V128:
case Wasm::TypeKind::Ref:
case Wasm::TypeKind::RefNull:
break;
default:
RELEASE_ASSERT_NOT_REACHED();
break;
}
}
ControlCatch& catchData = std::get<ControlCatch>(data);
catchData.m_kind = CatchKind::Catch;
m_codeBlock->addExceptionHandler({ HandlerType::Catch, catchData.m_tryStart->location(), catchData.m_tryEnd->location(), catchLabel->location(), m_tryDepth, exceptionIndex });
return { };
}
auto LLIntGenerator::addCatchAll(Stack& expressionStack, ControlType& data) -> PartialResult
{
finalizePreviousBlockForCatch(data, expressionStack);
WasmJmp::emit(this, data.m_continuation->bind(this));
return addCatchAllToUnreachable(data);
}
auto LLIntGenerator::addCatchAllToUnreachable(ControlType& data) -> PartialResult
{
m_usesExceptions = true;
Ref<Label> catchLabel = newEmittedLabel();
m_stackSize = data.stackSize();
VirtualRegister exception = push();
if (ControlType::isTry(data))
data.convertToCatch(catchLabel, exception);
ControlCatch& catchData = std::get<ControlCatch>(data);
catchData.m_kind = CatchKind::CatchAll;
WasmCatchAll::emit(this, exception);
m_codeBlock->addExceptionHandler({ HandlerType::CatchAll, catchData.m_tryStart->location(), catchData.m_tryEnd->location(), catchLabel->location(), m_tryDepth, 0 });
return { };
}
auto LLIntGenerator::addDelegate(ControlType& target, ControlType& data) -> PartialResult
{
return addDelegateToUnreachable(target, data);
}
auto LLIntGenerator::addDelegateToUnreachable(ControlType& target, ControlType& data) -> PartialResult
{
m_usesExceptions = true;
Ref<Label> delegateLabel = newEmittedLabel();
ASSERT(ControlType::isTry(target) || ControlType::isTopLevel(target));
unsigned targetDepth = ControlType::isTry(target) ? std::get<ControlTry>(target).m_tryDepth : 0;
ControlTry& tryData = std::get<ControlTry>(data);
m_codeBlock->addExceptionHandler({ HandlerType::Delegate, tryData.m_try->location(), delegateLabel->location(), 0, m_tryDepth, targetDepth });
return { };
}
auto LLIntGenerator::addThrow(unsigned exceptionIndex, ArgumentList& args, Stack&) -> PartialResult
{
m_usesExceptions = true;
// We have to materialize the arguments here since it might include constants or
// delayed moves, but the wasm_throw opcode expects all the arguments to be contiguous
// in the stack. The reason we don't call materializeConstantsAndLocals here is that
// it expects a stack, not a vector of ExpressionType arguments.
walkExpressionStack(args, m_stackSize + args.size(), [&](VirtualRegister& arg, VirtualRegister slot) {
if (arg == slot)
return;
WasmMov::emit(this, slot, arg);
arg = slot;
});
WasmThrow::emit(this, exceptionIndex, args.isEmpty() ? virtualRegisterForLocal(0) : args[0]);
return { };
}
auto LLIntGenerator::addRethrow(unsigned, ControlType& data) -> PartialResult
{
m_usesExceptions = true;
ASSERT(ControlType::isAnyCatch(data));
ControlCatch catchData = std::get<ControlCatch>(data);
WasmRethrow::emit(this, catchData.m_exception);
return { };
}
auto LLIntGenerator::addThrowRef(ExpressionType exception, Stack&) -> PartialResult
{
WasmThrowRef::emit(this, exception);
return { };
}
auto LLIntGenerator::addReturn(const ControlType& data, Stack& returnValues) -> PartialResult
{
if (!data.m_signature.m_signature->returnCount()) {
WasmRetVoid::emit(this);
return { };
}
// We should materialize locals when return more than one values, since
// it might clobber arguments before use them (see examples in wasm-tuple-return.js).
if (returnValues.size() > 1)
materializeConstantsAndLocals(returnValues);
// no need to drop keep here, since we have to move anyway
unifyValuesWithBlock(callInformationForCallee(*data.m_signature.m_signature), returnValues);
WasmRet::emit(this);
return { };
}
auto LLIntGenerator::addBranch(ControlType& data, ExpressionType condition, Stack& returnValues) -> PartialResult
{
RefPtr<Label> target = data.targetLabelForBranch();
RefPtr<Label> skip = nullptr;
materializeConstantsAndLocals(returnValues);
if (condition.isValid()) {
skip = newLabel();
WasmJfalse::emit(this, condition, skip->bind(this));
}
dropKeep(returnValues, data, !skip);
WasmJmp::emit(this, target->bind(this));
if (skip)
emitLabel(*skip);
return { };
}
auto LLIntGenerator::addBranchNull(ControlType& data, ExpressionType reference, Stack& returnValues, bool shouldNegate, ExpressionType& result) -> PartialResult
{
checkConsistency();
// Leave a hole for the reference and avoid overwriting it with the condition.
if (!shouldNegate)
push(NoConsistencyCheck);
auto condition = push(NoConsistencyCheck);
WasmRefIsNull::emit(this, condition, reference);
if (shouldNegate)
WasmI32Eqz::emit(this, condition, condition);
// Pop temporary condition variable & reference
m_stackSize -= (1 + (shouldNegate ? 0 : 1));
WASM_FAIL_IF_HELPER_FAILS(addBranch(data, condition, returnValues));
checkConsistency();
if (!shouldNegate) {
result = push(NoConsistencyCheck);
if (reference != result)
WasmMov::emit(this, result, reference);
}
return { };
}
auto LLIntGenerator::addBranchCast(ControlType& data, ExpressionType reference, Stack& returnValues, bool allowNull, int32_t heapType, bool shouldNegate) -> PartialResult
{
ResultList results;
addCallBuiltin(LLIntBuiltin::RefTest, { reference, addConstantWithoutPush(Types::I32, static_cast<uint32_t>(allowNull)), addConstantWithoutPush(Types::I32, heapType), addConstantWithoutPush(Types::I32, static_cast<uint32_t>(shouldNegate)) }, results);
ASSERT(results.size() == 1);
auto condition = results.at(0);
// Pop temporary condition variable.
--m_stackSize;
WASM_FAIL_IF_HELPER_FAILS(addBranch(data, condition, returnValues));
return { };
}
auto LLIntGenerator::addSwitch(ExpressionType condition, const Vector<ControlType*>& targets, ControlType& defaultTarget, Stack& expressionStack) -> PartialResult
{
materializeConstantsAndLocals(expressionStack);
unsigned tableIndex = m_codeBlock->numberOfJumpTables();
auto& jumpTable = m_codeBlock->addJumpTable(targets.size() + 1);
WasmSwitch::emit(this, condition, tableIndex);
unsigned index = 0;
WasmInstructionStream::Offset offset = m_lastInstruction.offset();
auto addTarget = [&](ControlType& target) {
RefPtr<Label> targetLabel = target.targetLabelForBranch();
getDropKeepCount(target, jumpTable[index].startOffset, jumpTable[index].dropCount, jumpTable[index].keepCount);
if (targetLabel->isForward()) {
auto result = m_switches.add(targetLabel.get(), Vector<SwitchEntry>());
ASSERT(!jumpTable[index].target);
result.iterator->value.append(SwitchEntry { offset, &jumpTable[index++].target });
} else {
int jumpTarget = targetLabel->location() - offset;
ASSERT(jumpTarget);
jumpTable[index++].target = jumpTarget;
}
};
for (const auto& target : targets)
addTarget(*target);
addTarget(defaultTarget);
return { };
}
auto LLIntGenerator::endBlock(ControlEntry& entry, Stack& expressionStack) -> PartialResult
{
// FIXME: We only need to materialize constants here if there exists a jump to this label
// https://bugs.webkit.org/show_bug.cgi?id=203657
finalizePreviousBlockForCatch(entry.controlData, expressionStack);
return addEndToUnreachable(entry, expressionStack, false);
}
void LLIntGenerator::endTryTable(ControlType& data)
{
auto tryTable = std::get<ControlTryTable>(data);
auto targets = tryTable.targets;
unsigned tryEnd = m_lastInstruction.offset() + m_lastInstruction->size();
auto end = newLabel();
// jump past all handlers
WasmJmp::emit(this, end->bind(this));
auto oldStackSize = m_stackSize;
for (auto& target : targets) {
// Set up the catch handler here
auto targetLabel = target.target;
m_stackSize = target.targetStackSize;
ResultList results;
if (target.type == CatchKind::Catch || target.type == CatchKind::CatchRef) {
auto signature = target.exceptionSignature->as<FunctionSignature>();
results.reserveInitialCapacity(signature->argumentCount());
for (unsigned i = 0; i < signature->argumentCount(); ++i)
results.append(virtualRegisterForLocal(m_stackSize + i));
}
alignWideOpcode32();
RefPtr<Label> handlerLabel = newEmittedLabel();
switch (target.type) {
case CatchKind::Catch: {
WasmTryTableCatch::emit(this, static_cast<unsigned>(target.type), target.tag, virtualRegisterForLocal(0), target.exceptionSignature->as<FunctionSignature>()->argumentCount(), results.isEmpty() ? 0 : -results[0].offset());
break;
}
case CatchKind::CatchRef: {
VirtualRegister exception = virtualRegisterForLocal(m_stackSize + target.exceptionSignature->as<FunctionSignature>()->argumentCount());
WasmTryTableCatch::emit(this, static_cast<unsigned>(target.type), target.tag, exception, target.exceptionSignature->as<FunctionSignature>()->argumentCount(), results.isEmpty() ? 0 : -results[0].offset());
break;
}
case CatchKind::CatchAll: {
WasmTryTableCatch::emit(this, static_cast<unsigned>(target.type), target.tag, virtualRegisterForLocal(0), 0, 0);
break;
}
case CatchKind::CatchAllRef: {
VirtualRegister exception = virtualRegisterForLocal(m_stackSize);
WasmTryTableCatch::emit(this, static_cast<unsigned>(target.type), target.tag, exception, 0, 0);
break;
}
}
// finish the thunk with a jump
WasmJmp::emit(this, targetLabel->bind(this));
HandlerType handlerType;
switch (target.type) {
case CatchKind::Catch:
handlerType = HandlerType::TryTableCatch;
break;
case CatchKind::CatchRef:
handlerType = HandlerType::TryTableCatchRef;
break;
case CatchKind::CatchAll:
handlerType = HandlerType::TryTableCatchAll;
break;
case CatchKind::CatchAllRef:
handlerType = HandlerType::TryTableCatchAllRef;
break;
}
m_codeBlock->addExceptionHandler({ handlerType, tryTable.m_try.get().location(), tryEnd, handlerLabel->location(), m_tryDepth + 1, target.tag });
// reset for the next handler
m_stackSize = oldStackSize;
}
emitLabel(end);
}
auto LLIntGenerator::addEndToUnreachable(ControlEntry& entry, Stack& expressionStack, bool unreachable) -> PartialResult
{
ControlType& data = entry.controlData;
unsigned stackSize = data.stackSize();
if (ControlType::isAnyCatch(entry.controlData))
++stackSize; // Account for the caught exception
RELEASE_ASSERT(unreachable || m_stackSize == stackSize + data.m_signature.m_signature->returnCount());
if (ControlType::isTry(data) || ControlType::isTryTable(data) || ControlType::isAnyCatch(data))
--m_tryDepth;
if (ControlType::isTryTable(data))
endTryTable(data);
m_stackSize = data.stackSize();
for (unsigned i = 0; i < data.m_signature.m_signature->returnCount(); ++i) {
// We don't want to do a consistency check here because we just reset the stack size
// are pushing new values, while we already have the same values in the stack.
// The only reason we do things this way is so that it also works for unreachable blocks,
// since they might not have the right number of values in the expression stack.
// Instead, we do a stricter consistency check below.
auto tmp = push(NoConsistencyCheck);
ASSERT(unreachable || tmp == expressionStack[i].value());
if (unreachable)
entry.enclosedExpressionStack.constructAndAppend(data.m_signature.m_signature->returnType(i), tmp);
else
entry.enclosedExpressionStack.append(expressionStack[i]);
}
if (m_lastOpcodeID == wasm_jmp && data.m_continuation->unresolvedJumps().size() == 1 && data.m_continuation->unresolvedJumps()[0] == static_cast<int>(m_lastInstruction.offset())) {
linkSwitchTargets(*data.m_continuation, m_lastInstruction.offset());
m_lastOpcodeID = wasm_unreachable;
m_writer.rewind(m_lastInstruction);
} else
emitLabel(*data.m_continuation);
return { };
}
auto LLIntGenerator::endTopLevel(BlockSignature signature, const Stack& expressionStack) -> PartialResult
{
RELEASE_ASSERT(expressionStack.size() == signature.m_signature->returnCount());
if (m_usesSIMD)
m_info.markUsesSIMD(m_functionIndex);
if (m_usesExceptions)
m_info.markUsesExceptions(m_functionIndex);
if (m_usesAtomics)
m_info.markUsesAtomics(m_functionIndex);
m_info.doneSeeingFunction(m_functionIndex);
if (!signature.m_signature->returnCount()) {
WasmRetVoid::emit(this);
return { };
}
checkConsistency();
unifyValuesWithBlock(callInformationForCallee(*signature.m_signature), expressionStack);
WasmRet::emit(this);
return { };
}
auto LLIntGenerator::addCall(FunctionSpaceIndex functionIndex, const TypeDefinition& signature, ArgumentList& args, ResultList& results, CallType callType) -> PartialResult
{
bool isTailCall = callType == CallType::TailCall;
ASSERT(callType == CallType::Call || isTailCall);
ASSERT(signature.as<FunctionSignature>()->argumentCount() == args.size());
LLIntCallInformation wasmCalleeInfo = callInformationForCaller(*signature.as<FunctionSignature>());
if (!m_info.isImportedFunctionFromFunctionIndexSpace(functionIndex))
m_codeBlock->m_callees.testAndSet(functionIndex - m_info.importFunctionCount());
unifyValuesWithBlock(wasmCalleeInfo.arguments, args);
if (isTailCall) {
m_codeBlock->setTailCall(functionIndex, m_info.isImportedFunctionFromFunctionIndexSpace(functionIndex));
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>()));
WasmTailCall::emit(this, functionIndex, wasmCalleeInfo.stackOffset, wasmCalleeInfo.numberOfStackArguments, callerStackArgs);
} else
WasmCall::emit(this, functionIndex, wasmCalleeInfo.stackOffset, wasmCalleeInfo.numberOfStackArguments);
wasmCalleeInfo.commitResults(results);
return { };
}
auto LLIntGenerator::addCallIndirect(unsigned tableIndex, const TypeDefinition& signature, ArgumentList& args, ResultList& results, CallType callType) -> PartialResult
{
bool isTailCall = callType == CallType::TailCall;
ASSERT(callType == CallType::Call || isTailCall);
ExpressionType calleeIndex = args.takeLast();
const auto& functionSignature = *signature.expand().as<FunctionSignature>();
ASSERT(functionSignature.argumentCount() == args.size());
ASSERT(m_info.tableCount() > tableIndex);
ASSERT(m_info.tables[tableIndex].type() == TableElementType::Funcref);
LLIntCallInformation calleeInfo = callInformationForCaller(functionSignature);
unifyValuesWithBlock(calleeInfo.arguments, args);
if (isTailCall) {
m_codeBlock->setTailCallClobbersInstance();
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>()));
WasmTailCallIndirect::emit(this, calleeIndex, m_codeBlock->addSignature(signature), calleeInfo.stackOffset, calleeInfo.numberOfStackArguments, callerStackArgs, tableIndex);
} else
WasmCallIndirect::emit(this, calleeIndex, m_codeBlock->addSignature(signature), calleeInfo.stackOffset, calleeInfo.numberOfStackArguments, tableIndex);
calleeInfo.commitResults(results);
return { };
}
auto LLIntGenerator::addCallRef(const TypeDefinition& signature, ArgumentList& args, ResultList& results, CallType callType) -> PartialResult
{
bool isTailCall = callType == CallType::TailCall;
ASSERT(callType == CallType::Call || isTailCall);
ExpressionType callee = args.takeLast();
const auto& functionSignature = *signature.expand().as<FunctionSignature>();
LLIntCallInformation info = callInformationForCaller(functionSignature);
unifyValuesWithBlock(info.arguments, args);
if (isTailCall) {
m_codeBlock->setTailCallClobbersInstance();
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>()));
WasmTailCallRef::emit(this, callee, m_codeBlock->addSignature(signature), info.stackOffset, info.numberOfStackArguments, callerStackArgs);
} else
WasmCallRef::emit(this, callee, m_codeBlock->addSignature(signature), info.stackOffset, info.numberOfStackArguments);
info.commitResults(results);
return { };
}
auto LLIntGenerator::addRefIsNull(ExpressionType value, ExpressionType& result) -> PartialResult
{
result = push();
WasmRefIsNull::emit(this, result, value);
return { };
}
auto LLIntGenerator::addRefFunc(FunctionSpaceIndex index, ExpressionType& result) -> PartialResult
{
result = push();
WasmRefFunc::emit(this, result, index);
return { };
}
auto LLIntGenerator::addRefAsNonNull(ExpressionType reference, ExpressionType& result) -> PartialResult
{
result = push();
WasmRefAsNonNull::emit(this, result, reference);
return { };
}
auto LLIntGenerator::addRefEq(ExpressionType ref0, ExpressionType ref1, ExpressionType& result) -> PartialResult
{
return addI64Eq(ref0, ref1, result);
}
auto LLIntGenerator::addTableGet(unsigned tableIndex, ExpressionType index, ExpressionType& result) -> PartialResult
{
result = push();
WasmTableGet::emit(this, result, index, tableIndex);
return { };
}
auto LLIntGenerator::addTableSet(unsigned tableIndex, ExpressionType index, ExpressionType value) -> PartialResult
{
WasmTableSet::emit(this, index, value, tableIndex);
return { };
}
auto LLIntGenerator::addTableInit(unsigned elementIndex, unsigned tableIndex, ExpressionType dstOffset, ExpressionType srcOffset, ExpressionType length) -> PartialResult
{
WasmTableInit::emit(this, dstOffset, srcOffset, length, elementIndex, tableIndex);
return { };
}
auto LLIntGenerator::addElemDrop(unsigned elementIndex) -> PartialResult
{
ResultList results;
addCallBuiltin(LLIntBuiltin::ElemDrop, { addConstantWithoutPush(Types::I32, elementIndex) }, results);
return { };
}
auto LLIntGenerator::addTableSize(unsigned tableIndex, ExpressionType& result) -> PartialResult
{
ResultList results;
addCallBuiltin(LLIntBuiltin::TableSize, { addConstantWithoutPush(Types::I32, tableIndex) }, results);
result = results.at(0);
return { };
}
auto LLIntGenerator::addTableGrow(unsigned tableIndex, ExpressionType fill, ExpressionType delta, ExpressionType& result) -> PartialResult
{
result = push();
WasmTableGrow::emit(this, result, fill, delta, tableIndex);
return { };
}
auto LLIntGenerator::addTableFill(unsigned tableIndex, ExpressionType offset, ExpressionType fill, ExpressionType count) -> PartialResult
{
WasmTableFill::emit(this, offset, fill, count, tableIndex);
return { };
}
auto LLIntGenerator::addTableCopy(unsigned dstTableIndex, unsigned srcTableIndex, ExpressionType dstOffset, ExpressionType srcOffset, ExpressionType length) -> PartialResult
{
ResultList results;
addCallBuiltin(LLIntBuiltin::TableCopy, { dstOffset, srcOffset, length, addConstantWithoutPush(Types::I32, dstTableIndex), addConstantWithoutPush(Types::I32, srcTableIndex) }, results);
return { };
}
auto LLIntGenerator::addUnreachable() -> PartialResult
{
WasmUnreachable::emit(this);
return { };
}
auto LLIntGenerator::addCrash() -> PartialResult
{
WasmUnreachable::emit(this);
return { };
}
void LLIntGenerator::addCallBuiltin(LLIntBuiltin builtin, const ArgumentList args, ResultList& results)
{
const TypeDefinition& signature = TypeInformation::signatureForLLIntBuiltin(builtin);
ASSERT(signature.as<FunctionSignature>()->argumentCount() == args.size());
LLIntCallInformation info = callInformationForCaller(*signature.as<FunctionSignature>());
unifyValuesWithBlock(info.arguments, args);
WasmCallBuiltin::emit(this, static_cast<uint32_t>(builtin), info.stackOffset, info.numberOfStackArguments);
info.commitResults(results);
ASSERT(signature.as<FunctionSignature>()->returnCount() == results.size());
}
auto LLIntGenerator::addCurrentMemory(ExpressionType& result) -> PartialResult
{
ResultList results;
addCallBuiltin(LLIntBuiltin::CurrentMemory, { }, results);
result = results.at(0);
return { };
}
auto LLIntGenerator::addMemoryInit(unsigned dataSegmentIndex, ExpressionType dstAddress, ExpressionType srcAddress, ExpressionType length) -> PartialResult
{
ResultList results;
addCallBuiltin(LLIntBuiltin::MemoryInit, { dstAddress, srcAddress, length, addConstantWithoutPush(Types::I32, dataSegmentIndex) }, results);
return { };
}
auto LLIntGenerator::addDataDrop(unsigned dataSegmentIndex) -> PartialResult
{
ResultList results;
addCallBuiltin(LLIntBuiltin::DataDrop, { addConstantWithoutPush(Types::I32, dataSegmentIndex) }, results);
return { };
}
auto LLIntGenerator::addGrowMemory(ExpressionType delta, ExpressionType& result) -> PartialResult
{
result = push();
WasmGrowMemory::emit(this, result, delta);
return { };
}
auto LLIntGenerator::addMemoryFill(ExpressionType dstAddress, ExpressionType targetValue, ExpressionType count) -> PartialResult
{
ResultList results;
addCallBuiltin(LLIntBuiltin::MemoryFill, { dstAddress, targetValue, count }, results);
return { };
}
auto LLIntGenerator::addMemoryCopy(ExpressionType dstAddress, ExpressionType srcAddress, ExpressionType count) -> PartialResult
{
ResultList results;
addCallBuiltin(LLIntBuiltin::MemoryCopy, { dstAddress, srcAddress, count }, results);
return { };
}
auto LLIntGenerator::addSelect(ExpressionType condition, ExpressionType nonZero, ExpressionType zero, ExpressionType& result) -> PartialResult
{
result = push();
WasmSelect::emit(this, result, condition, nonZero, zero);
return { };
}
auto LLIntGenerator::load(LoadOpType op, ExpressionType pointer, ExpressionType& result, uint32_t offset) -> PartialResult
{
result = push();
switch (op) {
case LoadOpType::I32Load8S:
WasmI32Load8S::emit(this, result, pointer, offset);
break;
case LoadOpType::I64Load8S:
WasmI64Load8S::emit(this, result, pointer, offset);
break;
case LoadOpType::I32Load8U:
case LoadOpType::I64Load8U:
WasmLoad8U::emit(this, result, pointer, offset);
break;
case LoadOpType::I32Load16S:
WasmI32Load16S::emit(this, result, pointer, offset);
break;
case LoadOpType::I64Load16S:
WasmI64Load16S::emit(this, result, pointer, offset);
break;
case LoadOpType::I32Load16U:
case LoadOpType::I64Load16U:
WasmLoad16U::emit(this, result, pointer, offset);
break;
case LoadOpType::I32Load:
case LoadOpType::F32Load:
case LoadOpType::I64Load32U:
WasmLoad32U::emit(this, result, pointer, offset);
break;
case LoadOpType::I64Load32S:
WasmI64Load32S::emit(this, result, pointer, offset);
break;
case LoadOpType::I64Load:
case LoadOpType::F64Load:
WasmLoad64U::emit(this, result, pointer, offset);
break;
}
return { };
}
auto LLIntGenerator::store(StoreOpType op, ExpressionType pointer, ExpressionType value, uint32_t offset) -> PartialResult
{
switch (op) {
case StoreOpType::I64Store8:
case StoreOpType::I32Store8:
WasmStore8::emit(this, pointer, value, offset);
break;
case StoreOpType::I64Store16:
case StoreOpType::I32Store16:
WasmStore16::emit(this, pointer, value, offset);
break;
case StoreOpType::I64Store32:
case StoreOpType::I32Store:
case StoreOpType::F32Store:
WasmStore32::emit(this, pointer, value, offset);
break;
case StoreOpType::I64Store:
case StoreOpType::F64Store:
WasmStore64::emit(this, pointer, value, offset);
break;
}
return { };
}
auto LLIntGenerator::atomicLoad(ExtAtomicOpType op, Type, ExpressionType pointer, ExpressionType& result, uint32_t offset) -> PartialResult
{
m_usesAtomics = true;
result = push();
switch (op) {
case ExtAtomicOpType::I32AtomicLoad8U:
case ExtAtomicOpType::I64AtomicLoad8U:
WasmI64AtomicRmw8AddU::emit(this, result, pointer, offset, zeroConstant());
break;
case ExtAtomicOpType::I32AtomicLoad16U:
case ExtAtomicOpType::I64AtomicLoad16U:
WasmI64AtomicRmw16AddU::emit(this, result, pointer, offset, zeroConstant());
break;
case ExtAtomicOpType::I32AtomicLoad:
case ExtAtomicOpType::I64AtomicLoad32U:
WasmI64AtomicRmw32AddU::emit(this, result, pointer, offset, zeroConstant());
break;
case ExtAtomicOpType::I64AtomicLoad:
WasmI64AtomicRmwAdd::emit(this, result, pointer, offset, zeroConstant());
break;
default:
RELEASE_ASSERT_NOT_REACHED();
}
return { };
}
auto LLIntGenerator::atomicStore(ExtAtomicOpType op, Type, ExpressionType pointer, ExpressionType value, uint32_t offset) -> PartialResult
{
m_usesAtomics = true;
auto result = push();
switch (op) {
case ExtAtomicOpType::I32AtomicStore8U:
case ExtAtomicOpType::I64AtomicStore8U:
WasmI64AtomicRmw8XchgU::emit(this, result, pointer, offset, value);
break;
case ExtAtomicOpType::I32AtomicStore16U:
case ExtAtomicOpType::I64AtomicStore16U:
WasmI64AtomicRmw16XchgU::emit(this, result, pointer, offset, value);
break;
case ExtAtomicOpType::I32AtomicStore:
case ExtAtomicOpType::I64AtomicStore32U:
WasmI64AtomicRmw32XchgU::emit(this, result, pointer, offset, value);
break;
case ExtAtomicOpType::I64AtomicStore:
WasmI64AtomicRmwXchg::emit(this, result, pointer, offset, value);
break;
default:
RELEASE_ASSERT_NOT_REACHED();
}
didPopValueFromStack(result, "LLINT ATOMIC IGNORE"_s); // Ignore the result.
return { };
}
auto LLIntGenerator::atomicBinaryRMW(ExtAtomicOpType op, Type, ExpressionType pointer, ExpressionType value, ExpressionType& result, uint32_t offset) -> PartialResult
{
m_usesAtomics = true;
result = push();
switch (op) {
case ExtAtomicOpType::I32AtomicRmw8AddU:
case ExtAtomicOpType::I64AtomicRmw8AddU:
WasmI64AtomicRmw8AddU::emit(this, result, pointer, offset, value);
break;
case ExtAtomicOpType::I32AtomicRmw16AddU:
case ExtAtomicOpType::I64AtomicRmw16AddU:
WasmI64AtomicRmw16AddU::emit(this, result, pointer, offset, value);
break;
case ExtAtomicOpType::I32AtomicRmwAdd:
case ExtAtomicOpType::I64AtomicRmw32AddU:
WasmI64AtomicRmw32AddU::emit(this, result, pointer, offset, value);
break;
case ExtAtomicOpType::I64AtomicRmwAdd:
WasmI64AtomicRmwAdd::emit(this, result, pointer, offset, value);
break;
case ExtAtomicOpType::I32AtomicRmw8SubU:
case ExtAtomicOpType::I64AtomicRmw8SubU:
WasmI64AtomicRmw8SubU::emit(this, result, pointer, offset, value);
break;
case ExtAtomicOpType::I32AtomicRmw16SubU:
case ExtAtomicOpType::I64AtomicRmw16SubU:
WasmI64AtomicRmw16SubU::emit(this, result, pointer, offset, value);
break;
case ExtAtomicOpType::I32AtomicRmwSub:
case ExtAtomicOpType::I64AtomicRmw32SubU:
WasmI64AtomicRmw32SubU::emit(this, result, pointer, offset, value);
break;
case ExtAtomicOpType::I64AtomicRmwSub:
WasmI64AtomicRmwSub::emit(this, result, pointer, offset, value);
break;
case ExtAtomicOpType::I32AtomicRmw8AndU:
case ExtAtomicOpType::I64AtomicRmw8AndU:
WasmI64AtomicRmw8AndU::emit(this, result, pointer, offset, value);
break;
case ExtAtomicOpType::I32AtomicRmw16AndU:
case ExtAtomicOpType::I64AtomicRmw16AndU:
WasmI64AtomicRmw16AndU::emit(this, result, pointer, offset, value);
break;
case ExtAtomicOpType::I32AtomicRmwAnd:
case ExtAtomicOpType::I64AtomicRmw32AndU:
WasmI64AtomicRmw32AndU::emit(this, result, pointer, offset, value);
break;
case ExtAtomicOpType::I64AtomicRmwAnd:
WasmI64AtomicRmwAnd::emit(this, result, pointer, offset, value);
break;
case ExtAtomicOpType::I32AtomicRmw8OrU:
case ExtAtomicOpType::I64AtomicRmw8OrU:
WasmI64AtomicRmw8OrU::emit(this, result, pointer, offset, value);
break;
case ExtAtomicOpType::I32AtomicRmw16OrU:
case ExtAtomicOpType::I64AtomicRmw16OrU:
WasmI64AtomicRmw16OrU::emit(this, result, pointer, offset, value);
break;
case ExtAtomicOpType::I32AtomicRmwOr:
case ExtAtomicOpType::I64AtomicRmw32OrU:
WasmI64AtomicRmw32OrU::emit(this, result, pointer, offset, value);
break;
case ExtAtomicOpType::I64AtomicRmwOr:
WasmI64AtomicRmwOr::emit(this, result, pointer, offset, value);
break;
case ExtAtomicOpType::I32AtomicRmw8XorU:
case ExtAtomicOpType::I64AtomicRmw8XorU:
WasmI64AtomicRmw8XorU::emit(this, result, pointer, offset, value);
break;
case ExtAtomicOpType::I32AtomicRmw16XorU:
case ExtAtomicOpType::I64AtomicRmw16XorU:
WasmI64AtomicRmw16XorU::emit(this, result, pointer, offset, value);
break;
case ExtAtomicOpType::I32AtomicRmwXor:
case ExtAtomicOpType::I64AtomicRmw32XorU:
WasmI64AtomicRmw32XorU::emit(this, result, pointer, offset, value);
break;
case ExtAtomicOpType::I64AtomicRmwXor:
WasmI64AtomicRmwXor::emit(this, result, pointer, offset, value);
break;
case ExtAtomicOpType::I32AtomicRmw8XchgU:
case ExtAtomicOpType::I64AtomicRmw8XchgU:
WasmI64AtomicRmw8XchgU::emit(this, result, pointer, offset, value);
break;
case ExtAtomicOpType::I32AtomicRmw16XchgU:
case ExtAtomicOpType::I64AtomicRmw16XchgU:
WasmI64AtomicRmw16XchgU::emit(this, result, pointer, offset, value);
break;
case ExtAtomicOpType::I32AtomicRmwXchg:
case ExtAtomicOpType::I64AtomicRmw32XchgU:
WasmI64AtomicRmw32XchgU::emit(this, result, pointer, offset, value);
break;
case ExtAtomicOpType::I64AtomicRmwXchg:
WasmI64AtomicRmwXchg::emit(this, result, pointer, offset, value);
break;
default:
RELEASE_ASSERT_NOT_REACHED();
break;
}
return { };
}
auto LLIntGenerator::atomicCompareExchange(ExtAtomicOpType op, Type, ExpressionType pointer, ExpressionType expected, ExpressionType value, ExpressionType& result, uint32_t offset) -> PartialResult
{
m_usesAtomics = true;
result = push();
switch (op) {
case ExtAtomicOpType::I32AtomicRmw8CmpxchgU:
case ExtAtomicOpType::I64AtomicRmw8CmpxchgU:
WasmI64AtomicRmw8CmpxchgU::emit(this, result, pointer, offset, expected, value);
break;
case ExtAtomicOpType::I32AtomicRmw16CmpxchgU:
case ExtAtomicOpType::I64AtomicRmw16CmpxchgU:
WasmI64AtomicRmw16CmpxchgU::emit(this, result, pointer, offset, expected, value);
break;
case ExtAtomicOpType::I32AtomicRmwCmpxchg:
case ExtAtomicOpType::I64AtomicRmw32CmpxchgU:
WasmI64AtomicRmw32CmpxchgU::emit(this, result, pointer, offset, expected, value);
break;
case ExtAtomicOpType::I64AtomicRmwCmpxchg:
WasmI64AtomicRmwCmpxchg::emit(this, result, pointer, offset, expected, value);
break;
default:
RELEASE_ASSERT_NOT_REACHED();
break;
}
return { };
}
auto LLIntGenerator::atomicWait(ExtAtomicOpType op, ExpressionType pointer, ExpressionType value, ExpressionType timeout, ExpressionType& result, uint32_t offset) -> PartialResult
{
m_usesAtomics = true;
result = push();
switch (op) {
case ExtAtomicOpType::MemoryAtomicWait32:
WasmMemoryAtomicWait32::emit(this, result, pointer, offset, value, timeout);
break;
case ExtAtomicOpType::MemoryAtomicWait64:
WasmMemoryAtomicWait64::emit(this, result, pointer, offset, value, timeout);
break;
default:
RELEASE_ASSERT_NOT_REACHED();
break;
}
return { };
}
auto LLIntGenerator::atomicNotify(ExtAtomicOpType op, ExpressionType pointer, ExpressionType count, ExpressionType& result, uint32_t offset) -> PartialResult
{
m_usesAtomics = true;
result = push();
RELEASE_ASSERT(op == ExtAtomicOpType::MemoryAtomicNotify);
WasmMemoryAtomicNotify::emit(this, result, pointer, offset, count);
return { };
}
auto LLIntGenerator::atomicFence(ExtAtomicOpType, uint8_t) -> PartialResult
{
m_usesAtomics = true;
WasmAtomicFence::emit(this);
return { };
}
auto LLIntGenerator::truncSaturated(Ext1OpType op, ExpressionType operand, ExpressionType& result, Type, Type) -> PartialResult
{
result = push();
switch (op) {
case Ext1OpType::I32TruncSatF32S:
WasmI32TruncSatF32S::emit(this, result, operand);
break;
case Ext1OpType::I32TruncSatF32U:
WasmI32TruncSatF32U::emit(this, result, operand);
break;
case Ext1OpType::I32TruncSatF64S:
WasmI32TruncSatF64S::emit(this, result, operand);
break;
case Ext1OpType::I32TruncSatF64U:
WasmI32TruncSatF64U::emit(this, result, operand);
break;
case Ext1OpType::I64TruncSatF32S:
WasmI64TruncSatF32S::emit(this, result, operand);
break;
case Ext1OpType::I64TruncSatF32U:
WasmI64TruncSatF32U::emit(this, result, operand);
break;
case Ext1OpType::I64TruncSatF64S:
WasmI64TruncSatF64S::emit(this, result, operand);
break;
case Ext1OpType::I64TruncSatF64U:
WasmI64TruncSatF64U::emit(this, result, operand);
break;
default:
RELEASE_ASSERT_NOT_REACHED();
break;
}
return { };
}
auto LLIntGenerator::addRefI31(ExpressionType value, ExpressionType& result) -> PartialResult
{
result = push();
WasmRefI31::emit(this, result, value);
return { };
}
auto LLIntGenerator::addI31GetS(ExpressionType ref, ExpressionType& result) -> PartialResult
{
result = push();
WasmI31Get::emit(this, result, ref, true /* isSigned */);
return { };
}
auto LLIntGenerator::addI31GetU(ExpressionType ref, ExpressionType& result) -> PartialResult
{
result = push();
WasmI31Get::emit(this, result, ref, false /* isSigned */);
return { };
}
auto LLIntGenerator::addArrayNew(uint32_t index, ExpressionType size, ExpressionType value, ExpressionType& result) -> PartialResult
{
result = push();
WasmArrayNew::emit(this, result, size, value, index, static_cast<uint8_t>(ArrayGetKind::New));
return { };
}
auto LLIntGenerator::addArrayNewDefault(uint32_t index, ExpressionType size, ExpressionType& result) -> PartialResult
{
result = push();
WasmArrayNew::emit(this, result, size, ExpressionType(), index, static_cast<uint8_t>(ArrayGetKind::NewDefault));
return { };
}
auto LLIntGenerator::addArrayNewFixed(uint32_t index, ArgumentList& args, ExpressionType& result) -> PartialResult
{
// Special-case the 0-arguments case since the logic below only makes sense with at least one argument
if (!args.size()) {
result = push();
WasmArrayNew::emit(this, result, addConstantWithoutPush(Types::I32, args.size()), ExpressionType(), index, static_cast<uint8_t>(ArrayGetKind::NewFixed));
return { };
}
// Allocate stack slots for the arguments
m_stackSize += args.size();
// See the `addStructNew` operation for rationale
walkExpressionStack(args, [&](VirtualRegister& arg, VirtualRegister slot) {
if (arg == slot)
return;
WasmMov::emit(this, slot, arg);
arg = slot;
});
// Arguments are passed in reverse order (the last arg will be at the highest virtual register index, which
// will have the lowest address.)
// The implementation of array_new_fixed has to iterate over its arguments in reverse order.
result = args[0];
WasmArrayNew::emit(this, result, addConstantWithoutPush(Types::I32, args.size()), args.last(), index, static_cast<uint8_t>(ArrayGetKind::NewFixed));
// "Pop" arguments off stack, leaving the return value
m_stackSize -= args.size() - 1;
return { };
}
auto LLIntGenerator::addArrayNewData(uint32_t typeIndex, uint32_t dataIndex, ExpressionType size, ExpressionType offset, ExpressionType& result) -> PartialResult
{
ResultList results;
addCallBuiltin(LLIntBuiltin::ArrayNewData, { addConstantWithoutPush(Types::I32, typeIndex), addConstantWithoutPush(Types::I32, dataIndex), size, offset }, results);
result = results.at(0);
return { };
}
auto LLIntGenerator::addArrayNewElem(uint32_t typeIndex, uint32_t elemSegmentIndex, ExpressionType size, ExpressionType offset, ExpressionType& result) -> PartialResult
{
ResultList results;
addCallBuiltin(LLIntBuiltin::ArrayNewElem, { addConstantWithoutPush(Types::I32, typeIndex), addConstantWithoutPush(Types::I32, elemSegmentIndex), size, offset }, results);
result = results.at(0);
return { };
}
auto LLIntGenerator::addArrayGet(ExtGCOpType arrayGetKind, uint32_t typeIndex, ExpressionType arrayref, ExpressionType index, ExpressionType& result) -> PartialResult
{
result = push();
WasmArrayGet::emit(this, result, arrayref, index, typeIndex, static_cast<unsigned>(arrayGetKind));
return { };
}
auto LLIntGenerator::addArraySet(uint32_t typeIndex, ExpressionType arrayref, ExpressionType index, ExpressionType value) -> PartialResult
{
WasmArraySet::emit(this, arrayref, index, value, typeIndex);
return { };
}
auto LLIntGenerator::addArrayLen(ExpressionType arrayref, ExpressionType& result) -> PartialResult
{
result = push();
WasmArrayLen::emit(this, result, arrayref);
return { };
}
auto LLIntGenerator::addArrayFill(uint32_t typeIndex, ExpressionType arrayref, ExpressionType offset, ExpressionType value, ExpressionType size) -> PartialResult
{
WasmArrayFill::emit(this, arrayref, offset, value, size, typeIndex);
return { };
}
auto LLIntGenerator::addArrayCopy(uint32_t dstTypeIndex, ExpressionType dst, ExpressionType dstOffset, uint32_t srcTypeIndex, ExpressionType src, ExpressionType srcOffset, ExpressionType size) -> PartialResult
{
ResultList results;
addCallBuiltin(LLIntBuiltin::ArrayCopy, { addConstantWithoutPush(Types::I32, dstTypeIndex), dst, dstOffset, addConstantWithoutPush(Types::I32, srcTypeIndex), src, srcOffset, size }, results);
return { };
}
auto LLIntGenerator::addArrayInitElem(uint32_t dstTypeIndex, ExpressionType dst, ExpressionType dstOffset, uint32_t srcElementIndex, ExpressionType srcOffset, ExpressionType size) -> PartialResult
{
ResultList results;
addCallBuiltin(LLIntBuiltin::ArrayInitElem, { addConstantWithoutPush(Types::I32, dstTypeIndex), dst, dstOffset, addConstantWithoutPush(Types::I32, srcElementIndex), srcOffset, size }, results);
return { };
}
auto LLIntGenerator::addArrayInitData(uint32_t dstTypeIndex, ExpressionType dst, ExpressionType dstOffset, uint32_t srcDataIndex, ExpressionType srcOffset, ExpressionType size) -> PartialResult
{
ResultList results;
addCallBuiltin(LLIntBuiltin::ArrayInitData, { addConstantWithoutPush(Types::I32, dstTypeIndex), dst, dstOffset, addConstantWithoutPush(Types::I32, srcDataIndex), srcOffset, size }, results);
return { };
}
auto LLIntGenerator::addStructNew(uint32_t index, ArgumentList& args, ExpressionType& result) -> PartialResult
{
// Special-case the 0-arguments case since the logic below only makes sense with at least one argument
if (!args.size()) {
result = push();
WasmStructNew::emit(this, result, index, static_cast<bool>(UseDefaultValue::No), VirtualRegister());
return { };
}
// Allocate stack slots for walkExpressionStack() to use
m_stackSize += args.size();
// The logic here is similar to addThrow(); see comments there.
// It's important to use walkExpressionStack() here and not call push() explicitly,
// because the stack consistency checking that push() does will fail if there's
// more than one struct field. The parser pops the arguments off the stack before
// calling into this method, and by pushing arguments, we get out of sync
// with the parser's expression stack.
walkExpressionStack(args, [&](VirtualRegister& arg, VirtualRegister slot) {
if (arg == slot)
return;
WasmMov::emit(this, slot, arg);
arg = slot;
});
result = args[0];
WasmStructNew::emit(this, result, index, static_cast<bool>(UseDefaultValue::No), args.last());
// "Pop" arguments off, minus one slot for the return value
m_stackSize -= args.size() - 1;
return { };
}
auto LLIntGenerator::addStructNewDefault(uint32_t index, ExpressionType& result) -> PartialResult
{
result = push();
WasmStructNew::emit(this, result, index, static_cast<bool>(UseDefaultValue::Yes), { });
return { };
}
auto LLIntGenerator::addStructGet(ExtGCOpType structGetKind, ExpressionType structReference, const StructType&, uint32_t fieldIndex, ExpressionType& result) -> PartialResult
{
result = push();
WasmStructGet::emit(this, result, structReference, fieldIndex, static_cast<unsigned>(structGetKind));
return { };
}
auto LLIntGenerator::addStructSet(ExpressionType structReference, const StructType&, uint32_t fieldIndex, ExpressionType value) -> PartialResult
{
WasmStructSet::emit(this, structReference, fieldIndex, value);
return { };
}
auto LLIntGenerator::addRefTest(ExpressionType reference, bool allowNull, int32_t heapType, bool shouldNegate, ExpressionType& result) -> PartialResult
{
ResultList results;
addCallBuiltin(LLIntBuiltin::RefTest, { reference, addConstantWithoutPush(Types::I32, static_cast<uint32_t>(allowNull)), addConstantWithoutPush(Types::I32, heapType), addConstantWithoutPush(Types::I32, static_cast<uint32_t>(shouldNegate)) }, results);
ASSERT(results.size() == 1);
result = results.at(0);
return { };
}
auto LLIntGenerator::addRefCast(ExpressionType reference, bool allowNull, int32_t heapType, ExpressionType& result) -> PartialResult
{
ResultList results;
addCallBuiltin(LLIntBuiltin::RefCast, { reference, addConstantWithoutPush(Types::I32, static_cast<uint32_t>(allowNull)), addConstantWithoutPush(Types::I32, heapType) }, results);
ASSERT(results.size() == 1);
result = results.at(0);
return { };
}
auto LLIntGenerator::addAnyConvertExtern(ExpressionType reference, ExpressionType& result) -> PartialResult
{
ResultList results;
addCallBuiltin(LLIntBuiltin::AnyConvertExtern, { reference }, results);
ASSERT(results.size() == 1);
result = results.at(0);
return { };
}
auto LLIntGenerator::addExternConvertAny(ExpressionType reference, ExpressionType& result) -> PartialResult
{
result = push();
WasmExternConvertAny::emit(this, result, reference);
return { };
}
void LLIntGenerator::linkSwitchTargets(Label& label, unsigned location)
{
auto it = m_switches.find(&label);
if (it != m_switches.end()) {
for (const auto& entry : it->value) {
ASSERT(!*entry.jumpTarget);
*entry.jumpTarget = location - entry.offset;
}
m_switches.remove(it);
}
}
static void dumpExpressionStack(const CommaPrinter& comma, const LLIntGenerator::Stack& expressionStack)
{
dataLog(comma, "ExpressionStack:");
for (const auto& expression : expressionStack)
dataLog(comma, expression.value());
}
void LLIntGenerator::dump(const ControlStack& controlStack, const Stack* stack)
{
dataLogLn("Control stack: stackSize:(", m_stackSize.value(), ")");
for (size_t i = controlStack.size(); i--;) {
dataLog(" ", controlStack[i].controlData, ": ");
CommaPrinter comma(", "_s, ""_s);
dumpExpressionStack(comma, *stack);
stack = &controlStack[i].enclosedExpressionStack;
dataLogLn();
}
}
}
template<>
void GenericLabel<Wasm::GeneratorTraits>::setLocation(BytecodeGeneratorBase<Wasm::GeneratorTraits>& generator, unsigned location)
{
RELEASE_ASSERT(isForward());
m_location = location;
Wasm::LLIntGenerator* llintGenerator = static_cast<Wasm::LLIntGenerator*>(&generator);
llintGenerator->linkSwitchTargets(*this, m_location);
for (auto offset : m_unresolvedJumps) {
auto instruction = generator.m_writer.ref(offset);
int target = m_location - offset;
#define CASE(__op) \
case __op::opcodeID: \
instruction->cast<__op>()->setTargetLabel(BoundLabel(target), [&]() { \
generator.m_codeBlock->addOutOfLineJumpTarget(instruction.offset(), target); \
return BoundLabel(); \
}); \
break;
switch (instruction->opcodeID()) {
CASE(WasmJmp)
CASE(WasmJtrue)
CASE(WasmJfalse)
default:
RELEASE_ASSERT_NOT_REACHED();
}
#undef CASE
}
}
} // namespace JSC::Wasm
#endif // ENABLE(WEBASSEMBLY)
|