blob: 47bc3ff9364e21698b9f834eead66fa7a014c778 [file] [log] [blame]
Chris Lattnerb54c99c2004-02-06 05:42:53 +00001<!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>
9
10<div class="doc_title">TableGen Fundamentals</div>
11
Misha Brukman85234ac2004-05-12 18:31:21 +000012<div class="doc_text">
Chris Lattnerb54c99c2004-02-06 05:42:53 +000013<ul>
Misha Brukman85234ac2004-05-12 18:31:21 +000014 <li><a href="#introduction">Introduction</a>
Chris Lattnerb54c99c2004-02-06 05:42:53 +000015 <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>
Misha Brukman85234ac2004-05-12 18:31:21 +000019 </ol></li>
20 <li><a href="#syntax">TableGen syntax</a>
Chris Lattnerb54c99c2004-02-06 05:42:53 +000021 <ol>
Misha Brukman85234ac2004-05-12 18:31:21 +000022 <li><a href="#primitives">TableGen primitives</a>
Chris Lattnerb54c99c2004-02-06 05:42:53 +000023 <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>
Misha Brukman85234ac2004-05-12 18:31:21 +000027 </ol></li>
28 <li><a href="#classesdefs">Classes and definitions</a>
Chris Lattnerb54c99c2004-02-06 05:42:53 +000029 <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>
Misha Brukman85234ac2004-05-12 18:31:21 +000033 </ol></li>
34 <li><a href="#filescope">File scope entities</a>
Chris Lattnerb54c99c2004-02-06 05:42:53 +000035 <ol>
36 <li><a href="#include">File inclusion</a></li>
37 <li><a href="#globallet">'let' expressions</a></li>
Misha Brukman85234ac2004-05-12 18:31:21 +000038 </ol></li>
39 </ol></li>
40 <li><a href="#backends">TableGen backends</a>
Chris Lattnerb54c99c2004-02-06 05:42:53 +000041 <ol>
Chris Lattnerfa6f3092004-02-06 06:04:25 +000042 <li><a href="#">todo</a></li>
Misha Brukman85234ac2004-05-12 18:31:21 +000043 </ol></li>
44 <li><a href="#codegenerator">The LLVM code generator</a>
Chris Lattnerb54c99c2004-02-06 05:42:53 +000045 <ol>
Chris Lattnerfa6f3092004-02-06 06:04:25 +000046 <li><a href="#">todo</a></li>
Misha Brukman85234ac2004-05-12 18:31:21 +000047 </ol></li>
Chris Lattnerb54c99c2004-02-06 05:42:53 +000048</ul>
Misha Brukman85234ac2004-05-12 18:31:21 +000049</div>
Chris Lattnerb54c99c2004-02-06 05:42:53 +000050
Chris Lattner7911ce22004-05-23 21:07:27 +000051<div class="doc_author">
52 <p>Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a></p>
53</div>
54
Chris Lattnerb54c99c2004-02-06 05:42:53 +000055<!-- *********************************************************************** -->
56<div class="doc_section"><a name="introduction">Introduction</a></div>
57<!-- *********************************************************************** -->
58
59<div class="doc_text">
60
61<p>TableGen's purpose is to help a human develop and maintain records of
62domain-specific information. Because there may be a large number of these
63records, it is specifically designed to allow writing flexible descriptions and
64for common features of these records to be factored out. This reduces the
65amount of duplication in the description, reduces the chance of error, and
66makes it easier to structure domain specific information.</p>
67
68<p>The core part of TableGen <a href="#syntax">parses a file</a>, instantiates
69the declarations, and hands the result off to a domain-specific "<a
70href="#backends">TableGen backend</a>" for processing. The current major user
Misha Brukman85234ac2004-05-12 18:31:21 +000071of TableGen is the <a href="#codegenerator">LLVM code generator</a>.</p>
Chris Lattnerb54c99c2004-02-06 05:42:53 +000072
Misha Brukman85234ac2004-05-12 18:31:21 +000073<p>Note that if you work on TableGen much, and use emacs or vim, that you can
74find 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
76distribution, respectively.</p>
Chris Lattnerfa6f3092004-02-06 06:04:25 +000077
Chris Lattnerb54c99c2004-02-06 05:42:53 +000078</div>
79
80<!-- ======================================================================= -->
Misha Brukman85234ac2004-05-12 18:31:21 +000081<div class="doc_subsection"><a name="running">Basic concepts</a></div>
Chris Lattnerb54c99c2004-02-06 05:42:53 +000082
83<div class="doc_text">
84
Misha Brukman85234ac2004-05-12 18:31:21 +000085<p>TableGen files consist of two key parts: 'classes' and 'definitions', both
86of which are considered 'records'.</p>
Chris Lattnerb54c99c2004-02-06 05:42:53 +000087
Misha Brukman85234ac2004-05-12 18:31:21 +000088<p><b>TableGen records</b> have a unique name, a list of values, and a list of
Chris Lattnerb54c99c2004-02-06 05:42:53 +000089superclasses. The list of values is main data that TableGen builds for each
90record, it is this that holds the domain specific information for the
91application. The interpretation of this data is left to a specific <a
92href="#backends">TableGen backend</a>, but the structure and format rules are
Misha Brukman85234ac2004-05-12 18:31:21 +000093taken care of and fixed by TableGen.</p>
Chris Lattnerb54c99c2004-02-06 05:42:53 +000094
Misha Brukman85234ac2004-05-12 18:31:21 +000095<p><b>TableGen definitions</b> are the concrete form of 'records'. These
96generally do not have any undefined values, and are marked with the
97'<tt>def</tt>' keyword.</p>
Chris Lattnerb54c99c2004-02-06 05:42:53 +000098
Misha Brukman85234ac2004-05-12 18:31:21 +000099<p><b>TableGen classes</b> are abstract records that are used to build and
100describe other records. These 'classes' allow the end-user to build
101abstractions for either the domain they are targetting (such as "Register",
102"RegisterClass", and "Instruction" in the LLVM code generator) or for the
103implementor to help factor out common properties of records (such as "FPInst",
104which is used to represent floating point instructions in the X86 backend).
105TableGen keeps track of all of the classes that are used to build up a
106definition, so the backend can find all definitions of a particular class, such
107as "Instruction".</p>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000108
109</div>
110
111<!-- ======================================================================= -->
Misha Brukman85234ac2004-05-12 18:31:21 +0000112<div class="doc_subsection"><a name="example">An example record</a></div>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000113
114<div class="doc_text">
115
Misha Brukman85234ac2004-05-12 18:31:21 +0000116<p>With no other arguments, TableGen parses the specified file and prints out
117all of the classes, then all of the definitions. This is a good way to see what
118the various definitions expand to fully. Running this on the <tt>X86.td</tt>
119file prints this (at the time of this writing):</p>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000120
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000121<pre>
122...
Chris Lattnerfa6f3092004-02-06 06:04:25 +0000123<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 };
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000135 Format Form = MRMDestReg;
Chris Lattnerfa6f3092004-02-06 06:04:25 +0000136 <b>bits</b>&lt;5&gt; FormBits = { 0, 0, 0, 1, 1 };
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000137 ArgType Type = Arg8;
Chris Lattnerfa6f3092004-02-06 06:04:25 +0000138 <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 };
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000142 FPFormat FPForm = ?;
Chris Lattnerfa6f3092004-02-06 06:04:25 +0000143 <b>bits</b>&lt;3&gt; FPFormBits = { 0, 0, 0 };
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000144}
145...
Misha Brukman85234ac2004-05-12 18:31:21 +0000146</pre>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000147
Misha Brukman85234ac2004-05-12 18:31:21 +0000148<p>This definition corresponds to an 8-bit register-register add instruction in
149the X86. The string after the '<tt>def</tt>' string indicates the name of the
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000150record ("<tt>ADDrr8</tt>" in this case), and the comment at the end of the line
151indicates the superclasses of the definition. The body of the record contains
152all of the data that TableGen assembled for the record, indicating that the
153instruction is part of the "X86" namespace, should be printed as "<tt>add</tt>"
154in the assembly file, it is a two-address instruction, has a particular
155encoding, etc. The contents and semantics of the information in the record is
Misha Brukman85234ac2004-05-12 18:31:21 +0000156specific to the needs of the X86 backend, and is only shown as an example.</p>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000157
Misha Brukman85234ac2004-05-12 18:31:21 +0000158<p>As you can see, a lot of information is needed for every instruction
159supported by the code generator, and specifying it all manually would be
160unmaintainble, prone to bugs, and tiring to do in the first place. Because we
161are using TableGen, all of the information was derived from the following
162definition:</p>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000163
Misha Brukman85234ac2004-05-12 18:31:21 +0000164<pre>
Chris Lattnerfa6f3092004-02-06 06:04:25 +0000165<b>def</b> ADDrr8 : I2A8&lt;"add", 0x00, MRMDestReg&gt;,
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000166 Pattern&lt;(set R8, (plus R8, R8))&gt;;
Misha Brukman85234ac2004-05-12 18:31:21 +0000167</pre>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000168
Misha Brukman85234ac2004-05-12 18:31:21 +0000169<p>This definition makes use of the custom I2A8 (two address instruction with
1708-bit operand) class, which is defined in the X86-specific TableGen file to
171factor out the common features that instructions of its class share. A key
172feature of TableGen is that it allows the end-user to define the abstractions
173they prefer to use when describing their information.</p>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000174
175</div>
176
177<!-- ======================================================================= -->
Misha Brukman85234ac2004-05-12 18:31:21 +0000178<div class="doc_subsection"><a name="running">Running TableGen</a></div>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000179
180<div class="doc_text">
181
Misha Brukman85234ac2004-05-12 18:31:21 +0000182<p>TableGen runs just like any other LLVM tool. The first (optional) argument
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000183specifies the file to read. If a filename is not specified, <tt>tblgen</tt>
Misha Brukman85234ac2004-05-12 18:31:21 +0000184reads from standard input.</p>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000185
Misha Brukman85234ac2004-05-12 18:31:21 +0000186<p>To be useful, one of the <a href="#backends">TableGen backends</a> must be
187used. 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
189that subclass a particular type (which can be useful for building up an enum
190list of these records), use the <tt>--print-enums</tt> option:</p>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000191
Misha Brukman85234ac2004-05-12 18:31:21 +0000192<pre>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000193$ tblgen X86.td -print-enums -class=Register
194AH, AL, AX, BH, BL, BP, BX, CH, CL, CX, DH, DI, DL, DX,
195EAX, EBP, EBX, ECX, EDI, EDX, ESI, ESP, FP0, FP1, FP2, FP3, FP4, FP5, FP6,
196SI, SP, ST0, ST1, ST2, ST3, ST4, ST5, ST6, ST7,
197
198$ tblgen X86.td -print-enums -class=Instruction
199ADCrr32, ADDri16, ADDri16b, ADDri32, ADDri32b, ADDri8, ADDrr16, ADDrr32,
200ADDrr8, ADJCALLSTACKDOWN, ADJCALLSTACKUP, ANDri16, ANDri16b, ANDri32, ANDri32b,
201ANDri8, ANDrr16, ANDrr32, ANDrr8, BSWAPr32, CALLm32, CALLpcrel32, ...
Misha Brukman85234ac2004-05-12 18:31:21 +0000202</pre>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000203
Misha Brukman85234ac2004-05-12 18:31:21 +0000204<p>The default backend prints out all of the records, as described <a
205href="#example">above</a>.</p>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000206
Misha Brukman85234ac2004-05-12 18:31:21 +0000207<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
209to what you need and formats it in the appropriate way.</p>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000210
211</div>
212
213
214<!-- *********************************************************************** -->
215<div class="doc_section"><a name="syntax">TableGen syntax</a></div>
216<!-- *********************************************************************** -->
217
218<div class="doc_text">
Misha Brukman85234ac2004-05-12 18:31:21 +0000219<p>TableGen doesn't care about the meaning of data (that is up to the backend
220to define), but it does care about syntax, and it enforces a simple type system.
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000221This section describes the syntax and the constructs allowed in a TableGen file.
222</p>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000223</div>
224
225<!-- ======================================================================= -->
Misha Brukman85234ac2004-05-12 18:31:21 +0000226<div class="doc_subsection"><a name="primitives">TableGen primitives</a></div>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000227
Misha Brukman85234ac2004-05-12 18:31:21 +0000228<!-- -------------------------------------------------------------------------->
229<div class="doc_subsubsection"><a name="comments">TableGen comments</a></div>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000230
231<div class="doc_text">
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000232<p>TableGen supports BCPL style "<tt>//</tt>" comments, which run to the end of
233the line, and it also supports <b>nestable</b> "<tt>/* */</tt>" comments.</p>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000234</div>
235
Misha Brukman85234ac2004-05-12 18:31:21 +0000236<!-- -------------------------------------------------------------------------->
237<div class="doc_subsubsection"><a name="types">The TableGen type system</a>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000238</div>
239
240<div class="doc_text">
Misha Brukman85234ac2004-05-12 18:31:21 +0000241<p>TableGen files are strongly typed, in a simple (but complete) type-system.
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000242These types are used to perform automatic conversions, check for errors, and to
243help interface designers constrain the input that they allow. Every <a
244href="#valuedef">value definition</a> is required to have an associated type.
245</p>
246
Misha Brukman85234ac2004-05-12 18:31:21 +0000247<p>TableGen supports a mixture of very low-level types (such as <tt>bit</tt>)
248and very high-level types (such as <tt>dag</tt>). This flexibility is what
249allows it to describe a wide range of information conveniently and compactly.
250The TableGen types are:</p>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000251
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000252<ul>
Chris Lattnerfa6f3092004-02-06 06:04:25 +0000253<li>"<tt><b>bit</b></tt>" - A 'bit' is a boolean value that can hold either 0 or
Chris Lattnerb54c99c2004-02-06 05:42:53 +00002541.</li>
255
Chris Lattnerfa6f3092004-02-06 06:04:25 +0000256<li>"<tt><b>int</b></tt>" - The 'int' type represents a simple 32-bit integer
257value, such as 5.</li>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000258
Chris Lattnerfa6f3092004-02-06 06:04:25 +0000259<li>"<tt><b>string</b></tt>" - The 'string' type represents an ordered sequence
260of characters of arbitrary length.</li>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000261
John Criswellaa55c8c2004-02-12 18:11:53 +0000262<li>"<tt><b>bits</b>&lt;n&gt;</tt>" - A 'bits' type is an arbitrary, but fixed,
Chris Lattnerfa6f3092004-02-06 06:04:25 +0000263size integer that is broken up into individual bits. This type is useful
264because it can handle some bits being defined while others are undefined.</li>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000265
Chris Lattnerfa6f3092004-02-06 06:04:25 +0000266<li>"<tt><b>list</b>&lt;ty&gt;</tt>" - This type represents a list whose
267elements are some other type. The contained type is arbitrary: it can even be
268another list type.</li>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000269
270<li>Class type - Specifying a class name in a type context means that the
271defined value must be a subclass of the specified class. This is useful in
272conjunction with the "list" type, for example, to constrain the elements of the
Chris Lattnerfa6f3092004-02-06 06:04:25 +0000273list to a common base class (e.g., a <tt><b>list</b>&lt;Register&gt;</tt> can
274only contain definitions derived from the "<tt>Register</tt>" class).</li>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000275
Chris Lattnerfa6f3092004-02-06 06:04:25 +0000276<li>"<tt><b>code</b></tt>" - This represents a big hunk of text. NOTE: I don't
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000277remember why this is distinct from string!</li>
278
Chris Lattnerfa6f3092004-02-06 06:04:25 +0000279<li>"<tt><b>dag</b></tt>" - This type represents a nestable directed graph of
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000280elements.</li>
281</ul>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000282
Misha Brukman85234ac2004-05-12 18:31:21 +0000283<p>To date, these types have been sufficient for describing things that
284TableGen has been used for, but it is straight-forward to extend this list if
285needed.</p>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000286
287</div>
288
Misha Brukman85234ac2004-05-12 18:31:21 +0000289<!-- -------------------------------------------------------------------------->
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000290<div class="doc_subsubsection">
Misha Brukman85234ac2004-05-12 18:31:21 +0000291 <a name="values">TableGen values and expressions</a>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000292</div>
293
294<div>
Misha Brukman85234ac2004-05-12 18:31:21 +0000295<p>TableGen allows for a pretty reasonable number of different expression forms
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000296when building up values. These forms allow the TableGen file to be written in a
297natural syntax and flavor for the application. The current expression forms
Misha Brukman85234ac2004-05-12 18:31:21 +0000298supported include:</p>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000299
Misha Brukman85234ac2004-05-12 18:31:21 +0000300<ul>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000301<li>? - Uninitialized field.</li>
302<li>0b1001011 - Binary integer value.</li>
303<li>07654321 - Octal integer value (indicated by a leading 0).</li>
304<li>7 - Decimal integer value.</li>
305<li>0x7F - Hexadecimal integer value.</li>
306<li>"foo" - String value.</li>
307<li>[{ .... }] - Code fragment.</li>
308<li>[ X, Y, Z ] - List value.</li>
309<li>{ a, b, c } - Initializer for a "bits&lt;3&gt;" value.</li>
310<li>value - Value reference.</li>
311<li>value{17} - Access to one or more bits of a value.</li>
312<li>DEF - Reference to a record definition.</li>
313<li>X.Y - Reference to the subfield of a value.</li>
314
315<li>(DEF a, b) - A dag value. The first element is required to be a record
316definition, the remaining elements in the list may be arbitrary other values,
317including nested 'dag' values.</li>
318
Misha Brukman85234ac2004-05-12 18:31:21 +0000319</ul>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000320
Misha Brukman85234ac2004-05-12 18:31:21 +0000321<p>Note that all of the values have rules specifying how they convert to values
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000322for different types. These rules allow you to assign a value like "7" to a
Misha Brukman85234ac2004-05-12 18:31:21 +0000323"bits&lt;4&gt;" value, for example.</p>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000324
325</div>
326
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000327<!-- ======================================================================= -->
Misha Brukman85234ac2004-05-12 18:31:21 +0000328<div class="doc_subsection"><a name="classesdefs">Classes and definitions</a>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000329</div>
330
331<div>
Misha Brukman85234ac2004-05-12 18:31:21 +0000332<p>As mentioned in the <a href="#concepts">intro</a>, classes and definitions
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000333(collectively known as 'records') in TableGen are the main high-level unit of
334information that TableGen collects. Records are defined with a <tt>def</tt> or
335<tt>class</tt> keyword, the record name, and an optional list of "<a
336href="templateargs">template arguments</a>". If the record has superclasses,
337they are specified as a comma seperated list that starts with a colon character
338(":"). If <a href="#valuedef">value definitions</a> or <a href="#recordlet">let
John Criswellaa55c8c2004-02-12 18:11:53 +0000339expressions</a> are needed for the class, they are enclosed in curly braces
340("{}"); otherwise, the record ends with a semicolon. Here is a simple TableGen
Misha Brukman85234ac2004-05-12 18:31:21 +0000341file:</p>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000342
Misha Brukman85234ac2004-05-12 18:31:21 +0000343<pre>
Chris Lattnerfa6f3092004-02-06 06:04:25 +0000344<b>class</b> C { <b>bit</b> V = 1; }
345<b>def</b> X : C;
346<b>def</b> Y : C {
347 <b>string</b> Greeting = "hello";
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000348}
Misha Brukman85234ac2004-05-12 18:31:21 +0000349</pre>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000350
Misha Brukman85234ac2004-05-12 18:31:21 +0000351<p>This example defines two definitions, <tt>X</tt> and <tt>Y</tt>, both of
352which derive from the <tt>C</tt> class. Because of this, they both get the
353<tt>V</tt> bit value. The <tt>Y</tt> definition also gets the Greeting member
354as well.</p>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000355
Misha Brukman85234ac2004-05-12 18:31:21 +0000356<p>In general, classes are useful for collecting together the commonality
357between a group of records and isolating it in a single place. Also, classes
358permit the specification of default values for their subclasses, allowing the
359subclasses to override them as they wish.</p>
Chris Lattner23f54fa2004-02-06 06:37:00 +0000360
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000361</div>
362
Misha Brukman85234ac2004-05-12 18:31:21 +0000363<!---------------------------------------------------------------------------->
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000364<div class="doc_subsubsection">
Misha Brukman85234ac2004-05-12 18:31:21 +0000365 <a name="valuedef">Value definitions</a>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000366</div>
367
368<div class="doc_text">
Misha Brukman85234ac2004-05-12 18:31:21 +0000369<p>Value definitions define named entries in records. A value must be defined
John Criswellaa55c8c2004-02-12 18:11:53 +0000370before it can be referred to as the operand for another value definition or
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000371before the value is reset with a <a href="#recordlet">let expression</a>. A
372value is defined by specifying a <a href="#types">TableGen type</a> and a name.
373If an initial value is available, it may be specified after the type with an
Misha Brukman85234ac2004-05-12 18:31:21 +0000374equal sign. Value definitions require terminating semicolons.</p>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000375</div>
376
Misha Brukman85234ac2004-05-12 18:31:21 +0000377<!-- -------------------------------------------------------------------------->
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000378<div class="doc_subsubsection">
Misha Brukman85234ac2004-05-12 18:31:21 +0000379 <a name="recordlet">'let' expressions</a>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000380</div>
381
382<div class="doc_text">
Misha Brukman85234ac2004-05-12 18:31:21 +0000383<p>A record-level let expression is used to change the value of a value
384definition in a record. This is primarily useful when a superclass defines a
385value that a derived class or definition wants to override. Let expressions
386consist of the '<tt>let</tt>' keyword followed by a value name, an equal sign
387("="), and a new value. For example, a new class could be added to the example
388above, redefining the <tt>V</tt> field for all of its subclasses:</p>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000389
Misha Brukman85234ac2004-05-12 18:31:21 +0000390<pre>
Chris Lattnerfa6f3092004-02-06 06:04:25 +0000391<b>class</b> D : C { let V = 0; }
392<b>def</b> Z : D;
Misha Brukman85234ac2004-05-12 18:31:21 +0000393</pre>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000394
Misha Brukman85234ac2004-05-12 18:31:21 +0000395<p>In this case, the <tt>Z</tt> definition will have a zero value for its "V"
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000396value, despite the fact that it derives (indirectly) from the <tt>C</tt> class,
Misha Brukman85234ac2004-05-12 18:31:21 +0000397because the <tt>D</tt> class overrode its value.</p>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000398
399</div>
400
Misha Brukman85234ac2004-05-12 18:31:21 +0000401<!-- -------------------------------------------------------------------------->
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000402<div class="doc_subsubsection">
Misha Brukman85234ac2004-05-12 18:31:21 +0000403 <a name="templateargs">Class template arguments</a>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000404</div>
405
406<div class="doc_text">
Misha Brukman85234ac2004-05-12 18:31:21 +0000407<p>TableGen permits the definition of parameterized classes as well as normal
Chris Lattner23f54fa2004-02-06 06:37:00 +0000408concrete classes. Parameterized TableGen classes specify a list of variable
409bindings (which may optionally have defaults) that are bound when used. Here is
410a simple example:</p>
411
Misha Brukman85234ac2004-05-12 18:31:21 +0000412<pre>
Chris Lattner23f54fa2004-02-06 06:37:00 +0000413<b>class</b> FPFormat&lt;<b>bits</b>&lt;3&gt; val&gt; {
414 <b>bits</b>&lt;3&gt; Value = val;
415}
416<b>def</b> NotFP : FPFormat&lt;0&gt;;
417<b>def</b> ZeroArgFP : FPFormat&lt;1&gt;;
418<b>def</b> OneArgFP : FPFormat&lt;2&gt;;
419<b>def</b> OneArgFPRW : FPFormat&lt;3&gt;;
420<b>def</b> TwoArgFP : FPFormat&lt;4&gt;;
421<b>def</b> SpecialFP : FPFormat&lt;5&gt;;
Misha Brukman85234ac2004-05-12 18:31:21 +0000422</pre>
Chris Lattner23f54fa2004-02-06 06:37:00 +0000423
Misha Brukman85234ac2004-05-12 18:31:21 +0000424<p>In this case, template arguments are used as a space efficient way to specify
425a list of "enumeration values", each with a "Value" field set to the specified
Chris Lattner23f54fa2004-02-06 06:37:00 +0000426integer.</p>
427
428<p>The more esoteric forms of <a href="#values">TableGen expressions</a> are
429useful in conjunction with template arguments. As an example:</p>
430
Misha Brukman85234ac2004-05-12 18:31:21 +0000431<pre>
Chris Lattner23f54fa2004-02-06 06:37:00 +0000432<b>class</b> ModRefVal&lt;<b>bits</b>&lt;2&gt; val&gt; {
433 <b>bits</b>&lt;2&gt; Value = val;
434}
435
436<b>def</b> None : ModRefVal&lt;0&gt;;
437<b>def</b> Mod : ModRefVal&lt;1&gt;;
438<b>def</b> Ref : ModRefVal&lt;2&gt;;
439<b>def</b> ModRef : ModRefVal&lt;3&gt;;
440
441<b>class</b> Value&lt;ModRefVal MR&gt; {
442 <i>// decode some information into a more convenient format, while providing
443 // a nice interface to the user of the "Value" class.</i>
444 <b>bit</b> isMod = MR.Value{0};
445 <b>bit</b> isRef = MR.Value{1};
446
447 <i>// other stuff...</i>
448}
449
450<i>// Example uses</i>
451<b>def</b> bork : Value&lt;Mod&gt;;
452<b>def</b> zork : Value&lt;Ref&gt;;
453<b>def</b> hork : Value&lt;ModRef&gt;;
Misha Brukman85234ac2004-05-12 18:31:21 +0000454</pre>
Chris Lattner23f54fa2004-02-06 06:37:00 +0000455
Misha Brukman85234ac2004-05-12 18:31:21 +0000456<p>This is obviously a contrived example, but it shows how template arguments
457can be used to decouple the interface provided to the user of the class from the
Chris Lattner23f54fa2004-02-06 06:37:00 +0000458actual internal data representation expected by the class. In this case,
459running <tt>tblgen</tt> on the example prints the following definitions:</p>
460
Misha Brukman85234ac2004-05-12 18:31:21 +0000461<pre>
Chris Lattner23f54fa2004-02-06 06:37:00 +0000462<b>def</b> bork { <i>// Value</i>
463 bit isMod = 1;
464 bit isRef = 0;
465}
466<b>def</b> hork { <i>// Value</i>
467 bit isMod = 1;
468 bit isRef = 1;
469}
470<b>def</b> zork { <i>// Value</i>
471 bit isMod = 0;
472 bit isRef = 1;
473}
Misha Brukman85234ac2004-05-12 18:31:21 +0000474</pre>
Chris Lattner23f54fa2004-02-06 06:37:00 +0000475
Misha Brukman85234ac2004-05-12 18:31:21 +0000476<p> This shows that TableGen was able to dig into the argument and extract a
477piece of information that was requested by the designer of the "Value" class.
478For more realistic examples, please see existing users of TableGen, such as the
479X86 backend.</p>
Chris Lattner23f54fa2004-02-06 06:37:00 +0000480
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000481</div>
482
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000483<!-- ======================================================================= -->
484<div class="doc_subsection">
Misha Brukman85234ac2004-05-12 18:31:21 +0000485 <a name="filescope">File scope entities</a>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000486</div>
487
Misha Brukman85234ac2004-05-12 18:31:21 +0000488<!-- -------------------------------------------------------------------------->
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000489<div class="doc_subsubsection">
Misha Brukman85234ac2004-05-12 18:31:21 +0000490 <a name="include">File inclusion</a>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000491</div>
492
493<div class="doc_text">
Misha Brukman85234ac2004-05-12 18:31:21 +0000494<p>TableGen supports the '<tt>include</tt>' token, which textually substitutes
495the specified file in place of the include directive. The filename should be
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000496specified as a double quoted string immediately after the '<tt>include</tt>'
Misha Brukman85234ac2004-05-12 18:31:21 +0000497keyword. Example:</p>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000498
Misha Brukman85234ac2004-05-12 18:31:21 +0000499<pre>
Chris Lattner23f54fa2004-02-06 06:37:00 +0000500<b>include</b> "foo.td"
Misha Brukman85234ac2004-05-12 18:31:21 +0000501</pre>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000502
503</div>
504
Misha Brukman85234ac2004-05-12 18:31:21 +0000505<!-- -------------------------------------------------------------------------->
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000506<div class="doc_subsubsection">
Misha Brukman85234ac2004-05-12 18:31:21 +0000507 <a name="globallet">'let' expressions</a>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000508</div>
509
510<div class="doc_text">
Misha Brukman85234ac2004-05-12 18:31:21 +0000511<p> "let" expressions at file scope are similar to <a href="#recordlet">"let"
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000512expressions within a record</a>, except they can specify a value binding for
513multiple records at a time, and may be useful in certain other cases.
514File-scope let expressions are really just another way that TableGen allows the
Misha Brukman85234ac2004-05-12 18:31:21 +0000515end-user to factor out commonality from the records.</p>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000516
Misha Brukman85234ac2004-05-12 18:31:21 +0000517<p>File-scope "let" expressions take a comma-seperated list of bindings to
518apply, and one of more records to bind the values in. Here are some
519examples:</p>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000520
Misha Brukman85234ac2004-05-12 18:31:21 +0000521<pre>
Chris Lattnerfa6f3092004-02-06 06:04:25 +0000522<b>let</b> isTerminator = 1, isReturn = 1 <b>in</b>
523 <b>def</b> RET : X86Inst&lt;"ret", 0xC3, RawFrm, NoArg&gt;;
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000524
Chris Lattnerfa6f3092004-02-06 06:04:25 +0000525<b>let</b> isCall = 1 <b>in</b>
526 <i>// All calls clobber the non-callee saved registers...</i>
527 <b>let</b> Defs = [EAX, ECX, EDX, FP0, FP1, FP2, FP3, FP4, FP5, FP6] in {
528 <b>def</b> CALLpcrel32 : X86Inst&lt;"call", 0xE8, RawFrm, NoArg&gt;;
529 <b>def</b> CALLr32 : X86Inst&lt;"call", 0xFF, MRMS2r, Arg32&gt;;
530 <b>def</b> CALLm32 : X86Inst&lt;"call", 0xFF, MRMS2m, Arg32&gt;;
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000531 }
Misha Brukman85234ac2004-05-12 18:31:21 +0000532</pre>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000533
Misha Brukman85234ac2004-05-12 18:31:21 +0000534<p>File-scope "let" expressions are often useful when a couple of definitions
535need to be added to several records, and the records do not otherwise need to be
536opened, as in the case with the CALL* instructions above.</p>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000537</div>
538
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000539<!-- *********************************************************************** -->
540<div class="doc_section"><a name="backends">TableGen backends</a></div>
541<!-- *********************************************************************** -->
542
543<div class="doc_text">
Misha Brukman85234ac2004-05-12 18:31:21 +0000544<p>How they work, how to write one. This section should not contain details
545about any particular backend, except maybe -print-enums as an example. This
546should highlight the APIs in <tt>TableGen/Record.h</tt>.</p>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000547</div>
548
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000549<!-- *********************************************************************** -->
Misha Brukman85234ac2004-05-12 18:31:21 +0000550<div class="doc_section"><a name="codegenerator">The LLVM code generator</a>
551</div>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000552<!-- *********************************************************************** -->
553
554<div class="doc_text">
Misha Brukman85234ac2004-05-12 18:31:21 +0000555<p>This is just a temporary, convenient, place to put stuff about the code
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000556generator before it gets its own document. This should describe all of the
557tablegen backends used by the code generator and the classes/definitions they
Misha Brukman85234ac2004-05-12 18:31:21 +0000558expect.</p>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000559</div>
560
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000561<!-- *********************************************************************** -->
Misha Brukman85234ac2004-05-12 18:31:21 +0000562
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000563<hr>
Misha Brukman85234ac2004-05-12 18:31:21 +0000564<address>
565 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
566 src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a>
567 <a href="http://validator.w3.org/check/referer"><img
568 src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!" /></a>
569
570 <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
571 <a href="http://llvm.cs.uiuc.edu">LLVM Compiler Infrastructure</a><br>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000572 Last modified: $Date$
Misha Brukman85234ac2004-05-12 18:31:21 +0000573</address>
Chris Lattnerb54c99c2004-02-06 05:42:53 +0000574
575</body>
576</html>