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