add a rule for enums, patch by Zhanyong Wan!
[oota-llvm.git] / docs / TableGenFundamentals.html
index ee1d5b1fe015f1fe45ba74362298506426f3e3cc..f09e0d06420e5f99f44db1bec1fabb7683f5f06c 100644 (file)
@@ -104,8 +104,10 @@ definition, so the backend can find all definitions of a particular class, such
 as "Instruction".</p>
 
 <p><b>TableGen multiclasses</b> are groups of abstract records that are
-instantiated all at once.  Each instantiation can result in multiple TableGen
-definitions.</p>
+instantiated all at once.  Each instantiation can result in multiple
+TableGen definitions.  If a multiclass inherits from another multiclass,
+the definitions in the sub-multiclass become part of the current
+multiclass, as if they were declared in the current multiclass.</p>
 
 </div>
 
@@ -138,22 +140,20 @@ file prints this (at the time of this writing):</p>
   <b>bit</b> isIndirectBranch = 0;
   <b>bit</b> isBarrier = 0;
   <b>bit</b> isCall = 0;
-  <b>bit</b> isSimpleLoad = 0;
+  <b>bit</b> canFoldAsLoad = 0;
   <b>bit</b> mayLoad = 0;
   <b>bit</b> mayStore = 0;
   <b>bit</b> isImplicitDef = 0;
-  <b>bit</b> isTwoAddress = 1;
   <b>bit</b> isConvertibleToThreeAddress = 1;
   <b>bit</b> isCommutable = 1;
   <b>bit</b> isTerminator = 0;
   <b>bit</b> isReMaterializable = 0;
   <b>bit</b> isPredicable = 0;
   <b>bit</b> hasDelaySlot = 0;
-  <b>bit</b> usesCustomDAGSchedInserter = 0;
+  <b>bit</b> usesCustomInserter = 0;
   <b>bit</b> hasCtrlDep = 0;
   <b>bit</b> isNotDuplicable = 0;
   <b>bit</b> hasSideEffects = 0;
-  <b>bit</b> mayHaveSideEffects = 0;
   <b>bit</b> neverHasSideEffects = 0;
   InstrItinClass Itinerary = NoItinerary;
   <b>string</b> Constraints = "";
@@ -187,7 +187,7 @@ backend, and is only shown as an example.</p>
 
 <p>As you can see, a lot of information is needed for every instruction
 supported by the code generator, and specifying it all manually would be
-unmaintainble, prone to bugs, and tiring to do in the first place.  Because we
+unmaintainable, prone to bugs, and tiring to do in the first place.  Because we
 are using TableGen, all of the information was derived from the following
 definition:</p>
 
@@ -301,43 +301,41 @@ and very high-level types (such as <tt>dag</tt>).  This flexibility is what
 allows it to describe a wide range of information conveniently and compactly.
 The TableGen types are:</p>
 
-<ul>
 <dl>
-
-<di><tt><b>bit</b></tt></di>
+<dt><tt><b>bit</b></tt></dt>
   <dd>A 'bit' is a boolean value that can hold either 0 or 1.</dd>
 
-<di><tt><b>int</b></tt></di>
+<dt><tt><b>int</b></tt></dt>
   <dd>The 'int' type represents a simple 32-bit integer value, such as 5.</dd>
 
-<di><tt><b>string</b></tt></di>
+<dt><tt><b>string</b></tt></dt>
   <dd>The 'string' type represents an ordered sequence of characters of
   arbitrary length.</dd>
 
-<di><tt><b>bits</b>&lt;n&gt;</tt></di>
+<dt><tt><b>bits</b>&lt;n&gt;</tt></dt>
   <dd>A 'bits' type is an arbitrary, but fixed, size integer that is broken up
   into individual bits.  This type is useful because it can handle some bits
   being defined while others are undefined.</dd>
 
-<di><tt><b>list</b>&lt;ty&gt;</tt></di>
+<dt><tt><b>list</b>&lt;ty&gt;</tt></dt>
   <dd>This type represents a list whose elements are some other type.  The
   contained type is arbitrary: it can even be another list type.</dd>
 
-<di>Class type</di>
+<dt>Class type</dt>
   <dd>Specifying a class name in a type context means that the defined value
   must be a subclass of the specified class.  This is useful in conjunction with
   the <b><tt>list</tt></b> type, for example, to constrain the elements of the
   list to a common base class (e.g., a <tt><b>list</b>&lt;Register&gt;</tt> can
   only contain definitions derived from the "<tt>Register</tt>" class).</dd>
 
-<di><tt><b>dag</b></tt></di>
+<dt><tt><b>dag</b></tt></dt>
   <dd>This type represents a nestable directed graph of elements.</dd>
 
-<di><tt><b>code</b></tt></di>
-  <dd>This represents a big hunk of text. NOTE: I don't remember why this is
-  distinct from string!</dd>
+<dt><tt><b>code</b></tt></dt>
+  <dd>This represents a big hunk of text.  This is lexically distinct from 
+  string values because it doesn't require escapeing double quotes and other
+  common characters that occur in code.</dd>
 </dl>
-</ul>
 
 <p>To date, these types have been sufficient for describing things that
 TableGen has been used for, but it is straight-forward to extend this list if
@@ -357,51 +355,77 @@ when building up values.  These forms allow the TableGen file to be written in a
 natural syntax and flavor for the application.  The current expression forms
 supported include:</p>
 
-<ul>
 <dl>
-<di><tt>?</tt></di>
+<dt><tt>?</tt></dt>
   <dd>uninitialized field</dd>
-<di><tt>0b1001011</tt></di>
+<dt><tt>0b1001011</tt></dt>
   <dd>binary integer value</dd>
-<di><tt>07654321</tt></di>
+<dt><tt>07654321</tt></dt>
   <dd>octal integer value (indicated by a leading 0)</dd>
-<di><tt>7</tt></di>
+<dt><tt>7</tt></dt>
   <dd>decimal integer value</dd>
-<di><tt>0x7F</tt></di>
+<dt><tt>0x7F</tt></dt>
   <dd>hexadecimal integer value</dd>
-<di><tt>"foo"</tt></di>
+<dt><tt>"foo"</tt></dt>
   <dd>string value</dd>
-<di><tt>[{ ... }]</tt></di>
+<dt><tt>[{ ... }]</tt></dt>
   <dd>code fragment</dd>
-<di><tt>[ X, Y, Z ]</tt></di>
-  <dd>list value.</dd>
-<di><tt>{ a, b, c }</tt></di>
+<dt><tt>[ X, Y, Z ]&lt;type&gt;</tt></dt>
+  <dd>list value.  &lt;type&gt; is the type of the list 
+element and is usually optional.  In rare cases,
+TableGen is unable to deduce the element type in
+which case the user must specify it explicitly.</dd>
+<dt><tt>{ a, b, c }</tt></dt>
   <dd>initializer for a "bits&lt;3&gt;" value</dd>
-<di><tt>value</tt></di>
+<dt><tt>value</tt></dt>
   <dd>value reference</dd>
-<di><tt>value{17}</tt></di>
+<dt><tt>value{17}</tt></dt>
   <dd>access to one bit of a value</dd>
-<di><tt>value{15-17}</tt></di>
+<dt><tt>value{15-17}</tt></dt>
   <dd>access to multiple bits of a value</dd>
-<di><tt>DEF</tt></di>
+<dt><tt>DEF</tt></dt>
   <dd>reference to a record definition</dd>
-<di><tt>CLASS&lt;val list&gt;</tt></di>
+<dt><tt>CLASS&lt;val list&gt;</tt></dt>
   <dd>reference to a new anonymous definition of CLASS with the specified
       template arguments.</dd>
-<di><tt>X.Y</tt></di>
+<dt><tt>X.Y</tt></dt>
   <dd>reference to the subfield of a value</dd>
-<di><tt>list[4-7,17,2-3]</tt></di>
+<dt><tt>list[4-7,17,2-3]</tt></dt>
   <dd>A slice of the 'list' list, including elements 4,5,6,7,17,2, and 3 from
   it.  Elements may be included multiple times.</dd>
-<di><tt>(DEF a, b)</tt></di>
+<dt><tt>(DEF a, b)</tt></dt>
   <dd>a dag value.  The first element is required to be a record definition, the
   remaining elements in the list may be arbitrary other values, including nested
   `<tt>dag</tt>' values.</dd>
-<di><tt>!strconcat(a, b)</tt></di>
+<dt><tt>!strconcat(a, b)</tt></dt>
   <dd>A string value that is the result of concatenating the 'a' and 'b'
   strings.</dd>
+<dt><tt>!cast&lt;type&gt;(a)</tt></dt>
+  <dd>A symbol of type <em>type</em> obtained by looking up the string 'a' in
+the symbol table.  If the type of 'a' does not match <em>type</em>, TableGen
+aborts with an error. !cast&lt;string&gt; is a special case in that the argument must
+be an object defined by a 'def' construct.</dd>
+<dt><tt>!subst(a, b, c)</tt></dt>
+  <dd>If 'a' and 'b' are of string type or are symbol references, substitute 
+'b' for 'a' in 'c.'  This operation is analogous to $(subst) in GNU make.</dd>
+<dt><tt>!foreach(a, b, c)</tt></dt>
+  <dd>For each member 'b' of dag or list 'a' apply operator 'c.'  'b' is a 
+dummy variable that should be declared as a member variable of an instantiated 
+class.  This operation is analogous to $(foreach) in GNU make.</dd>
+<dt><tt>!car(a)</tt></dt>
+  <dd>The first element of list 'a.'</dd>
+<dt><tt>!cdr(a)</tt></dt>
+  <dd>The 2nd-N elements of list 'a.'</dd>
+<dt><tt>!null(a)</tt></dt>
+  <dd>An integer {0,1} indicating whether list 'a' is empty.</dd>
+<dt><tt>!if(a,b,c)</tt></dt>
+  <dd>'b' if the result of 'int' or 'bit' operator 'a' is nonzero,
+      'c' otherwise.</dd>
+<dt><tt>!eq(a,b)</tt></dt>
+  <dd>'bit 1' if string a is equal to string b, 0 otherwise.  This
+      only operates on string, int and bit objects.  Use !cast&lt;string&gt; to
+      compare other types of objects.</dd>
 </dl>
-</ul>
 
 <p>Note that all of the values have rules specifying how they convert to values
 for different types.  These rules allow you to assign a value like "<tt>7</tt>"
@@ -630,8 +654,10 @@ Here is an example TableGen fragment that shows this idea:
 
 <p>The name of the resultant definitions has the multidef fragment names
    appended to them, so this defines <tt>ADD_rr</tt>, <tt>ADD_ri</tt>,
-   <tt>SUB_rr</tt>, etc.  Using a multiclass this way is exactly equivalent to
-   instantiating the classes multiple times yourself, e.g. by writing:</p>
+   <tt>SUB_rr</tt>, etc.  A defm may inherit from multiple multiclasses,
+   instantiating definitions from each multiclass.  Using a multiclass
+   this way is exactly equivalent to instantiating the classes multiple
+   times yourself, e.g. by writing:</p>
 
 <div class="doc_code">
 <pre>
@@ -659,6 +685,91 @@ Here is an example TableGen fragment that shows this idea:
 </pre>
 </div>
 
+<p>
+A defm can also be used inside a multiclass providing several levels of
+multiclass instanciations.
+</p>
+
+<div class="doc_code">
+<pre>
+<b>class</b> Instruction&lt;bits&lt;4&gt; opc, string Name&gt; {
+  bits&lt;4&gt; opcode = opc;
+  string name = Name;
+}
+
+<b>multiclass</b> basic_r&lt;bits&lt;4&gt; opc&gt; {
+  <b>def</b> rr : Instruction&lt;opc, "rr"&gt;;
+  <b>def</b> rm : Instruction&lt;opc, "rm"&gt;;
+}
+
+<b>multiclass</b> basic_s&lt;bits&lt;4&gt; opc&gt; {
+  <b>defm</b> SS : basic_r&lt;opc&gt;;
+  <b>defm</b> SD : basic_r&lt;opc&gt;;
+  <b>def</b> X : Instruction&lt;opc, "x"&gt;;
+}
+
+<b>multiclass</b> basic_p&lt;bits&lt;4&gt; opc&gt; {
+  <b>defm</b> PS : basic_r&lt;opc&gt;;
+  <b>defm</b> PD : basic_r&lt;opc&gt;;
+  <b>def</b> Y : Instruction&lt;opc, "y"&gt;;
+}
+
+<b>defm</b> ADD : basic_s&lt;0xf&gt;, basic_p&lt;0xf&gt;;
+...
+
+<i>// Results</i>
+<b>def</b> ADDPDrm { ...
+<b>def</b> ADDPDrr { ...
+<b>def</b> ADDPSrm { ...
+<b>def</b> ADDPSrr { ...
+<b>def</b> ADDSDrm { ...
+<b>def</b> ADDSDrr { ...
+<b>def</b> ADDY { ...
+<b>def</b> ADDX { ...
+</pre>
+</div>
+
+<p>
+defm declarations can inherit from classes too, the
+rule to follow is that the class list must start after the
+last multiclass, and there must be at least one multiclass
+before them.
+</p>
+
+<div class="doc_code">
+<pre>
+<b>class</b> XD { bits&lt;4&gt; Prefix = 11; }
+<b>class</b> XS { bits&lt;4&gt; Prefix = 12; }
+
+<b>class</b> I&lt;bits<4&gt; op> {
+  bits&lt;4&gt; opcode = op;
+}
+
+<b>multiclass</b> R {
+  <b>def</b> rr : I&lt;4&gt;;
+  <b>def</b> rm : I&lt;2&gt;;
+}
+
+<b>multiclass</b> Y {
+  <b>defm</b> SS : R, XD;
+  <b>defm</b> SD : R, XS;
+}
+
+<b>defm</b> Instr : Y;
+
+<i>// Results</i>
+<b>def</b> InstrSDrm {
+  bits&lt;4&gt; opcode = { 0, 0, 1, 0 };
+  bits&lt;4&gt; Prefix = { 1, 1, 0, 0 };
+}
+...
+<b>def</b> InstrSSrr {
+  bits&lt;4&gt; opcode = { 0, 1, 0, 0 };
+  bits&lt;4&gt; Prefix = { 1, 0, 1, 1 };
+}
+</pre>
+</div>
+
 </div>
 
 <!-- ======================================================================= -->
@@ -699,7 +810,7 @@ File-scope let expressions are really just another way that TableGen allows the
 end-user to factor out commonality from the records.</p>
 
 <p>File-scope "let" expressions take a comma-separated list of bindings to
-apply, and one of more records to bind the values in.  Here are some
+apply, and one or more records to bind the values in.  Here are some
 examples:</p>
 
 <div class="doc_code">
@@ -712,12 +823,12 @@ examples:</p>
   <b>let</b> Defs = [EAX, ECX, EDX, FP0, FP1, FP2, FP3, FP4, FP5, FP6, ST0,
               MM0, MM1, MM2, MM3, MM4, MM5, MM6, MM7,
               XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6, XMM7, EFLAGS] <b>in</b> {
-    <b>def</b> CALLpcrel32 : Ii32<0xE8, RawFrm, (outs), (ins i32imm:$dst,variable_ops),
-                           "call\t${dst:call}", []>;
-    <b>def</b> CALL32r     : I<0xFF, MRM2r, (outs), (ins GR32:$dst, variable_ops),
-                        "call\t{*}$dst", [(X86call GR32:$dst)]>;
-    <b>def</b> CALL32m     : I<0xFF, MRM2m, (outs), (ins i32mem:$dst, variable_ops),
-                        "call\t{*}$dst", []>;
+    <b>def</b> CALLpcrel32 : Ii32&lt;0xE8, RawFrm, (outs), (ins i32imm:$dst,variable_ops),
+                           "call\t${dst:call}", []&gt;;
+    <b>def</b> CALL32r     : I&lt;0xFF, MRM2r, (outs), (ins GR32:$dst, variable_ops),
+                        "call\t{*}$dst", [(X86call GR32:$dst)]&gt;;
+    <b>def</b> CALL32m     : I&lt;0xFF, MRM2m, (outs), (ins i32mem:$dst, variable_ops),
+                        "call\t{*}$dst", []&gt;;
   }
 </pre>
 </div>
@@ -726,6 +837,48 @@ examples:</p>
 need to be added to several records, and the records do not otherwise need to be
 opened, as in the case with the <tt>CALL*</tt> instructions above.</p>
 
+<p>It's also possible to use "let" expressions inside multiclasses, providing
+more ways to factor out commonality from the records, specially if using
+several levels of multiclass instanciations. This also avoids the need of using
+"let" expressions within subsequent records inside a multiclass.</p> 
+
+<pre class="doc_code">
+<b>multiclass </b>basic_r&lt;bits&lt;4&gt; opc&gt; {
+  <b>let </b>Predicates = [HasSSE2] in {
+    <b>def </b>rr : Instruction&lt;opc, "rr"&gt;;
+    <b>def </b>rm : Instruction&lt;opc, "rm"&gt;;
+  }
+  <b>let </b>Predicates = [HasSSE3] in
+    <b>def </b>rx : Instruction&lt;opc, "rx"&gt;;
+}
+
+<b>multiclass </b>basic_ss&lt;bits&lt;4&gt; opc&gt; {
+  <b>let </b>IsDouble = 0 in
+    <b>defm </b>SS : basic_r&lt;opc&gt;;
+
+  <b>let </b>IsDouble = 1 in
+    <b>defm </b>SD : basic_r&lt;opc&gt;;
+}
+
+<b>defm </b>ADD : basic_ss&lt;0xf&gt;;
+</pre>
+</div>
+
+<!-- *********************************************************************** -->
+<div class="doc_section"><a name="codegen">Code Generator backend info</a></div>
+<!-- *********************************************************************** -->
+
+<div class="doc_text">
+
+<p>Expressions used by code generator to describe instructions and isel
+patterns:</p>
+
+<dl>
+<dt><tt>(implicit a)</tt></dt>
+  <dd>an implicitly defined physical register.  This tells the dag instruction
+  selection emitter the input pattern's extra definitions matches implicit
+  physical register definitions.</dd>
+</dl>
 </div>
 
 <!-- *********************************************************************** -->
@@ -745,9 +898,9 @@ This should highlight the APIs in <tt>TableGen/Record.h</tt>.</p>
 <hr>
 <address>
   <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
-  src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a>
+  src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a>
   <a href="http://validator.w3.org/check/referer"><img
-  src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!" /></a>
+  src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"></a>
 
   <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
   <a href="http://llvm.org">LLVM Compiler Infrastructure</a><br>