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/Config/llvm-config.h"
23 #include "llvm/Support/Allocator.h"
24 #include "llvm/Support/Casting.h"
25 #include "llvm/Support/Compiler.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/SMLoc.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/TableGen/Error.h"
30 #include "llvm/TableGen/Record.h"
40 static BumpPtrAllocator Allocator
;
42 //===----------------------------------------------------------------------===//
43 // Type implementations
44 //===----------------------------------------------------------------------===//
46 BitRecTy
BitRecTy::Shared
;
47 CodeRecTy
CodeRecTy::Shared
;
48 IntRecTy
IntRecTy::Shared
;
49 StringRecTy
StringRecTy::Shared
;
50 DagRecTy
DagRecTy::Shared
;
52 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
53 LLVM_DUMP_METHOD
void RecTy::dump() const { print(errs()); }
56 ListRecTy
*RecTy::getListTy() {
58 ListTy
= new(Allocator
) ListRecTy(this);
62 bool RecTy::typeIsConvertibleTo(const RecTy
*RHS
) const {
63 assert(RHS
&& "NULL pointer");
64 return Kind
== RHS
->getRecTyKind();
67 bool RecTy::typeIsA(const RecTy
*RHS
) const { return this == RHS
; }
69 bool BitRecTy::typeIsConvertibleTo(const RecTy
*RHS
) const{
70 if (RecTy::typeIsConvertibleTo(RHS
) || RHS
->getRecTyKind() == IntRecTyKind
)
72 if (const BitsRecTy
*BitsTy
= dyn_cast
<BitsRecTy
>(RHS
))
73 return BitsTy
->getNumBits() == 1;
77 BitsRecTy
*BitsRecTy::get(unsigned Sz
) {
78 static std::vector
<BitsRecTy
*> Shared
;
79 if (Sz
>= Shared
.size())
80 Shared
.resize(Sz
+ 1);
81 BitsRecTy
*&Ty
= Shared
[Sz
];
83 Ty
= new(Allocator
) BitsRecTy(Sz
);
87 std::string
BitsRecTy::getAsString() const {
88 return "bits<" + utostr(Size
) + ">";
91 bool BitsRecTy::typeIsConvertibleTo(const RecTy
*RHS
) const {
92 if (RecTy::typeIsConvertibleTo(RHS
)) //argument and the sender are same type
93 return cast
<BitsRecTy
>(RHS
)->Size
== Size
;
94 RecTyKind kind
= RHS
->getRecTyKind();
95 return (kind
== BitRecTyKind
&& Size
== 1) || (kind
== IntRecTyKind
);
98 bool BitsRecTy::typeIsA(const RecTy
*RHS
) const {
99 if (const BitsRecTy
*RHSb
= dyn_cast
<BitsRecTy
>(RHS
))
100 return RHSb
->Size
== Size
;
104 bool IntRecTy::typeIsConvertibleTo(const RecTy
*RHS
) const {
105 RecTyKind kind
= RHS
->getRecTyKind();
106 return kind
==BitRecTyKind
|| kind
==BitsRecTyKind
|| kind
==IntRecTyKind
;
109 bool CodeRecTy::typeIsConvertibleTo(const RecTy
*RHS
) const {
110 RecTyKind Kind
= RHS
->getRecTyKind();
111 return Kind
== CodeRecTyKind
|| Kind
== StringRecTyKind
;
114 std::string
StringRecTy::getAsString() const {
118 bool StringRecTy::typeIsConvertibleTo(const RecTy
*RHS
) const {
119 RecTyKind Kind
= RHS
->getRecTyKind();
120 return Kind
== StringRecTyKind
|| Kind
== CodeRecTyKind
;
123 std::string
ListRecTy::getAsString() const {
124 return "list<" + Ty
->getAsString() + ">";
127 bool ListRecTy::typeIsConvertibleTo(const RecTy
*RHS
) const {
128 if (const auto *ListTy
= dyn_cast
<ListRecTy
>(RHS
))
129 return Ty
->typeIsConvertibleTo(ListTy
->getElementType());
133 bool ListRecTy::typeIsA(const RecTy
*RHS
) const {
134 if (const ListRecTy
*RHSl
= dyn_cast
<ListRecTy
>(RHS
))
135 return getElementType()->typeIsA(RHSl
->getElementType());
139 std::string
DagRecTy::getAsString() const {
143 static void ProfileRecordRecTy(FoldingSetNodeID
&ID
,
144 ArrayRef
<Record
*> Classes
) {
145 ID
.AddInteger(Classes
.size());
146 for (Record
*R
: Classes
)
150 RecordRecTy
*RecordRecTy::get(ArrayRef
<Record
*> UnsortedClasses
) {
151 if (UnsortedClasses
.empty()) {
152 static RecordRecTy
AnyRecord(0);
156 FoldingSet
<RecordRecTy
> &ThePool
=
157 UnsortedClasses
[0]->getRecords().RecordTypePool
;
159 SmallVector
<Record
*, 4> Classes(UnsortedClasses
.begin(),
160 UnsortedClasses
.end());
161 llvm::sort(Classes
, [](Record
*LHS
, Record
*RHS
) {
162 return LHS
->getNameInitAsString() < RHS
->getNameInitAsString();
166 ProfileRecordRecTy(ID
, Classes
);
169 if (RecordRecTy
*Ty
= ThePool
.FindNodeOrInsertPos(ID
, IP
))
173 // Check for redundancy.
174 for (unsigned i
= 0; i
< Classes
.size(); ++i
) {
175 for (unsigned j
= 0; j
< Classes
.size(); ++j
) {
176 assert(i
== j
|| !Classes
[i
]->isSubClassOf(Classes
[j
]));
178 assert(&Classes
[0]->getRecords() == &Classes
[i
]->getRecords());
182 void *Mem
= Allocator
.Allocate(totalSizeToAlloc
<Record
*>(Classes
.size()),
183 alignof(RecordRecTy
));
184 RecordRecTy
*Ty
= new(Mem
) RecordRecTy(Classes
.size());
185 std::uninitialized_copy(Classes
.begin(), Classes
.end(),
186 Ty
->getTrailingObjects
<Record
*>());
187 ThePool
.InsertNode(Ty
, IP
);
191 void RecordRecTy::Profile(FoldingSetNodeID
&ID
) const {
192 ProfileRecordRecTy(ID
, getClasses());
195 std::string
RecordRecTy::getAsString() const {
197 return getClasses()[0]->getNameInitAsString();
199 std::string Str
= "{";
201 for (Record
*R
: getClasses()) {
205 Str
+= R
->getNameInitAsString();
211 bool RecordRecTy::isSubClassOf(Record
*Class
) const {
212 return llvm::any_of(getClasses(), [Class
](Record
*MySuperClass
) {
213 return MySuperClass
== Class
||
214 MySuperClass
->isSubClassOf(Class
);
218 bool RecordRecTy::typeIsConvertibleTo(const RecTy
*RHS
) const {
222 const RecordRecTy
*RTy
= dyn_cast
<RecordRecTy
>(RHS
);
226 return llvm::all_of(RTy
->getClasses(), [this](Record
*TargetClass
) {
227 return isSubClassOf(TargetClass
);
231 bool RecordRecTy::typeIsA(const RecTy
*RHS
) const {
232 return typeIsConvertibleTo(RHS
);
235 static RecordRecTy
*resolveRecordTypes(RecordRecTy
*T1
, RecordRecTy
*T2
) {
236 SmallVector
<Record
*, 4> CommonSuperClasses
;
237 SmallVector
<Record
*, 4> Stack
;
239 Stack
.insert(Stack
.end(), T1
->classes_begin(), T1
->classes_end());
241 while (!Stack
.empty()) {
242 Record
*R
= Stack
.back();
245 if (T2
->isSubClassOf(R
)) {
246 CommonSuperClasses
.push_back(R
);
248 R
->getDirectSuperClasses(Stack
);
252 return RecordRecTy::get(CommonSuperClasses
);
255 RecTy
*llvm::resolveTypes(RecTy
*T1
, RecTy
*T2
) {
259 if (RecordRecTy
*RecTy1
= dyn_cast
<RecordRecTy
>(T1
)) {
260 if (RecordRecTy
*RecTy2
= dyn_cast
<RecordRecTy
>(T2
))
261 return resolveRecordTypes(RecTy1
, RecTy2
);
264 if (T1
->typeIsConvertibleTo(T2
))
266 if (T2
->typeIsConvertibleTo(T1
))
269 if (ListRecTy
*ListTy1
= dyn_cast
<ListRecTy
>(T1
)) {
270 if (ListRecTy
*ListTy2
= dyn_cast
<ListRecTy
>(T2
)) {
271 RecTy
* NewType
= resolveTypes(ListTy1
->getElementType(),
272 ListTy2
->getElementType());
274 return NewType
->getListTy();
281 //===----------------------------------------------------------------------===//
282 // Initializer implementations
283 //===----------------------------------------------------------------------===//
285 void Init::anchor() {}
287 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
288 LLVM_DUMP_METHOD
void Init::dump() const { return print(errs()); }
291 UnsetInit
*UnsetInit::get() {
292 static UnsetInit TheInit
;
296 Init
*UnsetInit::getCastTo(RecTy
*Ty
) const {
297 return const_cast<UnsetInit
*>(this);
300 Init
*UnsetInit::convertInitializerTo(RecTy
*Ty
) const {
301 return const_cast<UnsetInit
*>(this);
304 BitInit
*BitInit::get(bool V
) {
305 static BitInit
True(true);
306 static BitInit
False(false);
308 return V
? &True
: &False
;
311 Init
*BitInit::convertInitializerTo(RecTy
*Ty
) const {
312 if (isa
<BitRecTy
>(Ty
))
313 return const_cast<BitInit
*>(this);
315 if (isa
<IntRecTy
>(Ty
))
316 return IntInit::get(getValue());
318 if (auto *BRT
= dyn_cast
<BitsRecTy
>(Ty
)) {
319 // Can only convert single bit.
320 if (BRT
->getNumBits() == 1)
321 return BitsInit::get(const_cast<BitInit
*>(this));
328 ProfileBitsInit(FoldingSetNodeID
&ID
, ArrayRef
<Init
*> Range
) {
329 ID
.AddInteger(Range
.size());
331 for (Init
*I
: Range
)
335 BitsInit
*BitsInit::get(ArrayRef
<Init
*> Range
) {
336 static FoldingSet
<BitsInit
> ThePool
;
339 ProfileBitsInit(ID
, Range
);
342 if (BitsInit
*I
= ThePool
.FindNodeOrInsertPos(ID
, IP
))
345 void *Mem
= Allocator
.Allocate(totalSizeToAlloc
<Init
*>(Range
.size()),
347 BitsInit
*I
= new(Mem
) BitsInit(Range
.size());
348 std::uninitialized_copy(Range
.begin(), Range
.end(),
349 I
->getTrailingObjects
<Init
*>());
350 ThePool
.InsertNode(I
, IP
);
354 void BitsInit::Profile(FoldingSetNodeID
&ID
) const {
355 ProfileBitsInit(ID
, makeArrayRef(getTrailingObjects
<Init
*>(), NumBits
));
358 Init
*BitsInit::convertInitializerTo(RecTy
*Ty
) const {
359 if (isa
<BitRecTy
>(Ty
)) {
360 if (getNumBits() != 1) return nullptr; // Only accept if just one bit!
364 if (auto *BRT
= dyn_cast
<BitsRecTy
>(Ty
)) {
365 // If the number of bits is right, return it. Otherwise we need to expand
367 if (getNumBits() != BRT
->getNumBits()) return nullptr;
368 return const_cast<BitsInit
*>(this);
371 if (isa
<IntRecTy
>(Ty
)) {
373 for (unsigned i
= 0, e
= getNumBits(); i
!= e
; ++i
)
374 if (auto *Bit
= dyn_cast
<BitInit
>(getBit(i
)))
375 Result
|= static_cast<int64_t>(Bit
->getValue()) << i
;
378 return IntInit::get(Result
);
385 BitsInit::convertInitializerBitRange(ArrayRef
<unsigned> Bits
) const {
386 SmallVector
<Init
*, 16> NewBits(Bits
.size());
388 for (unsigned i
= 0, e
= Bits
.size(); i
!= e
; ++i
) {
389 if (Bits
[i
] >= getNumBits())
391 NewBits
[i
] = getBit(Bits
[i
]);
393 return BitsInit::get(NewBits
);
396 bool BitsInit::isConcrete() const {
397 for (unsigned i
= 0, e
= getNumBits(); i
!= e
; ++i
) {
398 if (!getBit(i
)->isConcrete())
404 std::string
BitsInit::getAsString() const {
405 std::string Result
= "{ ";
406 for (unsigned i
= 0, e
= getNumBits(); i
!= e
; ++i
) {
407 if (i
) Result
+= ", ";
408 if (Init
*Bit
= getBit(e
-i
-1))
409 Result
+= Bit
->getAsString();
413 return Result
+ " }";
416 // resolveReferences - If there are any field references that refer to fields
417 // that have been filled in, we can propagate the values now.
418 Init
*BitsInit::resolveReferences(Resolver
&R
) const {
419 bool Changed
= false;
420 SmallVector
<Init
*, 16> NewBits(getNumBits());
422 Init
*CachedBitVarRef
= nullptr;
423 Init
*CachedBitVarResolved
= nullptr;
425 for (unsigned i
= 0, e
= getNumBits(); i
!= e
; ++i
) {
426 Init
*CurBit
= getBit(i
);
427 Init
*NewBit
= CurBit
;
429 if (VarBitInit
*CurBitVar
= dyn_cast
<VarBitInit
>(CurBit
)) {
430 if (CurBitVar
->getBitVar() != CachedBitVarRef
) {
431 CachedBitVarRef
= CurBitVar
->getBitVar();
432 CachedBitVarResolved
= CachedBitVarRef
->resolveReferences(R
);
435 NewBit
= CachedBitVarResolved
->getBit(CurBitVar
->getBitNum());
437 // getBit(0) implicitly converts int and bits<1> values to bit.
438 NewBit
= CurBit
->resolveReferences(R
)->getBit(0);
441 if (isa
<UnsetInit
>(NewBit
) && R
.keepUnsetBits())
444 Changed
|= CurBit
!= NewBit
;
448 return BitsInit::get(NewBits
);
450 return const_cast<BitsInit
*>(this);
453 IntInit
*IntInit::get(int64_t V
) {
454 static DenseMap
<int64_t, IntInit
*> ThePool
;
456 IntInit
*&I
= ThePool
[V
];
457 if (!I
) I
= new(Allocator
) IntInit(V
);
461 std::string
IntInit::getAsString() const {
462 return itostr(Value
);
465 static bool canFitInBitfield(int64_t Value
, unsigned NumBits
) {
466 // For example, with NumBits == 4, we permit Values from [-7 .. 15].
467 return (NumBits
>= sizeof(Value
) * 8) ||
468 (Value
>> NumBits
== 0) || (Value
>> (NumBits
-1) == -1);
471 Init
*IntInit::convertInitializerTo(RecTy
*Ty
) const {
472 if (isa
<IntRecTy
>(Ty
))
473 return const_cast<IntInit
*>(this);
475 if (isa
<BitRecTy
>(Ty
)) {
476 int64_t Val
= getValue();
477 if (Val
!= 0 && Val
!= 1) return nullptr; // Only accept 0 or 1 for a bit!
478 return BitInit::get(Val
!= 0);
481 if (auto *BRT
= dyn_cast
<BitsRecTy
>(Ty
)) {
482 int64_t Value
= getValue();
483 // Make sure this bitfield is large enough to hold the integer value.
484 if (!canFitInBitfield(Value
, BRT
->getNumBits()))
487 SmallVector
<Init
*, 16> NewBits(BRT
->getNumBits());
488 for (unsigned i
= 0; i
!= BRT
->getNumBits(); ++i
)
489 NewBits
[i
] = BitInit::get(Value
& ((i
< 64) ? (1LL << i
) : 0));
491 return BitsInit::get(NewBits
);
498 IntInit::convertInitializerBitRange(ArrayRef
<unsigned> Bits
) const {
499 SmallVector
<Init
*, 16> NewBits(Bits
.size());
501 for (unsigned i
= 0, e
= Bits
.size(); i
!= e
; ++i
) {
505 NewBits
[i
] = BitInit::get(Value
& (INT64_C(1) << Bits
[i
]));
507 return BitsInit::get(NewBits
);
510 CodeInit
*CodeInit::get(StringRef V
) {
511 static StringMap
<CodeInit
*, BumpPtrAllocator
&> ThePool(Allocator
);
513 auto &Entry
= *ThePool
.insert(std::make_pair(V
, nullptr)).first
;
515 Entry
.second
= new(Allocator
) CodeInit(Entry
.getKey());
519 StringInit
*StringInit::get(StringRef V
) {
520 static StringMap
<StringInit
*, BumpPtrAllocator
&> ThePool(Allocator
);
522 auto &Entry
= *ThePool
.insert(std::make_pair(V
, nullptr)).first
;
524 Entry
.second
= new(Allocator
) StringInit(Entry
.getKey());
528 Init
*StringInit::convertInitializerTo(RecTy
*Ty
) const {
529 if (isa
<StringRecTy
>(Ty
))
530 return const_cast<StringInit
*>(this);
531 if (isa
<CodeRecTy
>(Ty
))
532 return CodeInit::get(getValue());
537 Init
*CodeInit::convertInitializerTo(RecTy
*Ty
) const {
538 if (isa
<CodeRecTy
>(Ty
))
539 return const_cast<CodeInit
*>(this);
540 if (isa
<StringRecTy
>(Ty
))
541 return StringInit::get(getValue());
546 static void ProfileListInit(FoldingSetNodeID
&ID
,
547 ArrayRef
<Init
*> Range
,
549 ID
.AddInteger(Range
.size());
550 ID
.AddPointer(EltTy
);
552 for (Init
*I
: Range
)
556 ListInit
*ListInit::get(ArrayRef
<Init
*> Range
, RecTy
*EltTy
) {
557 static FoldingSet
<ListInit
> ThePool
;
560 ProfileListInit(ID
, Range
, EltTy
);
563 if (ListInit
*I
= ThePool
.FindNodeOrInsertPos(ID
, IP
))
566 assert(Range
.empty() || !isa
<TypedInit
>(Range
[0]) ||
567 cast
<TypedInit
>(Range
[0])->getType()->typeIsConvertibleTo(EltTy
));
569 void *Mem
= Allocator
.Allocate(totalSizeToAlloc
<Init
*>(Range
.size()),
571 ListInit
*I
= new(Mem
) ListInit(Range
.size(), EltTy
);
572 std::uninitialized_copy(Range
.begin(), Range
.end(),
573 I
->getTrailingObjects
<Init
*>());
574 ThePool
.InsertNode(I
, IP
);
578 void ListInit::Profile(FoldingSetNodeID
&ID
) const {
579 RecTy
*EltTy
= cast
<ListRecTy
>(getType())->getElementType();
581 ProfileListInit(ID
, getValues(), EltTy
);
584 Init
*ListInit::convertInitializerTo(RecTy
*Ty
) const {
586 return const_cast<ListInit
*>(this);
588 if (auto *LRT
= dyn_cast
<ListRecTy
>(Ty
)) {
589 SmallVector
<Init
*, 8> Elements
;
590 Elements
.reserve(getValues().size());
592 // Verify that all of the elements of the list are subclasses of the
593 // appropriate class!
594 bool Changed
= false;
595 RecTy
*ElementType
= LRT
->getElementType();
596 for (Init
*I
: getValues())
597 if (Init
*CI
= I
->convertInitializerTo(ElementType
)) {
598 Elements
.push_back(CI
);
605 return const_cast<ListInit
*>(this);
606 return ListInit::get(Elements
, ElementType
);
612 Init
*ListInit::convertInitListSlice(ArrayRef
<unsigned> Elements
) const {
613 SmallVector
<Init
*, 8> Vals
;
614 Vals
.reserve(Elements
.size());
615 for (unsigned Element
: Elements
) {
616 if (Element
>= size())
618 Vals
.push_back(getElement(Element
));
620 return ListInit::get(Vals
, getElementType());
623 Record
*ListInit::getElementAsRecord(unsigned i
) const {
624 assert(i
< NumValues
&& "List element index out of range!");
625 DefInit
*DI
= dyn_cast
<DefInit
>(getElement(i
));
627 PrintFatalError("Expected record in list!");
631 Init
*ListInit::resolveReferences(Resolver
&R
) const {
632 SmallVector
<Init
*, 8> Resolved
;
633 Resolved
.reserve(size());
634 bool Changed
= false;
636 for (Init
*CurElt
: getValues()) {
637 Init
*E
= CurElt
->resolveReferences(R
);
638 Changed
|= E
!= CurElt
;
639 Resolved
.push_back(E
);
643 return ListInit::get(Resolved
, getElementType());
644 return const_cast<ListInit
*>(this);
647 bool ListInit::isConcrete() const {
648 for (Init
*Element
: *this) {
649 if (!Element
->isConcrete())
655 std::string
ListInit::getAsString() const {
656 std::string Result
= "[";
657 const char *sep
= "";
658 for (Init
*Element
: *this) {
661 Result
+= Element
->getAsString();
666 Init
*OpInit::getBit(unsigned Bit
) const {
667 if (getType() == BitRecTy::get())
668 return const_cast<OpInit
*>(this);
669 return VarBitInit::get(const_cast<OpInit
*>(this), Bit
);
673 ProfileUnOpInit(FoldingSetNodeID
&ID
, unsigned Opcode
, Init
*Op
, RecTy
*Type
) {
674 ID
.AddInteger(Opcode
);
679 UnOpInit
*UnOpInit::get(UnaryOp Opc
, Init
*LHS
, RecTy
*Type
) {
680 static FoldingSet
<UnOpInit
> ThePool
;
683 ProfileUnOpInit(ID
, Opc
, LHS
, Type
);
686 if (UnOpInit
*I
= ThePool
.FindNodeOrInsertPos(ID
, IP
))
689 UnOpInit
*I
= new(Allocator
) UnOpInit(Opc
, LHS
, Type
);
690 ThePool
.InsertNode(I
, IP
);
694 void UnOpInit::Profile(FoldingSetNodeID
&ID
) const {
695 ProfileUnOpInit(ID
, getOpcode(), getOperand(), getType());
698 Init
*UnOpInit::Fold(Record
*CurRec
, bool IsFinal
) const {
699 switch (getOpcode()) {
701 if (isa
<StringRecTy
>(getType())) {
702 if (StringInit
*LHSs
= dyn_cast
<StringInit
>(LHS
))
705 if (DefInit
*LHSd
= dyn_cast
<DefInit
>(LHS
))
706 return StringInit::get(LHSd
->getAsString());
708 if (IntInit
*LHSi
= dyn_cast
<IntInit
>(LHS
))
709 return StringInit::get(LHSi
->getAsString());
710 } else if (isa
<RecordRecTy
>(getType())) {
711 if (StringInit
*Name
= dyn_cast
<StringInit
>(LHS
)) {
712 if (!CurRec
&& !IsFinal
)
714 assert(CurRec
&& "NULL pointer");
717 // Self-references are allowed, but their resolution is delayed until
718 // the final resolve to ensure that we get the correct type for them.
719 if (Name
== CurRec
->getNameInit()) {
724 D
= CurRec
->getRecords().getDef(Name
->getValue());
727 PrintFatalError(CurRec
->getLoc(),
728 Twine("Undefined reference to record: '") +
729 Name
->getValue() + "'\n");
734 DefInit
*DI
= DefInit::get(D
);
735 if (!DI
->getType()->typeIsA(getType())) {
736 PrintFatalError(CurRec
->getLoc(),
737 Twine("Expected type '") +
738 getType()->getAsString() + "', got '" +
739 DI
->getType()->getAsString() + "' in: " +
740 getAsString() + "\n");
746 if (Init
*NewInit
= LHS
->convertInitializerTo(getType()))
751 if (ListInit
*LHSl
= dyn_cast
<ListInit
>(LHS
)) {
752 assert(!LHSl
->empty() && "Empty list in head");
753 return LHSl
->getElement(0);
758 if (ListInit
*LHSl
= dyn_cast
<ListInit
>(LHS
)) {
759 assert(!LHSl
->empty() && "Empty list in tail");
760 // Note the +1. We can't just pass the result of getValues()
762 return ListInit::get(LHSl
->getValues().slice(1), LHSl
->getElementType());
767 if (ListInit
*LHSl
= dyn_cast
<ListInit
>(LHS
))
768 return IntInit::get(LHSl
->size());
772 if (ListInit
*LHSl
= dyn_cast
<ListInit
>(LHS
))
773 return IntInit::get(LHSl
->empty());
774 if (StringInit
*LHSs
= dyn_cast
<StringInit
>(LHS
))
775 return IntInit::get(LHSs
->getValue().empty());
778 return const_cast<UnOpInit
*>(this);
781 Init
*UnOpInit::resolveReferences(Resolver
&R
) const {
782 Init
*lhs
= LHS
->resolveReferences(R
);
784 if (LHS
!= lhs
|| (R
.isFinal() && getOpcode() == CAST
))
785 return (UnOpInit::get(getOpcode(), lhs
, getType()))
786 ->Fold(R
.getCurrentRecord(), R
.isFinal());
787 return const_cast<UnOpInit
*>(this);
790 std::string
UnOpInit::getAsString() const {
792 switch (getOpcode()) {
793 case CAST
: Result
= "!cast<" + getType()->getAsString() + ">"; break;
794 case HEAD
: Result
= "!head"; break;
795 case TAIL
: Result
= "!tail"; break;
796 case SIZE
: Result
= "!size"; break;
797 case EMPTY
: Result
= "!empty"; break;
799 return Result
+ "(" + LHS
->getAsString() + ")";
803 ProfileBinOpInit(FoldingSetNodeID
&ID
, unsigned Opcode
, Init
*LHS
, Init
*RHS
,
805 ID
.AddInteger(Opcode
);
811 BinOpInit
*BinOpInit::get(BinaryOp Opc
, Init
*LHS
,
812 Init
*RHS
, RecTy
*Type
) {
813 static FoldingSet
<BinOpInit
> ThePool
;
816 ProfileBinOpInit(ID
, Opc
, LHS
, RHS
, Type
);
819 if (BinOpInit
*I
= ThePool
.FindNodeOrInsertPos(ID
, IP
))
822 BinOpInit
*I
= new(Allocator
) BinOpInit(Opc
, LHS
, RHS
, Type
);
823 ThePool
.InsertNode(I
, IP
);
827 void BinOpInit::Profile(FoldingSetNodeID
&ID
) const {
828 ProfileBinOpInit(ID
, getOpcode(), getLHS(), getRHS(), getType());
831 static StringInit
*ConcatStringInits(const StringInit
*I0
,
832 const StringInit
*I1
) {
833 SmallString
<80> Concat(I0
->getValue());
834 Concat
.append(I1
->getValue());
835 return StringInit::get(Concat
);
838 Init
*BinOpInit::getStrConcat(Init
*I0
, Init
*I1
) {
839 // Shortcut for the common case of concatenating two strings.
840 if (const StringInit
*I0s
= dyn_cast
<StringInit
>(I0
))
841 if (const StringInit
*I1s
= dyn_cast
<StringInit
>(I1
))
842 return ConcatStringInits(I0s
, I1s
);
843 return BinOpInit::get(BinOpInit::STRCONCAT
, I0
, I1
, StringRecTy::get());
846 Init
*BinOpInit::Fold(Record
*CurRec
) const {
847 switch (getOpcode()) {
849 DagInit
*LHSs
= dyn_cast
<DagInit
>(LHS
);
850 DagInit
*RHSs
= dyn_cast
<DagInit
>(RHS
);
852 DefInit
*LOp
= dyn_cast
<DefInit
>(LHSs
->getOperator());
853 DefInit
*ROp
= dyn_cast
<DefInit
>(RHSs
->getOperator());
856 if (LOp
->getDef() != ROp
->getDef()) {
857 PrintFatalError(Twine("Concatenated Dag operators do not match: '") +
858 LHSs
->getAsString() + "' vs. '" + RHSs
->getAsString() +
861 SmallVector
<Init
*, 8> Args
;
862 SmallVector
<StringInit
*, 8> ArgNames
;
863 for (unsigned i
= 0, e
= LHSs
->getNumArgs(); i
!= e
; ++i
) {
864 Args
.push_back(LHSs
->getArg(i
));
865 ArgNames
.push_back(LHSs
->getArgName(i
));
867 for (unsigned i
= 0, e
= RHSs
->getNumArgs(); i
!= e
; ++i
) {
868 Args
.push_back(RHSs
->getArg(i
));
869 ArgNames
.push_back(RHSs
->getArgName(i
));
871 return DagInit::get(LHSs
->getOperator(), nullptr, Args
, ArgNames
);
876 ListInit
*LHSs
= dyn_cast
<ListInit
>(LHS
);
877 ListInit
*RHSs
= dyn_cast
<ListInit
>(RHS
);
879 SmallVector
<Init
*, 8> Args
;
880 Args
.insert(Args
.end(), LHSs
->begin(), LHSs
->end());
881 Args
.insert(Args
.end(), RHSs
->begin(), RHSs
->end());
882 return ListInit::get(Args
, LHSs
->getElementType());
887 StringInit
*LHSs
= dyn_cast
<StringInit
>(LHS
);
888 StringInit
*RHSs
= dyn_cast
<StringInit
>(RHS
);
890 return ConcatStringInits(LHSs
, RHSs
);
899 // try to fold eq comparison for 'bit' and 'int', otherwise fallback
900 // to string objects.
902 dyn_cast_or_null
<IntInit
>(LHS
->convertInitializerTo(IntRecTy::get()));
904 dyn_cast_or_null
<IntInit
>(RHS
->convertInitializerTo(IntRecTy::get()));
908 switch (getOpcode()) {
909 case EQ
: Result
= L
->getValue() == R
->getValue(); break;
910 case NE
: Result
= L
->getValue() != R
->getValue(); break;
911 case LE
: Result
= L
->getValue() <= R
->getValue(); break;
912 case LT
: Result
= L
->getValue() < R
->getValue(); break;
913 case GE
: Result
= L
->getValue() >= R
->getValue(); break;
914 case GT
: Result
= L
->getValue() > R
->getValue(); break;
915 default: llvm_unreachable("unhandled comparison");
917 return BitInit::get(Result
);
920 if (getOpcode() == EQ
|| getOpcode() == NE
) {
921 StringInit
*LHSs
= dyn_cast
<StringInit
>(LHS
);
922 StringInit
*RHSs
= dyn_cast
<StringInit
>(RHS
);
924 // Make sure we've resolved
926 bool Equal
= LHSs
->getValue() == RHSs
->getValue();
927 return BitInit::get(getOpcode() == EQ
? Equal
: !Equal
);
940 dyn_cast_or_null
<IntInit
>(LHS
->convertInitializerTo(IntRecTy::get()));
942 dyn_cast_or_null
<IntInit
>(RHS
->convertInitializerTo(IntRecTy::get()));
944 int64_t LHSv
= LHSi
->getValue(), RHSv
= RHSi
->getValue();
946 switch (getOpcode()) {
947 default: llvm_unreachable("Bad opcode!");
948 case ADD
: Result
= LHSv
+ RHSv
; break;
949 case AND
: Result
= LHSv
& RHSv
; break;
950 case OR
: Result
= LHSv
| RHSv
; break;
951 case SHL
: Result
= LHSv
<< RHSv
; break;
952 case SRA
: Result
= LHSv
>> RHSv
; break;
953 case SRL
: Result
= (uint64_t)LHSv
>> (uint64_t)RHSv
; break;
955 return IntInit::get(Result
);
960 return const_cast<BinOpInit
*>(this);
963 Init
*BinOpInit::resolveReferences(Resolver
&R
) const {
964 Init
*lhs
= LHS
->resolveReferences(R
);
965 Init
*rhs
= RHS
->resolveReferences(R
);
967 if (LHS
!= lhs
|| RHS
!= rhs
)
968 return (BinOpInit::get(getOpcode(), lhs
, rhs
, getType()))
969 ->Fold(R
.getCurrentRecord());
970 return const_cast<BinOpInit
*>(this);
973 std::string
BinOpInit::getAsString() const {
975 switch (getOpcode()) {
976 case CONCAT
: Result
= "!con"; break;
977 case ADD
: Result
= "!add"; break;
978 case AND
: Result
= "!and"; break;
979 case OR
: Result
= "!or"; break;
980 case SHL
: Result
= "!shl"; break;
981 case SRA
: Result
= "!sra"; break;
982 case SRL
: Result
= "!srl"; break;
983 case EQ
: Result
= "!eq"; break;
984 case NE
: Result
= "!ne"; break;
985 case LE
: Result
= "!le"; break;
986 case LT
: Result
= "!lt"; break;
987 case GE
: Result
= "!ge"; break;
988 case GT
: Result
= "!gt"; break;
989 case LISTCONCAT
: Result
= "!listconcat"; break;
990 case STRCONCAT
: Result
= "!strconcat"; break;
992 return Result
+ "(" + LHS
->getAsString() + ", " + RHS
->getAsString() + ")";
996 ProfileTernOpInit(FoldingSetNodeID
&ID
, unsigned Opcode
, Init
*LHS
, Init
*MHS
,
997 Init
*RHS
, RecTy
*Type
) {
998 ID
.AddInteger(Opcode
);
1002 ID
.AddPointer(Type
);
1005 TernOpInit
*TernOpInit::get(TernaryOp Opc
, Init
*LHS
, Init
*MHS
, Init
*RHS
,
1007 static FoldingSet
<TernOpInit
> ThePool
;
1009 FoldingSetNodeID ID
;
1010 ProfileTernOpInit(ID
, Opc
, LHS
, MHS
, RHS
, Type
);
1013 if (TernOpInit
*I
= ThePool
.FindNodeOrInsertPos(ID
, IP
))
1016 TernOpInit
*I
= new(Allocator
) TernOpInit(Opc
, LHS
, MHS
, RHS
, Type
);
1017 ThePool
.InsertNode(I
, IP
);
1021 void TernOpInit::Profile(FoldingSetNodeID
&ID
) const {
1022 ProfileTernOpInit(ID
, getOpcode(), getLHS(), getMHS(), getRHS(), getType());
1025 static Init
*ForeachApply(Init
*LHS
, Init
*MHSe
, Init
*RHS
, Record
*CurRec
) {
1026 MapResolver
R(CurRec
);
1028 return RHS
->resolveReferences(R
);
1031 static Init
*ForeachDagApply(Init
*LHS
, DagInit
*MHSd
, Init
*RHS
,
1033 bool Change
= false;
1034 Init
*Val
= ForeachApply(LHS
, MHSd
->getOperator(), RHS
, CurRec
);
1035 if (Val
!= MHSd
->getOperator())
1038 SmallVector
<std::pair
<Init
*, StringInit
*>, 8> NewArgs
;
1039 for (unsigned int i
= 0; i
< MHSd
->getNumArgs(); ++i
) {
1040 Init
*Arg
= MHSd
->getArg(i
);
1042 StringInit
*ArgName
= MHSd
->getArgName(i
);
1044 if (DagInit
*Argd
= dyn_cast
<DagInit
>(Arg
))
1045 NewArg
= ForeachDagApply(LHS
, Argd
, RHS
, CurRec
);
1047 NewArg
= ForeachApply(LHS
, Arg
, RHS
, CurRec
);
1049 NewArgs
.push_back(std::make_pair(NewArg
, ArgName
));
1055 return DagInit::get(Val
, nullptr, NewArgs
);
1059 // Applies RHS to all elements of MHS, using LHS as a temp variable.
1060 static Init
*ForeachHelper(Init
*LHS
, Init
*MHS
, Init
*RHS
, RecTy
*Type
,
1062 if (DagInit
*MHSd
= dyn_cast
<DagInit
>(MHS
))
1063 return ForeachDagApply(LHS
, MHSd
, RHS
, CurRec
);
1065 if (ListInit
*MHSl
= dyn_cast
<ListInit
>(MHS
)) {
1066 SmallVector
<Init
*, 8> NewList(MHSl
->begin(), MHSl
->end());
1068 for (Init
*&Item
: NewList
) {
1069 Init
*NewItem
= ForeachApply(LHS
, Item
, RHS
, CurRec
);
1070 if (NewItem
!= Item
)
1073 return ListInit::get(NewList
, cast
<ListRecTy
>(Type
)->getElementType());
1079 Init
*TernOpInit::Fold(Record
*CurRec
) const {
1080 switch (getOpcode()) {
1082 DefInit
*LHSd
= dyn_cast
<DefInit
>(LHS
);
1083 VarInit
*LHSv
= dyn_cast
<VarInit
>(LHS
);
1084 StringInit
*LHSs
= dyn_cast
<StringInit
>(LHS
);
1086 DefInit
*MHSd
= dyn_cast
<DefInit
>(MHS
);
1087 VarInit
*MHSv
= dyn_cast
<VarInit
>(MHS
);
1088 StringInit
*MHSs
= dyn_cast
<StringInit
>(MHS
);
1090 DefInit
*RHSd
= dyn_cast
<DefInit
>(RHS
);
1091 VarInit
*RHSv
= dyn_cast
<VarInit
>(RHS
);
1092 StringInit
*RHSs
= dyn_cast
<StringInit
>(RHS
);
1094 if (LHSd
&& MHSd
&& RHSd
) {
1095 Record
*Val
= RHSd
->getDef();
1096 if (LHSd
->getAsString() == RHSd
->getAsString())
1097 Val
= MHSd
->getDef();
1098 return DefInit::get(Val
);
1100 if (LHSv
&& MHSv
&& RHSv
) {
1101 std::string Val
= RHSv
->getName();
1102 if (LHSv
->getAsString() == RHSv
->getAsString())
1103 Val
= MHSv
->getName();
1104 return VarInit::get(Val
, getType());
1106 if (LHSs
&& MHSs
&& RHSs
) {
1107 std::string Val
= RHSs
->getValue();
1109 std::string::size_type found
;
1110 std::string::size_type idx
= 0;
1112 found
= Val
.find(LHSs
->getValue(), idx
);
1113 if (found
== std::string::npos
)
1115 Val
.replace(found
, LHSs
->getValue().size(), MHSs
->getValue());
1116 idx
= found
+ MHSs
->getValue().size();
1119 return StringInit::get(Val
);
1125 if (Init
*Result
= ForeachHelper(LHS
, MHS
, RHS
, getType(), CurRec
))
1131 if (IntInit
*LHSi
= dyn_cast_or_null
<IntInit
>(
1132 LHS
->convertInitializerTo(IntRecTy::get()))) {
1133 if (LHSi
->getValue())
1141 ListInit
*MHSl
= dyn_cast
<ListInit
>(MHS
);
1142 ListInit
*RHSl
= dyn_cast
<ListInit
>(RHS
);
1143 bool MHSok
= MHSl
|| isa
<UnsetInit
>(MHS
);
1144 bool RHSok
= RHSl
|| isa
<UnsetInit
>(RHS
);
1146 if (isa
<UnsetInit
>(MHS
) && isa
<UnsetInit
>(RHS
))
1147 break; // Typically prevented by the parser, but might happen with template args
1149 if (MHSok
&& RHSok
&& (!MHSl
|| !RHSl
|| MHSl
->size() == RHSl
->size())) {
1150 SmallVector
<std::pair
<Init
*, StringInit
*>, 8> Children
;
1151 unsigned Size
= MHSl
? MHSl
->size() : RHSl
->size();
1152 for (unsigned i
= 0; i
!= Size
; ++i
) {
1153 Init
*Node
= MHSl
? MHSl
->getElement(i
) : UnsetInit::get();
1154 Init
*Name
= RHSl
? RHSl
->getElement(i
) : UnsetInit::get();
1155 if (!isa
<StringInit
>(Name
) && !isa
<UnsetInit
>(Name
))
1156 return const_cast<TernOpInit
*>(this);
1157 Children
.emplace_back(Node
, dyn_cast
<StringInit
>(Name
));
1159 return DagInit::get(LHS
, nullptr, Children
);
1165 return const_cast<TernOpInit
*>(this);
1168 Init
*TernOpInit::resolveReferences(Resolver
&R
) const {
1169 Init
*lhs
= LHS
->resolveReferences(R
);
1171 if (getOpcode() == IF
&& lhs
!= LHS
) {
1172 if (IntInit
*Value
= dyn_cast_or_null
<IntInit
>(
1173 lhs
->convertInitializerTo(IntRecTy::get()))) {
1175 if (Value
->getValue())
1176 return MHS
->resolveReferences(R
);
1177 return RHS
->resolveReferences(R
);
1181 Init
*mhs
= MHS
->resolveReferences(R
);
1184 if (getOpcode() == FOREACH
) {
1185 ShadowResolver
SR(R
);
1187 rhs
= RHS
->resolveReferences(SR
);
1189 rhs
= RHS
->resolveReferences(R
);
1192 if (LHS
!= lhs
|| MHS
!= mhs
|| RHS
!= rhs
)
1193 return (TernOpInit::get(getOpcode(), lhs
, mhs
, rhs
, getType()))
1194 ->Fold(R
.getCurrentRecord());
1195 return const_cast<TernOpInit
*>(this);
1198 std::string
TernOpInit::getAsString() const {
1200 bool UnquotedLHS
= false;
1201 switch (getOpcode()) {
1202 case SUBST
: Result
= "!subst"; break;
1203 case FOREACH
: Result
= "!foreach"; UnquotedLHS
= true; break;
1204 case IF
: Result
= "!if"; break;
1205 case DAG
: Result
= "!dag"; break;
1207 return (Result
+ "(" +
1208 (UnquotedLHS
? LHS
->getAsUnquotedString() : LHS
->getAsString()) +
1209 ", " + MHS
->getAsString() + ", " + RHS
->getAsString() + ")");
1212 static void ProfileFoldOpInit(FoldingSetNodeID
&ID
, Init
*A
, Init
*B
,
1213 Init
*Start
, Init
*List
, Init
*Expr
,
1215 ID
.AddPointer(Start
);
1216 ID
.AddPointer(List
);
1219 ID
.AddPointer(Expr
);
1220 ID
.AddPointer(Type
);
1223 FoldOpInit
*FoldOpInit::get(Init
*Start
, Init
*List
, Init
*A
, Init
*B
,
1224 Init
*Expr
, RecTy
*Type
) {
1225 static FoldingSet
<FoldOpInit
> ThePool
;
1227 FoldingSetNodeID ID
;
1228 ProfileFoldOpInit(ID
, Start
, List
, A
, B
, Expr
, Type
);
1231 if (FoldOpInit
*I
= ThePool
.FindNodeOrInsertPos(ID
, IP
))
1234 FoldOpInit
*I
= new (Allocator
) FoldOpInit(Start
, List
, A
, B
, Expr
, Type
);
1235 ThePool
.InsertNode(I
, IP
);
1239 void FoldOpInit::Profile(FoldingSetNodeID
&ID
) const {
1240 ProfileFoldOpInit(ID
, Start
, List
, A
, B
, Expr
, getType());
1243 Init
*FoldOpInit::Fold(Record
*CurRec
) const {
1244 if (ListInit
*LI
= dyn_cast
<ListInit
>(List
)) {
1245 Init
*Accum
= Start
;
1246 for (Init
*Elt
: *LI
) {
1247 MapResolver
R(CurRec
);
1250 Accum
= Expr
->resolveReferences(R
);
1254 return const_cast<FoldOpInit
*>(this);
1257 Init
*FoldOpInit::resolveReferences(Resolver
&R
) const {
1258 Init
*NewStart
= Start
->resolveReferences(R
);
1259 Init
*NewList
= List
->resolveReferences(R
);
1260 ShadowResolver
SR(R
);
1263 Init
*NewExpr
= Expr
->resolveReferences(SR
);
1265 if (Start
== NewStart
&& List
== NewList
&& Expr
== NewExpr
)
1266 return const_cast<FoldOpInit
*>(this);
1268 return get(NewStart
, NewList
, A
, B
, NewExpr
, getType())
1269 ->Fold(R
.getCurrentRecord());
1272 Init
*FoldOpInit::getBit(unsigned Bit
) const {
1273 return VarBitInit::get(const_cast<FoldOpInit
*>(this), Bit
);
1276 std::string
FoldOpInit::getAsString() const {
1277 return (Twine("!foldl(") + Start
->getAsString() + ", " + List
->getAsString() +
1278 ", " + A
->getAsUnquotedString() + ", " + B
->getAsUnquotedString() +
1279 ", " + Expr
->getAsString() + ")")
1283 static void ProfileIsAOpInit(FoldingSetNodeID
&ID
, RecTy
*CheckType
,
1285 ID
.AddPointer(CheckType
);
1286 ID
.AddPointer(Expr
);
1289 IsAOpInit
*IsAOpInit::get(RecTy
*CheckType
, Init
*Expr
) {
1290 static FoldingSet
<IsAOpInit
> ThePool
;
1292 FoldingSetNodeID ID
;
1293 ProfileIsAOpInit(ID
, CheckType
, Expr
);
1296 if (IsAOpInit
*I
= ThePool
.FindNodeOrInsertPos(ID
, IP
))
1299 IsAOpInit
*I
= new (Allocator
) IsAOpInit(CheckType
, Expr
);
1300 ThePool
.InsertNode(I
, IP
);
1304 void IsAOpInit::Profile(FoldingSetNodeID
&ID
) const {
1305 ProfileIsAOpInit(ID
, CheckType
, Expr
);
1308 Init
*IsAOpInit::Fold() const {
1309 if (TypedInit
*TI
= dyn_cast
<TypedInit
>(Expr
)) {
1310 // Is the expression type known to be (a subclass of) the desired type?
1311 if (TI
->getType()->typeIsConvertibleTo(CheckType
))
1312 return IntInit::get(1);
1314 if (isa
<RecordRecTy
>(CheckType
)) {
1315 // If the target type is not a subclass of the expression type, or if
1316 // the expression has fully resolved to a record, we know that it can't
1317 // be of the required type.
1318 if (!CheckType
->typeIsConvertibleTo(TI
->getType()) || isa
<DefInit
>(Expr
))
1319 return IntInit::get(0);
1321 // We treat non-record types as not castable.
1322 return IntInit::get(0);
1325 return const_cast<IsAOpInit
*>(this);
1328 Init
*IsAOpInit::resolveReferences(Resolver
&R
) const {
1329 Init
*NewExpr
= Expr
->resolveReferences(R
);
1330 if (Expr
!= NewExpr
)
1331 return get(CheckType
, NewExpr
)->Fold();
1332 return const_cast<IsAOpInit
*>(this);
1335 Init
*IsAOpInit::getBit(unsigned Bit
) const {
1336 return VarBitInit::get(const_cast<IsAOpInit
*>(this), Bit
);
1339 std::string
IsAOpInit::getAsString() const {
1340 return (Twine("!isa<") + CheckType
->getAsString() + ">(" +
1341 Expr
->getAsString() + ")")
1345 RecTy
*TypedInit::getFieldType(StringInit
*FieldName
) const {
1346 if (RecordRecTy
*RecordType
= dyn_cast
<RecordRecTy
>(getType())) {
1347 for (Record
*Rec
: RecordType
->getClasses()) {
1348 if (RecordVal
*Field
= Rec
->getValue(FieldName
))
1349 return Field
->getType();
1356 TypedInit::convertInitializerTo(RecTy
*Ty
) const {
1357 if (getType() == Ty
|| getType()->typeIsA(Ty
))
1358 return const_cast<TypedInit
*>(this);
1360 if (isa
<BitRecTy
>(getType()) && isa
<BitsRecTy
>(Ty
) &&
1361 cast
<BitsRecTy
>(Ty
)->getNumBits() == 1)
1362 return BitsInit::get({const_cast<TypedInit
*>(this)});
1367 Init
*TypedInit::convertInitializerBitRange(ArrayRef
<unsigned> Bits
) const {
1368 BitsRecTy
*T
= dyn_cast
<BitsRecTy
>(getType());
1369 if (!T
) return nullptr; // Cannot subscript a non-bits variable.
1370 unsigned NumBits
= T
->getNumBits();
1372 SmallVector
<Init
*, 16> NewBits
;
1373 NewBits
.reserve(Bits
.size());
1374 for (unsigned Bit
: Bits
) {
1378 NewBits
.push_back(VarBitInit::get(const_cast<TypedInit
*>(this), Bit
));
1380 return BitsInit::get(NewBits
);
1383 Init
*TypedInit::getCastTo(RecTy
*Ty
) const {
1384 // Handle the common case quickly
1385 if (getType() == Ty
|| getType()->typeIsA(Ty
))
1386 return const_cast<TypedInit
*>(this);
1388 if (Init
*Converted
= convertInitializerTo(Ty
)) {
1389 assert(!isa
<TypedInit
>(Converted
) ||
1390 cast
<TypedInit
>(Converted
)->getType()->typeIsA(Ty
));
1394 if (!getType()->typeIsConvertibleTo(Ty
))
1397 return UnOpInit::get(UnOpInit::CAST
, const_cast<TypedInit
*>(this), Ty
)
1401 Init
*TypedInit::convertInitListSlice(ArrayRef
<unsigned> Elements
) const {
1402 ListRecTy
*T
= dyn_cast
<ListRecTy
>(getType());
1403 if (!T
) return nullptr; // Cannot subscript a non-list variable.
1405 if (Elements
.size() == 1)
1406 return VarListElementInit::get(const_cast<TypedInit
*>(this), Elements
[0]);
1408 SmallVector
<Init
*, 8> ListInits
;
1409 ListInits
.reserve(Elements
.size());
1410 for (unsigned Element
: Elements
)
1411 ListInits
.push_back(VarListElementInit::get(const_cast<TypedInit
*>(this),
1413 return ListInit::get(ListInits
, T
->getElementType());
1417 VarInit
*VarInit::get(StringRef VN
, RecTy
*T
) {
1418 Init
*Value
= StringInit::get(VN
);
1419 return VarInit::get(Value
, T
);
1422 VarInit
*VarInit::get(Init
*VN
, RecTy
*T
) {
1423 using Key
= std::pair
<RecTy
*, Init
*>;
1424 static DenseMap
<Key
, VarInit
*> ThePool
;
1426 Key
TheKey(std::make_pair(T
, VN
));
1428 VarInit
*&I
= ThePool
[TheKey
];
1430 I
= new(Allocator
) VarInit(VN
, T
);
1434 StringRef
VarInit::getName() const {
1435 StringInit
*NameString
= cast
<StringInit
>(getNameInit());
1436 return NameString
->getValue();
1439 Init
*VarInit::getBit(unsigned Bit
) const {
1440 if (getType() == BitRecTy::get())
1441 return const_cast<VarInit
*>(this);
1442 return VarBitInit::get(const_cast<VarInit
*>(this), Bit
);
1445 Init
*VarInit::resolveReferences(Resolver
&R
) const {
1446 if (Init
*Val
= R
.resolve(VarName
))
1448 return const_cast<VarInit
*>(this);
1451 VarBitInit
*VarBitInit::get(TypedInit
*T
, unsigned B
) {
1452 using Key
= std::pair
<TypedInit
*, unsigned>;
1453 static DenseMap
<Key
, VarBitInit
*> ThePool
;
1455 Key
TheKey(std::make_pair(T
, B
));
1457 VarBitInit
*&I
= ThePool
[TheKey
];
1459 I
= new(Allocator
) VarBitInit(T
, B
);
1463 std::string
VarBitInit::getAsString() const {
1464 return TI
->getAsString() + "{" + utostr(Bit
) + "}";
1467 Init
*VarBitInit::resolveReferences(Resolver
&R
) const {
1468 Init
*I
= TI
->resolveReferences(R
);
1470 return I
->getBit(getBitNum());
1472 return const_cast<VarBitInit
*>(this);
1475 VarListElementInit
*VarListElementInit::get(TypedInit
*T
,
1477 using Key
= std::pair
<TypedInit
*, unsigned>;
1478 static DenseMap
<Key
, VarListElementInit
*> ThePool
;
1480 Key
TheKey(std::make_pair(T
, E
));
1482 VarListElementInit
*&I
= ThePool
[TheKey
];
1483 if (!I
) I
= new(Allocator
) VarListElementInit(T
, E
);
1487 std::string
VarListElementInit::getAsString() const {
1488 return TI
->getAsString() + "[" + utostr(Element
) + "]";
1491 Init
*VarListElementInit::resolveReferences(Resolver
&R
) const {
1492 Init
*NewTI
= TI
->resolveReferences(R
);
1493 if (ListInit
*List
= dyn_cast
<ListInit
>(NewTI
)) {
1494 // Leave out-of-bounds array references as-is. This can happen without
1495 // being an error, e.g. in the untaken "branch" of an !if expression.
1496 if (getElementNum() < List
->size())
1497 return List
->getElement(getElementNum());
1499 if (NewTI
!= TI
&& isa
<TypedInit
>(NewTI
))
1500 return VarListElementInit::get(cast
<TypedInit
>(NewTI
), getElementNum());
1501 return const_cast<VarListElementInit
*>(this);
1504 Init
*VarListElementInit::getBit(unsigned Bit
) const {
1505 if (getType() == BitRecTy::get())
1506 return const_cast<VarListElementInit
*>(this);
1507 return VarBitInit::get(const_cast<VarListElementInit
*>(this), Bit
);
1510 DefInit::DefInit(Record
*D
)
1511 : TypedInit(IK_DefInit
, D
->getType()), Def(D
) {}
1513 DefInit
*DefInit::get(Record
*R
) {
1514 return R
->getDefInit();
1517 Init
*DefInit::convertInitializerTo(RecTy
*Ty
) const {
1518 if (auto *RRT
= dyn_cast
<RecordRecTy
>(Ty
))
1519 if (getType()->typeIsConvertibleTo(RRT
))
1520 return const_cast<DefInit
*>(this);
1524 RecTy
*DefInit::getFieldType(StringInit
*FieldName
) const {
1525 if (const RecordVal
*RV
= Def
->getValue(FieldName
))
1526 return RV
->getType();
1530 std::string
DefInit::getAsString() const {
1531 return Def
->getName();
1534 static void ProfileVarDefInit(FoldingSetNodeID
&ID
,
1536 ArrayRef
<Init
*> Args
) {
1537 ID
.AddInteger(Args
.size());
1538 ID
.AddPointer(Class
);
1540 for (Init
*I
: Args
)
1544 VarDefInit
*VarDefInit::get(Record
*Class
, ArrayRef
<Init
*> Args
) {
1545 static FoldingSet
<VarDefInit
> ThePool
;
1547 FoldingSetNodeID ID
;
1548 ProfileVarDefInit(ID
, Class
, Args
);
1551 if (VarDefInit
*I
= ThePool
.FindNodeOrInsertPos(ID
, IP
))
1554 void *Mem
= Allocator
.Allocate(totalSizeToAlloc
<Init
*>(Args
.size()),
1555 alignof(VarDefInit
));
1556 VarDefInit
*I
= new(Mem
) VarDefInit(Class
, Args
.size());
1557 std::uninitialized_copy(Args
.begin(), Args
.end(),
1558 I
->getTrailingObjects
<Init
*>());
1559 ThePool
.InsertNode(I
, IP
);
1563 void VarDefInit::Profile(FoldingSetNodeID
&ID
) const {
1564 ProfileVarDefInit(ID
, Class
, args());
1567 DefInit
*VarDefInit::instantiate() {
1569 RecordKeeper
&Records
= Class
->getRecords();
1570 auto NewRecOwner
= make_unique
<Record
>(Records
.getNewAnonymousName(),
1571 Class
->getLoc(), Records
,
1572 /*IsAnonymous=*/true);
1573 Record
*NewRec
= NewRecOwner
.get();
1575 // Copy values from class to instance
1576 for (const RecordVal
&Val
: Class
->getValues())
1577 NewRec
->addValue(Val
);
1579 // Substitute and resolve template arguments
1580 ArrayRef
<Init
*> TArgs
= Class
->getTemplateArgs();
1581 MapResolver
R(NewRec
);
1583 for (unsigned i
= 0, e
= TArgs
.size(); i
!= e
; ++i
) {
1584 if (i
< args_size())
1585 R
.set(TArgs
[i
], getArg(i
));
1587 R
.set(TArgs
[i
], NewRec
->getValue(TArgs
[i
])->getValue());
1589 NewRec
->removeValue(TArgs
[i
]);
1592 NewRec
->resolveReferences(R
);
1594 // Add superclasses.
1595 ArrayRef
<std::pair
<Record
*, SMRange
>> SCs
= Class
->getSuperClasses();
1596 for (const auto &SCPair
: SCs
)
1597 NewRec
->addSuperClass(SCPair
.first
, SCPair
.second
);
1599 NewRec
->addSuperClass(Class
,
1600 SMRange(Class
->getLoc().back(),
1601 Class
->getLoc().back()));
1603 // Resolve internal references and store in record keeper
1604 NewRec
->resolveReferences();
1605 Records
.addDef(std::move(NewRecOwner
));
1607 Def
= DefInit::get(NewRec
);
1613 Init
*VarDefInit::resolveReferences(Resolver
&R
) const {
1614 TrackUnresolvedResolver
UR(&R
);
1615 bool Changed
= false;
1616 SmallVector
<Init
*, 8> NewArgs
;
1617 NewArgs
.reserve(args_size());
1619 for (Init
*Arg
: args()) {
1620 Init
*NewArg
= Arg
->resolveReferences(UR
);
1621 NewArgs
.push_back(NewArg
);
1622 Changed
|= NewArg
!= Arg
;
1626 auto New
= VarDefInit::get(Class
, NewArgs
);
1627 if (!UR
.foundUnresolved())
1628 return New
->instantiate();
1631 return const_cast<VarDefInit
*>(this);
1634 Init
*VarDefInit::Fold() const {
1638 TrackUnresolvedResolver R
;
1639 for (Init
*Arg
: args())
1640 Arg
->resolveReferences(R
);
1642 if (!R
.foundUnresolved())
1643 return const_cast<VarDefInit
*>(this)->instantiate();
1644 return const_cast<VarDefInit
*>(this);
1647 std::string
VarDefInit::getAsString() const {
1648 std::string Result
= Class
->getNameInitAsString() + "<";
1649 const char *sep
= "";
1650 for (Init
*Arg
: args()) {
1653 Result
+= Arg
->getAsString();
1655 return Result
+ ">";
1658 FieldInit
*FieldInit::get(Init
*R
, StringInit
*FN
) {
1659 using Key
= std::pair
<Init
*, StringInit
*>;
1660 static DenseMap
<Key
, FieldInit
*> ThePool
;
1662 Key
TheKey(std::make_pair(R
, FN
));
1664 FieldInit
*&I
= ThePool
[TheKey
];
1665 if (!I
) I
= new(Allocator
) FieldInit(R
, FN
);
1669 Init
*FieldInit::getBit(unsigned Bit
) const {
1670 if (getType() == BitRecTy::get())
1671 return const_cast<FieldInit
*>(this);
1672 return VarBitInit::get(const_cast<FieldInit
*>(this), Bit
);
1675 Init
*FieldInit::resolveReferences(Resolver
&R
) const {
1676 Init
*NewRec
= Rec
->resolveReferences(R
);
1678 return FieldInit::get(NewRec
, FieldName
)->Fold(R
.getCurrentRecord());
1679 return const_cast<FieldInit
*>(this);
1682 Init
*FieldInit::Fold(Record
*CurRec
) const {
1683 if (DefInit
*DI
= dyn_cast
<DefInit
>(Rec
)) {
1684 Record
*Def
= DI
->getDef();
1686 PrintFatalError(CurRec
->getLoc(),
1687 Twine("Attempting to access field '") +
1688 FieldName
->getAsUnquotedString() + "' of '" +
1689 Rec
->getAsString() + "' is a forbidden self-reference");
1690 Init
*FieldVal
= Def
->getValue(FieldName
)->getValue();
1691 if (FieldVal
->isComplete())
1694 return const_cast<FieldInit
*>(this);
1697 static void ProfileDagInit(FoldingSetNodeID
&ID
, Init
*V
, StringInit
*VN
,
1698 ArrayRef
<Init
*> ArgRange
,
1699 ArrayRef
<StringInit
*> NameRange
) {
1703 ArrayRef
<Init
*>::iterator Arg
= ArgRange
.begin();
1704 ArrayRef
<StringInit
*>::iterator Name
= NameRange
.begin();
1705 while (Arg
!= ArgRange
.end()) {
1706 assert(Name
!= NameRange
.end() && "Arg name underflow!");
1707 ID
.AddPointer(*Arg
++);
1708 ID
.AddPointer(*Name
++);
1710 assert(Name
== NameRange
.end() && "Arg name overflow!");
1714 DagInit::get(Init
*V
, StringInit
*VN
, ArrayRef
<Init
*> ArgRange
,
1715 ArrayRef
<StringInit
*> NameRange
) {
1716 static FoldingSet
<DagInit
> ThePool
;
1718 FoldingSetNodeID ID
;
1719 ProfileDagInit(ID
, V
, VN
, ArgRange
, NameRange
);
1722 if (DagInit
*I
= ThePool
.FindNodeOrInsertPos(ID
, IP
))
1725 void *Mem
= Allocator
.Allocate(totalSizeToAlloc
<Init
*, StringInit
*>(ArgRange
.size(), NameRange
.size()), alignof(BitsInit
));
1726 DagInit
*I
= new(Mem
) DagInit(V
, VN
, ArgRange
.size(), NameRange
.size());
1727 std::uninitialized_copy(ArgRange
.begin(), ArgRange
.end(),
1728 I
->getTrailingObjects
<Init
*>());
1729 std::uninitialized_copy(NameRange
.begin(), NameRange
.end(),
1730 I
->getTrailingObjects
<StringInit
*>());
1731 ThePool
.InsertNode(I
, IP
);
1736 DagInit::get(Init
*V
, StringInit
*VN
,
1737 ArrayRef
<std::pair
<Init
*, StringInit
*>> args
) {
1738 SmallVector
<Init
*, 8> Args
;
1739 SmallVector
<StringInit
*, 8> Names
;
1741 for (const auto &Arg
: args
) {
1742 Args
.push_back(Arg
.first
);
1743 Names
.push_back(Arg
.second
);
1746 return DagInit::get(V
, VN
, Args
, Names
);
1749 void DagInit::Profile(FoldingSetNodeID
&ID
) const {
1750 ProfileDagInit(ID
, Val
, ValName
, makeArrayRef(getTrailingObjects
<Init
*>(), NumArgs
), makeArrayRef(getTrailingObjects
<StringInit
*>(), NumArgNames
));
1753 Init
*DagInit::resolveReferences(Resolver
&R
) const {
1754 SmallVector
<Init
*, 8> NewArgs
;
1755 NewArgs
.reserve(arg_size());
1756 bool ArgsChanged
= false;
1757 for (const Init
*Arg
: getArgs()) {
1758 Init
*NewArg
= Arg
->resolveReferences(R
);
1759 NewArgs
.push_back(NewArg
);
1760 ArgsChanged
|= NewArg
!= Arg
;
1763 Init
*Op
= Val
->resolveReferences(R
);
1764 if (Op
!= Val
|| ArgsChanged
)
1765 return DagInit::get(Op
, ValName
, NewArgs
, getArgNames());
1767 return const_cast<DagInit
*>(this);
1770 bool DagInit::isConcrete() const {
1771 if (!Val
->isConcrete())
1773 for (const Init
*Elt
: getArgs()) {
1774 if (!Elt
->isConcrete())
1780 std::string
DagInit::getAsString() const {
1781 std::string Result
= "(" + Val
->getAsString();
1783 Result
+= ":" + ValName
->getAsUnquotedString();
1785 Result
+= " " + getArg(0)->getAsString();
1786 if (getArgName(0)) Result
+= ":$" + getArgName(0)->getAsUnquotedString();
1787 for (unsigned i
= 1, e
= getNumArgs(); i
!= e
; ++i
) {
1788 Result
+= ", " + getArg(i
)->getAsString();
1789 if (getArgName(i
)) Result
+= ":$" + getArgName(i
)->getAsUnquotedString();
1792 return Result
+ ")";
1795 //===----------------------------------------------------------------------===//
1796 // Other implementations
1797 //===----------------------------------------------------------------------===//
1799 RecordVal::RecordVal(Init
*N
, RecTy
*T
, bool P
)
1800 : Name(N
), TyAndPrefix(T
, P
) {
1801 setValue(UnsetInit::get());
1802 assert(Value
&& "Cannot create unset value for current type!");
1805 StringRef
RecordVal::getName() const {
1806 return cast
<StringInit
>(getNameInit())->getValue();
1809 bool RecordVal::setValue(Init
*V
) {
1811 Value
= V
->getCastTo(getType());
1813 assert(!isa
<TypedInit
>(Value
) ||
1814 cast
<TypedInit
>(Value
)->getType()->typeIsA(getType()));
1815 if (BitsRecTy
*BTy
= dyn_cast
<BitsRecTy
>(getType())) {
1816 if (!isa
<BitsInit
>(Value
)) {
1817 SmallVector
<Init
*, 64> Bits
;
1818 Bits
.reserve(BTy
->getNumBits());
1819 for (unsigned i
= 0, e
= BTy
->getNumBits(); i
< e
; ++i
)
1820 Bits
.push_back(Value
->getBit(i
));
1821 Value
= BitsInit::get(Bits
);
1825 return Value
== nullptr;
1831 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1832 LLVM_DUMP_METHOD
void RecordVal::dump() const { errs() << *this; }
1835 void RecordVal::print(raw_ostream
&OS
, bool PrintSem
) const {
1836 if (getPrefix()) OS
<< "field ";
1837 OS
<< *getType() << " " << getNameInitAsString();
1840 OS
<< " = " << *getValue();
1842 if (PrintSem
) OS
<< ";\n";
1845 unsigned Record::LastID
= 0;
1847 void Record::checkName() {
1848 // Ensure the record name has string type.
1849 const TypedInit
*TypedName
= cast
<const TypedInit
>(Name
);
1850 if (!isa
<StringRecTy
>(TypedName
->getType()))
1851 PrintFatalError(getLoc(), Twine("Record name '") + Name
->getAsString() +
1852 "' is not a string!");
1855 RecordRecTy
*Record::getType() {
1856 SmallVector
<Record
*, 4> DirectSCs
;
1857 getDirectSuperClasses(DirectSCs
);
1858 return RecordRecTy::get(DirectSCs
);
1861 DefInit
*Record::getDefInit() {
1863 TheInit
= new(Allocator
) DefInit(this);
1867 void Record::setName(Init
*NewName
) {
1870 // DO NOT resolve record values to the name at this point because
1871 // there might be default values for arguments of this def. Those
1872 // arguments might not have been resolved yet so we don't want to
1873 // prematurely assume values for those arguments were not passed to
1876 // Nonetheless, it may be that some of this Record's values
1877 // reference the record name. Indeed, the reason for having the
1878 // record name be an Init is to provide this flexibility. The extra
1879 // resolve steps after completely instantiating defs takes care of
1880 // this. See TGParser::ParseDef and TGParser::ParseDefm.
1883 void Record::getDirectSuperClasses(SmallVectorImpl
<Record
*> &Classes
) const {
1884 ArrayRef
<std::pair
<Record
*, SMRange
>> SCs
= getSuperClasses();
1885 while (!SCs
.empty()) {
1886 // Superclasses are in reverse preorder, so 'back' is a direct superclass,
1887 // and its transitive superclasses are directly preceding it.
1888 Record
*SC
= SCs
.back().first
;
1889 SCs
= SCs
.drop_back(1 + SC
->getSuperClasses().size());
1890 Classes
.push_back(SC
);
1894 void Record::resolveReferences(Resolver
&R
, const RecordVal
*SkipVal
) {
1895 for (RecordVal
&Value
: Values
) {
1896 if (SkipVal
== &Value
) // Skip resolve the same field as the given one
1898 if (Init
*V
= Value
.getValue()) {
1899 Init
*VR
= V
->resolveReferences(R
);
1900 if (Value
.setValue(VR
)) {
1902 if (TypedInit
*VRT
= dyn_cast
<TypedInit
>(VR
))
1904 (Twine("of type '") + VRT
->getType()->getAsString() + "' ").str();
1905 PrintFatalError(getLoc(), Twine("Invalid value ") + Type
+
1906 "is found when setting '" +
1907 Value
.getNameInitAsString() +
1909 Value
.getType()->getAsString() +
1910 "' after resolving references: " +
1911 VR
->getAsUnquotedString() + "\n");
1915 Init
*OldName
= getNameInit();
1916 Init
*NewName
= Name
->resolveReferences(R
);
1917 if (NewName
!= OldName
) {
1918 // Re-register with RecordKeeper.
1923 void Record::resolveReferences() {
1924 RecordResolver
R(*this);
1926 resolveReferences(R
);
1929 void Record::resolveReferencesTo(const RecordVal
*RV
) {
1930 RecordValResolver
R(*this, RV
);
1931 resolveReferences(R
, RV
);
1934 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1935 LLVM_DUMP_METHOD
void Record::dump() const { errs() << *this; }
1938 raw_ostream
&llvm::operator<<(raw_ostream
&OS
, const Record
&R
) {
1939 OS
<< R
.getNameInitAsString();
1941 ArrayRef
<Init
*> TArgs
= R
.getTemplateArgs();
1942 if (!TArgs
.empty()) {
1944 bool NeedComma
= false;
1945 for (const Init
*TA
: TArgs
) {
1946 if (NeedComma
) OS
<< ", ";
1948 const RecordVal
*RV
= R
.getValue(TA
);
1949 assert(RV
&& "Template argument record not found??");
1950 RV
->print(OS
, false);
1956 ArrayRef
<std::pair
<Record
*, SMRange
>> SC
= R
.getSuperClasses();
1959 for (const auto &SuperPair
: SC
)
1960 OS
<< " " << SuperPair
.first
->getNameInitAsString();
1964 for (const RecordVal
&Val
: R
.getValues())
1965 if (Val
.getPrefix() && !R
.isTemplateArg(Val
.getNameInit()))
1967 for (const RecordVal
&Val
: R
.getValues())
1968 if (!Val
.getPrefix() && !R
.isTemplateArg(Val
.getNameInit()))
1974 Init
*Record::getValueInit(StringRef FieldName
) const {
1975 const RecordVal
*R
= getValue(FieldName
);
1976 if (!R
|| !R
->getValue())
1977 PrintFatalError(getLoc(), "Record `" + getName() +
1978 "' does not have a field named `" + FieldName
+ "'!\n");
1979 return R
->getValue();
1982 StringRef
Record::getValueAsString(StringRef FieldName
) const {
1983 const RecordVal
*R
= getValue(FieldName
);
1984 if (!R
|| !R
->getValue())
1985 PrintFatalError(getLoc(), "Record `" + getName() +
1986 "' does not have a field named `" + FieldName
+ "'!\n");
1988 if (StringInit
*SI
= dyn_cast
<StringInit
>(R
->getValue()))
1989 return SI
->getValue();
1990 if (CodeInit
*CI
= dyn_cast
<CodeInit
>(R
->getValue()))
1991 return CI
->getValue();
1993 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1994 FieldName
+ "' does not have a string initializer!");
1997 BitsInit
*Record::getValueAsBitsInit(StringRef FieldName
) const {
1998 const RecordVal
*R
= getValue(FieldName
);
1999 if (!R
|| !R
->getValue())
2000 PrintFatalError(getLoc(), "Record `" + getName() +
2001 "' does not have a field named `" + FieldName
+ "'!\n");
2003 if (BitsInit
*BI
= dyn_cast
<BitsInit
>(R
->getValue()))
2005 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2006 FieldName
+ "' does not have a BitsInit initializer!");
2009 ListInit
*Record::getValueAsListInit(StringRef FieldName
) const {
2010 const RecordVal
*R
= getValue(FieldName
);
2011 if (!R
|| !R
->getValue())
2012 PrintFatalError(getLoc(), "Record `" + getName() +
2013 "' does not have a field named `" + FieldName
+ "'!\n");
2015 if (ListInit
*LI
= dyn_cast
<ListInit
>(R
->getValue()))
2017 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2018 FieldName
+ "' does not have a list initializer!");
2021 std::vector
<Record
*>
2022 Record::getValueAsListOfDefs(StringRef FieldName
) const {
2023 ListInit
*List
= getValueAsListInit(FieldName
);
2024 std::vector
<Record
*> Defs
;
2025 for (Init
*I
: List
->getValues()) {
2026 if (DefInit
*DI
= dyn_cast
<DefInit
>(I
))
2027 Defs
.push_back(DI
->getDef());
2029 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2030 FieldName
+ "' list is not entirely DefInit!");
2035 int64_t Record::getValueAsInt(StringRef FieldName
) const {
2036 const RecordVal
*R
= getValue(FieldName
);
2037 if (!R
|| !R
->getValue())
2038 PrintFatalError(getLoc(), "Record `" + getName() +
2039 "' does not have a field named `" + FieldName
+ "'!\n");
2041 if (IntInit
*II
= dyn_cast
<IntInit
>(R
->getValue()))
2042 return II
->getValue();
2043 PrintFatalError(getLoc(), Twine("Record `") + getName() + "', field `" +
2045 "' does not have an int initializer: " +
2046 R
->getValue()->getAsString());
2049 std::vector
<int64_t>
2050 Record::getValueAsListOfInts(StringRef FieldName
) const {
2051 ListInit
*List
= getValueAsListInit(FieldName
);
2052 std::vector
<int64_t> Ints
;
2053 for (Init
*I
: List
->getValues()) {
2054 if (IntInit
*II
= dyn_cast
<IntInit
>(I
))
2055 Ints
.push_back(II
->getValue());
2057 PrintFatalError(getLoc(),
2058 Twine("Record `") + getName() + "', field `" + FieldName
+
2059 "' does not have a list of ints initializer: " +
2065 std::vector
<StringRef
>
2066 Record::getValueAsListOfStrings(StringRef FieldName
) const {
2067 ListInit
*List
= getValueAsListInit(FieldName
);
2068 std::vector
<StringRef
> Strings
;
2069 for (Init
*I
: List
->getValues()) {
2070 if (StringInit
*SI
= dyn_cast
<StringInit
>(I
))
2071 Strings
.push_back(SI
->getValue());
2073 PrintFatalError(getLoc(),
2074 Twine("Record `") + getName() + "', field `" + FieldName
+
2075 "' does not have a list of strings initializer: " +
2081 Record
*Record::getValueAsDef(StringRef FieldName
) const {
2082 const RecordVal
*R
= getValue(FieldName
);
2083 if (!R
|| !R
->getValue())
2084 PrintFatalError(getLoc(), "Record `" + getName() +
2085 "' does not have a field named `" + FieldName
+ "'!\n");
2087 if (DefInit
*DI
= dyn_cast
<DefInit
>(R
->getValue()))
2088 return DI
->getDef();
2089 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2090 FieldName
+ "' does not have a def initializer!");
2093 bool Record::getValueAsBit(StringRef FieldName
) const {
2094 const RecordVal
*R
= getValue(FieldName
);
2095 if (!R
|| !R
->getValue())
2096 PrintFatalError(getLoc(), "Record `" + getName() +
2097 "' does not have a field named `" + FieldName
+ "'!\n");
2099 if (BitInit
*BI
= dyn_cast
<BitInit
>(R
->getValue()))
2100 return BI
->getValue();
2101 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2102 FieldName
+ "' does not have a bit initializer!");
2105 bool Record::getValueAsBitOrUnset(StringRef FieldName
, bool &Unset
) const {
2106 const RecordVal
*R
= getValue(FieldName
);
2107 if (!R
|| !R
->getValue())
2108 PrintFatalError(getLoc(), "Record `" + getName() +
2109 "' does not have a field named `" + FieldName
.str() + "'!\n");
2111 if (isa
<UnsetInit
>(R
->getValue())) {
2116 if (BitInit
*BI
= dyn_cast
<BitInit
>(R
->getValue()))
2117 return BI
->getValue();
2118 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2119 FieldName
+ "' does not have a bit initializer!");
2122 DagInit
*Record::getValueAsDag(StringRef FieldName
) const {
2123 const RecordVal
*R
= getValue(FieldName
);
2124 if (!R
|| !R
->getValue())
2125 PrintFatalError(getLoc(), "Record `" + getName() +
2126 "' does not have a field named `" + FieldName
+ "'!\n");
2128 if (DagInit
*DI
= dyn_cast
<DagInit
>(R
->getValue()))
2130 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2131 FieldName
+ "' does not have a dag initializer!");
2134 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2135 LLVM_DUMP_METHOD
void RecordKeeper::dump() const { errs() << *this; }
2138 raw_ostream
&llvm::operator<<(raw_ostream
&OS
, const RecordKeeper
&RK
) {
2139 OS
<< "------------- Classes -----------------\n";
2140 for (const auto &C
: RK
.getClasses())
2141 OS
<< "class " << *C
.second
;
2143 OS
<< "------------- Defs -----------------\n";
2144 for (const auto &D
: RK
.getDefs())
2145 OS
<< "def " << *D
.second
;
2149 /// GetNewAnonymousName - Generate a unique anonymous name that can be used as
2151 Init
*RecordKeeper::getNewAnonymousName() {
2152 return StringInit::get("anonymous_" + utostr(AnonCounter
++));
2155 std::vector
<Record
*>
2156 RecordKeeper::getAllDerivedDefinitions(StringRef ClassName
) const {
2157 Record
*Class
= getClass(ClassName
);
2159 PrintFatalError("ERROR: Couldn't find the `" + ClassName
+ "' class!\n");
2161 std::vector
<Record
*> Defs
;
2162 for (const auto &D
: getDefs())
2163 if (D
.second
->isSubClassOf(Class
))
2164 Defs
.push_back(D
.second
.get());
2169 Init
*MapResolver::resolve(Init
*VarName
) {
2170 auto It
= Map
.find(VarName
);
2171 if (It
== Map
.end())
2174 Init
*I
= It
->second
.V
;
2176 if (!It
->second
.Resolved
&& Map
.size() > 1) {
2177 // Resolve mutual references among the mapped variables, but prevent
2178 // infinite recursion.
2180 I
= I
->resolveReferences(*this);
2181 Map
[VarName
] = {I
, true};
2187 Init
*RecordResolver::resolve(Init
*VarName
) {
2188 Init
*Val
= Cache
.lookup(VarName
);
2192 for (Init
*S
: Stack
) {
2194 return nullptr; // prevent infinite recursion
2197 if (RecordVal
*RV
= getCurrentRecord()->getValue(VarName
)) {
2198 if (!isa
<UnsetInit
>(RV
->getValue())) {
2199 Val
= RV
->getValue();
2200 Stack
.push_back(VarName
);
2201 Val
= Val
->resolveReferences(*this);
2206 Cache
[VarName
] = Val
;
2210 Init
*TrackUnresolvedResolver::resolve(Init
*VarName
) {
2214 I
= R
->resolve(VarName
);
2215 if (I
&& !FoundUnresolved
) {
2216 // Do not recurse into the resolved initializer, as that would change
2217 // the behavior of the resolver we're delegating, but do check to see
2218 // if there are unresolved variables remaining.
2219 TrackUnresolvedResolver Sub
;
2220 I
->resolveReferences(Sub
);
2221 FoundUnresolved
|= Sub
.FoundUnresolved
;
2226 FoundUnresolved
= true;
2230 Init
*HasReferenceResolver::resolve(Init
*VarName
)
2232 if (VarName
== VarNameToTrack
)