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