2 //===- Record.cpp - Record implementation ---------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 // Implement the tablegen record classes.
13 //===----------------------------------------------------------------------===//
17 #include "llvm/Support/DataTypes.h"
18 #include "llvm/Support/Format.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/FoldingSet.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include "llvm/ADT/StringExtras.h"
24 #include "llvm/ADT/StringMap.h"
28 //===----------------------------------------------------------------------===//
29 // std::string wrapper for DenseMap purposes
30 //===----------------------------------------------------------------------===//
32 /// TableGenStringKey - This is a wrapper for std::string suitable for
33 /// using as a key to a DenseMap. Because there isn't a particularly
34 /// good way to indicate tombstone or empty keys for strings, we want
35 /// to wrap std::string to indicate that this is a "special" string
36 /// not expected to take on certain values (those of the tombstone and
37 /// empty keys). This makes things a little safer as it clarifies
38 /// that DenseMap is really not appropriate for general strings.
40 class TableGenStringKey {
42 TableGenStringKey(const std::string &str) : data(str) {}
43 TableGenStringKey(const char *str) : data(str) {}
45 const std::string &str() const { return data; }
51 /// Specialize DenseMapInfo for TableGenStringKey.
54 template<> struct DenseMapInfo<TableGenStringKey> {
55 static inline TableGenStringKey getEmptyKey() {
56 TableGenStringKey Empty("<<<EMPTY KEY>>>");
59 static inline TableGenStringKey getTombstoneKey() {
60 TableGenStringKey Tombstone("<<<TOMBSTONE KEY>>>");
63 static unsigned getHashValue(const TableGenStringKey& Val) {
64 return HashString(Val.str());
66 static bool isEqual(const TableGenStringKey& LHS,
67 const TableGenStringKey& RHS) {
68 return LHS.str() == RHS.str();
74 //===----------------------------------------------------------------------===//
75 // Type implementations
76 //===----------------------------------------------------------------------===//
78 BitRecTy BitRecTy::Shared;
79 IntRecTy IntRecTy::Shared;
80 StringRecTy StringRecTy::Shared;
81 CodeRecTy CodeRecTy::Shared;
82 DagRecTy DagRecTy::Shared;
84 void RecTy::dump() const { print(errs()); }
86 ListRecTy *RecTy::getListTy() {
88 ListTy = new ListRecTy(this);
92 const Init *BitRecTy::convertValue(const BitsInit *BI) {
93 if (BI->getNumBits() != 1) return 0; // Only accept if just one bit!
97 bool BitRecTy::baseClassOf(const BitsRecTy *RHS) const {
98 return RHS->getNumBits() == 1;
101 const Init *BitRecTy::convertValue(const IntInit *II) {
102 int64_t Val = II->getValue();
103 if (Val != 0 && Val != 1) return 0; // Only accept 0 or 1 for a bit!
105 return BitInit::get(Val != 0);
108 const Init *BitRecTy::convertValue(const TypedInit *VI) {
109 if (dynamic_cast<BitRecTy*>(VI->getType()))
110 return VI; // Accept variable if it is already of bit type!
114 BitsRecTy *BitsRecTy::get(unsigned Sz) {
115 static std::vector<BitsRecTy*> Shared;
116 if (Sz >= Shared.size())
117 Shared.resize(Sz + 1);
118 BitsRecTy *&Ty = Shared[Sz];
120 Ty = new BitsRecTy(Sz);
124 std::string BitsRecTy::getAsString() const {
125 return "bits<" + utostr(Size) + ">";
128 const Init *BitsRecTy::convertValue(const UnsetInit *UI) {
129 SmallVector<const Init *, 16> NewBits(Size);
131 for (unsigned i = 0; i != Size; ++i)
132 NewBits[i] = UnsetInit::get();
134 return BitsInit::get(NewBits);
137 const Init *BitsRecTy::convertValue(const BitInit *UI) {
138 if (Size != 1) return 0; // Can only convert single bit.
139 return BitsInit::get(UI);
142 /// canFitInBitfield - Return true if the number of bits is large enough to hold
143 /// the integer value.
144 static bool canFitInBitfield(int64_t Value, unsigned NumBits) {
145 // For example, with NumBits == 4, we permit Values from [-7 .. 15].
146 return (NumBits >= sizeof(Value) * 8) ||
147 (Value >> NumBits == 0) || (Value >> (NumBits-1) == -1);
150 /// convertValue from Int initializer to bits type: Split the integer up into the
151 /// appropriate bits.
153 const Init *BitsRecTy::convertValue(const IntInit *II) {
154 int64_t Value = II->getValue();
155 // Make sure this bitfield is large enough to hold the integer value.
156 if (!canFitInBitfield(Value, Size))
159 SmallVector<const Init *, 16> NewBits(Size);
161 for (unsigned i = 0; i != Size; ++i)
162 NewBits[i] = BitInit::get(Value & (1LL << i));
164 return BitsInit::get(NewBits);
167 const Init *BitsRecTy::convertValue(const BitsInit *BI) {
168 // If the number of bits is right, return it. Otherwise we need to expand or
170 if (BI->getNumBits() == Size) return BI;
174 const Init *BitsRecTy::convertValue(const TypedInit *VI) {
175 if (BitsRecTy *BRT = dynamic_cast<BitsRecTy*>(VI->getType()))
176 if (BRT->Size == Size) {
177 SmallVector<const Init *, 16> NewBits(Size);
179 for (unsigned i = 0; i != Size; ++i)
180 NewBits[i] = VarBitInit::get(VI, i);
181 return BitsInit::get(NewBits);
184 if (Size == 1 && dynamic_cast<BitRecTy*>(VI->getType()))
185 return BitsInit::get(VI);
187 if (const TernOpInit *Tern = dynamic_cast<const TernOpInit*>(VI)) {
188 if (Tern->getOpcode() == TernOpInit::IF) {
189 const Init *LHS = Tern->getLHS();
190 const Init *MHS = Tern->getMHS();
191 const Init *RHS = Tern->getRHS();
193 const IntInit *MHSi = dynamic_cast<const IntInit*>(MHS);
194 const IntInit *RHSi = dynamic_cast<const IntInit*>(RHS);
197 int64_t MHSVal = MHSi->getValue();
198 int64_t RHSVal = RHSi->getValue();
200 if (canFitInBitfield(MHSVal, Size) && canFitInBitfield(RHSVal, Size)) {
201 SmallVector<const Init *, 16> NewBits(Size);
203 for (unsigned i = 0; i != Size; ++i)
205 TernOpInit::get(TernOpInit::IF, LHS,
206 IntInit::get((MHSVal & (1LL << i)) ? 1 : 0),
207 IntInit::get((RHSVal & (1LL << i)) ? 1 : 0),
210 return BitsInit::get(NewBits);
213 const BitsInit *MHSbs = dynamic_cast<const BitsInit*>(MHS);
214 const BitsInit *RHSbs = dynamic_cast<const BitsInit*>(RHS);
216 if (MHSbs && RHSbs) {
217 SmallVector<const Init *, 16> NewBits(Size);
219 for (unsigned i = 0; i != Size; ++i)
220 NewBits[i] = TernOpInit::get(TernOpInit::IF, LHS,
225 return BitsInit::get(NewBits);
234 const Init *IntRecTy::convertValue(const BitInit *BI) {
235 return IntInit::get(BI->getValue());
238 const Init *IntRecTy::convertValue(const BitsInit *BI) {
240 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
241 if (const BitInit *Bit = dynamic_cast<const BitInit*>(BI->getBit(i))) {
242 Result |= Bit->getValue() << i;
246 return IntInit::get(Result);
249 const Init *IntRecTy::convertValue(const TypedInit *TI) {
250 if (TI->getType()->typeIsConvertibleTo(this))
251 return TI; // Accept variable if already of the right type!
255 const Init *StringRecTy::convertValue(const UnOpInit *BO) {
256 if (BO->getOpcode() == UnOpInit::CAST) {
257 const Init *L = BO->getOperand()->convertInitializerTo(this);
258 if (L == 0) return 0;
259 if (L != BO->getOperand())
260 return UnOpInit::get(UnOpInit::CAST, L, new StringRecTy);
264 return convertValue((const TypedInit*)BO);
267 const Init *StringRecTy::convertValue(const BinOpInit *BO) {
268 if (BO->getOpcode() == BinOpInit::STRCONCAT) {
269 const Init *L = BO->getLHS()->convertInitializerTo(this);
270 const Init *R = BO->getRHS()->convertInitializerTo(this);
271 if (L == 0 || R == 0) return 0;
272 if (L != BO->getLHS() || R != BO->getRHS())
273 return BinOpInit::get(BinOpInit::STRCONCAT, L, R, new StringRecTy);
277 return convertValue((const TypedInit*)BO);
281 const Init *StringRecTy::convertValue(const TypedInit *TI) {
282 if (dynamic_cast<StringRecTy*>(TI->getType()))
283 return TI; // Accept variable if already of the right type!
287 std::string ListRecTy::getAsString() const {
288 return "list<" + Ty->getAsString() + ">";
291 const Init *ListRecTy::convertValue(const ListInit *LI) {
292 std::vector<const Init*> Elements;
294 // Verify that all of the elements of the list are subclasses of the
295 // appropriate class!
296 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
297 if (const Init *CI = LI->getElement(i)->convertInitializerTo(Ty))
298 Elements.push_back(CI);
302 ListRecTy *LType = dynamic_cast<ListRecTy*>(LI->getType());
307 return ListInit::get(Elements, this);
310 const Init *ListRecTy::convertValue(const TypedInit *TI) {
311 // Ensure that TI is compatible with our class.
312 if (ListRecTy *LRT = dynamic_cast<ListRecTy*>(TI->getType()))
313 if (LRT->getElementType()->typeIsConvertibleTo(getElementType()))
318 const Init *CodeRecTy::convertValue(const TypedInit *TI) {
319 if (TI->getType()->typeIsConvertibleTo(this))
324 const Init *DagRecTy::convertValue(const TypedInit *TI) {
325 if (TI->getType()->typeIsConvertibleTo(this))
330 const Init *DagRecTy::convertValue(const UnOpInit *BO) {
331 if (BO->getOpcode() == UnOpInit::CAST) {
332 const Init *L = BO->getOperand()->convertInitializerTo(this);
333 if (L == 0) return 0;
334 if (L != BO->getOperand())
335 return UnOpInit::get(UnOpInit::CAST, L, new DagRecTy);
341 const Init *DagRecTy::convertValue(const BinOpInit *BO) {
342 if (BO->getOpcode() == BinOpInit::CONCAT) {
343 const Init *L = BO->getLHS()->convertInitializerTo(this);
344 const Init *R = BO->getRHS()->convertInitializerTo(this);
345 if (L == 0 || R == 0) return 0;
346 if (L != BO->getLHS() || R != BO->getRHS())
347 return BinOpInit::get(BinOpInit::CONCAT, L, R, new DagRecTy);
353 RecordRecTy *RecordRecTy::get(Record *R) {
354 return &dynamic_cast<RecordRecTy&>(*R->getDefInit()->getType());
357 std::string RecordRecTy::getAsString() const {
358 return Rec->getName();
361 const Init *RecordRecTy::convertValue(const DefInit *DI) {
362 // Ensure that DI is a subclass of Rec.
363 if (!DI->getDef()->isSubClassOf(Rec))
368 const Init *RecordRecTy::convertValue(const TypedInit *TI) {
369 // Ensure that TI is compatible with Rec.
370 if (RecordRecTy *RRT = dynamic_cast<RecordRecTy*>(TI->getType()))
371 if (RRT->getRecord()->isSubClassOf(getRecord()) ||
372 RRT->getRecord() == getRecord())
377 bool RecordRecTy::baseClassOf(const RecordRecTy *RHS) const {
378 if (Rec == RHS->getRecord() || RHS->getRecord()->isSubClassOf(Rec))
381 const std::vector<Record*> &SC = Rec->getSuperClasses();
382 for (unsigned i = 0, e = SC.size(); i != e; ++i)
383 if (RHS->getRecord()->isSubClassOf(SC[i]))
390 /// resolveTypes - Find a common type that T1 and T2 convert to.
391 /// Return 0 if no such type exists.
393 RecTy *llvm::resolveTypes(RecTy *T1, RecTy *T2) {
394 if (!T1->typeIsConvertibleTo(T2)) {
395 if (!T2->typeIsConvertibleTo(T1)) {
396 // If one is a Record type, check superclasses
397 RecordRecTy *RecTy1 = dynamic_cast<RecordRecTy*>(T1);
399 // See if T2 inherits from a type T1 also inherits from
400 const std::vector<Record *> &T1SuperClasses =
401 RecTy1->getRecord()->getSuperClasses();
402 for(std::vector<Record *>::const_iterator i = T1SuperClasses.begin(),
403 iend = T1SuperClasses.end();
406 RecordRecTy *SuperRecTy1 = RecordRecTy::get(*i);
407 RecTy *NewType1 = resolveTypes(SuperRecTy1, T2);
409 if (NewType1 != SuperRecTy1) {
416 RecordRecTy *RecTy2 = dynamic_cast<RecordRecTy*>(T2);
418 // See if T1 inherits from a type T2 also inherits from
419 const std::vector<Record *> &T2SuperClasses =
420 RecTy2->getRecord()->getSuperClasses();
421 for (std::vector<Record *>::const_iterator i = T2SuperClasses.begin(),
422 iend = T2SuperClasses.end();
425 RecordRecTy *SuperRecTy2 = RecordRecTy::get(*i);
426 RecTy *NewType2 = resolveTypes(T1, SuperRecTy2);
428 if (NewType2 != SuperRecTy2) {
443 //===----------------------------------------------------------------------===//
444 // Initializer implementations
445 //===----------------------------------------------------------------------===//
447 void Init::dump() const { return print(errs()); }
449 const UnsetInit *UnsetInit::get() {
450 static const UnsetInit TheInit;
454 const BitInit *BitInit::get(bool V) {
455 static const BitInit True(true);
456 static const BitInit False(false);
458 return V ? &True : &False;
462 ProfileBitsInit(FoldingSetNodeID &ID, ArrayRef<const Init *> Range) {
463 ID.AddInteger(Range.size());
465 for (ArrayRef<const Init *>::iterator i = Range.begin(),
472 const BitsInit *BitsInit::get(ArrayRef<const Init *> Range) {
473 typedef FoldingSet<BitsInit> Pool;
477 ProfileBitsInit(ID, Range);
480 if (const BitsInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
483 BitsInit *I = new BitsInit(Range);
484 ThePool.InsertNode(I, IP);
489 void BitsInit::Profile(FoldingSetNodeID &ID) const {
490 ProfileBitsInit(ID, Bits);
494 BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) const {
495 SmallVector<const Init *, 16> NewBits(Bits.size());
497 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
498 if (Bits[i] >= getNumBits())
500 NewBits[i] = getBit(Bits[i]);
502 return BitsInit::get(NewBits);
505 std::string BitsInit::getAsString() const {
506 std::string Result = "{ ";
507 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
508 if (i) Result += ", ";
509 if (const Init *Bit = getBit(e-i-1))
510 Result += Bit->getAsString();
514 return Result + " }";
517 // resolveReferences - If there are any field references that refer to fields
518 // that have been filled in, we can propagate the values now.
520 const Init *BitsInit::resolveReferences(Record &R, const RecordVal *RV) const {
521 bool Changed = false;
522 SmallVector<const Init *, 16> NewBits(getNumBits());
524 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
526 const Init *CurBit = getBit(i);
530 CurBit = CurBit->resolveReferences(R, RV);
531 Changed |= B != CurBit;
532 } while (B != CurBit);
537 return BitsInit::get(NewBits);
542 const IntInit *IntInit::get(int64_t V) {
543 typedef DenseMap<int64_t, IntInit *> Pool;
546 IntInit *&I = ThePool[V];
547 if (!I) I = new IntInit(V);
551 std::string IntInit::getAsString() const {
552 return itostr(Value);
556 IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) const {
557 SmallVector<const Init *, 16> NewBits(Bits.size());
559 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
563 NewBits[i] = BitInit::get(Value & (INT64_C(1) << Bits[i]));
565 return BitsInit::get(NewBits);
568 const StringInit *StringInit::get(const std::string &V) {
569 typedef StringMap<StringInit *> Pool;
572 StringInit *&I = ThePool[V];
573 if (!I) I = new StringInit(V);
577 const CodeInit *CodeInit::get(const std::string &V) {
578 typedef StringMap<CodeInit *> Pool;
581 CodeInit *&I = ThePool[V];
582 if (!I) I = new CodeInit(V);
586 static void ProfileListInit(FoldingSetNodeID &ID,
587 ArrayRef<const Init *> Range,
589 ID.AddInteger(Range.size());
590 ID.AddPointer(EltTy);
592 for (ArrayRef<const Init *>::iterator i = Range.begin(),
599 const ListInit *ListInit::get(ArrayRef<const Init *> Range, RecTy *EltTy) {
600 typedef FoldingSet<ListInit> Pool;
603 // Just use the FoldingSetNodeID to compute a hash. Use a DenseMap
604 // for actual storage.
606 ProfileListInit(ID, Range, EltTy);
609 if (const ListInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
612 ListInit *I = new ListInit(Range, EltTy);
613 ThePool.InsertNode(I, IP);
617 void ListInit::Profile(FoldingSetNodeID &ID) const {
618 ListRecTy *ListType = dynamic_cast<ListRecTy *>(getType());
619 assert(ListType && "Bad type for ListInit!");
620 RecTy *EltTy = ListType->getElementType();
622 ProfileListInit(ID, Values, EltTy);
626 ListInit::convertInitListSlice(const std::vector<unsigned> &Elements) const {
627 std::vector<const Init*> Vals;
628 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
629 if (Elements[i] >= getSize())
631 Vals.push_back(getElement(Elements[i]));
633 return ListInit::get(Vals, getType());
636 Record *ListInit::getElementAsRecord(unsigned i) const {
637 assert(i < Values.size() && "List element index out of range!");
638 const DefInit *DI = dynamic_cast<const DefInit*>(Values[i]);
639 if (DI == 0) throw "Expected record in list!";
643 const Init *ListInit::resolveReferences(Record &R, const RecordVal *RV) const {
644 std::vector<const Init*> Resolved;
645 Resolved.reserve(getSize());
646 bool Changed = false;
648 for (unsigned i = 0, e = getSize(); i != e; ++i) {
650 const Init *CurElt = getElement(i);
654 CurElt = CurElt->resolveReferences(R, RV);
655 Changed |= E != CurElt;
656 } while (E != CurElt);
657 Resolved.push_back(E);
661 return ListInit::get(Resolved, getType());
665 const Init *ListInit::resolveListElementReference(Record &R,
666 const RecordVal *IRV,
667 unsigned Elt) const {
668 if (Elt >= getSize())
669 return 0; // Out of range reference.
670 const Init *E = getElement(Elt);
671 // If the element is set to some value, or if we are resolving a reference
672 // to a specific variable and that variable is explicitly unset, then
673 // replace the VarListElementInit with it.
674 if (IRV || !dynamic_cast<const UnsetInit*>(E))
679 std::string ListInit::getAsString() const {
680 std::string Result = "[";
681 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
682 if (i) Result += ", ";
683 Result += Values[i]->getAsString();
688 const Init *OpInit::resolveBitReference(Record &R, const RecordVal *IRV,
689 unsigned Bit) const {
690 const Init *Folded = Fold(&R, 0);
692 if (Folded != this) {
693 const TypedInit *Typed = dynamic_cast<const TypedInit *>(Folded);
695 return Typed->resolveBitReference(R, IRV, Bit);
702 const Init *OpInit::resolveListElementReference(Record &R, const RecordVal *IRV,
703 unsigned Elt) const {
704 const Init *Folded = Fold(&R, 0);
706 if (Folded != this) {
707 const TypedInit *Typed = dynamic_cast<const TypedInit *>(Folded);
709 return Typed->resolveListElementReference(R, IRV, Elt);
716 const UnOpInit *UnOpInit::get(UnaryOp opc, const Init *lhs, RecTy *Type) {
717 typedef std::pair<std::pair<unsigned, const Init *>, RecTy *> Key;
719 typedef DenseMap<Key, UnOpInit *> Pool;
722 Key TheKey(std::make_pair(std::make_pair(opc, lhs), Type));
724 UnOpInit *&I = ThePool[TheKey];
725 if (!I) I = new UnOpInit(opc, lhs, Type);
729 const Init *UnOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const {
730 switch (getOpcode()) {
731 default: assert(0 && "Unknown unop");
733 if (getType()->getAsString() == "string") {
734 const StringInit *LHSs = dynamic_cast<const StringInit*>(LHS);
739 const DefInit *LHSd = dynamic_cast<const DefInit*>(LHS);
741 return StringInit::get(LHSd->getDef()->getName());
744 const StringInit *LHSs = dynamic_cast<const StringInit*>(LHS);
746 std::string Name = LHSs->getValue();
748 // From TGParser::ParseIDValue
750 if (const RecordVal *RV = CurRec->getValue(Name)) {
751 if (RV->getType() != getType())
752 throw "type mismatch in cast";
753 return VarInit::get(Name, RV->getType());
756 std::string TemplateArgName = CurRec->getName()+":"+Name;
757 if (CurRec->isTemplateArg(TemplateArgName)) {
758 const RecordVal *RV = CurRec->getValue(TemplateArgName);
759 assert(RV && "Template arg doesn't exist??");
761 if (RV->getType() != getType())
762 throw "type mismatch in cast";
764 return VarInit::get(TemplateArgName, RV->getType());
769 std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
770 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
771 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
772 assert(RV && "Template arg doesn't exist??");
774 if (RV->getType() != getType())
775 throw "type mismatch in cast";
777 return VarInit::get(MCName, RV->getType());
781 if (Record *D = (CurRec->getRecords()).getDef(Name))
782 return DefInit::get(D);
784 throw TGError(CurRec->getLoc(), "Undefined reference:'" + Name + "'\n");
790 const ListInit *LHSl = dynamic_cast<const ListInit*>(LHS);
792 if (LHSl->getSize() == 0) {
793 assert(0 && "Empty list in car");
796 return LHSl->getElement(0);
801 const ListInit *LHSl = dynamic_cast<const ListInit*>(LHS);
803 if (LHSl->getSize() == 0) {
804 assert(0 && "Empty list in cdr");
807 // Note the +1. We can't just pass the result of getValues()
809 ArrayRef<const Init *>::iterator begin = LHSl->getValues().begin()+1;
810 ArrayRef<const Init *>::iterator end = LHSl->getValues().end();
811 const ListInit *Result =
812 ListInit::get(ArrayRef<const Init *>(begin, end - begin),
819 const ListInit *LHSl = dynamic_cast<const ListInit*>(LHS);
821 if (LHSl->getSize() == 0) {
822 return IntInit::get(1);
824 return IntInit::get(0);
827 const StringInit *LHSs = dynamic_cast<const StringInit*>(LHS);
829 if (LHSs->getValue().empty()) {
830 return IntInit::get(1);
832 return IntInit::get(0);
842 const Init *UnOpInit::resolveReferences(Record &R, const RecordVal *RV) const {
843 const Init *lhs = LHS->resolveReferences(R, RV);
846 return (UnOpInit::get(getOpcode(), lhs, getType()))->Fold(&R, 0);
850 std::string UnOpInit::getAsString() const {
853 case CAST: Result = "!cast<" + getType()->getAsString() + ">"; break;
854 case HEAD: Result = "!head"; break;
855 case TAIL: Result = "!tail"; break;
856 case EMPTY: Result = "!empty"; break;
858 return Result + "(" + LHS->getAsString() + ")";
861 const BinOpInit *BinOpInit::get(BinaryOp opc, const Init *lhs,
862 const Init *rhs, RecTy *Type) {
864 std::pair<std::pair<unsigned, const Init *>, const Init *>,
868 typedef DenseMap<Key, BinOpInit *> Pool;
871 Key TheKey(std::make_pair(std::make_pair(std::make_pair(opc, lhs), rhs),
874 BinOpInit *&I = ThePool[TheKey];
875 if (!I) I = new BinOpInit(opc, lhs, rhs, Type);
879 const Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const {
880 switch (getOpcode()) {
881 default: assert(0 && "Unknown binop");
883 const DagInit *LHSs = dynamic_cast<const DagInit*>(LHS);
884 const DagInit *RHSs = dynamic_cast<const DagInit*>(RHS);
886 const DefInit *LOp = dynamic_cast<const DefInit*>(LHSs->getOperator());
887 const DefInit *ROp = dynamic_cast<const DefInit*>(RHSs->getOperator());
888 if (LOp == 0 || ROp == 0 || LOp->getDef() != ROp->getDef())
889 throw "Concated Dag operators do not match!";
890 std::vector<const Init*> Args;
891 std::vector<std::string> ArgNames;
892 for (unsigned i = 0, e = LHSs->getNumArgs(); i != e; ++i) {
893 Args.push_back(LHSs->getArg(i));
894 ArgNames.push_back(LHSs->getArgName(i));
896 for (unsigned i = 0, e = RHSs->getNumArgs(); i != e; ++i) {
897 Args.push_back(RHSs->getArg(i));
898 ArgNames.push_back(RHSs->getArgName(i));
900 return DagInit::get(LHSs->getOperator(), "", Args, ArgNames);
905 const StringInit *LHSs = dynamic_cast<const StringInit*>(LHS);
906 const StringInit *RHSs = dynamic_cast<const StringInit*>(RHS);
908 return StringInit::get(LHSs->getValue() + RHSs->getValue());
912 // try to fold eq comparison for 'bit' and 'int', otherwise fallback
913 // to string objects.
915 dynamic_cast<const IntInit*>(LHS->convertInitializerTo(IntRecTy::get()));
917 dynamic_cast<const IntInit*>(RHS->convertInitializerTo(IntRecTy::get()));
920 return IntInit::get(L->getValue() == R->getValue());
922 const StringInit *LHSs = dynamic_cast<const StringInit*>(LHS);
923 const StringInit *RHSs = dynamic_cast<const StringInit*>(RHS);
925 // Make sure we've resolved
927 return IntInit::get(LHSs->getValue() == RHSs->getValue());
934 const IntInit *LHSi = dynamic_cast<const IntInit*>(LHS);
935 const IntInit *RHSi = dynamic_cast<const IntInit*>(RHS);
937 int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
939 switch (getOpcode()) {
940 default: assert(0 && "Bad opcode!");
941 case SHL: Result = LHSv << RHSv; break;
942 case SRA: Result = LHSv >> RHSv; break;
943 case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break;
945 return IntInit::get(Result);
953 const Init *BinOpInit::resolveReferences(Record &R, const RecordVal *RV) const {
954 const Init *lhs = LHS->resolveReferences(R, RV);
955 const Init *rhs = RHS->resolveReferences(R, RV);
957 if (LHS != lhs || RHS != rhs)
958 return (BinOpInit::get(getOpcode(), lhs, rhs, getType()))->Fold(&R, 0);
962 std::string BinOpInit::getAsString() const {
965 case CONCAT: Result = "!con"; break;
966 case SHL: Result = "!shl"; break;
967 case SRA: Result = "!sra"; break;
968 case SRL: Result = "!srl"; break;
969 case EQ: Result = "!eq"; break;
970 case STRCONCAT: Result = "!strconcat"; break;
972 return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")";
975 const TernOpInit *TernOpInit::get(TernaryOp opc, const Init *lhs,
976 const Init *mhs, const Init *rhs,
978 return new TernOpInit(opc, lhs, mhs, rhs, Type);
981 static const Init *ForeachHelper(const Init *LHS, const Init *MHS,
982 const Init *RHS, RecTy *Type,
983 Record *CurRec, MultiClass *CurMultiClass);
985 static const Init *EvaluateOperation(const OpInit *RHSo, const Init *LHS,
987 RecTy *Type, Record *CurRec,
988 MultiClass *CurMultiClass) {
989 std::vector<const Init *> NewOperands;
991 const TypedInit *TArg = dynamic_cast<const TypedInit*>(Arg);
993 // If this is a dag, recurse
994 if (TArg && TArg->getType()->getAsString() == "dag") {
995 const Init *Result = ForeachHelper(LHS, Arg, RHSo, Type,
996 CurRec, CurMultiClass);
1004 for (int i = 0; i < RHSo->getNumOperands(); ++i) {
1005 const OpInit *RHSoo = dynamic_cast<const OpInit*>(RHSo->getOperand(i));
1008 const Init *Result = EvaluateOperation(RHSoo, LHS, Arg,
1009 Type, CurRec, CurMultiClass);
1011 NewOperands.push_back(Result);
1013 NewOperands.push_back(Arg);
1015 } else if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
1016 NewOperands.push_back(Arg);
1018 NewOperands.push_back(RHSo->getOperand(i));
1022 // Now run the operator and use its result as the new leaf
1023 const OpInit *NewOp = RHSo->clone(NewOperands);
1024 const Init *NewVal = NewOp->Fold(CurRec, CurMultiClass);
1025 if (NewVal != NewOp)
1031 static const Init *ForeachHelper(const Init *LHS, const Init *MHS,
1032 const Init *RHS, RecTy *Type,
1034 MultiClass *CurMultiClass) {
1035 const DagInit *MHSd = dynamic_cast<const DagInit*>(MHS);
1036 const ListInit *MHSl = dynamic_cast<const ListInit*>(MHS);
1038 DagRecTy *DagType = dynamic_cast<DagRecTy*>(Type);
1039 ListRecTy *ListType = dynamic_cast<ListRecTy*>(Type);
1041 const OpInit *RHSo = dynamic_cast<const OpInit*>(RHS);
1044 throw TGError(CurRec->getLoc(), "!foreach requires an operator\n");
1047 const TypedInit *LHSt = dynamic_cast<const TypedInit*>(LHS);
1050 throw TGError(CurRec->getLoc(), "!foreach requires typed variable\n");
1053 if ((MHSd && DagType) || (MHSl && ListType)) {
1055 const Init *Val = MHSd->getOperator();
1056 const Init *Result = EvaluateOperation(RHSo, LHS, Val,
1057 Type, CurRec, CurMultiClass);
1062 std::vector<std::pair<const Init *, std::string> > args;
1063 for (unsigned int i = 0; i < MHSd->getNumArgs(); ++i) {
1065 std::string ArgName;
1066 Arg = MHSd->getArg(i);
1067 ArgName = MHSd->getArgName(i);
1070 const Init *Result = EvaluateOperation(RHSo, LHS, Arg, Type,
1071 CurRec, CurMultiClass);
1076 // TODO: Process arg names
1077 args.push_back(std::make_pair(Arg, ArgName));
1080 return DagInit::get(Val, "", args);
1083 std::vector<const Init *> NewOperands;
1084 std::vector<const Init *> NewList(MHSl->begin(), MHSl->end());
1086 for (std::vector<const Init *>::iterator li = NewList.begin(),
1087 liend = NewList.end();
1090 const Init *Item = *li;
1091 NewOperands.clear();
1092 for(int i = 0; i < RHSo->getNumOperands(); ++i) {
1093 // First, replace the foreach variable with the list item
1094 if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
1095 NewOperands.push_back(Item);
1097 NewOperands.push_back(RHSo->getOperand(i));
1101 // Now run the operator and use its result as the new list item
1102 const OpInit *NewOp = RHSo->clone(NewOperands);
1103 const Init *NewItem = NewOp->Fold(CurRec, CurMultiClass);
1104 if (NewItem != NewOp)
1107 return ListInit::get(NewList, MHSl->getType());
1113 const Init *TernOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const {
1114 switch (getOpcode()) {
1115 default: assert(0 && "Unknown binop");
1117 const DefInit *LHSd = dynamic_cast<const DefInit*>(LHS);
1118 const VarInit *LHSv = dynamic_cast<const VarInit*>(LHS);
1119 const StringInit *LHSs = dynamic_cast<const StringInit*>(LHS);
1121 const DefInit *MHSd = dynamic_cast<const DefInit*>(MHS);
1122 const VarInit *MHSv = dynamic_cast<const VarInit*>(MHS);
1123 const StringInit *MHSs = dynamic_cast<const StringInit*>(MHS);
1125 const DefInit *RHSd = dynamic_cast<const DefInit*>(RHS);
1126 const VarInit *RHSv = dynamic_cast<const VarInit*>(RHS);
1127 const StringInit *RHSs = dynamic_cast<const StringInit*>(RHS);
1129 if ((LHSd && MHSd && RHSd)
1130 || (LHSv && MHSv && RHSv)
1131 || (LHSs && MHSs && RHSs)) {
1133 Record *Val = RHSd->getDef();
1134 if (LHSd->getAsString() == RHSd->getAsString()) {
1135 Val = MHSd->getDef();
1137 return DefInit::get(Val);
1140 std::string Val = RHSv->getName();
1141 if (LHSv->getAsString() == RHSv->getAsString()) {
1142 Val = MHSv->getName();
1144 return VarInit::get(Val, getType());
1147 std::string Val = RHSs->getValue();
1149 std::string::size_type found;
1150 std::string::size_type idx = 0;
1152 found = Val.find(LHSs->getValue(), idx);
1153 if (found != std::string::npos) {
1154 Val.replace(found, LHSs->getValue().size(), MHSs->getValue());
1156 idx = found + MHSs->getValue().size();
1157 } while (found != std::string::npos);
1159 return StringInit::get(Val);
1166 const Init *Result = ForeachHelper(LHS, MHS, RHS, getType(),
1167 CurRec, CurMultiClass);
1175 const IntInit *LHSi = dynamic_cast<const IntInit*>(LHS);
1176 if (const Init *I = LHS->convertInitializerTo(IntRecTy::get()))
1177 LHSi = dynamic_cast<const IntInit*>(I);
1179 if (LHSi->getValue()) {
1192 const Init *TernOpInit::resolveReferences(Record &R,
1193 const RecordVal *RV) const {
1194 const Init *lhs = LHS->resolveReferences(R, RV);
1196 if (Opc == IF && lhs != LHS) {
1197 const IntInit *Value = dynamic_cast<const IntInit*>(lhs);
1198 if (const Init *I = lhs->convertInitializerTo(IntRecTy::get()))
1199 Value = dynamic_cast<const IntInit*>(I);
1202 if (Value->getValue()) {
1203 const Init *mhs = MHS->resolveReferences(R, RV);
1204 return (TernOpInit::get(getOpcode(), lhs, mhs,
1205 RHS, getType()))->Fold(&R, 0);
1207 const Init *rhs = RHS->resolveReferences(R, RV);
1208 return (TernOpInit::get(getOpcode(), lhs, MHS,
1209 rhs, getType()))->Fold(&R, 0);
1214 const Init *mhs = MHS->resolveReferences(R, RV);
1215 const Init *rhs = RHS->resolveReferences(R, RV);
1217 if (LHS != lhs || MHS != mhs || RHS != rhs)
1218 return (TernOpInit::get(getOpcode(), lhs, mhs, rhs,
1219 getType()))->Fold(&R, 0);
1223 std::string TernOpInit::getAsString() const {
1226 case SUBST: Result = "!subst"; break;
1227 case FOREACH: Result = "!foreach"; break;
1228 case IF: Result = "!if"; break;
1230 return Result + "(" + LHS->getAsString() + ", " + MHS->getAsString() + ", "
1231 + RHS->getAsString() + ")";
1234 RecTy *TypedInit::getFieldType(const std::string &FieldName) const {
1235 RecordRecTy *RecordType = dynamic_cast<RecordRecTy *>(getType());
1237 RecordVal *Field = RecordType->getRecord()->getValue(FieldName);
1239 return Field->getType();
1246 TypedInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) const {
1247 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
1248 if (T == 0) return 0; // Cannot subscript a non-bits variable.
1249 unsigned NumBits = T->getNumBits();
1251 SmallVector<const Init *, 16> NewBits(Bits.size());
1252 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
1253 if (Bits[i] >= NumBits)
1256 NewBits[i] = VarBitInit::get(this, Bits[i]);
1258 return BitsInit::get(NewBits);
1262 TypedInit::convertInitListSlice(const std::vector<unsigned> &Elements) const {
1263 ListRecTy *T = dynamic_cast<ListRecTy*>(getType());
1264 if (T == 0) return 0; // Cannot subscript a non-list variable.
1266 if (Elements.size() == 1)
1267 return VarListElementInit::get(this, Elements[0]);
1269 std::vector<const Init*> ListInits;
1270 ListInits.reserve(Elements.size());
1271 for (unsigned i = 0, e = Elements.size(); i != e; ++i)
1272 ListInits.push_back(VarListElementInit::get(this, Elements[i]));
1273 return ListInit::get(ListInits, T);
1277 const VarInit *VarInit::get(const std::string &VN, RecTy *T) {
1278 return new VarInit(VN, T);
1281 const Init *VarInit::resolveBitReference(Record &R, const RecordVal *IRV,
1282 unsigned Bit) const {
1283 if (R.isTemplateArg(getName())) return 0;
1284 if (IRV && IRV->getName() != getName()) return 0;
1286 RecordVal *RV = R.getValue(getName());
1287 assert(RV && "Reference to a non-existent variable?");
1288 assert(dynamic_cast<const BitsInit*>(RV->getValue()));
1289 const BitsInit *BI = (const BitsInit*)RV->getValue();
1291 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
1292 const Init *B = BI->getBit(Bit);
1294 // If the bit is set to some value, or if we are resolving a reference to a
1295 // specific variable and that variable is explicitly unset, then replace the
1296 // VarBitInit with it.
1297 if (IRV || !dynamic_cast<const UnsetInit*>(B))
1302 const Init *VarInit::resolveListElementReference(Record &R,
1303 const RecordVal *IRV,
1304 unsigned Elt) const {
1305 if (R.isTemplateArg(getName())) return 0;
1306 if (IRV && IRV->getName() != getName()) return 0;
1308 RecordVal *RV = R.getValue(getName());
1309 assert(RV && "Reference to a non-existent variable?");
1310 const ListInit *LI = dynamic_cast<const ListInit*>(RV->getValue());
1312 const VarInit *VI = dynamic_cast<const VarInit*>(RV->getValue());
1313 assert(VI && "Invalid list element!");
1314 return VarListElementInit::get(VI, Elt);
1317 if (Elt >= LI->getSize())
1318 return 0; // Out of range reference.
1319 const Init *E = LI->getElement(Elt);
1320 // If the element is set to some value, or if we are resolving a reference
1321 // to a specific variable and that variable is explicitly unset, then
1322 // replace the VarListElementInit with it.
1323 if (IRV || !dynamic_cast<const UnsetInit*>(E))
1329 RecTy *VarInit::getFieldType(const std::string &FieldName) const {
1330 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
1331 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
1332 return RV->getType();
1336 const Init *VarInit::getFieldInit(Record &R, const RecordVal *RV,
1337 const std::string &FieldName) const {
1338 if (dynamic_cast<RecordRecTy*>(getType()))
1339 if (const RecordVal *Val = R.getValue(VarName)) {
1340 if (RV != Val && (RV || dynamic_cast<const UnsetInit*>(Val->getValue())))
1342 const Init *TheInit = Val->getValue();
1343 assert(TheInit != this && "Infinite loop detected!");
1344 if (const Init *I = TheInit->getFieldInit(R, RV, FieldName))
1352 /// resolveReferences - This method is used by classes that refer to other
1353 /// variables which may not be defined at the time the expression is formed.
1354 /// If a value is set for the variable later, this method will be called on
1355 /// users of the value to allow the value to propagate out.
1357 const Init *VarInit::resolveReferences(Record &R, const RecordVal *RV) const {
1358 if (RecordVal *Val = R.getValue(VarName))
1359 if (RV == Val || (RV == 0 && !dynamic_cast<const UnsetInit*>(Val->getValue())))
1360 return Val->getValue();
1364 const VarBitInit *VarBitInit::get(const TypedInit *T, unsigned B) {
1365 return new VarBitInit(T, B);
1368 std::string VarBitInit::getAsString() const {
1369 return TI->getAsString() + "{" + utostr(Bit) + "}";
1372 const Init *VarBitInit::resolveReferences(Record &R,
1373 const RecordVal *RV) const {
1374 if (const Init *I = getVariable()->resolveBitReference(R, RV, getBitNum()))
1379 const VarListElementInit *VarListElementInit::get(const TypedInit *T,
1381 return new VarListElementInit(T, E);
1384 std::string VarListElementInit::getAsString() const {
1385 return TI->getAsString() + "[" + utostr(Element) + "]";
1388 const Init *VarListElementInit::resolveReferences(Record &R,
1389 const RecordVal *RV) const {
1390 if (const Init *I = getVariable()->resolveListElementReference(R, RV,
1396 const Init *VarListElementInit::resolveBitReference(Record &R,
1397 const RecordVal *RV,
1398 unsigned Bit) const {
1399 // FIXME: This should be implemented, to support references like:
1400 // bit B = AA[0]{1};
1404 const Init *VarListElementInit::
1405 resolveListElementReference(Record &R, const RecordVal *RV,
1406 unsigned Elt) const {
1407 // FIXME: This should be implemented, to support references like:
1408 // int B = AA[0][1];
1412 const DefInit *DefInit::get(Record *R) {
1413 return R->getDefInit();
1416 RecTy *DefInit::getFieldType(const std::string &FieldName) const {
1417 if (const RecordVal *RV = Def->getValue(FieldName))
1418 return RV->getType();
1422 const Init *DefInit::getFieldInit(Record &R, const RecordVal *RV,
1423 const std::string &FieldName) const {
1424 return Def->getValue(FieldName)->getValue();
1428 std::string DefInit::getAsString() const {
1429 return Def->getName();
1432 const FieldInit *FieldInit::get(const Init *R, const std::string &FN) {
1433 return new FieldInit(R, FN);
1436 const Init *FieldInit::resolveBitReference(Record &R, const RecordVal *RV,
1437 unsigned Bit) const {
1438 if (const Init *BitsVal = Rec->getFieldInit(R, RV, FieldName))
1439 if (const BitsInit *BI = dynamic_cast<const BitsInit*>(BitsVal)) {
1440 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
1441 const Init *B = BI->getBit(Bit);
1443 if (dynamic_cast<const BitInit*>(B)) // If the bit is set.
1444 return B; // Replace the VarBitInit with it.
1449 const Init *FieldInit::resolveListElementReference(Record &R,
1450 const RecordVal *RV,
1451 unsigned Elt) const {
1452 if (const Init *ListVal = Rec->getFieldInit(R, RV, FieldName))
1453 if (const ListInit *LI = dynamic_cast<const ListInit*>(ListVal)) {
1454 if (Elt >= LI->getSize()) return 0;
1455 const Init *E = LI->getElement(Elt);
1457 // If the element is set to some value, or if we are resolving a
1458 // reference to a specific variable and that variable is explicitly
1459 // unset, then replace the VarListElementInit with it.
1460 if (RV || !dynamic_cast<const UnsetInit*>(E))
1466 const Init *FieldInit::resolveReferences(Record &R, const RecordVal *RV) const {
1467 const Init *NewRec = RV ? Rec->resolveReferences(R, RV) : Rec;
1469 const Init *BitsVal = NewRec->getFieldInit(R, RV, FieldName);
1471 const Init *BVR = BitsVal->resolveReferences(R, RV);
1472 return BVR->isComplete() ? BVR : this;
1475 if (NewRec != Rec) {
1476 return FieldInit::get(NewRec, FieldName);
1482 DagInit::get(const Init *V, const std::string &VN,
1483 const std::vector<std::pair<const Init*, std::string> > &args) {
1484 typedef std::pair<const Init*, std::string> PairType;
1486 std::vector<const Init *> Args;
1487 std::vector<std::string> Names;
1489 for (std::vector<PairType>::const_iterator i = args.begin(),
1493 Args.push_back(i->first);
1494 Names.push_back(i->second);
1497 return DagInit::get(V, VN, Args, Names);
1501 DagInit::get(const Init *V, const std::string &VN,
1502 const std::vector<const Init*> &args,
1503 const std::vector<std::string> &argNames) {
1504 return new DagInit(V, VN, args, argNames);
1507 const Init *DagInit::resolveReferences(Record &R, const RecordVal *RV) const {
1508 std::vector<const Init*> NewArgs;
1509 for (unsigned i = 0, e = Args.size(); i != e; ++i)
1510 NewArgs.push_back(Args[i]->resolveReferences(R, RV));
1512 const Init *Op = Val->resolveReferences(R, RV);
1514 if (Args != NewArgs || Op != Val)
1515 return DagInit::get(Op, ValName, NewArgs, ArgNames);
1521 std::string DagInit::getAsString() const {
1522 std::string Result = "(" + Val->getAsString();
1523 if (!ValName.empty())
1524 Result += ":" + ValName;
1526 Result += " " + Args[0]->getAsString();
1527 if (!ArgNames[0].empty()) Result += ":$" + ArgNames[0];
1528 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
1529 Result += ", " + Args[i]->getAsString();
1530 if (!ArgNames[i].empty()) Result += ":$" + ArgNames[i];
1533 return Result + ")";
1537 //===----------------------------------------------------------------------===//
1538 // Other implementations
1539 //===----------------------------------------------------------------------===//
1541 RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
1542 : Name(N), Ty(T), Prefix(P) {
1543 Value = Ty->convertValue(UnsetInit::get());
1544 assert(Value && "Cannot create unset value for current type!");
1547 void RecordVal::dump() const { errs() << *this; }
1549 void RecordVal::print(raw_ostream &OS, bool PrintSem) const {
1550 if (getPrefix()) OS << "field ";
1551 OS << *getType() << " " << getName();
1554 OS << " = " << *getValue();
1556 if (PrintSem) OS << ";\n";
1559 unsigned Record::LastID = 0;
1561 DefInit *Record::getDefInit() {
1563 TheInit = new DefInit(this, new RecordRecTy(this));
1567 void Record::setName(const std::string &Name) {
1568 if (TrackedRecords.getDef(getName()) == this) {
1569 TrackedRecords.removeDef(getName());
1571 TrackedRecords.addDef(this);
1573 TrackedRecords.removeClass(getName());
1575 TrackedRecords.addClass(this);
1579 /// resolveReferencesTo - If anything in this record refers to RV, replace the
1580 /// reference to RV with the RHS of RV. If RV is null, we resolve all possible
1582 void Record::resolveReferencesTo(const RecordVal *RV) {
1583 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
1584 if (const Init *V = Values[i].getValue())
1585 Values[i].setValue(V->resolveReferences(*this, RV));
1589 void Record::dump() const { errs() << *this; }
1591 raw_ostream &llvm::operator<<(raw_ostream &OS, const Record &R) {
1594 const std::vector<std::string> &TArgs = R.getTemplateArgs();
1595 if (!TArgs.empty()) {
1597 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1599 const RecordVal *RV = R.getValue(TArgs[i]);
1600 assert(RV && "Template argument record not found??");
1601 RV->print(OS, false);
1607 const std::vector<Record*> &SC = R.getSuperClasses();
1610 for (unsigned i = 0, e = SC.size(); i != e; ++i)
1611 OS << " " << SC[i]->getName();
1615 const std::vector<RecordVal> &Vals = R.getValues();
1616 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
1617 if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
1619 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
1620 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
1626 /// getValueInit - Return the initializer for a value with the specified name,
1627 /// or throw an exception if the field does not exist.
1629 const Init *Record::getValueInit(StringRef FieldName) const {
1630 const RecordVal *R = getValue(FieldName);
1631 if (R == 0 || R->getValue() == 0)
1632 throw "Record `" + getName() + "' does not have a field named `" +
1633 FieldName.str() + "'!\n";
1634 return R->getValue();
1638 /// getValueAsString - This method looks up the specified field and returns its
1639 /// value as a string, throwing an exception if the field does not exist or if
1640 /// the value is not a string.
1642 std::string Record::getValueAsString(StringRef FieldName) const {
1643 const RecordVal *R = getValue(FieldName);
1644 if (R == 0 || R->getValue() == 0)
1645 throw "Record `" + getName() + "' does not have a field named `" +
1646 FieldName.str() + "'!\n";
1648 if (const StringInit *SI = dynamic_cast<const StringInit*>(R->getValue()))
1649 return SI->getValue();
1650 throw "Record `" + getName() + "', field `" + FieldName.str() +
1651 "' does not have a string initializer!";
1654 /// getValueAsBitsInit - This method looks up the specified field and returns
1655 /// its value as a BitsInit, throwing an exception if the field does not exist
1656 /// or if the value is not the right type.
1658 const BitsInit *Record::getValueAsBitsInit(StringRef FieldName) const {
1659 const RecordVal *R = getValue(FieldName);
1660 if (R == 0 || R->getValue() == 0)
1661 throw "Record `" + getName() + "' does not have a field named `" +
1662 FieldName.str() + "'!\n";
1664 if (const BitsInit *BI = dynamic_cast<const BitsInit*>(R->getValue()))
1666 throw "Record `" + getName() + "', field `" + FieldName.str() +
1667 "' does not have a BitsInit initializer!";
1670 /// getValueAsListInit - This method looks up the specified field and returns
1671 /// its value as a ListInit, throwing an exception if the field does not exist
1672 /// or if the value is not the right type.
1674 const ListInit *Record::getValueAsListInit(StringRef FieldName) const {
1675 const RecordVal *R = getValue(FieldName);
1676 if (R == 0 || R->getValue() == 0)
1677 throw "Record `" + getName() + "' does not have a field named `" +
1678 FieldName.str() + "'!\n";
1680 if (const ListInit *LI = dynamic_cast<const ListInit*>(R->getValue()))
1682 throw "Record `" + getName() + "', field `" + FieldName.str() +
1683 "' does not have a list initializer!";
1686 /// getValueAsListOfDefs - This method looks up the specified field and returns
1687 /// its value as a vector of records, throwing an exception if the field does
1688 /// not exist or if the value is not the right type.
1690 std::vector<Record*>
1691 Record::getValueAsListOfDefs(StringRef FieldName) const {
1692 const ListInit *List = getValueAsListInit(FieldName);
1693 std::vector<Record*> Defs;
1694 for (unsigned i = 0; i < List->getSize(); i++) {
1695 if (const DefInit *DI = dynamic_cast<const DefInit*>(List->getElement(i))) {
1696 Defs.push_back(DI->getDef());
1698 throw "Record `" + getName() + "', field `" + FieldName.str() +
1699 "' list is not entirely DefInit!";
1705 /// getValueAsInt - This method looks up the specified field and returns its
1706 /// value as an int64_t, throwing an exception if the field does not exist or if
1707 /// the value is not the right type.
1709 int64_t Record::getValueAsInt(StringRef FieldName) const {
1710 const RecordVal *R = getValue(FieldName);
1711 if (R == 0 || R->getValue() == 0)
1712 throw "Record `" + getName() + "' does not have a field named `" +
1713 FieldName.str() + "'!\n";
1715 if (const IntInit *II = dynamic_cast<const IntInit*>(R->getValue()))
1716 return II->getValue();
1717 throw "Record `" + getName() + "', field `" + FieldName.str() +
1718 "' does not have an int initializer!";
1721 /// getValueAsListOfInts - This method looks up the specified field and returns
1722 /// its value as a vector of integers, throwing an exception if the field does
1723 /// not exist or if the value is not the right type.
1725 std::vector<int64_t>
1726 Record::getValueAsListOfInts(StringRef FieldName) const {
1727 const ListInit *List = getValueAsListInit(FieldName);
1728 std::vector<int64_t> Ints;
1729 for (unsigned i = 0; i < List->getSize(); i++) {
1730 if (const IntInit *II = dynamic_cast<const IntInit*>(List->getElement(i))) {
1731 Ints.push_back(II->getValue());
1733 throw "Record `" + getName() + "', field `" + FieldName.str() +
1734 "' does not have a list of ints initializer!";
1740 /// getValueAsListOfStrings - This method looks up the specified field and
1741 /// returns its value as a vector of strings, throwing an exception if the
1742 /// field does not exist or if the value is not the right type.
1744 std::vector<std::string>
1745 Record::getValueAsListOfStrings(StringRef FieldName) const {
1746 const ListInit *List = getValueAsListInit(FieldName);
1747 std::vector<std::string> Strings;
1748 for (unsigned i = 0; i < List->getSize(); i++) {
1749 if (const StringInit *II = dynamic_cast<const StringInit*>(List->getElement(i))) {
1750 Strings.push_back(II->getValue());
1752 throw "Record `" + getName() + "', field `" + FieldName.str() +
1753 "' does not have a list of strings initializer!";
1759 /// getValueAsDef - This method looks up the specified field and returns its
1760 /// value as a Record, throwing an exception if the field does not exist or if
1761 /// the value is not the right type.
1763 Record *Record::getValueAsDef(StringRef FieldName) const {
1764 const RecordVal *R = getValue(FieldName);
1765 if (R == 0 || R->getValue() == 0)
1766 throw "Record `" + getName() + "' does not have a field named `" +
1767 FieldName.str() + "'!\n";
1769 if (const DefInit *DI = dynamic_cast<const DefInit*>(R->getValue()))
1770 return DI->getDef();
1771 throw "Record `" + getName() + "', field `" + FieldName.str() +
1772 "' does not have a def initializer!";
1775 /// getValueAsBit - This method looks up the specified field and returns its
1776 /// value as a bit, throwing an exception if the field does not exist or if
1777 /// the value is not the right type.
1779 bool Record::getValueAsBit(StringRef FieldName) const {
1780 const RecordVal *R = getValue(FieldName);
1781 if (R == 0 || R->getValue() == 0)
1782 throw "Record `" + getName() + "' does not have a field named `" +
1783 FieldName.str() + "'!\n";
1785 if (const BitInit *BI = dynamic_cast<const BitInit*>(R->getValue()))
1786 return BI->getValue();
1787 throw "Record `" + getName() + "', field `" + FieldName.str() +
1788 "' does not have a bit initializer!";
1791 /// getValueAsDag - This method looks up the specified field and returns its
1792 /// value as an Dag, throwing an exception if the field does not exist or if
1793 /// the value is not the right type.
1795 const DagInit *Record::getValueAsDag(StringRef FieldName) const {
1796 const RecordVal *R = getValue(FieldName);
1797 if (R == 0 || R->getValue() == 0)
1798 throw "Record `" + getName() + "' does not have a field named `" +
1799 FieldName.str() + "'!\n";
1801 if (const DagInit *DI = dynamic_cast<const DagInit*>(R->getValue()))
1803 throw "Record `" + getName() + "', field `" + FieldName.str() +
1804 "' does not have a dag initializer!";
1807 std::string Record::getValueAsCode(StringRef FieldName) const {
1808 const RecordVal *R = getValue(FieldName);
1809 if (R == 0 || R->getValue() == 0)
1810 throw "Record `" + getName() + "' does not have a field named `" +
1811 FieldName.str() + "'!\n";
1813 if (const CodeInit *CI = dynamic_cast<const CodeInit*>(R->getValue()))
1814 return CI->getValue();
1815 throw "Record `" + getName() + "', field `" + FieldName.str() +
1816 "' does not have a code initializer!";
1820 void MultiClass::dump() const {
1821 errs() << "Record:\n";
1824 errs() << "Defs:\n";
1825 for (RecordVector::const_iterator r = DefPrototypes.begin(),
1826 rend = DefPrototypes.end();
1834 void RecordKeeper::dump() const { errs() << *this; }
1836 raw_ostream &llvm::operator<<(raw_ostream &OS, const RecordKeeper &RK) {
1837 OS << "------------- Classes -----------------\n";
1838 const std::map<std::string, Record*> &Classes = RK.getClasses();
1839 for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
1840 E = Classes.end(); I != E; ++I)
1841 OS << "class " << *I->second;
1843 OS << "------------- Defs -----------------\n";
1844 const std::map<std::string, Record*> &Defs = RK.getDefs();
1845 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
1846 E = Defs.end(); I != E; ++I)
1847 OS << "def " << *I->second;
1852 /// getAllDerivedDefinitions - This method returns all concrete definitions
1853 /// that derive from the specified class name. If a class with the specified
1854 /// name does not exist, an error is printed and true is returned.
1855 std::vector<Record*>
1856 RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
1857 Record *Class = getClass(ClassName);
1859 throw "ERROR: Couldn't find the `" + ClassName + "' class!\n";
1861 std::vector<Record*> Defs;
1862 for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(),
1863 E = getDefs().end(); I != E; ++I)
1864 if (I->second->isSubClassOf(Class))
1865 Defs.push_back(I->second);