[llvm-commits] [llvm] r157432 - in /llvm/trunk: docs/TableGenFundamentals.html lib/TableGen/TGParser.cpp test/TableGen/ForeachLoop.td
Jakob Stoklund Olesen
stoklund at 2pi.dk
Thu May 24 15:17:40 PDT 2012
Author: stoklund
Date: Thu May 24 17:17:39 2012
New Revision: 157432
URL: http://llvm.org/viewvc/llvm-project?rev=157432&view=rev
Log:
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 ...
Modified:
llvm/trunk/docs/TableGenFundamentals.html
llvm/trunk/lib/TableGen/TGParser.cpp
llvm/trunk/test/TableGen/ForeachLoop.td
Modified: llvm/trunk/docs/TableGenFundamentals.html
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/docs/TableGenFundamentals.html?rev=157432&r1=157431&r2=157432&view=diff
==============================================================================
--- llvm/trunk/docs/TableGenFundamentals.html (original)
+++ llvm/trunk/docs/TableGenFundamentals.html Thu May 24 17:17:39 2012
@@ -402,14 +402,18 @@
<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 <var> = <list> in { <body> }</tt></dt>
-<dt><tt>foreach <var> = <list> in <def></tt></dt>
+<dt><tt>foreach <var> = [ <list> ] in { <body> }</tt></dt>
+<dt><tt>foreach <var> = [ <list> ] in <def></tt></dt>
<dd> Replicate <body> or <def>, replacing instances of
<var> with each value in <list>. <var> is scoped at the
level of the <tt>foreach</tt> loop and must not conflict with any other object
introduced in <body> or <def>. Currently only <tt>def</tt>s are
expanded within <body>.
</dd>
+<dt><tt>foreach <var> = 0-15 in ...</tt></dt>
+<dt><tt>foreach <var> = {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
Modified: llvm/trunk/lib/TableGen/TGParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/TableGen/TGParser.cpp?rev=157432&r1=157431&r2=157432&view=diff
==============================================================================
--- llvm/trunk/lib/TableGen/TGParser.cpp (original)
+++ llvm/trunk/lib/TableGen/TGParser.cpp Thu May 24 17:17:39 2012
@@ -1697,7 +1697,9 @@
/// 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 @@
}
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;
+ }
+ }
+
+ 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);
}
- RecTy *IterType = ListType->getElementType();
- VarInit *IterVar = VarInit::get(DeclName, IterType);
+ if (!IterType)
+ return 0;
- return IterVar;
+ return VarInit::get(DeclName, IterType);
}
/// ParseTemplateArgList - Read a template argument list, which is a non-empty
Modified: llvm/trunk/test/TableGen/ForeachLoop.td
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/TableGen/ForeachLoop.td?rev=157432&r1=157431&r2=157432&view=diff
==============================================================================
--- llvm/trunk/test/TableGen/ForeachLoop.td (original)
+++ llvm/trunk/test/TableGen/ForeachLoop.td Thu May 24 17:17:39 2012
@@ -6,11 +6,19 @@
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 @@
// 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
More information about the llvm-commits
mailing list