X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FTableGen%2FRecord.cpp;h=11e35b75375e4ac8dcf82fd4277e19f9d8efd72f;hb=53395c948459c88257c8ae57b65831fab51e4646;hp=6c7a66fcba1f55f1a8a75a0f56d4db2a25426ee4;hpb=be43b88fae1091a84d13defbfc69474f030d46ba;p=oota-llvm.git diff --git a/lib/TableGen/Record.cpp b/lib/TableGen/Record.cpp index 6c7a66fcba1..11e35b75375 100644 --- a/lib/TableGen/Record.cpp +++ b/lib/TableGen/Record.cpp @@ -88,9 +88,6 @@ DagRecTy DagRecTy::Shared; void RecTy::dump() const { print(errs()); } -void StringRecTy::anchor() { } -void DagRecTy::anchor() { } - ListRecTy *RecTy::getListTy() { if (!ListTy) ListTy.reset(new ListRecTy(this)); @@ -103,9 +100,9 @@ bool RecTy::typeIsConvertibleTo(const RecTy *RHS) const { } bool BitRecTy::typeIsConvertibleTo(const RecTy *RHS) const{ - if(RecTy::typeIsConvertibleTo(RHS) || RHS->getRecTyKind() == IntRecTyKind) + if (RecTy::typeIsConvertibleTo(RHS) || RHS->getRecTyKind() == IntRecTyKind) return true; - if(const BitsRecTy *BitsTy = dyn_cast(RHS)) + if (const BitsRecTy *BitsTy = dyn_cast(RHS)) return BitsTy->getNumBits() == 1; return false; } @@ -136,6 +133,9 @@ bool IntRecTy::typeIsConvertibleTo(const RecTy *RHS) const { return kind==BitRecTyKind || kind==BitsRecTyKind || kind==IntRecTyKind; } +std::string StringRecTy::getAsString() const { + return "string"; +} std::string ListRecTy::getAsString() const { return "list<" + Ty->getAsString() + ">"; @@ -147,6 +147,10 @@ bool ListRecTy::typeIsConvertibleTo(const RecTy *RHS) const { return false; } +std::string DagRecTy::getAsString() const { + return "dag"; +} + RecordRecTy *RecordRecTy::get(Record *R) { return dyn_cast(R->getDefInit()->getType()); } @@ -500,8 +504,8 @@ Init *ListInit::convertInitializerTo(RecTy *Ty) const { // Verify that all of the elements of the list are subclasses of the // appropriate class! - for (unsigned i = 0, e = getSize(); i != e; ++i) - if (Init *CI = getElement(i)->convertInitializerTo(LRT->getElementType())) + for (Init *I : getValues()) + if (Init *CI = I->convertInitializerTo(LRT->getElementType())) Elements.push_back(CI); else return nullptr; @@ -517,7 +521,7 @@ Init * ListInit::convertInitListSlice(const std::vector &Elements) const { std::vector Vals; for (unsigned i = 0, e = Elements.size(); i != e; ++i) { - if (Elements[i] >= getSize()) + if (Elements[i] >= size()) return nullptr; Vals.push_back(getElement(Elements[i])); } @@ -534,12 +538,11 @@ Record *ListInit::getElementAsRecord(unsigned i) const { Init *ListInit::resolveReferences(Record &R, const RecordVal *RV) const { std::vector Resolved; - Resolved.reserve(getSize()); + Resolved.reserve(size()); bool Changed = false; - for (unsigned i = 0, e = getSize(); i != e; ++i) { + for (Init *CurElt : getValues()) { Init *E; - Init *CurElt = getElement(i); do { E = CurElt; @@ -556,7 +559,7 @@ Init *ListInit::resolveReferences(Record &R, const RecordVal *RV) const { Init *ListInit::resolveListElementReference(Record &R, const RecordVal *IRV, unsigned Elt) const { - if (Elt >= getSize()) + if (Elt >= size()) return nullptr; // Out of range reference. Init *E = getElement(Elt); // If the element is set to some value, or if we are resolving a reference @@ -670,6 +673,14 @@ Init *UnOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const { PrintFatalError(CurRec->getLoc(), "Undefined reference:'" + Name + "'\n"); } + + if (isa(getType())) { + if (BitsInit *BI = dyn_cast(LHS)) { + if (Init *NewInit = BI->convertInitializerTo(IntRecTy::get())) + return NewInit; + break; + } + } } break; } @@ -711,7 +722,7 @@ Init *UnOpInit::resolveReferences(Record &R, const RecordVal *RV) const { std::string UnOpInit::getAsString() const { std::string Result; - switch (Opc) { + switch (getOpcode()) { case CAST: Result = "!cast<" + getType()->getAsString() + ">"; break; case HEAD: Result = "!head"; break; case TAIL: Result = "!tail"; break; @@ -839,7 +850,7 @@ Init *BinOpInit::resolveReferences(Record &R, const RecordVal *RV) const { std::string BinOpInit::getAsString() const { std::string Result; - switch (Opc) { + switch (getOpcode()) { case CONCAT: Result = "!con"; break; case ADD: Result = "!add"; break; case AND: Result = "!and"; break; @@ -888,7 +899,7 @@ static Init *EvaluateOperation(OpInit *RHSo, Init *LHS, Init *Arg, return ForeachHelper(LHS, Arg, RHSo, Type, CurRec, CurMultiClass); std::vector NewOperands; - for (int i = 0; i < RHSo->getNumOperands(); ++i) { + for (unsigned i = 0; i < RHSo->getNumOperands(); ++i) { if (auto *RHSoo = dyn_cast(RHSo->getOperand(i))) { if (Init *Result = EvaluateOperation(RHSoo, LHS, Arg, Type, CurRec, CurMultiClass)) @@ -910,8 +921,6 @@ static Init *EvaluateOperation(OpInit *RHSo, Init *LHS, Init *Arg, static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type, Record *CurRec, MultiClass *CurMultiClass) { - DagInit *MHSd = dyn_cast(MHS); - ListInit *MHSl = dyn_cast(MHS); OpInit *RHSo = dyn_cast(RHS); @@ -923,55 +932,52 @@ static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type, if (!LHSt) PrintFatalError(CurRec->getLoc(), "!foreach requires typed variable\n"); - if ((MHSd && isa(Type)) || (MHSl && isa(Type))) { - if (MHSd) { - Init *Val = MHSd->getOperator(); - Init *Result = EvaluateOperation(RHSo, LHS, Val, - Type, CurRec, CurMultiClass); - if (Result) - Val = Result; - - std::vector > args; - for (unsigned int i = 0; i < MHSd->getNumArgs(); ++i) { - Init *Arg; - std::string ArgName; - Arg = MHSd->getArg(i); - ArgName = MHSd->getArgName(i); - - // Process args - Init *Result = EvaluateOperation(RHSo, LHS, Arg, Type, - CurRec, CurMultiClass); - if (Result) - Arg = Result; - - // TODO: Process arg names - args.push_back(std::make_pair(Arg, ArgName)); - } - - return DagInit::get(Val, "", args); + DagInit *MHSd = dyn_cast(MHS); + if (MHSd && isa(Type)) { + Init *Val = MHSd->getOperator(); + if (Init *Result = EvaluateOperation(RHSo, LHS, Val, + Type, CurRec, CurMultiClass)) + Val = Result; + + std::vector > args; + for (unsigned int i = 0; i < MHSd->getNumArgs(); ++i) { + Init *Arg = MHSd->getArg(i); + std::string ArgName = MHSd->getArgName(i); + + // Process args + if (Init *Result = EvaluateOperation(RHSo, LHS, Arg, Type, + CurRec, CurMultiClass)) + Arg = Result; + + // TODO: Process arg names + args.push_back(std::make_pair(Arg, ArgName)); } - if (MHSl) { - std::vector NewOperands; - std::vector NewList(MHSl->begin(), MHSl->end()); - - for (Init *&Item : NewList) { - NewOperands.clear(); - for(int i = 0; i < RHSo->getNumOperands(); ++i) { - // First, replace the foreach variable with the list item - if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) - NewOperands.push_back(Item); - else - NewOperands.push_back(RHSo->getOperand(i)); - } - // Now run the operator and use its result as the new list item - const OpInit *NewOp = RHSo->clone(NewOperands); - Init *NewItem = NewOp->Fold(CurRec, CurMultiClass); - if (NewItem != NewOp) - Item = NewItem; + return DagInit::get(Val, "", args); + } + + ListInit *MHSl = dyn_cast(MHS); + if (MHSl && isa(Type)) { + std::vector NewOperands; + std::vector NewList(MHSl->begin(), MHSl->end()); + + for (Init *&Item : NewList) { + NewOperands.clear(); + for(unsigned i = 0; i < RHSo->getNumOperands(); ++i) { + // First, replace the foreach variable with the list item + if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) + NewOperands.push_back(Item); + else + NewOperands.push_back(RHSo->getOperand(i)); } - return ListInit::get(NewList, MHSl->getType()); + + // Now run the operator and use its result as the new list item + const OpInit *NewOp = RHSo->clone(NewOperands); + Init *NewItem = NewOp->Fold(CurRec, CurMultiClass); + if (NewItem != NewOp) + Item = NewItem; } + return ListInit::get(NewList, MHSl->getType()); } return nullptr; } @@ -1048,7 +1054,7 @@ Init *TernOpInit::resolveReferences(Record &R, const RecordVal *RV) const { Init *lhs = LHS->resolveReferences(R, RV); - if (Opc == IF && lhs != LHS) { + if (getOpcode() == IF && lhs != LHS) { IntInit *Value = dyn_cast(lhs); if (Init *I = lhs->convertInitializerTo(IntRecTy::get())) Value = dyn_cast(I); @@ -1076,7 +1082,7 @@ Init *TernOpInit::resolveReferences(Record &R, std::string TernOpInit::getAsString() const { std::string Result; - switch (Opc) { + switch (getOpcode()) { case SUBST: Result = "!subst"; break; case FOREACH: Result = "!foreach"; break; case IF: Result = "!if"; break; @@ -1240,7 +1246,7 @@ Init *VarInit::resolveListElementReference(Record &R, if (!LI) return VarListElementInit::get(cast(RV->getValue()), Elt); - if (Elt >= LI->getSize()) + if (Elt >= LI->size()) return nullptr; // Out of range reference. Init *E = LI->getElement(Elt); // If the element is set to some value, or if we are resolving a reference @@ -1351,8 +1357,8 @@ Init *VarListElementInit:: resolveListElementReference(Record &R, unsigned Elt) const { if (Init *Result = TI->resolveListElementReference(R, RV, Element)) { if (TypedInit *TInit = dyn_cast(Result)) { - Init *Result2 = TInit->resolveListElementReference(R, RV, Elt); - if (Result2) return Result2; + if (Init *Result2 = TInit->resolveListElementReference(R, RV, Elt)) + return Result2; return VarListElementInit::get(TInit, Elt); } return Result; @@ -1409,7 +1415,7 @@ Init *FieldInit::resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) const { if (Init *ListVal = Rec->getFieldInit(R, RV, FieldName)) if (ListInit *LI = dyn_cast(ListVal)) { - if (Elt >= LI->getSize()) return nullptr; + if (Elt >= LI->size()) return nullptr; Init *E = LI->getElement(Elt); // If the element is set to some value, or if we are resolving a @@ -1529,20 +1535,20 @@ std::string DagInit::getAsString() const { // Other implementations //===----------------------------------------------------------------------===// -RecordVal::RecordVal(Init *N, RecTy *T, unsigned P) - : Name(N), Ty(T), Prefix(P) { +RecordVal::RecordVal(Init *N, RecTy *T, bool P) + : NameAndPrefix(N, P), Ty(T) { Value = UnsetInit::get()->convertInitializerTo(Ty); assert(Value && "Cannot create unset value for current type!"); } -RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P) - : Name(StringInit::get(N)), Ty(T), Prefix(P) { +RecordVal::RecordVal(const std::string &N, RecTy *T, bool P) + : NameAndPrefix(StringInit::get(N), P), Ty(T) { Value = UnsetInit::get()->convertInitializerTo(Ty); assert(Value && "Cannot create unset value for current type!"); } const std::string &RecordVal::getName() const { - return cast(Name)->getValue(); + return cast(getNameInit())->getValue(); } void RecordVal::dump() const { errs() << *this; } @@ -1571,19 +1577,14 @@ void Record::init() { void Record::checkName() { // Ensure the record name has string type. const TypedInit *TypedName = cast(Name); - RecTy *Type = TypedName->getType(); - if (!isa(Type)) + if (!isa(TypedName->getType())) PrintFatalError(getLoc(), "Record name is not a string!"); } DefInit *Record::getDefInit() { - static DenseMap> ThePool; - if (TheInit) - return TheInit; - - std::unique_ptr &I = ThePool[this]; - if (!I) I.reset(new DefInit(this, new RecordRecTy(this))); - return I.get(); + if (!TheInit) + TheInit.reset(new DefInit(this, new RecordRecTy(this))); + return TheInit.get(); } const std::string &Record::getName() const { @@ -1640,12 +1641,14 @@ void Record::dump() const { errs() << *this; } raw_ostream &llvm::operator<<(raw_ostream &OS, const Record &R) { OS << R.getNameInitAsString(); - const std::vector &TArgs = R.getTemplateArgs(); + ArrayRef TArgs = R.getTemplateArgs(); if (!TArgs.empty()) { OS << "<"; - for (unsigned i = 0, e = TArgs.size(); i != e; ++i) { - if (i) OS << ", "; - const RecordVal *RV = R.getValue(TArgs[i]); + bool NeedComma = false; + for (const Init *TA : TArgs) { + if (NeedComma) OS << ", "; + NeedComma = true; + const RecordVal *RV = R.getValue(TA); assert(RV && "Template argument record not found??"); RV->print(OS, false); } @@ -1653,21 +1656,20 @@ raw_ostream &llvm::operator<<(raw_ostream &OS, const Record &R) { } OS << " {"; - const std::vector &SC = R.getSuperClasses(); + ArrayRef SC = R.getSuperClasses(); if (!SC.empty()) { OS << "\t//"; - for (unsigned i = 0, e = SC.size(); i != e; ++i) - OS << " " << SC[i]->getNameInitAsString(); + for (const Record *Super : SC) + OS << " " << Super->getNameInitAsString(); } OS << "\n"; - const std::vector &Vals = R.getValues(); - for (unsigned i = 0, e = Vals.size(); i != e; ++i) - if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName())) - OS << Vals[i]; - for (unsigned i = 0, e = Vals.size(); i != e; ++i) - if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName())) - OS << Vals[i]; + for (const RecordVal &Val : R.getValues()) + if (Val.getPrefix() && !R.isTemplateArg(Val.getName())) + OS << Val; + for (const RecordVal &Val : R.getValues()) + if (!Val.getPrefix() && !R.isTemplateArg(Val.getName())) + OS << Val; return OS << "}\n"; } @@ -1740,8 +1742,8 @@ std::vector Record::getValueAsListOfDefs(StringRef FieldName) const { ListInit *List = getValueAsListInit(FieldName); std::vector Defs; - for (unsigned i = 0; i < List->getSize(); i++) { - if (DefInit *DI = dyn_cast(List->getElement(i))) + for (Init *I : List->getValues()) { + if (DefInit *DI = dyn_cast(I)) Defs.push_back(DI->getDef()); else PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + @@ -1774,8 +1776,8 @@ std::vector Record::getValueAsListOfInts(StringRef FieldName) const { ListInit *List = getValueAsListInit(FieldName); std::vector Ints; - for (unsigned i = 0; i < List->getSize(); i++) { - if (IntInit *II = dyn_cast(List->getElement(i))) + for (Init *I : List->getValues()) { + if (IntInit *II = dyn_cast(I)) Ints.push_back(II->getValue()); else PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + @@ -1792,9 +1794,9 @@ std::vector Record::getValueAsListOfStrings(StringRef FieldName) const { ListInit *List = getValueAsListInit(FieldName); std::vector Strings; - for (unsigned i = 0; i < List->getSize(); i++) { - if (StringInit *II = dyn_cast(List->getElement(i))) - Strings.push_back(II->getValue()); + for (Init *I : List->getValues()) { + if (StringInit *SI = dyn_cast(I)) + Strings.push_back(SI->getValue()); else PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + FieldName + "' does not have a list of strings initializer!"); @@ -1916,23 +1918,23 @@ Init *llvm::QualifyName(Record &CurRec, MultiClass *CurMultiClass, RecTy *Type = cast(Name)->getType(); BinOpInit *NewName = - BinOpInit::get(BinOpInit::STRCONCAT, - BinOpInit::get(BinOpInit::STRCONCAT, - CurRec.getNameInit(), - StringInit::get(Scoper), - Type)->Fold(&CurRec, CurMultiClass), - Name, - Type); + BinOpInit::get(BinOpInit::STRCONCAT, + BinOpInit::get(BinOpInit::STRCONCAT, + CurRec.getNameInit(), + StringInit::get(Scoper), + Type)->Fold(&CurRec, CurMultiClass), + Name, + Type); if (CurMultiClass && Scoper != "::") { NewName = - BinOpInit::get(BinOpInit::STRCONCAT, - BinOpInit::get(BinOpInit::STRCONCAT, - CurMultiClass->Rec.getNameInit(), - StringInit::get("::"), - Type)->Fold(&CurRec, CurMultiClass), - NewName->Fold(&CurRec, CurMultiClass), - Type); + BinOpInit::get(BinOpInit::STRCONCAT, + BinOpInit::get(BinOpInit::STRCONCAT, + CurMultiClass->Rec.getNameInit(), + StringInit::get("::"), + Type)->Fold(&CurRec, CurMultiClass), + NewName->Fold(&CurRec, CurMultiClass), + Type); } return NewName->Fold(&CurRec, CurMultiClass);