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