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