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