blob: 45904c4000ded6fa65c35a858ec5a9915d03daac [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
10 fix it, file a documentation bug, or ask about it on llvmdev.
11
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
Renato Golinca105642014-03-20 16:08:34 +000019read :doc:`the introduction <index>` first.
Sean Silva1b600182013-01-07 02:43:44 +000020
Renato Golinca105642014-03-20 16:08:34 +000021TableGen syntax
22===============
Sean Silva1b600182013-01-07 02:43:44 +000023
Renato Golinca105642014-03-20 16:08:34 +000024TableGen doesn't care about the meaning of data (that is up to the backend to
25define), but it does care about syntax, and it enforces a simple type system.
26This section describes the syntax and the constructs allowed in a TableGen file.
Sean Silva1b600182013-01-07 02:43:44 +000027
Renato Golinca105642014-03-20 16:08:34 +000028TableGen primitives
29-------------------
Sean Silva1b600182013-01-07 02:43:44 +000030
Renato Golinca105642014-03-20 16:08:34 +000031TableGen comments
32^^^^^^^^^^^^^^^^^
Sean Silva1b600182013-01-07 02:43:44 +000033
Renato Golinca105642014-03-20 16:08:34 +000034TableGen supports C++ style "``//``" comments, which run to the end of the
35line, and it also supports **nestable** "``/* */``" comments.
Sean Silva1b600182013-01-07 02:43:44 +000036
Renato Golinca105642014-03-20 16:08:34 +000037.. _TableGen type:
Sean Silva1b600182013-01-07 02:43:44 +000038
Renato Golinca105642014-03-20 16:08:34 +000039The TableGen type system
40^^^^^^^^^^^^^^^^^^^^^^^^
Sean Silva1b600182013-01-07 02:43:44 +000041
Renato Golinca105642014-03-20 16:08:34 +000042TableGen files are strongly typed, in a simple (but complete) type-system.
43These types are used to perform automatic conversions, check for errors, and to
44help interface designers constrain the input that they allow. Every `value
45definition`_ is required to have an associated type.
Sean Silva1b600182013-01-07 02:43:44 +000046
Renato Golinca105642014-03-20 16:08:34 +000047TableGen supports a mixture of very low-level types (such as ``bit``) and very
48high-level types (such as ``dag``). This flexibility is what allows it to
49describe a wide range of information conveniently and compactly. The TableGen
50types are:
Sean Silva1b600182013-01-07 02:43:44 +000051
Renato Golinca105642014-03-20 16:08:34 +000052``bit``
53 A 'bit' is a boolean value that can hold either 0 or 1.
Sean Silva1b600182013-01-07 02:43:44 +000054
Renato Golinca105642014-03-20 16:08:34 +000055``int``
56 The 'int' type represents a simple 32-bit integer value, such as 5.
Sean Silva1b600182013-01-07 02:43:44 +000057
Renato Golinca105642014-03-20 16:08:34 +000058``string``
59 The 'string' type represents an ordered sequence of characters of arbitrary
60 length.
Sean Silva1b600182013-01-07 02:43:44 +000061
Renato Golinca105642014-03-20 16:08:34 +000062``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 Silva1b600182013-01-07 02:43:44 +000066
Renato Golinca105642014-03-20 16:08:34 +000067``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 Silva1b600182013-01-07 02:43:44 +000070
Renato Golinca105642014-03-20 16:08:34 +000071Class 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 Silva1b600182013-01-07 02:43:44 +000077
Renato Golinca105642014-03-20 16:08:34 +000078``dag``
79 This type represents a nestable directed graph of elements.
Sean Silva1b600182013-01-07 02:43:44 +000080
Renato Golinca105642014-03-20 16:08:34 +000081To date, these types have been sufficient for describing things that TableGen
82has been used for, but it is straight-forward to extend this list if needed.
Sean Silvacc373352014-02-09 02:43:50 +000083
Renato Golinca105642014-03-20 16:08:34 +000084.. _TableGen expressions:
Sean Silva543fd7f2013-01-09 02:20:30 +000085
Renato Golinca105642014-03-20 16:08:34 +000086TableGen values and expressions
87^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Sean Silva543fd7f2013-01-09 02:20:30 +000088
Renato Golinca105642014-03-20 16:08:34 +000089TableGen allows for a pretty reasonable number of different expression forms
90when building up values. These forms allow the TableGen file to be written in a
91natural syntax and flavor for the application. The current expression forms
92supported include:
Sean Silva1b600182013-01-07 02:43:44 +000093
Renato Golinca105642014-03-20 16:08:34 +000094``?``
95 uninitialized field
Sean Silva1b600182013-01-07 02:43:44 +000096
Renato Golinca105642014-03-20 16:08:34 +000097``0b1001011``
98 binary integer value
Sean Silva1b600182013-01-07 02:43:44 +000099
Renato Golinca105642014-03-20 16:08:34 +0000100``07654321``
101 octal integer value (indicated by a leading 0)
Sean Silva1b600182013-01-07 02:43:44 +0000102
Renato Golinca105642014-03-20 16:08:34 +0000103``7``
104 decimal integer value
Sean Silva1b600182013-01-07 02:43:44 +0000105
Renato Golinca105642014-03-20 16:08:34 +0000106``0x7F``
107 hexadecimal integer value
Sean Silva1b600182013-01-07 02:43:44 +0000108
Renato Golinca105642014-03-20 16:08:34 +0000109``"foo"``
110 string value
Sean Silva1b600182013-01-07 02:43:44 +0000111
Renato Golinca105642014-03-20 16:08:34 +0000112``[{ ... }]``
113 usually called a "code fragment", but is just a multiline string literal
Sean Silva1b600182013-01-07 02:43:44 +0000114
Renato Golinca105642014-03-20 16:08:34 +0000115``[ 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 Silva1b600182013-01-07 02:43:44 +0000119
Renato Golinca105642014-03-20 16:08:34 +0000120``{ a, b, c }``
121 initializer for a "bits<3>" value
Sean Silva1b600182013-01-07 02:43:44 +0000122
Renato Golinca105642014-03-20 16:08:34 +0000123``value``
124 value reference
Sean Silva1b600182013-01-07 02:43:44 +0000125
Renato Golinca105642014-03-20 16:08:34 +0000126``value{17}``
127 access to one bit of a value
Sean Silva1b600182013-01-07 02:43:44 +0000128
Renato Golinca105642014-03-20 16:08:34 +0000129``value{15-17}``
130 access to multiple bits of a value
Sean Silva1b600182013-01-07 02:43:44 +0000131
Renato Golinca105642014-03-20 16:08:34 +0000132``DEF``
133 reference to a record definition
Sean Silva1b600182013-01-07 02:43:44 +0000134
Renato Golinca105642014-03-20 16:08:34 +0000135``CLASS<val list>``
136 reference to a new anonymous definition of CLASS with the specified template
137 arguments.
Sean Silva1b600182013-01-07 02:43:44 +0000138
Renato Golinca105642014-03-20 16:08:34 +0000139``X.Y``
140 reference to the subfield of a value
Sean Silva1b600182013-01-07 02:43:44 +0000141
Renato Golinca105642014-03-20 16:08:34 +0000142``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 Silva1b600182013-01-07 02:43:44 +0000145
Renato Golinca105642014-03-20 16:08:34 +0000146``foreach <var> = [ <list> ] in { <body> }``
Sean Silva1b600182013-01-07 02:43:44 +0000147
Renato Golinca105642014-03-20 16:08:34 +0000148``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 Silva1b600182013-01-07 02:43:44 +0000153
Renato Golinca105642014-03-20 16:08:34 +0000154``foreach <var> = 0-15 in ...``
Sean Silva1b600182013-01-07 02:43:44 +0000155
Renato Golinca105642014-03-20 16:08:34 +0000156``foreach <var> = {0-15,32-47} in ...``
157 Loop over ranges of integers. The braces are required for multiple ranges.
Sean Silva1b600182013-01-07 02:43:44 +0000158
Renato Golinca105642014-03-20 16:08:34 +0000159``(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 Silva1b600182013-01-07 02:43:44 +0000163
Renato Golinca105642014-03-20 16:08:34 +0000164``!strconcat(a, b)``
165 A string value that is the result of concatenating the 'a' and 'b' strings.
Sean Silva1b600182013-01-07 02:43:44 +0000166
Renato Golinca105642014-03-20 16:08:34 +0000167``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 Silva1b600182013-01-07 02:43:44 +0000171
Renato Golinca105642014-03-20 16:08:34 +0000172``!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
204Note that all of the values have rules specifying how they convert to values
205for different types. These rules allow you to assign a value like "``7``"
206to a "``bits<4>``" value, for example.
207
208Classes and definitions
209-----------------------
210
211As 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
213collects. Records are defined with a ``def`` or ``class`` keyword, the record
214name, and an optional list of "`template arguments`_". If the record has
215superclasses, they are specified as a comma separated list that starts with a
216colon character ("``:``"). If `value definitions`_ or `let expressions`_ are
217needed for the class, they are enclosed in curly braces ("``{}``"); otherwise,
218the record ends with a semicolon.
219
220Here 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
230This example defines two definitions, ``X`` and ``Y``, both of which derive from
231the ``C`` class. Because of this, they both get the ``V`` bit value. The ``Y``
232definition also gets the Greeting member as well.
233
234In general, classes are useful for collecting together the commonality between a
235group of records and isolating it in a single place. Also, classes permit the
236specification of default values for their subclasses, allowing the subclasses to
237override them as they wish.
238
239.. _value definition:
240.. _value definitions:
241
242Value definitions
243^^^^^^^^^^^^^^^^^
244
245Value definitions define named entries in records. A value must be defined
246before it can be referred to as the operand for another value definition or
247before the value is reset with a `let expression`_. A value is defined by
248specifying a `TableGen type`_ and a name. If an initial value is available, it
249may be specified after the type with an equal sign. Value definitions require
250terminating semicolons.
251
252.. _let expression:
253.. _let expressions:
254.. _"let" expressions within a record:
255
256'let' expressions
257^^^^^^^^^^^^^^^^^
258
259A record-level let expression is used to change the value of a value definition
260in a record. This is primarily useful when a superclass defines a value that a
261derived 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
263value. For example, a new class could be added to the example above, redefining
264the ``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
271In this case, the ``Z`` definition will have a zero value for its ``V`` value,
272despite the fact that it derives (indirectly) from the ``C`` class, because the
273``D`` class overrode its value.
274
275.. _template arguments:
276
277Class template arguments
278^^^^^^^^^^^^^^^^^^^^^^^^
279
280TableGen permits the definition of parameterized classes as well as normal
281concrete classes. Parameterized TableGen classes specify a list of variable
282bindings (which may optionally have defaults) that are bound when used. Here is
283a 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
299In this case, template arguments are used as a space efficient way to specify a
300list of "enumeration values", each with a "``Value``" field set to the specified
301integer.
302
303The more esoteric forms of `TableGen expressions`_ are useful in conjunction
304with 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 Silva1b600182013-01-07 02:43:44 +0000330
Renato Golinca105642014-03-20 16:08:34 +0000331This is obviously a contrived example, but it shows how template arguments can
332be used to decouple the interface provided to the user of the class from the
333actual internal data representation expected by the class. In this case,
334running ``llvm-tblgen`` on the example prints the following definitions:
Sean Silva1b600182013-01-07 02:43:44 +0000335
Renato Golinca105642014-03-20 16:08:34 +0000336.. code-block:: llvm
Sean Silva1b600182013-01-07 02:43:44 +0000337
Renato Golinca105642014-03-20 16:08:34 +0000338 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 Silva1b600182013-01-07 02:43:44 +0000350
Renato Golinca105642014-03-20 16:08:34 +0000351This shows that TableGen was able to dig into the argument and extract a piece
352of information that was requested by the designer of the "Value" class. For
353more realistic examples, please see existing users of TableGen, such as the X86
354backend.
Sean Silva1b600182013-01-07 02:43:44 +0000355
Renato Golinca105642014-03-20 16:08:34 +0000356Multiclass definitions and instances
357^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Sean Silva1b600182013-01-07 02:43:44 +0000358
Renato Golinca105642014-03-20 16:08:34 +0000359While classes with template arguments are a good way to factor commonality
360between two instances of a definition, multiclasses allow a convenient notation
361for defining multiple definitions at once (instances of implicitly constructed
362classes). For example, consider an 3-address instruction set whose instructions
363come 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
365commonality exists, then in a separate place indicate what all the ops are.
Sean Silva1b600182013-01-07 02:43:44 +0000366
Renato Golinca105642014-03-20 16:08:34 +0000367Here is an example TableGen fragment that shows this idea:
Sean Silva1b600182013-01-07 02:43:44 +0000368
Renato Golinca105642014-03-20 16:08:34 +0000369.. code-block:: llvm
Sean Silva1b600182013-01-07 02:43:44 +0000370
Renato Golinca105642014-03-20 16:08:34 +0000371 def ops;
372 def GPR;
373 def Imm;
374 class inst<int opc, string asmstr, dag operandlist>;
Sean Silva1b600182013-01-07 02:43:44 +0000375
Renato Golinca105642014-03-20 16:08:34 +0000376 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 Silva1b600182013-01-07 02:43:44 +0000382
Renato Golinca105642014-03-20 16:08:34 +0000383 // 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 Silva1b600182013-01-07 02:43:44 +0000388
Renato Golinca105642014-03-20 16:08:34 +0000389The name of the resultant definitions has the multidef fragment names appended
390to them, so this defines ``ADD_rr``, ``ADD_ri``, ``SUB_rr``, etc. A defm may
391inherit from multiple multiclasses, instantiating definitions from each
392multiclass. Using a multiclass this way is exactly equivalent to instantiating
393the classes multiple times yourself, e.g. by writing:
Sean Silva1b600182013-01-07 02:43:44 +0000394
Renato Golinca105642014-03-20 16:08:34 +0000395.. code-block:: llvm
Sean Silva1b600182013-01-07 02:43:44 +0000396
Renato Golinca105642014-03-20 16:08:34 +0000397 def ops;
398 def GPR;
399 def Imm;
400 class inst<int opc, string asmstr, dag operandlist>;
Sean Silva1b600182013-01-07 02:43:44 +0000401
Renato Golinca105642014-03-20 16:08:34 +0000402 class rrinst<int opc, string asmstr>
403 : inst<opc, !strconcat(asmstr, " $dst, $src1, $src2"),
404 (ops GPR:$dst, GPR:$src1, GPR:$src2)>;
Sean Silva1b600182013-01-07 02:43:44 +0000405
Renato Golinca105642014-03-20 16:08:34 +0000406 class riinst<int opc, string asmstr>
407 : inst<opc, !strconcat(asmstr, " $dst, $src1, $src2"),
408 (ops GPR:$dst, GPR:$src1, Imm:$src2)>;
Sean Silva1b600182013-01-07 02:43:44 +0000409
Renato Golinca105642014-03-20 16:08:34 +0000410 // 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 Silva1b600182013-01-07 02:43:44 +0000418
Renato Golinca105642014-03-20 16:08:34 +0000419A ``defm`` can also be used inside a multiclass providing several levels of
420multiclass instantiations.
Sean Silva1b600182013-01-07 02:43:44 +0000421
Renato Golinca105642014-03-20 16:08:34 +0000422.. code-block:: llvm
Sean Silva1b600182013-01-07 02:43:44 +0000423
Renato Golinca105642014-03-20 16:08:34 +0000424 class Instruction<bits<4> opc, string Name> {
425 bits<4> opcode = opc;
426 string name = Name;
427 }
Sean Silva1b600182013-01-07 02:43:44 +0000428
Renato Golinca105642014-03-20 16:08:34 +0000429 multiclass basic_r<bits<4> opc> {
430 def rr : Instruction<opc, "rr">;
431 def rm : Instruction<opc, "rm">;
432 }
Sean Silva1b600182013-01-07 02:43:44 +0000433
Renato Golinca105642014-03-20 16:08:34 +0000434 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 Silva1b600182013-01-07 02:43:44 +0000439
Renato Golinca105642014-03-20 16:08:34 +0000440 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 Silva1b600182013-01-07 02:43:44 +0000445
Renato Golinca105642014-03-20 16:08:34 +0000446 defm ADD : basic_s<0xf>, basic_p<0xf>;
447 ...
Sean Silva1b600182013-01-07 02:43:44 +0000448
Renato Golinca105642014-03-20 16:08:34 +0000449 // 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 Silva1b600182013-01-07 02:43:44 +0000458
Renato Golinca105642014-03-20 16:08:34 +0000459``defm`` declarations can inherit from classes too, the rule to follow is that
460the class list must start after the last multiclass, and there must be at least
461one multiclass before them.
Sean Silva1b600182013-01-07 02:43:44 +0000462
Renato Golinca105642014-03-20 16:08:34 +0000463.. code-block:: llvm
Sean Silva1b600182013-01-07 02:43:44 +0000464
Renato Golinca105642014-03-20 16:08:34 +0000465 class XD { bits<4> Prefix = 11; }
466 class XS { bits<4> Prefix = 12; }
Sean Silva1b600182013-01-07 02:43:44 +0000467
Renato Golinca105642014-03-20 16:08:34 +0000468 class I<bits<4> op> {
469 bits<4> opcode = op;
470 }
Sean Silva1b600182013-01-07 02:43:44 +0000471
Renato Golinca105642014-03-20 16:08:34 +0000472 multiclass R {
473 def rr : I<4>;
474 def rm : I<2>;
475 }
Sean Silva1b600182013-01-07 02:43:44 +0000476
Renato Golinca105642014-03-20 16:08:34 +0000477 multiclass Y {
478 defm SS : R, XD;
479 defm SD : R, XS;
480 }
Sean Silva1b600182013-01-07 02:43:44 +0000481
Renato Golinca105642014-03-20 16:08:34 +0000482 defm Instr : Y;
Sean Silva1b600182013-01-07 02:43:44 +0000483
Renato Golinca105642014-03-20 16:08:34 +0000484 // 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 Silva1b600182013-01-07 02:43:44 +0000494
Renato Golinca105642014-03-20 16:08:34 +0000495File scope entities
496-------------------
Sean Silva1b600182013-01-07 02:43:44 +0000497
Renato Golinca105642014-03-20 16:08:34 +0000498File inclusion
499^^^^^^^^^^^^^^
Sean Silva1b600182013-01-07 02:43:44 +0000500
Renato Golinca105642014-03-20 16:08:34 +0000501TableGen supports the '``include``' token, which textually substitutes the
502specified file in place of the include directive. The filename should be
503specified as a double quoted string immediately after the '``include``' keyword.
504Example:
Sean Silva1b600182013-01-07 02:43:44 +0000505
Renato Golinca105642014-03-20 16:08:34 +0000506.. code-block:: llvm
Sean Silva1b600182013-01-07 02:43:44 +0000507
Renato Golinca105642014-03-20 16:08:34 +0000508 include "foo.td"
Sean Silva1b600182013-01-07 02:43:44 +0000509
Renato Golinca105642014-03-20 16:08:34 +0000510'let' expressions
511^^^^^^^^^^^^^^^^^
Sean Silva1b600182013-01-07 02:43:44 +0000512
Renato Golinca105642014-03-20 16:08:34 +0000513"Let" expressions at file scope are similar to `"let" expressions within a
514record`_, except they can specify a value binding for multiple records at a
515time, and may be useful in certain other cases. File-scope let expressions are
516really just another way that TableGen allows the end-user to factor out
517commonality from the records.
Sean Silva1b600182013-01-07 02:43:44 +0000518
Renato Golinca105642014-03-20 16:08:34 +0000519File-scope "let" expressions take a comma-separated list of bindings to apply,
520and one or more records to bind the values in. Here are some examples:
Sean Silva1b600182013-01-07 02:43:44 +0000521
Renato Golinca105642014-03-20 16:08:34 +0000522.. code-block:: llvm
Sean Silva1b600182013-01-07 02:43:44 +0000523
Renato Golinca105642014-03-20 16:08:34 +0000524 let isTerminator = 1, isReturn = 1, isBarrier = 1, hasCtrlDep = 1 in
525 def RET : I<0xC3, RawFrm, (outs), (ins), "ret", [(X86retflag 0)]>;
Sean Silva1b600182013-01-07 02:43:44 +0000526
Renato Golinca105642014-03-20 16:08:34 +0000527 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 Silva1b600182013-01-07 02:43:44 +0000539
Renato Golinca105642014-03-20 16:08:34 +0000540File-scope "let" expressions are often useful when a couple of definitions need
541to be added to several records, and the records do not otherwise need to be
542opened, as in the case with the ``CALL*`` instructions above.
Sean Silva1b600182013-01-07 02:43:44 +0000543
Renato Golinca105642014-03-20 16:08:34 +0000544It's also possible to use "let" expressions inside multiclasses, providing more
545ways to factor out commonality from the records, specially if using several
546levels of multiclass instantiations. This also avoids the need of using "let"
547expressions within subsequent records inside a multiclass.
Sean Silva1b600182013-01-07 02:43:44 +0000548
Renato Golinca105642014-03-20 16:08:34 +0000549.. code-block:: llvm
Sean Silva1b600182013-01-07 02:43:44 +0000550
Renato Golinca105642014-03-20 16:08:34 +0000551 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 Silva1b600182013-01-07 02:43:44 +0000559
Renato Golinca105642014-03-20 16:08:34 +0000560 multiclass basic_ss<bits<4> opc> {
561 let IsDouble = 0 in
562 defm SS : basic_r<opc>;
Sean Silva1b600182013-01-07 02:43:44 +0000563
Renato Golinca105642014-03-20 16:08:34 +0000564 let IsDouble = 1 in
565 defm SD : basic_r<opc>;
566 }
Sean Silva1b600182013-01-07 02:43:44 +0000567
Renato Golinca105642014-03-20 16:08:34 +0000568 defm ADD : basic_ss<0xf>;
Sean Silva1b600182013-01-07 02:43:44 +0000569
Renato Golinca105642014-03-20 16:08:34 +0000570Looping
571^^^^^^^
Sean Silva1b600182013-01-07 02:43:44 +0000572
Renato Golinca105642014-03-20 16:08:34 +0000573TableGen supports the '``foreach``' block, which textually replicates the loop
574body, substituting iterator values for iterator references in the body.
575Example:
Sean Silva1b600182013-01-07 02:43:44 +0000576
Renato Golinca105642014-03-20 16:08:34 +0000577.. code-block:: llvm
Sean Silva1b600182013-01-07 02:43:44 +0000578
Renato Golinca105642014-03-20 16:08:34 +0000579 foreach i = [0, 1, 2, 3] in {
580 def R#i : Register<...>;
581 def F#i : Register<...>;
582 }
Sean Silva1b600182013-01-07 02:43:44 +0000583
Renato Golinca105642014-03-20 16:08:34 +0000584This will create objects ``R0``, ``R1``, ``R2`` and ``R3``. ``foreach`` blocks
585may be nested. If there is only one item in the body the braces may be
586elided:
Sean Silva1b600182013-01-07 02:43:44 +0000587
Renato Golinca105642014-03-20 16:08:34 +0000588.. code-block:: llvm
Sean Silva1b600182013-01-07 02:43:44 +0000589
Renato Golinca105642014-03-20 16:08:34 +0000590 foreach i = [0, 1, 2, 3] in
591 def R#i : Register<...>;
Sean Silva1b600182013-01-07 02:43:44 +0000592
Renato Golinca105642014-03-20 16:08:34 +0000593Code Generator backend info
594===========================
Sean Silva1b600182013-01-07 02:43:44 +0000595
Renato Golinca105642014-03-20 16:08:34 +0000596Expressions used by code generator to describe instructions and isel patterns:
Sean Silva1b600182013-01-07 02:43:44 +0000597
Renato Golinca105642014-03-20 16:08:34 +0000598``(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 Silva1b600182013-01-07 02:43:44 +0000602