From fae8b1de47c004fefaa6c2683ae193d465b9d93f Mon Sep 17 00:00:00 2001 From: Jakob Stoklund Olesen Date: Thu, 24 May 2012 22:17:39 +0000 Subject: 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 --- lib/TableGen/TGParser.cpp | 65 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 50 insertions(+), 15 deletions(-) (limited to 'lib/TableGen/TGParser.cpp') diff --git a/lib/TableGen/TGParser.cpp b/lib/TableGen/TGParser.cpp index b23f410013..9424677800 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 Ranges; - ForeachListValue = dynamic_cast(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(List); + if (ForeachListValue == 0) { + TokError("Expected a Value list"); + return 0; + } + RecTy *ValueType = ForeachListValue->getType(); + ListRecTy *ListType = dynamic_cast(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(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 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 -- cgit v1.2.3