From b1ed0fc6308885c5887ed0474e8a4fbf63a39b45 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Tue, 3 Dec 2002 06:00:33 +0000 Subject: [PATCH] Continue implementing field initializers git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@4879 91177308-0d34-0410-b5e6-96231b3b80d8 --- support/tools/TableGen/Record.cpp | 70 ++++++++++++++++++------------- support/tools/TableGen/Record.h | 12 ++++++ utils/TableGen/Record.cpp | 70 ++++++++++++++++++------------- utils/TableGen/Record.h | 12 ++++++ 4 files changed, 104 insertions(+), 60 deletions(-) diff --git a/support/tools/TableGen/Record.cpp b/support/tools/TableGen/Record.cpp index 8f6d4305a17..5931cb8db26 100644 --- a/support/tools/TableGen/Record.cpp +++ b/support/tools/TableGen/Record.cpp @@ -82,25 +82,6 @@ Init *BitsRecTy::convertValue(TypedInit *VI) { return 0; } -#if 0 -Init *BitsRecTy::convertValue(FieldInit *VI) { - if (BitsRecTy *BRT = dynamic_cast(VI->getType())) - if (BRT->Size == Size) { - BitsInit *Ret = new BitsInit(Size); - for (unsigned i = 0; i != Size; ++i) - Ret->setBit(i, new VarBitInit(VI, i)); - return Ret; - } - if (Size == 1 && dynamic_cast(VI->getType())) { - BitsInit *Ret = new BitsInit(1); - Ret->setBit(0, VI); - return Ret; - } - return 0; -} -#endif - - Init *IntRecTy::convertValue(BitsInit *BI) { int Result = 0; for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) @@ -168,8 +149,8 @@ Init *BitsInit::convertInitializerBitRange(const std::vector &Bits) { void BitsInit::print(std::ostream &OS) const { //if (!printInHex(OS)) return; - if (!printAsVariable(OS)) return; - if (!printAsUnset(OS)) return; + //if (!printAsVariable(OS)) return; + //if (!printAsUnset(OS)) return; OS << "{ "; for (unsigned i = 0, e = getNumBits(); i != e; ++i) { @@ -282,13 +263,6 @@ Init *VarInit::convertInitializerBitRange(const std::vector &Bits) { return BI; } -RecTy *VarInit::getFieldType(const std::string &FieldName) const { - if (RecordRecTy *RTy = dynamic_cast(getType())) - if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName)) - return RV->getType(); - return 0; -} - Init *VarInit::resolveBitReference(Record &R, unsigned Bit) { if (R.isTemplateArg(getName())) return this; @@ -306,16 +280,43 @@ Init *VarInit::resolveBitReference(Record &R, unsigned Bit) { return this; } +RecTy *VarInit::getFieldType(const std::string &FieldName) const { + if (RecordRecTy *RTy = dynamic_cast(getType())) + if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName)) + return RV->getType(); + return 0; +} + +Init *VarInit::getFieldInit(Record &R, const std::string &FieldName) const { + if (RecordRecTy *RTy = dynamic_cast(getType())) + if (const RecordVal *RV = R.getValue(VarName)) + if (Init *I = RV->getValue()->getFieldInit(R, FieldName)) + return I; + else + return (Init*)this; + return 0; +} + Init *VarBitInit::resolveReferences(Record &R) { Init *I = getVariable()->resolveBitReference(R, getBitNum()); - if (I != getVariable()) return I; return this; } +RecTy *DefInit::getFieldType(const std::string &FieldName) const { + if (const RecordVal *RV = Def->getValue(FieldName)) + return RV->getType(); + return 0; +} + +Init *DefInit::getFieldInit(Record &R, const std::string &FieldName) const { + return Def->getValue(FieldName)->getValue(); +} + + void DefInit::print(std::ostream &OS) const { OS << Def->getName(); } @@ -337,7 +338,16 @@ Init *FieldInit::convertInitializerBitRange(const std::vector &Bits) { } Init *FieldInit::resolveBitReference(Record &R, unsigned Bit) { - // Can never be resolved yet. + Init *BitsVal = Rec->getFieldInit(R, FieldName); + assert(BitsVal && "No initializer found!"); + + if (BitsInit *BI = dynamic_cast(BitsVal)) { + assert(Bit < BI->getNumBits() && "Bit reference out of range!"); + Init *B = BI->getBit(Bit); + + if (dynamic_cast(B)) // If the bit is set... + return B; // Replace the VarBitInit with it. + } return this; } diff --git a/support/tools/TableGen/Record.h b/support/tools/TableGen/Record.h index e736f8091a3..8c551400a36 100644 --- a/support/tools/TableGen/Record.h +++ b/support/tools/TableGen/Record.h @@ -180,6 +180,14 @@ struct Init { /// virtual RecTy *getFieldType(const std::string &FieldName) const { return 0; } + /// getFieldInit - This method complements getFieldType to return the + /// initializer for the specified field. If getFieldType returns non-null + /// this method should return non-null, otherwise it returns null. + /// + virtual Init *getFieldInit(Record &R, const std::string &FieldName) const { + return 0; + } + /// resolveReferences - This method is used by classes that refer to other /// variables which may not be defined at the time they expression is formed. /// If a value is set for the variable later, this method will be called on @@ -352,6 +360,7 @@ public: virtual Init *resolveBitReference(Record &R, unsigned Bit); virtual RecTy *getFieldType(const std::string &FieldName) const; + virtual Init *getFieldInit(Record &R, const std::string &FieldName) const; virtual void print(std::ostream &OS) const { OS << VarName; } }; @@ -397,6 +406,9 @@ public: Record *getDef() const { return Def; } //virtual Init *convertInitializerBitRange(const std::vector &Bits); + + virtual RecTy *getFieldType(const std::string &FieldName) const; + virtual Init *getFieldInit(Record &R, const std::string &FieldName) const; virtual void print(std::ostream &OS) const; }; diff --git a/utils/TableGen/Record.cpp b/utils/TableGen/Record.cpp index 8f6d4305a17..5931cb8db26 100644 --- a/utils/TableGen/Record.cpp +++ b/utils/TableGen/Record.cpp @@ -82,25 +82,6 @@ Init *BitsRecTy::convertValue(TypedInit *VI) { return 0; } -#if 0 -Init *BitsRecTy::convertValue(FieldInit *VI) { - if (BitsRecTy *BRT = dynamic_cast(VI->getType())) - if (BRT->Size == Size) { - BitsInit *Ret = new BitsInit(Size); - for (unsigned i = 0; i != Size; ++i) - Ret->setBit(i, new VarBitInit(VI, i)); - return Ret; - } - if (Size == 1 && dynamic_cast(VI->getType())) { - BitsInit *Ret = new BitsInit(1); - Ret->setBit(0, VI); - return Ret; - } - return 0; -} -#endif - - Init *IntRecTy::convertValue(BitsInit *BI) { int Result = 0; for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) @@ -168,8 +149,8 @@ Init *BitsInit::convertInitializerBitRange(const std::vector &Bits) { void BitsInit::print(std::ostream &OS) const { //if (!printInHex(OS)) return; - if (!printAsVariable(OS)) return; - if (!printAsUnset(OS)) return; + //if (!printAsVariable(OS)) return; + //if (!printAsUnset(OS)) return; OS << "{ "; for (unsigned i = 0, e = getNumBits(); i != e; ++i) { @@ -282,13 +263,6 @@ Init *VarInit::convertInitializerBitRange(const std::vector &Bits) { return BI; } -RecTy *VarInit::getFieldType(const std::string &FieldName) const { - if (RecordRecTy *RTy = dynamic_cast(getType())) - if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName)) - return RV->getType(); - return 0; -} - Init *VarInit::resolveBitReference(Record &R, unsigned Bit) { if (R.isTemplateArg(getName())) return this; @@ -306,16 +280,43 @@ Init *VarInit::resolveBitReference(Record &R, unsigned Bit) { return this; } +RecTy *VarInit::getFieldType(const std::string &FieldName) const { + if (RecordRecTy *RTy = dynamic_cast(getType())) + if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName)) + return RV->getType(); + return 0; +} + +Init *VarInit::getFieldInit(Record &R, const std::string &FieldName) const { + if (RecordRecTy *RTy = dynamic_cast(getType())) + if (const RecordVal *RV = R.getValue(VarName)) + if (Init *I = RV->getValue()->getFieldInit(R, FieldName)) + return I; + else + return (Init*)this; + return 0; +} + Init *VarBitInit::resolveReferences(Record &R) { Init *I = getVariable()->resolveBitReference(R, getBitNum()); - if (I != getVariable()) return I; return this; } +RecTy *DefInit::getFieldType(const std::string &FieldName) const { + if (const RecordVal *RV = Def->getValue(FieldName)) + return RV->getType(); + return 0; +} + +Init *DefInit::getFieldInit(Record &R, const std::string &FieldName) const { + return Def->getValue(FieldName)->getValue(); +} + + void DefInit::print(std::ostream &OS) const { OS << Def->getName(); } @@ -337,7 +338,16 @@ Init *FieldInit::convertInitializerBitRange(const std::vector &Bits) { } Init *FieldInit::resolveBitReference(Record &R, unsigned Bit) { - // Can never be resolved yet. + Init *BitsVal = Rec->getFieldInit(R, FieldName); + assert(BitsVal && "No initializer found!"); + + if (BitsInit *BI = dynamic_cast(BitsVal)) { + assert(Bit < BI->getNumBits() && "Bit reference out of range!"); + Init *B = BI->getBit(Bit); + + if (dynamic_cast(B)) // If the bit is set... + return B; // Replace the VarBitInit with it. + } return this; } diff --git a/utils/TableGen/Record.h b/utils/TableGen/Record.h index e736f8091a3..8c551400a36 100644 --- a/utils/TableGen/Record.h +++ b/utils/TableGen/Record.h @@ -180,6 +180,14 @@ struct Init { /// virtual RecTy *getFieldType(const std::string &FieldName) const { return 0; } + /// getFieldInit - This method complements getFieldType to return the + /// initializer for the specified field. If getFieldType returns non-null + /// this method should return non-null, otherwise it returns null. + /// + virtual Init *getFieldInit(Record &R, const std::string &FieldName) const { + return 0; + } + /// resolveReferences - This method is used by classes that refer to other /// variables which may not be defined at the time they expression is formed. /// If a value is set for the variable later, this method will be called on @@ -352,6 +360,7 @@ public: virtual Init *resolveBitReference(Record &R, unsigned Bit); virtual RecTy *getFieldType(const std::string &FieldName) const; + virtual Init *getFieldInit(Record &R, const std::string &FieldName) const; virtual void print(std::ostream &OS) const { OS << VarName; } }; @@ -397,6 +406,9 @@ public: Record *getDef() const { return Def; } //virtual Init *convertInitializerBitRange(const std::vector &Bits); + + virtual RecTy *getFieldType(const std::string &FieldName) const; + virtual Init *getFieldInit(Record &R, const std::string &FieldName) const; virtual void print(std::ostream &OS) const; }; -- 2.34.1