Add Foreach Loop

Add some data structures to represent for loops.  These will be
referenced during object processing to do any needed iteration and
instantiation.

Add foreach keyword support to the lexer.

Add a mode to indicate that we're parsing a foreach loop.  This allows
the value parser to early-out when processing the foreach value list.

Add a routine to parse foreach iteration declarations.  This is
separate from ParseDeclaration because the type of the named value
(the iterator) doesn't match the type of the initializer value (the
value list).  It also needs to add two values to the foreach record:
the iterator and the value list.

Add parsing support for foreach.

Add the code to process foreach loops and create defs based
on iterator values.

Allow foreach loops to be matched at the top level.

When parsing an IDValue check if it is a foreach loop iterator for one
of the active loops.  If so, return a VarInit for it.

Add Emacs keyword support for foreach.

Add VIM keyword support for foreach.

Add tests to check foreach operation.

Add TableGen documentation for foreach.

Support foreach with multiple objects.

Support non-braced foreach body with one object.

Do not require types for the foreach declaration.  Assume the iterator
type from the iteration list element type.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@151164 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
David Greene
2012-02-22 16:09:41 +00:00
parent a3bf915888
commit cebb4ee93a
12 changed files with 777 additions and 7 deletions

View File

@@ -37,6 +37,7 @@
<ol>
<li><a href="#include">File inclusion</a></li>
<li><a href="#globallet">'let' expressions</a></li>
<li><a href="#foreach">'foreach' blocks</a></li>
</ol></li>
</ol></li>
<li><a href="#backends">TableGen backends</a>
@@ -401,6 +402,14 @@ which case the user must specify it explicitly.</dd>
<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>
<dt><tt>foreach &lt;var&gt; = &lt;list&gt; in { &lt;body&gt; }</tt></dt>
<dt><tt>foreach &lt;var&gt; = &lt;list&gt; in &lt;def&gt;</tt></dt>
<dd> Replicate &lt;body&gt; or &lt;def&gt;, replacing instances of
&lt;var&gt; with each value in &lt;list&gt;. &lt;var&gt; is scoped at the
level of the <tt>foreach</tt> loop and must not conflict with any other object
introduced in &lt;body&gt; or &lt;def&gt;. Currently only <tt>def</tt>s are
expanded within &lt;body&gt;.
</dd>
<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
@@ -880,6 +889,39 @@ several levels of multiclass instanciations. This also avoids the need of using
</pre>
</div>
<!-- -------------------------------------------------------------------------->
<h4>
<a name="foreach">Looping</a>
</h4>
<div>
<p>TableGen supports the '<tt>foreach</tt>' block, which textually replicates
the loop body, substituting iterator values for iterator references in the
body. Example:</p>
<div class="doc_code">
<pre>
<b>foreach</b> i = [0, 1, 2, 3] in {
<b>def</b> R#i : Register&lt;...&gt;;
<b>def</b> F#i : Register&lt;...&gt;;
}
</pre>
</div>
<p>This will create objects <tt>R0</tt>, <tt>R1</tt>, <tt>R2</tt> and
<tt>R3</tt>. <tt>foreach</tt> blocks may be nested. If there is only
one item in the body the braces may be elided:</p>
<div class="doc_code">
<pre>
<b>foreach</b> i = [0, 1, 2, 3] in
<b>def</b> R#i : Register&lt;...&gt;;
</pre>
</div>
</div>
</div>
</div>