Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 1 | =========================== |
| 2 | TableGen Language Reference |
| 3 | =========================== |
| 4 | |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 5 | .. contents:: |
| 6 | :local: |
| 7 | |
| 8 | .. warning:: |
| 9 | This document is extremely rough. If you find something lacking, please |
| 10 | fix it, file a documentation bug, or ask about it on llvmdev. |
| 11 | |
| 12 | Introduction |
| 13 | ============ |
| 14 | |
| 15 | This document is meant to be a normative spec about the TableGen language |
| 16 | in and of itself (i.e. how to understand a given construct in terms of how |
| 17 | it affects the final set of records represented by the TableGen file). If |
| 18 | you are unsure if this document is really what you are looking for, please |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 19 | read :doc:`the introduction <index>` first. |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 20 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 21 | TableGen syntax |
| 22 | =============== |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 23 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 24 | TableGen doesn't care about the meaning of data (that is up to the backend to |
| 25 | define), but it does care about syntax, and it enforces a simple type system. |
| 26 | This section describes the syntax and the constructs allowed in a TableGen file. |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 27 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 28 | TableGen primitives |
| 29 | ------------------- |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 30 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 31 | TableGen comments |
| 32 | ^^^^^^^^^^^^^^^^^ |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 33 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 34 | TableGen supports C++ style "``//``" comments, which run to the end of the |
| 35 | line, and it also supports **nestable** "``/* */``" comments. |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 36 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 37 | .. _TableGen type: |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 38 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 39 | The TableGen type system |
| 40 | ^^^^^^^^^^^^^^^^^^^^^^^^ |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 41 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 42 | TableGen files are strongly typed, in a simple (but complete) type-system. |
| 43 | These types are used to perform automatic conversions, check for errors, and to |
| 44 | help interface designers constrain the input that they allow. Every `value |
| 45 | definition`_ is required to have an associated type. |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 46 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 47 | TableGen supports a mixture of very low-level types (such as ``bit``) and very |
| 48 | high-level types (such as ``dag``). This flexibility is what allows it to |
| 49 | describe a wide range of information conveniently and compactly. The TableGen |
| 50 | types are: |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 51 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 52 | ``bit`` |
| 53 | A 'bit' is a boolean value that can hold either 0 or 1. |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 54 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 55 | ``int`` |
| 56 | The 'int' type represents a simple 32-bit integer value, such as 5. |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 57 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 58 | ``string`` |
| 59 | The 'string' type represents an ordered sequence of characters of arbitrary |
| 60 | length. |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 61 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 62 | ``bits<n>`` |
| 63 | A 'bits' type is an arbitrary, but fixed, size integer that is broken up |
| 64 | into individual bits. This type is useful because it can handle some bits |
| 65 | being defined while others are undefined. |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 66 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 67 | ``list<ty>`` |
| 68 | This type represents a list whose elements are some other type. The |
| 69 | contained type is arbitrary: it can even be another list type. |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 70 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 71 | Class type |
| 72 | Specifying a class name in a type context means that the defined value must |
| 73 | be a subclass of the specified class. This is useful in conjunction with |
| 74 | the ``list`` type, for example, to constrain the elements of the list to a |
| 75 | common base class (e.g., a ``list<Register>`` can only contain definitions |
| 76 | derived from the "``Register``" class). |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 77 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 78 | ``dag`` |
| 79 | This type represents a nestable directed graph of elements. |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 80 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 81 | To date, these types have been sufficient for describing things that TableGen |
| 82 | has been used for, but it is straight-forward to extend this list if needed. |
Sean Silva | cc37335 | 2014-02-09 02:43:50 +0000 | [diff] [blame] | 83 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 84 | .. _TableGen expressions: |
Sean Silva | 543fd7f | 2013-01-09 02:20:30 +0000 | [diff] [blame] | 85 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 86 | TableGen values and expressions |
| 87 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
Sean Silva | 543fd7f | 2013-01-09 02:20:30 +0000 | [diff] [blame] | 88 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 89 | TableGen allows for a pretty reasonable number of different expression forms |
| 90 | when building up values. These forms allow the TableGen file to be written in a |
| 91 | natural syntax and flavor for the application. The current expression forms |
| 92 | supported include: |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 93 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 94 | ``?`` |
| 95 | uninitialized field |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 96 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 97 | ``0b1001011`` |
| 98 | binary integer value |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 99 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 100 | ``07654321`` |
| 101 | octal integer value (indicated by a leading 0) |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 102 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 103 | ``7`` |
| 104 | decimal integer value |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 105 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 106 | ``0x7F`` |
| 107 | hexadecimal integer value |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 108 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 109 | ``"foo"`` |
| 110 | string value |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 111 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 112 | ``[{ ... }]`` |
| 113 | usually called a "code fragment", but is just a multiline string literal |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 114 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 115 | ``[ X, Y, Z ]<type>`` |
| 116 | list value. <type> is the type of the list element and is usually optional. |
| 117 | In rare cases, TableGen is unable to deduce the element type in which case |
| 118 | the user must specify it explicitly. |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 119 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 120 | ``{ a, b, c }`` |
| 121 | initializer for a "bits<3>" value |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 122 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 123 | ``value`` |
| 124 | value reference |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 125 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 126 | ``value{17}`` |
| 127 | access to one bit of a value |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 128 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 129 | ``value{15-17}`` |
| 130 | access to multiple bits of a value |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 131 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 132 | ``DEF`` |
| 133 | reference to a record definition |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 134 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 135 | ``CLASS<val list>`` |
| 136 | reference to a new anonymous definition of CLASS with the specified template |
| 137 | arguments. |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 138 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 139 | ``X.Y`` |
| 140 | reference to the subfield of a value |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 141 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 142 | ``list[4-7,17,2-3]`` |
| 143 | A slice of the 'list' list, including elements 4,5,6,7,17,2, and 3 from it. |
| 144 | Elements may be included multiple times. |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 145 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 146 | ``foreach <var> = [ <list> ] in { <body> }`` |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 147 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 148 | ``foreach <var> = [ <list> ] in <def>`` |
| 149 | Replicate <body> or <def>, replacing instances of <var> with each value |
| 150 | in <list>. <var> is scoped at the level of the ``foreach`` loop and must |
| 151 | not conflict with any other object introduced in <body> or <def>. Currently |
| 152 | only ``def``\s are expanded within <body>. |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 153 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 154 | ``foreach <var> = 0-15 in ...`` |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 155 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 156 | ``foreach <var> = {0-15,32-47} in ...`` |
| 157 | Loop over ranges of integers. The braces are required for multiple ranges. |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 158 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 159 | ``(DEF a, b)`` |
| 160 | a dag value. The first element is required to be a record definition, the |
| 161 | remaining elements in the list may be arbitrary other values, including |
| 162 | nested ```dag``' values. |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 163 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 164 | ``!strconcat(a, b)`` |
| 165 | A string value that is the result of concatenating the 'a' and 'b' strings. |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 166 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 167 | ``str1#str2`` |
| 168 | "#" (paste) is a shorthand for !strconcat. It may concatenate things that |
| 169 | are not quoted strings, in which case an implicit !cast<string> is done on |
| 170 | the operand of the paste. |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 171 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 172 | ``!cast<type>(a)`` |
| 173 | A symbol of type *type* obtained by looking up the string 'a' in the symbol |
| 174 | table. If the type of 'a' does not match *type*, TableGen aborts with an |
| 175 | error. !cast<string> is a special case in that the argument must be an |
| 176 | object defined by a 'def' construct. |
| 177 | |
| 178 | ``!subst(a, b, c)`` |
| 179 | If 'a' and 'b' are of string type or are symbol references, substitute 'b' |
| 180 | for 'a' in 'c.' This operation is analogous to $(subst) in GNU make. |
| 181 | |
| 182 | ``!foreach(a, b, c)`` |
| 183 | For each member 'b' of dag or list 'a' apply operator 'c.' 'b' is a dummy |
| 184 | variable that should be declared as a member variable of an instantiated |
| 185 | class. This operation is analogous to $(foreach) in GNU make. |
| 186 | |
| 187 | ``!head(a)`` |
| 188 | The first element of list 'a.' |
| 189 | |
| 190 | ``!tail(a)`` |
| 191 | The 2nd-N elements of list 'a.' |
| 192 | |
| 193 | ``!empty(a)`` |
| 194 | An integer {0,1} indicating whether list 'a' is empty. |
| 195 | |
| 196 | ``!if(a,b,c)`` |
| 197 | 'b' if the result of 'int' or 'bit' operator 'a' is nonzero, 'c' otherwise. |
| 198 | |
| 199 | ``!eq(a,b)`` |
| 200 | 'bit 1' if string a is equal to string b, 0 otherwise. This only operates |
| 201 | on string, int and bit objects. Use !cast<string> to compare other types of |
| 202 | objects. |
| 203 | |
| 204 | Note that all of the values have rules specifying how they convert to values |
| 205 | for different types. These rules allow you to assign a value like "``7``" |
| 206 | to a "``bits<4>``" value, for example. |
| 207 | |
| 208 | Classes and definitions |
| 209 | ----------------------- |
| 210 | |
| 211 | As mentioned in the :doc:`introduction <index>`, classes and definitions (collectively known as |
| 212 | 'records') in TableGen are the main high-level unit of information that TableGen |
| 213 | collects. Records are defined with a ``def`` or ``class`` keyword, the record |
| 214 | name, and an optional list of "`template arguments`_". If the record has |
| 215 | superclasses, they are specified as a comma separated list that starts with a |
| 216 | colon character ("``:``"). If `value definitions`_ or `let expressions`_ are |
| 217 | needed for the class, they are enclosed in curly braces ("``{}``"); otherwise, |
| 218 | the record ends with a semicolon. |
| 219 | |
| 220 | Here is a simple TableGen file: |
| 221 | |
| 222 | .. code-block:: llvm |
| 223 | |
| 224 | class C { bit V = 1; } |
| 225 | def X : C; |
| 226 | def Y : C { |
| 227 | string Greeting = "hello"; |
| 228 | } |
| 229 | |
| 230 | This example defines two definitions, ``X`` and ``Y``, both of which derive from |
| 231 | the ``C`` class. Because of this, they both get the ``V`` bit value. The ``Y`` |
| 232 | definition also gets the Greeting member as well. |
| 233 | |
| 234 | In general, classes are useful for collecting together the commonality between a |
| 235 | group of records and isolating it in a single place. Also, classes permit the |
| 236 | specification of default values for their subclasses, allowing the subclasses to |
| 237 | override them as they wish. |
| 238 | |
| 239 | .. _value definition: |
| 240 | .. _value definitions: |
| 241 | |
| 242 | Value definitions |
| 243 | ^^^^^^^^^^^^^^^^^ |
| 244 | |
| 245 | Value definitions define named entries in records. A value must be defined |
| 246 | before it can be referred to as the operand for another value definition or |
| 247 | before the value is reset with a `let expression`_. A value is defined by |
| 248 | specifying a `TableGen type`_ and a name. If an initial value is available, it |
| 249 | may be specified after the type with an equal sign. Value definitions require |
| 250 | terminating semicolons. |
| 251 | |
| 252 | .. _let expression: |
| 253 | .. _let expressions: |
| 254 | .. _"let" expressions within a record: |
| 255 | |
| 256 | 'let' expressions |
| 257 | ^^^^^^^^^^^^^^^^^ |
| 258 | |
| 259 | A record-level let expression is used to change the value of a value definition |
| 260 | in a record. This is primarily useful when a superclass defines a value that a |
| 261 | derived class or definition wants to override. Let expressions consist of the |
| 262 | '``let``' keyword followed by a value name, an equal sign ("``=``"), and a new |
| 263 | value. For example, a new class could be added to the example above, redefining |
| 264 | the ``V`` field for all of its subclasses: |
| 265 | |
| 266 | .. code-block:: llvm |
| 267 | |
| 268 | class D : C { let V = 0; } |
| 269 | def Z : D; |
| 270 | |
| 271 | In this case, the ``Z`` definition will have a zero value for its ``V`` value, |
| 272 | despite the fact that it derives (indirectly) from the ``C`` class, because the |
| 273 | ``D`` class overrode its value. |
| 274 | |
| 275 | .. _template arguments: |
| 276 | |
| 277 | Class template arguments |
| 278 | ^^^^^^^^^^^^^^^^^^^^^^^^ |
| 279 | |
| 280 | TableGen permits the definition of parameterized classes as well as normal |
| 281 | concrete classes. Parameterized TableGen classes specify a list of variable |
| 282 | bindings (which may optionally have defaults) that are bound when used. Here is |
| 283 | a simple example: |
| 284 | |
| 285 | .. code-block:: llvm |
| 286 | |
| 287 | class FPFormat<bits<3> val> { |
| 288 | bits<3> Value = val; |
| 289 | } |
| 290 | def NotFP : FPFormat<0>; |
| 291 | def ZeroArgFP : FPFormat<1>; |
| 292 | def OneArgFP : FPFormat<2>; |
| 293 | def OneArgFPRW : FPFormat<3>; |
| 294 | def TwoArgFP : FPFormat<4>; |
| 295 | def CompareFP : FPFormat<5>; |
| 296 | def CondMovFP : FPFormat<6>; |
| 297 | def SpecialFP : FPFormat<7>; |
| 298 | |
| 299 | In this case, template arguments are used as a space efficient way to specify a |
| 300 | list of "enumeration values", each with a "``Value``" field set to the specified |
| 301 | integer. |
| 302 | |
| 303 | The more esoteric forms of `TableGen expressions`_ are useful in conjunction |
| 304 | with template arguments. As an example: |
| 305 | |
| 306 | .. code-block:: llvm |
| 307 | |
| 308 | class ModRefVal<bits<2> val> { |
| 309 | bits<2> Value = val; |
| 310 | } |
| 311 | |
| 312 | def None : ModRefVal<0>; |
| 313 | def Mod : ModRefVal<1>; |
| 314 | def Ref : ModRefVal<2>; |
| 315 | def ModRef : ModRefVal<3>; |
| 316 | |
| 317 | class Value<ModRefVal MR> { |
| 318 | // Decode some information into a more convenient format, while providing |
| 319 | // a nice interface to the user of the "Value" class. |
| 320 | bit isMod = MR.Value{0}; |
| 321 | bit isRef = MR.Value{1}; |
| 322 | |
| 323 | // other stuff... |
| 324 | } |
| 325 | |
| 326 | // Example uses |
| 327 | def bork : Value<Mod>; |
| 328 | def zork : Value<Ref>; |
| 329 | def hork : Value<ModRef>; |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 330 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 331 | This is obviously a contrived example, but it shows how template arguments can |
| 332 | be used to decouple the interface provided to the user of the class from the |
| 333 | actual internal data representation expected by the class. In this case, |
| 334 | running ``llvm-tblgen`` on the example prints the following definitions: |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 335 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 336 | .. code-block:: llvm |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 337 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 338 | def bork { // Value |
| 339 | bit isMod = 1; |
| 340 | bit isRef = 0; |
| 341 | } |
| 342 | def hork { // Value |
| 343 | bit isMod = 1; |
| 344 | bit isRef = 1; |
| 345 | } |
| 346 | def zork { // Value |
| 347 | bit isMod = 0; |
| 348 | bit isRef = 1; |
| 349 | } |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 350 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 351 | This shows that TableGen was able to dig into the argument and extract a piece |
| 352 | of information that was requested by the designer of the "Value" class. For |
| 353 | more realistic examples, please see existing users of TableGen, such as the X86 |
| 354 | backend. |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 355 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 356 | Multiclass definitions and instances |
| 357 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 358 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 359 | While classes with template arguments are a good way to factor commonality |
| 360 | between two instances of a definition, multiclasses allow a convenient notation |
| 361 | for defining multiple definitions at once (instances of implicitly constructed |
| 362 | classes). For example, consider an 3-address instruction set whose instructions |
| 363 | come in two forms: "``reg = reg op reg``" and "``reg = reg op imm``" |
| 364 | (e.g. SPARC). In this case, you'd like to specify in one place that this |
| 365 | commonality exists, then in a separate place indicate what all the ops are. |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 366 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 367 | Here is an example TableGen fragment that shows this idea: |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 368 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 369 | .. code-block:: llvm |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 370 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 371 | def ops; |
| 372 | def GPR; |
| 373 | def Imm; |
| 374 | class inst<int opc, string asmstr, dag operandlist>; |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 375 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 376 | multiclass ri_inst<int opc, string asmstr> { |
| 377 | def _rr : inst<opc, !strconcat(asmstr, " $dst, $src1, $src2"), |
| 378 | (ops GPR:$dst, GPR:$src1, GPR:$src2)>; |
| 379 | def _ri : inst<opc, !strconcat(asmstr, " $dst, $src1, $src2"), |
| 380 | (ops GPR:$dst, GPR:$src1, Imm:$src2)>; |
| 381 | } |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 382 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 383 | // Instantiations of the ri_inst multiclass. |
| 384 | defm ADD : ri_inst<0b111, "add">; |
| 385 | defm SUB : ri_inst<0b101, "sub">; |
| 386 | defm MUL : ri_inst<0b100, "mul">; |
| 387 | ... |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 388 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 389 | The name of the resultant definitions has the multidef fragment names appended |
| 390 | to them, so this defines ``ADD_rr``, ``ADD_ri``, ``SUB_rr``, etc. A defm may |
| 391 | inherit from multiple multiclasses, instantiating definitions from each |
| 392 | multiclass. Using a multiclass this way is exactly equivalent to instantiating |
| 393 | the classes multiple times yourself, e.g. by writing: |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 394 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 395 | .. code-block:: llvm |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 396 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 397 | def ops; |
| 398 | def GPR; |
| 399 | def Imm; |
| 400 | class inst<int opc, string asmstr, dag operandlist>; |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 401 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 402 | class rrinst<int opc, string asmstr> |
| 403 | : inst<opc, !strconcat(asmstr, " $dst, $src1, $src2"), |
| 404 | (ops GPR:$dst, GPR:$src1, GPR:$src2)>; |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 405 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 406 | class riinst<int opc, string asmstr> |
| 407 | : inst<opc, !strconcat(asmstr, " $dst, $src1, $src2"), |
| 408 | (ops GPR:$dst, GPR:$src1, Imm:$src2)>; |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 409 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 410 | // Instantiations of the ri_inst multiclass. |
| 411 | def ADD_rr : rrinst<0b111, "add">; |
| 412 | def ADD_ri : riinst<0b111, "add">; |
| 413 | def SUB_rr : rrinst<0b101, "sub">; |
| 414 | def SUB_ri : riinst<0b101, "sub">; |
| 415 | def MUL_rr : rrinst<0b100, "mul">; |
| 416 | def MUL_ri : riinst<0b100, "mul">; |
| 417 | ... |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 418 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 419 | A ``defm`` can also be used inside a multiclass providing several levels of |
| 420 | multiclass instantiations. |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 421 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 422 | .. code-block:: llvm |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 423 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 424 | class Instruction<bits<4> opc, string Name> { |
| 425 | bits<4> opcode = opc; |
| 426 | string name = Name; |
| 427 | } |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 428 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 429 | multiclass basic_r<bits<4> opc> { |
| 430 | def rr : Instruction<opc, "rr">; |
| 431 | def rm : Instruction<opc, "rm">; |
| 432 | } |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 433 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 434 | multiclass basic_s<bits<4> opc> { |
| 435 | defm SS : basic_r<opc>; |
| 436 | defm SD : basic_r<opc>; |
| 437 | def X : Instruction<opc, "x">; |
| 438 | } |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 439 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 440 | multiclass basic_p<bits<4> opc> { |
| 441 | defm PS : basic_r<opc>; |
| 442 | defm PD : basic_r<opc>; |
| 443 | def Y : Instruction<opc, "y">; |
| 444 | } |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 445 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 446 | defm ADD : basic_s<0xf>, basic_p<0xf>; |
| 447 | ... |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 448 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 449 | // Results |
| 450 | def ADDPDrm { ... |
| 451 | def ADDPDrr { ... |
| 452 | def ADDPSrm { ... |
| 453 | def ADDPSrr { ... |
| 454 | def ADDSDrm { ... |
| 455 | def ADDSDrr { ... |
| 456 | def ADDY { ... |
| 457 | def ADDX { ... |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 458 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 459 | ``defm`` declarations can inherit from classes too, the rule to follow is that |
| 460 | the class list must start after the last multiclass, and there must be at least |
| 461 | one multiclass before them. |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 462 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 463 | .. code-block:: llvm |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 464 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 465 | class XD { bits<4> Prefix = 11; } |
| 466 | class XS { bits<4> Prefix = 12; } |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 467 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 468 | class I<bits<4> op> { |
| 469 | bits<4> opcode = op; |
| 470 | } |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 471 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 472 | multiclass R { |
| 473 | def rr : I<4>; |
| 474 | def rm : I<2>; |
| 475 | } |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 476 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 477 | multiclass Y { |
| 478 | defm SS : R, XD; |
| 479 | defm SD : R, XS; |
| 480 | } |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 481 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 482 | defm Instr : Y; |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 483 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 484 | // Results |
| 485 | def InstrSDrm { |
| 486 | bits<4> opcode = { 0, 0, 1, 0 }; |
| 487 | bits<4> Prefix = { 1, 1, 0, 0 }; |
| 488 | } |
| 489 | ... |
| 490 | def InstrSSrr { |
| 491 | bits<4> opcode = { 0, 1, 0, 0 }; |
| 492 | bits<4> Prefix = { 1, 0, 1, 1 }; |
| 493 | } |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 494 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 495 | File scope entities |
| 496 | ------------------- |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 497 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 498 | File inclusion |
| 499 | ^^^^^^^^^^^^^^ |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 500 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 501 | TableGen supports the '``include``' token, which textually substitutes the |
| 502 | specified file in place of the include directive. The filename should be |
| 503 | specified as a double quoted string immediately after the '``include``' keyword. |
| 504 | Example: |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 505 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 506 | .. code-block:: llvm |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 507 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 508 | include "foo.td" |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 509 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 510 | 'let' expressions |
| 511 | ^^^^^^^^^^^^^^^^^ |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 512 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 513 | "Let" expressions at file scope are similar to `"let" expressions within a |
| 514 | record`_, except they can specify a value binding for multiple records at a |
| 515 | time, and may be useful in certain other cases. File-scope let expressions are |
| 516 | really just another way that TableGen allows the end-user to factor out |
| 517 | commonality from the records. |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 518 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 519 | File-scope "let" expressions take a comma-separated list of bindings to apply, |
| 520 | and one or more records to bind the values in. Here are some examples: |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 521 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 522 | .. code-block:: llvm |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 523 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 524 | let isTerminator = 1, isReturn = 1, isBarrier = 1, hasCtrlDep = 1 in |
| 525 | def RET : I<0xC3, RawFrm, (outs), (ins), "ret", [(X86retflag 0)]>; |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 526 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 527 | let isCall = 1 in |
| 528 | // All calls clobber the non-callee saved registers... |
| 529 | let Defs = [EAX, ECX, EDX, FP0, FP1, FP2, FP3, FP4, FP5, FP6, ST0, |
| 530 | MM0, MM1, MM2, MM3, MM4, MM5, MM6, MM7, |
| 531 | XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6, XMM7, EFLAGS] in { |
| 532 | def CALLpcrel32 : Ii32<0xE8, RawFrm, (outs), (ins i32imm:$dst,variable_ops), |
| 533 | "call\t${dst:call}", []>; |
| 534 | def CALL32r : I<0xFF, MRM2r, (outs), (ins GR32:$dst, variable_ops), |
| 535 | "call\t{*}$dst", [(X86call GR32:$dst)]>; |
| 536 | def CALL32m : I<0xFF, MRM2m, (outs), (ins i32mem:$dst, variable_ops), |
| 537 | "call\t{*}$dst", []>; |
| 538 | } |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 539 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 540 | File-scope "let" expressions are often useful when a couple of definitions need |
| 541 | to be added to several records, and the records do not otherwise need to be |
| 542 | opened, as in the case with the ``CALL*`` instructions above. |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 543 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 544 | It's also possible to use "let" expressions inside multiclasses, providing more |
| 545 | ways to factor out commonality from the records, specially if using several |
| 546 | levels of multiclass instantiations. This also avoids the need of using "let" |
| 547 | expressions within subsequent records inside a multiclass. |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 548 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 549 | .. code-block:: llvm |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 550 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 551 | multiclass basic_r<bits<4> opc> { |
| 552 | let Predicates = [HasSSE2] in { |
| 553 | def rr : Instruction<opc, "rr">; |
| 554 | def rm : Instruction<opc, "rm">; |
| 555 | } |
| 556 | let Predicates = [HasSSE3] in |
| 557 | def rx : Instruction<opc, "rx">; |
| 558 | } |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 559 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 560 | multiclass basic_ss<bits<4> opc> { |
| 561 | let IsDouble = 0 in |
| 562 | defm SS : basic_r<opc>; |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 563 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 564 | let IsDouble = 1 in |
| 565 | defm SD : basic_r<opc>; |
| 566 | } |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 567 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 568 | defm ADD : basic_ss<0xf>; |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 569 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 570 | Looping |
| 571 | ^^^^^^^ |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 572 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 573 | TableGen supports the '``foreach``' block, which textually replicates the loop |
| 574 | body, substituting iterator values for iterator references in the body. |
| 575 | Example: |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 576 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 577 | .. code-block:: llvm |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 578 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 579 | foreach i = [0, 1, 2, 3] in { |
| 580 | def R#i : Register<...>; |
| 581 | def F#i : Register<...>; |
| 582 | } |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 583 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 584 | This will create objects ``R0``, ``R1``, ``R2`` and ``R3``. ``foreach`` blocks |
| 585 | may be nested. If there is only one item in the body the braces may be |
| 586 | elided: |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 587 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 588 | .. code-block:: llvm |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 589 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 590 | foreach i = [0, 1, 2, 3] in |
| 591 | def R#i : Register<...>; |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 592 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 593 | Code Generator backend info |
| 594 | =========================== |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 595 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 596 | Expressions used by code generator to describe instructions and isel patterns: |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 597 | |
Renato Golin | ca10564 | 2014-03-20 16:08:34 +0000 | [diff] [blame] | 598 | ``(implicit a)`` |
| 599 | an implicitly defined physical register. This tells the dag instruction |
| 600 | selection emitter the input pattern's extra definitions matches implicit |
| 601 | physical register definitions. |
Sean Silva | 1b60018 | 2013-01-07 02:43:44 +0000 | [diff] [blame] | 602 | |