<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="X-UA-Compatible" content="IE=Edge" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>TableGen Language Introduction — LLVM 8 documentation</title> <link rel="stylesheet" href="../_static/llvm-theme.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/language_data.js"></script> <link rel="index" title="Index" href="../genindex.html" /> <link rel="search" title="Search" href="../search.html" /> <link rel="next" title="TableGen Deficiencies" href="Deficiencies.html" /> <link rel="prev" title="TableGen Language Reference" href="LangRef.html" /> <style type="text/css"> table.right { float: right; margin-left: 20px; } table.right td { border: 1px solid #ccc; } </style> </head><body> <div class="logo"> <a href="../index.html"> <img src="../_static/logo.png" alt="LLVM Logo" width="250" height="88"/></a> </div> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="Deficiencies.html" title="TableGen Deficiencies" accesskey="N">next</a> |</li> <li class="right" > <a href="LangRef.html" title="TableGen Language Reference" accesskey="P">previous</a> |</li> <li><a href="http://llvm.org/">LLVM Home</a> | </li> <li><a href="../index.html">Documentation</a>»</li> <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">TableGen</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="body" role="main"> <div class="section" id="tablegen-language-introduction"> <h1>TableGen Language Introduction<a class="headerlink" href="#tablegen-language-introduction" title="Permalink to this headline">¶</a></h1> <div class="contents local topic" id="contents"> <ul class="simple"> <li><a class="reference internal" href="#introduction" id="id4">Introduction</a></li> <li><a class="reference internal" href="#tablegen-syntax" id="id5">TableGen syntax</a><ul> <li><a class="reference internal" href="#tablegen-primitives" id="id6">TableGen primitives</a><ul> <li><a class="reference internal" href="#tablegen-comments" id="id7">TableGen comments</a></li> <li><a class="reference internal" href="#the-tablegen-type-system" id="id8">The TableGen type system</a></li> <li><a class="reference internal" href="#tablegen-values-and-expressions" id="id9">TableGen values and expressions</a></li> </ul> </li> <li><a class="reference internal" href="#classes-and-definitions" id="id10">Classes and definitions</a><ul> <li><a class="reference internal" href="#value-definitions" id="id11">Value definitions</a></li> <li><a class="reference internal" href="#let-expressions-within-a-record" id="id12">‘let’ expressions</a></li> <li><a class="reference internal" href="#class-template-arguments" id="id13">Class template arguments</a></li> <li><a class="reference internal" href="#multiclass-definitions-and-instances" id="id14">Multiclass definitions and instances</a></li> </ul> </li> <li><a class="reference internal" href="#file-scope-entities" id="id15">File scope entities</a><ul> <li><a class="reference internal" href="#file-inclusion" id="id16">File inclusion</a></li> <li><a class="reference internal" href="#id3" id="id17">‘let’ expressions</a></li> <li><a class="reference internal" href="#looping" id="id18">Looping</a></li> </ul> </li> </ul> </li> <li><a class="reference internal" href="#code-generator-backend-info" id="id19">Code Generator backend info</a></li> </ul> </div> <div class="admonition warning"> <p class="first admonition-title">Warning</p> <p class="last">This document is extremely rough. If you find something lacking, please fix it, file a documentation bug, or ask about it on llvm-dev.</p> </div> <div class="section" id="introduction"> <h2><a class="toc-backref" href="#id4">Introduction</a><a class="headerlink" href="#introduction" title="Permalink to this headline">¶</a></h2> <p>This document is not meant to be a normative spec about the TableGen language in and of itself (i.e. how to understand a given construct in terms of how it affects the final set of records represented by the TableGen file). For the formal language specification, see <a class="reference internal" href="LangRef.html"><span class="doc">TableGen Language Reference</span></a>.</p> </div> <div class="section" id="tablegen-syntax"> <h2><a class="toc-backref" href="#id5">TableGen syntax</a><a class="headerlink" href="#tablegen-syntax" title="Permalink to this headline">¶</a></h2> <p>TableGen doesn’t care about the meaning of data (that is up to the backend to define), but it does care about syntax, and it enforces a simple type system. This section describes the syntax and the constructs allowed in a TableGen file.</p> <div class="section" id="tablegen-primitives"> <h3><a class="toc-backref" href="#id6">TableGen primitives</a><a class="headerlink" href="#tablegen-primitives" title="Permalink to this headline">¶</a></h3> <div class="section" id="tablegen-comments"> <h4><a class="toc-backref" href="#id7">TableGen comments</a><a class="headerlink" href="#tablegen-comments" title="Permalink to this headline">¶</a></h4> <p>TableGen supports C++ style “<code class="docutils literal notranslate"><span class="pre">//</span></code>” comments, which run to the end of the line, and it also supports <strong>nestable</strong> “<code class="docutils literal notranslate"><span class="pre">/*</span> <span class="pre">*/</span></code>” comments.</p> </div> <div class="section" id="the-tablegen-type-system"> <span id="tablegen-type"></span><h4><a class="toc-backref" href="#id8">The TableGen type system</a><a class="headerlink" href="#the-tablegen-type-system" title="Permalink to this headline">¶</a></h4> <p>TableGen files are strongly typed, in a simple (but complete) type-system. These types are used to perform automatic conversions, check for errors, and to help interface designers constrain the input that they allow. Every <a class="reference internal" href="#value-definition">value definition</a> is required to have an associated type.</p> <p>TableGen supports a mixture of very low-level types (such as <code class="docutils literal notranslate"><span class="pre">bit</span></code>) and very high-level types (such as <code class="docutils literal notranslate"><span class="pre">dag</span></code>). This flexibility is what allows it to describe a wide range of information conveniently and compactly. The TableGen types are:</p> <dl class="docutils"> <dt><code class="docutils literal notranslate"><span class="pre">bit</span></code></dt> <dd>A ‘bit’ is a boolean value that can hold either 0 or 1.</dd> <dt><code class="docutils literal notranslate"><span class="pre">int</span></code></dt> <dd>The ‘int’ type represents a simple 32-bit integer value, such as 5.</dd> <dt><code class="docutils literal notranslate"><span class="pre">string</span></code></dt> <dd>The ‘string’ type represents an ordered sequence of characters of arbitrary length.</dd> <dt><code class="docutils literal notranslate"><span class="pre">code</span></code></dt> <dd>The <cite>code</cite> type represents a code fragment, which can be single/multi-line string literal.</dd> <dt><code class="docutils literal notranslate"><span class="pre">bits<n></span></code></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> <dt><code class="docutils literal notranslate"><span class="pre">list<ty></span></code></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> <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 <code class="docutils literal notranslate"><span class="pre">list</span></code> type, for example, to constrain the elements of the list to a common base class (e.g., a <code class="docutils literal notranslate"><span class="pre">list<Register></span></code> can only contain definitions derived from the “<code class="docutils literal notranslate"><span class="pre">Register</span></code>” class).</dd> <dt><code class="docutils literal notranslate"><span class="pre">dag</span></code></dt> <dd>This type represents a nestable directed graph of elements.</dd> </dl> <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 needed.</p> </div> <div class="section" id="tablegen-values-and-expressions"> <span id="tablegen-expressions"></span><h4><a class="toc-backref" href="#id9">TableGen values and expressions</a><a class="headerlink" href="#tablegen-values-and-expressions" title="Permalink to this headline">¶</a></h4> <p>TableGen allows for a pretty reasonable number of different expression forms 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> <dl class="docutils"> <dt><code class="docutils literal notranslate"><span class="pre">?</span></code></dt> <dd>uninitialized field</dd> <dt><code class="docutils literal notranslate"><span class="pre">0b1001011</span></code></dt> <dd>binary integer value. Note that this is sized by the number of bits given and will not be silently extended/truncated.</dd> <dt><code class="docutils literal notranslate"><span class="pre">7</span></code></dt> <dd>decimal integer value</dd> <dt><code class="docutils literal notranslate"><span class="pre">0x7F</span></code></dt> <dd>hexadecimal integer value</dd> <dt><code class="docutils literal notranslate"><span class="pre">"foo"</span></code></dt> <dd>a single-line string value, can be assigned to <code class="docutils literal notranslate"><span class="pre">string</span></code> or <code class="docutils literal notranslate"><span class="pre">code</span></code> variable.</dd> <dt><code class="docutils literal notranslate"><span class="pre">[{</span> <span class="pre">...</span> <span class="pre">}]</span></code></dt> <dd>usually called a “code fragment”, but is just a multiline string literal</dd> <dt><code class="docutils literal notranslate"><span class="pre">[</span> <span class="pre">X,</span> <span class="pre">Y,</span> <span class="pre">Z</span> <span class="pre">]<type></span></code></dt> <dd>list value. <type> 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><code class="docutils literal notranslate"><span class="pre">{</span> <span class="pre">a,</span> <span class="pre">b,</span> <span class="pre">0b10</span> <span class="pre">}</span></code></dt> <dd>initializer for a “bits<4>” value. 1-bit from “a”, 1-bit from “b”, 2-bits from 0b10.</dd> <dt><code class="docutils literal notranslate"><span class="pre">value</span></code></dt> <dd>value reference</dd> <dt><code class="docutils literal notranslate"><span class="pre">value{17}</span></code></dt> <dd>access to one bit of a value</dd> <dt><code class="docutils literal notranslate"><span class="pre">value{15-17}</span></code></dt> <dd>access to an ordered sequence of bits of a value, in particular <code class="docutils literal notranslate"><span class="pre">value{15-17}</span></code> produces an order that is the reverse of <code class="docutils literal notranslate"><span class="pre">value{17-15}</span></code>.</dd> <dt><code class="docutils literal notranslate"><span class="pre">DEF</span></code></dt> <dd>reference to a record definition</dd> <dt><code class="docutils literal notranslate"><span class="pre">CLASS<val</span> <span class="pre">list></span></code></dt> <dd>reference to a new anonymous definition of CLASS with the specified template arguments.</dd> <dt><code class="docutils literal notranslate"><span class="pre">X.Y</span></code></dt> <dd>reference to the subfield of a value</dd> <dt><code class="docutils literal notranslate"><span class="pre">list[4-7,17,2-3]</span></code></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> </dl> <p><code class="docutils literal notranslate"><span class="pre">foreach</span> <span class="pre"><var></span> <span class="pre">=</span> <span class="pre">[</span> <span class="pre"><list></span> <span class="pre">]</span> <span class="pre">in</span> <span class="pre">{</span> <span class="pre"><body></span> <span class="pre">}</span></code></p> <dl class="docutils"> <dt><code class="docutils literal notranslate"><span class="pre">foreach</span> <span class="pre"><var></span> <span class="pre">=</span> <span class="pre">[</span> <span class="pre"><list></span> <span class="pre">]</span> <span class="pre">in</span> <span class="pre"><def></span></code></dt> <dd>Replicate <body> or <def>, replacing instances of <var> with each value in <list>. <var> is scoped at the level of the <code class="docutils literal notranslate"><span class="pre">foreach</span></code> loop and must not conflict with any other object introduced in <body> or <def>. Only <code class="docutils literal notranslate"><span class="pre">def</span></code>s and <code class="docutils literal notranslate"><span class="pre">defm</span></code>s are expanded within <body>.</dd> </dl> <p><code class="docutils literal notranslate"><span class="pre">foreach</span> <span class="pre"><var></span> <span class="pre">=</span> <span class="pre">0-15</span> <span class="pre">in</span> <span class="pre">...</span></code></p> <dl class="docutils"> <dt><code class="docutils literal notranslate"><span class="pre">foreach</span> <span class="pre"><var></span> <span class="pre">=</span> <span class="pre">{0-15,32-47}</span> <span class="pre">in</span> <span class="pre">...</span></code></dt> <dd>Loop over ranges of integers. The braces are required for multiple ranges.</dd> <dt><code class="docutils literal notranslate"><span class="pre">(DEF</span> <span class="pre">a,</span> <span class="pre">b)</span></code></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 <code class="docutils literal notranslate"><span class="pre">`dag</span></code>’ values.</dd> <dt><code class="docutils literal notranslate"><span class="pre">!con(a,</span> <span class="pre">b,</span> <span class="pre">...)</span></code></dt> <dd><p class="first">Concatenate two or more DAG nodes. Their operations must equal.</p> <p class="last">Example: !con((op a1:$name1, a2:$name2), (op b1:$name3)) results in the DAG node (op a1:$name1, a2:$name2, b1:$name3).</p> </dd> <dt><code class="docutils literal notranslate"><span class="pre">!dag(op,</span> <span class="pre">children,</span> <span class="pre">names)</span></code></dt> <dd><p class="first">Generate a DAG node programmatically. ‘children’ and ‘names’ must be lists of equal length or unset (‘?’). ‘names’ must be a ‘list<string>’.</p> <p>Due to limitations of the type system, ‘children’ must be a list of items of a common type. In practice, this means that they should either have the same type or be records with a common superclass. Mixing dag and non-dag items is not possible. However, ‘?’ can be used.</p> <p class="last">Example: !dag(op, [a1, a2, ?], [“name1”, “name2”, “name3”]) results in (op a1:$name1, a2:$name2, ?:$name3).</p> </dd> <dt><code class="docutils literal notranslate"><span class="pre">!listconcat(a,</span> <span class="pre">b,</span> <span class="pre">...)</span></code></dt> <dd>A list value that is the result of concatenating the ‘a’ and ‘b’ lists. The lists must have the same element type. More than two arguments are accepted with the result being the concatenation of all the lists given.</dd> <dt><code class="docutils literal notranslate"><span class="pre">!strconcat(a,</span> <span class="pre">b,</span> <span class="pre">...)</span></code></dt> <dd>A string value that is the result of concatenating the ‘a’ and ‘b’ strings. More than two arguments are accepted with the result being the concatenation of all the strings given.</dd> <dt><code class="docutils literal notranslate"><span class="pre">str1#str2</span></code></dt> <dd>“#” (paste) is a shorthand for !strconcat. It may concatenate things that are not quoted strings, in which case an implicit !cast<string> is done on the operand of the paste.</dd> <dt><code class="docutils literal notranslate"><span class="pre">!cast<type>(a)</span></code></dt> <dd><p class="first">If ‘a’ is a string, a record of type <em>type</em> obtained by looking up the string ‘a’ in the list of all records defined by the time that all template arguments in ‘a’ are fully resolved.</p> <p>For example, if !cast<type>(a) appears in a multiclass definition, or in a class instantiated inside of a multiclass definition, and ‘a’ does not reference any template arguments of the multiclass, then a record of name ‘a’ must be instantiated earlier in the source file. If ‘a’ does reference a template argument, then the lookup is delayed until defm statements instantiating the multiclass (or later, if the defm occurs in another multiclass and template arguments of the inner multiclass that are referenced by ‘a’ are substituted by values that themselves contain references to template arguments of the outer multiclass).</p> <p>If the type of ‘a’ does not match <em>type</em>, TableGen aborts with an error.</p> <p class="last">Otherwise, perform a normal type cast e.g. between an int and a bit, or between record types. This allows casting a record to a subclass, though if the types do not match, constant folding will be inhibited. !cast<string> is a special case in that the argument can be an int or a record. In the latter case, the record’s name is returned.</p> </dd> <dt><code class="docutils literal notranslate"><span class="pre">!isa<type>(a)</span></code></dt> <dd>Returns an integer: 1 if ‘a’ is dynamically of the given type, 0 otherwise.</dd> <dt><code class="docutils literal notranslate"><span class="pre">!subst(a,</span> <span class="pre">b,</span> <span class="pre">c)</span></code></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><code class="docutils literal notranslate"><span class="pre">!foreach(a,</span> <span class="pre">b,</span> <span class="pre">c)</span></code></dt> <dd>For each member of dag or list ‘b’ apply operator ‘c’. ‘a’ is the name of a variable that will be substituted by members of ‘b’ in ‘c’. This operation is analogous to $(foreach) in GNU make.</dd> <dt><code class="docutils literal notranslate"><span class="pre">!foldl(start,</span> <span class="pre">lst,</span> <span class="pre">a,</span> <span class="pre">b,</span> <span class="pre">expr)</span></code></dt> <dd>Perform a left-fold over ‘lst’ with the given starting value. ‘a’ and ‘b’ are variable names which will be substituted in ‘expr’. If you think of expr as a function f(a,b), the fold will compute ‘f(…f(f(start, lst[0]), lst[1]), …), lst[n-1])’ for a list of length n. As usual, ‘a’ will be of the type of ‘start’, and ‘b’ will be of the type of elements of ‘lst’. These types need not be the same, but ‘expr’ must be of the same type as ‘start’.</dd> <dt><code class="docutils literal notranslate"><span class="pre">!head(a)</span></code></dt> <dd>The first element of list ‘a.’</dd> <dt><code class="docutils literal notranslate"><span class="pre">!tail(a)</span></code></dt> <dd>The 2nd-N elements of list ‘a.’</dd> <dt><code class="docutils literal notranslate"><span class="pre">!empty(a)</span></code></dt> <dd>An integer {0,1} indicating whether list ‘a’ is empty.</dd> <dt><code class="docutils literal notranslate"><span class="pre">!size(a)</span></code></dt> <dd>An integer indicating the number of elements in list ‘a’.</dd> <dt><code class="docutils literal notranslate"><span class="pre">!if(a,b,c)</span></code></dt> <dd>‘b’ if the result of ‘int’ or ‘bit’ operator ‘a’ is nonzero, ‘c’ otherwise.</dd> <dt><code class="docutils literal notranslate"><span class="pre">!eq(a,b)</span></code></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<string> to compare other types of objects.</dd> <dt><code class="docutils literal notranslate"><span class="pre">!ne(a,b)</span></code></dt> <dd>The negation of <code class="docutils literal notranslate"><span class="pre">!eq(a,b)</span></code>.</dd> <dt><code class="docutils literal notranslate"><span class="pre">!le(a,b),</span> <span class="pre">!lt(a,b),</span> <span class="pre">!ge(a,b),</span> <span class="pre">!gt(a,b)</span></code></dt> <dd>(Signed) comparison of integer values that returns bit 1 or 0 depending on the result of the comparison.</dd> <dt><code class="docutils literal notranslate"><span class="pre">!shl(a,b)</span></code> <code class="docutils literal notranslate"><span class="pre">!srl(a,b)</span></code> <code class="docutils literal notranslate"><span class="pre">!sra(a,b)</span></code></dt> <dd>The usual shift operators. Operations are on 64-bit integers, the result is undefined for shift counts outside [0, 63].</dd> <dt><code class="docutils literal notranslate"><span class="pre">!add(a,b,...)</span></code> <code class="docutils literal notranslate"><span class="pre">!and(a,b,...)</span></code> <code class="docutils literal notranslate"><span class="pre">!or(a,b,...)</span></code></dt> <dd>The usual arithmetic and binary operators.</dd> </dl> <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 “<code class="docutils literal notranslate"><span class="pre">7</span></code>” to a “<code class="docutils literal notranslate"><span class="pre">bits<4></span></code>” value, for example.</p> </div> </div> <div class="section" id="classes-and-definitions"> <h3><a class="toc-backref" href="#id10">Classes and definitions</a><a class="headerlink" href="#classes-and-definitions" title="Permalink to this headline">¶</a></h3> <p>As mentioned in the <a class="reference internal" href="index.html"><span class="doc">introduction</span></a>, classes and definitions (collectively known as ‘records’) in TableGen are the main high-level unit of information that TableGen collects. Records are defined with a <code class="docutils literal notranslate"><span class="pre">def</span></code> or <code class="docutils literal notranslate"><span class="pre">class</span></code> keyword, the record name, and an optional list of “<a class="reference internal" href="#template-arguments">template arguments</a>”. If the record has superclasses, they are specified as a comma separated list that starts with a colon character (“<code class="docutils literal notranslate"><span class="pre">:</span></code>”). If <a class="reference internal" href="#value-definitions">value definitions</a> or <a class="reference internal" href="#let-expressions">let expressions</a> are needed for the class, they are enclosed in curly braces (“<code class="docutils literal notranslate"><span class="pre">{}</span></code>”); otherwise, the record ends with a semicolon.</p> <p>Here is a simple TableGen file:</p> <div class="highlight-text notranslate"><div class="highlight"><pre><span></span>class C { bit V = 1; } def X : C; def Y : C { string Greeting = "hello"; } </pre></div> </div> <p>This example defines two definitions, <code class="docutils literal notranslate"><span class="pre">X</span></code> and <code class="docutils literal notranslate"><span class="pre">Y</span></code>, both of which derive from the <code class="docutils literal notranslate"><span class="pre">C</span></code> class. Because of this, they both get the <code class="docutils literal notranslate"><span class="pre">V</span></code> bit value. The <code class="docutils literal notranslate"><span class="pre">Y</span></code> definition also gets the Greeting member as well.</p> <p>In general, classes are useful for collecting together the commonality between a group of records and isolating it in a single place. Also, classes permit the specification of default values for their subclasses, allowing the subclasses to override them as they wish.</p> <div class="section" id="value-definitions"> <span id="value-definition"></span><span id="id1"></span><h4><a class="toc-backref" href="#id11">Value definitions</a><a class="headerlink" href="#value-definitions" title="Permalink to this headline">¶</a></h4> <p>Value definitions define named entries in records. A value must be defined before it can be referred to as the operand for another value definition or before the value is reset with a <a class="reference internal" href="#let-expression">let expression</a>. A value is defined by specifying a <a class="reference internal" href="#tablegen-type">TableGen type</a> and a name. If an initial value is available, it may be specified after the type with an equal sign. Value definitions require terminating semicolons.</p> </div> <div class="section" id="let-expressions-within-a-record"> <span id="let-expressions"></span><span id="let-expression"></span><span id="id2"></span><h4><a class="toc-backref" href="#id12">‘let’ expressions</a><a class="headerlink" href="#let-expressions-within-a-record" title="Permalink to this headline">¶</a></h4> <p>A record-level let expression is used to change the value of a value definition in a record. This is primarily useful when a superclass defines a value that a derived class or definition wants to override. Let expressions consist of the ‘<code class="docutils literal notranslate"><span class="pre">let</span></code>’ keyword followed by a value name, an equal sign (“<code class="docutils literal notranslate"><span class="pre">=</span></code>”), and a new value. For example, a new class could be added to the example above, redefining the <code class="docutils literal notranslate"><span class="pre">V</span></code> field for all of its subclasses:</p> <div class="highlight-text notranslate"><div class="highlight"><pre><span></span>class D : C { let V = 0; } def Z : D; </pre></div> </div> <p>In this case, the <code class="docutils literal notranslate"><span class="pre">Z</span></code> definition will have a zero value for its <code class="docutils literal notranslate"><span class="pre">V</span></code> value, despite the fact that it derives (indirectly) from the <code class="docutils literal notranslate"><span class="pre">C</span></code> class, because the <code class="docutils literal notranslate"><span class="pre">D</span></code> class overrode its value.</p> <p>References between variables in a record are substituted late, which gives <code class="docutils literal notranslate"><span class="pre">let</span></code> expressions unusual power. Consider this admittedly silly example:</p> <div class="highlight-text notranslate"><div class="highlight"><pre><span></span>class A<int x> { int Y = x; int Yplus1 = !add(Y, 1); int xplus1 = !add(x, 1); } def Z : A<5> { let Y = 10; } </pre></div> </div> <p>The value of <code class="docutils literal notranslate"><span class="pre">Z.xplus1</span></code> will be 6, but the value of <code class="docutils literal notranslate"><span class="pre">Z.Yplus1</span></code> is 11. Use this power wisely.</p> </div> <div class="section" id="class-template-arguments"> <span id="template-arguments"></span><h4><a class="toc-backref" href="#id13">Class template arguments</a><a class="headerlink" href="#class-template-arguments" title="Permalink to this headline">¶</a></h4> <p>TableGen permits the definition of parameterized classes as well as normal concrete classes. Parameterized TableGen classes specify a list of variable bindings (which may optionally have defaults) that are bound when used. Here is a simple example:</p> <div class="highlight-text notranslate"><div class="highlight"><pre><span></span>class FPFormat<bits<3> val> { bits<3> Value = val; } def NotFP : FPFormat<0>; def ZeroArgFP : FPFormat<1>; def OneArgFP : FPFormat<2>; def OneArgFPRW : FPFormat<3>; def TwoArgFP : FPFormat<4>; def CompareFP : FPFormat<5>; def CondMovFP : FPFormat<6>; def SpecialFP : FPFormat<7>; </pre></div> </div> <p>In this case, template arguments are used as a space efficient way to specify a list of “enumeration values”, each with a “<code class="docutils literal notranslate"><span class="pre">Value</span></code>” field set to the specified integer.</p> <p>The more esoteric forms of <a class="reference internal" href="#tablegen-expressions">TableGen expressions</a> are useful in conjunction with template arguments. As an example:</p> <div class="highlight-text notranslate"><div class="highlight"><pre><span></span>class ModRefVal<bits<2> val> { bits<2> Value = val; } def None : ModRefVal<0>; def Mod : ModRefVal<1>; def Ref : ModRefVal<2>; def ModRef : ModRefVal<3>; class Value<ModRefVal MR> { // Decode some information into a more convenient format, while providing // a nice interface to the user of the "Value" class. bit isMod = MR.Value{0}; bit isRef = MR.Value{1}; // other stuff... } // Example uses def bork : Value<Mod>; def zork : Value<Ref>; def hork : Value<ModRef>; </pre></div> </div> <p>This is obviously a contrived example, but it shows how template arguments can be used to decouple the interface provided to the user of the class from the actual internal data representation expected by the class. In this case, running <code class="docutils literal notranslate"><span class="pre">llvm-tblgen</span></code> on the example prints the following definitions:</p> <div class="highlight-text notranslate"><div class="highlight"><pre><span></span>def bork { // Value bit isMod = 1; bit isRef = 0; } def hork { // Value bit isMod = 1; bit isRef = 1; } def zork { // Value bit isMod = 0; bit isRef = 1; } </pre></div> </div> <p>This shows that TableGen was able to dig into the argument and extract a piece of information that was requested by the designer of the “Value” class. For more realistic examples, please see existing users of TableGen, such as the X86 backend.</p> </div> <div class="section" id="multiclass-definitions-and-instances"> <h4><a class="toc-backref" href="#id14">Multiclass definitions and instances</a><a class="headerlink" href="#multiclass-definitions-and-instances" title="Permalink to this headline">¶</a></h4> <p>While classes with template arguments are a good way to factor commonality between two instances of a definition, multiclasses allow a convenient notation for defining multiple definitions at once (instances of implicitly constructed classes). For example, consider an 3-address instruction set whose instructions come in two forms: “<code class="docutils literal notranslate"><span class="pre">reg</span> <span class="pre">=</span> <span class="pre">reg</span> <span class="pre">op</span> <span class="pre">reg</span></code>” and “<code class="docutils literal notranslate"><span class="pre">reg</span> <span class="pre">=</span> <span class="pre">reg</span> <span class="pre">op</span> <span class="pre">imm</span></code>” (e.g. SPARC). In this case, you’d like to specify in one place that this commonality exists, then in a separate place indicate what all the ops are.</p> <p>Here is an example TableGen fragment that shows this idea:</p> <div class="highlight-text notranslate"><div class="highlight"><pre><span></span>def ops; def GPR; def Imm; class inst<int opc, string asmstr, dag operandlist>; multiclass ri_inst<int opc, string asmstr> { def _rr : inst<opc, !strconcat(asmstr, " $dst, $src1, $src2"), (ops GPR:$dst, GPR:$src1, GPR:$src2)>; def _ri : inst<opc, !strconcat(asmstr, " $dst, $src1, $src2"), (ops GPR:$dst, GPR:$src1, Imm:$src2)>; } // Instantiations of the ri_inst multiclass. defm ADD : ri_inst<0b111, "add">; defm SUB : ri_inst<0b101, "sub">; defm MUL : ri_inst<0b100, "mul">; ... </pre></div> </div> <p>The name of the resultant definitions has the multidef fragment names appended to them, so this defines <code class="docutils literal notranslate"><span class="pre">ADD_rr</span></code>, <code class="docutils literal notranslate"><span class="pre">ADD_ri</span></code>, <code class="docutils literal notranslate"><span class="pre">SUB_rr</span></code>, 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="highlight-text notranslate"><div class="highlight"><pre><span></span>def ops; def GPR; def Imm; class inst<int opc, string asmstr, dag operandlist>; class rrinst<int opc, string asmstr> : inst<opc, !strconcat(asmstr, " $dst, $src1, $src2"), (ops GPR:$dst, GPR:$src1, GPR:$src2)>; class riinst<int opc, string asmstr> : inst<opc, !strconcat(asmstr, " $dst, $src1, $src2"), (ops GPR:$dst, GPR:$src1, Imm:$src2)>; // Instantiations of the ri_inst multiclass. def ADD_rr : rrinst<0b111, "add">; def ADD_ri : riinst<0b111, "add">; def SUB_rr : rrinst<0b101, "sub">; def SUB_ri : riinst<0b101, "sub">; def MUL_rr : rrinst<0b100, "mul">; def MUL_ri : riinst<0b100, "mul">; ... </pre></div> </div> <p>A <code class="docutils literal notranslate"><span class="pre">defm</span></code> can also be used inside a multiclass providing several levels of multiclass instantiations.</p> <div class="highlight-text notranslate"><div class="highlight"><pre><span></span>class Instruction<bits<4> opc, string Name> { bits<4> opcode = opc; string name = Name; } multiclass basic_r<bits<4> opc> { def rr : Instruction<opc, "rr">; def rm : Instruction<opc, "rm">; } multiclass basic_s<bits<4> opc> { defm SS : basic_r<opc>; defm SD : basic_r<opc>; def X : Instruction<opc, "x">; } multiclass basic_p<bits<4> opc> { defm PS : basic_r<opc>; defm PD : basic_r<opc>; def Y : Instruction<opc, "y">; } defm ADD : basic_s<0xf>, basic_p<0xf>; ... // Results def ADDPDrm { ... def ADDPDrr { ... def ADDPSrm { ... def ADDPSrr { ... def ADDSDrm { ... def ADDSDrr { ... def ADDY { ... def ADDX { ... </pre></div> </div> <p><code class="docutils literal notranslate"><span class="pre">defm</span></code> 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="highlight-text notranslate"><div class="highlight"><pre><span></span>class XD { bits<4> Prefix = 11; } class XS { bits<4> Prefix = 12; } class I<bits<4> op> { bits<4> opcode = op; } multiclass R { def rr : I<4>; def rm : I<2>; } multiclass Y { defm SS : R, XD; defm SD : R, XS; } defm Instr : Y; // Results def InstrSDrm { bits<4> opcode = { 0, 0, 1, 0 }; bits<4> Prefix = { 1, 1, 0, 0 }; } ... def InstrSSrr { bits<4> opcode = { 0, 1, 0, 0 }; bits<4> Prefix = { 1, 0, 1, 1 }; } </pre></div> </div> </div> </div> <div class="section" id="file-scope-entities"> <h3><a class="toc-backref" href="#id15">File scope entities</a><a class="headerlink" href="#file-scope-entities" title="Permalink to this headline">¶</a></h3> <div class="section" id="file-inclusion"> <h4><a class="toc-backref" href="#id16">File inclusion</a><a class="headerlink" href="#file-inclusion" title="Permalink to this headline">¶</a></h4> <p>TableGen supports the ‘<code class="docutils literal notranslate"><span class="pre">include</span></code>’ token, which textually substitutes the specified file in place of the include directive. The filename should be specified as a double quoted string immediately after the ‘<code class="docutils literal notranslate"><span class="pre">include</span></code>’ keyword. Example:</p> <div class="highlight-text notranslate"><div class="highlight"><pre><span></span>include "foo.td" </pre></div> </div> </div> <div class="section" id="id3"> <h4><a class="toc-backref" href="#id17">‘let’ expressions</a><a class="headerlink" href="#id3" title="Permalink to this headline">¶</a></h4> <p>“Let” expressions at file scope are similar to <a class="reference internal" href="#let-expressions-within-a-record">“let” expressions within a record</a>, except they can specify a value binding for multiple records at a time, and may be useful in certain other cases. 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 or more records to bind the values in. Here are some examples:</p> <div class="highlight-text notranslate"><div class="highlight"><pre><span></span>let isTerminator = 1, isReturn = 1, isBarrier = 1, hasCtrlDep = 1 in def RET : I<0xC3, RawFrm, (outs), (ins), "ret", [(X86retflag 0)]>; let isCall = 1 in // All calls clobber the non-callee saved registers... let 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] in { def CALLpcrel32 : Ii32<0xE8, RawFrm, (outs), (ins i32imm:$dst,variable_ops), "call\t${dst:call}", []>; def CALL32r : I<0xFF, MRM2r, (outs), (ins GR32:$dst, variable_ops), "call\t{*}$dst", [(X86call GR32:$dst)]>; def CALL32m : I<0xFF, MRM2m, (outs), (ins i32mem:$dst, variable_ops), "call\t{*}$dst", []>; } </pre></div> </div> <p>File-scope “let” expressions are often useful when a couple of definitions need to be added to several records, and the records do not otherwise need to be opened, as in the case with the <code class="docutils literal notranslate"><span class="pre">CALL*</span></code> 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 instantiations. This also avoids the need of using “let” expressions within subsequent records inside a multiclass.</p> <div class="highlight-text notranslate"><div class="highlight"><pre><span></span>multiclass basic_r<bits<4> opc> { let Predicates = [HasSSE2] in { def rr : Instruction<opc, "rr">; def rm : Instruction<opc, "rm">; } let Predicates = [HasSSE3] in def rx : Instruction<opc, "rx">; } multiclass basic_ss<bits<4> opc> { let IsDouble = 0 in defm SS : basic_r<opc>; let IsDouble = 1 in defm SD : basic_r<opc>; } defm ADD : basic_ss<0xf>; </pre></div> </div> </div> <div class="section" id="looping"> <h4><a class="toc-backref" href="#id18">Looping</a><a class="headerlink" href="#looping" title="Permalink to this headline">¶</a></h4> <p>TableGen supports the ‘<code class="docutils literal notranslate"><span class="pre">foreach</span></code>’ block, which textually replicates the loop body, substituting iterator values for iterator references in the body. Example:</p> <div class="highlight-text notranslate"><div class="highlight"><pre><span></span>foreach i = [0, 1, 2, 3] in { def R#i : Register<...>; def F#i : Register<...>; } </pre></div> </div> <p>This will create objects <code class="docutils literal notranslate"><span class="pre">R0</span></code>, <code class="docutils literal notranslate"><span class="pre">R1</span></code>, <code class="docutils literal notranslate"><span class="pre">R2</span></code> and <code class="docutils literal notranslate"><span class="pre">R3</span></code>. <code class="docutils literal notranslate"><span class="pre">foreach</span></code> blocks may be nested. If there is only one item in the body the braces may be elided:</p> <div class="highlight-text notranslate"><div class="highlight"><pre><span></span>foreach i = [0, 1, 2, 3] in def R#i : Register<...>; </pre></div> </div> </div> </div> </div> <div class="section" id="code-generator-backend-info"> <h2><a class="toc-backref" href="#id19">Code Generator backend info</a><a class="headerlink" href="#code-generator-backend-info" title="Permalink to this headline">¶</a></h2> <p>Expressions used by code generator to describe instructions and isel patterns:</p> <dl class="docutils"> <dt><code class="docutils literal notranslate"><span class="pre">(implicit</span> <span class="pre">a)</span></code></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> </div> </div> </div> <div class="clearer"></div> </div> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="Deficiencies.html" title="TableGen Deficiencies" >next</a> |</li> <li class="right" > <a href="LangRef.html" title="TableGen Language Reference" >previous</a> |</li> <li><a href="http://llvm.org/">LLVM Home</a> | </li> <li><a href="../index.html">Documentation</a>»</li> <li class="nav-item nav-item-1"><a href="index.html" >TableGen</a> »</li> </ul> </div> <div class="footer" role="contentinfo"> © Copyright 2003-2020, LLVM Project. Last updated on 2020-09-07. Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.8.4. </div> </body> </html>