1 //===- Record.cpp - Record implementation ---------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // Implement the tablegen record classes. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/TableGen/Record.h" 15 #include "llvm/ADT/DenseMap.h" 16 #include "llvm/ADT/FoldingSet.h" 17 #include "llvm/ADT/Hashing.h" 18 #include "llvm/ADT/STLExtras.h" 19 #include "llvm/ADT/SmallVector.h" 20 #include "llvm/ADT/StringExtras.h" 21 #include "llvm/ADT/StringMap.h" 22 #include "llvm/Support/DataTypes.h" 23 #include "llvm/Support/ErrorHandling.h" 24 #include "llvm/Support/Format.h" 25 #include "llvm/TableGen/Error.h" 26 27 using namespace llvm; 28 29 //===----------------------------------------------------------------------===// 30 // std::string wrapper for DenseMap purposes 31 //===----------------------------------------------------------------------===// 32 33 namespace llvm { 34 35 /// TableGenStringKey - This is a wrapper for std::string suitable for 36 /// using as a key to a DenseMap. Because there isn't a particularly 37 /// good way to indicate tombstone or empty keys for strings, we want 38 /// to wrap std::string to indicate that this is a "special" string 39 /// not expected to take on certain values (those of the tombstone and 40 /// empty keys). This makes things a little safer as it clarifies 41 /// that DenseMap is really not appropriate for general strings. 42 43 class TableGenStringKey { 44 public: 45 TableGenStringKey(const std::string &str) : data(str) {} 46 TableGenStringKey(const char *str) : data(str) {} 47 48 const std::string &str() const { return data; } 49 50 friend hash_code hash_value(const TableGenStringKey &Value) { 51 using llvm::hash_value; 52 return hash_value(Value.str()); 53 } 54 private: 55 std::string data; 56 }; 57 58 /// Specialize DenseMapInfo for TableGenStringKey. 59 template<> struct DenseMapInfo<TableGenStringKey> { 60 static inline TableGenStringKey getEmptyKey() { 61 TableGenStringKey Empty("<<<EMPTY KEY>>>"); 62 return Empty; 63 } 64 static inline TableGenStringKey getTombstoneKey() { 65 TableGenStringKey Tombstone("<<<TOMBSTONE KEY>>>"); 66 return Tombstone; 67 } 68 static unsigned getHashValue(const TableGenStringKey& Val) { 69 using llvm::hash_value; 70 return hash_value(Val); 71 } 72 static bool isEqual(const TableGenStringKey& LHS, 73 const TableGenStringKey& RHS) { 74 return LHS.str() == RHS.str(); 75 } 76 }; 77 78 } // namespace llvm 79 80 //===----------------------------------------------------------------------===// 81 // Type implementations 82 //===----------------------------------------------------------------------===// 83 84 BitRecTy BitRecTy::Shared; 85 IntRecTy IntRecTy::Shared; 86 StringRecTy StringRecTy::Shared; 87 DagRecTy DagRecTy::Shared; 88 89 LLVM_DUMP_METHOD void RecTy::dump() const { print(errs()); } 90 91 ListRecTy *RecTy::getListTy() { 92 if (!ListTy) 93 ListTy.reset(new ListRecTy(this)); 94 return ListTy.get(); 95 } 96 97 bool RecTy::typeIsConvertibleTo(const RecTy *RHS) const { 98 assert(RHS && "NULL pointer"); 99 return Kind == RHS->getRecTyKind(); 100 } 101 102 bool BitRecTy::typeIsConvertibleTo(const RecTy *RHS) const{ 103 if (RecTy::typeIsConvertibleTo(RHS) || RHS->getRecTyKind() == IntRecTyKind) 104 return true; 105 if (const BitsRecTy *BitsTy = dyn_cast<BitsRecTy>(RHS)) 106 return BitsTy->getNumBits() == 1; 107 return false; 108 } 109 110 BitsRecTy *BitsRecTy::get(unsigned Sz) { 111 static std::vector<std::unique_ptr<BitsRecTy>> Shared; 112 if (Sz >= Shared.size()) 113 Shared.resize(Sz + 1); 114 std::unique_ptr<BitsRecTy> &Ty = Shared[Sz]; 115 if (!Ty) 116 Ty.reset(new BitsRecTy(Sz)); 117 return Ty.get(); 118 } 119 120 std::string BitsRecTy::getAsString() const { 121 return "bits<" + utostr(Size) + ">"; 122 } 123 124 bool BitsRecTy::typeIsConvertibleTo(const RecTy *RHS) const { 125 if (RecTy::typeIsConvertibleTo(RHS)) //argument and the sender are same type 126 return cast<BitsRecTy>(RHS)->Size == Size; 127 RecTyKind kind = RHS->getRecTyKind(); 128 return (kind == BitRecTyKind && Size == 1) || (kind == IntRecTyKind); 129 } 130 131 bool IntRecTy::typeIsConvertibleTo(const RecTy *RHS) const { 132 RecTyKind kind = RHS->getRecTyKind(); 133 return kind==BitRecTyKind || kind==BitsRecTyKind || kind==IntRecTyKind; 134 } 135 136 std::string StringRecTy::getAsString() const { 137 return "string"; 138 } 139 140 std::string ListRecTy::getAsString() const { 141 return "list<" + Ty->getAsString() + ">"; 142 } 143 144 bool ListRecTy::typeIsConvertibleTo(const RecTy *RHS) const { 145 if (const auto *ListTy = dyn_cast<ListRecTy>(RHS)) 146 return Ty->typeIsConvertibleTo(ListTy->getElementType()); 147 return false; 148 } 149 150 std::string DagRecTy::getAsString() const { 151 return "dag"; 152 } 153 154 RecordRecTy *RecordRecTy::get(Record *R) { 155 return dyn_cast<RecordRecTy>(R->getDefInit()->getType()); 156 } 157 158 std::string RecordRecTy::getAsString() const { 159 return Rec->getName(); 160 } 161 162 bool RecordRecTy::typeIsConvertibleTo(const RecTy *RHS) const { 163 const RecordRecTy *RTy = dyn_cast<RecordRecTy>(RHS); 164 if (!RTy) 165 return false; 166 167 if (RTy->getRecord() == Rec || Rec->isSubClassOf(RTy->getRecord())) 168 return true; 169 170 for (const auto &SCPair : RTy->getRecord()->getSuperClasses()) 171 if (Rec->isSubClassOf(SCPair.first)) 172 return true; 173 174 return false; 175 } 176 177 /// resolveTypes - Find a common type that T1 and T2 convert to. 178 /// Return null if no such type exists. 179 /// 180 RecTy *llvm::resolveTypes(RecTy *T1, RecTy *T2) { 181 if (T1->typeIsConvertibleTo(T2)) 182 return T2; 183 if (T2->typeIsConvertibleTo(T1)) 184 return T1; 185 186 // If one is a Record type, check superclasses 187 if (RecordRecTy *RecTy1 = dyn_cast<RecordRecTy>(T1)) { 188 // See if T2 inherits from a type T1 also inherits from 189 for (const auto &SuperPair1 : RecTy1->getRecord()->getSuperClasses()) { 190 RecordRecTy *SuperRecTy1 = RecordRecTy::get(SuperPair1.first); 191 RecTy *NewType1 = resolveTypes(SuperRecTy1, T2); 192 if (NewType1) 193 return NewType1; 194 } 195 } 196 if (RecordRecTy *RecTy2 = dyn_cast<RecordRecTy>(T2)) { 197 // See if T1 inherits from a type T2 also inherits from 198 for (const auto &SuperPair2 : RecTy2->getRecord()->getSuperClasses()) { 199 RecordRecTy *SuperRecTy2 = RecordRecTy::get(SuperPair2.first); 200 RecTy *NewType2 = resolveTypes(T1, SuperRecTy2); 201 if (NewType2) 202 return NewType2; 203 } 204 } 205 return nullptr; 206 } 207 208 209 //===----------------------------------------------------------------------===// 210 // Initializer implementations 211 //===----------------------------------------------------------------------===// 212 213 void Init::anchor() { } 214 LLVM_DUMP_METHOD void Init::dump() const { return print(errs()); } 215 216 UnsetInit *UnsetInit::get() { 217 static UnsetInit TheInit; 218 return &TheInit; 219 } 220 221 Init *UnsetInit::convertInitializerTo(RecTy *Ty) const { 222 if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) { 223 SmallVector<Init *, 16> NewBits(BRT->getNumBits()); 224 225 for (unsigned i = 0; i != BRT->getNumBits(); ++i) 226 NewBits[i] = UnsetInit::get(); 227 228 return BitsInit::get(NewBits); 229 } 230 231 // All other types can just be returned. 232 return const_cast<UnsetInit *>(this); 233 } 234 235 BitInit *BitInit::get(bool V) { 236 static BitInit True(true); 237 static BitInit False(false); 238 239 return V ? &True : &False; 240 } 241 242 Init *BitInit::convertInitializerTo(RecTy *Ty) const { 243 if (isa<BitRecTy>(Ty)) 244 return const_cast<BitInit *>(this); 245 246 if (isa<IntRecTy>(Ty)) 247 return IntInit::get(getValue()); 248 249 if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) { 250 // Can only convert single bit. 251 if (BRT->getNumBits() == 1) 252 return BitsInit::get(const_cast<BitInit *>(this)); 253 } 254 255 return nullptr; 256 } 257 258 static void 259 ProfileBitsInit(FoldingSetNodeID &ID, ArrayRef<Init *> Range) { 260 ID.AddInteger(Range.size()); 261 262 for (Init *I : Range) 263 ID.AddPointer(I); 264 } 265 266 BitsInit *BitsInit::get(ArrayRef<Init *> Range) { 267 static FoldingSet<BitsInit> ThePool; 268 static std::vector<std::unique_ptr<BitsInit>> TheActualPool; 269 270 FoldingSetNodeID ID; 271 ProfileBitsInit(ID, Range); 272 273 void *IP = nullptr; 274 if (BitsInit *I = ThePool.FindNodeOrInsertPos(ID, IP)) 275 return I; 276 277 void *Mem = ::operator new (totalSizeToAlloc<Init *>(Range.size())); 278 BitsInit *I = new (Mem) BitsInit(Range.size()); 279 std::uninitialized_copy(Range.begin(), Range.end(), 280 I->getTrailingObjects<Init *>()); 281 ThePool.InsertNode(I, IP); 282 TheActualPool.push_back(std::unique_ptr<BitsInit>(I)); 283 return I; 284 } 285 286 void BitsInit::Profile(FoldingSetNodeID &ID) const { 287 ProfileBitsInit(ID, makeArrayRef(getTrailingObjects<Init *>(), NumBits)); 288 } 289 290 Init *BitsInit::convertInitializerTo(RecTy *Ty) const { 291 if (isa<BitRecTy>(Ty)) { 292 if (getNumBits() != 1) return nullptr; // Only accept if just one bit! 293 return getBit(0); 294 } 295 296 if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) { 297 // If the number of bits is right, return it. Otherwise we need to expand 298 // or truncate. 299 if (getNumBits() != BRT->getNumBits()) return nullptr; 300 return const_cast<BitsInit *>(this); 301 } 302 303 if (isa<IntRecTy>(Ty)) { 304 int64_t Result = 0; 305 for (unsigned i = 0, e = getNumBits(); i != e; ++i) 306 if (auto *Bit = dyn_cast<BitInit>(getBit(i))) 307 Result |= static_cast<int64_t>(Bit->getValue()) << i; 308 else 309 return nullptr; 310 return IntInit::get(Result); 311 } 312 313 return nullptr; 314 } 315 316 Init * 317 BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) const { 318 SmallVector<Init *, 16> NewBits(Bits.size()); 319 320 for (unsigned i = 0, e = Bits.size(); i != e; ++i) { 321 if (Bits[i] >= getNumBits()) 322 return nullptr; 323 NewBits[i] = getBit(Bits[i]); 324 } 325 return BitsInit::get(NewBits); 326 } 327 328 std::string BitsInit::getAsString() const { 329 std::string Result = "{ "; 330 for (unsigned i = 0, e = getNumBits(); i != e; ++i) { 331 if (i) Result += ", "; 332 if (Init *Bit = getBit(e-i-1)) 333 Result += Bit->getAsString(); 334 else 335 Result += "*"; 336 } 337 return Result + " }"; 338 } 339 340 // Fix bit initializer to preserve the behavior that bit reference from a unset 341 // bits initializer will resolve into VarBitInit to keep the field name and bit 342 // number used in targets with fixed insn length. 343 static Init *fixBitInit(const RecordVal *RV, Init *Before, Init *After) { 344 if (RV || !isa<UnsetInit>(After)) 345 return After; 346 return Before; 347 } 348 349 // resolveReferences - If there are any field references that refer to fields 350 // that have been filled in, we can propagate the values now. 351 // 352 Init *BitsInit::resolveReferences(Record &R, const RecordVal *RV) const { 353 bool Changed = false; 354 SmallVector<Init *, 16> NewBits(getNumBits()); 355 356 Init *CachedInit = nullptr; 357 Init *CachedBitVar = nullptr; 358 bool CachedBitVarChanged = false; 359 360 for (unsigned i = 0, e = getNumBits(); i != e; ++i) { 361 Init *CurBit = getBit(i); 362 Init *CurBitVar = CurBit->getBitVar(); 363 364 NewBits[i] = CurBit; 365 366 if (CurBitVar == CachedBitVar) { 367 if (CachedBitVarChanged) { 368 Init *Bit = CachedInit->getBit(CurBit->getBitNum()); 369 NewBits[i] = fixBitInit(RV, CurBit, Bit); 370 } 371 continue; 372 } 373 CachedBitVar = CurBitVar; 374 CachedBitVarChanged = false; 375 376 Init *B; 377 do { 378 B = CurBitVar; 379 CurBitVar = CurBitVar->resolveReferences(R, RV); 380 CachedBitVarChanged |= B != CurBitVar; 381 Changed |= B != CurBitVar; 382 } while (B != CurBitVar); 383 CachedInit = CurBitVar; 384 385 if (CachedBitVarChanged) { 386 Init *Bit = CurBitVar->getBit(CurBit->getBitNum()); 387 NewBits[i] = fixBitInit(RV, CurBit, Bit); 388 } 389 } 390 391 if (Changed) 392 return BitsInit::get(NewBits); 393 394 return const_cast<BitsInit *>(this); 395 } 396 397 IntInit *IntInit::get(int64_t V) { 398 static DenseMap<int64_t, std::unique_ptr<IntInit>> ThePool; 399 400 std::unique_ptr<IntInit> &I = ThePool[V]; 401 if (!I) I.reset(new IntInit(V)); 402 return I.get(); 403 } 404 405 std::string IntInit::getAsString() const { 406 return itostr(Value); 407 } 408 409 /// canFitInBitfield - Return true if the number of bits is large enough to hold 410 /// the integer value. 411 static bool canFitInBitfield(int64_t Value, unsigned NumBits) { 412 // For example, with NumBits == 4, we permit Values from [-7 .. 15]. 413 return (NumBits >= sizeof(Value) * 8) || 414 (Value >> NumBits == 0) || (Value >> (NumBits-1) == -1); 415 } 416 417 Init *IntInit::convertInitializerTo(RecTy *Ty) const { 418 if (isa<IntRecTy>(Ty)) 419 return const_cast<IntInit *>(this); 420 421 if (isa<BitRecTy>(Ty)) { 422 int64_t Val = getValue(); 423 if (Val != 0 && Val != 1) return nullptr; // Only accept 0 or 1 for a bit! 424 return BitInit::get(Val != 0); 425 } 426 427 if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) { 428 int64_t Value = getValue(); 429 // Make sure this bitfield is large enough to hold the integer value. 430 if (!canFitInBitfield(Value, BRT->getNumBits())) 431 return nullptr; 432 433 SmallVector<Init *, 16> NewBits(BRT->getNumBits()); 434 for (unsigned i = 0; i != BRT->getNumBits(); ++i) 435 NewBits[i] = BitInit::get(Value & (1LL << i)); 436 437 return BitsInit::get(NewBits); 438 } 439 440 return nullptr; 441 } 442 443 Init * 444 IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) const { 445 SmallVector<Init *, 16> NewBits(Bits.size()); 446 447 for (unsigned i = 0, e = Bits.size(); i != e; ++i) { 448 if (Bits[i] >= 64) 449 return nullptr; 450 451 NewBits[i] = BitInit::get(Value & (INT64_C(1) << Bits[i])); 452 } 453 return BitsInit::get(NewBits); 454 } 455 456 StringInit *StringInit::get(StringRef V) { 457 static StringMap<std::unique_ptr<StringInit>> ThePool; 458 459 std::unique_ptr<StringInit> &I = ThePool[V]; 460 if (!I) I.reset(new StringInit(V)); 461 return I.get(); 462 } 463 464 Init *StringInit::convertInitializerTo(RecTy *Ty) const { 465 if (isa<StringRecTy>(Ty)) 466 return const_cast<StringInit *>(this); 467 468 return nullptr; 469 } 470 471 static void ProfileListInit(FoldingSetNodeID &ID, 472 ArrayRef<Init *> Range, 473 RecTy *EltTy) { 474 ID.AddInteger(Range.size()); 475 ID.AddPointer(EltTy); 476 477 for (Init *I : Range) 478 ID.AddPointer(I); 479 } 480 481 ListInit *ListInit::get(ArrayRef<Init *> Range, RecTy *EltTy) { 482 static FoldingSet<ListInit> ThePool; 483 static std::vector<std::unique_ptr<ListInit>> TheActualPool; 484 485 FoldingSetNodeID ID; 486 ProfileListInit(ID, Range, EltTy); 487 488 void *IP = nullptr; 489 if (ListInit *I = ThePool.FindNodeOrInsertPos(ID, IP)) 490 return I; 491 492 void *Mem = ::operator new (totalSizeToAlloc<Init *>(Range.size())); 493 ListInit *I = new (Mem) ListInit(Range.size(), EltTy); 494 std::uninitialized_copy(Range.begin(), Range.end(), 495 I->getTrailingObjects<Init *>()); 496 ThePool.InsertNode(I, IP); 497 TheActualPool.push_back(std::unique_ptr<ListInit>(I)); 498 return I; 499 } 500 501 void ListInit::Profile(FoldingSetNodeID &ID) const { 502 RecTy *EltTy = cast<ListRecTy>(getType())->getElementType(); 503 504 ProfileListInit(ID, getValues(), EltTy); 505 } 506 507 Init *ListInit::convertInitializerTo(RecTy *Ty) const { 508 if (auto *LRT = dyn_cast<ListRecTy>(Ty)) { 509 std::vector<Init*> Elements; 510 511 // Verify that all of the elements of the list are subclasses of the 512 // appropriate class! 513 for (Init *I : getValues()) 514 if (Init *CI = I->convertInitializerTo(LRT->getElementType())) 515 Elements.push_back(CI); 516 else 517 return nullptr; 518 519 if (isa<ListRecTy>(getType())) 520 return ListInit::get(Elements, Ty); 521 } 522 523 return nullptr; 524 } 525 526 Init * 527 ListInit::convertInitListSlice(const std::vector<unsigned> &Elements) const { 528 std::vector<Init*> Vals; 529 for (unsigned i = 0, e = Elements.size(); i != e; ++i) { 530 if (Elements[i] >= size()) 531 return nullptr; 532 Vals.push_back(getElement(Elements[i])); 533 } 534 return ListInit::get(Vals, getType()); 535 } 536 537 Record *ListInit::getElementAsRecord(unsigned i) const { 538 assert(i < NumValues && "List element index out of range!"); 539 DefInit *DI = dyn_cast<DefInit>(getElement(i)); 540 if (!DI) 541 PrintFatalError("Expected record in list!"); 542 return DI->getDef(); 543 } 544 545 Init *ListInit::resolveReferences(Record &R, const RecordVal *RV) const { 546 std::vector<Init*> Resolved; 547 Resolved.reserve(size()); 548 bool Changed = false; 549 550 for (Init *CurElt : getValues()) { 551 Init *E; 552 553 do { 554 E = CurElt; 555 CurElt = CurElt->resolveReferences(R, RV); 556 Changed |= E != CurElt; 557 } while (E != CurElt); 558 Resolved.push_back(E); 559 } 560 561 if (Changed) 562 return ListInit::get(Resolved, getType()); 563 return const_cast<ListInit *>(this); 564 } 565 566 Init *ListInit::resolveListElementReference(Record &R, const RecordVal *IRV, 567 unsigned Elt) const { 568 if (Elt >= size()) 569 return nullptr; // Out of range reference. 570 Init *E = getElement(Elt); 571 // If the element is set to some value, or if we are resolving a reference 572 // to a specific variable and that variable is explicitly unset, then 573 // replace the VarListElementInit with it. 574 if (IRV || !isa<UnsetInit>(E)) 575 return E; 576 return nullptr; 577 } 578 579 std::string ListInit::getAsString() const { 580 std::string Result = "["; 581 for (unsigned i = 0, e = NumValues; i != e; ++i) { 582 if (i) Result += ", "; 583 Result += getElement(i)->getAsString(); 584 } 585 return Result + "]"; 586 } 587 588 Init *OpInit::resolveListElementReference(Record &R, const RecordVal *IRV, 589 unsigned Elt) const { 590 Init *Resolved = resolveReferences(R, IRV); 591 OpInit *OResolved = dyn_cast<OpInit>(Resolved); 592 if (OResolved) { 593 Resolved = OResolved->Fold(&R, nullptr); 594 } 595 596 if (Resolved != this) { 597 TypedInit *Typed = cast<TypedInit>(Resolved); 598 if (Init *New = Typed->resolveListElementReference(R, IRV, Elt)) 599 return New; 600 return VarListElementInit::get(Typed, Elt); 601 } 602 603 return nullptr; 604 } 605 606 Init *OpInit::getBit(unsigned Bit) const { 607 if (getType() == BitRecTy::get()) 608 return const_cast<OpInit*>(this); 609 return VarBitInit::get(const_cast<OpInit*>(this), Bit); 610 } 611 612 static void 613 ProfileUnOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *Op, RecTy *Type) { 614 ID.AddInteger(Opcode); 615 ID.AddPointer(Op); 616 ID.AddPointer(Type); 617 } 618 619 UnOpInit *UnOpInit::get(UnaryOp Opc, Init *LHS, RecTy *Type) { 620 static FoldingSet<UnOpInit> ThePool; 621 static std::vector<std::unique_ptr<UnOpInit>> TheActualPool; 622 623 FoldingSetNodeID ID; 624 ProfileUnOpInit(ID, Opc, LHS, Type); 625 626 void *IP = nullptr; 627 if (UnOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP)) 628 return I; 629 630 UnOpInit *I = new UnOpInit(Opc, LHS, Type); 631 ThePool.InsertNode(I, IP); 632 TheActualPool.push_back(std::unique_ptr<UnOpInit>(I)); 633 return I; 634 } 635 636 void UnOpInit::Profile(FoldingSetNodeID &ID) const { 637 ProfileUnOpInit(ID, getOpcode(), getOperand(), getType()); 638 } 639 640 Init *UnOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const { 641 switch (getOpcode()) { 642 case CAST: { 643 if (isa<StringRecTy>(getType())) { 644 if (StringInit *LHSs = dyn_cast<StringInit>(LHS)) 645 return LHSs; 646 647 if (DefInit *LHSd = dyn_cast<DefInit>(LHS)) 648 return StringInit::get(LHSd->getAsString()); 649 650 if (IntInit *LHSi = dyn_cast<IntInit>(LHS)) 651 return StringInit::get(LHSi->getAsString()); 652 } else { 653 if (StringInit *LHSs = dyn_cast<StringInit>(LHS)) { 654 std::string Name = LHSs->getValue(); 655 656 // From TGParser::ParseIDValue 657 if (CurRec) { 658 if (const RecordVal *RV = CurRec->getValue(Name)) { 659 if (RV->getType() != getType()) 660 PrintFatalError("type mismatch in cast"); 661 return VarInit::get(Name, RV->getType()); 662 } 663 664 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, 665 ":"); 666 667 if (CurRec->isTemplateArg(TemplateArgName)) { 668 const RecordVal *RV = CurRec->getValue(TemplateArgName); 669 assert(RV && "Template arg doesn't exist??"); 670 671 if (RV->getType() != getType()) 672 PrintFatalError("type mismatch in cast"); 673 674 return VarInit::get(TemplateArgName, RV->getType()); 675 } 676 } 677 678 if (CurMultiClass) { 679 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name, 680 "::"); 681 682 if (CurMultiClass->Rec.isTemplateArg(MCName)) { 683 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName); 684 assert(RV && "Template arg doesn't exist??"); 685 686 if (RV->getType() != getType()) 687 PrintFatalError("type mismatch in cast"); 688 689 return VarInit::get(MCName, RV->getType()); 690 } 691 } 692 assert(CurRec && "NULL pointer"); 693 if (Record *D = (CurRec->getRecords()).getDef(Name)) 694 return DefInit::get(D); 695 696 PrintFatalError(CurRec->getLoc(), 697 "Undefined reference:'" + Name + "'\n"); 698 } 699 700 if (isa<IntRecTy>(getType())) { 701 if (BitsInit *BI = dyn_cast<BitsInit>(LHS)) { 702 if (Init *NewInit = BI->convertInitializerTo(IntRecTy::get())) 703 return NewInit; 704 break; 705 } 706 } 707 } 708 break; 709 } 710 case HEAD: { 711 if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) { 712 assert(!LHSl->empty() && "Empty list in head"); 713 return LHSl->getElement(0); 714 } 715 break; 716 } 717 case TAIL: { 718 if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) { 719 assert(!LHSl->empty() && "Empty list in tail"); 720 // Note the +1. We can't just pass the result of getValues() 721 // directly. 722 return ListInit::get(LHSl->getValues().slice(1), LHSl->getType()); 723 } 724 break; 725 } 726 case EMPTY: { 727 if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) 728 return IntInit::get(LHSl->empty()); 729 if (StringInit *LHSs = dyn_cast<StringInit>(LHS)) 730 return IntInit::get(LHSs->getValue().empty()); 731 732 break; 733 } 734 } 735 return const_cast<UnOpInit *>(this); 736 } 737 738 Init *UnOpInit::resolveReferences(Record &R, const RecordVal *RV) const { 739 Init *lhs = LHS->resolveReferences(R, RV); 740 741 if (LHS != lhs) 742 return (UnOpInit::get(getOpcode(), lhs, getType()))->Fold(&R, nullptr); 743 return Fold(&R, nullptr); 744 } 745 746 std::string UnOpInit::getAsString() const { 747 std::string Result; 748 switch (getOpcode()) { 749 case CAST: Result = "!cast<" + getType()->getAsString() + ">"; break; 750 case HEAD: Result = "!head"; break; 751 case TAIL: Result = "!tail"; break; 752 case EMPTY: Result = "!empty"; break; 753 } 754 return Result + "(" + LHS->getAsString() + ")"; 755 } 756 757 static void 758 ProfileBinOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *LHS, Init *RHS, 759 RecTy *Type) { 760 ID.AddInteger(Opcode); 761 ID.AddPointer(LHS); 762 ID.AddPointer(RHS); 763 ID.AddPointer(Type); 764 } 765 766 BinOpInit *BinOpInit::get(BinaryOp Opc, Init *LHS, 767 Init *RHS, RecTy *Type) { 768 static FoldingSet<BinOpInit> ThePool; 769 static std::vector<std::unique_ptr<BinOpInit>> TheActualPool; 770 771 FoldingSetNodeID ID; 772 ProfileBinOpInit(ID, Opc, LHS, RHS, Type); 773 774 void *IP = nullptr; 775 if (BinOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP)) 776 return I; 777 778 BinOpInit *I = new BinOpInit(Opc, LHS, RHS, Type); 779 ThePool.InsertNode(I, IP); 780 TheActualPool.push_back(std::unique_ptr<BinOpInit>(I)); 781 return I; 782 } 783 784 void BinOpInit::Profile(FoldingSetNodeID &ID) const { 785 ProfileBinOpInit(ID, getOpcode(), getLHS(), getRHS(), getType()); 786 } 787 788 Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const { 789 switch (getOpcode()) { 790 case CONCAT: { 791 DagInit *LHSs = dyn_cast<DagInit>(LHS); 792 DagInit *RHSs = dyn_cast<DagInit>(RHS); 793 if (LHSs && RHSs) { 794 DefInit *LOp = dyn_cast<DefInit>(LHSs->getOperator()); 795 DefInit *ROp = dyn_cast<DefInit>(RHSs->getOperator()); 796 if (!LOp || !ROp || LOp->getDef() != ROp->getDef()) 797 PrintFatalError("Concated Dag operators do not match!"); 798 std::vector<Init*> Args; 799 std::vector<std::string> ArgNames; 800 for (unsigned i = 0, e = LHSs->getNumArgs(); i != e; ++i) { 801 Args.push_back(LHSs->getArg(i)); 802 ArgNames.push_back(LHSs->getArgName(i)); 803 } 804 for (unsigned i = 0, e = RHSs->getNumArgs(); i != e; ++i) { 805 Args.push_back(RHSs->getArg(i)); 806 ArgNames.push_back(RHSs->getArgName(i)); 807 } 808 return DagInit::get(LHSs->getOperator(), "", Args, ArgNames); 809 } 810 break; 811 } 812 case LISTCONCAT: { 813 ListInit *LHSs = dyn_cast<ListInit>(LHS); 814 ListInit *RHSs = dyn_cast<ListInit>(RHS); 815 if (LHSs && RHSs) { 816 std::vector<Init *> Args; 817 Args.insert(Args.end(), LHSs->begin(), LHSs->end()); 818 Args.insert(Args.end(), RHSs->begin(), RHSs->end()); 819 return ListInit::get( 820 Args, cast<ListRecTy>(LHSs->getType())->getElementType()); 821 } 822 break; 823 } 824 case STRCONCAT: { 825 StringInit *LHSs = dyn_cast<StringInit>(LHS); 826 StringInit *RHSs = dyn_cast<StringInit>(RHS); 827 if (LHSs && RHSs) 828 return StringInit::get(LHSs->getValue() + RHSs->getValue()); 829 break; 830 } 831 case EQ: { 832 // try to fold eq comparison for 'bit' and 'int', otherwise fallback 833 // to string objects. 834 IntInit *L = 835 dyn_cast_or_null<IntInit>(LHS->convertInitializerTo(IntRecTy::get())); 836 IntInit *R = 837 dyn_cast_or_null<IntInit>(RHS->convertInitializerTo(IntRecTy::get())); 838 839 if (L && R) 840 return IntInit::get(L->getValue() == R->getValue()); 841 842 StringInit *LHSs = dyn_cast<StringInit>(LHS); 843 StringInit *RHSs = dyn_cast<StringInit>(RHS); 844 845 // Make sure we've resolved 846 if (LHSs && RHSs) 847 return IntInit::get(LHSs->getValue() == RHSs->getValue()); 848 849 break; 850 } 851 case ADD: 852 case AND: 853 case SHL: 854 case SRA: 855 case SRL: { 856 IntInit *LHSi = 857 dyn_cast_or_null<IntInit>(LHS->convertInitializerTo(IntRecTy::get())); 858 IntInit *RHSi = 859 dyn_cast_or_null<IntInit>(RHS->convertInitializerTo(IntRecTy::get())); 860 if (LHSi && RHSi) { 861 int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue(); 862 int64_t Result; 863 switch (getOpcode()) { 864 default: llvm_unreachable("Bad opcode!"); 865 case ADD: Result = LHSv + RHSv; break; 866 case AND: Result = LHSv & RHSv; break; 867 case SHL: Result = LHSv << RHSv; break; 868 case SRA: Result = LHSv >> RHSv; break; 869 case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break; 870 } 871 return IntInit::get(Result); 872 } 873 break; 874 } 875 } 876 return const_cast<BinOpInit *>(this); 877 } 878 879 Init *BinOpInit::resolveReferences(Record &R, const RecordVal *RV) const { 880 Init *lhs = LHS->resolveReferences(R, RV); 881 Init *rhs = RHS->resolveReferences(R, RV); 882 883 if (LHS != lhs || RHS != rhs) 884 return (BinOpInit::get(getOpcode(), lhs, rhs, getType()))->Fold(&R,nullptr); 885 return Fold(&R, nullptr); 886 } 887 888 std::string BinOpInit::getAsString() const { 889 std::string Result; 890 switch (getOpcode()) { 891 case CONCAT: Result = "!con"; break; 892 case ADD: Result = "!add"; break; 893 case AND: Result = "!and"; break; 894 case SHL: Result = "!shl"; break; 895 case SRA: Result = "!sra"; break; 896 case SRL: Result = "!srl"; break; 897 case EQ: Result = "!eq"; break; 898 case LISTCONCAT: Result = "!listconcat"; break; 899 case STRCONCAT: Result = "!strconcat"; break; 900 } 901 return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")"; 902 } 903 904 static void 905 ProfileTernOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *LHS, Init *MHS, 906 Init *RHS, RecTy *Type) { 907 ID.AddInteger(Opcode); 908 ID.AddPointer(LHS); 909 ID.AddPointer(MHS); 910 ID.AddPointer(RHS); 911 ID.AddPointer(Type); 912 } 913 914 TernOpInit *TernOpInit::get(TernaryOp Opc, Init *LHS, Init *MHS, Init *RHS, 915 RecTy *Type) { 916 static FoldingSet<TernOpInit> ThePool; 917 static std::vector<std::unique_ptr<TernOpInit>> TheActualPool; 918 919 FoldingSetNodeID ID; 920 ProfileTernOpInit(ID, Opc, LHS, MHS, RHS, Type); 921 922 void *IP = nullptr; 923 if (TernOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP)) 924 return I; 925 926 TernOpInit *I = new TernOpInit(Opc, LHS, MHS, RHS, Type); 927 ThePool.InsertNode(I, IP); 928 TheActualPool.push_back(std::unique_ptr<TernOpInit>(I)); 929 return I; 930 } 931 932 void TernOpInit::Profile(FoldingSetNodeID &ID) const { 933 ProfileTernOpInit(ID, getOpcode(), getLHS(), getMHS(), getRHS(), getType()); 934 } 935 936 static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type, 937 Record *CurRec, MultiClass *CurMultiClass); 938 939 static Init *EvaluateOperation(OpInit *RHSo, Init *LHS, Init *Arg, 940 RecTy *Type, Record *CurRec, 941 MultiClass *CurMultiClass) { 942 // If this is a dag, recurse 943 if (auto *TArg = dyn_cast<TypedInit>(Arg)) 944 if (isa<DagRecTy>(TArg->getType())) 945 return ForeachHelper(LHS, Arg, RHSo, Type, CurRec, CurMultiClass); 946 947 std::vector<Init *> NewOperands; 948 for (unsigned i = 0; i < RHSo->getNumOperands(); ++i) { 949 if (auto *RHSoo = dyn_cast<OpInit>(RHSo->getOperand(i))) { 950 if (Init *Result = EvaluateOperation(RHSoo, LHS, Arg, 951 Type, CurRec, CurMultiClass)) 952 NewOperands.push_back(Result); 953 else 954 NewOperands.push_back(Arg); 955 } else if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) { 956 NewOperands.push_back(Arg); 957 } else { 958 NewOperands.push_back(RHSo->getOperand(i)); 959 } 960 } 961 962 // Now run the operator and use its result as the new leaf 963 const OpInit *NewOp = RHSo->clone(NewOperands); 964 Init *NewVal = NewOp->Fold(CurRec, CurMultiClass); 965 return (NewVal != NewOp) ? NewVal : nullptr; 966 } 967 968 static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type, 969 Record *CurRec, MultiClass *CurMultiClass) { 970 971 OpInit *RHSo = dyn_cast<OpInit>(RHS); 972 973 if (!RHSo) 974 PrintFatalError(CurRec->getLoc(), "!foreach requires an operator\n"); 975 976 TypedInit *LHSt = dyn_cast<TypedInit>(LHS); 977 978 if (!LHSt) 979 PrintFatalError(CurRec->getLoc(), "!foreach requires typed variable\n"); 980 981 DagInit *MHSd = dyn_cast<DagInit>(MHS); 982 if (MHSd && isa<DagRecTy>(Type)) { 983 Init *Val = MHSd->getOperator(); 984 if (Init *Result = EvaluateOperation(RHSo, LHS, Val, 985 Type, CurRec, CurMultiClass)) 986 Val = Result; 987 988 std::vector<std::pair<Init *, std::string> > args; 989 for (unsigned int i = 0; i < MHSd->getNumArgs(); ++i) { 990 Init *Arg = MHSd->getArg(i); 991 std::string ArgName = MHSd->getArgName(i); 992 993 // Process args 994 if (Init *Result = EvaluateOperation(RHSo, LHS, Arg, Type, 995 CurRec, CurMultiClass)) 996 Arg = Result; 997 998 // TODO: Process arg names 999 args.push_back(std::make_pair(Arg, ArgName)); 1000 } 1001 1002 return DagInit::get(Val, "", args); 1003 } 1004 1005 ListInit *MHSl = dyn_cast<ListInit>(MHS); 1006 if (MHSl && isa<ListRecTy>(Type)) { 1007 std::vector<Init *> NewOperands; 1008 std::vector<Init *> NewList(MHSl->begin(), MHSl->end()); 1009 1010 for (Init *&Item : NewList) { 1011 NewOperands.clear(); 1012 for(unsigned i = 0; i < RHSo->getNumOperands(); ++i) { 1013 // First, replace the foreach variable with the list item 1014 if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) 1015 NewOperands.push_back(Item); 1016 else 1017 NewOperands.push_back(RHSo->getOperand(i)); 1018 } 1019 1020 // Now run the operator and use its result as the new list item 1021 const OpInit *NewOp = RHSo->clone(NewOperands); 1022 Init *NewItem = NewOp->Fold(CurRec, CurMultiClass); 1023 if (NewItem != NewOp) 1024 Item = NewItem; 1025 } 1026 return ListInit::get(NewList, MHSl->getType()); 1027 } 1028 return nullptr; 1029 } 1030 1031 Init *TernOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const { 1032 switch (getOpcode()) { 1033 case SUBST: { 1034 DefInit *LHSd = dyn_cast<DefInit>(LHS); 1035 VarInit *LHSv = dyn_cast<VarInit>(LHS); 1036 StringInit *LHSs = dyn_cast<StringInit>(LHS); 1037 1038 DefInit *MHSd = dyn_cast<DefInit>(MHS); 1039 VarInit *MHSv = dyn_cast<VarInit>(MHS); 1040 StringInit *MHSs = dyn_cast<StringInit>(MHS); 1041 1042 DefInit *RHSd = dyn_cast<DefInit>(RHS); 1043 VarInit *RHSv = dyn_cast<VarInit>(RHS); 1044 StringInit *RHSs = dyn_cast<StringInit>(RHS); 1045 1046 if (LHSd && MHSd && RHSd) { 1047 Record *Val = RHSd->getDef(); 1048 if (LHSd->getAsString() == RHSd->getAsString()) 1049 Val = MHSd->getDef(); 1050 return DefInit::get(Val); 1051 } 1052 if (LHSv && MHSv && RHSv) { 1053 std::string Val = RHSv->getName(); 1054 if (LHSv->getAsString() == RHSv->getAsString()) 1055 Val = MHSv->getName(); 1056 return VarInit::get(Val, getType()); 1057 } 1058 if (LHSs && MHSs && RHSs) { 1059 std::string Val = RHSs->getValue(); 1060 1061 std::string::size_type found; 1062 std::string::size_type idx = 0; 1063 while (true) { 1064 found = Val.find(LHSs->getValue(), idx); 1065 if (found == std::string::npos) 1066 break; 1067 Val.replace(found, LHSs->getValue().size(), MHSs->getValue()); 1068 idx = found + MHSs->getValue().size(); 1069 } 1070 1071 return StringInit::get(Val); 1072 } 1073 break; 1074 } 1075 1076 case FOREACH: { 1077 if (Init *Result = ForeachHelper(LHS, MHS, RHS, getType(), 1078 CurRec, CurMultiClass)) 1079 return Result; 1080 break; 1081 } 1082 1083 case IF: { 1084 IntInit *LHSi = dyn_cast<IntInit>(LHS); 1085 if (Init *I = LHS->convertInitializerTo(IntRecTy::get())) 1086 LHSi = dyn_cast<IntInit>(I); 1087 if (LHSi) { 1088 if (LHSi->getValue()) 1089 return MHS; 1090 return RHS; 1091 } 1092 break; 1093 } 1094 } 1095 1096 return const_cast<TernOpInit *>(this); 1097 } 1098 1099 Init *TernOpInit::resolveReferences(Record &R, 1100 const RecordVal *RV) const { 1101 Init *lhs = LHS->resolveReferences(R, RV); 1102 1103 if (getOpcode() == IF && lhs != LHS) { 1104 IntInit *Value = dyn_cast<IntInit>(lhs); 1105 if (Init *I = lhs->convertInitializerTo(IntRecTy::get())) 1106 Value = dyn_cast<IntInit>(I); 1107 if (Value) { 1108 // Short-circuit 1109 if (Value->getValue()) { 1110 Init *mhs = MHS->resolveReferences(R, RV); 1111 return (TernOpInit::get(getOpcode(), lhs, mhs, 1112 RHS, getType()))->Fold(&R, nullptr); 1113 } 1114 Init *rhs = RHS->resolveReferences(R, RV); 1115 return (TernOpInit::get(getOpcode(), lhs, MHS, 1116 rhs, getType()))->Fold(&R, nullptr); 1117 } 1118 } 1119 1120 Init *mhs = MHS->resolveReferences(R, RV); 1121 Init *rhs = RHS->resolveReferences(R, RV); 1122 1123 if (LHS != lhs || MHS != mhs || RHS != rhs) 1124 return (TernOpInit::get(getOpcode(), lhs, mhs, rhs, 1125 getType()))->Fold(&R, nullptr); 1126 return Fold(&R, nullptr); 1127 } 1128 1129 std::string TernOpInit::getAsString() const { 1130 std::string Result; 1131 switch (getOpcode()) { 1132 case SUBST: Result = "!subst"; break; 1133 case FOREACH: Result = "!foreach"; break; 1134 case IF: Result = "!if"; break; 1135 } 1136 return Result + "(" + LHS->getAsString() + ", " + MHS->getAsString() + ", " + 1137 RHS->getAsString() + ")"; 1138 } 1139 1140 RecTy *TypedInit::getFieldType(const std::string &FieldName) const { 1141 if (RecordRecTy *RecordType = dyn_cast<RecordRecTy>(getType())) 1142 if (RecordVal *Field = RecordType->getRecord()->getValue(FieldName)) 1143 return Field->getType(); 1144 return nullptr; 1145 } 1146 1147 Init * 1148 TypedInit::convertInitializerTo(RecTy *Ty) const { 1149 if (isa<IntRecTy>(Ty)) { 1150 if (getType()->typeIsConvertibleTo(Ty)) 1151 return const_cast<TypedInit *>(this); 1152 return nullptr; 1153 } 1154 1155 if (isa<StringRecTy>(Ty)) { 1156 if (isa<StringRecTy>(getType())) 1157 return const_cast<TypedInit *>(this); 1158 return nullptr; 1159 } 1160 1161 if (isa<BitRecTy>(Ty)) { 1162 // Accept variable if it is already of bit type! 1163 if (isa<BitRecTy>(getType())) 1164 return const_cast<TypedInit *>(this); 1165 if (auto *BitsTy = dyn_cast<BitsRecTy>(getType())) { 1166 // Accept only bits<1> expression. 1167 if (BitsTy->getNumBits() == 1) 1168 return const_cast<TypedInit *>(this); 1169 return nullptr; 1170 } 1171 // Ternary !if can be converted to bit, but only if both sides are 1172 // convertible to a bit. 1173 if (const auto *TOI = dyn_cast<TernOpInit>(this)) { 1174 if (TOI->getOpcode() == TernOpInit::TernaryOp::IF && 1175 TOI->getMHS()->convertInitializerTo(BitRecTy::get()) && 1176 TOI->getRHS()->convertInitializerTo(BitRecTy::get())) 1177 return const_cast<TypedInit *>(this); 1178 return nullptr; 1179 } 1180 return nullptr; 1181 } 1182 1183 if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) { 1184 if (BRT->getNumBits() == 1 && isa<BitRecTy>(getType())) 1185 return BitsInit::get(const_cast<TypedInit *>(this)); 1186 1187 if (getType()->typeIsConvertibleTo(BRT)) { 1188 SmallVector<Init *, 16> NewBits(BRT->getNumBits()); 1189 1190 for (unsigned i = 0; i != BRT->getNumBits(); ++i) 1191 NewBits[i] = VarBitInit::get(const_cast<TypedInit *>(this), i); 1192 return BitsInit::get(NewBits); 1193 } 1194 1195 return nullptr; 1196 } 1197 1198 if (auto *DLRT = dyn_cast<ListRecTy>(Ty)) { 1199 if (auto *SLRT = dyn_cast<ListRecTy>(getType())) 1200 if (SLRT->getElementType()->typeIsConvertibleTo(DLRT->getElementType())) 1201 return const_cast<TypedInit *>(this); 1202 return nullptr; 1203 } 1204 1205 if (auto *DRT = dyn_cast<DagRecTy>(Ty)) { 1206 if (getType()->typeIsConvertibleTo(DRT)) 1207 return const_cast<TypedInit *>(this); 1208 return nullptr; 1209 } 1210 1211 if (auto *SRRT = dyn_cast<RecordRecTy>(Ty)) { 1212 // Ensure that this is compatible with Rec. 1213 if (RecordRecTy *DRRT = dyn_cast<RecordRecTy>(getType())) 1214 if (DRRT->getRecord()->isSubClassOf(SRRT->getRecord()) || 1215 DRRT->getRecord() == SRRT->getRecord()) 1216 return const_cast<TypedInit *>(this); 1217 return nullptr; 1218 } 1219 1220 return nullptr; 1221 } 1222 1223 Init * 1224 TypedInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) const { 1225 BitsRecTy *T = dyn_cast<BitsRecTy>(getType()); 1226 if (!T) return nullptr; // Cannot subscript a non-bits variable. 1227 unsigned NumBits = T->getNumBits(); 1228 1229 SmallVector<Init *, 16> NewBits(Bits.size()); 1230 for (unsigned i = 0, e = Bits.size(); i != e; ++i) { 1231 if (Bits[i] >= NumBits) 1232 return nullptr; 1233 1234 NewBits[i] = VarBitInit::get(const_cast<TypedInit *>(this), Bits[i]); 1235 } 1236 return BitsInit::get(NewBits); 1237 } 1238 1239 Init * 1240 TypedInit::convertInitListSlice(const std::vector<unsigned> &Elements) const { 1241 ListRecTy *T = dyn_cast<ListRecTy>(getType()); 1242 if (!T) return nullptr; // Cannot subscript a non-list variable. 1243 1244 if (Elements.size() == 1) 1245 return VarListElementInit::get(const_cast<TypedInit *>(this), Elements[0]); 1246 1247 std::vector<Init*> ListInits; 1248 ListInits.reserve(Elements.size()); 1249 for (unsigned i = 0, e = Elements.size(); i != e; ++i) 1250 ListInits.push_back(VarListElementInit::get(const_cast<TypedInit *>(this), 1251 Elements[i])); 1252 return ListInit::get(ListInits, T); 1253 } 1254 1255 1256 VarInit *VarInit::get(const std::string &VN, RecTy *T) { 1257 Init *Value = StringInit::get(VN); 1258 return VarInit::get(Value, T); 1259 } 1260 1261 VarInit *VarInit::get(Init *VN, RecTy *T) { 1262 typedef std::pair<RecTy *, Init *> Key; 1263 static DenseMap<Key, std::unique_ptr<VarInit>> ThePool; 1264 1265 Key TheKey(std::make_pair(T, VN)); 1266 1267 std::unique_ptr<VarInit> &I = ThePool[TheKey]; 1268 if (!I) I.reset(new VarInit(VN, T)); 1269 return I.get(); 1270 } 1271 1272 const std::string &VarInit::getName() const { 1273 StringInit *NameString = cast<StringInit>(getNameInit()); 1274 return NameString->getValue(); 1275 } 1276 1277 Init *VarInit::getBit(unsigned Bit) const { 1278 if (getType() == BitRecTy::get()) 1279 return const_cast<VarInit*>(this); 1280 return VarBitInit::get(const_cast<VarInit*>(this), Bit); 1281 } 1282 1283 Init *VarInit::resolveListElementReference(Record &R, 1284 const RecordVal *IRV, 1285 unsigned Elt) const { 1286 if (R.isTemplateArg(getNameInit())) return nullptr; 1287 if (IRV && IRV->getNameInit() != getNameInit()) return nullptr; 1288 1289 RecordVal *RV = R.getValue(getNameInit()); 1290 assert(RV && "Reference to a non-existent variable?"); 1291 ListInit *LI = dyn_cast<ListInit>(RV->getValue()); 1292 if (!LI) 1293 return VarListElementInit::get(cast<TypedInit>(RV->getValue()), Elt); 1294 1295 if (Elt >= LI->size()) 1296 return nullptr; // Out of range reference. 1297 Init *E = LI->getElement(Elt); 1298 // If the element is set to some value, or if we are resolving a reference 1299 // to a specific variable and that variable is explicitly unset, then 1300 // replace the VarListElementInit with it. 1301 if (IRV || !isa<UnsetInit>(E)) 1302 return E; 1303 return nullptr; 1304 } 1305 1306 1307 RecTy *VarInit::getFieldType(const std::string &FieldName) const { 1308 if (RecordRecTy *RTy = dyn_cast<RecordRecTy>(getType())) 1309 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName)) 1310 return RV->getType(); 1311 return nullptr; 1312 } 1313 1314 Init *VarInit::getFieldInit(Record &R, const RecordVal *RV, 1315 const std::string &FieldName) const { 1316 if (isa<RecordRecTy>(getType())) 1317 if (const RecordVal *Val = R.getValue(VarName)) { 1318 if (RV != Val && (RV || isa<UnsetInit>(Val->getValue()))) 1319 return nullptr; 1320 Init *TheInit = Val->getValue(); 1321 assert(TheInit != this && "Infinite loop detected!"); 1322 if (Init *I = TheInit->getFieldInit(R, RV, FieldName)) 1323 return I; 1324 return nullptr; 1325 } 1326 return nullptr; 1327 } 1328 1329 /// resolveReferences - This method is used by classes that refer to other 1330 /// variables which may not be defined at the time the expression is formed. 1331 /// If a value is set for the variable later, this method will be called on 1332 /// users of the value to allow the value to propagate out. 1333 /// 1334 Init *VarInit::resolveReferences(Record &R, const RecordVal *RV) const { 1335 if (RecordVal *Val = R.getValue(VarName)) 1336 if (RV == Val || (!RV && !isa<UnsetInit>(Val->getValue()))) 1337 return Val->getValue(); 1338 return const_cast<VarInit *>(this); 1339 } 1340 1341 VarBitInit *VarBitInit::get(TypedInit *T, unsigned B) { 1342 typedef std::pair<TypedInit *, unsigned> Key; 1343 static DenseMap<Key, std::unique_ptr<VarBitInit>> ThePool; 1344 1345 Key TheKey(std::make_pair(T, B)); 1346 1347 std::unique_ptr<VarBitInit> &I = ThePool[TheKey]; 1348 if (!I) I.reset(new VarBitInit(T, B)); 1349 return I.get(); 1350 } 1351 1352 Init *VarBitInit::convertInitializerTo(RecTy *Ty) const { 1353 if (isa<BitRecTy>(Ty)) 1354 return const_cast<VarBitInit *>(this); 1355 1356 return nullptr; 1357 } 1358 1359 std::string VarBitInit::getAsString() const { 1360 return TI->getAsString() + "{" + utostr(Bit) + "}"; 1361 } 1362 1363 Init *VarBitInit::resolveReferences(Record &R, const RecordVal *RV) const { 1364 Init *I = TI->resolveReferences(R, RV); 1365 if (TI != I) 1366 return I->getBit(getBitNum()); 1367 1368 return const_cast<VarBitInit*>(this); 1369 } 1370 1371 VarListElementInit *VarListElementInit::get(TypedInit *T, 1372 unsigned E) { 1373 typedef std::pair<TypedInit *, unsigned> Key; 1374 static DenseMap<Key, std::unique_ptr<VarListElementInit>> ThePool; 1375 1376 Key TheKey(std::make_pair(T, E)); 1377 1378 std::unique_ptr<VarListElementInit> &I = ThePool[TheKey]; 1379 if (!I) I.reset(new VarListElementInit(T, E)); 1380 return I.get(); 1381 } 1382 1383 std::string VarListElementInit::getAsString() const { 1384 return TI->getAsString() + "[" + utostr(Element) + "]"; 1385 } 1386 1387 Init * 1388 VarListElementInit::resolveReferences(Record &R, const RecordVal *RV) const { 1389 if (Init *I = getVariable()->resolveListElementReference(R, RV, 1390 getElementNum())) 1391 return I; 1392 return const_cast<VarListElementInit *>(this); 1393 } 1394 1395 Init *VarListElementInit::getBit(unsigned Bit) const { 1396 if (getType() == BitRecTy::get()) 1397 return const_cast<VarListElementInit*>(this); 1398 return VarBitInit::get(const_cast<VarListElementInit*>(this), Bit); 1399 } 1400 1401 Init *VarListElementInit:: resolveListElementReference(Record &R, 1402 const RecordVal *RV, 1403 unsigned Elt) const { 1404 if (Init *Result = TI->resolveListElementReference(R, RV, Element)) { 1405 if (TypedInit *TInit = dyn_cast<TypedInit>(Result)) { 1406 if (Init *Result2 = TInit->resolveListElementReference(R, RV, Elt)) 1407 return Result2; 1408 return VarListElementInit::get(TInit, Elt); 1409 } 1410 return Result; 1411 } 1412 1413 return nullptr; 1414 } 1415 1416 DefInit *DefInit::get(Record *R) { 1417 return R->getDefInit(); 1418 } 1419 1420 Init *DefInit::convertInitializerTo(RecTy *Ty) const { 1421 if (auto *RRT = dyn_cast<RecordRecTy>(Ty)) 1422 if (getDef()->isSubClassOf(RRT->getRecord())) 1423 return const_cast<DefInit *>(this); 1424 return nullptr; 1425 } 1426 1427 RecTy *DefInit::getFieldType(const std::string &FieldName) const { 1428 if (const RecordVal *RV = Def->getValue(FieldName)) 1429 return RV->getType(); 1430 return nullptr; 1431 } 1432 1433 Init *DefInit::getFieldInit(Record &R, const RecordVal *RV, 1434 const std::string &FieldName) const { 1435 return Def->getValue(FieldName)->getValue(); 1436 } 1437 1438 1439 std::string DefInit::getAsString() const { 1440 return Def->getName(); 1441 } 1442 1443 FieldInit *FieldInit::get(Init *R, const std::string &FN) { 1444 typedef std::pair<Init *, TableGenStringKey> Key; 1445 static DenseMap<Key, std::unique_ptr<FieldInit>> ThePool; 1446 1447 Key TheKey(std::make_pair(R, FN)); 1448 1449 std::unique_ptr<FieldInit> &I = ThePool[TheKey]; 1450 if (!I) I.reset(new FieldInit(R, FN)); 1451 return I.get(); 1452 } 1453 1454 Init *FieldInit::getBit(unsigned Bit) const { 1455 if (getType() == BitRecTy::get()) 1456 return const_cast<FieldInit*>(this); 1457 return VarBitInit::get(const_cast<FieldInit*>(this), Bit); 1458 } 1459 1460 Init *FieldInit::resolveListElementReference(Record &R, const RecordVal *RV, 1461 unsigned Elt) const { 1462 if (Init *ListVal = Rec->getFieldInit(R, RV, FieldName)) 1463 if (ListInit *LI = dyn_cast<ListInit>(ListVal)) { 1464 if (Elt >= LI->size()) return nullptr; 1465 Init *E = LI->getElement(Elt); 1466 1467 // If the element is set to some value, or if we are resolving a 1468 // reference to a specific variable and that variable is explicitly 1469 // unset, then replace the VarListElementInit with it. 1470 if (RV || !isa<UnsetInit>(E)) 1471 return E; 1472 } 1473 return nullptr; 1474 } 1475 1476 Init *FieldInit::resolveReferences(Record &R, const RecordVal *RV) const { 1477 Init *NewRec = RV ? Rec->resolveReferences(R, RV) : Rec; 1478 1479 if (Init *BitsVal = NewRec->getFieldInit(R, RV, FieldName)) { 1480 Init *BVR = BitsVal->resolveReferences(R, RV); 1481 return BVR->isComplete() ? BVR : const_cast<FieldInit *>(this); 1482 } 1483 1484 if (NewRec != Rec) 1485 return FieldInit::get(NewRec, FieldName); 1486 return const_cast<FieldInit *>(this); 1487 } 1488 1489 static void ProfileDagInit(FoldingSetNodeID &ID, Init *V, const std::string &VN, 1490 ArrayRef<Init *> ArgRange, 1491 ArrayRef<std::string> NameRange) { 1492 ID.AddPointer(V); 1493 ID.AddString(VN); 1494 1495 ArrayRef<Init *>::iterator Arg = ArgRange.begin(); 1496 ArrayRef<std::string>::iterator Name = NameRange.begin(); 1497 while (Arg != ArgRange.end()) { 1498 assert(Name != NameRange.end() && "Arg name underflow!"); 1499 ID.AddPointer(*Arg++); 1500 ID.AddString(*Name++); 1501 } 1502 assert(Name == NameRange.end() && "Arg name overflow!"); 1503 } 1504 1505 DagInit * 1506 DagInit::get(Init *V, const std::string &VN, 1507 ArrayRef<Init *> ArgRange, 1508 ArrayRef<std::string> NameRange) { 1509 static FoldingSet<DagInit> ThePool; 1510 static std::vector<std::unique_ptr<DagInit>> TheActualPool; 1511 1512 FoldingSetNodeID ID; 1513 ProfileDagInit(ID, V, VN, ArgRange, NameRange); 1514 1515 void *IP = nullptr; 1516 if (DagInit *I = ThePool.FindNodeOrInsertPos(ID, IP)) 1517 return I; 1518 1519 DagInit *I = new DagInit(V, VN, ArgRange, NameRange); 1520 ThePool.InsertNode(I, IP); 1521 TheActualPool.push_back(std::unique_ptr<DagInit>(I)); 1522 return I; 1523 } 1524 1525 DagInit * 1526 DagInit::get(Init *V, const std::string &VN, 1527 const std::vector<std::pair<Init*, std::string> > &args) { 1528 std::vector<Init *> Args; 1529 std::vector<std::string> Names; 1530 1531 for (const auto &Arg : args) { 1532 Args.push_back(Arg.first); 1533 Names.push_back(Arg.second); 1534 } 1535 1536 return DagInit::get(V, VN, Args, Names); 1537 } 1538 1539 void DagInit::Profile(FoldingSetNodeID &ID) const { 1540 ProfileDagInit(ID, Val, ValName, Args, ArgNames); 1541 } 1542 1543 Init *DagInit::convertInitializerTo(RecTy *Ty) const { 1544 if (isa<DagRecTy>(Ty)) 1545 return const_cast<DagInit *>(this); 1546 1547 return nullptr; 1548 } 1549 1550 Init *DagInit::resolveReferences(Record &R, const RecordVal *RV) const { 1551 std::vector<Init*> NewArgs; 1552 for (unsigned i = 0, e = Args.size(); i != e; ++i) 1553 NewArgs.push_back(Args[i]->resolveReferences(R, RV)); 1554 1555 Init *Op = Val->resolveReferences(R, RV); 1556 1557 if (Args != NewArgs || Op != Val) 1558 return DagInit::get(Op, ValName, NewArgs, ArgNames); 1559 1560 return const_cast<DagInit *>(this); 1561 } 1562 1563 1564 std::string DagInit::getAsString() const { 1565 std::string Result = "(" + Val->getAsString(); 1566 if (!ValName.empty()) 1567 Result += ":" + ValName; 1568 if (!Args.empty()) { 1569 Result += " " + Args[0]->getAsString(); 1570 if (!ArgNames[0].empty()) Result += ":$" + ArgNames[0]; 1571 for (unsigned i = 1, e = Args.size(); i != e; ++i) { 1572 Result += ", " + Args[i]->getAsString(); 1573 if (!ArgNames[i].empty()) Result += ":$" + ArgNames[i]; 1574 } 1575 } 1576 return Result + ")"; 1577 } 1578 1579 1580 //===----------------------------------------------------------------------===// 1581 // Other implementations 1582 //===----------------------------------------------------------------------===// 1583 1584 RecordVal::RecordVal(Init *N, RecTy *T, bool P) 1585 : NameAndPrefix(N, P), Ty(T) { 1586 Value = UnsetInit::get()->convertInitializerTo(Ty); 1587 assert(Value && "Cannot create unset value for current type!"); 1588 } 1589 1590 RecordVal::RecordVal(const std::string &N, RecTy *T, bool P) 1591 : NameAndPrefix(StringInit::get(N), P), Ty(T) { 1592 Value = UnsetInit::get()->convertInitializerTo(Ty); 1593 assert(Value && "Cannot create unset value for current type!"); 1594 } 1595 1596 const std::string &RecordVal::getName() const { 1597 return cast<StringInit>(getNameInit())->getValue(); 1598 } 1599 1600 LLVM_DUMP_METHOD void RecordVal::dump() const { errs() << *this; } 1601 1602 void RecordVal::print(raw_ostream &OS, bool PrintSem) const { 1603 if (getPrefix()) OS << "field "; 1604 OS << *getType() << " " << getNameInitAsString(); 1605 1606 if (getValue()) 1607 OS << " = " << *getValue(); 1608 1609 if (PrintSem) OS << ";\n"; 1610 } 1611 1612 unsigned Record::LastID = 0; 1613 1614 void Record::init() { 1615 checkName(); 1616 1617 // Every record potentially has a def at the top. This value is 1618 // replaced with the top-level def name at instantiation time. 1619 RecordVal DN("NAME", StringRecTy::get(), 0); 1620 addValue(DN); 1621 } 1622 1623 void Record::checkName() { 1624 // Ensure the record name has string type. 1625 const TypedInit *TypedName = cast<const TypedInit>(Name); 1626 if (!isa<StringRecTy>(TypedName->getType())) 1627 PrintFatalError(getLoc(), "Record name is not a string!"); 1628 } 1629 1630 DefInit *Record::getDefInit() { 1631 if (!TheInit) 1632 TheInit.reset(new DefInit(this, new RecordRecTy(this))); 1633 return TheInit.get(); 1634 } 1635 1636 const std::string &Record::getName() const { 1637 return cast<StringInit>(Name)->getValue(); 1638 } 1639 1640 void Record::setName(Init *NewName) { 1641 Name = NewName; 1642 checkName(); 1643 // DO NOT resolve record values to the name at this point because 1644 // there might be default values for arguments of this def. Those 1645 // arguments might not have been resolved yet so we don't want to 1646 // prematurely assume values for those arguments were not passed to 1647 // this def. 1648 // 1649 // Nonetheless, it may be that some of this Record's values 1650 // reference the record name. Indeed, the reason for having the 1651 // record name be an Init is to provide this flexibility. The extra 1652 // resolve steps after completely instantiating defs takes care of 1653 // this. See TGParser::ParseDef and TGParser::ParseDefm. 1654 } 1655 1656 void Record::setName(const std::string &Name) { 1657 setName(StringInit::get(Name)); 1658 } 1659 1660 /// resolveReferencesTo - If anything in this record refers to RV, replace the 1661 /// reference to RV with the RHS of RV. If RV is null, we resolve all possible 1662 /// references. 1663 void Record::resolveReferencesTo(const RecordVal *RV) { 1664 for (unsigned i = 0, e = Values.size(); i != e; ++i) { 1665 if (RV == &Values[i]) // Skip resolve the same field as the given one 1666 continue; 1667 if (Init *V = Values[i].getValue()) 1668 if (Values[i].setValue(V->resolveReferences(*this, RV))) 1669 PrintFatalError(getLoc(), "Invalid value is found when setting '" + 1670 Values[i].getNameInitAsString() + 1671 "' after resolving references" + 1672 (RV ? " against '" + RV->getNameInitAsString() + 1673 "' of (" + RV->getValue()->getAsUnquotedString() + 1674 ")" 1675 : "") + "\n"); 1676 } 1677 Init *OldName = getNameInit(); 1678 Init *NewName = Name->resolveReferences(*this, RV); 1679 if (NewName != OldName) { 1680 // Re-register with RecordKeeper. 1681 setName(NewName); 1682 } 1683 } 1684 1685 LLVM_DUMP_METHOD void Record::dump() const { errs() << *this; } 1686 1687 raw_ostream &llvm::operator<<(raw_ostream &OS, const Record &R) { 1688 OS << R.getNameInitAsString(); 1689 1690 ArrayRef<Init *> TArgs = R.getTemplateArgs(); 1691 if (!TArgs.empty()) { 1692 OS << "<"; 1693 bool NeedComma = false; 1694 for (const Init *TA : TArgs) { 1695 if (NeedComma) OS << ", "; 1696 NeedComma = true; 1697 const RecordVal *RV = R.getValue(TA); 1698 assert(RV && "Template argument record not found??"); 1699 RV->print(OS, false); 1700 } 1701 OS << ">"; 1702 } 1703 1704 OS << " {"; 1705 ArrayRef<std::pair<Record *, SMRange>> SC = R.getSuperClasses(); 1706 if (!SC.empty()) { 1707 OS << "\t//"; 1708 for (const auto &SuperPair : SC) 1709 OS << " " << SuperPair.first->getNameInitAsString(); 1710 } 1711 OS << "\n"; 1712 1713 for (const RecordVal &Val : R.getValues()) 1714 if (Val.getPrefix() && !R.isTemplateArg(Val.getName())) 1715 OS << Val; 1716 for (const RecordVal &Val : R.getValues()) 1717 if (!Val.getPrefix() && !R.isTemplateArg(Val.getName())) 1718 OS << Val; 1719 1720 return OS << "}\n"; 1721 } 1722 1723 /// getValueInit - Return the initializer for a value with the specified name, 1724 /// or abort if the field does not exist. 1725 /// 1726 Init *Record::getValueInit(StringRef FieldName) const { 1727 const RecordVal *R = getValue(FieldName); 1728 if (!R || !R->getValue()) 1729 PrintFatalError(getLoc(), "Record `" + getName() + 1730 "' does not have a field named `" + FieldName + "'!\n"); 1731 return R->getValue(); 1732 } 1733 1734 1735 /// getValueAsString - This method looks up the specified field and returns its 1736 /// value as a string, aborts if the field does not exist or if 1737 /// the value is not a string. 1738 /// 1739 std::string Record::getValueAsString(StringRef FieldName) const { 1740 const RecordVal *R = getValue(FieldName); 1741 if (!R || !R->getValue()) 1742 PrintFatalError(getLoc(), "Record `" + getName() + 1743 "' does not have a field named `" + FieldName + "'!\n"); 1744 1745 if (StringInit *SI = dyn_cast<StringInit>(R->getValue())) 1746 return SI->getValue(); 1747 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + 1748 FieldName + "' does not have a string initializer!"); 1749 } 1750 1751 /// getValueAsBitsInit - This method looks up the specified field and returns 1752 /// its value as a BitsInit, aborts if the field does not exist or if 1753 /// the value is not the right type. 1754 /// 1755 BitsInit *Record::getValueAsBitsInit(StringRef FieldName) const { 1756 const RecordVal *R = getValue(FieldName); 1757 if (!R || !R->getValue()) 1758 PrintFatalError(getLoc(), "Record `" + getName() + 1759 "' does not have a field named `" + FieldName + "'!\n"); 1760 1761 if (BitsInit *BI = dyn_cast<BitsInit>(R->getValue())) 1762 return BI; 1763 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + 1764 FieldName + "' does not have a BitsInit initializer!"); 1765 } 1766 1767 /// getValueAsListInit - This method looks up the specified field and returns 1768 /// its value as a ListInit, aborting if the field does not exist or if 1769 /// the value is not the right type. 1770 /// 1771 ListInit *Record::getValueAsListInit(StringRef FieldName) const { 1772 const RecordVal *R = getValue(FieldName); 1773 if (!R || !R->getValue()) 1774 PrintFatalError(getLoc(), "Record `" + getName() + 1775 "' does not have a field named `" + FieldName + "'!\n"); 1776 1777 if (ListInit *LI = dyn_cast<ListInit>(R->getValue())) 1778 return LI; 1779 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + 1780 FieldName + "' does not have a list initializer!"); 1781 } 1782 1783 /// getValueAsListOfDefs - This method looks up the specified field and returns 1784 /// its value as a vector of records, aborting if the field does not exist 1785 /// or if the value is not the right type. 1786 /// 1787 std::vector<Record*> 1788 Record::getValueAsListOfDefs(StringRef FieldName) const { 1789 ListInit *List = getValueAsListInit(FieldName); 1790 std::vector<Record*> Defs; 1791 for (Init *I : List->getValues()) { 1792 if (DefInit *DI = dyn_cast<DefInit>(I)) 1793 Defs.push_back(DI->getDef()); 1794 else 1795 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + 1796 FieldName + "' list is not entirely DefInit!"); 1797 } 1798 return Defs; 1799 } 1800 1801 /// getValueAsInt - This method looks up the specified field and returns its 1802 /// value as an int64_t, aborting if the field does not exist or if the value 1803 /// is not the right type. 1804 /// 1805 int64_t Record::getValueAsInt(StringRef FieldName) const { 1806 const RecordVal *R = getValue(FieldName); 1807 if (!R || !R->getValue()) 1808 PrintFatalError(getLoc(), "Record `" + getName() + 1809 "' does not have a field named `" + FieldName + "'!\n"); 1810 1811 if (IntInit *II = dyn_cast<IntInit>(R->getValue())) 1812 return II->getValue(); 1813 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + 1814 FieldName + "' does not have an int initializer!"); 1815 } 1816 1817 /// getValueAsListOfInts - This method looks up the specified field and returns 1818 /// its value as a vector of integers, aborting if the field does not exist or 1819 /// if the value is not the right type. 1820 /// 1821 std::vector<int64_t> 1822 Record::getValueAsListOfInts(StringRef FieldName) const { 1823 ListInit *List = getValueAsListInit(FieldName); 1824 std::vector<int64_t> Ints; 1825 for (Init *I : List->getValues()) { 1826 if (IntInit *II = dyn_cast<IntInit>(I)) 1827 Ints.push_back(II->getValue()); 1828 else 1829 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + 1830 FieldName + "' does not have a list of ints initializer!"); 1831 } 1832 return Ints; 1833 } 1834 1835 /// getValueAsListOfStrings - This method looks up the specified field and 1836 /// returns its value as a vector of strings, aborting if the field does not 1837 /// exist or if the value is not the right type. 1838 /// 1839 std::vector<std::string> 1840 Record::getValueAsListOfStrings(StringRef FieldName) const { 1841 ListInit *List = getValueAsListInit(FieldName); 1842 std::vector<std::string> Strings; 1843 for (Init *I : List->getValues()) { 1844 if (StringInit *SI = dyn_cast<StringInit>(I)) 1845 Strings.push_back(SI->getValue()); 1846 else 1847 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + 1848 FieldName + "' does not have a list of strings initializer!"); 1849 } 1850 return Strings; 1851 } 1852 1853 /// getValueAsDef - This method looks up the specified field and returns its 1854 /// value as a Record, aborting if the field does not exist or if the value 1855 /// is not the right type. 1856 /// 1857 Record *Record::getValueAsDef(StringRef FieldName) const { 1858 const RecordVal *R = getValue(FieldName); 1859 if (!R || !R->getValue()) 1860 PrintFatalError(getLoc(), "Record `" + getName() + 1861 "' does not have a field named `" + FieldName + "'!\n"); 1862 1863 if (DefInit *DI = dyn_cast<DefInit>(R->getValue())) 1864 return DI->getDef(); 1865 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + 1866 FieldName + "' does not have a def initializer!"); 1867 } 1868 1869 /// getValueAsBit - This method looks up the specified field and returns its 1870 /// value as a bit, aborting if the field does not exist or if the value is 1871 /// not the right type. 1872 /// 1873 bool Record::getValueAsBit(StringRef FieldName) const { 1874 const RecordVal *R = getValue(FieldName); 1875 if (!R || !R->getValue()) 1876 PrintFatalError(getLoc(), "Record `" + getName() + 1877 "' does not have a field named `" + FieldName + "'!\n"); 1878 1879 if (BitInit *BI = dyn_cast<BitInit>(R->getValue())) 1880 return BI->getValue(); 1881 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + 1882 FieldName + "' does not have a bit initializer!"); 1883 } 1884 1885 bool Record::getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const { 1886 const RecordVal *R = getValue(FieldName); 1887 if (!R || !R->getValue()) 1888 PrintFatalError(getLoc(), "Record `" + getName() + 1889 "' does not have a field named `" + FieldName.str() + "'!\n"); 1890 1891 if (isa<UnsetInit>(R->getValue())) { 1892 Unset = true; 1893 return false; 1894 } 1895 Unset = false; 1896 if (BitInit *BI = dyn_cast<BitInit>(R->getValue())) 1897 return BI->getValue(); 1898 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + 1899 FieldName + "' does not have a bit initializer!"); 1900 } 1901 1902 /// getValueAsDag - This method looks up the specified field and returns its 1903 /// value as an Dag, aborting if the field does not exist or if the value is 1904 /// not the right type. 1905 /// 1906 DagInit *Record::getValueAsDag(StringRef FieldName) const { 1907 const RecordVal *R = getValue(FieldName); 1908 if (!R || !R->getValue()) 1909 PrintFatalError(getLoc(), "Record `" + getName() + 1910 "' does not have a field named `" + FieldName + "'!\n"); 1911 1912 if (DagInit *DI = dyn_cast<DagInit>(R->getValue())) 1913 return DI; 1914 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + 1915 FieldName + "' does not have a dag initializer!"); 1916 } 1917 1918 1919 LLVM_DUMP_METHOD void MultiClass::dump() const { 1920 errs() << "Record:\n"; 1921 Rec.dump(); 1922 1923 errs() << "Defs:\n"; 1924 for (const auto &Proto : DefPrototypes) 1925 Proto->dump(); 1926 } 1927 1928 1929 LLVM_DUMP_METHOD void RecordKeeper::dump() const { errs() << *this; } 1930 1931 raw_ostream &llvm::operator<<(raw_ostream &OS, const RecordKeeper &RK) { 1932 OS << "------------- Classes -----------------\n"; 1933 for (const auto &C : RK.getClasses()) 1934 OS << "class " << *C.second; 1935 1936 OS << "------------- Defs -----------------\n"; 1937 for (const auto &D : RK.getDefs()) 1938 OS << "def " << *D.second; 1939 return OS; 1940 } 1941 1942 1943 /// getAllDerivedDefinitions - This method returns all concrete definitions 1944 /// that derive from the specified class name. If a class with the specified 1945 /// name does not exist, an error is printed and true is returned. 1946 std::vector<Record*> 1947 RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const { 1948 Record *Class = getClass(ClassName); 1949 if (!Class) 1950 PrintFatalError("ERROR: Couldn't find the `" + ClassName + "' class!\n"); 1951 1952 std::vector<Record*> Defs; 1953 for (const auto &D : getDefs()) 1954 if (D.second->isSubClassOf(Class)) 1955 Defs.push_back(D.second.get()); 1956 1957 return Defs; 1958 } 1959 1960 /// QualifyName - Return an Init with a qualifier prefix referring 1961 /// to CurRec's name. 1962 Init *llvm::QualifyName(Record &CurRec, MultiClass *CurMultiClass, 1963 Init *Name, const std::string &Scoper) { 1964 RecTy *Type = cast<TypedInit>(Name)->getType(); 1965 1966 BinOpInit *NewName = 1967 BinOpInit::get(BinOpInit::STRCONCAT, 1968 BinOpInit::get(BinOpInit::STRCONCAT, 1969 CurRec.getNameInit(), 1970 StringInit::get(Scoper), 1971 Type)->Fold(&CurRec, CurMultiClass), 1972 Name, 1973 Type); 1974 1975 if (CurMultiClass && Scoper != "::") { 1976 NewName = 1977 BinOpInit::get(BinOpInit::STRCONCAT, 1978 BinOpInit::get(BinOpInit::STRCONCAT, 1979 CurMultiClass->Rec.getNameInit(), 1980 StringInit::get("::"), 1981 Type)->Fold(&CurRec, CurMultiClass), 1982 NewName->Fold(&CurRec, CurMultiClass), 1983 Type); 1984 } 1985 1986 return NewName->Fold(&CurRec, CurMultiClass); 1987 } 1988 1989 /// QualifyName - Return an Init with a qualifier prefix referring 1990 /// to CurRec's name. 1991 Init *llvm::QualifyName(Record &CurRec, MultiClass *CurMultiClass, 1992 const std::string &Name, 1993 const std::string &Scoper) { 1994 return QualifyName(CurRec, CurMultiClass, StringInit::get(Name), Scoper); 1995 } 1996