1 //===- Record.cpp - Record implementation ---------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // Implement the tablegen record classes.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/ADT/DenseMap.h"
15 #include "llvm/ADT/FoldingSet.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/Statistic.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/ADT/StringMap.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/ADT/StringSet.h"
23 #include "llvm/Config/llvm-config.h"
24 #include "llvm/Support/Allocator.h"
25 #include "llvm/Support/Casting.h"
26 #include "llvm/Support/Compiler.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/SMLoc.h"
29 #include "llvm/Support/raw_ostream.h"
30 #include "llvm/TableGen/Error.h"
31 #include "llvm/TableGen/Record.h"
42 #define DEBUG_TYPE "tblgen-records"
44 static BumpPtrAllocator Allocator
;
46 STATISTIC(CodeInitsConstructed
,
47 "The total number of unique CodeInits constructed");
49 //===----------------------------------------------------------------------===//
50 // Type implementations
51 //===----------------------------------------------------------------------===//
53 BitRecTy
BitRecTy::Shared
;
54 CodeRecTy
CodeRecTy::Shared
;
55 IntRecTy
IntRecTy::Shared
;
56 StringRecTy
StringRecTy::Shared
;
57 DagRecTy
DagRecTy::Shared
;
59 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
60 LLVM_DUMP_METHOD
void RecTy::dump() const { print(errs()); }
63 ListRecTy
*RecTy::getListTy() {
65 ListTy
= new(Allocator
) ListRecTy(this);
69 bool RecTy::typeIsConvertibleTo(const RecTy
*RHS
) const {
70 assert(RHS
&& "NULL pointer");
71 return Kind
== RHS
->getRecTyKind();
74 bool RecTy::typeIsA(const RecTy
*RHS
) const { return this == RHS
; }
76 bool BitRecTy::typeIsConvertibleTo(const RecTy
*RHS
) const{
77 if (RecTy::typeIsConvertibleTo(RHS
) || RHS
->getRecTyKind() == IntRecTyKind
)
79 if (const BitsRecTy
*BitsTy
= dyn_cast
<BitsRecTy
>(RHS
))
80 return BitsTy
->getNumBits() == 1;
84 BitsRecTy
*BitsRecTy::get(unsigned Sz
) {
85 static std::vector
<BitsRecTy
*> Shared
;
86 if (Sz
>= Shared
.size())
87 Shared
.resize(Sz
+ 1);
88 BitsRecTy
*&Ty
= Shared
[Sz
];
90 Ty
= new(Allocator
) BitsRecTy(Sz
);
94 std::string
BitsRecTy::getAsString() const {
95 return "bits<" + utostr(Size
) + ">";
98 bool BitsRecTy::typeIsConvertibleTo(const RecTy
*RHS
) const {
99 if (RecTy::typeIsConvertibleTo(RHS
)) //argument and the sender are same type
100 return cast
<BitsRecTy
>(RHS
)->Size
== Size
;
101 RecTyKind kind
= RHS
->getRecTyKind();
102 return (kind
== BitRecTyKind
&& Size
== 1) || (kind
== IntRecTyKind
);
105 bool BitsRecTy::typeIsA(const RecTy
*RHS
) const {
106 if (const BitsRecTy
*RHSb
= dyn_cast
<BitsRecTy
>(RHS
))
107 return RHSb
->Size
== Size
;
111 bool IntRecTy::typeIsConvertibleTo(const RecTy
*RHS
) const {
112 RecTyKind kind
= RHS
->getRecTyKind();
113 return kind
==BitRecTyKind
|| kind
==BitsRecTyKind
|| kind
==IntRecTyKind
;
116 bool CodeRecTy::typeIsConvertibleTo(const RecTy
*RHS
) const {
117 RecTyKind Kind
= RHS
->getRecTyKind();
118 return Kind
== CodeRecTyKind
|| Kind
== StringRecTyKind
;
121 std::string
StringRecTy::getAsString() const {
125 bool StringRecTy::typeIsConvertibleTo(const RecTy
*RHS
) const {
126 RecTyKind Kind
= RHS
->getRecTyKind();
127 return Kind
== StringRecTyKind
|| Kind
== CodeRecTyKind
;
130 std::string
ListRecTy::getAsString() const {
131 return "list<" + Ty
->getAsString() + ">";
134 bool ListRecTy::typeIsConvertibleTo(const RecTy
*RHS
) const {
135 if (const auto *ListTy
= dyn_cast
<ListRecTy
>(RHS
))
136 return Ty
->typeIsConvertibleTo(ListTy
->getElementType());
140 bool ListRecTy::typeIsA(const RecTy
*RHS
) const {
141 if (const ListRecTy
*RHSl
= dyn_cast
<ListRecTy
>(RHS
))
142 return getElementType()->typeIsA(RHSl
->getElementType());
146 std::string
DagRecTy::getAsString() const {
150 static void ProfileRecordRecTy(FoldingSetNodeID
&ID
,
151 ArrayRef
<Record
*> Classes
) {
152 ID
.AddInteger(Classes
.size());
153 for (Record
*R
: Classes
)
157 RecordRecTy
*RecordRecTy::get(ArrayRef
<Record
*> UnsortedClasses
) {
158 if (UnsortedClasses
.empty()) {
159 static RecordRecTy
AnyRecord(0);
163 FoldingSet
<RecordRecTy
> &ThePool
=
164 UnsortedClasses
[0]->getRecords().RecordTypePool
;
166 SmallVector
<Record
*, 4> Classes(UnsortedClasses
.begin(),
167 UnsortedClasses
.end());
168 llvm::sort(Classes
, [](Record
*LHS
, Record
*RHS
) {
169 return LHS
->getNameInitAsString() < RHS
->getNameInitAsString();
173 ProfileRecordRecTy(ID
, Classes
);
176 if (RecordRecTy
*Ty
= ThePool
.FindNodeOrInsertPos(ID
, IP
))
180 // Check for redundancy.
181 for (unsigned i
= 0; i
< Classes
.size(); ++i
) {
182 for (unsigned j
= 0; j
< Classes
.size(); ++j
) {
183 assert(i
== j
|| !Classes
[i
]->isSubClassOf(Classes
[j
]));
185 assert(&Classes
[0]->getRecords() == &Classes
[i
]->getRecords());
189 void *Mem
= Allocator
.Allocate(totalSizeToAlloc
<Record
*>(Classes
.size()),
190 alignof(RecordRecTy
));
191 RecordRecTy
*Ty
= new(Mem
) RecordRecTy(Classes
.size());
192 std::uninitialized_copy(Classes
.begin(), Classes
.end(),
193 Ty
->getTrailingObjects
<Record
*>());
194 ThePool
.InsertNode(Ty
, IP
);
198 void RecordRecTy::Profile(FoldingSetNodeID
&ID
) const {
199 ProfileRecordRecTy(ID
, getClasses());
202 std::string
RecordRecTy::getAsString() const {
204 return getClasses()[0]->getNameInitAsString();
206 std::string Str
= "{";
208 for (Record
*R
: getClasses()) {
212 Str
+= R
->getNameInitAsString();
218 bool RecordRecTy::isSubClassOf(Record
*Class
) const {
219 return llvm::any_of(getClasses(), [Class
](Record
*MySuperClass
) {
220 return MySuperClass
== Class
||
221 MySuperClass
->isSubClassOf(Class
);
225 bool RecordRecTy::typeIsConvertibleTo(const RecTy
*RHS
) const {
229 const RecordRecTy
*RTy
= dyn_cast
<RecordRecTy
>(RHS
);
233 return llvm::all_of(RTy
->getClasses(), [this](Record
*TargetClass
) {
234 return isSubClassOf(TargetClass
);
238 bool RecordRecTy::typeIsA(const RecTy
*RHS
) const {
239 return typeIsConvertibleTo(RHS
);
242 static RecordRecTy
*resolveRecordTypes(RecordRecTy
*T1
, RecordRecTy
*T2
) {
243 SmallVector
<Record
*, 4> CommonSuperClasses
;
244 SmallVector
<Record
*, 4> Stack
;
246 Stack
.insert(Stack
.end(), T1
->classes_begin(), T1
->classes_end());
248 while (!Stack
.empty()) {
249 Record
*R
= Stack
.back();
252 if (T2
->isSubClassOf(R
)) {
253 CommonSuperClasses
.push_back(R
);
255 R
->getDirectSuperClasses(Stack
);
259 return RecordRecTy::get(CommonSuperClasses
);
262 RecTy
*llvm::resolveTypes(RecTy
*T1
, RecTy
*T2
) {
266 if (RecordRecTy
*RecTy1
= dyn_cast
<RecordRecTy
>(T1
)) {
267 if (RecordRecTy
*RecTy2
= dyn_cast
<RecordRecTy
>(T2
))
268 return resolveRecordTypes(RecTy1
, RecTy2
);
271 if (T1
->typeIsConvertibleTo(T2
))
273 if (T2
->typeIsConvertibleTo(T1
))
276 if (ListRecTy
*ListTy1
= dyn_cast
<ListRecTy
>(T1
)) {
277 if (ListRecTy
*ListTy2
= dyn_cast
<ListRecTy
>(T2
)) {
278 RecTy
* NewType
= resolveTypes(ListTy1
->getElementType(),
279 ListTy2
->getElementType());
281 return NewType
->getListTy();
288 //===----------------------------------------------------------------------===//
289 // Initializer implementations
290 //===----------------------------------------------------------------------===//
292 void Init::anchor() {}
294 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
295 LLVM_DUMP_METHOD
void Init::dump() const { return print(errs()); }
298 UnsetInit
*UnsetInit::get() {
299 static UnsetInit TheInit
;
303 Init
*UnsetInit::getCastTo(RecTy
*Ty
) const {
304 return const_cast<UnsetInit
*>(this);
307 Init
*UnsetInit::convertInitializerTo(RecTy
*Ty
) const {
308 return const_cast<UnsetInit
*>(this);
311 BitInit
*BitInit::get(bool V
) {
312 static BitInit
True(true);
313 static BitInit
False(false);
315 return V
? &True
: &False
;
318 Init
*BitInit::convertInitializerTo(RecTy
*Ty
) const {
319 if (isa
<BitRecTy
>(Ty
))
320 return const_cast<BitInit
*>(this);
322 if (isa
<IntRecTy
>(Ty
))
323 return IntInit::get(getValue());
325 if (auto *BRT
= dyn_cast
<BitsRecTy
>(Ty
)) {
326 // Can only convert single bit.
327 if (BRT
->getNumBits() == 1)
328 return BitsInit::get(const_cast<BitInit
*>(this));
335 ProfileBitsInit(FoldingSetNodeID
&ID
, ArrayRef
<Init
*> Range
) {
336 ID
.AddInteger(Range
.size());
338 for (Init
*I
: Range
)
342 BitsInit
*BitsInit::get(ArrayRef
<Init
*> Range
) {
343 static FoldingSet
<BitsInit
> ThePool
;
346 ProfileBitsInit(ID
, Range
);
349 if (BitsInit
*I
= ThePool
.FindNodeOrInsertPos(ID
, IP
))
352 void *Mem
= Allocator
.Allocate(totalSizeToAlloc
<Init
*>(Range
.size()),
354 BitsInit
*I
= new(Mem
) BitsInit(Range
.size());
355 std::uninitialized_copy(Range
.begin(), Range
.end(),
356 I
->getTrailingObjects
<Init
*>());
357 ThePool
.InsertNode(I
, IP
);
361 void BitsInit::Profile(FoldingSetNodeID
&ID
) const {
362 ProfileBitsInit(ID
, makeArrayRef(getTrailingObjects
<Init
*>(), NumBits
));
365 Init
*BitsInit::convertInitializerTo(RecTy
*Ty
) const {
366 if (isa
<BitRecTy
>(Ty
)) {
367 if (getNumBits() != 1) return nullptr; // Only accept if just one bit!
371 if (auto *BRT
= dyn_cast
<BitsRecTy
>(Ty
)) {
372 // If the number of bits is right, return it. Otherwise we need to expand
374 if (getNumBits() != BRT
->getNumBits()) return nullptr;
375 return const_cast<BitsInit
*>(this);
378 if (isa
<IntRecTy
>(Ty
)) {
380 for (unsigned i
= 0, e
= getNumBits(); i
!= e
; ++i
)
381 if (auto *Bit
= dyn_cast
<BitInit
>(getBit(i
)))
382 Result
|= static_cast<int64_t>(Bit
->getValue()) << i
;
385 return IntInit::get(Result
);
392 BitsInit::convertInitializerBitRange(ArrayRef
<unsigned> Bits
) const {
393 SmallVector
<Init
*, 16> NewBits(Bits
.size());
395 for (unsigned i
= 0, e
= Bits
.size(); i
!= e
; ++i
) {
396 if (Bits
[i
] >= getNumBits())
398 NewBits
[i
] = getBit(Bits
[i
]);
400 return BitsInit::get(NewBits
);
403 bool BitsInit::isConcrete() const {
404 for (unsigned i
= 0, e
= getNumBits(); i
!= e
; ++i
) {
405 if (!getBit(i
)->isConcrete())
411 std::string
BitsInit::getAsString() const {
412 std::string Result
= "{ ";
413 for (unsigned i
= 0, e
= getNumBits(); i
!= e
; ++i
) {
414 if (i
) Result
+= ", ";
415 if (Init
*Bit
= getBit(e
-i
-1))
416 Result
+= Bit
->getAsString();
420 return Result
+ " }";
423 // resolveReferences - If there are any field references that refer to fields
424 // that have been filled in, we can propagate the values now.
425 Init
*BitsInit::resolveReferences(Resolver
&R
) const {
426 bool Changed
= false;
427 SmallVector
<Init
*, 16> NewBits(getNumBits());
429 Init
*CachedBitVarRef
= nullptr;
430 Init
*CachedBitVarResolved
= nullptr;
432 for (unsigned i
= 0, e
= getNumBits(); i
!= e
; ++i
) {
433 Init
*CurBit
= getBit(i
);
434 Init
*NewBit
= CurBit
;
436 if (VarBitInit
*CurBitVar
= dyn_cast
<VarBitInit
>(CurBit
)) {
437 if (CurBitVar
->getBitVar() != CachedBitVarRef
) {
438 CachedBitVarRef
= CurBitVar
->getBitVar();
439 CachedBitVarResolved
= CachedBitVarRef
->resolveReferences(R
);
441 assert(CachedBitVarResolved
&& "Unresolved bitvar reference");
442 NewBit
= CachedBitVarResolved
->getBit(CurBitVar
->getBitNum());
444 // getBit(0) implicitly converts int and bits<1> values to bit.
445 NewBit
= CurBit
->resolveReferences(R
)->getBit(0);
448 if (isa
<UnsetInit
>(NewBit
) && R
.keepUnsetBits())
451 Changed
|= CurBit
!= NewBit
;
455 return BitsInit::get(NewBits
);
457 return const_cast<BitsInit
*>(this);
460 IntInit
*IntInit::get(int64_t V
) {
461 static std::map
<int64_t, IntInit
*> ThePool
;
463 IntInit
*&I
= ThePool
[V
];
464 if (!I
) I
= new(Allocator
) IntInit(V
);
468 std::string
IntInit::getAsString() const {
469 return itostr(Value
);
472 static bool canFitInBitfield(int64_t Value
, unsigned NumBits
) {
473 // For example, with NumBits == 4, we permit Values from [-7 .. 15].
474 return (NumBits
>= sizeof(Value
) * 8) ||
475 (Value
>> NumBits
== 0) || (Value
>> (NumBits
-1) == -1);
478 Init
*IntInit::convertInitializerTo(RecTy
*Ty
) const {
479 if (isa
<IntRecTy
>(Ty
))
480 return const_cast<IntInit
*>(this);
482 if (isa
<BitRecTy
>(Ty
)) {
483 int64_t Val
= getValue();
484 if (Val
!= 0 && Val
!= 1) return nullptr; // Only accept 0 or 1 for a bit!
485 return BitInit::get(Val
!= 0);
488 if (auto *BRT
= dyn_cast
<BitsRecTy
>(Ty
)) {
489 int64_t Value
= getValue();
490 // Make sure this bitfield is large enough to hold the integer value.
491 if (!canFitInBitfield(Value
, BRT
->getNumBits()))
494 SmallVector
<Init
*, 16> NewBits(BRT
->getNumBits());
495 for (unsigned i
= 0; i
!= BRT
->getNumBits(); ++i
)
496 NewBits
[i
] = BitInit::get(Value
& ((i
< 64) ? (1LL << i
) : 0));
498 return BitsInit::get(NewBits
);
505 IntInit::convertInitializerBitRange(ArrayRef
<unsigned> Bits
) const {
506 SmallVector
<Init
*, 16> NewBits(Bits
.size());
508 for (unsigned i
= 0, e
= Bits
.size(); i
!= e
; ++i
) {
512 NewBits
[i
] = BitInit::get(Value
& (INT64_C(1) << Bits
[i
]));
514 return BitsInit::get(NewBits
);
517 CodeInit
*CodeInit::get(StringRef V
, const SMLoc
&Loc
) {
518 static StringSet
<BumpPtrAllocator
&> ThePool(Allocator
);
520 CodeInitsConstructed
++;
522 // Unlike StringMap, StringSet doesn't accept empty keys.
524 return new (Allocator
) CodeInit("", Loc
);
526 // Location tracking prevents us from de-duping CodeInits as we're never
527 // called with the same string and same location twice. However, we can at
528 // least de-dupe the strings for a modest saving.
529 auto &Entry
= *ThePool
.insert(V
).first
;
530 return new(Allocator
) CodeInit(Entry
.getKey(), Loc
);
533 StringInit
*StringInit::get(StringRef V
) {
534 static StringMap
<StringInit
*, BumpPtrAllocator
&> ThePool(Allocator
);
536 auto &Entry
= *ThePool
.insert(std::make_pair(V
, nullptr)).first
;
538 Entry
.second
= new(Allocator
) StringInit(Entry
.getKey());
542 Init
*StringInit::convertInitializerTo(RecTy
*Ty
) const {
543 if (isa
<StringRecTy
>(Ty
))
544 return const_cast<StringInit
*>(this);
545 if (isa
<CodeRecTy
>(Ty
))
546 return CodeInit::get(getValue(), SMLoc());
551 Init
*CodeInit::convertInitializerTo(RecTy
*Ty
) const {
552 if (isa
<CodeRecTy
>(Ty
))
553 return const_cast<CodeInit
*>(this);
554 if (isa
<StringRecTy
>(Ty
))
555 return StringInit::get(getValue());
560 static void ProfileListInit(FoldingSetNodeID
&ID
,
561 ArrayRef
<Init
*> Range
,
563 ID
.AddInteger(Range
.size());
564 ID
.AddPointer(EltTy
);
566 for (Init
*I
: Range
)
570 ListInit
*ListInit::get(ArrayRef
<Init
*> Range
, RecTy
*EltTy
) {
571 static FoldingSet
<ListInit
> ThePool
;
574 ProfileListInit(ID
, Range
, EltTy
);
577 if (ListInit
*I
= ThePool
.FindNodeOrInsertPos(ID
, IP
))
580 assert(Range
.empty() || !isa
<TypedInit
>(Range
[0]) ||
581 cast
<TypedInit
>(Range
[0])->getType()->typeIsConvertibleTo(EltTy
));
583 void *Mem
= Allocator
.Allocate(totalSizeToAlloc
<Init
*>(Range
.size()),
585 ListInit
*I
= new(Mem
) ListInit(Range
.size(), EltTy
);
586 std::uninitialized_copy(Range
.begin(), Range
.end(),
587 I
->getTrailingObjects
<Init
*>());
588 ThePool
.InsertNode(I
, IP
);
592 void ListInit::Profile(FoldingSetNodeID
&ID
) const {
593 RecTy
*EltTy
= cast
<ListRecTy
>(getType())->getElementType();
595 ProfileListInit(ID
, getValues(), EltTy
);
598 Init
*ListInit::convertInitializerTo(RecTy
*Ty
) const {
600 return const_cast<ListInit
*>(this);
602 if (auto *LRT
= dyn_cast
<ListRecTy
>(Ty
)) {
603 SmallVector
<Init
*, 8> Elements
;
604 Elements
.reserve(getValues().size());
606 // Verify that all of the elements of the list are subclasses of the
607 // appropriate class!
608 bool Changed
= false;
609 RecTy
*ElementType
= LRT
->getElementType();
610 for (Init
*I
: getValues())
611 if (Init
*CI
= I
->convertInitializerTo(ElementType
)) {
612 Elements
.push_back(CI
);
619 return const_cast<ListInit
*>(this);
620 return ListInit::get(Elements
, ElementType
);
626 Init
*ListInit::convertInitListSlice(ArrayRef
<unsigned> Elements
) const {
627 SmallVector
<Init
*, 8> Vals
;
628 Vals
.reserve(Elements
.size());
629 for (unsigned Element
: Elements
) {
630 if (Element
>= size())
632 Vals
.push_back(getElement(Element
));
634 return ListInit::get(Vals
, getElementType());
637 Record
*ListInit::getElementAsRecord(unsigned i
) const {
638 assert(i
< NumValues
&& "List element index out of range!");
639 DefInit
*DI
= dyn_cast
<DefInit
>(getElement(i
));
641 PrintFatalError("Expected record in list!");
645 Init
*ListInit::resolveReferences(Resolver
&R
) const {
646 SmallVector
<Init
*, 8> Resolved
;
647 Resolved
.reserve(size());
648 bool Changed
= false;
650 for (Init
*CurElt
: getValues()) {
651 Init
*E
= CurElt
->resolveReferences(R
);
652 Changed
|= E
!= CurElt
;
653 Resolved
.push_back(E
);
657 return ListInit::get(Resolved
, getElementType());
658 return const_cast<ListInit
*>(this);
661 bool ListInit::isConcrete() const {
662 for (Init
*Element
: *this) {
663 if (!Element
->isConcrete())
669 std::string
ListInit::getAsString() const {
670 std::string Result
= "[";
671 const char *sep
= "";
672 for (Init
*Element
: *this) {
675 Result
+= Element
->getAsString();
680 Init
*OpInit::getBit(unsigned Bit
) const {
681 if (getType() == BitRecTy::get())
682 return const_cast<OpInit
*>(this);
683 return VarBitInit::get(const_cast<OpInit
*>(this), Bit
);
687 ProfileUnOpInit(FoldingSetNodeID
&ID
, unsigned Opcode
, Init
*Op
, RecTy
*Type
) {
688 ID
.AddInteger(Opcode
);
693 UnOpInit
*UnOpInit::get(UnaryOp Opc
, Init
*LHS
, RecTy
*Type
) {
694 static FoldingSet
<UnOpInit
> ThePool
;
697 ProfileUnOpInit(ID
, Opc
, LHS
, Type
);
700 if (UnOpInit
*I
= ThePool
.FindNodeOrInsertPos(ID
, IP
))
703 UnOpInit
*I
= new(Allocator
) UnOpInit(Opc
, LHS
, Type
);
704 ThePool
.InsertNode(I
, IP
);
708 void UnOpInit::Profile(FoldingSetNodeID
&ID
) const {
709 ProfileUnOpInit(ID
, getOpcode(), getOperand(), getType());
712 Init
*UnOpInit::Fold(Record
*CurRec
, bool IsFinal
) const {
713 switch (getOpcode()) {
715 if (isa
<StringRecTy
>(getType())) {
716 if (StringInit
*LHSs
= dyn_cast
<StringInit
>(LHS
))
719 if (DefInit
*LHSd
= dyn_cast
<DefInit
>(LHS
))
720 return StringInit::get(LHSd
->getAsString());
722 if (IntInit
*LHSi
= dyn_cast
<IntInit
>(LHS
))
723 return StringInit::get(LHSi
->getAsString());
724 } else if (isa
<RecordRecTy
>(getType())) {
725 if (StringInit
*Name
= dyn_cast
<StringInit
>(LHS
)) {
726 if (!CurRec
&& !IsFinal
)
728 assert(CurRec
&& "NULL pointer");
731 // Self-references are allowed, but their resolution is delayed until
732 // the final resolve to ensure that we get the correct type for them.
733 if (Name
== CurRec
->getNameInit()) {
738 D
= CurRec
->getRecords().getDef(Name
->getValue());
741 PrintFatalError(CurRec
->getLoc(),
742 Twine("Undefined reference to record: '") +
743 Name
->getValue() + "'\n");
748 DefInit
*DI
= DefInit::get(D
);
749 if (!DI
->getType()->typeIsA(getType())) {
750 PrintFatalError(CurRec
->getLoc(),
751 Twine("Expected type '") +
752 getType()->getAsString() + "', got '" +
753 DI
->getType()->getAsString() + "' in: " +
754 getAsString() + "\n");
760 if (Init
*NewInit
= LHS
->convertInitializerTo(getType()))
765 if (ListInit
*LHSl
= dyn_cast
<ListInit
>(LHS
)) {
766 assert(!LHSl
->empty() && "Empty list in head");
767 return LHSl
->getElement(0);
772 if (ListInit
*LHSl
= dyn_cast
<ListInit
>(LHS
)) {
773 assert(!LHSl
->empty() && "Empty list in tail");
774 // Note the +1. We can't just pass the result of getValues()
776 return ListInit::get(LHSl
->getValues().slice(1), LHSl
->getElementType());
781 if (ListInit
*LHSl
= dyn_cast
<ListInit
>(LHS
))
782 return IntInit::get(LHSl
->size());
786 if (ListInit
*LHSl
= dyn_cast
<ListInit
>(LHS
))
787 return IntInit::get(LHSl
->empty());
788 if (StringInit
*LHSs
= dyn_cast
<StringInit
>(LHS
))
789 return IntInit::get(LHSs
->getValue().empty());
793 if (DagInit
*Dag
= dyn_cast
<DagInit
>(LHS
)) {
794 DefInit
*DI
= DefInit::get(Dag
->getOperatorAsDef({}));
795 if (!DI
->getType()->typeIsA(getType())) {
796 PrintFatalError(CurRec
->getLoc(),
797 Twine("Expected type '") +
798 getType()->getAsString() + "', got '" +
799 DI
->getType()->getAsString() + "' in: " +
800 getAsString() + "\n");
807 return const_cast<UnOpInit
*>(this);
810 Init
*UnOpInit::resolveReferences(Resolver
&R
) const {
811 Init
*lhs
= LHS
->resolveReferences(R
);
813 if (LHS
!= lhs
|| (R
.isFinal() && getOpcode() == CAST
))
814 return (UnOpInit::get(getOpcode(), lhs
, getType()))
815 ->Fold(R
.getCurrentRecord(), R
.isFinal());
816 return const_cast<UnOpInit
*>(this);
819 std::string
UnOpInit::getAsString() const {
821 switch (getOpcode()) {
822 case CAST
: Result
= "!cast<" + getType()->getAsString() + ">"; break;
823 case HEAD
: Result
= "!head"; break;
824 case TAIL
: Result
= "!tail"; break;
825 case SIZE
: Result
= "!size"; break;
826 case EMPTY
: Result
= "!empty"; break;
827 case GETOP
: Result
= "!getop"; break;
829 return Result
+ "(" + LHS
->getAsString() + ")";
833 ProfileBinOpInit(FoldingSetNodeID
&ID
, unsigned Opcode
, Init
*LHS
, Init
*RHS
,
835 ID
.AddInteger(Opcode
);
841 BinOpInit
*BinOpInit::get(BinaryOp Opc
, Init
*LHS
,
842 Init
*RHS
, RecTy
*Type
) {
843 static FoldingSet
<BinOpInit
> ThePool
;
846 ProfileBinOpInit(ID
, Opc
, LHS
, RHS
, Type
);
849 if (BinOpInit
*I
= ThePool
.FindNodeOrInsertPos(ID
, IP
))
852 BinOpInit
*I
= new(Allocator
) BinOpInit(Opc
, LHS
, RHS
, Type
);
853 ThePool
.InsertNode(I
, IP
);
857 void BinOpInit::Profile(FoldingSetNodeID
&ID
) const {
858 ProfileBinOpInit(ID
, getOpcode(), getLHS(), getRHS(), getType());
861 static StringInit
*ConcatStringInits(const StringInit
*I0
,
862 const StringInit
*I1
) {
863 SmallString
<80> Concat(I0
->getValue());
864 Concat
.append(I1
->getValue());
865 return StringInit::get(Concat
);
868 Init
*BinOpInit::getStrConcat(Init
*I0
, Init
*I1
) {
869 // Shortcut for the common case of concatenating two strings.
870 if (const StringInit
*I0s
= dyn_cast
<StringInit
>(I0
))
871 if (const StringInit
*I1s
= dyn_cast
<StringInit
>(I1
))
872 return ConcatStringInits(I0s
, I1s
);
873 return BinOpInit::get(BinOpInit::STRCONCAT
, I0
, I1
, StringRecTy::get());
876 static ListInit
*ConcatListInits(const ListInit
*LHS
,
877 const ListInit
*RHS
) {
878 SmallVector
<Init
*, 8> Args
;
879 Args
.insert(Args
.end(), LHS
->begin(), LHS
->end());
880 Args
.insert(Args
.end(), RHS
->begin(), RHS
->end());
881 return ListInit::get(Args
, LHS
->getElementType());
884 Init
*BinOpInit::getListConcat(TypedInit
*LHS
, Init
*RHS
) {
885 assert(isa
<ListRecTy
>(LHS
->getType()) && "First arg must be a list");
887 // Shortcut for the common case of concatenating two lists.
888 if (const ListInit
*LHSList
= dyn_cast
<ListInit
>(LHS
))
889 if (const ListInit
*RHSList
= dyn_cast
<ListInit
>(RHS
))
890 return ConcatListInits(LHSList
, RHSList
);
891 return BinOpInit::get(BinOpInit::LISTCONCAT
, LHS
, RHS
, LHS
->getType());
894 Init
*BinOpInit::getListSplat(TypedInit
*LHS
, Init
*RHS
) {
895 return BinOpInit::get(BinOpInit::LISTSPLAT
, LHS
, RHS
, LHS
->getType());
898 Init
*BinOpInit::Fold(Record
*CurRec
) const {
899 switch (getOpcode()) {
901 DagInit
*LHSs
= dyn_cast
<DagInit
>(LHS
);
902 DagInit
*RHSs
= dyn_cast
<DagInit
>(RHS
);
904 DefInit
*LOp
= dyn_cast
<DefInit
>(LHSs
->getOperator());
905 DefInit
*ROp
= dyn_cast
<DefInit
>(RHSs
->getOperator());
906 if ((!LOp
&& !isa
<UnsetInit
>(LHSs
->getOperator())) ||
907 (!ROp
&& !isa
<UnsetInit
>(RHSs
->getOperator())))
909 if (LOp
&& ROp
&& LOp
->getDef() != ROp
->getDef()) {
910 PrintFatalError(Twine("Concatenated Dag operators do not match: '") +
911 LHSs
->getAsString() + "' vs. '" + RHSs
->getAsString() +
914 Init
*Op
= LOp
? LOp
: ROp
;
916 Op
= UnsetInit::get();
918 SmallVector
<Init
*, 8> Args
;
919 SmallVector
<StringInit
*, 8> ArgNames
;
920 for (unsigned i
= 0, e
= LHSs
->getNumArgs(); i
!= e
; ++i
) {
921 Args
.push_back(LHSs
->getArg(i
));
922 ArgNames
.push_back(LHSs
->getArgName(i
));
924 for (unsigned i
= 0, e
= RHSs
->getNumArgs(); i
!= e
; ++i
) {
925 Args
.push_back(RHSs
->getArg(i
));
926 ArgNames
.push_back(RHSs
->getArgName(i
));
928 return DagInit::get(Op
, nullptr, Args
, ArgNames
);
933 ListInit
*LHSs
= dyn_cast
<ListInit
>(LHS
);
934 ListInit
*RHSs
= dyn_cast
<ListInit
>(RHS
);
936 SmallVector
<Init
*, 8> Args
;
937 Args
.insert(Args
.end(), LHSs
->begin(), LHSs
->end());
938 Args
.insert(Args
.end(), RHSs
->begin(), RHSs
->end());
939 return ListInit::get(Args
, LHSs
->getElementType());
944 TypedInit
*Value
= dyn_cast
<TypedInit
>(LHS
);
945 IntInit
*Size
= dyn_cast
<IntInit
>(RHS
);
947 SmallVector
<Init
*, 8> Args(Size
->getValue(), Value
);
948 return ListInit::get(Args
, Value
->getType());
953 StringInit
*LHSs
= dyn_cast
<StringInit
>(LHS
);
954 StringInit
*RHSs
= dyn_cast
<StringInit
>(RHS
);
956 return ConcatStringInits(LHSs
, RHSs
);
965 // try to fold eq comparison for 'bit' and 'int', otherwise fallback
966 // to string objects.
968 dyn_cast_or_null
<IntInit
>(LHS
->convertInitializerTo(IntRecTy::get()));
970 dyn_cast_or_null
<IntInit
>(RHS
->convertInitializerTo(IntRecTy::get()));
974 switch (getOpcode()) {
975 case EQ
: Result
= L
->getValue() == R
->getValue(); break;
976 case NE
: Result
= L
->getValue() != R
->getValue(); break;
977 case LE
: Result
= L
->getValue() <= R
->getValue(); break;
978 case LT
: Result
= L
->getValue() < R
->getValue(); break;
979 case GE
: Result
= L
->getValue() >= R
->getValue(); break;
980 case GT
: Result
= L
->getValue() > R
->getValue(); break;
981 default: llvm_unreachable("unhandled comparison");
983 return BitInit::get(Result
);
986 if (getOpcode() == EQ
|| getOpcode() == NE
) {
987 StringInit
*LHSs
= dyn_cast
<StringInit
>(LHS
);
988 StringInit
*RHSs
= dyn_cast
<StringInit
>(RHS
);
990 // Make sure we've resolved
992 bool Equal
= LHSs
->getValue() == RHSs
->getValue();
993 return BitInit::get(getOpcode() == EQ
? Equal
: !Equal
);
1000 DagInit
*Dag
= dyn_cast
<DagInit
>(LHS
);
1001 DefInit
*Op
= dyn_cast
<DefInit
>(RHS
);
1003 SmallVector
<Init
*, 8> Args
;
1004 SmallVector
<StringInit
*, 8> ArgNames
;
1005 for (unsigned i
= 0, e
= Dag
->getNumArgs(); i
!= e
; ++i
) {
1006 Args
.push_back(Dag
->getArg(i
));
1007 ArgNames
.push_back(Dag
->getArgName(i
));
1009 return DagInit::get(Op
, nullptr, Args
, ArgNames
);
1021 dyn_cast_or_null
<IntInit
>(LHS
->convertInitializerTo(IntRecTy::get()));
1023 dyn_cast_or_null
<IntInit
>(RHS
->convertInitializerTo(IntRecTy::get()));
1025 int64_t LHSv
= LHSi
->getValue(), RHSv
= RHSi
->getValue();
1027 switch (getOpcode()) {
1028 default: llvm_unreachable("Bad opcode!");
1029 case ADD
: Result
= LHSv
+ RHSv
; break;
1030 case MUL
: Result
= LHSv
* RHSv
; break;
1031 case AND
: Result
= LHSv
& RHSv
; break;
1032 case OR
: Result
= LHSv
| RHSv
; break;
1033 case SHL
: Result
= LHSv
<< RHSv
; break;
1034 case SRA
: Result
= LHSv
>> RHSv
; break;
1035 case SRL
: Result
= (uint64_t)LHSv
>> (uint64_t)RHSv
; break;
1037 return IntInit::get(Result
);
1042 return const_cast<BinOpInit
*>(this);
1045 Init
*BinOpInit::resolveReferences(Resolver
&R
) const {
1046 Init
*lhs
= LHS
->resolveReferences(R
);
1047 Init
*rhs
= RHS
->resolveReferences(R
);
1049 if (LHS
!= lhs
|| RHS
!= rhs
)
1050 return (BinOpInit::get(getOpcode(), lhs
, rhs
, getType()))
1051 ->Fold(R
.getCurrentRecord());
1052 return const_cast<BinOpInit
*>(this);
1055 std::string
BinOpInit::getAsString() const {
1057 switch (getOpcode()) {
1058 case CONCAT
: Result
= "!con"; break;
1059 case ADD
: Result
= "!add"; break;
1060 case MUL
: Result
= "!mul"; break;
1061 case AND
: Result
= "!and"; break;
1062 case OR
: Result
= "!or"; break;
1063 case SHL
: Result
= "!shl"; break;
1064 case SRA
: Result
= "!sra"; break;
1065 case SRL
: Result
= "!srl"; break;
1066 case EQ
: Result
= "!eq"; break;
1067 case NE
: Result
= "!ne"; break;
1068 case LE
: Result
= "!le"; break;
1069 case LT
: Result
= "!lt"; break;
1070 case GE
: Result
= "!ge"; break;
1071 case GT
: Result
= "!gt"; break;
1072 case LISTCONCAT
: Result
= "!listconcat"; break;
1073 case LISTSPLAT
: Result
= "!listsplat"; break;
1074 case STRCONCAT
: Result
= "!strconcat"; break;
1075 case SETOP
: Result
= "!setop"; break;
1077 return Result
+ "(" + LHS
->getAsString() + ", " + RHS
->getAsString() + ")";
1081 ProfileTernOpInit(FoldingSetNodeID
&ID
, unsigned Opcode
, Init
*LHS
, Init
*MHS
,
1082 Init
*RHS
, RecTy
*Type
) {
1083 ID
.AddInteger(Opcode
);
1087 ID
.AddPointer(Type
);
1090 TernOpInit
*TernOpInit::get(TernaryOp Opc
, Init
*LHS
, Init
*MHS
, Init
*RHS
,
1092 static FoldingSet
<TernOpInit
> ThePool
;
1094 FoldingSetNodeID ID
;
1095 ProfileTernOpInit(ID
, Opc
, LHS
, MHS
, RHS
, Type
);
1098 if (TernOpInit
*I
= ThePool
.FindNodeOrInsertPos(ID
, IP
))
1101 TernOpInit
*I
= new(Allocator
) TernOpInit(Opc
, LHS
, MHS
, RHS
, Type
);
1102 ThePool
.InsertNode(I
, IP
);
1106 void TernOpInit::Profile(FoldingSetNodeID
&ID
) const {
1107 ProfileTernOpInit(ID
, getOpcode(), getLHS(), getMHS(), getRHS(), getType());
1110 static Init
*ForeachApply(Init
*LHS
, Init
*MHSe
, Init
*RHS
, Record
*CurRec
) {
1111 MapResolver
R(CurRec
);
1113 return RHS
->resolveReferences(R
);
1116 static Init
*ForeachDagApply(Init
*LHS
, DagInit
*MHSd
, Init
*RHS
,
1118 bool Change
= false;
1119 Init
*Val
= ForeachApply(LHS
, MHSd
->getOperator(), RHS
, CurRec
);
1120 if (Val
!= MHSd
->getOperator())
1123 SmallVector
<std::pair
<Init
*, StringInit
*>, 8> NewArgs
;
1124 for (unsigned int i
= 0; i
< MHSd
->getNumArgs(); ++i
) {
1125 Init
*Arg
= MHSd
->getArg(i
);
1127 StringInit
*ArgName
= MHSd
->getArgName(i
);
1129 if (DagInit
*Argd
= dyn_cast
<DagInit
>(Arg
))
1130 NewArg
= ForeachDagApply(LHS
, Argd
, RHS
, CurRec
);
1132 NewArg
= ForeachApply(LHS
, Arg
, RHS
, CurRec
);
1134 NewArgs
.push_back(std::make_pair(NewArg
, ArgName
));
1140 return DagInit::get(Val
, nullptr, NewArgs
);
1144 // Applies RHS to all elements of MHS, using LHS as a temp variable.
1145 static Init
*ForeachHelper(Init
*LHS
, Init
*MHS
, Init
*RHS
, RecTy
*Type
,
1147 if (DagInit
*MHSd
= dyn_cast
<DagInit
>(MHS
))
1148 return ForeachDagApply(LHS
, MHSd
, RHS
, CurRec
);
1150 if (ListInit
*MHSl
= dyn_cast
<ListInit
>(MHS
)) {
1151 SmallVector
<Init
*, 8> NewList(MHSl
->begin(), MHSl
->end());
1153 for (Init
*&Item
: NewList
) {
1154 Init
*NewItem
= ForeachApply(LHS
, Item
, RHS
, CurRec
);
1155 if (NewItem
!= Item
)
1158 return ListInit::get(NewList
, cast
<ListRecTy
>(Type
)->getElementType());
1164 Init
*TernOpInit::Fold(Record
*CurRec
) const {
1165 switch (getOpcode()) {
1167 DefInit
*LHSd
= dyn_cast
<DefInit
>(LHS
);
1168 VarInit
*LHSv
= dyn_cast
<VarInit
>(LHS
);
1169 StringInit
*LHSs
= dyn_cast
<StringInit
>(LHS
);
1171 DefInit
*MHSd
= dyn_cast
<DefInit
>(MHS
);
1172 VarInit
*MHSv
= dyn_cast
<VarInit
>(MHS
);
1173 StringInit
*MHSs
= dyn_cast
<StringInit
>(MHS
);
1175 DefInit
*RHSd
= dyn_cast
<DefInit
>(RHS
);
1176 VarInit
*RHSv
= dyn_cast
<VarInit
>(RHS
);
1177 StringInit
*RHSs
= dyn_cast
<StringInit
>(RHS
);
1179 if (LHSd
&& MHSd
&& RHSd
) {
1180 Record
*Val
= RHSd
->getDef();
1181 if (LHSd
->getAsString() == RHSd
->getAsString())
1182 Val
= MHSd
->getDef();
1183 return DefInit::get(Val
);
1185 if (LHSv
&& MHSv
&& RHSv
) {
1186 std::string Val
= RHSv
->getName();
1187 if (LHSv
->getAsString() == RHSv
->getAsString())
1188 Val
= MHSv
->getName();
1189 return VarInit::get(Val
, getType());
1191 if (LHSs
&& MHSs
&& RHSs
) {
1192 std::string Val
= RHSs
->getValue();
1194 std::string::size_type found
;
1195 std::string::size_type idx
= 0;
1197 found
= Val
.find(LHSs
->getValue(), idx
);
1198 if (found
== std::string::npos
)
1200 Val
.replace(found
, LHSs
->getValue().size(), MHSs
->getValue());
1201 idx
= found
+ MHSs
->getValue().size();
1204 return StringInit::get(Val
);
1210 if (Init
*Result
= ForeachHelper(LHS
, MHS
, RHS
, getType(), CurRec
))
1216 if (IntInit
*LHSi
= dyn_cast_or_null
<IntInit
>(
1217 LHS
->convertInitializerTo(IntRecTy::get()))) {
1218 if (LHSi
->getValue())
1226 ListInit
*MHSl
= dyn_cast
<ListInit
>(MHS
);
1227 ListInit
*RHSl
= dyn_cast
<ListInit
>(RHS
);
1228 bool MHSok
= MHSl
|| isa
<UnsetInit
>(MHS
);
1229 bool RHSok
= RHSl
|| isa
<UnsetInit
>(RHS
);
1231 if (isa
<UnsetInit
>(MHS
) && isa
<UnsetInit
>(RHS
))
1232 break; // Typically prevented by the parser, but might happen with template args
1234 if (MHSok
&& RHSok
&& (!MHSl
|| !RHSl
|| MHSl
->size() == RHSl
->size())) {
1235 SmallVector
<std::pair
<Init
*, StringInit
*>, 8> Children
;
1236 unsigned Size
= MHSl
? MHSl
->size() : RHSl
->size();
1237 for (unsigned i
= 0; i
!= Size
; ++i
) {
1238 Init
*Node
= MHSl
? MHSl
->getElement(i
) : UnsetInit::get();
1239 Init
*Name
= RHSl
? RHSl
->getElement(i
) : UnsetInit::get();
1240 if (!isa
<StringInit
>(Name
) && !isa
<UnsetInit
>(Name
))
1241 return const_cast<TernOpInit
*>(this);
1242 Children
.emplace_back(Node
, dyn_cast
<StringInit
>(Name
));
1244 return DagInit::get(LHS
, nullptr, Children
);
1250 return const_cast<TernOpInit
*>(this);
1253 Init
*TernOpInit::resolveReferences(Resolver
&R
) const {
1254 Init
*lhs
= LHS
->resolveReferences(R
);
1256 if (getOpcode() == IF
&& lhs
!= LHS
) {
1257 if (IntInit
*Value
= dyn_cast_or_null
<IntInit
>(
1258 lhs
->convertInitializerTo(IntRecTy::get()))) {
1260 if (Value
->getValue())
1261 return MHS
->resolveReferences(R
);
1262 return RHS
->resolveReferences(R
);
1266 Init
*mhs
= MHS
->resolveReferences(R
);
1269 if (getOpcode() == FOREACH
) {
1270 ShadowResolver
SR(R
);
1272 rhs
= RHS
->resolveReferences(SR
);
1274 rhs
= RHS
->resolveReferences(R
);
1277 if (LHS
!= lhs
|| MHS
!= mhs
|| RHS
!= rhs
)
1278 return (TernOpInit::get(getOpcode(), lhs
, mhs
, rhs
, getType()))
1279 ->Fold(R
.getCurrentRecord());
1280 return const_cast<TernOpInit
*>(this);
1283 std::string
TernOpInit::getAsString() const {
1285 bool UnquotedLHS
= false;
1286 switch (getOpcode()) {
1287 case SUBST
: Result
= "!subst"; break;
1288 case FOREACH
: Result
= "!foreach"; UnquotedLHS
= true; break;
1289 case IF
: Result
= "!if"; break;
1290 case DAG
: Result
= "!dag"; break;
1292 return (Result
+ "(" +
1293 (UnquotedLHS
? LHS
->getAsUnquotedString() : LHS
->getAsString()) +
1294 ", " + MHS
->getAsString() + ", " + RHS
->getAsString() + ")");
1297 static void ProfileFoldOpInit(FoldingSetNodeID
&ID
, Init
*A
, Init
*B
,
1298 Init
*Start
, Init
*List
, Init
*Expr
,
1300 ID
.AddPointer(Start
);
1301 ID
.AddPointer(List
);
1304 ID
.AddPointer(Expr
);
1305 ID
.AddPointer(Type
);
1308 FoldOpInit
*FoldOpInit::get(Init
*Start
, Init
*List
, Init
*A
, Init
*B
,
1309 Init
*Expr
, RecTy
*Type
) {
1310 static FoldingSet
<FoldOpInit
> ThePool
;
1312 FoldingSetNodeID ID
;
1313 ProfileFoldOpInit(ID
, Start
, List
, A
, B
, Expr
, Type
);
1316 if (FoldOpInit
*I
= ThePool
.FindNodeOrInsertPos(ID
, IP
))
1319 FoldOpInit
*I
= new (Allocator
) FoldOpInit(Start
, List
, A
, B
, Expr
, Type
);
1320 ThePool
.InsertNode(I
, IP
);
1324 void FoldOpInit::Profile(FoldingSetNodeID
&ID
) const {
1325 ProfileFoldOpInit(ID
, Start
, List
, A
, B
, Expr
, getType());
1328 Init
*FoldOpInit::Fold(Record
*CurRec
) const {
1329 if (ListInit
*LI
= dyn_cast
<ListInit
>(List
)) {
1330 Init
*Accum
= Start
;
1331 for (Init
*Elt
: *LI
) {
1332 MapResolver
R(CurRec
);
1335 Accum
= Expr
->resolveReferences(R
);
1339 return const_cast<FoldOpInit
*>(this);
1342 Init
*FoldOpInit::resolveReferences(Resolver
&R
) const {
1343 Init
*NewStart
= Start
->resolveReferences(R
);
1344 Init
*NewList
= List
->resolveReferences(R
);
1345 ShadowResolver
SR(R
);
1348 Init
*NewExpr
= Expr
->resolveReferences(SR
);
1350 if (Start
== NewStart
&& List
== NewList
&& Expr
== NewExpr
)
1351 return const_cast<FoldOpInit
*>(this);
1353 return get(NewStart
, NewList
, A
, B
, NewExpr
, getType())
1354 ->Fold(R
.getCurrentRecord());
1357 Init
*FoldOpInit::getBit(unsigned Bit
) const {
1358 return VarBitInit::get(const_cast<FoldOpInit
*>(this), Bit
);
1361 std::string
FoldOpInit::getAsString() const {
1362 return (Twine("!foldl(") + Start
->getAsString() + ", " + List
->getAsString() +
1363 ", " + A
->getAsUnquotedString() + ", " + B
->getAsUnquotedString() +
1364 ", " + Expr
->getAsString() + ")")
1368 static void ProfileIsAOpInit(FoldingSetNodeID
&ID
, RecTy
*CheckType
,
1370 ID
.AddPointer(CheckType
);
1371 ID
.AddPointer(Expr
);
1374 IsAOpInit
*IsAOpInit::get(RecTy
*CheckType
, Init
*Expr
) {
1375 static FoldingSet
<IsAOpInit
> ThePool
;
1377 FoldingSetNodeID ID
;
1378 ProfileIsAOpInit(ID
, CheckType
, Expr
);
1381 if (IsAOpInit
*I
= ThePool
.FindNodeOrInsertPos(ID
, IP
))
1384 IsAOpInit
*I
= new (Allocator
) IsAOpInit(CheckType
, Expr
);
1385 ThePool
.InsertNode(I
, IP
);
1389 void IsAOpInit::Profile(FoldingSetNodeID
&ID
) const {
1390 ProfileIsAOpInit(ID
, CheckType
, Expr
);
1393 Init
*IsAOpInit::Fold() const {
1394 if (TypedInit
*TI
= dyn_cast
<TypedInit
>(Expr
)) {
1395 // Is the expression type known to be (a subclass of) the desired type?
1396 if (TI
->getType()->typeIsConvertibleTo(CheckType
))
1397 return IntInit::get(1);
1399 if (isa
<RecordRecTy
>(CheckType
)) {
1400 // If the target type is not a subclass of the expression type, or if
1401 // the expression has fully resolved to a record, we know that it can't
1402 // be of the required type.
1403 if (!CheckType
->typeIsConvertibleTo(TI
->getType()) || isa
<DefInit
>(Expr
))
1404 return IntInit::get(0);
1406 // We treat non-record types as not castable.
1407 return IntInit::get(0);
1410 return const_cast<IsAOpInit
*>(this);
1413 Init
*IsAOpInit::resolveReferences(Resolver
&R
) const {
1414 Init
*NewExpr
= Expr
->resolveReferences(R
);
1415 if (Expr
!= NewExpr
)
1416 return get(CheckType
, NewExpr
)->Fold();
1417 return const_cast<IsAOpInit
*>(this);
1420 Init
*IsAOpInit::getBit(unsigned Bit
) const {
1421 return VarBitInit::get(const_cast<IsAOpInit
*>(this), Bit
);
1424 std::string
IsAOpInit::getAsString() const {
1425 return (Twine("!isa<") + CheckType
->getAsString() + ">(" +
1426 Expr
->getAsString() + ")")
1430 RecTy
*TypedInit::getFieldType(StringInit
*FieldName
) const {
1431 if (RecordRecTy
*RecordType
= dyn_cast
<RecordRecTy
>(getType())) {
1432 for (Record
*Rec
: RecordType
->getClasses()) {
1433 if (RecordVal
*Field
= Rec
->getValue(FieldName
))
1434 return Field
->getType();
1441 TypedInit::convertInitializerTo(RecTy
*Ty
) const {
1442 if (getType() == Ty
|| getType()->typeIsA(Ty
))
1443 return const_cast<TypedInit
*>(this);
1445 if (isa
<BitRecTy
>(getType()) && isa
<BitsRecTy
>(Ty
) &&
1446 cast
<BitsRecTy
>(Ty
)->getNumBits() == 1)
1447 return BitsInit::get({const_cast<TypedInit
*>(this)});
1452 Init
*TypedInit::convertInitializerBitRange(ArrayRef
<unsigned> Bits
) const {
1453 BitsRecTy
*T
= dyn_cast
<BitsRecTy
>(getType());
1454 if (!T
) return nullptr; // Cannot subscript a non-bits variable.
1455 unsigned NumBits
= T
->getNumBits();
1457 SmallVector
<Init
*, 16> NewBits
;
1458 NewBits
.reserve(Bits
.size());
1459 for (unsigned Bit
: Bits
) {
1463 NewBits
.push_back(VarBitInit::get(const_cast<TypedInit
*>(this), Bit
));
1465 return BitsInit::get(NewBits
);
1468 Init
*TypedInit::getCastTo(RecTy
*Ty
) const {
1469 // Handle the common case quickly
1470 if (getType() == Ty
|| getType()->typeIsA(Ty
))
1471 return const_cast<TypedInit
*>(this);
1473 if (Init
*Converted
= convertInitializerTo(Ty
)) {
1474 assert(!isa
<TypedInit
>(Converted
) ||
1475 cast
<TypedInit
>(Converted
)->getType()->typeIsA(Ty
));
1479 if (!getType()->typeIsConvertibleTo(Ty
))
1482 return UnOpInit::get(UnOpInit::CAST
, const_cast<TypedInit
*>(this), Ty
)
1486 Init
*TypedInit::convertInitListSlice(ArrayRef
<unsigned> Elements
) const {
1487 ListRecTy
*T
= dyn_cast
<ListRecTy
>(getType());
1488 if (!T
) return nullptr; // Cannot subscript a non-list variable.
1490 if (Elements
.size() == 1)
1491 return VarListElementInit::get(const_cast<TypedInit
*>(this), Elements
[0]);
1493 SmallVector
<Init
*, 8> ListInits
;
1494 ListInits
.reserve(Elements
.size());
1495 for (unsigned Element
: Elements
)
1496 ListInits
.push_back(VarListElementInit::get(const_cast<TypedInit
*>(this),
1498 return ListInit::get(ListInits
, T
->getElementType());
1502 VarInit
*VarInit::get(StringRef VN
, RecTy
*T
) {
1503 Init
*Value
= StringInit::get(VN
);
1504 return VarInit::get(Value
, T
);
1507 VarInit
*VarInit::get(Init
*VN
, RecTy
*T
) {
1508 using Key
= std::pair
<RecTy
*, Init
*>;
1509 static DenseMap
<Key
, VarInit
*> ThePool
;
1511 Key
TheKey(std::make_pair(T
, VN
));
1513 VarInit
*&I
= ThePool
[TheKey
];
1515 I
= new(Allocator
) VarInit(VN
, T
);
1519 StringRef
VarInit::getName() const {
1520 StringInit
*NameString
= cast
<StringInit
>(getNameInit());
1521 return NameString
->getValue();
1524 Init
*VarInit::getBit(unsigned Bit
) const {
1525 if (getType() == BitRecTy::get())
1526 return const_cast<VarInit
*>(this);
1527 return VarBitInit::get(const_cast<VarInit
*>(this), Bit
);
1530 Init
*VarInit::resolveReferences(Resolver
&R
) const {
1531 if (Init
*Val
= R
.resolve(VarName
))
1533 return const_cast<VarInit
*>(this);
1536 VarBitInit
*VarBitInit::get(TypedInit
*T
, unsigned B
) {
1537 using Key
= std::pair
<TypedInit
*, unsigned>;
1538 static DenseMap
<Key
, VarBitInit
*> ThePool
;
1540 Key
TheKey(std::make_pair(T
, B
));
1542 VarBitInit
*&I
= ThePool
[TheKey
];
1544 I
= new(Allocator
) VarBitInit(T
, B
);
1548 std::string
VarBitInit::getAsString() const {
1549 return TI
->getAsString() + "{" + utostr(Bit
) + "}";
1552 Init
*VarBitInit::resolveReferences(Resolver
&R
) const {
1553 Init
*I
= TI
->resolveReferences(R
);
1555 return I
->getBit(getBitNum());
1557 return const_cast<VarBitInit
*>(this);
1560 VarListElementInit
*VarListElementInit::get(TypedInit
*T
,
1562 using Key
= std::pair
<TypedInit
*, unsigned>;
1563 static DenseMap
<Key
, VarListElementInit
*> ThePool
;
1565 Key
TheKey(std::make_pair(T
, E
));
1567 VarListElementInit
*&I
= ThePool
[TheKey
];
1568 if (!I
) I
= new(Allocator
) VarListElementInit(T
, E
);
1572 std::string
VarListElementInit::getAsString() const {
1573 return TI
->getAsString() + "[" + utostr(Element
) + "]";
1576 Init
*VarListElementInit::resolveReferences(Resolver
&R
) const {
1577 Init
*NewTI
= TI
->resolveReferences(R
);
1578 if (ListInit
*List
= dyn_cast
<ListInit
>(NewTI
)) {
1579 // Leave out-of-bounds array references as-is. This can happen without
1580 // being an error, e.g. in the untaken "branch" of an !if expression.
1581 if (getElementNum() < List
->size())
1582 return List
->getElement(getElementNum());
1584 if (NewTI
!= TI
&& isa
<TypedInit
>(NewTI
))
1585 return VarListElementInit::get(cast
<TypedInit
>(NewTI
), getElementNum());
1586 return const_cast<VarListElementInit
*>(this);
1589 Init
*VarListElementInit::getBit(unsigned Bit
) const {
1590 if (getType() == BitRecTy::get())
1591 return const_cast<VarListElementInit
*>(this);
1592 return VarBitInit::get(const_cast<VarListElementInit
*>(this), Bit
);
1595 DefInit::DefInit(Record
*D
)
1596 : TypedInit(IK_DefInit
, D
->getType()), Def(D
) {}
1598 DefInit
*DefInit::get(Record
*R
) {
1599 return R
->getDefInit();
1602 Init
*DefInit::convertInitializerTo(RecTy
*Ty
) const {
1603 if (auto *RRT
= dyn_cast
<RecordRecTy
>(Ty
))
1604 if (getType()->typeIsConvertibleTo(RRT
))
1605 return const_cast<DefInit
*>(this);
1609 RecTy
*DefInit::getFieldType(StringInit
*FieldName
) const {
1610 if (const RecordVal
*RV
= Def
->getValue(FieldName
))
1611 return RV
->getType();
1615 std::string
DefInit::getAsString() const {
1616 return Def
->getName();
1619 static void ProfileVarDefInit(FoldingSetNodeID
&ID
,
1621 ArrayRef
<Init
*> Args
) {
1622 ID
.AddInteger(Args
.size());
1623 ID
.AddPointer(Class
);
1625 for (Init
*I
: Args
)
1629 VarDefInit
*VarDefInit::get(Record
*Class
, ArrayRef
<Init
*> Args
) {
1630 static FoldingSet
<VarDefInit
> ThePool
;
1632 FoldingSetNodeID ID
;
1633 ProfileVarDefInit(ID
, Class
, Args
);
1636 if (VarDefInit
*I
= ThePool
.FindNodeOrInsertPos(ID
, IP
))
1639 void *Mem
= Allocator
.Allocate(totalSizeToAlloc
<Init
*>(Args
.size()),
1640 alignof(VarDefInit
));
1641 VarDefInit
*I
= new(Mem
) VarDefInit(Class
, Args
.size());
1642 std::uninitialized_copy(Args
.begin(), Args
.end(),
1643 I
->getTrailingObjects
<Init
*>());
1644 ThePool
.InsertNode(I
, IP
);
1648 void VarDefInit::Profile(FoldingSetNodeID
&ID
) const {
1649 ProfileVarDefInit(ID
, Class
, args());
1652 DefInit
*VarDefInit::instantiate() {
1654 RecordKeeper
&Records
= Class
->getRecords();
1655 auto NewRecOwner
= std::make_unique
<Record
>(Records
.getNewAnonymousName(),
1656 Class
->getLoc(), Records
,
1657 /*IsAnonymous=*/true);
1658 Record
*NewRec
= NewRecOwner
.get();
1660 // Copy values from class to instance
1661 for (const RecordVal
&Val
: Class
->getValues())
1662 NewRec
->addValue(Val
);
1664 // Substitute and resolve template arguments
1665 ArrayRef
<Init
*> TArgs
= Class
->getTemplateArgs();
1666 MapResolver
R(NewRec
);
1668 for (unsigned i
= 0, e
= TArgs
.size(); i
!= e
; ++i
) {
1669 if (i
< args_size())
1670 R
.set(TArgs
[i
], getArg(i
));
1672 R
.set(TArgs
[i
], NewRec
->getValue(TArgs
[i
])->getValue());
1674 NewRec
->removeValue(TArgs
[i
]);
1677 NewRec
->resolveReferences(R
);
1679 // Add superclasses.
1680 ArrayRef
<std::pair
<Record
*, SMRange
>> SCs
= Class
->getSuperClasses();
1681 for (const auto &SCPair
: SCs
)
1682 NewRec
->addSuperClass(SCPair
.first
, SCPair
.second
);
1684 NewRec
->addSuperClass(Class
,
1685 SMRange(Class
->getLoc().back(),
1686 Class
->getLoc().back()));
1688 // Resolve internal references and store in record keeper
1689 NewRec
->resolveReferences();
1690 Records
.addDef(std::move(NewRecOwner
));
1692 Def
= DefInit::get(NewRec
);
1698 Init
*VarDefInit::resolveReferences(Resolver
&R
) const {
1699 TrackUnresolvedResolver
UR(&R
);
1700 bool Changed
= false;
1701 SmallVector
<Init
*, 8> NewArgs
;
1702 NewArgs
.reserve(args_size());
1704 for (Init
*Arg
: args()) {
1705 Init
*NewArg
= Arg
->resolveReferences(UR
);
1706 NewArgs
.push_back(NewArg
);
1707 Changed
|= NewArg
!= Arg
;
1711 auto New
= VarDefInit::get(Class
, NewArgs
);
1712 if (!UR
.foundUnresolved())
1713 return New
->instantiate();
1716 return const_cast<VarDefInit
*>(this);
1719 Init
*VarDefInit::Fold() const {
1723 TrackUnresolvedResolver R
;
1724 for (Init
*Arg
: args())
1725 Arg
->resolveReferences(R
);
1727 if (!R
.foundUnresolved())
1728 return const_cast<VarDefInit
*>(this)->instantiate();
1729 return const_cast<VarDefInit
*>(this);
1732 std::string
VarDefInit::getAsString() const {
1733 std::string Result
= Class
->getNameInitAsString() + "<";
1734 const char *sep
= "";
1735 for (Init
*Arg
: args()) {
1738 Result
+= Arg
->getAsString();
1740 return Result
+ ">";
1743 FieldInit
*FieldInit::get(Init
*R
, StringInit
*FN
) {
1744 using Key
= std::pair
<Init
*, StringInit
*>;
1745 static DenseMap
<Key
, FieldInit
*> ThePool
;
1747 Key
TheKey(std::make_pair(R
, FN
));
1749 FieldInit
*&I
= ThePool
[TheKey
];
1750 if (!I
) I
= new(Allocator
) FieldInit(R
, FN
);
1754 Init
*FieldInit::getBit(unsigned Bit
) const {
1755 if (getType() == BitRecTy::get())
1756 return const_cast<FieldInit
*>(this);
1757 return VarBitInit::get(const_cast<FieldInit
*>(this), Bit
);
1760 Init
*FieldInit::resolveReferences(Resolver
&R
) const {
1761 Init
*NewRec
= Rec
->resolveReferences(R
);
1763 return FieldInit::get(NewRec
, FieldName
)->Fold(R
.getCurrentRecord());
1764 return const_cast<FieldInit
*>(this);
1767 Init
*FieldInit::Fold(Record
*CurRec
) const {
1768 if (DefInit
*DI
= dyn_cast
<DefInit
>(Rec
)) {
1769 Record
*Def
= DI
->getDef();
1771 PrintFatalError(CurRec
->getLoc(),
1772 Twine("Attempting to access field '") +
1773 FieldName
->getAsUnquotedString() + "' of '" +
1774 Rec
->getAsString() + "' is a forbidden self-reference");
1775 Init
*FieldVal
= Def
->getValue(FieldName
)->getValue();
1776 if (FieldVal
->isComplete())
1779 return const_cast<FieldInit
*>(this);
1782 static void ProfileCondOpInit(FoldingSetNodeID
&ID
,
1783 ArrayRef
<Init
*> CondRange
,
1784 ArrayRef
<Init
*> ValRange
,
1785 const RecTy
*ValType
) {
1786 assert(CondRange
.size() == ValRange
.size() &&
1787 "Number of conditions and values must match!");
1788 ID
.AddPointer(ValType
);
1789 ArrayRef
<Init
*>::iterator Case
= CondRange
.begin();
1790 ArrayRef
<Init
*>::iterator Val
= ValRange
.begin();
1792 while (Case
!= CondRange
.end()) {
1793 ID
.AddPointer(*Case
++);
1794 ID
.AddPointer(*Val
++);
1798 void CondOpInit::Profile(FoldingSetNodeID
&ID
) const {
1799 ProfileCondOpInit(ID
,
1800 makeArrayRef(getTrailingObjects
<Init
*>(), NumConds
),
1801 makeArrayRef(getTrailingObjects
<Init
*>() + NumConds
, NumConds
),
1806 CondOpInit::get(ArrayRef
<Init
*> CondRange
,
1807 ArrayRef
<Init
*> ValRange
, RecTy
*Ty
) {
1808 assert(CondRange
.size() == ValRange
.size() &&
1809 "Number of conditions and values must match!");
1811 static FoldingSet
<CondOpInit
> ThePool
;
1812 FoldingSetNodeID ID
;
1813 ProfileCondOpInit(ID
, CondRange
, ValRange
, Ty
);
1816 if (CondOpInit
*I
= ThePool
.FindNodeOrInsertPos(ID
, IP
))
1819 void *Mem
= Allocator
.Allocate(totalSizeToAlloc
<Init
*>(2*CondRange
.size()),
1821 CondOpInit
*I
= new(Mem
) CondOpInit(CondRange
.size(), Ty
);
1823 std::uninitialized_copy(CondRange
.begin(), CondRange
.end(),
1824 I
->getTrailingObjects
<Init
*>());
1825 std::uninitialized_copy(ValRange
.begin(), ValRange
.end(),
1826 I
->getTrailingObjects
<Init
*>()+CondRange
.size());
1827 ThePool
.InsertNode(I
, IP
);
1831 Init
*CondOpInit::resolveReferences(Resolver
&R
) const {
1832 SmallVector
<Init
*, 4> NewConds
;
1833 bool Changed
= false;
1834 for (const Init
*Case
: getConds()) {
1835 Init
*NewCase
= Case
->resolveReferences(R
);
1836 NewConds
.push_back(NewCase
);
1837 Changed
|= NewCase
!= Case
;
1840 SmallVector
<Init
*, 4> NewVals
;
1841 for (const Init
*Val
: getVals()) {
1842 Init
*NewVal
= Val
->resolveReferences(R
);
1843 NewVals
.push_back(NewVal
);
1844 Changed
|= NewVal
!= Val
;
1848 return (CondOpInit::get(NewConds
, NewVals
,
1849 getValType()))->Fold(R
.getCurrentRecord());
1851 return const_cast<CondOpInit
*>(this);
1854 Init
*CondOpInit::Fold(Record
*CurRec
) const {
1855 for ( unsigned i
= 0; i
< NumConds
; ++i
) {
1856 Init
*Cond
= getCond(i
);
1857 Init
*Val
= getVal(i
);
1859 if (IntInit
*CondI
= dyn_cast_or_null
<IntInit
>(
1860 Cond
->convertInitializerTo(IntRecTy::get()))) {
1861 if (CondI
->getValue())
1862 return Val
->convertInitializerTo(getValType());
1864 return const_cast<CondOpInit
*>(this);
1867 PrintFatalError(CurRec
->getLoc(),
1869 " does not have any true condition in:" +
1870 this->getAsString());
1874 bool CondOpInit::isConcrete() const {
1875 for (const Init
*Case
: getConds())
1876 if (!Case
->isConcrete())
1879 for (const Init
*Val
: getVals())
1880 if (!Val
->isConcrete())
1886 bool CondOpInit::isComplete() const {
1887 for (const Init
*Case
: getConds())
1888 if (!Case
->isComplete())
1891 for (const Init
*Val
: getVals())
1892 if (!Val
->isConcrete())
1898 std::string
CondOpInit::getAsString() const {
1899 std::string Result
= "!cond(";
1900 for (unsigned i
= 0; i
< getNumConds(); i
++) {
1901 Result
+= getCond(i
)->getAsString() + ": ";
1902 Result
+= getVal(i
)->getAsString();
1903 if (i
!= getNumConds()-1)
1906 return Result
+ ")";
1909 Init
*CondOpInit::getBit(unsigned Bit
) const {
1910 return VarBitInit::get(const_cast<CondOpInit
*>(this), Bit
);
1913 static void ProfileDagInit(FoldingSetNodeID
&ID
, Init
*V
, StringInit
*VN
,
1914 ArrayRef
<Init
*> ArgRange
,
1915 ArrayRef
<StringInit
*> NameRange
) {
1919 ArrayRef
<Init
*>::iterator Arg
= ArgRange
.begin();
1920 ArrayRef
<StringInit
*>::iterator Name
= NameRange
.begin();
1921 while (Arg
!= ArgRange
.end()) {
1922 assert(Name
!= NameRange
.end() && "Arg name underflow!");
1923 ID
.AddPointer(*Arg
++);
1924 ID
.AddPointer(*Name
++);
1926 assert(Name
== NameRange
.end() && "Arg name overflow!");
1930 DagInit::get(Init
*V
, StringInit
*VN
, ArrayRef
<Init
*> ArgRange
,
1931 ArrayRef
<StringInit
*> NameRange
) {
1932 static FoldingSet
<DagInit
> ThePool
;
1934 FoldingSetNodeID ID
;
1935 ProfileDagInit(ID
, V
, VN
, ArgRange
, NameRange
);
1938 if (DagInit
*I
= ThePool
.FindNodeOrInsertPos(ID
, IP
))
1941 void *Mem
= Allocator
.Allocate(totalSizeToAlloc
<Init
*, StringInit
*>(ArgRange
.size(), NameRange
.size()), alignof(BitsInit
));
1942 DagInit
*I
= new(Mem
) DagInit(V
, VN
, ArgRange
.size(), NameRange
.size());
1943 std::uninitialized_copy(ArgRange
.begin(), ArgRange
.end(),
1944 I
->getTrailingObjects
<Init
*>());
1945 std::uninitialized_copy(NameRange
.begin(), NameRange
.end(),
1946 I
->getTrailingObjects
<StringInit
*>());
1947 ThePool
.InsertNode(I
, IP
);
1952 DagInit::get(Init
*V
, StringInit
*VN
,
1953 ArrayRef
<std::pair
<Init
*, StringInit
*>> args
) {
1954 SmallVector
<Init
*, 8> Args
;
1955 SmallVector
<StringInit
*, 8> Names
;
1957 for (const auto &Arg
: args
) {
1958 Args
.push_back(Arg
.first
);
1959 Names
.push_back(Arg
.second
);
1962 return DagInit::get(V
, VN
, Args
, Names
);
1965 void DagInit::Profile(FoldingSetNodeID
&ID
) const {
1966 ProfileDagInit(ID
, Val
, ValName
, makeArrayRef(getTrailingObjects
<Init
*>(), NumArgs
), makeArrayRef(getTrailingObjects
<StringInit
*>(), NumArgNames
));
1969 Record
*DagInit::getOperatorAsDef(ArrayRef
<SMLoc
> Loc
) const {
1970 if (DefInit
*DefI
= dyn_cast
<DefInit
>(Val
))
1971 return DefI
->getDef();
1972 PrintFatalError(Loc
, "Expected record as operator");
1976 Init
*DagInit::resolveReferences(Resolver
&R
) const {
1977 SmallVector
<Init
*, 8> NewArgs
;
1978 NewArgs
.reserve(arg_size());
1979 bool ArgsChanged
= false;
1980 for (const Init
*Arg
: getArgs()) {
1981 Init
*NewArg
= Arg
->resolveReferences(R
);
1982 NewArgs
.push_back(NewArg
);
1983 ArgsChanged
|= NewArg
!= Arg
;
1986 Init
*Op
= Val
->resolveReferences(R
);
1987 if (Op
!= Val
|| ArgsChanged
)
1988 return DagInit::get(Op
, ValName
, NewArgs
, getArgNames());
1990 return const_cast<DagInit
*>(this);
1993 bool DagInit::isConcrete() const {
1994 if (!Val
->isConcrete())
1996 for (const Init
*Elt
: getArgs()) {
1997 if (!Elt
->isConcrete())
2003 std::string
DagInit::getAsString() const {
2004 std::string Result
= "(" + Val
->getAsString();
2006 Result
+= ":" + ValName
->getAsUnquotedString();
2008 Result
+= " " + getArg(0)->getAsString();
2009 if (getArgName(0)) Result
+= ":$" + getArgName(0)->getAsUnquotedString();
2010 for (unsigned i
= 1, e
= getNumArgs(); i
!= e
; ++i
) {
2011 Result
+= ", " + getArg(i
)->getAsString();
2012 if (getArgName(i
)) Result
+= ":$" + getArgName(i
)->getAsUnquotedString();
2015 return Result
+ ")";
2018 //===----------------------------------------------------------------------===//
2019 // Other implementations
2020 //===----------------------------------------------------------------------===//
2022 RecordVal::RecordVal(Init
*N
, RecTy
*T
, bool P
)
2023 : Name(N
), TyAndPrefix(T
, P
) {
2024 setValue(UnsetInit::get());
2025 assert(Value
&& "Cannot create unset value for current type!");
2028 StringRef
RecordVal::getName() const {
2029 return cast
<StringInit
>(getNameInit())->getValue();
2032 bool RecordVal::setValue(Init
*V
) {
2034 Value
= V
->getCastTo(getType());
2036 assert(!isa
<TypedInit
>(Value
) ||
2037 cast
<TypedInit
>(Value
)->getType()->typeIsA(getType()));
2038 if (BitsRecTy
*BTy
= dyn_cast
<BitsRecTy
>(getType())) {
2039 if (!isa
<BitsInit
>(Value
)) {
2040 SmallVector
<Init
*, 64> Bits
;
2041 Bits
.reserve(BTy
->getNumBits());
2042 for (unsigned i
= 0, e
= BTy
->getNumBits(); i
< e
; ++i
)
2043 Bits
.push_back(Value
->getBit(i
));
2044 Value
= BitsInit::get(Bits
);
2048 return Value
== nullptr;
2054 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2055 LLVM_DUMP_METHOD
void RecordVal::dump() const { errs() << *this; }
2058 void RecordVal::print(raw_ostream
&OS
, bool PrintSem
) const {
2059 if (getPrefix()) OS
<< "field ";
2060 OS
<< *getType() << " " << getNameInitAsString();
2063 OS
<< " = " << *getValue();
2065 if (PrintSem
) OS
<< ";\n";
2068 unsigned Record::LastID
= 0;
2070 void Record::checkName() {
2071 // Ensure the record name has string type.
2072 const TypedInit
*TypedName
= cast
<const TypedInit
>(Name
);
2073 if (!isa
<StringRecTy
>(TypedName
->getType()))
2074 PrintFatalError(getLoc(), Twine("Record name '") + Name
->getAsString() +
2075 "' is not a string!");
2078 RecordRecTy
*Record::getType() {
2079 SmallVector
<Record
*, 4> DirectSCs
;
2080 getDirectSuperClasses(DirectSCs
);
2081 return RecordRecTy::get(DirectSCs
);
2084 DefInit
*Record::getDefInit() {
2086 TheInit
= new(Allocator
) DefInit(this);
2090 void Record::setName(Init
*NewName
) {
2093 // DO NOT resolve record values to the name at this point because
2094 // there might be default values for arguments of this def. Those
2095 // arguments might not have been resolved yet so we don't want to
2096 // prematurely assume values for those arguments were not passed to
2099 // Nonetheless, it may be that some of this Record's values
2100 // reference the record name. Indeed, the reason for having the
2101 // record name be an Init is to provide this flexibility. The extra
2102 // resolve steps after completely instantiating defs takes care of
2103 // this. See TGParser::ParseDef and TGParser::ParseDefm.
2106 void Record::getDirectSuperClasses(SmallVectorImpl
<Record
*> &Classes
) const {
2107 ArrayRef
<std::pair
<Record
*, SMRange
>> SCs
= getSuperClasses();
2108 while (!SCs
.empty()) {
2109 // Superclasses are in reverse preorder, so 'back' is a direct superclass,
2110 // and its transitive superclasses are directly preceding it.
2111 Record
*SC
= SCs
.back().first
;
2112 SCs
= SCs
.drop_back(1 + SC
->getSuperClasses().size());
2113 Classes
.push_back(SC
);
2117 void Record::resolveReferences(Resolver
&R
, const RecordVal
*SkipVal
) {
2118 for (RecordVal
&Value
: Values
) {
2119 if (SkipVal
== &Value
) // Skip resolve the same field as the given one
2121 if (Init
*V
= Value
.getValue()) {
2122 Init
*VR
= V
->resolveReferences(R
);
2123 if (Value
.setValue(VR
)) {
2125 if (TypedInit
*VRT
= dyn_cast
<TypedInit
>(VR
))
2127 (Twine("of type '") + VRT
->getType()->getAsString() + "' ").str();
2128 PrintFatalError(getLoc(), Twine("Invalid value ") + Type
+
2129 "is found when setting '" +
2130 Value
.getNameInitAsString() +
2132 Value
.getType()->getAsString() +
2133 "' after resolving references: " +
2134 VR
->getAsUnquotedString() + "\n");
2138 Init
*OldName
= getNameInit();
2139 Init
*NewName
= Name
->resolveReferences(R
);
2140 if (NewName
!= OldName
) {
2141 // Re-register with RecordKeeper.
2146 void Record::resolveReferences() {
2147 RecordResolver
R(*this);
2149 resolveReferences(R
);
2152 void Record::resolveReferencesTo(const RecordVal
*RV
) {
2153 RecordValResolver
R(*this, RV
);
2154 resolveReferences(R
, RV
);
2157 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2158 LLVM_DUMP_METHOD
void Record::dump() const { errs() << *this; }
2161 raw_ostream
&llvm::operator<<(raw_ostream
&OS
, const Record
&R
) {
2162 OS
<< R
.getNameInitAsString();
2164 ArrayRef
<Init
*> TArgs
= R
.getTemplateArgs();
2165 if (!TArgs
.empty()) {
2167 bool NeedComma
= false;
2168 for (const Init
*TA
: TArgs
) {
2169 if (NeedComma
) OS
<< ", ";
2171 const RecordVal
*RV
= R
.getValue(TA
);
2172 assert(RV
&& "Template argument record not found??");
2173 RV
->print(OS
, false);
2179 ArrayRef
<std::pair
<Record
*, SMRange
>> SC
= R
.getSuperClasses();
2182 for (const auto &SuperPair
: SC
)
2183 OS
<< " " << SuperPair
.first
->getNameInitAsString();
2187 for (const RecordVal
&Val
: R
.getValues())
2188 if (Val
.getPrefix() && !R
.isTemplateArg(Val
.getNameInit()))
2190 for (const RecordVal
&Val
: R
.getValues())
2191 if (!Val
.getPrefix() && !R
.isTemplateArg(Val
.getNameInit()))
2197 Init
*Record::getValueInit(StringRef FieldName
) const {
2198 const RecordVal
*R
= getValue(FieldName
);
2199 if (!R
|| !R
->getValue())
2200 PrintFatalError(getLoc(), "Record `" + getName() +
2201 "' does not have a field named `" + FieldName
+ "'!\n");
2202 return R
->getValue();
2205 StringRef
Record::getValueAsString(StringRef FieldName
) const {
2206 const RecordVal
*R
= getValue(FieldName
);
2207 if (!R
|| !R
->getValue())
2208 PrintFatalError(getLoc(), "Record `" + getName() +
2209 "' does not have a field named `" + FieldName
+ "'!\n");
2211 if (StringInit
*SI
= dyn_cast
<StringInit
>(R
->getValue()))
2212 return SI
->getValue();
2213 if (CodeInit
*CI
= dyn_cast
<CodeInit
>(R
->getValue()))
2214 return CI
->getValue();
2216 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2217 FieldName
+ "' does not have a string initializer!");
2220 BitsInit
*Record::getValueAsBitsInit(StringRef FieldName
) const {
2221 const RecordVal
*R
= getValue(FieldName
);
2222 if (!R
|| !R
->getValue())
2223 PrintFatalError(getLoc(), "Record `" + getName() +
2224 "' does not have a field named `" + FieldName
+ "'!\n");
2226 if (BitsInit
*BI
= dyn_cast
<BitsInit
>(R
->getValue()))
2228 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2229 FieldName
+ "' does not have a BitsInit initializer!");
2232 ListInit
*Record::getValueAsListInit(StringRef FieldName
) const {
2233 const RecordVal
*R
= getValue(FieldName
);
2234 if (!R
|| !R
->getValue())
2235 PrintFatalError(getLoc(), "Record `" + getName() +
2236 "' does not have a field named `" + FieldName
+ "'!\n");
2238 if (ListInit
*LI
= dyn_cast
<ListInit
>(R
->getValue()))
2240 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2241 FieldName
+ "' does not have a list initializer!");
2244 std::vector
<Record
*>
2245 Record::getValueAsListOfDefs(StringRef FieldName
) const {
2246 ListInit
*List
= getValueAsListInit(FieldName
);
2247 std::vector
<Record
*> Defs
;
2248 for (Init
*I
: List
->getValues()) {
2249 if (DefInit
*DI
= dyn_cast
<DefInit
>(I
))
2250 Defs
.push_back(DI
->getDef());
2252 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2253 FieldName
+ "' list is not entirely DefInit!");
2258 int64_t Record::getValueAsInt(StringRef FieldName
) const {
2259 const RecordVal
*R
= getValue(FieldName
);
2260 if (!R
|| !R
->getValue())
2261 PrintFatalError(getLoc(), "Record `" + getName() +
2262 "' does not have a field named `" + FieldName
+ "'!\n");
2264 if (IntInit
*II
= dyn_cast
<IntInit
>(R
->getValue()))
2265 return II
->getValue();
2266 PrintFatalError(getLoc(), Twine("Record `") + getName() + "', field `" +
2268 "' does not have an int initializer: " +
2269 R
->getValue()->getAsString());
2272 std::vector
<int64_t>
2273 Record::getValueAsListOfInts(StringRef FieldName
) const {
2274 ListInit
*List
= getValueAsListInit(FieldName
);
2275 std::vector
<int64_t> Ints
;
2276 for (Init
*I
: List
->getValues()) {
2277 if (IntInit
*II
= dyn_cast
<IntInit
>(I
))
2278 Ints
.push_back(II
->getValue());
2280 PrintFatalError(getLoc(),
2281 Twine("Record `") + getName() + "', field `" + FieldName
+
2282 "' does not have a list of ints initializer: " +
2288 std::vector
<StringRef
>
2289 Record::getValueAsListOfStrings(StringRef FieldName
) const {
2290 ListInit
*List
= getValueAsListInit(FieldName
);
2291 std::vector
<StringRef
> Strings
;
2292 for (Init
*I
: List
->getValues()) {
2293 if (StringInit
*SI
= dyn_cast
<StringInit
>(I
))
2294 Strings
.push_back(SI
->getValue());
2296 PrintFatalError(getLoc(),
2297 Twine("Record `") + getName() + "', field `" + FieldName
+
2298 "' does not have a list of strings initializer: " +
2304 Record
*Record::getValueAsDef(StringRef FieldName
) const {
2305 const RecordVal
*R
= getValue(FieldName
);
2306 if (!R
|| !R
->getValue())
2307 PrintFatalError(getLoc(), "Record `" + getName() +
2308 "' does not have a field named `" + FieldName
+ "'!\n");
2310 if (DefInit
*DI
= dyn_cast
<DefInit
>(R
->getValue()))
2311 return DI
->getDef();
2312 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2313 FieldName
+ "' does not have a def initializer!");
2316 Record
*Record::getValueAsOptionalDef(StringRef FieldName
) const {
2317 const RecordVal
*R
= getValue(FieldName
);
2318 if (!R
|| !R
->getValue())
2319 PrintFatalError(getLoc(), "Record `" + getName() +
2320 "' does not have a field named `" + FieldName
+ "'!\n");
2322 if (DefInit
*DI
= dyn_cast
<DefInit
>(R
->getValue()))
2323 return DI
->getDef();
2324 if (isa
<UnsetInit
>(R
->getValue()))
2326 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2327 FieldName
+ "' does not have either a def initializer or '?'!");
2331 bool Record::getValueAsBit(StringRef FieldName
) const {
2332 const RecordVal
*R
= getValue(FieldName
);
2333 if (!R
|| !R
->getValue())
2334 PrintFatalError(getLoc(), "Record `" + getName() +
2335 "' does not have a field named `" + FieldName
+ "'!\n");
2337 if (BitInit
*BI
= dyn_cast
<BitInit
>(R
->getValue()))
2338 return BI
->getValue();
2339 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2340 FieldName
+ "' does not have a bit initializer!");
2343 bool Record::getValueAsBitOrUnset(StringRef FieldName
, bool &Unset
) const {
2344 const RecordVal
*R
= getValue(FieldName
);
2345 if (!R
|| !R
->getValue())
2346 PrintFatalError(getLoc(), "Record `" + getName() +
2347 "' does not have a field named `" + FieldName
.str() + "'!\n");
2349 if (isa
<UnsetInit
>(R
->getValue())) {
2354 if (BitInit
*BI
= dyn_cast
<BitInit
>(R
->getValue()))
2355 return BI
->getValue();
2356 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2357 FieldName
+ "' does not have a bit initializer!");
2360 DagInit
*Record::getValueAsDag(StringRef FieldName
) const {
2361 const RecordVal
*R
= getValue(FieldName
);
2362 if (!R
|| !R
->getValue())
2363 PrintFatalError(getLoc(), "Record `" + getName() +
2364 "' does not have a field named `" + FieldName
+ "'!\n");
2366 if (DagInit
*DI
= dyn_cast
<DagInit
>(R
->getValue()))
2368 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2369 FieldName
+ "' does not have a dag initializer!");
2372 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2373 LLVM_DUMP_METHOD
void RecordKeeper::dump() const { errs() << *this; }
2376 raw_ostream
&llvm::operator<<(raw_ostream
&OS
, const RecordKeeper
&RK
) {
2377 OS
<< "------------- Classes -----------------\n";
2378 for (const auto &C
: RK
.getClasses())
2379 OS
<< "class " << *C
.second
;
2381 OS
<< "------------- Defs -----------------\n";
2382 for (const auto &D
: RK
.getDefs())
2383 OS
<< "def " << *D
.second
;
2387 /// GetNewAnonymousName - Generate a unique anonymous name that can be used as
2389 Init
*RecordKeeper::getNewAnonymousName() {
2390 return StringInit::get("anonymous_" + utostr(AnonCounter
++));
2393 std::vector
<Record
*>
2394 RecordKeeper::getAllDerivedDefinitions(StringRef ClassName
) const {
2395 Record
*Class
= getClass(ClassName
);
2397 PrintFatalError("ERROR: Couldn't find the `" + ClassName
+ "' class!\n");
2399 std::vector
<Record
*> Defs
;
2400 for (const auto &D
: getDefs())
2401 if (D
.second
->isSubClassOf(Class
))
2402 Defs
.push_back(D
.second
.get());
2407 Init
*MapResolver::resolve(Init
*VarName
) {
2408 auto It
= Map
.find(VarName
);
2409 if (It
== Map
.end())
2412 Init
*I
= It
->second
.V
;
2414 if (!It
->second
.Resolved
&& Map
.size() > 1) {
2415 // Resolve mutual references among the mapped variables, but prevent
2416 // infinite recursion.
2418 I
= I
->resolveReferences(*this);
2419 Map
[VarName
] = {I
, true};
2425 Init
*RecordResolver::resolve(Init
*VarName
) {
2426 Init
*Val
= Cache
.lookup(VarName
);
2430 for (Init
*S
: Stack
) {
2432 return nullptr; // prevent infinite recursion
2435 if (RecordVal
*RV
= getCurrentRecord()->getValue(VarName
)) {
2436 if (!isa
<UnsetInit
>(RV
->getValue())) {
2437 Val
= RV
->getValue();
2438 Stack
.push_back(VarName
);
2439 Val
= Val
->resolveReferences(*this);
2444 Cache
[VarName
] = Val
;
2448 Init
*TrackUnresolvedResolver::resolve(Init
*VarName
) {
2452 I
= R
->resolve(VarName
);
2453 if (I
&& !FoundUnresolved
) {
2454 // Do not recurse into the resolved initializer, as that would change
2455 // the behavior of the resolver we're delegating, but do check to see
2456 // if there are unresolved variables remaining.
2457 TrackUnresolvedResolver Sub
;
2458 I
->resolveReferences(Sub
);
2459 FoundUnresolved
|= Sub
.FoundUnresolved
;
2464 FoundUnresolved
= true;
2468 Init
*HasReferenceResolver::resolve(Init
*VarName
)
2470 if (VarName
== VarNameToTrack
)