fold comparisons of gep'd alloca points with null to false,
[oota-llvm.git] / docs / TableGenFundamentals.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2                       "http://www.w3.org/TR/html4/strict.dtd">
3 <html>
4 <head>
5   <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
6   <title>TableGen Fundamentals</title>
7   <link rel="stylesheet" href="llvm.css" type="text/css">
8 </head>
9 <body>
10
11 <h1>TableGen Fundamentals</h1>
12
13 <div>
14 <ul>
15   <li><a href="#introduction">Introduction</a>
16   <ol>
17     <li><a href="#concepts">Basic concepts</a></li>
18     <li><a href="#example">An example record</a></li>
19     <li><a href="#running">Running TableGen</a></li>
20   </ol></li>
21   <li><a href="#syntax">TableGen syntax</a>
22   <ol>
23     <li><a href="#primitives">TableGen primitives</a>
24     <ol>
25       <li><a href="#comments">TableGen comments</a></li>
26       <li><a href="#types">The TableGen type system</a></li>
27       <li><a href="#values">TableGen values and expressions</a></li>
28     </ol></li>
29     <li><a href="#classesdefs">Classes and definitions</a>
30     <ol>
31       <li><a href="#valuedef">Value definitions</a></li>
32       <li><a href="#recordlet">'let' expressions</a></li>
33       <li><a href="#templateargs">Class template arguments</a></li>
34       <li><a href="#multiclass">Multiclass definitions and instances</a></li>
35     </ol></li>
36     <li><a href="#filescope">File scope entities</a>
37     <ol>
38       <li><a href="#include">File inclusion</a></li>
39       <li><a href="#globallet">'let' expressions</a></li>
40     </ol></li>
41   </ol></li>
42   <li><a href="#backends">TableGen backends</a>
43   <ol>
44     <li><a href="#">todo</a></li>
45   </ol></li>
46 </ul>
47 </div>
48
49 <div class="doc_author">
50   <p>Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a></p>
51 </div>
52
53 <!-- *********************************************************************** -->
54 <h2><a name="introduction">Introduction</a></h2>
55 <!-- *********************************************************************** -->
56
57 <div>
58
59 <p>TableGen's purpose is to help a human develop and maintain records of
60 domain-specific information.  Because there may be a large number of these
61 records, it is specifically designed to allow writing flexible descriptions and
62 for common features of these records to be factored out.  This reduces the
63 amount of duplication in the description, reduces the chance of error, and
64 makes it easier to structure domain specific information.</p>
65
66 <p>The core part of TableGen <a href="#syntax">parses a file</a>, instantiates
67 the declarations, and hands the result off to a domain-specific "<a
68 href="#backends">TableGen backend</a>" for processing.  The current major user
69 of TableGen is the <a href="CodeGenerator.html">LLVM code generator</a>.</p>
70
71 <p>Note that if you work on TableGen much, and use emacs or vim, that you can
72 find an emacs "TableGen mode" and a vim language file in the
73 <tt>llvm/utils/emacs</tt> and <tt>llvm/utils/vim</tt> directories of your LLVM
74 distribution, respectively.</p>
75
76 <!-- ======================================================================= -->
77 <h3><a name="concepts">Basic concepts</a></h3>
78
79 <div>
80
81 <p>TableGen files consist of two key parts: 'classes' and 'definitions', both
82 of which are considered 'records'.</p>
83
84 <p><b>TableGen records</b> have a unique name, a list of values, and a list of
85 superclasses.  The list of values is the main data that TableGen builds for each
86 record; it is this that holds the domain specific information for the
87 application.  The interpretation of this data is left to a specific <a
88 href="#backends">TableGen backend</a>, but the structure and format rules are
89 taken care of and are fixed by TableGen.</p>
90
91 <p><b>TableGen definitions</b> are the concrete form of 'records'.  These
92 generally do not have any undefined values, and are marked with the
93 '<tt>def</tt>' keyword.</p>
94
95 <p><b>TableGen classes</b> are abstract records that are used to build and
96 describe other records.  These 'classes' allow the end-user to build
97 abstractions for either the domain they are targeting (such as "Register",
98 "RegisterClass", and "Instruction" in the LLVM code generator) or for the
99 implementor to help factor out common properties of records (such as "FPInst",
100 which is used to represent floating point instructions in the X86 backend).
101 TableGen keeps track of all of the classes that are used to build up a
102 definition, so the backend can find all definitions of a particular class, such
103 as "Instruction".</p>
104
105 <p><b>TableGen multiclasses</b> are groups of abstract records that are
106 instantiated all at once.  Each instantiation can result in multiple
107 TableGen definitions.  If a multiclass inherits from another multiclass,
108 the definitions in the sub-multiclass become part of the current
109 multiclass, as if they were declared in the current multiclass.</p>
110
111 </div>
112
113 <!-- ======================================================================= -->
114 <h3><a name="example">An example record</a></h3>
115
116 <div>
117
118 <p>With no other arguments, TableGen parses the specified file and prints out
119 all of the classes, then all of the definitions.  This is a good way to see what
120 the various definitions expand to fully.  Running this on the <tt>X86.td</tt>
121 file prints this (at the time of this writing):</p>
122
123 <div class="doc_code">
124 <pre>
125 ...
126 <b>def</b> ADD32rr {   <i>// Instruction X86Inst I</i>
127   <b>string</b> Namespace = "X86";
128   <b>dag</b> OutOperandList = (outs GR32:$dst);
129   <b>dag</b> InOperandList = (ins GR32:$src1, GR32:$src2);
130   <b>string</b> AsmString = "add{l}\t{$src2, $dst|$dst, $src2}";
131   <b>list</b>&lt;dag&gt; Pattern = [(set GR32:$dst, (add GR32:$src1, GR32:$src2))];
132   <b>list</b>&lt;Register&gt; Uses = [];
133   <b>list</b>&lt;Register&gt; Defs = [EFLAGS];
134   <b>list</b>&lt;Predicate&gt; Predicates = [];
135   <b>int</b> CodeSize = 3;
136   <b>int</b> AddedComplexity = 0;
137   <b>bit</b> isReturn = 0;
138   <b>bit</b> isBranch = 0;
139   <b>bit</b> isIndirectBranch = 0;
140   <b>bit</b> isBarrier = 0;
141   <b>bit</b> isCall = 0;
142   <b>bit</b> canFoldAsLoad = 0;
143   <b>bit</b> mayLoad = 0;
144   <b>bit</b> mayStore = 0;
145   <b>bit</b> isImplicitDef = 0;
146   <b>bit</b> isConvertibleToThreeAddress = 1;
147   <b>bit</b> isCommutable = 1;
148   <b>bit</b> isTerminator = 0;
149   <b>bit</b> isReMaterializable = 0;
150   <b>bit</b> isPredicable = 0;
151   <b>bit</b> hasDelaySlot = 0;
152   <b>bit</b> usesCustomInserter = 0;
153   <b>bit</b> hasCtrlDep = 0;
154   <b>bit</b> isNotDuplicable = 0;
155   <b>bit</b> hasSideEffects = 0;
156   <b>bit</b> neverHasSideEffects = 0;
157   InstrItinClass Itinerary = NoItinerary;
158   <b>string</b> Constraints = "";
159   <b>string</b> DisableEncoding = "";
160   <b>bits</b>&lt;8&gt; Opcode = { 0, 0, 0, 0, 0, 0, 0, 1 };
161   Format Form = MRMDestReg;
162   <b>bits</b>&lt;6&gt; FormBits = { 0, 0, 0, 0, 1, 1 };
163   ImmType ImmT = NoImm;
164   <b>bits</b>&lt;3&gt; ImmTypeBits = { 0, 0, 0 };
165   <b>bit</b> hasOpSizePrefix = 0;
166   <b>bit</b> hasAdSizePrefix = 0;
167   <b>bits</b>&lt;4&gt; Prefix = { 0, 0, 0, 0 };
168   <b>bit</b> hasREX_WPrefix = 0;
169   FPFormat FPForm = ?;
170   <b>bits</b>&lt;3&gt; FPFormBits = { 0, 0, 0 };
171 }
172 ...
173 </pre>
174 </div>
175
176 <p>This definition corresponds to a 32-bit register-register add instruction in
177 the X86.  The string after the '<tt>def</tt>' string indicates the name of the
178 record&mdash;"<tt>ADD32rr</tt>" in this case&mdash;and the comment at the end of
179 the line indicates the superclasses of the definition.  The body of the record
180 contains all of the data that TableGen assembled for the record, indicating that
181 the instruction is part of the "X86" namespace, the pattern indicating how the
182 the instruction should be emitted into the assembly file, that it is a
183 two-address instruction, has a particular encoding, etc.  The contents and
184 semantics of the information in the record is specific to the needs of the X86
185 backend, and is only shown as an example.</p>
186
187 <p>As you can see, a lot of information is needed for every instruction
188 supported by the code generator, and specifying it all manually would be
189 unmaintainable, prone to bugs, and tiring to do in the first place.  Because we
190 are using TableGen, all of the information was derived from the following
191 definition:</p>
192
193 <div class="doc_code">
194 <pre>
195 let Defs = [EFLAGS],
196     isCommutable = 1,                  <i>// X = ADD Y,Z --&gt; X = ADD Z,Y</i>
197     isConvertibleToThreeAddress = 1 <b>in</b> <i>// Can transform into LEA.</i>
198 def ADD32rr  : I&lt;0x01, MRMDestReg, (outs GR32:$dst),
199                                    (ins GR32:$src1, GR32:$src2),
200                  "add{l}\t{$src2, $dst|$dst, $src2}",
201                  [(set GR32:$dst, (add GR32:$src1, GR32:$src2))]&gt;;
202 </pre>
203 </div>
204
205 <p>This definition makes use of the custom class <tt>I</tt> (extended from the
206 custom class <tt>X86Inst</tt>), which is defined in the X86-specific TableGen
207 file, to factor out the common features that instructions of its class share.  A
208 key feature of TableGen is that it allows the end-user to define the
209 abstractions they prefer to use when describing their information.</p>
210
211 <p>Each def record has a special entry called "NAME."  This is the
212 name of the def ("ADD32rr" above).  In the general case def names can
213 be formed from various kinds of string processing expressions and NAME
214 resolves to the final value obtained after resolving all of those
215 expressions.  The user may refer to NAME anywhere she desires to use
216 the ultimate name of the def.  NAME should not be defined anywhere
217 else in user code to avoid conflict problems.</p>
218
219 </div>
220
221 <!-- ======================================================================= -->
222 <h3><a name="running">Running TableGen</a></h3>
223
224 <div>
225
226 <p>TableGen runs just like any other LLVM tool.  The first (optional) argument
227 specifies the file to read.  If a filename is not specified, <tt>tblgen</tt>
228 reads from standard input.</p>
229
230 <p>To be useful, one of the <a href="#backends">TableGen backends</a> must be
231 used.  These backends are selectable on the command line (type '<tt>tblgen
232 -help</tt>' for a list).  For example, to get a list of all of the definitions
233 that subclass a particular type (which can be useful for building up an enum
234 list of these records), use the <tt>-print-enums</tt> option:</p>
235
236 <div class="doc_code">
237 <pre>
238 $ tblgen X86.td -print-enums -class=Register
239 AH, AL, AX, BH, BL, BP, BPL, BX, CH, CL, CX, DH, DI, DIL, DL, DX, EAX, EBP, EBX,
240 ECX, EDI, EDX, EFLAGS, EIP, ESI, ESP, FP0, FP1, FP2, FP3, FP4, FP5, FP6, IP,
241 MM0, MM1, MM2, MM3, MM4, MM5, MM6, MM7, R10, R10B, R10D, R10W, R11, R11B, R11D,
242 R11W, R12, R12B, R12D, R12W, R13, R13B, R13D, R13W, R14, R14B, R14D, R14W, R15,
243 R15B, R15D, R15W, R8, R8B, R8D, R8W, R9, R9B, R9D, R9W, RAX, RBP, RBX, RCX, RDI,
244 RDX, RIP, RSI, RSP, SI, SIL, SP, SPL, ST0, ST1, ST2, ST3, ST4, ST5, ST6, ST7,
245 XMM0, XMM1, XMM10, XMM11, XMM12, XMM13, XMM14, XMM15, XMM2, XMM3, XMM4, XMM5,
246 XMM6, XMM7, XMM8, XMM9,
247
248 $ tblgen X86.td -print-enums -class=Instruction 
249 ABS_F, ABS_Fp32, ABS_Fp64, ABS_Fp80, ADC32mi, ADC32mi8, ADC32mr, ADC32ri,
250 ADC32ri8, ADC32rm, ADC32rr, ADC64mi32, ADC64mi8, ADC64mr, ADC64ri32, ADC64ri8,
251 ADC64rm, ADC64rr, ADD16mi, ADD16mi8, ADD16mr, ADD16ri, ADD16ri8, ADD16rm,
252 ADD16rr, ADD32mi, ADD32mi8, ADD32mr, ADD32ri, ADD32ri8, ADD32rm, ADD32rr,
253 ADD64mi32, ADD64mi8, ADD64mr, ADD64ri32, ...
254 </pre>
255 </div>
256
257 <p>The default backend prints out all of the records, as described <a
258 href="#example">above</a>.</p>
259
260 <p>If you plan to use TableGen, you will most likely have to <a
261 href="#backends">write a backend</a> that extracts the information specific to
262 what you need and formats it in the appropriate way.</p>
263
264 </div>
265
266 </div>
267
268 <!-- *********************************************************************** -->
269 <h2><a name="syntax">TableGen syntax</a></h2>
270 <!-- *********************************************************************** -->
271
272 <div>
273
274 <p>TableGen doesn't care about the meaning of data (that is up to the backend to
275 define), but it does care about syntax, and it enforces a simple type system.
276 This section describes the syntax and the constructs allowed in a TableGen file.
277 </p>
278
279 <!-- ======================================================================= -->
280 <h3><a name="primitives">TableGen primitives</a></h3>
281
282 <div>
283
284 <!-- -------------------------------------------------------------------------->
285 <h4><a name="comments">TableGen comments</a></h4>
286
287 <div>
288
289 <p>TableGen supports BCPL style "<tt>//</tt>" comments, which run to the end of
290 the line, and it also supports <b>nestable</b> "<tt>/* */</tt>" comments.</p>
291
292 </div>
293
294 <!-- -------------------------------------------------------------------------->
295 <h4>
296   <a name="types">The TableGen type system</a>
297 </h4>
298
299 <div>
300
301 <p>TableGen files are strongly typed, in a simple (but complete) type-system.
302 These types are used to perform automatic conversions, check for errors, and to
303 help interface designers constrain the input that they allow.  Every <a
304 href="#valuedef">value definition</a> is required to have an associated type.
305 </p>
306
307 <p>TableGen supports a mixture of very low-level types (such as <tt>bit</tt>)
308 and very high-level types (such as <tt>dag</tt>).  This flexibility is what
309 allows it to describe a wide range of information conveniently and compactly.
310 The TableGen types are:</p>
311
312 <dl>
313 <dt><tt><b>bit</b></tt></dt>
314   <dd>A 'bit' is a boolean value that can hold either 0 or 1.</dd>
315
316 <dt><tt><b>int</b></tt></dt>
317   <dd>The 'int' type represents a simple 32-bit integer value, such as 5.</dd>
318
319 <dt><tt><b>string</b></tt></dt>
320   <dd>The 'string' type represents an ordered sequence of characters of
321   arbitrary length.</dd>
322
323 <dt><tt><b>bits</b>&lt;n&gt;</tt></dt>
324   <dd>A 'bits' type is an arbitrary, but fixed, size integer that is broken up
325   into individual bits.  This type is useful because it can handle some bits
326   being defined while others are undefined.</dd>
327
328 <dt><tt><b>list</b>&lt;ty&gt;</tt></dt>
329   <dd>This type represents a list whose elements are some other type.  The
330   contained type is arbitrary: it can even be another list type.</dd>
331
332 <dt>Class type</dt>
333   <dd>Specifying a class name in a type context means that the defined value
334   must be a subclass of the specified class.  This is useful in conjunction with
335   the <b><tt>list</tt></b> type, for example, to constrain the elements of the
336   list to a common base class (e.g., a <tt><b>list</b>&lt;Register&gt;</tt> can
337   only contain definitions derived from the "<tt>Register</tt>" class).</dd>
338
339 <dt><tt><b>dag</b></tt></dt>
340   <dd>This type represents a nestable directed graph of elements.</dd>
341
342 <dt><tt><b>code</b></tt></dt>
343   <dd>This represents a big hunk of text.  This is lexically distinct from 
344   string values because it doesn't require escapeing double quotes and other
345   common characters that occur in code.</dd>
346 </dl>
347
348 <p>To date, these types have been sufficient for describing things that
349 TableGen has been used for, but it is straight-forward to extend this list if
350 needed.</p>
351
352 </div>
353
354 <!-- -------------------------------------------------------------------------->
355 <h4>
356   <a name="values">TableGen values and expressions</a>
357 </h4>
358
359 <div>
360
361 <p>TableGen allows for a pretty reasonable number of different expression forms
362 when building up values.  These forms allow the TableGen file to be written in a
363 natural syntax and flavor for the application.  The current expression forms
364 supported include:</p>
365
366 <dl>
367 <dt><tt>?</tt></dt>
368   <dd>uninitialized field</dd>
369 <dt><tt>0b1001011</tt></dt>
370   <dd>binary integer value</dd>
371 <dt><tt>07654321</tt></dt>
372   <dd>octal integer value (indicated by a leading 0)</dd>
373 <dt><tt>7</tt></dt>
374   <dd>decimal integer value</dd>
375 <dt><tt>0x7F</tt></dt>
376   <dd>hexadecimal integer value</dd>
377 <dt><tt>"foo"</tt></dt>
378   <dd>string value</dd>
379 <dt><tt>[{ ... }]</tt></dt>
380   <dd>code fragment</dd>
381 <dt><tt>[ X, Y, Z ]&lt;type&gt;</tt></dt>
382   <dd>list value.  &lt;type&gt; is the type of the list 
383 element and is usually optional.  In rare cases,
384 TableGen is unable to deduce the element type in
385 which case the user must specify it explicitly.</dd>
386 <dt><tt>{ a, b, c }</tt></dt>
387   <dd>initializer for a "bits&lt;3&gt;" value</dd>
388 <dt><tt>value</tt></dt>
389   <dd>value reference</dd>
390 <dt><tt>value{17}</tt></dt>
391   <dd>access to one bit of a value</dd>
392 <dt><tt>value{15-17}</tt></dt>
393   <dd>access to multiple bits of a value</dd>
394 <dt><tt>DEF</tt></dt>
395   <dd>reference to a record definition</dd>
396 <dt><tt>CLASS&lt;val list&gt;</tt></dt>
397   <dd>reference to a new anonymous definition of CLASS with the specified
398       template arguments.</dd>
399 <dt><tt>X.Y</tt></dt>
400   <dd>reference to the subfield of a value</dd>
401 <dt><tt>list[4-7,17,2-3]</tt></dt>
402   <dd>A slice of the 'list' list, including elements 4,5,6,7,17,2, and 3 from
403   it.  Elements may be included multiple times.</dd>
404 <dt><tt>(DEF a, b)</tt></dt>
405   <dd>a dag value.  The first element is required to be a record definition, the
406   remaining elements in the list may be arbitrary other values, including nested
407   `<tt>dag</tt>' values.</dd>
408 <dt><tt>!strconcat(a, b)</tt></dt>
409   <dd>A string value that is the result of concatenating the 'a' and 'b'
410   strings.</dd>
411 <dt><tt>str1#str2</tt></dt>
412   <dd>"#" (paste) is a shorthand for !strconcat.  It may concatenate
413   things that are not quoted strings, in which case an implicit
414   !cast<string> is done on the operand of the paste.</dd>
415 <dt><tt>!cast&lt;type&gt;(a)</tt></dt>
416   <dd>A symbol of type <em>type</em> obtained by looking up the string 'a' in
417 the symbol table.  If the type of 'a' does not match <em>type</em>, TableGen
418 aborts with an error. !cast&lt;string&gt; is a special case in that the argument must
419 be an object defined by a 'def' construct.</dd>
420 <dt><tt>!subst(a, b, c)</tt></dt>
421   <dd>If 'a' and 'b' are of string type or are symbol references, substitute 
422 'b' for 'a' in 'c.'  This operation is analogous to $(subst) in GNU make.</dd>
423 <dt><tt>!foreach(a, b, c)</tt></dt>
424   <dd>For each member 'b' of dag or list 'a' apply operator 'c.'  'b' is a 
425 dummy variable that should be declared as a member variable of an instantiated 
426 class.  This operation is analogous to $(foreach) in GNU make.</dd>
427 <dt><tt>!head(a)</tt></dt>
428   <dd>The first element of list 'a.'</dd>
429 <dt><tt>!tail(a)</tt></dt>
430   <dd>The 2nd-N elements of list 'a.'</dd>
431 <dt><tt>!empty(a)</tt></dt>
432   <dd>An integer {0,1} indicating whether list 'a' is empty.</dd>
433 <dt><tt>!if(a,b,c)</tt></dt>
434   <dd>'b' if the result of 'int' or 'bit' operator 'a' is nonzero,
435       'c' otherwise.</dd>
436 <dt><tt>!eq(a,b)</tt></dt>
437   <dd>'bit 1' if string a is equal to string b, 0 otherwise.  This
438       only operates on string, int and bit objects.  Use !cast&lt;string&gt; to
439       compare other types of objects.</dd>
440 </dl>
441
442 <p>Note that all of the values have rules specifying how they convert to values
443 for different types.  These rules allow you to assign a value like "<tt>7</tt>"
444 to a "<tt>bits&lt;4&gt;</tt>" value, for example.</p>
445
446 </div>
447
448 </div>
449
450 <!-- ======================================================================= -->
451 <h3>
452   <a name="classesdefs">Classes and definitions</a>
453 </h3>
454
455 <div>
456
457 <p>As mentioned in the <a href="#concepts">intro</a>, classes and definitions
458 (collectively known as 'records') in TableGen are the main high-level unit of
459 information that TableGen collects.  Records are defined with a <tt>def</tt> or
460 <tt>class</tt> keyword, the record name, and an optional list of "<a
461 href="#templateargs">template arguments</a>".  If the record has superclasses,
462 they are specified as a comma separated list that starts with a colon character
463 ("<tt>:</tt>").  If <a href="#valuedef">value definitions</a> or <a
464 href="#recordlet">let expressions</a> are needed for the class, they are
465 enclosed in curly braces ("<tt>{}</tt>"); otherwise, the record ends with a
466 semicolon.</p>
467
468 <p>Here is a simple TableGen file:</p>
469
470 <div class="doc_code">
471 <pre>
472 <b>class</b> C { <b>bit</b> V = 1; }
473 <b>def</b> X : C;
474 <b>def</b> Y : C {
475   <b>string</b> Greeting = "hello";
476 }
477 </pre>
478 </div>
479
480 <p>This example defines two definitions, <tt>X</tt> and <tt>Y</tt>, both of
481 which derive from the <tt>C</tt> class.  Because of this, they both get the
482 <tt>V</tt> bit value.  The <tt>Y</tt> definition also gets the Greeting member
483 as well.</p>
484
485 <p>In general, classes are useful for collecting together the commonality
486 between a group of records and isolating it in a single place.  Also, classes
487 permit the specification of default values for their subclasses, allowing the
488 subclasses to override them as they wish.</p>
489
490 <!---------------------------------------------------------------------------->
491 <h4>
492   <a name="valuedef">Value definitions</a>
493 </h4>
494
495 <div>
496
497 <p>Value definitions define named entries in records.  A value must be defined
498 before it can be referred to as the operand for another value definition or
499 before the value is reset with a <a href="#recordlet">let expression</a>.  A
500 value is defined by specifying a <a href="#types">TableGen type</a> and a name.
501 If an initial value is available, it may be specified after the type with an
502 equal sign.  Value definitions require terminating semicolons.</p>
503
504 </div>
505
506 <!-- -------------------------------------------------------------------------->
507 <h4>
508   <a name="recordlet">'let' expressions</a>
509 </h4>
510
511 <div>
512
513 <p>A record-level let expression is used to change the value of a value
514 definition in a record.  This is primarily useful when a superclass defines a
515 value that a derived class or definition wants to override.  Let expressions
516 consist of the '<tt>let</tt>' keyword followed by a value name, an equal sign
517 ("<tt>=</tt>"), and a new value.  For example, a new class could be added to the
518 example above, redefining the <tt>V</tt> field for all of its subclasses:</p>
519
520 <div class="doc_code">
521 <pre>
522 <b>class</b> D : C { let V = 0; }
523 <b>def</b> Z : D;
524 </pre>
525 </div>
526
527 <p>In this case, the <tt>Z</tt> definition will have a zero value for its "V"
528 value, despite the fact that it derives (indirectly) from the <tt>C</tt> class,
529 because the <tt>D</tt> class overrode its value.</p>
530
531 </div>
532
533 <!-- -------------------------------------------------------------------------->
534 <h4>
535   <a name="templateargs">Class template arguments</a>
536 </h4>
537
538 <div>
539
540 <p>TableGen permits the definition of parameterized classes as well as normal
541 concrete classes.  Parameterized TableGen classes specify a list of variable
542 bindings (which may optionally have defaults) that are bound when used.  Here is
543 a simple example:</p>
544
545 <div class="doc_code">
546 <pre>
547 <b>class</b> FPFormat&lt;<b>bits</b>&lt;3&gt; val&gt; {
548   <b>bits</b>&lt;3&gt; Value = val;
549 }
550 <b>def</b> NotFP      : FPFormat&lt;0&gt;;
551 <b>def</b> ZeroArgFP  : FPFormat&lt;1&gt;;
552 <b>def</b> OneArgFP   : FPFormat&lt;2&gt;;
553 <b>def</b> OneArgFPRW : FPFormat&lt;3&gt;;
554 <b>def</b> TwoArgFP   : FPFormat&lt;4&gt;;
555 <b>def</b> CompareFP  : FPFormat&lt;5&gt;;
556 <b>def</b> CondMovFP  : FPFormat&lt;6&gt;;
557 <b>def</b> SpecialFP  : FPFormat&lt;7&gt;;
558 </pre>
559 </div>
560
561 <p>In this case, template arguments are used as a space efficient way to specify
562 a list of "enumeration values", each with a "<tt>Value</tt>" field set to the
563 specified integer.</p>
564
565 <p>The more esoteric forms of <a href="#values">TableGen expressions</a> are
566 useful in conjunction with template arguments.  As an example:</p>
567
568 <div class="doc_code">
569 <pre>
570 <b>class</b> ModRefVal&lt;<b>bits</b>&lt;2&gt; val&gt; {
571   <b>bits</b>&lt;2&gt; Value = val;
572 }
573
574 <b>def</b> None   : ModRefVal&lt;0&gt;;
575 <b>def</b> Mod    : ModRefVal&lt;1&gt;;
576 <b>def</b> Ref    : ModRefVal&lt;2&gt;;
577 <b>def</b> ModRef : ModRefVal&lt;3&gt;;
578
579 <b>class</b> Value&lt;ModRefVal MR&gt; {
580   <i>// Decode some information into a more convenient format, while providing
581   // a nice interface to the user of the "Value" class.</i>
582   <b>bit</b> isMod = MR.Value{0};
583   <b>bit</b> isRef = MR.Value{1};
584
585   <i>// other stuff...</i>
586 }
587
588 <i>// Example uses</i>
589 <b>def</b> bork : Value&lt;Mod&gt;;
590 <b>def</b> zork : Value&lt;Ref&gt;;
591 <b>def</b> hork : Value&lt;ModRef&gt;;
592 </pre>
593 </div>
594
595 <p>This is obviously a contrived example, but it shows how template arguments
596 can be used to decouple the interface provided to the user of the class from the
597 actual internal data representation expected by the class.  In this case,
598 running <tt>tblgen</tt> on the example prints the following definitions:</p>
599
600 <div class="doc_code">
601 <pre>
602 <b>def</b> bork {      <i>// Value</i>
603   <b>bit</b> isMod = 1;
604   <b>bit</b> isRef = 0;
605 }
606 <b>def</b> hork {      <i>// Value</i>
607   <b>bit</b> isMod = 1;
608   <b>bit</b> isRef = 1;
609 }
610 <b>def</b> zork {      <i>// Value</i>
611   <b>bit</b> isMod = 0;
612   <b>bit</b> isRef = 1;
613 }
614 </pre>
615 </div>
616
617 <p> This shows that TableGen was able to dig into the argument and extract a
618 piece of information that was requested by the designer of the "Value" class.
619 For more realistic examples, please see existing users of TableGen, such as the
620 X86 backend.</p>
621
622 </div>
623
624 <!-- -------------------------------------------------------------------------->
625 <h4>
626   <a name="multiclass">Multiclass definitions and instances</a>
627 </h4>
628
629 <div>
630
631 <p>
632 While classes with template arguments are a good way to factor commonality
633 between two instances of a definition, multiclasses allow a convenient notation
634 for defining multiple definitions at once (instances of implicitly constructed
635 classes).  For example, consider an 3-address instruction set whose instructions
636 come in two forms: "<tt>reg = reg op reg</tt>" and "<tt>reg = reg op imm</tt>"
637 (e.g. SPARC). In this case, you'd like to specify in one place that this
638 commonality exists, then in a separate place indicate what all the ops are.
639 </p>
640
641 <p>
642 Here is an example TableGen fragment that shows this idea:
643 </p>
644
645 <div class="doc_code">
646 <pre>
647 <b>def</b> ops;
648 <b>def</b> GPR;
649 <b>def</b> Imm;
650 <b>class</b> inst&lt;<b>int</b> opc, <b>string</b> asmstr, <b>dag</b> operandlist&gt;;
651
652 <b>multiclass</b> ri_inst&lt;<b>int</b> opc, <b>string</b> asmstr&gt; {
653   def _rr : inst&lt;opc, !strconcat(asmstr, " $dst, $src1, $src2"),
654                  (ops GPR:$dst, GPR:$src1, GPR:$src2)&gt;;
655   def _ri : inst&lt;opc, !strconcat(asmstr, " $dst, $src1, $src2"),
656                  (ops GPR:$dst, GPR:$src1, Imm:$src2)&gt;;
657 }
658
659 <i>// Instantiations of the ri_inst multiclass.</i>
660 <b>defm</b> ADD : ri_inst&lt;0b111, "add"&gt;;
661 <b>defm</b> SUB : ri_inst&lt;0b101, "sub"&gt;;
662 <b>defm</b> MUL : ri_inst&lt;0b100, "mul"&gt;;
663 ...
664 </pre>
665 </div>
666
667 <p>The name of the resultant definitions has the multidef fragment names
668    appended to them, so this defines <tt>ADD_rr</tt>, <tt>ADD_ri</tt>,
669    <tt>SUB_rr</tt>, etc.  A defm may inherit from multiple multiclasses,
670    instantiating definitions from each multiclass.  Using a multiclass
671    this way is exactly equivalent to instantiating the classes multiple
672    times yourself, e.g. by writing:</p>
673
674 <div class="doc_code">
675 <pre>
676 <b>def</b> ops;
677 <b>def</b> GPR;
678 <b>def</b> Imm;
679 <b>class</b> inst&lt;<b>int</b> opc, <b>string</b> asmstr, <b>dag</b> operandlist&gt;;
680
681 <b>class</b> rrinst&lt;<b>int</b> opc, <b>string</b> asmstr&gt;
682   : inst&lt;opc, !strconcat(asmstr, " $dst, $src1, $src2"),
683          (ops GPR:$dst, GPR:$src1, GPR:$src2)&gt;;
684
685 <b>class</b> riinst&lt;<b>int</b> opc, <b>string</b> asmstr&gt;
686   : inst&lt;opc, !strconcat(asmstr, " $dst, $src1, $src2"),
687          (ops GPR:$dst, GPR:$src1, Imm:$src2)&gt;;
688
689 <i>// Instantiations of the ri_inst multiclass.</i>
690 <b>def</b> ADD_rr : rrinst&lt;0b111, "add"&gt;;
691 <b>def</b> ADD_ri : riinst&lt;0b111, "add"&gt;;
692 <b>def</b> SUB_rr : rrinst&lt;0b101, "sub"&gt;;
693 <b>def</b> SUB_ri : riinst&lt;0b101, "sub"&gt;;
694 <b>def</b> MUL_rr : rrinst&lt;0b100, "mul"&gt;;
695 <b>def</b> MUL_ri : riinst&lt;0b100, "mul"&gt;;
696 ...
697 </pre>
698 </div>
699
700 <p>
701 A defm can also be used inside a multiclass providing several levels of
702 multiclass instanciations.
703 </p>
704
705 <div class="doc_code">
706 <pre>
707 <b>class</b> Instruction&lt;bits&lt;4&gt; opc, string Name&gt; {
708   bits&lt;4&gt; opcode = opc;
709   string name = Name;
710 }
711
712 <b>multiclass</b> basic_r&lt;bits&lt;4&gt; opc&gt; {
713   <b>def</b> rr : Instruction&lt;opc, "rr"&gt;;
714   <b>def</b> rm : Instruction&lt;opc, "rm"&gt;;
715 }
716
717 <b>multiclass</b> basic_s&lt;bits&lt;4&gt; opc&gt; {
718   <b>defm</b> SS : basic_r&lt;opc&gt;;
719   <b>defm</b> SD : basic_r&lt;opc&gt;;
720   <b>def</b> X : Instruction&lt;opc, "x"&gt;;
721 }
722
723 <b>multiclass</b> basic_p&lt;bits&lt;4&gt; opc&gt; {
724   <b>defm</b> PS : basic_r&lt;opc&gt;;
725   <b>defm</b> PD : basic_r&lt;opc&gt;;
726   <b>def</b> Y : Instruction&lt;opc, "y"&gt;;
727 }
728
729 <b>defm</b> ADD : basic_s&lt;0xf&gt;, basic_p&lt;0xf&gt;;
730 ...
731
732 <i>// Results</i>
733 <b>def</b> ADDPDrm { ...
734 <b>def</b> ADDPDrr { ...
735 <b>def</b> ADDPSrm { ...
736 <b>def</b> ADDPSrr { ...
737 <b>def</b> ADDSDrm { ...
738 <b>def</b> ADDSDrr { ...
739 <b>def</b> ADDY { ...
740 <b>def</b> ADDX { ...
741 </pre>
742 </div>
743
744 <p>
745 defm declarations can inherit from classes too, the
746 rule to follow is that the class list must start after the
747 last multiclass, and there must be at least one multiclass
748 before them.
749 </p>
750
751 <div class="doc_code">
752 <pre>
753 <b>class</b> XD { bits&lt;4&gt; Prefix = 11; }
754 <b>class</b> XS { bits&lt;4&gt; Prefix = 12; }
755
756 <b>class</b> I&lt;bits<4&gt; op> {
757   bits&lt;4&gt; opcode = op;
758 }
759
760 <b>multiclass</b> R {
761   <b>def</b> rr : I&lt;4&gt;;
762   <b>def</b> rm : I&lt;2&gt;;
763 }
764
765 <b>multiclass</b> Y {
766   <b>defm</b> SS : R, XD;
767   <b>defm</b> SD : R, XS;
768 }
769
770 <b>defm</b> Instr : Y;
771
772 <i>// Results</i>
773 <b>def</b> InstrSDrm {
774   bits&lt;4&gt; opcode = { 0, 0, 1, 0 };
775   bits&lt;4&gt; Prefix = { 1, 1, 0, 0 };
776 }
777 ...
778 <b>def</b> InstrSSrr {
779   bits&lt;4&gt; opcode = { 0, 1, 0, 0 };
780   bits&lt;4&gt; Prefix = { 1, 0, 1, 1 };
781 }
782 </pre>
783 </div>
784
785 </div>
786
787 </div>
788
789 <!-- ======================================================================= -->
790 <h3>
791   <a name="filescope">File scope entities</a>
792 </h3>
793
794 <div>
795
796 <!-- -------------------------------------------------------------------------->
797 <h4>
798   <a name="include">File inclusion</a>
799 </h4>
800
801 <div>
802 <p>TableGen supports the '<tt>include</tt>' token, which textually substitutes
803 the specified file in place of the include directive.  The filename should be
804 specified as a double quoted string immediately after the '<tt>include</tt>'
805 keyword.  Example:</p>
806
807 <div class="doc_code">
808 <pre>
809 <b>include</b> "foo.td"
810 </pre>
811 </div>
812
813 </div>
814
815 <!-- -------------------------------------------------------------------------->
816 <h4>
817   <a name="globallet">'let' expressions</a>
818 </h4>
819
820 <div>
821
822 <p>"Let" expressions at file scope are similar to <a href="#recordlet">"let"
823 expressions within a record</a>, except they can specify a value binding for
824 multiple records at a time, and may be useful in certain other cases.
825 File-scope let expressions are really just another way that TableGen allows the
826 end-user to factor out commonality from the records.</p>
827
828 <p>File-scope "let" expressions take a comma-separated list of bindings to
829 apply, and one or more records to bind the values in.  Here are some
830 examples:</p>
831
832 <div class="doc_code">
833 <pre>
834 <b>let</b> isTerminator = 1, isReturn = 1, isBarrier = 1, hasCtrlDep = 1 <b>in</b>
835   <b>def</b> RET : I&lt;0xC3, RawFrm, (outs), (ins), "ret", [(X86retflag 0)]&gt;;
836
837 <b>let</b> isCall = 1 <b>in</b>
838   <i>// All calls clobber the non-callee saved registers...</i>
839   <b>let</b> Defs = [EAX, ECX, EDX, FP0, FP1, FP2, FP3, FP4, FP5, FP6, ST0,
840               MM0, MM1, MM2, MM3, MM4, MM5, MM6, MM7,
841               XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6, XMM7, EFLAGS] <b>in</b> {
842     <b>def</b> CALLpcrel32 : Ii32&lt;0xE8, RawFrm, (outs), (ins i32imm:$dst,variable_ops),
843                            "call\t${dst:call}", []&gt;;
844     <b>def</b> CALL32r     : I&lt;0xFF, MRM2r, (outs), (ins GR32:$dst, variable_ops),
845                         "call\t{*}$dst", [(X86call GR32:$dst)]&gt;;
846     <b>def</b> CALL32m     : I&lt;0xFF, MRM2m, (outs), (ins i32mem:$dst, variable_ops),
847                         "call\t{*}$dst", []&gt;;
848   }
849 </pre>
850 </div>
851
852 <p>File-scope "let" expressions are often useful when a couple of definitions
853 need to be added to several records, and the records do not otherwise need to be
854 opened, as in the case with the <tt>CALL*</tt> instructions above.</p>
855
856 <p>It's also possible to use "let" expressions inside multiclasses, providing
857 more ways to factor out commonality from the records, specially if using
858 several levels of multiclass instanciations. This also avoids the need of using
859 "let" expressions within subsequent records inside a multiclass.</p> 
860
861 <pre class="doc_code">
862 <b>multiclass </b>basic_r&lt;bits&lt;4&gt; opc&gt; {
863   <b>let </b>Predicates = [HasSSE2] in {
864     <b>def </b>rr : Instruction&lt;opc, "rr"&gt;;
865     <b>def </b>rm : Instruction&lt;opc, "rm"&gt;;
866   }
867   <b>let </b>Predicates = [HasSSE3] in
868     <b>def </b>rx : Instruction&lt;opc, "rx"&gt;;
869 }
870
871 <b>multiclass </b>basic_ss&lt;bits&lt;4&gt; opc&gt; {
872   <b>let </b>IsDouble = 0 in
873     <b>defm </b>SS : basic_r&lt;opc&gt;;
874
875   <b>let </b>IsDouble = 1 in
876     <b>defm </b>SD : basic_r&lt;opc&gt;;
877 }
878
879 <b>defm </b>ADD : basic_ss&lt;0xf&gt;;
880 </pre>
881 </div>
882
883 </div>
884
885 </div>
886
887 <!-- *********************************************************************** -->
888 <h2><a name="codegen">Code Generator backend info</a></h2>
889 <!-- *********************************************************************** -->
890
891 <div>
892
893 <p>Expressions used by code generator to describe instructions and isel
894 patterns:</p>
895
896 <dl>
897 <dt><tt>(implicit a)</tt></dt>
898   <dd>an implicitly defined physical register.  This tells the dag instruction
899   selection emitter the input pattern's extra definitions matches implicit
900   physical register definitions.</dd>
901 </dl>
902 </div>
903
904 <!-- *********************************************************************** -->
905 <h2><a name="backends">TableGen backends</a></h2>
906 <!-- *********************************************************************** -->
907
908 <div>
909
910 <p>TODO: How they work, how to write one.  This section should not contain
911 details about any particular backend, except maybe -print-enums as an example.
912 This should highlight the APIs in <tt>TableGen/Record.h</tt>.</p>
913
914 </div>
915
916 <!-- *********************************************************************** -->
917
918 <hr>
919 <address>
920   <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
921   src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a>
922   <a href="http://validator.w3.org/check/referer"><img
923   src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"></a>
924
925   <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
926   <a href="http://llvm.org/">LLVM Compiler Infrastructure</a><br>
927   Last modified: $Date$
928 </address>
929
930 </body>
931 </html>