blob: 59be6af3504adfb400cdc66597faa4cdae4c6b9e [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
Nicola Zaghena8967562019-03-01 09:46:29 +0000103 :!gt !ne !mul
Matt Arsenault1c8d9332016-11-15 06:49:28 +0000104
Javed Absara3e3d852019-01-25 10:25:25 +0000105TableGen also has !cond operator that needs a slightly different
106syntax compared to other "bang operators":
107
108.. productionlist::
109 CondOperator: !cond
110
Sean Silva1b600182013-01-07 02:43:44 +0000111
Renato Golin33f973a2014-04-01 09:51:49 +0000112Syntax
113======
Sean Silva1b600182013-01-07 02:43:44 +0000114
Renato Golin33f973a2014-04-01 09:51:49 +0000115TableGen has an ``include`` mechanism. It does not play a role in the
116syntax per se, since it is lexically replaced with the contents of the
117included file.
Sean Silva1b600182013-01-07 02:43:44 +0000118
Renato Golin33f973a2014-04-01 09:51:49 +0000119.. productionlist::
120 IncludeDirective: "include" `TokString`
Sean Silva1b600182013-01-07 02:43:44 +0000121
Renato Golin33f973a2014-04-01 09:51:49 +0000122TableGen's top-level production consists of "objects".
Sean Silva1b600182013-01-07 02:43:44 +0000123
Renato Golin33f973a2014-04-01 09:51:49 +0000124.. productionlist::
125 TableGenFile: `Object`*
Nicolai Haehnlefcd65252018-03-09 12:24:42 +0000126 Object: `Class` | `Def` | `Defm` | `Defset` | `Let` | `MultiClass` |
127 `Foreach`
Sean Silva1b600182013-01-07 02:43:44 +0000128
Renato Golin33f973a2014-04-01 09:51:49 +0000129``class``\es
130------------
Sean Silva1b600182013-01-07 02:43:44 +0000131
Renato Golin33f973a2014-04-01 09:51:49 +0000132.. productionlist::
133 Class: "class" `TokIdentifier` [`TemplateArgList`] `ObjectBody`
Nicolai Haehnle01d261f2018-06-04 14:26:05 +0000134 TemplateArgList: "<" `Declaration` ("," `Declaration`)* ">"
Sean Silva1b600182013-01-07 02:43:44 +0000135
Renato Golin33f973a2014-04-01 09:51:49 +0000136A ``class`` declaration creates a record which other records can inherit
137from. A class can be parametrized by a list of "template arguments", whose
138values can be used in the class body.
Sean Silva1b600182013-01-07 02:43:44 +0000139
Renato Golin33f973a2014-04-01 09:51:49 +0000140A given class can only be defined once. A ``class`` declaration is
141considered to define the class if any of the following is true:
Sean Silva1b600182013-01-07 02:43:44 +0000142
Renato Golin33f973a2014-04-01 09:51:49 +0000143.. break ObjectBody into its consituents so that they are present here?
Sean Silva1b600182013-01-07 02:43:44 +0000144
Renato Golin33f973a2014-04-01 09:51:49 +0000145#. The :token:`TemplateArgList` is present.
146#. The :token:`Body` in the :token:`ObjectBody` is present and is not empty.
147#. The :token:`BaseClassList` in the :token:`ObjectBody` is present.
Sean Silva1b600182013-01-07 02:43:44 +0000148
Javed Absara3e3d852019-01-25 10:25:25 +0000149You can declare an empty class by giving an empty :token:`TemplateArgList`
Renato Golin33f973a2014-04-01 09:51:49 +0000150and an empty :token:`ObjectBody`. This can serve as a restricted form of
151forward declaration: note that records deriving from the forward-declared
152class will inherit no fields from it since the record expansion is done
153when the record is parsed.
Sean Silva1b600182013-01-07 02:43:44 +0000154
Nicolai Haehnle01d261f2018-06-04 14:26:05 +0000155Every class has an implicit template argument called ``NAME``, which is set
156to the name of the instantiating ``def`` or ``defm``. The result is undefined
157if the class is instantiated by an anonymous record.
Sean Silva1b600182013-01-07 02:43:44 +0000158
Renato Golin33f973a2014-04-01 09:51:49 +0000159Declarations
160------------
Sean Silva1b600182013-01-07 02:43:44 +0000161
Renato Golin33f973a2014-04-01 09:51:49 +0000162.. Omitting mention of arcane "field" prefix to discourage its use.
Sean Silva1b600182013-01-07 02:43:44 +0000163
Renato Golin33f973a2014-04-01 09:51:49 +0000164The declaration syntax is pretty much what you would expect as a C++
165programmer.
Sean Silva1b600182013-01-07 02:43:44 +0000166
Renato Golin33f973a2014-04-01 09:51:49 +0000167.. productionlist::
168 Declaration: `Type` `TokIdentifier` ["=" `Value`]
Sean Silva1b600182013-01-07 02:43:44 +0000169
Sylvestre Ledru7d540502016-07-02 19:28:40 +0000170It assigns the value to the identifier.
Sean Silva1b600182013-01-07 02:43:44 +0000171
Renato Golin33f973a2014-04-01 09:51:49 +0000172Types
173-----
Sean Silva1b600182013-01-07 02:43:44 +0000174
Renato Golin33f973a2014-04-01 09:51:49 +0000175.. productionlist::
176 Type: "string" | "code" | "bit" | "int" | "dag"
177 :| "bits" "<" `TokInteger` ">"
178 :| "list" "<" `Type` ">"
179 :| `ClassID`
180 ClassID: `TokIdentifier`
Sean Silva1b600182013-01-07 02:43:44 +0000181
Renato Golin33f973a2014-04-01 09:51:49 +0000182Both ``string`` and ``code`` correspond to the string type; the difference
183is purely to indicate programmer intention.
Renato Golinca105642014-03-20 16:08:34 +0000184
Renato Golin33f973a2014-04-01 09:51:49 +0000185The :token:`ClassID` must identify a class that has been previously
186declared or defined.
Renato Golinca105642014-03-20 16:08:34 +0000187
Renato Golin33f973a2014-04-01 09:51:49 +0000188Values
189------
Renato Golinca105642014-03-20 16:08:34 +0000190
Renato Golin33f973a2014-04-01 09:51:49 +0000191.. productionlist::
192 Value: `SimpleValue` `ValueSuffix`*
193 ValueSuffix: "{" `RangeList` "}"
194 :| "[" `RangeList` "]"
195 :| "." `TokIdentifier`
196 RangeList: `RangePiece` ("," `RangePiece`)*
197 RangePiece: `TokInteger`
198 :| `TokInteger` "-" `TokInteger`
199 :| `TokInteger` `TokInteger`
Renato Golinca105642014-03-20 16:08:34 +0000200
Renato Golin33f973a2014-04-01 09:51:49 +0000201The peculiar last form of :token:`RangePiece` is due to the fact that the
202"``-``" is included in the :token:`TokInteger`, hence ``1-5`` gets lexed as
203two consecutive :token:`TokInteger`'s, with values ``1`` and ``-5``,
204instead of "1", "-", and "5".
205The :token:`RangeList` can be thought of as specifying "list slice" in some
206contexts.
Renato Golinca105642014-03-20 16:08:34 +0000207
Renato Golinca105642014-03-20 16:08:34 +0000208
Renato Golin33f973a2014-04-01 09:51:49 +0000209:token:`SimpleValue` has a number of forms:
Renato Golinca105642014-03-20 16:08:34 +0000210
Renato Golinca105642014-03-20 16:08:34 +0000211
Renato Golin33f973a2014-04-01 09:51:49 +0000212.. productionlist::
213 SimpleValue: `TokIdentifier`
Renato Golinca105642014-03-20 16:08:34 +0000214
Renato Golin33f973a2014-04-01 09:51:49 +0000215The value will be the variable referenced by the identifier. It can be one
216of:
Renato Golinca105642014-03-20 16:08:34 +0000217
Renato Golin33f973a2014-04-01 09:51:49 +0000218.. The code for this is exceptionally abstruse. These examples are a
219 best-effort attempt.
Renato Golinca105642014-03-20 16:08:34 +0000220
Renato Golin33f973a2014-04-01 09:51:49 +0000221* name of a ``def``, such as the use of ``Bar`` in::
Renato Golinca105642014-03-20 16:08:34 +0000222
Renato Golin33f973a2014-04-01 09:51:49 +0000223 def Bar : SomeClass {
224 int X = 5;
225 }
Renato Golinca105642014-03-20 16:08:34 +0000226
Renato Golin33f973a2014-04-01 09:51:49 +0000227 def Foo {
228 SomeClass Baz = Bar;
229 }
Renato Golinca105642014-03-20 16:08:34 +0000230
Renato Golin33f973a2014-04-01 09:51:49 +0000231* value local to a ``def``, such as the use of ``Bar`` in::
Renato Golinca105642014-03-20 16:08:34 +0000232
Renato Golin33f973a2014-04-01 09:51:49 +0000233 def Foo {
234 int Bar = 5;
235 int Baz = Bar;
236 }
Renato Golinca105642014-03-20 16:08:34 +0000237
Nicolai Haehnlee2ef7562018-06-04 14:26:12 +0000238 Values defined in superclasses can be accessed the same way.
239
Renato Golin33f973a2014-04-01 09:51:49 +0000240* a template arg of a ``class``, such as the use of ``Bar`` in::
Renato Golinca105642014-03-20 16:08:34 +0000241
Renato Golin33f973a2014-04-01 09:51:49 +0000242 class Foo<int Bar> {
243 int Baz = Bar;
244 }
Renato Golinca105642014-03-20 16:08:34 +0000245
Nicolai Haehnlee2ef7562018-06-04 14:26:12 +0000246* value local to a ``class``, such as the use of ``Bar`` in::
Renato Golinca105642014-03-20 16:08:34 +0000247
Nicolai Haehnlee2ef7562018-06-04 14:26:12 +0000248 class Foo {
Renato Golin33f973a2014-04-01 09:51:49 +0000249 int Bar = 5;
250 int Baz = Bar;
251 }
Renato Golinca105642014-03-20 16:08:34 +0000252
Renato Golin33f973a2014-04-01 09:51:49 +0000253* a template arg to a ``multiclass``, such as the use of ``Bar`` in::
Renato Golinca105642014-03-20 16:08:34 +0000254
Renato Golin33f973a2014-04-01 09:51:49 +0000255 multiclass Foo<int Bar> {
Nicolai Haehnlee2ef7562018-06-04 14:26:12 +0000256 def : SomeClass<Bar>;
Renato Golin33f973a2014-04-01 09:51:49 +0000257 }
Renato Golinca105642014-03-20 16:08:34 +0000258
Nicolai Haehnlee2ef7562018-06-04 14:26:12 +0000259* the iteration variable of a ``foreach``, such as the use of ``i`` in::
260
261 foreach i = 0-5 in
262 def Foo#i;
263
264* a variable defined by ``defset``
265
Nicolai Haehnle01d261f2018-06-04 14:26:05 +0000266* the implicit template argument ``NAME`` in a ``class`` or ``multiclass``
267
Renato Golin33f973a2014-04-01 09:51:49 +0000268.. productionlist::
269 SimpleValue: `TokInteger`
Renato Golinca105642014-03-20 16:08:34 +0000270
Renato Golin33f973a2014-04-01 09:51:49 +0000271This represents the numeric value of the integer.
Renato Golinca105642014-03-20 16:08:34 +0000272
Renato Golin33f973a2014-04-01 09:51:49 +0000273.. productionlist::
274 SimpleValue: `TokString`+
Renato Golinca105642014-03-20 16:08:34 +0000275
Renato Golin33f973a2014-04-01 09:51:49 +0000276Multiple adjacent string literals are concatenated like in C/C++. The value
277is the concatenation of the strings.
Renato Golinca105642014-03-20 16:08:34 +0000278
Renato Golin33f973a2014-04-01 09:51:49 +0000279.. productionlist::
280 SimpleValue: `TokCodeFragment`
Renato Golinca105642014-03-20 16:08:34 +0000281
Renato Golin33f973a2014-04-01 09:51:49 +0000282The value is the string value of the code fragment.
Renato Golinca105642014-03-20 16:08:34 +0000283
Renato Golin33f973a2014-04-01 09:51:49 +0000284.. productionlist::
285 SimpleValue: "?"
Renato Golinca105642014-03-20 16:08:34 +0000286
Renato Golin33f973a2014-04-01 09:51:49 +0000287``?`` represents an "unset" initializer.
Sean Silva1b600182013-01-07 02:43:44 +0000288
Renato Golin33f973a2014-04-01 09:51:49 +0000289.. productionlist::
290 SimpleValue: "{" `ValueList` "}"
291 ValueList: [`ValueListNE`]
292 ValueListNE: `Value` ("," `Value`)*
Sean Silva1b600182013-01-07 02:43:44 +0000293
Renato Golin33f973a2014-04-01 09:51:49 +0000294This represents a sequence of bits, as would be used to initialize a
295``bits<n>`` field (where ``n`` is the number of bits).
Sean Silva1b600182013-01-07 02:43:44 +0000296
Renato Golin33f973a2014-04-01 09:51:49 +0000297.. productionlist::
298 SimpleValue: `ClassID` "<" `ValueListNE` ">"
Sean Silva1b600182013-01-07 02:43:44 +0000299
Renato Golin33f973a2014-04-01 09:51:49 +0000300This generates a new anonymous record definition (as would be created by an
301unnamed ``def`` inheriting from the given class with the given template
302arguments) and the value is the value of that record definition.
Sean Silva1b600182013-01-07 02:43:44 +0000303
Renato Golin33f973a2014-04-01 09:51:49 +0000304.. productionlist::
305 SimpleValue: "[" `ValueList` "]" ["<" `Type` ">"]
Sean Silva1b600182013-01-07 02:43:44 +0000306
Renato Golin33f973a2014-04-01 09:51:49 +0000307A list initializer. The optional :token:`Type` can be used to indicate a
308specific element type, otherwise the element type will be deduced from the
309given values.
Sean Silva1b600182013-01-07 02:43:44 +0000310
Renato Golin33f973a2014-04-01 09:51:49 +0000311.. The initial `DagArg` of the dag must start with an identifier or
312 !cast, but this is more of an implementation detail and so for now just
313 leave it out.
Sean Silva1b600182013-01-07 02:43:44 +0000314
Renato Golin33f973a2014-04-01 09:51:49 +0000315.. productionlist::
Simon Tatham047c1ab2018-04-23 09:15:47 +0000316 SimpleValue: "(" `DagArg` [`DagArgList`] ")"
Renato Golin33f973a2014-04-01 09:51:49 +0000317 DagArgList: `DagArg` ("," `DagArg`)*
318 DagArg: `Value` [":" `TokVarName`] | `TokVarName`
Sean Silva1b600182013-01-07 02:43:44 +0000319
Renato Golin33f973a2014-04-01 09:51:49 +0000320The initial :token:`DagArg` is called the "operator" of the dag.
Sean Silva1b600182013-01-07 02:43:44 +0000321
Renato Golin33f973a2014-04-01 09:51:49 +0000322.. productionlist::
323 SimpleValue: `BangOperator` ["<" `Type` ">"] "(" `ValueListNE` ")"
Javed Absara3e3d852019-01-25 10:25:25 +0000324 :| `CondOperator` "(" `CondVal` ("," `CondVal`)* ")"
325 CondVal: `Value` ":" `Value`
Sean Silva1b600182013-01-07 02:43:44 +0000326
Renato Golin33f973a2014-04-01 09:51:49 +0000327Bodies
328------
Sean Silva1b600182013-01-07 02:43:44 +0000329
Renato Golin33f973a2014-04-01 09:51:49 +0000330.. productionlist::
331 ObjectBody: `BaseClassList` `Body`
332 BaseClassList: [":" `BaseClassListNE`]
333 BaseClassListNE: `SubClassRef` ("," `SubClassRef`)*
334 SubClassRef: (`ClassID` | `MultiClassID`) ["<" `ValueList` ">"]
335 DefmID: `TokIdentifier`
Sean Silva1b600182013-01-07 02:43:44 +0000336
Renato Golin33f973a2014-04-01 09:51:49 +0000337The version with the :token:`MultiClassID` is only valid in the
338:token:`BaseClassList` of a ``defm``.
339The :token:`MultiClassID` should be the name of a ``multiclass``.
Sean Silva1b600182013-01-07 02:43:44 +0000340
Renato Golin33f973a2014-04-01 09:51:49 +0000341.. put this somewhere else
Sean Silva1b600182013-01-07 02:43:44 +0000342
Renato Golin33f973a2014-04-01 09:51:49 +0000343It is after parsing the base class list that the "let stack" is applied.
Sean Silva1b600182013-01-07 02:43:44 +0000344
Renato Golin33f973a2014-04-01 09:51:49 +0000345.. productionlist::
346 Body: ";" | "{" BodyList "}"
347 BodyList: BodyItem*
348 BodyItem: `Declaration` ";"
Simon Tatham047c1ab2018-04-23 09:15:47 +0000349 :| "let" `TokIdentifier` [ "{" `RangeList` "}" ] "=" `Value` ";"
Sean Silva1b600182013-01-07 02:43:44 +0000350
Renato Golin33f973a2014-04-01 09:51:49 +0000351The ``let`` form allows overriding the value of an inherited field.
Sean Silva1b600182013-01-07 02:43:44 +0000352
Renato Golin33f973a2014-04-01 09:51:49 +0000353``def``
354-------
Sean Silva1b600182013-01-07 02:43:44 +0000355
Renato Golin33f973a2014-04-01 09:51:49 +0000356.. productionlist::
Nicolai Haehnle01d261f2018-06-04 14:26:05 +0000357 Def: "def" [`Value`] `ObjectBody`
Sean Silva1b600182013-01-07 02:43:44 +0000358
Nicolai Haehnle01d261f2018-06-04 14:26:05 +0000359Defines a record whose name is given by the optional :token:`Value`. The value
360is parsed in a special mode where global identifiers (records and variables
361defined by ``defset``) are not recognized, and all unrecognized identifiers
362are interpreted as strings.
363
364If no name is given, the record is anonymous. The final name of anonymous
365records is undefined, but globally unique.
Sean Silva1b600182013-01-07 02:43:44 +0000366
Renato Golin33f973a2014-04-01 09:51:49 +0000367Special handling occurs if this ``def`` appears inside a ``multiclass`` or
368a ``foreach``.
Sean Silva1b600182013-01-07 02:43:44 +0000369
Nicolai Haehnle01d261f2018-06-04 14:26:05 +0000370When a non-anonymous record is defined in a multiclass and the given name
371does not contain a reference to the implicit template argument ``NAME``, such
372a reference will automatically be prepended. That is, the following are
373equivalent inside a multiclass::
374
375 def Foo;
376 def NAME#Foo;
377
Renato Golin33f973a2014-04-01 09:51:49 +0000378``defm``
379--------
Sean Silva1b600182013-01-07 02:43:44 +0000380
Renato Golin33f973a2014-04-01 09:51:49 +0000381.. productionlist::
Nicolai Haehnle01d261f2018-06-04 14:26:05 +0000382 Defm: "defm" [`Value`] ":" `BaseClassListNE` ";"
Sean Silva1b600182013-01-07 02:43:44 +0000383
Nicolai Haehnle01d261f2018-06-04 14:26:05 +0000384The :token:`BaseClassList` is a list of at least one ``multiclass`` and any
385number of ``class``'s. The ``multiclass``'s must occur before any ``class``'s.
386
387Instantiates all records defined in all given ``multiclass``'s and adds the
388given ``class``'s as superclasses.
389
390The name is parsed in the same special mode used by ``def``. If the name is
391missing, a globally unique string is used instead (but instantiated records
392are not considered to be anonymous, unless they were originally defined by an
393anonymous ``def``) That is, the following have different semantics::
394
395 defm : SomeMultiClass<...>; // some globally unique name
396 defm "" : SomeMultiClass<...>; // empty name string
397
398When it occurs inside a multiclass, the second variant is equivalent to
399``defm NAME : ...``. More generally, when ``defm`` occurs in a multiclass and
400its name does not contain a reference to the implicit template argument
401``NAME``, such a reference will automatically be prepended. That is, the
402following are equivalent inside a multiclass::
403
404 defm Foo : SomeMultiClass<...>;
405 defm NAME#Foo : SomeMultiClass<...>;
Sean Silva1b600182013-01-07 02:43:44 +0000406
Nicolai Haehnlefcd65252018-03-09 12:24:42 +0000407``defset``
408----------
409.. productionlist::
410 Defset: "defset" `Type` `TokIdentifier` "=" "{" `Object`* "}"
411
412All records defined inside the braces via ``def`` and ``defm`` are collected
413in a globally accessible list of the given name (in addition to being added
414to the global collection of records as usual). Anonymous records created inside
415initializier expressions using the ``Class<args...>`` syntax are never collected
416in a defset.
417
418The given type must be ``list<A>``, where ``A`` is some class. It is an error
419to define a record (via ``def`` or ``defm``) inside the braces which doesn't
420derive from ``A``.
421
Renato Golin33f973a2014-04-01 09:51:49 +0000422``foreach``
423-----------
Sean Silva1b600182013-01-07 02:43:44 +0000424
Renato Golin33f973a2014-04-01 09:51:49 +0000425.. productionlist::
Nicolai Haehnle8aa9d582018-03-09 12:24:30 +0000426 Foreach: "foreach" `ForeachDeclaration` "in" "{" `Object`* "}"
427 :| "foreach" `ForeachDeclaration` "in" `Object`
428 ForeachDeclaration: ID "=" ( "{" `RangeList` "}" | `RangePiece` | `Value` )
Sean Silva1b600182013-01-07 02:43:44 +0000429
Renato Golin33f973a2014-04-01 09:51:49 +0000430The value assigned to the variable in the declaration is iterated over and
431the object or object list is reevaluated with the variable set at each
432iterated value.
Sean Silva1b600182013-01-07 02:43:44 +0000433
Nicolai Haehnle8aa9d582018-03-09 12:24:30 +0000434Note that the productions involving RangeList and RangePiece have precedence
435over the more generic value parsing based on the first token.
436
Renato Golin33f973a2014-04-01 09:51:49 +0000437Top-Level ``let``
438-----------------
Sean Silva1b600182013-01-07 02:43:44 +0000439
Renato Golin33f973a2014-04-01 09:51:49 +0000440.. productionlist::
441 Let: "let" `LetList` "in" "{" `Object`* "}"
442 :| "let" `LetList` "in" `Object`
443 LetList: `LetItem` ("," `LetItem`)*
444 LetItem: `TokIdentifier` [`RangeList`] "=" `Value`
Sean Silva1b600182013-01-07 02:43:44 +0000445
Renato Golin33f973a2014-04-01 09:51:49 +0000446This is effectively equivalent to ``let`` inside the body of a record
447except that it applies to multiple records at a time. The bindings are
448applied at the end of parsing the base classes of a record.
Sean Silva1b600182013-01-07 02:43:44 +0000449
Renato Golin33f973a2014-04-01 09:51:49 +0000450``multiclass``
451--------------
Sean Silva1b600182013-01-07 02:43:44 +0000452
Renato Golin33f973a2014-04-01 09:51:49 +0000453.. productionlist::
454 MultiClass: "multiclass" `TokIdentifier` [`TemplateArgList`]
455 : [":" `BaseMultiClassList`] "{" `MultiClassObject`+ "}"
456 BaseMultiClassList: `MultiClassID` ("," `MultiClassID`)*
457 MultiClassID: `TokIdentifier`
458 MultiClassObject: `Def` | `Defm` | `Let` | `Foreach`
Vyacheslav Zakharinf7d079e2018-11-27 18:57:43 +0000459
460Preprocessing Support
461=====================
462
463TableGen's embedded preprocessor is only intended for conditional compilation.
464It supports the following directives:
465
466.. productionlist::
467 LineBegin: ^
468 LineEnd: "\n" | "\r" | EOF
469 WhiteSpace: " " | "\t"
470 CStyleComment: "/*" (.* - "*/") "*/"
471 BCPLComment: "//" (.* - `LineEnd`) `LineEnd`
472 WhiteSpaceOrCStyleComment: `WhiteSpace` | `CStyleComment`
473 WhiteSpaceOrAnyComment: `WhiteSpace` | `CStyleComment` | `BCPLComment`
474 MacroName: `ualpha` (`ualpha` | "0"..."9")*
475 PrepDefine: `LineBegin` (`WhiteSpaceOrCStyleComment`)*
476 : "#define" (`WhiteSpace`)+ `MacroName`
477 : (`WhiteSpaceOrAnyComment`)* `LineEnd`
478 PrepIfdef: `LineBegin` (`WhiteSpaceOrCStyleComment`)*
479 : "#ifdef" (`WhiteSpace`)+ `MacroName`
480 : (`WhiteSpaceOrAnyComment`)* `LineEnd`
481 PrepElse: `LineBegin` (`WhiteSpaceOrCStyleComment`)*
482 : "#else" (`WhiteSpaceOrAnyComment`)* `LineEnd`
483 PrepEndif: `LineBegin` (`WhiteSpaceOrCStyleComment`)*
484 : "#endif" (`WhiteSpaceOrAnyComment`)* `LineEnd`
485 PrepRegContentException: `PredIfdef` | `PredElse` | `PredEndif` | EOF
486 PrepRegion: .* - `PrepRegContentException`
487 :| `PrepIfDef`
488 : (`PrepRegion`)*
489 : [`PrepElse`]
490 : (`PrepRegion`)*
491 : `PrepEndif`
492
493:token:`PrepRegion` may occur anywhere in a TD file, as long as it matches
494the grammar specification.
495
496:token:`PrepDefine` allows defining a :token:`MacroName` so that any following
497:token:`PrepIfdef` - :token:`PrepElse` preprocessing region part and
498:token:`PrepIfdef` - :token:`PrepEndif` preprocessing region
499are enabled for TableGen tokens parsing.
500
501A preprocessing region, starting (i.e. having its :token:`PrepIfdef`) in a file,
502must end (i.e. have its :token:`PrepEndif`) in the same file.
503
504A :token:`MacroName` may be defined externally by using ``{ -D<NAME> }``
505option of TableGen.