blob: 26c3ff38dc522b2091ba66c6ce2cdcdbdcd31840 [file] [log] [blame]
Chris Lattner149a2492004-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 Brukman692cec02004-05-12 18:31:21 +000012<div class="doc_text">
Chris Lattner149a2492004-02-06 05:42:53 +000013<ul>
Misha Brukman692cec02004-05-12 18:31:21 +000014 <li><a href="#introduction">Introduction</a>
Chris Lattner149a2492004-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 Brukman692cec02004-05-12 18:31:21 +000019 </ol></li>
20 <li><a href="#syntax">TableGen syntax</a>
Chris Lattner149a2492004-02-06 05:42:53 +000021 <ol>
Misha Brukman692cec02004-05-12 18:31:21 +000022 <li><a href="#primitives">TableGen primitives</a>
Chris Lattner149a2492004-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 Brukman692cec02004-05-12 18:31:21 +000027 </ol></li>
28 <li><a href="#classesdefs">Classes and definitions</a>
Chris Lattner149a2492004-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 Brukman692cec02004-05-12 18:31:21 +000033 </ol></li>
34 <li><a href="#filescope">File scope entities</a>
Chris Lattner149a2492004-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 Brukman692cec02004-05-12 18:31:21 +000038 </ol></li>
39 </ol></li>
40 <li><a href="#backends">TableGen backends</a>
Chris Lattner149a2492004-02-06 05:42:53 +000041 <ol>
Chris Lattner1215e322004-02-06 06:04:25 +000042 <li><a href="#">todo</a></li>
Misha Brukman692cec02004-05-12 18:31:21 +000043 </ol></li>
Chris Lattner149a2492004-02-06 05:42:53 +000044</ul>
Misha Brukman692cec02004-05-12 18:31:21 +000045</div>
Chris Lattner149a2492004-02-06 05:42:53 +000046
Chris Lattner020e1fc2004-05-23 21:07:27 +000047<div class="doc_author">
48 <p>Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a></p>
49</div>
50
Chris Lattner149a2492004-02-06 05:42:53 +000051<!-- *********************************************************************** -->
52<div class="doc_section"><a name="introduction">Introduction</a></div>
53<!-- *********************************************************************** -->
54
55<div class="doc_text">
56
57<p>TableGen's purpose is to help a human develop and maintain records of
58domain-specific information. Because there may be a large number of these
59records, it is specifically designed to allow writing flexible descriptions and
60for common features of these records to be factored out. This reduces the
61amount of duplication in the description, reduces the chance of error, and
62makes it easier to structure domain specific information.</p>
63
64<p>The core part of TableGen <a href="#syntax">parses a file</a>, instantiates
65the declarations, and hands the result off to a domain-specific "<a
66href="#backends">TableGen backend</a>" for processing. The current major user
Chris Lattner7f500132004-07-26 21:16:55 +000067of TableGen is the <a href="CodeGenerator.html">LLVM code generator</a>.</p>
Chris Lattner149a2492004-02-06 05:42:53 +000068
Misha Brukman692cec02004-05-12 18:31:21 +000069<p>Note that if you work on TableGen much, and use emacs or vim, that you can
70find an emacs "TableGen mode" and a vim language file in
71<tt>llvm/utils/emacs</tt> and <tt>llvm/utils/vim</tt> directory of your LLVM
72distribution, respectively.</p>
Chris Lattner1215e322004-02-06 06:04:25 +000073
Chris Lattner149a2492004-02-06 05:42:53 +000074</div>
75
76<!-- ======================================================================= -->
Misha Brukman692cec02004-05-12 18:31:21 +000077<div class="doc_subsection"><a name="running">Basic concepts</a></div>
Chris Lattner149a2492004-02-06 05:42:53 +000078
79<div class="doc_text">
80
Misha Brukman692cec02004-05-12 18:31:21 +000081<p>TableGen files consist of two key parts: 'classes' and 'definitions', both
82of which are considered 'records'.</p>
Chris Lattner149a2492004-02-06 05:42:53 +000083
Misha Brukman692cec02004-05-12 18:31:21 +000084<p><b>TableGen records</b> have a unique name, a list of values, and a list of
Chris Lattner149a2492004-02-06 05:42:53 +000085superclasses. The list of values is main data that TableGen builds for each
86record, it is this that holds the domain specific information for the
87application. The interpretation of this data is left to a specific <a
88href="#backends">TableGen backend</a>, but the structure and format rules are
Misha Brukman692cec02004-05-12 18:31:21 +000089taken care of and fixed by TableGen.</p>
Chris Lattner149a2492004-02-06 05:42:53 +000090
Misha Brukman692cec02004-05-12 18:31:21 +000091<p><b>TableGen definitions</b> are the concrete form of 'records'. These
92generally do not have any undefined values, and are marked with the
93'<tt>def</tt>' keyword.</p>
Chris Lattner149a2492004-02-06 05:42:53 +000094
Misha Brukman692cec02004-05-12 18:31:21 +000095<p><b>TableGen classes</b> are abstract records that are used to build and
96describe other records. These 'classes' allow the end-user to build
97abstractions for either the domain they are targetting (such as "Register",
98"RegisterClass", and "Instruction" in the LLVM code generator) or for the
99implementor to help factor out common properties of records (such as "FPInst",
100which is used to represent floating point instructions in the X86 backend).
101TableGen keeps track of all of the classes that are used to build up a
102definition, so the backend can find all definitions of a particular class, such
103as "Instruction".</p>
Chris Lattner149a2492004-02-06 05:42:53 +0000104
105</div>
106
107<!-- ======================================================================= -->
Misha Brukman692cec02004-05-12 18:31:21 +0000108<div class="doc_subsection"><a name="example">An example record</a></div>
Chris Lattner149a2492004-02-06 05:42:53 +0000109
110<div class="doc_text">
111
Misha Brukman692cec02004-05-12 18:31:21 +0000112<p>With no other arguments, TableGen parses the specified file and prints out
113all of the classes, then all of the definitions. This is a good way to see what
114the various definitions expand to fully. Running this on the <tt>X86.td</tt>
115file prints this (at the time of this writing):</p>
Chris Lattner149a2492004-02-06 05:42:53 +0000116
Chris Lattner149a2492004-02-06 05:42:53 +0000117<pre>
118...
Chris Lattner1215e322004-02-06 06:04:25 +0000119<b>def</b> ADDrr8 { <i>// Instruction X86Inst I2A8 Pattern</i>
120 <b>string</b> Name = "add";
121 <b>string</b> Namespace = "X86";
122 <b>list</b>&lt;Register&gt; Uses = [];
123 <b>list</b>&lt;Register&gt; Defs = [];
124 <b>bit</b> isReturn = 0;
125 <b>bit</b> isBranch = 0;
126 <b>bit</b> isCall = 0;
127 <b>bit</b> isTwoAddress = 1;
128 <b>bit</b> isTerminator = 0;
129 <b>dag</b> Pattern = (set R8, (plus R8, R8));
130 <b>bits</b>&lt;8&gt; Opcode = { 0, 0, 0, 0, 0, 0, 0, 0 };
Chris Lattner149a2492004-02-06 05:42:53 +0000131 Format Form = MRMDestReg;
Chris Lattner1215e322004-02-06 06:04:25 +0000132 <b>bits</b>&lt;5&gt; FormBits = { 0, 0, 0, 1, 1 };
Chris Lattner149a2492004-02-06 05:42:53 +0000133 ArgType Type = Arg8;
Chris Lattner1215e322004-02-06 06:04:25 +0000134 <b>bits</b>&lt;3&gt; TypeBits = { 0, 0, 1 };
135 <b>bit</b> hasOpSizePrefix = 0;
136 <b>bit</b> printImplicitUses = 0;
137 <b>bits</b>&lt;4&gt; Prefix = { 0, 0, 0, 0 };
Chris Lattner149a2492004-02-06 05:42:53 +0000138 FPFormat FPForm = ?;
Chris Lattner1215e322004-02-06 06:04:25 +0000139 <b>bits</b>&lt;3&gt; FPFormBits = { 0, 0, 0 };
Chris Lattner149a2492004-02-06 05:42:53 +0000140}
141...
Misha Brukman692cec02004-05-12 18:31:21 +0000142</pre>
Chris Lattner149a2492004-02-06 05:42:53 +0000143
Misha Brukman692cec02004-05-12 18:31:21 +0000144<p>This definition corresponds to an 8-bit register-register add instruction in
145the X86. The string after the '<tt>def</tt>' string indicates the name of the
Chris Lattner149a2492004-02-06 05:42:53 +0000146record ("<tt>ADDrr8</tt>" in this case), and the comment at the end of the line
147indicates the superclasses of the definition. The body of the record contains
148all of the data that TableGen assembled for the record, indicating that the
149instruction is part of the "X86" namespace, should be printed as "<tt>add</tt>"
150in the assembly file, it is a two-address instruction, has a particular
151encoding, etc. The contents and semantics of the information in the record is
Misha Brukman692cec02004-05-12 18:31:21 +0000152specific to the needs of the X86 backend, and is only shown as an example.</p>
Chris Lattner149a2492004-02-06 05:42:53 +0000153
Misha Brukman692cec02004-05-12 18:31:21 +0000154<p>As you can see, a lot of information is needed for every instruction
155supported by the code generator, and specifying it all manually would be
156unmaintainble, prone to bugs, and tiring to do in the first place. Because we
157are using TableGen, all of the information was derived from the following
158definition:</p>
Chris Lattner149a2492004-02-06 05:42:53 +0000159
Misha Brukman692cec02004-05-12 18:31:21 +0000160<pre>
Chris Lattner1215e322004-02-06 06:04:25 +0000161<b>def</b> ADDrr8 : I2A8&lt;"add", 0x00, MRMDestReg&gt;,
Chris Lattner149a2492004-02-06 05:42:53 +0000162 Pattern&lt;(set R8, (plus R8, R8))&gt;;
Misha Brukman692cec02004-05-12 18:31:21 +0000163</pre>
Chris Lattner149a2492004-02-06 05:42:53 +0000164
Misha Brukman692cec02004-05-12 18:31:21 +0000165<p>This definition makes use of the custom I2A8 (two address instruction with
1668-bit operand) class, which is defined in the X86-specific TableGen file to
167factor out the common features that instructions of its class share. A key
168feature of TableGen is that it allows the end-user to define the abstractions
169they prefer to use when describing their information.</p>
Chris Lattner149a2492004-02-06 05:42:53 +0000170
171</div>
172
173<!-- ======================================================================= -->
Misha Brukman692cec02004-05-12 18:31:21 +0000174<div class="doc_subsection"><a name="running">Running TableGen</a></div>
Chris Lattner149a2492004-02-06 05:42:53 +0000175
176<div class="doc_text">
177
Misha Brukman692cec02004-05-12 18:31:21 +0000178<p>TableGen runs just like any other LLVM tool. The first (optional) argument
Chris Lattner149a2492004-02-06 05:42:53 +0000179specifies the file to read. If a filename is not specified, <tt>tblgen</tt>
Misha Brukman692cec02004-05-12 18:31:21 +0000180reads from standard input.</p>
Chris Lattner149a2492004-02-06 05:42:53 +0000181
Misha Brukman692cec02004-05-12 18:31:21 +0000182<p>To be useful, one of the <a href="#backends">TableGen backends</a> must be
183used. These backends are selectable on the command line (type '<tt>tblgen
184--help</tt>' for a list). For example, to get a list of all of the definitions
185that subclass a particular type (which can be useful for building up an enum
186list of these records), use the <tt>--print-enums</tt> option:</p>
Chris Lattner149a2492004-02-06 05:42:53 +0000187
Misha Brukman692cec02004-05-12 18:31:21 +0000188<pre>
Chris Lattner149a2492004-02-06 05:42:53 +0000189$ tblgen X86.td -print-enums -class=Register
190AH, AL, AX, BH, BL, BP, BX, CH, CL, CX, DH, DI, DL, DX,
191EAX, EBP, EBX, ECX, EDI, EDX, ESI, ESP, FP0, FP1, FP2, FP3, FP4, FP5, FP6,
192SI, SP, ST0, ST1, ST2, ST3, ST4, ST5, ST6, ST7,
193
194$ tblgen X86.td -print-enums -class=Instruction
195ADCrr32, ADDri16, ADDri16b, ADDri32, ADDri32b, ADDri8, ADDrr16, ADDrr32,
196ADDrr8, ADJCALLSTACKDOWN, ADJCALLSTACKUP, ANDri16, ANDri16b, ANDri32, ANDri32b,
197ANDri8, ANDrr16, ANDrr32, ANDrr8, BSWAPr32, CALLm32, CALLpcrel32, ...
Misha Brukman692cec02004-05-12 18:31:21 +0000198</pre>
Chris Lattner149a2492004-02-06 05:42:53 +0000199
Misha Brukman692cec02004-05-12 18:31:21 +0000200<p>The default backend prints out all of the records, as described <a
201href="#example">above</a>.</p>
Chris Lattner149a2492004-02-06 05:42:53 +0000202
Misha Brukman692cec02004-05-12 18:31:21 +0000203<p>If you plan to use TableGen for some purpose, you will most likely have to
204<a href="#backends">write a backend</a> that extracts the information specific
205to what you need and formats it in the appropriate way.</p>
Chris Lattner149a2492004-02-06 05:42:53 +0000206
207</div>
208
209
210<!-- *********************************************************************** -->
211<div class="doc_section"><a name="syntax">TableGen syntax</a></div>
212<!-- *********************************************************************** -->
213
214<div class="doc_text">
Misha Brukman692cec02004-05-12 18:31:21 +0000215<p>TableGen doesn't care about the meaning of data (that is up to the backend
216to define), but it does care about syntax, and it enforces a simple type system.
Chris Lattner149a2492004-02-06 05:42:53 +0000217This section describes the syntax and the constructs allowed in a TableGen file.
218</p>
Chris Lattner149a2492004-02-06 05:42:53 +0000219</div>
220
221<!-- ======================================================================= -->
Misha Brukman692cec02004-05-12 18:31:21 +0000222<div class="doc_subsection"><a name="primitives">TableGen primitives</a></div>
Chris Lattner149a2492004-02-06 05:42:53 +0000223
Misha Brukman692cec02004-05-12 18:31:21 +0000224<!-- -------------------------------------------------------------------------->
225<div class="doc_subsubsection"><a name="comments">TableGen comments</a></div>
Chris Lattner149a2492004-02-06 05:42:53 +0000226
227<div class="doc_text">
Chris Lattner149a2492004-02-06 05:42:53 +0000228<p>TableGen supports BCPL style "<tt>//</tt>" comments, which run to the end of
229the line, and it also supports <b>nestable</b> "<tt>/* */</tt>" comments.</p>
Chris Lattner149a2492004-02-06 05:42:53 +0000230</div>
231
Misha Brukman692cec02004-05-12 18:31:21 +0000232<!-- -------------------------------------------------------------------------->
Misha Brukman94192b62004-06-03 16:59:59 +0000233<div class="doc_subsubsection">
234 <a name="types">The TableGen type system</a>
Chris Lattner149a2492004-02-06 05:42:53 +0000235</div>
236
237<div class="doc_text">
Misha Brukman692cec02004-05-12 18:31:21 +0000238<p>TableGen files are strongly typed, in a simple (but complete) type-system.
Chris Lattner149a2492004-02-06 05:42:53 +0000239These types are used to perform automatic conversions, check for errors, and to
240help interface designers constrain the input that they allow. Every <a
241href="#valuedef">value definition</a> is required to have an associated type.
242</p>
243
Misha Brukman692cec02004-05-12 18:31:21 +0000244<p>TableGen supports a mixture of very low-level types (such as <tt>bit</tt>)
245and very high-level types (such as <tt>dag</tt>). This flexibility is what
246allows it to describe a wide range of information conveniently and compactly.
247The TableGen types are:</p>
Chris Lattner149a2492004-02-06 05:42:53 +0000248
Chris Lattner149a2492004-02-06 05:42:53 +0000249<ul>
Chris Lattner1215e322004-02-06 06:04:25 +0000250<li>"<tt><b>bit</b></tt>" - A 'bit' is a boolean value that can hold either 0 or
Chris Lattner149a2492004-02-06 05:42:53 +00002511.</li>
252
Chris Lattner1215e322004-02-06 06:04:25 +0000253<li>"<tt><b>int</b></tt>" - The 'int' type represents a simple 32-bit integer
254value, such as 5.</li>
Chris Lattner149a2492004-02-06 05:42:53 +0000255
Chris Lattner1215e322004-02-06 06:04:25 +0000256<li>"<tt><b>string</b></tt>" - The 'string' type represents an ordered sequence
257of characters of arbitrary length.</li>
Chris Lattner149a2492004-02-06 05:42:53 +0000258
John Criswell407f6d42004-02-12 18:11:53 +0000259<li>"<tt><b>bits</b>&lt;n&gt;</tt>" - A 'bits' type is an arbitrary, but fixed,
Chris Lattner1215e322004-02-06 06:04:25 +0000260size integer that is broken up into individual bits. This type is useful
261because it can handle some bits being defined while others are undefined.</li>
Chris Lattner149a2492004-02-06 05:42:53 +0000262
Chris Lattner1215e322004-02-06 06:04:25 +0000263<li>"<tt><b>list</b>&lt;ty&gt;</tt>" - This type represents a list whose
264elements are some other type. The contained type is arbitrary: it can even be
265another list type.</li>
Chris Lattner149a2492004-02-06 05:42:53 +0000266
267<li>Class type - Specifying a class name in a type context means that the
268defined value must be a subclass of the specified class. This is useful in
269conjunction with the "list" type, for example, to constrain the elements of the
Chris Lattner1215e322004-02-06 06:04:25 +0000270list to a common base class (e.g., a <tt><b>list</b>&lt;Register&gt;</tt> can
271only contain definitions derived from the "<tt>Register</tt>" class).</li>
Chris Lattner149a2492004-02-06 05:42:53 +0000272
Chris Lattner1215e322004-02-06 06:04:25 +0000273<li>"<tt><b>code</b></tt>" - This represents a big hunk of text. NOTE: I don't
Chris Lattner149a2492004-02-06 05:42:53 +0000274remember why this is distinct from string!</li>
275
Chris Lattner1215e322004-02-06 06:04:25 +0000276<li>"<tt><b>dag</b></tt>" - This type represents a nestable directed graph of
Chris Lattner149a2492004-02-06 05:42:53 +0000277elements.</li>
278</ul>
Chris Lattner149a2492004-02-06 05:42:53 +0000279
Misha Brukman692cec02004-05-12 18:31:21 +0000280<p>To date, these types have been sufficient for describing things that
281TableGen has been used for, but it is straight-forward to extend this list if
282needed.</p>
Chris Lattner149a2492004-02-06 05:42:53 +0000283
284</div>
285
Misha Brukman692cec02004-05-12 18:31:21 +0000286<!-- -------------------------------------------------------------------------->
Chris Lattner149a2492004-02-06 05:42:53 +0000287<div class="doc_subsubsection">
Misha Brukman692cec02004-05-12 18:31:21 +0000288 <a name="values">TableGen values and expressions</a>
Chris Lattner149a2492004-02-06 05:42:53 +0000289</div>
290
Misha Brukman94192b62004-06-03 16:59:59 +0000291<div class="doc_text">
292
Misha Brukman692cec02004-05-12 18:31:21 +0000293<p>TableGen allows for a pretty reasonable number of different expression forms
Chris Lattner149a2492004-02-06 05:42:53 +0000294when building up values. These forms allow the TableGen file to be written in a
295natural syntax and flavor for the application. The current expression forms
Misha Brukman692cec02004-05-12 18:31:21 +0000296supported include:</p>
Chris Lattner149a2492004-02-06 05:42:53 +0000297
Misha Brukman692cec02004-05-12 18:31:21 +0000298<ul>
Misha Brukman0fc57a62004-07-28 22:09:29 +0000299<li><tt>?</tt> - uninitialized field</li>
300<li><tt>0b1001011</tt> - binary integer value</li>
301<li><tt>07654321</tt> - octal integer value (indicated by a leading 0)</li>
302<li><tt>7</tt> - decimal integer value</li>
303<li><tt>0x7F</tt> - hexadecimal integer value</li>
304<li><tt>"foo"</tt> - string value</li>
305<li><tt>[{ ... }]</tt> - code fragment</li>
306<li><tt>[ X, Y, Z ]</tt> - list value.</li>
307<li><tt>{ a, b, c }</tt> - initializer for a "bits&lt;3&gt;" value</li>
308<li><tt>value</tt> - value reference</li>
309<li><tt>value{17}</tt> - access to one bit of a value</li>
310<li><tt>value{15-17}</tt> - access to multiple bits of a value</li>
311<li><tt>DEF</tt> - reference to a record definition</li>
Chris Lattner808c6422005-09-08 18:47:21 +0000312<li><tt>CLASS&lt;val list&gt;</tt> - reference to a new anonymous definition of
313 CLASS with the specified template arguments.</li>
Misha Brukman0fc57a62004-07-28 22:09:29 +0000314<li><tt>X.Y</tt> - reference to the subfield of a value</li>
Misha Brukman95908882004-08-04 22:00:05 +0000315<li><tt>list[4-7,17,2-3]</tt> - A slice of the 'list' list, including elements
3164,5,6,7,17,2, and 3 from it. Elements may be included multiple times.</li>
Misha Brukman0fc57a62004-07-28 22:09:29 +0000317<li><tt>(DEF a, b)</tt> - a dag value. The first element is required to be a
318record definition, the remaining elements in the list may be arbitrary other
319values, including nested `<tt>dag</tt>' values.</li>
Misha Brukman692cec02004-05-12 18:31:21 +0000320</ul>
Chris Lattner149a2492004-02-06 05:42:53 +0000321
Misha Brukman692cec02004-05-12 18:31:21 +0000322<p>Note that all of the values have rules specifying how they convert to values
Chris Lattner149a2492004-02-06 05:42:53 +0000323for different types. These rules allow you to assign a value like "7" to a
Misha Brukman692cec02004-05-12 18:31:21 +0000324"bits&lt;4&gt;" value, for example.</p>
Chris Lattner149a2492004-02-06 05:42:53 +0000325
326</div>
327
Chris Lattner149a2492004-02-06 05:42:53 +0000328<!-- ======================================================================= -->
Misha Brukman94192b62004-06-03 16:59:59 +0000329<div class="doc_subsection">
330 <a name="classesdefs">Classes and definitions</a>
Chris Lattner149a2492004-02-06 05:42:53 +0000331</div>
332
Misha Brukman94192b62004-06-03 16:59:59 +0000333<div class="doc_text">
334
Misha Brukman692cec02004-05-12 18:31:21 +0000335<p>As mentioned in the <a href="#concepts">intro</a>, classes and definitions
Chris Lattner149a2492004-02-06 05:42:53 +0000336(collectively known as 'records') in TableGen are the main high-level unit of
337information that TableGen collects. Records are defined with a <tt>def</tt> or
338<tt>class</tt> keyword, the record name, and an optional list of "<a
Misha Brukman31551592004-06-03 23:42:24 +0000339href="#templateargs">template arguments</a>". If the record has superclasses,
Chris Lattner149a2492004-02-06 05:42:53 +0000340they are specified as a comma seperated list that starts with a colon character
341(":"). If <a href="#valuedef">value definitions</a> or <a href="#recordlet">let
John Criswell407f6d42004-02-12 18:11:53 +0000342expressions</a> are needed for the class, they are enclosed in curly braces
343("{}"); otherwise, the record ends with a semicolon. Here is a simple TableGen
Misha Brukman692cec02004-05-12 18:31:21 +0000344file:</p>
Chris Lattner149a2492004-02-06 05:42:53 +0000345
Misha Brukman692cec02004-05-12 18:31:21 +0000346<pre>
Chris Lattner1215e322004-02-06 06:04:25 +0000347<b>class</b> C { <b>bit</b> V = 1; }
348<b>def</b> X : C;
349<b>def</b> Y : C {
350 <b>string</b> Greeting = "hello";
Chris Lattner149a2492004-02-06 05:42:53 +0000351}
Misha Brukman692cec02004-05-12 18:31:21 +0000352</pre>
Chris Lattner149a2492004-02-06 05:42:53 +0000353
Misha Brukman692cec02004-05-12 18:31:21 +0000354<p>This example defines two definitions, <tt>X</tt> and <tt>Y</tt>, both of
355which derive from the <tt>C</tt> class. Because of this, they both get the
356<tt>V</tt> bit value. The <tt>Y</tt> definition also gets the Greeting member
357as well.</p>
Chris Lattner149a2492004-02-06 05:42:53 +0000358
Misha Brukman692cec02004-05-12 18:31:21 +0000359<p>In general, classes are useful for collecting together the commonality
360between a group of records and isolating it in a single place. Also, classes
361permit the specification of default values for their subclasses, allowing the
362subclasses to override them as they wish.</p>
Chris Lattner55927232004-02-06 06:37:00 +0000363
Chris Lattner149a2492004-02-06 05:42:53 +0000364</div>
365
Misha Brukman692cec02004-05-12 18:31:21 +0000366<!---------------------------------------------------------------------------->
Chris Lattner149a2492004-02-06 05:42:53 +0000367<div class="doc_subsubsection">
Misha Brukman692cec02004-05-12 18:31:21 +0000368 <a name="valuedef">Value definitions</a>
Chris Lattner149a2492004-02-06 05:42:53 +0000369</div>
370
371<div class="doc_text">
Misha Brukman692cec02004-05-12 18:31:21 +0000372<p>Value definitions define named entries in records. A value must be defined
John Criswell407f6d42004-02-12 18:11:53 +0000373before it can be referred to as the operand for another value definition or
Chris Lattner149a2492004-02-06 05:42:53 +0000374before the value is reset with a <a href="#recordlet">let expression</a>. A
375value is defined by specifying a <a href="#types">TableGen type</a> and a name.
376If an initial value is available, it may be specified after the type with an
Misha Brukman692cec02004-05-12 18:31:21 +0000377equal sign. Value definitions require terminating semicolons.</p>
Chris Lattner149a2492004-02-06 05:42:53 +0000378</div>
379
Misha Brukman692cec02004-05-12 18:31:21 +0000380<!-- -------------------------------------------------------------------------->
Chris Lattner149a2492004-02-06 05:42:53 +0000381<div class="doc_subsubsection">
Misha Brukman692cec02004-05-12 18:31:21 +0000382 <a name="recordlet">'let' expressions</a>
Chris Lattner149a2492004-02-06 05:42:53 +0000383</div>
384
385<div class="doc_text">
Misha Brukman692cec02004-05-12 18:31:21 +0000386<p>A record-level let expression is used to change the value of a value
387definition in a record. This is primarily useful when a superclass defines a
388value that a derived class or definition wants to override. Let expressions
389consist of the '<tt>let</tt>' keyword followed by a value name, an equal sign
390("="), and a new value. For example, a new class could be added to the example
391above, redefining the <tt>V</tt> field for all of its subclasses:</p>
Chris Lattner149a2492004-02-06 05:42:53 +0000392
Misha Brukman692cec02004-05-12 18:31:21 +0000393<pre>
Chris Lattner1215e322004-02-06 06:04:25 +0000394<b>class</b> D : C { let V = 0; }
395<b>def</b> Z : D;
Misha Brukman692cec02004-05-12 18:31:21 +0000396</pre>
Chris Lattner149a2492004-02-06 05:42:53 +0000397
Misha Brukman692cec02004-05-12 18:31:21 +0000398<p>In this case, the <tt>Z</tt> definition will have a zero value for its "V"
Chris Lattner149a2492004-02-06 05:42:53 +0000399value, despite the fact that it derives (indirectly) from the <tt>C</tt> class,
Misha Brukman692cec02004-05-12 18:31:21 +0000400because the <tt>D</tt> class overrode its value.</p>
Chris Lattner149a2492004-02-06 05:42:53 +0000401
402</div>
403
Misha Brukman692cec02004-05-12 18:31:21 +0000404<!-- -------------------------------------------------------------------------->
Chris Lattner149a2492004-02-06 05:42:53 +0000405<div class="doc_subsubsection">
Misha Brukman692cec02004-05-12 18:31:21 +0000406 <a name="templateargs">Class template arguments</a>
Chris Lattner149a2492004-02-06 05:42:53 +0000407</div>
408
409<div class="doc_text">
Misha Brukman692cec02004-05-12 18:31:21 +0000410<p>TableGen permits the definition of parameterized classes as well as normal
Chris Lattner55927232004-02-06 06:37:00 +0000411concrete classes. Parameterized TableGen classes specify a list of variable
412bindings (which may optionally have defaults) that are bound when used. Here is
413a simple example:</p>
414
Misha Brukman692cec02004-05-12 18:31:21 +0000415<pre>
Chris Lattner55927232004-02-06 06:37:00 +0000416<b>class</b> FPFormat&lt;<b>bits</b>&lt;3&gt; val&gt; {
417 <b>bits</b>&lt;3&gt; Value = val;
418}
419<b>def</b> NotFP : FPFormat&lt;0&gt;;
420<b>def</b> ZeroArgFP : FPFormat&lt;1&gt;;
421<b>def</b> OneArgFP : FPFormat&lt;2&gt;;
422<b>def</b> OneArgFPRW : FPFormat&lt;3&gt;;
423<b>def</b> TwoArgFP : FPFormat&lt;4&gt;;
424<b>def</b> SpecialFP : FPFormat&lt;5&gt;;
Misha Brukman692cec02004-05-12 18:31:21 +0000425</pre>
Chris Lattner55927232004-02-06 06:37:00 +0000426
Misha Brukman692cec02004-05-12 18:31:21 +0000427<p>In this case, template arguments are used as a space efficient way to specify
428a list of "enumeration values", each with a "Value" field set to the specified
Chris Lattner55927232004-02-06 06:37:00 +0000429integer.</p>
430
431<p>The more esoteric forms of <a href="#values">TableGen expressions</a> are
432useful in conjunction with template arguments. As an example:</p>
433
Misha Brukman692cec02004-05-12 18:31:21 +0000434<pre>
Chris Lattner55927232004-02-06 06:37:00 +0000435<b>class</b> ModRefVal&lt;<b>bits</b>&lt;2&gt; val&gt; {
436 <b>bits</b>&lt;2&gt; Value = val;
437}
438
439<b>def</b> None : ModRefVal&lt;0&gt;;
440<b>def</b> Mod : ModRefVal&lt;1&gt;;
441<b>def</b> Ref : ModRefVal&lt;2&gt;;
442<b>def</b> ModRef : ModRefVal&lt;3&gt;;
443
444<b>class</b> Value&lt;ModRefVal MR&gt; {
445 <i>// decode some information into a more convenient format, while providing
446 // a nice interface to the user of the "Value" class.</i>
447 <b>bit</b> isMod = MR.Value{0};
448 <b>bit</b> isRef = MR.Value{1};
449
450 <i>// other stuff...</i>
451}
452
453<i>// Example uses</i>
454<b>def</b> bork : Value&lt;Mod&gt;;
455<b>def</b> zork : Value&lt;Ref&gt;;
456<b>def</b> hork : Value&lt;ModRef&gt;;
Misha Brukman692cec02004-05-12 18:31:21 +0000457</pre>
Chris Lattner55927232004-02-06 06:37:00 +0000458
Misha Brukman692cec02004-05-12 18:31:21 +0000459<p>This is obviously a contrived example, but it shows how template arguments
460can be used to decouple the interface provided to the user of the class from the
Chris Lattner55927232004-02-06 06:37:00 +0000461actual internal data representation expected by the class. In this case,
462running <tt>tblgen</tt> on the example prints the following definitions:</p>
463
Misha Brukman692cec02004-05-12 18:31:21 +0000464<pre>
Chris Lattner55927232004-02-06 06:37:00 +0000465<b>def</b> bork { <i>// Value</i>
466 bit isMod = 1;
467 bit isRef = 0;
468}
469<b>def</b> hork { <i>// Value</i>
470 bit isMod = 1;
471 bit isRef = 1;
472}
473<b>def</b> zork { <i>// Value</i>
474 bit isMod = 0;
475 bit isRef = 1;
476}
Misha Brukman692cec02004-05-12 18:31:21 +0000477</pre>
Chris Lattner55927232004-02-06 06:37:00 +0000478
Misha Brukman692cec02004-05-12 18:31:21 +0000479<p> This shows that TableGen was able to dig into the argument and extract a
480piece of information that was requested by the designer of the "Value" class.
481For more realistic examples, please see existing users of TableGen, such as the
482X86 backend.</p>
Chris Lattner55927232004-02-06 06:37:00 +0000483
Chris Lattner149a2492004-02-06 05:42:53 +0000484</div>
485
Chris Lattner149a2492004-02-06 05:42:53 +0000486<!-- ======================================================================= -->
487<div class="doc_subsection">
Misha Brukman692cec02004-05-12 18:31:21 +0000488 <a name="filescope">File scope entities</a>
Chris Lattner149a2492004-02-06 05:42:53 +0000489</div>
490
Misha Brukman692cec02004-05-12 18:31:21 +0000491<!-- -------------------------------------------------------------------------->
Chris Lattner149a2492004-02-06 05:42:53 +0000492<div class="doc_subsubsection">
Misha Brukman692cec02004-05-12 18:31:21 +0000493 <a name="include">File inclusion</a>
Chris Lattner149a2492004-02-06 05:42:53 +0000494</div>
495
496<div class="doc_text">
Misha Brukman692cec02004-05-12 18:31:21 +0000497<p>TableGen supports the '<tt>include</tt>' token, which textually substitutes
498the specified file in place of the include directive. The filename should be
Chris Lattner149a2492004-02-06 05:42:53 +0000499specified as a double quoted string immediately after the '<tt>include</tt>'
Misha Brukman692cec02004-05-12 18:31:21 +0000500keyword. Example:</p>
Chris Lattner149a2492004-02-06 05:42:53 +0000501
Misha Brukman692cec02004-05-12 18:31:21 +0000502<pre>
Chris Lattner55927232004-02-06 06:37:00 +0000503<b>include</b> "foo.td"
Misha Brukman692cec02004-05-12 18:31:21 +0000504</pre>
Chris Lattner149a2492004-02-06 05:42:53 +0000505
506</div>
507
Misha Brukman692cec02004-05-12 18:31:21 +0000508<!-- -------------------------------------------------------------------------->
Chris Lattner149a2492004-02-06 05:42:53 +0000509<div class="doc_subsubsection">
Misha Brukman692cec02004-05-12 18:31:21 +0000510 <a name="globallet">'let' expressions</a>
Chris Lattner149a2492004-02-06 05:42:53 +0000511</div>
512
513<div class="doc_text">
Misha Brukman692cec02004-05-12 18:31:21 +0000514<p> "let" expressions at file scope are similar to <a href="#recordlet">"let"
Chris Lattner149a2492004-02-06 05:42:53 +0000515expressions within a record</a>, except they can specify a value binding for
516multiple records at a time, and may be useful in certain other cases.
517File-scope let expressions are really just another way that TableGen allows the
Misha Brukman692cec02004-05-12 18:31:21 +0000518end-user to factor out commonality from the records.</p>
Chris Lattner149a2492004-02-06 05:42:53 +0000519
Misha Brukman692cec02004-05-12 18:31:21 +0000520<p>File-scope "let" expressions take a comma-seperated list of bindings to
521apply, and one of more records to bind the values in. Here are some
522examples:</p>
Chris Lattner149a2492004-02-06 05:42:53 +0000523
Misha Brukman692cec02004-05-12 18:31:21 +0000524<pre>
Chris Lattner1215e322004-02-06 06:04:25 +0000525<b>let</b> isTerminator = 1, isReturn = 1 <b>in</b>
526 <b>def</b> RET : X86Inst&lt;"ret", 0xC3, RawFrm, NoArg&gt;;
Chris Lattner149a2492004-02-06 05:42:53 +0000527
Chris Lattner1215e322004-02-06 06:04:25 +0000528<b>let</b> isCall = 1 <b>in</b>
529 <i>// All calls clobber the non-callee saved registers...</i>
530 <b>let</b> Defs = [EAX, ECX, EDX, FP0, FP1, FP2, FP3, FP4, FP5, FP6] in {
531 <b>def</b> CALLpcrel32 : X86Inst&lt;"call", 0xE8, RawFrm, NoArg&gt;;
532 <b>def</b> CALLr32 : X86Inst&lt;"call", 0xFF, MRMS2r, Arg32&gt;;
533 <b>def</b> CALLm32 : X86Inst&lt;"call", 0xFF, MRMS2m, Arg32&gt;;
Chris Lattner149a2492004-02-06 05:42:53 +0000534 }
Misha Brukman692cec02004-05-12 18:31:21 +0000535</pre>
Chris Lattner149a2492004-02-06 05:42:53 +0000536
Misha Brukman692cec02004-05-12 18:31:21 +0000537<p>File-scope "let" expressions are often useful when a couple of definitions
538need to be added to several records, and the records do not otherwise need to be
539opened, as in the case with the CALL* instructions above.</p>
Chris Lattner149a2492004-02-06 05:42:53 +0000540</div>
541
Chris Lattner149a2492004-02-06 05:42:53 +0000542<!-- *********************************************************************** -->
543<div class="doc_section"><a name="backends">TableGen backends</a></div>
544<!-- *********************************************************************** -->
545
546<div class="doc_text">
Misha Brukman692cec02004-05-12 18:31:21 +0000547<p>How they work, how to write one. This section should not contain details
548about any particular backend, except maybe -print-enums as an example. This
549should highlight the APIs in <tt>TableGen/Record.h</tt>.</p>
Chris Lattner149a2492004-02-06 05:42:53 +0000550</div>
551
Chris Lattner149a2492004-02-06 05:42:53 +0000552<!-- *********************************************************************** -->
Misha Brukman692cec02004-05-12 18:31:21 +0000553
Chris Lattner149a2492004-02-06 05:42:53 +0000554<hr>
Misha Brukman692cec02004-05-12 18:31:21 +0000555<address>
556 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
557 src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a>
558 <a href="http://validator.w3.org/check/referer"><img
559 src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!" /></a>
560
561 <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
562 <a href="http://llvm.cs.uiuc.edu">LLVM Compiler Infrastructure</a><br>
Chris Lattner149a2492004-02-06 05:42:53 +0000563 Last modified: $Date$
Misha Brukman692cec02004-05-12 18:31:21 +0000564</address>
Chris Lattner149a2492004-02-06 05:42:53 +0000565
566</body>
567</html>