1 //===- Record.cpp - Record implementation ---------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Implement the tablegen record classes.
12 //===----------------------------------------------------------------------===//
16 #include "llvm/Support/DataTypes.h"
17 #include "llvm/Support/ErrorHandling.h"
18 #include "llvm/Support/Format.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/FoldingSet.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include "llvm/ADT/StringExtras.h"
24 #include "llvm/ADT/StringMap.h"
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 Init *BitRecTy::convertValue(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 Init *BitRecTy::convertValue(IntInit *II) {
102 int64_t Val = II->getValue();
103 if (Val != 0 && Val != 1) return 0; // Only accept 0 or 1 for a bit!
105 return BitInit::get(Val != 0);
108 Init *BitRecTy::convertValue(TypedInit *VI) {
109 if (dynamic_cast<BitRecTy*>(VI->getType()))
110 return VI; // Accept variable if it is already of bit type!
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 Init *BitsRecTy::convertValue(UnsetInit *UI) {
129 SmallVector<Init *, 16> NewBits(Size);
131 for (unsigned i = 0; i != Size; ++i)
132 NewBits[i] = UnsetInit::get();
134 return BitsInit::get(NewBits);
137 Init *BitsRecTy::convertValue(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 Init *BitsRecTy::convertValue(IntInit *II) {
154 int64_t Value = II->getValue();
155 // Make sure this bitfield is large enough to hold the integer value.
156 if (!canFitInBitfield(Value, Size))
159 SmallVector<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 Init *BitsRecTy::convertValue(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 Init *BitsRecTy::convertValue(TypedInit *VI) {
175 if (BitsRecTy *BRT = dynamic_cast<BitsRecTy*>(VI->getType()))
176 if (BRT->Size == Size) {
177 SmallVector<Init *, 16> NewBits(Size);
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 (TernOpInit *Tern = dynamic_cast<TernOpInit*>(VI)) {
188 if (Tern->getOpcode() == TernOpInit::IF) {
189 Init *LHS = Tern->getLHS();
190 Init *MHS = Tern->getMHS();
191 Init *RHS = Tern->getRHS();
193 IntInit *MHSi = dynamic_cast<IntInit*>(MHS);
194 IntInit *RHSi = dynamic_cast<IntInit*>(RHS);
197 int64_t MHSVal = MHSi->getValue();
198 int64_t RHSVal = RHSi->getValue();
200 if (canFitInBitfield(MHSVal, Size) && canFitInBitfield(RHSVal, Size)) {
201 SmallVector<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 BitsInit *MHSbs = dynamic_cast<BitsInit*>(MHS);
214 BitsInit *RHSbs = dynamic_cast<BitsInit*>(RHS);
216 if (MHSbs && RHSbs) {
217 SmallVector<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 Init *IntRecTy::convertValue(BitInit *BI) {
235 return IntInit::get(BI->getValue());
238 Init *IntRecTy::convertValue(BitsInit *BI) {
240 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
241 if (BitInit *Bit = dynamic_cast<BitInit*>(BI->getBit(i))) {
242 Result |= Bit->getValue() << i;
246 return IntInit::get(Result);
249 Init *IntRecTy::convertValue(TypedInit *TI) {
250 if (TI->getType()->typeIsConvertibleTo(this))
251 return TI; // Accept variable if already of the right type!
255 Init *StringRecTy::convertValue(UnOpInit *BO) {
256 if (BO->getOpcode() == UnOpInit::CAST) {
257 Init *L = BO->getOperand()->convertInitializerTo(this);
258 if (L == 0) return 0;
259 if (L != BO->getOperand())
260 return UnOpInit::get(UnOpInit::CAST, L, new StringRecTy);
264 return convertValue((TypedInit*)BO);
267 Init *StringRecTy::convertValue(BinOpInit *BO) {
268 if (BO->getOpcode() == BinOpInit::STRCONCAT) {
269 Init *L = BO->getLHS()->convertInitializerTo(this);
270 Init *R = BO->getRHS()->convertInitializerTo(this);
271 if (L == 0 || R == 0) return 0;
272 if (L != BO->getLHS() || R != BO->getRHS())
273 return BinOpInit::get(BinOpInit::STRCONCAT, L, R, new StringRecTy);
277 return convertValue((TypedInit*)BO);
281 Init *StringRecTy::convertValue(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 Init *ListRecTy::convertValue(ListInit *LI) {
292 std::vector<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 (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 Init *ListRecTy::convertValue(TypedInit *TI) {
311 // Ensure that TI is compatible with our class.
312 if (ListRecTy *LRT = dynamic_cast<ListRecTy*>(TI->getType()))
313 if (LRT->getElementType()->typeIsConvertibleTo(getElementType()))
318 Init *CodeRecTy::convertValue(TypedInit *TI) {
319 if (TI->getType()->typeIsConvertibleTo(this))
324 Init *DagRecTy::convertValue(TypedInit *TI) {
325 if (TI->getType()->typeIsConvertibleTo(this))
330 Init *DagRecTy::convertValue(UnOpInit *BO) {
331 if (BO->getOpcode() == UnOpInit::CAST) {
332 Init *L = BO->getOperand()->convertInitializerTo(this);
333 if (L == 0) return 0;
334 if (L != BO->getOperand())
335 return UnOpInit::get(UnOpInit::CAST, L, new DagRecTy);
341 Init *DagRecTy::convertValue(BinOpInit *BO) {
342 if (BO->getOpcode() == BinOpInit::CONCAT) {
343 Init *L = BO->getLHS()->convertInitializerTo(this);
344 Init *R = BO->getRHS()->convertInitializerTo(this);
345 if (L == 0 || R == 0) return 0;
346 if (L != BO->getLHS() || R != BO->getRHS())
347 return BinOpInit::get(BinOpInit::CONCAT, L, R, new DagRecTy);
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 Init *RecordRecTy::convertValue(DefInit *DI) {
362 // Ensure that DI is a subclass of Rec.
363 if (!DI->getDef()->isSubClassOf(Rec))
368 Init *RecordRecTy::convertValue(TypedInit *TI) {
369 // Ensure that TI is compatible with Rec.
370 if (RecordRecTy *RRT = dynamic_cast<RecordRecTy*>(TI->getType()))
371 if (RRT->getRecord()->isSubClassOf(getRecord()) ||
372 RRT->getRecord() == getRecord())
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 UnsetInit *UnsetInit::get() {
450 static UnsetInit TheInit;
454 BitInit *BitInit::get(bool V) {
455 static BitInit True(true);
456 static BitInit False(false);
458 return V ? &True : &False;
462 ProfileBitsInit(FoldingSetNodeID &ID, ArrayRef<Init *> Range) {
463 ID.AddInteger(Range.size());
465 for (ArrayRef<Init *>::iterator i = Range.begin(),
472 BitsInit *BitsInit::get(ArrayRef<Init *> Range) {
473 typedef FoldingSet<BitsInit> Pool;
477 ProfileBitsInit(ID, Range);
480 if (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<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 (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 Init *BitsInit::resolveReferences(Record &R, const RecordVal *RV) const {
521 bool Changed = false;
522 SmallVector<Init *, 16> NewBits(getNumBits());
524 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
526 Init *CurBit = getBit(i);
530 CurBit = CurBit->resolveReferences(R, RV);
531 Changed |= B != CurBit;
532 } while (B != CurBit);
537 return BitsInit::get(NewBits);
539 return const_cast<BitsInit *>(this);
542 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<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 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 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<Init *> Range,
589 ID.AddInteger(Range.size());
590 ID.AddPointer(EltTy);
592 for (ArrayRef<Init *>::iterator i = Range.begin(),
599 ListInit *ListInit::get(ArrayRef<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 (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<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 DefInit *DI = dynamic_cast<DefInit*>(Values[i]);
639 if (DI == 0) throw "Expected record in list!";
643 Init *ListInit::resolveReferences(Record &R, const RecordVal *RV) const {
644 std::vector<Init*> Resolved;
645 Resolved.reserve(getSize());
646 bool Changed = false;
648 for (unsigned i = 0, e = getSize(); i != e; ++i) {
650 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());
662 return const_cast<ListInit *>(this);
665 Init *ListInit::resolveListElementReference(Record &R, const RecordVal *IRV,
666 unsigned Elt) const {
667 if (Elt >= getSize())
668 return 0; // Out of range reference.
669 Init *E = getElement(Elt);
670 // If the element is set to some value, or if we are resolving a reference
671 // to a specific variable and that variable is explicitly unset, then
672 // replace the VarListElementInit with it.
673 if (IRV || !dynamic_cast<UnsetInit*>(E))
678 std::string ListInit::getAsString() const {
679 std::string Result = "[";
680 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
681 if (i) Result += ", ";
682 Result += Values[i]->getAsString();
687 Init *OpInit::resolveBitReference(Record &R, const RecordVal *IRV,
688 unsigned Bit) const {
689 Init *Folded = Fold(&R, 0);
691 if (Folded != this) {
692 TypedInit *Typed = dynamic_cast<TypedInit *>(Folded);
694 return Typed->resolveBitReference(R, IRV, Bit);
701 Init *OpInit::resolveListElementReference(Record &R, const RecordVal *IRV,
702 unsigned Elt) const {
703 Init *Folded = Fold(&R, 0);
705 if (Folded != this) {
706 TypedInit *Typed = dynamic_cast<TypedInit *>(Folded);
708 return Typed->resolveListElementReference(R, IRV, Elt);
715 UnOpInit *UnOpInit::get(UnaryOp opc, Init *lhs, RecTy *Type) {
716 typedef std::pair<std::pair<unsigned, Init *>, RecTy *> Key;
718 typedef DenseMap<Key, UnOpInit *> Pool;
721 Key TheKey(std::make_pair(std::make_pair(opc, lhs), Type));
723 UnOpInit *&I = ThePool[TheKey];
724 if (!I) I = new UnOpInit(opc, lhs, Type);
728 Init *UnOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const {
729 switch (getOpcode()) {
730 default: assert(0 && "Unknown unop");
732 if (getType()->getAsString() == "string") {
733 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
738 DefInit *LHSd = dynamic_cast<DefInit*>(LHS);
740 return StringInit::get(LHSd->getDef()->getName());
743 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
745 std::string Name = LHSs->getValue();
747 // From TGParser::ParseIDValue
749 if (const RecordVal *RV = CurRec->getValue(Name)) {
750 if (RV->getType() != getType())
751 throw "type mismatch in cast";
752 return VarInit::get(Name, RV->getType());
755 std::string TemplateArgName = CurRec->getName()+":"+Name;
756 if (CurRec->isTemplateArg(TemplateArgName)) {
757 const RecordVal *RV = CurRec->getValue(TemplateArgName);
758 assert(RV && "Template arg doesn't exist??");
760 if (RV->getType() != getType())
761 throw "type mismatch in cast";
763 return VarInit::get(TemplateArgName, RV->getType());
768 std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
769 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
770 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
771 assert(RV && "Template arg doesn't exist??");
773 if (RV->getType() != getType())
774 throw "type mismatch in cast";
776 return VarInit::get(MCName, RV->getType());
780 if (Record *D = (CurRec->getRecords()).getDef(Name))
781 return DefInit::get(D);
783 throw TGError(CurRec->getLoc(), "Undefined reference:'" + Name + "'\n");
789 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
791 if (LHSl->getSize() == 0) {
792 assert(0 && "Empty list in car");
795 return LHSl->getElement(0);
800 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
802 if (LHSl->getSize() == 0) {
803 assert(0 && "Empty list in cdr");
806 // Note the +1. We can't just pass the result of getValues()
808 ArrayRef<Init *>::iterator begin = LHSl->getValues().begin()+1;
809 ArrayRef<Init *>::iterator end = LHSl->getValues().end();
811 ListInit::get(ArrayRef<Init *>(begin, end - begin),
818 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
820 if (LHSl->getSize() == 0) {
821 return IntInit::get(1);
823 return IntInit::get(0);
826 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
828 if (LHSs->getValue().empty()) {
829 return IntInit::get(1);
831 return IntInit::get(0);
838 return const_cast<UnOpInit *>(this);
841 Init *UnOpInit::resolveReferences(Record &R, const RecordVal *RV) const {
842 Init *lhs = LHS->resolveReferences(R, RV);
845 return (UnOpInit::get(getOpcode(), lhs, getType()))->Fold(&R, 0);
849 std::string UnOpInit::getAsString() const {
852 case CAST: Result = "!cast<" + getType()->getAsString() + ">"; break;
853 case HEAD: Result = "!head"; break;
854 case TAIL: Result = "!tail"; break;
855 case EMPTY: Result = "!empty"; break;
857 return Result + "(" + LHS->getAsString() + ")";
860 BinOpInit *BinOpInit::get(BinaryOp opc, Init *lhs,
861 Init *rhs, RecTy *Type) {
863 std::pair<std::pair<unsigned, Init *>, Init *>,
867 typedef DenseMap<Key, BinOpInit *> Pool;
870 Key TheKey(std::make_pair(std::make_pair(std::make_pair(opc, lhs), rhs),
873 BinOpInit *&I = ThePool[TheKey];
874 if (!I) I = new BinOpInit(opc, lhs, rhs, Type);
878 Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const {
879 switch (getOpcode()) {
880 default: assert(0 && "Unknown binop");
882 DagInit *LHSs = dynamic_cast<DagInit*>(LHS);
883 DagInit *RHSs = dynamic_cast<DagInit*>(RHS);
885 DefInit *LOp = dynamic_cast<DefInit*>(LHSs->getOperator());
886 DefInit *ROp = dynamic_cast<DefInit*>(RHSs->getOperator());
887 if (LOp == 0 || ROp == 0 || LOp->getDef() != ROp->getDef())
888 throw "Concated Dag operators do not match!";
889 std::vector<Init*> Args;
890 std::vector<std::string> ArgNames;
891 for (unsigned i = 0, e = LHSs->getNumArgs(); i != e; ++i) {
892 Args.push_back(LHSs->getArg(i));
893 ArgNames.push_back(LHSs->getArgName(i));
895 for (unsigned i = 0, e = RHSs->getNumArgs(); i != e; ++i) {
896 Args.push_back(RHSs->getArg(i));
897 ArgNames.push_back(RHSs->getArgName(i));
899 return DagInit::get(LHSs->getOperator(), "", Args, ArgNames);
904 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
905 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
907 return StringInit::get(LHSs->getValue() + RHSs->getValue());
911 // try to fold eq comparison for 'bit' and 'int', otherwise fallback
912 // to string objects.
914 dynamic_cast<IntInit*>(LHS->convertInitializerTo(IntRecTy::get()));
916 dynamic_cast<IntInit*>(RHS->convertInitializerTo(IntRecTy::get()));
919 return IntInit::get(L->getValue() == R->getValue());
921 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
922 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
924 // Make sure we've resolved
926 return IntInit::get(LHSs->getValue() == RHSs->getValue());
933 IntInit *LHSi = dynamic_cast<IntInit*>(LHS);
934 IntInit *RHSi = dynamic_cast<IntInit*>(RHS);
936 int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
938 switch (getOpcode()) {
939 default: assert(0 && "Bad opcode!");
940 case SHL: Result = LHSv << RHSv; break;
941 case SRA: Result = LHSv >> RHSv; break;
942 case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break;
944 return IntInit::get(Result);
949 return const_cast<BinOpInit *>(this);
952 Init *BinOpInit::resolveReferences(Record &R, const RecordVal *RV) const {
953 Init *lhs = LHS->resolveReferences(R, RV);
954 Init *rhs = RHS->resolveReferences(R, RV);
956 if (LHS != lhs || RHS != rhs)
957 return (BinOpInit::get(getOpcode(), lhs, rhs, getType()))->Fold(&R, 0);
961 std::string BinOpInit::getAsString() const {
964 case CONCAT: Result = "!con"; break;
965 case SHL: Result = "!shl"; break;
966 case SRA: Result = "!sra"; break;
967 case SRL: Result = "!srl"; break;
968 case EQ: Result = "!eq"; break;
969 case STRCONCAT: Result = "!strconcat"; break;
971 return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")";
974 TernOpInit *TernOpInit::get(TernaryOp opc, Init *lhs,
975 Init *mhs, Init *rhs,
979 std::pair<std::pair<unsigned, RecTy *>, Init *>,
985 typedef DenseMap<Key, TernOpInit *> Pool;
988 Key TheKey(std::make_pair(std::make_pair(std::make_pair(std::make_pair(opc,
994 TernOpInit *&I = ThePool[TheKey];
995 if (!I) I = new TernOpInit(opc, lhs, mhs, rhs, Type);
999 static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
1000 Record *CurRec, MultiClass *CurMultiClass);
1002 static Init *EvaluateOperation(OpInit *RHSo, Init *LHS, Init *Arg,
1003 RecTy *Type, Record *CurRec,
1004 MultiClass *CurMultiClass) {
1005 std::vector<Init *> NewOperands;
1007 TypedInit *TArg = dynamic_cast<TypedInit*>(Arg);
1009 // If this is a dag, recurse
1010 if (TArg && TArg->getType()->getAsString() == "dag") {
1011 Init *Result = ForeachHelper(LHS, Arg, RHSo, Type,
1012 CurRec, CurMultiClass);
1020 for (int i = 0; i < RHSo->getNumOperands(); ++i) {
1021 OpInit *RHSoo = dynamic_cast<OpInit*>(RHSo->getOperand(i));
1024 Init *Result = EvaluateOperation(RHSoo, LHS, Arg,
1025 Type, CurRec, CurMultiClass);
1027 NewOperands.push_back(Result);
1029 NewOperands.push_back(Arg);
1031 } else if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
1032 NewOperands.push_back(Arg);
1034 NewOperands.push_back(RHSo->getOperand(i));
1038 // Now run the operator and use its result as the new leaf
1039 const OpInit *NewOp = RHSo->clone(NewOperands);
1040 Init *NewVal = NewOp->Fold(CurRec, CurMultiClass);
1041 if (NewVal != NewOp)
1047 static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
1048 Record *CurRec, MultiClass *CurMultiClass) {
1049 DagInit *MHSd = dynamic_cast<DagInit*>(MHS);
1050 ListInit *MHSl = dynamic_cast<ListInit*>(MHS);
1052 DagRecTy *DagType = dynamic_cast<DagRecTy*>(Type);
1053 ListRecTy *ListType = dynamic_cast<ListRecTy*>(Type);
1055 OpInit *RHSo = dynamic_cast<OpInit*>(RHS);
1058 throw TGError(CurRec->getLoc(), "!foreach requires an operator\n");
1061 TypedInit *LHSt = dynamic_cast<TypedInit*>(LHS);
1064 throw TGError(CurRec->getLoc(), "!foreach requires typed variable\n");
1067 if ((MHSd && DagType) || (MHSl && ListType)) {
1069 Init *Val = MHSd->getOperator();
1070 Init *Result = EvaluateOperation(RHSo, LHS, Val,
1071 Type, CurRec, CurMultiClass);
1076 std::vector<std::pair<Init *, std::string> > args;
1077 for (unsigned int i = 0; i < MHSd->getNumArgs(); ++i) {
1079 std::string ArgName;
1080 Arg = MHSd->getArg(i);
1081 ArgName = MHSd->getArgName(i);
1084 Init *Result = EvaluateOperation(RHSo, LHS, Arg, Type,
1085 CurRec, CurMultiClass);
1090 // TODO: Process arg names
1091 args.push_back(std::make_pair(Arg, ArgName));
1094 return DagInit::get(Val, "", args);
1097 std::vector<Init *> NewOperands;
1098 std::vector<Init *> NewList(MHSl->begin(), MHSl->end());
1100 for (std::vector<Init *>::iterator li = NewList.begin(),
1101 liend = NewList.end();
1105 NewOperands.clear();
1106 for(int i = 0; i < RHSo->getNumOperands(); ++i) {
1107 // First, replace the foreach variable with the list item
1108 if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
1109 NewOperands.push_back(Item);
1111 NewOperands.push_back(RHSo->getOperand(i));
1115 // Now run the operator and use its result as the new list item
1116 const OpInit *NewOp = RHSo->clone(NewOperands);
1117 Init *NewItem = NewOp->Fold(CurRec, CurMultiClass);
1118 if (NewItem != NewOp)
1121 return ListInit::get(NewList, MHSl->getType());
1127 Init *TernOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const {
1128 switch (getOpcode()) {
1129 default: assert(0 && "Unknown binop");
1131 DefInit *LHSd = dynamic_cast<DefInit*>(LHS);
1132 VarInit *LHSv = dynamic_cast<VarInit*>(LHS);
1133 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
1135 DefInit *MHSd = dynamic_cast<DefInit*>(MHS);
1136 VarInit *MHSv = dynamic_cast<VarInit*>(MHS);
1137 StringInit *MHSs = dynamic_cast<StringInit*>(MHS);
1139 DefInit *RHSd = dynamic_cast<DefInit*>(RHS);
1140 VarInit *RHSv = dynamic_cast<VarInit*>(RHS);
1141 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
1143 if ((LHSd && MHSd && RHSd)
1144 || (LHSv && MHSv && RHSv)
1145 || (LHSs && MHSs && RHSs)) {
1147 Record *Val = RHSd->getDef();
1148 if (LHSd->getAsString() == RHSd->getAsString()) {
1149 Val = MHSd->getDef();
1151 return DefInit::get(Val);
1154 std::string Val = RHSv->getName();
1155 if (LHSv->getAsString() == RHSv->getAsString()) {
1156 Val = MHSv->getName();
1158 return VarInit::get(Val, getType());
1161 std::string Val = RHSs->getValue();
1163 std::string::size_type found;
1164 std::string::size_type idx = 0;
1166 found = Val.find(LHSs->getValue(), idx);
1167 if (found != std::string::npos) {
1168 Val.replace(found, LHSs->getValue().size(), MHSs->getValue());
1170 idx = found + MHSs->getValue().size();
1171 } while (found != std::string::npos);
1173 return StringInit::get(Val);
1180 Init *Result = ForeachHelper(LHS, MHS, RHS, getType(),
1181 CurRec, CurMultiClass);
1189 IntInit *LHSi = dynamic_cast<IntInit*>(LHS);
1190 if (Init *I = LHS->convertInitializerTo(IntRecTy::get()))
1191 LHSi = dynamic_cast<IntInit*>(I);
1193 if (LHSi->getValue()) {
1203 return const_cast<TernOpInit *>(this);
1206 Init *TernOpInit::resolveReferences(Record &R,
1207 const RecordVal *RV) const {
1208 Init *lhs = LHS->resolveReferences(R, RV);
1210 if (Opc == IF && lhs != LHS) {
1211 IntInit *Value = dynamic_cast<IntInit*>(lhs);
1212 if (Init *I = lhs->convertInitializerTo(IntRecTy::get()))
1213 Value = dynamic_cast<IntInit*>(I);
1216 if (Value->getValue()) {
1217 Init *mhs = MHS->resolveReferences(R, RV);
1218 return (TernOpInit::get(getOpcode(), lhs, mhs,
1219 RHS, getType()))->Fold(&R, 0);
1221 Init *rhs = RHS->resolveReferences(R, RV);
1222 return (TernOpInit::get(getOpcode(), lhs, MHS,
1223 rhs, getType()))->Fold(&R, 0);
1228 Init *mhs = MHS->resolveReferences(R, RV);
1229 Init *rhs = RHS->resolveReferences(R, RV);
1231 if (LHS != lhs || MHS != mhs || RHS != rhs)
1232 return (TernOpInit::get(getOpcode(), lhs, mhs, rhs,
1233 getType()))->Fold(&R, 0);
1237 std::string TernOpInit::getAsString() const {
1240 case SUBST: Result = "!subst"; break;
1241 case FOREACH: Result = "!foreach"; break;
1242 case IF: Result = "!if"; break;
1244 return Result + "(" + LHS->getAsString() + ", " + MHS->getAsString() + ", "
1245 + RHS->getAsString() + ")";
1248 RecTy *TypedInit::getFieldType(const std::string &FieldName) const {
1249 RecordRecTy *RecordType = dynamic_cast<RecordRecTy *>(getType());
1251 RecordVal *Field = RecordType->getRecord()->getValue(FieldName);
1253 return Field->getType();
1260 TypedInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) const {
1261 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
1262 if (T == 0) return 0; // Cannot subscript a non-bits variable.
1263 unsigned NumBits = T->getNumBits();
1265 SmallVector<Init *, 16> NewBits(Bits.size());
1266 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
1267 if (Bits[i] >= NumBits)
1270 NewBits[i] = VarBitInit::get(const_cast<TypedInit *>(this), Bits[i]);
1272 return BitsInit::get(NewBits);
1276 TypedInit::convertInitListSlice(const std::vector<unsigned> &Elements) const {
1277 ListRecTy *T = dynamic_cast<ListRecTy*>(getType());
1278 if (T == 0) return 0; // Cannot subscript a non-list variable.
1280 if (Elements.size() == 1)
1281 return VarListElementInit::get(const_cast<TypedInit *>(this), Elements[0]);
1283 std::vector<Init*> ListInits;
1284 ListInits.reserve(Elements.size());
1285 for (unsigned i = 0, e = Elements.size(); i != e; ++i)
1286 ListInits.push_back(VarListElementInit::get(const_cast<TypedInit *>(this),
1288 return ListInit::get(ListInits, T);
1292 VarInit *VarInit::get(const std::string &VN, RecTy *T) {
1293 typedef std::pair<RecTy *, TableGenStringKey> Key;
1294 typedef DenseMap<Key, VarInit *> Pool;
1295 static Pool ThePool;
1297 Key TheKey(std::make_pair(T, VN));
1299 VarInit *&I = ThePool[TheKey];
1300 if (!I) I = new VarInit(VN, T);
1304 Init *VarInit::resolveBitReference(Record &R, const RecordVal *IRV,
1305 unsigned Bit) const {
1306 if (R.isTemplateArg(getName())) return 0;
1307 if (IRV && IRV->getName() != getName()) return 0;
1309 RecordVal *RV = R.getValue(getName());
1310 assert(RV && "Reference to a non-existent variable?");
1311 assert(dynamic_cast<BitsInit*>(RV->getValue()));
1312 BitsInit *BI = (BitsInit*)RV->getValue();
1314 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
1315 Init *B = BI->getBit(Bit);
1317 // If the bit is set to some value, or if we are resolving a reference to a
1318 // specific variable and that variable is explicitly unset, then replace the
1319 // VarBitInit with it.
1320 if (IRV || !dynamic_cast<UnsetInit*>(B))
1325 Init *VarInit::resolveListElementReference(Record &R,
1326 const RecordVal *IRV,
1327 unsigned Elt) const {
1328 if (R.isTemplateArg(getName())) return 0;
1329 if (IRV && IRV->getName() != getName()) return 0;
1331 RecordVal *RV = R.getValue(getName());
1332 assert(RV && "Reference to a non-existent variable?");
1333 ListInit *LI = dynamic_cast<ListInit*>(RV->getValue());
1335 VarInit *VI = dynamic_cast<VarInit*>(RV->getValue());
1336 assert(VI && "Invalid list element!");
1337 return VarListElementInit::get(VI, Elt);
1340 if (Elt >= LI->getSize())
1341 return 0; // Out of range reference.
1342 Init *E = LI->getElement(Elt);
1343 // If the element is set to some value, or if we are resolving a reference
1344 // to a specific variable and that variable is explicitly unset, then
1345 // replace the VarListElementInit with it.
1346 if (IRV || !dynamic_cast<UnsetInit*>(E))
1352 RecTy *VarInit::getFieldType(const std::string &FieldName) const {
1353 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
1354 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
1355 return RV->getType();
1359 Init *VarInit::getFieldInit(Record &R, const RecordVal *RV,
1360 const std::string &FieldName) const {
1361 if (dynamic_cast<RecordRecTy*>(getType()))
1362 if (const RecordVal *Val = R.getValue(VarName)) {
1363 if (RV != Val && (RV || dynamic_cast<UnsetInit*>(Val->getValue())))
1365 Init *TheInit = Val->getValue();
1366 assert(TheInit != this && "Infinite loop detected!");
1367 if (Init *I = TheInit->getFieldInit(R, RV, FieldName))
1375 /// resolveReferences - This method is used by classes that refer to other
1376 /// variables which may not be defined at the time the expression is formed.
1377 /// If a value is set for the variable later, this method will be called on
1378 /// users of the value to allow the value to propagate out.
1380 Init *VarInit::resolveReferences(Record &R, const RecordVal *RV) const {
1381 if (RecordVal *Val = R.getValue(VarName))
1382 if (RV == Val || (RV == 0 && !dynamic_cast<UnsetInit*>(Val->getValue())))
1383 return Val->getValue();
1384 return const_cast<VarInit *>(this);
1387 VarBitInit *VarBitInit::get(TypedInit *T, unsigned B) {
1388 typedef std::pair<TypedInit *, unsigned> Key;
1389 typedef DenseMap<Key, VarBitInit *> Pool;
1391 static Pool ThePool;
1393 Key TheKey(std::make_pair(T, B));
1395 VarBitInit *&I = ThePool[TheKey];
1396 if (!I) I = new VarBitInit(T, B);
1400 std::string VarBitInit::getAsString() const {
1401 return TI->getAsString() + "{" + utostr(Bit) + "}";
1404 Init *VarBitInit::resolveReferences(Record &R, const RecordVal *RV) const {
1405 if (Init *I = getVariable()->resolveBitReference(R, RV, getBitNum()))
1407 return const_cast<VarBitInit *>(this);
1410 VarListElementInit *VarListElementInit::get(TypedInit *T,
1412 typedef std::pair<TypedInit *, unsigned> Key;
1413 typedef DenseMap<Key, VarListElementInit *> Pool;
1415 static Pool ThePool;
1417 Key TheKey(std::make_pair(T, E));
1419 VarListElementInit *&I = ThePool[TheKey];
1420 if (!I) I = new VarListElementInit(T, E);
1424 std::string VarListElementInit::getAsString() const {
1425 return TI->getAsString() + "[" + utostr(Element) + "]";
1429 VarListElementInit::resolveReferences(Record &R, const RecordVal *RV) const {
1430 if (Init *I = getVariable()->resolveListElementReference(R, RV,
1433 return const_cast<VarListElementInit *>(this);
1436 Init *VarListElementInit::resolveBitReference(Record &R, const RecordVal *RV,
1437 unsigned Bit) const {
1438 // FIXME: This should be implemented, to support references like:
1439 // bit B = AA[0]{1};
1443 Init *VarListElementInit:: resolveListElementReference(Record &R,
1444 const RecordVal *RV,
1445 unsigned Elt) const {
1446 // FIXME: This should be implemented, to support references like:
1447 // int B = AA[0][1];
1451 DefInit *DefInit::get(Record *R) {
1452 return R->getDefInit();
1455 RecTy *DefInit::getFieldType(const std::string &FieldName) const {
1456 if (const RecordVal *RV = Def->getValue(FieldName))
1457 return RV->getType();
1461 Init *DefInit::getFieldInit(Record &R, const RecordVal *RV,
1462 const std::string &FieldName) const {
1463 return Def->getValue(FieldName)->getValue();
1467 std::string DefInit::getAsString() const {
1468 return Def->getName();
1471 FieldInit *FieldInit::get(Init *R, const std::string &FN) {
1472 typedef std::pair<Init *, TableGenStringKey> Key;
1473 typedef DenseMap<Key, FieldInit *> Pool;
1474 static Pool ThePool;
1476 Key TheKey(std::make_pair(R, FN));
1478 FieldInit *&I = ThePool[TheKey];
1479 if (!I) I = new FieldInit(R, FN);
1483 Init *FieldInit::resolveBitReference(Record &R, const RecordVal *RV,
1484 unsigned Bit) const {
1485 if (Init *BitsVal = Rec->getFieldInit(R, RV, FieldName))
1486 if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
1487 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
1488 Init *B = BI->getBit(Bit);
1490 if (dynamic_cast<BitInit*>(B)) // If the bit is set.
1491 return B; // Replace the VarBitInit with it.
1496 Init *FieldInit::resolveListElementReference(Record &R, const RecordVal *RV,
1497 unsigned Elt) const {
1498 if (Init *ListVal = Rec->getFieldInit(R, RV, FieldName))
1499 if (ListInit *LI = dynamic_cast<ListInit*>(ListVal)) {
1500 if (Elt >= LI->getSize()) return 0;
1501 Init *E = LI->getElement(Elt);
1503 // If the element is set to some value, or if we are resolving a
1504 // reference to a specific variable and that variable is explicitly
1505 // unset, then replace the VarListElementInit with it.
1506 if (RV || !dynamic_cast<UnsetInit*>(E))
1512 Init *FieldInit::resolveReferences(Record &R, const RecordVal *RV) const {
1513 Init *NewRec = RV ? Rec->resolveReferences(R, RV) : Rec;
1515 Init *BitsVal = NewRec->getFieldInit(R, RV, FieldName);
1517 Init *BVR = BitsVal->resolveReferences(R, RV);
1518 return BVR->isComplete() ? BVR : const_cast<FieldInit *>(this);
1521 if (NewRec != Rec) {
1522 return FieldInit::get(NewRec, FieldName);
1524 return const_cast<FieldInit *>(this);
1527 void ProfileDagInit(FoldingSetNodeID &ID,
1529 const std::string &VN,
1530 ArrayRef<Init *> ArgRange,
1531 ArrayRef<std::string> NameRange) {
1535 ArrayRef<Init *>::iterator Arg = ArgRange.begin();
1536 ArrayRef<std::string>::iterator Name = NameRange.begin();
1537 while (Arg != ArgRange.end()) {
1538 assert(Name != NameRange.end() && "Arg name underflow!");
1539 ID.AddPointer(*Arg++);
1540 ID.AddString(*Name++);
1542 assert(Name == NameRange.end() && "Arg name overflow!");
1546 DagInit::get(Init *V, const std::string &VN,
1547 ArrayRef<Init *> ArgRange,
1548 ArrayRef<std::string> NameRange) {
1549 typedef FoldingSet<DagInit> Pool;
1550 static Pool ThePool;
1552 FoldingSetNodeID ID;
1553 ProfileDagInit(ID, V, VN, ArgRange, NameRange);
1556 if (DagInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
1559 DagInit *I = new DagInit(V, VN, ArgRange, NameRange);
1560 ThePool.InsertNode(I, IP);
1566 DagInit::get(Init *V, const std::string &VN,
1567 const std::vector<std::pair<Init*, std::string> > &args) {
1568 typedef std::pair<Init*, std::string> PairType;
1570 std::vector<Init *> Args;
1571 std::vector<std::string> Names;
1573 for (std::vector<PairType>::const_iterator i = args.begin(),
1577 Args.push_back(i->first);
1578 Names.push_back(i->second);
1581 return DagInit::get(V, VN, Args, Names);
1584 void DagInit::Profile(FoldingSetNodeID &ID) const {
1585 ProfileDagInit(ID, Val, ValName, Args, ArgNames);
1588 Init *DagInit::resolveReferences(Record &R, const RecordVal *RV) const {
1589 std::vector<Init*> NewArgs;
1590 for (unsigned i = 0, e = Args.size(); i != e; ++i)
1591 NewArgs.push_back(Args[i]->resolveReferences(R, RV));
1593 Init *Op = Val->resolveReferences(R, RV);
1595 if (Args != NewArgs || Op != Val)
1596 return DagInit::get(Op, ValName, NewArgs, ArgNames);
1598 return const_cast<DagInit *>(this);
1602 std::string DagInit::getAsString() const {
1603 std::string Result = "(" + Val->getAsString();
1604 if (!ValName.empty())
1605 Result += ":" + ValName;
1607 Result += " " + Args[0]->getAsString();
1608 if (!ArgNames[0].empty()) Result += ":$" + ArgNames[0];
1609 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
1610 Result += ", " + Args[i]->getAsString();
1611 if (!ArgNames[i].empty()) Result += ":$" + ArgNames[i];
1614 return Result + ")";
1618 //===----------------------------------------------------------------------===//
1619 // Other implementations
1620 //===----------------------------------------------------------------------===//
1622 RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
1623 : Name(N), Ty(T), Prefix(P) {
1624 Value = Ty->convertValue(UnsetInit::get());
1625 assert(Value && "Cannot create unset value for current type!");
1628 void RecordVal::dump() const { errs() << *this; }
1630 void RecordVal::print(raw_ostream &OS, bool PrintSem) const {
1631 if (getPrefix()) OS << "field ";
1632 OS << *getType() << " " << getName();
1635 OS << " = " << *getValue();
1637 if (PrintSem) OS << ";\n";
1640 unsigned Record::LastID = 0;
1642 void Record::checkName() {
1643 // Ensure the record name has string type.
1644 const TypedInit *TypedName = dynamic_cast<const TypedInit *>(Name);
1645 assert(TypedName && "Record name is not typed!");
1646 RecTy *Type = TypedName->getType();
1647 if (dynamic_cast<StringRecTy *>(Type) == 0) {
1648 llvm_unreachable("Record name is not a string!");
1652 DefInit *Record::getDefInit() {
1654 TheInit = new DefInit(this, new RecordRecTy(this));
1658 const std::string &Record::getName() const {
1659 const StringInit *NameString =
1660 dynamic_cast<const StringInit *>(Name);
1661 assert(NameString && "Record name is not a string!");
1662 return NameString->getValue();
1665 void Record::setName(Init *NewName) {
1666 if (TrackedRecords.getDef(Name->getAsUnquotedString()) == this) {
1667 TrackedRecords.removeDef(Name->getAsUnquotedString());
1669 TrackedRecords.addDef(this);
1671 TrackedRecords.removeClass(Name->getAsUnquotedString());
1673 TrackedRecords.addClass(this);
1676 // Since the Init for the name was changed, see if we can resolve
1677 // any of it using members of the Record.
1678 Init *ComputedName = Name->resolveReferences(*this, 0);
1679 if (ComputedName != Name) {
1680 setName(ComputedName);
1682 // DO NOT resolve record values to the name at this point because
1683 // there might be default values for arguments of this def. Those
1684 // arguments might not have been resolved yet so we don't want to
1685 // prematurely assume values for those arguments were not passed to
1688 // Nonetheless, it may be that some of this Record's values
1689 // reference the record name. Indeed, the reason for having the
1690 // record name be an Init is to provide this flexibility. The extra
1691 // resolve steps after completely instantiating defs takes care of
1692 // this. See TGParser::ParseDef and TGParser::ParseDefm.
1695 void Record::setName(const std::string &Name) {
1696 setName(StringInit::get(Name));
1699 /// resolveReferencesTo - If anything in this record refers to RV, replace the
1700 /// reference to RV with the RHS of RV. If RV is null, we resolve all possible
1702 void Record::resolveReferencesTo(const RecordVal *RV) {
1703 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
1704 if (Init *V = Values[i].getValue())
1705 Values[i].setValue(V->resolveReferences(*this, RV));
1709 void Record::dump() const { errs() << *this; }
1711 raw_ostream &llvm::operator<<(raw_ostream &OS, const Record &R) {
1714 const std::vector<std::string> &TArgs = R.getTemplateArgs();
1715 if (!TArgs.empty()) {
1717 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1719 const RecordVal *RV = R.getValue(TArgs[i]);
1720 assert(RV && "Template argument record not found??");
1721 RV->print(OS, false);
1727 const std::vector<Record*> &SC = R.getSuperClasses();
1730 for (unsigned i = 0, e = SC.size(); i != e; ++i)
1731 OS << " " << SC[i]->getName();
1735 const std::vector<RecordVal> &Vals = R.getValues();
1736 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
1737 if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
1739 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
1740 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
1746 /// getValueInit - Return the initializer for a value with the specified name,
1747 /// or throw an exception if the field does not exist.
1749 Init *Record::getValueInit(StringRef FieldName) const {
1750 const RecordVal *R = getValue(FieldName);
1751 if (R == 0 || R->getValue() == 0)
1752 throw "Record `" + getName() + "' does not have a field named `" +
1753 FieldName.str() + "'!\n";
1754 return R->getValue();
1758 /// getValueAsString - This method looks up the specified field and returns its
1759 /// value as a string, throwing an exception if the field does not exist or if
1760 /// the value is not a string.
1762 std::string Record::getValueAsString(StringRef FieldName) const {
1763 const RecordVal *R = getValue(FieldName);
1764 if (R == 0 || R->getValue() == 0)
1765 throw "Record `" + getName() + "' does not have a field named `" +
1766 FieldName.str() + "'!\n";
1768 if (StringInit *SI = dynamic_cast<StringInit*>(R->getValue()))
1769 return SI->getValue();
1770 throw "Record `" + getName() + "', field `" + FieldName.str() +
1771 "' does not have a string initializer!";
1774 /// getValueAsBitsInit - This method looks up the specified field and returns
1775 /// its value as a BitsInit, throwing an exception if the field does not exist
1776 /// or if the value is not the right type.
1778 BitsInit *Record::getValueAsBitsInit(StringRef FieldName) const {
1779 const RecordVal *R = getValue(FieldName);
1780 if (R == 0 || R->getValue() == 0)
1781 throw "Record `" + getName() + "' does not have a field named `" +
1782 FieldName.str() + "'!\n";
1784 if (BitsInit *BI = dynamic_cast<BitsInit*>(R->getValue()))
1786 throw "Record `" + getName() + "', field `" + FieldName.str() +
1787 "' does not have a BitsInit initializer!";
1790 /// getValueAsListInit - This method looks up the specified field and returns
1791 /// its value as a ListInit, throwing an exception if the field does not exist
1792 /// or if the value is not the right type.
1794 ListInit *Record::getValueAsListInit(StringRef FieldName) const {
1795 const RecordVal *R = getValue(FieldName);
1796 if (R == 0 || R->getValue() == 0)
1797 throw "Record `" + getName() + "' does not have a field named `" +
1798 FieldName.str() + "'!\n";
1800 if (ListInit *LI = dynamic_cast<ListInit*>(R->getValue()))
1802 throw "Record `" + getName() + "', field `" + FieldName.str() +
1803 "' does not have a list initializer!";
1806 /// getValueAsListOfDefs - This method looks up the specified field and returns
1807 /// its value as a vector of records, throwing an exception if the field does
1808 /// not exist or if the value is not the right type.
1810 std::vector<Record*>
1811 Record::getValueAsListOfDefs(StringRef FieldName) const {
1812 ListInit *List = getValueAsListInit(FieldName);
1813 std::vector<Record*> Defs;
1814 for (unsigned i = 0; i < List->getSize(); i++) {
1815 if (DefInit *DI = dynamic_cast<DefInit*>(List->getElement(i))) {
1816 Defs.push_back(DI->getDef());
1818 throw "Record `" + getName() + "', field `" + FieldName.str() +
1819 "' list is not entirely DefInit!";
1825 /// getValueAsInt - This method looks up the specified field and returns its
1826 /// value as an int64_t, throwing an exception if the field does not exist or if
1827 /// the value is not the right type.
1829 int64_t Record::getValueAsInt(StringRef FieldName) const {
1830 const RecordVal *R = getValue(FieldName);
1831 if (R == 0 || R->getValue() == 0)
1832 throw "Record `" + getName() + "' does not have a field named `" +
1833 FieldName.str() + "'!\n";
1835 if (IntInit *II = dynamic_cast<IntInit*>(R->getValue()))
1836 return II->getValue();
1837 throw "Record `" + getName() + "', field `" + FieldName.str() +
1838 "' does not have an int initializer!";
1841 /// getValueAsListOfInts - This method looks up the specified field and returns
1842 /// its value as a vector of integers, throwing an exception if the field does
1843 /// not exist or if the value is not the right type.
1845 std::vector<int64_t>
1846 Record::getValueAsListOfInts(StringRef FieldName) const {
1847 ListInit *List = getValueAsListInit(FieldName);
1848 std::vector<int64_t> Ints;
1849 for (unsigned i = 0; i < List->getSize(); i++) {
1850 if (IntInit *II = dynamic_cast<IntInit*>(List->getElement(i))) {
1851 Ints.push_back(II->getValue());
1853 throw "Record `" + getName() + "', field `" + FieldName.str() +
1854 "' does not have a list of ints initializer!";
1860 /// getValueAsListOfStrings - This method looks up the specified field and
1861 /// returns its value as a vector of strings, throwing an exception if the
1862 /// field does not exist or if the value is not the right type.
1864 std::vector<std::string>
1865 Record::getValueAsListOfStrings(StringRef FieldName) const {
1866 ListInit *List = getValueAsListInit(FieldName);
1867 std::vector<std::string> Strings;
1868 for (unsigned i = 0; i < List->getSize(); i++) {
1869 if (StringInit *II = dynamic_cast<StringInit*>(List->getElement(i))) {
1870 Strings.push_back(II->getValue());
1872 throw "Record `" + getName() + "', field `" + FieldName.str() +
1873 "' does not have a list of strings initializer!";
1879 /// getValueAsDef - This method looks up the specified field and returns its
1880 /// value as a Record, throwing an exception if the field does not exist or if
1881 /// the value is not the right type.
1883 Record *Record::getValueAsDef(StringRef FieldName) const {
1884 const RecordVal *R = getValue(FieldName);
1885 if (R == 0 || R->getValue() == 0)
1886 throw "Record `" + getName() + "' does not have a field named `" +
1887 FieldName.str() + "'!\n";
1889 if (DefInit *DI = dynamic_cast<DefInit*>(R->getValue()))
1890 return DI->getDef();
1891 throw "Record `" + getName() + "', field `" + FieldName.str() +
1892 "' does not have a def initializer!";
1895 /// getValueAsBit - This method looks up the specified field and returns its
1896 /// value as a bit, throwing an exception if the field does not exist or if
1897 /// the value is not the right type.
1899 bool Record::getValueAsBit(StringRef FieldName) const {
1900 const RecordVal *R = getValue(FieldName);
1901 if (R == 0 || R->getValue() == 0)
1902 throw "Record `" + getName() + "' does not have a field named `" +
1903 FieldName.str() + "'!\n";
1905 if (BitInit *BI = dynamic_cast<BitInit*>(R->getValue()))
1906 return BI->getValue();
1907 throw "Record `" + getName() + "', field `" + FieldName.str() +
1908 "' does not have a bit initializer!";
1911 /// getValueAsDag - This method looks up the specified field and returns its
1912 /// value as an Dag, throwing an exception if the field does not exist or if
1913 /// the value is not the right type.
1915 DagInit *Record::getValueAsDag(StringRef FieldName) const {
1916 const RecordVal *R = getValue(FieldName);
1917 if (R == 0 || R->getValue() == 0)
1918 throw "Record `" + getName() + "' does not have a field named `" +
1919 FieldName.str() + "'!\n";
1921 if (DagInit *DI = dynamic_cast<DagInit*>(R->getValue()))
1923 throw "Record `" + getName() + "', field `" + FieldName.str() +
1924 "' does not have a dag initializer!";
1927 std::string Record::getValueAsCode(StringRef FieldName) const {
1928 const RecordVal *R = getValue(FieldName);
1929 if (R == 0 || R->getValue() == 0)
1930 throw "Record `" + getName() + "' does not have a field named `" +
1931 FieldName.str() + "'!\n";
1933 if (CodeInit *CI = dynamic_cast<CodeInit*>(R->getValue()))
1934 return CI->getValue();
1935 throw "Record `" + getName() + "', field `" + FieldName.str() +
1936 "' does not have a code initializer!";
1940 void MultiClass::dump() const {
1941 errs() << "Record:\n";
1944 errs() << "Defs:\n";
1945 for (RecordVector::const_iterator r = DefPrototypes.begin(),
1946 rend = DefPrototypes.end();
1954 void RecordKeeper::dump() const { errs() << *this; }
1956 raw_ostream &llvm::operator<<(raw_ostream &OS, const RecordKeeper &RK) {
1957 OS << "------------- Classes -----------------\n";
1958 const std::map<std::string, Record*> &Classes = RK.getClasses();
1959 for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
1960 E = Classes.end(); I != E; ++I)
1961 OS << "class " << *I->second;
1963 OS << "------------- Defs -----------------\n";
1964 const std::map<std::string, Record*> &Defs = RK.getDefs();
1965 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
1966 E = Defs.end(); I != E; ++I)
1967 OS << "def " << *I->second;
1972 /// getAllDerivedDefinitions - This method returns all concrete definitions
1973 /// that derive from the specified class name. If a class with the specified
1974 /// name does not exist, an error is printed and true is returned.
1975 std::vector<Record*>
1976 RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
1977 Record *Class = getClass(ClassName);
1979 throw "ERROR: Couldn't find the `" + ClassName + "' class!\n";
1981 std::vector<Record*> Defs;
1982 for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(),
1983 E = getDefs().end(); I != E; ++I)
1984 if (I->second->isSubClassOf(Class))
1985 Defs.push_back(I->second);