diff options
author | Jakob Stoklund Olesen <stoklund@2pi.dk> | 2012-05-24 22:17:33 +0000 |
---|---|---|
committer | Jakob Stoklund Olesen <stoklund@2pi.dk> | 2012-05-24 22:17:33 +0000 |
commit | 8e5286e18ffbe4716ef92cd1de8901942d685e1b (patch) | |
tree | 0aab7922af3c402867db4d1bb8821b4a8c8e6109 /lib/TableGen/TGParser.cpp | |
parent | bb0a242d86f46c66cb4e3eee25516c99ae24f361 (diff) |
Simplify TGParser::ProcessForEachDefs.
Use static type checking.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@157430 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/TableGen/TGParser.cpp')
-rw-r--r-- | lib/TableGen/TGParser.cpp | 155 |
1 files changed, 63 insertions, 92 deletions
diff --git a/lib/TableGen/TGParser.cpp b/lib/TableGen/TGParser.cpp index 04c4fc158f..68d9ef4c09 100644 --- a/lib/TableGen/TGParser.cpp +++ b/lib/TableGen/TGParser.cpp @@ -292,107 +292,78 @@ bool TGParser::AddSubMultiClass(MultiClass *CurMC, /// ProcessForeachDefs - Given a record, apply all of the variable /// values in all surrounding foreach loops, creating new records for /// each combination of values. -bool TGParser::ProcessForeachDefs(Record *CurRec, MultiClass *CurMultiClass, - SMLoc Loc) { +bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc) { + if (Loops.empty()) + return false; + // We want to instantiate a new copy of CurRec for each combination // of nested loop iterator values. We don't want top instantiate // any copies until we have values for each loop iterator. IterSet IterVals; - for (LoopVector::iterator Loop = Loops.begin(), LoopEnd = Loops.end(); - Loop != LoopEnd; - ++Loop) { - // Process this loop. - if (ProcessForeachDefs(CurRec, CurMultiClass, Loc, - IterVals, *Loop, Loop+1)) { - Error(Loc, - "Could not process loops for def " + CurRec->getNameInitAsString()); - return true; - } - } - - return false; + return ProcessForeachDefs(CurRec, Loc, IterVals); } /// ProcessForeachDefs - Given a record, a loop and a loop iterator, /// apply each of the variable values in this loop and then process /// subloops. -bool TGParser::ProcessForeachDefs(Record *CurRec, MultiClass *CurMultiClass, - SMLoc Loc, IterSet &IterVals, - ForeachLoop &CurLoop, - LoopVector::iterator NextLoop) { - Init *IterVar = CurLoop.IterVar; - ListInit *List = dynamic_cast<ListInit *>(CurLoop.ListValue); - - if (List == 0) { - Error(Loc, "Loop list is not a list"); - return true; - } - - // Process each value. - for (int64_t i = 0; i < List->getSize(); ++i) { - Init *ItemVal = List->resolveListElementReference(*CurRec, 0, i); - IterVals.push_back(IterRecord(IterVar, ItemVal)); - - if (IterVals.size() == Loops.size()) { - // Ok, we have all of the iterator values for this point in the - // iteration space. Instantiate a new record to reflect this - // combination of values. - Record *IterRec = new Record(*CurRec); - - // Set the iterator values now. - for (IterSet::iterator i = IterVals.begin(), iend = IterVals.end(); - i != iend; - ++i) { - VarInit *IterVar = dynamic_cast<VarInit *>(i->IterVar); - if (IterVar == 0) { - Error(Loc, "foreach iterator is unresolved"); - return true; - } - - TypedInit *IVal = dynamic_cast<TypedInit *>(i->IterValue); - if (IVal == 0) { - Error(Loc, "foreach iterator value is untyped"); - return true; - } - - IterRec->addValue(RecordVal(IterVar->getName(), IVal->getType(), false)); +bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc, IterSet &IterVals){ + // Recursively build a tuple of iterator values. + if (IterVals.size() != Loops.size()) { + assert(IterVals.size() < Loops.size()); + ForeachLoop &CurLoop = Loops[IterVals.size()]; + ListInit *List = dynamic_cast<ListInit *>(CurLoop.ListValue); + if (List == 0) { + Error(Loc, "Loop list is not a list"); + return true; + } - if (SetValue(IterRec, Loc, IterVar->getName(), - std::vector<unsigned>(), IVal)) { - Error(Loc, "when instantiating this def"); - return true; - } + // Process each value. + for (int64_t i = 0; i < List->getSize(); ++i) { + Init *ItemVal = List->resolveListElementReference(*CurRec, 0, i); + IterVals.push_back(IterRecord(CurLoop.IterVar, ItemVal)); + if (ProcessForeachDefs(CurRec, Loc, IterVals)) + return true; + IterVals.pop_back(); + } + return false; + } - // Resolve it next. - IterRec->resolveReferencesTo(IterRec->getValue(IterVar->getName())); + // This is the bottom of the recursion. We have all of the iterator values + // for this point in the iteration space. Instantiate a new record to + // reflect this combination of values. + Record *IterRec = new Record(*CurRec); - // Remove it. - IterRec->removeValue(IterVar->getName()); - } + // Set the iterator values now. + for (unsigned i = 0, e = IterVals.size(); i != e; ++i) { + VarInit *IterVar = IterVals[i].IterVar; + TypedInit *IVal = dynamic_cast<TypedInit *>(IterVals[i].IterValue); + if (IVal == 0) { + Error(Loc, "foreach iterator value is untyped"); + return true; + } - if (Records.getDef(IterRec->getNameInitAsString())) { - Error(Loc, "def already exists: " + IterRec->getNameInitAsString()); - return true; - } + IterRec->addValue(RecordVal(IterVar->getName(), IVal->getType(), false)); - Records.addDef(IterRec); - IterRec->resolveReferences(); + if (SetValue(IterRec, Loc, IterVar->getName(), + std::vector<unsigned>(), IVal)) { + Error(Loc, "when instantiating this def"); + return true; } - if (NextLoop != Loops.end()) { - // Process nested loops. - if (ProcessForeachDefs(CurRec, CurMultiClass, Loc, IterVals, *NextLoop, - NextLoop+1)) { - Error(Loc, - "Could not process loops for def " + - CurRec->getNameInitAsString()); - return true; - } - } + // Resolve it next. + IterRec->resolveReferencesTo(IterRec->getValue(IterVar->getName())); - // We're done with this iterator. - IterVals.pop_back(); + // Remove it. + IterRec->removeValue(IterVar->getName()); } + + if (Records.getDef(IterRec->getNameInitAsString())) { + Error(Loc, "def already exists: " + IterRec->getNameInitAsString()); + return true; + } + + Records.addDef(IterRec); + IterRec->resolveReferences(); return false; } @@ -1728,7 +1699,7 @@ Init *TGParser::ParseDeclaration(Record *CurRec, /// /// ForeachDeclaration ::= ID '=' Value /// -Init *TGParser::ParseForeachDeclaration(Init *&ForeachListValue) { +VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) { if (Lex.getCode() != tgtok::Id) { TokError("Expected identifier in foreach declaration"); return 0; @@ -1745,15 +1716,15 @@ Init *TGParser::ParseForeachDeclaration(Init *&ForeachListValue) { Lex.Lex(); // Eat the '=' // Expect a list initializer. - ForeachListValue = ParseValue(0, 0, ParseForeachMode); + Init *List = ParseSimpleValue(0, 0, ParseForeachMode); - TypedInit *TypedList = dynamic_cast<TypedInit *>(ForeachListValue); - if (TypedList == 0) { - TokError("Value list is untyped"); + ForeachListValue = dynamic_cast<ListInit*>(List); + if (ForeachListValue == 0) { + TokError("Expected a Value list"); return 0; } - RecTy *ValueType = TypedList->getType(); + RecTy *ValueType = ForeachListValue->getType(); ListRecTy *ListType = dynamic_cast<ListRecTy *>(ValueType); if (ListType == 0) { TokError("Value list is not of list type"); @@ -1978,7 +1949,7 @@ bool TGParser::ParseDef(MultiClass *CurMultiClass) { } } - if (ProcessForeachDefs(CurRec, CurMultiClass, DefLoc)) { + if (ProcessForeachDefs(CurRec, DefLoc)) { Error(DefLoc, "Could not process loops for def" + CurRec->getNameInitAsString()); return true; @@ -1999,8 +1970,8 @@ bool TGParser::ParseForeach(MultiClass *CurMultiClass) { // Make a temporary object to record items associated with the for // loop. - Init *ListValue = 0; - Init *IterName = ParseForeachDeclaration(ListValue); + ListInit *ListValue = 0; + VarInit *IterName = ParseForeachDeclaration(ListValue); if (IterName == 0) return TokError("expected declaration in for"); |