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