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
|
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>1 TableGen Programmer’s Reference — LLVM 13 documentation</title>
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="../_static/llvm-theme.css" type="text/css" />
<script id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="TableGen Fundamentals" href="../TableGenFundamentals.html" />
<link rel="prev" title="1 TableGen Backend Developer’s Guide" href="BackGuide.html" />
<style type="text/css">
table.right { float: right; margin-left: 20px; }
table.right td { border: 1px solid #ccc; }
</style>
</head><body>
<div class="logo">
<a href="../index.html">
<img src="../_static/logo.png"
alt="LLVM Logo" width="250" height="88"/></a>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../TableGenFundamentals.html" title="TableGen Fundamentals"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="BackGuide.html" title="1 TableGen Backend Developer’s Guide"
accesskey="P">previous</a> |</li>
<li><a href="https://llvm.org/">LLVM Home</a> | </li>
<li><a href="../index.html">Documentation</a>»</li>
<li class="nav-item nav-item-1"><a href="../UserGuides.html" >User Guides</a> »</li>
<li class="nav-item nav-item-2"><a href="index.html" accesskey="U">TableGen Overview</a> »</li>
<li class="nav-item nav-item-this"><a href=""><span class="sectnum">1</span> TableGen Programmer’s Reference</a></li>
</ul>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3>Documentation</h3>
<ul class="want-points">
<li><a href="https://llvm.org/docs/GettingStartedTutorials.html">Getting Started/Tutorials</a></li>
<li><a href="https://llvm.org/docs/UserGuides.html">User Guides</a></li>
<li><a href="https://llvm.org/docs/Reference.html">Reference</a></li>
</ul>
<h3>Getting Involved</h3>
<ul class="want-points">
<li><a href="https://llvm.org/docs/Contributing.html">Contributing to LLVM</a></li>
<li><a href="https://llvm.org/docs/HowToSubmitABug.html">Submitting Bug Reports</a></li>
<li><a href="https://llvm.org/docs/GettingInvolved.html#mailing-lists">Mailing Lists</a></li>
<li><a href="https://llvm.org/docs/GettingInvolved.html#irc">IRC</a></li>
<li><a href="https://llvm.org/docs/GettingInvolved.html#meetups-and-social-events">Meetups and Social Events</a></li>
</ul>
<h3>Additional Links</h3>
<ul class="want-points">
<li><a href="https://llvm.org/docs/FAQ.html">FAQ</a></li>
<li><a href="https://llvm.org/docs/Lexicon.html">Glossary</a></li>
<li><a href="https://llvm.org/pubs">Publications</a></li>
<li><a href="https://github.com/llvm/llvm-project//">Github Repository</a></li>
</ul>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../_sources/TableGen/ProgRef.rst.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" />
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>$('#searchbox').show(0);</script>
</div>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="tablegen-programmer-s-reference">
<h1><span class="sectnum">1</span> TableGen Programmer’s Reference<a class="headerlink" href="#tablegen-programmer-s-reference" title="Permalink to this headline">¶</a></h1>
<div class="contents local topic" id="contents">
<ul class="auto-toc simple">
<li><p><a class="reference internal" href="#introduction" id="id1"><span class="sectnum">1.1</span> Introduction</a></p>
<ul class="auto-toc">
<li><p><a class="reference internal" href="#concepts" id="id2"><span class="sectnum">1.1.1</span> Concepts</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#source-files" id="id3"><span class="sectnum">1.2</span> Source Files</a></p></li>
<li><p><a class="reference internal" href="#lexical-analysis" id="id4"><span class="sectnum">1.3</span> Lexical Analysis</a></p>
<ul class="auto-toc">
<li><p><a class="reference internal" href="#literals" id="id5"><span class="sectnum">1.3.1</span> Literals</a></p></li>
<li><p><a class="reference internal" href="#identifiers" id="id6"><span class="sectnum">1.3.2</span> Identifiers</a></p></li>
<li><p><a class="reference internal" href="#bang-operators" id="id7"><span class="sectnum">1.3.3</span> Bang operators</a></p></li>
<li><p><a class="reference internal" href="#include-files" id="id8"><span class="sectnum">1.3.4</span> Include files</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#types" id="id9"><span class="sectnum">1.4</span> Types</a></p></li>
<li><p><a class="reference internal" href="#values-and-expressions" id="id10"><span class="sectnum">1.5</span> Values and Expressions</a></p>
<ul class="auto-toc">
<li><p><a class="reference internal" href="#simple-values" id="id11"><span class="sectnum">1.5.1</span> Simple values</a></p></li>
<li><p><a class="reference internal" href="#suffixed-values" id="id12"><span class="sectnum">1.5.2</span> Suffixed values</a></p></li>
<li><p><a class="reference internal" href="#the-paste-operator" id="id13"><span class="sectnum">1.5.3</span> The paste operator</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#statements" id="id14"><span class="sectnum">1.6</span> Statements</a></p>
<ul class="auto-toc">
<li><p><a class="reference internal" href="#class-define-an-abstract-record-class" id="id15"><span class="sectnum">1.6.1</span> <code class="docutils literal notranslate"><span class="pre">class</span></code> — define an abstract record class</a></p>
<ul class="auto-toc">
<li><p><a class="reference internal" href="#record-bodies" id="id16"><span class="sectnum">1.6.1.1</span> Record Bodies</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#def-define-a-concrete-record" id="id17"><span class="sectnum">1.6.2</span> <code class="docutils literal notranslate"><span class="pre">def</span></code> — define a concrete record</a></p></li>
<li><p><a class="reference internal" href="#examples-classes-and-records" id="id18"><span class="sectnum">1.6.3</span> Examples: classes and records</a></p></li>
<li><p><a class="reference internal" href="#let-override-fields-in-classes-or-records" id="id19"><span class="sectnum">1.6.4</span> <code class="docutils literal notranslate"><span class="pre">let</span></code> — override fields in classes or records</a></p></li>
<li><p><a class="reference internal" href="#multiclass-define-multiple-records" id="id20"><span class="sectnum">1.6.5</span> <code class="docutils literal notranslate"><span class="pre">multiclass</span></code> — define multiple records</a></p></li>
<li><p><a class="reference internal" href="#defm-invoke-multiclasses-to-define-multiple-records" id="id21"><span class="sectnum">1.6.6</span> <code class="docutils literal notranslate"><span class="pre">defm</span></code> — invoke multiclasses to define multiple records</a></p></li>
<li><p><a class="reference internal" href="#examples-multiclasses-and-defms" id="id22"><span class="sectnum">1.6.7</span> Examples: multiclasses and defms</a></p></li>
<li><p><a class="reference internal" href="#defset-create-a-definition-set" id="id23"><span class="sectnum">1.6.8</span> <code class="docutils literal notranslate"><span class="pre">defset</span></code> — create a definition set</a></p></li>
<li><p><a class="reference internal" href="#defvar-define-a-variable" id="id24"><span class="sectnum">1.6.9</span> <code class="docutils literal notranslate"><span class="pre">defvar</span></code> — define a variable</a></p></li>
<li><p><a class="reference internal" href="#foreach-iterate-over-a-sequence-of-statements" id="id25"><span class="sectnum">1.6.10</span> <code class="docutils literal notranslate"><span class="pre">foreach</span></code> — iterate over a sequence of statements</a></p></li>
<li><p><a class="reference internal" href="#if-select-statements-based-on-a-test" id="id26"><span class="sectnum">1.6.11</span> <code class="docutils literal notranslate"><span class="pre">if</span></code> — select statements based on a test</a></p></li>
<li><p><a class="reference internal" href="#assert-check-that-a-condition-is-true" id="id27"><span class="sectnum">1.6.12</span> <code class="docutils literal notranslate"><span class="pre">assert</span></code> — check that a condition is true</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#additional-details" id="id28"><span class="sectnum">1.7</span> Additional Details</a></p>
<ul class="auto-toc">
<li><p><a class="reference internal" href="#directed-acyclic-graphs-dags" id="id29"><span class="sectnum">1.7.1</span> Directed acyclic graphs (DAGs)</a></p></li>
<li><p><a class="reference internal" href="#defvar-in-a-record-body" id="id30"><span class="sectnum">1.7.2</span> Defvar in a record body</a></p></li>
<li><p><a class="reference internal" href="#how-records-are-built" id="id31"><span class="sectnum">1.7.3</span> How records are built</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#using-classes-as-subroutines" id="id32"><span class="sectnum">1.8</span> Using Classes as Subroutines</a></p></li>
<li><p><a class="reference internal" href="#preprocessing-facilities" id="id33"><span class="sectnum">1.9</span> Preprocessing Facilities</a></p></li>
<li><p><a class="reference internal" href="#appendix-a-bang-operators" id="id34"><span class="sectnum">1.10</span> Appendix A: Bang Operators</a></p></li>
<li><p><a class="reference internal" href="#appendix-b-paste-operator-examples" id="id35"><span class="sectnum">1.11</span> Appendix B: Paste Operator Examples</a></p></li>
<li><p><a class="reference internal" href="#appendix-c-sample-record" id="id36"><span class="sectnum">1.12</span> Appendix C: Sample Record</a></p></li>
</ul>
</div>
<div class="section" id="introduction">
<h2><a class="toc-backref" href="#id1"><span class="sectnum">1.1</span> Introduction</a><a class="headerlink" href="#introduction" title="Permalink to this headline">¶</a></h2>
<p>The purpose of TableGen is to generate complex output files based on
information from source files that are significantly easier to code than the
output files would be, and also easier to maintain and modify over time. The
information is coded in a declarative style involving classes and records,
which are then processed by TableGen. The internalized records are passed on
to various <em>backends</em>, which extract information from a subset of the records
and generate one or more output files. These output files are typically
<code class="docutils literal notranslate"><span class="pre">.inc</span></code> files for C++, but may be any type of file that the backend
developer needs.</p>
<p>This document describes the LLVM TableGen facility in detail. It is intended
for the programmer who is using TableGen to produce code for a project. If
you are looking for a simple overview, check out the <a class="reference internal" href="index.html"><span class="doc">TableGen Overview</span></a>. The various <code class="docutils literal notranslate"><span class="pre">*-tblgen</span></code> commands used to invoke TableGen are
described in <a class="reference internal" href="../CommandGuide/tblgen.html"><span class="doc">tblgen Family - Description to C++
Code</span></a>.</p>
<p>An example of a backend is <code class="docutils literal notranslate"><span class="pre">RegisterInfo</span></code>, which generates the register
file information for a particular target machine, for use by the LLVM
target-independent code generator. See <a class="reference internal" href="BackEnds.html"><span class="doc">TableGen Backends</span></a>
for a description of the LLVM TableGen backends, and <a class="reference internal" href="BackGuide.html"><span class="doc">TableGen
Backend Developer’s Guide</span></a> for a guide to writing a new
backend.</p>
<p>Here are a few of the things backends can do.</p>
<ul class="simple">
<li><p>Generate the register file information for a particular target machine.</p></li>
<li><p>Generate the instruction definitions for a target.</p></li>
<li><p>Generate the patterns that the code generator uses to match instructions
to intermediate representation (IR) nodes.</p></li>
<li><p>Generate semantic attribute identifiers for Clang.</p></li>
<li><p>Generate abstract syntax tree (AST) declaration node definitions for Clang.</p></li>
<li><p>Generate AST statement node definitions for Clang.</p></li>
</ul>
<div class="section" id="concepts">
<h3><a class="toc-backref" href="#id2"><span class="sectnum">1.1.1</span> Concepts</a><a class="headerlink" href="#concepts" title="Permalink to this headline">¶</a></h3>
<p>TableGen source files contain two primary items: <em>abstract records</em> and
<em>concrete records</em>. In this and other TableGen documents, abstract records
are called <em>classes.</em> (These classes are different from C++ classes and do
not map onto them.) In addition, concrete records are usually just called
records, although sometimes the term <em>record</em> refers to both classes and
concrete records. The distinction should be clear in context.</p>
<p>Classes and concrete records have a unique <em>name</em>, either chosen by
the programmer or generated by TableGen. Associated with that name
is a list of <em>fields</em> with values and an optional list of <em>parent classes</em>
(sometimes called base or super classes). The fields are the primary data that
backends will process. Note that TableGen assigns no meanings to fields; the
meanings are entirely up to the backends and the programs that incorporate
the output of those backends.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>The term “parent class” can refer to a class that is a parent of another
class, and also to a class from which a concrete record inherits. This
nonstandard use of the term arises because TableGen treats classes and
concrete records similarly.</p>
</div>
<p>A backend processes some subset of the concrete records built by the
TableGen parser and emits the output files. These files are usually C++
<code class="docutils literal notranslate"><span class="pre">.inc</span></code> files that are included by the programs that require the data in
those records. However, a backend can produce any type of output files. For
example, it could produce a data file containing messages tagged with
identifiers and substitution parameters. In a complex use case such as the
LLVM code generator, there can be many concrete records and some of them can
have an unexpectedly large number of fields, resulting in large output files.</p>
<p>In order to reduce the complexity of TableGen files, classes are used to
abstract out groups of record fields. For example, a few classes may
abstract the concept of a machine register file, while other classes may
abstract the instruction formats, and still others may abstract the
individual instructions. TableGen allows an arbitrary hierarchy of classes,
so that the abstract classes for two concepts can share a third superclass that
abstracts common “sub-concepts” from the two original concepts.</p>
<p>In order to make classes more useful, a concrete record (or another class)
can request a class as a parent class and pass <em>template arguments</em> to it.
These template arguments can be used in the fields of the parent class to
initialize them in a custom manner. That is, record or class <code class="docutils literal notranslate"><span class="pre">A</span></code> can
request parent class <code class="docutils literal notranslate"><span class="pre">S</span></code> with one set of template arguments, while record or class
<code class="docutils literal notranslate"><span class="pre">B</span></code> can request <code class="docutils literal notranslate"><span class="pre">S</span></code> with a different set of arguments. Without template
arguments, many more classes would be required, one for each combination of
the template arguments.</p>
<p>Both classes and concrete records can include fields that are uninitialized.
The uninitialized “value” is represented by a question mark (<code class="docutils literal notranslate"><span class="pre">?</span></code>). Classes
often have uninitialized fields that are expected to be filled in when those
classes are inherited by concrete records. Even so, some fields of concrete
records may remain uninitialized.</p>
<p>TableGen provides <em>multiclasses</em> to collect a group of record definitions in
one place. A multiclass is a sort of macro that can be “invoked” to define
multiple concrete records all at once. A multiclass can inherit from other
multiclasses, which means that the multiclass inherits all the definitions
from its parent multiclasses.</p>
<p><a class="reference internal" href="#appendix-c-sample-record">Appendix C: Sample Record</a> illustrates a complex record in the Intel X86
target and the simple way in which it is defined.</p>
</div>
</div>
<div class="section" id="source-files">
<h2><a class="toc-backref" href="#id3"><span class="sectnum">1.2</span> Source Files</a><a class="headerlink" href="#source-files" title="Permalink to this headline">¶</a></h2>
<p>TableGen source files are plain ASCII text files. The files can contain
statements, comments, and blank lines (see <a class="reference internal" href="#lexical-analysis">Lexical Analysis</a>). The standard file
extension for TableGen files is <code class="docutils literal notranslate"><span class="pre">.td</span></code>.</p>
<p>TableGen files can grow quite large, so there is an include mechanism that
allows one file to include the content of another file (see <a class="reference internal" href="#include-files">Include
Files</a>). This allows large files to be broken up into smaller ones, and
also provides a simple library mechanism where multiple source files can
include the same library file.</p>
<p>TableGen supports a simple preprocessor that can be used to conditionalize
portions of <code class="docutils literal notranslate"><span class="pre">.td</span></code> files. See <a class="reference internal" href="#preprocessing-facilities">Preprocessing Facilities</a> for more
information.</p>
</div>
<div class="section" id="lexical-analysis">
<h2><a class="toc-backref" href="#id4"><span class="sectnum">1.3</span> Lexical Analysis</a><a class="headerlink" href="#lexical-analysis" title="Permalink to this headline">¶</a></h2>
<p>The lexical and syntax notation used here is intended to imitate
<a class="reference external" href="http://docs.python.org/py3k/reference/introduction.html#notation">Python’s</a> notation. In particular, for lexical definitions, the productions
operate at the character level and there is no implied whitespace between
elements. The syntax definitions operate at the token level, so there is
implied whitespace between tokens.</p>
<p>TableGen supports BCPL-style comments (<code class="docutils literal notranslate"><span class="pre">//</span> <span class="pre">...</span></code>) and nestable C-style
comments (<code class="docutils literal notranslate"><span class="pre">/*</span> <span class="pre">...</span> <span class="pre">*/</span></code>).
TableGen also provides simple <a class="reference internal" href="#preprocessing-facilities">Preprocessing Facilities</a>.</p>
<p>Formfeed characters may be used freely in files to produce page breaks when
the file is printed for review.</p>
<p>The following are the basic punctuation tokens:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>- + [ ] { } ( ) < > : ; . ... = ? #
</pre></div>
</div>
<div class="section" id="literals">
<h3><a class="toc-backref" href="#id5"><span class="sectnum">1.3.1</span> Literals</a><a class="headerlink" href="#literals" title="Permalink to this headline">¶</a></h3>
<p>Numeric literals take one of the following forms:</p>
<pre>
<strong id="grammar-token-TokInteger"><span id="grammar-token-tokinteger"></span>TokInteger </strong> ::= <a class="reference internal" href="#grammar-token-DecimalInteger"><code class="xref docutils literal notranslate"><span class="pre">DecimalInteger</span></code></a> | <a class="reference internal" href="#grammar-token-HexInteger"><code class="xref docutils literal notranslate"><span class="pre">HexInteger</span></code></a> | <a class="reference internal" href="#grammar-token-BinInteger"><code class="xref docutils literal notranslate"><span class="pre">BinInteger</span></code></a>
<strong id="grammar-token-DecimalInteger"><span id="grammar-token-decimalinteger"></span>DecimalInteger</strong> ::= ["+" | "-"] ("0"..."9")+
<strong id="grammar-token-HexInteger"><span id="grammar-token-hexinteger"></span>HexInteger </strong> ::= "0x" ("0"..."9" | "a"..."f" | "A"..."F")+
<strong id="grammar-token-BinInteger"><span id="grammar-token-bininteger"></span>BinInteger </strong> ::= "0b" ("0" | "1")+
</pre>
<p>Observe that the <a class="reference internal" href="#grammar-token-DecimalInteger"><code class="xref std std-token docutils literal notranslate"><span class="pre">DecimalInteger</span></code></a> token includes the optional <code class="docutils literal notranslate"><span class="pre">+</span></code>
or <code class="docutils literal notranslate"><span class="pre">-</span></code> sign, unlike most languages where the sign would be treated as a
unary operator.</p>
<p>TableGen has two kinds of string literals:</p>
<pre>
<strong id="grammar-token-TokString"><span id="grammar-token-tokstring"></span>TokString</strong> ::= '"' (non-'"' characters and escapes) '"'
<strong id="grammar-token-TokCode"><span id="grammar-token-tokcode"></span>TokCode </strong> ::= "[{" (shortest text not containing "}]") "}]"
</pre>
<p>A <a class="reference internal" href="#grammar-token-TokCode"><code class="xref std std-token docutils literal notranslate"><span class="pre">TokCode</span></code></a> is nothing more than a multi-line string literal
delimited by <code class="docutils literal notranslate"><span class="pre">[{</span></code> and <code class="docutils literal notranslate"><span class="pre">}]</span></code>. It can break across lines and the
line breaks are retained in the string.</p>
<p>The current implementation accepts the following escape sequences:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>\\ \<span class="s1">' </span><span class="se">\"</span><span class="s1"> </span><span class="se">\t</span><span class="s1"> </span><span class="se">\n</span>
</pre></div>
</div>
</div>
<div class="section" id="identifiers">
<h3><a class="toc-backref" href="#id6"><span class="sectnum">1.3.2</span> Identifiers</a><a class="headerlink" href="#identifiers" title="Permalink to this headline">¶</a></h3>
<p>TableGen has name- and identifier-like tokens, which are case-sensitive.</p>
<pre>
<strong id="grammar-token-ualpha">ualpha </strong> ::= "a"..."z" | "A"..."Z" | "_"
<strong id="grammar-token-TokIdentifier"><span id="grammar-token-tokidentifier"></span>TokIdentifier</strong> ::= ("0"..."9")* <a class="reference internal" href="#grammar-token-ualpha"><code class="xref docutils literal notranslate"><span class="pre">ualpha</span></code></a> (<a class="reference internal" href="#grammar-token-ualpha"><code class="xref docutils literal notranslate"><span class="pre">ualpha</span></code></a> | "0"..."9")*
<strong id="grammar-token-TokVarName"><span id="grammar-token-tokvarname"></span>TokVarName </strong> ::= "$" <a class="reference internal" href="#grammar-token-ualpha"><code class="xref docutils literal notranslate"><span class="pre">ualpha</span></code></a> (<a class="reference internal" href="#grammar-token-ualpha"><code class="xref docutils literal notranslate"><span class="pre">ualpha</span></code></a> | "0"..."9")*
</pre>
<p>Note that, unlike most languages, TableGen allows <a class="reference internal" href="#grammar-token-TokIdentifier"><code class="xref std std-token docutils literal notranslate"><span class="pre">TokIdentifier</span></code></a> to
begin with an integer. In case of ambiguity, a token is interpreted as a
numeric literal rather than an identifier.</p>
<p>TableGen has the following reserved keywords, which cannot be used as
identifiers:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">assert</span> <span class="n">bit</span> <span class="n">bits</span> <span class="k">class</span> <span class="nc">code</span>
<span class="n">dag</span> <span class="k">def</span> <span class="nf">else</span> <span class="n">false</span> <span class="n">foreach</span>
<span class="n">defm</span> <span class="n">defset</span> <span class="n">defvar</span> <span class="n">field</span> <span class="k">if</span>
<span class="ow">in</span> <span class="n">include</span> <span class="nb">int</span> <span class="n">let</span> <span class="nb">list</span>
<span class="n">multiclass</span> <span class="n">string</span> <span class="n">then</span> <span class="n">true</span>
</pre></div>
</div>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>The <code class="docutils literal notranslate"><span class="pre">field</span></code> reserved word is deprecated.</p>
</div>
</div>
<div class="section" id="bang-operators">
<h3><a class="toc-backref" href="#id7"><span class="sectnum">1.3.3</span> Bang operators</a><a class="headerlink" href="#bang-operators" title="Permalink to this headline">¶</a></h3>
<p>TableGen provides “bang operators” that have a wide variety of uses:</p>
<pre>
<strong id="grammar-token-BangOperator"><span id="grammar-token-bangoperator"></span>BangOperator</strong> ::= one of
!add !and !cast !con !dag
!empty !eq !filter !find !foldl
!foreach !ge !getdagop !gt !head
!if !interleave !isa !le !listconcat
!listsplat !lt !mul !ne !not
!or !setdagop !shl !size !sra
!srl !strconcat !sub !subst !substr
!tail !xor
</pre>
<p>The <code class="docutils literal notranslate"><span class="pre">!cond</span></code> operator has a slightly different
syntax compared to other bang operators, so it is defined separately:</p>
<pre>
<strong id="grammar-token-CondOperator"><span id="grammar-token-condoperator"></span>CondOperator</strong> ::= !cond
</pre>
<p>See <a class="reference internal" href="#appendix-a-bang-operators">Appendix A: Bang Operators</a> for a description of each bang operator.</p>
</div>
<div class="section" id="include-files">
<h3><a class="toc-backref" href="#id8"><span class="sectnum">1.3.4</span> Include files</a><a class="headerlink" href="#include-files" title="Permalink to this headline">¶</a></h3>
<p>TableGen has an include mechanism. The content of the included file
lexically replaces the <code class="docutils literal notranslate"><span class="pre">include</span></code> directive and is then parsed as if it was
originally in the main file.</p>
<pre>
<strong id="grammar-token-IncludeDirective"><span id="grammar-token-includedirective"></span>IncludeDirective</strong> ::= "include" <a class="reference internal" href="#grammar-token-TokString"><code class="xref docutils literal notranslate"><span class="pre">TokString</span></code></a>
</pre>
<p>Portions of the main file and included files can be conditionalized using
preprocessor directives.</p>
<pre>
<strong id="grammar-token-PreprocessorDirective"><span id="grammar-token-preprocessordirective"></span>PreprocessorDirective</strong> ::= "#define" | "#ifdef" | "#ifndef"
</pre>
</div>
</div>
<div class="section" id="types">
<h2><a class="toc-backref" href="#id9"><span class="sectnum">1.4</span> Types</a><a class="headerlink" href="#types" title="Permalink to this headline">¶</a></h2>
<p>The TableGen language is statically typed, using a simple but complete type
system. Types are used to check for errors, to perform implicit conversions,
and to help interface designers constrain the allowed input. Every value is
required to have an associated type.</p>
<p>TableGen supports a mixture of low-level types (e.g., <code class="docutils literal notranslate"><span class="pre">bit</span></code>) and
high-level types (e.g., <code class="docutils literal notranslate"><span class="pre">dag</span></code>). This flexibility allows you to describe a
wide range of records conveniently and compactly.</p>
<pre>
<strong id="grammar-token-Type"><span id="grammar-token-type"></span>Type </strong> ::= "bit" | "int" | "string" | "dag"
| "bits" "<" <a class="reference internal" href="#grammar-token-TokInteger"><code class="xref docutils literal notranslate"><span class="pre">TokInteger</span></code></a> ">"
| "list" "<" <a class="reference internal" href="#grammar-token-Type"><code class="xref docutils literal notranslate"><span class="pre">Type</span></code></a> ">"
| <a class="reference internal" href="#grammar-token-ClassID"><code class="xref docutils literal notranslate"><span class="pre">ClassID</span></code></a>
<strong id="grammar-token-ClassID"><span id="grammar-token-classid"></span>ClassID</strong> ::= <a class="reference internal" href="#grammar-token-TokIdentifier"><code class="xref docutils literal notranslate"><span class="pre">TokIdentifier</span></code></a>
</pre>
<dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">bit</span></code></dt><dd><p>A <code class="docutils literal notranslate"><span class="pre">bit</span></code> is a boolean value that can be 0 or 1.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">int</span></code></dt><dd><p>The <code class="docutils literal notranslate"><span class="pre">int</span></code> type represents a simple 64-bit integer value, such as 5 or
-42.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">string</span></code></dt><dd><p>The <code class="docutils literal notranslate"><span class="pre">string</span></code> type represents an ordered sequence of characters of arbitrary
length.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">bits<</span></code><em>n</em><code class="docutils literal notranslate"><span class="pre">></span></code></dt><dd><p>The <code class="docutils literal notranslate"><span class="pre">bits</span></code> type is a fixed-sized integer of arbitrary length <em>n</em> that
is treated as separate bits. These bits can be accessed individually.
A field of this type is useful for representing an instruction operation
code, register number, or address mode/register/displacement. The bits of
the field can be set individually or as subfields. For example, in an
instruction address, the addressing mode, base register number, and
displacement can be set separately.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">list<</span></code><em>type</em><code class="docutils literal notranslate"><span class="pre">></span></code></dt><dd><p>This type represents a list whose elements are of the <em>type</em> specified in
angle brackets. The element type is arbitrary; it can even be another
list type. List elements are indexed from 0.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">dag</span></code></dt><dd><p>This type represents a nestable directed acyclic graph (DAG) of nodes.
Each node has an <em>operator</em> and zero or more <em>arguments</em> (or <em>operands</em>).
An argument can be
another <code class="docutils literal notranslate"><span class="pre">dag</span></code> object, allowing an arbitrary tree of nodes and edges.
As an example, DAGs are used to represent code patterns for use by
the code generator instruction selection algorithms. See <a class="reference internal" href="#directed-acyclic-graphs-dags">Directed
acyclic graphs (DAGs)</a> for more details;</p>
</dd>
<dt><a class="reference internal" href="#grammar-token-ClassID"><code class="xref std std-token docutils literal notranslate"><span class="pre">ClassID</span></code></a></dt><dd><p>Specifying a class name in a type context indicates
that the type of the defined value must
be a subclass of the specified class. This is useful in conjunction with
the <code class="docutils literal notranslate"><span class="pre">list</span></code> type; for example, to constrain the elements of the list to a
common base class (e.g., a <code class="docutils literal notranslate"><span class="pre">list<Register></span></code> can only contain definitions
derived from the <code class="docutils literal notranslate"><span class="pre">Register</span></code> class).
The <a class="reference internal" href="#grammar-token-ClassID"><code class="xref std std-token docutils literal notranslate"><span class="pre">ClassID</span></code></a> must name a class that has been previously
declared or defined.</p>
</dd>
</dl>
</div>
<div class="section" id="values-and-expressions">
<h2><a class="toc-backref" href="#id10"><span class="sectnum">1.5</span> Values and Expressions</a><a class="headerlink" href="#values-and-expressions" title="Permalink to this headline">¶</a></h2>
<p>There are many contexts in TableGen statements where a value is required. A
common example is in the definition of a record, where each field is
specified by a name and an optional value. TableGen allows for a reasonable
number of different forms when building up value expressions. These forms
allow the TableGen file to be written in a syntax that is natural for the
application.</p>
<p>Note that all of the values have rules for converting them from one type to
another. For example, these rules allow you to assign a value like <code class="docutils literal notranslate"><span class="pre">7</span></code>
to an entity of type <code class="docutils literal notranslate"><span class="pre">bits<4></span></code>.</p>
<pre>
<strong id="grammar-token-Value"><span id="grammar-token-value"></span>Value </strong> ::= <a class="reference internal" href="#grammar-token-SimpleValue"><code class="xref docutils literal notranslate"><span class="pre">SimpleValue</span></code></a> <a class="reference internal" href="#grammar-token-ValueSuffix"><code class="xref docutils literal notranslate"><span class="pre">ValueSuffix</span></code></a>*
| <a class="reference internal" href="#grammar-token-Value"><code class="xref docutils literal notranslate"><span class="pre">Value</span></code></a> "#" <a class="reference internal" href="#grammar-token-Value"><code class="xref docutils literal notranslate"><span class="pre">Value</span></code></a>
<strong id="grammar-token-ValueSuffix"><span id="grammar-token-valuesuffix"></span>ValueSuffix</strong> ::= "{" <a class="reference internal" href="#grammar-token-RangeList"><code class="xref docutils literal notranslate"><span class="pre">RangeList</span></code></a> "}"
| "[" <a class="reference internal" href="#grammar-token-RangeList"><code class="xref docutils literal notranslate"><span class="pre">RangeList</span></code></a> "]"
| "." <a class="reference internal" href="#grammar-token-TokIdentifier"><code class="xref docutils literal notranslate"><span class="pre">TokIdentifier</span></code></a>
<strong id="grammar-token-RangeList"><span id="grammar-token-rangelist"></span>RangeList </strong> ::= <a class="reference internal" href="#grammar-token-RangePiece"><code class="xref docutils literal notranslate"><span class="pre">RangePiece</span></code></a> ("," <a class="reference internal" href="#grammar-token-RangePiece"><code class="xref docutils literal notranslate"><span class="pre">RangePiece</span></code></a>)*
<strong id="grammar-token-RangePiece"><span id="grammar-token-rangepiece"></span>RangePiece </strong> ::= <a class="reference internal" href="#grammar-token-TokInteger"><code class="xref docutils literal notranslate"><span class="pre">TokInteger</span></code></a>
| <a class="reference internal" href="#grammar-token-TokInteger"><code class="xref docutils literal notranslate"><span class="pre">TokInteger</span></code></a> "..." <a class="reference internal" href="#grammar-token-TokInteger"><code class="xref docutils literal notranslate"><span class="pre">TokInteger</span></code></a>
| <a class="reference internal" href="#grammar-token-TokInteger"><code class="xref docutils literal notranslate"><span class="pre">TokInteger</span></code></a> "-" <a class="reference internal" href="#grammar-token-TokInteger"><code class="xref docutils literal notranslate"><span class="pre">TokInteger</span></code></a>
| <a class="reference internal" href="#grammar-token-TokInteger"><code class="xref docutils literal notranslate"><span class="pre">TokInteger</span></code></a> <a class="reference internal" href="#grammar-token-TokInteger"><code class="xref docutils literal notranslate"><span class="pre">TokInteger</span></code></a>
</pre>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>The peculiar last form of <a class="reference internal" href="#grammar-token-RangePiece"><code class="xref std std-token docutils literal notranslate"><span class="pre">RangePiece</span></code></a> is due to the fact that the
“<code class="docutils literal notranslate"><span class="pre">-</span></code>” is included in the <a class="reference internal" href="#grammar-token-TokInteger"><code class="xref std std-token docutils literal notranslate"><span class="pre">TokInteger</span></code></a>, hence <code class="docutils literal notranslate"><span class="pre">1-5</span></code> gets lexed as
two consecutive tokens, with values <code class="docutils literal notranslate"><span class="pre">1</span></code> and <code class="docutils literal notranslate"><span class="pre">-5</span></code>, instead of “1”, “-“,
and “5”. The use of hyphen as the range punctuation is deprecated.</p>
</div>
<div class="section" id="simple-values">
<h3><a class="toc-backref" href="#id11"><span class="sectnum">1.5.1</span> Simple values</a><a class="headerlink" href="#simple-values" title="Permalink to this headline">¶</a></h3>
<p>The <a class="reference internal" href="#grammar-token-SimpleValue"><code class="xref std std-token docutils literal notranslate"><span class="pre">SimpleValue</span></code></a> has a number of forms.</p>
<pre>
<strong id="grammar-token-SimpleValue"><span id="grammar-token-simplevalue"></span>SimpleValue</strong> ::= <a class="reference internal" href="#grammar-token-TokInteger"><code class="xref docutils literal notranslate"><span class="pre">TokInteger</span></code></a> | <a class="reference internal" href="#grammar-token-TokString"><code class="xref docutils literal notranslate"><span class="pre">TokString</span></code></a>+ | <a class="reference internal" href="#grammar-token-TokCode"><code class="xref docutils literal notranslate"><span class="pre">TokCode</span></code></a>
</pre>
<p>A value can be an integer literal, a string literal, or a code literal.
Multiple adjacent string literals are concatenated as in C/C++; the simple
value is the concatenation of the strings. Code literals become strings and
are then indistinguishable from them.</p>
<pre>
<strong id="grammar-token-SimpleValue2"><span id="grammar-token-simplevalue2"></span>SimpleValue2</strong> ::= "true" | "false"
</pre>
<p>The <code class="docutils literal notranslate"><span class="pre">true</span></code> and <code class="docutils literal notranslate"><span class="pre">false</span></code> literals are essentially syntactic sugar for the
integer values 1 and 0. They improve the readability of TableGen files when
boolean values are used in field initializations, bit sequences, <code class="docutils literal notranslate"><span class="pre">if</span></code>
statements, etc. When parsed, these literals are converted to integers.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>Although <code class="docutils literal notranslate"><span class="pre">true</span></code> and <code class="docutils literal notranslate"><span class="pre">false</span></code> are literal names for 1 and 0, we
recommend as a stylistic rule that you use them for boolean
values only.</p>
</div>
<pre>
<strong id="grammar-token-SimpleValue3"><span id="grammar-token-simplevalue3"></span>SimpleValue3</strong> ::= "?"
</pre>
<p>A question mark represents an uninitialized value.</p>
<pre>
<strong id="grammar-token-SimpleValue4"><span id="grammar-token-simplevalue4"></span>SimpleValue4</strong> ::= "{" [<a class="reference internal" href="#grammar-token-ValueList"><code class="xref docutils literal notranslate"><span class="pre">ValueList</span></code></a>] "}"
<strong id="grammar-token-ValueList"><span id="grammar-token-valuelist"></span>ValueList </strong> ::= <a class="reference internal" href="#grammar-token-ValueListNE"><code class="xref docutils literal notranslate"><span class="pre">ValueListNE</span></code></a>
<strong id="grammar-token-ValueListNE"><span id="grammar-token-valuelistne"></span>ValueListNE </strong> ::= <a class="reference internal" href="#grammar-token-Value"><code class="xref docutils literal notranslate"><span class="pre">Value</span></code></a> ("," <a class="reference internal" href="#grammar-token-Value"><code class="xref docutils literal notranslate"><span class="pre">Value</span></code></a>)*
</pre>
<p>This value represents a sequence of bits, which can be used to initialize a
<code class="docutils literal notranslate"><span class="pre">bits<</span></code><em>n</em><code class="docutils literal notranslate"><span class="pre">></span></code> field (note the braces). When doing so, the values
must represent a total of <em>n</em> bits.</p>
<pre>
<strong id="grammar-token-SimpleValue5"><span id="grammar-token-simplevalue5"></span>SimpleValue5</strong> ::= "[" <a class="reference internal" href="#grammar-token-ValueList"><code class="xref docutils literal notranslate"><span class="pre">ValueList</span></code></a> "]" ["<" <a class="reference internal" href="#grammar-token-Type"><code class="xref docutils literal notranslate"><span class="pre">Type</span></code></a> ">"]
</pre>
<p>This value is a list initializer (note the brackets). The values in brackets
are the elements of the list. The optional <a class="reference internal" href="#grammar-token-Type"><code class="xref std std-token docutils literal notranslate"><span class="pre">Type</span></code></a> can be used to
indicate a specific element type; otherwise the element type is inferred
from the given values. TableGen can usually infer the type, although
sometimes not when the value is the empty list (<code class="docutils literal notranslate"><span class="pre">[]</span></code>).</p>
<pre>
<strong id="grammar-token-SimpleValue6"><span id="grammar-token-simplevalue6"></span>SimpleValue6</strong> ::= "(" <a class="reference internal" href="#grammar-token-DagArg"><code class="xref docutils literal notranslate"><span class="pre">DagArg</span></code></a> [<a class="reference internal" href="#grammar-token-DagArgList"><code class="xref docutils literal notranslate"><span class="pre">DagArgList</span></code></a>] ")"
<strong id="grammar-token-DagArgList"><span id="grammar-token-dagarglist"></span>DagArgList </strong> ::= <a class="reference internal" href="#grammar-token-DagArg"><code class="xref docutils literal notranslate"><span class="pre">DagArg</span></code></a> ("," <a class="reference internal" href="#grammar-token-DagArg"><code class="xref docutils literal notranslate"><span class="pre">DagArg</span></code></a>)*
<strong id="grammar-token-DagArg"><span id="grammar-token-dagarg"></span>DagArg </strong> ::= <a class="reference internal" href="#grammar-token-Value"><code class="xref docutils literal notranslate"><span class="pre">Value</span></code></a> [":" <a class="reference internal" href="#grammar-token-TokVarName"><code class="xref docutils literal notranslate"><span class="pre">TokVarName</span></code></a>] | <a class="reference internal" href="#grammar-token-TokVarName"><code class="xref docutils literal notranslate"><span class="pre">TokVarName</span></code></a>
</pre>
<p>This represents a DAG initializer (note the parentheses). The first
<a class="reference internal" href="#grammar-token-DagArg"><code class="xref std std-token docutils literal notranslate"><span class="pre">DagArg</span></code></a> is called the “operator” of the DAG and must be a record.
See <a class="reference internal" href="#directed-acyclic-graphs-dags">Directed acyclic graphs (DAGs)</a> for more details.</p>
<pre>
<strong id="grammar-token-SimpleValue7"><span id="grammar-token-simplevalue7"></span>SimpleValue7</strong> ::= <a class="reference internal" href="#grammar-token-TokIdentifier"><code class="xref docutils literal notranslate"><span class="pre">TokIdentifier</span></code></a>
</pre>
<p>The resulting value is the value of the entity named by the identifier. The
possible identifiers are described here, but the descriptions will make more
sense after reading the remainder of this guide.</p>
<ul>
<li><p>A template argument of a <code class="docutils literal notranslate"><span class="pre">class</span></code>, such as the use of <code class="docutils literal notranslate"><span class="pre">Bar</span></code> in:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Foo</span> <span class="o"><</span><span class="nb">int</span> <span class="n">Bar</span><span class="o">></span> <span class="p">{</span>
<span class="nb">int</span> <span class="n">Baz</span> <span class="o">=</span> <span class="n">Bar</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</div>
</li>
<li><p>The implicit template argument <code class="docutils literal notranslate"><span class="pre">NAME</span></code> in a <code class="docutils literal notranslate"><span class="pre">class</span></code> or <code class="docutils literal notranslate"><span class="pre">multiclass</span></code>
definition (see <a class="reference internal" href="#name">NAME</a>).</p></li>
<li><p>A field local to a <code class="docutils literal notranslate"><span class="pre">class</span></code>, such as the use of <code class="docutils literal notranslate"><span class="pre">Bar</span></code> in:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Foo</span> <span class="p">{</span>
<span class="nb">int</span> <span class="n">Bar</span> <span class="o">=</span> <span class="mi">5</span><span class="p">;</span>
<span class="nb">int</span> <span class="n">Baz</span> <span class="o">=</span> <span class="n">Bar</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</div>
</li>
<li><p>The name of a record definition, such as the use of <code class="docutils literal notranslate"><span class="pre">Bar</span></code> in the
definition of <code class="docutils literal notranslate"><span class="pre">Foo</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">Bar</span> <span class="p">:</span> <span class="n">SomeClass</span> <span class="p">{</span>
<span class="nb">int</span> <span class="n">X</span> <span class="o">=</span> <span class="mi">5</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">def</span> <span class="nf">Foo</span> <span class="p">{</span>
<span class="n">SomeClass</span> <span class="n">Baz</span> <span class="o">=</span> <span class="n">Bar</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</div>
</li>
<li><p>A field local to a record definition, such as the use of <code class="docutils literal notranslate"><span class="pre">Bar</span></code> in:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">Foo</span> <span class="p">{</span>
<span class="nb">int</span> <span class="n">Bar</span> <span class="o">=</span> <span class="mi">5</span><span class="p">;</span>
<span class="nb">int</span> <span class="n">Baz</span> <span class="o">=</span> <span class="n">Bar</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Fields inherited from the record’s parent classes can be accessed the same way.</p>
</li>
<li><p>A template argument of a <code class="docutils literal notranslate"><span class="pre">multiclass</span></code>, such as the use of <code class="docutils literal notranslate"><span class="pre">Bar</span></code> in:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">multiclass</span> <span class="n">Foo</span> <span class="o"><</span><span class="nb">int</span> <span class="n">Bar</span><span class="o">></span> <span class="p">{</span>
<span class="k">def</span> <span class="p">:</span> <span class="n">SomeClass</span><span class="o"><</span><span class="n">Bar</span><span class="o">></span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</div>
</li>
<li><p>A variable defined with the <code class="docutils literal notranslate"><span class="pre">defvar</span></code> or <code class="docutils literal notranslate"><span class="pre">defset</span></code> statements.</p></li>
<li><p>The iteration variable of a <code class="docutils literal notranslate"><span class="pre">foreach</span></code>, such as the use of <code class="docutils literal notranslate"><span class="pre">i</span></code> in:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">foreach</span> <span class="n">i</span> <span class="o">=</span> <span class="mf">0.</span><span class="o">..</span><span class="mi">5</span> <span class="ow">in</span>
<span class="k">def</span> <span class="nf">Foo</span><span class="c1">#i;</span>
</pre></div>
</div>
</li>
</ul>
<pre>
<strong id="grammar-token-SimpleValue8"><span id="grammar-token-simplevalue8"></span>SimpleValue8</strong> ::= <a class="reference internal" href="#grammar-token-ClassID"><code class="xref docutils literal notranslate"><span class="pre">ClassID</span></code></a> "<" <a class="reference internal" href="#grammar-token-ValueListNE"><code class="xref docutils literal notranslate"><span class="pre">ValueListNE</span></code></a> ">"
</pre>
<p>This form creates a new anonymous record definition (as would be created by an
unnamed <code class="docutils literal notranslate"><span class="pre">def</span></code> inheriting from the given class with the given template
arguments; see <a class="reference internal" href="#def">def</a>) and the value is that record. A field of the record can be
obtained using a suffix; see <a class="reference internal" href="#suffixed-values">Suffixed Values</a>.</p>
<p>Invoking a class in this manner can provide a simple subroutine facility.
See <a class="reference internal" href="#using-classes-as-subroutines">Using Classes as Subroutines</a> for more information.</p>
<pre>
<strong id="grammar-token-SimpleValue9"><span id="grammar-token-simplevalue9"></span>SimpleValue9</strong> ::= <a class="reference internal" href="#grammar-token-BangOperator"><code class="xref docutils literal notranslate"><span class="pre">BangOperator</span></code></a> ["<" <a class="reference internal" href="#grammar-token-Type"><code class="xref docutils literal notranslate"><span class="pre">Type</span></code></a> ">"] "(" <a class="reference internal" href="#grammar-token-ValueListNE"><code class="xref docutils literal notranslate"><span class="pre">ValueListNE</span></code></a> ")"
| <a class="reference internal" href="#grammar-token-CondOperator"><code class="xref docutils literal notranslate"><span class="pre">CondOperator</span></code></a> "(" <a class="reference internal" href="#grammar-token-CondClause"><code class="xref docutils literal notranslate"><span class="pre">CondClause</span></code></a> ("," <a class="reference internal" href="#grammar-token-CondClause"><code class="xref docutils literal notranslate"><span class="pre">CondClause</span></code></a>)* ")"
<strong id="grammar-token-CondClause"><span id="grammar-token-condclause"></span>CondClause </strong> ::= <a class="reference internal" href="#grammar-token-Value"><code class="xref docutils literal notranslate"><span class="pre">Value</span></code></a> ":" <a class="reference internal" href="#grammar-token-Value"><code class="xref docutils literal notranslate"><span class="pre">Value</span></code></a>
</pre>
<p>The bang operators provide functions that are not available with the other
simple values. Except in the case of <code class="docutils literal notranslate"><span class="pre">!cond</span></code>, a bang operator takes a list
of arguments enclosed in parentheses and performs some function on those
arguments, producing a value for that bang operator. The <code class="docutils literal notranslate"><span class="pre">!cond</span></code> operator
takes a list of pairs of arguments separated by colons. See <a class="reference internal" href="#appendix-a-bang-operators">Appendix A:
Bang Operators</a> for a description of each bang operator.</p>
</div>
<div class="section" id="suffixed-values">
<h3><a class="toc-backref" href="#id12"><span class="sectnum">1.5.2</span> Suffixed values</a><a class="headerlink" href="#suffixed-values" title="Permalink to this headline">¶</a></h3>
<p>The <a class="reference internal" href="#grammar-token-SimpleValue"><code class="xref std std-token docutils literal notranslate"><span class="pre">SimpleValue</span></code></a> values described above can be specified with
certain suffixes. The purpose of a suffix is to obtain a subvalue of the
primary value. Here are the possible suffixes for some primary <em>value</em>.</p>
<dl class="simple">
<dt><em>value</em><code class="docutils literal notranslate"><span class="pre">{17}</span></code></dt><dd><p>The final value is bit 17 of the integer <em>value</em> (note the braces).</p>
</dd>
<dt><em>value</em><code class="docutils literal notranslate"><span class="pre">{8...15}</span></code></dt><dd><p>The final value is bits 8–15 of the integer <em>value</em>. The order of the
bits can be reversed by specifying <code class="docutils literal notranslate"><span class="pre">{15...8}</span></code>.</p>
</dd>
<dt><em>value</em><code class="docutils literal notranslate"><span class="pre">[4]</span></code></dt><dd><p>The final value is element 4 of the list <em>value</em> (note the brackets).
In other words, the brackets act as a subscripting operator on the list.
This is the case only when a single element is specified.</p>
</dd>
<dt><em>value</em><code class="docutils literal notranslate"><span class="pre">[4...7,17,2...3,4]</span></code></dt><dd><p>The final value is a new list that is a slice of the list <em>value</em>.
The new list contains elements 4, 5, 6, 7, 17, 2, 3, and 4.
Elements may be included multiple times and in any order. This is the result
only when more than one element is specified.</p>
</dd>
<dt><em>value</em><code class="docutils literal notranslate"><span class="pre">.</span></code><em>field</em></dt><dd><p>The final value is the value of the specified <em>field</em> in the specified
record <em>value</em>.</p>
</dd>
</dl>
</div>
<div class="section" id="the-paste-operator">
<h3><a class="toc-backref" href="#id13"><span class="sectnum">1.5.3</span> The paste operator</a><a class="headerlink" href="#the-paste-operator" title="Permalink to this headline">¶</a></h3>
<p>The paste operator (<code class="docutils literal notranslate"><span class="pre">#</span></code>) is the only infix operator available in TableGen
expressions. It allows you to concatenate strings or lists, but has a few
unusual features.</p>
<p>The paste operator can be used when specifying the record name in a
<a class="reference internal" href="#grammar-token-Def"><code class="xref std std-token docutils literal notranslate"><span class="pre">Def</span></code></a> or <a class="reference internal" href="#grammar-token-Defm"><code class="xref std std-token docutils literal notranslate"><span class="pre">Defm</span></code></a> statement, in which case it must construct a
string. If an operand is an undefined name (<a class="reference internal" href="#grammar-token-TokIdentifier"><code class="xref std std-token docutils literal notranslate"><span class="pre">TokIdentifier</span></code></a>) or the
name of a global <a class="reference internal" href="#grammar-token-Defvar"><code class="xref std std-token docutils literal notranslate"><span class="pre">Defvar</span></code></a> or <a class="reference internal" href="#grammar-token-Defset"><code class="xref std std-token docutils literal notranslate"><span class="pre">Defset</span></code></a>, it is treated as a
verbatim string of characters. The value of a global name is not used.</p>
<p>The paste operator can be used in all other value expressions, in which case
it can construct a string or a list. Rather oddly, but consistent with the
previous case, if the <em>right-hand-side</em> operand is an undefined name or a
global name, it is treated as a verbatim string of characters. The
left-hand-side operand is treated normally.</p>
<p><a class="reference internal" href="#appendix-b-paste-operator-examples">Appendix B: Paste Operator Examples</a> presents examples of the behavior of
the paste operator.</p>
</div>
</div>
<div class="section" id="statements">
<h2><a class="toc-backref" href="#id14"><span class="sectnum">1.6</span> Statements</a><a class="headerlink" href="#statements" title="Permalink to this headline">¶</a></h2>
<p>The following statements may appear at the top level of TableGen source
files.</p>
<pre>
<strong id="grammar-token-TableGenFile"><span id="grammar-token-tablegenfile"></span>TableGenFile</strong> ::= <a class="reference internal" href="#grammar-token-Statement"><code class="xref docutils literal notranslate"><span class="pre">Statement</span></code></a>*
<strong id="grammar-token-Statement"><span id="grammar-token-statement"></span>Statement </strong> ::= <a class="reference internal" href="#grammar-token-Assert"><code class="xref docutils literal notranslate"><span class="pre">Assert</span></code></a> | <a class="reference internal" href="#grammar-token-Class"><code class="xref docutils literal notranslate"><span class="pre">Class</span></code></a> | <a class="reference internal" href="#grammar-token-Def"><code class="xref docutils literal notranslate"><span class="pre">Def</span></code></a> | <a class="reference internal" href="#grammar-token-Defm"><code class="xref docutils literal notranslate"><span class="pre">Defm</span></code></a> | <a class="reference internal" href="#grammar-token-Defset"><code class="xref docutils literal notranslate"><span class="pre">Defset</span></code></a> | <a class="reference internal" href="#grammar-token-Defvar"><code class="xref docutils literal notranslate"><span class="pre">Defvar</span></code></a>
| <a class="reference internal" href="#grammar-token-Foreach"><code class="xref docutils literal notranslate"><span class="pre">Foreach</span></code></a> | <a class="reference internal" href="#grammar-token-If"><code class="xref docutils literal notranslate"><span class="pre">If</span></code></a> | <a class="reference internal" href="#grammar-token-Let"><code class="xref docutils literal notranslate"><span class="pre">Let</span></code></a> | <a class="reference internal" href="#grammar-token-MultiClass"><code class="xref docutils literal notranslate"><span class="pre">MultiClass</span></code></a>
</pre>
<p>The following sections describe each of these top-level statements.</p>
<div class="section" id="class-define-an-abstract-record-class">
<h3><a class="toc-backref" href="#id15"><span class="sectnum">1.6.1</span> <code class="docutils literal notranslate"><span class="pre">class</span></code> — define an abstract record class</a><a class="headerlink" href="#class-define-an-abstract-record-class" title="Permalink to this headline">¶</a></h3>
<p>A <code class="docutils literal notranslate"><span class="pre">class</span></code> statement defines an abstract record class from which other
classes and records can inherit.</p>
<pre>
<strong id="grammar-token-Class"><span id="grammar-token-class"></span>Class </strong> ::= "class" <a class="reference internal" href="#grammar-token-ClassID"><code class="xref docutils literal notranslate"><span class="pre">ClassID</span></code></a> [<a class="reference internal" href="#grammar-token-TemplateArgList"><code class="xref docutils literal notranslate"><span class="pre">TemplateArgList</span></code></a>] <a class="reference internal" href="#grammar-token-RecordBody"><code class="xref docutils literal notranslate"><span class="pre">RecordBody</span></code></a>
<strong id="grammar-token-TemplateArgList"><span id="grammar-token-templatearglist"></span>TemplateArgList</strong> ::= "<" <a class="reference internal" href="#grammar-token-TemplateArgDecl"><code class="xref docutils literal notranslate"><span class="pre">TemplateArgDecl</span></code></a> ("," <a class="reference internal" href="#grammar-token-TemplateArgDecl"><code class="xref docutils literal notranslate"><span class="pre">TemplateArgDecl</span></code></a>)* ">"
<strong id="grammar-token-TemplateArgDecl"><span id="grammar-token-templateargdecl"></span>TemplateArgDecl</strong> ::= <a class="reference internal" href="#grammar-token-Type"><code class="xref docutils literal notranslate"><span class="pre">Type</span></code></a> <a class="reference internal" href="#grammar-token-TokIdentifier"><code class="xref docutils literal notranslate"><span class="pre">TokIdentifier</span></code></a> ["=" <a class="reference internal" href="#grammar-token-Value"><code class="xref docutils literal notranslate"><span class="pre">Value</span></code></a>]
</pre>
<p>A class can be parameterized by a list of “template arguments,” whose values
can be used in the class’s record body. These template arguments are
specified each time the class is inherited by another class or record.</p>
<p>If a template argument is not assigned a default value with <code class="docutils literal notranslate"><span class="pre">=</span></code>, it is
uninitialized (has the “value” <code class="docutils literal notranslate"><span class="pre">?</span></code>) and must be specified in the template
argument list when the class is inherited (required argument). If an
argument is assigned a default value, then it need not be specified in the
argument list (optional argument). In the declaration, all required template
arguments must precede any optional arguments. The template argument default
values are evaluated from left to right.</p>
<p>The <a class="reference internal" href="#grammar-token-RecordBody"><code class="xref std std-token docutils literal notranslate"><span class="pre">RecordBody</span></code></a> is defined below. It can include a list of
parent classes from which the current class inherits, along with field
definitions and other statements. When a class <code class="docutils literal notranslate"><span class="pre">C</span></code> inherits from another
class <code class="docutils literal notranslate"><span class="pre">D</span></code>, the fields of <code class="docutils literal notranslate"><span class="pre">D</span></code> are effectively merged into the fields of
<code class="docutils literal notranslate"><span class="pre">C</span></code>.</p>
<p>A given class can only be defined once. A <code class="docutils literal notranslate"><span class="pre">class</span></code> statement is
considered to define the class if <em>any</em> of the following are true (the
<a class="reference internal" href="#grammar-token-RecordBody"><code class="xref std std-token docutils literal notranslate"><span class="pre">RecordBody</span></code></a> elements are described below).</p>
<ul class="simple">
<li><p>The <a class="reference internal" href="#grammar-token-TemplateArgList"><code class="xref std std-token docutils literal notranslate"><span class="pre">TemplateArgList</span></code></a> is present, or</p></li>
<li><p>The <a class="reference internal" href="#grammar-token-ParentClassList"><code class="xref std std-token docutils literal notranslate"><span class="pre">ParentClassList</span></code></a> in the <a class="reference internal" href="#grammar-token-RecordBody"><code class="xref std std-token docutils literal notranslate"><span class="pre">RecordBody</span></code></a> is present, or</p></li>
<li><p>The <a class="reference internal" href="#grammar-token-Body"><code class="xref std std-token docutils literal notranslate"><span class="pre">Body</span></code></a> in the <a class="reference internal" href="#grammar-token-RecordBody"><code class="xref std std-token docutils literal notranslate"><span class="pre">RecordBody</span></code></a> is present and not empty.</p></li>
</ul>
<p>You can declare an empty class by specifying an empty <a class="reference internal" href="#grammar-token-TemplateArgList"><code class="xref std std-token docutils literal notranslate"><span class="pre">TemplateArgList</span></code></a>
and an empty <a class="reference internal" href="#grammar-token-RecordBody"><code class="xref std std-token docutils literal notranslate"><span class="pre">RecordBody</span></code></a>. This can serve as a restricted form of
forward declaration. Note that records derived from a forward-declared
class will inherit no fields from it, because those records are built when
their declarations are parsed, and thus before the class is finally defined.</p>
<p id="name">Every class has an implicit template argument named <code class="docutils literal notranslate"><span class="pre">NAME</span></code> (uppercase),
which is bound to the name of the <a class="reference internal" href="#grammar-token-Def"><code class="xref std std-token docutils literal notranslate"><span class="pre">Def</span></code></a> or <a class="reference internal" href="#grammar-token-Defm"><code class="xref std std-token docutils literal notranslate"><span class="pre">Defm</span></code></a> inheriting
from the class. If the class is inherited by an anonymous record, the name
is unspecified but globally unique.</p>
<p>See <a class="reference internal" href="#examples-classes-and-records">Examples: classes and records</a> for examples.</p>
<div class="section" id="record-bodies">
<h4><a class="toc-backref" href="#id16"><span class="sectnum">1.6.1.1</span> Record Bodies</a><a class="headerlink" href="#record-bodies" title="Permalink to this headline">¶</a></h4>
<p>Record bodies appear in both class and record definitions. A record body can
include a parent class list, which specifies the classes from which the
current class or record inherits fields. Such classes are called the
parent classes of the class or record. The record body also
includes the main body of the definition, which contains the specification
of the fields of the class or record.</p>
<pre>
<strong id="grammar-token-RecordBody"><span id="grammar-token-recordbody"></span>RecordBody </strong> ::= <a class="reference internal" href="#grammar-token-ParentClassList"><code class="xref docutils literal notranslate"><span class="pre">ParentClassList</span></code></a> <a class="reference internal" href="#grammar-token-Body"><code class="xref docutils literal notranslate"><span class="pre">Body</span></code></a>
<strong id="grammar-token-ParentClassList"><span id="grammar-token-parentclasslist"></span>ParentClassList </strong> ::= [":" <a class="reference internal" href="#grammar-token-ParentClassListNE"><code class="xref docutils literal notranslate"><span class="pre">ParentClassListNE</span></code></a>]
<strong id="grammar-token-ParentClassListNE"><span id="grammar-token-parentclasslistne"></span>ParentClassListNE</strong> ::= <a class="reference internal" href="#grammar-token-ClassRef"><code class="xref docutils literal notranslate"><span class="pre">ClassRef</span></code></a> ("," <a class="reference internal" href="#grammar-token-ClassRef"><code class="xref docutils literal notranslate"><span class="pre">ClassRef</span></code></a>)*
<strong id="grammar-token-ClassRef"><span id="grammar-token-classref"></span>ClassRef </strong> ::= (<a class="reference internal" href="#grammar-token-ClassID"><code class="xref docutils literal notranslate"><span class="pre">ClassID</span></code></a> | <a class="reference internal" href="#grammar-token-MultiClassID"><code class="xref docutils literal notranslate"><span class="pre">MultiClassID</span></code></a>) ["<" [<a class="reference internal" href="#grammar-token-ValueList"><code class="xref docutils literal notranslate"><span class="pre">ValueList</span></code></a>] ">"]
</pre>
<p>A <a class="reference internal" href="#grammar-token-ParentClassList"><code class="xref std std-token docutils literal notranslate"><span class="pre">ParentClassList</span></code></a> containing a <a class="reference internal" href="#grammar-token-MultiClassID"><code class="xref std std-token docutils literal notranslate"><span class="pre">MultiClassID</span></code></a> is valid only
in the class list of a <code class="docutils literal notranslate"><span class="pre">defm</span></code> statement. In that case, the ID must be the
name of a multiclass.</p>
<pre>
<strong id="grammar-token-Body"><span id="grammar-token-body"></span>Body </strong> ::= ";" | "{" <a class="reference internal" href="#grammar-token-BodyItem"><code class="xref docutils literal notranslate"><span class="pre">BodyItem</span></code></a>* "}"
<strong id="grammar-token-BodyItem"><span id="grammar-token-bodyitem"></span>BodyItem</strong> ::= (<a class="reference internal" href="#grammar-token-Type"><code class="xref docutils literal notranslate"><span class="pre">Type</span></code></a> | "code") <a class="reference internal" href="#grammar-token-TokIdentifier"><code class="xref docutils literal notranslate"><span class="pre">TokIdentifier</span></code></a> ["=" <a class="reference internal" href="#grammar-token-Value"><code class="xref docutils literal notranslate"><span class="pre">Value</span></code></a>] ";"
| "let" <a class="reference internal" href="#grammar-token-TokIdentifier"><code class="xref docutils literal notranslate"><span class="pre">TokIdentifier</span></code></a> ["{" <a class="reference internal" href="#grammar-token-RangeList"><code class="xref docutils literal notranslate"><span class="pre">RangeList</span></code></a> "}"] "=" <a class="reference internal" href="#grammar-token-Value"><code class="xref docutils literal notranslate"><span class="pre">Value</span></code></a> ";"
| "defvar" <a class="reference internal" href="#grammar-token-TokIdentifier"><code class="xref docutils literal notranslate"><span class="pre">TokIdentifier</span></code></a> "=" <a class="reference internal" href="#grammar-token-Value"><code class="xref docutils literal notranslate"><span class="pre">Value</span></code></a> ";"
| <a class="reference internal" href="#grammar-token-Assert"><code class="xref docutils literal notranslate"><span class="pre">Assert</span></code></a>
</pre>
<p>A field definition in the body specifies a field to be included in the class
or record. If no initial value is specified, then the field’s value is
uninitialized. The type must be specified; TableGen will not infer it from
the value. The keyword <code class="docutils literal notranslate"><span class="pre">code</span></code> may be used to emphasize that the field
has a string value that is code.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">let</span></code> form is used to reset a field to a new value. This can be done
for fields defined directly in the body or fields inherited from parent
classes. A <a class="reference internal" href="#grammar-token-RangeList"><code class="xref std std-token docutils literal notranslate"><span class="pre">RangeList</span></code></a> can be specified to reset certain bits in a
<code class="docutils literal notranslate"><span class="pre">bit<n></span></code> field.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">defvar</span></code> form defines a variable whose value can be used in other
value expressions within the body. The variable is not a field: it does not
become a field of the class or record being defined. Variables are provided
to hold temporary values while processing the body. See <a class="reference internal" href="#defvar-in-a-record-body">Defvar in a Record
Body</a> for more details.</p>
<p>When class <code class="docutils literal notranslate"><span class="pre">C2</span></code> inherits from class <code class="docutils literal notranslate"><span class="pre">C1</span></code>, it acquires all the field
definitions of <code class="docutils literal notranslate"><span class="pre">C1</span></code>. As those definitions are merged into class <code class="docutils literal notranslate"><span class="pre">C2</span></code>, any
template arguments passed to <code class="docutils literal notranslate"><span class="pre">C1</span></code> by <code class="docutils literal notranslate"><span class="pre">C2</span></code> are substituted into the
definitions. In other words, the abstract record fields defined by <code class="docutils literal notranslate"><span class="pre">C1</span></code> are
expanded with the template arguments before being merged into <code class="docutils literal notranslate"><span class="pre">C2</span></code>.</p>
</div>
</div>
<div class="section" id="def-define-a-concrete-record">
<span id="def"></span><h3><a class="toc-backref" href="#id17"><span class="sectnum">1.6.2</span> <code class="docutils literal notranslate"><span class="pre">def</span></code> — define a concrete record</a><a class="headerlink" href="#def-define-a-concrete-record" title="Permalink to this headline">¶</a></h3>
<p>A <code class="docutils literal notranslate"><span class="pre">def</span></code> statement defines a new concrete record.</p>
<pre>
<strong id="grammar-token-Def"><span id="grammar-token-def"></span>Def </strong> ::= "def" [<a class="reference internal" href="#grammar-token-NameValue"><code class="xref docutils literal notranslate"><span class="pre">NameValue</span></code></a>] <a class="reference internal" href="#grammar-token-RecordBody"><code class="xref docutils literal notranslate"><span class="pre">RecordBody</span></code></a>
<strong id="grammar-token-NameValue"><span id="grammar-token-namevalue"></span>NameValue</strong> ::= <a class="reference internal" href="#grammar-token-Value"><code class="xref docutils literal notranslate"><span class="pre">Value</span></code></a> (parsed in a special mode)
</pre>
<p>The name value is optional. If specified, it is parsed in a special mode
where undefined (unrecognized) identifiers are interpreted as literal
strings. In particular, global identifiers are considered unrecognized.
These include global variables defined by <code class="docutils literal notranslate"><span class="pre">defvar</span></code> and <code class="docutils literal notranslate"><span class="pre">defset</span></code>. A
record name can be the null string.</p>
<p>If no name value is given, the record is <em>anonymous</em>. The final name of an
anonymous record is unspecified but globally unique.</p>
<p>Special handling occurs if a <code class="docutils literal notranslate"><span class="pre">def</span></code> appears inside a <code class="docutils literal notranslate"><span class="pre">multiclass</span></code>
statement. See the <code class="docutils literal notranslate"><span class="pre">multiclass</span></code> section below for details.</p>
<p>A record can inherit from one or more classes by specifying the
<a class="reference internal" href="#grammar-token-ParentClassList"><code class="xref std std-token docutils literal notranslate"><span class="pre">ParentClassList</span></code></a> clause at the beginning of its record body. All of
the fields in the parent classes are added to the record. If two or more
parent classes provide the same field, the record ends up with the field value
of the last parent class.</p>
<p>As a special case, the name of a record can be passed as a template argument
to that record’s parent classes. For example:</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>class A <dag d> {
dag the_dag = d;
}
def rec1 : A<(ops rec1)>
</pre></div>
</div>
<p>The DAG <code class="docutils literal notranslate"><span class="pre">(ops</span> <span class="pre">rec1)</span></code> is passed as a template argument to class <code class="docutils literal notranslate"><span class="pre">A</span></code>. Notice
that the DAG includes <code class="docutils literal notranslate"><span class="pre">rec1</span></code>, the record being defined.</p>
<p>The steps taken to create a new record are somewhat complex. See <a class="reference internal" href="#how-records-are-built">How
records are built</a>.</p>
<p>See <a class="reference internal" href="#examples-classes-and-records">Examples: classes and records</a> for examples.</p>
</div>
<div class="section" id="examples-classes-and-records">
<h3><a class="toc-backref" href="#id18"><span class="sectnum">1.6.3</span> Examples: classes and records</a><a class="headerlink" href="#examples-classes-and-records" title="Permalink to this headline">¶</a></h3>
<p>Here is a simple TableGen file with one class and two record definitions.</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>class C {
bit V = true;
}
def X : C;
def Y : C {
let V = false;
string Greeting = "Hello!";
}
</pre></div>
</div>
<p>First, the abstract class <code class="docutils literal notranslate"><span class="pre">C</span></code> is defined. It has one field named <code class="docutils literal notranslate"><span class="pre">V</span></code>
that is a bit initialized to true.</p>
<p>Next, two records are defined, derived from class <code class="docutils literal notranslate"><span class="pre">C</span></code>; that is, with <code class="docutils literal notranslate"><span class="pre">C</span></code>
as their parent class. Thus they both inherit the <code class="docutils literal notranslate"><span class="pre">V</span></code> field. Record <code class="docutils literal notranslate"><span class="pre">Y</span></code>
also defines another string field, <code class="docutils literal notranslate"><span class="pre">Greeting</span></code>, which is initialized to
<code class="docutils literal notranslate"><span class="pre">"Hello!"</span></code>. In addition, <code class="docutils literal notranslate"><span class="pre">Y</span></code> overrides the inherited <code class="docutils literal notranslate"><span class="pre">V</span></code> field,
setting it to false.</p>
<p>A class is useful for isolating the common features of multiple records in
one place. A class can initialize common fields to default values, but
records inheriting from that class can override the defaults.</p>
<p>TableGen supports the definition of parameterized classes as well as
nonparameterized ones. Parameterized classes specify a list of variable
declarations, which may optionally have defaults, that are bound when the
class is specified as a parent class of another class or record.</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>class FPFormat <bits<3> val> {
bits<3> Value = val;
}
def NotFP : FPFormat<0>;
def ZeroArgFP : FPFormat<1>;
def OneArgFP : FPFormat<2>;
def OneArgFPRW : FPFormat<3>;
def TwoArgFP : FPFormat<4>;
def CompareFP : FPFormat<5>;
def CondMovFP : FPFormat<6>;
def SpecialFP : FPFormat<7>;
</pre></div>
</div>
<p>The purpose of the <code class="docutils literal notranslate"><span class="pre">FPFormat</span></code> class is to act as a sort of enumerated
type. It provides a single field, <code class="docutils literal notranslate"><span class="pre">Value</span></code>, which holds a 3-bit number. Its
template argument, <code class="docutils literal notranslate"><span class="pre">val</span></code>, is used to set the <code class="docutils literal notranslate"><span class="pre">Value</span></code> field. Each of the
eight records is defined with <code class="docutils literal notranslate"><span class="pre">FPFormat</span></code> as its parent class. The
enumeration value is passed in angle brackets as the template argument. Each
record will inherent the <code class="docutils literal notranslate"><span class="pre">Value</span></code> field with the appropriate enumeration
value.</p>
<p>Here is a more complex example of classes with template arguments. First, we
define a class similar to the <code class="docutils literal notranslate"><span class="pre">FPFormat</span></code> class above. It takes a template
argument and uses it to initialize a field named <code class="docutils literal notranslate"><span class="pre">Value</span></code>. Then we define
four records that inherit the <code class="docutils literal notranslate"><span class="pre">Value</span></code> field with its four different
integer values.</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>class ModRefVal <bits<2> val> {
bits<2> Value = val;
}
def None : ModRefVal<0>;
def Mod : ModRefVal<1>;
def Ref : ModRefVal<2>;
def ModRef : ModRefVal<3>;
</pre></div>
</div>
<p>This is somewhat contrived, but let’s say we would like to examine the two
bits of the <code class="docutils literal notranslate"><span class="pre">Value</span></code> field independently. We can define a class that
accepts a <code class="docutils literal notranslate"><span class="pre">ModRefVal</span></code> record as a template argument and splits up its
value into two fields, one bit each. Then we can define records that inherit from
<code class="docutils literal notranslate"><span class="pre">ModRefBits</span></code> and so acquire two fields from it, one for each bit in the
<code class="docutils literal notranslate"><span class="pre">ModRefVal</span></code> record passed as the template argument.</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>class ModRefBits <ModRefVal mrv> {
// Break the value up into its bits, which can provide a nice
// interface to the ModRefVal values.
bit isMod = mrv.Value{0};
bit isRef = mrv.Value{1};
}
// Example uses.
def foo : ModRefBits<Mod>;
def bar : ModRefBits<Ref>;
def snork : ModRefBits<ModRef>;
</pre></div>
</div>
<p>This illustrates how one class can be defined to reorganize the
fields in another class, thus hiding the internal representation of that
other class.</p>
<p>Running <code class="docutils literal notranslate"><span class="pre">llvm-tblgen</span></code> on the example prints the following definitions:</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>def bar { // Value
bit isMod = 0;
bit isRef = 1;
}
def foo { // Value
bit isMod = 1;
bit isRef = 0;
}
def snork { // Value
bit isMod = 1;
bit isRef = 1;
}
</pre></div>
</div>
</div>
<div class="section" id="let-override-fields-in-classes-or-records">
<h3><a class="toc-backref" href="#id19"><span class="sectnum">1.6.4</span> <code class="docutils literal notranslate"><span class="pre">let</span></code> — override fields in classes or records</a><a class="headerlink" href="#let-override-fields-in-classes-or-records" title="Permalink to this headline">¶</a></h3>
<p>A <code class="docutils literal notranslate"><span class="pre">let</span></code> statement collects a set of field values (sometimes called
<em>bindings</em>) and applies them to all the classes and records defined by
statements within the scope of the <code class="docutils literal notranslate"><span class="pre">let</span></code>.</p>
<pre>
<strong id="grammar-token-Let"><span id="grammar-token-let"></span>Let </strong> ::= "let" <a class="reference internal" href="#grammar-token-LetList"><code class="xref docutils literal notranslate"><span class="pre">LetList</span></code></a> "in" "{" <a class="reference internal" href="#grammar-token-Statement"><code class="xref docutils literal notranslate"><span class="pre">Statement</span></code></a>* "}"
| "let" <a class="reference internal" href="#grammar-token-LetList"><code class="xref docutils literal notranslate"><span class="pre">LetList</span></code></a> "in" <a class="reference internal" href="#grammar-token-Statement"><code class="xref docutils literal notranslate"><span class="pre">Statement</span></code></a>
<strong id="grammar-token-LetList"><span id="grammar-token-letlist"></span>LetList</strong> ::= <a class="reference internal" href="#grammar-token-LetItem"><code class="xref docutils literal notranslate"><span class="pre">LetItem</span></code></a> ("," <a class="reference internal" href="#grammar-token-LetItem"><code class="xref docutils literal notranslate"><span class="pre">LetItem</span></code></a>)*
<strong id="grammar-token-LetItem"><span id="grammar-token-letitem"></span>LetItem</strong> ::= <a class="reference internal" href="#grammar-token-TokIdentifier"><code class="xref docutils literal notranslate"><span class="pre">TokIdentifier</span></code></a> ["<" <a class="reference internal" href="#grammar-token-RangeList"><code class="xref docutils literal notranslate"><span class="pre">RangeList</span></code></a> ">"] "=" <a class="reference internal" href="#grammar-token-Value"><code class="xref docutils literal notranslate"><span class="pre">Value</span></code></a>
</pre>
<p>The <code class="docutils literal notranslate"><span class="pre">let</span></code> statement establishes a scope, which is a sequence of statements
in braces or a single statement with no braces. The bindings in the
<a class="reference internal" href="#grammar-token-LetList"><code class="xref std std-token docutils literal notranslate"><span class="pre">LetList</span></code></a> apply to the statements in that scope.</p>
<p>The field names in the <a class="reference internal" href="#grammar-token-LetList"><code class="xref std std-token docutils literal notranslate"><span class="pre">LetList</span></code></a> must name fields in classes inherited by
the classes and records defined in the statements. The field values are
applied to the classes and records <em>after</em> the records inherit all the fields from
their parent classes. So the <code class="docutils literal notranslate"><span class="pre">let</span></code> acts to override inherited field
values. A <code class="docutils literal notranslate"><span class="pre">let</span></code> cannot override the value of a template argument.</p>
<p>Top-level <code class="docutils literal notranslate"><span class="pre">let</span></code> statements are often useful when a few fields need to be
overridden in several records. Here are two examples. Note that <code class="docutils literal notranslate"><span class="pre">let</span></code>
statements can be nested.</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>let isTerminator = true, isReturn = true, isBarrier = true, hasCtrlDep = true in
def RET : I<0xC3, RawFrm, (outs), (ins), "ret", [(X86retflag 0)]>;
let isCall = true in
// All calls clobber the non-callee saved registers...
let Defs = [EAX, ECX, EDX, FP0, FP1, FP2, FP3, FP4, FP5, FP6, ST0,
MM0, MM1, MM2, MM3, MM4, MM5, MM6, MM7, XMM0, XMM1, XMM2,
XMM3, XMM4, XMM5, XMM6, XMM7, EFLAGS] in {
def CALLpcrel32 : Ii32<0xE8, RawFrm, (outs), (ins i32imm:$dst, variable_ops),
"call\t${dst:call}", []>;
def CALL32r : I<0xFF, MRM2r, (outs), (ins GR32:$dst, variable_ops),
"call\t{*}$dst", [(X86call GR32:$dst)]>;
def CALL32m : I<0xFF, MRM2m, (outs), (ins i32mem:$dst, variable_ops),
"call\t{*}$dst", []>;
}
</pre></div>
</div>
<p>Note that a top-level <code class="docutils literal notranslate"><span class="pre">let</span></code> will not override fields defined in the classes or records
themselves.</p>
</div>
<div class="section" id="multiclass-define-multiple-records">
<h3><a class="toc-backref" href="#id20"><span class="sectnum">1.6.5</span> <code class="docutils literal notranslate"><span class="pre">multiclass</span></code> — define multiple records</a><a class="headerlink" href="#multiclass-define-multiple-records" title="Permalink to this headline">¶</a></h3>
<p>While classes with template arguments are a good way to factor out commonality
between multiple records, multiclasses allow a convenient method for
defining many records at once. For example, consider a 3-address
instruction architecture whose instructions come in two formats: <code class="docutils literal notranslate"><span class="pre">reg</span> <span class="pre">=</span> <span class="pre">reg</span>
<span class="pre">op</span> <span class="pre">reg</span></code> and <code class="docutils literal notranslate"><span class="pre">reg</span> <span class="pre">=</span> <span class="pre">reg</span> <span class="pre">op</span> <span class="pre">imm</span></code> (e.g., SPARC). We would like to specify in
one place that these two common formats exist, then in a separate place
specify what all the operations are. The <code class="docutils literal notranslate"><span class="pre">multiclass</span></code> and <code class="docutils literal notranslate"><span class="pre">defm</span></code>
statements accomplish this goal. You can think of a multiclass as a macro or
template that expands into multiple records.</p>
<pre>
<strong id="grammar-token-MultiClass"><span id="grammar-token-multiclass"></span>MultiClass </strong> ::= "multiclass" <a class="reference internal" href="#grammar-token-TokIdentifier"><code class="xref docutils literal notranslate"><span class="pre">TokIdentifier</span></code></a> [<a class="reference internal" href="#grammar-token-TemplateArgList"><code class="xref docutils literal notranslate"><span class="pre">TemplateArgList</span></code></a>]
[":" <a class="reference internal" href="#grammar-token-ParentMultiClassList"><code class="xref docutils literal notranslate"><span class="pre">ParentMultiClassList</span></code></a>]
"{" <a class="reference internal" href="#grammar-token-MultiClassStatement"><code class="xref docutils literal notranslate"><span class="pre">MultiClassStatement</span></code></a>+ "}"
<strong id="grammar-token-ParentMultiClassList"><span id="grammar-token-parentmulticlasslist"></span>ParentMultiClassList</strong> ::= <a class="reference internal" href="#grammar-token-MultiClassID"><code class="xref docutils literal notranslate"><span class="pre">MultiClassID</span></code></a> ("," <a class="reference internal" href="#grammar-token-MultiClassID"><code class="xref docutils literal notranslate"><span class="pre">MultiClassID</span></code></a>)*
<strong id="grammar-token-MultiClassID"><span id="grammar-token-multiclassid"></span>MultiClassID </strong> ::= <a class="reference internal" href="#grammar-token-TokIdentifier"><code class="xref docutils literal notranslate"><span class="pre">TokIdentifier</span></code></a>
<strong id="grammar-token-MultiClassStatement"><span id="grammar-token-multiclassstatement"></span>MultiClassStatement </strong> ::= <a class="reference internal" href="#grammar-token-Assert"><code class="xref docutils literal notranslate"><span class="pre">Assert</span></code></a> | <a class="reference internal" href="#grammar-token-Def"><code class="xref docutils literal notranslate"><span class="pre">Def</span></code></a> | <a class="reference internal" href="#grammar-token-Defm"><code class="xref docutils literal notranslate"><span class="pre">Defm</span></code></a> | <a class="reference internal" href="#grammar-token-Defvar"><code class="xref docutils literal notranslate"><span class="pre">Defvar</span></code></a> | <a class="reference internal" href="#grammar-token-Foreach"><code class="xref docutils literal notranslate"><span class="pre">Foreach</span></code></a> | <a class="reference internal" href="#grammar-token-If"><code class="xref docutils literal notranslate"><span class="pre">If</span></code></a> | <a class="reference internal" href="#grammar-token-Let"><code class="xref docutils literal notranslate"><span class="pre">Let</span></code></a>
</pre>
<p>As with regular classes, the multiclass has a name and can accept template
arguments. A multiclass can inherit from other multiclasses, which causes
the other multiclasses to be expanded and contribute to the record
definitions in the inheriting multiclass. The body of the multiclass
contains a series of statements that define records, using <a class="reference internal" href="#grammar-token-Def"><code class="xref std std-token docutils literal notranslate"><span class="pre">Def</span></code></a> and
<a class="reference internal" href="#grammar-token-Defm"><code class="xref std std-token docutils literal notranslate"><span class="pre">Defm</span></code></a>. In addition, <a class="reference internal" href="#grammar-token-Defvar"><code class="xref std std-token docutils literal notranslate"><span class="pre">Defvar</span></code></a>, <a class="reference internal" href="#grammar-token-Foreach"><code class="xref std std-token docutils literal notranslate"><span class="pre">Foreach</span></code></a>, and
<a class="reference internal" href="#grammar-token-Let"><code class="xref std std-token docutils literal notranslate"><span class="pre">Let</span></code></a> statements can be used to factor out even more common elements.
The <a class="reference internal" href="#grammar-token-If"><code class="xref std std-token docutils literal notranslate"><span class="pre">If</span></code></a> and <a class="reference internal" href="#grammar-token-Assert"><code class="xref std std-token docutils literal notranslate"><span class="pre">Assert</span></code></a> statements can also be used.</p>
<p>Also as with regular classes, the multiclass has the implicit template
argument <code class="docutils literal notranslate"><span class="pre">NAME</span></code> (see <a class="reference internal" href="#name">NAME</a>). When a named (non-anonymous) record is
defined in a multiclass and the record’s name does not include a use of the
template argument <code class="docutils literal notranslate"><span class="pre">NAME</span></code>, such a use is automatically <em>prepended</em>
to the name. That is, the following are equivalent inside a multiclass:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">Foo</span> <span class="o">...</span>
<span class="k">def</span> <span class="nf">NAME</span> <span class="c1"># Foo ...</span>
</pre></div>
</div>
<p>The records defined in a multiclass are created when the multiclass is
“instantiated” or “invoked” by a <code class="docutils literal notranslate"><span class="pre">defm</span></code> statement outside the multiclass
definition. Each <code class="docutils literal notranslate"><span class="pre">def</span></code> statement in the multiclass produces a record. As
with top-level <code class="docutils literal notranslate"><span class="pre">def</span></code> statements, these definitions can inherit from
multiple parent classes.</p>
<p>See <a class="reference internal" href="#examples-multiclasses-and-defms">Examples: multiclasses and defms</a> for examples.</p>
</div>
<div class="section" id="defm-invoke-multiclasses-to-define-multiple-records">
<h3><a class="toc-backref" href="#id21"><span class="sectnum">1.6.6</span> <code class="docutils literal notranslate"><span class="pre">defm</span></code> — invoke multiclasses to define multiple records</a><a class="headerlink" href="#defm-invoke-multiclasses-to-define-multiple-records" title="Permalink to this headline">¶</a></h3>
<p>Once multiclasses have been defined, you use the <code class="docutils literal notranslate"><span class="pre">defm</span></code> statement to
“invoke” them and process the multiple record definitions in those
multiclasses. Those record definitions are specified by <code class="docutils literal notranslate"><span class="pre">def</span></code>
statements in the multiclasses, and indirectly by <code class="docutils literal notranslate"><span class="pre">defm</span></code> statements.</p>
<pre>
<strong id="grammar-token-Defm"><span id="grammar-token-defm"></span>Defm</strong> ::= "defm" [<a class="reference internal" href="#grammar-token-NameValue"><code class="xref docutils literal notranslate"><span class="pre">NameValue</span></code></a>] <a class="reference internal" href="#grammar-token-ParentClassList"><code class="xref docutils literal notranslate"><span class="pre">ParentClassList</span></code></a> ";"
</pre>
<p>The optional <a class="reference internal" href="#grammar-token-NameValue"><code class="xref std std-token docutils literal notranslate"><span class="pre">NameValue</span></code></a> is formed in the same way as the name of a
<code class="docutils literal notranslate"><span class="pre">def</span></code>. The <a class="reference internal" href="#grammar-token-ParentClassList"><code class="xref std std-token docutils literal notranslate"><span class="pre">ParentClassList</span></code></a> is a colon followed by a list of at
least one multiclass and any number of regular classes. The multiclasses
must precede the regular classes. Note that the <code class="docutils literal notranslate"><span class="pre">defm</span></code> does not have a
body.</p>
<p>This statement instantiates all the records defined in all the specified
multiclasses, either directly by <code class="docutils literal notranslate"><span class="pre">def</span></code> statements or indirectly by
<code class="docutils literal notranslate"><span class="pre">defm</span></code> statements. These records also receive the fields defined in any
regular classes included in the parent class list. This is useful for adding
a common set of fields to all the records created by the <code class="docutils literal notranslate"><span class="pre">defm</span></code>.</p>
<p>The name is parsed in the same special mode used by <code class="docutils literal notranslate"><span class="pre">def</span></code>. If the name is
not included, an unspecified but globally unique name is provided. That is,
the following examples end up with different names:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">defm</span> <span class="p">:</span> <span class="n">SomeMultiClass</span><span class="o"><...></span><span class="p">;</span> <span class="o">//</span> <span class="n">A</span> <span class="n">globally</span> <span class="n">unique</span> <span class="n">name</span><span class="o">.</span>
<span class="n">defm</span> <span class="s2">""</span> <span class="p">:</span> <span class="n">SomeMultiClass</span><span class="o"><...></span><span class="p">;</span> <span class="o">//</span> <span class="n">An</span> <span class="n">empty</span> <span class="n">name</span><span class="o">.</span>
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">defm</span></code> statement can be used in a multiclass body. When this occurs,
the second variant is equivalent to:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">defm</span> <span class="n">NAME</span> <span class="p">:</span> <span class="n">SomeMultiClass</span><span class="o"><...></span><span class="p">;</span>
</pre></div>
</div>
<p>More generally, when <code class="docutils literal notranslate"><span class="pre">defm</span></code> occurs in a multiclass and its name does not
include a use of the implicit template argument <code class="docutils literal notranslate"><span class="pre">NAME</span></code>, then <code class="docutils literal notranslate"><span class="pre">NAME</span></code> will
be prepended automatically. That is, the following are equivalent inside a
multiclass:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">defm</span> <span class="n">Foo</span> <span class="p">:</span> <span class="n">SomeMultiClass</span><span class="o"><...></span><span class="p">;</span>
<span class="n">defm</span> <span class="n">NAME</span> <span class="c1"># Foo : SomeMultiClass<...>;</span>
</pre></div>
</div>
<p>See <a class="reference internal" href="#examples-multiclasses-and-defms">Examples: multiclasses and defms</a> for examples.</p>
</div>
<div class="section" id="examples-multiclasses-and-defms">
<h3><a class="toc-backref" href="#id22"><span class="sectnum">1.6.7</span> Examples: multiclasses and defms</a><a class="headerlink" href="#examples-multiclasses-and-defms" title="Permalink to this headline">¶</a></h3>
<p>Here is a simple example using <code class="docutils literal notranslate"><span class="pre">multiclass</span></code> and <code class="docutils literal notranslate"><span class="pre">defm</span></code>. Consider a
3-address instruction architecture whose instructions come in two formats:
<code class="docutils literal notranslate"><span class="pre">reg</span> <span class="pre">=</span> <span class="pre">reg</span> <span class="pre">op</span> <span class="pre">reg</span></code> and <code class="docutils literal notranslate"><span class="pre">reg</span> <span class="pre">=</span> <span class="pre">reg</span> <span class="pre">op</span> <span class="pre">imm</span></code> (immediate). The SPARC is an
example of such an architecture.</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>def ops;
def GPR;
def Imm;
class inst <int opc, string asmstr, dag operandlist>;
multiclass ri_inst <int opc, string asmstr> {
def _rr : inst<opc, !strconcat(asmstr, " $dst, $src1, $src2"),
(ops GPR:$dst, GPR:$src1, GPR:$src2)>;
def _ri : inst<opc, !strconcat(asmstr, " $dst, $src1, $src2"),
(ops GPR:$dst, GPR:$src1, Imm:$src2)>;
}
// Define records for each instruction in the RR and RI formats.
defm ADD : ri_inst<0b111, "add">;
defm SUB : ri_inst<0b101, "sub">;
defm MUL : ri_inst<0b100, "mul">;
</pre></div>
</div>
<p>Each use of the <code class="docutils literal notranslate"><span class="pre">ri_inst</span></code> multiclass defines two records, one with the
<code class="docutils literal notranslate"><span class="pre">_rr</span></code> suffix and one with <code class="docutils literal notranslate"><span class="pre">_ri</span></code>. Recall that the name of the <code class="docutils literal notranslate"><span class="pre">defm</span></code>
that uses a multiclass is prepended to the names of the records defined in
that multiclass. So the resulting definitions are named:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">ADD_rr</span><span class="p">,</span> <span class="n">ADD_ri</span>
<span class="n">SUB_rr</span><span class="p">,</span> <span class="n">SUB_ri</span>
<span class="n">MUL_rr</span><span class="p">,</span> <span class="n">MUL_ri</span>
</pre></div>
</div>
<p>Without the <code class="docutils literal notranslate"><span class="pre">multiclass</span></code> feature, the instructions would have to be
defined as follows.</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>def ops;
def GPR;
def Imm;
class inst <int opc, string asmstr, dag operandlist>;
class rrinst <int opc, string asmstr>
: inst<opc, !strconcat(asmstr, " $dst, $src1, $src2"),
(ops GPR:$dst, GPR:$src1, GPR:$src2)>;
class riinst <int opc, string asmstr>
: inst<opc, !strconcat(asmstr, " $dst, $src1, $src2"),
(ops GPR:$dst, GPR:$src1, Imm:$src2)>;
// Define records for each instruction in the RR and RI formats.
def ADD_rr : rrinst<0b111, "add">;
def ADD_ri : riinst<0b111, "add">;
def SUB_rr : rrinst<0b101, "sub">;
def SUB_ri : riinst<0b101, "sub">;
def MUL_rr : rrinst<0b100, "mul">;
def MUL_ri : riinst<0b100, "mul">;
</pre></div>
</div>
<p>A <code class="docutils literal notranslate"><span class="pre">defm</span></code> can be used in a multiclass to “invoke” other multiclasses and
create the records defined in those multiclasses in addition to the records
defined in the current multiclass. In the following example, the <code class="docutils literal notranslate"><span class="pre">basic_s</span></code>
and <code class="docutils literal notranslate"><span class="pre">basic_p</span></code> multiclasses contain <code class="docutils literal notranslate"><span class="pre">defm</span></code> statements that refer to the
<code class="docutils literal notranslate"><span class="pre">basic_r</span></code> multiclass. The <code class="docutils literal notranslate"><span class="pre">basic_r</span></code> multiclass contains only <code class="docutils literal notranslate"><span class="pre">def</span></code>
statements.</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>class Instruction <bits<4> opc, string Name> {
bits<4> opcode = opc;
string name = Name;
}
multiclass basic_r <bits<4> opc> {
def rr : Instruction<opc, "rr">;
def rm : Instruction<opc, "rm">;
}
multiclass basic_s <bits<4> opc> {
defm SS : basic_r<opc>;
defm SD : basic_r<opc>;
def X : Instruction<opc, "x">;
}
multiclass basic_p <bits<4> opc> {
defm PS : basic_r<opc>;
defm PD : basic_r<opc>;
def Y : Instruction<opc, "y">;
}
defm ADD : basic_s<0xf>, basic_p<0xf>;
</pre></div>
</div>
<p>The final <code class="docutils literal notranslate"><span class="pre">defm</span></code> creates the following records, five from the <code class="docutils literal notranslate"><span class="pre">basic_s</span></code>
multiclass and five from the <code class="docutils literal notranslate"><span class="pre">basic_p</span></code> multiclass:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">ADDSSrr</span><span class="p">,</span> <span class="n">ADDSSrm</span>
<span class="n">ADDSDrr</span><span class="p">,</span> <span class="n">ADDSDrm</span>
<span class="n">ADDX</span>
<span class="n">ADDPSrr</span><span class="p">,</span> <span class="n">ADDPSrm</span>
<span class="n">ADDPDrr</span><span class="p">,</span> <span class="n">ADDPDrm</span>
<span class="n">ADDY</span>
</pre></div>
</div>
<p>A <code class="docutils literal notranslate"><span class="pre">defm</span></code> statement, both at top level and in a multiclass, can inherit
from regular classes in addition to multiclasses. The rule is that the
regular classes must be listed after the multiclasses, and there must be at least
one multiclass.</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>class XD {
bits<4> Prefix = 11;
}
class XS {
bits<4> Prefix = 12;
}
class I <bits<4> op> {
bits<4> opcode = op;
}
multiclass R {
def rr : I<4>;
def rm : I<2>;
}
multiclass Y {
defm SS : R, XD; // First multiclass R, then regular class XD.
defm SD : R, XS;
}
defm Instr : Y;
</pre></div>
</div>
<p>This example will create four records, shown here in alphabetical order with
their fields.</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>def InstrSDrm {
bits<4> opcode = { 0, 0, 1, 0 };
bits<4> Prefix = { 1, 1, 0, 0 };
}
def InstrSDrr {
bits<4> opcode = { 0, 1, 0, 0 };
bits<4> Prefix = { 1, 1, 0, 0 };
}
def InstrSSrm {
bits<4> opcode = { 0, 0, 1, 0 };
bits<4> Prefix = { 1, 0, 1, 1 };
}
def InstrSSrr {
bits<4> opcode = { 0, 1, 0, 0 };
bits<4> Prefix = { 1, 0, 1, 1 };
}
</pre></div>
</div>
<p>It’s also possible to use <code class="docutils literal notranslate"><span class="pre">let</span></code> statements inside multiclasses, providing
another way to factor out commonality from the records, especially when
using several levels of multiclass instantiations.</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>multiclass basic_r <bits<4> opc> {
let Predicates = [HasSSE2] in {
def rr : Instruction<opc, "rr">;
def rm : Instruction<opc, "rm">;
}
let Predicates = [HasSSE3] in
def rx : Instruction<opc, "rx">;
}
multiclass basic_ss <bits<4> opc> {
let IsDouble = false in
defm SS : basic_r<opc>;
let IsDouble = true in
defm SD : basic_r<opc>;
}
defm ADD : basic_ss<0xf>;
</pre></div>
</div>
</div>
<div class="section" id="defset-create-a-definition-set">
<h3><a class="toc-backref" href="#id23"><span class="sectnum">1.6.8</span> <code class="docutils literal notranslate"><span class="pre">defset</span></code> — create a definition set</a><a class="headerlink" href="#defset-create-a-definition-set" title="Permalink to this headline">¶</a></h3>
<p>The <code class="docutils literal notranslate"><span class="pre">defset</span></code> statement is used to collect a set of records into a global
list of records.</p>
<pre>
<strong id="grammar-token-Defset"><span id="grammar-token-defset"></span>Defset</strong> ::= "defset" <a class="reference internal" href="#grammar-token-Type"><code class="xref docutils literal notranslate"><span class="pre">Type</span></code></a> <a class="reference internal" href="#grammar-token-TokIdentifier"><code class="xref docutils literal notranslate"><span class="pre">TokIdentifier</span></code></a> "=" "{" <a class="reference internal" href="#grammar-token-Statement"><code class="xref docutils literal notranslate"><span class="pre">Statement</span></code></a>* "}"
</pre>
<p>All records defined inside the braces via <code class="docutils literal notranslate"><span class="pre">def</span></code> and <code class="docutils literal notranslate"><span class="pre">defm</span></code> are defined
as usual, and they are also collected in a global list of the given name
(<a class="reference internal" href="#grammar-token-TokIdentifier"><code class="xref std std-token docutils literal notranslate"><span class="pre">TokIdentifier</span></code></a>).</p>
<p>The specified type must be <code class="docutils literal notranslate"><span class="pre">list<</span></code><em>class</em><code class="docutils literal notranslate"><span class="pre">></span></code>, where <em>class</em> is some
record class. The <code class="docutils literal notranslate"><span class="pre">defset</span></code> statement establishes a scope for its
statements. It is an error to define a record in the scope of the
<code class="docutils literal notranslate"><span class="pre">defset</span></code> that is not of type <em>class</em>.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">defset</span></code> statement can be nested. The inner <code class="docutils literal notranslate"><span class="pre">defset</span></code> adds the
records to its own set, and all those records are also added to the outer
set.</p>
<p>Anonymous records created inside initialization expressions using the
<code class="docutils literal notranslate"><span class="pre">ClassID<...></span></code> syntax are not collected in the set.</p>
</div>
<div class="section" id="defvar-define-a-variable">
<h3><a class="toc-backref" href="#id24"><span class="sectnum">1.6.9</span> <code class="docutils literal notranslate"><span class="pre">defvar</span></code> — define a variable</a><a class="headerlink" href="#defvar-define-a-variable" title="Permalink to this headline">¶</a></h3>
<p>A <code class="docutils literal notranslate"><span class="pre">defvar</span></code> statement defines a global variable. Its value can be used
throughout the statements that follow the definition.</p>
<pre>
<strong id="grammar-token-Defvar"><span id="grammar-token-defvar"></span>Defvar</strong> ::= "defvar" <a class="reference internal" href="#grammar-token-TokIdentifier"><code class="xref docutils literal notranslate"><span class="pre">TokIdentifier</span></code></a> "=" <a class="reference internal" href="#grammar-token-Value"><code class="xref docutils literal notranslate"><span class="pre">Value</span></code></a> ";"
</pre>
<p>The identifier on the left of the <code class="docutils literal notranslate"><span class="pre">=</span></code> is defined to be a global variable
whose value is given by the value expression on the right of the <code class="docutils literal notranslate"><span class="pre">=</span></code>. The
type of the variable is automatically inferred.</p>
<p>Once a variable has been defined, it cannot be set to another value.</p>
<p>Variables defined in a top-level <code class="docutils literal notranslate"><span class="pre">foreach</span></code> go out of scope at the end of
each loop iteration, so their value in one iteration is not available in
the next iteration. The following <code class="docutils literal notranslate"><span class="pre">defvar</span></code> will not work:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>defvar i = !add(i, 1)
</pre></div>
</div>
<p>Variables can also be defined with <code class="docutils literal notranslate"><span class="pre">defvar</span></code> in a record body. See
<a class="reference internal" href="#defvar-in-a-record-body">Defvar in a Record Body</a> for more details.</p>
</div>
<div class="section" id="foreach-iterate-over-a-sequence-of-statements">
<h3><a class="toc-backref" href="#id25"><span class="sectnum">1.6.10</span> <code class="docutils literal notranslate"><span class="pre">foreach</span></code> — iterate over a sequence of statements</a><a class="headerlink" href="#foreach-iterate-over-a-sequence-of-statements" title="Permalink to this headline">¶</a></h3>
<p>The <code class="docutils literal notranslate"><span class="pre">foreach</span></code> statement iterates over a series of statements, varying a
variable over a sequence of values.</p>
<pre>
<strong id="grammar-token-Foreach"><span id="grammar-token-foreach"></span>Foreach </strong> ::= "foreach" <a class="reference internal" href="#grammar-token-ForeachIterator"><code class="xref docutils literal notranslate"><span class="pre">ForeachIterator</span></code></a> "in" "{" <a class="reference internal" href="#grammar-token-Statement"><code class="xref docutils literal notranslate"><span class="pre">Statement</span></code></a>* "}"
| "foreach" <a class="reference internal" href="#grammar-token-ForeachIterator"><code class="xref docutils literal notranslate"><span class="pre">ForeachIterator</span></code></a> "in" <a class="reference internal" href="#grammar-token-Statement"><code class="xref docutils literal notranslate"><span class="pre">Statement</span></code></a>
<strong id="grammar-token-ForeachIterator"><span id="grammar-token-foreachiterator"></span>ForeachIterator</strong> ::= <a class="reference internal" href="#grammar-token-TokIdentifier"><code class="xref docutils literal notranslate"><span class="pre">TokIdentifier</span></code></a> "=" ("{" <a class="reference internal" href="#grammar-token-RangeList"><code class="xref docutils literal notranslate"><span class="pre">RangeList</span></code></a> "}" | <a class="reference internal" href="#grammar-token-RangePiece"><code class="xref docutils literal notranslate"><span class="pre">RangePiece</span></code></a> | <a class="reference internal" href="#grammar-token-Value"><code class="xref docutils literal notranslate"><span class="pre">Value</span></code></a>)
</pre>
<p>The body of the <code class="docutils literal notranslate"><span class="pre">foreach</span></code> is a series of statements in braces or a
single statement with no braces. The statements are re-evaluated once for
each value in the range list, range piece, or single value. On each
iteration, the <a class="reference internal" href="#grammar-token-TokIdentifier"><code class="xref std std-token docutils literal notranslate"><span class="pre">TokIdentifier</span></code></a> variable is set to the value and can
be used in the statements.</p>
<p>The statement list establishes an inner scope. Variables local to a
<code class="docutils literal notranslate"><span class="pre">foreach</span></code> go out of scope at the end of each loop iteration, so their
values do not carry over from one iteration to the next. Foreach loops may
be nested.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">foreach</span></code> statement can also be used in a record <a class="reference internal" href="#grammar-token-Body"><code class="xref std std-token docutils literal notranslate"><span class="pre">Body</span></code></a>.</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>foreach i = [0, 1, 2, 3] in {
def R#i : Register<...>;
def F#i : Register<...>;
}
</pre></div>
</div>
<p>This loop defines records named <code class="docutils literal notranslate"><span class="pre">R0</span></code>, <code class="docutils literal notranslate"><span class="pre">R1</span></code>, <code class="docutils literal notranslate"><span class="pre">R2</span></code>, and <code class="docutils literal notranslate"><span class="pre">R3</span></code>, along
with <code class="docutils literal notranslate"><span class="pre">F0</span></code>, <code class="docutils literal notranslate"><span class="pre">F1</span></code>, <code class="docutils literal notranslate"><span class="pre">F2</span></code>, and <code class="docutils literal notranslate"><span class="pre">F3</span></code>.</p>
</div>
<div class="section" id="if-select-statements-based-on-a-test">
<h3><a class="toc-backref" href="#id26"><span class="sectnum">1.6.11</span> <code class="docutils literal notranslate"><span class="pre">if</span></code> — select statements based on a test</a><a class="headerlink" href="#if-select-statements-based-on-a-test" title="Permalink to this headline">¶</a></h3>
<p>The <code class="docutils literal notranslate"><span class="pre">if</span></code> statement allows one of two statement groups to be selected based
on the value of an expression.</p>
<pre>
<strong id="grammar-token-If"><span id="grammar-token-if"></span>If </strong> ::= "if" <a class="reference internal" href="#grammar-token-Value"><code class="xref docutils literal notranslate"><span class="pre">Value</span></code></a> "then" <a class="reference internal" href="#grammar-token-IfBody"><code class="xref docutils literal notranslate"><span class="pre">IfBody</span></code></a>
| "if" <a class="reference internal" href="#grammar-token-Value"><code class="xref docutils literal notranslate"><span class="pre">Value</span></code></a> "then" <a class="reference internal" href="#grammar-token-IfBody"><code class="xref docutils literal notranslate"><span class="pre">IfBody</span></code></a> "else" <a class="reference internal" href="#grammar-token-IfBody"><code class="xref docutils literal notranslate"><span class="pre">IfBody</span></code></a>
<strong id="grammar-token-IfBody"><span id="grammar-token-ifbody"></span>IfBody</strong> ::= "{" <a class="reference internal" href="#grammar-token-Statement"><code class="xref docutils literal notranslate"><span class="pre">Statement</span></code></a>* "}" | <a class="reference internal" href="#grammar-token-Statement"><code class="xref docutils literal notranslate"><span class="pre">Statement</span></code></a>
</pre>
<p>The value expression is evaluated. If it evaluates to true (in the same
sense used by the bang operators), then the statements following the
<code class="docutils literal notranslate"><span class="pre">then</span></code> reserved word are processed. Otherwise, if there is an <code class="docutils literal notranslate"><span class="pre">else</span></code>
reserved word, the statements following the <code class="docutils literal notranslate"><span class="pre">else</span></code> are processed. If the
value is false and there is no <code class="docutils literal notranslate"><span class="pre">else</span></code> arm, no statements are processed.</p>
<p>Because the braces around the <code class="docutils literal notranslate"><span class="pre">then</span></code> statements are optional, this grammar rule
has the usual ambiguity with “dangling else” clauses, and it is resolved in
the usual way: in a case like <code class="docutils literal notranslate"><span class="pre">if</span> <span class="pre">v1</span> <span class="pre">then</span> <span class="pre">if</span> <span class="pre">v2</span> <span class="pre">then</span> <span class="pre">{...}</span> <span class="pre">else</span> <span class="pre">{...}</span></code>, the
<code class="docutils literal notranslate"><span class="pre">else</span></code> associates with the inner <code class="docutils literal notranslate"><span class="pre">if</span></code> rather than the outer one.</p>
<p>The <a class="reference internal" href="#grammar-token-IfBody"><code class="xref std std-token docutils literal notranslate"><span class="pre">IfBody</span></code></a> of the then and else arms of the <code class="docutils literal notranslate"><span class="pre">if</span></code> establish an
inner scope. Any <code class="docutils literal notranslate"><span class="pre">defvar</span></code> variables defined in the bodies go out of scope
when the bodies are finished (see <a class="reference internal" href="#defvar-in-a-record-body">Defvar in a Record Body</a> for more details).</p>
<p>The <code class="docutils literal notranslate"><span class="pre">if</span></code> statement can also be used in a record <a class="reference internal" href="#grammar-token-Body"><code class="xref std std-token docutils literal notranslate"><span class="pre">Body</span></code></a>.</p>
</div>
<div class="section" id="assert-check-that-a-condition-is-true">
<h3><a class="toc-backref" href="#id27"><span class="sectnum">1.6.12</span> <code class="docutils literal notranslate"><span class="pre">assert</span></code> — check that a condition is true</a><a class="headerlink" href="#assert-check-that-a-condition-is-true" title="Permalink to this headline">¶</a></h3>
<p>The <code class="docutils literal notranslate"><span class="pre">assert</span></code> statement checks a boolean condition to be sure that it is true
and prints an error message if it is not.</p>
<pre>
<strong id="grammar-token-Assert"><span id="grammar-token-assert"></span>Assert</strong> ::= "assert" <code class="xref docutils literal notranslate"><span class="pre">condition</span></code> "," <code class="xref docutils literal notranslate"><span class="pre">message</span></code> ";"
</pre>
<p>If the boolean condition is true, the statement does nothing. If the
condition is false, it prints a nonfatal error message. The <strong>message</strong>, which
can be an arbitrary string expression, is included in the error message as a
note. The exact behavior of the <code class="docutils literal notranslate"><span class="pre">assert</span></code> statement depends on its
placement.</p>
<ul class="simple">
<li><p>At top level, the assertion is checked immediately.</p></li>
<li><p>In a record definition, the statement is saved and all assertions are
checked after the record is completely built.</p></li>
<li><p>In a class definition, the assertions are saved and inherited by all
the subclasses and records that inherit from the class. The assertions are
then checked when the records are completely built.</p></li>
<li><p>In a multiclass definition, the assertions are saved with the other
components of the multiclass and then checked each time the multiclass
is instantiated with <code class="docutils literal notranslate"><span class="pre">defm</span></code>.</p></li>
</ul>
<p>Using assertions in TableGen files can simplify record checking in TableGen
backends. Here is an example of an <code class="docutils literal notranslate"><span class="pre">assert</span></code> in two class definitions.</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>class PersonName<string name> {
assert !le(!size(name), 32), "person name is too long: " # name;
string Name = name;
}
class Person<string name, int age> : PersonName<name> {
assert !and(!ge(age, 1), !le(age, 120)), "person age is invalid: " # age;
int Age = age;
}
def Rec20 : Person<"Donald Knuth", 60> {
...
}
</pre></div>
</div>
</div>
</div>
<div class="section" id="additional-details">
<h2><a class="toc-backref" href="#id28"><span class="sectnum">1.7</span> Additional Details</a><a class="headerlink" href="#additional-details" title="Permalink to this headline">¶</a></h2>
<div class="section" id="directed-acyclic-graphs-dags">
<h3><a class="toc-backref" href="#id29"><span class="sectnum">1.7.1</span> Directed acyclic graphs (DAGs)</a><a class="headerlink" href="#directed-acyclic-graphs-dags" title="Permalink to this headline">¶</a></h3>
<p>A directed acyclic graph can be represented directly in TableGen using the
<code class="docutils literal notranslate"><span class="pre">dag</span></code> datatype. A DAG node consists of an operator and zero or more
arguments (or operands). Each argument can be of any desired type. By using
another DAG node as an argument, an arbitrary graph of DAG nodes can be
built.</p>
<p>The syntax of a <code class="docutils literal notranslate"><span class="pre">dag</span></code> instance is:</p>
<blockquote>
<div><p><code class="docutils literal notranslate"><span class="pre">(</span></code> <em>operator</em> <em>argument1</em><code class="docutils literal notranslate"><span class="pre">,</span></code> <em>argument2</em><code class="docutils literal notranslate"><span class="pre">,</span></code> … <code class="docutils literal notranslate"><span class="pre">)</span></code></p>
</div></blockquote>
<p>The operator must be present and must be a record. There can be zero or more
arguments, separated by commas. The operator and arguments can have three
formats.</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 32%" />
<col style="width: 68%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p>Format</p></th>
<th class="head"><p>Meaning</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><em>value</em></p></td>
<td><p>argument value</p></td>
</tr>
<tr class="row-odd"><td><p><em>value</em><code class="docutils literal notranslate"><span class="pre">:</span></code><em>name</em></p></td>
<td><p>argument value and associated name</p></td>
</tr>
<tr class="row-even"><td><p><em>name</em></p></td>
<td><p>argument name with unset (uninitialized) value</p></td>
</tr>
</tbody>
</table>
<p>The <em>value</em> can be any TableGen value. The <em>name</em>, if present, must be a
<a class="reference internal" href="#grammar-token-TokVarName"><code class="xref std std-token docutils literal notranslate"><span class="pre">TokVarName</span></code></a>, which starts with a dollar sign (<code class="docutils literal notranslate"><span class="pre">$</span></code>). The purpose of
a name is to tag an operator or argument in a DAG with a particular meaning,
or to associate an argument in one DAG with a like-named argument in another
DAG.</p>
<p>The following bang operators are useful for working with DAGs:
<code class="docutils literal notranslate"><span class="pre">!con</span></code>, <code class="docutils literal notranslate"><span class="pre">!dag</span></code>, <code class="docutils literal notranslate"><span class="pre">!empty</span></code>, <code class="docutils literal notranslate"><span class="pre">!foreach</span></code>, <code class="docutils literal notranslate"><span class="pre">!getdagop</span></code>, <code class="docutils literal notranslate"><span class="pre">!setdagop</span></code>, <code class="docutils literal notranslate"><span class="pre">!size</span></code>.</p>
</div>
<div class="section" id="defvar-in-a-record-body">
<h3><a class="toc-backref" href="#id30"><span class="sectnum">1.7.2</span> Defvar in a record body</a><a class="headerlink" href="#defvar-in-a-record-body" title="Permalink to this headline">¶</a></h3>
<p>In addition to defining global variables, the <code class="docutils literal notranslate"><span class="pre">defvar</span></code> statement can
be used inside the <a class="reference internal" href="#grammar-token-Body"><code class="xref std std-token docutils literal notranslate"><span class="pre">Body</span></code></a> of a class or record definition to define
local variables. The scope of the variable extends from the <code class="docutils literal notranslate"><span class="pre">defvar</span></code>
statement to the end of the body. It cannot be set to a different value
within its scope. The <code class="docutils literal notranslate"><span class="pre">defvar</span></code> statement can also be used in the statement
list of a <code class="docutils literal notranslate"><span class="pre">foreach</span></code>, which establishes a scope.</p>
<p>A variable named <code class="docutils literal notranslate"><span class="pre">V</span></code> in an inner scope shadows (hides) any variables <code class="docutils literal notranslate"><span class="pre">V</span></code>
in outer scopes. In particular, <code class="docutils literal notranslate"><span class="pre">V</span></code> in a record body shadows a global
<code class="docutils literal notranslate"><span class="pre">V</span></code>, and <code class="docutils literal notranslate"><span class="pre">V</span></code> in a <code class="docutils literal notranslate"><span class="pre">foreach</span></code> statement list shadows any <code class="docutils literal notranslate"><span class="pre">V</span></code> in
surrounding record or global scopes.</p>
<p>Variables defined in a <code class="docutils literal notranslate"><span class="pre">foreach</span></code> go out of scope at the end of
each loop iteration, so their value in one iteration is not available in
the next iteration. The following <code class="docutils literal notranslate"><span class="pre">defvar</span></code> will not work:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>defvar i = !add(i, 1)
</pre></div>
</div>
</div>
<div class="section" id="how-records-are-built">
<h3><a class="toc-backref" href="#id31"><span class="sectnum">1.7.3</span> How records are built</a><a class="headerlink" href="#how-records-are-built" title="Permalink to this headline">¶</a></h3>
<p>The following steps are taken by TableGen when a record is built. Classes are simply
abstract records and so go through the same steps.</p>
<ol class="arabic simple">
<li><p>Build the record name (<a class="reference internal" href="#grammar-token-NameValue"><code class="xref std std-token docutils literal notranslate"><span class="pre">NameValue</span></code></a>) and create an empty record.</p></li>
<li><p>Parse the parent classes in the <a class="reference internal" href="#grammar-token-ParentClassList"><code class="xref std std-token docutils literal notranslate"><span class="pre">ParentClassList</span></code></a> from left to
right, visiting each parent class’s ancestor classes from top to bottom.</p></li>
</ol>
<blockquote>
<div><ol class="loweralpha simple">
<li><p>Add the fields from the parent class to the record.</p></li>
<li><p>Substitute the template arguments into those fields.</p></li>
<li><p>Add the parent class to the record’s list of inherited classes.</p></li>
</ol>
</div></blockquote>
<ol class="arabic simple" start="3">
<li><p>Apply any top-level <code class="docutils literal notranslate"><span class="pre">let</span></code> bindings to the record. Recall that top-level
bindings only apply to inherited fields.</p></li>
<li><p>Parse the body of the record.</p></li>
</ol>
<blockquote>
<div><ul class="simple">
<li><p>Add any fields to the record.</p></li>
<li><p>Modify the values of fields according to local <code class="docutils literal notranslate"><span class="pre">let</span></code> statements.</p></li>
<li><p>Define any <code class="docutils literal notranslate"><span class="pre">defvar</span></code> variables.</p></li>
</ul>
</div></blockquote>
<ol class="arabic simple" start="5">
<li><p>Make a pass over all the fields to resolve any inter-field references.</p></li>
<li><p>Add the record to the master record list.</p></li>
</ol>
<p>Because references between fields are resolved (step 5) after <code class="docutils literal notranslate"><span class="pre">let</span></code> bindings are
applied (step 3), the <code class="docutils literal notranslate"><span class="pre">let</span></code> statement has unusual power. For example:</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>class C <int x> {
int Y = x;
int Yplus1 = !add(Y, 1);
int xplus1 = !add(x, 1);
}
let Y = 10 in {
def rec1 : C<5> {
}
}
def rec2 : C<5> {
let Y = 10;
}
</pre></div>
</div>
<p>In both cases, one where a top-level <code class="docutils literal notranslate"><span class="pre">let</span></code> is used to bind <code class="docutils literal notranslate"><span class="pre">Y</span></code> and one
where a local <code class="docutils literal notranslate"><span class="pre">let</span></code> does the same thing, the results are:</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>def rec1 { // C
int Y = 10;
int Yplus1 = 11;
int xplus1 = 6;
}
def rec2 { // C
int Y = 10;
int Yplus1 = 11;
int xplus1 = 6;
}
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">Yplus1</span></code> is 11 because the <code class="docutils literal notranslate"><span class="pre">let</span> <span class="pre">Y</span></code> is performed before the <code class="docutils literal notranslate"><span class="pre">!add(Y,</span>
<span class="pre">1)</span></code> is resolved. Use this power wisely.</p>
</div>
</div>
<div class="section" id="using-classes-as-subroutines">
<h2><a class="toc-backref" href="#id32"><span class="sectnum">1.8</span> Using Classes as Subroutines</a><a class="headerlink" href="#using-classes-as-subroutines" title="Permalink to this headline">¶</a></h2>
<p>As described in <a class="reference internal" href="#simple-values">Simple values</a>, a class can be invoked in an expression
and passed template arguments. This causes TableGen to create a new anonymous
record inheriting from that class. As usual, the record receives all the
fields defined in the class.</p>
<p>This feature can be employed as a simple subroutine facility. The class can
use the template arguments to define various variables and fields, which end
up in the anonymous record. Those fields can then be retrieved in the
expression invoking the class as follows. Assume that the field <code class="docutils literal notranslate"><span class="pre">ret</span></code>
contains the final value of the subroutine.</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>int Result = ... CalcValue<arg>.ret ...;
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">CalcValue</span></code> class is invoked with the template argument <code class="docutils literal notranslate"><span class="pre">arg</span></code>. It
calculates a value for the <code class="docutils literal notranslate"><span class="pre">ret</span></code> field, which is then retrieved at the
“point of call” in the initialization for the Result field. The anonymous
record created in this example serves no other purpose than to carry the
result value.</p>
<p>Here is a practical example. The class <code class="docutils literal notranslate"><span class="pre">isValidSize</span></code> determines whether a
specified number of bytes represents a valid data size. The bit <code class="docutils literal notranslate"><span class="pre">ret</span></code> is
set appropriately. The field <code class="docutils literal notranslate"><span class="pre">ValidSize</span></code> obtains its initial value by
invoking <code class="docutils literal notranslate"><span class="pre">isValidSize</span></code> with the data size and retrieving the <code class="docutils literal notranslate"><span class="pre">ret</span></code> field
from the resulting anonymous record.</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>class isValidSize<int size> {
bit ret = !cond(!eq(size, 1): 1,
!eq(size, 2): 1,
!eq(size, 4): 1,
!eq(size, 8): 1,
!eq(size, 16): 1,
true: 0);
}
def Data1 {
int Size = ...;
bit ValidSize = isValidSize<Size>.ret;
}
</pre></div>
</div>
</div>
<div class="section" id="preprocessing-facilities">
<h2><a class="toc-backref" href="#id33"><span class="sectnum">1.9</span> Preprocessing Facilities</a><a class="headerlink" href="#preprocessing-facilities" title="Permalink to this headline">¶</a></h2>
<p>The preprocessor embedded in TableGen is intended only for simple
conditional compilation. It supports the following directives, which are
specified somewhat informally.</p>
<pre>
<strong id="grammar-token-LineBegin"><span id="grammar-token-linebegin"></span>LineBegin </strong> ::= beginning of line
<strong id="grammar-token-LineEnd"><span id="grammar-token-lineend"></span>LineEnd </strong> ::= newline | return | EOF
<strong id="grammar-token-WhiteSpace"><span id="grammar-token-whitespace"></span>WhiteSpace </strong> ::= space | tab
<strong id="grammar-token-CComment"><span id="grammar-token-ccomment"></span>CComment </strong> ::= "/*" ... "*/"
<strong id="grammar-token-BCPLComment"><span id="grammar-token-bcplcomment"></span>BCPLComment </strong> ::= "//" ... <a class="reference internal" href="#grammar-token-LineEnd"><code class="xref docutils literal notranslate"><span class="pre">LineEnd</span></code></a>
<strong id="grammar-token-WhiteSpaceOrCComment"><span id="grammar-token-whitespaceorccomment"></span>WhiteSpaceOrCComment </strong> ::= <a class="reference internal" href="#grammar-token-WhiteSpace"><code class="xref docutils literal notranslate"><span class="pre">WhiteSpace</span></code></a> | <a class="reference internal" href="#grammar-token-CComment"><code class="xref docutils literal notranslate"><span class="pre">CComment</span></code></a>
<strong id="grammar-token-WhiteSpaceOrAnyComment"><span id="grammar-token-whitespaceoranycomment"></span>WhiteSpaceOrAnyComment</strong> ::= <a class="reference internal" href="#grammar-token-WhiteSpace"><code class="xref docutils literal notranslate"><span class="pre">WhiteSpace</span></code></a> | <a class="reference internal" href="#grammar-token-CComment"><code class="xref docutils literal notranslate"><span class="pre">CComment</span></code></a> | <a class="reference internal" href="#grammar-token-BCPLComment"><code class="xref docutils literal notranslate"><span class="pre">BCPLComment</span></code></a>
<strong id="grammar-token-MacroName"><span id="grammar-token-macroname"></span>MacroName </strong> ::= <a class="reference internal" href="#grammar-token-ualpha"><code class="xref docutils literal notranslate"><span class="pre">ualpha</span></code></a> (<a class="reference internal" href="#grammar-token-ualpha"><code class="xref docutils literal notranslate"><span class="pre">ualpha</span></code></a> | "0"..."9")*
<strong id="grammar-token-PreDefine"><span id="grammar-token-predefine"></span>PreDefine </strong> ::= <a class="reference internal" href="#grammar-token-LineBegin"><code class="xref docutils literal notranslate"><span class="pre">LineBegin</span></code></a> (<a class="reference internal" href="#grammar-token-WhiteSpaceOrCComment"><code class="xref docutils literal notranslate"><span class="pre">WhiteSpaceOrCComment</span></code></a>)*
"#define" (<a class="reference internal" href="#grammar-token-WhiteSpace"><code class="xref docutils literal notranslate"><span class="pre">WhiteSpace</span></code></a>)+ <a class="reference internal" href="#grammar-token-MacroName"><code class="xref docutils literal notranslate"><span class="pre">MacroName</span></code></a>
(<a class="reference internal" href="#grammar-token-WhiteSpaceOrAnyComment"><code class="xref docutils literal notranslate"><span class="pre">WhiteSpaceOrAnyComment</span></code></a>)* <a class="reference internal" href="#grammar-token-LineEnd"><code class="xref docutils literal notranslate"><span class="pre">LineEnd</span></code></a>
<strong id="grammar-token-PreIfdef"><span id="grammar-token-preifdef"></span>PreIfdef </strong> ::= <a class="reference internal" href="#grammar-token-LineBegin"><code class="xref docutils literal notranslate"><span class="pre">LineBegin</span></code></a> (<a class="reference internal" href="#grammar-token-WhiteSpaceOrCComment"><code class="xref docutils literal notranslate"><span class="pre">WhiteSpaceOrCComment</span></code></a>)*
("#ifdef" | "#ifndef") (<a class="reference internal" href="#grammar-token-WhiteSpace"><code class="xref docutils literal notranslate"><span class="pre">WhiteSpace</span></code></a>)+ <a class="reference internal" href="#grammar-token-MacroName"><code class="xref docutils literal notranslate"><span class="pre">MacroName</span></code></a>
(<a class="reference internal" href="#grammar-token-WhiteSpaceOrAnyComment"><code class="xref docutils literal notranslate"><span class="pre">WhiteSpaceOrAnyComment</span></code></a>)* <a class="reference internal" href="#grammar-token-LineEnd"><code class="xref docutils literal notranslate"><span class="pre">LineEnd</span></code></a>
<strong id="grammar-token-PreElse"><span id="grammar-token-preelse"></span>PreElse </strong> ::= <a class="reference internal" href="#grammar-token-LineBegin"><code class="xref docutils literal notranslate"><span class="pre">LineBegin</span></code></a> (<a class="reference internal" href="#grammar-token-WhiteSpaceOrCComment"><code class="xref docutils literal notranslate"><span class="pre">WhiteSpaceOrCComment</span></code></a>)*
"#else" (<a class="reference internal" href="#grammar-token-WhiteSpaceOrAnyComment"><code class="xref docutils literal notranslate"><span class="pre">WhiteSpaceOrAnyComment</span></code></a>)* <a class="reference internal" href="#grammar-token-LineEnd"><code class="xref docutils literal notranslate"><span class="pre">LineEnd</span></code></a>
<strong id="grammar-token-PreEndif"><span id="grammar-token-preendif"></span>PreEndif </strong> ::= <a class="reference internal" href="#grammar-token-LineBegin"><code class="xref docutils literal notranslate"><span class="pre">LineBegin</span></code></a> (<a class="reference internal" href="#grammar-token-WhiteSpaceOrCComment"><code class="xref docutils literal notranslate"><span class="pre">WhiteSpaceOrCComment</span></code></a>)*
"#endif" (<a class="reference internal" href="#grammar-token-WhiteSpaceOrAnyComment"><code class="xref docutils literal notranslate"><span class="pre">WhiteSpaceOrAnyComment</span></code></a>)* <a class="reference internal" href="#grammar-token-LineEnd"><code class="xref docutils literal notranslate"><span class="pre">LineEnd</span></code></a>
</pre>
<p>A <a class="reference internal" href="#grammar-token-MacroName"><code class="xref std std-token docutils literal notranslate"><span class="pre">MacroName</span></code></a> can be defined anywhere in a TableGen file. The name has
no value; it can only be tested to see whether it is defined.</p>
<p>A macro test region begins with an <code class="docutils literal notranslate"><span class="pre">#ifdef</span></code> or <code class="docutils literal notranslate"><span class="pre">#ifndef</span></code> directive. If
the macro name is defined (<code class="docutils literal notranslate"><span class="pre">#ifdef</span></code>) or undefined (<code class="docutils literal notranslate"><span class="pre">#ifndef</span></code>), then the
source code between the directive and the corresponding <code class="docutils literal notranslate"><span class="pre">#else</span></code> or
<code class="docutils literal notranslate"><span class="pre">#endif</span></code> is processed. If the test fails but there is an <code class="docutils literal notranslate"><span class="pre">#else</span></code>
clause, the source code between the <code class="docutils literal notranslate"><span class="pre">#else</span></code> and the <code class="docutils literal notranslate"><span class="pre">#endif</span></code> is
processed. If the test fails and there is no <code class="docutils literal notranslate"><span class="pre">#else</span></code> clause, then no
source code in the test region is processed.</p>
<p>Test regions may be nested, but they must be properly nested. A region
started in a file must end in that file; that is, must have its
<code class="docutils literal notranslate"><span class="pre">#endif</span></code> in the same file.</p>
<p>A <a class="reference internal" href="#grammar-token-MacroName"><code class="xref std std-token docutils literal notranslate"><span class="pre">MacroName</span></code></a> may be defined externally using the <code class="docutils literal notranslate"><span class="pre">-D</span></code> option on the
<code class="docutils literal notranslate"><span class="pre">*-tblgen</span></code> command line:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">llvm</span><span class="o">-</span><span class="n">tblgen</span> <span class="bp">self</span><span class="o">-</span><span class="n">reference</span><span class="o">.</span><span class="n">td</span> <span class="o">-</span><span class="n">Dmacro1</span> <span class="o">-</span><span class="n">Dmacro3</span>
</pre></div>
</div>
</div>
<div class="section" id="appendix-a-bang-operators">
<h2><a class="toc-backref" href="#id34"><span class="sectnum">1.10</span> Appendix A: Bang Operators</a><a class="headerlink" href="#appendix-a-bang-operators" title="Permalink to this headline">¶</a></h2>
<p>Bang operators act as functions in value expressions. A bang operator takes
one or more arguments, operates on them, and produces a result. If the
operator produces a boolean result, the result value will be 1 for true or 0
for false. When an operator tests a boolean argument, it interprets 0 as false
and non-0 as true.</p>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>The <code class="docutils literal notranslate"><span class="pre">!getop</span></code> and <code class="docutils literal notranslate"><span class="pre">!setop</span></code> bang operators are deprecated in favor of
<code class="docutils literal notranslate"><span class="pre">!getdagop</span></code> and <code class="docutils literal notranslate"><span class="pre">!setdagop</span></code>.</p>
</div>
<dl>
<dt><code class="docutils literal notranslate"><span class="pre">!add(</span></code><em>a</em><code class="docutils literal notranslate"><span class="pre">,</span></code> <em>b</em><code class="docutils literal notranslate"><span class="pre">,</span> <span class="pre">...)</span></code></dt><dd><p>This operator adds <em>a</em>, <em>b</em>, etc., and produces the sum.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">!and(</span></code><em>a</em><code class="docutils literal notranslate"><span class="pre">,</span></code> <em>b</em><code class="docutils literal notranslate"><span class="pre">,</span> <span class="pre">...)</span></code></dt><dd><p>This operator does a bitwise AND on <em>a</em>, <em>b</em>, etc., and produces the
result. A logical AND can be performed if all the arguments are either
0 or 1.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">!cast<</span></code><em>type</em><code class="docutils literal notranslate"><span class="pre">>(</span></code><em>a</em><code class="docutils literal notranslate"><span class="pre">)</span></code></dt><dd><p>This operator performs a cast on <em>a</em> and produces the result.
If <em>a</em> is not a string, then a straightforward cast is performed, say
between an <code class="docutils literal notranslate"><span class="pre">int</span></code> and a <code class="docutils literal notranslate"><span class="pre">bit</span></code>, or between record types. This allows
casting a record to a class. If a record is cast to <code class="docutils literal notranslate"><span class="pre">string</span></code>, the
record’s name is produced.</p>
<p>If <em>a</em> is a string, then it is treated as a record name and looked up in
the list of all defined records. The resulting record is expected to be of
the specified <em>type</em>.</p>
<p>For example, if <code class="docutils literal notranslate"><span class="pre">!cast<</span></code><em>type</em><code class="docutils literal notranslate"><span class="pre">>(</span></code><em>name</em><code class="docutils literal notranslate"><span class="pre">)</span></code>
appears in a multiclass definition, or in a
class instantiated inside a multiclass definition, and the <em>name</em> does not
reference any template arguments of the multiclass, then a record by
that name must have been instantiated earlier
in the source file. If <em>name</em> does reference
a template argument, then the lookup is delayed until <code class="docutils literal notranslate"><span class="pre">defm</span></code> statements
instantiating the multiclass (or later, if the defm occurs in another
multiclass and template arguments of the inner multiclass that are
referenced by <em>name</em> are substituted by values that themselves contain
references to template arguments of the outer multiclass).</p>
<p>If the type of <em>a</em> does not match <em>type</em>, TableGen raises an error.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">!con(</span></code><em>a</em><code class="docutils literal notranslate"><span class="pre">,</span></code> <em>b</em><code class="docutils literal notranslate"><span class="pre">,</span> <span class="pre">...)</span></code></dt><dd><p>This operator concatenates the DAG nodes <em>a</em>, <em>b</em>, etc. Their operations
must equal.</p>
<p><code class="docutils literal notranslate"><span class="pre">!con((op</span> <span class="pre">a1:$name1,</span> <span class="pre">a2:$name2),</span> <span class="pre">(op</span> <span class="pre">b1:$name3))</span></code></p>
<p>results in the DAG node <code class="docutils literal notranslate"><span class="pre">(op</span> <span class="pre">a1:$name1,</span> <span class="pre">a2:$name2,</span> <span class="pre">b1:$name3)</span></code>.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">!cond(</span></code><em>cond1</em> <code class="docutils literal notranslate"><span class="pre">:</span></code> <em>val1</em><code class="docutils literal notranslate"><span class="pre">,</span></code> <em>cond2</em> <code class="docutils literal notranslate"><span class="pre">:</span></code> <em>val2</em><code class="docutils literal notranslate"><span class="pre">,</span> <span class="pre">...,</span></code> <em>condn</em> <code class="docutils literal notranslate"><span class="pre">:</span></code> <em>valn</em><code class="docutils literal notranslate"><span class="pre">)</span></code></dt><dd><p>This operator tests <em>cond1</em> and returns <em>val1</em> if the result is true.
If false, the operator tests <em>cond2</em> and returns <em>val2</em> if the result is
true. And so forth. An error is reported if no conditions are true.</p>
<p>This example produces the sign word for an integer:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>!cond(!lt(x, 0) : "negative", !eq(x, 0) : "zero", true : "positive")
</pre></div>
</div>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">!dag(</span></code><em>op</em><code class="docutils literal notranslate"><span class="pre">,</span></code> <em>arguments</em><code class="docutils literal notranslate"><span class="pre">,</span></code> <em>names</em><code class="docutils literal notranslate"><span class="pre">)</span></code></dt><dd><p>This operator creates a DAG node with the given operator and
arguments. The <em>arguments</em> and <em>names</em> arguments must be lists
of equal length or uninitialized (<code class="docutils literal notranslate"><span class="pre">?</span></code>). The <em>names</em> argument
must be of type <code class="docutils literal notranslate"><span class="pre">list<string></span></code>.</p>
<p>Due to limitations of the type system, <em>arguments</em> must be a list of items
of a common type. In practice, this means that they should either have the
same type or be records with a common parent class. Mixing <code class="docutils literal notranslate"><span class="pre">dag</span></code> and
non-<code class="docutils literal notranslate"><span class="pre">dag</span></code> items is not possible. However, <code class="docutils literal notranslate"><span class="pre">?</span></code> can be used.</p>
<p>Example: <code class="docutils literal notranslate"><span class="pre">!dag(op,</span> <span class="pre">[a1,</span> <span class="pre">a2,</span> <span class="pre">?],</span> <span class="pre">["name1",</span> <span class="pre">"name2",</span> <span class="pre">"name3"])</span></code> results in
<code class="docutils literal notranslate"><span class="pre">(op</span> <span class="pre">a1-value:$name1,</span> <span class="pre">a2-value:$name2,</span> <span class="pre">?:$name3)</span></code>.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">!empty(</span></code><em>a</em><code class="docutils literal notranslate"><span class="pre">)</span></code></dt><dd><p>This operator produces 1 if the string, list, or DAG <em>a</em> is empty; 0 otherwise.
A dag is empty if it has no arguments; the operator does not count.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">!eq(</span></code> <em>a</em><cite>,</cite> <em>b</em><code class="docutils literal notranslate"><span class="pre">)</span></code></dt><dd><p>This operator produces 1 if <em>a</em> is equal to <em>b</em>; 0 otherwise.
The arguments must be <code class="docutils literal notranslate"><span class="pre">bit</span></code>, <code class="docutils literal notranslate"><span class="pre">bits</span></code>, <code class="docutils literal notranslate"><span class="pre">int</span></code>, <code class="docutils literal notranslate"><span class="pre">string</span></code>, or
record values. Use <code class="docutils literal notranslate"><span class="pre">!cast<string></span></code> to compare other types of objects.</p>
</dd>
</dl>
<p><code class="docutils literal notranslate"><span class="pre">!filter(</span></code><em>var</em><code class="docutils literal notranslate"><span class="pre">,</span></code> <em>list</em><code class="docutils literal notranslate"><span class="pre">,</span></code> <em>predicate</em><code class="docutils literal notranslate"><span class="pre">)</span></code></p>
<blockquote>
<div><p>This operator creates a new <code class="docutils literal notranslate"><span class="pre">list</span></code> by filtering the elements in
<em>list</em>. To perform the filtering, TableGen binds the variable <em>var</em> to each
element and then evaluates the <em>predicate</em> expression, which presumably
refers to <em>var</em>. The predicate must
produce a boolean value (<code class="docutils literal notranslate"><span class="pre">bit</span></code>, <code class="docutils literal notranslate"><span class="pre">bits</span></code>, or <code class="docutils literal notranslate"><span class="pre">int</span></code>). The value is
interpreted as with <code class="docutils literal notranslate"><span class="pre">!if</span></code>:
if the value is 0, the element is not included in the new list. If the value
is anything else, the element is included.</p>
</div></blockquote>
<dl>
<dt><code class="docutils literal notranslate"><span class="pre">!find(</span></code><em>string1</em><code class="docutils literal notranslate"><span class="pre">,</span></code> <em>string2</em>[<code class="docutils literal notranslate"><span class="pre">,</span></code> <em>start</em>]<code class="docutils literal notranslate"><span class="pre">)</span></code></dt><dd><p>This operator searches for <em>string2</em> in <em>string1</em> and produces its
position. The starting position of the search may be specified by <em>start</em>,
which can range between 0 and the length of <em>string1</em>; the default is 0.
If the string is not found, the result is -1.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">!foldl(</span></code><em>init</em><code class="docutils literal notranslate"><span class="pre">,</span></code> <em>list</em><code class="docutils literal notranslate"><span class="pre">,</span></code> <em>acc</em><code class="docutils literal notranslate"><span class="pre">,</span></code> <em>var</em><code class="docutils literal notranslate"><span class="pre">,</span></code> <em>expr</em><code class="docutils literal notranslate"><span class="pre">)</span></code></dt><dd><p>This operator performs a left-fold over the items in <em>list</em>. The
variable <em>acc</em> acts as the accumulator and is initialized to <em>init</em>.
The variable <em>var</em> is bound to each element in the <em>list</em>. The
expression is evaluated for each element and presumably uses <em>acc</em> and
<em>var</em> to calculate the accumulated value, which <code class="docutils literal notranslate"><span class="pre">!foldl</span></code> stores back in
<em>acc</em>. The type of <em>acc</em> is the same as <em>init</em>; the type of <em>var</em> is the
same as the elements of <em>list</em>; <em>expr</em> must have the same type as <em>init</em>.</p>
<p>The following example computes the total of the <code class="docutils literal notranslate"><span class="pre">Number</span></code> field in the
list of records in <code class="docutils literal notranslate"><span class="pre">RecList</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>int x = !foldl(0, RecList, total, rec, !add(total, rec.Number));
</pre></div>
</div>
<p>If your goal is to filter the list and produce a new list that includes only
some of the elements, see <code class="docutils literal notranslate"><span class="pre">!filter</span></code>.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">!foreach(</span></code><em>var</em><code class="docutils literal notranslate"><span class="pre">,</span></code> <em>sequence</em><code class="docutils literal notranslate"><span class="pre">,</span></code> <em>expr</em><code class="docutils literal notranslate"><span class="pre">)</span></code></dt><dd><p>This operator creates a new <code class="docutils literal notranslate"><span class="pre">list</span></code>/<code class="docutils literal notranslate"><span class="pre">dag</span></code> in which each element is a
function of the corresponding element in the <em>sequence</em> <code class="docutils literal notranslate"><span class="pre">list</span></code>/<code class="docutils literal notranslate"><span class="pre">dag</span></code>.
To perform the function, TableGen binds the variable <em>var</em> to an element
and then evaluates the expression. The expression presumably refers
to the variable <em>var</em> and calculates the result value.</p>
<p>If you simply want to create a list of a certain length containing
the same value repeated multiple times, see <code class="docutils literal notranslate"><span class="pre">!listsplat</span></code>.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">!ge(</span></code><em>a</em><cite>,</cite> <em>b</em><code class="docutils literal notranslate"><span class="pre">)</span></code></dt><dd><p>This operator produces 1 if <em>a</em> is greater than or equal to <em>b</em>; 0 otherwise.
The arguments must be <code class="docutils literal notranslate"><span class="pre">bit</span></code>, <code class="docutils literal notranslate"><span class="pre">bits</span></code>, <code class="docutils literal notranslate"><span class="pre">int</span></code>, or <code class="docutils literal notranslate"><span class="pre">string</span></code> values.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">!getdagop(</span></code><em>dag</em><code class="docutils literal notranslate"><span class="pre">)</span></code> –or– <code class="docutils literal notranslate"><span class="pre">!getdagop<</span></code><em>type</em><code class="docutils literal notranslate"><span class="pre">>(</span></code><em>dag</em><code class="docutils literal notranslate"><span class="pre">)</span></code></dt><dd><p>This operator produces the operator of the given <em>dag</em> node.
Example: <code class="docutils literal notranslate"><span class="pre">!getdagop((foo</span> <span class="pre">1,</span> <span class="pre">2))</span></code> results in <code class="docutils literal notranslate"><span class="pre">foo</span></code>. Recall that
DAG operators are always records.</p>
<p>The result of <code class="docutils literal notranslate"><span class="pre">!getdagop</span></code> can be used directly in a context where
any record class at all is acceptable (typically placing it into
another dag value). But in other contexts, it must be explicitly
cast to a particular class. The <code class="docutils literal notranslate"><span class="pre"><</span></code><em>type</em><code class="docutils literal notranslate"><span class="pre">></span></code> syntax is
provided to make this easy.</p>
<p>For example, to assign the result to a value of type <code class="docutils literal notranslate"><span class="pre">BaseClass</span></code>, you
could write either of these:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>BaseClass b = !getdagop<BaseClass>(someDag);
BaseClass b = !cast<BaseClass>(!getdagop(someDag));
</pre></div>
</div>
<p>But to create a new DAG node that reuses the operator from another, no
cast is necessary:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>dag d = !dag(!getdagop(someDag), args, names);
</pre></div>
</div>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">!gt(</span></code><em>a</em><cite>,</cite> <em>b</em><code class="docutils literal notranslate"><span class="pre">)</span></code></dt><dd><p>This operator produces 1 if <em>a</em> is greater than <em>b</em>; 0 otherwise.
The arguments must be <code class="docutils literal notranslate"><span class="pre">bit</span></code>, <code class="docutils literal notranslate"><span class="pre">bits</span></code>, <code class="docutils literal notranslate"><span class="pre">int</span></code>, or <code class="docutils literal notranslate"><span class="pre">string</span></code> values.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">!head(</span></code><em>a</em><code class="docutils literal notranslate"><span class="pre">)</span></code></dt><dd><p>This operator produces the zeroth element of the list <em>a</em>.
(See also <code class="docutils literal notranslate"><span class="pre">!tail</span></code>.)</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">!if(</span></code><em>test</em><code class="docutils literal notranslate"><span class="pre">,</span></code> <em>then</em><code class="docutils literal notranslate"><span class="pre">,</span></code> <em>else</em><code class="docutils literal notranslate"><span class="pre">)</span></code></dt><dd><p>This operator evaluates the <em>test</em>, which must produce a <code class="docutils literal notranslate"><span class="pre">bit</span></code> or
<code class="docutils literal notranslate"><span class="pre">int</span></code>. If the result is not 0, the <em>then</em> expression is produced; otherwise
the <em>else</em> expression is produced.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">!interleave(</span></code><em>list</em><code class="docutils literal notranslate"><span class="pre">,</span></code> <em>delim</em><code class="docutils literal notranslate"><span class="pre">)</span></code></dt><dd><p>This operator concatenates the items in the <em>list</em>, interleaving the
<em>delim</em> string between each pair, and produces the resulting string.
The list can be a list of string, int, bits, or bit. An empty list
results in an empty string. The delimiter can be the empty string.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">!isa<</span></code><em>type</em><code class="docutils literal notranslate"><span class="pre">>(</span></code><em>a</em><code class="docutils literal notranslate"><span class="pre">)</span></code></dt><dd><p>This operator produces 1 if the type of <em>a</em> is a subtype of the given <em>type</em>; 0
otherwise.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">!le(</span></code><em>a</em><code class="docutils literal notranslate"><span class="pre">,</span></code> <em>b</em><code class="docutils literal notranslate"><span class="pre">)</span></code></dt><dd><p>This operator produces 1 if <em>a</em> is less than or equal to <em>b</em>; 0 otherwise.
The arguments must be <code class="docutils literal notranslate"><span class="pre">bit</span></code>, <code class="docutils literal notranslate"><span class="pre">bits</span></code>, <code class="docutils literal notranslate"><span class="pre">int</span></code>, or <code class="docutils literal notranslate"><span class="pre">string</span></code> values.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">!listconcat(</span></code><em>list1</em><code class="docutils literal notranslate"><span class="pre">,</span></code> <em>list2</em><code class="docutils literal notranslate"><span class="pre">,</span> <span class="pre">...)</span></code></dt><dd><p>This operator concatenates the list arguments <em>list1</em>, <em>list2</em>, etc., and
produces the resulting list. The lists must have the same element type.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">!listsplat(</span></code><em>value</em><code class="docutils literal notranslate"><span class="pre">,</span></code> <em>count</em><code class="docutils literal notranslate"><span class="pre">)</span></code></dt><dd><p>This operator produces a list of length <em>count</em> whose elements are all
equal to the <em>value</em>. For example, <code class="docutils literal notranslate"><span class="pre">!listsplat(42,</span> <span class="pre">3)</span></code> results in
<code class="docutils literal notranslate"><span class="pre">[42,</span> <span class="pre">42,</span> <span class="pre">42]</span></code>.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">!lt(</span></code><em>a</em><cite>,</cite> <em>b</em><code class="docutils literal notranslate"><span class="pre">)</span></code></dt><dd><p>This operator produces 1 if <em>a</em> is less than <em>b</em>; 0 otherwise.
The arguments must be <code class="docutils literal notranslate"><span class="pre">bit</span></code>, <code class="docutils literal notranslate"><span class="pre">bits</span></code>, <code class="docutils literal notranslate"><span class="pre">int</span></code>, or <code class="docutils literal notranslate"><span class="pre">string</span></code> values.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">!mul(</span></code><em>a</em><code class="docutils literal notranslate"><span class="pre">,</span></code> <em>b</em><code class="docutils literal notranslate"><span class="pre">,</span> <span class="pre">...)</span></code></dt><dd><p>This operator multiplies <em>a</em>, <em>b</em>, etc., and produces the product.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">!ne(</span></code><em>a</em><cite>,</cite> <em>b</em><code class="docutils literal notranslate"><span class="pre">)</span></code></dt><dd><p>This operator produces 1 if <em>a</em> is not equal to <em>b</em>; 0 otherwise.
The arguments must be <code class="docutils literal notranslate"><span class="pre">bit</span></code>, <code class="docutils literal notranslate"><span class="pre">bits</span></code>, <code class="docutils literal notranslate"><span class="pre">int</span></code>, <code class="docutils literal notranslate"><span class="pre">string</span></code>,
or record values. Use <code class="docutils literal notranslate"><span class="pre">!cast<string></span></code> to compare other types of objects.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">!not(</span></code><em>a</em><code class="docutils literal notranslate"><span class="pre">)</span></code></dt><dd><p>This operator performs a logical NOT on <em>a</em>, which must be
an integer. The argument 0 results in 1 (true); any other
argument results in 0 (false).</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">!or(</span></code><em>a</em><code class="docutils literal notranslate"><span class="pre">,</span></code> <em>b</em><code class="docutils literal notranslate"><span class="pre">,</span> <span class="pre">...)</span></code></dt><dd><p>This operator does a bitwise OR on <em>a</em>, <em>b</em>, etc., and produces the
result. A logical OR can be performed if all the arguments are either
0 or 1.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">!setdagop(</span></code><em>dag</em><code class="docutils literal notranslate"><span class="pre">,</span></code> <em>op</em><code class="docutils literal notranslate"><span class="pre">)</span></code></dt><dd><p>This operator produces a DAG node with the same arguments as <em>dag</em>, but with its
operator replaced with <em>op</em>.</p>
<p>Example: <code class="docutils literal notranslate"><span class="pre">!setdagop((foo</span> <span class="pre">1,</span> <span class="pre">2),</span> <span class="pre">bar)</span></code> results in <code class="docutils literal notranslate"><span class="pre">(bar</span> <span class="pre">1,</span> <span class="pre">2)</span></code>.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">!shl(</span></code><em>a</em><code class="docutils literal notranslate"><span class="pre">,</span></code> <em>count</em><code class="docutils literal notranslate"><span class="pre">)</span></code></dt><dd><p>This operator shifts <em>a</em> left logically by <em>count</em> bits and produces the resulting
value. The operation is performed on a 64-bit integer; the result
is undefined for shift counts outside 0…63.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">!size(</span></code><em>a</em><code class="docutils literal notranslate"><span class="pre">)</span></code></dt><dd><p>This operator produces the size of the string, list, or dag <em>a</em>.
The size of a DAG is the number of arguments; the operator does not count.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">!sra(</span></code><em>a</em><code class="docutils literal notranslate"><span class="pre">,</span></code> <em>count</em><code class="docutils literal notranslate"><span class="pre">)</span></code></dt><dd><p>This operator shifts <em>a</em> right arithmetically by <em>count</em> bits and produces the resulting
value. The operation is performed on a 64-bit integer; the result
is undefined for shift counts outside 0…63.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">!srl(</span></code><em>a</em><code class="docutils literal notranslate"><span class="pre">,</span></code> <em>count</em><code class="docutils literal notranslate"><span class="pre">)</span></code></dt><dd><p>This operator shifts <em>a</em> right logically by <em>count</em> bits and produces the resulting
value. The operation is performed on a 64-bit integer; the result
is undefined for shift counts outside 0…63.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">!strconcat(</span></code><em>str1</em><code class="docutils literal notranslate"><span class="pre">,</span></code> <em>str2</em><code class="docutils literal notranslate"><span class="pre">,</span> <span class="pre">...)</span></code></dt><dd><p>This operator concatenates the string arguments <em>str1</em>, <em>str2</em>, etc., and
produces the resulting string.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">!sub(</span></code><em>a</em><code class="docutils literal notranslate"><span class="pre">,</span></code> <em>b</em><code class="docutils literal notranslate"><span class="pre">)</span></code></dt><dd><p>This operator subtracts <em>b</em> from <em>a</em> and produces the arithmetic difference.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">!subst(</span></code><em>target</em><code class="docutils literal notranslate"><span class="pre">,</span></code> <em>repl</em><code class="docutils literal notranslate"><span class="pre">,</span></code> <em>value</em><code class="docutils literal notranslate"><span class="pre">)</span></code></dt><dd><p>This operator replaces all occurrences of the <em>target</em> in the <em>value</em> with
the <em>repl</em> and produces the resulting value. The <em>value</em> can
be a string, in which case substring substitution is performed.</p>
<p>The <em>value</em> can be a record name, in which case the operator produces the <em>repl</em>
record if the <em>target</em> record name equals the <em>value</em> record name; otherwise it
produces the <em>value</em>.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">!substr(</span></code><em>string</em><code class="docutils literal notranslate"><span class="pre">,</span></code> <em>start</em>[<code class="docutils literal notranslate"><span class="pre">,</span></code> <em>length</em>]<code class="docutils literal notranslate"><span class="pre">)</span></code></dt><dd><p>This operator extracts a substring of the given <em>string</em>. The starting
position of the substring is specified by <em>start</em>, which can range
between 0 and the length of the string. The length of the substring
is specified by <em>length</em>; if not specified, the rest of the string is
extracted. The <em>start</em> and <em>length</em> arguments must be integers.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">!tail(</span></code><em>a</em><code class="docutils literal notranslate"><span class="pre">)</span></code></dt><dd><p>This operator produces a new list with all the elements
of the list <em>a</em> except for the zeroth one. (See also <code class="docutils literal notranslate"><span class="pre">!head</span></code>.)</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">!xor(</span></code><em>a</em><code class="docutils literal notranslate"><span class="pre">,</span></code> <em>b</em><code class="docutils literal notranslate"><span class="pre">,</span> <span class="pre">...)</span></code></dt><dd><p>This operator does a bitwise EXCLUSIVE OR on <em>a</em>, <em>b</em>, etc., and produces
the result. A logical XOR can be performed if all the arguments are either
0 or 1.</p>
</dd>
</dl>
</div>
<div class="section" id="appendix-b-paste-operator-examples">
<h2><a class="toc-backref" href="#id35"><span class="sectnum">1.11</span> Appendix B: Paste Operator Examples</a><a class="headerlink" href="#appendix-b-paste-operator-examples" title="Permalink to this headline">¶</a></h2>
<p>Here is an example illustrating the use of the paste operator in record names.</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>defvar suffix = "_suffstring";
defvar some_ints = [0, 1, 2, 3];
def name # suffix {
}
foreach i = [1, 2] in {
def rec # i {
}
}
</pre></div>
</div>
<p>The first <code class="docutils literal notranslate"><span class="pre">def</span></code> does not use the value of the <code class="docutils literal notranslate"><span class="pre">suffix</span></code> variable. The
second def does use the value of the <code class="docutils literal notranslate"><span class="pre">i</span></code> iterator variable, because it is not a
global name. The following records are produced.</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>def namesuffix {
}
def rec1 {
}
def rec2 {
}
</pre></div>
</div>
<p>Here is a second example illustrating the paste operator in field value expressions.</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>def test {
string strings = suffix # suffix;
list<int> integers = some_ints # [4, 5, 6];
}
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">strings</span></code> field expression uses <code class="docutils literal notranslate"><span class="pre">suffix</span></code> on both sides of the paste
operator. It is evaluated normally on the left hand side, but taken verbatim
on the right hand side. The <code class="docutils literal notranslate"><span class="pre">integers</span></code> field expression uses the value of
the <code class="docutils literal notranslate"><span class="pre">some_ints</span></code> variable and a literal list. The following record is
produced.</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>def test {
string strings = "_suffstringsuffix";
list<int> ints = [0, 1, 2, 3, 4, 5, 6];
}
</pre></div>
</div>
</div>
<div class="section" id="appendix-c-sample-record">
<h2><a class="toc-backref" href="#id36"><span class="sectnum">1.12</span> Appendix C: Sample Record</a><a class="headerlink" href="#appendix-c-sample-record" title="Permalink to this headline">¶</a></h2>
<p>One target machine supported by LLVM is the Intel x86. The following output
from TableGen shows the record that is created to represent the 32-bit
register-to-register ADD instruction.</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>def ADD32rr { // InstructionEncoding Instruction X86Inst I ITy Sched BinOpRR BinOpRR_RF
int Size = 0;
string DecoderNamespace = "";
list<Predicate> Predicates = [];
string DecoderMethod = "";
bit hasCompleteDecoder = 1;
string Namespace = "X86";
dag OutOperandList = (outs GR32:$dst);
dag InOperandList = (ins GR32:$src1, GR32:$src2);
string AsmString = "add{l} {$src2, $src1|$src1, $src2}";
EncodingByHwMode EncodingInfos = ?;
list<dag> Pattern = [(set GR32:$dst, EFLAGS, (X86add_flag GR32:$src1, GR32:$src2))];
list<Register> Uses = [];
list<Register> Defs = [EFLAGS];
int CodeSize = 3;
int AddedComplexity = 0;
bit isPreISelOpcode = 0;
bit isReturn = 0;
bit isBranch = 0;
bit isEHScopeReturn = 0;
bit isIndirectBranch = 0;
bit isCompare = 0;
bit isMoveImm = 0;
bit isMoveReg = 0;
bit isBitcast = 0;
bit isSelect = 0;
bit isBarrier = 0;
bit isCall = 0;
bit isAdd = 0;
bit isTrap = 0;
bit canFoldAsLoad = 0;
bit mayLoad = ?;
bit mayStore = ?;
bit mayRaiseFPException = 0;
bit isConvertibleToThreeAddress = 1;
bit isCommutable = 1;
bit isTerminator = 0;
bit isReMaterializable = 0;
bit isPredicable = 0;
bit isUnpredicable = 0;
bit hasDelaySlot = 0;
bit usesCustomInserter = 0;
bit hasPostISelHook = 0;
bit hasCtrlDep = 0;
bit isNotDuplicable = 0;
bit isConvergent = 0;
bit isAuthenticated = 0;
bit isAsCheapAsAMove = 0;
bit hasExtraSrcRegAllocReq = 0;
bit hasExtraDefRegAllocReq = 0;
bit isRegSequence = 0;
bit isPseudo = 0;
bit isExtractSubreg = 0;
bit isInsertSubreg = 0;
bit variadicOpsAreDefs = 0;
bit hasSideEffects = ?;
bit isCodeGenOnly = 0;
bit isAsmParserOnly = 0;
bit hasNoSchedulingInfo = 0;
InstrItinClass Itinerary = NoItinerary;
list<SchedReadWrite> SchedRW = [WriteALU];
string Constraints = "$src1 = $dst";
string DisableEncoding = "";
string PostEncoderMethod = "";
bits<64> TSFlags = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0 };
string AsmMatchConverter = "";
string TwoOperandAliasConstraint = "";
string AsmVariantName = "";
bit UseNamedOperandTable = 0;
bit FastISelShouldIgnore = 0;
bits<8> Opcode = { 0, 0, 0, 0, 0, 0, 0, 1 };
Format Form = MRMDestReg;
bits<7> FormBits = { 0, 1, 0, 1, 0, 0, 0 };
ImmType ImmT = NoImm;
bit ForceDisassemble = 0;
OperandSize OpSize = OpSize32;
bits<2> OpSizeBits = { 1, 0 };
AddressSize AdSize = AdSizeX;
bits<2> AdSizeBits = { 0, 0 };
Prefix OpPrefix = NoPrfx;
bits<3> OpPrefixBits = { 0, 0, 0 };
Map OpMap = OB;
bits<3> OpMapBits = { 0, 0, 0 };
bit hasREX_WPrefix = 0;
FPFormat FPForm = NotFP;
bit hasLockPrefix = 0;
Domain ExeDomain = GenericDomain;
bit hasREPPrefix = 0;
Encoding OpEnc = EncNormal;
bits<2> OpEncBits = { 0, 0 };
bit HasVEX_W = 0;
bit IgnoresVEX_W = 0;
bit EVEX_W1_VEX_W0 = 0;
bit hasVEX_4V = 0;
bit hasVEX_L = 0;
bit ignoresVEX_L = 0;
bit hasEVEX_K = 0;
bit hasEVEX_Z = 0;
bit hasEVEX_L2 = 0;
bit hasEVEX_B = 0;
bits<3> CD8_Form = { 0, 0, 0 };
int CD8_EltSize = 0;
bit hasEVEX_RC = 0;
bit hasNoTrackPrefix = 0;
bits<7> VectSize = { 0, 0, 1, 0, 0, 0, 0 };
bits<7> CD8_Scale = { 0, 0, 0, 0, 0, 0, 0 };
string FoldGenRegForm = ?;
string EVEX2VEXOverride = ?;
bit isMemoryFoldable = 1;
bit notEVEX2VEXConvertible = 0;
}
</pre></div>
</div>
<p>On the first line of the record, you can see that the <code class="docutils literal notranslate"><span class="pre">ADD32rr</span></code> record
inherited from eight classes. Although the inheritance hierarchy is complex,
using parent classes is much simpler than specifying the 109 individual
fields for each instruction.</p>
<p>Here is the code fragment used to define <code class="docutils literal notranslate"><span class="pre">ADD32rr</span></code> and multiple other
<code class="docutils literal notranslate"><span class="pre">ADD</span></code> instructions:</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>defm ADD : ArithBinOp_RF<0x00, 0x02, 0x04, "add", MRM0r, MRM0m,
X86add_flag, add, 1, 1, 1>;
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">defm</span></code> statement tells TableGen that <code class="docutils literal notranslate"><span class="pre">ArithBinOp_RF</span></code> is a
multiclass, which contains multiple concrete record definitions that inherit
from <code class="docutils literal notranslate"><span class="pre">BinOpRR_RF</span></code>. That class, in turn, inherits from <code class="docutils literal notranslate"><span class="pre">BinOpRR</span></code>, which
inherits from <code class="docutils literal notranslate"><span class="pre">ITy</span></code> and <code class="docutils literal notranslate"><span class="pre">Sched</span></code>, and so forth. The fields are inherited
from all the parent classes; for example, <code class="docutils literal notranslate"><span class="pre">IsIndirectBranch</span></code> is inherited
from the <code class="docutils literal notranslate"><span class="pre">Instruction</span></code> class.</p>
</div>
</div>
<div class="clearer"></div>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../TableGenFundamentals.html" title="TableGen Fundamentals"
>next</a> |</li>
<li class="right" >
<a href="BackGuide.html" title="1 TableGen Backend Developer’s Guide"
>previous</a> |</li>
<li><a href="https://llvm.org/">LLVM Home</a> | </li>
<li><a href="../index.html">Documentation</a>»</li>
<li class="nav-item nav-item-1"><a href="../UserGuides.html" >User Guides</a> »</li>
<li class="nav-item nav-item-2"><a href="index.html" >TableGen Overview</a> »</li>
<li class="nav-item nav-item-this"><a href=""><span class="sectnum">1</span> TableGen Programmer’s Reference</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">
© Copyright 2003-2021, LLVM Project.
Last updated on 2021-09-18.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 3.5.4.
</div>
</body>
</html>
|