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