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