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, Record *CurRec, 923 MultiClass *CurMultiClass); 924 925 // Evaluates operation RHSo after replacing all operands matching LHS with Arg. 926 static Init *EvaluateOperation(OpInit *RHSo, Init *LHS, Init *Arg, 927 Record *CurRec, MultiClass *CurMultiClass) { 928 929 SmallVector<Init *, 8> NewOperands; 930 NewOperands.reserve(RHSo->getNumOperands()); 931 for (unsigned i = 0, e = RHSo->getNumOperands(); i < e; ++i) { 932 if (auto *RHSoo = dyn_cast<OpInit>(RHSo->getOperand(i))) { 933 if (Init *Result = 934 EvaluateOperation(RHSoo, LHS, Arg, CurRec, CurMultiClass)) 935 NewOperands.push_back(Result); 936 else 937 NewOperands.push_back(RHSoo); 938 } else if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) { 939 NewOperands.push_back(Arg); 940 } else { 941 NewOperands.push_back(RHSo->getOperand(i)); 942 } 943 } 944 945 // Now run the operator and use its result as the new leaf 946 const OpInit *NewOp = RHSo->clone(NewOperands); 947 Init *NewVal = NewOp->Fold(CurRec, CurMultiClass); 948 return (NewVal != NewOp) ? NewVal : nullptr; 949 } 950 951 // Applies RHS to all elements of MHS, using LHS as a temp variable. 952 static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, Record *CurRec, 953 MultiClass *CurMultiClass) { 954 OpInit *RHSo = dyn_cast<OpInit>(RHS); 955 956 if (!RHSo) 957 PrintFatalError(CurRec->getLoc(), "!foreach requires an operator\n"); 958 959 TypedInit *LHSt = dyn_cast<TypedInit>(LHS); 960 961 if (!LHSt) 962 PrintFatalError(CurRec->getLoc(), "!foreach requires typed variable\n"); 963 964 DagInit *MHSd = dyn_cast<DagInit>(MHS); 965 if (MHSd) { 966 Init *Val = MHSd->getOperator(); 967 if (Init *Result = EvaluateOperation(RHSo, LHS, Val, CurRec, CurMultiClass)) 968 Val = Result; 969 970 SmallVector<std::pair<Init *, StringInit *>, 8> args; 971 for (unsigned int i = 0; i < MHSd->getNumArgs(); ++i) { 972 Init *Arg = MHSd->getArg(i); 973 StringInit *ArgName = MHSd->getArgName(i); 974 // If this is a dag, recurse 975 if (isa<DagInit>(Arg)) { 976 if (Init *Result = ForeachHelper(LHS, Arg, RHSo, CurRec, CurMultiClass)) 977 Arg = Result; 978 } else if (Init *Result = 979 EvaluateOperation(RHSo, LHS, Arg, CurRec, CurMultiClass)) { 980 Arg = Result; 981 } 982 983 // TODO: Process arg names 984 args.push_back(std::make_pair(Arg, ArgName)); 985 } 986 987 return DagInit::get(Val, nullptr, args); 988 } 989 990 ListInit *MHSl = dyn_cast<ListInit>(MHS); 991 if (MHSl) { 992 SmallVector<Init *, 8> NewList(MHSl->begin(), MHSl->end()); 993 for (Init *&Arg : NewList) { 994 if (Init *Result = 995 EvaluateOperation(RHSo, LHS, Arg, CurRec, CurMultiClass)) 996 Arg = Result; 997 } 998 return ListInit::get(NewList, MHSl->getType()); 999 } 1000 return nullptr; 1001 } 1002 1003 Init *TernOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const { 1004 switch (getOpcode()) { 1005 case SUBST: { 1006 DefInit *LHSd = dyn_cast<DefInit>(LHS); 1007 VarInit *LHSv = dyn_cast<VarInit>(LHS); 1008 StringInit *LHSs = dyn_cast<StringInit>(LHS); 1009 1010 DefInit *MHSd = dyn_cast<DefInit>(MHS); 1011 VarInit *MHSv = dyn_cast<VarInit>(MHS); 1012 StringInit *MHSs = dyn_cast<StringInit>(MHS); 1013 1014 DefInit *RHSd = dyn_cast<DefInit>(RHS); 1015 VarInit *RHSv = dyn_cast<VarInit>(RHS); 1016 StringInit *RHSs = dyn_cast<StringInit>(RHS); 1017 1018 if (LHSd && MHSd && RHSd) { 1019 Record *Val = RHSd->getDef(); 1020 if (LHSd->getAsString() == RHSd->getAsString()) 1021 Val = MHSd->getDef(); 1022 return DefInit::get(Val); 1023 } 1024 if (LHSv && MHSv && RHSv) { 1025 std::string Val = RHSv->getName(); 1026 if (LHSv->getAsString() == RHSv->getAsString()) 1027 Val = MHSv->getName(); 1028 return VarInit::get(Val, getType()); 1029 } 1030 if (LHSs && MHSs && RHSs) { 1031 std::string Val = RHSs->getValue(); 1032 1033 std::string::size_type found; 1034 std::string::size_type idx = 0; 1035 while (true) { 1036 found = Val.find(LHSs->getValue(), idx); 1037 if (found == std::string::npos) 1038 break; 1039 Val.replace(found, LHSs->getValue().size(), MHSs->getValue()); 1040 idx = found + MHSs->getValue().size(); 1041 } 1042 1043 return StringInit::get(Val); 1044 } 1045 break; 1046 } 1047 1048 case FOREACH: { 1049 if (Init *Result = ForeachHelper(LHS, MHS, RHS, CurRec, CurMultiClass)) 1050 return Result; 1051 break; 1052 } 1053 1054 case IF: { 1055 IntInit *LHSi = dyn_cast<IntInit>(LHS); 1056 if (Init *I = LHS->convertInitializerTo(IntRecTy::get())) 1057 LHSi = dyn_cast<IntInit>(I); 1058 if (LHSi) { 1059 if (LHSi->getValue()) 1060 return MHS; 1061 return RHS; 1062 } 1063 break; 1064 } 1065 } 1066 1067 return const_cast<TernOpInit *>(this); 1068 } 1069 1070 Init *TernOpInit::resolveReferences(Record &R, 1071 const RecordVal *RV) const { 1072 Init *lhs = LHS->resolveReferences(R, RV); 1073 1074 if (getOpcode() == IF && lhs != LHS) { 1075 IntInit *Value = dyn_cast<IntInit>(lhs); 1076 if (Init *I = lhs->convertInitializerTo(IntRecTy::get())) 1077 Value = dyn_cast<IntInit>(I); 1078 if (Value) { 1079 // Short-circuit 1080 if (Value->getValue()) { 1081 Init *mhs = MHS->resolveReferences(R, RV); 1082 return (TernOpInit::get(getOpcode(), lhs, mhs, 1083 RHS, getType()))->Fold(&R, nullptr); 1084 } 1085 Init *rhs = RHS->resolveReferences(R, RV); 1086 return (TernOpInit::get(getOpcode(), lhs, MHS, 1087 rhs, getType()))->Fold(&R, nullptr); 1088 } 1089 } 1090 1091 Init *mhs = MHS->resolveReferences(R, RV); 1092 Init *rhs = RHS->resolveReferences(R, RV); 1093 1094 if (LHS != lhs || MHS != mhs || RHS != rhs) 1095 return (TernOpInit::get(getOpcode(), lhs, mhs, rhs, 1096 getType()))->Fold(&R, nullptr); 1097 return Fold(&R, nullptr); 1098 } 1099 1100 std::string TernOpInit::getAsString() const { 1101 std::string Result; 1102 switch (getOpcode()) { 1103 case SUBST: Result = "!subst"; break; 1104 case FOREACH: Result = "!foreach"; break; 1105 case IF: Result = "!if"; break; 1106 } 1107 return Result + "(" + LHS->getAsString() + ", " + MHS->getAsString() + ", " + 1108 RHS->getAsString() + ")"; 1109 } 1110 1111 RecTy *TypedInit::getFieldType(StringInit *FieldName) const { 1112 if (RecordRecTy *RecordType = dyn_cast<RecordRecTy>(getType())) 1113 if (RecordVal *Field = RecordType->getRecord()->getValue(FieldName)) 1114 return Field->getType(); 1115 return nullptr; 1116 } 1117 1118 Init * 1119 TypedInit::convertInitializerTo(RecTy *Ty) const { 1120 if (isa<IntRecTy>(Ty)) { 1121 if (getType()->typeIsConvertibleTo(Ty)) 1122 return const_cast<TypedInit *>(this); 1123 return nullptr; 1124 } 1125 1126 if (isa<StringRecTy>(Ty)) { 1127 if (isa<StringRecTy>(getType())) 1128 return const_cast<TypedInit *>(this); 1129 return nullptr; 1130 } 1131 1132 if (isa<CodeRecTy>(Ty)) { 1133 if (isa<CodeRecTy>(getType())) 1134 return const_cast<TypedInit *>(this); 1135 return nullptr; 1136 } 1137 1138 if (isa<BitRecTy>(Ty)) { 1139 // Accept variable if it is already of bit type! 1140 if (isa<BitRecTy>(getType())) 1141 return const_cast<TypedInit *>(this); 1142 if (auto *BitsTy = dyn_cast<BitsRecTy>(getType())) { 1143 // Accept only bits<1> expression. 1144 if (BitsTy->getNumBits() == 1) 1145 return const_cast<TypedInit *>(this); 1146 return nullptr; 1147 } 1148 // Ternary !if can be converted to bit, but only if both sides are 1149 // convertible to a bit. 1150 if (const auto *TOI = dyn_cast<TernOpInit>(this)) { 1151 if (TOI->getOpcode() == TernOpInit::TernaryOp::IF && 1152 TOI->getMHS()->convertInitializerTo(BitRecTy::get()) && 1153 TOI->getRHS()->convertInitializerTo(BitRecTy::get())) 1154 return const_cast<TypedInit *>(this); 1155 return nullptr; 1156 } 1157 return nullptr; 1158 } 1159 1160 if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) { 1161 if (BRT->getNumBits() == 1 && isa<BitRecTy>(getType())) 1162 return BitsInit::get(const_cast<TypedInit *>(this)); 1163 1164 if (getType()->typeIsConvertibleTo(BRT)) { 1165 SmallVector<Init *, 16> NewBits(BRT->getNumBits()); 1166 1167 for (unsigned i = 0; i != BRT->getNumBits(); ++i) 1168 NewBits[i] = VarBitInit::get(const_cast<TypedInit *>(this), i); 1169 return BitsInit::get(NewBits); 1170 } 1171 1172 return nullptr; 1173 } 1174 1175 if (auto *DLRT = dyn_cast<ListRecTy>(Ty)) { 1176 if (auto *SLRT = dyn_cast<ListRecTy>(getType())) 1177 if (SLRT->getElementType()->typeIsConvertibleTo(DLRT->getElementType())) 1178 return const_cast<TypedInit *>(this); 1179 return nullptr; 1180 } 1181 1182 if (auto *DRT = dyn_cast<DagRecTy>(Ty)) { 1183 if (getType()->typeIsConvertibleTo(DRT)) 1184 return const_cast<TypedInit *>(this); 1185 return nullptr; 1186 } 1187 1188 if (auto *SRRT = dyn_cast<RecordRecTy>(Ty)) { 1189 // Ensure that this is compatible with Rec. 1190 if (RecordRecTy *DRRT = dyn_cast<RecordRecTy>(getType())) 1191 if (DRRT->getRecord()->isSubClassOf(SRRT->getRecord()) || 1192 DRRT->getRecord() == SRRT->getRecord()) 1193 return const_cast<TypedInit *>(this); 1194 return nullptr; 1195 } 1196 1197 return nullptr; 1198 } 1199 1200 Init *TypedInit::convertInitializerBitRange(ArrayRef<unsigned> Bits) const { 1201 BitsRecTy *T = dyn_cast<BitsRecTy>(getType()); 1202 if (!T) return nullptr; // Cannot subscript a non-bits variable. 1203 unsigned NumBits = T->getNumBits(); 1204 1205 SmallVector<Init *, 16> NewBits; 1206 NewBits.reserve(Bits.size()); 1207 for (unsigned Bit : Bits) { 1208 if (Bit >= NumBits) 1209 return nullptr; 1210 1211 NewBits.push_back(VarBitInit::get(const_cast<TypedInit *>(this), Bit)); 1212 } 1213 return BitsInit::get(NewBits); 1214 } 1215 1216 Init *TypedInit::convertInitListSlice(ArrayRef<unsigned> Elements) const { 1217 ListRecTy *T = dyn_cast<ListRecTy>(getType()); 1218 if (!T) return nullptr; // Cannot subscript a non-list variable. 1219 1220 if (Elements.size() == 1) 1221 return VarListElementInit::get(const_cast<TypedInit *>(this), Elements[0]); 1222 1223 SmallVector<Init*, 8> ListInits; 1224 ListInits.reserve(Elements.size()); 1225 for (unsigned Element : Elements) 1226 ListInits.push_back(VarListElementInit::get(const_cast<TypedInit *>(this), 1227 Element)); 1228 return ListInit::get(ListInits, T); 1229 } 1230 1231 1232 VarInit *VarInit::get(StringRef VN, RecTy *T) { 1233 Init *Value = StringInit::get(VN); 1234 return VarInit::get(Value, T); 1235 } 1236 1237 VarInit *VarInit::get(Init *VN, RecTy *T) { 1238 using Key = std::pair<RecTy *, Init *>; 1239 static DenseMap<Key, VarInit*> ThePool; 1240 1241 Key TheKey(std::make_pair(T, VN)); 1242 1243 VarInit *&I = ThePool[TheKey]; 1244 if (!I) 1245 I = new(Allocator) VarInit(VN, T); 1246 return I; 1247 } 1248 1249 StringRef VarInit::getName() const { 1250 StringInit *NameString = cast<StringInit>(getNameInit()); 1251 return NameString->getValue(); 1252 } 1253 1254 Init *VarInit::getBit(unsigned Bit) const { 1255 if (getType() == BitRecTy::get()) 1256 return const_cast<VarInit*>(this); 1257 return VarBitInit::get(const_cast<VarInit*>(this), Bit); 1258 } 1259 1260 Init *VarInit::resolveListElementReference(Record &R, 1261 const RecordVal *IRV, 1262 unsigned Elt) const { 1263 if (R.isTemplateArg(getNameInit())) return nullptr; 1264 if (IRV && IRV->getNameInit() != getNameInit()) return nullptr; 1265 1266 RecordVal *RV = R.getValue(getNameInit()); 1267 assert(RV && "Reference to a non-existent variable?"); 1268 ListInit *LI = dyn_cast<ListInit>(RV->getValue()); 1269 if (!LI) 1270 return VarListElementInit::get(cast<TypedInit>(RV->getValue()), Elt); 1271 1272 if (Elt >= LI->size()) 1273 return nullptr; // Out of range reference. 1274 Init *E = LI->getElement(Elt); 1275 // If the element is set to some value, or if we are resolving a reference 1276 // to a specific variable and that variable is explicitly unset, then 1277 // replace the VarListElementInit with it. 1278 if (IRV || !isa<UnsetInit>(E)) 1279 return E; 1280 return nullptr; 1281 } 1282 1283 RecTy *VarInit::getFieldType(StringInit *FieldName) const { 1284 if (RecordRecTy *RTy = dyn_cast<RecordRecTy>(getType())) 1285 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName)) 1286 return RV->getType(); 1287 return nullptr; 1288 } 1289 1290 Init *VarInit::getFieldInit(Record &R, const RecordVal *RV, 1291 StringInit *FieldName) const { 1292 if (isa<RecordRecTy>(getType())) 1293 if (const RecordVal *Val = R.getValue(VarName)) { 1294 if (RV != Val && (RV || isa<UnsetInit>(Val->getValue()))) 1295 return nullptr; 1296 Init *TheInit = Val->getValue(); 1297 assert(TheInit != this && "Infinite loop detected!"); 1298 if (Init *I = TheInit->getFieldInit(R, RV, FieldName)) 1299 return I; 1300 return nullptr; 1301 } 1302 return nullptr; 1303 } 1304 1305 Init *VarInit::resolveReferences(Record &R, const RecordVal *RV) const { 1306 if (RecordVal *Val = R.getValue(VarName)) 1307 if (RV == Val || (!RV && !isa<UnsetInit>(Val->getValue()))) 1308 return Val->getValue(); 1309 return const_cast<VarInit *>(this); 1310 } 1311 1312 VarBitInit *VarBitInit::get(TypedInit *T, unsigned B) { 1313 using Key = std::pair<TypedInit *, unsigned>; 1314 static DenseMap<Key, VarBitInit*> ThePool; 1315 1316 Key TheKey(std::make_pair(T, B)); 1317 1318 VarBitInit *&I = ThePool[TheKey]; 1319 if (!I) 1320 I = new(Allocator) VarBitInit(T, B); 1321 return I; 1322 } 1323 1324 Init *VarBitInit::convertInitializerTo(RecTy *Ty) const { 1325 if (isa<BitRecTy>(Ty)) 1326 return const_cast<VarBitInit *>(this); 1327 1328 return nullptr; 1329 } 1330 1331 std::string VarBitInit::getAsString() const { 1332 return TI->getAsString() + "{" + utostr(Bit) + "}"; 1333 } 1334 1335 Init *VarBitInit::resolveReferences(Record &R, const RecordVal *RV) const { 1336 Init *I = TI->resolveReferences(R, RV); 1337 if (TI != I) 1338 return I->getBit(getBitNum()); 1339 1340 return const_cast<VarBitInit*>(this); 1341 } 1342 1343 VarListElementInit *VarListElementInit::get(TypedInit *T, 1344 unsigned E) { 1345 using Key = std::pair<TypedInit *, unsigned>; 1346 static DenseMap<Key, VarListElementInit*> ThePool; 1347 1348 Key TheKey(std::make_pair(T, E)); 1349 1350 VarListElementInit *&I = ThePool[TheKey]; 1351 if (!I) I = new(Allocator) VarListElementInit(T, E); 1352 return I; 1353 } 1354 1355 std::string VarListElementInit::getAsString() const { 1356 return TI->getAsString() + "[" + utostr(Element) + "]"; 1357 } 1358 1359 Init * 1360 VarListElementInit::resolveReferences(Record &R, const RecordVal *RV) const { 1361 if (Init *I = getVariable()->resolveListElementReference(R, RV, 1362 getElementNum())) 1363 return I; 1364 return const_cast<VarListElementInit *>(this); 1365 } 1366 1367 Init *VarListElementInit::getBit(unsigned Bit) const { 1368 if (getType() == BitRecTy::get()) 1369 return const_cast<VarListElementInit*>(this); 1370 return VarBitInit::get(const_cast<VarListElementInit*>(this), Bit); 1371 } 1372 1373 Init *VarListElementInit:: resolveListElementReference(Record &R, 1374 const RecordVal *RV, 1375 unsigned Elt) const { 1376 if (Init *Result = TI->resolveListElementReference(R, RV, Element)) { 1377 if (TypedInit *TInit = dyn_cast<TypedInit>(Result)) { 1378 if (Init *Result2 = TInit->resolveListElementReference(R, RV, Elt)) 1379 return Result2; 1380 return VarListElementInit::get(TInit, Elt); 1381 } 1382 return Result; 1383 } 1384 1385 return nullptr; 1386 } 1387 1388 DefInit *DefInit::get(Record *R) { 1389 return R->getDefInit(); 1390 } 1391 1392 Init *DefInit::convertInitializerTo(RecTy *Ty) const { 1393 if (auto *RRT = dyn_cast<RecordRecTy>(Ty)) 1394 if (getDef()->isSubClassOf(RRT->getRecord())) 1395 return const_cast<DefInit *>(this); 1396 return nullptr; 1397 } 1398 1399 RecTy *DefInit::getFieldType(StringInit *FieldName) const { 1400 if (const RecordVal *RV = Def->getValue(FieldName)) 1401 return RV->getType(); 1402 return nullptr; 1403 } 1404 1405 Init *DefInit::getFieldInit(Record &R, const RecordVal *RV, 1406 StringInit *FieldName) const { 1407 return Def->getValue(FieldName)->getValue(); 1408 } 1409 1410 std::string DefInit::getAsString() const { 1411 return Def->getName(); 1412 } 1413 1414 FieldInit *FieldInit::get(Init *R, StringInit *FN) { 1415 using Key = std::pair<Init *, StringInit *>; 1416 static DenseMap<Key, FieldInit*> ThePool; 1417 1418 Key TheKey(std::make_pair(R, FN)); 1419 1420 FieldInit *&I = ThePool[TheKey]; 1421 if (!I) I = new(Allocator) FieldInit(R, FN); 1422 return I; 1423 } 1424 1425 Init *FieldInit::getBit(unsigned Bit) const { 1426 if (getType() == BitRecTy::get()) 1427 return const_cast<FieldInit*>(this); 1428 return VarBitInit::get(const_cast<FieldInit*>(this), Bit); 1429 } 1430 1431 Init *FieldInit::resolveListElementReference(Record &R, const RecordVal *RV, 1432 unsigned Elt) const { 1433 if (Init *ListVal = Rec->getFieldInit(R, RV, FieldName)) 1434 if (ListInit *LI = dyn_cast<ListInit>(ListVal)) { 1435 if (Elt >= LI->size()) return nullptr; 1436 Init *E = LI->getElement(Elt); 1437 1438 // If the element is set to some value, or if we are resolving a 1439 // reference to a specific variable and that variable is explicitly 1440 // unset, then replace the VarListElementInit with it. 1441 if (RV || !isa<UnsetInit>(E)) 1442 return E; 1443 } 1444 return nullptr; 1445 } 1446 1447 Init *FieldInit::resolveReferences(Record &R, const RecordVal *RV) const { 1448 Init *NewRec = RV ? Rec->resolveReferences(R, RV) : Rec; 1449 1450 if (Init *BitsVal = NewRec->getFieldInit(R, RV, FieldName)) { 1451 Init *BVR = BitsVal->resolveReferences(R, RV); 1452 return BVR->isComplete() ? BVR : const_cast<FieldInit *>(this); 1453 } 1454 1455 if (NewRec != Rec) 1456 return FieldInit::get(NewRec, FieldName); 1457 return const_cast<FieldInit *>(this); 1458 } 1459 1460 static void ProfileDagInit(FoldingSetNodeID &ID, Init *V, StringInit *VN, 1461 ArrayRef<Init *> ArgRange, 1462 ArrayRef<StringInit *> NameRange) { 1463 ID.AddPointer(V); 1464 ID.AddPointer(VN); 1465 1466 ArrayRef<Init *>::iterator Arg = ArgRange.begin(); 1467 ArrayRef<StringInit *>::iterator Name = NameRange.begin(); 1468 while (Arg != ArgRange.end()) { 1469 assert(Name != NameRange.end() && "Arg name underflow!"); 1470 ID.AddPointer(*Arg++); 1471 ID.AddPointer(*Name++); 1472 } 1473 assert(Name == NameRange.end() && "Arg name overflow!"); 1474 } 1475 1476 DagInit * 1477 DagInit::get(Init *V, StringInit *VN, ArrayRef<Init *> ArgRange, 1478 ArrayRef<StringInit *> NameRange) { 1479 static FoldingSet<DagInit> ThePool; 1480 1481 FoldingSetNodeID ID; 1482 ProfileDagInit(ID, V, VN, ArgRange, NameRange); 1483 1484 void *IP = nullptr; 1485 if (DagInit *I = ThePool.FindNodeOrInsertPos(ID, IP)) 1486 return I; 1487 1488 void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *, StringInit *>(ArgRange.size(), NameRange.size()), alignof(BitsInit)); 1489 DagInit *I = new(Mem) DagInit(V, VN, ArgRange.size(), NameRange.size()); 1490 std::uninitialized_copy(ArgRange.begin(), ArgRange.end(), 1491 I->getTrailingObjects<Init *>()); 1492 std::uninitialized_copy(NameRange.begin(), NameRange.end(), 1493 I->getTrailingObjects<StringInit *>()); 1494 ThePool.InsertNode(I, IP); 1495 return I; 1496 } 1497 1498 DagInit * 1499 DagInit::get(Init *V, StringInit *VN, 1500 ArrayRef<std::pair<Init*, StringInit*>> args) { 1501 SmallVector<Init *, 8> Args; 1502 SmallVector<StringInit *, 8> Names; 1503 1504 for (const auto &Arg : args) { 1505 Args.push_back(Arg.first); 1506 Names.push_back(Arg.second); 1507 } 1508 1509 return DagInit::get(V, VN, Args, Names); 1510 } 1511 1512 void DagInit::Profile(FoldingSetNodeID &ID) const { 1513 ProfileDagInit(ID, Val, ValName, makeArrayRef(getTrailingObjects<Init *>(), NumArgs), makeArrayRef(getTrailingObjects<StringInit *>(), NumArgNames)); 1514 } 1515 1516 Init *DagInit::convertInitializerTo(RecTy *Ty) const { 1517 if (isa<DagRecTy>(Ty)) 1518 return const_cast<DagInit *>(this); 1519 1520 return nullptr; 1521 } 1522 1523 Init *DagInit::resolveReferences(Record &R, const RecordVal *RV) const { 1524 SmallVector<Init*, 8> NewArgs; 1525 NewArgs.reserve(arg_size()); 1526 bool ArgsChanged = false; 1527 for (const Init *Arg : getArgs()) { 1528 Init *NewArg = Arg->resolveReferences(R, RV); 1529 NewArgs.push_back(NewArg); 1530 ArgsChanged |= NewArg != Arg; 1531 } 1532 1533 Init *Op = Val->resolveReferences(R, RV); 1534 if (Op != Val || ArgsChanged) 1535 return DagInit::get(Op, ValName, NewArgs, getArgNames()); 1536 1537 return const_cast<DagInit *>(this); 1538 } 1539 1540 std::string DagInit::getAsString() const { 1541 std::string Result = "(" + Val->getAsString(); 1542 if (ValName) 1543 Result += ":" + ValName->getAsUnquotedString(); 1544 if (!arg_empty()) { 1545 Result += " " + getArg(0)->getAsString(); 1546 if (getArgName(0)) Result += ":$" + getArgName(0)->getAsUnquotedString(); 1547 for (unsigned i = 1, e = getNumArgs(); i != e; ++i) { 1548 Result += ", " + getArg(i)->getAsString(); 1549 if (getArgName(i)) Result += ":$" + getArgName(i)->getAsUnquotedString(); 1550 } 1551 } 1552 return Result + ")"; 1553 } 1554 1555 //===----------------------------------------------------------------------===// 1556 // Other implementations 1557 //===----------------------------------------------------------------------===// 1558 1559 RecordVal::RecordVal(Init *N, RecTy *T, bool P) 1560 : Name(N), TyAndPrefix(T, P) { 1561 Value = UnsetInit::get()->convertInitializerTo(T); 1562 assert(Value && "Cannot create unset value for current type!"); 1563 } 1564 1565 StringRef RecordVal::getName() const { 1566 return cast<StringInit>(getNameInit())->getValue(); 1567 } 1568 1569 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 1570 LLVM_DUMP_METHOD void RecordVal::dump() const { errs() << *this; } 1571 #endif 1572 1573 void RecordVal::print(raw_ostream &OS, bool PrintSem) const { 1574 if (getPrefix()) OS << "field "; 1575 OS << *getType() << " " << getNameInitAsString(); 1576 1577 if (getValue()) 1578 OS << " = " << *getValue(); 1579 1580 if (PrintSem) OS << ";\n"; 1581 } 1582 1583 unsigned Record::LastID = 0; 1584 1585 void Record::init() { 1586 checkName(); 1587 1588 // Every record potentially has a def at the top. This value is 1589 // replaced with the top-level def name at instantiation time. 1590 addValue(RecordVal(StringInit::get("NAME"), StringRecTy::get(), false)); 1591 } 1592 1593 void Record::checkName() { 1594 // Ensure the record name has string type. 1595 const TypedInit *TypedName = cast<const TypedInit>(Name); 1596 if (!isa<StringRecTy>(TypedName->getType())) 1597 PrintFatalError(getLoc(), "Record name is not a string!"); 1598 } 1599 1600 DefInit *Record::getDefInit() { 1601 if (!TheInit) 1602 TheInit = new(Allocator) DefInit(this, new(Allocator) RecordRecTy(this)); 1603 return TheInit; 1604 } 1605 1606 void Record::setName(Init *NewName) { 1607 Name = NewName; 1608 checkName(); 1609 // DO NOT resolve record values to the name at this point because 1610 // there might be default values for arguments of this def. Those 1611 // arguments might not have been resolved yet so we don't want to 1612 // prematurely assume values for those arguments were not passed to 1613 // this def. 1614 // 1615 // Nonetheless, it may be that some of this Record's values 1616 // reference the record name. Indeed, the reason for having the 1617 // record name be an Init is to provide this flexibility. The extra 1618 // resolve steps after completely instantiating defs takes care of 1619 // this. See TGParser::ParseDef and TGParser::ParseDefm. 1620 } 1621 1622 void Record::resolveReferencesTo(const RecordVal *RV) { 1623 for (RecordVal &Value : Values) { 1624 if (RV == &Value) // Skip resolve the same field as the given one 1625 continue; 1626 if (Init *V = Value.getValue()) 1627 if (Value.setValue(V->resolveReferences(*this, RV))) 1628 PrintFatalError(getLoc(), "Invalid value is found when setting '" + 1629 Value.getNameInitAsString() + 1630 "' after resolving references" + 1631 (RV ? " against '" + RV->getNameInitAsString() + 1632 "' of (" + RV->getValue()->getAsUnquotedString() + 1633 ")" 1634 : "") + "\n"); 1635 } 1636 Init *OldName = getNameInit(); 1637 Init *NewName = Name->resolveReferences(*this, RV); 1638 if (NewName != OldName) { 1639 // Re-register with RecordKeeper. 1640 setName(NewName); 1641 } 1642 } 1643 1644 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 1645 LLVM_DUMP_METHOD void Record::dump() const { errs() << *this; } 1646 #endif 1647 1648 raw_ostream &llvm::operator<<(raw_ostream &OS, const Record &R) { 1649 OS << R.getNameInitAsString(); 1650 1651 ArrayRef<Init *> TArgs = R.getTemplateArgs(); 1652 if (!TArgs.empty()) { 1653 OS << "<"; 1654 bool NeedComma = false; 1655 for (const Init *TA : TArgs) { 1656 if (NeedComma) OS << ", "; 1657 NeedComma = true; 1658 const RecordVal *RV = R.getValue(TA); 1659 assert(RV && "Template argument record not found??"); 1660 RV->print(OS, false); 1661 } 1662 OS << ">"; 1663 } 1664 1665 OS << " {"; 1666 ArrayRef<std::pair<Record *, SMRange>> SC = R.getSuperClasses(); 1667 if (!SC.empty()) { 1668 OS << "\t//"; 1669 for (const auto &SuperPair : SC) 1670 OS << " " << SuperPair.first->getNameInitAsString(); 1671 } 1672 OS << "\n"; 1673 1674 for (const RecordVal &Val : R.getValues()) 1675 if (Val.getPrefix() && !R.isTemplateArg(Val.getNameInit())) 1676 OS << Val; 1677 for (const RecordVal &Val : R.getValues()) 1678 if (!Val.getPrefix() && !R.isTemplateArg(Val.getNameInit())) 1679 OS << Val; 1680 1681 return OS << "}\n"; 1682 } 1683 1684 Init *Record::getValueInit(StringRef FieldName) const { 1685 const RecordVal *R = getValue(FieldName); 1686 if (!R || !R->getValue()) 1687 PrintFatalError(getLoc(), "Record `" + getName() + 1688 "' does not have a field named `" + FieldName + "'!\n"); 1689 return R->getValue(); 1690 } 1691 1692 StringRef Record::getValueAsString(StringRef FieldName) const { 1693 const RecordVal *R = getValue(FieldName); 1694 if (!R || !R->getValue()) 1695 PrintFatalError(getLoc(), "Record `" + getName() + 1696 "' does not have a field named `" + FieldName + "'!\n"); 1697 1698 if (StringInit *SI = dyn_cast<StringInit>(R->getValue())) 1699 return SI->getValue(); 1700 if (CodeInit *CI = dyn_cast<CodeInit>(R->getValue())) 1701 return CI->getValue(); 1702 1703 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + 1704 FieldName + "' does not have a string initializer!"); 1705 } 1706 1707 BitsInit *Record::getValueAsBitsInit(StringRef FieldName) const { 1708 const RecordVal *R = getValue(FieldName); 1709 if (!R || !R->getValue()) 1710 PrintFatalError(getLoc(), "Record `" + getName() + 1711 "' does not have a field named `" + FieldName + "'!\n"); 1712 1713 if (BitsInit *BI = dyn_cast<BitsInit>(R->getValue())) 1714 return BI; 1715 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + 1716 FieldName + "' does not have a BitsInit initializer!"); 1717 } 1718 1719 ListInit *Record::getValueAsListInit(StringRef FieldName) const { 1720 const RecordVal *R = getValue(FieldName); 1721 if (!R || !R->getValue()) 1722 PrintFatalError(getLoc(), "Record `" + getName() + 1723 "' does not have a field named `" + FieldName + "'!\n"); 1724 1725 if (ListInit *LI = dyn_cast<ListInit>(R->getValue())) 1726 return LI; 1727 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + 1728 FieldName + "' does not have a list initializer!"); 1729 } 1730 1731 std::vector<Record*> 1732 Record::getValueAsListOfDefs(StringRef FieldName) const { 1733 ListInit *List = getValueAsListInit(FieldName); 1734 std::vector<Record*> Defs; 1735 for (Init *I : List->getValues()) { 1736 if (DefInit *DI = dyn_cast<DefInit>(I)) 1737 Defs.push_back(DI->getDef()); 1738 else 1739 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + 1740 FieldName + "' list is not entirely DefInit!"); 1741 } 1742 return Defs; 1743 } 1744 1745 int64_t Record::getValueAsInt(StringRef FieldName) const { 1746 const RecordVal *R = getValue(FieldName); 1747 if (!R || !R->getValue()) 1748 PrintFatalError(getLoc(), "Record `" + getName() + 1749 "' does not have a field named `" + FieldName + "'!\n"); 1750 1751 if (IntInit *II = dyn_cast<IntInit>(R->getValue())) 1752 return II->getValue(); 1753 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + 1754 FieldName + "' does not have an int initializer!"); 1755 } 1756 1757 std::vector<int64_t> 1758 Record::getValueAsListOfInts(StringRef FieldName) const { 1759 ListInit *List = getValueAsListInit(FieldName); 1760 std::vector<int64_t> Ints; 1761 for (Init *I : List->getValues()) { 1762 if (IntInit *II = dyn_cast<IntInit>(I)) 1763 Ints.push_back(II->getValue()); 1764 else 1765 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + 1766 FieldName + "' does not have a list of ints initializer!"); 1767 } 1768 return Ints; 1769 } 1770 1771 std::vector<StringRef> 1772 Record::getValueAsListOfStrings(StringRef FieldName) const { 1773 ListInit *List = getValueAsListInit(FieldName); 1774 std::vector<StringRef> Strings; 1775 for (Init *I : List->getValues()) { 1776 if (StringInit *SI = dyn_cast<StringInit>(I)) 1777 Strings.push_back(SI->getValue()); 1778 else 1779 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + 1780 FieldName + "' does not have a list of strings initializer!"); 1781 } 1782 return Strings; 1783 } 1784 1785 Record *Record::getValueAsDef(StringRef FieldName) const { 1786 const RecordVal *R = getValue(FieldName); 1787 if (!R || !R->getValue()) 1788 PrintFatalError(getLoc(), "Record `" + getName() + 1789 "' does not have a field named `" + FieldName + "'!\n"); 1790 1791 if (DefInit *DI = dyn_cast<DefInit>(R->getValue())) 1792 return DI->getDef(); 1793 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + 1794 FieldName + "' does not have a def initializer!"); 1795 } 1796 1797 bool Record::getValueAsBit(StringRef FieldName) const { 1798 const RecordVal *R = getValue(FieldName); 1799 if (!R || !R->getValue()) 1800 PrintFatalError(getLoc(), "Record `" + getName() + 1801 "' does not have a field named `" + FieldName + "'!\n"); 1802 1803 if (BitInit *BI = dyn_cast<BitInit>(R->getValue())) 1804 return BI->getValue(); 1805 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + 1806 FieldName + "' does not have a bit initializer!"); 1807 } 1808 1809 bool Record::getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const { 1810 const RecordVal *R = getValue(FieldName); 1811 if (!R || !R->getValue()) 1812 PrintFatalError(getLoc(), "Record `" + getName() + 1813 "' does not have a field named `" + FieldName.str() + "'!\n"); 1814 1815 if (isa<UnsetInit>(R->getValue())) { 1816 Unset = true; 1817 return false; 1818 } 1819 Unset = false; 1820 if (BitInit *BI = dyn_cast<BitInit>(R->getValue())) 1821 return BI->getValue(); 1822 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + 1823 FieldName + "' does not have a bit initializer!"); 1824 } 1825 1826 DagInit *Record::getValueAsDag(StringRef FieldName) const { 1827 const RecordVal *R = getValue(FieldName); 1828 if (!R || !R->getValue()) 1829 PrintFatalError(getLoc(), "Record `" + getName() + 1830 "' does not have a field named `" + FieldName + "'!\n"); 1831 1832 if (DagInit *DI = dyn_cast<DagInit>(R->getValue())) 1833 return DI; 1834 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + 1835 FieldName + "' does not have a dag initializer!"); 1836 } 1837 1838 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 1839 LLVM_DUMP_METHOD void MultiClass::dump() const { 1840 errs() << "Record:\n"; 1841 Rec.dump(); 1842 1843 errs() << "Defs:\n"; 1844 for (const auto &Proto : DefPrototypes) 1845 Proto->dump(); 1846 } 1847 1848 LLVM_DUMP_METHOD void RecordKeeper::dump() const { errs() << *this; } 1849 #endif 1850 1851 raw_ostream &llvm::operator<<(raw_ostream &OS, const RecordKeeper &RK) { 1852 OS << "------------- Classes -----------------\n"; 1853 for (const auto &C : RK.getClasses()) 1854 OS << "class " << *C.second; 1855 1856 OS << "------------- Defs -----------------\n"; 1857 for (const auto &D : RK.getDefs()) 1858 OS << "def " << *D.second; 1859 return OS; 1860 } 1861 1862 std::vector<Record *> 1863 RecordKeeper::getAllDerivedDefinitions(StringRef ClassName) const { 1864 Record *Class = getClass(ClassName); 1865 if (!Class) 1866 PrintFatalError("ERROR: Couldn't find the `" + ClassName + "' class!\n"); 1867 1868 std::vector<Record*> Defs; 1869 for (const auto &D : getDefs()) 1870 if (D.second->isSubClassOf(Class)) 1871 Defs.push_back(D.second.get()); 1872 1873 return Defs; 1874 } 1875 1876 static Init *GetStrConcat(Init *I0, Init *I1) { 1877 // Shortcut for the common case of concatenating two strings. 1878 if (const StringInit *I0s = dyn_cast<StringInit>(I0)) 1879 if (const StringInit *I1s = dyn_cast<StringInit>(I1)) 1880 return ConcatStringInits(I0s, I1s); 1881 return BinOpInit::get(BinOpInit::STRCONCAT, I0, I1, StringRecTy::get()); 1882 } 1883 1884 Init *llvm::QualifyName(Record &CurRec, MultiClass *CurMultiClass, 1885 Init *Name, StringRef Scoper) { 1886 Init *NewName = GetStrConcat(CurRec.getNameInit(), StringInit::get(Scoper)); 1887 NewName = GetStrConcat(NewName, Name); 1888 if (CurMultiClass && Scoper != "::") { 1889 Init *Prefix = GetStrConcat(CurMultiClass->Rec.getNameInit(), 1890 StringInit::get("::")); 1891 NewName = GetStrConcat(Prefix, NewName); 1892 } 1893 1894 if (BinOpInit *BinOp = dyn_cast<BinOpInit>(NewName)) 1895 NewName = BinOp->Fold(&CurRec, CurMultiClass); 1896 return NewName; 1897 } 1898