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 //===----------------------------------------------------------------------===//
14 #include "llvm/TableGen/Record.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/FoldingSet.h"
17 #include "llvm/ADT/Hashing.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/ADT/StringMap.h"
22 #include "llvm/Support/Compiler.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/TableGen/Error.h"
31 static BumpPtrAllocator Allocator;
33 //===----------------------------------------------------------------------===//
34 // Type implementations
35 //===----------------------------------------------------------------------===//
37 BitRecTy BitRecTy::Shared;
38 CodeRecTy CodeRecTy::Shared;
39 IntRecTy IntRecTy::Shared;
40 StringRecTy StringRecTy::Shared;
41 DagRecTy DagRecTy::Shared;
43 LLVM_DUMP_METHOD void RecTy::dump() const { print(errs()); }
45 ListRecTy *RecTy::getListTy() {
47 ListTy = new(Allocator) ListRecTy(this);
51 bool RecTy::typeIsConvertibleTo(const RecTy *RHS) const {
52 assert(RHS && "NULL pointer");
53 return Kind == RHS->getRecTyKind();
56 bool BitRecTy::typeIsConvertibleTo(const RecTy *RHS) const{
57 if (RecTy::typeIsConvertibleTo(RHS) || RHS->getRecTyKind() == IntRecTyKind)
59 if (const BitsRecTy *BitsTy = dyn_cast<BitsRecTy>(RHS))
60 return BitsTy->getNumBits() == 1;
64 BitsRecTy *BitsRecTy::get(unsigned Sz) {
65 static std::vector<BitsRecTy*> Shared;
66 if (Sz >= Shared.size())
67 Shared.resize(Sz + 1);
68 BitsRecTy *&Ty = Shared[Sz];
70 Ty = new(Allocator) BitsRecTy(Sz);
74 std::string BitsRecTy::getAsString() const {
75 return "bits<" + utostr(Size) + ">";
78 bool BitsRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
79 if (RecTy::typeIsConvertibleTo(RHS)) //argument and the sender are same type
80 return cast<BitsRecTy>(RHS)->Size == Size;
81 RecTyKind kind = RHS->getRecTyKind();
82 return (kind == BitRecTyKind && Size == 1) || (kind == IntRecTyKind);
85 bool IntRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
86 RecTyKind kind = RHS->getRecTyKind();
87 return kind==BitRecTyKind || kind==BitsRecTyKind || kind==IntRecTyKind;
90 std::string StringRecTy::getAsString() const {
94 std::string ListRecTy::getAsString() const {
95 return "list<" + Ty->getAsString() + ">";
98 bool ListRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
99 if (const auto *ListTy = dyn_cast<ListRecTy>(RHS))
100 return Ty->typeIsConvertibleTo(ListTy->getElementType());
104 std::string DagRecTy::getAsString() const {
108 RecordRecTy *RecordRecTy::get(Record *R) {
109 return dyn_cast<RecordRecTy>(R->getDefInit()->getType());
112 std::string RecordRecTy::getAsString() const {
113 return Rec->getName();
116 bool RecordRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
117 const RecordRecTy *RTy = dyn_cast<RecordRecTy>(RHS);
121 if (RTy->getRecord() == Rec || Rec->isSubClassOf(RTy->getRecord()))
124 for (const auto &SCPair : RTy->getRecord()->getSuperClasses())
125 if (Rec->isSubClassOf(SCPair.first))
131 RecTy *llvm::resolveTypes(RecTy *T1, RecTy *T2) {
132 if (T1->typeIsConvertibleTo(T2))
134 if (T2->typeIsConvertibleTo(T1))
137 // If one is a Record type, check superclasses
138 if (RecordRecTy *RecTy1 = dyn_cast<RecordRecTy>(T1)) {
139 // See if T2 inherits from a type T1 also inherits from
140 for (const auto &SuperPair1 : RecTy1->getRecord()->getSuperClasses()) {
141 RecordRecTy *SuperRecTy1 = RecordRecTy::get(SuperPair1.first);
142 RecTy *NewType1 = resolveTypes(SuperRecTy1, T2);
147 if (RecordRecTy *RecTy2 = dyn_cast<RecordRecTy>(T2)) {
148 // See if T1 inherits from a type T2 also inherits from
149 for (const auto &SuperPair2 : RecTy2->getRecord()->getSuperClasses()) {
150 RecordRecTy *SuperRecTy2 = RecordRecTy::get(SuperPair2.first);
151 RecTy *NewType2 = resolveTypes(T1, SuperRecTy2);
159 //===----------------------------------------------------------------------===//
160 // Initializer implementations
161 //===----------------------------------------------------------------------===//
163 void Init::anchor() { }
164 LLVM_DUMP_METHOD void Init::dump() const { return print(errs()); }
166 UnsetInit *UnsetInit::get() {
167 static UnsetInit TheInit;
171 Init *UnsetInit::convertInitializerTo(RecTy *Ty) const {
172 if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
173 SmallVector<Init *, 16> NewBits(BRT->getNumBits());
175 for (unsigned i = 0; i != BRT->getNumBits(); ++i)
176 NewBits[i] = UnsetInit::get();
178 return BitsInit::get(NewBits);
181 // All other types can just be returned.
182 return const_cast<UnsetInit *>(this);
185 BitInit *BitInit::get(bool V) {
186 static BitInit True(true);
187 static BitInit False(false);
189 return V ? &True : &False;
192 Init *BitInit::convertInitializerTo(RecTy *Ty) const {
193 if (isa<BitRecTy>(Ty))
194 return const_cast<BitInit *>(this);
196 if (isa<IntRecTy>(Ty))
197 return IntInit::get(getValue());
199 if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
200 // Can only convert single bit.
201 if (BRT->getNumBits() == 1)
202 return BitsInit::get(const_cast<BitInit *>(this));
209 ProfileBitsInit(FoldingSetNodeID &ID, ArrayRef<Init *> Range) {
210 ID.AddInteger(Range.size());
212 for (Init *I : Range)
216 BitsInit *BitsInit::get(ArrayRef<Init *> Range) {
217 static FoldingSet<BitsInit> ThePool;
218 static std::vector<BitsInit*> TheActualPool;
221 ProfileBitsInit(ID, Range);
224 if (BitsInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
227 void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *>(Range.size()),
229 BitsInit *I = new(Mem) BitsInit(Range.size());
230 std::uninitialized_copy(Range.begin(), Range.end(),
231 I->getTrailingObjects<Init *>());
232 ThePool.InsertNode(I, IP);
233 TheActualPool.push_back(I);
237 void BitsInit::Profile(FoldingSetNodeID &ID) const {
238 ProfileBitsInit(ID, makeArrayRef(getTrailingObjects<Init *>(), NumBits));
241 Init *BitsInit::convertInitializerTo(RecTy *Ty) const {
242 if (isa<BitRecTy>(Ty)) {
243 if (getNumBits() != 1) return nullptr; // Only accept if just one bit!
247 if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
248 // If the number of bits is right, return it. Otherwise we need to expand
250 if (getNumBits() != BRT->getNumBits()) return nullptr;
251 return const_cast<BitsInit *>(this);
254 if (isa<IntRecTy>(Ty)) {
256 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
257 if (auto *Bit = dyn_cast<BitInit>(getBit(i)))
258 Result |= static_cast<int64_t>(Bit->getValue()) << i;
261 return IntInit::get(Result);
268 BitsInit::convertInitializerBitRange(ArrayRef<unsigned> Bits) const {
269 SmallVector<Init *, 16> NewBits(Bits.size());
271 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
272 if (Bits[i] >= getNumBits())
274 NewBits[i] = getBit(Bits[i]);
276 return BitsInit::get(NewBits);
279 std::string BitsInit::getAsString() const {
280 std::string Result = "{ ";
281 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
282 if (i) Result += ", ";
283 if (Init *Bit = getBit(e-i-1))
284 Result += Bit->getAsString();
288 return Result + " }";
291 // Fix bit initializer to preserve the behavior that bit reference from a unset
292 // bits initializer will resolve into VarBitInit to keep the field name and bit
293 // number used in targets with fixed insn length.
294 static Init *fixBitInit(const RecordVal *RV, Init *Before, Init *After) {
295 if (RV || !isa<UnsetInit>(After))
300 // resolveReferences - If there are any field references that refer to fields
301 // that have been filled in, we can propagate the values now.
303 Init *BitsInit::resolveReferences(Record &R, const RecordVal *RV) const {
304 bool Changed = false;
305 SmallVector<Init *, 16> NewBits(getNumBits());
307 Init *CachedInit = nullptr;
308 Init *CachedBitVar = nullptr;
309 bool CachedBitVarChanged = false;
311 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
312 Init *CurBit = getBit(i);
313 Init *CurBitVar = CurBit->getBitVar();
317 if (CurBitVar == CachedBitVar) {
318 if (CachedBitVarChanged) {
319 Init *Bit = CachedInit->getBit(CurBit->getBitNum());
320 NewBits[i] = fixBitInit(RV, CurBit, Bit);
324 CachedBitVar = CurBitVar;
325 CachedBitVarChanged = false;
330 CurBitVar = CurBitVar->resolveReferences(R, RV);
331 CachedBitVarChanged |= B != CurBitVar;
332 Changed |= B != CurBitVar;
333 } while (B != CurBitVar);
334 CachedInit = CurBitVar;
336 if (CachedBitVarChanged) {
337 Init *Bit = CurBitVar->getBit(CurBit->getBitNum());
338 NewBits[i] = fixBitInit(RV, CurBit, Bit);
343 return BitsInit::get(NewBits);
345 return const_cast<BitsInit *>(this);
348 IntInit *IntInit::get(int64_t V) {
349 static DenseMap<int64_t, IntInit*> ThePool;
351 IntInit *&I = ThePool[V];
352 if (!I) I = new(Allocator) IntInit(V);
356 std::string IntInit::getAsString() const {
357 return itostr(Value);
360 static bool canFitInBitfield(int64_t Value, unsigned NumBits) {
361 // For example, with NumBits == 4, we permit Values from [-7 .. 15].
362 return (NumBits >= sizeof(Value) * 8) ||
363 (Value >> NumBits == 0) || (Value >> (NumBits-1) == -1);
366 Init *IntInit::convertInitializerTo(RecTy *Ty) const {
367 if (isa<IntRecTy>(Ty))
368 return const_cast<IntInit *>(this);
370 if (isa<BitRecTy>(Ty)) {
371 int64_t Val = getValue();
372 if (Val != 0 && Val != 1) return nullptr; // Only accept 0 or 1 for a bit!
373 return BitInit::get(Val != 0);
376 if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
377 int64_t Value = getValue();
378 // Make sure this bitfield is large enough to hold the integer value.
379 if (!canFitInBitfield(Value, BRT->getNumBits()))
382 SmallVector<Init *, 16> NewBits(BRT->getNumBits());
383 for (unsigned i = 0; i != BRT->getNumBits(); ++i)
384 NewBits[i] = BitInit::get(Value & (1LL << i));
386 return BitsInit::get(NewBits);
393 IntInit::convertInitializerBitRange(ArrayRef<unsigned> Bits) const {
394 SmallVector<Init *, 16> NewBits(Bits.size());
396 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
400 NewBits[i] = BitInit::get(Value & (INT64_C(1) << Bits[i]));
402 return BitsInit::get(NewBits);
405 CodeInit *CodeInit::get(StringRef V) {
406 static DenseMap<StringRef, CodeInit*> ThePool;
408 auto I = ThePool.insert(std::make_pair(V, nullptr));
410 StringRef VCopy = V.copy(Allocator);
411 I.first->first = VCopy;
412 I.first->second = new(Allocator) CodeInit(VCopy);
414 return I.first->second;
417 StringInit *StringInit::get(StringRef V) {
418 static DenseMap<StringRef, StringInit*> ThePool;
420 auto I = ThePool.insert(std::make_pair(V, nullptr));
422 StringRef VCopy = V.copy(Allocator);
423 I.first->first = VCopy;
424 I.first->second = new(Allocator) StringInit(VCopy);
426 return I.first->second;
429 Init *StringInit::convertInitializerTo(RecTy *Ty) const {
430 if (isa<StringRecTy>(Ty))
431 return const_cast<StringInit *>(this);
436 Init *CodeInit::convertInitializerTo(RecTy *Ty) const {
437 if (isa<CodeRecTy>(Ty))
438 return const_cast<CodeInit *>(this);
443 static void ProfileListInit(FoldingSetNodeID &ID,
444 ArrayRef<Init *> Range,
446 ID.AddInteger(Range.size());
447 ID.AddPointer(EltTy);
449 for (Init *I : Range)
453 ListInit *ListInit::get(ArrayRef<Init *> Range, RecTy *EltTy) {
454 static FoldingSet<ListInit> ThePool;
455 static std::vector<ListInit*> TheActualPool;
458 ProfileListInit(ID, Range, EltTy);
461 if (ListInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
464 void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *>(Range.size()),
466 ListInit *I = new(Mem) ListInit(Range.size(), EltTy);
467 std::uninitialized_copy(Range.begin(), Range.end(),
468 I->getTrailingObjects<Init *>());
469 ThePool.InsertNode(I, IP);
470 TheActualPool.push_back(I);
474 void ListInit::Profile(FoldingSetNodeID &ID) const {
475 RecTy *EltTy = cast<ListRecTy>(getType())->getElementType();
477 ProfileListInit(ID, getValues(), EltTy);
480 Init *ListInit::convertInitializerTo(RecTy *Ty) const {
482 return const_cast<ListInit*>(this);
484 if (auto *LRT = dyn_cast<ListRecTy>(Ty)) {
485 SmallVector<Init*, 8> Elements;
486 Elements.reserve(getValues().size());
488 // Verify that all of the elements of the list are subclasses of the
489 // appropriate class!
490 bool Changed = false;
491 RecTy *ElementType = LRT->getElementType();
492 for (Init *I : getValues())
493 if (Init *CI = I->convertInitializerTo(ElementType)) {
494 Elements.push_back(CI);
501 return const_cast<ListInit*>(this);
502 return ListInit::get(Elements, Ty);
508 Init *ListInit::convertInitListSlice(ArrayRef<unsigned> Elements) const {
509 SmallVector<Init*, 8> Vals;
510 Vals.reserve(Elements.size());
511 for (unsigned Element : Elements) {
512 if (Element >= size())
514 Vals.push_back(getElement(Element));
516 return ListInit::get(Vals, getType());
519 Record *ListInit::getElementAsRecord(unsigned i) const {
520 assert(i < NumValues && "List element index out of range!");
521 DefInit *DI = dyn_cast<DefInit>(getElement(i));
523 PrintFatalError("Expected record in list!");
527 Init *ListInit::resolveReferences(Record &R, const RecordVal *RV) const {
528 SmallVector<Init*, 8> Resolved;
529 Resolved.reserve(size());
530 bool Changed = false;
532 for (Init *CurElt : getValues()) {
537 CurElt = CurElt->resolveReferences(R, RV);
538 Changed |= E != CurElt;
539 } while (E != CurElt);
540 Resolved.push_back(E);
544 return ListInit::get(Resolved, getType());
545 return const_cast<ListInit *>(this);
548 Init *ListInit::resolveListElementReference(Record &R, const RecordVal *IRV,
549 unsigned Elt) const {
551 return nullptr; // Out of range reference.
552 Init *E = getElement(Elt);
553 // If the element is set to some value, or if we are resolving a reference
554 // to a specific variable and that variable is explicitly unset, then
555 // replace the VarListElementInit with it.
556 if (IRV || !isa<UnsetInit>(E))
561 std::string ListInit::getAsString() const {
562 std::string Result = "[";
563 const char *sep = "";
564 for (Init *Element : *this) {
567 Result += Element->getAsString();
572 Init *OpInit::resolveListElementReference(Record &R, const RecordVal *IRV,
573 unsigned Elt) const {
574 Init *Resolved = resolveReferences(R, IRV);
575 OpInit *OResolved = dyn_cast<OpInit>(Resolved);
577 Resolved = OResolved->Fold(&R, nullptr);
580 if (Resolved != this) {
581 TypedInit *Typed = cast<TypedInit>(Resolved);
582 if (Init *New = Typed->resolveListElementReference(R, IRV, Elt))
584 return VarListElementInit::get(Typed, Elt);
590 Init *OpInit::getBit(unsigned Bit) const {
591 if (getType() == BitRecTy::get())
592 return const_cast<OpInit*>(this);
593 return VarBitInit::get(const_cast<OpInit*>(this), Bit);
597 ProfileUnOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *Op, RecTy *Type) {
598 ID.AddInteger(Opcode);
603 UnOpInit *UnOpInit::get(UnaryOp Opc, Init *LHS, RecTy *Type) {
604 static FoldingSet<UnOpInit> ThePool;
605 static std::vector<UnOpInit*> TheActualPool;
608 ProfileUnOpInit(ID, Opc, LHS, Type);
611 if (UnOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
614 UnOpInit *I = new(Allocator) UnOpInit(Opc, LHS, Type);
615 ThePool.InsertNode(I, IP);
616 TheActualPool.push_back(I);
620 void UnOpInit::Profile(FoldingSetNodeID &ID) const {
621 ProfileUnOpInit(ID, getOpcode(), getOperand(), getType());
624 Init *UnOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const {
625 switch (getOpcode()) {
627 if (isa<StringRecTy>(getType())) {
628 if (StringInit *LHSs = dyn_cast<StringInit>(LHS))
631 if (DefInit *LHSd = dyn_cast<DefInit>(LHS))
632 return StringInit::get(LHSd->getAsString());
634 if (IntInit *LHSi = dyn_cast<IntInit>(LHS))
635 return StringInit::get(LHSi->getAsString());
637 if (StringInit *Name = dyn_cast<StringInit>(LHS)) {
638 // From TGParser::ParseIDValue
640 if (const RecordVal *RV = CurRec->getValue(Name)) {
641 if (RV->getType() != getType())
642 PrintFatalError("type mismatch in cast");
643 return VarInit::get(Name, RV->getType());
646 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name,
649 if (CurRec->isTemplateArg(TemplateArgName)) {
650 const RecordVal *RV = CurRec->getValue(TemplateArgName);
651 assert(RV && "Template arg doesn't exist??");
653 if (RV->getType() != getType())
654 PrintFatalError("type mismatch in cast");
656 return VarInit::get(TemplateArgName, RV->getType());
661 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
664 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
665 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
666 assert(RV && "Template arg doesn't exist??");
668 if (RV->getType() != getType())
669 PrintFatalError("type mismatch in cast");
671 return VarInit::get(MCName, RV->getType());
674 assert(CurRec && "NULL pointer");
675 if (Record *D = (CurRec->getRecords()).getDef(Name->getValue()))
676 return DefInit::get(D);
678 PrintFatalError(CurRec->getLoc(),
679 "Undefined reference:'" + Name->getValue() + "'\n");
682 if (isa<IntRecTy>(getType())) {
683 if (BitsInit *BI = dyn_cast<BitsInit>(LHS)) {
684 if (Init *NewInit = BI->convertInitializerTo(IntRecTy::get()))
693 if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) {
694 assert(!LHSl->empty() && "Empty list in head");
695 return LHSl->getElement(0);
700 if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) {
701 assert(!LHSl->empty() && "Empty list in tail");
702 // Note the +1. We can't just pass the result of getValues()
704 return ListInit::get(LHSl->getValues().slice(1), LHSl->getType());
709 if (ListInit *LHSl = dyn_cast<ListInit>(LHS))
710 return IntInit::get(LHSl->empty());
711 if (StringInit *LHSs = dyn_cast<StringInit>(LHS))
712 return IntInit::get(LHSs->getValue().empty());
717 return const_cast<UnOpInit *>(this);
720 Init *UnOpInit::resolveReferences(Record &R, const RecordVal *RV) const {
721 Init *lhs = LHS->resolveReferences(R, RV);
724 return (UnOpInit::get(getOpcode(), lhs, getType()))->Fold(&R, nullptr);
725 return Fold(&R, nullptr);
728 std::string UnOpInit::getAsString() const {
730 switch (getOpcode()) {
731 case CAST: Result = "!cast<" + getType()->getAsString() + ">"; break;
732 case HEAD: Result = "!head"; break;
733 case TAIL: Result = "!tail"; break;
734 case EMPTY: Result = "!empty"; break;
736 return Result + "(" + LHS->getAsString() + ")";
740 ProfileBinOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *LHS, Init *RHS,
742 ID.AddInteger(Opcode);
748 BinOpInit *BinOpInit::get(BinaryOp Opc, Init *LHS,
749 Init *RHS, RecTy *Type) {
750 static FoldingSet<BinOpInit> ThePool;
751 static std::vector<BinOpInit*> TheActualPool;
754 ProfileBinOpInit(ID, Opc, LHS, RHS, Type);
757 if (BinOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
760 BinOpInit *I = new(Allocator) BinOpInit(Opc, LHS, RHS, Type);
761 ThePool.InsertNode(I, IP);
762 TheActualPool.push_back(I);
766 void BinOpInit::Profile(FoldingSetNodeID &ID) const {
767 ProfileBinOpInit(ID, getOpcode(), getLHS(), getRHS(), getType());
770 static StringInit *ConcatStringInits(const StringInit *I0,
771 const StringInit *I1) {
772 SmallString<80> Concat(I0->getValue());
773 Concat.append(I1->getValue());
774 return StringInit::get(Concat);
777 Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const {
778 switch (getOpcode()) {
780 DagInit *LHSs = dyn_cast<DagInit>(LHS);
781 DagInit *RHSs = dyn_cast<DagInit>(RHS);
783 DefInit *LOp = dyn_cast<DefInit>(LHSs->getOperator());
784 DefInit *ROp = dyn_cast<DefInit>(RHSs->getOperator());
785 if (!LOp || !ROp || LOp->getDef() != ROp->getDef())
786 PrintFatalError("Concated Dag operators do not match!");
787 SmallVector<Init*, 8> Args;
788 SmallVector<StringInit*, 8> ArgNames;
789 for (unsigned i = 0, e = LHSs->getNumArgs(); i != e; ++i) {
790 Args.push_back(LHSs->getArg(i));
791 ArgNames.push_back(LHSs->getArgName(i));
793 for (unsigned i = 0, e = RHSs->getNumArgs(); i != e; ++i) {
794 Args.push_back(RHSs->getArg(i));
795 ArgNames.push_back(RHSs->getArgName(i));
797 return DagInit::get(LHSs->getOperator(), nullptr, Args, ArgNames);
802 ListInit *LHSs = dyn_cast<ListInit>(LHS);
803 ListInit *RHSs = dyn_cast<ListInit>(RHS);
805 SmallVector<Init *, 8> Args;
806 Args.insert(Args.end(), LHSs->begin(), LHSs->end());
807 Args.insert(Args.end(), RHSs->begin(), RHSs->end());
808 return ListInit::get(
809 Args, cast<ListRecTy>(LHSs->getType())->getElementType());
814 StringInit *LHSs = dyn_cast<StringInit>(LHS);
815 StringInit *RHSs = dyn_cast<StringInit>(RHS);
817 return ConcatStringInits(LHSs, RHSs);
821 // try to fold eq comparison for 'bit' and 'int', otherwise fallback
822 // to string objects.
824 dyn_cast_or_null<IntInit>(LHS->convertInitializerTo(IntRecTy::get()));
826 dyn_cast_or_null<IntInit>(RHS->convertInitializerTo(IntRecTy::get()));
829 return IntInit::get(L->getValue() == R->getValue());
831 StringInit *LHSs = dyn_cast<StringInit>(LHS);
832 StringInit *RHSs = dyn_cast<StringInit>(RHS);
834 // Make sure we've resolved
836 return IntInit::get(LHSs->getValue() == RHSs->getValue());
847 dyn_cast_or_null<IntInit>(LHS->convertInitializerTo(IntRecTy::get()));
849 dyn_cast_or_null<IntInit>(RHS->convertInitializerTo(IntRecTy::get()));
851 int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
853 switch (getOpcode()) {
854 default: llvm_unreachable("Bad opcode!");
855 case ADD: Result = LHSv + RHSv; break;
856 case AND: Result = LHSv & RHSv; break;
857 case OR: Result = LHSv | RHSv; break;
858 case SHL: Result = LHSv << RHSv; break;
859 case SRA: Result = LHSv >> RHSv; break;
860 case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break;
862 return IntInit::get(Result);
867 return const_cast<BinOpInit *>(this);
870 Init *BinOpInit::resolveReferences(Record &R, const RecordVal *RV) const {
871 Init *lhs = LHS->resolveReferences(R, RV);
872 Init *rhs = RHS->resolveReferences(R, RV);
874 if (LHS != lhs || RHS != rhs)
875 return (BinOpInit::get(getOpcode(), lhs, rhs, getType()))->Fold(&R,nullptr);
876 return Fold(&R, nullptr);
879 std::string BinOpInit::getAsString() const {
881 switch (getOpcode()) {
882 case CONCAT: Result = "!con"; break;
883 case ADD: Result = "!add"; break;
884 case AND: Result = "!and"; break;
885 case OR: Result = "!or"; break;
886 case SHL: Result = "!shl"; break;
887 case SRA: Result = "!sra"; break;
888 case SRL: Result = "!srl"; break;
889 case EQ: Result = "!eq"; break;
890 case LISTCONCAT: Result = "!listconcat"; break;
891 case STRCONCAT: Result = "!strconcat"; break;
893 return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")";
897 ProfileTernOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *LHS, Init *MHS,
898 Init *RHS, RecTy *Type) {
899 ID.AddInteger(Opcode);
906 TernOpInit *TernOpInit::get(TernaryOp Opc, Init *LHS, Init *MHS, Init *RHS,
908 static FoldingSet<TernOpInit> ThePool;
909 static std::vector<TernOpInit*> TheActualPool;
912 ProfileTernOpInit(ID, Opc, LHS, MHS, RHS, Type);
915 if (TernOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
918 TernOpInit *I = new(Allocator) TernOpInit(Opc, LHS, MHS, RHS, Type);
919 ThePool.InsertNode(I, IP);
920 TheActualPool.push_back(I);
924 void TernOpInit::Profile(FoldingSetNodeID &ID) const {
925 ProfileTernOpInit(ID, getOpcode(), getLHS(), getMHS(), getRHS(), getType());
928 static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
929 Record *CurRec, MultiClass *CurMultiClass);
931 static Init *EvaluateOperation(OpInit *RHSo, Init *LHS, Init *Arg,
932 RecTy *Type, Record *CurRec,
933 MultiClass *CurMultiClass) {
934 // If this is a dag, recurse
935 if (auto *TArg = dyn_cast<TypedInit>(Arg))
936 if (isa<DagRecTy>(TArg->getType()))
937 return ForeachHelper(LHS, Arg, RHSo, Type, CurRec, CurMultiClass);
939 SmallVector<Init *, 8> NewOperands;
940 NewOperands.reserve(RHSo->getNumOperands());
941 for (unsigned i = 0, e = RHSo->getNumOperands(); i < e; ++i) {
942 if (auto *RHSoo = dyn_cast<OpInit>(RHSo->getOperand(i))) {
943 if (Init *Result = EvaluateOperation(RHSoo, LHS, Arg,
944 Type, CurRec, CurMultiClass))
945 NewOperands.push_back(Result);
947 NewOperands.push_back(Arg);
948 } else if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
949 NewOperands.push_back(Arg);
951 NewOperands.push_back(RHSo->getOperand(i));
955 // Now run the operator and use its result as the new leaf
956 const OpInit *NewOp = RHSo->clone(NewOperands);
957 Init *NewVal = NewOp->Fold(CurRec, CurMultiClass);
958 return (NewVal != NewOp) ? NewVal : nullptr;
961 static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
962 Record *CurRec, MultiClass *CurMultiClass) {
964 OpInit *RHSo = dyn_cast<OpInit>(RHS);
967 PrintFatalError(CurRec->getLoc(), "!foreach requires an operator\n");
969 TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
972 PrintFatalError(CurRec->getLoc(), "!foreach requires typed variable\n");
974 DagInit *MHSd = dyn_cast<DagInit>(MHS);
975 if (MHSd && isa<DagRecTy>(Type)) {
976 Init *Val = MHSd->getOperator();
977 if (Init *Result = EvaluateOperation(RHSo, LHS, Val,
978 Type, CurRec, CurMultiClass))
981 SmallVector<std::pair<Init *, StringInit*>, 8> args;
982 for (unsigned int i = 0; i < MHSd->getNumArgs(); ++i) {
983 Init *Arg = MHSd->getArg(i);
984 StringInit *ArgName = MHSd->getArgName(i);
987 if (Init *Result = EvaluateOperation(RHSo, LHS, Arg, Type,
988 CurRec, CurMultiClass))
991 // TODO: Process arg names
992 args.push_back(std::make_pair(Arg, ArgName));
995 return DagInit::get(Val, nullptr, args);
998 ListInit *MHSl = dyn_cast<ListInit>(MHS);
999 if (MHSl && isa<ListRecTy>(Type)) {
1000 SmallVector<Init *, 8> NewOperands;
1001 SmallVector<Init *, 8> NewList(MHSl->begin(), MHSl->end());
1003 for (Init *&Item : NewList) {
1004 NewOperands.clear();
1005 for(unsigned i = 0; i < RHSo->getNumOperands(); ++i) {
1006 // First, replace the foreach variable with the list item
1007 if (LHS->getAsString() == RHSo->getOperand(i)->getAsString())
1008 NewOperands.push_back(Item);
1010 NewOperands.push_back(RHSo->getOperand(i));
1013 // Now run the operator and use its result as the new list item
1014 const OpInit *NewOp = RHSo->clone(NewOperands);
1015 Init *NewItem = NewOp->Fold(CurRec, CurMultiClass);
1016 if (NewItem != NewOp)
1019 return ListInit::get(NewList, MHSl->getType());
1024 Init *TernOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const {
1025 switch (getOpcode()) {
1027 DefInit *LHSd = dyn_cast<DefInit>(LHS);
1028 VarInit *LHSv = dyn_cast<VarInit>(LHS);
1029 StringInit *LHSs = dyn_cast<StringInit>(LHS);
1031 DefInit *MHSd = dyn_cast<DefInit>(MHS);
1032 VarInit *MHSv = dyn_cast<VarInit>(MHS);
1033 StringInit *MHSs = dyn_cast<StringInit>(MHS);
1035 DefInit *RHSd = dyn_cast<DefInit>(RHS);
1036 VarInit *RHSv = dyn_cast<VarInit>(RHS);
1037 StringInit *RHSs = dyn_cast<StringInit>(RHS);
1039 if (LHSd && MHSd && RHSd) {
1040 Record *Val = RHSd->getDef();
1041 if (LHSd->getAsString() == RHSd->getAsString())
1042 Val = MHSd->getDef();
1043 return DefInit::get(Val);
1045 if (LHSv && MHSv && RHSv) {
1046 std::string Val = RHSv->getName();
1047 if (LHSv->getAsString() == RHSv->getAsString())
1048 Val = MHSv->getName();
1049 return VarInit::get(Val, getType());
1051 if (LHSs && MHSs && RHSs) {
1052 std::string Val = RHSs->getValue();
1054 std::string::size_type found;
1055 std::string::size_type idx = 0;
1057 found = Val.find(LHSs->getValue(), idx);
1058 if (found == std::string::npos)
1060 Val.replace(found, LHSs->getValue().size(), MHSs->getValue());
1061 idx = found + MHSs->getValue().size();
1064 return StringInit::get(Val);
1070 if (Init *Result = ForeachHelper(LHS, MHS, RHS, getType(),
1071 CurRec, CurMultiClass))
1077 IntInit *LHSi = dyn_cast<IntInit>(LHS);
1078 if (Init *I = LHS->convertInitializerTo(IntRecTy::get()))
1079 LHSi = dyn_cast<IntInit>(I);
1081 if (LHSi->getValue())
1089 return const_cast<TernOpInit *>(this);
1092 Init *TernOpInit::resolveReferences(Record &R,
1093 const RecordVal *RV) const {
1094 Init *lhs = LHS->resolveReferences(R, RV);
1096 if (getOpcode() == IF && lhs != LHS) {
1097 IntInit *Value = dyn_cast<IntInit>(lhs);
1098 if (Init *I = lhs->convertInitializerTo(IntRecTy::get()))
1099 Value = dyn_cast<IntInit>(I);
1102 if (Value->getValue()) {
1103 Init *mhs = MHS->resolveReferences(R, RV);
1104 return (TernOpInit::get(getOpcode(), lhs, mhs,
1105 RHS, getType()))->Fold(&R, nullptr);
1107 Init *rhs = RHS->resolveReferences(R, RV);
1108 return (TernOpInit::get(getOpcode(), lhs, MHS,
1109 rhs, getType()))->Fold(&R, nullptr);
1113 Init *mhs = MHS->resolveReferences(R, RV);
1114 Init *rhs = RHS->resolveReferences(R, RV);
1116 if (LHS != lhs || MHS != mhs || RHS != rhs)
1117 return (TernOpInit::get(getOpcode(), lhs, mhs, rhs,
1118 getType()))->Fold(&R, nullptr);
1119 return Fold(&R, nullptr);
1122 std::string TernOpInit::getAsString() const {
1124 switch (getOpcode()) {
1125 case SUBST: Result = "!subst"; break;
1126 case FOREACH: Result = "!foreach"; break;
1127 case IF: Result = "!if"; break;
1129 return Result + "(" + LHS->getAsString() + ", " + MHS->getAsString() + ", " +
1130 RHS->getAsString() + ")";
1133 RecTy *TypedInit::getFieldType(StringInit *FieldName) const {
1134 if (RecordRecTy *RecordType = dyn_cast<RecordRecTy>(getType()))
1135 if (RecordVal *Field = RecordType->getRecord()->getValue(FieldName))
1136 return Field->getType();
1141 TypedInit::convertInitializerTo(RecTy *Ty) const {
1142 if (isa<IntRecTy>(Ty)) {
1143 if (getType()->typeIsConvertibleTo(Ty))
1144 return const_cast<TypedInit *>(this);
1148 if (isa<StringRecTy>(Ty)) {
1149 if (isa<StringRecTy>(getType()))
1150 return const_cast<TypedInit *>(this);
1154 if (isa<CodeRecTy>(Ty)) {
1155 if (isa<CodeRecTy>(getType()))
1156 return const_cast<TypedInit *>(this);
1160 if (isa<BitRecTy>(Ty)) {
1161 // Accept variable if it is already of bit type!
1162 if (isa<BitRecTy>(getType()))
1163 return const_cast<TypedInit *>(this);
1164 if (auto *BitsTy = dyn_cast<BitsRecTy>(getType())) {
1165 // Accept only bits<1> expression.
1166 if (BitsTy->getNumBits() == 1)
1167 return const_cast<TypedInit *>(this);
1170 // Ternary !if can be converted to bit, but only if both sides are
1171 // convertible to a bit.
1172 if (const auto *TOI = dyn_cast<TernOpInit>(this)) {
1173 if (TOI->getOpcode() == TernOpInit::TernaryOp::IF &&
1174 TOI->getMHS()->convertInitializerTo(BitRecTy::get()) &&
1175 TOI->getRHS()->convertInitializerTo(BitRecTy::get()))
1176 return const_cast<TypedInit *>(this);
1182 if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
1183 if (BRT->getNumBits() == 1 && isa<BitRecTy>(getType()))
1184 return BitsInit::get(const_cast<TypedInit *>(this));
1186 if (getType()->typeIsConvertibleTo(BRT)) {
1187 SmallVector<Init *, 16> NewBits(BRT->getNumBits());
1189 for (unsigned i = 0; i != BRT->getNumBits(); ++i)
1190 NewBits[i] = VarBitInit::get(const_cast<TypedInit *>(this), i);
1191 return BitsInit::get(NewBits);
1197 if (auto *DLRT = dyn_cast<ListRecTy>(Ty)) {
1198 if (auto *SLRT = dyn_cast<ListRecTy>(getType()))
1199 if (SLRT->getElementType()->typeIsConvertibleTo(DLRT->getElementType()))
1200 return const_cast<TypedInit *>(this);
1204 if (auto *DRT = dyn_cast<DagRecTy>(Ty)) {
1205 if (getType()->typeIsConvertibleTo(DRT))
1206 return const_cast<TypedInit *>(this);
1210 if (auto *SRRT = dyn_cast<RecordRecTy>(Ty)) {
1211 // Ensure that this is compatible with Rec.
1212 if (RecordRecTy *DRRT = dyn_cast<RecordRecTy>(getType()))
1213 if (DRRT->getRecord()->isSubClassOf(SRRT->getRecord()) ||
1214 DRRT->getRecord() == SRRT->getRecord())
1215 return const_cast<TypedInit *>(this);
1222 Init *TypedInit::convertInitializerBitRange(ArrayRef<unsigned> Bits) const {
1223 BitsRecTy *T = dyn_cast<BitsRecTy>(getType());
1224 if (!T) return nullptr; // Cannot subscript a non-bits variable.
1225 unsigned NumBits = T->getNumBits();
1227 SmallVector<Init *, 16> NewBits;
1228 NewBits.reserve(Bits.size());
1229 for (unsigned Bit : Bits) {
1233 NewBits.push_back(VarBitInit::get(const_cast<TypedInit *>(this), Bit));
1235 return BitsInit::get(NewBits);
1238 Init *TypedInit::convertInitListSlice(ArrayRef<unsigned> Elements) const {
1239 ListRecTy *T = dyn_cast<ListRecTy>(getType());
1240 if (!T) return nullptr; // Cannot subscript a non-list variable.
1242 if (Elements.size() == 1)
1243 return VarListElementInit::get(const_cast<TypedInit *>(this), Elements[0]);
1245 SmallVector<Init*, 8> ListInits;
1246 ListInits.reserve(Elements.size());
1247 for (unsigned Element : Elements)
1248 ListInits.push_back(VarListElementInit::get(const_cast<TypedInit *>(this),
1250 return ListInit::get(ListInits, T);
1254 VarInit *VarInit::get(StringRef VN, RecTy *T) {
1255 Init *Value = StringInit::get(VN);
1256 return VarInit::get(Value, T);
1259 VarInit *VarInit::get(Init *VN, RecTy *T) {
1260 typedef std::pair<RecTy *, Init *> Key;
1261 static DenseMap<Key, VarInit*> ThePool;
1263 Key TheKey(std::make_pair(T, VN));
1265 VarInit *&I = ThePool[TheKey];
1267 I = new(Allocator) VarInit(VN, T);
1271 StringRef VarInit::getName() const {
1272 StringInit *NameString = cast<StringInit>(getNameInit());
1273 return NameString->getValue();
1276 Init *VarInit::getBit(unsigned Bit) const {
1277 if (getType() == BitRecTy::get())
1278 return const_cast<VarInit*>(this);
1279 return VarBitInit::get(const_cast<VarInit*>(this), Bit);
1282 Init *VarInit::resolveListElementReference(Record &R,
1283 const RecordVal *IRV,
1284 unsigned Elt) const {
1285 if (R.isTemplateArg(getNameInit())) return nullptr;
1286 if (IRV && IRV->getNameInit() != getNameInit()) return nullptr;
1288 RecordVal *RV = R.getValue(getNameInit());
1289 assert(RV && "Reference to a non-existent variable?");
1290 ListInit *LI = dyn_cast<ListInit>(RV->getValue());
1292 return VarListElementInit::get(cast<TypedInit>(RV->getValue()), Elt);
1294 if (Elt >= LI->size())
1295 return nullptr; // Out of range reference.
1296 Init *E = LI->getElement(Elt);
1297 // If the element is set to some value, or if we are resolving a reference
1298 // to a specific variable and that variable is explicitly unset, then
1299 // replace the VarListElementInit with it.
1300 if (IRV || !isa<UnsetInit>(E))
1305 RecTy *VarInit::getFieldType(StringInit *FieldName) const {
1306 if (RecordRecTy *RTy = dyn_cast<RecordRecTy>(getType()))
1307 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
1308 return RV->getType();
1312 Init *VarInit::getFieldInit(Record &R, const RecordVal *RV,
1313 StringInit *FieldName) const {
1314 if (isa<RecordRecTy>(getType()))
1315 if (const RecordVal *Val = R.getValue(VarName)) {
1316 if (RV != Val && (RV || isa<UnsetInit>(Val->getValue())))
1318 Init *TheInit = Val->getValue();
1319 assert(TheInit != this && "Infinite loop detected!");
1320 if (Init *I = TheInit->getFieldInit(R, RV, FieldName))
1327 Init *VarInit::resolveReferences(Record &R, const RecordVal *RV) const {
1328 if (RecordVal *Val = R.getValue(VarName))
1329 if (RV == Val || (!RV && !isa<UnsetInit>(Val->getValue())))
1330 return Val->getValue();
1331 return const_cast<VarInit *>(this);
1334 VarBitInit *VarBitInit::get(TypedInit *T, unsigned B) {
1335 typedef std::pair<TypedInit *, unsigned> Key;
1336 static DenseMap<Key, VarBitInit*> ThePool;
1338 Key TheKey(std::make_pair(T, B));
1340 VarBitInit *&I = ThePool[TheKey];
1342 I = new(Allocator) VarBitInit(T, B);
1346 Init *VarBitInit::convertInitializerTo(RecTy *Ty) const {
1347 if (isa<BitRecTy>(Ty))
1348 return const_cast<VarBitInit *>(this);
1353 std::string VarBitInit::getAsString() const {
1354 return TI->getAsString() + "{" + utostr(Bit) + "}";
1357 Init *VarBitInit::resolveReferences(Record &R, const RecordVal *RV) const {
1358 Init *I = TI->resolveReferences(R, RV);
1360 return I->getBit(getBitNum());
1362 return const_cast<VarBitInit*>(this);
1365 VarListElementInit *VarListElementInit::get(TypedInit *T,
1367 typedef std::pair<TypedInit *, unsigned> Key;
1368 static DenseMap<Key, VarListElementInit*> ThePool;
1370 Key TheKey(std::make_pair(T, E));
1372 VarListElementInit *&I = ThePool[TheKey];
1373 if (!I) I = new(Allocator) VarListElementInit(T, E);
1377 std::string VarListElementInit::getAsString() const {
1378 return TI->getAsString() + "[" + utostr(Element) + "]";
1382 VarListElementInit::resolveReferences(Record &R, const RecordVal *RV) const {
1383 if (Init *I = getVariable()->resolveListElementReference(R, RV,
1386 return const_cast<VarListElementInit *>(this);
1389 Init *VarListElementInit::getBit(unsigned Bit) const {
1390 if (getType() == BitRecTy::get())
1391 return const_cast<VarListElementInit*>(this);
1392 return VarBitInit::get(const_cast<VarListElementInit*>(this), Bit);
1395 Init *VarListElementInit:: resolveListElementReference(Record &R,
1396 const RecordVal *RV,
1397 unsigned Elt) const {
1398 if (Init *Result = TI->resolveListElementReference(R, RV, Element)) {
1399 if (TypedInit *TInit = dyn_cast<TypedInit>(Result)) {
1400 if (Init *Result2 = TInit->resolveListElementReference(R, RV, Elt))
1402 return VarListElementInit::get(TInit, Elt);
1410 DefInit *DefInit::get(Record *R) {
1411 return R->getDefInit();
1414 Init *DefInit::convertInitializerTo(RecTy *Ty) const {
1415 if (auto *RRT = dyn_cast<RecordRecTy>(Ty))
1416 if (getDef()->isSubClassOf(RRT->getRecord()))
1417 return const_cast<DefInit *>(this);
1421 RecTy *DefInit::getFieldType(StringInit *FieldName) const {
1422 if (const RecordVal *RV = Def->getValue(FieldName))
1423 return RV->getType();
1427 Init *DefInit::getFieldInit(Record &R, const RecordVal *RV,
1428 StringInit *FieldName) const {
1429 return Def->getValue(FieldName)->getValue();
1432 std::string DefInit::getAsString() const {
1433 return Def->getName();
1436 FieldInit *FieldInit::get(Init *R, StringInit *FN) {
1437 typedef std::pair<Init *, StringInit *> Key;
1438 static DenseMap<Key, FieldInit*> ThePool;
1440 Key TheKey(std::make_pair(R, FN));
1442 FieldInit *&I = ThePool[TheKey];
1443 if (!I) I = new(Allocator) FieldInit(R, FN);
1447 Init *FieldInit::getBit(unsigned Bit) const {
1448 if (getType() == BitRecTy::get())
1449 return const_cast<FieldInit*>(this);
1450 return VarBitInit::get(const_cast<FieldInit*>(this), Bit);
1453 Init *FieldInit::resolveListElementReference(Record &R, const RecordVal *RV,
1454 unsigned Elt) const {
1455 if (Init *ListVal = Rec->getFieldInit(R, RV, FieldName))
1456 if (ListInit *LI = dyn_cast<ListInit>(ListVal)) {
1457 if (Elt >= LI->size()) return nullptr;
1458 Init *E = LI->getElement(Elt);
1460 // If the element is set to some value, or if we are resolving a
1461 // reference to a specific variable and that variable is explicitly
1462 // unset, then replace the VarListElementInit with it.
1463 if (RV || !isa<UnsetInit>(E))
1469 Init *FieldInit::resolveReferences(Record &R, const RecordVal *RV) const {
1470 Init *NewRec = RV ? Rec->resolveReferences(R, RV) : Rec;
1472 if (Init *BitsVal = NewRec->getFieldInit(R, RV, FieldName)) {
1473 Init *BVR = BitsVal->resolveReferences(R, RV);
1474 return BVR->isComplete() ? BVR : const_cast<FieldInit *>(this);
1478 return FieldInit::get(NewRec, FieldName);
1479 return const_cast<FieldInit *>(this);
1482 static void ProfileDagInit(FoldingSetNodeID &ID, Init *V, StringInit *VN,
1483 ArrayRef<Init *> ArgRange,
1484 ArrayRef<StringInit *> NameRange) {
1488 ArrayRef<Init *>::iterator Arg = ArgRange.begin();
1489 ArrayRef<StringInit *>::iterator Name = NameRange.begin();
1490 while (Arg != ArgRange.end()) {
1491 assert(Name != NameRange.end() && "Arg name underflow!");
1492 ID.AddPointer(*Arg++);
1493 ID.AddPointer(*Name++);
1495 assert(Name == NameRange.end() && "Arg name overflow!");
1499 DagInit::get(Init *V, StringInit *VN, ArrayRef<Init *> ArgRange,
1500 ArrayRef<StringInit *> NameRange) {
1501 static FoldingSet<DagInit> ThePool;
1502 static std::vector<DagInit*> TheActualPool;
1504 FoldingSetNodeID ID;
1505 ProfileDagInit(ID, V, VN, ArgRange, NameRange);
1508 if (DagInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
1511 DagInit *I = new(Allocator) DagInit(V, VN, ArgRange, NameRange);
1512 ThePool.InsertNode(I, IP);
1513 TheActualPool.push_back(I);
1518 DagInit::get(Init *V, StringInit *VN,
1519 ArrayRef<std::pair<Init*, StringInit*>> args) {
1520 SmallVector<Init *, 8> Args;
1521 SmallVector<StringInit *, 8> Names;
1523 for (const auto &Arg : args) {
1524 Args.push_back(Arg.first);
1525 Names.push_back(Arg.second);
1528 return DagInit::get(V, VN, Args, Names);
1531 void DagInit::Profile(FoldingSetNodeID &ID) const {
1532 ProfileDagInit(ID, Val, ValName, Args, ArgNames);
1535 Init *DagInit::convertInitializerTo(RecTy *Ty) const {
1536 if (isa<DagRecTy>(Ty))
1537 return const_cast<DagInit *>(this);
1542 Init *DagInit::resolveReferences(Record &R, const RecordVal *RV) const {
1543 SmallVector<Init*, 8> NewArgs;
1544 NewArgs.reserve(Args.size());
1545 bool ArgsChanged = false;
1546 for (const Init *Arg : Args) {
1547 Init *NewArg = Arg->resolveReferences(R, RV);
1548 NewArgs.push_back(NewArg);
1549 ArgsChanged |= NewArg != Arg;
1552 Init *Op = Val->resolveReferences(R, RV);
1553 if (Op != Val || ArgsChanged)
1554 return DagInit::get(Op, ValName, NewArgs, ArgNames);
1556 return const_cast<DagInit *>(this);
1559 std::string DagInit::getAsString() const {
1560 std::string Result = "(" + Val->getAsString();
1562 Result += ":" + ValName->getAsUnquotedString();
1563 if (!Args.empty()) {
1564 Result += " " + Args[0]->getAsString();
1565 if (ArgNames[0]) Result += ":$" + ArgNames[0]->getAsUnquotedString();
1566 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
1567 Result += ", " + Args[i]->getAsString();
1568 if (ArgNames[i]) Result += ":$" + ArgNames[i]->getAsUnquotedString();
1571 return Result + ")";
1574 //===----------------------------------------------------------------------===//
1575 // Other implementations
1576 //===----------------------------------------------------------------------===//
1578 RecordVal::RecordVal(Init *N, RecTy *T, bool P)
1579 : Name(N), TyAndPrefix(T, P) {
1580 Value = UnsetInit::get()->convertInitializerTo(T);
1581 assert(Value && "Cannot create unset value for current type!");
1584 RecordVal::RecordVal(StringRef N, RecTy *T, bool P)
1585 : Name(StringInit::get(N)), TyAndPrefix(T, P) {
1586 Value = UnsetInit::get()->convertInitializerTo(T);
1587 assert(Value && "Cannot create unset value for current type!");
1590 StringRef RecordVal::getName() const {
1591 return cast<StringInit>(getNameInit())->getValue();
1594 LLVM_DUMP_METHOD void RecordVal::dump() const { errs() << *this; }
1596 void RecordVal::print(raw_ostream &OS, bool PrintSem) const {
1597 if (getPrefix()) OS << "field ";
1598 OS << *getType() << " " << getNameInitAsString();
1601 OS << " = " << *getValue();
1603 if (PrintSem) OS << ";\n";
1606 unsigned Record::LastID = 0;
1608 void Record::init() {
1611 // Every record potentially has a def at the top. This value is
1612 // replaced with the top-level def name at instantiation time.
1613 RecordVal DN("NAME", StringRecTy::get(), false);
1617 void Record::checkName() {
1618 // Ensure the record name has string type.
1619 const TypedInit *TypedName = cast<const TypedInit>(Name);
1620 if (!isa<StringRecTy>(TypedName->getType()))
1621 PrintFatalError(getLoc(), "Record name is not a string!");
1624 DefInit *Record::getDefInit() {
1626 TheInit = new(Allocator) DefInit(this, new(Allocator) RecordRecTy(this));
1630 StringRef Record::getName() const {
1631 return cast<StringInit>(Name)->getValue();
1634 void Record::setName(Init *NewName) {
1637 // DO NOT resolve record values to the name at this point because
1638 // there might be default values for arguments of this def. Those
1639 // arguments might not have been resolved yet so we don't want to
1640 // prematurely assume values for those arguments were not passed to
1643 // Nonetheless, it may be that some of this Record's values
1644 // reference the record name. Indeed, the reason for having the
1645 // record name be an Init is to provide this flexibility. The extra
1646 // resolve steps after completely instantiating defs takes care of
1647 // this. See TGParser::ParseDef and TGParser::ParseDefm.
1650 void Record::setName(StringRef Name) {
1651 setName(StringInit::get(Name));
1654 void Record::resolveReferencesTo(const RecordVal *RV) {
1655 for (RecordVal &Value : Values) {
1656 if (RV == &Value) // Skip resolve the same field as the given one
1658 if (Init *V = Value.getValue())
1659 if (Value.setValue(V->resolveReferences(*this, RV)))
1660 PrintFatalError(getLoc(), "Invalid value is found when setting '" +
1661 Value.getNameInitAsString() +
1662 "' after resolving references" +
1663 (RV ? " against '" + RV->getNameInitAsString() +
1664 "' of (" + RV->getValue()->getAsUnquotedString() +
1668 Init *OldName = getNameInit();
1669 Init *NewName = Name->resolveReferences(*this, RV);
1670 if (NewName != OldName) {
1671 // Re-register with RecordKeeper.
1676 LLVM_DUMP_METHOD void Record::dump() const { errs() << *this; }
1678 raw_ostream &llvm::operator<<(raw_ostream &OS, const Record &R) {
1679 OS << R.getNameInitAsString();
1681 ArrayRef<Init *> TArgs = R.getTemplateArgs();
1682 if (!TArgs.empty()) {
1684 bool NeedComma = false;
1685 for (const Init *TA : TArgs) {
1686 if (NeedComma) OS << ", ";
1688 const RecordVal *RV = R.getValue(TA);
1689 assert(RV && "Template argument record not found??");
1690 RV->print(OS, false);
1696 ArrayRef<std::pair<Record *, SMRange>> SC = R.getSuperClasses();
1699 for (const auto &SuperPair : SC)
1700 OS << " " << SuperPair.first->getNameInitAsString();
1704 for (const RecordVal &Val : R.getValues())
1705 if (Val.getPrefix() && !R.isTemplateArg(Val.getNameInit()))
1707 for (const RecordVal &Val : R.getValues())
1708 if (!Val.getPrefix() && !R.isTemplateArg(Val.getNameInit()))
1714 Init *Record::getValueInit(StringRef FieldName) const {
1715 const RecordVal *R = getValue(FieldName);
1716 if (!R || !R->getValue())
1717 PrintFatalError(getLoc(), "Record `" + getName() +
1718 "' does not have a field named `" + FieldName + "'!\n");
1719 return R->getValue();
1722 std::string Record::getValueAsString(StringRef FieldName) const {
1723 const RecordVal *R = getValue(FieldName);
1724 if (!R || !R->getValue())
1725 PrintFatalError(getLoc(), "Record `" + getName() +
1726 "' does not have a field named `" + FieldName + "'!\n");
1728 if (StringInit *SI = dyn_cast<StringInit>(R->getValue()))
1729 return SI->getValue();
1730 if (CodeInit *CI = dyn_cast<CodeInit>(R->getValue()))
1731 return CI->getValue();
1733 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1734 FieldName + "' does not have a string initializer!");
1737 BitsInit *Record::getValueAsBitsInit(StringRef FieldName) const {
1738 const RecordVal *R = getValue(FieldName);
1739 if (!R || !R->getValue())
1740 PrintFatalError(getLoc(), "Record `" + getName() +
1741 "' does not have a field named `" + FieldName + "'!\n");
1743 if (BitsInit *BI = dyn_cast<BitsInit>(R->getValue()))
1745 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1746 FieldName + "' does not have a BitsInit initializer!");
1749 ListInit *Record::getValueAsListInit(StringRef FieldName) const {
1750 const RecordVal *R = getValue(FieldName);
1751 if (!R || !R->getValue())
1752 PrintFatalError(getLoc(), "Record `" + getName() +
1753 "' does not have a field named `" + FieldName + "'!\n");
1755 if (ListInit *LI = dyn_cast<ListInit>(R->getValue()))
1757 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1758 FieldName + "' does not have a list initializer!");
1761 std::vector<Record*>
1762 Record::getValueAsListOfDefs(StringRef FieldName) const {
1763 ListInit *List = getValueAsListInit(FieldName);
1764 std::vector<Record*> Defs;
1765 for (Init *I : List->getValues()) {
1766 if (DefInit *DI = dyn_cast<DefInit>(I))
1767 Defs.push_back(DI->getDef());
1769 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1770 FieldName + "' list is not entirely DefInit!");
1775 int64_t Record::getValueAsInt(StringRef FieldName) const {
1776 const RecordVal *R = getValue(FieldName);
1777 if (!R || !R->getValue())
1778 PrintFatalError(getLoc(), "Record `" + getName() +
1779 "' does not have a field named `" + FieldName + "'!\n");
1781 if (IntInit *II = dyn_cast<IntInit>(R->getValue()))
1782 return II->getValue();
1783 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1784 FieldName + "' does not have an int initializer!");
1787 std::vector<int64_t>
1788 Record::getValueAsListOfInts(StringRef FieldName) const {
1789 ListInit *List = getValueAsListInit(FieldName);
1790 std::vector<int64_t> Ints;
1791 for (Init *I : List->getValues()) {
1792 if (IntInit *II = dyn_cast<IntInit>(I))
1793 Ints.push_back(II->getValue());
1795 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1796 FieldName + "' does not have a list of ints initializer!");
1801 std::vector<std::string>
1802 Record::getValueAsListOfStrings(StringRef FieldName) const {
1803 ListInit *List = getValueAsListInit(FieldName);
1804 std::vector<std::string> Strings;
1805 for (Init *I : List->getValues()) {
1806 if (StringInit *SI = dyn_cast<StringInit>(I))
1807 Strings.push_back(SI->getValue());
1809 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1810 FieldName + "' does not have a list of strings initializer!");
1815 Record *Record::getValueAsDef(StringRef FieldName) const {
1816 const RecordVal *R = getValue(FieldName);
1817 if (!R || !R->getValue())
1818 PrintFatalError(getLoc(), "Record `" + getName() +
1819 "' does not have a field named `" + FieldName + "'!\n");
1821 if (DefInit *DI = dyn_cast<DefInit>(R->getValue()))
1822 return DI->getDef();
1823 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1824 FieldName + "' does not have a def initializer!");
1827 bool Record::getValueAsBit(StringRef FieldName) const {
1828 const RecordVal *R = getValue(FieldName);
1829 if (!R || !R->getValue())
1830 PrintFatalError(getLoc(), "Record `" + getName() +
1831 "' does not have a field named `" + FieldName + "'!\n");
1833 if (BitInit *BI = dyn_cast<BitInit>(R->getValue()))
1834 return BI->getValue();
1835 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1836 FieldName + "' does not have a bit initializer!");
1839 bool Record::getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const {
1840 const RecordVal *R = getValue(FieldName);
1841 if (!R || !R->getValue())
1842 PrintFatalError(getLoc(), "Record `" + getName() +
1843 "' does not have a field named `" + FieldName.str() + "'!\n");
1845 if (isa<UnsetInit>(R->getValue())) {
1850 if (BitInit *BI = dyn_cast<BitInit>(R->getValue()))
1851 return BI->getValue();
1852 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1853 FieldName + "' does not have a bit initializer!");
1856 DagInit *Record::getValueAsDag(StringRef FieldName) const {
1857 const RecordVal *R = getValue(FieldName);
1858 if (!R || !R->getValue())
1859 PrintFatalError(getLoc(), "Record `" + getName() +
1860 "' does not have a field named `" + FieldName + "'!\n");
1862 if (DagInit *DI = dyn_cast<DagInit>(R->getValue()))
1864 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1865 FieldName + "' does not have a dag initializer!");
1868 LLVM_DUMP_METHOD void MultiClass::dump() const {
1869 errs() << "Record:\n";
1872 errs() << "Defs:\n";
1873 for (const auto &Proto : DefPrototypes)
1877 LLVM_DUMP_METHOD void RecordKeeper::dump() const { errs() << *this; }
1879 raw_ostream &llvm::operator<<(raw_ostream &OS, const RecordKeeper &RK) {
1880 OS << "------------- Classes -----------------\n";
1881 for (const auto &C : RK.getClasses())
1882 OS << "class " << *C.second;
1884 OS << "------------- Defs -----------------\n";
1885 for (const auto &D : RK.getDefs())
1886 OS << "def " << *D.second;
1890 std::vector<Record *>
1891 RecordKeeper::getAllDerivedDefinitions(StringRef ClassName) const {
1892 Record *Class = getClass(ClassName);
1894 PrintFatalError("ERROR: Couldn't find the `" + ClassName + "' class!\n");
1896 std::vector<Record*> Defs;
1897 for (const auto &D : getDefs())
1898 if (D.second->isSubClassOf(Class))
1899 Defs.push_back(D.second.get());
1904 static Init *GetStrConcat(Init *I0, Init *I1) {
1905 // Shortcut for the common case of concatenating two strings.
1906 if (const StringInit *I0s = dyn_cast<StringInit>(I0))
1907 if (const StringInit *I1s = dyn_cast<StringInit>(I1))
1908 return ConcatStringInits(I0s, I1s);
1909 return BinOpInit::get(BinOpInit::STRCONCAT, I0, I1, StringRecTy::get());
1912 Init *llvm::QualifyName(Record &CurRec, MultiClass *CurMultiClass,
1913 Init *Name, StringRef Scoper) {
1914 Init *NewName = GetStrConcat(CurRec.getNameInit(), StringInit::get(Scoper));
1915 NewName = GetStrConcat(NewName, Name);
1916 if (CurMultiClass && Scoper != "::") {
1917 Init *Prefix = GetStrConcat(CurMultiClass->Rec.getNameInit(),
1918 StringInit::get("::"));
1919 NewName = GetStrConcat(Prefix, NewName);
1922 if (BinOpInit *BinOp = dyn_cast<BinOpInit>(NewName))
1923 NewName = BinOp->Fold(&CurRec, CurMultiClass);