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
|
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>1 TableGen Backend Developer’s Guide — 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="1 TableGen Programmer’s Reference" href="ProgRef.html" />
<link rel="prev" title="TableGen BackEnds" href="BackEnds.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="ProgRef.html" title="1 TableGen Programmer’s Reference"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="BackEnds.html" title="TableGen BackEnds"
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 Backend Developer’s Guide</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/BackGuide.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-backend-developer-s-guide">
<h1><span class="sectnum">1</span> TableGen Backend Developer’s Guide<a class="headerlink" href="#tablegen-backend-developer-s-guide" 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></li>
<li><p><a class="reference internal" href="#data-structures" id="id2"><span class="sectnum">1.2</span> Data Structures</a></p>
<ul class="auto-toc">
<li><p><a class="reference internal" href="#recordkeeper" id="id3"><span class="sectnum">1.2.1</span> <code class="docutils literal notranslate"><span class="pre">RecordKeeper</span></code></a></p></li>
<li><p><a class="reference internal" href="#record" id="id4"><span class="sectnum">1.2.2</span> <code class="docutils literal notranslate"><span class="pre">Record</span></code></a></p></li>
<li><p><a class="reference internal" href="#recordval" id="id5"><span class="sectnum">1.2.3</span> <code class="docutils literal notranslate"><span class="pre">RecordVal</span></code></a></p></li>
<li><p><a class="reference internal" href="#recty" id="id6"><span class="sectnum">1.2.4</span> <code class="docutils literal notranslate"><span class="pre">RecTy</span></code></a></p>
<ul class="auto-toc">
<li><p><a class="reference internal" href="#bitsrecty" id="id7"><span class="sectnum">1.2.4.1</span> <code class="docutils literal notranslate"><span class="pre">BitsRecTy</span></code></a></p></li>
<li><p><a class="reference internal" href="#listrecty" id="id8"><span class="sectnum">1.2.4.2</span> <code class="docutils literal notranslate"><span class="pre">ListRecTy</span></code></a></p></li>
<li><p><a class="reference internal" href="#recordrecty" id="id9"><span class="sectnum">1.2.4.3</span> <code class="docutils literal notranslate"><span class="pre">RecordRecTy</span></code></a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#init" id="id10"><span class="sectnum">1.2.5</span> <code class="docutils literal notranslate"><span class="pre">Init</span></code></a></p>
<ul class="auto-toc">
<li><p><a class="reference internal" href="#unsetinit" id="id11"><span class="sectnum">1.2.5.1</span> <code class="docutils literal notranslate"><span class="pre">UnsetInit</span></code></a></p></li>
<li><p><a class="reference internal" href="#typedinit" id="id12"><span class="sectnum">1.2.5.2</span> <code class="docutils literal notranslate"><span class="pre">TypedInit</span></code></a></p></li>
<li><p><a class="reference internal" href="#bitinit" id="id13"><span class="sectnum">1.2.5.3</span> <code class="docutils literal notranslate"><span class="pre">BitInit</span></code></a></p></li>
<li><p><a class="reference internal" href="#bitsinit" id="id14"><span class="sectnum">1.2.5.4</span> <code class="docutils literal notranslate"><span class="pre">BitsInit</span></code></a></p></li>
<li><p><a class="reference internal" href="#daginit" id="id15"><span class="sectnum">1.2.5.5</span> <code class="docutils literal notranslate"><span class="pre">DagInit</span></code></a></p></li>
<li><p><a class="reference internal" href="#definit" id="id16"><span class="sectnum">1.2.5.6</span> <code class="docutils literal notranslate"><span class="pre">DefInit</span></code></a></p></li>
<li><p><a class="reference internal" href="#intinit" id="id17"><span class="sectnum">1.2.5.7</span> <code class="docutils literal notranslate"><span class="pre">IntInit</span></code></a></p></li>
<li><p><a class="reference internal" href="#listinit" id="id18"><span class="sectnum">1.2.5.8</span> <code class="docutils literal notranslate"><span class="pre">ListInit</span></code></a></p></li>
<li><p><a class="reference internal" href="#stringinit" id="id19"><span class="sectnum">1.2.5.9</span> <code class="docutils literal notranslate"><span class="pre">StringInit</span></code></a></p></li>
</ul>
</li>
</ul>
</li>
<li><p><a class="reference internal" href="#creating-a-new-backend" id="id20"><span class="sectnum">1.3</span> Creating a New Backend</a></p></li>
<li><p><a class="reference internal" href="#the-backend-skeleton" id="id21"><span class="sectnum">1.4</span> The Backend Skeleton</a></p></li>
<li><p><a class="reference internal" href="#getting-classes" id="id22"><span class="sectnum">1.5</span> Getting Classes</a></p></li>
<li><p><a class="reference internal" href="#getting-records" id="id23"><span class="sectnum">1.6</span> Getting Records</a></p></li>
<li><p><a class="reference internal" href="#getting-record-names-and-fields" id="id24"><span class="sectnum">1.7</span> Getting Record Names and Fields</a></p></li>
<li><p><a class="reference internal" href="#getting-record-superclasses" id="id25"><span class="sectnum">1.8</span> Getting Record Superclasses</a></p></li>
<li><p><a class="reference internal" href="#emitting-text-to-the-output-stream" id="id26"><span class="sectnum">1.9</span> Emitting Text to the Output Stream</a></p></li>
<li><p><a class="reference internal" href="#printing-error-messages" id="id27"><span class="sectnum">1.10</span> Printing Error Messages</a></p></li>
<li><p><a class="reference internal" href="#debugging-tools" id="id28"><span class="sectnum">1.11</span> Debugging Tools</a></p>
<ul class="auto-toc">
<li><p><a class="reference internal" href="#the-printrecords-backend" id="id29"><span class="sectnum">1.11.1</span> The <code class="docutils literal notranslate"><span class="pre">PrintRecords</span></code> Backend</a></p></li>
<li><p><a class="reference internal" href="#the-printdetailedrecords-backend" id="id30"><span class="sectnum">1.11.2</span> The <code class="docutils literal notranslate"><span class="pre">PrintDetailedRecords</span></code> Backend</a></p></li>
<li><p><a class="reference internal" href="#timing-tablegen-phases" id="id31"><span class="sectnum">1.11.3</span> Timing TableGen Phases</a></p></li>
</ul>
</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 backends, which extract information from a subset of the records
and generate an output file. 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 is a guide to writing a backend for TableGen. It is not a
complete reference manual, but rather a guide to using the facilities
provided by TableGen for the backends. For a complete reference to the
various data structures and functions involved, see the primary TableGen
header file (<code class="docutils literal notranslate"><span class="pre">record.h</span></code>) and/or the Doxygen documentation.</p>
<p>This document assumes that you have read the <a class="reference internal" href="ProgRef.html"><span class="doc">TableGen Programmer’s
Reference</span></a>, which provides a detailed reference for coding
TableGen source files. For a description of the existing backends, see
<a class="reference internal" href="BackEnds.html"><span class="doc">TableGen BackEnds</span></a>.</p>
</div>
<div class="section" id="data-structures">
<h2><a class="toc-backref" href="#id2"><span class="sectnum">1.2</span> Data Structures</a><a class="headerlink" href="#data-structures" title="Permalink to this headline">¶</a></h2>
<p>The following sections describe the data structures that contain the classes
and records that are collected from the TableGen source files by the
TableGen parser. Note that the term <em>class</em> refers to an abstract record
class, while the term <em>record</em> refers to a concrete record.</p>
<p>Unless otherwise noted, functions associated with classes are instance
functions.</p>
<div class="section" id="recordkeeper">
<h3><a class="toc-backref" href="#id3"><span class="sectnum">1.2.1</span> <code class="docutils literal notranslate"><span class="pre">RecordKeeper</span></code></a><a class="headerlink" href="#recordkeeper" title="Permalink to this headline">¶</a></h3>
<p>An instance of the <code class="docutils literal notranslate"><span class="pre">RecordKeeper</span></code> class acts as the container for all the
classes and records parsed and collected by TableGen. The <code class="docutils literal notranslate"><span class="pre">RecordKeeper</span></code>
instance is passed to the backend when it is invoked by TableGen. This class
is usually abbreviated <code class="docutils literal notranslate"><span class="pre">RK</span></code>.</p>
<p>There are two maps in the recordkeeper, one for classes and one for records
(the latter often referred to as <em>defs</em>). Each map maps the class or record
name to an instance of the <code class="docutils literal notranslate"><span class="pre">Record</span></code> class (see <a class="reference internal" href="#record">Record</a>), which contains
all the information about that class or record.</p>
<p>In addition to the two maps, the <code class="docutils literal notranslate"><span class="pre">RecordKeeper</span></code> instance contains:</p>
<ul class="simple">
<li><p>A map that maps the names of global variables to their values.
Global variables are defined in TableGen files with outer
<code class="docutils literal notranslate"><span class="pre">defvar</span></code> statements.</p></li>
<li><p>A counter for naming anonymous records.</p></li>
</ul>
<p>The <code class="docutils literal notranslate"><span class="pre">RecordKeeper</span></code> class provides a few useful functions.</p>
<ul class="simple">
<li><p>Functions to get the complete class and record maps.</p></li>
<li><p>Functions to get a subset of the records based on their parent classes.</p></li>
<li><p>Functions to get individual classes, records, and globals, by name.</p></li>
</ul>
<p>A <code class="docutils literal notranslate"><span class="pre">RecordKeeper</span></code> instance can be printed to an output stream with the <code class="docutils literal notranslate"><span class="pre"><<</span></code>
operator.</p>
</div>
<div class="section" id="record">
<h3><a class="toc-backref" href="#id4"><span class="sectnum">1.2.2</span> <code class="docutils literal notranslate"><span class="pre">Record</span></code></a><a class="headerlink" href="#record" title="Permalink to this headline">¶</a></h3>
<p>Each class or record built by TableGen is represented by an instance of
the <code class="docutils literal notranslate"><span class="pre">Record</span></code> class. The <code class="docutils literal notranslate"><span class="pre">RecordKeeper</span></code> instance contains one map for the
classes and one for the records. The primary data members of a record are
the record name, the vector of field names and their values, and the vector of
superclasses of the record.</p>
<p>The record name is stored as a pointer to an <code class="docutils literal notranslate"><span class="pre">Init</span></code> (see <a class="reference internal" href="#init">Init</a>), which
is a class whose instances hold TableGen values (sometimes referred to as
<em>initializers</em>). The field names and values are stored in a vector of
<code class="docutils literal notranslate"><span class="pre">RecordVal</span></code> instances (see <a class="reference internal" href="#recordval">RecordVal</a>), each of which contains both the
field name and its value. The superclass vector contains a sequence of
pairs, with each pair including the superclass record and its source
file location.</p>
<p>In addition to those members, a <code class="docutils literal notranslate"><span class="pre">Record</span></code> instance contains:</p>
<ul class="simple">
<li><p>A vector of source file locations that includes the record definition
itself, plus the locations of any multiclasses involved in its definition.</p></li>
<li><p>For a class record, a vector of the class’s template arguments.</p></li>
<li><p>An instance of <code class="docutils literal notranslate"><span class="pre">DefInit</span></code> (see <a class="reference internal" href="#definit">DefInit</a>) corresponding to this record.</p></li>
<li><p>A unique record ID.</p></li>
<li><p>A boolean that specifies whether this is a class definition.</p></li>
<li><p>A boolean that specifies whether this is an anonymous record.</p></li>
</ul>
<p>The <code class="docutils literal notranslate"><span class="pre">Record</span></code> class provides many useful functions.</p>
<ul class="simple">
<li><p>Functions to get the record name, fields, source file locations,
template arguments, and unique ID.</p></li>
<li><p>Functions to get all the record’s superclasses or just its direct
superclasses.</p></li>
<li><p>Functions to get a particular field value by specifying its name in various
forms, and returning its value in various forms
(see <a class="reference internal" href="#getting-record-names-and-fields">Getting Record Names and Fields</a>).</p></li>
<li><p>Boolean functions to check the various attributes of the record.</p></li>
</ul>
<p>A <code class="docutils literal notranslate"><span class="pre">Record</span></code> instance can be printed to an output stream with the <code class="docutils literal notranslate"><span class="pre"><<</span></code>
operator.</p>
</div>
<div class="section" id="recordval">
<h3><a class="toc-backref" href="#id5"><span class="sectnum">1.2.3</span> <code class="docutils literal notranslate"><span class="pre">RecordVal</span></code></a><a class="headerlink" href="#recordval" title="Permalink to this headline">¶</a></h3>
<p>Each field of a record is stored in an instance of the <code class="docutils literal notranslate"><span class="pre">RecordVal</span></code> class.
The <code class="docutils literal notranslate"><span class="pre">Record</span></code> instance includes a vector of these value instances. A
<code class="docutils literal notranslate"><span class="pre">RecordVal</span></code> instance contains the name of the field, stored in an <code class="docutils literal notranslate"><span class="pre">Init</span></code>
instance. It also contains the value of the field, likewise stored in an
<code class="docutils literal notranslate"><span class="pre">Init</span></code>. (A better name for this class might be <code class="docutils literal notranslate"><span class="pre">RecordField</span></code>.)</p>
<p>In addition to those primary members, the <code class="docutils literal notranslate"><span class="pre">RecordVal</span></code> has other data members.</p>
<ul class="simple">
<li><p>The source file location of the field definition.</p></li>
<li><p>The type of the field, stored as an instance
of the <code class="docutils literal notranslate"><span class="pre">RecTy</span></code> class (see <a class="reference internal" href="#recty">RecTy</a>).</p></li>
</ul>
<p>The <code class="docutils literal notranslate"><span class="pre">RecordVal</span></code> class provides some useful functions.</p>
<ul class="simple">
<li><p>Functions to get the name of the field in various forms.</p></li>
<li><p>A function to get the type of the field.</p></li>
<li><p>A function to get the value of the field.</p></li>
<li><p>A function to get the source file location.</p></li>
</ul>
<p>Note that field values are more easily obtained directly from the <code class="docutils literal notranslate"><span class="pre">Record</span></code>
instance (see <a class="reference internal" href="#record">Record</a>).</p>
<p>A <code class="docutils literal notranslate"><span class="pre">RecordVal</span></code> instance can be printed to an output stream with the <code class="docutils literal notranslate"><span class="pre"><<</span></code>
operator.</p>
</div>
<div class="section" id="recty">
<h3><a class="toc-backref" href="#id6"><span class="sectnum">1.2.4</span> <code class="docutils literal notranslate"><span class="pre">RecTy</span></code></a><a class="headerlink" href="#recty" title="Permalink to this headline">¶</a></h3>
<p>The <code class="docutils literal notranslate"><span class="pre">RecTy</span></code> class is used to represent the types of field values. It is
the base class for a series of subclasses, one for each of the
available field types. The <code class="docutils literal notranslate"><span class="pre">RecTy</span></code> class has one data member that is an
enumerated type specifying the specific type of field value. (A better
name for this class might be <code class="docutils literal notranslate"><span class="pre">FieldTy</span></code>.)</p>
<p>The <code class="docutils literal notranslate"><span class="pre">RecTy</span></code> class provides a few useful functions.</p>
<ul class="simple">
<li><p>A virtual function to get the type name as a string.</p></li>
<li><p>A virtual function to check whether all the values of this type can
be converted to another given type.</p></li>
<li><p>A virtual function to check whether this type is a subtype of
another given type.</p></li>
<li><p>A function to get the corresponding <code class="docutils literal notranslate"><span class="pre">list</span></code>
type for lists with elements of this type. For example, the function
returns the <code class="docutils literal notranslate"><span class="pre">list<int></span></code> type when called with the <code class="docutils literal notranslate"><span class="pre">int</span></code> type.</p></li>
</ul>
<p>The subclasses that inherit from <code class="docutils literal notranslate"><span class="pre">RecTy</span></code> are
<code class="docutils literal notranslate"><span class="pre">BitRecTy</span></code>,
<code class="docutils literal notranslate"><span class="pre">BitsRecTy</span></code>,
<code class="docutils literal notranslate"><span class="pre">CodeRecTy</span></code>,
<code class="docutils literal notranslate"><span class="pre">DagRecTy</span></code>,
<code class="docutils literal notranslate"><span class="pre">IntRecTy</span></code>,
<code class="docutils literal notranslate"><span class="pre">ListRecTy</span></code>,
<code class="docutils literal notranslate"><span class="pre">RecordRecTy</span></code>, and
<code class="docutils literal notranslate"><span class="pre">StringRecTy</span></code>.
Some of these classes have additional members that
are described in the following subsections.</p>
<p><em>All</em> of the classes derived from <code class="docutils literal notranslate"><span class="pre">RecTy</span></code> provide the <code class="docutils literal notranslate"><span class="pre">get()</span></code> function.
It returns an instance of <code class="docutils literal notranslate"><span class="pre">Recty</span></code> corresponding to the derived class.
Some of the <code class="docutils literal notranslate"><span class="pre">get()</span></code> functions require an argument to
specify which particular variant of the type is desired. These arguments are
described in the following subsections.</p>
<p>A <code class="docutils literal notranslate"><span class="pre">RecTy</span></code> instance can be printed to an output stream with the <code class="docutils literal notranslate"><span class="pre"><<</span></code>
operator.</p>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>It is not specified whether there is a single <code class="docutils literal notranslate"><span class="pre">RecTy</span></code> instance of a
particular type or multiple instances.</p>
</div>
<div class="section" id="bitsrecty">
<h4><a class="toc-backref" href="#id7"><span class="sectnum">1.2.4.1</span> <code class="docutils literal notranslate"><span class="pre">BitsRecTy</span></code></a><a class="headerlink" href="#bitsrecty" title="Permalink to this headline">¶</a></h4>
<p>This class includes a data member with the size of the <code class="docutils literal notranslate"><span class="pre">bits</span></code> value and a
function to get that size.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">get()</span></code> function takes the length of the sequence, <em>n</em>, and returns the
<code class="docutils literal notranslate"><span class="pre">BitsRecTy</span></code> type corresponding to <code class="docutils literal notranslate"><span class="pre">bits<</span></code><em>n</em><code class="docutils literal notranslate"><span class="pre">></span></code>.</p>
</div>
<div class="section" id="listrecty">
<h4><a class="toc-backref" href="#id8"><span class="sectnum">1.2.4.2</span> <code class="docutils literal notranslate"><span class="pre">ListRecTy</span></code></a><a class="headerlink" href="#listrecty" title="Permalink to this headline">¶</a></h4>
<p>This class includes a data member that specifies the type of the list’s
elements and a function to get that type.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">get()</span></code> function takes the <code class="docutils literal notranslate"><span class="pre">RecTy</span></code> <em>type</em> of the list members and
returns the <code class="docutils literal notranslate"><span class="pre">ListRecTy</span></code> type corresponding to <code class="docutils literal notranslate"><span class="pre">list<</span></code><em>type</em><code class="docutils literal notranslate"><span class="pre">></span></code>.</p>
</div>
<div class="section" id="recordrecty">
<h4><a class="toc-backref" href="#id9"><span class="sectnum">1.2.4.3</span> <code class="docutils literal notranslate"><span class="pre">RecordRecTy</span></code></a><a class="headerlink" href="#recordrecty" title="Permalink to this headline">¶</a></h4>
<p>This class includes data members that contain the list of parent classes of
this record. It also provides a function to obtain the array of classes and
two functions to get the iterator <code class="docutils literal notranslate"><span class="pre">begin()</span></code> and <code class="docutils literal notranslate"><span class="pre">end()</span></code> values. The
class defines a type for the return values of the latter two functions.</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>using const_record_iterator = Record * const *;
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">get()</span></code> function takes an <code class="docutils literal notranslate"><span class="pre">ArrayRef</span></code> of pointers to the <code class="docutils literal notranslate"><span class="pre">Record</span></code>
instances of the <em>direct</em> superclasses of the record and returns the <code class="docutils literal notranslate"><span class="pre">RecordRecTy</span></code>
corresponding to the record inheriting from those superclasses.</p>
</div>
</div>
<div class="section" id="init">
<h3><a class="toc-backref" href="#id10"><span class="sectnum">1.2.5</span> <code class="docutils literal notranslate"><span class="pre">Init</span></code></a><a class="headerlink" href="#init" title="Permalink to this headline">¶</a></h3>
<p>The <code class="docutils literal notranslate"><span class="pre">Init</span></code> class is used to represent TableGen values. The name derives
from <em>initialization value</em>. This class should not be confused with the
<code class="docutils literal notranslate"><span class="pre">RecordVal</span></code> class, which represents record fields, both their names and
values. The <code class="docutils literal notranslate"><span class="pre">Init</span></code> class is the base class for a series of subclasses, one
for each of the available value types. The primary data member of <code class="docutils literal notranslate"><span class="pre">Init</span></code>
is an enumerated type that represents the specific type of the value.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">Init</span></code> class provides a few useful functions.</p>
<ul class="simple">
<li><p>A function to get the type enumerator.</p></li>
<li><p>A boolean virtual function to determine whether a value is completely
specified; that is, has no uninitialized subvalues.</p></li>
<li><p>Virtual functions to get the value as a string.</p></li>
<li><p>Virtual functions to cast the value to other types, implement the bit
range feature of TableGen, and implement the list slice feature.</p></li>
<li><p>A virtual function to get a particular bit of the value.</p></li>
</ul>
<p>The subclasses that inherit directly from <code class="docutils literal notranslate"><span class="pre">Init</span></code> are
<code class="docutils literal notranslate"><span class="pre">UnsetInit</span></code> and <code class="docutils literal notranslate"><span class="pre">TypedInit</span></code>.</p>
<p>An <code class="docutils literal notranslate"><span class="pre">Init</span></code> instance can be printed to an output stream with the <code class="docutils literal notranslate"><span class="pre"><<</span></code>
operator.</p>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>It is not specified whether two separate initialization values with
the same underlying type and value (e.g., two strings with the value
“Hello”) are represented by two <code class="docutils literal notranslate"><span class="pre">Init</span></code>s or share the same <code class="docutils literal notranslate"><span class="pre">Init</span></code>.</p>
</div>
<div class="section" id="unsetinit">
<h4><a class="toc-backref" href="#id11"><span class="sectnum">1.2.5.1</span> <code class="docutils literal notranslate"><span class="pre">UnsetInit</span></code></a><a class="headerlink" href="#unsetinit" title="Permalink to this headline">¶</a></h4>
<p>This class, a subclass of <code class="docutils literal notranslate"><span class="pre">Init</span></code>, represents the unset (uninitialized)
value. The static function <code class="docutils literal notranslate"><span class="pre">get()</span></code> can be used to obtain the singleton
<code class="docutils literal notranslate"><span class="pre">Init</span></code> of this type.</p>
</div>
<div class="section" id="typedinit">
<h4><a class="toc-backref" href="#id12"><span class="sectnum">1.2.5.2</span> <code class="docutils literal notranslate"><span class="pre">TypedInit</span></code></a><a class="headerlink" href="#typedinit" title="Permalink to this headline">¶</a></h4>
<p>This class, a subclass of <code class="docutils literal notranslate"><span class="pre">Init</span></code>, acts as the parent class of the classes
that represent specific value types (except for the unset value). These
classes include <code class="docutils literal notranslate"><span class="pre">BitInit</span></code>, <code class="docutils literal notranslate"><span class="pre">BitsInit</span></code>, <code class="docutils literal notranslate"><span class="pre">DagInit</span></code>, <code class="docutils literal notranslate"><span class="pre">DefInit</span></code>,
<code class="docutils literal notranslate"><span class="pre">IntInit</span></code>, <code class="docutils literal notranslate"><span class="pre">ListInit</span></code>, and <code class="docutils literal notranslate"><span class="pre">StringInit</span></code>. (There are additional derived
types used by the TableGen parser.)</p>
<p>This class includes a data member that specifies the <code class="docutils literal notranslate"><span class="pre">RecTy</span></code> type of the
value. It provides a function to get that <code class="docutils literal notranslate"><span class="pre">RecTy</span></code> type.</p>
</div>
<div class="section" id="bitinit">
<h4><a class="toc-backref" href="#id13"><span class="sectnum">1.2.5.3</span> <code class="docutils literal notranslate"><span class="pre">BitInit</span></code></a><a class="headerlink" href="#bitinit" title="Permalink to this headline">¶</a></h4>
<p>The <code class="docutils literal notranslate"><span class="pre">BitInit</span></code> class is a subclass of <code class="docutils literal notranslate"><span class="pre">TypedInit</span></code>. Its instances
represent the possible values of a bit: 0 or 1. It includes a data member
that contains the bit.</p>
<p><em>All</em> of the classes derived from <code class="docutils literal notranslate"><span class="pre">TypedInit</span></code> provide the following functions.</p>
<ul class="simple">
<li><p>A static function named <code class="docutils literal notranslate"><span class="pre">get()</span></code> that returns an <code class="docutils literal notranslate"><span class="pre">Init</span></code> representing
the specified value(s). In the case of <code class="docutils literal notranslate"><span class="pre">BitInit</span></code>, <code class="docutils literal notranslate"><span class="pre">get(true)</span></code> returns
an instance of <code class="docutils literal notranslate"><span class="pre">BitInit</span></code> representing true, while <code class="docutils literal notranslate"><span class="pre">get(false)</span></code> returns
an instance
representing false. As noted above, it is not specified whether there
is exactly one or more than one <code class="docutils literal notranslate"><span class="pre">BitInit</span></code> representing true (or false).</p></li>
<li><p>A function named <code class="docutils literal notranslate"><span class="pre">GetValue()</span></code> that returns the value of the instance
in a more direct form, in this case as a <code class="docutils literal notranslate"><span class="pre">bool</span></code>.</p></li>
</ul>
</div>
<div class="section" id="bitsinit">
<h4><a class="toc-backref" href="#id14"><span class="sectnum">1.2.5.4</span> <code class="docutils literal notranslate"><span class="pre">BitsInit</span></code></a><a class="headerlink" href="#bitsinit" title="Permalink to this headline">¶</a></h4>
<p>The <code class="docutils literal notranslate"><span class="pre">BitsInit</span></code> class is a subclass of <code class="docutils literal notranslate"><span class="pre">TypedInit</span></code>. Its instances
represent sequences of bits, from high-order to low-order. It includes a
data member with the length of the sequence and a vector of pointers to
<code class="docutils literal notranslate"><span class="pre">Init</span></code> instances, one per bit.</p>
<p>The class provides the usual <code class="docutils literal notranslate"><span class="pre">get()</span></code> function. It does not provide the
<code class="docutils literal notranslate"><span class="pre">getValue()</span></code> function.</p>
<p>The class provides the following additional functions.</p>
<ul class="simple">
<li><p>A function to get the number of bits in the sequence.</p></li>
<li><p>A function that gets a bit specified by an integer index.</p></li>
</ul>
</div>
<div class="section" id="daginit">
<h4><a class="toc-backref" href="#id15"><span class="sectnum">1.2.5.5</span> <code class="docutils literal notranslate"><span class="pre">DagInit</span></code></a><a class="headerlink" href="#daginit" title="Permalink to this headline">¶</a></h4>
<p>The <code class="docutils literal notranslate"><span class="pre">DagInit</span></code> class is a subclass of <code class="docutils literal notranslate"><span class="pre">TypedInit</span></code>. Its instances
represent the possible direct acyclic graphs (<code class="docutils literal notranslate"><span class="pre">dag</span></code>).</p>
<p>The class includes a pointer to an <code class="docutils literal notranslate"><span class="pre">Init</span></code> for the DAG operator and a
pointer to a <code class="docutils literal notranslate"><span class="pre">StringInit</span></code> for the operator name. It includes the count of
DAG operands and the count of operand names. Finally, it includes a vector of
pointers to <code class="docutils literal notranslate"><span class="pre">Init</span></code> instances for the operands and another to
<code class="docutils literal notranslate"><span class="pre">StringInit</span></code> instances for the operand names.
(The DAG operands are also referred to as <em>arguments</em>.)</p>
<p>The class provides two forms of the usual <code class="docutils literal notranslate"><span class="pre">get()</span></code> function. It does not
provide the usual <code class="docutils literal notranslate"><span class="pre">getValue()</span></code> function.</p>
<p>The class provides many additional functions:</p>
<ul class="simple">
<li><p>Functions to get the operator in various forms and to get the
operator name in various forms.</p></li>
<li><p>Functions to determine whether there are any operands and to get the
number of operands.</p></li>
<li><p>Functions to the get the operands, both individually and together.</p></li>
<li><p>Functions to determine whether there are any names and to
get the number of names</p></li>
<li><p>Functions to the get the names, both individually and together.</p></li>
<li><p>Functions to get the operand iterator <code class="docutils literal notranslate"><span class="pre">begin()</span></code> and <code class="docutils literal notranslate"><span class="pre">end()</span></code> values.</p></li>
<li><p>Functions to get the name iterator <code class="docutils literal notranslate"><span class="pre">begin()</span></code> and <code class="docutils literal notranslate"><span class="pre">end()</span></code> values.</p></li>
</ul>
<p>The class defines two types for the return values of the operand and name
iterators.</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>using const_arg_iterator = SmallVectorImpl<Init*>::const_iterator;
using const_name_iterator = SmallVectorImpl<StringInit*>::const_iterator;
</pre></div>
</div>
</div>
<div class="section" id="definit">
<h4><a class="toc-backref" href="#id16"><span class="sectnum">1.2.5.6</span> <code class="docutils literal notranslate"><span class="pre">DefInit</span></code></a><a class="headerlink" href="#definit" title="Permalink to this headline">¶</a></h4>
<p>The <code class="docutils literal notranslate"><span class="pre">DefInit</span></code> class is a subclass of <code class="docutils literal notranslate"><span class="pre">TypedInit</span></code>. Its instances
represent the records that were collected by TableGen. It includes a data
member that is a pointer to the record’s <code class="docutils literal notranslate"><span class="pre">Record</span></code> instance.</p>
<p>The class provides the usual <code class="docutils literal notranslate"><span class="pre">get()</span></code> function. It does not provide
<code class="docutils literal notranslate"><span class="pre">getValue()</span></code>. Instead, it provides <code class="docutils literal notranslate"><span class="pre">getDef()</span></code>, which returns the
<code class="docutils literal notranslate"><span class="pre">Record</span></code> instance.</p>
</div>
<div class="section" id="intinit">
<h4><a class="toc-backref" href="#id17"><span class="sectnum">1.2.5.7</span> <code class="docutils literal notranslate"><span class="pre">IntInit</span></code></a><a class="headerlink" href="#intinit" title="Permalink to this headline">¶</a></h4>
<p>The <code class="docutils literal notranslate"><span class="pre">IntInit</span></code> class is a subclass of <code class="docutils literal notranslate"><span class="pre">TypedInit</span></code>. Its instances
represent the possible values of a 64-bit integer. It includes a data member
that contains the integer.</p>
<p>The class provides the usual <code class="docutils literal notranslate"><span class="pre">get()</span></code> and <code class="docutils literal notranslate"><span class="pre">getValue()</span></code> functions. The
latter function returns the integer as an <code class="docutils literal notranslate"><span class="pre">int64_t</span></code>.</p>
<p>The class also provides a function, <code class="docutils literal notranslate"><span class="pre">getBit()</span></code>, to obtain a specified bit
of the integer value.</p>
</div>
<div class="section" id="listinit">
<h4><a class="toc-backref" href="#id18"><span class="sectnum">1.2.5.8</span> <code class="docutils literal notranslate"><span class="pre">ListInit</span></code></a><a class="headerlink" href="#listinit" title="Permalink to this headline">¶</a></h4>
<p>The <code class="docutils literal notranslate"><span class="pre">ListInit</span></code> class is a subclass of <code class="docutils literal notranslate"><span class="pre">TypedInit</span></code>. Its instances
represent lists of elements of some type. It includes a data member with the
length of the list and a vector of pointers to <code class="docutils literal notranslate"><span class="pre">Init</span></code> instances, one per
element.</p>
<p>The class provides the usual <code class="docutils literal notranslate"><span class="pre">get()</span></code> and <code class="docutils literal notranslate"><span class="pre">getValues()</span></code> functions. The
latter function returns an <code class="docutils literal notranslate"><span class="pre">ArrayRef</span></code> of the vector of pointers to <code class="docutils literal notranslate"><span class="pre">Init</span></code>
instances.</p>
<p>The class provides these additional functions.</p>
<ul class="simple">
<li><p>A function to get the element type.</p></li>
<li><p>Functions to get the length of the vector and to determine whether
it is empty.</p></li>
<li><p>Functions to get an element specified by an integer index and return
it in various forms.</p></li>
<li><p>Functions to get the iterator <code class="docutils literal notranslate"><span class="pre">begin()</span></code> and <code class="docutils literal notranslate"><span class="pre">end()</span></code> values. The
class defines a type for the return type of these two functions.</p></li>
</ul>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>using const_iterator = Init *const *;
</pre></div>
</div>
</div>
<div class="section" id="stringinit">
<h4><a class="toc-backref" href="#id19"><span class="sectnum">1.2.5.9</span> <code class="docutils literal notranslate"><span class="pre">StringInit</span></code></a><a class="headerlink" href="#stringinit" title="Permalink to this headline">¶</a></h4>
<p>The <code class="docutils literal notranslate"><span class="pre">StringInit</span></code> class is a subclass of <code class="docutils literal notranslate"><span class="pre">TypedInit</span></code>. Its instances
represent arbitrary-length strings. It includes a data member
that contains a <code class="docutils literal notranslate"><span class="pre">StringRef</span></code> of the value.</p>
<p>The class provides the usual <code class="docutils literal notranslate"><span class="pre">get()</span></code> and <code class="docutils literal notranslate"><span class="pre">getValue()</span></code> functions. The
latter function returns the <code class="docutils literal notranslate"><span class="pre">StringRef</span></code>.</p>
</div>
</div>
</div>
<div class="section" id="creating-a-new-backend">
<h2><a class="toc-backref" href="#id20"><span class="sectnum">1.3</span> Creating a New Backend</a><a class="headerlink" href="#creating-a-new-backend" title="Permalink to this headline">¶</a></h2>
<p>The following steps are required to create a new backend for TableGen.</p>
<ol class="arabic simple">
<li><p>Invent a name for your backend C++ file, say <code class="docutils literal notranslate"><span class="pre">GenAddressModes</span></code>.</p></li>
<li><p>Write the new backend, using the file <code class="docutils literal notranslate"><span class="pre">TableGenBackendSkeleton.cpp</span></code>
as a starting point.</p></li>
<li><p>Determine which instance of TableGen requires the new backend. There is
one instance for Clang and another for LLVM. Or you may be building
your own instance.</p></li>
<li><p>Modify the selected <code class="docutils literal notranslate"><span class="pre">tablegen.cpp</span></code> to include your new backend.</p></li>
</ol>
<blockquote>
<div><ol class="loweralpha simple">
<li><p>Add the name to the enumerated type <code class="docutils literal notranslate"><span class="pre">ActionType</span></code>.</p></li>
<li><p>Add a keyword to the <code class="docutils literal notranslate"><span class="pre">ActionType</span></code> command option using the
<code class="docutils literal notranslate"><span class="pre">clEnumValN()</span></code> function.</p></li>
<li><p>Add a case to the <code class="docutils literal notranslate"><span class="pre">switch</span></code> statement in the <em>xxx</em><code class="docutils literal notranslate"><span class="pre">TableGenMain()</span></code>
function. It should invoke the “main function” of your backend, which
in this case, according to convention, is named <code class="docutils literal notranslate"><span class="pre">EmitAddressModes</span></code>.</p></li>
</ol>
</div></blockquote>
<ol class="arabic simple" start="5">
<li><p>Add a declaration of your “main function” to the corresponding
<code class="docutils literal notranslate"><span class="pre">TableGenBackends.h</span></code> header file.</p></li>
<li><p>Add your backend C++ file to the appropriate <code class="docutils literal notranslate"><span class="pre">CMakeLists.txt</span></code> file so
that it will be built.</p></li>
<li><p>Add your C++ file to the system.</p></li>
</ol>
</div>
<div class="section" id="the-backend-skeleton">
<h2><a class="toc-backref" href="#id21"><span class="sectnum">1.4</span> The Backend Skeleton</a><a class="headerlink" href="#the-backend-skeleton" title="Permalink to this headline">¶</a></h2>
<p>The file <code class="docutils literal notranslate"><span class="pre">TableGenBackendSkeleton.cpp</span></code> provides a skeleton C++ translation
unit for writing a new TableGen backend. Here are a few notes on the file.</p>
<ul class="simple">
<li><p>The list of includes is the minimal list required by most backends.</p></li>
<li><p>As with all LLVM C++ files, it has a <code class="docutils literal notranslate"><span class="pre">using</span> <span class="pre">namespace</span> <span class="pre">llvm;</span></code> statement.
It also has an anonymous namespace that contains all the file-specific
data structure definitions, along with the class embodying the emitter
data members and functions. Continuing with the <code class="docutils literal notranslate"><span class="pre">GenAddressModes</span></code> example,
this class is named <code class="docutils literal notranslate"><span class="pre">AddressModesEmitter</span></code>.</p></li>
<li><p>The constructor for the emitter class accepts a <code class="docutils literal notranslate"><span class="pre">RecordKeeper</span></code> reference,
typically named <code class="docutils literal notranslate"><span class="pre">RK</span></code>. The <code class="docutils literal notranslate"><span class="pre">RecordKeeper</span></code> reference is saved in a data
member so that records can be obtained from it. This data member is usually
named <code class="docutils literal notranslate"><span class="pre">Records</span></code>.</p></li>
<li><p>One function is named <code class="docutils literal notranslate"><span class="pre">run</span></code>. It is invoked by the backend’s “main
function” to collect records and emit the output file. It accepts an instance
of the <code class="docutils literal notranslate"><span class="pre">raw_ostream</span></code> class, typically named <code class="docutils literal notranslate"><span class="pre">OS</span></code>. The output file is
emitted by writing to this stream.</p></li>
<li><p>The <code class="docutils literal notranslate"><span class="pre">run</span></code> function should use the <code class="docutils literal notranslate"><span class="pre">emitSourceFileHeader</span></code> helper function
to include a standard header in the emitted file.</p></li>
<li><p>The only function in the <code class="docutils literal notranslate"><span class="pre">llvm</span></code> namespace is the backend “main function.”
In this example, it is named <code class="docutils literal notranslate"><span class="pre">EmitAddressModes</span></code>. It creates an instance
of the <code class="docutils literal notranslate"><span class="pre">AddressModesEmitter</span></code> class, passing the <code class="docutils literal notranslate"><span class="pre">RecordKeeper</span></code>
instance, then invokes the <code class="docutils literal notranslate"><span class="pre">run</span></code> function, passing the <code class="docutils literal notranslate"><span class="pre">raw_ostream</span></code>
instance.</p></li>
</ul>
<p>All the examples in the remainder of this document will assume the naming
conventions used in the skeleton file.</p>
</div>
<div class="section" id="getting-classes">
<h2><a class="toc-backref" href="#id22"><span class="sectnum">1.5</span> Getting Classes</a><a class="headerlink" href="#getting-classes" title="Permalink to this headline">¶</a></h2>
<p>The <code class="docutils literal notranslate"><span class="pre">RecordKeeper</span></code> class provides two functions for getting the
<code class="docutils literal notranslate"><span class="pre">Record</span></code> instances for classes defined in the TableGen files.</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">getClasses()</span></code> returns a <code class="docutils literal notranslate"><span class="pre">RecordMap</span></code> reference for all the classes.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">getClass(</span></code><em>name</em><code class="docutils literal notranslate"><span class="pre">)</span></code> returns a <code class="docutils literal notranslate"><span class="pre">Record</span></code> reference for the named
class.</p></li>
</ul>
<p>If you need to iterate over all the class records:</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>for (auto ClassPair : Records.getClasses()) {
Record *ClassRec = ClassPair.second.get();
...
}
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">ClassPair.second</span></code> gets the class’s <code class="docutils literal notranslate"><span class="pre">unique_ptr</span></code>, then <code class="docutils literal notranslate"><span class="pre">.get()</span></code> gets the
class <code class="docutils literal notranslate"><span class="pre">Record</span></code> itself.</p>
</div>
<div class="section" id="getting-records">
<h2><a class="toc-backref" href="#id23"><span class="sectnum">1.6</span> Getting Records</a><a class="headerlink" href="#getting-records" title="Permalink to this headline">¶</a></h2>
<p>The <code class="docutils literal notranslate"><span class="pre">RecordKeeper</span></code> class provides four functions for getting the
<code class="docutils literal notranslate"><span class="pre">Record</span></code> instances for concrete records defined in the TableGen files.</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">getDefs()</span></code> returns a <code class="docutils literal notranslate"><span class="pre">RecordMap</span></code> reference for all the concrete
records.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">getDef(</span></code><em>name</em><code class="docutils literal notranslate"><span class="pre">)</span></code> returns a <code class="docutils literal notranslate"><span class="pre">Record</span></code> reference for the named
concrete record.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">getAllDerivedDefinitions(</span></code><em>classname</em><code class="docutils literal notranslate"><span class="pre">)</span></code> returns a vector of
<code class="docutils literal notranslate"><span class="pre">Record</span></code> references for the concrete records that derive from the
given class.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">getAllDerivedDefinitions(</span></code><em>classnames</em><code class="docutils literal notranslate"><span class="pre">)</span></code> returns
a vector of <code class="docutils literal notranslate"><span class="pre">Record</span></code> references for the concrete records that derive from
<em>all</em> of the given classes.</p></li>
</ul>
<p>This statement obtains all the records that derive from the <code class="docutils literal notranslate"><span class="pre">Attribute</span></code>
class and iterates over them.</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>auto AttrRecords = Records.getAllDerivedDefinitions("Attribute");
for (Record *AttrRec : AttrRecords) {
...
}
</pre></div>
</div>
</div>
<div class="section" id="getting-record-names-and-fields">
<h2><a class="toc-backref" href="#id24"><span class="sectnum">1.7</span> Getting Record Names and Fields</a><a class="headerlink" href="#getting-record-names-and-fields" title="Permalink to this headline">¶</a></h2>
<p>As described above (see <a class="reference internal" href="#record">Record</a>), there are multiple functions that
return the name of a record. One particularly useful one is
<code class="docutils literal notranslate"><span class="pre">getNameInitAsString()</span></code>, which returns the name as a <code class="docutils literal notranslate"><span class="pre">std::string</span></code>.</p>
<p>There are also multiple functions that return the fields of a record. To
obtain and iterate over all the fields:</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>for (const RecordVal &Field : SomeRec->getValues()) {
...
}
</pre></div>
</div>
<p>You will recall that <code class="docutils literal notranslate"><span class="pre">RecordVal</span></code> is the class whose instances contain
information about the fields in records.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">getValue()</span></code> function returns the <code class="docutils literal notranslate"><span class="pre">RecordVal</span></code> instance for a field
specified by name. There are multiple overloaded functions, some taking a
<code class="docutils literal notranslate"><span class="pre">StringRef</span></code> and others taking a <code class="docutils literal notranslate"><span class="pre">const</span> <span class="pre">Init</span> <span class="pre">*</span></code>. Some functions return a
<code class="docutils literal notranslate"><span class="pre">RecordVal</span> <span class="pre">*</span></code> and others return a <code class="docutils literal notranslate"><span class="pre">const</span> <span class="pre">RecordVal</span> <span class="pre">*</span></code>. If the field does
not exist, a fatal error message is printed.</p>
<p>More often than not, you are interested in the value of the field, not all
the information in the <code class="docutils literal notranslate"><span class="pre">RecordVal</span></code>. There is a large set of functions that
take a field name in some form and return its value. One function,
<code class="docutils literal notranslate"><span class="pre">getValueInit</span></code>, returns the value as an <code class="docutils literal notranslate"><span class="pre">Init</span> <span class="pre">*</span></code>. Another function,
<code class="docutils literal notranslate"><span class="pre">isValueUnset</span></code>, returns a boolean specifying whether the value is unset
(uninitialized).</p>
<p>Most of the functions return the value in some more useful form. For
example:</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>std::vector<int64_t> RegCosts =
SomeRec->getValueAsListOfInts("RegCosts");
</pre></div>
</div>
<p>The field <code class="docutils literal notranslate"><span class="pre">RegCosts</span></code> is assumed to be a list of integers. That list is
returned as a <code class="docutils literal notranslate"><span class="pre">std::vector</span></code> of 64-bit integers. If the field is not a list
of integers, a fatal error message is printed.</p>
<p>Here is a function that returns a field value as a <code class="docutils literal notranslate"><span class="pre">Record</span></code>, but returns
null if the field does not exist.</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>if (Record *BaseRec = SomeRec->getValueAsOptionalDef(BaseFieldName)) {
...
}
</pre></div>
</div>
<p>The field is assumed to have another record as its value. That record is returned
as a pointer to a <code class="docutils literal notranslate"><span class="pre">Record</span></code>. If the field does not exist or is unset, the
functions returns null.</p>
</div>
<div class="section" id="getting-record-superclasses">
<h2><a class="toc-backref" href="#id25"><span class="sectnum">1.8</span> Getting Record Superclasses</a><a class="headerlink" href="#getting-record-superclasses" title="Permalink to this headline">¶</a></h2>
<p>The <code class="docutils literal notranslate"><span class="pre">Record</span></code> class provides a function to obtain the superclasses of a
record. It is named <code class="docutils literal notranslate"><span class="pre">getSuperClasses</span></code> and returns an <code class="docutils literal notranslate"><span class="pre">ArrayRef</span></code> of an
array of <code class="docutils literal notranslate"><span class="pre">std::pair</span></code> pairs. The superclasses are in post-order: the order
in which the superclasses were visited while copying their fields into the
record. Each pair consists of a pointer to the <code class="docutils literal notranslate"><span class="pre">Record</span></code> instance for a
superclass record and an instance of the <code class="docutils literal notranslate"><span class="pre">SMRange</span></code> class. The range
indicates the source file locations of the beginning and end of the class
definition.</p>
<p>This example obtains the superclasses of the <code class="docutils literal notranslate"><span class="pre">Prototype</span></code> record and then
iterates over the pairs in the returned array.</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>ArrayRef<std::pair<Record *, SMRange>>
Superclasses = Prototype->getSuperClasses();
for (const auto &SuperPair : Superclasses) {
...
}
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">Record</span></code> class also provides a function, <code class="docutils literal notranslate"><span class="pre">getDirectSuperClasses</span></code>, to
append the <em>direct</em> superclasses of a record to a given vector of type
<code class="docutils literal notranslate"><span class="pre">SmallVectorImpl<Record</span> <span class="pre">*></span></code>.</p>
</div>
<div class="section" id="emitting-text-to-the-output-stream">
<h2><a class="toc-backref" href="#id26"><span class="sectnum">1.9</span> Emitting Text to the Output Stream</a><a class="headerlink" href="#emitting-text-to-the-output-stream" title="Permalink to this headline">¶</a></h2>
<p>The <code class="docutils literal notranslate"><span class="pre">run</span></code> function is passed a <code class="docutils literal notranslate"><span class="pre">raw_ostream</span></code> to which it prints the
output file. By convention, this stream is saved in the emitter class member
named <code class="docutils literal notranslate"><span class="pre">OS</span></code>, although some <code class="docutils literal notranslate"><span class="pre">run</span></code> functions are simple and just use the
stream without saving it. The output can be produced by writing values
directly to the output stream, or by using the <code class="docutils literal notranslate"><span class="pre">std::format()</span></code> or
<code class="docutils literal notranslate"><span class="pre">llvm::formatv()</span></code> functions.</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>OS << "#ifndef " << NodeName << "\n";
OS << format("0x%0*x, ", Digits, Value);
</pre></div>
</div>
<p>Instances of the following classes can be printed using the <code class="docutils literal notranslate"><span class="pre"><<</span></code> operator:
<code class="docutils literal notranslate"><span class="pre">RecordKeeper</span></code>,
<code class="docutils literal notranslate"><span class="pre">Record</span></code>,
<code class="docutils literal notranslate"><span class="pre">RecTy</span></code>,
<code class="docutils literal notranslate"><span class="pre">RecordVal</span></code>, and
<code class="docutils literal notranslate"><span class="pre">Init</span></code>.</p>
<p>The helper function <code class="docutils literal notranslate"><span class="pre">emitSourceFileHeader()</span></code> prints the header comment
that should be included at the top of every output file. A call to it is
included in the skeleton backend file <code class="docutils literal notranslate"><span class="pre">TableGenBackendSkeleton.cpp</span></code>.</p>
</div>
<div class="section" id="printing-error-messages">
<h2><a class="toc-backref" href="#id27"><span class="sectnum">1.10</span> Printing Error Messages</a><a class="headerlink" href="#printing-error-messages" title="Permalink to this headline">¶</a></h2>
<p>TableGen records are often derived from multiple classes and also often
defined through a sequence of multiclasses. Because of this, it can be
difficult for backends to report clear error messages with accurate source
file locations. To make error reporting easier, five error reporting
functions are provided, each with four overloads.</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">PrintWarning</span></code> prints a message tagged as a warning.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">PrintError</span></code> prints a message tagged as an error.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">PrintFatalError</span></code> prints a message tagged as an error and then terminates.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">PrintNote</span></code> prints a note. It is often used after one of the previous
functions to provide more information.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">PrintFatalNote</span></code> prints a note and then terminates.</p></li>
</ul>
<p>Each of these five functions is overloaded four times.</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">PrintError(const</span> <span class="pre">Twine</span> <span class="pre">&Msg)</span></code>:
Prints the message with no source file location.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">PrintError(ArrayRef<SMLoc></span> <span class="pre">ErrorLoc,</span> <span class="pre">const</span> <span class="pre">Twine</span> <span class="pre">&Msg)</span></code>:
Prints the message followed by the specified source line,
along with a pointer to the item in error. The array of
source file locations is typically taken from a <code class="docutils literal notranslate"><span class="pre">Record</span></code> instance.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">PrintError(const</span> <span class="pre">Record</span> <span class="pre">*Rec,</span> <span class="pre">const</span> <span class="pre">Twine</span> <span class="pre">&Msg)</span></code>:
Prints the message followed by the source line associated with the
specified record (see <a class="reference internal" href="#record">Record</a>).</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">PrintError(const</span> <span class="pre">RecordVal</span> <span class="pre">*RecVal,</span> <span class="pre">const</span> <span class="pre">Twine</span> <span class="pre">&Msg)</span></code>:
Prints the message followed by the source line associated with the
specified record field (see <a class="reference internal" href="#recordval">RecordVal</a>).</p></li>
</ul>
<p>Using these functions, the goal is to produce the most specific error report
possible.</p>
</div>
<div class="section" id="debugging-tools">
<h2><a class="toc-backref" href="#id28"><span class="sectnum">1.11</span> Debugging Tools</a><a class="headerlink" href="#debugging-tools" title="Permalink to this headline">¶</a></h2>
<p>TableGen provides some tools to aid in debugging backends.</p>
<div class="section" id="the-printrecords-backend">
<h3><a class="toc-backref" href="#id29"><span class="sectnum">1.11.1</span> The <code class="docutils literal notranslate"><span class="pre">PrintRecords</span></code> Backend</a><a class="headerlink" href="#the-printrecords-backend" title="Permalink to this headline">¶</a></h3>
<p>The TableGen command option <code class="docutils literal notranslate"><span class="pre">--print-records</span></code> invokes a simple backend
that prints all the classes and records defined in the source files. This is
the default backend option. The format of the output is guaranteed to be
constant over time, so that the output can be compared in tests. The output
looks like this:</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>------------- Classes -----------------
...
class XEntry<string XEntry:str = ?, int XEntry:val1 = ?> { // XBase
string Str = XEntry:str;
bits<8> Val1 = { !cast<bits<8>>(XEntry:val1){7}, ... };
bit Val3 = 1;
}
...
------------- Defs -----------------
def ATable { // GenericTable
string FilterClass = "AEntry";
string CppTypeName = "AEntry";
list<string> Fields = ["Str", "Val1", "Val2"];
list<string> PrimaryKey = ["Val1", "Val2"];
string PrimaryKeyName = "lookupATableByValues";
bit PrimaryKeyEarlyOut = 0;
}
...
def anonymous_0 { // AEntry
string Str = "Bob";
bits<8> Val1 = { 0, 0, 0, 0, 0, 1, 0, 1 };
bits<10> Val2 = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1 };
}
</pre></div>
</div>
<p>Classes are shown with their template arguments, parent classes (following
<code class="docutils literal notranslate"><span class="pre">//</span></code>), and fields. Records are shown with their parent classes and
fields. Note that anonymous records are named <code class="docutils literal notranslate"><span class="pre">anonymous_0</span></code>,
<code class="docutils literal notranslate"><span class="pre">anonymous_1</span></code>, etc.</p>
</div>
<div class="section" id="the-printdetailedrecords-backend">
<h3><a class="toc-backref" href="#id30"><span class="sectnum">1.11.2</span> The <code class="docutils literal notranslate"><span class="pre">PrintDetailedRecords</span></code> Backend</a><a class="headerlink" href="#the-printdetailedrecords-backend" title="Permalink to this headline">¶</a></h3>
<p>The TableGen command option <code class="docutils literal notranslate"><span class="pre">--print-detailed-records</span></code> invokes a backend
that prints all the global variables, classes, and records defined in the
source files. The format of the output is <em>not</em> guaranteed to be constant
over time. The output looks like this.</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>DETAILED RECORDS for file llvm-project\llvm\lib\target\arc\arc.td
-------------------- Global Variables (5) --------------------
AMDGPUBufferIntrinsics = [int_amdgcn_buffer_load_format, ...
AMDGPUImageDimAtomicIntrinsics = [int_amdgcn_image_atomic_swap_1d, ...
...
-------------------- Classes (758) --------------------
AMDGPUBufferLoad |IntrinsicsAMDGPU.td:879|
Template args:
LLVMType AMDGPUBufferLoad:data_ty = llvm_any_ty |IntrinsicsAMDGPU.td:879|
Superclasses: (SDPatternOperator) Intrinsic AMDGPURsrcIntrinsic
Fields:
list<SDNodeProperty> Properties = [SDNPMemOperand] |Intrinsics.td:348|
string LLVMName = "" |Intrinsics.td:343|
...
-------------------- Records (12303) --------------------
AMDGPUSample_lz_o |IntrinsicsAMDGPU.td:560|
Defm sequence: |IntrinsicsAMDGPU.td:584| |IntrinsicsAMDGPU.td:566|
Superclasses: AMDGPUSampleVariant
Fields:
string UpperCaseMod = "_LZ_O" |IntrinsicsAMDGPU.td:542|
string LowerCaseMod = "_lz_o" |IntrinsicsAMDGPU.td:543|
...
</pre></div>
</div>
<ul class="simple">
<li><p>Global variables defined with outer <code class="docutils literal notranslate"><span class="pre">defvar</span></code> statements are shown with
their values.</p></li>
<li><p>The classes are shown with their source location, template arguments,
superclasses, and fields.</p></li>
<li><p>The records are shown with their source location, <code class="docutils literal notranslate"><span class="pre">defm</span></code> sequence,
superclasses, and fields.</p></li>
</ul>
<p>Superclasses are shown in the order processed, with indirect superclasses in
parentheses. Each field is shown with its value and the source location at
which it was set.
The <code class="docutils literal notranslate"><span class="pre">defm</span></code> sequence gives the locations of the <code class="docutils literal notranslate"><span class="pre">defm</span></code> statements that
were involved in generating the record, in the order they were invoked.</p>
</div>
<div class="section" id="timing-tablegen-phases">
<h3><a class="toc-backref" href="#id31"><span class="sectnum">1.11.3</span> Timing TableGen Phases</a><a class="headerlink" href="#timing-tablegen-phases" title="Permalink to this headline">¶</a></h3>
<p>TableGen provides a phase timing feature that produces a report of the time
used by the various phases of parsing the source files and running the
selected backend. This feature is enabled with the <code class="docutils literal notranslate"><span class="pre">--time-phases</span></code> option
of the TableGen command.</p>
<p>If the backend is <em>not</em> instrumented for timing, then a report such as the
following is produced. This is the timing for the
<code class="docutils literal notranslate"><span class="pre">--print-detailed-records</span></code> backend run on the AMDGPU target.</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>===-------------------------------------------------------------------------===
TableGen Phase Timing
===-------------------------------------------------------------------------===
Total Execution Time: 101.0106 seconds (102.4819 wall clock)
---User Time--- --System Time-- --User+System-- ---Wall Time--- --- Name ---
85.5197 ( 84.9%) 0.1560 ( 50.0%) 85.6757 ( 84.8%) 85.7009 ( 83.6%) Backend overall
15.1789 ( 15.1%) 0.0000 ( 0.0%) 15.1789 ( 15.0%) 15.1829 ( 14.8%) Parse, build records
0.0000 ( 0.0%) 0.1560 ( 50.0%) 0.1560 ( 0.2%) 1.5981 ( 1.6%) Write output
100.6986 (100.0%) 0.3120 (100.0%) 101.0106 (100.0%) 102.4819 (100.0%) Total
</pre></div>
</div>
<p>Note that all the time for the backend is lumped under “Backend overall”.</p>
<p>If the backend is instrumented for timing, then its processing is
divided into phases and each one timed separately. This is the timing for
the <code class="docutils literal notranslate"><span class="pre">--emit-dag-isel</span></code> backend run on the AMDGPU target.</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>===-------------------------------------------------------------------------===
TableGen Phase Timing
===-------------------------------------------------------------------------===
Total Execution Time: 746.3868 seconds (747.1447 wall clock)
---User Time--- --System Time-- --User+System-- ---Wall Time--- --- Name ---
657.7938 ( 88.1%) 0.1404 ( 90.0%) 657.9342 ( 88.1%) 658.6497 ( 88.2%) Emit matcher table
70.2317 ( 9.4%) 0.0000 ( 0.0%) 70.2317 ( 9.4%) 70.2700 ( 9.4%) Convert to matchers
14.8825 ( 2.0%) 0.0156 ( 10.0%) 14.8981 ( 2.0%) 14.9009 ( 2.0%) Parse, build records
2.1840 ( 0.3%) 0.0000 ( 0.0%) 2.1840 ( 0.3%) 2.1791 ( 0.3%) Sort patterns
1.1388 ( 0.2%) 0.0000 ( 0.0%) 1.1388 ( 0.2%) 1.1401 ( 0.2%) Optimize matchers
0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0050 ( 0.0%) Write output
746.2308 (100.0%) 0.1560 (100.0%) 746.3868 (100.0%) 747.1447 (100.0%) Total
</pre></div>
</div>
<p>The backend has been divided into four phases and timed separately.</p>
<p>If you want to instrument a backend, refer to the backend <code class="docutils literal notranslate"><span class="pre">DAGISelEmitter.cpp</span></code>
and search for <code class="docutils literal notranslate"><span class="pre">Records.startTimer</span></code>.</p>
</div>
</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="ProgRef.html" title="1 TableGen Programmer’s Reference"
>next</a> |</li>
<li class="right" >
<a href="BackEnds.html" title="TableGen BackEnds"
>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 Backend Developer’s Guide</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>
|