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/ADT/ArrayRef.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/FoldingSet.h"
17 #include "llvm/ADT/SmallString.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/ADT/StringMap.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/Support/Allocator.h"
23 #include "llvm/Support/Casting.h"
24 #include "llvm/Support/Compiler.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/SMLoc.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include "llvm/TableGen/Error.h"
29 #include "llvm/TableGen/Record.h"
39 static BumpPtrAllocator Allocator;
41 //===----------------------------------------------------------------------===//
42 // Type implementations
43 //===----------------------------------------------------------------------===//
45 BitRecTy BitRecTy::Shared;
46 CodeRecTy CodeRecTy::Shared;
47 IntRecTy IntRecTy::Shared;
48 StringRecTy StringRecTy::Shared;
49 DagRecTy DagRecTy::Shared;
51 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
52 LLVM_DUMP_METHOD void RecTy::dump() const { print(errs()); }
55 ListRecTy *RecTy::getListTy() {
57 ListTy = new(Allocator) ListRecTy(this);
61 bool RecTy::typeIsConvertibleTo(const RecTy *RHS) const {
62 assert(RHS && "NULL pointer");
63 return Kind == RHS->getRecTyKind();
66 bool BitRecTy::typeIsConvertibleTo(const RecTy *RHS) const{
67 if (RecTy::typeIsConvertibleTo(RHS) || RHS->getRecTyKind() == IntRecTyKind)
69 if (const BitsRecTy *BitsTy = dyn_cast<BitsRecTy>(RHS))
70 return BitsTy->getNumBits() == 1;
74 BitsRecTy *BitsRecTy::get(unsigned Sz) {
75 static std::vector<BitsRecTy*> Shared;
76 if (Sz >= Shared.size())
77 Shared.resize(Sz + 1);
78 BitsRecTy *&Ty = Shared[Sz];
80 Ty = new(Allocator) BitsRecTy(Sz);
84 std::string BitsRecTy::getAsString() const {
85 return "bits<" + utostr(Size) + ">";
88 bool BitsRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
89 if (RecTy::typeIsConvertibleTo(RHS)) //argument and the sender are same type
90 return cast<BitsRecTy>(RHS)->Size == Size;
91 RecTyKind kind = RHS->getRecTyKind();
92 return (kind == BitRecTyKind && Size == 1) || (kind == IntRecTyKind);
95 bool IntRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
96 RecTyKind kind = RHS->getRecTyKind();
97 return kind==BitRecTyKind || kind==BitsRecTyKind || kind==IntRecTyKind;
100 std::string StringRecTy::getAsString() const {
104 std::string ListRecTy::getAsString() const {
105 return "list<" + Ty->getAsString() + ">";
108 bool ListRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
109 if (const auto *ListTy = dyn_cast<ListRecTy>(RHS))
110 return Ty->typeIsConvertibleTo(ListTy->getElementType());
114 std::string DagRecTy::getAsString() const {
118 RecordRecTy *RecordRecTy::get(Record *R) {
119 return dyn_cast<RecordRecTy>(R->getDefInit()->getType());
122 std::string RecordRecTy::getAsString() const {
123 return Rec->getName();
126 bool RecordRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
127 const RecordRecTy *RTy = dyn_cast<RecordRecTy>(RHS);
131 if (RTy->getRecord() == Rec || Rec->isSubClassOf(RTy->getRecord()))
134 for (const auto &SCPair : RTy->getRecord()->getSuperClasses())
135 if (Rec->isSubClassOf(SCPair.first))
141 RecTy *llvm::resolveTypes(RecTy *T1, RecTy *T2) {
142 if (T1->typeIsConvertibleTo(T2))
144 if (T2->typeIsConvertibleTo(T1))
147 // If one is a Record type, check superclasses
148 if (RecordRecTy *RecTy1 = dyn_cast<RecordRecTy>(T1)) {
149 // See if T2 inherits from a type T1 also inherits from
150 for (const auto &SuperPair1 : RecTy1->getRecord()->getSuperClasses()) {
151 RecordRecTy *SuperRecTy1 = RecordRecTy::get(SuperPair1.first);
152 RecTy *NewType1 = resolveTypes(SuperRecTy1, T2);
157 if (RecordRecTy *RecTy2 = dyn_cast<RecordRecTy>(T2)) {
158 // See if T1 inherits from a type T2 also inherits from
159 for (const auto &SuperPair2 : RecTy2->getRecord()->getSuperClasses()) {
160 RecordRecTy *SuperRecTy2 = RecordRecTy::get(SuperPair2.first);
161 RecTy *NewType2 = resolveTypes(T1, SuperRecTy2);
169 //===----------------------------------------------------------------------===//
170 // Initializer implementations
171 //===----------------------------------------------------------------------===//
173 void Init::anchor() {}
175 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
176 LLVM_DUMP_METHOD void Init::dump() const { return print(errs()); }
179 UnsetInit *UnsetInit::get() {
180 static UnsetInit TheInit;
184 Init *UnsetInit::convertInitializerTo(RecTy *Ty) const {
185 if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
186 SmallVector<Init *, 16> NewBits(BRT->getNumBits());
188 for (unsigned i = 0; i != BRT->getNumBits(); ++i)
189 NewBits[i] = UnsetInit::get();
191 return BitsInit::get(NewBits);
194 // All other types can just be returned.
195 return const_cast<UnsetInit *>(this);
198 BitInit *BitInit::get(bool V) {
199 static BitInit True(true);
200 static BitInit False(false);
202 return V ? &True : &False;
205 Init *BitInit::convertInitializerTo(RecTy *Ty) const {
206 if (isa<BitRecTy>(Ty))
207 return const_cast<BitInit *>(this);
209 if (isa<IntRecTy>(Ty))
210 return IntInit::get(getValue());
212 if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
213 // Can only convert single bit.
214 if (BRT->getNumBits() == 1)
215 return BitsInit::get(const_cast<BitInit *>(this));
222 ProfileBitsInit(FoldingSetNodeID &ID, ArrayRef<Init *> Range) {
223 ID.AddInteger(Range.size());
225 for (Init *I : Range)
229 BitsInit *BitsInit::get(ArrayRef<Init *> Range) {
230 static FoldingSet<BitsInit> ThePool;
233 ProfileBitsInit(ID, Range);
236 if (BitsInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
239 void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *>(Range.size()),
241 BitsInit *I = new(Mem) BitsInit(Range.size());
242 std::uninitialized_copy(Range.begin(), Range.end(),
243 I->getTrailingObjects<Init *>());
244 ThePool.InsertNode(I, IP);
248 void BitsInit::Profile(FoldingSetNodeID &ID) const {
249 ProfileBitsInit(ID, makeArrayRef(getTrailingObjects<Init *>(), NumBits));
252 Init *BitsInit::convertInitializerTo(RecTy *Ty) const {
253 if (isa<BitRecTy>(Ty)) {
254 if (getNumBits() != 1) return nullptr; // Only accept if just one bit!
258 if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
259 // If the number of bits is right, return it. Otherwise we need to expand
261 if (getNumBits() != BRT->getNumBits()) return nullptr;
262 return const_cast<BitsInit *>(this);
265 if (isa<IntRecTy>(Ty)) {
267 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
268 if (auto *Bit = dyn_cast<BitInit>(getBit(i)))
269 Result |= static_cast<int64_t>(Bit->getValue()) << i;
272 return IntInit::get(Result);
279 BitsInit::convertInitializerBitRange(ArrayRef<unsigned> Bits) const {
280 SmallVector<Init *, 16> NewBits(Bits.size());
282 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
283 if (Bits[i] >= getNumBits())
285 NewBits[i] = getBit(Bits[i]);
287 return BitsInit::get(NewBits);
290 std::string BitsInit::getAsString() const {
291 std::string Result = "{ ";
292 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
293 if (i) Result += ", ";
294 if (Init *Bit = getBit(e-i-1))
295 Result += Bit->getAsString();
299 return Result + " }";
302 // Fix bit initializer to preserve the behavior that bit reference from a unset
303 // bits initializer will resolve into VarBitInit to keep the field name and bit
304 // number used in targets with fixed insn length.
305 static Init *fixBitInit(const RecordVal *RV, Init *Before, Init *After) {
306 if (RV || !isa<UnsetInit>(After))
311 // resolveReferences - If there are any field references that refer to fields
312 // that have been filled in, we can propagate the values now.
313 Init *BitsInit::resolveReferences(Record &R, const RecordVal *RV) const {
314 bool Changed = false;
315 SmallVector<Init *, 16> NewBits(getNumBits());
317 Init *CachedInit = nullptr;
318 Init *CachedBitVar = nullptr;
319 bool CachedBitVarChanged = false;
321 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
322 Init *CurBit = getBit(i);
323 Init *CurBitVar = CurBit->getBitVar();
327 if (CurBitVar == CachedBitVar) {
328 if (CachedBitVarChanged) {
329 Init *Bit = CachedInit->getBit(CurBit->getBitNum());
330 NewBits[i] = fixBitInit(RV, CurBit, Bit);
334 CachedBitVar = CurBitVar;
335 CachedBitVarChanged = false;
340 CurBitVar = CurBitVar->resolveReferences(R, RV);
341 CachedBitVarChanged |= B != CurBitVar;
342 Changed |= B != CurBitVar;
343 } while (B != CurBitVar);
344 CachedInit = CurBitVar;
346 if (CachedBitVarChanged) {
347 Init *Bit = CurBitVar->getBit(CurBit->getBitNum());
348 NewBits[i] = fixBitInit(RV, CurBit, Bit);
353 return BitsInit::get(NewBits);
355 return const_cast<BitsInit *>(this);
358 IntInit *IntInit::get(int64_t V) {
359 static DenseMap<int64_t, IntInit*> ThePool;
361 IntInit *&I = ThePool[V];
362 if (!I) I = new(Allocator) IntInit(V);
366 std::string IntInit::getAsString() const {
367 return itostr(Value);
370 static bool canFitInBitfield(int64_t Value, unsigned NumBits) {
371 // For example, with NumBits == 4, we permit Values from [-7 .. 15].
372 return (NumBits >= sizeof(Value) * 8) ||
373 (Value >> NumBits == 0) || (Value >> (NumBits-1) == -1);
376 Init *IntInit::convertInitializerTo(RecTy *Ty) const {
377 if (isa<IntRecTy>(Ty))
378 return const_cast<IntInit *>(this);
380 if (isa<BitRecTy>(Ty)) {
381 int64_t Val = getValue();
382 if (Val != 0 && Val != 1) return nullptr; // Only accept 0 or 1 for a bit!
383 return BitInit::get(Val != 0);
386 if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
387 int64_t Value = getValue();
388 // Make sure this bitfield is large enough to hold the integer value.
389 if (!canFitInBitfield(Value, BRT->getNumBits()))
392 SmallVector<Init *, 16> NewBits(BRT->getNumBits());
393 for (unsigned i = 0; i != BRT->getNumBits(); ++i)
394 NewBits[i] = BitInit::get(Value & (1LL << i));
396 return BitsInit::get(NewBits);
403 IntInit::convertInitializerBitRange(ArrayRef<unsigned> Bits) const {
404 SmallVector<Init *, 16> NewBits(Bits.size());
406 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
410 NewBits[i] = BitInit::get(Value & (INT64_C(1) << Bits[i]));
412 return BitsInit::get(NewBits);
415 CodeInit *CodeInit::get(StringRef V) {
416 static StringMap<CodeInit*, BumpPtrAllocator &> ThePool(Allocator);
418 auto &Entry = *ThePool.insert(std::make_pair(V, nullptr)).first;
420 Entry.second = new(Allocator) CodeInit(Entry.getKey());
424 StringInit *StringInit::get(StringRef V) {
425 static StringMap<StringInit*, BumpPtrAllocator &> ThePool(Allocator);
427 auto &Entry = *ThePool.insert(std::make_pair(V, nullptr)).first;
429 Entry.second = new(Allocator) StringInit(Entry.getKey());
433 Init *StringInit::convertInitializerTo(RecTy *Ty) const {
434 if (isa<StringRecTy>(Ty))
435 return const_cast<StringInit *>(this);
440 Init *CodeInit::convertInitializerTo(RecTy *Ty) const {
441 if (isa<CodeRecTy>(Ty))
442 return const_cast<CodeInit *>(this);
447 static void ProfileListInit(FoldingSetNodeID &ID,
448 ArrayRef<Init *> Range,
450 ID.AddInteger(Range.size());
451 ID.AddPointer(EltTy);
453 for (Init *I : Range)
457 ListInit *ListInit::get(ArrayRef<Init *> Range, RecTy *EltTy) {
458 static FoldingSet<ListInit> ThePool;
461 ProfileListInit(ID, Range, EltTy);
464 if (ListInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
467 void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *>(Range.size()),
469 ListInit *I = new(Mem) ListInit(Range.size(), EltTy);
470 std::uninitialized_copy(Range.begin(), Range.end(),
471 I->getTrailingObjects<Init *>());
472 ThePool.InsertNode(I, IP);
476 void ListInit::Profile(FoldingSetNodeID &ID) const {
477 RecTy *EltTy = cast<ListRecTy>(getType())->getElementType();
479 ProfileListInit(ID, getValues(), EltTy);
482 Init *ListInit::convertInitializerTo(RecTy *Ty) const {
484 return const_cast<ListInit*>(this);
486 if (auto *LRT = dyn_cast<ListRecTy>(Ty)) {
487 SmallVector<Init*, 8> Elements;
488 Elements.reserve(getValues().size());
490 // Verify that all of the elements of the list are subclasses of the
491 // appropriate class!
492 bool Changed = false;
493 RecTy *ElementType = LRT->getElementType();
494 for (Init *I : getValues())
495 if (Init *CI = I->convertInitializerTo(ElementType)) {
496 Elements.push_back(CI);
503 return const_cast<ListInit*>(this);
504 return ListInit::get(Elements, Ty);
510 Init *ListInit::convertInitListSlice(ArrayRef<unsigned> Elements) const {
511 SmallVector<Init*, 8> Vals;
512 Vals.reserve(Elements.size());
513 for (unsigned Element : Elements) {
514 if (Element >= size())
516 Vals.push_back(getElement(Element));
518 return ListInit::get(Vals, getType());
521 Record *ListInit::getElementAsRecord(unsigned i) const {
522 assert(i < NumValues && "List element index out of range!");
523 DefInit *DI = dyn_cast<DefInit>(getElement(i));
525 PrintFatalError("Expected record in list!");
529 Init *ListInit::resolveReferences(Record &R, const RecordVal *RV) const {
530 SmallVector<Init*, 8> Resolved;
531 Resolved.reserve(size());
532 bool Changed = false;
534 for (Init *CurElt : getValues()) {
539 CurElt = CurElt->resolveReferences(R, RV);
540 Changed |= E != CurElt;
541 } while (E != CurElt);
542 Resolved.push_back(E);
546 return ListInit::get(Resolved, getType());
547 return const_cast<ListInit *>(this);
550 Init *ListInit::resolveListElementReference(Record &R, const RecordVal *IRV,
551 unsigned Elt) const {
553 return nullptr; // Out of range reference.
554 Init *E = getElement(Elt);
555 // If the element is set to some value, or if we are resolving a reference
556 // to a specific variable and that variable is explicitly unset, then
557 // replace the VarListElementInit with it.
558 if (IRV || !isa<UnsetInit>(E))
563 std::string ListInit::getAsString() const {
564 std::string Result = "[";
565 const char *sep = "";
566 for (Init *Element : *this) {
569 Result += Element->getAsString();
574 Init *OpInit::resolveListElementReference(Record &R, const RecordVal *IRV,
575 unsigned Elt) const {
576 Init *Resolved = resolveReferences(R, IRV);
577 OpInit *OResolved = dyn_cast<OpInit>(Resolved);
579 Resolved = OResolved->Fold(&R, nullptr);
582 if (Resolved != this) {
583 TypedInit *Typed = cast<TypedInit>(Resolved);
584 if (Init *New = Typed->resolveListElementReference(R, IRV, Elt))
586 return VarListElementInit::get(Typed, Elt);
592 Init *OpInit::getBit(unsigned Bit) const {
593 if (getType() == BitRecTy::get())
594 return const_cast<OpInit*>(this);
595 return VarBitInit::get(const_cast<OpInit*>(this), Bit);
599 ProfileUnOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *Op, RecTy *Type) {
600 ID.AddInteger(Opcode);
605 UnOpInit *UnOpInit::get(UnaryOp Opc, Init *LHS, RecTy *Type) {
606 static FoldingSet<UnOpInit> ThePool;
609 ProfileUnOpInit(ID, Opc, LHS, Type);
612 if (UnOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
615 UnOpInit *I = new(Allocator) UnOpInit(Opc, LHS, Type);
616 ThePool.InsertNode(I, IP);
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());
715 return const_cast<UnOpInit *>(this);
718 Init *UnOpInit::resolveReferences(Record &R, const RecordVal *RV) const {
719 Init *lhs = LHS->resolveReferences(R, RV);
722 return (UnOpInit::get(getOpcode(), lhs, getType()))->Fold(&R, nullptr);
723 return Fold(&R, nullptr);
726 std::string UnOpInit::getAsString() const {
728 switch (getOpcode()) {
729 case CAST: Result = "!cast<" + getType()->getAsString() + ">"; break;
730 case HEAD: Result = "!head"; break;
731 case TAIL: Result = "!tail"; break;
732 case EMPTY: Result = "!empty"; break;
734 return Result + "(" + LHS->getAsString() + ")";
738 ProfileBinOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *LHS, Init *RHS,
740 ID.AddInteger(Opcode);
746 BinOpInit *BinOpInit::get(BinaryOp Opc, Init *LHS,
747 Init *RHS, RecTy *Type) {
748 static FoldingSet<BinOpInit> ThePool;
751 ProfileBinOpInit(ID, Opc, LHS, RHS, Type);
754 if (BinOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
757 BinOpInit *I = new(Allocator) BinOpInit(Opc, LHS, RHS, Type);
758 ThePool.InsertNode(I, IP);
762 void BinOpInit::Profile(FoldingSetNodeID &ID) const {
763 ProfileBinOpInit(ID, getOpcode(), getLHS(), getRHS(), getType());
766 static StringInit *ConcatStringInits(const StringInit *I0,
767 const StringInit *I1) {
768 SmallString<80> Concat(I0->getValue());
769 Concat.append(I1->getValue());
770 return StringInit::get(Concat);
773 Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const {
774 switch (getOpcode()) {
776 DagInit *LHSs = dyn_cast<DagInit>(LHS);
777 DagInit *RHSs = dyn_cast<DagInit>(RHS);
779 DefInit *LOp = dyn_cast<DefInit>(LHSs->getOperator());
780 DefInit *ROp = dyn_cast<DefInit>(RHSs->getOperator());
781 if (!LOp || !ROp || LOp->getDef() != ROp->getDef())
782 PrintFatalError("Concated Dag operators do not match!");
783 SmallVector<Init*, 8> Args;
784 SmallVector<StringInit*, 8> ArgNames;
785 for (unsigned i = 0, e = LHSs->getNumArgs(); i != e; ++i) {
786 Args.push_back(LHSs->getArg(i));
787 ArgNames.push_back(LHSs->getArgName(i));
789 for (unsigned i = 0, e = RHSs->getNumArgs(); i != e; ++i) {
790 Args.push_back(RHSs->getArg(i));
791 ArgNames.push_back(RHSs->getArgName(i));
793 return DagInit::get(LHSs->getOperator(), nullptr, Args, ArgNames);
798 ListInit *LHSs = dyn_cast<ListInit>(LHS);
799 ListInit *RHSs = dyn_cast<ListInit>(RHS);
801 SmallVector<Init *, 8> Args;
802 Args.insert(Args.end(), LHSs->begin(), LHSs->end());
803 Args.insert(Args.end(), RHSs->begin(), RHSs->end());
804 return ListInit::get(
805 Args, cast<ListRecTy>(LHSs->getType())->getElementType());
810 StringInit *LHSs = dyn_cast<StringInit>(LHS);
811 StringInit *RHSs = dyn_cast<StringInit>(RHS);
813 return ConcatStringInits(LHSs, RHSs);
817 // try to fold eq comparison for 'bit' and 'int', otherwise fallback
818 // to string objects.
820 dyn_cast_or_null<IntInit>(LHS->convertInitializerTo(IntRecTy::get()));
822 dyn_cast_or_null<IntInit>(RHS->convertInitializerTo(IntRecTy::get()));
825 return IntInit::get(L->getValue() == R->getValue());
827 StringInit *LHSs = dyn_cast<StringInit>(LHS);
828 StringInit *RHSs = dyn_cast<StringInit>(RHS);
830 // Make sure we've resolved
832 return IntInit::get(LHSs->getValue() == RHSs->getValue());
843 dyn_cast_or_null<IntInit>(LHS->convertInitializerTo(IntRecTy::get()));
845 dyn_cast_or_null<IntInit>(RHS->convertInitializerTo(IntRecTy::get()));
847 int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
849 switch (getOpcode()) {
850 default: llvm_unreachable("Bad opcode!");
851 case ADD: Result = LHSv + RHSv; break;
852 case AND: Result = LHSv & RHSv; break;
853 case OR: Result = LHSv | RHSv; break;
854 case SHL: Result = LHSv << RHSv; break;
855 case SRA: Result = LHSv >> RHSv; break;
856 case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break;
858 return IntInit::get(Result);
863 return const_cast<BinOpInit *>(this);
866 Init *BinOpInit::resolveReferences(Record &R, const RecordVal *RV) const {
867 Init *lhs = LHS->resolveReferences(R, RV);
868 Init *rhs = RHS->resolveReferences(R, RV);
870 if (LHS != lhs || RHS != rhs)
871 return (BinOpInit::get(getOpcode(), lhs, rhs, getType()))->Fold(&R,nullptr);
872 return Fold(&R, nullptr);
875 std::string BinOpInit::getAsString() const {
877 switch (getOpcode()) {
878 case CONCAT: Result = "!con"; break;
879 case ADD: Result = "!add"; break;
880 case AND: Result = "!and"; break;
881 case OR: Result = "!or"; break;
882 case SHL: Result = "!shl"; break;
883 case SRA: Result = "!sra"; break;
884 case SRL: Result = "!srl"; break;
885 case EQ: Result = "!eq"; break;
886 case LISTCONCAT: Result = "!listconcat"; break;
887 case STRCONCAT: Result = "!strconcat"; break;
889 return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")";
893 ProfileTernOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *LHS, Init *MHS,
894 Init *RHS, RecTy *Type) {
895 ID.AddInteger(Opcode);
902 TernOpInit *TernOpInit::get(TernaryOp Opc, Init *LHS, Init *MHS, Init *RHS,
904 static FoldingSet<TernOpInit> ThePool;
907 ProfileTernOpInit(ID, Opc, LHS, MHS, RHS, Type);
910 if (TernOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
913 TernOpInit *I = new(Allocator) TernOpInit(Opc, LHS, MHS, RHS, Type);
914 ThePool.InsertNode(I, IP);
918 void TernOpInit::Profile(FoldingSetNodeID &ID) const {
919 ProfileTernOpInit(ID, getOpcode(), getLHS(), getMHS(), getRHS(), getType());
922 static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
923 Record *CurRec, MultiClass *CurMultiClass);
925 static Init *EvaluateOperation(OpInit *RHSo, Init *LHS, Init *Arg,
926 RecTy *Type, Record *CurRec,
927 MultiClass *CurMultiClass) {
928 // If this is a dag, recurse
929 if (auto *TArg = dyn_cast<TypedInit>(Arg))
930 if (isa<DagRecTy>(TArg->getType()))
931 return ForeachHelper(LHS, Arg, RHSo, Type, CurRec, CurMultiClass);
933 SmallVector<Init *, 8> NewOperands;
934 NewOperands.reserve(RHSo->getNumOperands());
935 for (unsigned i = 0, e = RHSo->getNumOperands(); i < e; ++i) {
936 if (auto *RHSoo = dyn_cast<OpInit>(RHSo->getOperand(i))) {
937 if (Init *Result = EvaluateOperation(RHSoo, LHS, Arg,
938 Type, CurRec, CurMultiClass))
939 NewOperands.push_back(Result);
941 NewOperands.push_back(Arg);
942 } else if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
943 NewOperands.push_back(Arg);
945 NewOperands.push_back(RHSo->getOperand(i));
949 // Now run the operator and use its result as the new leaf
950 const OpInit *NewOp = RHSo->clone(NewOperands);
951 Init *NewVal = NewOp->Fold(CurRec, CurMultiClass);
952 return (NewVal != NewOp) ? NewVal : nullptr;
955 static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
956 Record *CurRec, MultiClass *CurMultiClass) {
957 OpInit *RHSo = dyn_cast<OpInit>(RHS);
960 PrintFatalError(CurRec->getLoc(), "!foreach requires an operator\n");
962 TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
965 PrintFatalError(CurRec->getLoc(), "!foreach requires typed variable\n");
967 DagInit *MHSd = dyn_cast<DagInit>(MHS);
968 if (MHSd && isa<DagRecTy>(Type)) {
969 Init *Val = MHSd->getOperator();
970 if (Init *Result = EvaluateOperation(RHSo, LHS, Val,
971 Type, CurRec, CurMultiClass))
974 SmallVector<std::pair<Init *, StringInit*>, 8> args;
975 for (unsigned int i = 0; i < MHSd->getNumArgs(); ++i) {
976 Init *Arg = MHSd->getArg(i);
977 StringInit *ArgName = MHSd->getArgName(i);
980 if (Init *Result = EvaluateOperation(RHSo, LHS, Arg, Type,
981 CurRec, CurMultiClass))
984 // TODO: Process arg names
985 args.push_back(std::make_pair(Arg, ArgName));
988 return DagInit::get(Val, nullptr, args);
991 ListInit *MHSl = dyn_cast<ListInit>(MHS);
992 if (MHSl && isa<ListRecTy>(Type)) {
993 SmallVector<Init *, 8> NewOperands;
994 SmallVector<Init *, 8> NewList(MHSl->begin(), MHSl->end());
996 for (Init *&Item : NewList) {
998 for(unsigned i = 0; i < RHSo->getNumOperands(); ++i) {
999 // First, replace the foreach variable with the list item
1000 if (LHS->getAsString() == RHSo->getOperand(i)->getAsString())
1001 NewOperands.push_back(Item);
1003 NewOperands.push_back(RHSo->getOperand(i));
1006 // Now run the operator and use its result as the new list item
1007 const OpInit *NewOp = RHSo->clone(NewOperands);
1008 Init *NewItem = NewOp->Fold(CurRec, CurMultiClass);
1009 if (NewItem != NewOp)
1012 return ListInit::get(NewList, MHSl->getType());
1017 Init *TernOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const {
1018 switch (getOpcode()) {
1020 DefInit *LHSd = dyn_cast<DefInit>(LHS);
1021 VarInit *LHSv = dyn_cast<VarInit>(LHS);
1022 StringInit *LHSs = dyn_cast<StringInit>(LHS);
1024 DefInit *MHSd = dyn_cast<DefInit>(MHS);
1025 VarInit *MHSv = dyn_cast<VarInit>(MHS);
1026 StringInit *MHSs = dyn_cast<StringInit>(MHS);
1028 DefInit *RHSd = dyn_cast<DefInit>(RHS);
1029 VarInit *RHSv = dyn_cast<VarInit>(RHS);
1030 StringInit *RHSs = dyn_cast<StringInit>(RHS);
1032 if (LHSd && MHSd && RHSd) {
1033 Record *Val = RHSd->getDef();
1034 if (LHSd->getAsString() == RHSd->getAsString())
1035 Val = MHSd->getDef();
1036 return DefInit::get(Val);
1038 if (LHSv && MHSv && RHSv) {
1039 std::string Val = RHSv->getName();
1040 if (LHSv->getAsString() == RHSv->getAsString())
1041 Val = MHSv->getName();
1042 return VarInit::get(Val, getType());
1044 if (LHSs && MHSs && RHSs) {
1045 std::string Val = RHSs->getValue();
1047 std::string::size_type found;
1048 std::string::size_type idx = 0;
1050 found = Val.find(LHSs->getValue(), idx);
1051 if (found == std::string::npos)
1053 Val.replace(found, LHSs->getValue().size(), MHSs->getValue());
1054 idx = found + MHSs->getValue().size();
1057 return StringInit::get(Val);
1063 if (Init *Result = ForeachHelper(LHS, MHS, RHS, getType(),
1064 CurRec, CurMultiClass))
1070 IntInit *LHSi = dyn_cast<IntInit>(LHS);
1071 if (Init *I = LHS->convertInitializerTo(IntRecTy::get()))
1072 LHSi = dyn_cast<IntInit>(I);
1074 if (LHSi->getValue())
1082 return const_cast<TernOpInit *>(this);
1085 Init *TernOpInit::resolveReferences(Record &R,
1086 const RecordVal *RV) const {
1087 Init *lhs = LHS->resolveReferences(R, RV);
1089 if (getOpcode() == IF && lhs != LHS) {
1090 IntInit *Value = dyn_cast<IntInit>(lhs);
1091 if (Init *I = lhs->convertInitializerTo(IntRecTy::get()))
1092 Value = dyn_cast<IntInit>(I);
1095 if (Value->getValue()) {
1096 Init *mhs = MHS->resolveReferences(R, RV);
1097 return (TernOpInit::get(getOpcode(), lhs, mhs,
1098 RHS, getType()))->Fold(&R, nullptr);
1100 Init *rhs = RHS->resolveReferences(R, RV);
1101 return (TernOpInit::get(getOpcode(), lhs, MHS,
1102 rhs, getType()))->Fold(&R, nullptr);
1106 Init *mhs = MHS->resolveReferences(R, RV);
1107 Init *rhs = RHS->resolveReferences(R, RV);
1109 if (LHS != lhs || MHS != mhs || RHS != rhs)
1110 return (TernOpInit::get(getOpcode(), lhs, mhs, rhs,
1111 getType()))->Fold(&R, nullptr);
1112 return Fold(&R, nullptr);
1115 std::string TernOpInit::getAsString() const {
1117 switch (getOpcode()) {
1118 case SUBST: Result = "!subst"; break;
1119 case FOREACH: Result = "!foreach"; break;
1120 case IF: Result = "!if"; break;
1122 return Result + "(" + LHS->getAsString() + ", " + MHS->getAsString() + ", " +
1123 RHS->getAsString() + ")";
1126 RecTy *TypedInit::getFieldType(StringInit *FieldName) const {
1127 if (RecordRecTy *RecordType = dyn_cast<RecordRecTy>(getType()))
1128 if (RecordVal *Field = RecordType->getRecord()->getValue(FieldName))
1129 return Field->getType();
1134 TypedInit::convertInitializerTo(RecTy *Ty) const {
1135 if (isa<IntRecTy>(Ty)) {
1136 if (getType()->typeIsConvertibleTo(Ty))
1137 return const_cast<TypedInit *>(this);
1141 if (isa<StringRecTy>(Ty)) {
1142 if (isa<StringRecTy>(getType()))
1143 return const_cast<TypedInit *>(this);
1147 if (isa<CodeRecTy>(Ty)) {
1148 if (isa<CodeRecTy>(getType()))
1149 return const_cast<TypedInit *>(this);
1153 if (isa<BitRecTy>(Ty)) {
1154 // Accept variable if it is already of bit type!
1155 if (isa<BitRecTy>(getType()))
1156 return const_cast<TypedInit *>(this);
1157 if (auto *BitsTy = dyn_cast<BitsRecTy>(getType())) {
1158 // Accept only bits<1> expression.
1159 if (BitsTy->getNumBits() == 1)
1160 return const_cast<TypedInit *>(this);
1163 // Ternary !if can be converted to bit, but only if both sides are
1164 // convertible to a bit.
1165 if (const auto *TOI = dyn_cast<TernOpInit>(this)) {
1166 if (TOI->getOpcode() == TernOpInit::TernaryOp::IF &&
1167 TOI->getMHS()->convertInitializerTo(BitRecTy::get()) &&
1168 TOI->getRHS()->convertInitializerTo(BitRecTy::get()))
1169 return const_cast<TypedInit *>(this);
1175 if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
1176 if (BRT->getNumBits() == 1 && isa<BitRecTy>(getType()))
1177 return BitsInit::get(const_cast<TypedInit *>(this));
1179 if (getType()->typeIsConvertibleTo(BRT)) {
1180 SmallVector<Init *, 16> NewBits(BRT->getNumBits());
1182 for (unsigned i = 0; i != BRT->getNumBits(); ++i)
1183 NewBits[i] = VarBitInit::get(const_cast<TypedInit *>(this), i);
1184 return BitsInit::get(NewBits);
1190 if (auto *DLRT = dyn_cast<ListRecTy>(Ty)) {
1191 if (auto *SLRT = dyn_cast<ListRecTy>(getType()))
1192 if (SLRT->getElementType()->typeIsConvertibleTo(DLRT->getElementType()))
1193 return const_cast<TypedInit *>(this);
1197 if (auto *DRT = dyn_cast<DagRecTy>(Ty)) {
1198 if (getType()->typeIsConvertibleTo(DRT))
1199 return const_cast<TypedInit *>(this);
1203 if (auto *SRRT = dyn_cast<RecordRecTy>(Ty)) {
1204 // Ensure that this is compatible with Rec.
1205 if (RecordRecTy *DRRT = dyn_cast<RecordRecTy>(getType()))
1206 if (DRRT->getRecord()->isSubClassOf(SRRT->getRecord()) ||
1207 DRRT->getRecord() == SRRT->getRecord())
1208 return const_cast<TypedInit *>(this);
1215 Init *TypedInit::convertInitializerBitRange(ArrayRef<unsigned> Bits) const {
1216 BitsRecTy *T = dyn_cast<BitsRecTy>(getType());
1217 if (!T) return nullptr; // Cannot subscript a non-bits variable.
1218 unsigned NumBits = T->getNumBits();
1220 SmallVector<Init *, 16> NewBits;
1221 NewBits.reserve(Bits.size());
1222 for (unsigned Bit : Bits) {
1226 NewBits.push_back(VarBitInit::get(const_cast<TypedInit *>(this), Bit));
1228 return BitsInit::get(NewBits);
1231 Init *TypedInit::convertInitListSlice(ArrayRef<unsigned> Elements) const {
1232 ListRecTy *T = dyn_cast<ListRecTy>(getType());
1233 if (!T) return nullptr; // Cannot subscript a non-list variable.
1235 if (Elements.size() == 1)
1236 return VarListElementInit::get(const_cast<TypedInit *>(this), Elements[0]);
1238 SmallVector<Init*, 8> ListInits;
1239 ListInits.reserve(Elements.size());
1240 for (unsigned Element : Elements)
1241 ListInits.push_back(VarListElementInit::get(const_cast<TypedInit *>(this),
1243 return ListInit::get(ListInits, T);
1247 VarInit *VarInit::get(StringRef VN, RecTy *T) {
1248 Init *Value = StringInit::get(VN);
1249 return VarInit::get(Value, T);
1252 VarInit *VarInit::get(Init *VN, RecTy *T) {
1253 using Key = std::pair<RecTy *, Init *>;
1254 static DenseMap<Key, VarInit*> ThePool;
1256 Key TheKey(std::make_pair(T, VN));
1258 VarInit *&I = ThePool[TheKey];
1260 I = new(Allocator) VarInit(VN, T);
1264 StringRef VarInit::getName() const {
1265 StringInit *NameString = cast<StringInit>(getNameInit());
1266 return NameString->getValue();
1269 Init *VarInit::getBit(unsigned Bit) const {
1270 if (getType() == BitRecTy::get())
1271 return const_cast<VarInit*>(this);
1272 return VarBitInit::get(const_cast<VarInit*>(this), Bit);
1275 Init *VarInit::resolveListElementReference(Record &R,
1276 const RecordVal *IRV,
1277 unsigned Elt) const {
1278 if (R.isTemplateArg(getNameInit())) return nullptr;
1279 if (IRV && IRV->getNameInit() != getNameInit()) return nullptr;
1281 RecordVal *RV = R.getValue(getNameInit());
1282 assert(RV && "Reference to a non-existent variable?");
1283 ListInit *LI = dyn_cast<ListInit>(RV->getValue());
1285 return VarListElementInit::get(cast<TypedInit>(RV->getValue()), Elt);
1287 if (Elt >= LI->size())
1288 return nullptr; // Out of range reference.
1289 Init *E = LI->getElement(Elt);
1290 // If the element is set to some value, or if we are resolving a reference
1291 // to a specific variable and that variable is explicitly unset, then
1292 // replace the VarListElementInit with it.
1293 if (IRV || !isa<UnsetInit>(E))
1298 RecTy *VarInit::getFieldType(StringInit *FieldName) const {
1299 if (RecordRecTy *RTy = dyn_cast<RecordRecTy>(getType()))
1300 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
1301 return RV->getType();
1305 Init *VarInit::getFieldInit(Record &R, const RecordVal *RV,
1306 StringInit *FieldName) const {
1307 if (isa<RecordRecTy>(getType()))
1308 if (const RecordVal *Val = R.getValue(VarName)) {
1309 if (RV != Val && (RV || isa<UnsetInit>(Val->getValue())))
1311 Init *TheInit = Val->getValue();
1312 assert(TheInit != this && "Infinite loop detected!");
1313 if (Init *I = TheInit->getFieldInit(R, RV, FieldName))
1320 Init *VarInit::resolveReferences(Record &R, const RecordVal *RV) const {
1321 if (RecordVal *Val = R.getValue(VarName))
1322 if (RV == Val || (!RV && !isa<UnsetInit>(Val->getValue())))
1323 return Val->getValue();
1324 return const_cast<VarInit *>(this);
1327 VarBitInit *VarBitInit::get(TypedInit *T, unsigned B) {
1328 using Key = std::pair<TypedInit *, unsigned>;
1329 static DenseMap<Key, VarBitInit*> ThePool;
1331 Key TheKey(std::make_pair(T, B));
1333 VarBitInit *&I = ThePool[TheKey];
1335 I = new(Allocator) VarBitInit(T, B);
1339 Init *VarBitInit::convertInitializerTo(RecTy *Ty) const {
1340 if (isa<BitRecTy>(Ty))
1341 return const_cast<VarBitInit *>(this);
1346 std::string VarBitInit::getAsString() const {
1347 return TI->getAsString() + "{" + utostr(Bit) + "}";
1350 Init *VarBitInit::resolveReferences(Record &R, const RecordVal *RV) const {
1351 Init *I = TI->resolveReferences(R, RV);
1353 return I->getBit(getBitNum());
1355 return const_cast<VarBitInit*>(this);
1358 VarListElementInit *VarListElementInit::get(TypedInit *T,
1360 using Key = std::pair<TypedInit *, unsigned>;
1361 static DenseMap<Key, VarListElementInit*> ThePool;
1363 Key TheKey(std::make_pair(T, E));
1365 VarListElementInit *&I = ThePool[TheKey];
1366 if (!I) I = new(Allocator) VarListElementInit(T, E);
1370 std::string VarListElementInit::getAsString() const {
1371 return TI->getAsString() + "[" + utostr(Element) + "]";
1375 VarListElementInit::resolveReferences(Record &R, const RecordVal *RV) const {
1376 if (Init *I = getVariable()->resolveListElementReference(R, RV,
1379 return const_cast<VarListElementInit *>(this);
1382 Init *VarListElementInit::getBit(unsigned Bit) const {
1383 if (getType() == BitRecTy::get())
1384 return const_cast<VarListElementInit*>(this);
1385 return VarBitInit::get(const_cast<VarListElementInit*>(this), Bit);
1388 Init *VarListElementInit:: resolveListElementReference(Record &R,
1389 const RecordVal *RV,
1390 unsigned Elt) const {
1391 if (Init *Result = TI->resolveListElementReference(R, RV, Element)) {
1392 if (TypedInit *TInit = dyn_cast<TypedInit>(Result)) {
1393 if (Init *Result2 = TInit->resolveListElementReference(R, RV, Elt))
1395 return VarListElementInit::get(TInit, Elt);
1403 DefInit *DefInit::get(Record *R) {
1404 return R->getDefInit();
1407 Init *DefInit::convertInitializerTo(RecTy *Ty) const {
1408 if (auto *RRT = dyn_cast<RecordRecTy>(Ty))
1409 if (getDef()->isSubClassOf(RRT->getRecord()))
1410 return const_cast<DefInit *>(this);
1414 RecTy *DefInit::getFieldType(StringInit *FieldName) const {
1415 if (const RecordVal *RV = Def->getValue(FieldName))
1416 return RV->getType();
1420 Init *DefInit::getFieldInit(Record &R, const RecordVal *RV,
1421 StringInit *FieldName) const {
1422 return Def->getValue(FieldName)->getValue();
1425 std::string DefInit::getAsString() const {
1426 return Def->getName();
1429 FieldInit *FieldInit::get(Init *R, StringInit *FN) {
1430 using Key = std::pair<Init *, StringInit *>;
1431 static DenseMap<Key, FieldInit*> ThePool;
1433 Key TheKey(std::make_pair(R, FN));
1435 FieldInit *&I = ThePool[TheKey];
1436 if (!I) I = new(Allocator) FieldInit(R, FN);
1440 Init *FieldInit::getBit(unsigned Bit) const {
1441 if (getType() == BitRecTy::get())
1442 return const_cast<FieldInit*>(this);
1443 return VarBitInit::get(const_cast<FieldInit*>(this), Bit);
1446 Init *FieldInit::resolveListElementReference(Record &R, const RecordVal *RV,
1447 unsigned Elt) const {
1448 if (Init *ListVal = Rec->getFieldInit(R, RV, FieldName))
1449 if (ListInit *LI = dyn_cast<ListInit>(ListVal)) {
1450 if (Elt >= LI->size()) return nullptr;
1451 Init *E = LI->getElement(Elt);
1453 // If the element is set to some value, or if we are resolving a
1454 // reference to a specific variable and that variable is explicitly
1455 // unset, then replace the VarListElementInit with it.
1456 if (RV || !isa<UnsetInit>(E))
1462 Init *FieldInit::resolveReferences(Record &R, const RecordVal *RV) const {
1463 Init *NewRec = RV ? Rec->resolveReferences(R, RV) : Rec;
1465 if (Init *BitsVal = NewRec->getFieldInit(R, RV, FieldName)) {
1466 Init *BVR = BitsVal->resolveReferences(R, RV);
1467 return BVR->isComplete() ? BVR : const_cast<FieldInit *>(this);
1471 return FieldInit::get(NewRec, FieldName);
1472 return const_cast<FieldInit *>(this);
1475 static void ProfileDagInit(FoldingSetNodeID &ID, Init *V, StringInit *VN,
1476 ArrayRef<Init *> ArgRange,
1477 ArrayRef<StringInit *> NameRange) {
1481 ArrayRef<Init *>::iterator Arg = ArgRange.begin();
1482 ArrayRef<StringInit *>::iterator Name = NameRange.begin();
1483 while (Arg != ArgRange.end()) {
1484 assert(Name != NameRange.end() && "Arg name underflow!");
1485 ID.AddPointer(*Arg++);
1486 ID.AddPointer(*Name++);
1488 assert(Name == NameRange.end() && "Arg name overflow!");
1492 DagInit::get(Init *V, StringInit *VN, ArrayRef<Init *> ArgRange,
1493 ArrayRef<StringInit *> NameRange) {
1494 static FoldingSet<DagInit> ThePool;
1496 FoldingSetNodeID ID;
1497 ProfileDagInit(ID, V, VN, ArgRange, NameRange);
1500 if (DagInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
1503 void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *, StringInit *>(ArgRange.size(), NameRange.size()), alignof(BitsInit));
1504 DagInit *I = new(Mem) DagInit(V, VN, ArgRange.size(), NameRange.size());
1505 std::uninitialized_copy(ArgRange.begin(), ArgRange.end(),
1506 I->getTrailingObjects<Init *>());
1507 std::uninitialized_copy(NameRange.begin(), NameRange.end(),
1508 I->getTrailingObjects<StringInit *>());
1509 ThePool.InsertNode(I, IP);
1514 DagInit::get(Init *V, StringInit *VN,
1515 ArrayRef<std::pair<Init*, StringInit*>> args) {
1516 SmallVector<Init *, 8> Args;
1517 SmallVector<StringInit *, 8> Names;
1519 for (const auto &Arg : args) {
1520 Args.push_back(Arg.first);
1521 Names.push_back(Arg.second);
1524 return DagInit::get(V, VN, Args, Names);
1527 void DagInit::Profile(FoldingSetNodeID &ID) const {
1528 ProfileDagInit(ID, Val, ValName, makeArrayRef(getTrailingObjects<Init *>(), NumArgs), makeArrayRef(getTrailingObjects<StringInit *>(), NumArgNames));
1531 Init *DagInit::convertInitializerTo(RecTy *Ty) const {
1532 if (isa<DagRecTy>(Ty))
1533 return const_cast<DagInit *>(this);
1538 Init *DagInit::resolveReferences(Record &R, const RecordVal *RV) const {
1539 SmallVector<Init*, 8> NewArgs;
1540 NewArgs.reserve(arg_size());
1541 bool ArgsChanged = false;
1542 for (const Init *Arg : getArgs()) {
1543 Init *NewArg = Arg->resolveReferences(R, RV);
1544 NewArgs.push_back(NewArg);
1545 ArgsChanged |= NewArg != Arg;
1548 Init *Op = Val->resolveReferences(R, RV);
1549 if (Op != Val || ArgsChanged)
1550 return DagInit::get(Op, ValName, NewArgs, getArgNames());
1552 return const_cast<DagInit *>(this);
1555 std::string DagInit::getAsString() const {
1556 std::string Result = "(" + Val->getAsString();
1558 Result += ":" + ValName->getAsUnquotedString();
1560 Result += " " + getArg(0)->getAsString();
1561 if (getArgName(0)) Result += ":$" + getArgName(0)->getAsUnquotedString();
1562 for (unsigned i = 1, e = getNumArgs(); i != e; ++i) {
1563 Result += ", " + getArg(i)->getAsString();
1564 if (getArgName(i)) Result += ":$" + getArgName(i)->getAsUnquotedString();
1567 return Result + ")";
1570 //===----------------------------------------------------------------------===//
1571 // Other implementations
1572 //===----------------------------------------------------------------------===//
1574 RecordVal::RecordVal(Init *N, RecTy *T, bool P)
1575 : Name(N), TyAndPrefix(T, P) {
1576 Value = UnsetInit::get()->convertInitializerTo(T);
1577 assert(Value && "Cannot create unset value for current type!");
1580 StringRef RecordVal::getName() const {
1581 return cast<StringInit>(getNameInit())->getValue();
1584 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1585 LLVM_DUMP_METHOD void RecordVal::dump() const { errs() << *this; }
1588 void RecordVal::print(raw_ostream &OS, bool PrintSem) const {
1589 if (getPrefix()) OS << "field ";
1590 OS << *getType() << " " << getNameInitAsString();
1593 OS << " = " << *getValue();
1595 if (PrintSem) OS << ";\n";
1598 unsigned Record::LastID = 0;
1600 void Record::init() {
1603 // Every record potentially has a def at the top. This value is
1604 // replaced with the top-level def name at instantiation time.
1605 addValue(RecordVal(StringInit::get("NAME"), StringRecTy::get(), false));
1608 void Record::checkName() {
1609 // Ensure the record name has string type.
1610 const TypedInit *TypedName = cast<const TypedInit>(Name);
1611 if (!isa<StringRecTy>(TypedName->getType()))
1612 PrintFatalError(getLoc(), "Record name is not a string!");
1615 DefInit *Record::getDefInit() {
1617 TheInit = new(Allocator) DefInit(this, new(Allocator) RecordRecTy(this));
1621 StringRef Record::getName() const {
1622 return cast<StringInit>(Name)->getValue();
1625 void Record::setName(Init *NewName) {
1628 // DO NOT resolve record values to the name at this point because
1629 // there might be default values for arguments of this def. Those
1630 // arguments might not have been resolved yet so we don't want to
1631 // prematurely assume values for those arguments were not passed to
1634 // Nonetheless, it may be that some of this Record's values
1635 // reference the record name. Indeed, the reason for having the
1636 // record name be an Init is to provide this flexibility. The extra
1637 // resolve steps after completely instantiating defs takes care of
1638 // this. See TGParser::ParseDef and TGParser::ParseDefm.
1641 void Record::resolveReferencesTo(const RecordVal *RV) {
1642 for (RecordVal &Value : Values) {
1643 if (RV == &Value) // Skip resolve the same field as the given one
1645 if (Init *V = Value.getValue())
1646 if (Value.setValue(V->resolveReferences(*this, RV)))
1647 PrintFatalError(getLoc(), "Invalid value is found when setting '" +
1648 Value.getNameInitAsString() +
1649 "' after resolving references" +
1650 (RV ? " against '" + RV->getNameInitAsString() +
1651 "' of (" + RV->getValue()->getAsUnquotedString() +
1655 Init *OldName = getNameInit();
1656 Init *NewName = Name->resolveReferences(*this, RV);
1657 if (NewName != OldName) {
1658 // Re-register with RecordKeeper.
1663 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1664 LLVM_DUMP_METHOD void Record::dump() const { errs() << *this; }
1667 raw_ostream &llvm::operator<<(raw_ostream &OS, const Record &R) {
1668 OS << R.getNameInitAsString();
1670 ArrayRef<Init *> TArgs = R.getTemplateArgs();
1671 if (!TArgs.empty()) {
1673 bool NeedComma = false;
1674 for (const Init *TA : TArgs) {
1675 if (NeedComma) OS << ", ";
1677 const RecordVal *RV = R.getValue(TA);
1678 assert(RV && "Template argument record not found??");
1679 RV->print(OS, false);
1685 ArrayRef<std::pair<Record *, SMRange>> SC = R.getSuperClasses();
1688 for (const auto &SuperPair : SC)
1689 OS << " " << SuperPair.first->getNameInitAsString();
1693 for (const RecordVal &Val : R.getValues())
1694 if (Val.getPrefix() && !R.isTemplateArg(Val.getNameInit()))
1696 for (const RecordVal &Val : R.getValues())
1697 if (!Val.getPrefix() && !R.isTemplateArg(Val.getNameInit()))
1703 Init *Record::getValueInit(StringRef FieldName) const {
1704 const RecordVal *R = getValue(FieldName);
1705 if (!R || !R->getValue())
1706 PrintFatalError(getLoc(), "Record `" + getName() +
1707 "' does not have a field named `" + FieldName + "'!\n");
1708 return R->getValue();
1711 StringRef Record::getValueAsString(StringRef FieldName) const {
1712 const RecordVal *R = getValue(FieldName);
1713 if (!R || !R->getValue())
1714 PrintFatalError(getLoc(), "Record `" + getName() +
1715 "' does not have a field named `" + FieldName + "'!\n");
1717 if (StringInit *SI = dyn_cast<StringInit>(R->getValue()))
1718 return SI->getValue();
1719 if (CodeInit *CI = dyn_cast<CodeInit>(R->getValue()))
1720 return CI->getValue();
1722 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1723 FieldName + "' does not have a string initializer!");
1726 BitsInit *Record::getValueAsBitsInit(StringRef FieldName) const {
1727 const RecordVal *R = getValue(FieldName);
1728 if (!R || !R->getValue())
1729 PrintFatalError(getLoc(), "Record `" + getName() +
1730 "' does not have a field named `" + FieldName + "'!\n");
1732 if (BitsInit *BI = dyn_cast<BitsInit>(R->getValue()))
1734 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1735 FieldName + "' does not have a BitsInit initializer!");
1738 ListInit *Record::getValueAsListInit(StringRef FieldName) const {
1739 const RecordVal *R = getValue(FieldName);
1740 if (!R || !R->getValue())
1741 PrintFatalError(getLoc(), "Record `" + getName() +
1742 "' does not have a field named `" + FieldName + "'!\n");
1744 if (ListInit *LI = dyn_cast<ListInit>(R->getValue()))
1746 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1747 FieldName + "' does not have a list initializer!");
1750 std::vector<Record*>
1751 Record::getValueAsListOfDefs(StringRef FieldName) const {
1752 ListInit *List = getValueAsListInit(FieldName);
1753 std::vector<Record*> Defs;
1754 for (Init *I : List->getValues()) {
1755 if (DefInit *DI = dyn_cast<DefInit>(I))
1756 Defs.push_back(DI->getDef());
1758 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1759 FieldName + "' list is not entirely DefInit!");
1764 int64_t Record::getValueAsInt(StringRef FieldName) const {
1765 const RecordVal *R = getValue(FieldName);
1766 if (!R || !R->getValue())
1767 PrintFatalError(getLoc(), "Record `" + getName() +
1768 "' does not have a field named `" + FieldName + "'!\n");
1770 if (IntInit *II = dyn_cast<IntInit>(R->getValue()))
1771 return II->getValue();
1772 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1773 FieldName + "' does not have an int initializer!");
1776 std::vector<int64_t>
1777 Record::getValueAsListOfInts(StringRef FieldName) const {
1778 ListInit *List = getValueAsListInit(FieldName);
1779 std::vector<int64_t> Ints;
1780 for (Init *I : List->getValues()) {
1781 if (IntInit *II = dyn_cast<IntInit>(I))
1782 Ints.push_back(II->getValue());
1784 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1785 FieldName + "' does not have a list of ints initializer!");
1790 std::vector<StringRef>
1791 Record::getValueAsListOfStrings(StringRef FieldName) const {
1792 ListInit *List = getValueAsListInit(FieldName);
1793 std::vector<StringRef> Strings;
1794 for (Init *I : List->getValues()) {
1795 if (StringInit *SI = dyn_cast<StringInit>(I))
1796 Strings.push_back(SI->getValue());
1798 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1799 FieldName + "' does not have a list of strings initializer!");
1804 Record *Record::getValueAsDef(StringRef FieldName) const {
1805 const RecordVal *R = getValue(FieldName);
1806 if (!R || !R->getValue())
1807 PrintFatalError(getLoc(), "Record `" + getName() +
1808 "' does not have a field named `" + FieldName + "'!\n");
1810 if (DefInit *DI = dyn_cast<DefInit>(R->getValue()))
1811 return DI->getDef();
1812 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1813 FieldName + "' does not have a def initializer!");
1816 bool Record::getValueAsBit(StringRef FieldName) const {
1817 const RecordVal *R = getValue(FieldName);
1818 if (!R || !R->getValue())
1819 PrintFatalError(getLoc(), "Record `" + getName() +
1820 "' does not have a field named `" + FieldName + "'!\n");
1822 if (BitInit *BI = dyn_cast<BitInit>(R->getValue()))
1823 return BI->getValue();
1824 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1825 FieldName + "' does not have a bit initializer!");
1828 bool Record::getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const {
1829 const RecordVal *R = getValue(FieldName);
1830 if (!R || !R->getValue())
1831 PrintFatalError(getLoc(), "Record `" + getName() +
1832 "' does not have a field named `" + FieldName.str() + "'!\n");
1834 if (isa<UnsetInit>(R->getValue())) {
1839 if (BitInit *BI = dyn_cast<BitInit>(R->getValue()))
1840 return BI->getValue();
1841 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1842 FieldName + "' does not have a bit initializer!");
1845 DagInit *Record::getValueAsDag(StringRef FieldName) const {
1846 const RecordVal *R = getValue(FieldName);
1847 if (!R || !R->getValue())
1848 PrintFatalError(getLoc(), "Record `" + getName() +
1849 "' does not have a field named `" + FieldName + "'!\n");
1851 if (DagInit *DI = dyn_cast<DagInit>(R->getValue()))
1853 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1854 FieldName + "' does not have a dag initializer!");
1857 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1858 LLVM_DUMP_METHOD void MultiClass::dump() const {
1859 errs() << "Record:\n";
1862 errs() << "Defs:\n";
1863 for (const auto &Proto : DefPrototypes)
1867 LLVM_DUMP_METHOD void RecordKeeper::dump() const { errs() << *this; }
1870 raw_ostream &llvm::operator<<(raw_ostream &OS, const RecordKeeper &RK) {
1871 OS << "------------- Classes -----------------\n";
1872 for (const auto &C : RK.getClasses())
1873 OS << "class " << *C.second;
1875 OS << "------------- Defs -----------------\n";
1876 for (const auto &D : RK.getDefs())
1877 OS << "def " << *D.second;
1881 std::vector<Record *>
1882 RecordKeeper::getAllDerivedDefinitions(StringRef ClassName) const {
1883 Record *Class = getClass(ClassName);
1885 PrintFatalError("ERROR: Couldn't find the `" + ClassName + "' class!\n");
1887 std::vector<Record*> Defs;
1888 for (const auto &D : getDefs())
1889 if (D.second->isSubClassOf(Class))
1890 Defs.push_back(D.second.get());
1895 static Init *GetStrConcat(Init *I0, Init *I1) {
1896 // Shortcut for the common case of concatenating two strings.
1897 if (const StringInit *I0s = dyn_cast<StringInit>(I0))
1898 if (const StringInit *I1s = dyn_cast<StringInit>(I1))
1899 return ConcatStringInits(I0s, I1s);
1900 return BinOpInit::get(BinOpInit::STRCONCAT, I0, I1, StringRecTy::get());
1903 Init *llvm::QualifyName(Record &CurRec, MultiClass *CurMultiClass,
1904 Init *Name, StringRef Scoper) {
1905 Init *NewName = GetStrConcat(CurRec.getNameInit(), StringInit::get(Scoper));
1906 NewName = GetStrConcat(NewName, Name);
1907 if (CurMultiClass && Scoper != "::") {
1908 Init *Prefix = GetStrConcat(CurMultiClass->Rec.getNameInit(),
1909 StringInit::get("::"));
1910 NewName = GetStrConcat(Prefix, NewName);
1913 if (BinOpInit *BinOp = dyn_cast<BinOpInit>(NewName))
1914 NewName = BinOp->Fold(&CurRec, CurMultiClass);