LLVM API Documentation
00001 //===- Record.cpp - Record implementation ---------------------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // Implement the tablegen record classes. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/TableGen/Record.h" 00015 #include "llvm/ADT/DenseMap.h" 00016 #include "llvm/ADT/FoldingSet.h" 00017 #include "llvm/ADT/Hashing.h" 00018 #include "llvm/ADT/STLExtras.h" 00019 #include "llvm/ADT/SmallVector.h" 00020 #include "llvm/ADT/StringExtras.h" 00021 #include "llvm/ADT/StringMap.h" 00022 #include "llvm/Support/DataTypes.h" 00023 #include "llvm/Support/ErrorHandling.h" 00024 #include "llvm/Support/Format.h" 00025 #include "llvm/TableGen/Error.h" 00026 00027 using namespace llvm; 00028 00029 //===----------------------------------------------------------------------===// 00030 // std::string wrapper for DenseMap purposes 00031 //===----------------------------------------------------------------------===// 00032 00033 namespace llvm { 00034 00035 /// TableGenStringKey - This is a wrapper for std::string suitable for 00036 /// using as a key to a DenseMap. Because there isn't a particularly 00037 /// good way to indicate tombstone or empty keys for strings, we want 00038 /// to wrap std::string to indicate that this is a "special" string 00039 /// not expected to take on certain values (those of the tombstone and 00040 /// empty keys). This makes things a little safer as it clarifies 00041 /// that DenseMap is really not appropriate for general strings. 00042 00043 class TableGenStringKey { 00044 public: 00045 TableGenStringKey(const std::string &str) : data(str) {} 00046 TableGenStringKey(const char *str) : data(str) {} 00047 00048 const std::string &str() const { return data; } 00049 00050 friend hash_code hash_value(const TableGenStringKey &Value) { 00051 using llvm::hash_value; 00052 return hash_value(Value.str()); 00053 } 00054 private: 00055 std::string data; 00056 }; 00057 00058 /// Specialize DenseMapInfo for TableGenStringKey. 00059 template<> struct DenseMapInfo<TableGenStringKey> { 00060 static inline TableGenStringKey getEmptyKey() { 00061 TableGenStringKey Empty("<<<EMPTY KEY>>>"); 00062 return Empty; 00063 } 00064 static inline TableGenStringKey getTombstoneKey() { 00065 TableGenStringKey Tombstone("<<<TOMBSTONE KEY>>>"); 00066 return Tombstone; 00067 } 00068 static unsigned getHashValue(const TableGenStringKey& Val) { 00069 using llvm::hash_value; 00070 return hash_value(Val); 00071 } 00072 static bool isEqual(const TableGenStringKey& LHS, 00073 const TableGenStringKey& RHS) { 00074 return LHS.str() == RHS.str(); 00075 } 00076 }; 00077 00078 } // namespace llvm 00079 00080 //===----------------------------------------------------------------------===// 00081 // Type implementations 00082 //===----------------------------------------------------------------------===// 00083 00084 BitRecTy BitRecTy::Shared; 00085 IntRecTy IntRecTy::Shared; 00086 StringRecTy StringRecTy::Shared; 00087 DagRecTy DagRecTy::Shared; 00088 00089 void RecTy::anchor() { } 00090 void RecTy::dump() const { print(errs()); } 00091 00092 ListRecTy *RecTy::getListTy() { 00093 if (!ListTy) 00094 ListTy = new ListRecTy(this); 00095 return ListTy; 00096 } 00097 00098 bool RecTy::baseClassOf(const RecTy *RHS) const{ 00099 assert (RHS && "NULL pointer"); 00100 return Kind == RHS->getRecTyKind(); 00101 } 00102 00103 Init *BitRecTy::convertValue(BitsInit *BI) { 00104 if (BI->getNumBits() != 1) return 0; // Only accept if just one bit! 00105 return BI->getBit(0); 00106 } 00107 00108 Init *BitRecTy::convertValue(IntInit *II) { 00109 int64_t Val = II->getValue(); 00110 if (Val != 0 && Val != 1) return 0; // Only accept 0 or 1 for a bit! 00111 00112 return BitInit::get(Val != 0); 00113 } 00114 00115 Init *BitRecTy::convertValue(TypedInit *VI) { 00116 RecTy *Ty = VI->getType(); 00117 if (isa<BitRecTy>(Ty) || isa<BitsRecTy>(Ty) || isa<IntRecTy>(Ty)) 00118 return VI; // Accept variable if it is already of bit type! 00119 return 0; 00120 } 00121 00122 bool BitRecTy::baseClassOf(const RecTy *RHS) const{ 00123 if(RecTy::baseClassOf(RHS) || getRecTyKind() == IntRecTyKind) 00124 return true; 00125 if(const BitsRecTy *BitsTy = dyn_cast<BitsRecTy>(RHS)) 00126 return BitsTy->getNumBits() == 1; 00127 return false; 00128 } 00129 00130 BitsRecTy *BitsRecTy::get(unsigned Sz) { 00131 static std::vector<BitsRecTy*> Shared; 00132 if (Sz >= Shared.size()) 00133 Shared.resize(Sz + 1); 00134 BitsRecTy *&Ty = Shared[Sz]; 00135 if (!Ty) 00136 Ty = new BitsRecTy(Sz); 00137 return Ty; 00138 } 00139 00140 std::string BitsRecTy::getAsString() const { 00141 return "bits<" + utostr(Size) + ">"; 00142 } 00143 00144 Init *BitsRecTy::convertValue(UnsetInit *UI) { 00145 SmallVector<Init *, 16> NewBits(Size); 00146 00147 for (unsigned i = 0; i != Size; ++i) 00148 NewBits[i] = UnsetInit::get(); 00149 00150 return BitsInit::get(NewBits); 00151 } 00152 00153 Init *BitsRecTy::convertValue(BitInit *UI) { 00154 if (Size != 1) return 0; // Can only convert single bit. 00155 return BitsInit::get(UI); 00156 } 00157 00158 /// canFitInBitfield - Return true if the number of bits is large enough to hold 00159 /// the integer value. 00160 static bool canFitInBitfield(int64_t Value, unsigned NumBits) { 00161 // For example, with NumBits == 4, we permit Values from [-7 .. 15]. 00162 return (NumBits >= sizeof(Value) * 8) || 00163 (Value >> NumBits == 0) || (Value >> (NumBits-1) == -1); 00164 } 00165 00166 /// convertValue from Int initializer to bits type: Split the integer up into the 00167 /// appropriate bits. 00168 /// 00169 Init *BitsRecTy::convertValue(IntInit *II) { 00170 int64_t Value = II->getValue(); 00171 // Make sure this bitfield is large enough to hold the integer value. 00172 if (!canFitInBitfield(Value, Size)) 00173 return 0; 00174 00175 SmallVector<Init *, 16> NewBits(Size); 00176 00177 for (unsigned i = 0; i != Size; ++i) 00178 NewBits[i] = BitInit::get(Value & (1LL << i)); 00179 00180 return BitsInit::get(NewBits); 00181 } 00182 00183 Init *BitsRecTy::convertValue(BitsInit *BI) { 00184 // If the number of bits is right, return it. Otherwise we need to expand or 00185 // truncate. 00186 if (BI->getNumBits() == Size) return BI; 00187 return 0; 00188 } 00189 00190 Init *BitsRecTy::convertValue(TypedInit *VI) { 00191 if (Size == 1 && isa<BitRecTy>(VI->getType())) 00192 return BitsInit::get(VI); 00193 00194 if (VI->getType()->typeIsConvertibleTo(this)) { 00195 SmallVector<Init *, 16> NewBits(Size); 00196 00197 for (unsigned i = 0; i != Size; ++i) 00198 NewBits[i] = VarBitInit::get(VI, i); 00199 return BitsInit::get(NewBits); 00200 } 00201 00202 return 0; 00203 } 00204 00205 bool BitsRecTy::baseClassOf(const RecTy *RHS) const{ 00206 if (RecTy::baseClassOf(RHS)) //argument and the receiver are the same type 00207 return cast<BitsRecTy>(RHS)->Size == Size; 00208 RecTyKind kind = RHS->getRecTyKind(); 00209 return (kind == BitRecTyKind && Size == 1) || (kind == IntRecTyKind); 00210 } 00211 00212 Init *IntRecTy::convertValue(BitInit *BI) { 00213 return IntInit::get(BI->getValue()); 00214 } 00215 00216 Init *IntRecTy::convertValue(BitsInit *BI) { 00217 int64_t Result = 0; 00218 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) 00219 if (BitInit *Bit = dyn_cast<BitInit>(BI->getBit(i))) { 00220 Result |= Bit->getValue() << i; 00221 } else { 00222 return 0; 00223 } 00224 return IntInit::get(Result); 00225 } 00226 00227 Init *IntRecTy::convertValue(TypedInit *TI) { 00228 if (TI->getType()->typeIsConvertibleTo(this)) 00229 return TI; // Accept variable if already of the right type! 00230 return 0; 00231 } 00232 00233 bool IntRecTy::baseClassOf(const RecTy *RHS) const{ 00234 RecTyKind kind = RHS->getRecTyKind(); 00235 return kind==BitRecTyKind || kind==BitsRecTyKind || kind==IntRecTyKind; 00236 } 00237 00238 Init *StringRecTy::convertValue(UnOpInit *BO) { 00239 if (BO->getOpcode() == UnOpInit::CAST) { 00240 Init *L = BO->getOperand()->convertInitializerTo(this); 00241 if (L == 0) return 0; 00242 if (L != BO->getOperand()) 00243 return UnOpInit::get(UnOpInit::CAST, L, new StringRecTy); 00244 return BO; 00245 } 00246 00247 return convertValue((TypedInit*)BO); 00248 } 00249 00250 Init *StringRecTy::convertValue(BinOpInit *BO) { 00251 if (BO->getOpcode() == BinOpInit::STRCONCAT) { 00252 Init *L = BO->getLHS()->convertInitializerTo(this); 00253 Init *R = BO->getRHS()->convertInitializerTo(this); 00254 if (L == 0 || R == 0) return 0; 00255 if (L != BO->getLHS() || R != BO->getRHS()) 00256 return BinOpInit::get(BinOpInit::STRCONCAT, L, R, new StringRecTy); 00257 return BO; 00258 } 00259 00260 return convertValue((TypedInit*)BO); 00261 } 00262 00263 00264 Init *StringRecTy::convertValue(TypedInit *TI) { 00265 if (isa<StringRecTy>(TI->getType())) 00266 return TI; // Accept variable if already of the right type! 00267 return 0; 00268 } 00269 00270 std::string ListRecTy::getAsString() const { 00271 return "list<" + Ty->getAsString() + ">"; 00272 } 00273 00274 Init *ListRecTy::convertValue(ListInit *LI) { 00275 std::vector<Init*> Elements; 00276 00277 // Verify that all of the elements of the list are subclasses of the 00278 // appropriate class! 00279 for (unsigned i = 0, e = LI->getSize(); i != e; ++i) 00280 if (Init *CI = LI->getElement(i)->convertInitializerTo(Ty)) 00281 Elements.push_back(CI); 00282 else 00283 return 0; 00284 00285 if (!isa<ListRecTy>(LI->getType())) 00286 return 0; 00287 00288 return ListInit::get(Elements, this); 00289 } 00290 00291 Init *ListRecTy::convertValue(TypedInit *TI) { 00292 // Ensure that TI is compatible with our class. 00293 if (ListRecTy *LRT = dyn_cast<ListRecTy>(TI->getType())) 00294 if (LRT->getElementType()->typeIsConvertibleTo(getElementType())) 00295 return TI; 00296 return 0; 00297 } 00298 00299 bool ListRecTy::baseClassOf(const RecTy *RHS) const{ 00300 if(const ListRecTy* ListTy = dyn_cast<ListRecTy>(RHS)) 00301 return ListTy->getElementType()->typeIsConvertibleTo(Ty); 00302 return false; 00303 } 00304 00305 Init *DagRecTy::convertValue(TypedInit *TI) { 00306 if (TI->getType()->typeIsConvertibleTo(this)) 00307 return TI; 00308 return 0; 00309 } 00310 00311 Init *DagRecTy::convertValue(UnOpInit *BO) { 00312 if (BO->getOpcode() == UnOpInit::CAST) { 00313 Init *L = BO->getOperand()->convertInitializerTo(this); 00314 if (L == 0) return 0; 00315 if (L != BO->getOperand()) 00316 return UnOpInit::get(UnOpInit::CAST, L, new DagRecTy); 00317 return BO; 00318 } 00319 return 0; 00320 } 00321 00322 Init *DagRecTy::convertValue(BinOpInit *BO) { 00323 if (BO->getOpcode() == BinOpInit::CONCAT) { 00324 Init *L = BO->getLHS()->convertInitializerTo(this); 00325 Init *R = BO->getRHS()->convertInitializerTo(this); 00326 if (L == 0 || R == 0) return 0; 00327 if (L != BO->getLHS() || R != BO->getRHS()) 00328 return BinOpInit::get(BinOpInit::CONCAT, L, R, new DagRecTy); 00329 return BO; 00330 } 00331 return 0; 00332 } 00333 00334 RecordRecTy *RecordRecTy::get(Record *R) { 00335 return dyn_cast<RecordRecTy>(R->getDefInit()->getType()); 00336 } 00337 00338 std::string RecordRecTy::getAsString() const { 00339 return Rec->getName(); 00340 } 00341 00342 Init *RecordRecTy::convertValue(DefInit *DI) { 00343 // Ensure that DI is a subclass of Rec. 00344 if (!DI->getDef()->isSubClassOf(Rec)) 00345 return 0; 00346 return DI; 00347 } 00348 00349 Init *RecordRecTy::convertValue(TypedInit *TI) { 00350 // Ensure that TI is compatible with Rec. 00351 if (RecordRecTy *RRT = dyn_cast<RecordRecTy>(TI->getType())) 00352 if (RRT->getRecord()->isSubClassOf(getRecord()) || 00353 RRT->getRecord() == getRecord()) 00354 return TI; 00355 return 0; 00356 } 00357 00358 bool RecordRecTy::baseClassOf(const RecTy *RHS) const{ 00359 const RecordRecTy *RTy = dyn_cast<RecordRecTy>(RHS); 00360 if (!RTy) 00361 return false; 00362 00363 if (Rec == RTy->getRecord() || RTy->getRecord()->isSubClassOf(Rec)) 00364 return true; 00365 00366 const std::vector<Record*> &SC = Rec->getSuperClasses(); 00367 for (unsigned i = 0, e = SC.size(); i != e; ++i) 00368 if (RTy->getRecord()->isSubClassOf(SC[i])) 00369 return true; 00370 00371 return false; 00372 } 00373 00374 /// resolveTypes - Find a common type that T1 and T2 convert to. 00375 /// Return 0 if no such type exists. 00376 /// 00377 RecTy *llvm::resolveTypes(RecTy *T1, RecTy *T2) { 00378 if (T1->typeIsConvertibleTo(T2)) 00379 return T2; 00380 if (T2->typeIsConvertibleTo(T1)) 00381 return T1; 00382 00383 // If one is a Record type, check superclasses 00384 if (RecordRecTy *RecTy1 = dyn_cast<RecordRecTy>(T1)) { 00385 // See if T2 inherits from a type T1 also inherits from 00386 const std::vector<Record *> &T1SuperClasses = 00387 RecTy1->getRecord()->getSuperClasses(); 00388 for(std::vector<Record *>::const_iterator i = T1SuperClasses.begin(), 00389 iend = T1SuperClasses.end(); 00390 i != iend; 00391 ++i) { 00392 RecordRecTy *SuperRecTy1 = RecordRecTy::get(*i); 00393 RecTy *NewType1 = resolveTypes(SuperRecTy1, T2); 00394 if (NewType1 != 0) { 00395 if (NewType1 != SuperRecTy1) { 00396 delete SuperRecTy1; 00397 } 00398 return NewType1; 00399 } 00400 } 00401 } 00402 if (RecordRecTy *RecTy2 = dyn_cast<RecordRecTy>(T2)) { 00403 // See if T1 inherits from a type T2 also inherits from 00404 const std::vector<Record *> &T2SuperClasses = 00405 RecTy2->getRecord()->getSuperClasses(); 00406 for (std::vector<Record *>::const_iterator i = T2SuperClasses.begin(), 00407 iend = T2SuperClasses.end(); 00408 i != iend; 00409 ++i) { 00410 RecordRecTy *SuperRecTy2 = RecordRecTy::get(*i); 00411 RecTy *NewType2 = resolveTypes(T1, SuperRecTy2); 00412 if (NewType2 != 0) { 00413 if (NewType2 != SuperRecTy2) { 00414 delete SuperRecTy2; 00415 } 00416 return NewType2; 00417 } 00418 } 00419 } 00420 return 0; 00421 } 00422 00423 00424 //===----------------------------------------------------------------------===// 00425 // Initializer implementations 00426 //===----------------------------------------------------------------------===// 00427 00428 void Init::anchor() { } 00429 void Init::dump() const { return print(errs()); } 00430 00431 void UnsetInit::anchor() { } 00432 00433 UnsetInit *UnsetInit::get() { 00434 static UnsetInit TheInit; 00435 return &TheInit; 00436 } 00437 00438 void BitInit::anchor() { } 00439 00440 BitInit *BitInit::get(bool V) { 00441 static BitInit True(true); 00442 static BitInit False(false); 00443 00444 return V ? &True : &False; 00445 } 00446 00447 static void 00448 ProfileBitsInit(FoldingSetNodeID &ID, ArrayRef<Init *> Range) { 00449 ID.AddInteger(Range.size()); 00450 00451 for (ArrayRef<Init *>::iterator i = Range.begin(), 00452 iend = Range.end(); 00453 i != iend; 00454 ++i) 00455 ID.AddPointer(*i); 00456 } 00457 00458 BitsInit *BitsInit::get(ArrayRef<Init *> Range) { 00459 typedef FoldingSet<BitsInit> Pool; 00460 static Pool ThePool; 00461 00462 FoldingSetNodeID ID; 00463 ProfileBitsInit(ID, Range); 00464 00465 void *IP = 0; 00466 if (BitsInit *I = ThePool.FindNodeOrInsertPos(ID, IP)) 00467 return I; 00468 00469 BitsInit *I = new BitsInit(Range); 00470 ThePool.InsertNode(I, IP); 00471 00472 return I; 00473 } 00474 00475 void BitsInit::Profile(FoldingSetNodeID &ID) const { 00476 ProfileBitsInit(ID, Bits); 00477 } 00478 00479 Init * 00480 BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) const { 00481 SmallVector<Init *, 16> NewBits(Bits.size()); 00482 00483 for (unsigned i = 0, e = Bits.size(); i != e; ++i) { 00484 if (Bits[i] >= getNumBits()) 00485 return 0; 00486 NewBits[i] = getBit(Bits[i]); 00487 } 00488 return BitsInit::get(NewBits); 00489 } 00490 00491 std::string BitsInit::getAsString() const { 00492 std::string Result = "{ "; 00493 for (unsigned i = 0, e = getNumBits(); i != e; ++i) { 00494 if (i) Result += ", "; 00495 if (Init *Bit = getBit(e-i-1)) 00496 Result += Bit->getAsString(); 00497 else 00498 Result += "*"; 00499 } 00500 return Result + " }"; 00501 } 00502 00503 // Fix bit initializer to preserve the behavior that bit reference from a unset 00504 // bits initializer will resolve into VarBitInit to keep the field name and bit 00505 // number used in targets with fixed insn length. 00506 static Init *fixBitInit(const RecordVal *RV, Init *Before, Init *After) { 00507 if (RV || After != UnsetInit::get()) 00508 return After; 00509 return Before; 00510 } 00511 00512 // resolveReferences - If there are any field references that refer to fields 00513 // that have been filled in, we can propagate the values now. 00514 // 00515 Init *BitsInit::resolveReferences(Record &R, const RecordVal *RV) const { 00516 bool Changed = false; 00517 SmallVector<Init *, 16> NewBits(getNumBits()); 00518 00519 Init *CachedInit = 0; 00520 Init *CachedBitVar = 0; 00521 bool CachedBitVarChanged = false; 00522 00523 for (unsigned i = 0, e = getNumBits(); i != e; ++i) { 00524 Init *CurBit = Bits[i]; 00525 Init *CurBitVar = CurBit->getBitVar(); 00526 00527 NewBits[i] = CurBit; 00528 00529 if (CurBitVar == CachedBitVar) { 00530 if (CachedBitVarChanged) { 00531 Init *Bit = CachedInit->getBit(CurBit->getBitNum()); 00532 NewBits[i] = fixBitInit(RV, CurBit, Bit); 00533 } 00534 continue; 00535 } 00536 CachedBitVar = CurBitVar; 00537 CachedBitVarChanged = false; 00538 00539 Init *B; 00540 do { 00541 B = CurBitVar; 00542 CurBitVar = CurBitVar->resolveReferences(R, RV); 00543 CachedBitVarChanged |= B != CurBitVar; 00544 Changed |= B != CurBitVar; 00545 } while (B != CurBitVar); 00546 CachedInit = CurBitVar; 00547 00548 if (CachedBitVarChanged) { 00549 Init *Bit = CurBitVar->getBit(CurBit->getBitNum()); 00550 NewBits[i] = fixBitInit(RV, CurBit, Bit); 00551 } 00552 } 00553 00554 if (Changed) 00555 return BitsInit::get(NewBits); 00556 00557 return const_cast<BitsInit *>(this); 00558 } 00559 00560 IntInit *IntInit::get(int64_t V) { 00561 typedef DenseMap<int64_t, IntInit *> Pool; 00562 static Pool ThePool; 00563 00564 IntInit *&I = ThePool[V]; 00565 if (!I) I = new IntInit(V); 00566 return I; 00567 } 00568 00569 std::string IntInit::getAsString() const { 00570 return itostr(Value); 00571 } 00572 00573 Init * 00574 IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) const { 00575 SmallVector<Init *, 16> NewBits(Bits.size()); 00576 00577 for (unsigned i = 0, e = Bits.size(); i != e; ++i) { 00578 if (Bits[i] >= 64) 00579 return 0; 00580 00581 NewBits[i] = BitInit::get(Value & (INT64_C(1) << Bits[i])); 00582 } 00583 return BitsInit::get(NewBits); 00584 } 00585 00586 void StringInit::anchor() { } 00587 00588 StringInit *StringInit::get(StringRef V) { 00589 typedef StringMap<StringInit *> Pool; 00590 static Pool ThePool; 00591 00592 StringInit *&I = ThePool[V]; 00593 if (!I) I = new StringInit(V); 00594 return I; 00595 } 00596 00597 static void ProfileListInit(FoldingSetNodeID &ID, 00598 ArrayRef<Init *> Range, 00599 RecTy *EltTy) { 00600 ID.AddInteger(Range.size()); 00601 ID.AddPointer(EltTy); 00602 00603 for (ArrayRef<Init *>::iterator i = Range.begin(), 00604 iend = Range.end(); 00605 i != iend; 00606 ++i) 00607 ID.AddPointer(*i); 00608 } 00609 00610 ListInit *ListInit::get(ArrayRef<Init *> Range, RecTy *EltTy) { 00611 typedef FoldingSet<ListInit> Pool; 00612 static Pool ThePool; 00613 00614 // Just use the FoldingSetNodeID to compute a hash. Use a DenseMap 00615 // for actual storage. 00616 FoldingSetNodeID ID; 00617 ProfileListInit(ID, Range, EltTy); 00618 00619 void *IP = 0; 00620 if (ListInit *I = ThePool.FindNodeOrInsertPos(ID, IP)) 00621 return I; 00622 00623 ListInit *I = new ListInit(Range, EltTy); 00624 ThePool.InsertNode(I, IP); 00625 return I; 00626 } 00627 00628 void ListInit::Profile(FoldingSetNodeID &ID) const { 00629 ListRecTy *ListType = dyn_cast<ListRecTy>(getType()); 00630 assert(ListType && "Bad type for ListInit!"); 00631 RecTy *EltTy = ListType->getElementType(); 00632 00633 ProfileListInit(ID, Values, EltTy); 00634 } 00635 00636 Init * 00637 ListInit::convertInitListSlice(const std::vector<unsigned> &Elements) const { 00638 std::vector<Init*> Vals; 00639 for (unsigned i = 0, e = Elements.size(); i != e; ++i) { 00640 if (Elements[i] >= getSize()) 00641 return 0; 00642 Vals.push_back(getElement(Elements[i])); 00643 } 00644 return ListInit::get(Vals, getType()); 00645 } 00646 00647 Record *ListInit::getElementAsRecord(unsigned i) const { 00648 assert(i < Values.size() && "List element index out of range!"); 00649 DefInit *DI = dyn_cast<DefInit>(Values[i]); 00650 if (DI == 0) 00651 PrintFatalError("Expected record in list!"); 00652 return DI->getDef(); 00653 } 00654 00655 Init *ListInit::resolveReferences(Record &R, const RecordVal *RV) const { 00656 std::vector<Init*> Resolved; 00657 Resolved.reserve(getSize()); 00658 bool Changed = false; 00659 00660 for (unsigned i = 0, e = getSize(); i != e; ++i) { 00661 Init *E; 00662 Init *CurElt = getElement(i); 00663 00664 do { 00665 E = CurElt; 00666 CurElt = CurElt->resolveReferences(R, RV); 00667 Changed |= E != CurElt; 00668 } while (E != CurElt); 00669 Resolved.push_back(E); 00670 } 00671 00672 if (Changed) 00673 return ListInit::get(Resolved, getType()); 00674 return const_cast<ListInit *>(this); 00675 } 00676 00677 Init *ListInit::resolveListElementReference(Record &R, const RecordVal *IRV, 00678 unsigned Elt) const { 00679 if (Elt >= getSize()) 00680 return 0; // Out of range reference. 00681 Init *E = getElement(Elt); 00682 // If the element is set to some value, or if we are resolving a reference 00683 // to a specific variable and that variable is explicitly unset, then 00684 // replace the VarListElementInit with it. 00685 if (IRV || !isa<UnsetInit>(E)) 00686 return E; 00687 return 0; 00688 } 00689 00690 std::string ListInit::getAsString() const { 00691 std::string Result = "["; 00692 for (unsigned i = 0, e = Values.size(); i != e; ++i) { 00693 if (i) Result += ", "; 00694 Result += Values[i]->getAsString(); 00695 } 00696 return Result + "]"; 00697 } 00698 00699 Init *OpInit::resolveListElementReference(Record &R, const RecordVal *IRV, 00700 unsigned Elt) const { 00701 Init *Resolved = resolveReferences(R, IRV); 00702 OpInit *OResolved = dyn_cast<OpInit>(Resolved); 00703 if (OResolved) { 00704 Resolved = OResolved->Fold(&R, 0); 00705 } 00706 00707 if (Resolved != this) { 00708 TypedInit *Typed = dyn_cast<TypedInit>(Resolved); 00709 assert(Typed && "Expected typed init for list reference"); 00710 if (Typed) { 00711 Init *New = Typed->resolveListElementReference(R, IRV, Elt); 00712 if (New) 00713 return New; 00714 return VarListElementInit::get(Typed, Elt); 00715 } 00716 } 00717 00718 return 0; 00719 } 00720 00721 Init *OpInit::getBit(unsigned Bit) const { 00722 if (getType() == BitRecTy::get()) 00723 return const_cast<OpInit*>(this); 00724 return VarBitInit::get(const_cast<OpInit*>(this), Bit); 00725 } 00726 00727 UnOpInit *UnOpInit::get(UnaryOp opc, Init *lhs, RecTy *Type) { 00728 typedef std::pair<std::pair<unsigned, Init *>, RecTy *> Key; 00729 00730 typedef DenseMap<Key, UnOpInit *> Pool; 00731 static Pool ThePool; 00732 00733 Key TheKey(std::make_pair(std::make_pair(opc, lhs), Type)); 00734 00735 UnOpInit *&I = ThePool[TheKey]; 00736 if (!I) I = new UnOpInit(opc, lhs, Type); 00737 return I; 00738 } 00739 00740 Init *UnOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const { 00741 switch (getOpcode()) { 00742 case CAST: { 00743 if (getType()->getAsString() == "string") { 00744 if (StringInit *LHSs = dyn_cast<StringInit>(LHS)) 00745 return LHSs; 00746 00747 if (DefInit *LHSd = dyn_cast<DefInit>(LHS)) 00748 return StringInit::get(LHSd->getDef()->getName()); 00749 00750 if (IntInit *LHSi = dyn_cast<IntInit>(LHS)) 00751 return StringInit::get(LHSi->getAsString()); 00752 } else { 00753 if (StringInit *LHSs = dyn_cast<StringInit>(LHS)) { 00754 std::string Name = LHSs->getValue(); 00755 00756 // From TGParser::ParseIDValue 00757 if (CurRec) { 00758 if (const RecordVal *RV = CurRec->getValue(Name)) { 00759 if (RV->getType() != getType()) 00760 PrintFatalError("type mismatch in cast"); 00761 return VarInit::get(Name, RV->getType()); 00762 } 00763 00764 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, 00765 ":"); 00766 00767 if (CurRec->isTemplateArg(TemplateArgName)) { 00768 const RecordVal *RV = CurRec->getValue(TemplateArgName); 00769 assert(RV && "Template arg doesn't exist??"); 00770 00771 if (RV->getType() != getType()) 00772 PrintFatalError("type mismatch in cast"); 00773 00774 return VarInit::get(TemplateArgName, RV->getType()); 00775 } 00776 } 00777 00778 if (CurMultiClass) { 00779 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name, "::"); 00780 00781 if (CurMultiClass->Rec.isTemplateArg(MCName)) { 00782 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName); 00783 assert(RV && "Template arg doesn't exist??"); 00784 00785 if (RV->getType() != getType()) 00786 PrintFatalError("type mismatch in cast"); 00787 00788 return VarInit::get(MCName, RV->getType()); 00789 } 00790 } 00791 00792 if (Record *D = (CurRec->getRecords()).getDef(Name)) 00793 return DefInit::get(D); 00794 00795 PrintFatalError(CurRec->getLoc(), 00796 "Undefined reference:'" + Name + "'\n"); 00797 } 00798 } 00799 break; 00800 } 00801 case HEAD: { 00802 if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) { 00803 if (LHSl->getSize() == 0) { 00804 assert(0 && "Empty list in car"); 00805 return 0; 00806 } 00807 return LHSl->getElement(0); 00808 } 00809 break; 00810 } 00811 case TAIL: { 00812 if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) { 00813 if (LHSl->getSize() == 0) { 00814 assert(0 && "Empty list in cdr"); 00815 return 0; 00816 } 00817 // Note the +1. We can't just pass the result of getValues() 00818 // directly. 00819 ArrayRef<Init *>::iterator begin = LHSl->getValues().begin()+1; 00820 ArrayRef<Init *>::iterator end = LHSl->getValues().end(); 00821 ListInit *Result = 00822 ListInit::get(ArrayRef<Init *>(begin, end - begin), 00823 LHSl->getType()); 00824 return Result; 00825 } 00826 break; 00827 } 00828 case EMPTY: { 00829 if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) { 00830 if (LHSl->getSize() == 0) { 00831 return IntInit::get(1); 00832 } else { 00833 return IntInit::get(0); 00834 } 00835 } 00836 if (StringInit *LHSs = dyn_cast<StringInit>(LHS)) { 00837 if (LHSs->getValue().empty()) { 00838 return IntInit::get(1); 00839 } else { 00840 return IntInit::get(0); 00841 } 00842 } 00843 00844 break; 00845 } 00846 } 00847 return const_cast<UnOpInit *>(this); 00848 } 00849 00850 Init *UnOpInit::resolveReferences(Record &R, const RecordVal *RV) const { 00851 Init *lhs = LHS->resolveReferences(R, RV); 00852 00853 if (LHS != lhs) 00854 return (UnOpInit::get(getOpcode(), lhs, getType()))->Fold(&R, 0); 00855 return Fold(&R, 0); 00856 } 00857 00858 std::string UnOpInit::getAsString() const { 00859 std::string Result; 00860 switch (Opc) { 00861 case CAST: Result = "!cast<" + getType()->getAsString() + ">"; break; 00862 case HEAD: Result = "!head"; break; 00863 case TAIL: Result = "!tail"; break; 00864 case EMPTY: Result = "!empty"; break; 00865 } 00866 return Result + "(" + LHS->getAsString() + ")"; 00867 } 00868 00869 BinOpInit *BinOpInit::get(BinaryOp opc, Init *lhs, 00870 Init *rhs, RecTy *Type) { 00871 typedef std::pair< 00872 std::pair<std::pair<unsigned, Init *>, Init *>, 00873 RecTy * 00874 > Key; 00875 00876 typedef DenseMap<Key, BinOpInit *> Pool; 00877 static Pool ThePool; 00878 00879 Key TheKey(std::make_pair(std::make_pair(std::make_pair(opc, lhs), rhs), 00880 Type)); 00881 00882 BinOpInit *&I = ThePool[TheKey]; 00883 if (!I) I = new BinOpInit(opc, lhs, rhs, Type); 00884 return I; 00885 } 00886 00887 Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const { 00888 switch (getOpcode()) { 00889 case CONCAT: { 00890 DagInit *LHSs = dyn_cast<DagInit>(LHS); 00891 DagInit *RHSs = dyn_cast<DagInit>(RHS); 00892 if (LHSs && RHSs) { 00893 DefInit *LOp = dyn_cast<DefInit>(LHSs->getOperator()); 00894 DefInit *ROp = dyn_cast<DefInit>(RHSs->getOperator()); 00895 if (LOp == 0 || ROp == 0 || LOp->getDef() != ROp->getDef()) 00896 PrintFatalError("Concated Dag operators do not match!"); 00897 std::vector<Init*> Args; 00898 std::vector<std::string> ArgNames; 00899 for (unsigned i = 0, e = LHSs->getNumArgs(); i != e; ++i) { 00900 Args.push_back(LHSs->getArg(i)); 00901 ArgNames.push_back(LHSs->getArgName(i)); 00902 } 00903 for (unsigned i = 0, e = RHSs->getNumArgs(); i != e; ++i) { 00904 Args.push_back(RHSs->getArg(i)); 00905 ArgNames.push_back(RHSs->getArgName(i)); 00906 } 00907 return DagInit::get(LHSs->getOperator(), "", Args, ArgNames); 00908 } 00909 break; 00910 } 00911 case STRCONCAT: { 00912 StringInit *LHSs = dyn_cast<StringInit>(LHS); 00913 StringInit *RHSs = dyn_cast<StringInit>(RHS); 00914 if (LHSs && RHSs) 00915 return StringInit::get(LHSs->getValue() + RHSs->getValue()); 00916 break; 00917 } 00918 case EQ: { 00919 // try to fold eq comparison for 'bit' and 'int', otherwise fallback 00920 // to string objects. 00921 IntInit *L = 00922 dyn_cast_or_null<IntInit>(LHS->convertInitializerTo(IntRecTy::get())); 00923 IntInit *R = 00924 dyn_cast_or_null<IntInit>(RHS->convertInitializerTo(IntRecTy::get())); 00925 00926 if (L && R) 00927 return IntInit::get(L->getValue() == R->getValue()); 00928 00929 StringInit *LHSs = dyn_cast<StringInit>(LHS); 00930 StringInit *RHSs = dyn_cast<StringInit>(RHS); 00931 00932 // Make sure we've resolved 00933 if (LHSs && RHSs) 00934 return IntInit::get(LHSs->getValue() == RHSs->getValue()); 00935 00936 break; 00937 } 00938 case ADD: 00939 case SHL: 00940 case SRA: 00941 case SRL: { 00942 IntInit *LHSi = dyn_cast<IntInit>(LHS); 00943 IntInit *RHSi = dyn_cast<IntInit>(RHS); 00944 if (LHSi && RHSi) { 00945 int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue(); 00946 int64_t Result; 00947 switch (getOpcode()) { 00948 default: llvm_unreachable("Bad opcode!"); 00949 case ADD: Result = LHSv + RHSv; break; 00950 case SHL: Result = LHSv << RHSv; break; 00951 case SRA: Result = LHSv >> RHSv; break; 00952 case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break; 00953 } 00954 return IntInit::get(Result); 00955 } 00956 break; 00957 } 00958 } 00959 return const_cast<BinOpInit *>(this); 00960 } 00961 00962 Init *BinOpInit::resolveReferences(Record &R, const RecordVal *RV) const { 00963 Init *lhs = LHS->resolveReferences(R, RV); 00964 Init *rhs = RHS->resolveReferences(R, RV); 00965 00966 if (LHS != lhs || RHS != rhs) 00967 return (BinOpInit::get(getOpcode(), lhs, rhs, getType()))->Fold(&R, 0); 00968 return Fold(&R, 0); 00969 } 00970 00971 std::string BinOpInit::getAsString() const { 00972 std::string Result; 00973 switch (Opc) { 00974 case CONCAT: Result = "!con"; break; 00975 case ADD: Result = "!add"; break; 00976 case SHL: Result = "!shl"; break; 00977 case SRA: Result = "!sra"; break; 00978 case SRL: Result = "!srl"; break; 00979 case EQ: Result = "!eq"; break; 00980 case STRCONCAT: Result = "!strconcat"; break; 00981 } 00982 return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")"; 00983 } 00984 00985 TernOpInit *TernOpInit::get(TernaryOp opc, Init *lhs, 00986 Init *mhs, Init *rhs, 00987 RecTy *Type) { 00988 typedef std::pair< 00989 std::pair< 00990 std::pair<std::pair<unsigned, RecTy *>, Init *>, 00991 Init * 00992 >, 00993 Init * 00994 > Key; 00995 00996 typedef DenseMap<Key, TernOpInit *> Pool; 00997 static Pool ThePool; 00998 00999 Key TheKey(std::make_pair(std::make_pair(std::make_pair(std::make_pair(opc, 01000 Type), 01001 lhs), 01002 mhs), 01003 rhs)); 01004 01005 TernOpInit *&I = ThePool[TheKey]; 01006 if (!I) I = new TernOpInit(opc, lhs, mhs, rhs, Type); 01007 return I; 01008 } 01009 01010 static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type, 01011 Record *CurRec, MultiClass *CurMultiClass); 01012 01013 static Init *EvaluateOperation(OpInit *RHSo, Init *LHS, Init *Arg, 01014 RecTy *Type, Record *CurRec, 01015 MultiClass *CurMultiClass) { 01016 std::vector<Init *> NewOperands; 01017 01018 TypedInit *TArg = dyn_cast<TypedInit>(Arg); 01019 01020 // If this is a dag, recurse 01021 if (TArg && TArg->getType()->getAsString() == "dag") { 01022 Init *Result = ForeachHelper(LHS, Arg, RHSo, Type, 01023 CurRec, CurMultiClass); 01024 if (Result != 0) { 01025 return Result; 01026 } else { 01027 return 0; 01028 } 01029 } 01030 01031 for (int i = 0; i < RHSo->getNumOperands(); ++i) { 01032 OpInit *RHSoo = dyn_cast<OpInit>(RHSo->getOperand(i)); 01033 01034 if (RHSoo) { 01035 Init *Result = EvaluateOperation(RHSoo, LHS, Arg, 01036 Type, CurRec, CurMultiClass); 01037 if (Result != 0) { 01038 NewOperands.push_back(Result); 01039 } else { 01040 NewOperands.push_back(Arg); 01041 } 01042 } else if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) { 01043 NewOperands.push_back(Arg); 01044 } else { 01045 NewOperands.push_back(RHSo->getOperand(i)); 01046 } 01047 } 01048 01049 // Now run the operator and use its result as the new leaf 01050 const OpInit *NewOp = RHSo->clone(NewOperands); 01051 Init *NewVal = NewOp->Fold(CurRec, CurMultiClass); 01052 if (NewVal != NewOp) 01053 return NewVal; 01054 01055 return 0; 01056 } 01057 01058 static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type, 01059 Record *CurRec, MultiClass *CurMultiClass) { 01060 DagInit *MHSd = dyn_cast<DagInit>(MHS); 01061 ListInit *MHSl = dyn_cast<ListInit>(MHS); 01062 01063 OpInit *RHSo = dyn_cast<OpInit>(RHS); 01064 01065 if (!RHSo) { 01066 PrintFatalError(CurRec->getLoc(), "!foreach requires an operator\n"); 01067 } 01068 01069 TypedInit *LHSt = dyn_cast<TypedInit>(LHS); 01070 01071 if (!LHSt) 01072 PrintFatalError(CurRec->getLoc(), "!foreach requires typed variable\n"); 01073 01074 if ((MHSd && isa<DagRecTy>(Type)) || (MHSl && isa<ListRecTy>(Type))) { 01075 if (MHSd) { 01076 Init *Val = MHSd->getOperator(); 01077 Init *Result = EvaluateOperation(RHSo, LHS, Val, 01078 Type, CurRec, CurMultiClass); 01079 if (Result != 0) { 01080 Val = Result; 01081 } 01082 01083 std::vector<std::pair<Init *, std::string> > args; 01084 for (unsigned int i = 0; i < MHSd->getNumArgs(); ++i) { 01085 Init *Arg; 01086 std::string ArgName; 01087 Arg = MHSd->getArg(i); 01088 ArgName = MHSd->getArgName(i); 01089 01090 // Process args 01091 Init *Result = EvaluateOperation(RHSo, LHS, Arg, Type, 01092 CurRec, CurMultiClass); 01093 if (Result != 0) { 01094 Arg = Result; 01095 } 01096 01097 // TODO: Process arg names 01098 args.push_back(std::make_pair(Arg, ArgName)); 01099 } 01100 01101 return DagInit::get(Val, "", args); 01102 } 01103 if (MHSl) { 01104 std::vector<Init *> NewOperands; 01105 std::vector<Init *> NewList(MHSl->begin(), MHSl->end()); 01106 01107 for (std::vector<Init *>::iterator li = NewList.begin(), 01108 liend = NewList.end(); 01109 li != liend; 01110 ++li) { 01111 Init *Item = *li; 01112 NewOperands.clear(); 01113 for(int i = 0; i < RHSo->getNumOperands(); ++i) { 01114 // First, replace the foreach variable with the list item 01115 if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) { 01116 NewOperands.push_back(Item); 01117 } else { 01118 NewOperands.push_back(RHSo->getOperand(i)); 01119 } 01120 } 01121 01122 // Now run the operator and use its result as the new list item 01123 const OpInit *NewOp = RHSo->clone(NewOperands); 01124 Init *NewItem = NewOp->Fold(CurRec, CurMultiClass); 01125 if (NewItem != NewOp) 01126 *li = NewItem; 01127 } 01128 return ListInit::get(NewList, MHSl->getType()); 01129 } 01130 } 01131 return 0; 01132 } 01133 01134 Init *TernOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const { 01135 switch (getOpcode()) { 01136 case SUBST: { 01137 DefInit *LHSd = dyn_cast<DefInit>(LHS); 01138 VarInit *LHSv = dyn_cast<VarInit>(LHS); 01139 StringInit *LHSs = dyn_cast<StringInit>(LHS); 01140 01141 DefInit *MHSd = dyn_cast<DefInit>(MHS); 01142 VarInit *MHSv = dyn_cast<VarInit>(MHS); 01143 StringInit *MHSs = dyn_cast<StringInit>(MHS); 01144 01145 DefInit *RHSd = dyn_cast<DefInit>(RHS); 01146 VarInit *RHSv = dyn_cast<VarInit>(RHS); 01147 StringInit *RHSs = dyn_cast<StringInit>(RHS); 01148 01149 if ((LHSd && MHSd && RHSd) 01150 || (LHSv && MHSv && RHSv) 01151 || (LHSs && MHSs && RHSs)) { 01152 if (RHSd) { 01153 Record *Val = RHSd->getDef(); 01154 if (LHSd->getAsString() == RHSd->getAsString()) { 01155 Val = MHSd->getDef(); 01156 } 01157 return DefInit::get(Val); 01158 } 01159 if (RHSv) { 01160 std::string Val = RHSv->getName(); 01161 if (LHSv->getAsString() == RHSv->getAsString()) { 01162 Val = MHSv->getName(); 01163 } 01164 return VarInit::get(Val, getType()); 01165 } 01166 if (RHSs) { 01167 std::string Val = RHSs->getValue(); 01168 01169 std::string::size_type found; 01170 std::string::size_type idx = 0; 01171 do { 01172 found = Val.find(LHSs->getValue(), idx); 01173 if (found != std::string::npos) { 01174 Val.replace(found, LHSs->getValue().size(), MHSs->getValue()); 01175 } 01176 idx = found + MHSs->getValue().size(); 01177 } while (found != std::string::npos); 01178 01179 return StringInit::get(Val); 01180 } 01181 } 01182 break; 01183 } 01184 01185 case FOREACH: { 01186 Init *Result = ForeachHelper(LHS, MHS, RHS, getType(), 01187 CurRec, CurMultiClass); 01188 if (Result != 0) { 01189 return Result; 01190 } 01191 break; 01192 } 01193 01194 case IF: { 01195 IntInit *LHSi = dyn_cast<IntInit>(LHS); 01196 if (Init *I = LHS->convertInitializerTo(IntRecTy::get())) 01197 LHSi = dyn_cast<IntInit>(I); 01198 if (LHSi) { 01199 if (LHSi->getValue()) { 01200 return MHS; 01201 } else { 01202 return RHS; 01203 } 01204 } 01205 break; 01206 } 01207 } 01208 01209 return const_cast<TernOpInit *>(this); 01210 } 01211 01212 Init *TernOpInit::resolveReferences(Record &R, 01213 const RecordVal *RV) const { 01214 Init *lhs = LHS->resolveReferences(R, RV); 01215 01216 if (Opc == IF && lhs != LHS) { 01217 IntInit *Value = dyn_cast<IntInit>(lhs); 01218 if (Init *I = lhs->convertInitializerTo(IntRecTy::get())) 01219 Value = dyn_cast<IntInit>(I); 01220 if (Value != 0) { 01221 // Short-circuit 01222 if (Value->getValue()) { 01223 Init *mhs = MHS->resolveReferences(R, RV); 01224 return (TernOpInit::get(getOpcode(), lhs, mhs, 01225 RHS, getType()))->Fold(&R, 0); 01226 } else { 01227 Init *rhs = RHS->resolveReferences(R, RV); 01228 return (TernOpInit::get(getOpcode(), lhs, MHS, 01229 rhs, getType()))->Fold(&R, 0); 01230 } 01231 } 01232 } 01233 01234 Init *mhs = MHS->resolveReferences(R, RV); 01235 Init *rhs = RHS->resolveReferences(R, RV); 01236 01237 if (LHS != lhs || MHS != mhs || RHS != rhs) 01238 return (TernOpInit::get(getOpcode(), lhs, mhs, rhs, 01239 getType()))->Fold(&R, 0); 01240 return Fold(&R, 0); 01241 } 01242 01243 std::string TernOpInit::getAsString() const { 01244 std::string Result; 01245 switch (Opc) { 01246 case SUBST: Result = "!subst"; break; 01247 case FOREACH: Result = "!foreach"; break; 01248 case IF: Result = "!if"; break; 01249 } 01250 return Result + "(" + LHS->getAsString() + ", " + MHS->getAsString() + ", " 01251 + RHS->getAsString() + ")"; 01252 } 01253 01254 RecTy *TypedInit::getFieldType(const std::string &FieldName) const { 01255 if (RecordRecTy *RecordType = dyn_cast<RecordRecTy>(getType())) 01256 if (RecordVal *Field = RecordType->getRecord()->getValue(FieldName)) 01257 return Field->getType(); 01258 return 0; 01259 } 01260 01261 Init * 01262 TypedInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) const { 01263 BitsRecTy *T = dyn_cast<BitsRecTy>(getType()); 01264 if (T == 0) return 0; // Cannot subscript a non-bits variable. 01265 unsigned NumBits = T->getNumBits(); 01266 01267 SmallVector<Init *, 16> NewBits(Bits.size()); 01268 for (unsigned i = 0, e = Bits.size(); i != e; ++i) { 01269 if (Bits[i] >= NumBits) 01270 return 0; 01271 01272 NewBits[i] = VarBitInit::get(const_cast<TypedInit *>(this), Bits[i]); 01273 } 01274 return BitsInit::get(NewBits); 01275 } 01276 01277 Init * 01278 TypedInit::convertInitListSlice(const std::vector<unsigned> &Elements) const { 01279 ListRecTy *T = dyn_cast<ListRecTy>(getType()); 01280 if (T == 0) return 0; // Cannot subscript a non-list variable. 01281 01282 if (Elements.size() == 1) 01283 return VarListElementInit::get(const_cast<TypedInit *>(this), Elements[0]); 01284 01285 std::vector<Init*> ListInits; 01286 ListInits.reserve(Elements.size()); 01287 for (unsigned i = 0, e = Elements.size(); i != e; ++i) 01288 ListInits.push_back(VarListElementInit::get(const_cast<TypedInit *>(this), 01289 Elements[i])); 01290 return ListInit::get(ListInits, T); 01291 } 01292 01293 01294 VarInit *VarInit::get(const std::string &VN, RecTy *T) { 01295 Init *Value = StringInit::get(VN); 01296 return VarInit::get(Value, T); 01297 } 01298 01299 VarInit *VarInit::get(Init *VN, RecTy *T) { 01300 typedef std::pair<RecTy *, Init *> Key; 01301 typedef DenseMap<Key, VarInit *> Pool; 01302 static Pool ThePool; 01303 01304 Key TheKey(std::make_pair(T, VN)); 01305 01306 VarInit *&I = ThePool[TheKey]; 01307 if (!I) I = new VarInit(VN, T); 01308 return I; 01309 } 01310 01311 const std::string &VarInit::getName() const { 01312 StringInit *NameString = dyn_cast<StringInit>(getNameInit()); 01313 assert(NameString && "VarInit name is not a string!"); 01314 return NameString->getValue(); 01315 } 01316 01317 Init *VarInit::getBit(unsigned Bit) const { 01318 if (getType() == BitRecTy::get()) 01319 return const_cast<VarInit*>(this); 01320 return VarBitInit::get(const_cast<VarInit*>(this), Bit); 01321 } 01322 01323 Init *VarInit::resolveListElementReference(Record &R, 01324 const RecordVal *IRV, 01325 unsigned Elt) const { 01326 if (R.isTemplateArg(getNameInit())) return 0; 01327 if (IRV && IRV->getNameInit() != getNameInit()) return 0; 01328 01329 RecordVal *RV = R.getValue(getNameInit()); 01330 assert(RV && "Reference to a non-existent variable?"); 01331 ListInit *LI = dyn_cast<ListInit>(RV->getValue()); 01332 if (!LI) { 01333 TypedInit *VI = dyn_cast<TypedInit>(RV->getValue()); 01334 assert(VI && "Invalid list element!"); 01335 return VarListElementInit::get(VI, Elt); 01336 } 01337 01338 if (Elt >= LI->getSize()) 01339 return 0; // Out of range reference. 01340 Init *E = LI->getElement(Elt); 01341 // If the element is set to some value, or if we are resolving a reference 01342 // to a specific variable and that variable is explicitly unset, then 01343 // replace the VarListElementInit with it. 01344 if (IRV || !isa<UnsetInit>(E)) 01345 return E; 01346 return 0; 01347 } 01348 01349 01350 RecTy *VarInit::getFieldType(const std::string &FieldName) const { 01351 if (RecordRecTy *RTy = dyn_cast<RecordRecTy>(getType())) 01352 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName)) 01353 return RV->getType(); 01354 return 0; 01355 } 01356 01357 Init *VarInit::getFieldInit(Record &R, const RecordVal *RV, 01358 const std::string &FieldName) const { 01359 if (isa<RecordRecTy>(getType())) 01360 if (const RecordVal *Val = R.getValue(VarName)) { 01361 if (RV != Val && (RV || isa<UnsetInit>(Val->getValue()))) 01362 return 0; 01363 Init *TheInit = Val->getValue(); 01364 assert(TheInit != this && "Infinite loop detected!"); 01365 if (Init *I = TheInit->getFieldInit(R, RV, FieldName)) 01366 return I; 01367 else 01368 return 0; 01369 } 01370 return 0; 01371 } 01372 01373 /// resolveReferences - This method is used by classes that refer to other 01374 /// variables which may not be defined at the time the expression is formed. 01375 /// If a value is set for the variable later, this method will be called on 01376 /// users of the value to allow the value to propagate out. 01377 /// 01378 Init *VarInit::resolveReferences(Record &R, const RecordVal *RV) const { 01379 if (RecordVal *Val = R.getValue(VarName)) 01380 if (RV == Val || (RV == 0 && !isa<UnsetInit>(Val->getValue()))) 01381 return Val->getValue(); 01382 return const_cast<VarInit *>(this); 01383 } 01384 01385 VarBitInit *VarBitInit::get(TypedInit *T, unsigned B) { 01386 typedef std::pair<TypedInit *, unsigned> Key; 01387 typedef DenseMap<Key, VarBitInit *> Pool; 01388 01389 static Pool ThePool; 01390 01391 Key TheKey(std::make_pair(T, B)); 01392 01393 VarBitInit *&I = ThePool[TheKey]; 01394 if (!I) I = new VarBitInit(T, B); 01395 return I; 01396 } 01397 01398 std::string VarBitInit::getAsString() const { 01399 return TI->getAsString() + "{" + utostr(Bit) + "}"; 01400 } 01401 01402 Init *VarBitInit::resolveReferences(Record &R, const RecordVal *RV) const { 01403 Init *I = TI->resolveReferences(R, RV); 01404 if (TI != I) 01405 return I->getBit(getBitNum()); 01406 01407 return const_cast<VarBitInit*>(this); 01408 } 01409 01410 VarListElementInit *VarListElementInit::get(TypedInit *T, 01411 unsigned E) { 01412 typedef std::pair<TypedInit *, unsigned> Key; 01413 typedef DenseMap<Key, VarListElementInit *> Pool; 01414 01415 static Pool ThePool; 01416 01417 Key TheKey(std::make_pair(T, E)); 01418 01419 VarListElementInit *&I = ThePool[TheKey]; 01420 if (!I) I = new VarListElementInit(T, E); 01421 return I; 01422 } 01423 01424 std::string VarListElementInit::getAsString() const { 01425 return TI->getAsString() + "[" + utostr(Element) + "]"; 01426 } 01427 01428 Init * 01429 VarListElementInit::resolveReferences(Record &R, const RecordVal *RV) const { 01430 if (Init *I = getVariable()->resolveListElementReference(R, RV, 01431 getElementNum())) 01432 return I; 01433 return const_cast<VarListElementInit *>(this); 01434 } 01435 01436 Init *VarListElementInit::getBit(unsigned Bit) const { 01437 if (getType() == BitRecTy::get()) 01438 return const_cast<VarListElementInit*>(this); 01439 return VarBitInit::get(const_cast<VarListElementInit*>(this), Bit); 01440 } 01441 01442 Init *VarListElementInit:: resolveListElementReference(Record &R, 01443 const RecordVal *RV, 01444 unsigned Elt) const { 01445 Init *Result = TI->resolveListElementReference(R, RV, Element); 01446 01447 if (Result) { 01448 if (TypedInit *TInit = dyn_cast<TypedInit>(Result)) { 01449 Init *Result2 = TInit->resolveListElementReference(R, RV, Elt); 01450 if (Result2) return Result2; 01451 return new VarListElementInit(TInit, Elt); 01452 } 01453 return Result; 01454 } 01455 01456 return 0; 01457 } 01458 01459 DefInit *DefInit::get(Record *R) { 01460 return R->getDefInit(); 01461 } 01462 01463 RecTy *DefInit::getFieldType(const std::string &FieldName) const { 01464 if (const RecordVal *RV = Def->getValue(FieldName)) 01465 return RV->getType(); 01466 return 0; 01467 } 01468 01469 Init *DefInit::getFieldInit(Record &R, const RecordVal *RV, 01470 const std::string &FieldName) const { 01471 return Def->getValue(FieldName)->getValue(); 01472 } 01473 01474 01475 std::string DefInit::getAsString() const { 01476 return Def->getName(); 01477 } 01478 01479 FieldInit *FieldInit::get(Init *R, const std::string &FN) { 01480 typedef std::pair<Init *, TableGenStringKey> Key; 01481 typedef DenseMap<Key, FieldInit *> Pool; 01482 static Pool ThePool; 01483 01484 Key TheKey(std::make_pair(R, FN)); 01485 01486 FieldInit *&I = ThePool[TheKey]; 01487 if (!I) I = new FieldInit(R, FN); 01488 return I; 01489 } 01490 01491 Init *FieldInit::getBit(unsigned Bit) const { 01492 if (getType() == BitRecTy::get()) 01493 return const_cast<FieldInit*>(this); 01494 return VarBitInit::get(const_cast<FieldInit*>(this), Bit); 01495 } 01496 01497 Init *FieldInit::resolveListElementReference(Record &R, const RecordVal *RV, 01498 unsigned Elt) const { 01499 if (Init *ListVal = Rec->getFieldInit(R, RV, FieldName)) 01500 if (ListInit *LI = dyn_cast<ListInit>(ListVal)) { 01501 if (Elt >= LI->getSize()) return 0; 01502 Init *E = LI->getElement(Elt); 01503 01504 // If the element is set to some value, or if we are resolving a 01505 // reference to a specific variable and that variable is explicitly 01506 // unset, then replace the VarListElementInit with it. 01507 if (RV || !isa<UnsetInit>(E)) 01508 return E; 01509 } 01510 return 0; 01511 } 01512 01513 Init *FieldInit::resolveReferences(Record &R, const RecordVal *RV) const { 01514 Init *NewRec = RV ? Rec->resolveReferences(R, RV) : Rec; 01515 01516 Init *BitsVal = NewRec->getFieldInit(R, RV, FieldName); 01517 if (BitsVal) { 01518 Init *BVR = BitsVal->resolveReferences(R, RV); 01519 return BVR->isComplete() ? BVR : const_cast<FieldInit *>(this); 01520 } 01521 01522 if (NewRec != Rec) { 01523 return FieldInit::get(NewRec, FieldName); 01524 } 01525 return const_cast<FieldInit *>(this); 01526 } 01527 01528 static void ProfileDagInit(FoldingSetNodeID &ID, Init *V, const std::string &VN, 01529 ArrayRef<Init *> ArgRange, 01530 ArrayRef<std::string> NameRange) { 01531 ID.AddPointer(V); 01532 ID.AddString(VN); 01533 01534 ArrayRef<Init *>::iterator Arg = ArgRange.begin(); 01535 ArrayRef<std::string>::iterator Name = NameRange.begin(); 01536 while (Arg != ArgRange.end()) { 01537 assert(Name != NameRange.end() && "Arg name underflow!"); 01538 ID.AddPointer(*Arg++); 01539 ID.AddString(*Name++); 01540 } 01541 assert(Name == NameRange.end() && "Arg name overflow!"); 01542 } 01543 01544 DagInit * 01545 DagInit::get(Init *V, const std::string &VN, 01546 ArrayRef<Init *> ArgRange, 01547 ArrayRef<std::string> NameRange) { 01548 typedef FoldingSet<DagInit> Pool; 01549 static Pool ThePool; 01550 01551 FoldingSetNodeID ID; 01552 ProfileDagInit(ID, V, VN, ArgRange, NameRange); 01553 01554 void *IP = 0; 01555 if (DagInit *I = ThePool.FindNodeOrInsertPos(ID, IP)) 01556 return I; 01557 01558 DagInit *I = new DagInit(V, VN, ArgRange, NameRange); 01559 ThePool.InsertNode(I, IP); 01560 01561 return I; 01562 } 01563 01564 DagInit * 01565 DagInit::get(Init *V, const std::string &VN, 01566 const std::vector<std::pair<Init*, std::string> > &args) { 01567 typedef std::pair<Init*, std::string> PairType; 01568 01569 std::vector<Init *> Args; 01570 std::vector<std::string> Names; 01571 01572 for (std::vector<PairType>::const_iterator i = args.begin(), 01573 iend = args.end(); 01574 i != iend; 01575 ++i) { 01576 Args.push_back(i->first); 01577 Names.push_back(i->second); 01578 } 01579 01580 return DagInit::get(V, VN, Args, Names); 01581 } 01582 01583 void DagInit::Profile(FoldingSetNodeID &ID) const { 01584 ProfileDagInit(ID, Val, ValName, Args, ArgNames); 01585 } 01586 01587 Init *DagInit::resolveReferences(Record &R, const RecordVal *RV) const { 01588 std::vector<Init*> NewArgs; 01589 for (unsigned i = 0, e = Args.size(); i != e; ++i) 01590 NewArgs.push_back(Args[i]->resolveReferences(R, RV)); 01591 01592 Init *Op = Val->resolveReferences(R, RV); 01593 01594 if (Args != NewArgs || Op != Val) 01595 return DagInit::get(Op, ValName, NewArgs, ArgNames); 01596 01597 return const_cast<DagInit *>(this); 01598 } 01599 01600 01601 std::string DagInit::getAsString() const { 01602 std::string Result = "(" + Val->getAsString(); 01603 if (!ValName.empty()) 01604 Result += ":" + ValName; 01605 if (Args.size()) { 01606 Result += " " + Args[0]->getAsString(); 01607 if (!ArgNames[0].empty()) Result += ":$" + ArgNames[0]; 01608 for (unsigned i = 1, e = Args.size(); i != e; ++i) { 01609 Result += ", " + Args[i]->getAsString(); 01610 if (!ArgNames[i].empty()) Result += ":$" + ArgNames[i]; 01611 } 01612 } 01613 return Result + ")"; 01614 } 01615 01616 01617 //===----------------------------------------------------------------------===// 01618 // Other implementations 01619 //===----------------------------------------------------------------------===// 01620 01621 RecordVal::RecordVal(Init *N, RecTy *T, unsigned P) 01622 : Name(N), Ty(T), Prefix(P) { 01623 Value = Ty->convertValue(UnsetInit::get()); 01624 assert(Value && "Cannot create unset value for current type!"); 01625 } 01626 01627 RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P) 01628 : Name(StringInit::get(N)), Ty(T), Prefix(P) { 01629 Value = Ty->convertValue(UnsetInit::get()); 01630 assert(Value && "Cannot create unset value for current type!"); 01631 } 01632 01633 const std::string &RecordVal::getName() const { 01634 StringInit *NameString = dyn_cast<StringInit>(Name); 01635 assert(NameString && "RecordVal name is not a string!"); 01636 return NameString->getValue(); 01637 } 01638 01639 void RecordVal::dump() const { errs() << *this; } 01640 01641 void RecordVal::print(raw_ostream &OS, bool PrintSem) const { 01642 if (getPrefix()) OS << "field "; 01643 OS << *getType() << " " << getNameInitAsString(); 01644 01645 if (getValue()) 01646 OS << " = " << *getValue(); 01647 01648 if (PrintSem) OS << ";\n"; 01649 } 01650 01651 unsigned Record::LastID = 0; 01652 01653 void Record::init() { 01654 checkName(); 01655 01656 // Every record potentially has a def at the top. This value is 01657 // replaced with the top-level def name at instantiation time. 01658 RecordVal DN("NAME", StringRecTy::get(), 0); 01659 addValue(DN); 01660 } 01661 01662 void Record::checkName() { 01663 // Ensure the record name has string type. 01664 const TypedInit *TypedName = dyn_cast<const TypedInit>(Name); 01665 assert(TypedName && "Record name is not typed!"); 01666 RecTy *Type = TypedName->getType(); 01667 if (!isa<StringRecTy>(Type)) 01668 PrintFatalError(getLoc(), "Record name is not a string!"); 01669 } 01670 01671 DefInit *Record::getDefInit() { 01672 if (!TheInit) 01673 TheInit = new DefInit(this, new RecordRecTy(this)); 01674 return TheInit; 01675 } 01676 01677 const std::string &Record::getName() const { 01678 const StringInit *NameString = dyn_cast<StringInit>(Name); 01679 assert(NameString && "Record name is not a string!"); 01680 return NameString->getValue(); 01681 } 01682 01683 void Record::setName(Init *NewName) { 01684 if (TrackedRecords.getDef(Name->getAsUnquotedString()) == this) { 01685 TrackedRecords.removeDef(Name->getAsUnquotedString()); 01686 TrackedRecords.addDef(this); 01687 } else if (TrackedRecords.getClass(Name->getAsUnquotedString()) == this) { 01688 TrackedRecords.removeClass(Name->getAsUnquotedString()); 01689 TrackedRecords.addClass(this); 01690 } // Otherwise this isn't yet registered. 01691 Name = NewName; 01692 checkName(); 01693 // DO NOT resolve record values to the name at this point because 01694 // there might be default values for arguments of this def. Those 01695 // arguments might not have been resolved yet so we don't want to 01696 // prematurely assume values for those arguments were not passed to 01697 // this def. 01698 // 01699 // Nonetheless, it may be that some of this Record's values 01700 // reference the record name. Indeed, the reason for having the 01701 // record name be an Init is to provide this flexibility. The extra 01702 // resolve steps after completely instantiating defs takes care of 01703 // this. See TGParser::ParseDef and TGParser::ParseDefm. 01704 } 01705 01706 void Record::setName(const std::string &Name) { 01707 setName(StringInit::get(Name)); 01708 } 01709 01710 /// resolveReferencesTo - If anything in this record refers to RV, replace the 01711 /// reference to RV with the RHS of RV. If RV is null, we resolve all possible 01712 /// references. 01713 void Record::resolveReferencesTo(const RecordVal *RV) { 01714 for (unsigned i = 0, e = Values.size(); i != e; ++i) { 01715 if (RV == &Values[i]) // Skip resolve the same field as the given one 01716 continue; 01717 if (Init *V = Values[i].getValue()) 01718 if (Values[i].setValue(V->resolveReferences(*this, RV))) 01719 PrintFatalError(getLoc(), "Invalid value is found when setting '" 01720 + Values[i].getNameInitAsString() 01721 + "' after resolving references" 01722 + (RV ? " against '" + RV->getNameInitAsString() 01723 + "' of (" 01724 + RV->getValue()->getAsUnquotedString() + ")" 01725 : "") 01726 + "\n"); 01727 } 01728 Init *OldName = getNameInit(); 01729 Init *NewName = Name->resolveReferences(*this, RV); 01730 if (NewName != OldName) { 01731 // Re-register with RecordKeeper. 01732 setName(NewName); 01733 } 01734 } 01735 01736 void Record::dump() const { errs() << *this; } 01737 01738 raw_ostream &llvm::operator<<(raw_ostream &OS, const Record &R) { 01739 OS << R.getNameInitAsString(); 01740 01741 const std::vector<Init *> &TArgs = R.getTemplateArgs(); 01742 if (!TArgs.empty()) { 01743 OS << "<"; 01744 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) { 01745 if (i) OS << ", "; 01746 const RecordVal *RV = R.getValue(TArgs[i]); 01747 assert(RV && "Template argument record not found??"); 01748 RV->print(OS, false); 01749 } 01750 OS << ">"; 01751 } 01752 01753 OS << " {"; 01754 const std::vector<Record*> &SC = R.getSuperClasses(); 01755 if (!SC.empty()) { 01756 OS << "\t//"; 01757 for (unsigned i = 0, e = SC.size(); i != e; ++i) 01758 OS << " " << SC[i]->getNameInitAsString(); 01759 } 01760 OS << "\n"; 01761 01762 const std::vector<RecordVal> &Vals = R.getValues(); 01763 for (unsigned i = 0, e = Vals.size(); i != e; ++i) 01764 if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName())) 01765 OS << Vals[i]; 01766 for (unsigned i = 0, e = Vals.size(); i != e; ++i) 01767 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName())) 01768 OS << Vals[i]; 01769 01770 return OS << "}\n"; 01771 } 01772 01773 /// getValueInit - Return the initializer for a value with the specified name, 01774 /// or abort if the field does not exist. 01775 /// 01776 Init *Record::getValueInit(StringRef FieldName) const { 01777 const RecordVal *R = getValue(FieldName); 01778 if (R == 0 || R->getValue() == 0) 01779 PrintFatalError(getLoc(), "Record `" + getName() + 01780 "' does not have a field named `" + FieldName.str() + "'!\n"); 01781 return R->getValue(); 01782 } 01783 01784 01785 /// getValueAsString - This method looks up the specified field and returns its 01786 /// value as a string, aborts if the field does not exist or if 01787 /// the value is not a string. 01788 /// 01789 std::string Record::getValueAsString(StringRef FieldName) const { 01790 const RecordVal *R = getValue(FieldName); 01791 if (R == 0 || R->getValue() == 0) 01792 PrintFatalError(getLoc(), "Record `" + getName() + 01793 "' does not have a field named `" + FieldName.str() + "'!\n"); 01794 01795 if (StringInit *SI = dyn_cast<StringInit>(R->getValue())) 01796 return SI->getValue(); 01797 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + 01798 FieldName.str() + "' does not have a string initializer!"); 01799 } 01800 01801 /// getValueAsBitsInit - This method looks up the specified field and returns 01802 /// its value as a BitsInit, aborts if the field does not exist or if 01803 /// the value is not the right type. 01804 /// 01805 BitsInit *Record::getValueAsBitsInit(StringRef FieldName) const { 01806 const RecordVal *R = getValue(FieldName); 01807 if (R == 0 || R->getValue() == 0) 01808 PrintFatalError(getLoc(), "Record `" + getName() + 01809 "' does not have a field named `" + FieldName.str() + "'!\n"); 01810 01811 if (BitsInit *BI = dyn_cast<BitsInit>(R->getValue())) 01812 return BI; 01813 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + 01814 FieldName.str() + "' does not have a BitsInit initializer!"); 01815 } 01816 01817 /// getValueAsListInit - This method looks up the specified field and returns 01818 /// its value as a ListInit, aborting if the field does not exist or if 01819 /// the value is not the right type. 01820 /// 01821 ListInit *Record::getValueAsListInit(StringRef FieldName) const { 01822 const RecordVal *R = getValue(FieldName); 01823 if (R == 0 || R->getValue() == 0) 01824 PrintFatalError(getLoc(), "Record `" + getName() + 01825 "' does not have a field named `" + FieldName.str() + "'!\n"); 01826 01827 if (ListInit *LI = dyn_cast<ListInit>(R->getValue())) 01828 return LI; 01829 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + 01830 FieldName.str() + "' does not have a list initializer!"); 01831 } 01832 01833 /// getValueAsListOfDefs - This method looks up the specified field and returns 01834 /// its value as a vector of records, aborting if the field does not exist 01835 /// or if the value is not the right type. 01836 /// 01837 std::vector<Record*> 01838 Record::getValueAsListOfDefs(StringRef FieldName) const { 01839 ListInit *List = getValueAsListInit(FieldName); 01840 std::vector<Record*> Defs; 01841 for (unsigned i = 0; i < List->getSize(); i++) { 01842 if (DefInit *DI = dyn_cast<DefInit>(List->getElement(i))) { 01843 Defs.push_back(DI->getDef()); 01844 } else { 01845 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + 01846 FieldName.str() + "' list is not entirely DefInit!"); 01847 } 01848 } 01849 return Defs; 01850 } 01851 01852 /// getValueAsInt - This method looks up the specified field and returns its 01853 /// value as an int64_t, aborting if the field does not exist or if the value 01854 /// is not the right type. 01855 /// 01856 int64_t Record::getValueAsInt(StringRef FieldName) const { 01857 const RecordVal *R = getValue(FieldName); 01858 if (R == 0 || R->getValue() == 0) 01859 PrintFatalError(getLoc(), "Record `" + getName() + 01860 "' does not have a field named `" + FieldName.str() + "'!\n"); 01861 01862 if (IntInit *II = dyn_cast<IntInit>(R->getValue())) 01863 return II->getValue(); 01864 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + 01865 FieldName.str() + "' does not have an int initializer!"); 01866 } 01867 01868 /// getValueAsListOfInts - This method looks up the specified field and returns 01869 /// its value as a vector of integers, aborting if the field does not exist or 01870 /// if the value is not the right type. 01871 /// 01872 std::vector<int64_t> 01873 Record::getValueAsListOfInts(StringRef FieldName) const { 01874 ListInit *List = getValueAsListInit(FieldName); 01875 std::vector<int64_t> Ints; 01876 for (unsigned i = 0; i < List->getSize(); i++) { 01877 if (IntInit *II = dyn_cast<IntInit>(List->getElement(i))) { 01878 Ints.push_back(II->getValue()); 01879 } else { 01880 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + 01881 FieldName.str() + "' does not have a list of ints initializer!"); 01882 } 01883 } 01884 return Ints; 01885 } 01886 01887 /// getValueAsListOfStrings - This method looks up the specified field and 01888 /// returns its value as a vector of strings, aborting if the field does not 01889 /// exist or if the value is not the right type. 01890 /// 01891 std::vector<std::string> 01892 Record::getValueAsListOfStrings(StringRef FieldName) const { 01893 ListInit *List = getValueAsListInit(FieldName); 01894 std::vector<std::string> Strings; 01895 for (unsigned i = 0; i < List->getSize(); i++) { 01896 if (StringInit *II = dyn_cast<StringInit>(List->getElement(i))) { 01897 Strings.push_back(II->getValue()); 01898 } else { 01899 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + 01900 FieldName.str() + "' does not have a list of strings initializer!"); 01901 } 01902 } 01903 return Strings; 01904 } 01905 01906 /// getValueAsDef - This method looks up the specified field and returns its 01907 /// value as a Record, aborting if the field does not exist or if the value 01908 /// is not the right type. 01909 /// 01910 Record *Record::getValueAsDef(StringRef FieldName) const { 01911 const RecordVal *R = getValue(FieldName); 01912 if (R == 0 || R->getValue() == 0) 01913 PrintFatalError(getLoc(), "Record `" + getName() + 01914 "' does not have a field named `" + FieldName.str() + "'!\n"); 01915 01916 if (DefInit *DI = dyn_cast<DefInit>(R->getValue())) 01917 return DI->getDef(); 01918 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + 01919 FieldName.str() + "' does not have a def initializer!"); 01920 } 01921 01922 /// getValueAsBit - This method looks up the specified field and returns its 01923 /// value as a bit, aborting if the field does not exist or if the value is 01924 /// not the right type. 01925 /// 01926 bool Record::getValueAsBit(StringRef FieldName) const { 01927 const RecordVal *R = getValue(FieldName); 01928 if (R == 0 || R->getValue() == 0) 01929 PrintFatalError(getLoc(), "Record `" + getName() + 01930 "' does not have a field named `" + FieldName.str() + "'!\n"); 01931 01932 if (BitInit *BI = dyn_cast<BitInit>(R->getValue())) 01933 return BI->getValue(); 01934 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + 01935 FieldName.str() + "' does not have a bit initializer!"); 01936 } 01937 01938 bool Record::getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const { 01939 const RecordVal *R = getValue(FieldName); 01940 if (R == 0 || R->getValue() == 0) 01941 PrintFatalError(getLoc(), "Record `" + getName() + 01942 "' does not have a field named `" + FieldName.str() + "'!\n"); 01943 01944 if (R->getValue() == UnsetInit::get()) { 01945 Unset = true; 01946 return false; 01947 } 01948 Unset = false; 01949 if (BitInit *BI = dyn_cast<BitInit>(R->getValue())) 01950 return BI->getValue(); 01951 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + 01952 FieldName.str() + "' does not have a bit initializer!"); 01953 } 01954 01955 /// getValueAsDag - This method looks up the specified field and returns its 01956 /// value as an Dag, aborting if the field does not exist or if the value is 01957 /// not the right type. 01958 /// 01959 DagInit *Record::getValueAsDag(StringRef FieldName) const { 01960 const RecordVal *R = getValue(FieldName); 01961 if (R == 0 || R->getValue() == 0) 01962 PrintFatalError(getLoc(), "Record `" + getName() + 01963 "' does not have a field named `" + FieldName.str() + "'!\n"); 01964 01965 if (DagInit *DI = dyn_cast<DagInit>(R->getValue())) 01966 return DI; 01967 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + 01968 FieldName.str() + "' does not have a dag initializer!"); 01969 } 01970 01971 01972 void MultiClass::dump() const { 01973 errs() << "Record:\n"; 01974 Rec.dump(); 01975 01976 errs() << "Defs:\n"; 01977 for (RecordVector::const_iterator r = DefPrototypes.begin(), 01978 rend = DefPrototypes.end(); 01979 r != rend; 01980 ++r) { 01981 (*r)->dump(); 01982 } 01983 } 01984 01985 01986 void RecordKeeper::dump() const { errs() << *this; } 01987 01988 raw_ostream &llvm::operator<<(raw_ostream &OS, const RecordKeeper &RK) { 01989 OS << "------------- Classes -----------------\n"; 01990 const std::map<std::string, Record*> &Classes = RK.getClasses(); 01991 for (std::map<std::string, Record*>::const_iterator I = Classes.begin(), 01992 E = Classes.end(); I != E; ++I) 01993 OS << "class " << *I->second; 01994 01995 OS << "------------- Defs -----------------\n"; 01996 const std::map<std::string, Record*> &Defs = RK.getDefs(); 01997 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(), 01998 E = Defs.end(); I != E; ++I) 01999 OS << "def " << *I->second; 02000 return OS; 02001 } 02002 02003 02004 /// getAllDerivedDefinitions - This method returns all concrete definitions 02005 /// that derive from the specified class name. If a class with the specified 02006 /// name does not exist, an error is printed and true is returned. 02007 std::vector<Record*> 02008 RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const { 02009 Record *Class = getClass(ClassName); 02010 if (!Class) 02011 PrintFatalError("ERROR: Couldn't find the `" + ClassName + "' class!\n"); 02012 02013 std::vector<Record*> Defs; 02014 for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(), 02015 E = getDefs().end(); I != E; ++I) 02016 if (I->second->isSubClassOf(Class)) 02017 Defs.push_back(I->second); 02018 02019 return Defs; 02020 } 02021 02022 /// QualifyName - Return an Init with a qualifier prefix referring 02023 /// to CurRec's name. 02024 Init *llvm::QualifyName(Record &CurRec, MultiClass *CurMultiClass, 02025 Init *Name, const std::string &Scoper) { 02026 RecTy *Type = dyn_cast<TypedInit>(Name)->getType(); 02027 02028 BinOpInit *NewName = 02029 BinOpInit::get(BinOpInit::STRCONCAT, 02030 BinOpInit::get(BinOpInit::STRCONCAT, 02031 CurRec.getNameInit(), 02032 StringInit::get(Scoper), 02033 Type)->Fold(&CurRec, CurMultiClass), 02034 Name, 02035 Type); 02036 02037 if (CurMultiClass && Scoper != "::") { 02038 NewName = 02039 BinOpInit::get(BinOpInit::STRCONCAT, 02040 BinOpInit::get(BinOpInit::STRCONCAT, 02041 CurMultiClass->Rec.getNameInit(), 02042 StringInit::get("::"), 02043 Type)->Fold(&CurRec, CurMultiClass), 02044 NewName->Fold(&CurRec, CurMultiClass), 02045 Type); 02046 } 02047 02048 return NewName->Fold(&CurRec, CurMultiClass); 02049 } 02050 02051 /// QualifyName - Return an Init with a qualifier prefix referring 02052 /// to CurRec's name. 02053 Init *llvm::QualifyName(Record &CurRec, MultiClass *CurMultiClass, 02054 const std::string &Name, 02055 const std::string &Scoper) { 02056 return QualifyName(CurRec, CurMultiClass, StringInit::get(Name), Scoper); 02057 }