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