Removed extra href close tag
[llvm-complete.git] / docs / TableGenFundamentals.html
blob0d04eff96bfc1df41f9afcc1f02b528876601ce9
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
3 <html>
4 <head>
5 <title>TableGen Fundamentals</title>
6 <link rel="stylesheet" href="llvm.css" type="text/css">
7 </head>
8 <body>
10 <div class="doc_title">TableGen Fundamentals</div>
12 <div class="doc_text">
13 <ul>
14 <li><a href="#introduction">Introduction</a>
15 <ol>
16 <li><a href="#concepts">Basic concepts</a></li>
17 <li><a href="#example">An example record</a></li>
18 <li><a href="#running">Running TableGen</a></li>
19 </ol></li>
20 <li><a href="#syntax">TableGen syntax</a>
21 <ol>
22 <li><a href="#primitives">TableGen primitives</a>
23 <ol>
24 <li><a href="#comments">TableGen comments</a></li>
25 <li><a href="#types">The TableGen type system</a></li>
26 <li><a href="#values">TableGen values and expressions</a></li>
27 </ol></li>
28 <li><a href="#classesdefs">Classes and definitions</a>
29 <ol>
30 <li><a href="#valuedef">Value definitions</a></li>
31 <li><a href="#recordlet">'let' expressions</a></li>
32 <li><a href="#templateargs">Class template arguments</a></li>
33 </ol></li>
34 <li><a href="#filescope">File scope entities</a>
35 <ol>
36 <li><a href="#include">File inclusion</a></li>
37 <li><a href="#globallet">'let' expressions</a></li>
38 </ol></li>
39 </ol></li>
40 <li><a href="#backends">TableGen backends</a>
41 <ol>
42 <li><a href="#">todo</a></li>
43 </ol></li>
44 <li><a href="#codegenerator">The LLVM code generator</a>
45 <ol>
46 <li><a href="#">todo</a></li>
47 </ol></li>
48 </ul>
49 </div>
51 <div class="doc_author">
52 <p>Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a></p>
53 </div>
55 <!-- *********************************************************************** -->
56 <div class="doc_section"><a name="introduction">Introduction</a></div>
57 <!-- *********************************************************************** -->
59 <div class="doc_text">
61 <p>TableGen's purpose is to help a human develop and maintain records of
62 domain-specific information. Because there may be a large number of these
63 records, it is specifically designed to allow writing flexible descriptions and
64 for common features of these records to be factored out. This reduces the
65 amount of duplication in the description, reduces the chance of error, and
66 makes it easier to structure domain specific information.</p>
68 <p>The core part of TableGen <a href="#syntax">parses a file</a>, instantiates
69 the declarations, and hands the result off to a domain-specific "<a
70 href="#backends">TableGen backend</a>" for processing. The current major user
71 of TableGen is the <a href="#codegenerator">LLVM code generator</a>.</p>
73 <p>Note that if you work on TableGen much, and use emacs or vim, that you can
74 find an emacs "TableGen mode" and a vim language file in
75 <tt>llvm/utils/emacs</tt> and <tt>llvm/utils/vim</tt> directory of your LLVM
76 distribution, respectively.</p>
78 </div>
80 <!-- ======================================================================= -->
81 <div class="doc_subsection"><a name="running">Basic concepts</a></div>
83 <div class="doc_text">
85 <p>TableGen files consist of two key parts: 'classes' and 'definitions', both
86 of which are considered 'records'.</p>
88 <p><b>TableGen records</b> have a unique name, a list of values, and a list of
89 superclasses. The list of values is main data that TableGen builds for each
90 record, it is this that holds the domain specific information for the
91 application. The interpretation of this data is left to a specific <a
92 href="#backends">TableGen backend</a>, but the structure and format rules are
93 taken care of and fixed by TableGen.</p>
95 <p><b>TableGen definitions</b> are the concrete form of 'records'. These
96 generally do not have any undefined values, and are marked with the
97 '<tt>def</tt>' keyword.</p>
99 <p><b>TableGen classes</b> are abstract records that are used to build and
100 describe other records. These 'classes' allow the end-user to build
101 abstractions for either the domain they are targetting (such as "Register",
102 "RegisterClass", and "Instruction" in the LLVM code generator) or for the
103 implementor to help factor out common properties of records (such as "FPInst",
104 which is used to represent floating point instructions in the X86 backend).
105 TableGen keeps track of all of the classes that are used to build up a
106 definition, so the backend can find all definitions of a particular class, such
107 as "Instruction".</p>
109 </div>
111 <!-- ======================================================================= -->
112 <div class="doc_subsection"><a name="example">An example record</a></div>
114 <div class="doc_text">
116 <p>With no other arguments, TableGen parses the specified file and prints out
117 all of the classes, then all of the definitions. This is a good way to see what
118 the various definitions expand to fully. Running this on the <tt>X86.td</tt>
119 file prints this (at the time of this writing):</p>
121 <pre>
123 <b>def</b> ADDrr8 { <i>// Instruction X86Inst I2A8 Pattern</i>
124 <b>string</b> Name = "add";
125 <b>string</b> Namespace = "X86";
126 <b>list</b>&lt;Register&gt; Uses = [];
127 <b>list</b>&lt;Register&gt; Defs = [];
128 <b>bit</b> isReturn = 0;
129 <b>bit</b> isBranch = 0;
130 <b>bit</b> isCall = 0;
131 <b>bit</b> isTwoAddress = 1;
132 <b>bit</b> isTerminator = 0;
133 <b>dag</b> Pattern = (set R8, (plus R8, R8));
134 <b>bits</b>&lt;8&gt; Opcode = { 0, 0, 0, 0, 0, 0, 0, 0 };
135 Format Form = MRMDestReg;
136 <b>bits</b>&lt;5&gt; FormBits = { 0, 0, 0, 1, 1 };
137 ArgType Type = Arg8;
138 <b>bits</b>&lt;3&gt; TypeBits = { 0, 0, 1 };
139 <b>bit</b> hasOpSizePrefix = 0;
140 <b>bit</b> printImplicitUses = 0;
141 <b>bits</b>&lt;4&gt; Prefix = { 0, 0, 0, 0 };
142 FPFormat FPForm = ?;
143 <b>bits</b>&lt;3&gt; FPFormBits = { 0, 0, 0 };
146 </pre>
148 <p>This definition corresponds to an 8-bit register-register add instruction in
149 the X86. The string after the '<tt>def</tt>' string indicates the name of the
150 record ("<tt>ADDrr8</tt>" in this case), and the comment at the end of the line
151 indicates the superclasses of the definition. The body of the record contains
152 all of the data that TableGen assembled for the record, indicating that the
153 instruction is part of the "X86" namespace, should be printed as "<tt>add</tt>"
154 in the assembly file, it is a two-address instruction, has a particular
155 encoding, etc. The contents and semantics of the information in the record is
156 specific to the needs of the X86 backend, and is only shown as an example.</p>
158 <p>As you can see, a lot of information is needed for every instruction
159 supported by the code generator, and specifying it all manually would be
160 unmaintainble, prone to bugs, and tiring to do in the first place. Because we
161 are using TableGen, all of the information was derived from the following
162 definition:</p>
164 <pre>
165 <b>def</b> ADDrr8 : I2A8&lt;"add", 0x00, MRMDestReg&gt;,
166 Pattern&lt;(set R8, (plus R8, R8))&gt;;
167 </pre>
169 <p>This definition makes use of the custom I2A8 (two address instruction with
170 8-bit operand) class, which is defined in the X86-specific TableGen file to
171 factor out the common features that instructions of its class share. A key
172 feature of TableGen is that it allows the end-user to define the abstractions
173 they prefer to use when describing their information.</p>
175 </div>
177 <!-- ======================================================================= -->
178 <div class="doc_subsection"><a name="running">Running TableGen</a></div>
180 <div class="doc_text">
182 <p>TableGen runs just like any other LLVM tool. The first (optional) argument
183 specifies the file to read. If a filename is not specified, <tt>tblgen</tt>
184 reads from standard input.</p>
186 <p>To be useful, one of the <a href="#backends">TableGen backends</a> must be
187 used. These backends are selectable on the command line (type '<tt>tblgen
188 --help</tt>' for a list). For example, to get a list of all of the definitions
189 that subclass a particular type (which can be useful for building up an enum
190 list of these records), use the <tt>--print-enums</tt> option:</p>
192 <pre>
193 $ tblgen X86.td -print-enums -class=Register
194 AH, AL, AX, BH, BL, BP, BX, CH, CL, CX, DH, DI, DL, DX,
195 EAX, EBP, EBX, ECX, EDI, EDX, ESI, ESP, FP0, FP1, FP2, FP3, FP4, FP5, FP6,
196 SI, SP, ST0, ST1, ST2, ST3, ST4, ST5, ST6, ST7,
198 $ tblgen X86.td -print-enums -class=Instruction
199 ADCrr32, ADDri16, ADDri16b, ADDri32, ADDri32b, ADDri8, ADDrr16, ADDrr32,
200 ADDrr8, ADJCALLSTACKDOWN, ADJCALLSTACKUP, ANDri16, ANDri16b, ANDri32, ANDri32b,
201 ANDri8, ANDrr16, ANDrr32, ANDrr8, BSWAPr32, CALLm32, CALLpcrel32, ...
202 </pre>
204 <p>The default backend prints out all of the records, as described <a
205 href="#example">above</a>.</p>
207 <p>If you plan to use TableGen for some purpose, you will most likely have to
208 <a href="#backends">write a backend</a> that extracts the information specific
209 to what you need and formats it in the appropriate way.</p>
211 </div>
214 <!-- *********************************************************************** -->
215 <div class="doc_section"><a name="syntax">TableGen syntax</a></div>
216 <!-- *********************************************************************** -->
218 <div class="doc_text">
219 <p>TableGen doesn't care about the meaning of data (that is up to the backend
220 to define), but it does care about syntax, and it enforces a simple type system.
221 This section describes the syntax and the constructs allowed in a TableGen file.
222 </p>
223 </div>
225 <!-- ======================================================================= -->
226 <div class="doc_subsection"><a name="primitives">TableGen primitives</a></div>
228 <!-- -------------------------------------------------------------------------->
229 <div class="doc_subsubsection"><a name="comments">TableGen comments</a></div>
231 <div class="doc_text">
232 <p>TableGen supports BCPL style "<tt>//</tt>" comments, which run to the end of
233 the line, and it also supports <b>nestable</b> "<tt>/* */</tt>" comments.</p>
234 </div>
236 <!-- -------------------------------------------------------------------------->
237 <div class="doc_subsubsection">
238 <a name="types">The TableGen type system</a>
239 </div>
241 <div class="doc_text">
242 <p>TableGen files are strongly typed, in a simple (but complete) type-system.
243 These types are used to perform automatic conversions, check for errors, and to
244 help interface designers constrain the input that they allow. Every <a
245 href="#valuedef">value definition</a> is required to have an associated type.
246 </p>
248 <p>TableGen supports a mixture of very low-level types (such as <tt>bit</tt>)
249 and very high-level types (such as <tt>dag</tt>). This flexibility is what
250 allows it to describe a wide range of information conveniently and compactly.
251 The TableGen types are:</p>
253 <ul>
254 <li>"<tt><b>bit</b></tt>" - A 'bit' is a boolean value that can hold either 0 or
255 1.</li>
257 <li>"<tt><b>int</b></tt>" - The 'int' type represents a simple 32-bit integer
258 value, such as 5.</li>
260 <li>"<tt><b>string</b></tt>" - The 'string' type represents an ordered sequence
261 of characters of arbitrary length.</li>
263 <li>"<tt><b>bits</b>&lt;n&gt;</tt>" - A 'bits' type is an arbitrary, but fixed,
264 size integer that is broken up into individual bits. This type is useful
265 because it can handle some bits being defined while others are undefined.</li>
267 <li>"<tt><b>list</b>&lt;ty&gt;</tt>" - This type represents a list whose
268 elements are some other type. The contained type is arbitrary: it can even be
269 another list type.</li>
271 <li>Class type - Specifying a class name in a type context means that the
272 defined value must be a subclass of the specified class. This is useful in
273 conjunction with the "list" type, for example, to constrain the elements of the
274 list to a common base class (e.g., a <tt><b>list</b>&lt;Register&gt;</tt> can
275 only contain definitions derived from the "<tt>Register</tt>" class).</li>
277 <li>"<tt><b>code</b></tt>" - This represents a big hunk of text. NOTE: I don't
278 remember why this is distinct from string!</li>
280 <li>"<tt><b>dag</b></tt>" - This type represents a nestable directed graph of
281 elements.</li>
282 </ul>
284 <p>To date, these types have been sufficient for describing things that
285 TableGen has been used for, but it is straight-forward to extend this list if
286 needed.</p>
288 </div>
290 <!-- -------------------------------------------------------------------------->
291 <div class="doc_subsubsection">
292 <a name="values">TableGen values and expressions</a>
293 </div>
295 <div class="doc_text">
297 <p>TableGen allows for a pretty reasonable number of different expression forms
298 when building up values. These forms allow the TableGen file to be written in a
299 natural syntax and flavor for the application. The current expression forms
300 supported include:</p>
302 <ul>
303 <li>? - Uninitialized field.</li>
304 <li>0b1001011 - Binary integer value.</li>
305 <li>07654321 - Octal integer value (indicated by a leading 0).</li>
306 <li>7 - Decimal integer value.</li>
307 <li>0x7F - Hexadecimal integer value.</li>
308 <li>"foo" - String value.</li>
309 <li>[{ .... }] - Code fragment.</li>
310 <li>[ X, Y, Z ] - List value.</li>
311 <li>{ a, b, c } - Initializer for a "bits&lt;3&gt;" value.</li>
312 <li>value - Value reference.</li>
313 <li>value{17} - Access to one or more bits of a value.</li>
314 <li>DEF - Reference to a record definition.</li>
315 <li>X.Y - Reference to the subfield of a value.</li>
317 <li>(DEF a, b) - A dag value. The first element is required to be a record
318 definition, the remaining elements in the list may be arbitrary other values,
319 including nested 'dag' values.</li>
321 </ul>
323 <p>Note that all of the values have rules specifying how they convert to values
324 for different types. These rules allow you to assign a value like "7" to a
325 "bits&lt;4&gt;" value, for example.</p>
327 </div>
329 <!-- ======================================================================= -->
330 <div class="doc_subsection">
331 <a name="classesdefs">Classes and definitions</a>
332 </div>
334 <div class="doc_text">
336 <p>As mentioned in the <a href="#concepts">intro</a>, classes and definitions
337 (collectively known as 'records') in TableGen are the main high-level unit of
338 information that TableGen collects. Records are defined with a <tt>def</tt> or
339 <tt>class</tt> keyword, the record name, and an optional list of "<a
340 href="#templateargs">template arguments</a>". If the record has superclasses,
341 they are specified as a comma seperated list that starts with a colon character
342 (":"). If <a href="#valuedef">value definitions</a> or <a href="#recordlet">let
343 expressions</a> are needed for the class, they are enclosed in curly braces
344 ("{}"); otherwise, the record ends with a semicolon. Here is a simple TableGen
345 file:</p>
347 <pre>
348 <b>class</b> C { <b>bit</b> V = 1; }
349 <b>def</b> X : C;
350 <b>def</b> Y : C {
351 <b>string</b> Greeting = "hello";
353 </pre>
355 <p>This example defines two definitions, <tt>X</tt> and <tt>Y</tt>, both of
356 which derive from the <tt>C</tt> class. Because of this, they both get the
357 <tt>V</tt> bit value. The <tt>Y</tt> definition also gets the Greeting member
358 as well.</p>
360 <p>In general, classes are useful for collecting together the commonality
361 between a group of records and isolating it in a single place. Also, classes
362 permit the specification of default values for their subclasses, allowing the
363 subclasses to override them as they wish.</p>
365 </div>
367 <!---------------------------------------------------------------------------->
368 <div class="doc_subsubsection">
369 <a name="valuedef">Value definitions</a>
370 </div>
372 <div class="doc_text">
373 <p>Value definitions define named entries in records. A value must be defined
374 before it can be referred to as the operand for another value definition or
375 before the value is reset with a <a href="#recordlet">let expression</a>. A
376 value is defined by specifying a <a href="#types">TableGen type</a> and a name.
377 If an initial value is available, it may be specified after the type with an
378 equal sign. Value definitions require terminating semicolons.</p>
379 </div>
381 <!-- -------------------------------------------------------------------------->
382 <div class="doc_subsubsection">
383 <a name="recordlet">'let' expressions</a>
384 </div>
386 <div class="doc_text">
387 <p>A record-level let expression is used to change the value of a value
388 definition in a record. This is primarily useful when a superclass defines a
389 value that a derived class or definition wants to override. Let expressions
390 consist of the '<tt>let</tt>' keyword followed by a value name, an equal sign
391 ("="), and a new value. For example, a new class could be added to the example
392 above, redefining the <tt>V</tt> field for all of its subclasses:</p>
394 <pre>
395 <b>class</b> D : C { let V = 0; }
396 <b>def</b> Z : D;
397 </pre>
399 <p>In this case, the <tt>Z</tt> definition will have a zero value for its "V"
400 value, despite the fact that it derives (indirectly) from the <tt>C</tt> class,
401 because the <tt>D</tt> class overrode its value.</p>
403 </div>
405 <!-- -------------------------------------------------------------------------->
406 <div class="doc_subsubsection">
407 <a name="templateargs">Class template arguments</a>
408 </div>
410 <div class="doc_text">
411 <p>TableGen permits the definition of parameterized classes as well as normal
412 concrete classes. Parameterized TableGen classes specify a list of variable
413 bindings (which may optionally have defaults) that are bound when used. Here is
414 a simple example:</p>
416 <pre>
417 <b>class</b> FPFormat&lt;<b>bits</b>&lt;3&gt; val&gt; {
418 <b>bits</b>&lt;3&gt; Value = val;
420 <b>def</b> NotFP : FPFormat&lt;0&gt;;
421 <b>def</b> ZeroArgFP : FPFormat&lt;1&gt;;
422 <b>def</b> OneArgFP : FPFormat&lt;2&gt;;
423 <b>def</b> OneArgFPRW : FPFormat&lt;3&gt;;
424 <b>def</b> TwoArgFP : FPFormat&lt;4&gt;;
425 <b>def</b> SpecialFP : FPFormat&lt;5&gt;;
426 </pre>
428 <p>In this case, template arguments are used as a space efficient way to specify
429 a list of "enumeration values", each with a "Value" field set to the specified
430 integer.</p>
432 <p>The more esoteric forms of <a href="#values">TableGen expressions</a> are
433 useful in conjunction with template arguments. As an example:</p>
435 <pre>
436 <b>class</b> ModRefVal&lt;<b>bits</b>&lt;2&gt; val&gt; {
437 <b>bits</b>&lt;2&gt; Value = val;
440 <b>def</b> None : ModRefVal&lt;0&gt;;
441 <b>def</b> Mod : ModRefVal&lt;1&gt;;
442 <b>def</b> Ref : ModRefVal&lt;2&gt;;
443 <b>def</b> ModRef : ModRefVal&lt;3&gt;;
445 <b>class</b> Value&lt;ModRefVal MR&gt; {
446 <i>// decode some information into a more convenient format, while providing
447 // a nice interface to the user of the "Value" class.</i>
448 <b>bit</b> isMod = MR.Value{0};
449 <b>bit</b> isRef = MR.Value{1};
451 <i>// other stuff...</i>
454 <i>// Example uses</i>
455 <b>def</b> bork : Value&lt;Mod&gt;;
456 <b>def</b> zork : Value&lt;Ref&gt;;
457 <b>def</b> hork : Value&lt;ModRef&gt;;
458 </pre>
460 <p>This is obviously a contrived example, but it shows how template arguments
461 can be used to decouple the interface provided to the user of the class from the
462 actual internal data representation expected by the class. In this case,
463 running <tt>tblgen</tt> on the example prints the following definitions:</p>
465 <pre>
466 <b>def</b> bork { <i>// Value</i>
467 bit isMod = 1;
468 bit isRef = 0;
470 <b>def</b> hork { <i>// Value</i>
471 bit isMod = 1;
472 bit isRef = 1;
474 <b>def</b> zork { <i>// Value</i>
475 bit isMod = 0;
476 bit isRef = 1;
478 </pre>
480 <p> This shows that TableGen was able to dig into the argument and extract a
481 piece of information that was requested by the designer of the "Value" class.
482 For more realistic examples, please see existing users of TableGen, such as the
483 X86 backend.</p>
485 </div>
487 <!-- ======================================================================= -->
488 <div class="doc_subsection">
489 <a name="filescope">File scope entities</a>
490 </div>
492 <!-- -------------------------------------------------------------------------->
493 <div class="doc_subsubsection">
494 <a name="include">File inclusion</a>
495 </div>
497 <div class="doc_text">
498 <p>TableGen supports the '<tt>include</tt>' token, which textually substitutes
499 the specified file in place of the include directive. The filename should be
500 specified as a double quoted string immediately after the '<tt>include</tt>'
501 keyword. Example:</p>
503 <pre>
504 <b>include</b> "foo.td"
505 </pre>
507 </div>
509 <!-- -------------------------------------------------------------------------->
510 <div class="doc_subsubsection">
511 <a name="globallet">'let' expressions</a>
512 </div>
514 <div class="doc_text">
515 <p> "let" expressions at file scope are similar to <a href="#recordlet">"let"
516 expressions within a record</a>, except they can specify a value binding for
517 multiple records at a time, and may be useful in certain other cases.
518 File-scope let expressions are really just another way that TableGen allows the
519 end-user to factor out commonality from the records.</p>
521 <p>File-scope "let" expressions take a comma-seperated list of bindings to
522 apply, and one of more records to bind the values in. Here are some
523 examples:</p>
525 <pre>
526 <b>let</b> isTerminator = 1, isReturn = 1 <b>in</b>
527 <b>def</b> RET : X86Inst&lt;"ret", 0xC3, RawFrm, NoArg&gt;;
529 <b>let</b> isCall = 1 <b>in</b>
530 <i>// All calls clobber the non-callee saved registers...</i>
531 <b>let</b> Defs = [EAX, ECX, EDX, FP0, FP1, FP2, FP3, FP4, FP5, FP6] in {
532 <b>def</b> CALLpcrel32 : X86Inst&lt;"call", 0xE8, RawFrm, NoArg&gt;;
533 <b>def</b> CALLr32 : X86Inst&lt;"call", 0xFF, MRMS2r, Arg32&gt;;
534 <b>def</b> CALLm32 : X86Inst&lt;"call", 0xFF, MRMS2m, Arg32&gt;;
536 </pre>
538 <p>File-scope "let" expressions are often useful when a couple of definitions
539 need to be added to several records, and the records do not otherwise need to be
540 opened, as in the case with the CALL* instructions above.</p>
541 </div>
543 <!-- *********************************************************************** -->
544 <div class="doc_section"><a name="backends">TableGen backends</a></div>
545 <!-- *********************************************************************** -->
547 <div class="doc_text">
548 <p>How they work, how to write one. This section should not contain details
549 about any particular backend, except maybe -print-enums as an example. This
550 should highlight the APIs in <tt>TableGen/Record.h</tt>.</p>
551 </div>
553 <!-- *********************************************************************** -->
554 <div class="doc_section"><a name="codegenerator">The LLVM code generator</a>
555 </div>
556 <!-- *********************************************************************** -->
558 <div class="doc_text">
559 <p>This is just a temporary, convenient, place to put stuff about the code
560 generator before it gets its own document. This should describe all of the
561 tablegen backends used by the code generator and the classes/definitions they
562 expect.</p>
563 </div>
565 <!-- *********************************************************************** -->
567 <hr>
568 <address>
569 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
570 src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a>
571 <a href="http://validator.w3.org/check/referer"><img
572 src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!" /></a>
574 <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
575 <a href="http://llvm.cs.uiuc.edu">LLVM Compiler Infrastructure</a><br>
576 Last modified: $Date$
577 </address>
579 </body>
580 </html>