blob: 2efee12ec9d5b991814cde6bafba0be76eaa96c5 [file] [log] [blame]
Sean Silva1b600182013-01-07 02:43:44 +00001===========================
2TableGen Language Reference
3===========================
4
Sean Silva1b600182013-01-07 02:43:44 +00005.. contents::
6 :local:
7
8.. warning::
9 This document is extremely rough. If you find something lacking, please
Tanya Lattner0d28f802015-08-05 03:51:17 +000010 fix it, file a documentation bug, or ask about it on llvm-dev.
Sean Silva1b600182013-01-07 02:43:44 +000011
12Introduction
13============
14
15This document is meant to be a normative spec about the TableGen language
16in and of itself (i.e. how to understand a given construct in terms of how
17it affects the final set of records represented by the TableGen file). If
18you are unsure if this document is really what you are looking for, please
Sean Silva397ee6e2014-04-07 22:46:40 +000019read the :doc:`introduction to TableGen <index>` first.
Sean Silva1b600182013-01-07 02:43:44 +000020
Renato Golin33f973a2014-04-01 09:51:49 +000021Notation
22========
Sean Silva1b600182013-01-07 02:43:44 +000023
Renato Golin33f973a2014-04-01 09:51:49 +000024The lexical and syntax notation used here is intended to imitate
25`Python's`_. In particular, for lexical definitions, the productions
26operate at the character level and there is no implied whitespace between
27elements. The syntax definitions operate at the token level, so there is
28implied whitespace between tokens.
Sean Silva1b600182013-01-07 02:43:44 +000029
Renato Golin33f973a2014-04-01 09:51:49 +000030.. _`Python's`: http://docs.python.org/py3k/reference/introduction.html#notation
Sean Silva1b600182013-01-07 02:43:44 +000031
Renato Golin33f973a2014-04-01 09:51:49 +000032Lexical Analysis
33================
Sean Silva1b600182013-01-07 02:43:44 +000034
Renato Golin33f973a2014-04-01 09:51:49 +000035TableGen supports BCPL (``// ...``) and nestable C-style (``/* ... */``)
Vyacheslav Zakharinf7d079e2018-11-27 18:57:43 +000036comments. TableGen also provides simple `Preprocessing Support`_.
Sean Silva1b600182013-01-07 02:43:44 +000037
Renato Golin33f973a2014-04-01 09:51:49 +000038The following is a listing of the basic punctuation tokens::
Sean Silva1b600182013-01-07 02:43:44 +000039
Renato Golin33f973a2014-04-01 09:51:49 +000040 - + [ ] { } ( ) < > : ; . = ? #
Sean Silva1b600182013-01-07 02:43:44 +000041
Renato Golin33f973a2014-04-01 09:51:49 +000042Numeric literals take one of the following forms:
Sean Silva1b600182013-01-07 02:43:44 +000043
Renato Golin33f973a2014-04-01 09:51:49 +000044.. TableGen actually will lex some pretty strange sequences an interpret
45 them as numbers. What is shown here is an attempt to approximate what it
46 "should" accept.
Sean Silva1b600182013-01-07 02:43:44 +000047
Renato Golin33f973a2014-04-01 09:51:49 +000048.. productionlist::
49 TokInteger: `DecimalInteger` | `HexInteger` | `BinInteger`
50 DecimalInteger: ["+" | "-"] ("0"..."9")+
51 HexInteger: "0x" ("0"..."9" | "a"..."f" | "A"..."F")+
52 BinInteger: "0b" ("0" | "1")+
Sean Silva1b600182013-01-07 02:43:44 +000053
Renato Golin33f973a2014-04-01 09:51:49 +000054One aspect to note is that the :token:`DecimalInteger` token *includes* the
55``+`` or ``-``, as opposed to having ``+`` and ``-`` be unary operators as
56most languages do.
Sean Silva1b600182013-01-07 02:43:44 +000057
Pete Cooper9b90dc72014-08-07 05:47:13 +000058Also note that :token:`BinInteger` creates a value of type ``bits<n>``
59(where ``n`` is the number of bits). This will implicitly convert to
60integers when needed.
61
Renato Golin33f973a2014-04-01 09:51:49 +000062TableGen has identifier-like tokens:
Sean Silva1b600182013-01-07 02:43:44 +000063
Renato Golin33f973a2014-04-01 09:51:49 +000064.. productionlist::
65 ualpha: "a"..."z" | "A"..."Z" | "_"
66 TokIdentifier: ("0"..."9")* `ualpha` (`ualpha` | "0"..."9")*
67 TokVarName: "$" `ualpha` (`ualpha` | "0"..."9")*
Sean Silva1b600182013-01-07 02:43:44 +000068
Renato Golin33f973a2014-04-01 09:51:49 +000069Note that unlike most languages, TableGen allows :token:`TokIdentifier` to
70begin with a number. In case of ambiguity, a token will be interpreted as a
71numeric literal rather than an identifier.
Sean Silva1b600182013-01-07 02:43:44 +000072
Renato Golin33f973a2014-04-01 09:51:49 +000073TableGen also has two string-like literals:
Sean Silva1b600182013-01-07 02:43:44 +000074
Renato Golin33f973a2014-04-01 09:51:49 +000075.. productionlist::
76 TokString: '"' <non-'"' characters and C-like escapes> '"'
77 TokCodeFragment: "[{" <shortest text not containing "}]"> "}]"
Sean Silva1b600182013-01-07 02:43:44 +000078
Renato Golin33f973a2014-04-01 09:51:49 +000079:token:`TokCodeFragment` is essentially a multiline string literal
80delimited by ``[{`` and ``}]``.
Sean Silvacc373352014-02-09 02:43:50 +000081
Renato Golin33f973a2014-04-01 09:51:49 +000082.. note::
83 The current implementation accepts the following C-like escapes::
Sean Silva543fd7f2013-01-09 02:20:30 +000084
Renato Golin33f973a2014-04-01 09:51:49 +000085 \\ \' \" \t \n
Sean Silva543fd7f2013-01-09 02:20:30 +000086
Renato Golin33f973a2014-04-01 09:51:49 +000087TableGen also has the following keywords::
Sean Silva1b600182013-01-07 02:43:44 +000088
Renato Golin33f973a2014-04-01 09:51:49 +000089 bit bits class code dag
90 def foreach defm field in
91 int let list multiclass string
Sean Silva1b600182013-01-07 02:43:44 +000092
Renato Golin33f973a2014-04-01 09:51:49 +000093TableGen also has "bang operators" which have a
94wide variety of meanings:
Sean Silva1b600182013-01-07 02:43:44 +000095
Renato Golin33f973a2014-04-01 09:51:49 +000096.. productionlist::
97 BangOperator: one of
98 :!eq !if !head !tail !con
Joerg Sonnenberger0a537272014-09-03 13:17:03 +000099 :!add !shl !sra !srl !and
Matt Arsenault1c8d9332016-11-15 06:49:28 +0000100 :!or !empty !subst !foreach !strconcat
Nicolai Haehnled34f6842018-03-06 13:49:16 +0000101 :!cast !listconcat !size !foldl
Nicolai Haehnleaa9ca692018-03-14 11:00:57 +0000102 :!isa !dag !le !lt !ge
103 :!gt !ne
Matt Arsenault1c8d9332016-11-15 06:49:28 +0000104
Sean Silva1b600182013-01-07 02:43:44 +0000105
Renato Golin33f973a2014-04-01 09:51:49 +0000106Syntax
107======
Sean Silva1b600182013-01-07 02:43:44 +0000108
Renato Golin33f973a2014-04-01 09:51:49 +0000109TableGen has an ``include`` mechanism. It does not play a role in the
110syntax per se, since it is lexically replaced with the contents of the
111included file.
Sean Silva1b600182013-01-07 02:43:44 +0000112
Renato Golin33f973a2014-04-01 09:51:49 +0000113.. productionlist::
114 IncludeDirective: "include" `TokString`
Sean Silva1b600182013-01-07 02:43:44 +0000115
Renato Golin33f973a2014-04-01 09:51:49 +0000116TableGen's top-level production consists of "objects".
Sean Silva1b600182013-01-07 02:43:44 +0000117
Renato Golin33f973a2014-04-01 09:51:49 +0000118.. productionlist::
119 TableGenFile: `Object`*
Nicolai Haehnlefcd65252018-03-09 12:24:42 +0000120 Object: `Class` | `Def` | `Defm` | `Defset` | `Let` | `MultiClass` |
121 `Foreach`
Sean Silva1b600182013-01-07 02:43:44 +0000122
Renato Golin33f973a2014-04-01 09:51:49 +0000123``class``\es
124------------
Sean Silva1b600182013-01-07 02:43:44 +0000125
Renato Golin33f973a2014-04-01 09:51:49 +0000126.. productionlist::
127 Class: "class" `TokIdentifier` [`TemplateArgList`] `ObjectBody`
Nicolai Haehnle01d261f2018-06-04 14:26:05 +0000128 TemplateArgList: "<" `Declaration` ("," `Declaration`)* ">"
Sean Silva1b600182013-01-07 02:43:44 +0000129
Renato Golin33f973a2014-04-01 09:51:49 +0000130A ``class`` declaration creates a record which other records can inherit
131from. A class can be parametrized by a list of "template arguments", whose
132values can be used in the class body.
Sean Silva1b600182013-01-07 02:43:44 +0000133
Renato Golin33f973a2014-04-01 09:51:49 +0000134A given class can only be defined once. A ``class`` declaration is
135considered to define the class if any of the following is true:
Sean Silva1b600182013-01-07 02:43:44 +0000136
Renato Golin33f973a2014-04-01 09:51:49 +0000137.. break ObjectBody into its consituents so that they are present here?
Sean Silva1b600182013-01-07 02:43:44 +0000138
Renato Golin33f973a2014-04-01 09:51:49 +0000139#. The :token:`TemplateArgList` is present.
140#. The :token:`Body` in the :token:`ObjectBody` is present and is not empty.
141#. The :token:`BaseClassList` in the :token:`ObjectBody` is present.
Sean Silva1b600182013-01-07 02:43:44 +0000142
Renato Golin33f973a2014-04-01 09:51:49 +0000143You can declare an empty class by giving and empty :token:`TemplateArgList`
144and an empty :token:`ObjectBody`. This can serve as a restricted form of
145forward declaration: note that records deriving from the forward-declared
146class will inherit no fields from it since the record expansion is done
147when the record is parsed.
Sean Silva1b600182013-01-07 02:43:44 +0000148
Nicolai Haehnle01d261f2018-06-04 14:26:05 +0000149Every class has an implicit template argument called ``NAME``, which is set
150to the name of the instantiating ``def`` or ``defm``. The result is undefined
151if the class is instantiated by an anonymous record.
Sean Silva1b600182013-01-07 02:43:44 +0000152
Renato Golin33f973a2014-04-01 09:51:49 +0000153Declarations
154------------
Sean Silva1b600182013-01-07 02:43:44 +0000155
Renato Golin33f973a2014-04-01 09:51:49 +0000156.. Omitting mention of arcane "field" prefix to discourage its use.
Sean Silva1b600182013-01-07 02:43:44 +0000157
Renato Golin33f973a2014-04-01 09:51:49 +0000158The declaration syntax is pretty much what you would expect as a C++
159programmer.
Sean Silva1b600182013-01-07 02:43:44 +0000160
Renato Golin33f973a2014-04-01 09:51:49 +0000161.. productionlist::
162 Declaration: `Type` `TokIdentifier` ["=" `Value`]
Sean Silva1b600182013-01-07 02:43:44 +0000163
Sylvestre Ledru7d540502016-07-02 19:28:40 +0000164It assigns the value to the identifier.
Sean Silva1b600182013-01-07 02:43:44 +0000165
Renato Golin33f973a2014-04-01 09:51:49 +0000166Types
167-----
Sean Silva1b600182013-01-07 02:43:44 +0000168
Renato Golin33f973a2014-04-01 09:51:49 +0000169.. productionlist::
170 Type: "string" | "code" | "bit" | "int" | "dag"
171 :| "bits" "<" `TokInteger` ">"
172 :| "list" "<" `Type` ">"
173 :| `ClassID`
174 ClassID: `TokIdentifier`
Sean Silva1b600182013-01-07 02:43:44 +0000175
Renato Golin33f973a2014-04-01 09:51:49 +0000176Both ``string`` and ``code`` correspond to the string type; the difference
177is purely to indicate programmer intention.
Renato Golinca105642014-03-20 16:08:34 +0000178
Renato Golin33f973a2014-04-01 09:51:49 +0000179The :token:`ClassID` must identify a class that has been previously
180declared or defined.
Renato Golinca105642014-03-20 16:08:34 +0000181
Renato Golin33f973a2014-04-01 09:51:49 +0000182Values
183------
Renato Golinca105642014-03-20 16:08:34 +0000184
Renato Golin33f973a2014-04-01 09:51:49 +0000185.. productionlist::
186 Value: `SimpleValue` `ValueSuffix`*
187 ValueSuffix: "{" `RangeList` "}"
188 :| "[" `RangeList` "]"
189 :| "." `TokIdentifier`
190 RangeList: `RangePiece` ("," `RangePiece`)*
191 RangePiece: `TokInteger`
192 :| `TokInteger` "-" `TokInteger`
193 :| `TokInteger` `TokInteger`
Renato Golinca105642014-03-20 16:08:34 +0000194
Renato Golin33f973a2014-04-01 09:51:49 +0000195The peculiar last form of :token:`RangePiece` is due to the fact that the
196"``-``" is included in the :token:`TokInteger`, hence ``1-5`` gets lexed as
197two consecutive :token:`TokInteger`'s, with values ``1`` and ``-5``,
198instead of "1", "-", and "5".
199The :token:`RangeList` can be thought of as specifying "list slice" in some
200contexts.
Renato Golinca105642014-03-20 16:08:34 +0000201
Renato Golinca105642014-03-20 16:08:34 +0000202
Renato Golin33f973a2014-04-01 09:51:49 +0000203:token:`SimpleValue` has a number of forms:
Renato Golinca105642014-03-20 16:08:34 +0000204
Renato Golinca105642014-03-20 16:08:34 +0000205
Renato Golin33f973a2014-04-01 09:51:49 +0000206.. productionlist::
207 SimpleValue: `TokIdentifier`
Renato Golinca105642014-03-20 16:08:34 +0000208
Renato Golin33f973a2014-04-01 09:51:49 +0000209The value will be the variable referenced by the identifier. It can be one
210of:
Renato Golinca105642014-03-20 16:08:34 +0000211
Renato Golin33f973a2014-04-01 09:51:49 +0000212.. The code for this is exceptionally abstruse. These examples are a
213 best-effort attempt.
Renato Golinca105642014-03-20 16:08:34 +0000214
Renato Golin33f973a2014-04-01 09:51:49 +0000215* name of a ``def``, such as the use of ``Bar`` in::
Renato Golinca105642014-03-20 16:08:34 +0000216
Renato Golin33f973a2014-04-01 09:51:49 +0000217 def Bar : SomeClass {
218 int X = 5;
219 }
Renato Golinca105642014-03-20 16:08:34 +0000220
Renato Golin33f973a2014-04-01 09:51:49 +0000221 def Foo {
222 SomeClass Baz = Bar;
223 }
Renato Golinca105642014-03-20 16:08:34 +0000224
Renato Golin33f973a2014-04-01 09:51:49 +0000225* value local to a ``def``, such as the use of ``Bar`` in::
Renato Golinca105642014-03-20 16:08:34 +0000226
Renato Golin33f973a2014-04-01 09:51:49 +0000227 def Foo {
228 int Bar = 5;
229 int Baz = Bar;
230 }
Renato Golinca105642014-03-20 16:08:34 +0000231
Nicolai Haehnlee2ef7562018-06-04 14:26:12 +0000232 Values defined in superclasses can be accessed the same way.
233
Renato Golin33f973a2014-04-01 09:51:49 +0000234* a template arg of a ``class``, such as the use of ``Bar`` in::
Renato Golinca105642014-03-20 16:08:34 +0000235
Renato Golin33f973a2014-04-01 09:51:49 +0000236 class Foo<int Bar> {
237 int Baz = Bar;
238 }
Renato Golinca105642014-03-20 16:08:34 +0000239
Nicolai Haehnlee2ef7562018-06-04 14:26:12 +0000240* value local to a ``class``, such as the use of ``Bar`` in::
Renato Golinca105642014-03-20 16:08:34 +0000241
Nicolai Haehnlee2ef7562018-06-04 14:26:12 +0000242 class Foo {
Renato Golin33f973a2014-04-01 09:51:49 +0000243 int Bar = 5;
244 int Baz = Bar;
245 }
Renato Golinca105642014-03-20 16:08:34 +0000246
Renato Golin33f973a2014-04-01 09:51:49 +0000247* a template arg to a ``multiclass``, such as the use of ``Bar`` in::
Renato Golinca105642014-03-20 16:08:34 +0000248
Renato Golin33f973a2014-04-01 09:51:49 +0000249 multiclass Foo<int Bar> {
Nicolai Haehnlee2ef7562018-06-04 14:26:12 +0000250 def : SomeClass<Bar>;
Renato Golin33f973a2014-04-01 09:51:49 +0000251 }
Renato Golinca105642014-03-20 16:08:34 +0000252
Nicolai Haehnlee2ef7562018-06-04 14:26:12 +0000253* the iteration variable of a ``foreach``, such as the use of ``i`` in::
254
255 foreach i = 0-5 in
256 def Foo#i;
257
258* a variable defined by ``defset``
259
Nicolai Haehnle01d261f2018-06-04 14:26:05 +0000260* the implicit template argument ``NAME`` in a ``class`` or ``multiclass``
261
Renato Golin33f973a2014-04-01 09:51:49 +0000262.. productionlist::
263 SimpleValue: `TokInteger`
Renato Golinca105642014-03-20 16:08:34 +0000264
Renato Golin33f973a2014-04-01 09:51:49 +0000265This represents the numeric value of the integer.
Renato Golinca105642014-03-20 16:08:34 +0000266
Renato Golin33f973a2014-04-01 09:51:49 +0000267.. productionlist::
268 SimpleValue: `TokString`+
Renato Golinca105642014-03-20 16:08:34 +0000269
Renato Golin33f973a2014-04-01 09:51:49 +0000270Multiple adjacent string literals are concatenated like in C/C++. The value
271is the concatenation of the strings.
Renato Golinca105642014-03-20 16:08:34 +0000272
Renato Golin33f973a2014-04-01 09:51:49 +0000273.. productionlist::
274 SimpleValue: `TokCodeFragment`
Renato Golinca105642014-03-20 16:08:34 +0000275
Renato Golin33f973a2014-04-01 09:51:49 +0000276The value is the string value of the code fragment.
Renato Golinca105642014-03-20 16:08:34 +0000277
Renato Golin33f973a2014-04-01 09:51:49 +0000278.. productionlist::
279 SimpleValue: "?"
Renato Golinca105642014-03-20 16:08:34 +0000280
Renato Golin33f973a2014-04-01 09:51:49 +0000281``?`` represents an "unset" initializer.
Sean Silva1b600182013-01-07 02:43:44 +0000282
Renato Golin33f973a2014-04-01 09:51:49 +0000283.. productionlist::
284 SimpleValue: "{" `ValueList` "}"
285 ValueList: [`ValueListNE`]
286 ValueListNE: `Value` ("," `Value`)*
Sean Silva1b600182013-01-07 02:43:44 +0000287
Renato Golin33f973a2014-04-01 09:51:49 +0000288This represents a sequence of bits, as would be used to initialize a
289``bits<n>`` field (where ``n`` is the number of bits).
Sean Silva1b600182013-01-07 02:43:44 +0000290
Renato Golin33f973a2014-04-01 09:51:49 +0000291.. productionlist::
292 SimpleValue: `ClassID` "<" `ValueListNE` ">"
Sean Silva1b600182013-01-07 02:43:44 +0000293
Renato Golin33f973a2014-04-01 09:51:49 +0000294This generates a new anonymous record definition (as would be created by an
295unnamed ``def`` inheriting from the given class with the given template
296arguments) and the value is the value of that record definition.
Sean Silva1b600182013-01-07 02:43:44 +0000297
Renato Golin33f973a2014-04-01 09:51:49 +0000298.. productionlist::
299 SimpleValue: "[" `ValueList` "]" ["<" `Type` ">"]
Sean Silva1b600182013-01-07 02:43:44 +0000300
Renato Golin33f973a2014-04-01 09:51:49 +0000301A list initializer. The optional :token:`Type` can be used to indicate a
302specific element type, otherwise the element type will be deduced from the
303given values.
Sean Silva1b600182013-01-07 02:43:44 +0000304
Renato Golin33f973a2014-04-01 09:51:49 +0000305.. The initial `DagArg` of the dag must start with an identifier or
306 !cast, but this is more of an implementation detail and so for now just
307 leave it out.
Sean Silva1b600182013-01-07 02:43:44 +0000308
Renato Golin33f973a2014-04-01 09:51:49 +0000309.. productionlist::
Simon Tatham047c1ab2018-04-23 09:15:47 +0000310 SimpleValue: "(" `DagArg` [`DagArgList`] ")"
Renato Golin33f973a2014-04-01 09:51:49 +0000311 DagArgList: `DagArg` ("," `DagArg`)*
312 DagArg: `Value` [":" `TokVarName`] | `TokVarName`
Sean Silva1b600182013-01-07 02:43:44 +0000313
Renato Golin33f973a2014-04-01 09:51:49 +0000314The initial :token:`DagArg` is called the "operator" of the dag.
Sean Silva1b600182013-01-07 02:43:44 +0000315
Renato Golin33f973a2014-04-01 09:51:49 +0000316.. productionlist::
317 SimpleValue: `BangOperator` ["<" `Type` ">"] "(" `ValueListNE` ")"
Sean Silva1b600182013-01-07 02:43:44 +0000318
Renato Golin33f973a2014-04-01 09:51:49 +0000319Bodies
320------
Sean Silva1b600182013-01-07 02:43:44 +0000321
Renato Golin33f973a2014-04-01 09:51:49 +0000322.. productionlist::
323 ObjectBody: `BaseClassList` `Body`
324 BaseClassList: [":" `BaseClassListNE`]
325 BaseClassListNE: `SubClassRef` ("," `SubClassRef`)*
326 SubClassRef: (`ClassID` | `MultiClassID`) ["<" `ValueList` ">"]
327 DefmID: `TokIdentifier`
Sean Silva1b600182013-01-07 02:43:44 +0000328
Renato Golin33f973a2014-04-01 09:51:49 +0000329The version with the :token:`MultiClassID` is only valid in the
330:token:`BaseClassList` of a ``defm``.
331The :token:`MultiClassID` should be the name of a ``multiclass``.
Sean Silva1b600182013-01-07 02:43:44 +0000332
Renato Golin33f973a2014-04-01 09:51:49 +0000333.. put this somewhere else
Sean Silva1b600182013-01-07 02:43:44 +0000334
Renato Golin33f973a2014-04-01 09:51:49 +0000335It is after parsing the base class list that the "let stack" is applied.
Sean Silva1b600182013-01-07 02:43:44 +0000336
Renato Golin33f973a2014-04-01 09:51:49 +0000337.. productionlist::
338 Body: ";" | "{" BodyList "}"
339 BodyList: BodyItem*
340 BodyItem: `Declaration` ";"
Simon Tatham047c1ab2018-04-23 09:15:47 +0000341 :| "let" `TokIdentifier` [ "{" `RangeList` "}" ] "=" `Value` ";"
Sean Silva1b600182013-01-07 02:43:44 +0000342
Renato Golin33f973a2014-04-01 09:51:49 +0000343The ``let`` form allows overriding the value of an inherited field.
Sean Silva1b600182013-01-07 02:43:44 +0000344
Renato Golin33f973a2014-04-01 09:51:49 +0000345``def``
346-------
Sean Silva1b600182013-01-07 02:43:44 +0000347
Renato Golin33f973a2014-04-01 09:51:49 +0000348.. productionlist::
Nicolai Haehnle01d261f2018-06-04 14:26:05 +0000349 Def: "def" [`Value`] `ObjectBody`
Sean Silva1b600182013-01-07 02:43:44 +0000350
Nicolai Haehnle01d261f2018-06-04 14:26:05 +0000351Defines a record whose name is given by the optional :token:`Value`. The value
352is parsed in a special mode where global identifiers (records and variables
353defined by ``defset``) are not recognized, and all unrecognized identifiers
354are interpreted as strings.
355
356If no name is given, the record is anonymous. The final name of anonymous
357records is undefined, but globally unique.
Sean Silva1b600182013-01-07 02:43:44 +0000358
Renato Golin33f973a2014-04-01 09:51:49 +0000359Special handling occurs if this ``def`` appears inside a ``multiclass`` or
360a ``foreach``.
Sean Silva1b600182013-01-07 02:43:44 +0000361
Nicolai Haehnle01d261f2018-06-04 14:26:05 +0000362When a non-anonymous record is defined in a multiclass and the given name
363does not contain a reference to the implicit template argument ``NAME``, such
364a reference will automatically be prepended. That is, the following are
365equivalent inside a multiclass::
366
367 def Foo;
368 def NAME#Foo;
369
Renato Golin33f973a2014-04-01 09:51:49 +0000370``defm``
371--------
Sean Silva1b600182013-01-07 02:43:44 +0000372
Renato Golin33f973a2014-04-01 09:51:49 +0000373.. productionlist::
Nicolai Haehnle01d261f2018-06-04 14:26:05 +0000374 Defm: "defm" [`Value`] ":" `BaseClassListNE` ";"
Sean Silva1b600182013-01-07 02:43:44 +0000375
Nicolai Haehnle01d261f2018-06-04 14:26:05 +0000376The :token:`BaseClassList` is a list of at least one ``multiclass`` and any
377number of ``class``'s. The ``multiclass``'s must occur before any ``class``'s.
378
379Instantiates all records defined in all given ``multiclass``'s and adds the
380given ``class``'s as superclasses.
381
382The name is parsed in the same special mode used by ``def``. If the name is
383missing, a globally unique string is used instead (but instantiated records
384are not considered to be anonymous, unless they were originally defined by an
385anonymous ``def``) That is, the following have different semantics::
386
387 defm : SomeMultiClass<...>; // some globally unique name
388 defm "" : SomeMultiClass<...>; // empty name string
389
390When it occurs inside a multiclass, the second variant is equivalent to
391``defm NAME : ...``. More generally, when ``defm`` occurs in a multiclass and
392its name does not contain a reference to the implicit template argument
393``NAME``, such a reference will automatically be prepended. That is, the
394following are equivalent inside a multiclass::
395
396 defm Foo : SomeMultiClass<...>;
397 defm NAME#Foo : SomeMultiClass<...>;
Sean Silva1b600182013-01-07 02:43:44 +0000398
Nicolai Haehnlefcd65252018-03-09 12:24:42 +0000399``defset``
400----------
401.. productionlist::
402 Defset: "defset" `Type` `TokIdentifier` "=" "{" `Object`* "}"
403
404All records defined inside the braces via ``def`` and ``defm`` are collected
405in a globally accessible list of the given name (in addition to being added
406to the global collection of records as usual). Anonymous records created inside
407initializier expressions using the ``Class<args...>`` syntax are never collected
408in a defset.
409
410The given type must be ``list<A>``, where ``A`` is some class. It is an error
411to define a record (via ``def`` or ``defm``) inside the braces which doesn't
412derive from ``A``.
413
Renato Golin33f973a2014-04-01 09:51:49 +0000414``foreach``
415-----------
Sean Silva1b600182013-01-07 02:43:44 +0000416
Renato Golin33f973a2014-04-01 09:51:49 +0000417.. productionlist::
Nicolai Haehnle8aa9d582018-03-09 12:24:30 +0000418 Foreach: "foreach" `ForeachDeclaration` "in" "{" `Object`* "}"
419 :| "foreach" `ForeachDeclaration` "in" `Object`
420 ForeachDeclaration: ID "=" ( "{" `RangeList` "}" | `RangePiece` | `Value` )
Sean Silva1b600182013-01-07 02:43:44 +0000421
Renato Golin33f973a2014-04-01 09:51:49 +0000422The value assigned to the variable in the declaration is iterated over and
423the object or object list is reevaluated with the variable set at each
424iterated value.
Sean Silva1b600182013-01-07 02:43:44 +0000425
Nicolai Haehnle8aa9d582018-03-09 12:24:30 +0000426Note that the productions involving RangeList and RangePiece have precedence
427over the more generic value parsing based on the first token.
428
Renato Golin33f973a2014-04-01 09:51:49 +0000429Top-Level ``let``
430-----------------
Sean Silva1b600182013-01-07 02:43:44 +0000431
Renato Golin33f973a2014-04-01 09:51:49 +0000432.. productionlist::
433 Let: "let" `LetList` "in" "{" `Object`* "}"
434 :| "let" `LetList` "in" `Object`
435 LetList: `LetItem` ("," `LetItem`)*
436 LetItem: `TokIdentifier` [`RangeList`] "=" `Value`
Sean Silva1b600182013-01-07 02:43:44 +0000437
Renato Golin33f973a2014-04-01 09:51:49 +0000438This is effectively equivalent to ``let`` inside the body of a record
439except that it applies to multiple records at a time. The bindings are
440applied at the end of parsing the base classes of a record.
Sean Silva1b600182013-01-07 02:43:44 +0000441
Renato Golin33f973a2014-04-01 09:51:49 +0000442``multiclass``
443--------------
Sean Silva1b600182013-01-07 02:43:44 +0000444
Renato Golin33f973a2014-04-01 09:51:49 +0000445.. productionlist::
446 MultiClass: "multiclass" `TokIdentifier` [`TemplateArgList`]
447 : [":" `BaseMultiClassList`] "{" `MultiClassObject`+ "}"
448 BaseMultiClassList: `MultiClassID` ("," `MultiClassID`)*
449 MultiClassID: `TokIdentifier`
450 MultiClassObject: `Def` | `Defm` | `Let` | `Foreach`
Vyacheslav Zakharinf7d079e2018-11-27 18:57:43 +0000451
452Preprocessing Support
453=====================
454
455TableGen's embedded preprocessor is only intended for conditional compilation.
456It supports the following directives:
457
458.. productionlist::
459 LineBegin: ^
460 LineEnd: "\n" | "\r" | EOF
461 WhiteSpace: " " | "\t"
462 CStyleComment: "/*" (.* - "*/") "*/"
463 BCPLComment: "//" (.* - `LineEnd`) `LineEnd`
464 WhiteSpaceOrCStyleComment: `WhiteSpace` | `CStyleComment`
465 WhiteSpaceOrAnyComment: `WhiteSpace` | `CStyleComment` | `BCPLComment`
466 MacroName: `ualpha` (`ualpha` | "0"..."9")*
467 PrepDefine: `LineBegin` (`WhiteSpaceOrCStyleComment`)*
468 : "#define" (`WhiteSpace`)+ `MacroName`
469 : (`WhiteSpaceOrAnyComment`)* `LineEnd`
470 PrepIfdef: `LineBegin` (`WhiteSpaceOrCStyleComment`)*
471 : "#ifdef" (`WhiteSpace`)+ `MacroName`
472 : (`WhiteSpaceOrAnyComment`)* `LineEnd`
473 PrepElse: `LineBegin` (`WhiteSpaceOrCStyleComment`)*
474 : "#else" (`WhiteSpaceOrAnyComment`)* `LineEnd`
475 PrepEndif: `LineBegin` (`WhiteSpaceOrCStyleComment`)*
476 : "#endif" (`WhiteSpaceOrAnyComment`)* `LineEnd`
477 PrepRegContentException: `PredIfdef` | `PredElse` | `PredEndif` | EOF
478 PrepRegion: .* - `PrepRegContentException`
479 :| `PrepIfDef`
480 : (`PrepRegion`)*
481 : [`PrepElse`]
482 : (`PrepRegion`)*
483 : `PrepEndif`
484
485:token:`PrepRegion` may occur anywhere in a TD file, as long as it matches
486the grammar specification.
487
488:token:`PrepDefine` allows defining a :token:`MacroName` so that any following
489:token:`PrepIfdef` - :token:`PrepElse` preprocessing region part and
490:token:`PrepIfdef` - :token:`PrepEndif` preprocessing region
491are enabled for TableGen tokens parsing.
492
493A preprocessing region, starting (i.e. having its :token:`PrepIfdef`) in a file,
494must end (i.e. have its :token:`PrepEndif`) in the same file.
495
496A :token:`MacroName` may be defined externally by using ``{ -D<NAME> }``
497option of TableGen.