From: Sean Silva Date: Mon, 7 Jan 2013 02:30:19 +0000 (+0000) Subject: Simplify TableGen type-compatibility checks. X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=37fda193d292d78c672b8019723c7907af312c43;p=oota-llvm.git Simplify TableGen type-compatibility checks. Patch by Elior Malul! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@171684 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/TableGen/Record.h b/include/llvm/TableGen/Record.h index 647c47f27c5..8968534aba2 100644 --- a/include/llvm/TableGen/Record.h +++ b/include/llvm/TableGen/Record.h @@ -128,16 +128,8 @@ public: // These methods should only be called from subclasses of Init return convertValue((TypedInit*)FI); } -public: // These methods should only be called by subclasses of RecTy. - // baseClassOf - These virtual methods should be overloaded to return true iff - // all values of type 'RHS' can be converted to the 'this' type. - virtual bool baseClassOf(const BitRecTy *RHS) const { return false; } - virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; } - virtual bool baseClassOf(const IntRecTy *RHS) const { return false; } - virtual bool baseClassOf(const StringRecTy *RHS) const { return false; } - virtual bool baseClassOf(const ListRecTy *RHS) const { return false; } - virtual bool baseClassOf(const DagRecTy *RHS) const { return false; } - virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; } +public: + virtual bool baseClassOf(const RecTy*) const; }; inline raw_ostream &operator<<(raw_ostream &OS, const RecTy &Ty) { @@ -179,14 +171,7 @@ public: virtual bool typeIsConvertibleTo(const RecTy *RHS) const { return RHS->baseClassOf(this); } - virtual bool baseClassOf(const BitRecTy *RHS) const { return true; } - virtual bool baseClassOf(const BitsRecTy *RHS) const; - virtual bool baseClassOf(const IntRecTy *RHS) const { return true; } - virtual bool baseClassOf(const StringRecTy *RHS) const { return false; } - virtual bool baseClassOf(const ListRecTy *RHS) const { return false; } - virtual bool baseClassOf(const DagRecTy *RHS) const { return false; } - virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; } - + virtual bool baseClassOf(const RecTy*) const; }; @@ -226,16 +211,7 @@ public: virtual bool typeIsConvertibleTo(const RecTy *RHS) const { return RHS->baseClassOf(this); } - virtual bool baseClassOf(const BitRecTy *RHS) const { return Size == 1; } - virtual bool baseClassOf(const BitsRecTy *RHS) const { - return RHS->Size == Size; - } - virtual bool baseClassOf(const IntRecTy *RHS) const { return true; } - virtual bool baseClassOf(const StringRecTy *RHS) const { return false; } - virtual bool baseClassOf(const ListRecTy *RHS) const { return false; } - virtual bool baseClassOf(const DagRecTy *RHS) const { return false; } - virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; } - + virtual bool baseClassOf(const RecTy*) const; }; @@ -273,14 +249,7 @@ public: return RHS->baseClassOf(this); } - virtual bool baseClassOf(const BitRecTy *RHS) const { return true; } - virtual bool baseClassOf(const BitsRecTy *RHS) const { return true; } - virtual bool baseClassOf(const IntRecTy *RHS) const { return true; } - virtual bool baseClassOf(const StringRecTy *RHS) const { return false; } - virtual bool baseClassOf(const ListRecTy *RHS) const { return false; } - virtual bool baseClassOf(const DagRecTy *RHS) const { return false; } - virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; } - + virtual bool baseClassOf(const RecTy*) const; }; /// StringRecTy - 'string' - Represent an string value @@ -317,14 +286,6 @@ public: virtual bool typeIsConvertibleTo(const RecTy *RHS) const { return RHS->baseClassOf(this); } - - virtual bool baseClassOf(const BitRecTy *RHS) const { return false; } - virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; } - virtual bool baseClassOf(const IntRecTy *RHS) const { return false; } - virtual bool baseClassOf(const StringRecTy *RHS) const { return true; } - virtual bool baseClassOf(const ListRecTy *RHS) const { return false; } - virtual bool baseClassOf(const DagRecTy *RHS) const { return false; } - virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; } }; // ListRecTy - 'list' - Represent a list of values, all of which must be of @@ -366,15 +327,7 @@ public: return RHS->baseClassOf(this); } - virtual bool baseClassOf(const BitRecTy *RHS) const { return false; } - virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; } - virtual bool baseClassOf(const IntRecTy *RHS) const { return false; } - virtual bool baseClassOf(const StringRecTy *RHS) const { return false; } - virtual bool baseClassOf(const ListRecTy *RHS) const { - return RHS->getElementType()->typeIsConvertibleTo(Ty); - } - virtual bool baseClassOf(const DagRecTy *RHS) const { return false; } - virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; } + virtual bool baseClassOf(const RecTy*) const; }; /// DagRecTy - 'dag' - Represent a dag fragment @@ -410,14 +363,6 @@ public: virtual bool typeIsConvertibleTo(const RecTy *RHS) const { return RHS->baseClassOf(this); } - - virtual bool baseClassOf(const BitRecTy *RHS) const { return false; } - virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; } - virtual bool baseClassOf(const IntRecTy *RHS) const { return false; } - virtual bool baseClassOf(const StringRecTy *RHS) const { return false; } - virtual bool baseClassOf(const ListRecTy *RHS) const { return false; } - virtual bool baseClassOf(const DagRecTy *RHS) const { return true; } - virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; } }; @@ -458,13 +403,7 @@ public: virtual bool typeIsConvertibleTo(const RecTy *RHS) const { return RHS->baseClassOf(this); } - virtual bool baseClassOf(const BitRecTy *RHS) const { return false; } - virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; } - virtual bool baseClassOf(const IntRecTy *RHS) const { return false; } - virtual bool baseClassOf(const StringRecTy *RHS) const { return false; } - virtual bool baseClassOf(const ListRecTy *RHS) const { return false; } - virtual bool baseClassOf(const DagRecTy *RHS) const { return false; } - virtual bool baseClassOf(const RecordRecTy *RHS) const; + virtual bool baseClassOf(const RecTy*) const; }; /// resolveTypes - Find a common type that T1 and T2 convert to. diff --git a/lib/TableGen/Record.cpp b/lib/TableGen/Record.cpp index 9b65adcf85e..b1d3a5bbb15 100644 --- a/lib/TableGen/Record.cpp +++ b/lib/TableGen/Record.cpp @@ -95,15 +95,16 @@ ListRecTy *RecTy::getListTy() { return ListTy; } +bool RecTy::baseClassOf(const RecTy *RHS) const{ + assert (RHS && "NULL pointer"); + return Kind == RHS->getRecTyKind(); +} + Init *BitRecTy::convertValue(BitsInit *BI) { if (BI->getNumBits() != 1) return 0; // Only accept if just one bit! return BI->getBit(0); } -bool BitRecTy::baseClassOf(const BitsRecTy *RHS) const { - return RHS->getNumBits() == 1; -} - Init *BitRecTy::convertValue(IntInit *II) { int64_t Val = II->getValue(); if (Val != 0 && Val != 1) return 0; // Only accept 0 or 1 for a bit! @@ -118,6 +119,14 @@ Init *BitRecTy::convertValue(TypedInit *VI) { return 0; } +bool BitRecTy::baseClassOf(const RecTy *RHS) const{ + if(RecTy::baseClassOf(RHS) || getRecTyKind() == IntRecTyKind) + return true; + if(const BitsRecTy *BitsTy = dyn_cast(RHS)) + return BitsTy->getNumBits() == 1; + return false; +} + BitsRecTy *BitsRecTy::get(unsigned Sz) { static std::vector Shared; if (Sz >= Shared.size()) @@ -193,6 +202,13 @@ Init *BitsRecTy::convertValue(TypedInit *VI) { return 0; } +bool BitsRecTy::baseClassOf(const RecTy *RHS) const{ + if (RecTy::baseClassOf(RHS)) //argument and the receiver are the same type + return cast(RHS)->Size == Size; + RecTyKind kind = RHS->getRecTyKind(); + return (kind == BitRecTyKind && Size == 1) || (kind == IntRecTyKind); +} + Init *IntRecTy::convertValue(BitInit *BI) { return IntInit::get(BI->getValue()); } @@ -214,6 +230,11 @@ Init *IntRecTy::convertValue(TypedInit *TI) { return 0; } +bool IntRecTy::baseClassOf(const RecTy *RHS) const{ + RecTyKind kind = RHS->getRecTyKind(); + return kind==BitRecTyKind || kind==BitsRecTyKind || kind==IntRecTyKind; +} + Init *StringRecTy::convertValue(UnOpInit *BO) { if (BO->getOpcode() == UnOpInit::CAST) { Init *L = BO->getOperand()->convertInitializerTo(this); @@ -275,6 +296,12 @@ Init *ListRecTy::convertValue(TypedInit *TI) { return 0; } +bool ListRecTy::baseClassOf(const RecTy *RHS) const{ + if(const ListRecTy* ListTy = dyn_cast(RHS)) + return ListTy->getElementType()->typeIsConvertibleTo(Ty); + return false; +} + Init *DagRecTy::convertValue(TypedInit *TI) { if (TI->getType()->typeIsConvertibleTo(this)) return TI; @@ -328,13 +355,17 @@ Init *RecordRecTy::convertValue(TypedInit *TI) { return 0; } -bool RecordRecTy::baseClassOf(const RecordRecTy *RHS) const { - if (Rec == RHS->getRecord() || RHS->getRecord()->isSubClassOf(Rec)) +bool RecordRecTy::baseClassOf(const RecTy *RHS) const{ + const RecordRecTy *RTy = dyn_cast(RHS); + if (!RTy) + return false; + + if (Rec == RTy->getRecord() || RTy->getRecord()->isSubClassOf(Rec)) return true; const std::vector &SC = Rec->getSuperClasses(); for (unsigned i = 0, e = SC.size(); i != e; ++i) - if (RHS->getRecord()->isSubClassOf(SC[i])) + if (RTy->getRecord()->isSubClassOf(SC[i])) return true; return false;