LangRef.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. ===========================
  2. TableGen Language Reference
  3. ===========================
  4. .. contents::
  5. :local:
  6. .. warning::
  7. This document is extremely rough. If you find something lacking, please
  8. fix it, file a documentation bug, or ask about it on llvm-dev.
  9. Introduction
  10. ============
  11. This document is meant to be a normative spec about the TableGen language
  12. in and of itself (i.e. how to understand a given construct in terms of how
  13. it affects the final set of records represented by the TableGen file). If
  14. you are unsure if this document is really what you are looking for, please
  15. read the :doc:`introduction to TableGen <index>` first.
  16. Notation
  17. ========
  18. The lexical and syntax notation used here is intended to imitate
  19. `Python's`_. In particular, for lexical definitions, the productions
  20. operate at the character level and there is no implied whitespace between
  21. elements. The syntax definitions operate at the token level, so there is
  22. implied whitespace between tokens.
  23. .. _`Python's`: http://docs.python.org/py3k/reference/introduction.html#notation
  24. Lexical Analysis
  25. ================
  26. TableGen supports BCPL (``// ...``) and nestable C-style (``/* ... */``)
  27. comments.
  28. The following is a listing of the basic punctuation tokens::
  29. - + [ ] { } ( ) < > : ; . = ? #
  30. Numeric literals take one of the following forms:
  31. .. TableGen actually will lex some pretty strange sequences an interpret
  32. them as numbers. What is shown here is an attempt to approximate what it
  33. "should" accept.
  34. .. productionlist::
  35. TokInteger: `DecimalInteger` | `HexInteger` | `BinInteger`
  36. DecimalInteger: ["+" | "-"] ("0"..."9")+
  37. HexInteger: "0x" ("0"..."9" | "a"..."f" | "A"..."F")+
  38. BinInteger: "0b" ("0" | "1")+
  39. One aspect to note is that the :token:`DecimalInteger` token *includes* the
  40. ``+`` or ``-``, as opposed to having ``+`` and ``-`` be unary operators as
  41. most languages do.
  42. Also note that :token:`BinInteger` creates a value of type ``bits<n>``
  43. (where ``n`` is the number of bits). This will implicitly convert to
  44. integers when needed.
  45. TableGen has identifier-like tokens:
  46. .. productionlist::
  47. ualpha: "a"..."z" | "A"..."Z" | "_"
  48. TokIdentifier: ("0"..."9")* `ualpha` (`ualpha` | "0"..."9")*
  49. TokVarName: "$" `ualpha` (`ualpha` | "0"..."9")*
  50. Note that unlike most languages, TableGen allows :token:`TokIdentifier` to
  51. begin with a number. In case of ambiguity, a token will be interpreted as a
  52. numeric literal rather than an identifier.
  53. TableGen also has two string-like literals:
  54. .. productionlist::
  55. TokString: '"' <non-'"' characters and C-like escapes> '"'
  56. TokCodeFragment: "[{" <shortest text not containing "}]"> "}]"
  57. :token:`TokCodeFragment` is essentially a multiline string literal
  58. delimited by ``[{`` and ``}]``.
  59. .. note::
  60. The current implementation accepts the following C-like escapes::
  61. \\ \' \" \t \n
  62. TableGen also has the following keywords::
  63. bit bits class code dag
  64. def foreach defm field in
  65. int let list multiclass string
  66. TableGen also has "bang operators" which have a
  67. wide variety of meanings:
  68. .. productionlist::
  69. BangOperator: one of
  70. :!eq !if !head !tail !con
  71. :!add !shl !sra !srl !and
  72. :!cast !empty !subst !foreach !listconcat !strconcat
  73. Syntax
  74. ======
  75. TableGen has an ``include`` mechanism. It does not play a role in the
  76. syntax per se, since it is lexically replaced with the contents of the
  77. included file.
  78. .. productionlist::
  79. IncludeDirective: "include" `TokString`
  80. TableGen's top-level production consists of "objects".
  81. .. productionlist::
  82. TableGenFile: `Object`*
  83. Object: `Class` | `Def` | `Defm` | `Let` | `MultiClass` | `Foreach`
  84. ``class``\es
  85. ------------
  86. .. productionlist::
  87. Class: "class" `TokIdentifier` [`TemplateArgList`] `ObjectBody`
  88. A ``class`` declaration creates a record which other records can inherit
  89. from. A class can be parametrized by a list of "template arguments", whose
  90. values can be used in the class body.
  91. A given class can only be defined once. A ``class`` declaration is
  92. considered to define the class if any of the following is true:
  93. .. break ObjectBody into its consituents so that they are present here?
  94. #. The :token:`TemplateArgList` is present.
  95. #. The :token:`Body` in the :token:`ObjectBody` is present and is not empty.
  96. #. The :token:`BaseClassList` in the :token:`ObjectBody` is present.
  97. You can declare an empty class by giving and empty :token:`TemplateArgList`
  98. and an empty :token:`ObjectBody`. This can serve as a restricted form of
  99. forward declaration: note that records deriving from the forward-declared
  100. class will inherit no fields from it since the record expansion is done
  101. when the record is parsed.
  102. .. productionlist::
  103. TemplateArgList: "<" `Declaration` ("," `Declaration`)* ">"
  104. Declarations
  105. ------------
  106. .. Omitting mention of arcane "field" prefix to discourage its use.
  107. The declaration syntax is pretty much what you would expect as a C++
  108. programmer.
  109. .. productionlist::
  110. Declaration: `Type` `TokIdentifier` ["=" `Value`]
  111. It assigns the value to the identifer.
  112. Types
  113. -----
  114. .. productionlist::
  115. Type: "string" | "code" | "bit" | "int" | "dag"
  116. :| "bits" "<" `TokInteger` ">"
  117. :| "list" "<" `Type` ">"
  118. :| `ClassID`
  119. ClassID: `TokIdentifier`
  120. Both ``string`` and ``code`` correspond to the string type; the difference
  121. is purely to indicate programmer intention.
  122. The :token:`ClassID` must identify a class that has been previously
  123. declared or defined.
  124. Values
  125. ------
  126. .. productionlist::
  127. Value: `SimpleValue` `ValueSuffix`*
  128. ValueSuffix: "{" `RangeList` "}"
  129. :| "[" `RangeList` "]"
  130. :| "." `TokIdentifier`
  131. RangeList: `RangePiece` ("," `RangePiece`)*
  132. RangePiece: `TokInteger`
  133. :| `TokInteger` "-" `TokInteger`
  134. :| `TokInteger` `TokInteger`
  135. The peculiar last form of :token:`RangePiece` is due to the fact that the
  136. "``-``" is included in the :token:`TokInteger`, hence ``1-5`` gets lexed as
  137. two consecutive :token:`TokInteger`'s, with values ``1`` and ``-5``,
  138. instead of "1", "-", and "5".
  139. The :token:`RangeList` can be thought of as specifying "list slice" in some
  140. contexts.
  141. :token:`SimpleValue` has a number of forms:
  142. .. productionlist::
  143. SimpleValue: `TokIdentifier`
  144. The value will be the variable referenced by the identifier. It can be one
  145. of:
  146. .. The code for this is exceptionally abstruse. These examples are a
  147. best-effort attempt.
  148. * name of a ``def``, such as the use of ``Bar`` in::
  149. def Bar : SomeClass {
  150. int X = 5;
  151. }
  152. def Foo {
  153. SomeClass Baz = Bar;
  154. }
  155. * value local to a ``def``, such as the use of ``Bar`` in::
  156. def Foo {
  157. int Bar = 5;
  158. int Baz = Bar;
  159. }
  160. * a template arg of a ``class``, such as the use of ``Bar`` in::
  161. class Foo<int Bar> {
  162. int Baz = Bar;
  163. }
  164. * value local to a ``multiclass``, such as the use of ``Bar`` in::
  165. multiclass Foo {
  166. int Bar = 5;
  167. int Baz = Bar;
  168. }
  169. * a template arg to a ``multiclass``, such as the use of ``Bar`` in::
  170. multiclass Foo<int Bar> {
  171. int Baz = Bar;
  172. }
  173. .. productionlist::
  174. SimpleValue: `TokInteger`
  175. This represents the numeric value of the integer.
  176. .. productionlist::
  177. SimpleValue: `TokString`+
  178. Multiple adjacent string literals are concatenated like in C/C++. The value
  179. is the concatenation of the strings.
  180. .. productionlist::
  181. SimpleValue: `TokCodeFragment`
  182. The value is the string value of the code fragment.
  183. .. productionlist::
  184. SimpleValue: "?"
  185. ``?`` represents an "unset" initializer.
  186. .. productionlist::
  187. SimpleValue: "{" `ValueList` "}"
  188. ValueList: [`ValueListNE`]
  189. ValueListNE: `Value` ("," `Value`)*
  190. This represents a sequence of bits, as would be used to initialize a
  191. ``bits<n>`` field (where ``n`` is the number of bits).
  192. .. productionlist::
  193. SimpleValue: `ClassID` "<" `ValueListNE` ">"
  194. This generates a new anonymous record definition (as would be created by an
  195. unnamed ``def`` inheriting from the given class with the given template
  196. arguments) and the value is the value of that record definition.
  197. .. productionlist::
  198. SimpleValue: "[" `ValueList` "]" ["<" `Type` ">"]
  199. A list initializer. The optional :token:`Type` can be used to indicate a
  200. specific element type, otherwise the element type will be deduced from the
  201. given values.
  202. .. The initial `DagArg` of the dag must start with an identifier or
  203. !cast, but this is more of an implementation detail and so for now just
  204. leave it out.
  205. .. productionlist::
  206. SimpleValue: "(" `DagArg` `DagArgList` ")"
  207. DagArgList: `DagArg` ("," `DagArg`)*
  208. DagArg: `Value` [":" `TokVarName`] | `TokVarName`
  209. The initial :token:`DagArg` is called the "operator" of the dag.
  210. .. productionlist::
  211. SimpleValue: `BangOperator` ["<" `Type` ">"] "(" `ValueListNE` ")"
  212. Bodies
  213. ------
  214. .. productionlist::
  215. ObjectBody: `BaseClassList` `Body`
  216. BaseClassList: [":" `BaseClassListNE`]
  217. BaseClassListNE: `SubClassRef` ("," `SubClassRef`)*
  218. SubClassRef: (`ClassID` | `MultiClassID`) ["<" `ValueList` ">"]
  219. DefmID: `TokIdentifier`
  220. The version with the :token:`MultiClassID` is only valid in the
  221. :token:`BaseClassList` of a ``defm``.
  222. The :token:`MultiClassID` should be the name of a ``multiclass``.
  223. .. put this somewhere else
  224. It is after parsing the base class list that the "let stack" is applied.
  225. .. productionlist::
  226. Body: ";" | "{" BodyList "}"
  227. BodyList: BodyItem*
  228. BodyItem: `Declaration` ";"
  229. :| "let" `TokIdentifier` [`RangeList`] "=" `Value` ";"
  230. The ``let`` form allows overriding the value of an inherited field.
  231. ``def``
  232. -------
  233. .. TODO::
  234. There can be pastes in the names here, like ``#NAME#``. Look into that
  235. and document it (it boils down to ParseIDValue with IDParseMode ==
  236. ParseNameMode). ParseObjectName calls into the general ParseValue, with
  237. the only different from "arbitrary expression parsing" being IDParseMode
  238. == Mode.
  239. .. productionlist::
  240. Def: "def" `TokIdentifier` `ObjectBody`
  241. Defines a record whose name is given by the :token:`TokIdentifier`. The
  242. fields of the record are inherited from the base classes and defined in the
  243. body.
  244. Special handling occurs if this ``def`` appears inside a ``multiclass`` or
  245. a ``foreach``.
  246. ``defm``
  247. --------
  248. .. productionlist::
  249. Defm: "defm" `TokIdentifier` ":" `BaseClassListNE` ";"
  250. Note that in the :token:`BaseClassList`, all of the ``multiclass``'s must
  251. precede any ``class``'s that appear.
  252. ``foreach``
  253. -----------
  254. .. productionlist::
  255. Foreach: "foreach" `Declaration` "in" "{" `Object`* "}"
  256. :| "foreach" `Declaration` "in" `Object`
  257. The value assigned to the variable in the declaration is iterated over and
  258. the object or object list is reevaluated with the variable set at each
  259. iterated value.
  260. Top-Level ``let``
  261. -----------------
  262. .. productionlist::
  263. Let: "let" `LetList` "in" "{" `Object`* "}"
  264. :| "let" `LetList` "in" `Object`
  265. LetList: `LetItem` ("," `LetItem`)*
  266. LetItem: `TokIdentifier` [`RangeList`] "=" `Value`
  267. This is effectively equivalent to ``let`` inside the body of a record
  268. except that it applies to multiple records at a time. The bindings are
  269. applied at the end of parsing the base classes of a record.
  270. ``multiclass``
  271. --------------
  272. .. productionlist::
  273. MultiClass: "multiclass" `TokIdentifier` [`TemplateArgList`]
  274. : [":" `BaseMultiClassList`] "{" `MultiClassObject`+ "}"
  275. BaseMultiClassList: `MultiClassID` ("," `MultiClassID`)*
  276. MultiClassID: `TokIdentifier`
  277. MultiClassObject: `Def` | `Defm` | `Let` | `Foreach`