diff options
Diffstat (limited to 'llvm/lib')
-rw-r--r-- | llvm/lib/TableGen/Record.cpp | 31 | ||||
-rw-r--r-- | llvm/lib/TableGen/TGLexer.cpp | 2 | ||||
-rw-r--r-- | llvm/lib/TableGen/TGLexer.h | 2 | ||||
-rw-r--r-- | llvm/lib/TableGen/TGParser.cpp | 51 |
4 files changed, 79 insertions, 7 deletions
diff --git a/llvm/lib/TableGen/Record.cpp b/llvm/lib/TableGen/Record.cpp index 9405fb5ae4e..9db842dc678 100644 --- a/llvm/lib/TableGen/Record.cpp +++ b/llvm/lib/TableGen/Record.cpp @@ -788,6 +788,21 @@ Init *UnOpInit::Fold(Record *CurRec, bool IsFinal) const { if (StringInit *LHSs = dyn_cast<StringInit>(LHS)) return IntInit::get(LHSs->getValue().empty()); break; + + case GETOP: + if (DagInit *Dag = dyn_cast<DagInit>(LHS)) { + DefInit *DI = DefInit::get(Dag->getOperatorAsDef({})); + if (!DI->getType()->typeIsA(getType())) { + PrintFatalError(CurRec->getLoc(), + Twine("Expected type '") + + getType()->getAsString() + "', got '" + + DI->getType()->getAsString() + "' in: " + + getAsString() + "\n"); + } else { + return DI; + } + } + break; } return const_cast<UnOpInit *>(this); } @@ -809,6 +824,7 @@ std::string UnOpInit::getAsString() const { case TAIL: Result = "!tail"; break; case SIZE: Result = "!size"; break; case EMPTY: Result = "!empty"; break; + case GETOP: Result = "!getop"; break; } return Result + "(" + LHS->getAsString() + ")"; } @@ -980,6 +996,20 @@ Init *BinOpInit::Fold(Record *CurRec) const { break; } + case SETOP: { + DagInit *Dag = dyn_cast<DagInit>(LHS); + DefInit *Op = dyn_cast<DefInit>(RHS); + if (Dag && Op) { + SmallVector<Init*, 8> Args; + SmallVector<StringInit*, 8> ArgNames; + for (unsigned i = 0, e = Dag->getNumArgs(); i != e; ++i) { + Args.push_back(Dag->getArg(i)); + ArgNames.push_back(Dag->getArgName(i)); + } + return DagInit::get(Op, nullptr, Args, ArgNames); + } + break; + } case ADD: case MUL: case AND: @@ -1042,6 +1072,7 @@ std::string BinOpInit::getAsString() const { case LISTCONCAT: Result = "!listconcat"; break; case LISTSPLAT: Result = "!listsplat"; break; case STRCONCAT: Result = "!strconcat"; break; + case SETOP: Result = "!setop"; break; } return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")"; } diff --git a/llvm/lib/TableGen/TGLexer.cpp b/llvm/lib/TableGen/TGLexer.cpp index 425952338c6..d22c96e8129 100644 --- a/llvm/lib/TableGen/TGLexer.cpp +++ b/llvm/lib/TableGen/TGLexer.cpp @@ -559,6 +559,8 @@ tgtok::TokKind TGLexer::LexExclaim() { .Case("listconcat", tgtok::XListConcat) .Case("listsplat", tgtok::XListSplat) .Case("strconcat", tgtok::XStrConcat) + .Case("setop", tgtok::XSetOp) + .Case("getop", tgtok::XGetOp) .Default(tgtok::Error); return Kind != tgtok::Error ? Kind : ReturnError(Start-1, "Unknown operator"); diff --git a/llvm/lib/TableGen/TGLexer.h b/llvm/lib/TableGen/TGLexer.h index 11a9bd303f5..b5cf4bd7374 100644 --- a/llvm/lib/TableGen/TGLexer.h +++ b/llvm/lib/TableGen/TGLexer.h @@ -51,7 +51,7 @@ namespace tgtok { // !keywords. XConcat, XADD, XMUL, XAND, XOR, XSRA, XSRL, XSHL, XListConcat, XListSplat, XStrConcat, XCast, XSubst, XForEach, XFoldl, XHead, XTail, XSize, XEmpty, - XIf, XCond, XEq, XIsA, XDag, XNe, XLe, XLt, XGe, XGt, + XIf, XCond, XEq, XIsA, XDag, XNe, XLe, XLt, XGe, XGt, XSetOp, XGetOp, // Integer value. IntVal, diff --git a/llvm/lib/TableGen/TGParser.cpp b/llvm/lib/TableGen/TGParser.cpp index 2ac950b0b6f..71786328c46 100644 --- a/llvm/lib/TableGen/TGParser.cpp +++ b/llvm/lib/TableGen/TGParser.cpp @@ -905,7 +905,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 +942,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 +1078,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 +1104,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 +1113,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 +1172,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 +1237,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 ',' @@ -2025,7 +2062,7 @@ 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 && - Lex.getCode() != tgtok::question) { + Lex.getCode() != tgtok::question && Lex.getCode() != tgtok::XGetOp) { TokError("expected identifier in dag init"); return nullptr; } @@ -2063,7 +2100,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: @@ -2082,7 +2120,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: |