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/StringExtras.h" 19 #include "llvm/ADT/StringMap.h" 20 #include "llvm/ADT/StringRef.h" 21 #include "llvm/Config/llvm-config.h" 22 #include "llvm/Support/Allocator.h" 23 #include "llvm/Support/Casting.h" 24 #include "llvm/Support/Compiler.h" 25 #include "llvm/Support/ErrorHandling.h" 26 #include "llvm/Support/SMLoc.h" 27 #include "llvm/Support/raw_ostream.h" 28 #include "llvm/TableGen/Error.h" 29 #include "llvm/TableGen/Record.h" 30 #include <cassert> 31 #include <cstdint> 32 #include <memory> 33 #include <string> 34 #include <utility> 35 #include <vector> 36 37 using namespace llvm; 38 39 static BumpPtrAllocator Allocator; 40 41 //===----------------------------------------------------------------------===// 42 // Type implementations 43 //===----------------------------------------------------------------------===// 44 45 BitRecTy BitRecTy::Shared; 46 CodeRecTy CodeRecTy::Shared; 47 IntRecTy IntRecTy::Shared; 48 StringRecTy StringRecTy::Shared; 49 DagRecTy DagRecTy::Shared; 50 51 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 52 LLVM_DUMP_METHOD void RecTy::dump() const { print(errs()); } 53 #endif 54 55 ListRecTy *RecTy::getListTy() { 56 if (!ListTy) 57 ListTy = new(Allocator) ListRecTy(this); 58 return ListTy; 59 } 60 61 bool RecTy::typeIsConvertibleTo(const RecTy *RHS) const { 62 assert(RHS && "NULL pointer"); 63 return Kind == RHS->getRecTyKind(); 64 } 65 66 bool RecTy::typeIsA(const RecTy *RHS) const { return this == RHS; } 67 68 bool BitRecTy::typeIsConvertibleTo(const RecTy *RHS) const{ 69 if (RecTy::typeIsConvertibleTo(RHS) || RHS->getRecTyKind() == IntRecTyKind) 70 return true; 71 if (const BitsRecTy *BitsTy = dyn_cast<BitsRecTy>(RHS)) 72 return BitsTy->getNumBits() == 1; 73 return false; 74 } 75 76 BitsRecTy *BitsRecTy::get(unsigned Sz) { 77 static std::vector<BitsRecTy*> Shared; 78 if (Sz >= Shared.size()) 79 Shared.resize(Sz + 1); 80 BitsRecTy *&Ty = Shared[Sz]; 81 if (!Ty) 82 Ty = new(Allocator) BitsRecTy(Sz); 83 return Ty; 84 } 85 86 std::string BitsRecTy::getAsString() const { 87 return "bits<" + utostr(Size) + ">"; 88 } 89 90 bool BitsRecTy::typeIsConvertibleTo(const RecTy *RHS) const { 91 if (RecTy::typeIsConvertibleTo(RHS)) //argument and the sender are same type 92 return cast<BitsRecTy>(RHS)->Size == Size; 93 RecTyKind kind = RHS->getRecTyKind(); 94 return (kind == BitRecTyKind && Size == 1) || (kind == IntRecTyKind); 95 } 96 97 bool BitsRecTy::typeIsA(const RecTy *RHS) const { 98 if (const BitsRecTy *RHSb = dyn_cast<BitsRecTy>(RHS)) 99 return RHSb->Size == Size; 100 return false; 101 } 102 103 bool IntRecTy::typeIsConvertibleTo(const RecTy *RHS) const { 104 RecTyKind kind = RHS->getRecTyKind(); 105 return kind==BitRecTyKind || kind==BitsRecTyKind || kind==IntRecTyKind; 106 } 107 108 bool CodeRecTy::typeIsConvertibleTo(const RecTy *RHS) const { 109 RecTyKind Kind = RHS->getRecTyKind(); 110 return Kind == CodeRecTyKind || Kind == StringRecTyKind; 111 } 112 113 std::string StringRecTy::getAsString() const { 114 return "string"; 115 } 116 117 bool StringRecTy::typeIsConvertibleTo(const RecTy *RHS) const { 118 RecTyKind Kind = RHS->getRecTyKind(); 119 return Kind == StringRecTyKind || Kind == CodeRecTyKind; 120 } 121 122 std::string ListRecTy::getAsString() const { 123 return "list<" + Ty->getAsString() + ">"; 124 } 125 126 bool ListRecTy::typeIsConvertibleTo(const RecTy *RHS) const { 127 if (const auto *ListTy = dyn_cast<ListRecTy>(RHS)) 128 return Ty->typeIsConvertibleTo(ListTy->getElementType()); 129 return false; 130 } 131 132 bool ListRecTy::typeIsA(const RecTy *RHS) const { 133 if (const ListRecTy *RHSl = dyn_cast<ListRecTy>(RHS)) 134 return getElementType()->typeIsA(RHSl->getElementType()); 135 return false; 136 } 137 138 std::string DagRecTy::getAsString() const { 139 return "dag"; 140 } 141 142 static void ProfileRecordRecTy(FoldingSetNodeID &ID, 143 ArrayRef<Record *> Classes) { 144 ID.AddInteger(Classes.size()); 145 for (Record *R : Classes) 146 ID.AddPointer(R); 147 } 148 149 RecordRecTy *RecordRecTy::get(ArrayRef<Record *> UnsortedClasses) { 150 if (UnsortedClasses.empty()) { 151 static RecordRecTy AnyRecord(0); 152 return &AnyRecord; 153 } 154 155 FoldingSet<RecordRecTy> &ThePool = 156 UnsortedClasses[0]->getRecords().RecordTypePool; 157 158 SmallVector<Record *, 4> Classes(UnsortedClasses.begin(), 159 UnsortedClasses.end()); 160 llvm::sort(Classes, [](Record *LHS, Record *RHS) { 161 return LHS->getNameInitAsString() < RHS->getNameInitAsString(); 162 }); 163 164 FoldingSetNodeID ID; 165 ProfileRecordRecTy(ID, Classes); 166 167 void *IP = nullptr; 168 if (RecordRecTy *Ty = ThePool.FindNodeOrInsertPos(ID, IP)) 169 return Ty; 170 171 #ifndef NDEBUG 172 // Check for redundancy. 173 for (unsigned i = 0; i < Classes.size(); ++i) { 174 for (unsigned j = 0; j < Classes.size(); ++j) { 175 assert(i == j || !Classes[i]->isSubClassOf(Classes[j])); 176 } 177 assert(&Classes[0]->getRecords() == &Classes[i]->getRecords()); 178 } 179 #endif 180 181 void *Mem = Allocator.Allocate(totalSizeToAlloc<Record *>(Classes.size()), 182 alignof(RecordRecTy)); 183 RecordRecTy *Ty = new(Mem) RecordRecTy(Classes.size()); 184 std::uninitialized_copy(Classes.begin(), Classes.end(), 185 Ty->getTrailingObjects<Record *>()); 186 ThePool.InsertNode(Ty, IP); 187 return Ty; 188 } 189 190 void RecordRecTy::Profile(FoldingSetNodeID &ID) const { 191 ProfileRecordRecTy(ID, getClasses()); 192 } 193 194 std::string RecordRecTy::getAsString() const { 195 if (NumClasses == 1) 196 return getClasses()[0]->getNameInitAsString(); 197 198 std::string Str = "{"; 199 bool First = true; 200 for (Record *R : getClasses()) { 201 if (!First) 202 Str += ", "; 203 First = false; 204 Str += R->getNameInitAsString(); 205 } 206 Str += "}"; 207 return Str; 208 } 209 210 bool RecordRecTy::isSubClassOf(Record *Class) const { 211 return llvm::any_of(getClasses(), [Class](Record *MySuperClass) { 212 return MySuperClass == Class || 213 MySuperClass->isSubClassOf(Class); 214 }); 215 } 216 217 bool RecordRecTy::typeIsConvertibleTo(const RecTy *RHS) const { 218 if (this == RHS) 219 return true; 220 221 const RecordRecTy *RTy = dyn_cast<RecordRecTy>(RHS); 222 if (!RTy) 223 return false; 224 225 return llvm::all_of(RTy->getClasses(), [this](Record *TargetClass) { 226 return isSubClassOf(TargetClass); 227 }); 228 } 229 230 bool RecordRecTy::typeIsA(const RecTy *RHS) const { 231 return typeIsConvertibleTo(RHS); 232 } 233 234 static RecordRecTy *resolveRecordTypes(RecordRecTy *T1, RecordRecTy *T2) { 235 SmallVector<Record *, 4> CommonSuperClasses; 236 SmallVector<Record *, 4> Stack; 237 238 Stack.insert(Stack.end(), T1->classes_begin(), T1->classes_end()); 239 240 while (!Stack.empty()) { 241 Record *R = Stack.back(); 242 Stack.pop_back(); 243 244 if (T2->isSubClassOf(R)) { 245 CommonSuperClasses.push_back(R); 246 } else { 247 R->getDirectSuperClasses(Stack); 248 } 249 } 250 251 return RecordRecTy::get(CommonSuperClasses); 252 } 253 254 RecTy *llvm::resolveTypes(RecTy *T1, RecTy *T2) { 255 if (T1 == T2) 256 return T1; 257 258 if (RecordRecTy *RecTy1 = dyn_cast<RecordRecTy>(T1)) { 259 if (RecordRecTy *RecTy2 = dyn_cast<RecordRecTy>(T2)) 260 return resolveRecordTypes(RecTy1, RecTy2); 261 } 262 263 if (T1->typeIsConvertibleTo(T2)) 264 return T2; 265 if (T2->typeIsConvertibleTo(T1)) 266 return T1; 267 268 if (ListRecTy *ListTy1 = dyn_cast<ListRecTy>(T1)) { 269 if (ListRecTy *ListTy2 = dyn_cast<ListRecTy>(T2)) { 270 RecTy* NewType = resolveTypes(ListTy1->getElementType(), 271 ListTy2->getElementType()); 272 if (NewType) 273 return NewType->getListTy(); 274 } 275 } 276 277 return nullptr; 278 } 279 280 //===----------------------------------------------------------------------===// 281 // Initializer implementations 282 //===----------------------------------------------------------------------===// 283 284 void Init::anchor() {} 285 286 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 287 LLVM_DUMP_METHOD void Init::dump() const { return print(errs()); } 288 #endif 289 290 UnsetInit *UnsetInit::get() { 291 static UnsetInit TheInit; 292 return &TheInit; 293 } 294 295 Init *UnsetInit::getCastTo(RecTy *Ty) const { 296 return const_cast<UnsetInit *>(this); 297 } 298 299 Init *UnsetInit::convertInitializerTo(RecTy *Ty) const { 300 return const_cast<UnsetInit *>(this); 301 } 302 303 BitInit *BitInit::get(bool V) { 304 static BitInit True(true); 305 static BitInit False(false); 306 307 return V ? &True : &False; 308 } 309 310 Init *BitInit::convertInitializerTo(RecTy *Ty) const { 311 if (isa<BitRecTy>(Ty)) 312 return const_cast<BitInit *>(this); 313 314 if (isa<IntRecTy>(Ty)) 315 return IntInit::get(getValue()); 316 317 if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) { 318 // Can only convert single bit. 319 if (BRT->getNumBits() == 1) 320 return BitsInit::get(const_cast<BitInit *>(this)); 321 } 322 323 return nullptr; 324 } 325 326 static void 327 ProfileBitsInit(FoldingSetNodeID &ID, ArrayRef<Init *> Range) { 328 ID.AddInteger(Range.size()); 329 330 for (Init *I : Range) 331 ID.AddPointer(I); 332 } 333 334 BitsInit *BitsInit::get(ArrayRef<Init *> Range) { 335 static FoldingSet<BitsInit> ThePool; 336 337 FoldingSetNodeID ID; 338 ProfileBitsInit(ID, Range); 339 340 void *IP = nullptr; 341 if (BitsInit *I = ThePool.FindNodeOrInsertPos(ID, IP)) 342 return I; 343 344 void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *>(Range.size()), 345 alignof(BitsInit)); 346 BitsInit *I = new(Mem) BitsInit(Range.size()); 347 std::uninitialized_copy(Range.begin(), Range.end(), 348 I->getTrailingObjects<Init *>()); 349 ThePool.InsertNode(I, IP); 350 return I; 351 } 352 353 void BitsInit::Profile(FoldingSetNodeID &ID) const { 354 ProfileBitsInit(ID, makeArrayRef(getTrailingObjects<Init *>(), NumBits)); 355 } 356 357 Init *BitsInit::convertInitializerTo(RecTy *Ty) const { 358 if (isa<BitRecTy>(Ty)) { 359 if (getNumBits() != 1) return nullptr; // Only accept if just one bit! 360 return getBit(0); 361 } 362 363 if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) { 364 // If the number of bits is right, return it. Otherwise we need to expand 365 // or truncate. 366 if (getNumBits() != BRT->getNumBits()) return nullptr; 367 return const_cast<BitsInit *>(this); 368 } 369 370 if (isa<IntRecTy>(Ty)) { 371 int64_t Result = 0; 372 for (unsigned i = 0, e = getNumBits(); i != e; ++i) 373 if (auto *Bit = dyn_cast<BitInit>(getBit(i))) 374 Result |= static_cast<int64_t>(Bit->getValue()) << i; 375 else 376 return nullptr; 377 return IntInit::get(Result); 378 } 379 380 return nullptr; 381 } 382 383 Init * 384 BitsInit::convertInitializerBitRange(ArrayRef<unsigned> Bits) const { 385 SmallVector<Init *, 16> NewBits(Bits.size()); 386 387 for (unsigned i = 0, e = Bits.size(); i != e; ++i) { 388 if (Bits[i] >= getNumBits()) 389 return nullptr; 390 NewBits[i] = getBit(Bits[i]); 391 } 392 return BitsInit::get(NewBits); 393 } 394 395 bool BitsInit::isConcrete() const { 396 for (unsigned i = 0, e = getNumBits(); i != e; ++i) { 397 if (!getBit(i)->isConcrete()) 398 return false; 399 } 400 return true; 401 } 402 403 std::string BitsInit::getAsString() const { 404 std::string Result = "{ "; 405 for (unsigned i = 0, e = getNumBits(); i != e; ++i) { 406 if (i) Result += ", "; 407 if (Init *Bit = getBit(e-i-1)) 408 Result += Bit->getAsString(); 409 else 410 Result += "*"; 411 } 412 return Result + " }"; 413 } 414 415 // resolveReferences - If there are any field references that refer to fields 416 // that have been filled in, we can propagate the values now. 417 Init *BitsInit::resolveReferences(Resolver &R) const { 418 bool Changed = false; 419 SmallVector<Init *, 16> NewBits(getNumBits()); 420 421 Init *CachedBitVarRef = nullptr; 422 Init *CachedBitVarResolved = nullptr; 423 424 for (unsigned i = 0, e = getNumBits(); i != e; ++i) { 425 Init *CurBit = getBit(i); 426 Init *NewBit = CurBit; 427 428 if (VarBitInit *CurBitVar = dyn_cast<VarBitInit>(CurBit)) { 429 if (CurBitVar->getBitVar() != CachedBitVarRef) { 430 CachedBitVarRef = CurBitVar->getBitVar(); 431 CachedBitVarResolved = CachedBitVarRef->resolveReferences(R); 432 } 433 434 NewBit = CachedBitVarResolved->getBit(CurBitVar->getBitNum()); 435 } else { 436 // getBit(0) implicitly converts int and bits<1> values to bit. 437 NewBit = CurBit->resolveReferences(R)->getBit(0); 438 } 439 440 if (isa<UnsetInit>(NewBit) && R.keepUnsetBits()) 441 NewBit = CurBit; 442 NewBits[i] = NewBit; 443 Changed |= CurBit != NewBit; 444 } 445 446 if (Changed) 447 return BitsInit::get(NewBits); 448 449 return const_cast<BitsInit *>(this); 450 } 451 452 IntInit *IntInit::get(int64_t V) { 453 static DenseMap<int64_t, IntInit*> ThePool; 454 455 IntInit *&I = ThePool[V]; 456 if (!I) I = new(Allocator) IntInit(V); 457 return I; 458 } 459 460 std::string IntInit::getAsString() const { 461 return itostr(Value); 462 } 463 464 static bool canFitInBitfield(int64_t Value, unsigned NumBits) { 465 // For example, with NumBits == 4, we permit Values from [-7 .. 15]. 466 return (NumBits >= sizeof(Value) * 8) || 467 (Value >> NumBits == 0) || (Value >> (NumBits-1) == -1); 468 } 469 470 Init *IntInit::convertInitializerTo(RecTy *Ty) const { 471 if (isa<IntRecTy>(Ty)) 472 return const_cast<IntInit *>(this); 473 474 if (isa<BitRecTy>(Ty)) { 475 int64_t Val = getValue(); 476 if (Val != 0 && Val != 1) return nullptr; // Only accept 0 or 1 for a bit! 477 return BitInit::get(Val != 0); 478 } 479 480 if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) { 481 int64_t Value = getValue(); 482 // Make sure this bitfield is large enough to hold the integer value. 483 if (!canFitInBitfield(Value, BRT->getNumBits())) 484 return nullptr; 485 486 SmallVector<Init *, 16> NewBits(BRT->getNumBits()); 487 for (unsigned i = 0; i != BRT->getNumBits(); ++i) 488 NewBits[i] = BitInit::get(Value & ((i < 64) ? (1LL << i) : 0)); 489 490 return BitsInit::get(NewBits); 491 } 492 493 return nullptr; 494 } 495 496 Init * 497 IntInit::convertInitializerBitRange(ArrayRef<unsigned> Bits) const { 498 SmallVector<Init *, 16> NewBits(Bits.size()); 499 500 for (unsigned i = 0, e = Bits.size(); i != e; ++i) { 501 if (Bits[i] >= 64) 502 return nullptr; 503 504 NewBits[i] = BitInit::get(Value & (INT64_C(1) << Bits[i])); 505 } 506 return BitsInit::get(NewBits); 507 } 508 509 CodeInit *CodeInit::get(StringRef V) { 510 static StringMap<CodeInit*, BumpPtrAllocator &> ThePool(Allocator); 511 512 auto &Entry = *ThePool.insert(std::make_pair(V, nullptr)).first; 513 if (!Entry.second) 514 Entry.second = new(Allocator) CodeInit(Entry.getKey()); 515 return Entry.second; 516 } 517 518 StringInit *StringInit::get(StringRef V) { 519 static StringMap<StringInit*, BumpPtrAllocator &> ThePool(Allocator); 520 521 auto &Entry = *ThePool.insert(std::make_pair(V, nullptr)).first; 522 if (!Entry.second) 523 Entry.second = new(Allocator) StringInit(Entry.getKey()); 524 return Entry.second; 525 } 526 527 Init *StringInit::convertInitializerTo(RecTy *Ty) const { 528 if (isa<StringRecTy>(Ty)) 529 return const_cast<StringInit *>(this); 530 if (isa<CodeRecTy>(Ty)) 531 return CodeInit::get(getValue()); 532 533 return nullptr; 534 } 535 536 Init *CodeInit::convertInitializerTo(RecTy *Ty) const { 537 if (isa<CodeRecTy>(Ty)) 538 return const_cast<CodeInit *>(this); 539 if (isa<StringRecTy>(Ty)) 540 return StringInit::get(getValue()); 541 542 return nullptr; 543 } 544 545 static void ProfileListInit(FoldingSetNodeID &ID, 546 ArrayRef<Init *> Range, 547 RecTy *EltTy) { 548 ID.AddInteger(Range.size()); 549 ID.AddPointer(EltTy); 550 551 for (Init *I : Range) 552 ID.AddPointer(I); 553 } 554 555 ListInit *ListInit::get(ArrayRef<Init *> Range, RecTy *EltTy) { 556 static FoldingSet<ListInit> ThePool; 557 558 FoldingSetNodeID ID; 559 ProfileListInit(ID, Range, EltTy); 560 561 void *IP = nullptr; 562 if (ListInit *I = ThePool.FindNodeOrInsertPos(ID, IP)) 563 return I; 564 565 assert(Range.empty() || !isa<TypedInit>(Range[0]) || 566 cast<TypedInit>(Range[0])->getType()->typeIsConvertibleTo(EltTy)); 567 568 void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *>(Range.size()), 569 alignof(ListInit)); 570 ListInit *I = new(Mem) ListInit(Range.size(), EltTy); 571 std::uninitialized_copy(Range.begin(), Range.end(), 572 I->getTrailingObjects<Init *>()); 573 ThePool.InsertNode(I, IP); 574 return I; 575 } 576 577 void ListInit::Profile(FoldingSetNodeID &ID) const { 578 RecTy *EltTy = cast<ListRecTy>(getType())->getElementType(); 579 580 ProfileListInit(ID, getValues(), EltTy); 581 } 582 583 Init *ListInit::convertInitializerTo(RecTy *Ty) const { 584 if (getType() == Ty) 585 return const_cast<ListInit*>(this); 586 587 if (auto *LRT = dyn_cast<ListRecTy>(Ty)) { 588 SmallVector<Init*, 8> Elements; 589 Elements.reserve(getValues().size()); 590 591 // Verify that all of the elements of the list are subclasses of the 592 // appropriate class! 593 bool Changed = false; 594 RecTy *ElementType = LRT->getElementType(); 595 for (Init *I : getValues()) 596 if (Init *CI = I->convertInitializerTo(ElementType)) { 597 Elements.push_back(CI); 598 if (CI != I) 599 Changed = true; 600 } else 601 return nullptr; 602 603 if (!Changed) 604 return const_cast<ListInit*>(this); 605 return ListInit::get(Elements, ElementType); 606 } 607 608 return nullptr; 609 } 610 611 Init *ListInit::convertInitListSlice(ArrayRef<unsigned> Elements) const { 612 SmallVector<Init*, 8> Vals; 613 Vals.reserve(Elements.size()); 614 for (unsigned Element : Elements) { 615 if (Element >= size()) 616 return nullptr; 617 Vals.push_back(getElement(Element)); 618 } 619 return ListInit::get(Vals, getElementType()); 620 } 621 622 Record *ListInit::getElementAsRecord(unsigned i) const { 623 assert(i < NumValues && "List element index out of range!"); 624 DefInit *DI = dyn_cast<DefInit>(getElement(i)); 625 if (!DI) 626 PrintFatalError("Expected record in list!"); 627 return DI->getDef(); 628 } 629 630 Init *ListInit::resolveReferences(Resolver &R) const { 631 SmallVector<Init*, 8> Resolved; 632 Resolved.reserve(size()); 633 bool Changed = false; 634 635 for (Init *CurElt : getValues()) { 636 Init *E = CurElt->resolveReferences(R); 637 Changed |= E != CurElt; 638 Resolved.push_back(E); 639 } 640 641 if (Changed) 642 return ListInit::get(Resolved, getElementType()); 643 return const_cast<ListInit *>(this); 644 } 645 646 bool ListInit::isConcrete() const { 647 for (Init *Element : *this) { 648 if (!Element->isConcrete()) 649 return false; 650 } 651 return true; 652 } 653 654 std::string ListInit::getAsString() const { 655 std::string Result = "["; 656 const char *sep = ""; 657 for (Init *Element : *this) { 658 Result += sep; 659 sep = ", "; 660 Result += Element->getAsString(); 661 } 662 return Result + "]"; 663 } 664 665 Init *OpInit::getBit(unsigned Bit) const { 666 if (getType() == BitRecTy::get()) 667 return const_cast<OpInit*>(this); 668 return VarBitInit::get(const_cast<OpInit*>(this), Bit); 669 } 670 671 static void 672 ProfileUnOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *Op, RecTy *Type) { 673 ID.AddInteger(Opcode); 674 ID.AddPointer(Op); 675 ID.AddPointer(Type); 676 } 677 678 UnOpInit *UnOpInit::get(UnaryOp Opc, Init *LHS, RecTy *Type) { 679 static FoldingSet<UnOpInit> ThePool; 680 681 FoldingSetNodeID ID; 682 ProfileUnOpInit(ID, Opc, LHS, Type); 683 684 void *IP = nullptr; 685 if (UnOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP)) 686 return I; 687 688 UnOpInit *I = new(Allocator) UnOpInit(Opc, LHS, Type); 689 ThePool.InsertNode(I, IP); 690 return I; 691 } 692 693 void UnOpInit::Profile(FoldingSetNodeID &ID) const { 694 ProfileUnOpInit(ID, getOpcode(), getOperand(), getType()); 695 } 696 697 Init *UnOpInit::Fold(Record *CurRec, bool IsFinal) const { 698 switch (getOpcode()) { 699 case CAST: 700 if (isa<StringRecTy>(getType())) { 701 if (StringInit *LHSs = dyn_cast<StringInit>(LHS)) 702 return LHSs; 703 704 if (DefInit *LHSd = dyn_cast<DefInit>(LHS)) 705 return StringInit::get(LHSd->getAsString()); 706 707 if (IntInit *LHSi = dyn_cast<IntInit>(LHS)) 708 return StringInit::get(LHSi->getAsString()); 709 } else if (isa<RecordRecTy>(getType())) { 710 if (StringInit *Name = dyn_cast<StringInit>(LHS)) { 711 if (!CurRec && !IsFinal) 712 break; 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 bool UnquotedLHS = false; 1200 switch (getOpcode()) { 1201 case SUBST: Result = "!subst"; break; 1202 case FOREACH: Result = "!foreach"; UnquotedLHS = true; break; 1203 case IF: Result = "!if"; break; 1204 case DAG: Result = "!dag"; break; 1205 } 1206 return (Result + "(" + 1207 (UnquotedLHS ? LHS->getAsUnquotedString() : LHS->getAsString()) + 1208 ", " + MHS->getAsString() + ", " + RHS->getAsString() + ")"); 1209 } 1210 1211 static void ProfileFoldOpInit(FoldingSetNodeID &ID, Init *A, Init *B, 1212 Init *Start, Init *List, Init *Expr, 1213 RecTy *Type) { 1214 ID.AddPointer(Start); 1215 ID.AddPointer(List); 1216 ID.AddPointer(A); 1217 ID.AddPointer(B); 1218 ID.AddPointer(Expr); 1219 ID.AddPointer(Type); 1220 } 1221 1222 FoldOpInit *FoldOpInit::get(Init *Start, Init *List, Init *A, Init *B, 1223 Init *Expr, RecTy *Type) { 1224 static FoldingSet<FoldOpInit> ThePool; 1225 1226 FoldingSetNodeID ID; 1227 ProfileFoldOpInit(ID, Start, List, A, B, Expr, Type); 1228 1229 void *IP = nullptr; 1230 if (FoldOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP)) 1231 return I; 1232 1233 FoldOpInit *I = new (Allocator) FoldOpInit(Start, List, A, B, Expr, Type); 1234 ThePool.InsertNode(I, IP); 1235 return I; 1236 } 1237 1238 void FoldOpInit::Profile(FoldingSetNodeID &ID) const { 1239 ProfileFoldOpInit(ID, Start, List, A, B, Expr, getType()); 1240 } 1241 1242 Init *FoldOpInit::Fold(Record *CurRec) const { 1243 if (ListInit *LI = dyn_cast<ListInit>(List)) { 1244 Init *Accum = Start; 1245 for (Init *Elt : *LI) { 1246 MapResolver R(CurRec); 1247 R.set(A, Accum); 1248 R.set(B, Elt); 1249 Accum = Expr->resolveReferences(R); 1250 } 1251 return Accum; 1252 } 1253 return const_cast<FoldOpInit *>(this); 1254 } 1255 1256 Init *FoldOpInit::resolveReferences(Resolver &R) const { 1257 Init *NewStart = Start->resolveReferences(R); 1258 Init *NewList = List->resolveReferences(R); 1259 ShadowResolver SR(R); 1260 SR.addShadow(A); 1261 SR.addShadow(B); 1262 Init *NewExpr = Expr->resolveReferences(SR); 1263 1264 if (Start == NewStart && List == NewList && Expr == NewExpr) 1265 return const_cast<FoldOpInit *>(this); 1266 1267 return get(NewStart, NewList, A, B, NewExpr, getType()) 1268 ->Fold(R.getCurrentRecord()); 1269 } 1270 1271 Init *FoldOpInit::getBit(unsigned Bit) const { 1272 return VarBitInit::get(const_cast<FoldOpInit *>(this), Bit); 1273 } 1274 1275 std::string FoldOpInit::getAsString() const { 1276 return (Twine("!foldl(") + Start->getAsString() + ", " + List->getAsString() + 1277 ", " + A->getAsUnquotedString() + ", " + B->getAsUnquotedString() + 1278 ", " + Expr->getAsString() + ")") 1279 .str(); 1280 } 1281 1282 static void ProfileIsAOpInit(FoldingSetNodeID &ID, RecTy *CheckType, 1283 Init *Expr) { 1284 ID.AddPointer(CheckType); 1285 ID.AddPointer(Expr); 1286 } 1287 1288 IsAOpInit *IsAOpInit::get(RecTy *CheckType, Init *Expr) { 1289 static FoldingSet<IsAOpInit> ThePool; 1290 1291 FoldingSetNodeID ID; 1292 ProfileIsAOpInit(ID, CheckType, Expr); 1293 1294 void *IP = nullptr; 1295 if (IsAOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP)) 1296 return I; 1297 1298 IsAOpInit *I = new (Allocator) IsAOpInit(CheckType, Expr); 1299 ThePool.InsertNode(I, IP); 1300 return I; 1301 } 1302 1303 void IsAOpInit::Profile(FoldingSetNodeID &ID) const { 1304 ProfileIsAOpInit(ID, CheckType, Expr); 1305 } 1306 1307 Init *IsAOpInit::Fold() const { 1308 if (TypedInit *TI = dyn_cast<TypedInit>(Expr)) { 1309 // Is the expression type known to be (a subclass of) the desired type? 1310 if (TI->getType()->typeIsConvertibleTo(CheckType)) 1311 return IntInit::get(1); 1312 1313 if (isa<RecordRecTy>(CheckType)) { 1314 // If the target type is not a subclass of the expression type, or if 1315 // the expression has fully resolved to a record, we know that it can't 1316 // be of the required type. 1317 if (!CheckType->typeIsConvertibleTo(TI->getType()) || isa<DefInit>(Expr)) 1318 return IntInit::get(0); 1319 } else { 1320 // We treat non-record types as not castable. 1321 return IntInit::get(0); 1322 } 1323 } 1324 return const_cast<IsAOpInit *>(this); 1325 } 1326 1327 Init *IsAOpInit::resolveReferences(Resolver &R) const { 1328 Init *NewExpr = Expr->resolveReferences(R); 1329 if (Expr != NewExpr) 1330 return get(CheckType, NewExpr)->Fold(); 1331 return const_cast<IsAOpInit *>(this); 1332 } 1333 1334 Init *IsAOpInit::getBit(unsigned Bit) const { 1335 return VarBitInit::get(const_cast<IsAOpInit *>(this), Bit); 1336 } 1337 1338 std::string IsAOpInit::getAsString() const { 1339 return (Twine("!isa<") + CheckType->getAsString() + ">(" + 1340 Expr->getAsString() + ")") 1341 .str(); 1342 } 1343 1344 RecTy *TypedInit::getFieldType(StringInit *FieldName) const { 1345 if (RecordRecTy *RecordType = dyn_cast<RecordRecTy>(getType())) { 1346 for (Record *Rec : RecordType->getClasses()) { 1347 if (RecordVal *Field = Rec->getValue(FieldName)) 1348 return Field->getType(); 1349 } 1350 } 1351 return nullptr; 1352 } 1353 1354 Init * 1355 TypedInit::convertInitializerTo(RecTy *Ty) const { 1356 if (getType() == Ty || getType()->typeIsA(Ty)) 1357 return const_cast<TypedInit *>(this); 1358 1359 if (isa<BitRecTy>(getType()) && isa<BitsRecTy>(Ty) && 1360 cast<BitsRecTy>(Ty)->getNumBits() == 1) 1361 return BitsInit::get({const_cast<TypedInit *>(this)}); 1362 1363 return nullptr; 1364 } 1365 1366 Init *TypedInit::convertInitializerBitRange(ArrayRef<unsigned> Bits) const { 1367 BitsRecTy *T = dyn_cast<BitsRecTy>(getType()); 1368 if (!T) return nullptr; // Cannot subscript a non-bits variable. 1369 unsigned NumBits = T->getNumBits(); 1370 1371 SmallVector<Init *, 16> NewBits; 1372 NewBits.reserve(Bits.size()); 1373 for (unsigned Bit : Bits) { 1374 if (Bit >= NumBits) 1375 return nullptr; 1376 1377 NewBits.push_back(VarBitInit::get(const_cast<TypedInit *>(this), Bit)); 1378 } 1379 return BitsInit::get(NewBits); 1380 } 1381 1382 Init *TypedInit::getCastTo(RecTy *Ty) const { 1383 // Handle the common case quickly 1384 if (getType() == Ty || getType()->typeIsA(Ty)) 1385 return const_cast<TypedInit *>(this); 1386 1387 if (Init *Converted = convertInitializerTo(Ty)) { 1388 assert(!isa<TypedInit>(Converted) || 1389 cast<TypedInit>(Converted)->getType()->typeIsA(Ty)); 1390 return Converted; 1391 } 1392 1393 if (!getType()->typeIsConvertibleTo(Ty)) 1394 return nullptr; 1395 1396 return UnOpInit::get(UnOpInit::CAST, const_cast<TypedInit *>(this), Ty) 1397 ->Fold(nullptr); 1398 } 1399 1400 Init *TypedInit::convertInitListSlice(ArrayRef<unsigned> Elements) const { 1401 ListRecTy *T = dyn_cast<ListRecTy>(getType()); 1402 if (!T) return nullptr; // Cannot subscript a non-list variable. 1403 1404 if (Elements.size() == 1) 1405 return VarListElementInit::get(const_cast<TypedInit *>(this), Elements[0]); 1406 1407 SmallVector<Init*, 8> ListInits; 1408 ListInits.reserve(Elements.size()); 1409 for (unsigned Element : Elements) 1410 ListInits.push_back(VarListElementInit::get(const_cast<TypedInit *>(this), 1411 Element)); 1412 return ListInit::get(ListInits, T->getElementType()); 1413 } 1414 1415 1416 VarInit *VarInit::get(StringRef VN, RecTy *T) { 1417 Init *Value = StringInit::get(VN); 1418 return VarInit::get(Value, T); 1419 } 1420 1421 VarInit *VarInit::get(Init *VN, RecTy *T) { 1422 using Key = std::pair<RecTy *, Init *>; 1423 static DenseMap<Key, VarInit*> ThePool; 1424 1425 Key TheKey(std::make_pair(T, VN)); 1426 1427 VarInit *&I = ThePool[TheKey]; 1428 if (!I) 1429 I = new(Allocator) VarInit(VN, T); 1430 return I; 1431 } 1432 1433 StringRef VarInit::getName() const { 1434 StringInit *NameString = cast<StringInit>(getNameInit()); 1435 return NameString->getValue(); 1436 } 1437 1438 Init *VarInit::getBit(unsigned Bit) const { 1439 if (getType() == BitRecTy::get()) 1440 return const_cast<VarInit*>(this); 1441 return VarBitInit::get(const_cast<VarInit*>(this), Bit); 1442 } 1443 1444 Init *VarInit::resolveReferences(Resolver &R) const { 1445 if (Init *Val = R.resolve(VarName)) 1446 return Val; 1447 return const_cast<VarInit *>(this); 1448 } 1449 1450 VarBitInit *VarBitInit::get(TypedInit *T, unsigned B) { 1451 using Key = std::pair<TypedInit *, unsigned>; 1452 static DenseMap<Key, VarBitInit*> ThePool; 1453 1454 Key TheKey(std::make_pair(T, B)); 1455 1456 VarBitInit *&I = ThePool[TheKey]; 1457 if (!I) 1458 I = new(Allocator) VarBitInit(T, B); 1459 return I; 1460 } 1461 1462 std::string VarBitInit::getAsString() const { 1463 return TI->getAsString() + "{" + utostr(Bit) + "}"; 1464 } 1465 1466 Init *VarBitInit::resolveReferences(Resolver &R) const { 1467 Init *I = TI->resolveReferences(R); 1468 if (TI != I) 1469 return I->getBit(getBitNum()); 1470 1471 return const_cast<VarBitInit*>(this); 1472 } 1473 1474 VarListElementInit *VarListElementInit::get(TypedInit *T, 1475 unsigned E) { 1476 using Key = std::pair<TypedInit *, unsigned>; 1477 static DenseMap<Key, VarListElementInit*> ThePool; 1478 1479 Key TheKey(std::make_pair(T, E)); 1480 1481 VarListElementInit *&I = ThePool[TheKey]; 1482 if (!I) I = new(Allocator) VarListElementInit(T, E); 1483 return I; 1484 } 1485 1486 std::string VarListElementInit::getAsString() const { 1487 return TI->getAsString() + "[" + utostr(Element) + "]"; 1488 } 1489 1490 Init *VarListElementInit::resolveReferences(Resolver &R) const { 1491 Init *NewTI = TI->resolveReferences(R); 1492 if (ListInit *List = dyn_cast<ListInit>(NewTI)) { 1493 // Leave out-of-bounds array references as-is. This can happen without 1494 // being an error, e.g. in the untaken "branch" of an !if expression. 1495 if (getElementNum() < List->size()) 1496 return List->getElement(getElementNum()); 1497 } 1498 if (NewTI != TI && isa<TypedInit>(NewTI)) 1499 return VarListElementInit::get(cast<TypedInit>(NewTI), getElementNum()); 1500 return const_cast<VarListElementInit *>(this); 1501 } 1502 1503 Init *VarListElementInit::getBit(unsigned Bit) const { 1504 if (getType() == BitRecTy::get()) 1505 return const_cast<VarListElementInit*>(this); 1506 return VarBitInit::get(const_cast<VarListElementInit*>(this), Bit); 1507 } 1508 1509 DefInit::DefInit(Record *D) 1510 : TypedInit(IK_DefInit, D->getType()), Def(D) {} 1511 1512 DefInit *DefInit::get(Record *R) { 1513 return R->getDefInit(); 1514 } 1515 1516 Init *DefInit::convertInitializerTo(RecTy *Ty) const { 1517 if (auto *RRT = dyn_cast<RecordRecTy>(Ty)) 1518 if (getType()->typeIsConvertibleTo(RRT)) 1519 return const_cast<DefInit *>(this); 1520 return nullptr; 1521 } 1522 1523 RecTy *DefInit::getFieldType(StringInit *FieldName) const { 1524 if (const RecordVal *RV = Def->getValue(FieldName)) 1525 return RV->getType(); 1526 return nullptr; 1527 } 1528 1529 std::string DefInit::getAsString() const { 1530 return Def->getName(); 1531 } 1532 1533 static void ProfileVarDefInit(FoldingSetNodeID &ID, 1534 Record *Class, 1535 ArrayRef<Init *> Args) { 1536 ID.AddInteger(Args.size()); 1537 ID.AddPointer(Class); 1538 1539 for (Init *I : Args) 1540 ID.AddPointer(I); 1541 } 1542 1543 VarDefInit *VarDefInit::get(Record *Class, ArrayRef<Init *> Args) { 1544 static FoldingSet<VarDefInit> ThePool; 1545 1546 FoldingSetNodeID ID; 1547 ProfileVarDefInit(ID, Class, Args); 1548 1549 void *IP = nullptr; 1550 if (VarDefInit *I = ThePool.FindNodeOrInsertPos(ID, IP)) 1551 return I; 1552 1553 void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *>(Args.size()), 1554 alignof(VarDefInit)); 1555 VarDefInit *I = new(Mem) VarDefInit(Class, Args.size()); 1556 std::uninitialized_copy(Args.begin(), Args.end(), 1557 I->getTrailingObjects<Init *>()); 1558 ThePool.InsertNode(I, IP); 1559 return I; 1560 } 1561 1562 void VarDefInit::Profile(FoldingSetNodeID &ID) const { 1563 ProfileVarDefInit(ID, Class, args()); 1564 } 1565 1566 DefInit *VarDefInit::instantiate() { 1567 if (!Def) { 1568 RecordKeeper &Records = Class->getRecords(); 1569 auto NewRecOwner = make_unique<Record>(Records.getNewAnonymousName(), 1570 Class->getLoc(), Records, 1571 /*IsAnonymous=*/true); 1572 Record *NewRec = NewRecOwner.get(); 1573 1574 // Copy values from class to instance 1575 for (const RecordVal &Val : Class->getValues()) 1576 NewRec->addValue(Val); 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 ProfileCondOpInit(FoldingSetNodeID &ID, 1697 ArrayRef<Init *> CondRange, 1698 ArrayRef<Init *> ValRange, 1699 const RecTy *ValType) { 1700 assert(CondRange.size() == ValRange.size() && 1701 "Number of conditions and values must match!"); 1702 ID.AddPointer(ValType); 1703 ArrayRef<Init *>::iterator Case = CondRange.begin(); 1704 ArrayRef<Init *>::iterator Val = ValRange.begin(); 1705 1706 while (Case != CondRange.end()) { 1707 ID.AddPointer(*Case++); 1708 ID.AddPointer(*Val++); 1709 } 1710 } 1711 1712 void CondOpInit::Profile(FoldingSetNodeID &ID) const { 1713 ProfileCondOpInit(ID, 1714 makeArrayRef(getTrailingObjects<Init *>(), NumConds), 1715 makeArrayRef(getTrailingObjects<Init *>() + NumConds, NumConds), 1716 ValType); 1717 } 1718 1719 CondOpInit * 1720 CondOpInit::get(ArrayRef<Init *> CondRange, 1721 ArrayRef<Init *> ValRange, RecTy *Ty) { 1722 assert(CondRange.size() == ValRange.size() && 1723 "Number of conditions and values must match!"); 1724 1725 static FoldingSet<CondOpInit> ThePool; 1726 FoldingSetNodeID ID; 1727 ProfileCondOpInit(ID, CondRange, ValRange, Ty); 1728 1729 void *IP = nullptr; 1730 if (CondOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP)) 1731 return I; 1732 1733 void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *>(2*CondRange.size()), 1734 alignof(BitsInit)); 1735 CondOpInit *I = new(Mem) CondOpInit(CondRange.size(), Ty); 1736 1737 std::uninitialized_copy(CondRange.begin(), CondRange.end(), 1738 I->getTrailingObjects<Init *>()); 1739 std::uninitialized_copy(ValRange.begin(), ValRange.end(), 1740 I->getTrailingObjects<Init *>()+CondRange.size()); 1741 ThePool.InsertNode(I, IP); 1742 return I; 1743 } 1744 1745 Init *CondOpInit::resolveReferences(Resolver &R) const { 1746 SmallVector<Init*, 4> NewConds; 1747 bool Changed = false; 1748 for (const Init *Case : getConds()) { 1749 Init *NewCase = Case->resolveReferences(R); 1750 NewConds.push_back(NewCase); 1751 Changed |= NewCase != Case; 1752 } 1753 1754 SmallVector<Init*, 4> NewVals; 1755 for (const Init *Val : getVals()) { 1756 Init *NewVal = Val->resolveReferences(R); 1757 NewVals.push_back(NewVal); 1758 Changed |= NewVal != Val; 1759 } 1760 1761 if (Changed) 1762 return (CondOpInit::get(NewConds, NewVals, 1763 getValType()))->Fold(R.getCurrentRecord()); 1764 1765 return const_cast<CondOpInit *>(this); 1766 } 1767 1768 Init *CondOpInit::Fold(Record *CurRec) const { 1769 for ( unsigned i = 0; i < NumConds; ++i) { 1770 Init *Cond = getCond(i); 1771 Init *Val = getVal(i); 1772 1773 if (IntInit *CondI = dyn_cast_or_null<IntInit>( 1774 Cond->convertInitializerTo(IntRecTy::get()))) { 1775 if (CondI->getValue()) 1776 return Val->convertInitializerTo(getValType()); 1777 } else 1778 return const_cast<CondOpInit *>(this); 1779 } 1780 1781 PrintFatalError(CurRec->getLoc(), 1782 CurRec->getName() + 1783 " does not have any true condition in:" + 1784 this->getAsString()); 1785 return nullptr; 1786 } 1787 1788 bool CondOpInit::isConcrete() const { 1789 for (const Init *Case : getConds()) 1790 if (!Case->isConcrete()) 1791 return false; 1792 1793 for (const Init *Val : getVals()) 1794 if (!Val->isConcrete()) 1795 return false; 1796 1797 return true; 1798 } 1799 1800 bool CondOpInit::isComplete() const { 1801 for (const Init *Case : getConds()) 1802 if (!Case->isComplete()) 1803 return false; 1804 1805 for (const Init *Val : getVals()) 1806 if (!Val->isConcrete()) 1807 return false; 1808 1809 return true; 1810 } 1811 1812 std::string CondOpInit::getAsString() const { 1813 std::string Result = "!cond("; 1814 for (unsigned i = 0; i < getNumConds(); i++) { 1815 Result += getCond(i)->getAsString() + ": "; 1816 Result += getVal(i)->getAsString(); 1817 if (i != getNumConds()-1) 1818 Result += ", "; 1819 } 1820 return Result + ")"; 1821 } 1822 1823 Init *CondOpInit::getBit(unsigned Bit) const { 1824 return VarBitInit::get(const_cast<CondOpInit *>(this), Bit); 1825 } 1826 1827 static void ProfileDagInit(FoldingSetNodeID &ID, Init *V, StringInit *VN, 1828 ArrayRef<Init *> ArgRange, 1829 ArrayRef<StringInit *> NameRange) { 1830 ID.AddPointer(V); 1831 ID.AddPointer(VN); 1832 1833 ArrayRef<Init *>::iterator Arg = ArgRange.begin(); 1834 ArrayRef<StringInit *>::iterator Name = NameRange.begin(); 1835 while (Arg != ArgRange.end()) { 1836 assert(Name != NameRange.end() && "Arg name underflow!"); 1837 ID.AddPointer(*Arg++); 1838 ID.AddPointer(*Name++); 1839 } 1840 assert(Name == NameRange.end() && "Arg name overflow!"); 1841 } 1842 1843 DagInit * 1844 DagInit::get(Init *V, StringInit *VN, ArrayRef<Init *> ArgRange, 1845 ArrayRef<StringInit *> NameRange) { 1846 static FoldingSet<DagInit> ThePool; 1847 1848 FoldingSetNodeID ID; 1849 ProfileDagInit(ID, V, VN, ArgRange, NameRange); 1850 1851 void *IP = nullptr; 1852 if (DagInit *I = ThePool.FindNodeOrInsertPos(ID, IP)) 1853 return I; 1854 1855 void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *, StringInit *>(ArgRange.size(), NameRange.size()), alignof(BitsInit)); 1856 DagInit *I = new(Mem) DagInit(V, VN, ArgRange.size(), NameRange.size()); 1857 std::uninitialized_copy(ArgRange.begin(), ArgRange.end(), 1858 I->getTrailingObjects<Init *>()); 1859 std::uninitialized_copy(NameRange.begin(), NameRange.end(), 1860 I->getTrailingObjects<StringInit *>()); 1861 ThePool.InsertNode(I, IP); 1862 return I; 1863 } 1864 1865 DagInit * 1866 DagInit::get(Init *V, StringInit *VN, 1867 ArrayRef<std::pair<Init*, StringInit*>> args) { 1868 SmallVector<Init *, 8> Args; 1869 SmallVector<StringInit *, 8> Names; 1870 1871 for (const auto &Arg : args) { 1872 Args.push_back(Arg.first); 1873 Names.push_back(Arg.second); 1874 } 1875 1876 return DagInit::get(V, VN, Args, Names); 1877 } 1878 1879 void DagInit::Profile(FoldingSetNodeID &ID) const { 1880 ProfileDagInit(ID, Val, ValName, makeArrayRef(getTrailingObjects<Init *>(), NumArgs), makeArrayRef(getTrailingObjects<StringInit *>(), NumArgNames)); 1881 } 1882 1883 Init *DagInit::resolveReferences(Resolver &R) const { 1884 SmallVector<Init*, 8> NewArgs; 1885 NewArgs.reserve(arg_size()); 1886 bool ArgsChanged = false; 1887 for (const Init *Arg : getArgs()) { 1888 Init *NewArg = Arg->resolveReferences(R); 1889 NewArgs.push_back(NewArg); 1890 ArgsChanged |= NewArg != Arg; 1891 } 1892 1893 Init *Op = Val->resolveReferences(R); 1894 if (Op != Val || ArgsChanged) 1895 return DagInit::get(Op, ValName, NewArgs, getArgNames()); 1896 1897 return const_cast<DagInit *>(this); 1898 } 1899 1900 bool DagInit::isConcrete() const { 1901 if (!Val->isConcrete()) 1902 return false; 1903 for (const Init *Elt : getArgs()) { 1904 if (!Elt->isConcrete()) 1905 return false; 1906 } 1907 return true; 1908 } 1909 1910 std::string DagInit::getAsString() const { 1911 std::string Result = "(" + Val->getAsString(); 1912 if (ValName) 1913 Result += ":" + ValName->getAsUnquotedString(); 1914 if (!arg_empty()) { 1915 Result += " " + getArg(0)->getAsString(); 1916 if (getArgName(0)) Result += ":$" + getArgName(0)->getAsUnquotedString(); 1917 for (unsigned i = 1, e = getNumArgs(); i != e; ++i) { 1918 Result += ", " + getArg(i)->getAsString(); 1919 if (getArgName(i)) Result += ":$" + getArgName(i)->getAsUnquotedString(); 1920 } 1921 } 1922 return Result + ")"; 1923 } 1924 1925 //===----------------------------------------------------------------------===// 1926 // Other implementations 1927 //===----------------------------------------------------------------------===// 1928 1929 RecordVal::RecordVal(Init *N, RecTy *T, bool P) 1930 : Name(N), TyAndPrefix(T, P) { 1931 setValue(UnsetInit::get()); 1932 assert(Value && "Cannot create unset value for current type!"); 1933 } 1934 1935 StringRef RecordVal::getName() const { 1936 return cast<StringInit>(getNameInit())->getValue(); 1937 } 1938 1939 bool RecordVal::setValue(Init *V) { 1940 if (V) { 1941 Value = V->getCastTo(getType()); 1942 if (Value) { 1943 assert(!isa<TypedInit>(Value) || 1944 cast<TypedInit>(Value)->getType()->typeIsA(getType())); 1945 if (BitsRecTy *BTy = dyn_cast<BitsRecTy>(getType())) { 1946 if (!isa<BitsInit>(Value)) { 1947 SmallVector<Init *, 64> Bits; 1948 Bits.reserve(BTy->getNumBits()); 1949 for (unsigned i = 0, e = BTy->getNumBits(); i < e; ++i) 1950 Bits.push_back(Value->getBit(i)); 1951 Value = BitsInit::get(Bits); 1952 } 1953 } 1954 } 1955 return Value == nullptr; 1956 } 1957 Value = nullptr; 1958 return false; 1959 } 1960 1961 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 1962 LLVM_DUMP_METHOD void RecordVal::dump() const { errs() << *this; } 1963 #endif 1964 1965 void RecordVal::print(raw_ostream &OS, bool PrintSem) const { 1966 if (getPrefix()) OS << "field "; 1967 OS << *getType() << " " << getNameInitAsString(); 1968 1969 if (getValue()) 1970 OS << " = " << *getValue(); 1971 1972 if (PrintSem) OS << ";\n"; 1973 } 1974 1975 unsigned Record::LastID = 0; 1976 1977 void Record::checkName() { 1978 // Ensure the record name has string type. 1979 const TypedInit *TypedName = cast<const TypedInit>(Name); 1980 if (!isa<StringRecTy>(TypedName->getType())) 1981 PrintFatalError(getLoc(), Twine("Record name '") + Name->getAsString() + 1982 "' is not a string!"); 1983 } 1984 1985 RecordRecTy *Record::getType() { 1986 SmallVector<Record *, 4> DirectSCs; 1987 getDirectSuperClasses(DirectSCs); 1988 return RecordRecTy::get(DirectSCs); 1989 } 1990 1991 DefInit *Record::getDefInit() { 1992 if (!TheInit) 1993 TheInit = new(Allocator) DefInit(this); 1994 return TheInit; 1995 } 1996 1997 void Record::setName(Init *NewName) { 1998 Name = NewName; 1999 checkName(); 2000 // DO NOT resolve record values to the name at this point because 2001 // there might be default values for arguments of this def. Those 2002 // arguments might not have been resolved yet so we don't want to 2003 // prematurely assume values for those arguments were not passed to 2004 // this def. 2005 // 2006 // Nonetheless, it may be that some of this Record's values 2007 // reference the record name. Indeed, the reason for having the 2008 // record name be an Init is to provide this flexibility. The extra 2009 // resolve steps after completely instantiating defs takes care of 2010 // this. See TGParser::ParseDef and TGParser::ParseDefm. 2011 } 2012 2013 void Record::getDirectSuperClasses(SmallVectorImpl<Record *> &Classes) const { 2014 ArrayRef<std::pair<Record *, SMRange>> SCs = getSuperClasses(); 2015 while (!SCs.empty()) { 2016 // Superclasses are in reverse preorder, so 'back' is a direct superclass, 2017 // and its transitive superclasses are directly preceding it. 2018 Record *SC = SCs.back().first; 2019 SCs = SCs.drop_back(1 + SC->getSuperClasses().size()); 2020 Classes.push_back(SC); 2021 } 2022 } 2023 2024 void Record::resolveReferences(Resolver &R, const RecordVal *SkipVal) { 2025 for (RecordVal &Value : Values) { 2026 if (SkipVal == &Value) // Skip resolve the same field as the given one 2027 continue; 2028 if (Init *V = Value.getValue()) { 2029 Init *VR = V->resolveReferences(R); 2030 if (Value.setValue(VR)) { 2031 std::string Type; 2032 if (TypedInit *VRT = dyn_cast<TypedInit>(VR)) 2033 Type = 2034 (Twine("of type '") + VRT->getType()->getAsString() + "' ").str(); 2035 PrintFatalError(getLoc(), Twine("Invalid value ") + Type + 2036 "is found when setting '" + 2037 Value.getNameInitAsString() + 2038 "' of type '" + 2039 Value.getType()->getAsString() + 2040 "' after resolving references: " + 2041 VR->getAsUnquotedString() + "\n"); 2042 } 2043 } 2044 } 2045 Init *OldName = getNameInit(); 2046 Init *NewName = Name->resolveReferences(R); 2047 if (NewName != OldName) { 2048 // Re-register with RecordKeeper. 2049 setName(NewName); 2050 } 2051 } 2052 2053 void Record::resolveReferences() { 2054 RecordResolver R(*this); 2055 R.setFinal(true); 2056 resolveReferences(R); 2057 } 2058 2059 void Record::resolveReferencesTo(const RecordVal *RV) { 2060 RecordValResolver R(*this, RV); 2061 resolveReferences(R, RV); 2062 } 2063 2064 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 2065 LLVM_DUMP_METHOD void Record::dump() const { errs() << *this; } 2066 #endif 2067 2068 raw_ostream &llvm::operator<<(raw_ostream &OS, const Record &R) { 2069 OS << R.getNameInitAsString(); 2070 2071 ArrayRef<Init *> TArgs = R.getTemplateArgs(); 2072 if (!TArgs.empty()) { 2073 OS << "<"; 2074 bool NeedComma = false; 2075 for (const Init *TA : TArgs) { 2076 if (NeedComma) OS << ", "; 2077 NeedComma = true; 2078 const RecordVal *RV = R.getValue(TA); 2079 assert(RV && "Template argument record not found??"); 2080 RV->print(OS, false); 2081 } 2082 OS << ">"; 2083 } 2084 2085 OS << " {"; 2086 ArrayRef<std::pair<Record *, SMRange>> SC = R.getSuperClasses(); 2087 if (!SC.empty()) { 2088 OS << "\t//"; 2089 for (const auto &SuperPair : SC) 2090 OS << " " << SuperPair.first->getNameInitAsString(); 2091 } 2092 OS << "\n"; 2093 2094 for (const RecordVal &Val : R.getValues()) 2095 if (Val.getPrefix() && !R.isTemplateArg(Val.getNameInit())) 2096 OS << Val; 2097 for (const RecordVal &Val : R.getValues()) 2098 if (!Val.getPrefix() && !R.isTemplateArg(Val.getNameInit())) 2099 OS << Val; 2100 2101 return OS << "}\n"; 2102 } 2103 2104 Init *Record::getValueInit(StringRef FieldName) const { 2105 const RecordVal *R = getValue(FieldName); 2106 if (!R || !R->getValue()) 2107 PrintFatalError(getLoc(), "Record `" + getName() + 2108 "' does not have a field named `" + FieldName + "'!\n"); 2109 return R->getValue(); 2110 } 2111 2112 StringRef Record::getValueAsString(StringRef FieldName) 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 + "'!\n"); 2117 2118 if (StringInit *SI = dyn_cast<StringInit>(R->getValue())) 2119 return SI->getValue(); 2120 if (CodeInit *CI = dyn_cast<CodeInit>(R->getValue())) 2121 return CI->getValue(); 2122 2123 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + 2124 FieldName + "' does not have a string initializer!"); 2125 } 2126 2127 BitsInit *Record::getValueAsBitsInit(StringRef FieldName) const { 2128 const RecordVal *R = getValue(FieldName); 2129 if (!R || !R->getValue()) 2130 PrintFatalError(getLoc(), "Record `" + getName() + 2131 "' does not have a field named `" + FieldName + "'!\n"); 2132 2133 if (BitsInit *BI = dyn_cast<BitsInit>(R->getValue())) 2134 return BI; 2135 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + 2136 FieldName + "' does not have a BitsInit initializer!"); 2137 } 2138 2139 ListInit *Record::getValueAsListInit(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 2145 if (ListInit *LI = dyn_cast<ListInit>(R->getValue())) 2146 return LI; 2147 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + 2148 FieldName + "' does not have a list initializer!"); 2149 } 2150 2151 std::vector<Record*> 2152 Record::getValueAsListOfDefs(StringRef FieldName) const { 2153 ListInit *List = getValueAsListInit(FieldName); 2154 std::vector<Record*> Defs; 2155 for (Init *I : List->getValues()) { 2156 if (DefInit *DI = dyn_cast<DefInit>(I)) 2157 Defs.push_back(DI->getDef()); 2158 else 2159 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + 2160 FieldName + "' list is not entirely DefInit!"); 2161 } 2162 return Defs; 2163 } 2164 2165 int64_t Record::getValueAsInt(StringRef FieldName) const { 2166 const RecordVal *R = getValue(FieldName); 2167 if (!R || !R->getValue()) 2168 PrintFatalError(getLoc(), "Record `" + getName() + 2169 "' does not have a field named `" + FieldName + "'!\n"); 2170 2171 if (IntInit *II = dyn_cast<IntInit>(R->getValue())) 2172 return II->getValue(); 2173 PrintFatalError(getLoc(), Twine("Record `") + getName() + "', field `" + 2174 FieldName + 2175 "' does not have an int initializer: " + 2176 R->getValue()->getAsString()); 2177 } 2178 2179 std::vector<int64_t> 2180 Record::getValueAsListOfInts(StringRef FieldName) const { 2181 ListInit *List = getValueAsListInit(FieldName); 2182 std::vector<int64_t> Ints; 2183 for (Init *I : List->getValues()) { 2184 if (IntInit *II = dyn_cast<IntInit>(I)) 2185 Ints.push_back(II->getValue()); 2186 else 2187 PrintFatalError(getLoc(), 2188 Twine("Record `") + getName() + "', field `" + FieldName + 2189 "' does not have a list of ints initializer: " + 2190 I->getAsString()); 2191 } 2192 return Ints; 2193 } 2194 2195 std::vector<StringRef> 2196 Record::getValueAsListOfStrings(StringRef FieldName) const { 2197 ListInit *List = getValueAsListInit(FieldName); 2198 std::vector<StringRef> Strings; 2199 for (Init *I : List->getValues()) { 2200 if (StringInit *SI = dyn_cast<StringInit>(I)) 2201 Strings.push_back(SI->getValue()); 2202 else 2203 PrintFatalError(getLoc(), 2204 Twine("Record `") + getName() + "', field `" + FieldName + 2205 "' does not have a list of strings initializer: " + 2206 I->getAsString()); 2207 } 2208 return Strings; 2209 } 2210 2211 Record *Record::getValueAsDef(StringRef FieldName) const { 2212 const RecordVal *R = getValue(FieldName); 2213 if (!R || !R->getValue()) 2214 PrintFatalError(getLoc(), "Record `" + getName() + 2215 "' does not have a field named `" + FieldName + "'!\n"); 2216 2217 if (DefInit *DI = dyn_cast<DefInit>(R->getValue())) 2218 return DI->getDef(); 2219 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + 2220 FieldName + "' does not have a def initializer!"); 2221 } 2222 2223 bool Record::getValueAsBit(StringRef FieldName) const { 2224 const RecordVal *R = getValue(FieldName); 2225 if (!R || !R->getValue()) 2226 PrintFatalError(getLoc(), "Record `" + getName() + 2227 "' does not have a field named `" + FieldName + "'!\n"); 2228 2229 if (BitInit *BI = dyn_cast<BitInit>(R->getValue())) 2230 return BI->getValue(); 2231 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + 2232 FieldName + "' does not have a bit initializer!"); 2233 } 2234 2235 bool Record::getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const { 2236 const RecordVal *R = getValue(FieldName); 2237 if (!R || !R->getValue()) 2238 PrintFatalError(getLoc(), "Record `" + getName() + 2239 "' does not have a field named `" + FieldName.str() + "'!\n"); 2240 2241 if (isa<UnsetInit>(R->getValue())) { 2242 Unset = true; 2243 return false; 2244 } 2245 Unset = false; 2246 if (BitInit *BI = dyn_cast<BitInit>(R->getValue())) 2247 return BI->getValue(); 2248 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + 2249 FieldName + "' does not have a bit initializer!"); 2250 } 2251 2252 DagInit *Record::getValueAsDag(StringRef FieldName) const { 2253 const RecordVal *R = getValue(FieldName); 2254 if (!R || !R->getValue()) 2255 PrintFatalError(getLoc(), "Record `" + getName() + 2256 "' does not have a field named `" + FieldName + "'!\n"); 2257 2258 if (DagInit *DI = dyn_cast<DagInit>(R->getValue())) 2259 return DI; 2260 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + 2261 FieldName + "' does not have a dag initializer!"); 2262 } 2263 2264 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 2265 LLVM_DUMP_METHOD void RecordKeeper::dump() const { errs() << *this; } 2266 #endif 2267 2268 raw_ostream &llvm::operator<<(raw_ostream &OS, const RecordKeeper &RK) { 2269 OS << "------------- Classes -----------------\n"; 2270 for (const auto &C : RK.getClasses()) 2271 OS << "class " << *C.second; 2272 2273 OS << "------------- Defs -----------------\n"; 2274 for (const auto &D : RK.getDefs()) 2275 OS << "def " << *D.second; 2276 return OS; 2277 } 2278 2279 /// GetNewAnonymousName - Generate a unique anonymous name that can be used as 2280 /// an identifier. 2281 Init *RecordKeeper::getNewAnonymousName() { 2282 return StringInit::get("anonymous_" + utostr(AnonCounter++)); 2283 } 2284 2285 std::vector<Record *> 2286 RecordKeeper::getAllDerivedDefinitions(StringRef ClassName) const { 2287 Record *Class = getClass(ClassName); 2288 if (!Class) 2289 PrintFatalError("ERROR: Couldn't find the `" + ClassName + "' class!\n"); 2290 2291 std::vector<Record*> Defs; 2292 for (const auto &D : getDefs()) 2293 if (D.second->isSubClassOf(Class)) 2294 Defs.push_back(D.second.get()); 2295 2296 return Defs; 2297 } 2298 2299 Init *MapResolver::resolve(Init *VarName) { 2300 auto It = Map.find(VarName); 2301 if (It == Map.end()) 2302 return nullptr; 2303 2304 Init *I = It->second.V; 2305 2306 if (!It->second.Resolved && Map.size() > 1) { 2307 // Resolve mutual references among the mapped variables, but prevent 2308 // infinite recursion. 2309 Map.erase(It); 2310 I = I->resolveReferences(*this); 2311 Map[VarName] = {I, true}; 2312 } 2313 2314 return I; 2315 } 2316 2317 Init *RecordResolver::resolve(Init *VarName) { 2318 Init *Val = Cache.lookup(VarName); 2319 if (Val) 2320 return Val; 2321 2322 for (Init *S : Stack) { 2323 if (S == VarName) 2324 return nullptr; // prevent infinite recursion 2325 } 2326 2327 if (RecordVal *RV = getCurrentRecord()->getValue(VarName)) { 2328 if (!isa<UnsetInit>(RV->getValue())) { 2329 Val = RV->getValue(); 2330 Stack.push_back(VarName); 2331 Val = Val->resolveReferences(*this); 2332 Stack.pop_back(); 2333 } 2334 } 2335 2336 Cache[VarName] = Val; 2337 return Val; 2338 } 2339 2340 Init *TrackUnresolvedResolver::resolve(Init *VarName) { 2341 Init *I = nullptr; 2342 2343 if (R) { 2344 I = R->resolve(VarName); 2345 if (I && !FoundUnresolved) { 2346 // Do not recurse into the resolved initializer, as that would change 2347 // the behavior of the resolver we're delegating, but do check to see 2348 // if there are unresolved variables remaining. 2349 TrackUnresolvedResolver Sub; 2350 I->resolveReferences(Sub); 2351 FoundUnresolved |= Sub.FoundUnresolved; 2352 } 2353 } 2354 2355 if (!I) 2356 FoundUnresolved = true; 2357 return I; 2358 } 2359 2360 Init *HasReferenceResolver::resolve(Init *VarName) 2361 { 2362 if (VarName == VarNameToTrack) 2363 Found = true; 2364 return nullptr; 2365 } 2366