aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJakob Stoklund Olesen <stoklund@2pi.dk>2012-05-24 22:17:39 +0000
committerJakob Stoklund Olesen <stoklund@2pi.dk>2012-05-24 22:17:39 +0000
commitfae8b1de47c004fefaa6c2683ae193d465b9d93f (patch)
tree26d07890a81e7e3d41ff71f57000d86383c00d59
parent72cba6cdf640411e2fb6207858a0abd87c4286fc (diff)
downloadexternal_llvm-fae8b1de47c004fefaa6c2683ae193d465b9d93f.zip
external_llvm-fae8b1de47c004fefaa6c2683ae193d465b9d93f.tar.gz
external_llvm-fae8b1de47c004fefaa6c2683ae193d465b9d93f.tar.bz2
Add support for range expressions in TableGen foreach loops.
Like this: foreach i = 0-127 in ... Use braces for composite ranges: foreach i = {0-3,9-7} in ... git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@157432 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--docs/TableGenFundamentals.html8
-rw-r--r--lib/TableGen/TGParser.cpp65
-rw-r--r--test/TableGen/ForeachLoop.td23
3 files changed, 77 insertions, 19 deletions
diff --git a/docs/TableGenFundamentals.html b/docs/TableGenFundamentals.html
index 58f9876..5490eeb 100644
--- a/docs/TableGenFundamentals.html
+++ b/docs/TableGenFundamentals.html
@@ -402,14 +402,18 @@ 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>
+<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>foreach &lt;var&gt; = 0-15 in ...</tt></dt>
+<dt><tt>foreach &lt;var&gt; = {0-15,32-47} in ...</tt></dt>
+ <dd>Loop over ranges of integers. The braces are required for multiple
+ ranges.</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
diff --git a/lib/TableGen/TGParser.cpp b/lib/TableGen/TGParser.cpp
index b23f410..9424677 100644
--- a/lib/TableGen/TGParser.cpp
+++ b/lib/TableGen/TGParser.cpp
@@ -1697,7 +1697,9 @@ Init *TGParser::ParseDeclaration(Record *CurRec,
/// the name of the declared object or a NULL Init on error. Return
/// the name of the parsed initializer list through ForeachListName.
///
-/// ForeachDeclaration ::= ID '=' Value
+/// ForeachDeclaration ::= ID '=' '[' ValueList ']'
+/// ForeachDeclaration ::= ID '=' '{' RangeList '}'
+/// ForeachDeclaration ::= ID '=' RangePiece
///
VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) {
if (Lex.getCode() != tgtok::Id) {
@@ -1715,26 +1717,59 @@ VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) {
}
Lex.Lex(); // Eat the '='
- // Expect a list initializer.
- Init *List = ParseSimpleValue(0, 0, ParseForeachMode);
+ RecTy *IterType = 0;
+ std::vector<unsigned> Ranges;
- ForeachListValue = dynamic_cast<ListInit*>(List);
- if (ForeachListValue == 0) {
- TokError("Expected a Value list");
- return 0;
+ switch (Lex.getCode()) {
+ default: TokError("Unknown token when expecting a range list"); return 0;
+ case tgtok::l_square: { // '[' ValueList ']'
+ Init *List = ParseSimpleValue(0, 0, ParseForeachMode);
+ ForeachListValue = dynamic_cast<ListInit*>(List);
+ if (ForeachListValue == 0) {
+ TokError("Expected a Value list");
+ return 0;
+ }
+ RecTy *ValueType = ForeachListValue->getType();
+ ListRecTy *ListType = dynamic_cast<ListRecTy *>(ValueType);
+ if (ListType == 0) {
+ TokError("Value list is not of list type");
+ return 0;
+ }
+ IterType = ListType->getElementType();
+ break;
}
- RecTy *ValueType = ForeachListValue->getType();
- ListRecTy *ListType = dynamic_cast<ListRecTy *>(ValueType);
- if (ListType == 0) {
- TokError("Value list is not of list type");
- return 0;
+ case tgtok::IntVal: { // RangePiece.
+ if (ParseRangePiece(Ranges))
+ return 0;
+ break;
+ }
+
+ case tgtok::l_brace: { // '{' RangeList '}'
+ Lex.Lex(); // eat the '{'
+ Ranges = ParseRangeList();
+ if (Lex.getCode() != tgtok::r_brace) {
+ TokError("expected '}' at end of bit range list");
+ return 0;
+ }
+ Lex.Lex();
+ break;
+ }
}
- RecTy *IterType = ListType->getElementType();
- VarInit *IterVar = VarInit::get(DeclName, IterType);
+ if (!Ranges.empty()) {
+ assert(!IterType && "Type already initialized?");
+ IterType = IntRecTy::get();
+ std::vector<Init*> Values;
+ for (unsigned i = 0, e = Ranges.size(); i != e; ++i)
+ Values.push_back(IntInit::get(Ranges[i]));
+ ForeachListValue = ListInit::get(Values, IterType);
+ }
+
+ if (!IterType)
+ return 0;
- return IterVar;
+ return VarInit::get(DeclName, IterType);
}
/// ParseTemplateArgList - Read a template argument list, which is a non-empty
diff --git a/test/TableGen/ForeachLoop.td b/test/TableGen/ForeachLoop.td
index 3426096..4aacc74 100644
--- a/test/TableGen/ForeachLoop.td
+++ b/test/TableGen/ForeachLoop.td
@@ -6,11 +6,19 @@ class Register<string name, int idx> {
int Index = idx;
}
+// CHECK-NOT: !strconcat
+
+foreach i = 0-3 in
+ def Q#i : Register<"Q"#i, i>;
+
+// CHECK: def Q0
+// CHECK: def Q1
+// CHECK: def Q2
+// CHECK: def Q3
+
foreach i = [0, 1, 2, 3, 4, 5, 6, 7] in
def R#i : Register<"R"#i, i>;
-// CHECK-NOT: !strconcat
-
// CHECK: def R0
// CHECK: string Name = "R0";
// CHECK: int Index = 0;
@@ -42,3 +50,14 @@ foreach i = [0, 1, 2, 3, 4, 5, 6, 7] in
// CHECK: def R7
// CHECK: string Name = "R7";
// CHECK: int Index = 7;
+
+foreach i = {0-3,9-7} in
+ def S#i : Register<"Q"#i, i>;
+
+// CHECK: def S0
+// CHECK: def S1
+// CHECK: def S2
+// CHECK: def S3
+// CHECK: def S7
+// CHECK: def S8
+// CHECK: def S9