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