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