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