1=============================== 2TableGen Programmer's Reference 3=============================== 4 5.. sectnum:: 6 7.. contents:: 8 :local: 9 10Introduction 11============ 12 13The purpose of TableGen is to generate complex output files based on 14information from source files that are significantly easier to code than the 15output files would be, and also easier to maintain and modify over time. The 16information is coded in a declarative style involving classes and records, 17which are then processed by TableGen. The internalized records are passed on 18to various *backends*, which extract information from a subset of the records 19and generate one or more output files. These output files are typically 20``.inc`` files for C++, but may be any type of file that the backend 21developer needs. 22 23This document describes the LLVM TableGen facility in detail. It is intended 24for the programmer who is using TableGen to produce code for a project. If 25you are looking for a simple overview, check out the :doc:`TableGen Overview 26<./index>`. The various ``*-tblgen`` commands used to invoke TableGen are 27described in :doc:`tblgen Family - Description to C++ 28Code<../CommandGuide/tblgen>`. 29 30An example of a backend is ``RegisterInfo``, which generates the register 31file information for a particular target machine, for use by the LLVM 32target-independent code generator. See :doc:`TableGen Backends <./BackEnds>` 33for a description of the LLVM TableGen backends, and :doc:`TableGen 34Backend Developer's Guide <./BackGuide>` for a guide to writing a new 35backend. 36 37Here are a few of the things backends can do. 38 39* Generate the register file information for a particular target machine. 40 41* Generate the instruction definitions for a target. 42 43* Generate the patterns that the code generator uses to match instructions 44 to intermediate representation (IR) nodes. 45 46* Generate semantic attribute identifiers for Clang. 47 48* Generate abstract syntax tree (AST) declaration node definitions for Clang. 49 50* Generate AST statement node definitions for Clang. 51 52 53Concepts 54-------- 55 56TableGen source files contain two primary items: *abstract records* and 57*concrete records*. In this and other TableGen documents, abstract records 58are called *classes.* (These classes are different from C++ classes and do 59not map onto them.) In addition, concrete records are usually just called 60records, although sometimes the term *record* refers to both classes and 61concrete records. The distinction should be clear in context. 62 63Classes and concrete records have a unique *name*, either chosen by 64the programmer or generated by TableGen. Associated with that name 65is a list of *fields* with values and an optional list of *parent classes* 66(sometimes called base or super classes). The fields are the primary data that 67backends will process. Note that TableGen assigns no meanings to fields; the 68meanings are entirely up to the backends and the programs that incorporate 69the output of those backends. 70 71.. note:: 72 73 The term "parent class" can refer to a class that is a parent of another 74 class, and also to a class from which a concrete record inherits. This 75 nonstandard use of the term arises because TableGen treats classes and 76 concrete records similarly. 77 78A backend processes some subset of the concrete records built by the 79TableGen parser and emits the output files. These files are usually C++ 80``.inc`` files that are included by the programs that require the data in 81those records. However, a backend can produce any type of output files. For 82example, it could produce a data file containing messages tagged with 83identifiers and substitution parameters. In a complex use case such as the 84LLVM code generator, there can be many concrete records and some of them can 85have an unexpectedly large number of fields, resulting in large output files. 86 87In order to reduce the complexity of TableGen files, classes are used to 88abstract out groups of record fields. For example, a few classes may 89abstract the concept of a machine register file, while other classes may 90abstract the instruction formats, and still others may abstract the 91individual instructions. TableGen allows an arbitrary hierarchy of classes, 92so that the abstract classes for two concepts can share a third superclass that 93abstracts common "sub-concepts" from the two original concepts. 94 95In order to make classes more useful, a concrete record (or another class) 96can request a class as a parent class and pass *template arguments* to it. 97These template arguments can be used in the fields of the parent class to 98initialize them in a custom manner. That is, record or class ``A`` can 99request parent class ``S`` with one set of template arguments, while record or class 100``B`` can request ``S`` with a different set of arguments. Without template 101arguments, many more classes would be required, one for each combination of 102the template arguments. 103 104Both classes and concrete records can include fields that are uninitialized. 105The uninitialized "value" is represented by a question mark (``?``). Classes 106often have uninitialized fields that are expected to be filled in when those 107classes are inherited by concrete records. Even so, some fields of concrete 108records may remain uninitialized. 109 110TableGen provides *multiclasses* to collect a group of record definitions in 111one place. A multiclass is a sort of macro that can be "invoked" to define 112multiple concrete records all at once. A multiclass can inherit from other 113multiclasses, which means that the multiclass inherits all the definitions 114from its parent multiclasses. 115 116`Appendix C: Sample Record`_ illustrates a complex record in the Intel X86 117target and the simple way in which it is defined. 118 119Source Files 120============ 121 122TableGen source files are plain ASCII text files. The files can contain 123statements, comments, and blank lines (see `Lexical Analysis`_). The standard file 124extension for TableGen files is ``.td``. 125 126TableGen files can grow quite large, so there is an include mechanism that 127allows one file to include the content of another file (see `Include 128Files`_). This allows large files to be broken up into smaller ones, and 129also provides a simple library mechanism where multiple source files can 130include the same library file. 131 132TableGen supports a simple preprocessor that can be used to conditionalize 133portions of ``.td`` files. See `Preprocessing Facilities`_ for more 134information. 135 136Lexical Analysis 137================ 138 139The lexical and syntax notation used here is intended to imitate 140`Python's`_ notation. In particular, for lexical definitions, the productions 141operate at the character level and there is no implied whitespace between 142elements. The syntax definitions operate at the token level, so there is 143implied whitespace between tokens. 144 145.. _`Python's`: http://docs.python.org/py3k/reference/introduction.html#notation 146 147TableGen supports BCPL-style comments (``// ...``) and nestable C-style 148comments (``/* ... */``). 149TableGen also provides simple `Preprocessing Facilities`_. 150 151Formfeed characters may be used freely in files to produce page breaks when 152the file is printed for review. 153 154The following are the basic punctuation tokens:: 155 156 - + [ ] { } ( ) < > : ; . ... = ? # 157 158Literals 159-------- 160 161Numeric literals take one of the following forms: 162 163.. productionlist:: 164 TokInteger: `DecimalInteger` | `HexInteger` | `BinInteger` 165 DecimalInteger: ["+" | "-"] ("0"..."9")+ 166 HexInteger: "0x" ("0"..."9" | "a"..."f" | "A"..."F")+ 167 BinInteger: "0b" ("0" | "1")+ 168 169Observe that the :token:`DecimalInteger` token includes the optional ``+`` 170or ``-`` sign, unlike most languages where the sign would be treated as a 171unary operator. 172 173TableGen has two kinds of string literals: 174 175.. productionlist:: 176 TokString: '"' (non-'"' characters and escapes) '"' 177 TokCode: "[{" (shortest text not containing "}]") "}]" 178 179A :token:`TokCode` is nothing more than a multi-line string literal 180delimited by ``[{`` and ``}]``. It can break across lines and the 181line breaks are retained in the string. 182 183The current implementation accepts the following escape sequences:: 184 185 \\ \' \" \t \n 186 187Identifiers 188----------- 189 190TableGen has name- and identifier-like tokens, which are case-sensitive. 191 192.. productionlist:: 193 ualpha: "a"..."z" | "A"..."Z" | "_" 194 TokIdentifier: ("0"..."9")* `ualpha` (`ualpha` | "0"..."9")* 195 TokVarName: "$" `ualpha` (`ualpha` | "0"..."9")* 196 197Note that, unlike most languages, TableGen allows :token:`TokIdentifier` to 198begin with an integer. In case of ambiguity, a token is interpreted as a 199numeric literal rather than an identifier. 200 201TableGen has the following reserved keywords, which cannot be used as 202identifiers:: 203 204 assert bit bits class code 205 dag def else false foreach 206 defm defset defvar field if 207 in include int let list 208 multiclass string then true 209 210.. warning:: 211 The ``field`` reserved word is deprecated. 212 213Bang operators 214-------------- 215 216TableGen provides "bang operators" that have a wide variety of uses: 217 218.. productionlist:: 219 BangOperator: one of 220 : !add !and !cast !con !dag 221 : !empty !eq !foldl !foreach !filter 222 : !ge !getdagop !gt !head !if 223 : !interleave !isa !le !listconcat !listsplat 224 : !lt !mul !ne !not !or 225 : !setdagop !shl !size !sra !srl 226 : !strconcat !sub !subst !substr !tail 227 : !xor 228 229The ``!cond`` operator has a slightly different 230syntax compared to other bang operators, so it is defined separately: 231 232.. productionlist:: 233 CondOperator: !cond 234 235See `Appendix A: Bang Operators`_ for a description of each bang operator. 236 237Include files 238------------- 239 240TableGen has an include mechanism. The content of the included file 241lexically replaces the ``include`` directive and is then parsed as if it was 242originally in the main file. 243 244.. productionlist:: 245 IncludeDirective: "include" `TokString` 246 247Portions of the main file and included files can be conditionalized using 248preprocessor directives. 249 250.. productionlist:: 251 PreprocessorDirective: "#define" | "#ifdef" | "#ifndef" 252 253Types 254===== 255 256The TableGen language is statically typed, using a simple but complete type 257system. Types are used to check for errors, to perform implicit conversions, 258and to help interface designers constrain the allowed input. Every value is 259required to have an associated type. 260 261TableGen supports a mixture of low-level types (e.g., ``bit``) and 262high-level types (e.g., ``dag``). This flexibility allows you to describe a 263wide range of records conveniently and compactly. 264 265.. productionlist:: 266 Type: "bit" | "int" | "string" | "dag" 267 :| "bits" "<" `TokInteger` ">" 268 :| "list" "<" `Type` ">" 269 :| `ClassID` 270 ClassID: `TokIdentifier` 271 272``bit`` 273 A ``bit`` is a boolean value that can be 0 or 1. 274 275``int`` 276 The ``int`` type represents a simple 64-bit integer value, such as 5 or 277 -42. 278 279``string`` 280 The ``string`` type represents an ordered sequence of characters of arbitrary 281 length. 282 283``bits<``\ *n*\ ``>`` 284 The ``bits`` type is a fixed-sized integer of arbitrary length *n* that 285 is treated as separate bits. These bits can be accessed individually. 286 A field of this type is useful for representing an instruction operation 287 code, register number, or address mode/register/displacement. The bits of 288 the field can be set individually or as subfields. For example, in an 289 instruction address, the addressing mode, base register number, and 290 displacement can be set separately. 291 292``list<``\ *type*\ ``>`` 293 This type represents a list whose elements are of the *type* specified in 294 angle brackets. The element type is arbitrary; it can even be another 295 list type. List elements are indexed from 0. 296 297``dag`` 298 This type represents a nestable directed acyclic graph (DAG) of nodes. 299 Each node has an *operator* and zero or more *arguments* (or *operands*). 300 An argument can be 301 another ``dag`` object, allowing an arbitrary tree of nodes and edges. 302 As an example, DAGs are used to represent code patterns for use by 303 the code generator instruction selection algorithms. See `Directed 304 acyclic graphs (DAGs)`_ for more details; 305 306:token:`ClassID` 307 Specifying a class name in a type context indicates 308 that the type of the defined value must 309 be a subclass of the specified class. This is useful in conjunction with 310 the ``list`` type; for example, to constrain the elements of the list to a 311 common base class (e.g., a ``list<Register>`` can only contain definitions 312 derived from the ``Register`` class). 313 The :token:`ClassID` must name a class that has been previously 314 declared or defined. 315 316 317Values and Expressions 318====================== 319 320There are many contexts in TableGen statements where a value is required. A 321common example is in the definition of a record, where each field is 322specified by a name and an optional value. TableGen allows for a reasonable 323number of different forms when building up value expressions. These forms 324allow the TableGen file to be written in a syntax that is natural for the 325application. 326 327Note that all of the values have rules for converting them from one type to 328another. For example, these rules allow you to assign a value like ``7`` 329to an entity of type ``bits<4>``. 330 331.. productionlist:: 332 Value: `SimpleValue` `ValueSuffix`* 333 :| `Value` "#" `Value` 334 ValueSuffix: "{" `RangeList` "}" 335 :| "[" `RangeList` "]" 336 :| "." `TokIdentifier` 337 RangeList: `RangePiece` ("," `RangePiece`)* 338 RangePiece: `TokInteger` 339 :| `TokInteger` "..." `TokInteger` 340 :| `TokInteger` "-" `TokInteger` 341 :| `TokInteger` `TokInteger` 342 343.. warning:: 344 The peculiar last form of :token:`RangePiece` is due to the fact that the 345 "``-``" is included in the :token:`TokInteger`, hence ``1-5`` gets lexed as 346 two consecutive tokens, with values ``1`` and ``-5``, instead of "1", "-", 347 and "5". The use of hyphen as the range punctuation is deprecated. 348 349Simple values 350------------- 351 352The :token:`SimpleValue` has a number of forms. 353 354.. productionlist:: 355 SimpleValue: `TokInteger` | `TokString`+ | `TokCode` 356 357A value can be an integer literal, a string literal, or a code literal. 358Multiple adjacent string literals are concatenated as in C/C++; the simple 359value is the concatenation of the strings. Code literals become strings and 360are then indistinguishable from them. 361 362.. productionlist:: 363 SimpleValue2: "true" | "false" 364 365The ``true`` and ``false`` literals are essentially syntactic sugar for the 366integer values 1 and 0. They improve the readability of TableGen files when 367boolean values are used in field initializations, bit sequences, ``if`` 368statements, etc. When parsed, these literals are converted to integers. 369 370.. note:: 371 372 Although ``true`` and ``false`` are literal names for 1 and 0, we 373 recommend as a stylistic rule that you use them for boolean 374 values only. 375 376.. productionlist:: 377 SimpleValue3: "?" 378 379A question mark represents an uninitialized value. 380 381.. productionlist:: 382 SimpleValue4: "{" [`ValueList`] "}" 383 ValueList: `ValueListNE` 384 ValueListNE: `Value` ("," `Value`)* 385 386This value represents a sequence of bits, which can be used to initialize a 387``bits<``\ *n*\ ``>`` field (note the braces). When doing so, the values 388must represent a total of *n* bits. 389 390.. productionlist:: 391 SimpleValue5: "[" `ValueList` "]" ["<" `Type` ">"] 392 393This value is a list initializer (note the brackets). The values in brackets 394are the elements of the list. The optional :token:`Type` can be used to 395indicate a specific element type; otherwise the element type is inferred 396from the given values. TableGen can usually infer the type, although 397sometimes not when the value is the empty list (``[]``). 398 399.. productionlist:: 400 SimpleValue6: "(" `DagArg` [`DagArgList`] ")" 401 DagArgList: `DagArg` ("," `DagArg`)* 402 DagArg: `Value` [":" `TokVarName`] | `TokVarName` 403 404This represents a DAG initializer (note the parentheses). The first 405:token:`DagArg` is called the "operator" of the DAG and must be a record. 406See `Directed acyclic graphs (DAGs)`_ for more details. 407 408.. productionlist:: 409 SimpleValue7: `TokIdentifier` 410 411The resulting value is the value of the entity named by the identifier. The 412possible identifiers are described here, but the descriptions will make more 413sense after reading the remainder of this guide. 414 415.. The code for this is exceptionally abstruse. These examples are a 416 best-effort attempt. 417 418* A template argument of a ``class``, such as the use of ``Bar`` in:: 419 420 class Foo <int Bar> { 421 int Baz = Bar; 422 } 423 424* The implicit template argument ``NAME`` in a ``class`` or ``multiclass`` 425 definition (see `NAME`_). 426 427* A field local to a ``class``, such as the use of ``Bar`` in:: 428 429 class Foo { 430 int Bar = 5; 431 int Baz = Bar; 432 } 433 434* The name of a record definition, such as the use of ``Bar`` in the 435 definition of ``Foo``:: 436 437 def Bar : SomeClass { 438 int X = 5; 439 } 440 441 def Foo { 442 SomeClass Baz = Bar; 443 } 444 445* A field local to a record definition, such as the use of ``Bar`` in:: 446 447 def Foo { 448 int Bar = 5; 449 int Baz = Bar; 450 } 451 452 Fields inherited from the record's parent classes can be accessed the same way. 453 454* A template argument of a ``multiclass``, such as the use of ``Bar`` in:: 455 456 multiclass Foo <int Bar> { 457 def : SomeClass<Bar>; 458 } 459 460* A variable defined with the ``defvar`` or ``defset`` statements. 461 462* The iteration variable of a ``foreach``, such as the use of ``i`` in:: 463 464 foreach i = 0...5 in 465 def Foo#i; 466 467.. productionlist:: 468 SimpleValue8: `ClassID` "<" `ValueListNE` ">" 469 470This form creates a new anonymous record definition (as would be created by an 471unnamed ``def`` inheriting from the given class with the given template 472arguments; see `def`_) and the value is that record. A field of the record can be 473obtained using a suffix; see `Suffixed Values`_. 474 475Invoking a class in this manner can provide a simple subroutine facility. 476See `Using Classes as Subroutines`_ for more information. 477 478.. productionlist:: 479 SimpleValue9: `BangOperator` ["<" `Type` ">"] "(" `ValueListNE` ")" 480 :| `CondOperator` "(" `CondClause` ("," `CondClause`)* ")" 481 CondClause: `Value` ":" `Value` 482 483The bang operators provide functions that are not available with the other 484simple values. Except in the case of ``!cond``, a bang operator takes a list 485of arguments enclosed in parentheses and performs some function on those 486arguments, producing a value for that bang operator. The ``!cond`` operator 487takes a list of pairs of arguments separated by colons. See `Appendix A: 488Bang Operators`_ for a description of each bang operator. 489 490 491Suffixed values 492--------------- 493 494The :token:`SimpleValue` values described above can be specified with 495certain suffixes. The purpose of a suffix is to obtain a subvalue of the 496primary value. Here are the possible suffixes for some primary *value*. 497 498*value*\ ``{17}`` 499 The final value is bit 17 of the integer *value* (note the braces). 500 501*value*\ ``{8...15}`` 502 The final value is bits 8--15 of the integer *value*. The order of the 503 bits can be reversed by specifying ``{15...8}``. 504 505*value*\ ``[4]`` 506 The final value is element 4 of the list *value* (note the brackets). 507 In other words, the brackets act as a subscripting operator on the list. 508 This is the case only when a single element is specified. 509 510*value*\ ``[4...7,17,2...3,4]`` 511 The final value is a new list that is a slice of the list *value*. 512 The new list contains elements 4, 5, 6, 7, 17, 2, 3, and 4. 513 Elements may be included multiple times and in any order. This is the result 514 only when more than one element is specified. 515 516*value*\ ``.``\ *field* 517 The final value is the value of the specified *field* in the specified 518 record *value*. 519 520The paste operator 521------------------ 522 523The paste operator (``#``) is the only infix operator available in TableGen 524expressions. It allows you to concatenate strings or lists, but has a few 525unusual features. 526 527The paste operator can be used when specifying the record name in a 528:token:`Def` or :token:`Defm` statement, in which case it must construct a 529string. If an operand is an undefined name (:token:`TokIdentifier`) or the 530name of a global :token:`Defvar` or :token:`Defset`, it is treated as a 531verbatim string of characters. The value of a global name is not used. 532 533The paste operator can be used in all other value expressions, in which case 534it can construct a string or a list. Rather oddly, but consistent with the 535previous case, if the *right-hand-side* operand is an undefined name or a 536global name, it is treated as a verbatim string of characters. The 537left-hand-side operand is treated normally. 538 539`Appendix B: Paste Operator Examples`_ presents examples of the behavior of 540the paste operator. 541 542Statements 543========== 544 545The following statements may appear at the top level of TableGen source 546files. 547 548.. productionlist:: 549 TableGenFile: `Statement`* 550 Statement: `Assert` | `Class` | `Def` | `Defm` | `Defset` | `Defvar` 551 :| `Foreach` | `If` | `Let` | `MultiClass` 552 553The following sections describe each of these top-level statements. 554 555 556``class`` --- define an abstract record class 557--------------------------------------------- 558 559A ``class`` statement defines an abstract record class from which other 560classes and records can inherit. 561 562.. productionlist:: 563 Class: "class" `ClassID` [`TemplateArgList`] `RecordBody` 564 TemplateArgList: "<" `TemplateArgDecl` ("," `TemplateArgDecl`)* ">" 565 TemplateArgDecl: `Type` `TokIdentifier` ["=" `Value`] 566 567A class can be parameterized by a list of "template arguments," whose values 568can be used in the class's record body. These template arguments are 569specified each time the class is inherited by another class or record. 570 571If a template argument is not assigned a default value with ``=``, it is 572uninitialized (has the "value" ``?``) and must be specified in the template 573argument list when the class is inherited (required argument). If an 574argument is assigned a default value, then it need not be specified in the 575argument list (optional argument). In the declaration, all required template 576arguments must precede any optional arguments. The template argument default 577values are evaluated from left to right. 578 579The :token:`RecordBody` is defined below. It can include a list of 580parent classes from which the current class inherits, along with field 581definitions and other statements. When a class ``C`` inherits from another 582class ``D``, the fields of ``D`` are effectively merged into the fields of 583``C``. 584 585A given class can only be defined once. A ``class`` statement is 586considered to define the class if *any* of the following are true (the 587:token:`RecordBody` elements are described below). 588 589* The :token:`TemplateArgList` is present, or 590* The :token:`ParentClassList` in the :token:`RecordBody` is present, or 591* The :token:`Body` in the :token:`RecordBody` is present and not empty. 592 593You can declare an empty class by specifying an empty :token:`TemplateArgList` 594and an empty :token:`RecordBody`. This can serve as a restricted form of 595forward declaration. Note that records derived from a forward-declared 596class will inherit no fields from it, because those records are built when 597their declarations are parsed, and thus before the class is finally defined. 598 599.. _NAME: 600 601Every class has an implicit template argument named ``NAME`` (uppercase), 602which is bound to the name of the :token:`Def` or :token:`Defm` inheriting 603from the class. If the class is inherited by an anonymous record, the name 604is unspecified but globally unique. 605 606See `Examples: classes and records`_ for examples. 607 608Record Bodies 609````````````` 610 611Record bodies appear in both class and record definitions. A record body can 612include a parent class list, which specifies the classes from which the 613current class or record inherits fields. Such classes are called the 614parent classes of the class or record. The record body also 615includes the main body of the definition, which contains the specification 616of the fields of the class or record. 617 618.. productionlist:: 619 RecordBody: `ParentClassList` `Body` 620 ParentClassList: [":" `ParentClassListNE`] 621 ParentClassListNE: `ClassRef` ("," `ClassRef`)* 622 ClassRef: (`ClassID` | `MultiClassID`) ["<" [`ValueList`] ">"] 623 624A :token:`ParentClassList` containing a :token:`MultiClassID` is valid only 625in the class list of a ``defm`` statement. In that case, the ID must be the 626name of a multiclass. 627 628.. productionlist:: 629 Body: ";" | "{" `BodyItem`* "}" 630 BodyItem: (`Type` | "code") `TokIdentifier` ["=" `Value`] ";" 631 :| "let" `TokIdentifier` ["{" `RangeList` "}"] "=" `Value` ";" 632 :| "defvar" `TokIdentifier` "=" `Value` ";" 633 :| `Assert` 634 635A field definition in the body specifies a field to be included in the class 636or record. If no initial value is specified, then the field's value is 637uninitialized. The type must be specified; TableGen will not infer it from 638the value. The keyword ``code`` may be used to emphasize that the field 639has a string value that is code. 640 641The ``let`` form is used to reset a field to a new value. This can be done 642for fields defined directly in the body or fields inherited from parent 643classes. A :token:`RangeList` can be specified to reset certain bits in a 644``bit<n>`` field. 645 646The ``defvar`` form defines a variable whose value can be used in other 647value expressions within the body. The variable is not a field: it does not 648become a field of the class or record being defined. Variables are provided 649to hold temporary values while processing the body. See `Defvar in a Record 650Body`_ for more details. 651 652When class ``C2`` inherits from class ``C1``, it acquires all the field 653definitions of ``C1``. As those definitions are merged into class ``C2``, any 654template arguments passed to ``C1`` by ``C2`` are substituted into the 655definitions. In other words, the abstract record fields defined by ``C1`` are 656expanded with the template arguments before being merged into ``C2``. 657 658 659.. _def: 660 661``def`` --- define a concrete record 662------------------------------------ 663 664A ``def`` statement defines a new concrete record. 665 666.. productionlist:: 667 Def: "def" [`NameValue`] `RecordBody` 668 NameValue: `Value` (parsed in a special mode) 669 670The name value is optional. If specified, it is parsed in a special mode 671where undefined (unrecognized) identifiers are interpreted as literal 672strings. In particular, global identifiers are considered unrecognized. 673These include global variables defined by ``defvar`` and ``defset``. A 674record name can be the null string. 675 676If no name value is given, the record is *anonymous*. The final name of an 677anonymous record is unspecified but globally unique. 678 679Special handling occurs if a ``def`` appears inside a ``multiclass`` 680statement. See the ``multiclass`` section below for details. 681 682A record can inherit from one or more classes by specifying the 683:token:`ParentClassList` clause at the beginning of its record body. All of 684the fields in the parent classes are added to the record. If two or more 685parent classes provide the same field, the record ends up with the field value 686of the last parent class. 687 688As a special case, the name of a record can be passed as a template argument 689to that record's parent classes. For example: 690 691.. code-block:: text 692 693 class A <dag d> { 694 dag the_dag = d; 695 } 696 697 def rec1 : A<(ops rec1)> 698 699The DAG ``(ops rec1)`` is passed as a template argument to class ``A``. Notice 700that the DAG includes ``rec1``, the record being defined. 701 702The steps taken to create a new record are somewhat complex. See `How 703records are built`_. 704 705See `Examples: classes and records`_ for examples. 706 707 708Examples: classes and records 709----------------------------- 710 711Here is a simple TableGen file with one class and two record definitions. 712 713.. code-block:: text 714 715 class C { 716 bit V = true; 717 } 718 719 def X : C; 720 def Y : C { 721 let V = false; 722 string Greeting = "Hello!"; 723 } 724 725First, the abstract class ``C`` is defined. It has one field named ``V`` 726that is a bit initialized to true. 727 728Next, two records are defined, derived from class ``C``; that is, with ``C`` 729as their parent class. Thus they both inherit the ``V`` field. Record ``Y`` 730also defines another string field, ``Greeting``, which is initialized to 731``"Hello!"``. In addition, ``Y`` overrides the inherited ``V`` field, 732setting it to false. 733 734A class is useful for isolating the common features of multiple records in 735one place. A class can initialize common fields to default values, but 736records inheriting from that class can override the defaults. 737 738TableGen supports the definition of parameterized classes as well as 739nonparameterized ones. Parameterized classes specify a list of variable 740declarations, which may optionally have defaults, that are bound when the 741class is specified as a parent class of another class or record. 742 743.. code-block:: text 744 745 class FPFormat <bits<3> val> { 746 bits<3> Value = val; 747 } 748 749 def NotFP : FPFormat<0>; 750 def ZeroArgFP : FPFormat<1>; 751 def OneArgFP : FPFormat<2>; 752 def OneArgFPRW : FPFormat<3>; 753 def TwoArgFP : FPFormat<4>; 754 def CompareFP : FPFormat<5>; 755 def CondMovFP : FPFormat<6>; 756 def SpecialFP : FPFormat<7>; 757 758The purpose of the ``FPFormat`` class is to act as a sort of enumerated 759type. It provides a single field, ``Value``, which holds a 3-bit number. Its 760template argument, ``val``, is used to set the ``Value`` field. Each of the 761eight records is defined with ``FPFormat`` as its parent class. The 762enumeration value is passed in angle brackets as the template argument. Each 763record will inherent the ``Value`` field with the appropriate enumeration 764value. 765 766Here is a more complex example of classes with template arguments. First, we 767define a class similar to the ``FPFormat`` class above. It takes a template 768argument and uses it to initialize a field named ``Value``. Then we define 769four records that inherit the ``Value`` field with its four different 770integer values. 771 772.. code-block:: text 773 774 class ModRefVal <bits<2> val> { 775 bits<2> Value = val; 776 } 777 778 def None : ModRefVal<0>; 779 def Mod : ModRefVal<1>; 780 def Ref : ModRefVal<2>; 781 def ModRef : ModRefVal<3>; 782 783This is somewhat contrived, but let's say we would like to examine the two 784bits of the ``Value`` field independently. We can define a class that 785accepts a ``ModRefVal`` record as a template argument and splits up its 786value into two fields, one bit each. Then we can define records that inherit from 787``ModRefBits`` and so acquire two fields from it, one for each bit in the 788``ModRefVal`` record passed as the template argument. 789 790.. code-block:: text 791 792 class ModRefBits <ModRefVal mrv> { 793 // Break the value up into its bits, which can provide a nice 794 // interface to the ModRefVal values. 795 bit isMod = mrv.Value{0}; 796 bit isRef = mrv.Value{1}; 797 } 798 799 // Example uses. 800 def foo : ModRefBits<Mod>; 801 def bar : ModRefBits<Ref>; 802 def snork : ModRefBits<ModRef>; 803 804This illustrates how one class can be defined to reorganize the 805fields in another class, thus hiding the internal representation of that 806other class. 807 808Running ``llvm-tblgen`` on the example prints the following definitions: 809 810.. code-block:: text 811 812 def bar { // Value 813 bit isMod = 0; 814 bit isRef = 1; 815 } 816 def foo { // Value 817 bit isMod = 1; 818 bit isRef = 0; 819 } 820 def snork { // Value 821 bit isMod = 1; 822 bit isRef = 1; 823 } 824 825``let`` --- override fields in classes or records 826------------------------------------------------- 827 828A ``let`` statement collects a set of field values (sometimes called 829*bindings*) and applies them to all the classes and records defined by 830statements within the scope of the ``let``. 831 832.. productionlist:: 833 Let: "let" `LetList` "in" "{" `Statement`* "}" 834 :| "let" `LetList` "in" `Statement` 835 LetList: `LetItem` ("," `LetItem`)* 836 LetItem: `TokIdentifier` ["<" `RangeList` ">"] "=" `Value` 837 838The ``let`` statement establishes a scope, which is a sequence of statements 839in braces or a single statement with no braces. The bindings in the 840:token:`LetList` apply to the statements in that scope. 841 842The field names in the :token:`LetList` must name fields in classes inherited by 843the classes and records defined in the statements. The field values are 844applied to the classes and records *after* the records inherit all the fields from 845their parent classes. So the ``let`` acts to override inherited field 846values. A ``let`` cannot override the value of a template argument. 847 848Top-level ``let`` statements are often useful when a few fields need to be 849overriden in several records. Here are two examples. Note that ``let`` 850statements can be nested. 851 852.. code-block:: text 853 854 let isTerminator = true, isReturn = true, isBarrier = true, hasCtrlDep = true in 855 def RET : I<0xC3, RawFrm, (outs), (ins), "ret", [(X86retflag 0)]>; 856 857 let isCall = true in 858 // All calls clobber the non-callee saved registers... 859 let Defs = [EAX, ECX, EDX, FP0, FP1, FP2, FP3, FP4, FP5, FP6, ST0, 860 MM0, MM1, MM2, MM3, MM4, MM5, MM6, MM7, XMM0, XMM1, XMM2, 861 XMM3, XMM4, XMM5, XMM6, XMM7, EFLAGS] in { 862 def CALLpcrel32 : Ii32<0xE8, RawFrm, (outs), (ins i32imm:$dst, variable_ops), 863 "call\t${dst:call}", []>; 864 def CALL32r : I<0xFF, MRM2r, (outs), (ins GR32:$dst, variable_ops), 865 "call\t{*}$dst", [(X86call GR32:$dst)]>; 866 def CALL32m : I<0xFF, MRM2m, (outs), (ins i32mem:$dst, variable_ops), 867 "call\t{*}$dst", []>; 868 } 869 870Note that a top-level ``let`` will not override fields defined in the classes or records 871themselves. 872 873 874``multiclass`` --- define multiple records 875------------------------------------------ 876 877While classes with template arguments are a good way to factor out commonality 878between multiple records, multiclasses allow a convenient method for 879defining many records at once. For example, consider a 3-address 880instruction architecture whose instructions come in two formats: ``reg = reg 881op reg`` and ``reg = reg op imm`` (e.g., SPARC). We would like to specify in 882one place that these two common formats exist, then in a separate place 883specify what all the operations are. The ``multiclass`` and ``defm`` 884statements accomplish this goal. You can think of a multiclass as a macro or 885template that expands into multiple records. 886 887.. productionlist:: 888 MultiClass: "multiclass" `TokIdentifier` [`TemplateArgList`] 889 : [":" `ParentMultiClassList`] 890 : "{" `MultiClassStatement`+ "}" 891 ParentMultiClassList: `MultiClassID` ("," `MultiClassID`)* 892 MultiClassID: `TokIdentifier` 893 MultiClassStatement: `Assert` | `Def` | `Defm` | `Defvar` | `Foreach` | `If` | `Let` 894 895As with regular classes, the multiclass has a name and can accept template 896arguments. A multiclass can inherit from other multiclasses, which causes 897the other multiclasses to be expanded and contribute to the record 898definitions in the inheriting multiclass. The body of the multiclass 899contains a series of statements that define records, using :token:`Def` and 900:token:`Defm`. In addition, :token:`Defvar`, :token:`Foreach`, and 901:token:`Let` statements can be used to factor out even more common elements. 902The :token:`If` and :token:`Assert` statements can also be used. 903 904Also as with regular classes, the multiclass has the implicit template 905argument ``NAME`` (see NAME_). When a named (non-anonymous) record is 906defined in a multiclass and the record's name does not include a use of the 907template argument ``NAME``, such a use is automatically *prepended* 908to the name. That is, the following are equivalent inside a multiclass:: 909 910 def Foo ... 911 def NAME # Foo ... 912 913The records defined in a multiclass are created when the multiclass is 914"instantiated" or "invoked" by a ``defm`` statement outside the multiclass 915definition. Each ``def`` statement in the multiclass produces a record. As 916with top-level ``def`` statements, these definitions can inherit from 917multiple parent classes. 918 919See `Examples: multiclasses and defms`_ for examples. 920 921 922``defm`` --- invoke multiclasses to define multiple records 923----------------------------------------------------------- 924 925Once multiclasses have been defined, you use the ``defm`` statement to 926"invoke" them and process the multiple record definitions in those 927multiclasses. Those record definitions are specified by ``def`` 928statements in the multiclasses, and indirectly by ``defm`` statements. 929 930.. productionlist:: 931 Defm: "defm" [`NameValue`] `ParentClassList` ";" 932 933The optional :token:`NameValue` is formed in the same way as the name of a 934``def``. The :token:`ParentClassList` is a colon followed by a list of at 935least one multiclass and any number of regular classes. The multiclasses 936must precede the regular classes. Note that the ``defm`` does not have a 937body. 938 939This statement instantiates all the records defined in all the specified 940multiclasses, either directly by ``def`` statements or indirectly by 941``defm`` statements. These records also receive the fields defined in any 942regular classes included in the parent class list. This is useful for adding 943a common set of fields to all the records created by the ``defm``. 944 945The name is parsed in the same special mode used by ``def``. If the name is 946not included, an unspecified but globally unique name is provided. That is, 947the following examples end up with different names:: 948 949 defm : SomeMultiClass<...>; // A globally unique name. 950 defm "" : SomeMultiClass<...>; // An empty name. 951 952The ``defm`` statement can be used in a multiclass body. When this occurs, 953the second variant is equivalent to:: 954 955 defm NAME : SomeMultiClass<...>; 956 957More generally, when ``defm`` occurs in a multiclass and its name does not 958include a use of the implicit template argument ``NAME``, then ``NAME`` will 959be prepended automatically. That is, the following are equivalent inside a 960multiclass:: 961 962 defm Foo : SomeMultiClass<...>; 963 defm NAME # Foo : SomeMultiClass<...>; 964 965See `Examples: multiclasses and defms`_ for examples. 966 967Examples: multiclasses and defms 968-------------------------------- 969 970Here is a simple example using ``multiclass`` and ``defm``. Consider a 9713-address instruction architecture whose instructions come in two formats: 972``reg = reg op reg`` and ``reg = reg op imm`` (immediate). The SPARC is an 973example of such an architecture. 974 975.. code-block:: text 976 977 def ops; 978 def GPR; 979 def Imm; 980 class inst <int opc, string asmstr, dag operandlist>; 981 982 multiclass ri_inst <int opc, string asmstr> { 983 def _rr : inst<opc, !strconcat(asmstr, " $dst, $src1, $src2"), 984 (ops GPR:$dst, GPR:$src1, GPR:$src2)>; 985 def _ri : inst<opc, !strconcat(asmstr, " $dst, $src1, $src2"), 986 (ops GPR:$dst, GPR:$src1, Imm:$src2)>; 987 } 988 989 // Define records for each instruction in the RR and RI formats. 990 defm ADD : ri_inst<0b111, "add">; 991 defm SUB : ri_inst<0b101, "sub">; 992 defm MUL : ri_inst<0b100, "mul">; 993 994Each use of the ``ri_inst`` multiclass defines two records, one with the 995``_rr`` suffix and one with ``_ri``. Recall that the name of the ``defm`` 996that uses a multiclass is prepended to the names of the records defined in 997that multiclass. So the resulting definitions are named:: 998 999 ADD_rr, ADD_ri 1000 SUB_rr, SUB_ri 1001 MUL_rr, MUL_ri 1002 1003Without the ``multiclass`` feature, the instructions would have to be 1004defined as follows. 1005 1006.. code-block:: text 1007 1008 def ops; 1009 def GPR; 1010 def Imm; 1011 class inst <int opc, string asmstr, dag operandlist>; 1012 1013 class rrinst <int opc, string asmstr> 1014 : inst<opc, !strconcat(asmstr, " $dst, $src1, $src2"), 1015 (ops GPR:$dst, GPR:$src1, GPR:$src2)>; 1016 1017 class riinst <int opc, string asmstr> 1018 : inst<opc, !strconcat(asmstr, " $dst, $src1, $src2"), 1019 (ops GPR:$dst, GPR:$src1, Imm:$src2)>; 1020 1021 // Define records for each instruction in the RR and RI formats. 1022 def ADD_rr : rrinst<0b111, "add">; 1023 def ADD_ri : riinst<0b111, "add">; 1024 def SUB_rr : rrinst<0b101, "sub">; 1025 def SUB_ri : riinst<0b101, "sub">; 1026 def MUL_rr : rrinst<0b100, "mul">; 1027 def MUL_ri : riinst<0b100, "mul">; 1028 1029A ``defm`` can be used in a multiclass to "invoke" other multiclasses and 1030create the records defined in those multiclasses in addition to the records 1031defined in the current multiclass. In the following example, the ``basic_s`` 1032and ``basic_p`` multiclasses contain ``defm`` statements that refer to the 1033``basic_r`` multiclass. The ``basic_r`` multiclass contains only ``def`` 1034statements. 1035 1036.. code-block:: text 1037 1038 class Instruction <bits<4> opc, string Name> { 1039 bits<4> opcode = opc; 1040 string name = Name; 1041 } 1042 1043 multiclass basic_r <bits<4> opc> { 1044 def rr : Instruction<opc, "rr">; 1045 def rm : Instruction<opc, "rm">; 1046 } 1047 1048 multiclass basic_s <bits<4> opc> { 1049 defm SS : basic_r<opc>; 1050 defm SD : basic_r<opc>; 1051 def X : Instruction<opc, "x">; 1052 } 1053 1054 multiclass basic_p <bits<4> opc> { 1055 defm PS : basic_r<opc>; 1056 defm PD : basic_r<opc>; 1057 def Y : Instruction<opc, "y">; 1058 } 1059 1060 defm ADD : basic_s<0xf>, basic_p<0xf>; 1061 1062The final ``defm`` creates the following records, five from the ``basic_s`` 1063multiclass and five from the ``basic_p`` multiclass:: 1064 1065 ADDSSrr, ADDSSrm 1066 ADDSDrr, ADDSDrm 1067 ADDX 1068 ADDPSrr, ADDPSrm 1069 ADDPDrr, ADDPDrm 1070 ADDY 1071 1072A ``defm`` statement, both at top level and in a multiclass, can inherit 1073from regular classes in addition to multiclasses. The rule is that the 1074regular classes must be listed after the multiclasses, and there must be at least 1075one multiclass. 1076 1077.. code-block:: text 1078 1079 class XD { 1080 bits<4> Prefix = 11; 1081 } 1082 class XS { 1083 bits<4> Prefix = 12; 1084 } 1085 class I <bits<4> op> { 1086 bits<4> opcode = op; 1087 } 1088 1089 multiclass R { 1090 def rr : I<4>; 1091 def rm : I<2>; 1092 } 1093 1094 multiclass Y { 1095 defm SS : R, XD; // First multiclass R, then regular class XD. 1096 defm SD : R, XS; 1097 } 1098 1099 defm Instr : Y; 1100 1101This example will create four records, shown here in alphabetical order with 1102their fields. 1103 1104.. code-block:: text 1105 1106 def InstrSDrm { 1107 bits<4> opcode = { 0, 0, 1, 0 }; 1108 bits<4> Prefix = { 1, 1, 0, 0 }; 1109 } 1110 1111 def InstrSDrr { 1112 bits<4> opcode = { 0, 1, 0, 0 }; 1113 bits<4> Prefix = { 1, 1, 0, 0 }; 1114 } 1115 1116 def InstrSSrm { 1117 bits<4> opcode = { 0, 0, 1, 0 }; 1118 bits<4> Prefix = { 1, 0, 1, 1 }; 1119 } 1120 1121 def InstrSSrr { 1122 bits<4> opcode = { 0, 1, 0, 0 }; 1123 bits<4> Prefix = { 1, 0, 1, 1 }; 1124 } 1125 1126It's also possible to use ``let`` statements inside multiclasses, providing 1127another way to factor out commonality from the records, especially when 1128using several levels of multiclass instantiations. 1129 1130.. code-block:: text 1131 1132 multiclass basic_r <bits<4> opc> { 1133 let Predicates = [HasSSE2] in { 1134 def rr : Instruction<opc, "rr">; 1135 def rm : Instruction<opc, "rm">; 1136 } 1137 let Predicates = [HasSSE3] in 1138 def rx : Instruction<opc, "rx">; 1139 } 1140 1141 multiclass basic_ss <bits<4> opc> { 1142 let IsDouble = false in 1143 defm SS : basic_r<opc>; 1144 1145 let IsDouble = true in 1146 defm SD : basic_r<opc>; 1147 } 1148 1149 defm ADD : basic_ss<0xf>; 1150 1151 1152``defset`` --- create a definition set 1153-------------------------------------- 1154 1155The ``defset`` statement is used to collect a set of records into a global 1156list of records. 1157 1158.. productionlist:: 1159 Defset: "defset" `Type` `TokIdentifier` "=" "{" `Statement`* "}" 1160 1161All records defined inside the braces via ``def`` and ``defm`` are defined 1162as usual, and they are also collected in a global list of the given name 1163(:token:`TokIdentifier`). 1164 1165The specified type must be ``list<``\ *class*\ ``>``, where *class* is some 1166record class. The ``defset`` statement establishes a scope for its 1167statements. It is an error to define a record in the scope of the 1168``defset`` that is not of type *class*. 1169 1170The ``defset`` statement can be nested. The inner ``defset`` adds the 1171records to its own set, and all those records are also added to the outer 1172set. 1173 1174Anonymous records created inside initialization expressions using the 1175``ClassID<...>`` syntax are not collected in the set. 1176 1177 1178``defvar`` --- define a variable 1179-------------------------------- 1180 1181A ``defvar`` statement defines a global variable. Its value can be used 1182throughout the statements that follow the definition. 1183 1184.. productionlist:: 1185 Defvar: "defvar" `TokIdentifier` "=" `Value` ";" 1186 1187The identifier on the left of the ``=`` is defined to be a global variable 1188whose value is given by the value expression on the right of the ``=``. The 1189type of the variable is automatically inferred. 1190 1191Once a variable has been defined, it cannot be set to another value. 1192 1193Variables defined in a top-level ``foreach`` go out of scope at the end of 1194each loop iteration, so their value in one iteration is not available in 1195the next iteration. The following ``defvar`` will not work:: 1196 1197 defvar i = !add(i, 1) 1198 1199Variables can also be defined with ``defvar`` in a record body. See 1200`Defvar in a Record Body`_ for more details. 1201 1202``foreach`` --- iterate over a sequence of statements 1203----------------------------------------------------- 1204 1205The ``foreach`` statement iterates over a series of statements, varying a 1206variable over a sequence of values. 1207 1208.. productionlist:: 1209 Foreach: "foreach" `ForeachIterator` "in" "{" `Statement`* "}" 1210 :| "foreach" `ForeachIterator` "in" `Statement` 1211 ForeachIterator: `TokIdentifier` "=" ("{" `RangeList` "}" | `RangePiece` | `Value`) 1212 1213The body of the ``foreach`` is a series of statements in braces or a 1214single statement with no braces. The statements are re-evaluated once for 1215each value in the range list, range piece, or single value. On each 1216iteration, the :token:`TokIdentifier` variable is set to the value and can 1217be used in the statements. 1218 1219The statement list establishes an inner scope. Variables local to a 1220``foreach`` go out of scope at the end of each loop iteration, so their 1221values do not carry over from one iteration to the next. Foreach loops may 1222be nested. 1223 1224The ``foreach`` statement can also be used in a record :token:`Body`. 1225 1226.. Note that the productions involving RangeList and RangePiece have precedence 1227 over the more generic value parsing based on the first token. 1228 1229.. code-block:: text 1230 1231 foreach i = [0, 1, 2, 3] in { 1232 def R#i : Register<...>; 1233 def F#i : Register<...>; 1234 } 1235 1236This loop defines records named ``R0``, ``R1``, ``R2``, and ``R3``, along 1237with ``F0``, ``F1``, ``F2``, and ``F3``. 1238 1239 1240``if`` --- select statements based on a test 1241-------------------------------------------- 1242 1243The ``if`` statement allows one of two statement groups to be selected based 1244on the value of an expression. 1245 1246.. productionlist:: 1247 If: "if" `Value` "then" `IfBody` 1248 :| "if" `Value` "then" `IfBody` "else" `IfBody` 1249 IfBody: "{" `Statement`* "}" | `Statement` 1250 1251The value expression is evaluated. If it evaluates to true (in the same 1252sense used by the bang operators), then the statements following the 1253``then`` reserved word are processed. Otherwise, if there is an ``else`` 1254reserved word, the statements following the ``else`` are processed. If the 1255value is false and there is no ``else`` arm, no statements are processed. 1256 1257Because the braces around the ``then`` statements are optional, this grammar rule 1258has the usual ambiguity with "dangling else" clauses, and it is resolved in 1259the usual way: in a case like ``if v1 then if v2 then {...} else {...}``, the 1260``else`` associates with the inner ``if`` rather than the outer one. 1261 1262The :token:`IfBody` of the then and else arms of the ``if`` establish an 1263inner scope. Any ``defvar`` variables defined in the bodies go out of scope 1264when the bodies are finished (see `Defvar in a Record Body`_ for more details). 1265 1266The ``if`` statement can also be used in a record :token:`Body`. 1267 1268 1269``assert`` --- check that a condition is true 1270--------------------------------------------- 1271 1272The ``assert`` statement checks a boolean condition to be sure that it is true 1273and prints an error message if it is not. 1274 1275.. productionlist:: 1276 Assert: "assert" `condition` "," `message` ";" 1277 1278If the boolean condition is true, the statement does nothing. If the 1279condition is false, it prints a nonfatal error message. The **message**, which 1280can be an arbitrary string expression, is included in the error message as a 1281note. The exact behavior of the ``assert`` statement depends on its 1282placement. 1283 1284* At top level, the assertion is checked immediately. 1285 1286* In a record definition, the statement is saved and all assertions are 1287 checked after the record is completely built. 1288 1289* In a class definition, the assertions are saved and inherited by all 1290 the subclasses and records that inherit from the class. The assertions are 1291 then checked when the records are completely built. 1292 1293* In a multiclass definition, the assertions are saved with the other 1294 components of the multiclass and then checked each time the multiclass 1295 is instantiated with ``defm``. 1296 1297Using assertions in TableGen files can simplify record checking in TableGen 1298backends. Here is an example of an ``assert`` in two class definitions. 1299 1300.. code-block:: text 1301 1302 class PersonName<string name> { 1303 assert !le(!size(name), 32), "person name is too long: " # name; 1304 string Name = name; 1305 } 1306 1307 class Person<string name, int age> : PersonName<name> { 1308 assert !and(!ge(age, 1), !le(age, 120)), "person age is invalid: " # age; 1309 int Age = age; 1310 } 1311 1312 def Rec20 : Person<"Donald Knuth", 60> { 1313 ... 1314 } 1315 1316 1317Additional Details 1318================== 1319 1320Directed acyclic graphs (DAGs) 1321------------------------------ 1322 1323A directed acyclic graph can be represented directly in TableGen using the 1324``dag`` datatype. A DAG node consists of an operator and zero or more 1325arguments (or operands). Each argument can be of any desired type. By using 1326another DAG node as an argument, an arbitrary graph of DAG nodes can be 1327built. 1328 1329The syntax of a ``dag`` instance is: 1330 1331 ``(`` *operator* *argument1*\ ``,`` *argument2*\ ``,`` ... ``)`` 1332 1333The operator must be present and must be a record. There can be zero or more 1334arguments, separated by commas. The operator and arguments can have three 1335formats. 1336 1337====================== ============================================= 1338Format Meaning 1339====================== ============================================= 1340*value* argument value 1341*value*\ ``:``\ *name* argument value and associated name 1342*name* argument name with unset (uninitialized) value 1343====================== ============================================= 1344 1345The *value* can be any TableGen value. The *name*, if present, must be a 1346:token:`TokVarName`, which starts with a dollar sign (``$``). The purpose of 1347a name is to tag an operator or argument in a DAG with a particular meaning, 1348or to associate an argument in one DAG with a like-named argument in another 1349DAG. 1350 1351The following bang operators are useful for working with DAGs: 1352``!con``, ``!dag``, ``!empty``, ``!foreach``, ``!getdagop``, ``!setdagop``, ``!size``. 1353 1354Defvar in a record body 1355----------------------- 1356 1357In addition to defining global variables, the ``defvar`` statement can 1358be used inside the :token:`Body` of a class or record definition to define 1359local variables. The scope of the variable extends from the ``defvar`` 1360statement to the end of the body. It cannot be set to a different value 1361within its scope. The ``defvar`` statement can also be used in the statement 1362list of a ``foreach``, which establishes a scope. 1363 1364A variable named ``V`` in an inner scope shadows (hides) any variables ``V`` 1365in outer scopes. In particular, ``V`` in a record body shadows a global 1366``V``, and ``V`` in a ``foreach`` statement list shadows any ``V`` in 1367surrounding record or global scopes. 1368 1369Variables defined in a ``foreach`` go out of scope at the end of 1370each loop iteration, so their value in one iteration is not available in 1371the next iteration. The following ``defvar`` will not work:: 1372 1373 defvar i = !add(i, 1) 1374 1375How records are built 1376--------------------- 1377 1378The following steps are taken by TableGen when a record is built. Classes are simply 1379abstract records and so go through the same steps. 1380 13811. Build the record name (:token:`NameValue`) and create an empty record. 1382 13832. Parse the parent classes in the :token:`ParentClassList` from left to 1384 right, visiting each parent class's ancestor classes from top to bottom. 1385 1386 a. Add the fields from the parent class to the record. 1387 b. Substitute the template arguments into those fields. 1388 c. Add the parent class to the record's list of inherited classes. 1389 13903. Apply any top-level ``let`` bindings to the record. Recall that top-level 1391 bindings only apply to inherited fields. 1392 13934. Parse the body of the record. 1394 1395 * Add any fields to the record. 1396 * Modify the values of fields according to local ``let`` statements. 1397 * Define any ``defvar`` variables. 1398 13995. Make a pass over all the fields to resolve any inter-field references. 1400 14016. Add the record to the master record list. 1402 1403Because references between fields are resolved (step 5) after ``let`` bindings are 1404applied (step 3), the ``let`` statement has unusual power. For example: 1405 1406.. code-block:: text 1407 1408 class C <int x> { 1409 int Y = x; 1410 int Yplus1 = !add(Y, 1); 1411 int xplus1 = !add(x, 1); 1412 } 1413 1414 let Y = 10 in { 1415 def rec1 : C<5> { 1416 } 1417 } 1418 1419 def rec2 : C<5> { 1420 let Y = 10; 1421 } 1422 1423In both cases, one where a top-level ``let`` is used to bind ``Y`` and one 1424where a local ``let`` does the same thing, the results are: 1425 1426.. code-block:: text 1427 1428 def rec1 { // C 1429 int Y = 10; 1430 int Yplus1 = 11; 1431 int xplus1 = 6; 1432 } 1433 def rec2 { // C 1434 int Y = 10; 1435 int Yplus1 = 11; 1436 int xplus1 = 6; 1437 } 1438 1439``Yplus1`` is 11 because the ``let Y`` is performed before the ``!add(Y, 14401)`` is resolved. Use this power wisely. 1441 1442 1443Using Classes as Subroutines 1444============================ 1445 1446As described in `Simple values`_, a class can be invoked in an expression 1447and passed template arguments. This causes TableGen to create a new anonymous 1448record inheriting from that class. As usual, the record receives all the 1449fields defined in the class. 1450 1451This feature can be employed as a simple subroutine facility. The class can 1452use the template arguments to define various variables and fields, which end 1453up in the anonymous record. Those fields can then be retrieved in the 1454expression invoking the class as follows. Assume that the field ``ret`` 1455contains the final value of the subroutine. 1456 1457.. code-block:: text 1458 1459 int Result = ... CalcValue<arg>.ret ...; 1460 1461The ``CalcValue`` class is invoked with the template argument ``arg``. It 1462calculates a value for the ``ret`` field, which is then retrieved at the 1463"point of call" in the initialization for the Result field. The anonymous 1464record created in this example serves no other purpose than to carry the 1465result value. 1466 1467Here is a practical example. The class ``isValidSize`` determines whether a 1468specified number of bytes represents a valid data size. The bit ``ret`` is 1469set appropriately. The field ``ValidSize`` obtains its initial value by 1470invoking ``isValidSize`` with the data size and retrieving the ``ret`` field 1471from the resulting anonymous record. 1472 1473.. code-block:: text 1474 1475 class isValidSize<int size> { 1476 bit ret = !cond(!eq(size, 1): 1, 1477 !eq(size, 2): 1, 1478 !eq(size, 4): 1, 1479 !eq(size, 8): 1, 1480 !eq(size, 16): 1, 1481 true: 0); 1482 } 1483 1484 def Data1 { 1485 int Size = ...; 1486 bit ValidSize = isValidSize<Size>.ret; 1487 } 1488 1489Preprocessing Facilities 1490======================== 1491 1492The preprocessor embedded in TableGen is intended only for simple 1493conditional compilation. It supports the following directives, which are 1494specified somewhat informally. 1495 1496.. productionlist:: 1497 LineBegin: beginning of line 1498 LineEnd: newline | return | EOF 1499 WhiteSpace: space | tab 1500 CComment: "/*" ... "*/" 1501 BCPLComment: "//" ... `LineEnd` 1502 WhiteSpaceOrCComment: `WhiteSpace` | `CComment` 1503 WhiteSpaceOrAnyComment: `WhiteSpace` | `CComment` | `BCPLComment` 1504 MacroName: `ualpha` (`ualpha` | "0"..."9")* 1505 PreDefine: `LineBegin` (`WhiteSpaceOrCComment`)* 1506 : "#define" (`WhiteSpace`)+ `MacroName` 1507 : (`WhiteSpaceOrAnyComment`)* `LineEnd` 1508 PreIfdef: `LineBegin` (`WhiteSpaceOrCComment`)* 1509 : ("#ifdef" | "#ifndef") (`WhiteSpace`)+ `MacroName` 1510 : (`WhiteSpaceOrAnyComment`)* `LineEnd` 1511 PreElse: `LineBegin` (`WhiteSpaceOrCComment`)* 1512 : "#else" (`WhiteSpaceOrAnyComment`)* `LineEnd` 1513 PreEndif: `LineBegin` (`WhiteSpaceOrCComment`)* 1514 : "#endif" (`WhiteSpaceOrAnyComment`)* `LineEnd` 1515 1516.. 1517 PreRegContentException: `PreIfdef` | `PreElse` | `PreEndif` | EOF 1518 PreRegion: .* - `PreRegContentException` 1519 :| `PreIfdef` 1520 : (`PreRegion`)* 1521 : [`PreElse`] 1522 : (`PreRegion`)* 1523 : `PreEndif` 1524 1525A :token:`MacroName` can be defined anywhere in a TableGen file. The name has 1526no value; it can only be tested to see whether it is defined. 1527 1528A macro test region begins with an ``#ifdef`` or ``#ifndef`` directive. If 1529the macro name is defined (``#ifdef``) or undefined (``#ifndef``), then the 1530source code between the directive and the corresponding ``#else`` or 1531``#endif`` is processed. If the test fails but there is an ``#else`` 1532clause, the source code between the ``#else`` and the ``#endif`` is 1533processed. If the test fails and there is no ``#else`` clause, then no 1534source code in the test region is processed. 1535 1536Test regions may be nested, but they must be properly nested. A region 1537started in a file must end in that file; that is, must have its 1538``#endif`` in the same file. 1539 1540A :token:`MacroName` may be defined externally using the ``-D`` option on the 1541``*-tblgen`` command line:: 1542 1543 llvm-tblgen self-reference.td -Dmacro1 -Dmacro3 1544 1545Appendix A: Bang Operators 1546========================== 1547 1548Bang operators act as functions in value expressions. A bang operator takes 1549one or more arguments, operates on them, and produces a result. If the 1550operator produces a boolean result, the result value will be 1 for true or 0 1551for false. When an operator tests a boolean argument, it interprets 0 as false 1552and non-0 as true. 1553 1554.. warning:: 1555 The ``!getop`` and ``!setop`` bang operators are deprecated in favor of 1556 ``!getdagop`` and ``!setdagop``. 1557 1558``!add(``\ *a*\ ``,`` *b*\ ``, ...)`` 1559 This operator adds *a*, *b*, etc., and produces the sum. 1560 1561``!and(``\ *a*\ ``,`` *b*\ ``, ...)`` 1562 This operator does a bitwise AND on *a*, *b*, etc., and produces the 1563 result. A logical AND can be performed if all the arguments are either 1564 0 or 1. 1565 1566``!cast<``\ *type*\ ``>(``\ *a*\ ``)`` 1567 This operator performs a cast on *a* and produces the result. 1568 If *a* is not a string, then a straightforward cast is performed, say 1569 between an ``int`` and a ``bit``, or between record types. This allows 1570 casting a record to a class. If a record is cast to ``string``, the 1571 record's name is produced. 1572 1573 If *a* is a string, then it is treated as a record name and looked up in 1574 the list of all defined records. The resulting record is expected to be of 1575 the specified *type*. 1576 1577 For example, if ``!cast<``\ *type*\ ``>(``\ *name*\ ``)`` 1578 appears in a multiclass definition, or in a 1579 class instantiated inside a multiclass definition, and the *name* does not 1580 reference any template arguments of the multiclass, then a record by 1581 that name must have been instantiated earlier 1582 in the source file. If *name* does reference 1583 a template argument, then the lookup is delayed until ``defm`` statements 1584 instantiating the multiclass (or later, if the defm occurs in another 1585 multiclass and template arguments of the inner multiclass that are 1586 referenced by *name* are substituted by values that themselves contain 1587 references to template arguments of the outer multiclass). 1588 1589 If the type of *a* does not match *type*, TableGen raises an error. 1590 1591``!con(``\ *a*\ ``,`` *b*\ ``, ...)`` 1592 This operator concatenates the DAG nodes *a*, *b*, etc. Their operations 1593 must equal. 1594 1595 ``!con((op a1:$name1, a2:$name2), (op b1:$name3))`` 1596 1597 results in the DAG node ``(op a1:$name1, a2:$name2, b1:$name3)``. 1598 1599``!cond(``\ *cond1* ``:`` *val1*\ ``,`` *cond2* ``:`` *val2*\ ``, ...,`` *condn* ``:`` *valn*\ ``)`` 1600 This operator tests *cond1* and returns *val1* if the result is true. 1601 If false, the operator tests *cond2* and returns *val2* if the result is 1602 true. And so forth. An error is reported if no conditions are true. 1603 1604 This example produces the sign word for an integer:: 1605 1606 !cond(!lt(x, 0) : "negative", !eq(x, 0) : "zero", true : "positive") 1607 1608``!dag(``\ *op*\ ``,`` *arguments*\ ``,`` *names*\ ``)`` 1609 This operator creates a DAG node with the given operator and 1610 arguments. The *arguments* and *names* arguments must be lists 1611 of equal length or uninitialized (``?``). The *names* argument 1612 must be of type ``list<string>``. 1613 1614 Due to limitations of the type system, *arguments* must be a list of items 1615 of a common type. In practice, this means that they should either have the 1616 same type or be records with a common parent class. Mixing ``dag`` and 1617 non-``dag`` items is not possible. However, ``?`` can be used. 1618 1619 Example: ``!dag(op, [a1, a2, ?], ["name1", "name2", "name3"])`` results in 1620 ``(op a1-value:$name1, a2-value:$name2, ?:$name3)``. 1621 1622``!empty(``\ *a*\ ``)`` 1623 This operator produces 1 if the string, list, or DAG *a* is empty; 0 otherwise. 1624 A dag is empty if it has no arguments; the operator does not count. 1625 1626``!eq(`` *a*\ `,` *b*\ ``)`` 1627 This operator produces 1 if *a* is equal to *b*; 0 otherwise. 1628 The arguments must be ``bit``, ``bits``, ``int``, ``string``, or 1629 record values. Use ``!cast<string>`` to compare other types of objects. 1630 1631``!filter(``\ *var*\ ``,`` *list*\ ``,`` *predicate*\ ``)`` 1632 1633 This operator creates a new ``list`` by filtering the elements in 1634 *list*. To perform the filtering, TableGen binds the variable *var* to each 1635 element and then evaluates the *predicate* expression, which presumably 1636 refers to *var*. The predicate must 1637 produce a boolean value (``bit``, ``bits``, or ``int``). The value is 1638 interpreted as with ``!if``: 1639 if the value is 0, the element is not included in the new list. If the value 1640 is anything else, the element is included. 1641 1642``!foldl(``\ *init*\ ``,`` *list*\ ``,`` *acc*\ ``,`` *var*\ ``,`` *expr*\ ``)`` 1643 This operator performs a left-fold over the items in *list*. The 1644 variable *acc* acts as the accumulator and is initialized to *init*. 1645 The variable *var* is bound to each element in the *list*. The 1646 expression is evaluated for each element and presumably uses *acc* and 1647 *var* to calculate the accumulated value, which ``!foldl`` stores back in 1648 *acc*. The type of *acc* is the same as *init*; the type of *var* is the 1649 same as the elements of *list*; *expr* must have the same type as *init*. 1650 1651 The following example computes the total of the ``Number`` field in the 1652 list of records in ``RecList``:: 1653 1654 int x = !foldl(0, RecList, total, rec, !add(total, rec.Number)); 1655 1656 If your goal is to filter the list and produce a new list that includes only 1657 some of the elements, see ``!filter``. 1658 1659``!foreach(``\ *var*\ ``,`` *sequence*\ ``,`` *expr*\ ``)`` 1660 This operator creates a new ``list``/``dag`` in which each element is a 1661 function of the corresponding element in the *sequence* ``list``/``dag``. 1662 To perform the function, TableGen binds the variable *var* to an element 1663 and then evaluates the expression. The expression presumably refers 1664 to the variable *var* and calculates the result value. 1665 1666 If you simply want to create a list of a certain length containing 1667 the same value repeated multiple times, see ``!listsplat``. 1668 1669``!ge(``\ *a*\ `,` *b*\ ``)`` 1670 This operator produces 1 if *a* is greater than or equal to *b*; 0 otherwise. 1671 The arguments must be ``bit``, ``bits``, ``int``, or ``string`` values. 1672 1673``!getdagop(``\ *dag*\ ``)`` --or-- ``!getdagop<``\ *type*\ ``>(``\ *dag*\ ``)`` 1674 This operator produces the operator of the given *dag* node. 1675 Example: ``!getdagop((foo 1, 2))`` results in ``foo``. Recall that 1676 DAG operators are always records. 1677 1678 The result of ``!getdagop`` can be used directly in a context where 1679 any record class at all is acceptable (typically placing it into 1680 another dag value). But in other contexts, it must be explicitly 1681 cast to a particular class. The ``<``\ *type*\ ``>`` syntax is 1682 provided to make this easy. 1683 1684 For example, to assign the result to a value of type ``BaseClass``, you 1685 could write either of these:: 1686 1687 BaseClass b = !getdagop<BaseClass>(someDag); 1688 BaseClass b = !cast<BaseClass>(!getdagop(someDag)); 1689 1690 But to create a new DAG node that reuses the operator from another, no 1691 cast is necessary:: 1692 1693 dag d = !dag(!getdagop(someDag), args, names); 1694 1695``!gt(``\ *a*\ `,` *b*\ ``)`` 1696 This operator produces 1 if *a* is greater than *b*; 0 otherwise. 1697 The arguments must be ``bit``, ``bits``, ``int``, or ``string`` values. 1698 1699``!head(``\ *a*\ ``)`` 1700 This operator produces the zeroth element of the list *a*. 1701 (See also ``!tail``.) 1702 1703``!if(``\ *test*\ ``,`` *then*\ ``,`` *else*\ ``)`` 1704 This operator evaluates the *test*, which must produce a ``bit`` or 1705 ``int``. If the result is not 0, the *then* expression is produced; otherwise 1706 the *else* expression is produced. 1707 1708``!interleave(``\ *list*\ ``,`` *delim*\ ``)`` 1709 This operator concatenates the items in the *list*, interleaving the 1710 *delim* string between each pair, and produces the resulting string. 1711 The list can be a list of string, int, bits, or bit. An empty list 1712 results in an empty string. The delimiter can be the empty string. 1713 1714``!isa<``\ *type*\ ``>(``\ *a*\ ``)`` 1715 This operator produces 1 if the type of *a* is a subtype of the given *type*; 0 1716 otherwise. 1717 1718``!le(``\ *a*\ ``,`` *b*\ ``)`` 1719 This operator produces 1 if *a* is less than or equal to *b*; 0 otherwise. 1720 The arguments must be ``bit``, ``bits``, ``int``, or ``string`` values. 1721 1722``!listconcat(``\ *list1*\ ``,`` *list2*\ ``, ...)`` 1723 This operator concatenates the list arguments *list1*, *list2*, etc., and 1724 produces the resulting list. The lists must have the same element type. 1725 1726``!listsplat(``\ *value*\ ``,`` *count*\ ``)`` 1727 This operator produces a list of length *count* whose elements are all 1728 equal to the *value*. For example, ``!listsplat(42, 3)`` results in 1729 ``[42, 42, 42]``. 1730 1731``!lt(``\ *a*\ `,` *b*\ ``)`` 1732 This operator produces 1 if *a* is less than *b*; 0 otherwise. 1733 The arguments must be ``bit``, ``bits``, ``int``, or ``string`` values. 1734 1735``!mul(``\ *a*\ ``,`` *b*\ ``, ...)`` 1736 This operator multiplies *a*, *b*, etc., and produces the product. 1737 1738``!ne(``\ *a*\ `,` *b*\ ``)`` 1739 This operator produces 1 if *a* is not equal to *b*; 0 otherwise. 1740 The arguments must be ``bit``, ``bits``, ``int``, ``string``, 1741 or record values. Use ``!cast<string>`` to compare other types of objects. 1742 1743``!not(``\ *a*\ ``)`` 1744 This operator performs a logical NOT on *a*, which must be 1745 an integer. The argument 0 results in 1 (true); any other 1746 argument results in 0 (false). 1747 1748``!or(``\ *a*\ ``,`` *b*\ ``, ...)`` 1749 This operator does a bitwise OR on *a*, *b*, etc., and produces the 1750 result. A logical OR can be performed if all the arguments are either 1751 0 or 1. 1752 1753``!setdagop(``\ *dag*\ ``,`` *op*\ ``)`` 1754 This operator produces a DAG node with the same arguments as *dag*, but with its 1755 operator replaced with *op*. 1756 1757 Example: ``!setdagop((foo 1, 2), bar)`` results in ``(bar 1, 2)``. 1758 1759``!shl(``\ *a*\ ``,`` *count*\ ``)`` 1760 This operator shifts *a* left logically by *count* bits and produces the resulting 1761 value. The operation is performed on a 64-bit integer; the result 1762 is undefined for shift counts outside 0...63. 1763 1764``!size(``\ *a*\ ``)`` 1765 This operator produces the size of the string, list, or dag *a*. 1766 The size of a DAG is the number of arguments; the operator does not count. 1767 1768``!sra(``\ *a*\ ``,`` *count*\ ``)`` 1769 This operator shifts *a* right arithmetically by *count* bits and produces the resulting 1770 value. The operation is performed on a 64-bit integer; the result 1771 is undefined for shift counts outside 0...63. 1772 1773``!srl(``\ *a*\ ``,`` *count*\ ``)`` 1774 This operator shifts *a* right logically by *count* bits and produces the resulting 1775 value. The operation is performed on a 64-bit integer; the result 1776 is undefined for shift counts outside 0...63. 1777 1778``!strconcat(``\ *str1*\ ``,`` *str2*\ ``, ...)`` 1779 This operator concatenates the string arguments *str1*, *str2*, etc., and 1780 produces the resulting string. 1781 1782``!sub(``\ *a*\ ``,`` *b*\ ``)`` 1783 This operator subtracts *b* from *a* and produces the arithmetic difference. 1784 1785``!subst(``\ *target*\ ``,`` *repl*\ ``,`` *value*\ ``)`` 1786 This operator replaces all occurrences of the *target* in the *value* with 1787 the *repl* and produces the resulting value. The *value* can 1788 be a string, in which case substring substitution is performed. 1789 1790 The *value* can be a record name, in which case the operator produces the *repl* 1791 record if the *target* record name equals the *value* record name; otherwise it 1792 produces the *value*. 1793 1794``!substr(``\ *string*\ ``,`` *start*\ [``,`` *length*]\ ``)`` 1795 This operator extracts a substring of the given *string*. The starting 1796 position of the substring is specified by *start*, which can range 1797 between 0 and the length of the string. The length of the substring 1798 is specified by *length*; if not specified, the rest of the string is 1799 extracted. The *start* and *length* arguments must be integers. 1800 1801``!tail(``\ *a*\ ``)`` 1802 This operator produces a new list with all the elements 1803 of the list *a* except for the zeroth one. (See also ``!head``.) 1804 1805``!xor(``\ *a*\ ``,`` *b*\ ``, ...)`` 1806 This operator does a bitwise EXCLUSIVE OR on *a*, *b*, etc., and produces 1807 the result. A logical XOR can be performed if all the arguments are either 1808 0 or 1. 1809 1810Appendix B: Paste Operator Examples 1811=================================== 1812 1813Here is an example illustrating the use of the paste operator in record names. 1814 1815.. code-block:: text 1816 1817 defvar suffix = "_suffstring"; 1818 defvar some_ints = [0, 1, 2, 3]; 1819 1820 def name # suffix { 1821 } 1822 1823 foreach i = [1, 2] in { 1824 def rec # i { 1825 } 1826 } 1827 1828The first ``def`` does not use the value of the ``suffix`` variable. The 1829second def does use the value of the ``i`` iterator variable, because it is not a 1830global name. The following records are produced. 1831 1832.. code-block:: text 1833 1834 def namesuffix { 1835 } 1836 def rec1 { 1837 } 1838 def rec2 { 1839 } 1840 1841Here is a second example illustrating the paste operator in field value expressions. 1842 1843.. code-block:: text 1844 1845 def test { 1846 string strings = suffix # suffix; 1847 list<int> integers = some_ints # [4, 5, 6]; 1848 } 1849 1850The ``strings`` field expression uses ``suffix`` on both sides of the paste 1851operator. It is evaluated normally on the left hand side, but taken verbatim 1852on the right hand side. The ``integers`` field expression uses the value of 1853the ``some_ints`` variable and a literal list. The following record is 1854produced. 1855 1856.. code-block:: text 1857 1858 def test { 1859 string strings = "_suffstringsuffix"; 1860 list<int> ints = [0, 1, 2, 3, 4, 5, 6]; 1861 } 1862 1863 1864Appendix C: Sample Record 1865========================= 1866 1867One target machine supported by LLVM is the Intel x86. The following output 1868from TableGen shows the record that is created to represent the 32-bit 1869register-to-register ADD instruction. 1870 1871.. code-block:: text 1872 1873 def ADD32rr { // InstructionEncoding Instruction X86Inst I ITy Sched BinOpRR BinOpRR_RF 1874 int Size = 0; 1875 string DecoderNamespace = ""; 1876 list<Predicate> Predicates = []; 1877 string DecoderMethod = ""; 1878 bit hasCompleteDecoder = 1; 1879 string Namespace = "X86"; 1880 dag OutOperandList = (outs GR32:$dst); 1881 dag InOperandList = (ins GR32:$src1, GR32:$src2); 1882 string AsmString = "add{l} {$src2, $src1|$src1, $src2}"; 1883 EncodingByHwMode EncodingInfos = ?; 1884 list<dag> Pattern = [(set GR32:$dst, EFLAGS, (X86add_flag GR32:$src1, GR32:$src2))]; 1885 list<Register> Uses = []; 1886 list<Register> Defs = [EFLAGS]; 1887 int CodeSize = 3; 1888 int AddedComplexity = 0; 1889 bit isPreISelOpcode = 0; 1890 bit isReturn = 0; 1891 bit isBranch = 0; 1892 bit isEHScopeReturn = 0; 1893 bit isIndirectBranch = 0; 1894 bit isCompare = 0; 1895 bit isMoveImm = 0; 1896 bit isMoveReg = 0; 1897 bit isBitcast = 0; 1898 bit isSelect = 0; 1899 bit isBarrier = 0; 1900 bit isCall = 0; 1901 bit isAdd = 0; 1902 bit isTrap = 0; 1903 bit canFoldAsLoad = 0; 1904 bit mayLoad = ?; 1905 bit mayStore = ?; 1906 bit mayRaiseFPException = 0; 1907 bit isConvertibleToThreeAddress = 1; 1908 bit isCommutable = 1; 1909 bit isTerminator = 0; 1910 bit isReMaterializable = 0; 1911 bit isPredicable = 0; 1912 bit isUnpredicable = 0; 1913 bit hasDelaySlot = 0; 1914 bit usesCustomInserter = 0; 1915 bit hasPostISelHook = 0; 1916 bit hasCtrlDep = 0; 1917 bit isNotDuplicable = 0; 1918 bit isConvergent = 0; 1919 bit isAuthenticated = 0; 1920 bit isAsCheapAsAMove = 0; 1921 bit hasExtraSrcRegAllocReq = 0; 1922 bit hasExtraDefRegAllocReq = 0; 1923 bit isRegSequence = 0; 1924 bit isPseudo = 0; 1925 bit isExtractSubreg = 0; 1926 bit isInsertSubreg = 0; 1927 bit variadicOpsAreDefs = 0; 1928 bit hasSideEffects = ?; 1929 bit isCodeGenOnly = 0; 1930 bit isAsmParserOnly = 0; 1931 bit hasNoSchedulingInfo = 0; 1932 InstrItinClass Itinerary = NoItinerary; 1933 list<SchedReadWrite> SchedRW = [WriteALU]; 1934 string Constraints = "$src1 = $dst"; 1935 string DisableEncoding = ""; 1936 string PostEncoderMethod = ""; 1937 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 }; 1938 string AsmMatchConverter = ""; 1939 string TwoOperandAliasConstraint = ""; 1940 string AsmVariantName = ""; 1941 bit UseNamedOperandTable = 0; 1942 bit FastISelShouldIgnore = 0; 1943 bits<8> Opcode = { 0, 0, 0, 0, 0, 0, 0, 1 }; 1944 Format Form = MRMDestReg; 1945 bits<7> FormBits = { 0, 1, 0, 1, 0, 0, 0 }; 1946 ImmType ImmT = NoImm; 1947 bit ForceDisassemble = 0; 1948 OperandSize OpSize = OpSize32; 1949 bits<2> OpSizeBits = { 1, 0 }; 1950 AddressSize AdSize = AdSizeX; 1951 bits<2> AdSizeBits = { 0, 0 }; 1952 Prefix OpPrefix = NoPrfx; 1953 bits<3> OpPrefixBits = { 0, 0, 0 }; 1954 Map OpMap = OB; 1955 bits<3> OpMapBits = { 0, 0, 0 }; 1956 bit hasREX_WPrefix = 0; 1957 FPFormat FPForm = NotFP; 1958 bit hasLockPrefix = 0; 1959 Domain ExeDomain = GenericDomain; 1960 bit hasREPPrefix = 0; 1961 Encoding OpEnc = EncNormal; 1962 bits<2> OpEncBits = { 0, 0 }; 1963 bit HasVEX_W = 0; 1964 bit IgnoresVEX_W = 0; 1965 bit EVEX_W1_VEX_W0 = 0; 1966 bit hasVEX_4V = 0; 1967 bit hasVEX_L = 0; 1968 bit ignoresVEX_L = 0; 1969 bit hasEVEX_K = 0; 1970 bit hasEVEX_Z = 0; 1971 bit hasEVEX_L2 = 0; 1972 bit hasEVEX_B = 0; 1973 bits<3> CD8_Form = { 0, 0, 0 }; 1974 int CD8_EltSize = 0; 1975 bit hasEVEX_RC = 0; 1976 bit hasNoTrackPrefix = 0; 1977 bits<7> VectSize = { 0, 0, 1, 0, 0, 0, 0 }; 1978 bits<7> CD8_Scale = { 0, 0, 0, 0, 0, 0, 0 }; 1979 string FoldGenRegForm = ?; 1980 string EVEX2VEXOverride = ?; 1981 bit isMemoryFoldable = 1; 1982 bit notEVEX2VEXConvertible = 0; 1983 } 1984 1985On the first line of the record, you can see that the ``ADD32rr`` record 1986inherited from eight classes. Although the inheritance hierarchy is complex, 1987using parent classes is much simpler than specifying the 109 individual 1988fields for each instruction. 1989 1990Here is the code fragment used to define ``ADD32rr`` and multiple other 1991``ADD`` instructions: 1992 1993.. code-block:: text 1994 1995 defm ADD : ArithBinOp_RF<0x00, 0x02, 0x04, "add", MRM0r, MRM0m, 1996 X86add_flag, add, 1, 1, 1>; 1997 1998The ``defm`` statement tells TableGen that ``ArithBinOp_RF`` is a 1999multiclass, which contains multiple concrete record definitions that inherit 2000from ``BinOpRR_RF``. That class, in turn, inherits from ``BinOpRR``, which 2001inherits from ``ITy`` and ``Sched``, and so forth. The fields are inherited 2002from all the parent classes; for example, ``IsIndirectBranch`` is inherited 2003from the ``Instruction`` class. 2004