diff options
Diffstat (limited to 'llvm/lib/TableGen/TGParser.cpp')
-rw-r--r-- | llvm/lib/TableGen/TGParser.cpp | 258 |
1 files changed, 242 insertions, 16 deletions
diff --git a/llvm/lib/TableGen/TGParser.cpp b/llvm/lib/TableGen/TGParser.cpp index c373e2899a5d..01cc1af34ab6 100644 --- a/llvm/lib/TableGen/TGParser.cpp +++ b/llvm/lib/TableGen/TGParser.cpp @@ -391,9 +391,11 @@ bool TGParser::resolve(const ForeachLoop &Loop, SubstStack &Substs, bool Error = false; for (auto Elt : *LI) { - Substs.emplace_back(Loop.IterVar->getNameInit(), Elt); + if (Loop.IterVar) + Substs.emplace_back(Loop.IterVar->getNameInit(), Elt); Error = resolve(Loop.Entries, Substs, Final, Dest); - Substs.pop_back(); + if (Loop.IterVar) + Substs.pop_back(); if (Error) break; } @@ -482,7 +484,7 @@ bool TGParser::addDefOne(std::unique_ptr<Record> Rec) { static bool isObjectStart(tgtok::TokKind K) { return K == tgtok::Class || K == tgtok::Def || K == tgtok::Defm || K == tgtok::Let || K == tgtok::MultiClass || K == tgtok::Foreach || - K == tgtok::Defset; + K == tgtok::Defset || K == tgtok::Defvar || K == tgtok::If; } /// ParseObjectName - If a valid object name is specified, return it. If no @@ -869,11 +871,17 @@ Init *TGParser::ParseIDValue(Record *CurRec, StringInit *Name, SMLoc NameLoc, } } + if (CurLocalScope) + if (Init *I = CurLocalScope->getVar(Name->getValue())) + return I; + // If this is in a foreach loop, make sure it's not a loop iterator for (const auto &L : Loops) { - VarInit *IterVar = dyn_cast<VarInit>(L->IterVar); - if (IterVar && IterVar->getNameInit() == Name) - return IterVar; + if (L->IterVar) { + VarInit *IterVar = dyn_cast<VarInit>(L->IterVar); + if (IterVar && IterVar->getNameInit() == Name) + return IterVar; + } } if (Mode == ParseNameMode) @@ -905,7 +913,8 @@ Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) { case tgtok::XTail: case tgtok::XSize: case tgtok::XEmpty: - case tgtok::XCast: { // Value ::= !unop '(' Value ')' + case tgtok::XCast: + case tgtok::XGetOp: { // Value ::= !unop '(' Value ')' UnOpInit::UnaryOp Code; RecTy *Type = nullptr; @@ -941,6 +950,28 @@ Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) { Code = UnOpInit::EMPTY; Type = IntRecTy::get(); break; + case tgtok::XGetOp: + Lex.Lex(); // eat the operation + if (Lex.getCode() == tgtok::less) { + // Parse an optional type suffix, so that you can say + // !getop<BaseClass>(someDag) as a shorthand for + // !cast<BaseClass>(!getop(someDag)). + Type = ParseOperatorType(); + + if (!Type) { + TokError("did not get type for unary operator"); + return nullptr; + } + + if (!isa<RecordRecTy>(Type)) { + TokError("type for !getop must be a record type"); + // but keep parsing, to consume the operand + } + } else { + Type = RecordRecTy::get({}); + } + Code = UnOpInit::GETOP; + break; } if (Lex.getCode() != tgtok::l_paren) { TokError("expected '(' after unary operator"); @@ -1055,7 +1086,8 @@ Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) { case tgtok::XGt: case tgtok::XListConcat: case tgtok::XListSplat: - case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')' + case tgtok::XStrConcat: + case tgtok::XSetOp: { // Value ::= !binop '(' Value ',' Value ')' tgtok::TokKind OpTok = Lex.getCode(); SMLoc OpLoc = Lex.getLoc(); Lex.Lex(); // eat the operation @@ -1080,6 +1112,7 @@ Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) { case tgtok::XListConcat: Code = BinOpInit::LISTCONCAT; break; case tgtok::XListSplat: Code = BinOpInit::LISTSPLAT; break; case tgtok::XStrConcat: Code = BinOpInit::STRCONCAT; break; + case tgtok::XSetOp: Code = BinOpInit::SETOP; break; } RecTy *Type = nullptr; @@ -1088,6 +1121,7 @@ Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) { default: llvm_unreachable("Unhandled code!"); case tgtok::XConcat: + case tgtok::XSetOp: Type = DagRecTy::get(); ArgType = DagRecTy::get(); break; @@ -1146,7 +1180,6 @@ Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) { InitList.push_back(ParseValue(CurRec, ArgType)); if (!InitList.back()) return nullptr; - // All BinOps require their arguments to be of compatible types. RecTy *ListType = cast<TypedInit>(InitList.back())->getType(); if (!ArgType) { ArgType = ListType; @@ -1212,6 +1245,18 @@ Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) { ArgType = Resolved; } + // Deal with BinOps whose arguments have different types, by + // rewriting ArgType in between them. + switch (Code) { + case BinOpInit::SETOP: + // After parsing the first dag argument, switch to expecting + // a record, with no restriction on its superclasses. + ArgType = RecordRecTy::get({}); + break; + default: + break; + } + if (Lex.getCode() != tgtok::comma) break; Lex.Lex(); // eat the ',' @@ -2024,7 +2069,8 @@ Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType, } case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')' Lex.Lex(); // eat the '(' - if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) { + if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast && + Lex.getCode() != tgtok::question && Lex.getCode() != tgtok::XGetOp) { TokError("expected identifier in dag init"); return nullptr; } @@ -2062,7 +2108,8 @@ Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType, case tgtok::XTail: case tgtok::XSize: case tgtok::XEmpty: - case tgtok::XCast: // Value ::= !unop '(' Value ')' + case tgtok::XCast: + case tgtok::XGetOp: // Value ::= !unop '(' Value ')' case tgtok::XIsA: case tgtok::XConcat: case tgtok::XDag: @@ -2081,7 +2128,8 @@ Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType, case tgtok::XGt: case tgtok::XListConcat: case tgtok::XListSplat: - case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')' + case tgtok::XStrConcat: + case tgtok::XSetOp: // Value ::= !binop '(' Value ',' Value ')' case tgtok::XIf: case tgtok::XCond: case tgtok::XFoldl: @@ -2555,7 +2603,11 @@ bool TGParser::ParseTemplateArgList(Record *CurRec) { /// /// BodyItem ::= Declaration ';' /// BodyItem ::= LET ID OptionalBitList '=' Value ';' +/// BodyItem ::= Defvar bool TGParser::ParseBodyItem(Record *CurRec) { + if (Lex.getCode() == tgtok::Defvar) + return ParseDefvar(); + if (Lex.getCode() != tgtok::Let) { if (!ParseDeclaration(CurRec, false)) return true; @@ -2618,10 +2670,15 @@ bool TGParser::ParseBody(Record *CurRec) { // Eat the '{'. Lex.Lex(); + // An object body introduces a new scope for local variables. + TGLocalVarScope *BodyScope = PushLocalScope(); + while (Lex.getCode() != tgtok::r_brace) if (ParseBodyItem(CurRec)) return true; + PopLocalScope(BodyScope); + // Eat the '}'. Lex.Lex(); return false; @@ -2760,6 +2817,45 @@ bool TGParser::ParseDefset() { return false; } +/// ParseDefvar - Parse a defvar statement. +/// +/// Defvar ::= DEFVAR Id '=' Value ';' +/// +bool TGParser::ParseDefvar() { + assert(Lex.getCode() == tgtok::Defvar); + Lex.Lex(); // Eat the 'defvar' token + + if (Lex.getCode() != tgtok::Id) + return TokError("expected identifier"); + StringInit *DeclName = StringInit::get(Lex.getCurStrVal()); + if (CurLocalScope) { + if (CurLocalScope->varAlreadyDefined(DeclName->getValue())) + return TokError("local variable of this name already exists"); + } else { + if (Records.getGlobal(DeclName->getValue())) + return TokError("def or global variable of this name already exists"); + } + + if (Lex.Lex() != tgtok::equal) // Eat the identifier + return TokError("expected '='"); + Lex.Lex(); // Eat the '=' + + Init *Value = ParseValue(nullptr); + if (!Value) + return true; + + if (Lex.getCode() != tgtok::semi) + return TokError("expected ';'"); + Lex.Lex(); // Eat the ';' + + if (CurLocalScope) + CurLocalScope->addVar(DeclName->getValue(), Value); + else + Records.addExtraGlobal(DeclName->getValue(), Value); + + return false; +} + /// ParseForeach - Parse a for statement. Return the record corresponding /// to it. This returns true on error. /// @@ -2785,6 +2881,9 @@ bool TGParser::ParseForeach(MultiClass *CurMultiClass) { // Create a loop object and remember it. Loops.push_back(std::make_unique<ForeachLoop>(Loc, IterName, ListValue)); + // A foreach loop introduces a new scope for local variables. + TGLocalVarScope *ForeachScope = PushLocalScope(); + if (Lex.getCode() != tgtok::l_brace) { // FOREACH Declaration IN Object if (ParseObject(CurMultiClass)) @@ -2805,6 +2904,8 @@ bool TGParser::ParseForeach(MultiClass *CurMultiClass) { Lex.Lex(); // Eat the } } + PopLocalScope(ForeachScope); + // Resolve the loop or store it for later resolution. std::unique_ptr<ForeachLoop> Loop = std::move(Loops.back()); Loops.pop_back(); @@ -2812,6 +2913,115 @@ bool TGParser::ParseForeach(MultiClass *CurMultiClass) { return addEntry(std::move(Loop)); } +/// ParseIf - Parse an if statement. +/// +/// If ::= IF Value THEN IfBody +/// If ::= IF Value THEN IfBody ELSE IfBody +/// +bool TGParser::ParseIf(MultiClass *CurMultiClass) { + SMLoc Loc = Lex.getLoc(); + assert(Lex.getCode() == tgtok::If && "Unknown tok"); + Lex.Lex(); // Eat the 'if' token. + + // Make a temporary object to record items associated with the for + // loop. + Init *Condition = ParseValue(nullptr); + if (!Condition) + return true; + + if (Lex.getCode() != tgtok::Then) + return TokError("Unknown tok"); + Lex.Lex(); // Eat the 'then' + + // We have to be able to save if statements to execute later, and they have + // to live on the same stack as foreach loops. The simplest implementation + // technique is to convert each 'then' or 'else' clause *into* a foreach + // loop, over a list of length 0 or 1 depending on the condition, and with no + // iteration variable being assigned. + + ListInit *EmptyList = ListInit::get({}, BitRecTy::get()); + ListInit *SingletonList = ListInit::get({BitInit::get(1)}, BitRecTy::get()); + RecTy *BitListTy = ListRecTy::get(BitRecTy::get()); + + // The foreach containing the then-clause selects SingletonList if + // the condition is true. + Init *ThenClauseList = + TernOpInit::get(TernOpInit::IF, Condition, SingletonList, EmptyList, + BitListTy) + ->Fold(nullptr); + Loops.push_back(std::make_unique<ForeachLoop>(Loc, nullptr, ThenClauseList)); + + if (ParseIfBody(CurMultiClass, "then")) + return true; + + std::unique_ptr<ForeachLoop> Loop = std::move(Loops.back()); + Loops.pop_back(); + + if (addEntry(std::move(Loop))) + return true; + + // Now look for an optional else clause. The if-else syntax has the usual + // dangling-else ambiguity, and by greedily matching an else here if we can, + // we implement the usual resolution of pairing with the innermost unmatched + // if. + if (Lex.getCode() == tgtok::ElseKW) { + Lex.Lex(); // Eat the 'else' + + // The foreach containing the else-clause uses the same pair of lists as + // above, but this time, selects SingletonList if the condition is *false*. + Init *ElseClauseList = + TernOpInit::get(TernOpInit::IF, Condition, EmptyList, SingletonList, + BitListTy) + ->Fold(nullptr); + Loops.push_back( + std::make_unique<ForeachLoop>(Loc, nullptr, ElseClauseList)); + + if (ParseIfBody(CurMultiClass, "else")) + return true; + + Loop = std::move(Loops.back()); + Loops.pop_back(); + + if (addEntry(std::move(Loop))) + return true; + } + + return false; +} + +/// ParseIfBody - Parse the then-clause or else-clause of an if statement. +/// +/// IfBody ::= Object +/// IfBody ::= '{' ObjectList '}' +/// +bool TGParser::ParseIfBody(MultiClass *CurMultiClass, StringRef Kind) { + TGLocalVarScope *BodyScope = PushLocalScope(); + + if (Lex.getCode() != tgtok::l_brace) { + // A single object. + if (ParseObject(CurMultiClass)) + return true; + } else { + SMLoc BraceLoc = Lex.getLoc(); + // A braced block. + Lex.Lex(); // eat the '{'. + + // Parse the object list. + if (ParseObjectList(CurMultiClass)) + return true; + + if (Lex.getCode() != tgtok::r_brace) { + TokError("expected '}' at end of '" + Kind + "' clause"); + return Error(BraceLoc, "to match this '{'"); + } + + Lex.Lex(); // Eat the } + } + + PopLocalScope(BodyScope); + return false; +} + /// ParseClass - Parse a tblgen class definition. /// /// ClassInst ::= CLASS ID TemplateArgList? ObjectBody @@ -2917,6 +3127,8 @@ bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) { return TokError("expected 'in' at end of top-level 'let'"); Lex.Lex(); + TGLocalVarScope *LetScope = PushLocalScope(); + // If this is a scalar let, just handle it now if (Lex.getCode() != tgtok::l_brace) { // LET LetList IN Object @@ -2938,6 +3150,8 @@ bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) { Lex.Lex(); } + PopLocalScope(LetScope); + // Outside this let scope, this let block is not active. LetStack.pop_back(); return false; @@ -3011,21 +3225,28 @@ bool TGParser::ParseMultiClass() { if (Lex.Lex() == tgtok::r_brace) // eat the '{'. return TokError("multiclass must contain at least one def"); + // A multiclass body introduces a new scope for local variables. + TGLocalVarScope *MulticlassScope = PushLocalScope(); + while (Lex.getCode() != tgtok::r_brace) { switch (Lex.getCode()) { default: - return TokError("expected 'let', 'def', 'defm' or 'foreach' in " - "multiclass body"); + return TokError("expected 'let', 'def', 'defm', 'defvar', 'foreach' " + "or 'if' in multiclass body"); case tgtok::Let: case tgtok::Def: case tgtok::Defm: + case tgtok::Defvar: case tgtok::Foreach: + case tgtok::If: if (ParseObject(CurMultiClass)) return true; break; } } Lex.Lex(); // eat the '}'. + + PopLocalScope(MulticlassScope); } CurMultiClass = nullptr; @@ -3167,19 +3388,24 @@ bool TGParser::ParseDefm(MultiClass *CurMultiClass) { /// Object ::= DefMInst /// Object ::= LETCommand '{' ObjectList '}' /// Object ::= LETCommand Object +/// Object ::= Defset +/// Object ::= Defvar bool TGParser::ParseObject(MultiClass *MC) { switch (Lex.getCode()) { default: - return TokError("Expected class, def, defm, defset, multiclass, let or " - "foreach"); + return TokError("Expected class, def, defm, defset, multiclass, let, " + "foreach or if"); case tgtok::Let: return ParseTopLevelLet(MC); case tgtok::Def: return ParseDef(MC); case tgtok::Foreach: return ParseForeach(MC); + case tgtok::If: return ParseIf(MC); case tgtok::Defm: return ParseDefm(MC); case tgtok::Defset: if (MC) return TokError("defset is not allowed inside multiclass"); return ParseDefset(); + case tgtok::Defvar: + return ParseDefvar(); case tgtok::Class: if (MC) return TokError("class is not allowed inside multiclass"); |