1 //===- Record.cpp - Record implementation ---------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // Implement the tablegen record classes. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/ADT/ArrayRef.h" 14 #include "llvm/ADT/DenseMap.h" 15 #include "llvm/ADT/FoldingSet.h" 16 #include "llvm/ADT/SmallString.h" 17 #include "llvm/ADT/SmallVector.h" 18 #include "llvm/ADT/Statistic.h" 19 #include "llvm/ADT/StringExtras.h" 20 #include "llvm/ADT/StringMap.h" 21 #include "llvm/ADT/StringRef.h" 22 #include "llvm/ADT/StringSet.h" 23 #include "llvm/Config/llvm-config.h" 24 #include "llvm/Support/Allocator.h" 25 #include "llvm/Support/Casting.h" 26 #include "llvm/Support/Compiler.h" 27 #include "llvm/Support/ErrorHandling.h" 28 #include "llvm/Support/SMLoc.h" 29 #include "llvm/Support/raw_ostream.h" 30 #include "llvm/TableGen/Error.h" 31 #include "llvm/TableGen/Record.h" 32 #include <cassert> 33 #include <cstdint> 34 #include <memory> 35 #include <map> 36 #include <string> 37 #include <utility> 38 #include <vector> 39 40 using namespace llvm; 41 42 #define DEBUG_TYPE "tblgen-records" 43 44 static BumpPtrAllocator Allocator; 45 46 STATISTIC(CodeInitsConstructed, 47 "The total number of unique CodeInits constructed"); 48 49 //===----------------------------------------------------------------------===// 50 // Type implementations 51 //===----------------------------------------------------------------------===// 52 53 BitRecTy BitRecTy::Shared; 54 CodeRecTy CodeRecTy::Shared; 55 IntRecTy IntRecTy::Shared; 56 StringRecTy StringRecTy::Shared; 57 DagRecTy DagRecTy::Shared; 58 59 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 60 LLVM_DUMP_METHOD void RecTy::dump() const { print(errs()); } 61 #endif 62 63 ListRecTy *RecTy::getListTy() { 64 if (!ListTy) 65 ListTy = new(Allocator) ListRecTy(this); 66 return ListTy; 67 } 68 69 bool RecTy::typeIsConvertibleTo(const RecTy *RHS) const { 70 assert(RHS && "NULL pointer"); 71 return Kind == RHS->getRecTyKind(); 72 } 73 74 bool RecTy::typeIsA(const RecTy *RHS) const { return this == RHS; } 75 76 bool BitRecTy::typeIsConvertibleTo(const RecTy *RHS) const{ 77 if (RecTy::typeIsConvertibleTo(RHS) || RHS->getRecTyKind() == IntRecTyKind) 78 return true; 79 if (const BitsRecTy *BitsTy = dyn_cast<BitsRecTy>(RHS)) 80 return BitsTy->getNumBits() == 1; 81 return false; 82 } 83 84 BitsRecTy *BitsRecTy::get(unsigned Sz) { 85 static std::vector<BitsRecTy*> Shared; 86 if (Sz >= Shared.size()) 87 Shared.resize(Sz + 1); 88 BitsRecTy *&Ty = Shared[Sz]; 89 if (!Ty) 90 Ty = new(Allocator) BitsRecTy(Sz); 91 return Ty; 92 } 93 94 std::string BitsRecTy::getAsString() const { 95 return "bits<" + utostr(Size) + ">"; 96 } 97 98 bool BitsRecTy::typeIsConvertibleTo(const RecTy *RHS) const { 99 if (RecTy::typeIsConvertibleTo(RHS)) //argument and the sender are same type 100 return cast<BitsRecTy>(RHS)->Size == Size; 101 RecTyKind kind = RHS->getRecTyKind(); 102 return (kind == BitRecTyKind && Size == 1) || (kind == IntRecTyKind); 103 } 104 105 bool BitsRecTy::typeIsA(const RecTy *RHS) const { 106 if (const BitsRecTy *RHSb = dyn_cast<BitsRecTy>(RHS)) 107 return RHSb->Size == Size; 108 return false; 109 } 110 111 bool IntRecTy::typeIsConvertibleTo(const RecTy *RHS) const { 112 RecTyKind kind = RHS->getRecTyKind(); 113 return kind==BitRecTyKind || kind==BitsRecTyKind || kind==IntRecTyKind; 114 } 115 116 bool CodeRecTy::typeIsConvertibleTo(const RecTy *RHS) const { 117 RecTyKind Kind = RHS->getRecTyKind(); 118 return Kind == CodeRecTyKind || Kind == StringRecTyKind; 119 } 120 121 std::string StringRecTy::getAsString() const { 122 return "string"; 123 } 124 125 bool StringRecTy::typeIsConvertibleTo(const RecTy *RHS) const { 126 RecTyKind Kind = RHS->getRecTyKind(); 127 return Kind == StringRecTyKind || Kind == CodeRecTyKind; 128 } 129 130 std::string ListRecTy::getAsString() const { 131 return "list<" + ElementTy->getAsString() + ">"; 132 } 133 134 bool ListRecTy::typeIsConvertibleTo(const RecTy *RHS) const { 135 if (const auto *ListTy = dyn_cast<ListRecTy>(RHS)) 136 return ElementTy->typeIsConvertibleTo(ListTy->getElementType()); 137 return false; 138 } 139 140 bool ListRecTy::typeIsA(const RecTy *RHS) const { 141 if (const ListRecTy *RHSl = dyn_cast<ListRecTy>(RHS)) 142 return getElementType()->typeIsA(RHSl->getElementType()); 143 return false; 144 } 145 146 std::string DagRecTy::getAsString() const { 147 return "dag"; 148 } 149 150 static void ProfileRecordRecTy(FoldingSetNodeID &ID, 151 ArrayRef<Record *> Classes) { 152 ID.AddInteger(Classes.size()); 153 for (Record *R : Classes) 154 ID.AddPointer(R); 155 } 156 157 RecordRecTy *RecordRecTy::get(ArrayRef<Record *> UnsortedClasses) { 158 if (UnsortedClasses.empty()) { 159 static RecordRecTy AnyRecord(0); 160 return &AnyRecord; 161 } 162 163 FoldingSet<RecordRecTy> &ThePool = 164 UnsortedClasses[0]->getRecords().RecordTypePool; 165 166 SmallVector<Record *, 4> Classes(UnsortedClasses.begin(), 167 UnsortedClasses.end()); 168 llvm::sort(Classes, [](Record *LHS, Record *RHS) { 169 return LHS->getNameInitAsString() < RHS->getNameInitAsString(); 170 }); 171 172 FoldingSetNodeID ID; 173 ProfileRecordRecTy(ID, Classes); 174 175 void *IP = nullptr; 176 if (RecordRecTy *Ty = ThePool.FindNodeOrInsertPos(ID, IP)) 177 return Ty; 178 179 #ifndef NDEBUG 180 // Check for redundancy. 181 for (unsigned i = 0; i < Classes.size(); ++i) { 182 for (unsigned j = 0; j < Classes.size(); ++j) { 183 assert(i == j || !Classes[i]->isSubClassOf(Classes[j])); 184 } 185 assert(&Classes[0]->getRecords() == &Classes[i]->getRecords()); 186 } 187 #endif 188 189 void *Mem = Allocator.Allocate(totalSizeToAlloc<Record *>(Classes.size()), 190 alignof(RecordRecTy)); 191 RecordRecTy *Ty = new(Mem) RecordRecTy(Classes.size()); 192 std::uninitialized_copy(Classes.begin(), Classes.end(), 193 Ty->getTrailingObjects<Record *>()); 194 ThePool.InsertNode(Ty, IP); 195 return Ty; 196 } 197 198 void RecordRecTy::Profile(FoldingSetNodeID &ID) const { 199 ProfileRecordRecTy(ID, getClasses()); 200 } 201 202 std::string RecordRecTy::getAsString() const { 203 if (NumClasses == 1) 204 return getClasses()[0]->getNameInitAsString(); 205 206 std::string Str = "{"; 207 bool First = true; 208 for (Record *R : getClasses()) { 209 if (!First) 210 Str += ", "; 211 First = false; 212 Str += R->getNameInitAsString(); 213 } 214 Str += "}"; 215 return Str; 216 } 217 218 bool RecordRecTy::isSubClassOf(Record *Class) const { 219 return llvm::any_of(getClasses(), [Class](Record *MySuperClass) { 220 return MySuperClass == Class || 221 MySuperClass->isSubClassOf(Class); 222 }); 223 } 224 225 bool RecordRecTy::typeIsConvertibleTo(const RecTy *RHS) const { 226 if (this == RHS) 227 return true; 228 229 const RecordRecTy *RTy = dyn_cast<RecordRecTy>(RHS); 230 if (!RTy) 231 return false; 232 233 return llvm::all_of(RTy->getClasses(), [this](Record *TargetClass) { 234 return isSubClassOf(TargetClass); 235 }); 236 } 237 238 bool RecordRecTy::typeIsA(const RecTy *RHS) const { 239 return typeIsConvertibleTo(RHS); 240 } 241 242 static RecordRecTy *resolveRecordTypes(RecordRecTy *T1, RecordRecTy *T2) { 243 SmallVector<Record *, 4> CommonSuperClasses; 244 SmallVector<Record *, 4> Stack; 245 246 Stack.insert(Stack.end(), T1->classes_begin(), T1->classes_end()); 247 248 while (!Stack.empty()) { 249 Record *R = Stack.back(); 250 Stack.pop_back(); 251 252 if (T2->isSubClassOf(R)) { 253 CommonSuperClasses.push_back(R); 254 } else { 255 R->getDirectSuperClasses(Stack); 256 } 257 } 258 259 return RecordRecTy::get(CommonSuperClasses); 260 } 261 262 RecTy *llvm::resolveTypes(RecTy *T1, RecTy *T2) { 263 if (T1 == T2) 264 return T1; 265 266 if (RecordRecTy *RecTy1 = dyn_cast<RecordRecTy>(T1)) { 267 if (RecordRecTy *RecTy2 = dyn_cast<RecordRecTy>(T2)) 268 return resolveRecordTypes(RecTy1, RecTy2); 269 } 270 271 if (T1->typeIsConvertibleTo(T2)) 272 return T2; 273 if (T2->typeIsConvertibleTo(T1)) 274 return T1; 275 276 if (ListRecTy *ListTy1 = dyn_cast<ListRecTy>(T1)) { 277 if (ListRecTy *ListTy2 = dyn_cast<ListRecTy>(T2)) { 278 RecTy* NewType = resolveTypes(ListTy1->getElementType(), 279 ListTy2->getElementType()); 280 if (NewType) 281 return NewType->getListTy(); 282 } 283 } 284 285 return nullptr; 286 } 287 288 //===----------------------------------------------------------------------===// 289 // Initializer implementations 290 //===----------------------------------------------------------------------===// 291 292 void Init::anchor() {} 293 294 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 295 LLVM_DUMP_METHOD void Init::dump() const { return print(errs()); } 296 #endif 297 298 UnsetInit *UnsetInit::get() { 299 static UnsetInit TheInit; 300 return &TheInit; 301 } 302 303 Init *UnsetInit::getCastTo(RecTy *Ty) const { 304 return const_cast<UnsetInit *>(this); 305 } 306 307 Init *UnsetInit::convertInitializerTo(RecTy *Ty) const { 308 return const_cast<UnsetInit *>(this); 309 } 310 311 BitInit *BitInit::get(bool V) { 312 static BitInit True(true); 313 static BitInit False(false); 314 315 return V ? &True : &False; 316 } 317 318 Init *BitInit::convertInitializerTo(RecTy *Ty) const { 319 if (isa<BitRecTy>(Ty)) 320 return const_cast<BitInit *>(this); 321 322 if (isa<IntRecTy>(Ty)) 323 return IntInit::get(getValue()); 324 325 if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) { 326 // Can only convert single bit. 327 if (BRT->getNumBits() == 1) 328 return BitsInit::get(const_cast<BitInit *>(this)); 329 } 330 331 return nullptr; 332 } 333 334 static void 335 ProfileBitsInit(FoldingSetNodeID &ID, ArrayRef<Init *> Range) { 336 ID.AddInteger(Range.size()); 337 338 for (Init *I : Range) 339 ID.AddPointer(I); 340 } 341 342 BitsInit *BitsInit::get(ArrayRef<Init *> Range) { 343 static FoldingSet<BitsInit> ThePool; 344 345 FoldingSetNodeID ID; 346 ProfileBitsInit(ID, Range); 347 348 void *IP = nullptr; 349 if (BitsInit *I = ThePool.FindNodeOrInsertPos(ID, IP)) 350 return I; 351 352 void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *>(Range.size()), 353 alignof(BitsInit)); 354 BitsInit *I = new(Mem) BitsInit(Range.size()); 355 std::uninitialized_copy(Range.begin(), Range.end(), 356 I->getTrailingObjects<Init *>()); 357 ThePool.InsertNode(I, IP); 358 return I; 359 } 360 361 void BitsInit::Profile(FoldingSetNodeID &ID) const { 362 ProfileBitsInit(ID, makeArrayRef(getTrailingObjects<Init *>(), NumBits)); 363 } 364 365 Init *BitsInit::convertInitializerTo(RecTy *Ty) const { 366 if (isa<BitRecTy>(Ty)) { 367 if (getNumBits() != 1) return nullptr; // Only accept if just one bit! 368 return getBit(0); 369 } 370 371 if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) { 372 // If the number of bits is right, return it. Otherwise we need to expand 373 // or truncate. 374 if (getNumBits() != BRT->getNumBits()) return nullptr; 375 return const_cast<BitsInit *>(this); 376 } 377 378 if (isa<IntRecTy>(Ty)) { 379 int64_t Result = 0; 380 for (unsigned i = 0, e = getNumBits(); i != e; ++i) 381 if (auto *Bit = dyn_cast<BitInit>(getBit(i))) 382 Result |= static_cast<int64_t>(Bit->getValue()) << i; 383 else 384 return nullptr; 385 return IntInit::get(Result); 386 } 387 388 return nullptr; 389 } 390 391 Init * 392 BitsInit::convertInitializerBitRange(ArrayRef<unsigned> Bits) const { 393 SmallVector<Init *, 16> NewBits(Bits.size()); 394 395 for (unsigned i = 0, e = Bits.size(); i != e; ++i) { 396 if (Bits[i] >= getNumBits()) 397 return nullptr; 398 NewBits[i] = getBit(Bits[i]); 399 } 400 return BitsInit::get(NewBits); 401 } 402 403 bool BitsInit::isConcrete() const { 404 for (unsigned i = 0, e = getNumBits(); i != e; ++i) { 405 if (!getBit(i)->isConcrete()) 406 return false; 407 } 408 return true; 409 } 410 411 std::string BitsInit::getAsString() const { 412 std::string Result = "{ "; 413 for (unsigned i = 0, e = getNumBits(); i != e; ++i) { 414 if (i) Result += ", "; 415 if (Init *Bit = getBit(e-i-1)) 416 Result += Bit->getAsString(); 417 else 418 Result += "*"; 419 } 420 return Result + " }"; 421 } 422 423 // resolveReferences - If there are any field references that refer to fields 424 // that have been filled in, we can propagate the values now. 425 Init *BitsInit::resolveReferences(Resolver &R) const { 426 bool Changed = false; 427 SmallVector<Init *, 16> NewBits(getNumBits()); 428 429 Init *CachedBitVarRef = nullptr; 430 Init *CachedBitVarResolved = nullptr; 431 432 for (unsigned i = 0, e = getNumBits(); i != e; ++i) { 433 Init *CurBit = getBit(i); 434 Init *NewBit = CurBit; 435 436 if (VarBitInit *CurBitVar = dyn_cast<VarBitInit>(CurBit)) { 437 if (CurBitVar->getBitVar() != CachedBitVarRef) { 438 CachedBitVarRef = CurBitVar->getBitVar(); 439 CachedBitVarResolved = CachedBitVarRef->resolveReferences(R); 440 } 441 assert(CachedBitVarResolved && "Unresolved bitvar reference"); 442 NewBit = CachedBitVarResolved->getBit(CurBitVar->getBitNum()); 443 } else { 444 // getBit(0) implicitly converts int and bits<1> values to bit. 445 NewBit = CurBit->resolveReferences(R)->getBit(0); 446 } 447 448 if (isa<UnsetInit>(NewBit) && R.keepUnsetBits()) 449 NewBit = CurBit; 450 NewBits[i] = NewBit; 451 Changed |= CurBit != NewBit; 452 } 453 454 if (Changed) 455 return BitsInit::get(NewBits); 456 457 return const_cast<BitsInit *>(this); 458 } 459 460 IntInit *IntInit::get(int64_t V) { 461 static std::map<int64_t, IntInit*> ThePool; 462 463 IntInit *&I = ThePool[V]; 464 if (!I) I = new(Allocator) IntInit(V); 465 return I; 466 } 467 468 std::string IntInit::getAsString() const { 469 return itostr(Value); 470 } 471 472 static bool canFitInBitfield(int64_t Value, unsigned NumBits) { 473 // For example, with NumBits == 4, we permit Values from [-7 .. 15]. 474 return (NumBits >= sizeof(Value) * 8) || 475 (Value >> NumBits == 0) || (Value >> (NumBits-1) == -1); 476 } 477 478 Init *IntInit::convertInitializerTo(RecTy *Ty) const { 479 if (isa<IntRecTy>(Ty)) 480 return const_cast<IntInit *>(this); 481 482 if (isa<BitRecTy>(Ty)) { 483 int64_t Val = getValue(); 484 if (Val != 0 && Val != 1) return nullptr; // Only accept 0 or 1 for a bit! 485 return BitInit::get(Val != 0); 486 } 487 488 if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) { 489 int64_t Value = getValue(); 490 // Make sure this bitfield is large enough to hold the integer value. 491 if (!canFitInBitfield(Value, BRT->getNumBits())) 492 return nullptr; 493 494 SmallVector<Init *, 16> NewBits(BRT->getNumBits()); 495 for (unsigned i = 0; i != BRT->getNumBits(); ++i) 496 NewBits[i] = BitInit::get(Value & ((i < 64) ? (1LL << i) : 0)); 497 498 return BitsInit::get(NewBits); 499 } 500 501 return nullptr; 502 } 503 504 Init * 505 IntInit::convertInitializerBitRange(ArrayRef<unsigned> Bits) const { 506 SmallVector<Init *, 16> NewBits(Bits.size()); 507 508 for (unsigned i = 0, e = Bits.size(); i != e; ++i) { 509 if (Bits[i] >= 64) 510 return nullptr; 511 512 NewBits[i] = BitInit::get(Value & (INT64_C(1) << Bits[i])); 513 } 514 return BitsInit::get(NewBits); 515 } 516 517 CodeInit *CodeInit::get(StringRef V, const SMLoc &Loc) { 518 static StringSet<BumpPtrAllocator &> ThePool(Allocator); 519 520 CodeInitsConstructed++; 521 522 // Unlike StringMap, StringSet doesn't accept empty keys. 523 if (V.empty()) 524 return new (Allocator) CodeInit("", Loc); 525 526 // Location tracking prevents us from de-duping CodeInits as we're never 527 // called with the same string and same location twice. However, we can at 528 // least de-dupe the strings for a modest saving. 529 auto &Entry = *ThePool.insert(V).first; 530 return new(Allocator) CodeInit(Entry.getKey(), Loc); 531 } 532 533 StringInit *StringInit::get(StringRef V) { 534 static StringMap<StringInit*, BumpPtrAllocator &> ThePool(Allocator); 535 536 auto &Entry = *ThePool.insert(std::make_pair(V, nullptr)).first; 537 if (!Entry.second) 538 Entry.second = new(Allocator) StringInit(Entry.getKey()); 539 return Entry.second; 540 } 541 542 Init *StringInit::convertInitializerTo(RecTy *Ty) const { 543 if (isa<StringRecTy>(Ty)) 544 return const_cast<StringInit *>(this); 545 if (isa<CodeRecTy>(Ty)) 546 return CodeInit::get(getValue(), SMLoc()); 547 548 return nullptr; 549 } 550 551 Init *CodeInit::convertInitializerTo(RecTy *Ty) const { 552 if (isa<CodeRecTy>(Ty)) 553 return const_cast<CodeInit *>(this); 554 if (isa<StringRecTy>(Ty)) 555 return StringInit::get(getValue()); 556 557 return nullptr; 558 } 559 560 static void ProfileListInit(FoldingSetNodeID &ID, 561 ArrayRef<Init *> Range, 562 RecTy *EltTy) { 563 ID.AddInteger(Range.size()); 564 ID.AddPointer(EltTy); 565 566 for (Init *I : Range) 567 ID.AddPointer(I); 568 } 569 570 ListInit *ListInit::get(ArrayRef<Init *> Range, RecTy *EltTy) { 571 static FoldingSet<ListInit> ThePool; 572 573 FoldingSetNodeID ID; 574 ProfileListInit(ID, Range, EltTy); 575 576 void *IP = nullptr; 577 if (ListInit *I = ThePool.FindNodeOrInsertPos(ID, IP)) 578 return I; 579 580 assert(Range.empty() || !isa<TypedInit>(Range[0]) || 581 cast<TypedInit>(Range[0])->getType()->typeIsConvertibleTo(EltTy)); 582 583 void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *>(Range.size()), 584 alignof(ListInit)); 585 ListInit *I = new(Mem) ListInit(Range.size(), EltTy); 586 std::uninitialized_copy(Range.begin(), Range.end(), 587 I->getTrailingObjects<Init *>()); 588 ThePool.InsertNode(I, IP); 589 return I; 590 } 591 592 void ListInit::Profile(FoldingSetNodeID &ID) const { 593 RecTy *EltTy = cast<ListRecTy>(getType())->getElementType(); 594 595 ProfileListInit(ID, getValues(), EltTy); 596 } 597 598 Init *ListInit::convertInitializerTo(RecTy *Ty) const { 599 if (getType() == Ty) 600 return const_cast<ListInit*>(this); 601 602 if (auto *LRT = dyn_cast<ListRecTy>(Ty)) { 603 SmallVector<Init*, 8> Elements; 604 Elements.reserve(getValues().size()); 605 606 // Verify that all of the elements of the list are subclasses of the 607 // appropriate class! 608 bool Changed = false; 609 RecTy *ElementType = LRT->getElementType(); 610 for (Init *I : getValues()) 611 if (Init *CI = I->convertInitializerTo(ElementType)) { 612 Elements.push_back(CI); 613 if (CI != I) 614 Changed = true; 615 } else 616 return nullptr; 617 618 if (!Changed) 619 return const_cast<ListInit*>(this); 620 return ListInit::get(Elements, ElementType); 621 } 622 623 return nullptr; 624 } 625 626 Init *ListInit::convertInitListSlice(ArrayRef<unsigned> Elements) const { 627 SmallVector<Init*, 8> Vals; 628 Vals.reserve(Elements.size()); 629 for (unsigned Element : Elements) { 630 if (Element >= size()) 631 return nullptr; 632 Vals.push_back(getElement(Element)); 633 } 634 return ListInit::get(Vals, getElementType()); 635 } 636 637 Record *ListInit::getElementAsRecord(unsigned i) const { 638 assert(i < NumValues && "List element index out of range!"); 639 DefInit *DI = dyn_cast<DefInit>(getElement(i)); 640 if (!DI) 641 PrintFatalError("Expected record in list!"); 642 return DI->getDef(); 643 } 644 645 Init *ListInit::resolveReferences(Resolver &R) const { 646 SmallVector<Init*, 8> Resolved; 647 Resolved.reserve(size()); 648 bool Changed = false; 649 650 for (Init *CurElt : getValues()) { 651 Init *E = CurElt->resolveReferences(R); 652 Changed |= E != CurElt; 653 Resolved.push_back(E); 654 } 655 656 if (Changed) 657 return ListInit::get(Resolved, getElementType()); 658 return const_cast<ListInit *>(this); 659 } 660 661 bool ListInit::isConcrete() const { 662 for (Init *Element : *this) { 663 if (!Element->isConcrete()) 664 return false; 665 } 666 return true; 667 } 668 669 std::string ListInit::getAsString() const { 670 std::string Result = "["; 671 const char *sep = ""; 672 for (Init *Element : *this) { 673 Result += sep; 674 sep = ", "; 675 Result += Element->getAsString(); 676 } 677 return Result + "]"; 678 } 679 680 Init *OpInit::getBit(unsigned Bit) const { 681 if (getType() == BitRecTy::get()) 682 return const_cast<OpInit*>(this); 683 return VarBitInit::get(const_cast<OpInit*>(this), Bit); 684 } 685 686 static void 687 ProfileUnOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *Op, RecTy *Type) { 688 ID.AddInteger(Opcode); 689 ID.AddPointer(Op); 690 ID.AddPointer(Type); 691 } 692 693 UnOpInit *UnOpInit::get(UnaryOp Opc, Init *LHS, RecTy *Type) { 694 static FoldingSet<UnOpInit> ThePool; 695 696 FoldingSetNodeID ID; 697 ProfileUnOpInit(ID, Opc, LHS, Type); 698 699 void *IP = nullptr; 700 if (UnOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP)) 701 return I; 702 703 UnOpInit *I = new(Allocator) UnOpInit(Opc, LHS, Type); 704 ThePool.InsertNode(I, IP); 705 return I; 706 } 707 708 void UnOpInit::Profile(FoldingSetNodeID &ID) const { 709 ProfileUnOpInit(ID, getOpcode(), getOperand(), getType()); 710 } 711 712 Init *UnOpInit::Fold(Record *CurRec, bool IsFinal) const { 713 switch (getOpcode()) { 714 case CAST: 715 if (isa<StringRecTy>(getType())) { 716 if (StringInit *LHSs = dyn_cast<StringInit>(LHS)) 717 return LHSs; 718 719 if (DefInit *LHSd = dyn_cast<DefInit>(LHS)) 720 return StringInit::get(LHSd->getAsString()); 721 722 if (IntInit *LHSi = dyn_cast<IntInit>(LHS)) 723 return StringInit::get(LHSi->getAsString()); 724 } else if (isa<RecordRecTy>(getType())) { 725 if (StringInit *Name = dyn_cast<StringInit>(LHS)) { 726 if (!CurRec && !IsFinal) 727 break; 728 assert(CurRec && "NULL pointer"); 729 Record *D; 730 731 // Self-references are allowed, but their resolution is delayed until 732 // the final resolve to ensure that we get the correct type for them. 733 if (Name == CurRec->getNameInit()) { 734 if (!IsFinal) 735 break; 736 D = CurRec; 737 } else { 738 D = CurRec->getRecords().getDef(Name->getValue()); 739 if (!D) { 740 if (IsFinal) 741 PrintFatalError(CurRec->getLoc(), 742 Twine("Undefined reference to record: '") + 743 Name->getValue() + "'\n"); 744 break; 745 } 746 } 747 748 DefInit *DI = DefInit::get(D); 749 if (!DI->getType()->typeIsA(getType())) { 750 PrintFatalError(CurRec->getLoc(), 751 Twine("Expected type '") + 752 getType()->getAsString() + "', got '" + 753 DI->getType()->getAsString() + "' in: " + 754 getAsString() + "\n"); 755 } 756 return DI; 757 } 758 } 759 760 if (Init *NewInit = LHS->convertInitializerTo(getType())) 761 return NewInit; 762 break; 763 764 case NOT: 765 if (IntInit *LHSi = 766 dyn_cast_or_null<IntInit>(LHS->convertInitializerTo(IntRecTy::get()))) 767 return IntInit::get(LHSi->getValue() ? 0 : 1); 768 break; 769 770 case HEAD: 771 if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) { 772 assert(!LHSl->empty() && "Empty list in head"); 773 return LHSl->getElement(0); 774 } 775 break; 776 777 case TAIL: 778 if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) { 779 assert(!LHSl->empty() && "Empty list in tail"); 780 // Note the +1. We can't just pass the result of getValues() 781 // directly. 782 return ListInit::get(LHSl->getValues().slice(1), LHSl->getElementType()); 783 } 784 break; 785 786 case SIZE: 787 if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) 788 return IntInit::get(LHSl->size()); 789 if (DagInit *LHSd = dyn_cast<DagInit>(LHS)) 790 return IntInit::get(LHSd->arg_size()); 791 if (StringInit *LHSs = dyn_cast<StringInit>(LHS)) 792 return IntInit::get(LHSs->getValue().size()); 793 break; 794 795 case EMPTY: 796 if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) 797 return IntInit::get(LHSl->empty()); 798 if (DagInit *LHSd = dyn_cast<DagInit>(LHS)) 799 return IntInit::get(LHSd->arg_empty()); 800 if (StringInit *LHSs = dyn_cast<StringInit>(LHS)) 801 return IntInit::get(LHSs->getValue().empty()); 802 break; 803 804 case GETDAGOP: 805 if (DagInit *Dag = dyn_cast<DagInit>(LHS)) { 806 DefInit *DI = DefInit::get(Dag->getOperatorAsDef({})); 807 if (!DI->getType()->typeIsA(getType())) { 808 PrintFatalError(CurRec->getLoc(), 809 Twine("Expected type '") + 810 getType()->getAsString() + "', got '" + 811 DI->getType()->getAsString() + "' in: " + 812 getAsString() + "\n"); 813 } else { 814 return DI; 815 } 816 } 817 break; 818 } 819 return const_cast<UnOpInit *>(this); 820 } 821 822 Init *UnOpInit::resolveReferences(Resolver &R) const { 823 Init *lhs = LHS->resolveReferences(R); 824 825 if (LHS != lhs || (R.isFinal() && getOpcode() == CAST)) 826 return (UnOpInit::get(getOpcode(), lhs, getType())) 827 ->Fold(R.getCurrentRecord(), R.isFinal()); 828 return const_cast<UnOpInit *>(this); 829 } 830 831 std::string UnOpInit::getAsString() const { 832 std::string Result; 833 switch (getOpcode()) { 834 case CAST: Result = "!cast<" + getType()->getAsString() + ">"; break; 835 case NOT: Result = "!not"; break; 836 case HEAD: Result = "!head"; break; 837 case TAIL: Result = "!tail"; break; 838 case SIZE: Result = "!size"; break; 839 case EMPTY: Result = "!empty"; break; 840 case GETDAGOP: Result = "!getdagop"; break; 841 } 842 return Result + "(" + LHS->getAsString() + ")"; 843 } 844 845 static void 846 ProfileBinOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *LHS, Init *RHS, 847 RecTy *Type) { 848 ID.AddInteger(Opcode); 849 ID.AddPointer(LHS); 850 ID.AddPointer(RHS); 851 ID.AddPointer(Type); 852 } 853 854 BinOpInit *BinOpInit::get(BinaryOp Opc, Init *LHS, 855 Init *RHS, RecTy *Type) { 856 static FoldingSet<BinOpInit> ThePool; 857 858 FoldingSetNodeID ID; 859 ProfileBinOpInit(ID, Opc, LHS, RHS, Type); 860 861 void *IP = nullptr; 862 if (BinOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP)) 863 return I; 864 865 BinOpInit *I = new(Allocator) BinOpInit(Opc, LHS, RHS, Type); 866 ThePool.InsertNode(I, IP); 867 return I; 868 } 869 870 void BinOpInit::Profile(FoldingSetNodeID &ID) const { 871 ProfileBinOpInit(ID, getOpcode(), getLHS(), getRHS(), getType()); 872 } 873 874 static StringInit *ConcatStringInits(const StringInit *I0, 875 const StringInit *I1) { 876 SmallString<80> Concat(I0->getValue()); 877 Concat.append(I1->getValue()); 878 return StringInit::get(Concat); 879 } 880 881 static StringInit *interleaveStringList(const ListInit *List, 882 const StringInit *Delim) { 883 if (List->size() == 0) 884 return StringInit::get(""); 885 SmallString<80> Result(dyn_cast<StringInit>(List->getElement(0))->getValue()); 886 887 for (unsigned I = 1, E = List->size(); I < E; ++I) { 888 Result.append(Delim->getValue()); 889 Result.append(dyn_cast<StringInit>(List->getElement(I))->getValue()); 890 } 891 return StringInit::get(Result); 892 } 893 894 static StringInit *interleaveIntList(const ListInit *List, 895 const StringInit *Delim) { 896 if (List->size() == 0) 897 return StringInit::get(""); 898 SmallString<80> Result(dyn_cast<IntInit>(List->getElement(0)-> 899 getCastTo(IntRecTy::get()))->getAsString()); 900 901 for (unsigned I = 1, E = List->size(); I < E; ++I) { 902 Result.append(Delim->getValue()); 903 Result.append(dyn_cast<IntInit>(List->getElement(I)-> 904 getCastTo(IntRecTy::get()))->getAsString()); 905 } 906 return StringInit::get(Result); 907 } 908 909 Init *BinOpInit::getStrConcat(Init *I0, Init *I1) { 910 // Shortcut for the common case of concatenating two strings. 911 if (const StringInit *I0s = dyn_cast<StringInit>(I0)) 912 if (const StringInit *I1s = dyn_cast<StringInit>(I1)) 913 return ConcatStringInits(I0s, I1s); 914 return BinOpInit::get(BinOpInit::STRCONCAT, I0, I1, StringRecTy::get()); 915 } 916 917 static ListInit *ConcatListInits(const ListInit *LHS, 918 const ListInit *RHS) { 919 SmallVector<Init *, 8> Args; 920 Args.insert(Args.end(), LHS->begin(), LHS->end()); 921 Args.insert(Args.end(), RHS->begin(), RHS->end()); 922 return ListInit::get(Args, LHS->getElementType()); 923 } 924 925 Init *BinOpInit::getListConcat(TypedInit *LHS, Init *RHS) { 926 assert(isa<ListRecTy>(LHS->getType()) && "First arg must be a list"); 927 928 // Shortcut for the common case of concatenating two lists. 929 if (const ListInit *LHSList = dyn_cast<ListInit>(LHS)) 930 if (const ListInit *RHSList = dyn_cast<ListInit>(RHS)) 931 return ConcatListInits(LHSList, RHSList); 932 return BinOpInit::get(BinOpInit::LISTCONCAT, LHS, RHS, LHS->getType()); 933 } 934 935 Init *BinOpInit::Fold(Record *CurRec) const { 936 switch (getOpcode()) { 937 case CONCAT: { 938 DagInit *LHSs = dyn_cast<DagInit>(LHS); 939 DagInit *RHSs = dyn_cast<DagInit>(RHS); 940 if (LHSs && RHSs) { 941 DefInit *LOp = dyn_cast<DefInit>(LHSs->getOperator()); 942 DefInit *ROp = dyn_cast<DefInit>(RHSs->getOperator()); 943 if ((!LOp && !isa<UnsetInit>(LHSs->getOperator())) || 944 (!ROp && !isa<UnsetInit>(RHSs->getOperator()))) 945 break; 946 if (LOp && ROp && LOp->getDef() != ROp->getDef()) { 947 PrintFatalError(Twine("Concatenated Dag operators do not match: '") + 948 LHSs->getAsString() + "' vs. '" + RHSs->getAsString() + 949 "'"); 950 } 951 Init *Op = LOp ? LOp : ROp; 952 if (!Op) 953 Op = UnsetInit::get(); 954 955 SmallVector<Init*, 8> Args; 956 SmallVector<StringInit*, 8> ArgNames; 957 for (unsigned i = 0, e = LHSs->getNumArgs(); i != e; ++i) { 958 Args.push_back(LHSs->getArg(i)); 959 ArgNames.push_back(LHSs->getArgName(i)); 960 } 961 for (unsigned i = 0, e = RHSs->getNumArgs(); i != e; ++i) { 962 Args.push_back(RHSs->getArg(i)); 963 ArgNames.push_back(RHSs->getArgName(i)); 964 } 965 return DagInit::get(Op, nullptr, Args, ArgNames); 966 } 967 break; 968 } 969 case LISTCONCAT: { 970 ListInit *LHSs = dyn_cast<ListInit>(LHS); 971 ListInit *RHSs = dyn_cast<ListInit>(RHS); 972 if (LHSs && RHSs) { 973 SmallVector<Init *, 8> Args; 974 Args.insert(Args.end(), LHSs->begin(), LHSs->end()); 975 Args.insert(Args.end(), RHSs->begin(), RHSs->end()); 976 return ListInit::get(Args, LHSs->getElementType()); 977 } 978 break; 979 } 980 case LISTSPLAT: { 981 TypedInit *Value = dyn_cast<TypedInit>(LHS); 982 IntInit *Size = dyn_cast<IntInit>(RHS); 983 if (Value && Size) { 984 SmallVector<Init *, 8> Args(Size->getValue(), Value); 985 return ListInit::get(Args, Value->getType()); 986 } 987 break; 988 } 989 case STRCONCAT: { 990 StringInit *LHSs = dyn_cast<StringInit>(LHS); 991 StringInit *RHSs = dyn_cast<StringInit>(RHS); 992 if (LHSs && RHSs) 993 return ConcatStringInits(LHSs, RHSs); 994 break; 995 } 996 case INTERLEAVE: { 997 ListInit *List = dyn_cast<ListInit>(LHS); 998 StringInit *Delim = dyn_cast<StringInit>(RHS); 999 if (List && Delim) { 1000 if (isa<StringRecTy>(List->getElementType())) 1001 return interleaveStringList(List, Delim); 1002 else 1003 return interleaveIntList(List, Delim); 1004 } 1005 break; 1006 } 1007 case EQ: 1008 case NE: 1009 case LE: 1010 case LT: 1011 case GE: 1012 case GT: { 1013 // try to fold eq comparison for 'bit' and 'int', otherwise fallback 1014 // to string objects. 1015 IntInit *L = 1016 dyn_cast_or_null<IntInit>(LHS->convertInitializerTo(IntRecTy::get())); 1017 IntInit *R = 1018 dyn_cast_or_null<IntInit>(RHS->convertInitializerTo(IntRecTy::get())); 1019 1020 if (L && R) { 1021 bool Result; 1022 switch (getOpcode()) { 1023 case EQ: Result = L->getValue() == R->getValue(); break; 1024 case NE: Result = L->getValue() != R->getValue(); break; 1025 case LE: Result = L->getValue() <= R->getValue(); break; 1026 case LT: Result = L->getValue() < R->getValue(); break; 1027 case GE: Result = L->getValue() >= R->getValue(); break; 1028 case GT: Result = L->getValue() > R->getValue(); break; 1029 default: llvm_unreachable("unhandled comparison"); 1030 } 1031 return BitInit::get(Result); 1032 } 1033 1034 if (getOpcode() == EQ || getOpcode() == NE) { 1035 StringInit *LHSs = dyn_cast<StringInit>(LHS); 1036 StringInit *RHSs = dyn_cast<StringInit>(RHS); 1037 1038 // Make sure we've resolved 1039 if (LHSs && RHSs) { 1040 bool Equal = LHSs->getValue() == RHSs->getValue(); 1041 return BitInit::get(getOpcode() == EQ ? Equal : !Equal); 1042 } 1043 } 1044 1045 break; 1046 } 1047 case SETDAGOP: { 1048 DagInit *Dag = dyn_cast<DagInit>(LHS); 1049 DefInit *Op = dyn_cast<DefInit>(RHS); 1050 if (Dag && Op) { 1051 SmallVector<Init*, 8> Args; 1052 SmallVector<StringInit*, 8> ArgNames; 1053 for (unsigned i = 0, e = Dag->getNumArgs(); i != e; ++i) { 1054 Args.push_back(Dag->getArg(i)); 1055 ArgNames.push_back(Dag->getArgName(i)); 1056 } 1057 return DagInit::get(Op, nullptr, Args, ArgNames); 1058 } 1059 break; 1060 } 1061 case ADD: 1062 case SUB: 1063 case MUL: 1064 case AND: 1065 case OR: 1066 case XOR: 1067 case SHL: 1068 case SRA: 1069 case SRL: { 1070 IntInit *LHSi = 1071 dyn_cast_or_null<IntInit>(LHS->convertInitializerTo(IntRecTy::get())); 1072 IntInit *RHSi = 1073 dyn_cast_or_null<IntInit>(RHS->convertInitializerTo(IntRecTy::get())); 1074 if (LHSi && RHSi) { 1075 int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue(); 1076 int64_t Result; 1077 switch (getOpcode()) { 1078 default: llvm_unreachable("Bad opcode!"); 1079 case ADD: Result = LHSv + RHSv; break; 1080 case SUB: Result = LHSv - RHSv; break; 1081 case MUL: Result = LHSv * RHSv; break; 1082 case AND: Result = LHSv & RHSv; break; 1083 case OR: Result = LHSv | RHSv; break; 1084 case XOR: Result = LHSv ^ RHSv; break; 1085 case SHL: Result = (uint64_t)LHSv << (uint64_t)RHSv; break; 1086 case SRA: Result = LHSv >> RHSv; break; 1087 case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break; 1088 } 1089 return IntInit::get(Result); 1090 } 1091 break; 1092 } 1093 } 1094 return const_cast<BinOpInit *>(this); 1095 } 1096 1097 Init *BinOpInit::resolveReferences(Resolver &R) const { 1098 Init *lhs = LHS->resolveReferences(R); 1099 Init *rhs = RHS->resolveReferences(R); 1100 1101 if (LHS != lhs || RHS != rhs) 1102 return (BinOpInit::get(getOpcode(), lhs, rhs, getType())) 1103 ->Fold(R.getCurrentRecord()); 1104 return const_cast<BinOpInit *>(this); 1105 } 1106 1107 std::string BinOpInit::getAsString() const { 1108 std::string Result; 1109 switch (getOpcode()) { 1110 case CONCAT: Result = "!con"; break; 1111 case ADD: Result = "!add"; break; 1112 case SUB: Result = "!sub"; break; 1113 case MUL: Result = "!mul"; break; 1114 case AND: Result = "!and"; break; 1115 case OR: Result = "!or"; break; 1116 case XOR: Result = "!xor"; break; 1117 case SHL: Result = "!shl"; break; 1118 case SRA: Result = "!sra"; break; 1119 case SRL: Result = "!srl"; break; 1120 case EQ: Result = "!eq"; break; 1121 case NE: Result = "!ne"; break; 1122 case LE: Result = "!le"; break; 1123 case LT: Result = "!lt"; break; 1124 case GE: Result = "!ge"; break; 1125 case GT: Result = "!gt"; break; 1126 case LISTCONCAT: Result = "!listconcat"; break; 1127 case LISTSPLAT: Result = "!listsplat"; break; 1128 case STRCONCAT: Result = "!strconcat"; break; 1129 case INTERLEAVE: Result = "!interleave"; break; 1130 case SETDAGOP: Result = "!setdagop"; break; 1131 } 1132 return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")"; 1133 } 1134 1135 static void 1136 ProfileTernOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *LHS, Init *MHS, 1137 Init *RHS, RecTy *Type) { 1138 ID.AddInteger(Opcode); 1139 ID.AddPointer(LHS); 1140 ID.AddPointer(MHS); 1141 ID.AddPointer(RHS); 1142 ID.AddPointer(Type); 1143 } 1144 1145 TernOpInit *TernOpInit::get(TernaryOp Opc, Init *LHS, Init *MHS, Init *RHS, 1146 RecTy *Type) { 1147 static FoldingSet<TernOpInit> ThePool; 1148 1149 FoldingSetNodeID ID; 1150 ProfileTernOpInit(ID, Opc, LHS, MHS, RHS, Type); 1151 1152 void *IP = nullptr; 1153 if (TernOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP)) 1154 return I; 1155 1156 TernOpInit *I = new(Allocator) TernOpInit(Opc, LHS, MHS, RHS, Type); 1157 ThePool.InsertNode(I, IP); 1158 return I; 1159 } 1160 1161 void TernOpInit::Profile(FoldingSetNodeID &ID) const { 1162 ProfileTernOpInit(ID, getOpcode(), getLHS(), getMHS(), getRHS(), getType()); 1163 } 1164 1165 static Init *ForeachApply(Init *LHS, Init *MHSe, Init *RHS, Record *CurRec) { 1166 MapResolver R(CurRec); 1167 R.set(LHS, MHSe); 1168 return RHS->resolveReferences(R); 1169 } 1170 1171 static Init *ForeachDagApply(Init *LHS, DagInit *MHSd, Init *RHS, 1172 Record *CurRec) { 1173 bool Change = false; 1174 Init *Val = ForeachApply(LHS, MHSd->getOperator(), RHS, CurRec); 1175 if (Val != MHSd->getOperator()) 1176 Change = true; 1177 1178 SmallVector<std::pair<Init *, StringInit *>, 8> NewArgs; 1179 for (unsigned int i = 0; i < MHSd->getNumArgs(); ++i) { 1180 Init *Arg = MHSd->getArg(i); 1181 Init *NewArg; 1182 StringInit *ArgName = MHSd->getArgName(i); 1183 1184 if (DagInit *Argd = dyn_cast<DagInit>(Arg)) 1185 NewArg = ForeachDagApply(LHS, Argd, RHS, CurRec); 1186 else 1187 NewArg = ForeachApply(LHS, Arg, RHS, CurRec); 1188 1189 NewArgs.push_back(std::make_pair(NewArg, ArgName)); 1190 if (Arg != NewArg) 1191 Change = true; 1192 } 1193 1194 if (Change) 1195 return DagInit::get(Val, nullptr, NewArgs); 1196 return MHSd; 1197 } 1198 1199 // Applies RHS to all elements of MHS, using LHS as a temp variable. 1200 static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type, 1201 Record *CurRec) { 1202 if (DagInit *MHSd = dyn_cast<DagInit>(MHS)) 1203 return ForeachDagApply(LHS, MHSd, RHS, CurRec); 1204 1205 if (ListInit *MHSl = dyn_cast<ListInit>(MHS)) { 1206 SmallVector<Init *, 8> NewList(MHSl->begin(), MHSl->end()); 1207 1208 for (Init *&Item : NewList) { 1209 Init *NewItem = ForeachApply(LHS, Item, RHS, CurRec); 1210 if (NewItem != Item) 1211 Item = NewItem; 1212 } 1213 return ListInit::get(NewList, cast<ListRecTy>(Type)->getElementType()); 1214 } 1215 1216 return nullptr; 1217 } 1218 1219 Init *TernOpInit::Fold(Record *CurRec) const { 1220 switch (getOpcode()) { 1221 case SUBST: { 1222 DefInit *LHSd = dyn_cast<DefInit>(LHS); 1223 VarInit *LHSv = dyn_cast<VarInit>(LHS); 1224 StringInit *LHSs = dyn_cast<StringInit>(LHS); 1225 1226 DefInit *MHSd = dyn_cast<DefInit>(MHS); 1227 VarInit *MHSv = dyn_cast<VarInit>(MHS); 1228 StringInit *MHSs = dyn_cast<StringInit>(MHS); 1229 1230 DefInit *RHSd = dyn_cast<DefInit>(RHS); 1231 VarInit *RHSv = dyn_cast<VarInit>(RHS); 1232 StringInit *RHSs = dyn_cast<StringInit>(RHS); 1233 1234 if (LHSd && MHSd && RHSd) { 1235 Record *Val = RHSd->getDef(); 1236 if (LHSd->getAsString() == RHSd->getAsString()) 1237 Val = MHSd->getDef(); 1238 return DefInit::get(Val); 1239 } 1240 if (LHSv && MHSv && RHSv) { 1241 std::string Val = std::string(RHSv->getName()); 1242 if (LHSv->getAsString() == RHSv->getAsString()) 1243 Val = std::string(MHSv->getName()); 1244 return VarInit::get(Val, getType()); 1245 } 1246 if (LHSs && MHSs && RHSs) { 1247 std::string Val = std::string(RHSs->getValue()); 1248 1249 std::string::size_type found; 1250 std::string::size_type idx = 0; 1251 while (true) { 1252 found = Val.find(std::string(LHSs->getValue()), idx); 1253 if (found == std::string::npos) 1254 break; 1255 Val.replace(found, LHSs->getValue().size(), 1256 std::string(MHSs->getValue())); 1257 idx = found + MHSs->getValue().size(); 1258 } 1259 1260 return StringInit::get(Val); 1261 } 1262 break; 1263 } 1264 1265 case FOREACH: { 1266 if (Init *Result = ForeachHelper(LHS, MHS, RHS, getType(), CurRec)) 1267 return Result; 1268 break; 1269 } 1270 1271 case IF: { 1272 if (IntInit *LHSi = dyn_cast_or_null<IntInit>( 1273 LHS->convertInitializerTo(IntRecTy::get()))) { 1274 if (LHSi->getValue()) 1275 return MHS; 1276 return RHS; 1277 } 1278 break; 1279 } 1280 1281 case DAG: { 1282 ListInit *MHSl = dyn_cast<ListInit>(MHS); 1283 ListInit *RHSl = dyn_cast<ListInit>(RHS); 1284 bool MHSok = MHSl || isa<UnsetInit>(MHS); 1285 bool RHSok = RHSl || isa<UnsetInit>(RHS); 1286 1287 if (isa<UnsetInit>(MHS) && isa<UnsetInit>(RHS)) 1288 break; // Typically prevented by the parser, but might happen with template args 1289 1290 if (MHSok && RHSok && (!MHSl || !RHSl || MHSl->size() == RHSl->size())) { 1291 SmallVector<std::pair<Init *, StringInit *>, 8> Children; 1292 unsigned Size = MHSl ? MHSl->size() : RHSl->size(); 1293 for (unsigned i = 0; i != Size; ++i) { 1294 Init *Node = MHSl ? MHSl->getElement(i) : UnsetInit::get(); 1295 Init *Name = RHSl ? RHSl->getElement(i) : UnsetInit::get(); 1296 if (!isa<StringInit>(Name) && !isa<UnsetInit>(Name)) 1297 return const_cast<TernOpInit *>(this); 1298 Children.emplace_back(Node, dyn_cast<StringInit>(Name)); 1299 } 1300 return DagInit::get(LHS, nullptr, Children); 1301 } 1302 break; 1303 } 1304 } 1305 1306 return const_cast<TernOpInit *>(this); 1307 } 1308 1309 Init *TernOpInit::resolveReferences(Resolver &R) const { 1310 Init *lhs = LHS->resolveReferences(R); 1311 1312 if (getOpcode() == IF && lhs != LHS) { 1313 if (IntInit *Value = dyn_cast_or_null<IntInit>( 1314 lhs->convertInitializerTo(IntRecTy::get()))) { 1315 // Short-circuit 1316 if (Value->getValue()) 1317 return MHS->resolveReferences(R); 1318 return RHS->resolveReferences(R); 1319 } 1320 } 1321 1322 Init *mhs = MHS->resolveReferences(R); 1323 Init *rhs; 1324 1325 if (getOpcode() == FOREACH) { 1326 ShadowResolver SR(R); 1327 SR.addShadow(lhs); 1328 rhs = RHS->resolveReferences(SR); 1329 } else { 1330 rhs = RHS->resolveReferences(R); 1331 } 1332 1333 if (LHS != lhs || MHS != mhs || RHS != rhs) 1334 return (TernOpInit::get(getOpcode(), lhs, mhs, rhs, getType())) 1335 ->Fold(R.getCurrentRecord()); 1336 return const_cast<TernOpInit *>(this); 1337 } 1338 1339 std::string TernOpInit::getAsString() const { 1340 std::string Result; 1341 bool UnquotedLHS = false; 1342 switch (getOpcode()) { 1343 case SUBST: Result = "!subst"; break; 1344 case FOREACH: Result = "!foreach"; UnquotedLHS = true; break; 1345 case IF: Result = "!if"; break; 1346 case DAG: Result = "!dag"; break; 1347 } 1348 return (Result + "(" + 1349 (UnquotedLHS ? LHS->getAsUnquotedString() : LHS->getAsString()) + 1350 ", " + MHS->getAsString() + ", " + RHS->getAsString() + ")"); 1351 } 1352 1353 static void ProfileFoldOpInit(FoldingSetNodeID &ID, Init *A, Init *B, 1354 Init *Start, Init *List, Init *Expr, 1355 RecTy *Type) { 1356 ID.AddPointer(Start); 1357 ID.AddPointer(List); 1358 ID.AddPointer(A); 1359 ID.AddPointer(B); 1360 ID.AddPointer(Expr); 1361 ID.AddPointer(Type); 1362 } 1363 1364 FoldOpInit *FoldOpInit::get(Init *Start, Init *List, Init *A, Init *B, 1365 Init *Expr, RecTy *Type) { 1366 static FoldingSet<FoldOpInit> ThePool; 1367 1368 FoldingSetNodeID ID; 1369 ProfileFoldOpInit(ID, Start, List, A, B, Expr, Type); 1370 1371 void *IP = nullptr; 1372 if (FoldOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP)) 1373 return I; 1374 1375 FoldOpInit *I = new (Allocator) FoldOpInit(Start, List, A, B, Expr, Type); 1376 ThePool.InsertNode(I, IP); 1377 return I; 1378 } 1379 1380 void FoldOpInit::Profile(FoldingSetNodeID &ID) const { 1381 ProfileFoldOpInit(ID, Start, List, A, B, Expr, getType()); 1382 } 1383 1384 Init *FoldOpInit::Fold(Record *CurRec) const { 1385 if (ListInit *LI = dyn_cast<ListInit>(List)) { 1386 Init *Accum = Start; 1387 for (Init *Elt : *LI) { 1388 MapResolver R(CurRec); 1389 R.set(A, Accum); 1390 R.set(B, Elt); 1391 Accum = Expr->resolveReferences(R); 1392 } 1393 return Accum; 1394 } 1395 return const_cast<FoldOpInit *>(this); 1396 } 1397 1398 Init *FoldOpInit::resolveReferences(Resolver &R) const { 1399 Init *NewStart = Start->resolveReferences(R); 1400 Init *NewList = List->resolveReferences(R); 1401 ShadowResolver SR(R); 1402 SR.addShadow(A); 1403 SR.addShadow(B); 1404 Init *NewExpr = Expr->resolveReferences(SR); 1405 1406 if (Start == NewStart && List == NewList && Expr == NewExpr) 1407 return const_cast<FoldOpInit *>(this); 1408 1409 return get(NewStart, NewList, A, B, NewExpr, getType()) 1410 ->Fold(R.getCurrentRecord()); 1411 } 1412 1413 Init *FoldOpInit::getBit(unsigned Bit) const { 1414 return VarBitInit::get(const_cast<FoldOpInit *>(this), Bit); 1415 } 1416 1417 std::string FoldOpInit::getAsString() const { 1418 return (Twine("!foldl(") + Start->getAsString() + ", " + List->getAsString() + 1419 ", " + A->getAsUnquotedString() + ", " + B->getAsUnquotedString() + 1420 ", " + Expr->getAsString() + ")") 1421 .str(); 1422 } 1423 1424 static void ProfileIsAOpInit(FoldingSetNodeID &ID, RecTy *CheckType, 1425 Init *Expr) { 1426 ID.AddPointer(CheckType); 1427 ID.AddPointer(Expr); 1428 } 1429 1430 IsAOpInit *IsAOpInit::get(RecTy *CheckType, Init *Expr) { 1431 static FoldingSet<IsAOpInit> ThePool; 1432 1433 FoldingSetNodeID ID; 1434 ProfileIsAOpInit(ID, CheckType, Expr); 1435 1436 void *IP = nullptr; 1437 if (IsAOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP)) 1438 return I; 1439 1440 IsAOpInit *I = new (Allocator) IsAOpInit(CheckType, Expr); 1441 ThePool.InsertNode(I, IP); 1442 return I; 1443 } 1444 1445 void IsAOpInit::Profile(FoldingSetNodeID &ID) const { 1446 ProfileIsAOpInit(ID, CheckType, Expr); 1447 } 1448 1449 Init *IsAOpInit::Fold() const { 1450 if (TypedInit *TI = dyn_cast<TypedInit>(Expr)) { 1451 // Is the expression type known to be (a subclass of) the desired type? 1452 if (TI->getType()->typeIsConvertibleTo(CheckType)) 1453 return IntInit::get(1); 1454 1455 if (isa<RecordRecTy>(CheckType)) { 1456 // If the target type is not a subclass of the expression type, or if 1457 // the expression has fully resolved to a record, we know that it can't 1458 // be of the required type. 1459 if (!CheckType->typeIsConvertibleTo(TI->getType()) || isa<DefInit>(Expr)) 1460 return IntInit::get(0); 1461 } else { 1462 // We treat non-record types as not castable. 1463 return IntInit::get(0); 1464 } 1465 } 1466 return const_cast<IsAOpInit *>(this); 1467 } 1468 1469 Init *IsAOpInit::resolveReferences(Resolver &R) const { 1470 Init *NewExpr = Expr->resolveReferences(R); 1471 if (Expr != NewExpr) 1472 return get(CheckType, NewExpr)->Fold(); 1473 return const_cast<IsAOpInit *>(this); 1474 } 1475 1476 Init *IsAOpInit::getBit(unsigned Bit) const { 1477 return VarBitInit::get(const_cast<IsAOpInit *>(this), Bit); 1478 } 1479 1480 std::string IsAOpInit::getAsString() const { 1481 return (Twine("!isa<") + CheckType->getAsString() + ">(" + 1482 Expr->getAsString() + ")") 1483 .str(); 1484 } 1485 1486 RecTy *TypedInit::getFieldType(StringInit *FieldName) const { 1487 if (RecordRecTy *RecordType = dyn_cast<RecordRecTy>(getType())) { 1488 for (Record *Rec : RecordType->getClasses()) { 1489 if (RecordVal *Field = Rec->getValue(FieldName)) 1490 return Field->getType(); 1491 } 1492 } 1493 return nullptr; 1494 } 1495 1496 Init * 1497 TypedInit::convertInitializerTo(RecTy *Ty) const { 1498 if (getType() == Ty || getType()->typeIsA(Ty)) 1499 return const_cast<TypedInit *>(this); 1500 1501 if (isa<BitRecTy>(getType()) && isa<BitsRecTy>(Ty) && 1502 cast<BitsRecTy>(Ty)->getNumBits() == 1) 1503 return BitsInit::get({const_cast<TypedInit *>(this)}); 1504 1505 return nullptr; 1506 } 1507 1508 Init *TypedInit::convertInitializerBitRange(ArrayRef<unsigned> Bits) const { 1509 BitsRecTy *T = dyn_cast<BitsRecTy>(getType()); 1510 if (!T) return nullptr; // Cannot subscript a non-bits variable. 1511 unsigned NumBits = T->getNumBits(); 1512 1513 SmallVector<Init *, 16> NewBits; 1514 NewBits.reserve(Bits.size()); 1515 for (unsigned Bit : Bits) { 1516 if (Bit >= NumBits) 1517 return nullptr; 1518 1519 NewBits.push_back(VarBitInit::get(const_cast<TypedInit *>(this), Bit)); 1520 } 1521 return BitsInit::get(NewBits); 1522 } 1523 1524 Init *TypedInit::getCastTo(RecTy *Ty) const { 1525 // Handle the common case quickly 1526 if (getType() == Ty || getType()->typeIsA(Ty)) 1527 return const_cast<TypedInit *>(this); 1528 1529 if (Init *Converted = convertInitializerTo(Ty)) { 1530 assert(!isa<TypedInit>(Converted) || 1531 cast<TypedInit>(Converted)->getType()->typeIsA(Ty)); 1532 return Converted; 1533 } 1534 1535 if (!getType()->typeIsConvertibleTo(Ty)) 1536 return nullptr; 1537 1538 return UnOpInit::get(UnOpInit::CAST, const_cast<TypedInit *>(this), Ty) 1539 ->Fold(nullptr); 1540 } 1541 1542 Init *TypedInit::convertInitListSlice(ArrayRef<unsigned> Elements) const { 1543 ListRecTy *T = dyn_cast<ListRecTy>(getType()); 1544 if (!T) return nullptr; // Cannot subscript a non-list variable. 1545 1546 if (Elements.size() == 1) 1547 return VarListElementInit::get(const_cast<TypedInit *>(this), Elements[0]); 1548 1549 SmallVector<Init*, 8> ListInits; 1550 ListInits.reserve(Elements.size()); 1551 for (unsigned Element : Elements) 1552 ListInits.push_back(VarListElementInit::get(const_cast<TypedInit *>(this), 1553 Element)); 1554 return ListInit::get(ListInits, T->getElementType()); 1555 } 1556 1557 1558 VarInit *VarInit::get(StringRef VN, RecTy *T) { 1559 Init *Value = StringInit::get(VN); 1560 return VarInit::get(Value, T); 1561 } 1562 1563 VarInit *VarInit::get(Init *VN, RecTy *T) { 1564 using Key = std::pair<RecTy *, Init *>; 1565 static DenseMap<Key, VarInit*> ThePool; 1566 1567 Key TheKey(std::make_pair(T, VN)); 1568 1569 VarInit *&I = ThePool[TheKey]; 1570 if (!I) 1571 I = new(Allocator) VarInit(VN, T); 1572 return I; 1573 } 1574 1575 StringRef VarInit::getName() const { 1576 StringInit *NameString = cast<StringInit>(getNameInit()); 1577 return NameString->getValue(); 1578 } 1579 1580 Init *VarInit::getBit(unsigned Bit) const { 1581 if (getType() == BitRecTy::get()) 1582 return const_cast<VarInit*>(this); 1583 return VarBitInit::get(const_cast<VarInit*>(this), Bit); 1584 } 1585 1586 Init *VarInit::resolveReferences(Resolver &R) const { 1587 if (Init *Val = R.resolve(VarName)) 1588 return Val; 1589 return const_cast<VarInit *>(this); 1590 } 1591 1592 VarBitInit *VarBitInit::get(TypedInit *T, unsigned B) { 1593 using Key = std::pair<TypedInit *, unsigned>; 1594 static DenseMap<Key, VarBitInit*> ThePool; 1595 1596 Key TheKey(std::make_pair(T, B)); 1597 1598 VarBitInit *&I = ThePool[TheKey]; 1599 if (!I) 1600 I = new(Allocator) VarBitInit(T, B); 1601 return I; 1602 } 1603 1604 std::string VarBitInit::getAsString() const { 1605 return TI->getAsString() + "{" + utostr(Bit) + "}"; 1606 } 1607 1608 Init *VarBitInit::resolveReferences(Resolver &R) const { 1609 Init *I = TI->resolveReferences(R); 1610 if (TI != I) 1611 return I->getBit(getBitNum()); 1612 1613 return const_cast<VarBitInit*>(this); 1614 } 1615 1616 VarListElementInit *VarListElementInit::get(TypedInit *T, 1617 unsigned E) { 1618 using Key = std::pair<TypedInit *, unsigned>; 1619 static DenseMap<Key, VarListElementInit*> ThePool; 1620 1621 Key TheKey(std::make_pair(T, E)); 1622 1623 VarListElementInit *&I = ThePool[TheKey]; 1624 if (!I) I = new(Allocator) VarListElementInit(T, E); 1625 return I; 1626 } 1627 1628 std::string VarListElementInit::getAsString() const { 1629 return TI->getAsString() + "[" + utostr(Element) + "]"; 1630 } 1631 1632 Init *VarListElementInit::resolveReferences(Resolver &R) const { 1633 Init *NewTI = TI->resolveReferences(R); 1634 if (ListInit *List = dyn_cast<ListInit>(NewTI)) { 1635 // Leave out-of-bounds array references as-is. This can happen without 1636 // being an error, e.g. in the untaken "branch" of an !if expression. 1637 if (getElementNum() < List->size()) 1638 return List->getElement(getElementNum()); 1639 } 1640 if (NewTI != TI && isa<TypedInit>(NewTI)) 1641 return VarListElementInit::get(cast<TypedInit>(NewTI), getElementNum()); 1642 return const_cast<VarListElementInit *>(this); 1643 } 1644 1645 Init *VarListElementInit::getBit(unsigned Bit) const { 1646 if (getType() == BitRecTy::get()) 1647 return const_cast<VarListElementInit*>(this); 1648 return VarBitInit::get(const_cast<VarListElementInit*>(this), Bit); 1649 } 1650 1651 DefInit::DefInit(Record *D) 1652 : TypedInit(IK_DefInit, D->getType()), Def(D) {} 1653 1654 DefInit *DefInit::get(Record *R) { 1655 return R->getDefInit(); 1656 } 1657 1658 Init *DefInit::convertInitializerTo(RecTy *Ty) const { 1659 if (auto *RRT = dyn_cast<RecordRecTy>(Ty)) 1660 if (getType()->typeIsConvertibleTo(RRT)) 1661 return const_cast<DefInit *>(this); 1662 return nullptr; 1663 } 1664 1665 RecTy *DefInit::getFieldType(StringInit *FieldName) const { 1666 if (const RecordVal *RV = Def->getValue(FieldName)) 1667 return RV->getType(); 1668 return nullptr; 1669 } 1670 1671 std::string DefInit::getAsString() const { return std::string(Def->getName()); } 1672 1673 static void ProfileVarDefInit(FoldingSetNodeID &ID, 1674 Record *Class, 1675 ArrayRef<Init *> Args) { 1676 ID.AddInteger(Args.size()); 1677 ID.AddPointer(Class); 1678 1679 for (Init *I : Args) 1680 ID.AddPointer(I); 1681 } 1682 1683 VarDefInit *VarDefInit::get(Record *Class, ArrayRef<Init *> Args) { 1684 static FoldingSet<VarDefInit> ThePool; 1685 1686 FoldingSetNodeID ID; 1687 ProfileVarDefInit(ID, Class, Args); 1688 1689 void *IP = nullptr; 1690 if (VarDefInit *I = ThePool.FindNodeOrInsertPos(ID, IP)) 1691 return I; 1692 1693 void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *>(Args.size()), 1694 alignof(VarDefInit)); 1695 VarDefInit *I = new(Mem) VarDefInit(Class, Args.size()); 1696 std::uninitialized_copy(Args.begin(), Args.end(), 1697 I->getTrailingObjects<Init *>()); 1698 ThePool.InsertNode(I, IP); 1699 return I; 1700 } 1701 1702 void VarDefInit::Profile(FoldingSetNodeID &ID) const { 1703 ProfileVarDefInit(ID, Class, args()); 1704 } 1705 1706 DefInit *VarDefInit::instantiate() { 1707 if (!Def) { 1708 RecordKeeper &Records = Class->getRecords(); 1709 auto NewRecOwner = std::make_unique<Record>(Records.getNewAnonymousName(), 1710 Class->getLoc(), Records, 1711 /*IsAnonymous=*/true); 1712 Record *NewRec = NewRecOwner.get(); 1713 1714 // Copy values from class to instance 1715 for (const RecordVal &Val : Class->getValues()) 1716 NewRec->addValue(Val); 1717 1718 // Substitute and resolve template arguments 1719 ArrayRef<Init *> TArgs = Class->getTemplateArgs(); 1720 MapResolver R(NewRec); 1721 1722 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) { 1723 if (i < args_size()) 1724 R.set(TArgs[i], getArg(i)); 1725 else 1726 R.set(TArgs[i], NewRec->getValue(TArgs[i])->getValue()); 1727 1728 NewRec->removeValue(TArgs[i]); 1729 } 1730 1731 NewRec->resolveReferences(R); 1732 1733 // Add superclasses. 1734 ArrayRef<std::pair<Record *, SMRange>> SCs = Class->getSuperClasses(); 1735 for (const auto &SCPair : SCs) 1736 NewRec->addSuperClass(SCPair.first, SCPair.second); 1737 1738 NewRec->addSuperClass(Class, 1739 SMRange(Class->getLoc().back(), 1740 Class->getLoc().back())); 1741 1742 // Resolve internal references and store in record keeper 1743 NewRec->resolveReferences(); 1744 Records.addDef(std::move(NewRecOwner)); 1745 1746 Def = DefInit::get(NewRec); 1747 } 1748 1749 return Def; 1750 } 1751 1752 Init *VarDefInit::resolveReferences(Resolver &R) const { 1753 TrackUnresolvedResolver UR(&R); 1754 bool Changed = false; 1755 SmallVector<Init *, 8> NewArgs; 1756 NewArgs.reserve(args_size()); 1757 1758 for (Init *Arg : args()) { 1759 Init *NewArg = Arg->resolveReferences(UR); 1760 NewArgs.push_back(NewArg); 1761 Changed |= NewArg != Arg; 1762 } 1763 1764 if (Changed) { 1765 auto New = VarDefInit::get(Class, NewArgs); 1766 if (!UR.foundUnresolved()) 1767 return New->instantiate(); 1768 return New; 1769 } 1770 return const_cast<VarDefInit *>(this); 1771 } 1772 1773 Init *VarDefInit::Fold() const { 1774 if (Def) 1775 return Def; 1776 1777 TrackUnresolvedResolver R; 1778 for (Init *Arg : args()) 1779 Arg->resolveReferences(R); 1780 1781 if (!R.foundUnresolved()) 1782 return const_cast<VarDefInit *>(this)->instantiate(); 1783 return const_cast<VarDefInit *>(this); 1784 } 1785 1786 std::string VarDefInit::getAsString() const { 1787 std::string Result = Class->getNameInitAsString() + "<"; 1788 const char *sep = ""; 1789 for (Init *Arg : args()) { 1790 Result += sep; 1791 sep = ", "; 1792 Result += Arg->getAsString(); 1793 } 1794 return Result + ">"; 1795 } 1796 1797 FieldInit *FieldInit::get(Init *R, StringInit *FN) { 1798 using Key = std::pair<Init *, StringInit *>; 1799 static DenseMap<Key, FieldInit*> ThePool; 1800 1801 Key TheKey(std::make_pair(R, FN)); 1802 1803 FieldInit *&I = ThePool[TheKey]; 1804 if (!I) I = new(Allocator) FieldInit(R, FN); 1805 return I; 1806 } 1807 1808 Init *FieldInit::getBit(unsigned Bit) const { 1809 if (getType() == BitRecTy::get()) 1810 return const_cast<FieldInit*>(this); 1811 return VarBitInit::get(const_cast<FieldInit*>(this), Bit); 1812 } 1813 1814 Init *FieldInit::resolveReferences(Resolver &R) const { 1815 Init *NewRec = Rec->resolveReferences(R); 1816 if (NewRec != Rec) 1817 return FieldInit::get(NewRec, FieldName)->Fold(R.getCurrentRecord()); 1818 return const_cast<FieldInit *>(this); 1819 } 1820 1821 Init *FieldInit::Fold(Record *CurRec) const { 1822 if (DefInit *DI = dyn_cast<DefInit>(Rec)) { 1823 Record *Def = DI->getDef(); 1824 if (Def == CurRec) 1825 PrintFatalError(CurRec->getLoc(), 1826 Twine("Attempting to access field '") + 1827 FieldName->getAsUnquotedString() + "' of '" + 1828 Rec->getAsString() + "' is a forbidden self-reference"); 1829 Init *FieldVal = Def->getValue(FieldName)->getValue(); 1830 if (FieldVal->isComplete()) 1831 return FieldVal; 1832 } 1833 return const_cast<FieldInit *>(this); 1834 } 1835 1836 bool FieldInit::isConcrete() const { 1837 if (DefInit *DI = dyn_cast<DefInit>(Rec)) { 1838 Init *FieldVal = DI->getDef()->getValue(FieldName)->getValue(); 1839 return FieldVal->isConcrete(); 1840 } 1841 return false; 1842 } 1843 1844 static void ProfileCondOpInit(FoldingSetNodeID &ID, 1845 ArrayRef<Init *> CondRange, 1846 ArrayRef<Init *> ValRange, 1847 const RecTy *ValType) { 1848 assert(CondRange.size() == ValRange.size() && 1849 "Number of conditions and values must match!"); 1850 ID.AddPointer(ValType); 1851 ArrayRef<Init *>::iterator Case = CondRange.begin(); 1852 ArrayRef<Init *>::iterator Val = ValRange.begin(); 1853 1854 while (Case != CondRange.end()) { 1855 ID.AddPointer(*Case++); 1856 ID.AddPointer(*Val++); 1857 } 1858 } 1859 1860 void CondOpInit::Profile(FoldingSetNodeID &ID) const { 1861 ProfileCondOpInit(ID, 1862 makeArrayRef(getTrailingObjects<Init *>(), NumConds), 1863 makeArrayRef(getTrailingObjects<Init *>() + NumConds, NumConds), 1864 ValType); 1865 } 1866 1867 CondOpInit * 1868 CondOpInit::get(ArrayRef<Init *> CondRange, 1869 ArrayRef<Init *> ValRange, RecTy *Ty) { 1870 assert(CondRange.size() == ValRange.size() && 1871 "Number of conditions and values must match!"); 1872 1873 static FoldingSet<CondOpInit> ThePool; 1874 FoldingSetNodeID ID; 1875 ProfileCondOpInit(ID, CondRange, ValRange, Ty); 1876 1877 void *IP = nullptr; 1878 if (CondOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP)) 1879 return I; 1880 1881 void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *>(2*CondRange.size()), 1882 alignof(BitsInit)); 1883 CondOpInit *I = new(Mem) CondOpInit(CondRange.size(), Ty); 1884 1885 std::uninitialized_copy(CondRange.begin(), CondRange.end(), 1886 I->getTrailingObjects<Init *>()); 1887 std::uninitialized_copy(ValRange.begin(), ValRange.end(), 1888 I->getTrailingObjects<Init *>()+CondRange.size()); 1889 ThePool.InsertNode(I, IP); 1890 return I; 1891 } 1892 1893 Init *CondOpInit::resolveReferences(Resolver &R) const { 1894 SmallVector<Init*, 4> NewConds; 1895 bool Changed = false; 1896 for (const Init *Case : getConds()) { 1897 Init *NewCase = Case->resolveReferences(R); 1898 NewConds.push_back(NewCase); 1899 Changed |= NewCase != Case; 1900 } 1901 1902 SmallVector<Init*, 4> NewVals; 1903 for (const Init *Val : getVals()) { 1904 Init *NewVal = Val->resolveReferences(R); 1905 NewVals.push_back(NewVal); 1906 Changed |= NewVal != Val; 1907 } 1908 1909 if (Changed) 1910 return (CondOpInit::get(NewConds, NewVals, 1911 getValType()))->Fold(R.getCurrentRecord()); 1912 1913 return const_cast<CondOpInit *>(this); 1914 } 1915 1916 Init *CondOpInit::Fold(Record *CurRec) const { 1917 for ( unsigned i = 0; i < NumConds; ++i) { 1918 Init *Cond = getCond(i); 1919 Init *Val = getVal(i); 1920 1921 if (IntInit *CondI = dyn_cast_or_null<IntInit>( 1922 Cond->convertInitializerTo(IntRecTy::get()))) { 1923 if (CondI->getValue()) 1924 return Val->convertInitializerTo(getValType()); 1925 } else 1926 return const_cast<CondOpInit *>(this); 1927 } 1928 1929 PrintFatalError(CurRec->getLoc(), 1930 CurRec->getName() + 1931 " does not have any true condition in:" + 1932 this->getAsString()); 1933 return nullptr; 1934 } 1935 1936 bool CondOpInit::isConcrete() const { 1937 for (const Init *Case : getConds()) 1938 if (!Case->isConcrete()) 1939 return false; 1940 1941 for (const Init *Val : getVals()) 1942 if (!Val->isConcrete()) 1943 return false; 1944 1945 return true; 1946 } 1947 1948 bool CondOpInit::isComplete() const { 1949 for (const Init *Case : getConds()) 1950 if (!Case->isComplete()) 1951 return false; 1952 1953 for (const Init *Val : getVals()) 1954 if (!Val->isConcrete()) 1955 return false; 1956 1957 return true; 1958 } 1959 1960 std::string CondOpInit::getAsString() const { 1961 std::string Result = "!cond("; 1962 for (unsigned i = 0; i < getNumConds(); i++) { 1963 Result += getCond(i)->getAsString() + ": "; 1964 Result += getVal(i)->getAsString(); 1965 if (i != getNumConds()-1) 1966 Result += ", "; 1967 } 1968 return Result + ")"; 1969 } 1970 1971 Init *CondOpInit::getBit(unsigned Bit) const { 1972 return VarBitInit::get(const_cast<CondOpInit *>(this), Bit); 1973 } 1974 1975 static void ProfileDagInit(FoldingSetNodeID &ID, Init *V, StringInit *VN, 1976 ArrayRef<Init *> ArgRange, 1977 ArrayRef<StringInit *> NameRange) { 1978 ID.AddPointer(V); 1979 ID.AddPointer(VN); 1980 1981 ArrayRef<Init *>::iterator Arg = ArgRange.begin(); 1982 ArrayRef<StringInit *>::iterator Name = NameRange.begin(); 1983 while (Arg != ArgRange.end()) { 1984 assert(Name != NameRange.end() && "Arg name underflow!"); 1985 ID.AddPointer(*Arg++); 1986 ID.AddPointer(*Name++); 1987 } 1988 assert(Name == NameRange.end() && "Arg name overflow!"); 1989 } 1990 1991 DagInit * 1992 DagInit::get(Init *V, StringInit *VN, ArrayRef<Init *> ArgRange, 1993 ArrayRef<StringInit *> NameRange) { 1994 static FoldingSet<DagInit> ThePool; 1995 1996 FoldingSetNodeID ID; 1997 ProfileDagInit(ID, V, VN, ArgRange, NameRange); 1998 1999 void *IP = nullptr; 2000 if (DagInit *I = ThePool.FindNodeOrInsertPos(ID, IP)) 2001 return I; 2002 2003 void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *, StringInit *>(ArgRange.size(), NameRange.size()), alignof(BitsInit)); 2004 DagInit *I = new(Mem) DagInit(V, VN, ArgRange.size(), NameRange.size()); 2005 std::uninitialized_copy(ArgRange.begin(), ArgRange.end(), 2006 I->getTrailingObjects<Init *>()); 2007 std::uninitialized_copy(NameRange.begin(), NameRange.end(), 2008 I->getTrailingObjects<StringInit *>()); 2009 ThePool.InsertNode(I, IP); 2010 return I; 2011 } 2012 2013 DagInit * 2014 DagInit::get(Init *V, StringInit *VN, 2015 ArrayRef<std::pair<Init*, StringInit*>> args) { 2016 SmallVector<Init *, 8> Args; 2017 SmallVector<StringInit *, 8> Names; 2018 2019 for (const auto &Arg : args) { 2020 Args.push_back(Arg.first); 2021 Names.push_back(Arg.second); 2022 } 2023 2024 return DagInit::get(V, VN, Args, Names); 2025 } 2026 2027 void DagInit::Profile(FoldingSetNodeID &ID) const { 2028 ProfileDagInit(ID, Val, ValName, makeArrayRef(getTrailingObjects<Init *>(), NumArgs), makeArrayRef(getTrailingObjects<StringInit *>(), NumArgNames)); 2029 } 2030 2031 Record *DagInit::getOperatorAsDef(ArrayRef<SMLoc> Loc) const { 2032 if (DefInit *DefI = dyn_cast<DefInit>(Val)) 2033 return DefI->getDef(); 2034 PrintFatalError(Loc, "Expected record as operator"); 2035 return nullptr; 2036 } 2037 2038 Init *DagInit::resolveReferences(Resolver &R) const { 2039 SmallVector<Init*, 8> NewArgs; 2040 NewArgs.reserve(arg_size()); 2041 bool ArgsChanged = false; 2042 for (const Init *Arg : getArgs()) { 2043 Init *NewArg = Arg->resolveReferences(R); 2044 NewArgs.push_back(NewArg); 2045 ArgsChanged |= NewArg != Arg; 2046 } 2047 2048 Init *Op = Val->resolveReferences(R); 2049 if (Op != Val || ArgsChanged) 2050 return DagInit::get(Op, ValName, NewArgs, getArgNames()); 2051 2052 return const_cast<DagInit *>(this); 2053 } 2054 2055 bool DagInit::isConcrete() const { 2056 if (!Val->isConcrete()) 2057 return false; 2058 for (const Init *Elt : getArgs()) { 2059 if (!Elt->isConcrete()) 2060 return false; 2061 } 2062 return true; 2063 } 2064 2065 std::string DagInit::getAsString() const { 2066 std::string Result = "(" + Val->getAsString(); 2067 if (ValName) 2068 Result += ":" + ValName->getAsUnquotedString(); 2069 if (!arg_empty()) { 2070 Result += " " + getArg(0)->getAsString(); 2071 if (getArgName(0)) Result += ":$" + getArgName(0)->getAsUnquotedString(); 2072 for (unsigned i = 1, e = getNumArgs(); i != e; ++i) { 2073 Result += ", " + getArg(i)->getAsString(); 2074 if (getArgName(i)) Result += ":$" + getArgName(i)->getAsUnquotedString(); 2075 } 2076 } 2077 return Result + ")"; 2078 } 2079 2080 //===----------------------------------------------------------------------===// 2081 // Other implementations 2082 //===----------------------------------------------------------------------===// 2083 2084 RecordVal::RecordVal(Init *N, RecTy *T, bool P) 2085 : Name(N), TyAndPrefix(T, P) { 2086 setValue(UnsetInit::get()); 2087 assert(Value && "Cannot create unset value for current type!"); 2088 } 2089 2090 // This constructor accepts the same arguments as the above, but also 2091 // a source location. 2092 RecordVal::RecordVal(Init *N, SMLoc Loc, RecTy *T, bool P) 2093 : Name(N), Loc(Loc), TyAndPrefix(T, P) { 2094 setValue(UnsetInit::get()); 2095 assert(Value && "Cannot create unset value for current type!"); 2096 } 2097 2098 StringRef RecordVal::getName() const { 2099 return cast<StringInit>(getNameInit())->getValue(); 2100 } 2101 2102 bool RecordVal::setValue(Init *V) { 2103 if (V) { 2104 Value = V->getCastTo(getType()); 2105 if (Value) { 2106 assert(!isa<TypedInit>(Value) || 2107 cast<TypedInit>(Value)->getType()->typeIsA(getType())); 2108 if (BitsRecTy *BTy = dyn_cast<BitsRecTy>(getType())) { 2109 if (!isa<BitsInit>(Value)) { 2110 SmallVector<Init *, 64> Bits; 2111 Bits.reserve(BTy->getNumBits()); 2112 for (unsigned I = 0, E = BTy->getNumBits(); I < E; ++I) 2113 Bits.push_back(Value->getBit(I)); 2114 Value = BitsInit::get(Bits); 2115 } 2116 } 2117 } 2118 return Value == nullptr; 2119 } 2120 Value = nullptr; 2121 return false; 2122 } 2123 2124 // This version of setValue takes an source location and resets the 2125 // location in the RecordVal. 2126 bool RecordVal::setValue(Init *V, SMLoc NewLoc) { 2127 Loc = NewLoc; 2128 if (V) { 2129 Value = V->getCastTo(getType()); 2130 if (Value) { 2131 assert(!isa<TypedInit>(Value) || 2132 cast<TypedInit>(Value)->getType()->typeIsA(getType())); 2133 if (BitsRecTy *BTy = dyn_cast<BitsRecTy>(getType())) { 2134 if (!isa<BitsInit>(Value)) { 2135 SmallVector<Init *, 64> Bits; 2136 Bits.reserve(BTy->getNumBits()); 2137 for (unsigned I = 0, E = BTy->getNumBits(); I < E; ++I) 2138 Bits.push_back(Value->getBit(I)); 2139 Value = BitsInit::get(Bits); 2140 } 2141 } 2142 } 2143 return Value == nullptr; 2144 } 2145 Value = nullptr; 2146 return false; 2147 } 2148 2149 #include "llvm/TableGen/Record.h" 2150 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 2151 LLVM_DUMP_METHOD void RecordVal::dump() const { errs() << *this; } 2152 #endif 2153 2154 void RecordVal::print(raw_ostream &OS, bool PrintSem) const { 2155 if (getPrefix()) OS << "field "; 2156 OS << *getType() << " " << getNameInitAsString(); 2157 2158 if (getValue()) 2159 OS << " = " << *getValue(); 2160 2161 if (PrintSem) OS << ";\n"; 2162 } 2163 2164 unsigned Record::LastID = 0; 2165 2166 void Record::checkName() { 2167 // Ensure the record name has string type. 2168 const TypedInit *TypedName = cast<const TypedInit>(Name); 2169 if (!isa<StringRecTy>(TypedName->getType())) 2170 PrintFatalError(getLoc(), Twine("Record name '") + Name->getAsString() + 2171 "' is not a string!"); 2172 } 2173 2174 RecordRecTy *Record::getType() { 2175 SmallVector<Record *, 4> DirectSCs; 2176 getDirectSuperClasses(DirectSCs); 2177 return RecordRecTy::get(DirectSCs); 2178 } 2179 2180 DefInit *Record::getDefInit() { 2181 if (!CorrespondingDefInit) 2182 CorrespondingDefInit = new (Allocator) DefInit(this); 2183 return CorrespondingDefInit; 2184 } 2185 2186 void Record::setName(Init *NewName) { 2187 Name = NewName; 2188 checkName(); 2189 // DO NOT resolve record values to the name at this point because 2190 // there might be default values for arguments of this def. Those 2191 // arguments might not have been resolved yet so we don't want to 2192 // prematurely assume values for those arguments were not passed to 2193 // this def. 2194 // 2195 // Nonetheless, it may be that some of this Record's values 2196 // reference the record name. Indeed, the reason for having the 2197 // record name be an Init is to provide this flexibility. The extra 2198 // resolve steps after completely instantiating defs takes care of 2199 // this. See TGParser::ParseDef and TGParser::ParseDefm. 2200 } 2201 2202 // NOTE for the next two functions: 2203 // Superclasses are in post-order, so the final one is a direct 2204 // superclass. All of its transitive superclases immediately precede it, 2205 // so we can step through the direct superclasses in reverse order. 2206 2207 bool Record::hasDirectSuperClass(const Record *Superclass) const { 2208 ArrayRef<std::pair<Record *, SMRange>> SCs = getSuperClasses(); 2209 2210 for (int I = SCs.size() - 1; I >= 0; --I) { 2211 const Record *SC = SCs[I].first; 2212 if (SC == Superclass) 2213 return true; 2214 I -= SC->getSuperClasses().size(); 2215 } 2216 2217 return false; 2218 } 2219 2220 void Record::getDirectSuperClasses(SmallVectorImpl<Record *> &Classes) const { 2221 ArrayRef<std::pair<Record *, SMRange>> SCs = getSuperClasses(); 2222 2223 while (!SCs.empty()) { 2224 Record *SC = SCs.back().first; 2225 SCs = SCs.drop_back(1 + SC->getSuperClasses().size()); 2226 Classes.push_back(SC); 2227 } 2228 } 2229 2230 void Record::resolveReferences(Resolver &R, const RecordVal *SkipVal) { 2231 for (RecordVal &Value : Values) { 2232 if (SkipVal == &Value) // Skip resolve the same field as the given one 2233 continue; 2234 if (Init *V = Value.getValue()) { 2235 Init *VR = V->resolveReferences(R); 2236 if (Value.setValue(VR)) { 2237 std::string Type; 2238 if (TypedInit *VRT = dyn_cast<TypedInit>(VR)) 2239 Type = 2240 (Twine("of type '") + VRT->getType()->getAsString() + "' ").str(); 2241 PrintFatalError(getLoc(), Twine("Invalid value ") + Type + 2242 "is found when setting '" + 2243 Value.getNameInitAsString() + 2244 "' of type '" + 2245 Value.getType()->getAsString() + 2246 "' after resolving references: " + 2247 VR->getAsUnquotedString() + "\n"); 2248 } 2249 } 2250 } 2251 Init *OldName = getNameInit(); 2252 Init *NewName = Name->resolveReferences(R); 2253 if (NewName != OldName) { 2254 // Re-register with RecordKeeper. 2255 setName(NewName); 2256 } 2257 } 2258 2259 void Record::resolveReferences() { 2260 RecordResolver R(*this); 2261 R.setFinal(true); 2262 resolveReferences(R); 2263 } 2264 2265 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 2266 LLVM_DUMP_METHOD void Record::dump() const { errs() << *this; } 2267 #endif 2268 2269 raw_ostream &llvm::operator<<(raw_ostream &OS, const Record &R) { 2270 OS << R.getNameInitAsString(); 2271 2272 ArrayRef<Init *> TArgs = R.getTemplateArgs(); 2273 if (!TArgs.empty()) { 2274 OS << "<"; 2275 bool NeedComma = false; 2276 for (const Init *TA : TArgs) { 2277 if (NeedComma) OS << ", "; 2278 NeedComma = true; 2279 const RecordVal *RV = R.getValue(TA); 2280 assert(RV && "Template argument record not found??"); 2281 RV->print(OS, false); 2282 } 2283 OS << ">"; 2284 } 2285 2286 OS << " {"; 2287 ArrayRef<std::pair<Record *, SMRange>> SC = R.getSuperClasses(); 2288 if (!SC.empty()) { 2289 OS << "\t//"; 2290 for (const auto &SuperPair : SC) 2291 OS << " " << SuperPair.first->getNameInitAsString(); 2292 } 2293 OS << "\n"; 2294 2295 for (const RecordVal &Val : R.getValues()) 2296 if (Val.getPrefix() && !R.isTemplateArg(Val.getNameInit())) 2297 OS << Val; 2298 for (const RecordVal &Val : R.getValues()) 2299 if (!Val.getPrefix() && !R.isTemplateArg(Val.getNameInit())) 2300 OS << Val; 2301 2302 return OS << "}\n"; 2303 } 2304 2305 Init *Record::getValueInit(StringRef FieldName) const { 2306 const RecordVal *R = getValue(FieldName); 2307 if (!R || !R->getValue()) 2308 PrintFatalError(getLoc(), "Record `" + getName() + 2309 "' does not have a field named `" + FieldName + "'!\n"); 2310 return R->getValue(); 2311 } 2312 2313 StringRef Record::getValueAsString(StringRef FieldName) const { 2314 llvm::Optional<StringRef> S = getValueAsOptionalString(FieldName); 2315 if (!S.hasValue()) 2316 PrintFatalError(getLoc(), "Record `" + getName() + 2317 "' does not have a field named `" + FieldName + "'!\n"); 2318 return S.getValue(); 2319 } 2320 llvm::Optional<StringRef> 2321 Record::getValueAsOptionalString(StringRef FieldName) const { 2322 const RecordVal *R = getValue(FieldName); 2323 if (!R || !R->getValue()) 2324 return llvm::Optional<StringRef>(); 2325 if (isa<UnsetInit>(R->getValue())) 2326 return llvm::Optional<StringRef>(); 2327 2328 if (StringInit *SI = dyn_cast<StringInit>(R->getValue())) 2329 return SI->getValue(); 2330 if (CodeInit *CI = dyn_cast<CodeInit>(R->getValue())) 2331 return CI->getValue(); 2332 2333 PrintFatalError(getLoc(), 2334 "Record `" + getName() + "', ` field `" + FieldName + 2335 "' exists but does not have a string initializer!"); 2336 } 2337 llvm::Optional<StringRef> 2338 Record::getValueAsOptionalCode(StringRef FieldName) const { 2339 const RecordVal *R = getValue(FieldName); 2340 if (!R || !R->getValue()) 2341 return llvm::Optional<StringRef>(); 2342 if (isa<UnsetInit>(R->getValue())) 2343 return llvm::Optional<StringRef>(); 2344 2345 if (CodeInit *CI = dyn_cast<CodeInit>(R->getValue())) 2346 return CI->getValue(); 2347 2348 PrintFatalError(getLoc(), 2349 "Record `" + getName() + "', field `" + FieldName + 2350 "' exists but does not have a code initializer!"); 2351 } 2352 2353 BitsInit *Record::getValueAsBitsInit(StringRef FieldName) const { 2354 const RecordVal *R = getValue(FieldName); 2355 if (!R || !R->getValue()) 2356 PrintFatalError(getLoc(), "Record `" + getName() + 2357 "' does not have a field named `" + FieldName + "'!\n"); 2358 2359 if (BitsInit *BI = dyn_cast<BitsInit>(R->getValue())) 2360 return BI; 2361 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + FieldName + 2362 "' exists but does not have a bits value"); 2363 } 2364 2365 ListInit *Record::getValueAsListInit(StringRef FieldName) const { 2366 const RecordVal *R = getValue(FieldName); 2367 if (!R || !R->getValue()) 2368 PrintFatalError(getLoc(), "Record `" + getName() + 2369 "' does not have a field named `" + FieldName + "'!\n"); 2370 2371 if (ListInit *LI = dyn_cast<ListInit>(R->getValue())) 2372 return LI; 2373 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + FieldName + 2374 "' exists but does not have a list value"); 2375 } 2376 2377 std::vector<Record*> 2378 Record::getValueAsListOfDefs(StringRef FieldName) const { 2379 ListInit *List = getValueAsListInit(FieldName); 2380 std::vector<Record*> Defs; 2381 for (Init *I : List->getValues()) { 2382 if (DefInit *DI = dyn_cast<DefInit>(I)) 2383 Defs.push_back(DI->getDef()); 2384 else 2385 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + 2386 FieldName + "' list is not entirely DefInit!"); 2387 } 2388 return Defs; 2389 } 2390 2391 int64_t Record::getValueAsInt(StringRef FieldName) const { 2392 const RecordVal *R = getValue(FieldName); 2393 if (!R || !R->getValue()) 2394 PrintFatalError(getLoc(), "Record `" + getName() + 2395 "' does not have a field named `" + FieldName + "'!\n"); 2396 2397 if (IntInit *II = dyn_cast<IntInit>(R->getValue())) 2398 return II->getValue(); 2399 PrintFatalError(getLoc(), Twine("Record `") + getName() + "', field `" + 2400 FieldName + 2401 "' exists but does not have an int value: " + 2402 R->getValue()->getAsString()); 2403 } 2404 2405 std::vector<int64_t> 2406 Record::getValueAsListOfInts(StringRef FieldName) const { 2407 ListInit *List = getValueAsListInit(FieldName); 2408 std::vector<int64_t> Ints; 2409 for (Init *I : List->getValues()) { 2410 if (IntInit *II = dyn_cast<IntInit>(I)) 2411 Ints.push_back(II->getValue()); 2412 else 2413 PrintFatalError(getLoc(), 2414 Twine("Record `") + getName() + "', field `" + FieldName + 2415 "' exists but does not have a list of ints value: " + 2416 I->getAsString()); 2417 } 2418 return Ints; 2419 } 2420 2421 std::vector<StringRef> 2422 Record::getValueAsListOfStrings(StringRef FieldName) const { 2423 ListInit *List = getValueAsListInit(FieldName); 2424 std::vector<StringRef> Strings; 2425 for (Init *I : List->getValues()) { 2426 if (StringInit *SI = dyn_cast<StringInit>(I)) 2427 Strings.push_back(SI->getValue()); 2428 else if (CodeInit *CI = dyn_cast<CodeInit>(I)) 2429 Strings.push_back(CI->getValue()); 2430 else 2431 PrintFatalError(getLoc(), 2432 Twine("Record `") + getName() + "', field `" + FieldName + 2433 "' exists but does not have a list of strings value: " + 2434 I->getAsString()); 2435 } 2436 return Strings; 2437 } 2438 2439 Record *Record::getValueAsDef(StringRef FieldName) const { 2440 const RecordVal *R = getValue(FieldName); 2441 if (!R || !R->getValue()) 2442 PrintFatalError(getLoc(), "Record `" + getName() + 2443 "' does not have a field named `" + FieldName + "'!\n"); 2444 2445 if (DefInit *DI = dyn_cast<DefInit>(R->getValue())) 2446 return DI->getDef(); 2447 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + 2448 FieldName + "' does not have a def initializer!"); 2449 } 2450 2451 Record *Record::getValueAsOptionalDef(StringRef FieldName) const { 2452 const RecordVal *R = getValue(FieldName); 2453 if (!R || !R->getValue()) 2454 PrintFatalError(getLoc(), "Record `" + getName() + 2455 "' does not have a field named `" + FieldName + "'!\n"); 2456 2457 if (DefInit *DI = dyn_cast<DefInit>(R->getValue())) 2458 return DI->getDef(); 2459 if (isa<UnsetInit>(R->getValue())) 2460 return nullptr; 2461 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + 2462 FieldName + "' does not have either a def initializer or '?'!"); 2463 } 2464 2465 2466 bool Record::getValueAsBit(StringRef FieldName) const { 2467 const RecordVal *R = getValue(FieldName); 2468 if (!R || !R->getValue()) 2469 PrintFatalError(getLoc(), "Record `" + getName() + 2470 "' does not have a field named `" + FieldName + "'!\n"); 2471 2472 if (BitInit *BI = dyn_cast<BitInit>(R->getValue())) 2473 return BI->getValue(); 2474 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + 2475 FieldName + "' does not have a bit initializer!"); 2476 } 2477 2478 bool Record::getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const { 2479 const RecordVal *R = getValue(FieldName); 2480 if (!R || !R->getValue()) 2481 PrintFatalError(getLoc(), "Record `" + getName() + 2482 "' does not have a field named `" + FieldName.str() + "'!\n"); 2483 2484 if (isa<UnsetInit>(R->getValue())) { 2485 Unset = true; 2486 return false; 2487 } 2488 Unset = false; 2489 if (BitInit *BI = dyn_cast<BitInit>(R->getValue())) 2490 return BI->getValue(); 2491 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + 2492 FieldName + "' does not have a bit initializer!"); 2493 } 2494 2495 DagInit *Record::getValueAsDag(StringRef FieldName) const { 2496 const RecordVal *R = getValue(FieldName); 2497 if (!R || !R->getValue()) 2498 PrintFatalError(getLoc(), "Record `" + getName() + 2499 "' does not have a field named `" + FieldName + "'!\n"); 2500 2501 if (DagInit *DI = dyn_cast<DagInit>(R->getValue())) 2502 return DI; 2503 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + 2504 FieldName + "' does not have a dag initializer!"); 2505 } 2506 2507 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 2508 LLVM_DUMP_METHOD void RecordKeeper::dump() const { errs() << *this; } 2509 #endif 2510 2511 raw_ostream &llvm::operator<<(raw_ostream &OS, const RecordKeeper &RK) { 2512 OS << "------------- Classes -----------------\n"; 2513 for (const auto &C : RK.getClasses()) 2514 OS << "class " << *C.second; 2515 2516 OS << "------------- Defs -----------------\n"; 2517 for (const auto &D : RK.getDefs()) 2518 OS << "def " << *D.second; 2519 return OS; 2520 } 2521 2522 /// GetNewAnonymousName - Generate a unique anonymous name that can be used as 2523 /// an identifier. 2524 Init *RecordKeeper::getNewAnonymousName() { 2525 return StringInit::get("anonymous_" + utostr(AnonCounter++)); 2526 } 2527 2528 std::vector<Record *> RecordKeeper::getAllDerivedDefinitions( 2529 const ArrayRef<StringRef> ClassNames) const { 2530 SmallVector<Record *, 2> ClassRecs; 2531 std::vector<Record *> Defs; 2532 2533 assert(ClassNames.size() > 0 && "At least one class must be passed."); 2534 for (const auto &ClassName : ClassNames) { 2535 Record *Class = getClass(ClassName); 2536 if (!Class) 2537 PrintFatalError("The class '" + ClassName + "' is not defined\n"); 2538 ClassRecs.push_back(Class); 2539 } 2540 2541 for (const auto &OneDef : getDefs()) { 2542 if (all_of(ClassRecs, [&OneDef](const Record *Class) { 2543 return OneDef.second->isSubClassOf(Class); 2544 })) 2545 Defs.push_back(OneDef.second.get()); 2546 } 2547 2548 return Defs; 2549 } 2550 2551 Init *MapResolver::resolve(Init *VarName) { 2552 auto It = Map.find(VarName); 2553 if (It == Map.end()) 2554 return nullptr; 2555 2556 Init *I = It->second.V; 2557 2558 if (!It->second.Resolved && Map.size() > 1) { 2559 // Resolve mutual references among the mapped variables, but prevent 2560 // infinite recursion. 2561 Map.erase(It); 2562 I = I->resolveReferences(*this); 2563 Map[VarName] = {I, true}; 2564 } 2565 2566 return I; 2567 } 2568 2569 Init *RecordResolver::resolve(Init *VarName) { 2570 Init *Val = Cache.lookup(VarName); 2571 if (Val) 2572 return Val; 2573 2574 for (Init *S : Stack) { 2575 if (S == VarName) 2576 return nullptr; // prevent infinite recursion 2577 } 2578 2579 if (RecordVal *RV = getCurrentRecord()->getValue(VarName)) { 2580 if (!isa<UnsetInit>(RV->getValue())) { 2581 Val = RV->getValue(); 2582 Stack.push_back(VarName); 2583 Val = Val->resolveReferences(*this); 2584 Stack.pop_back(); 2585 } 2586 } 2587 2588 Cache[VarName] = Val; 2589 return Val; 2590 } 2591 2592 Init *TrackUnresolvedResolver::resolve(Init *VarName) { 2593 Init *I = nullptr; 2594 2595 if (R) { 2596 I = R->resolve(VarName); 2597 if (I && !FoundUnresolved) { 2598 // Do not recurse into the resolved initializer, as that would change 2599 // the behavior of the resolver we're delegating, but do check to see 2600 // if there are unresolved variables remaining. 2601 TrackUnresolvedResolver Sub; 2602 I->resolveReferences(Sub); 2603 FoundUnresolved |= Sub.FoundUnresolved; 2604 } 2605 } 2606 2607 if (!I) 2608 FoundUnresolved = true; 2609 return I; 2610 } 2611 2612 Init *HasReferenceResolver::resolve(Init *VarName) 2613 { 2614 if (VarName == VarNameToTrack) 2615 Found = true; 2616 return nullptr; 2617 } 2618