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/StringExtras.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/Config/llvm-config.h"
22 #include "llvm/Support/Allocator.h"
23 #include "llvm/Support/Casting.h"
24 #include "llvm/Support/Compiler.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/SMLoc.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include "llvm/TableGen/Error.h"
29 #include "llvm/TableGen/Record.h"
39 static BumpPtrAllocator Allocator
;
41 //===----------------------------------------------------------------------===//
42 // Type implementations
43 //===----------------------------------------------------------------------===//
45 BitRecTy
BitRecTy::Shared
;
46 CodeRecTy
CodeRecTy::Shared
;
47 IntRecTy
IntRecTy::Shared
;
48 StringRecTy
StringRecTy::Shared
;
49 DagRecTy
DagRecTy::Shared
;
51 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
52 LLVM_DUMP_METHOD
void RecTy::dump() const { print(errs()); }
55 ListRecTy
*RecTy::getListTy() {
57 ListTy
= new(Allocator
) ListRecTy(this);
61 bool RecTy::typeIsConvertibleTo(const RecTy
*RHS
) const {
62 assert(RHS
&& "NULL pointer");
63 return Kind
== RHS
->getRecTyKind();
66 bool RecTy::typeIsA(const RecTy
*RHS
) const { return this == RHS
; }
68 bool BitRecTy::typeIsConvertibleTo(const RecTy
*RHS
) const{
69 if (RecTy::typeIsConvertibleTo(RHS
) || RHS
->getRecTyKind() == IntRecTyKind
)
71 if (const BitsRecTy
*BitsTy
= dyn_cast
<BitsRecTy
>(RHS
))
72 return BitsTy
->getNumBits() == 1;
76 BitsRecTy
*BitsRecTy::get(unsigned Sz
) {
77 static std::vector
<BitsRecTy
*> Shared
;
78 if (Sz
>= Shared
.size())
79 Shared
.resize(Sz
+ 1);
80 BitsRecTy
*&Ty
= Shared
[Sz
];
82 Ty
= new(Allocator
) BitsRecTy(Sz
);
86 std::string
BitsRecTy::getAsString() const {
87 return "bits<" + utostr(Size
) + ">";
90 bool BitsRecTy::typeIsConvertibleTo(const RecTy
*RHS
) const {
91 if (RecTy::typeIsConvertibleTo(RHS
)) //argument and the sender are same type
92 return cast
<BitsRecTy
>(RHS
)->Size
== Size
;
93 RecTyKind kind
= RHS
->getRecTyKind();
94 return (kind
== BitRecTyKind
&& Size
== 1) || (kind
== IntRecTyKind
);
97 bool BitsRecTy::typeIsA(const RecTy
*RHS
) const {
98 if (const BitsRecTy
*RHSb
= dyn_cast
<BitsRecTy
>(RHS
))
99 return RHSb
->Size
== Size
;
103 bool IntRecTy::typeIsConvertibleTo(const RecTy
*RHS
) const {
104 RecTyKind kind
= RHS
->getRecTyKind();
105 return kind
==BitRecTyKind
|| kind
==BitsRecTyKind
|| kind
==IntRecTyKind
;
108 bool CodeRecTy::typeIsConvertibleTo(const RecTy
*RHS
) const {
109 RecTyKind Kind
= RHS
->getRecTyKind();
110 return Kind
== CodeRecTyKind
|| Kind
== StringRecTyKind
;
113 std::string
StringRecTy::getAsString() const {
117 bool StringRecTy::typeIsConvertibleTo(const RecTy
*RHS
) const {
118 RecTyKind Kind
= RHS
->getRecTyKind();
119 return Kind
== StringRecTyKind
|| Kind
== CodeRecTyKind
;
122 std::string
ListRecTy::getAsString() const {
123 return "list<" + Ty
->getAsString() + ">";
126 bool ListRecTy::typeIsConvertibleTo(const RecTy
*RHS
) const {
127 if (const auto *ListTy
= dyn_cast
<ListRecTy
>(RHS
))
128 return Ty
->typeIsConvertibleTo(ListTy
->getElementType());
132 bool ListRecTy::typeIsA(const RecTy
*RHS
) const {
133 if (const ListRecTy
*RHSl
= dyn_cast
<ListRecTy
>(RHS
))
134 return getElementType()->typeIsA(RHSl
->getElementType());
138 std::string
DagRecTy::getAsString() const {
142 static void ProfileRecordRecTy(FoldingSetNodeID
&ID
,
143 ArrayRef
<Record
*> Classes
) {
144 ID
.AddInteger(Classes
.size());
145 for (Record
*R
: Classes
)
149 RecordRecTy
*RecordRecTy::get(ArrayRef
<Record
*> UnsortedClasses
) {
150 if (UnsortedClasses
.empty()) {
151 static RecordRecTy
AnyRecord(0);
155 FoldingSet
<RecordRecTy
> &ThePool
=
156 UnsortedClasses
[0]->getRecords().RecordTypePool
;
158 SmallVector
<Record
*, 4> Classes(UnsortedClasses
.begin(),
159 UnsortedClasses
.end());
160 llvm::sort(Classes
, [](Record
*LHS
, Record
*RHS
) {
161 return LHS
->getNameInitAsString() < RHS
->getNameInitAsString();
165 ProfileRecordRecTy(ID
, Classes
);
168 if (RecordRecTy
*Ty
= ThePool
.FindNodeOrInsertPos(ID
, IP
))
172 // Check for redundancy.
173 for (unsigned i
= 0; i
< Classes
.size(); ++i
) {
174 for (unsigned j
= 0; j
< Classes
.size(); ++j
) {
175 assert(i
== j
|| !Classes
[i
]->isSubClassOf(Classes
[j
]));
177 assert(&Classes
[0]->getRecords() == &Classes
[i
]->getRecords());
181 void *Mem
= Allocator
.Allocate(totalSizeToAlloc
<Record
*>(Classes
.size()),
182 alignof(RecordRecTy
));
183 RecordRecTy
*Ty
= new(Mem
) RecordRecTy(Classes
.size());
184 std::uninitialized_copy(Classes
.begin(), Classes
.end(),
185 Ty
->getTrailingObjects
<Record
*>());
186 ThePool
.InsertNode(Ty
, IP
);
190 void RecordRecTy::Profile(FoldingSetNodeID
&ID
) const {
191 ProfileRecordRecTy(ID
, getClasses());
194 std::string
RecordRecTy::getAsString() const {
196 return getClasses()[0]->getNameInitAsString();
198 std::string Str
= "{";
200 for (Record
*R
: getClasses()) {
204 Str
+= R
->getNameInitAsString();
210 bool RecordRecTy::isSubClassOf(Record
*Class
) const {
211 return llvm::any_of(getClasses(), [Class
](Record
*MySuperClass
) {
212 return MySuperClass
== Class
||
213 MySuperClass
->isSubClassOf(Class
);
217 bool RecordRecTy::typeIsConvertibleTo(const RecTy
*RHS
) const {
221 const RecordRecTy
*RTy
= dyn_cast
<RecordRecTy
>(RHS
);
225 return llvm::all_of(RTy
->getClasses(), [this](Record
*TargetClass
) {
226 return isSubClassOf(TargetClass
);
230 bool RecordRecTy::typeIsA(const RecTy
*RHS
) const {
231 return typeIsConvertibleTo(RHS
);
234 static RecordRecTy
*resolveRecordTypes(RecordRecTy
*T1
, RecordRecTy
*T2
) {
235 SmallVector
<Record
*, 4> CommonSuperClasses
;
236 SmallVector
<Record
*, 4> Stack
;
238 Stack
.insert(Stack
.end(), T1
->classes_begin(), T1
->classes_end());
240 while (!Stack
.empty()) {
241 Record
*R
= Stack
.back();
244 if (T2
->isSubClassOf(R
)) {
245 CommonSuperClasses
.push_back(R
);
247 R
->getDirectSuperClasses(Stack
);
251 return RecordRecTy::get(CommonSuperClasses
);
254 RecTy
*llvm::resolveTypes(RecTy
*T1
, RecTy
*T2
) {
258 if (RecordRecTy
*RecTy1
= dyn_cast
<RecordRecTy
>(T1
)) {
259 if (RecordRecTy
*RecTy2
= dyn_cast
<RecordRecTy
>(T2
))
260 return resolveRecordTypes(RecTy1
, RecTy2
);
263 if (T1
->typeIsConvertibleTo(T2
))
265 if (T2
->typeIsConvertibleTo(T1
))
268 if (ListRecTy
*ListTy1
= dyn_cast
<ListRecTy
>(T1
)) {
269 if (ListRecTy
*ListTy2
= dyn_cast
<ListRecTy
>(T2
)) {
270 RecTy
* NewType
= resolveTypes(ListTy1
->getElementType(),
271 ListTy2
->getElementType());
273 return NewType
->getListTy();
280 //===----------------------------------------------------------------------===//
281 // Initializer implementations
282 //===----------------------------------------------------------------------===//
284 void Init::anchor() {}
286 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
287 LLVM_DUMP_METHOD
void Init::dump() const { return print(errs()); }
290 UnsetInit
*UnsetInit::get() {
291 static UnsetInit TheInit
;
295 Init
*UnsetInit::getCastTo(RecTy
*Ty
) const {
296 return const_cast<UnsetInit
*>(this);
299 Init
*UnsetInit::convertInitializerTo(RecTy
*Ty
) const {
300 return const_cast<UnsetInit
*>(this);
303 BitInit
*BitInit::get(bool V
) {
304 static BitInit
True(true);
305 static BitInit
False(false);
307 return V
? &True
: &False
;
310 Init
*BitInit::convertInitializerTo(RecTy
*Ty
) const {
311 if (isa
<BitRecTy
>(Ty
))
312 return const_cast<BitInit
*>(this);
314 if (isa
<IntRecTy
>(Ty
))
315 return IntInit::get(getValue());
317 if (auto *BRT
= dyn_cast
<BitsRecTy
>(Ty
)) {
318 // Can only convert single bit.
319 if (BRT
->getNumBits() == 1)
320 return BitsInit::get(const_cast<BitInit
*>(this));
327 ProfileBitsInit(FoldingSetNodeID
&ID
, ArrayRef
<Init
*> Range
) {
328 ID
.AddInteger(Range
.size());
330 for (Init
*I
: Range
)
334 BitsInit
*BitsInit::get(ArrayRef
<Init
*> Range
) {
335 static FoldingSet
<BitsInit
> ThePool
;
338 ProfileBitsInit(ID
, Range
);
341 if (BitsInit
*I
= ThePool
.FindNodeOrInsertPos(ID
, IP
))
344 void *Mem
= Allocator
.Allocate(totalSizeToAlloc
<Init
*>(Range
.size()),
346 BitsInit
*I
= new(Mem
) BitsInit(Range
.size());
347 std::uninitialized_copy(Range
.begin(), Range
.end(),
348 I
->getTrailingObjects
<Init
*>());
349 ThePool
.InsertNode(I
, IP
);
353 void BitsInit::Profile(FoldingSetNodeID
&ID
) const {
354 ProfileBitsInit(ID
, makeArrayRef(getTrailingObjects
<Init
*>(), NumBits
));
357 Init
*BitsInit::convertInitializerTo(RecTy
*Ty
) const {
358 if (isa
<BitRecTy
>(Ty
)) {
359 if (getNumBits() != 1) return nullptr; // Only accept if just one bit!
363 if (auto *BRT
= dyn_cast
<BitsRecTy
>(Ty
)) {
364 // If the number of bits is right, return it. Otherwise we need to expand
366 if (getNumBits() != BRT
->getNumBits()) return nullptr;
367 return const_cast<BitsInit
*>(this);
370 if (isa
<IntRecTy
>(Ty
)) {
372 for (unsigned i
= 0, e
= getNumBits(); i
!= e
; ++i
)
373 if (auto *Bit
= dyn_cast
<BitInit
>(getBit(i
)))
374 Result
|= static_cast<int64_t>(Bit
->getValue()) << i
;
377 return IntInit::get(Result
);
384 BitsInit::convertInitializerBitRange(ArrayRef
<unsigned> Bits
) const {
385 SmallVector
<Init
*, 16> NewBits(Bits
.size());
387 for (unsigned i
= 0, e
= Bits
.size(); i
!= e
; ++i
) {
388 if (Bits
[i
] >= getNumBits())
390 NewBits
[i
] = getBit(Bits
[i
]);
392 return BitsInit::get(NewBits
);
395 bool BitsInit::isConcrete() const {
396 for (unsigned i
= 0, e
= getNumBits(); i
!= e
; ++i
) {
397 if (!getBit(i
)->isConcrete())
403 std::string
BitsInit::getAsString() const {
404 std::string Result
= "{ ";
405 for (unsigned i
= 0, e
= getNumBits(); i
!= e
; ++i
) {
406 if (i
) Result
+= ", ";
407 if (Init
*Bit
= getBit(e
-i
-1))
408 Result
+= Bit
->getAsString();
412 return Result
+ " }";
415 // resolveReferences - If there are any field references that refer to fields
416 // that have been filled in, we can propagate the values now.
417 Init
*BitsInit::resolveReferences(Resolver
&R
) const {
418 bool Changed
= false;
419 SmallVector
<Init
*, 16> NewBits(getNumBits());
421 Init
*CachedBitVarRef
= nullptr;
422 Init
*CachedBitVarResolved
= nullptr;
424 for (unsigned i
= 0, e
= getNumBits(); i
!= e
; ++i
) {
425 Init
*CurBit
= getBit(i
);
426 Init
*NewBit
= CurBit
;
428 if (VarBitInit
*CurBitVar
= dyn_cast
<VarBitInit
>(CurBit
)) {
429 if (CurBitVar
->getBitVar() != CachedBitVarRef
) {
430 CachedBitVarRef
= CurBitVar
->getBitVar();
431 CachedBitVarResolved
= CachedBitVarRef
->resolveReferences(R
);
434 NewBit
= CachedBitVarResolved
->getBit(CurBitVar
->getBitNum());
436 // getBit(0) implicitly converts int and bits<1> values to bit.
437 NewBit
= CurBit
->resolveReferences(R
)->getBit(0);
440 if (isa
<UnsetInit
>(NewBit
) && R
.keepUnsetBits())
443 Changed
|= CurBit
!= NewBit
;
447 return BitsInit::get(NewBits
);
449 return const_cast<BitsInit
*>(this);
452 IntInit
*IntInit::get(int64_t V
) {
453 static DenseMap
<int64_t, IntInit
*> ThePool
;
455 IntInit
*&I
= ThePool
[V
];
456 if (!I
) I
= new(Allocator
) IntInit(V
);
460 std::string
IntInit::getAsString() const {
461 return itostr(Value
);
464 static bool canFitInBitfield(int64_t Value
, unsigned NumBits
) {
465 // For example, with NumBits == 4, we permit Values from [-7 .. 15].
466 return (NumBits
>= sizeof(Value
) * 8) ||
467 (Value
>> NumBits
== 0) || (Value
>> (NumBits
-1) == -1);
470 Init
*IntInit::convertInitializerTo(RecTy
*Ty
) const {
471 if (isa
<IntRecTy
>(Ty
))
472 return const_cast<IntInit
*>(this);
474 if (isa
<BitRecTy
>(Ty
)) {
475 int64_t Val
= getValue();
476 if (Val
!= 0 && Val
!= 1) return nullptr; // Only accept 0 or 1 for a bit!
477 return BitInit::get(Val
!= 0);
480 if (auto *BRT
= dyn_cast
<BitsRecTy
>(Ty
)) {
481 int64_t Value
= getValue();
482 // Make sure this bitfield is large enough to hold the integer value.
483 if (!canFitInBitfield(Value
, BRT
->getNumBits()))
486 SmallVector
<Init
*, 16> NewBits(BRT
->getNumBits());
487 for (unsigned i
= 0; i
!= BRT
->getNumBits(); ++i
)
488 NewBits
[i
] = BitInit::get(Value
& ((i
< 64) ? (1LL << i
) : 0));
490 return BitsInit::get(NewBits
);
497 IntInit::convertInitializerBitRange(ArrayRef
<unsigned> Bits
) const {
498 SmallVector
<Init
*, 16> NewBits(Bits
.size());
500 for (unsigned i
= 0, e
= Bits
.size(); i
!= e
; ++i
) {
504 NewBits
[i
] = BitInit::get(Value
& (INT64_C(1) << Bits
[i
]));
506 return BitsInit::get(NewBits
);
509 CodeInit
*CodeInit::get(StringRef V
) {
510 static StringMap
<CodeInit
*, BumpPtrAllocator
&> ThePool(Allocator
);
512 auto &Entry
= *ThePool
.insert(std::make_pair(V
, nullptr)).first
;
514 Entry
.second
= new(Allocator
) CodeInit(Entry
.getKey());
518 StringInit
*StringInit::get(StringRef V
) {
519 static StringMap
<StringInit
*, BumpPtrAllocator
&> ThePool(Allocator
);
521 auto &Entry
= *ThePool
.insert(std::make_pair(V
, nullptr)).first
;
523 Entry
.second
= new(Allocator
) StringInit(Entry
.getKey());
527 Init
*StringInit::convertInitializerTo(RecTy
*Ty
) const {
528 if (isa
<StringRecTy
>(Ty
))
529 return const_cast<StringInit
*>(this);
530 if (isa
<CodeRecTy
>(Ty
))
531 return CodeInit::get(getValue());
536 Init
*CodeInit::convertInitializerTo(RecTy
*Ty
) const {
537 if (isa
<CodeRecTy
>(Ty
))
538 return const_cast<CodeInit
*>(this);
539 if (isa
<StringRecTy
>(Ty
))
540 return StringInit::get(getValue());
545 static void ProfileListInit(FoldingSetNodeID
&ID
,
546 ArrayRef
<Init
*> Range
,
548 ID
.AddInteger(Range
.size());
549 ID
.AddPointer(EltTy
);
551 for (Init
*I
: Range
)
555 ListInit
*ListInit::get(ArrayRef
<Init
*> Range
, RecTy
*EltTy
) {
556 static FoldingSet
<ListInit
> ThePool
;
559 ProfileListInit(ID
, Range
, EltTy
);
562 if (ListInit
*I
= ThePool
.FindNodeOrInsertPos(ID
, IP
))
565 assert(Range
.empty() || !isa
<TypedInit
>(Range
[0]) ||
566 cast
<TypedInit
>(Range
[0])->getType()->typeIsConvertibleTo(EltTy
));
568 void *Mem
= Allocator
.Allocate(totalSizeToAlloc
<Init
*>(Range
.size()),
570 ListInit
*I
= new(Mem
) ListInit(Range
.size(), EltTy
);
571 std::uninitialized_copy(Range
.begin(), Range
.end(),
572 I
->getTrailingObjects
<Init
*>());
573 ThePool
.InsertNode(I
, IP
);
577 void ListInit::Profile(FoldingSetNodeID
&ID
) const {
578 RecTy
*EltTy
= cast
<ListRecTy
>(getType())->getElementType();
580 ProfileListInit(ID
, getValues(), EltTy
);
583 Init
*ListInit::convertInitializerTo(RecTy
*Ty
) const {
585 return const_cast<ListInit
*>(this);
587 if (auto *LRT
= dyn_cast
<ListRecTy
>(Ty
)) {
588 SmallVector
<Init
*, 8> Elements
;
589 Elements
.reserve(getValues().size());
591 // Verify that all of the elements of the list are subclasses of the
592 // appropriate class!
593 bool Changed
= false;
594 RecTy
*ElementType
= LRT
->getElementType();
595 for (Init
*I
: getValues())
596 if (Init
*CI
= I
->convertInitializerTo(ElementType
)) {
597 Elements
.push_back(CI
);
604 return const_cast<ListInit
*>(this);
605 return ListInit::get(Elements
, ElementType
);
611 Init
*ListInit::convertInitListSlice(ArrayRef
<unsigned> Elements
) const {
612 SmallVector
<Init
*, 8> Vals
;
613 Vals
.reserve(Elements
.size());
614 for (unsigned Element
: Elements
) {
615 if (Element
>= size())
617 Vals
.push_back(getElement(Element
));
619 return ListInit::get(Vals
, getElementType());
622 Record
*ListInit::getElementAsRecord(unsigned i
) const {
623 assert(i
< NumValues
&& "List element index out of range!");
624 DefInit
*DI
= dyn_cast
<DefInit
>(getElement(i
));
626 PrintFatalError("Expected record in list!");
630 Init
*ListInit::resolveReferences(Resolver
&R
) const {
631 SmallVector
<Init
*, 8> Resolved
;
632 Resolved
.reserve(size());
633 bool Changed
= false;
635 for (Init
*CurElt
: getValues()) {
636 Init
*E
= CurElt
->resolveReferences(R
);
637 Changed
|= E
!= CurElt
;
638 Resolved
.push_back(E
);
642 return ListInit::get(Resolved
, getElementType());
643 return const_cast<ListInit
*>(this);
646 bool ListInit::isConcrete() const {
647 for (Init
*Element
: *this) {
648 if (!Element
->isConcrete())
654 std::string
ListInit::getAsString() const {
655 std::string Result
= "[";
656 const char *sep
= "";
657 for (Init
*Element
: *this) {
660 Result
+= Element
->getAsString();
665 Init
*OpInit::getBit(unsigned Bit
) const {
666 if (getType() == BitRecTy::get())
667 return const_cast<OpInit
*>(this);
668 return VarBitInit::get(const_cast<OpInit
*>(this), Bit
);
672 ProfileUnOpInit(FoldingSetNodeID
&ID
, unsigned Opcode
, Init
*Op
, RecTy
*Type
) {
673 ID
.AddInteger(Opcode
);
678 UnOpInit
*UnOpInit::get(UnaryOp Opc
, Init
*LHS
, RecTy
*Type
) {
679 static FoldingSet
<UnOpInit
> ThePool
;
682 ProfileUnOpInit(ID
, Opc
, LHS
, Type
);
685 if (UnOpInit
*I
= ThePool
.FindNodeOrInsertPos(ID
, IP
))
688 UnOpInit
*I
= new(Allocator
) UnOpInit(Opc
, LHS
, Type
);
689 ThePool
.InsertNode(I
, IP
);
693 void UnOpInit::Profile(FoldingSetNodeID
&ID
) const {
694 ProfileUnOpInit(ID
, getOpcode(), getOperand(), getType());
697 Init
*UnOpInit::Fold(Record
*CurRec
, bool IsFinal
) const {
698 switch (getOpcode()) {
700 if (isa
<StringRecTy
>(getType())) {
701 if (StringInit
*LHSs
= dyn_cast
<StringInit
>(LHS
))
704 if (DefInit
*LHSd
= dyn_cast
<DefInit
>(LHS
))
705 return StringInit::get(LHSd
->getAsString());
707 if (IntInit
*LHSi
= dyn_cast
<IntInit
>(LHS
))
708 return StringInit::get(LHSi
->getAsString());
709 } else if (isa
<RecordRecTy
>(getType())) {
710 if (StringInit
*Name
= dyn_cast
<StringInit
>(LHS
)) {
711 if (!CurRec
&& !IsFinal
)
713 assert(CurRec
&& "NULL pointer");
716 // Self-references are allowed, but their resolution is delayed until
717 // the final resolve to ensure that we get the correct type for them.
718 if (Name
== CurRec
->getNameInit()) {
723 D
= CurRec
->getRecords().getDef(Name
->getValue());
726 PrintFatalError(CurRec
->getLoc(),
727 Twine("Undefined reference to record: '") +
728 Name
->getValue() + "'\n");
733 DefInit
*DI
= DefInit::get(D
);
734 if (!DI
->getType()->typeIsA(getType())) {
735 PrintFatalError(CurRec
->getLoc(),
736 Twine("Expected type '") +
737 getType()->getAsString() + "', got '" +
738 DI
->getType()->getAsString() + "' in: " +
739 getAsString() + "\n");
745 if (Init
*NewInit
= LHS
->convertInitializerTo(getType()))
750 if (ListInit
*LHSl
= dyn_cast
<ListInit
>(LHS
)) {
751 assert(!LHSl
->empty() && "Empty list in head");
752 return LHSl
->getElement(0);
757 if (ListInit
*LHSl
= dyn_cast
<ListInit
>(LHS
)) {
758 assert(!LHSl
->empty() && "Empty list in tail");
759 // Note the +1. We can't just pass the result of getValues()
761 return ListInit::get(LHSl
->getValues().slice(1), LHSl
->getElementType());
766 if (ListInit
*LHSl
= dyn_cast
<ListInit
>(LHS
))
767 return IntInit::get(LHSl
->size());
771 if (ListInit
*LHSl
= dyn_cast
<ListInit
>(LHS
))
772 return IntInit::get(LHSl
->empty());
773 if (StringInit
*LHSs
= dyn_cast
<StringInit
>(LHS
))
774 return IntInit::get(LHSs
->getValue().empty());
777 return const_cast<UnOpInit
*>(this);
780 Init
*UnOpInit::resolveReferences(Resolver
&R
) const {
781 Init
*lhs
= LHS
->resolveReferences(R
);
783 if (LHS
!= lhs
|| (R
.isFinal() && getOpcode() == CAST
))
784 return (UnOpInit::get(getOpcode(), lhs
, getType()))
785 ->Fold(R
.getCurrentRecord(), R
.isFinal());
786 return const_cast<UnOpInit
*>(this);
789 std::string
UnOpInit::getAsString() const {
791 switch (getOpcode()) {
792 case CAST
: Result
= "!cast<" + getType()->getAsString() + ">"; break;
793 case HEAD
: Result
= "!head"; break;
794 case TAIL
: Result
= "!tail"; break;
795 case SIZE
: Result
= "!size"; break;
796 case EMPTY
: Result
= "!empty"; break;
798 return Result
+ "(" + LHS
->getAsString() + ")";
802 ProfileBinOpInit(FoldingSetNodeID
&ID
, unsigned Opcode
, Init
*LHS
, Init
*RHS
,
804 ID
.AddInteger(Opcode
);
810 BinOpInit
*BinOpInit::get(BinaryOp Opc
, Init
*LHS
,
811 Init
*RHS
, RecTy
*Type
) {
812 static FoldingSet
<BinOpInit
> ThePool
;
815 ProfileBinOpInit(ID
, Opc
, LHS
, RHS
, Type
);
818 if (BinOpInit
*I
= ThePool
.FindNodeOrInsertPos(ID
, IP
))
821 BinOpInit
*I
= new(Allocator
) BinOpInit(Opc
, LHS
, RHS
, Type
);
822 ThePool
.InsertNode(I
, IP
);
826 void BinOpInit::Profile(FoldingSetNodeID
&ID
) const {
827 ProfileBinOpInit(ID
, getOpcode(), getLHS(), getRHS(), getType());
830 static StringInit
*ConcatStringInits(const StringInit
*I0
,
831 const StringInit
*I1
) {
832 SmallString
<80> Concat(I0
->getValue());
833 Concat
.append(I1
->getValue());
834 return StringInit::get(Concat
);
837 Init
*BinOpInit::getStrConcat(Init
*I0
, Init
*I1
) {
838 // Shortcut for the common case of concatenating two strings.
839 if (const StringInit
*I0s
= dyn_cast
<StringInit
>(I0
))
840 if (const StringInit
*I1s
= dyn_cast
<StringInit
>(I1
))
841 return ConcatStringInits(I0s
, I1s
);
842 return BinOpInit::get(BinOpInit::STRCONCAT
, I0
, I1
, StringRecTy::get());
845 Init
*BinOpInit::Fold(Record
*CurRec
) const {
846 switch (getOpcode()) {
848 DagInit
*LHSs
= dyn_cast
<DagInit
>(LHS
);
849 DagInit
*RHSs
= dyn_cast
<DagInit
>(RHS
);
851 DefInit
*LOp
= dyn_cast
<DefInit
>(LHSs
->getOperator());
852 DefInit
*ROp
= dyn_cast
<DefInit
>(RHSs
->getOperator());
855 if (LOp
->getDef() != ROp
->getDef()) {
856 PrintFatalError(Twine("Concatenated Dag operators do not match: '") +
857 LHSs
->getAsString() + "' vs. '" + RHSs
->getAsString() +
860 SmallVector
<Init
*, 8> Args
;
861 SmallVector
<StringInit
*, 8> ArgNames
;
862 for (unsigned i
= 0, e
= LHSs
->getNumArgs(); i
!= e
; ++i
) {
863 Args
.push_back(LHSs
->getArg(i
));
864 ArgNames
.push_back(LHSs
->getArgName(i
));
866 for (unsigned i
= 0, e
= RHSs
->getNumArgs(); i
!= e
; ++i
) {
867 Args
.push_back(RHSs
->getArg(i
));
868 ArgNames
.push_back(RHSs
->getArgName(i
));
870 return DagInit::get(LHSs
->getOperator(), nullptr, Args
, ArgNames
);
875 ListInit
*LHSs
= dyn_cast
<ListInit
>(LHS
);
876 ListInit
*RHSs
= dyn_cast
<ListInit
>(RHS
);
878 SmallVector
<Init
*, 8> Args
;
879 Args
.insert(Args
.end(), LHSs
->begin(), LHSs
->end());
880 Args
.insert(Args
.end(), RHSs
->begin(), RHSs
->end());
881 return ListInit::get(Args
, LHSs
->getElementType());
886 StringInit
*LHSs
= dyn_cast
<StringInit
>(LHS
);
887 StringInit
*RHSs
= dyn_cast
<StringInit
>(RHS
);
889 return ConcatStringInits(LHSs
, RHSs
);
898 // try to fold eq comparison for 'bit' and 'int', otherwise fallback
899 // to string objects.
901 dyn_cast_or_null
<IntInit
>(LHS
->convertInitializerTo(IntRecTy::get()));
903 dyn_cast_or_null
<IntInit
>(RHS
->convertInitializerTo(IntRecTy::get()));
907 switch (getOpcode()) {
908 case EQ
: Result
= L
->getValue() == R
->getValue(); break;
909 case NE
: Result
= L
->getValue() != R
->getValue(); break;
910 case LE
: Result
= L
->getValue() <= R
->getValue(); break;
911 case LT
: Result
= L
->getValue() < R
->getValue(); break;
912 case GE
: Result
= L
->getValue() >= R
->getValue(); break;
913 case GT
: Result
= L
->getValue() > R
->getValue(); break;
914 default: llvm_unreachable("unhandled comparison");
916 return BitInit::get(Result
);
919 if (getOpcode() == EQ
|| getOpcode() == NE
) {
920 StringInit
*LHSs
= dyn_cast
<StringInit
>(LHS
);
921 StringInit
*RHSs
= dyn_cast
<StringInit
>(RHS
);
923 // Make sure we've resolved
925 bool Equal
= LHSs
->getValue() == RHSs
->getValue();
926 return BitInit::get(getOpcode() == EQ
? Equal
: !Equal
);
939 dyn_cast_or_null
<IntInit
>(LHS
->convertInitializerTo(IntRecTy::get()));
941 dyn_cast_or_null
<IntInit
>(RHS
->convertInitializerTo(IntRecTy::get()));
943 int64_t LHSv
= LHSi
->getValue(), RHSv
= RHSi
->getValue();
945 switch (getOpcode()) {
946 default: llvm_unreachable("Bad opcode!");
947 case ADD
: Result
= LHSv
+ RHSv
; break;
948 case AND
: Result
= LHSv
& RHSv
; break;
949 case OR
: Result
= LHSv
| RHSv
; break;
950 case SHL
: Result
= LHSv
<< RHSv
; break;
951 case SRA
: Result
= LHSv
>> RHSv
; break;
952 case SRL
: Result
= (uint64_t)LHSv
>> (uint64_t)RHSv
; break;
954 return IntInit::get(Result
);
959 return const_cast<BinOpInit
*>(this);
962 Init
*BinOpInit::resolveReferences(Resolver
&R
) const {
963 Init
*lhs
= LHS
->resolveReferences(R
);
964 Init
*rhs
= RHS
->resolveReferences(R
);
966 if (LHS
!= lhs
|| RHS
!= rhs
)
967 return (BinOpInit::get(getOpcode(), lhs
, rhs
, getType()))
968 ->Fold(R
.getCurrentRecord());
969 return const_cast<BinOpInit
*>(this);
972 std::string
BinOpInit::getAsString() const {
974 switch (getOpcode()) {
975 case CONCAT
: Result
= "!con"; break;
976 case ADD
: Result
= "!add"; break;
977 case AND
: Result
= "!and"; break;
978 case OR
: Result
= "!or"; break;
979 case SHL
: Result
= "!shl"; break;
980 case SRA
: Result
= "!sra"; break;
981 case SRL
: Result
= "!srl"; break;
982 case EQ
: Result
= "!eq"; break;
983 case NE
: Result
= "!ne"; break;
984 case LE
: Result
= "!le"; break;
985 case LT
: Result
= "!lt"; break;
986 case GE
: Result
= "!ge"; break;
987 case GT
: Result
= "!gt"; break;
988 case LISTCONCAT
: Result
= "!listconcat"; break;
989 case STRCONCAT
: Result
= "!strconcat"; break;
991 return Result
+ "(" + LHS
->getAsString() + ", " + RHS
->getAsString() + ")";
995 ProfileTernOpInit(FoldingSetNodeID
&ID
, unsigned Opcode
, Init
*LHS
, Init
*MHS
,
996 Init
*RHS
, RecTy
*Type
) {
997 ID
.AddInteger(Opcode
);
1001 ID
.AddPointer(Type
);
1004 TernOpInit
*TernOpInit::get(TernaryOp Opc
, Init
*LHS
, Init
*MHS
, Init
*RHS
,
1006 static FoldingSet
<TernOpInit
> ThePool
;
1008 FoldingSetNodeID ID
;
1009 ProfileTernOpInit(ID
, Opc
, LHS
, MHS
, RHS
, Type
);
1012 if (TernOpInit
*I
= ThePool
.FindNodeOrInsertPos(ID
, IP
))
1015 TernOpInit
*I
= new(Allocator
) TernOpInit(Opc
, LHS
, MHS
, RHS
, Type
);
1016 ThePool
.InsertNode(I
, IP
);
1020 void TernOpInit::Profile(FoldingSetNodeID
&ID
) const {
1021 ProfileTernOpInit(ID
, getOpcode(), getLHS(), getMHS(), getRHS(), getType());
1024 static Init
*ForeachApply(Init
*LHS
, Init
*MHSe
, Init
*RHS
, Record
*CurRec
) {
1025 MapResolver
R(CurRec
);
1027 return RHS
->resolveReferences(R
);
1030 static Init
*ForeachDagApply(Init
*LHS
, DagInit
*MHSd
, Init
*RHS
,
1032 bool Change
= false;
1033 Init
*Val
= ForeachApply(LHS
, MHSd
->getOperator(), RHS
, CurRec
);
1034 if (Val
!= MHSd
->getOperator())
1037 SmallVector
<std::pair
<Init
*, StringInit
*>, 8> NewArgs
;
1038 for (unsigned int i
= 0; i
< MHSd
->getNumArgs(); ++i
) {
1039 Init
*Arg
= MHSd
->getArg(i
);
1041 StringInit
*ArgName
= MHSd
->getArgName(i
);
1043 if (DagInit
*Argd
= dyn_cast
<DagInit
>(Arg
))
1044 NewArg
= ForeachDagApply(LHS
, Argd
, RHS
, CurRec
);
1046 NewArg
= ForeachApply(LHS
, Arg
, RHS
, CurRec
);
1048 NewArgs
.push_back(std::make_pair(NewArg
, ArgName
));
1054 return DagInit::get(Val
, nullptr, NewArgs
);
1058 // Applies RHS to all elements of MHS, using LHS as a temp variable.
1059 static Init
*ForeachHelper(Init
*LHS
, Init
*MHS
, Init
*RHS
, RecTy
*Type
,
1061 if (DagInit
*MHSd
= dyn_cast
<DagInit
>(MHS
))
1062 return ForeachDagApply(LHS
, MHSd
, RHS
, CurRec
);
1064 if (ListInit
*MHSl
= dyn_cast
<ListInit
>(MHS
)) {
1065 SmallVector
<Init
*, 8> NewList(MHSl
->begin(), MHSl
->end());
1067 for (Init
*&Item
: NewList
) {
1068 Init
*NewItem
= ForeachApply(LHS
, Item
, RHS
, CurRec
);
1069 if (NewItem
!= Item
)
1072 return ListInit::get(NewList
, cast
<ListRecTy
>(Type
)->getElementType());
1078 Init
*TernOpInit::Fold(Record
*CurRec
) const {
1079 switch (getOpcode()) {
1081 DefInit
*LHSd
= dyn_cast
<DefInit
>(LHS
);
1082 VarInit
*LHSv
= dyn_cast
<VarInit
>(LHS
);
1083 StringInit
*LHSs
= dyn_cast
<StringInit
>(LHS
);
1085 DefInit
*MHSd
= dyn_cast
<DefInit
>(MHS
);
1086 VarInit
*MHSv
= dyn_cast
<VarInit
>(MHS
);
1087 StringInit
*MHSs
= dyn_cast
<StringInit
>(MHS
);
1089 DefInit
*RHSd
= dyn_cast
<DefInit
>(RHS
);
1090 VarInit
*RHSv
= dyn_cast
<VarInit
>(RHS
);
1091 StringInit
*RHSs
= dyn_cast
<StringInit
>(RHS
);
1093 if (LHSd
&& MHSd
&& RHSd
) {
1094 Record
*Val
= RHSd
->getDef();
1095 if (LHSd
->getAsString() == RHSd
->getAsString())
1096 Val
= MHSd
->getDef();
1097 return DefInit::get(Val
);
1099 if (LHSv
&& MHSv
&& RHSv
) {
1100 std::string Val
= RHSv
->getName();
1101 if (LHSv
->getAsString() == RHSv
->getAsString())
1102 Val
= MHSv
->getName();
1103 return VarInit::get(Val
, getType());
1105 if (LHSs
&& MHSs
&& RHSs
) {
1106 std::string Val
= RHSs
->getValue();
1108 std::string::size_type found
;
1109 std::string::size_type idx
= 0;
1111 found
= Val
.find(LHSs
->getValue(), idx
);
1112 if (found
== std::string::npos
)
1114 Val
.replace(found
, LHSs
->getValue().size(), MHSs
->getValue());
1115 idx
= found
+ MHSs
->getValue().size();
1118 return StringInit::get(Val
);
1124 if (Init
*Result
= ForeachHelper(LHS
, MHS
, RHS
, getType(), CurRec
))
1130 if (IntInit
*LHSi
= dyn_cast_or_null
<IntInit
>(
1131 LHS
->convertInitializerTo(IntRecTy::get()))) {
1132 if (LHSi
->getValue())
1140 ListInit
*MHSl
= dyn_cast
<ListInit
>(MHS
);
1141 ListInit
*RHSl
= dyn_cast
<ListInit
>(RHS
);
1142 bool MHSok
= MHSl
|| isa
<UnsetInit
>(MHS
);
1143 bool RHSok
= RHSl
|| isa
<UnsetInit
>(RHS
);
1145 if (isa
<UnsetInit
>(MHS
) && isa
<UnsetInit
>(RHS
))
1146 break; // Typically prevented by the parser, but might happen with template args
1148 if (MHSok
&& RHSok
&& (!MHSl
|| !RHSl
|| MHSl
->size() == RHSl
->size())) {
1149 SmallVector
<std::pair
<Init
*, StringInit
*>, 8> Children
;
1150 unsigned Size
= MHSl
? MHSl
->size() : RHSl
->size();
1151 for (unsigned i
= 0; i
!= Size
; ++i
) {
1152 Init
*Node
= MHSl
? MHSl
->getElement(i
) : UnsetInit::get();
1153 Init
*Name
= RHSl
? RHSl
->getElement(i
) : UnsetInit::get();
1154 if (!isa
<StringInit
>(Name
) && !isa
<UnsetInit
>(Name
))
1155 return const_cast<TernOpInit
*>(this);
1156 Children
.emplace_back(Node
, dyn_cast
<StringInit
>(Name
));
1158 return DagInit::get(LHS
, nullptr, Children
);
1164 return const_cast<TernOpInit
*>(this);
1167 Init
*TernOpInit::resolveReferences(Resolver
&R
) const {
1168 Init
*lhs
= LHS
->resolveReferences(R
);
1170 if (getOpcode() == IF
&& lhs
!= LHS
) {
1171 if (IntInit
*Value
= dyn_cast_or_null
<IntInit
>(
1172 lhs
->convertInitializerTo(IntRecTy::get()))) {
1174 if (Value
->getValue())
1175 return MHS
->resolveReferences(R
);
1176 return RHS
->resolveReferences(R
);
1180 Init
*mhs
= MHS
->resolveReferences(R
);
1183 if (getOpcode() == FOREACH
) {
1184 ShadowResolver
SR(R
);
1186 rhs
= RHS
->resolveReferences(SR
);
1188 rhs
= RHS
->resolveReferences(R
);
1191 if (LHS
!= lhs
|| MHS
!= mhs
|| RHS
!= rhs
)
1192 return (TernOpInit::get(getOpcode(), lhs
, mhs
, rhs
, getType()))
1193 ->Fold(R
.getCurrentRecord());
1194 return const_cast<TernOpInit
*>(this);
1197 std::string
TernOpInit::getAsString() const {
1199 bool UnquotedLHS
= false;
1200 switch (getOpcode()) {
1201 case SUBST
: Result
= "!subst"; break;
1202 case FOREACH
: Result
= "!foreach"; UnquotedLHS
= true; break;
1203 case IF
: Result
= "!if"; break;
1204 case DAG
: Result
= "!dag"; break;
1206 return (Result
+ "(" +
1207 (UnquotedLHS
? LHS
->getAsUnquotedString() : LHS
->getAsString()) +
1208 ", " + MHS
->getAsString() + ", " + RHS
->getAsString() + ")");
1211 static void ProfileFoldOpInit(FoldingSetNodeID
&ID
, Init
*A
, Init
*B
,
1212 Init
*Start
, Init
*List
, Init
*Expr
,
1214 ID
.AddPointer(Start
);
1215 ID
.AddPointer(List
);
1218 ID
.AddPointer(Expr
);
1219 ID
.AddPointer(Type
);
1222 FoldOpInit
*FoldOpInit::get(Init
*Start
, Init
*List
, Init
*A
, Init
*B
,
1223 Init
*Expr
, RecTy
*Type
) {
1224 static FoldingSet
<FoldOpInit
> ThePool
;
1226 FoldingSetNodeID ID
;
1227 ProfileFoldOpInit(ID
, Start
, List
, A
, B
, Expr
, Type
);
1230 if (FoldOpInit
*I
= ThePool
.FindNodeOrInsertPos(ID
, IP
))
1233 FoldOpInit
*I
= new (Allocator
) FoldOpInit(Start
, List
, A
, B
, Expr
, Type
);
1234 ThePool
.InsertNode(I
, IP
);
1238 void FoldOpInit::Profile(FoldingSetNodeID
&ID
) const {
1239 ProfileFoldOpInit(ID
, Start
, List
, A
, B
, Expr
, getType());
1242 Init
*FoldOpInit::Fold(Record
*CurRec
) const {
1243 if (ListInit
*LI
= dyn_cast
<ListInit
>(List
)) {
1244 Init
*Accum
= Start
;
1245 for (Init
*Elt
: *LI
) {
1246 MapResolver
R(CurRec
);
1249 Accum
= Expr
->resolveReferences(R
);
1253 return const_cast<FoldOpInit
*>(this);
1256 Init
*FoldOpInit::resolveReferences(Resolver
&R
) const {
1257 Init
*NewStart
= Start
->resolveReferences(R
);
1258 Init
*NewList
= List
->resolveReferences(R
);
1259 ShadowResolver
SR(R
);
1262 Init
*NewExpr
= Expr
->resolveReferences(SR
);
1264 if (Start
== NewStart
&& List
== NewList
&& Expr
== NewExpr
)
1265 return const_cast<FoldOpInit
*>(this);
1267 return get(NewStart
, NewList
, A
, B
, NewExpr
, getType())
1268 ->Fold(R
.getCurrentRecord());
1271 Init
*FoldOpInit::getBit(unsigned Bit
) const {
1272 return VarBitInit::get(const_cast<FoldOpInit
*>(this), Bit
);
1275 std::string
FoldOpInit::getAsString() const {
1276 return (Twine("!foldl(") + Start
->getAsString() + ", " + List
->getAsString() +
1277 ", " + A
->getAsUnquotedString() + ", " + B
->getAsUnquotedString() +
1278 ", " + Expr
->getAsString() + ")")
1282 static void ProfileIsAOpInit(FoldingSetNodeID
&ID
, RecTy
*CheckType
,
1284 ID
.AddPointer(CheckType
);
1285 ID
.AddPointer(Expr
);
1288 IsAOpInit
*IsAOpInit::get(RecTy
*CheckType
, Init
*Expr
) {
1289 static FoldingSet
<IsAOpInit
> ThePool
;
1291 FoldingSetNodeID ID
;
1292 ProfileIsAOpInit(ID
, CheckType
, Expr
);
1295 if (IsAOpInit
*I
= ThePool
.FindNodeOrInsertPos(ID
, IP
))
1298 IsAOpInit
*I
= new (Allocator
) IsAOpInit(CheckType
, Expr
);
1299 ThePool
.InsertNode(I
, IP
);
1303 void IsAOpInit::Profile(FoldingSetNodeID
&ID
) const {
1304 ProfileIsAOpInit(ID
, CheckType
, Expr
);
1307 Init
*IsAOpInit::Fold() const {
1308 if (TypedInit
*TI
= dyn_cast
<TypedInit
>(Expr
)) {
1309 // Is the expression type known to be (a subclass of) the desired type?
1310 if (TI
->getType()->typeIsConvertibleTo(CheckType
))
1311 return IntInit::get(1);
1313 if (isa
<RecordRecTy
>(CheckType
)) {
1314 // If the target type is not a subclass of the expression type, or if
1315 // the expression has fully resolved to a record, we know that it can't
1316 // be of the required type.
1317 if (!CheckType
->typeIsConvertibleTo(TI
->getType()) || isa
<DefInit
>(Expr
))
1318 return IntInit::get(0);
1320 // We treat non-record types as not castable.
1321 return IntInit::get(0);
1324 return const_cast<IsAOpInit
*>(this);
1327 Init
*IsAOpInit::resolveReferences(Resolver
&R
) const {
1328 Init
*NewExpr
= Expr
->resolveReferences(R
);
1329 if (Expr
!= NewExpr
)
1330 return get(CheckType
, NewExpr
)->Fold();
1331 return const_cast<IsAOpInit
*>(this);
1334 Init
*IsAOpInit::getBit(unsigned Bit
) const {
1335 return VarBitInit::get(const_cast<IsAOpInit
*>(this), Bit
);
1338 std::string
IsAOpInit::getAsString() const {
1339 return (Twine("!isa<") + CheckType
->getAsString() + ">(" +
1340 Expr
->getAsString() + ")")
1344 RecTy
*TypedInit::getFieldType(StringInit
*FieldName
) const {
1345 if (RecordRecTy
*RecordType
= dyn_cast
<RecordRecTy
>(getType())) {
1346 for (Record
*Rec
: RecordType
->getClasses()) {
1347 if (RecordVal
*Field
= Rec
->getValue(FieldName
))
1348 return Field
->getType();
1355 TypedInit::convertInitializerTo(RecTy
*Ty
) const {
1356 if (getType() == Ty
|| getType()->typeIsA(Ty
))
1357 return const_cast<TypedInit
*>(this);
1359 if (isa
<BitRecTy
>(getType()) && isa
<BitsRecTy
>(Ty
) &&
1360 cast
<BitsRecTy
>(Ty
)->getNumBits() == 1)
1361 return BitsInit::get({const_cast<TypedInit
*>(this)});
1366 Init
*TypedInit::convertInitializerBitRange(ArrayRef
<unsigned> Bits
) const {
1367 BitsRecTy
*T
= dyn_cast
<BitsRecTy
>(getType());
1368 if (!T
) return nullptr; // Cannot subscript a non-bits variable.
1369 unsigned NumBits
= T
->getNumBits();
1371 SmallVector
<Init
*, 16> NewBits
;
1372 NewBits
.reserve(Bits
.size());
1373 for (unsigned Bit
: Bits
) {
1377 NewBits
.push_back(VarBitInit::get(const_cast<TypedInit
*>(this), Bit
));
1379 return BitsInit::get(NewBits
);
1382 Init
*TypedInit::getCastTo(RecTy
*Ty
) const {
1383 // Handle the common case quickly
1384 if (getType() == Ty
|| getType()->typeIsA(Ty
))
1385 return const_cast<TypedInit
*>(this);
1387 if (Init
*Converted
= convertInitializerTo(Ty
)) {
1388 assert(!isa
<TypedInit
>(Converted
) ||
1389 cast
<TypedInit
>(Converted
)->getType()->typeIsA(Ty
));
1393 if (!getType()->typeIsConvertibleTo(Ty
))
1396 return UnOpInit::get(UnOpInit::CAST
, const_cast<TypedInit
*>(this), Ty
)
1400 Init
*TypedInit::convertInitListSlice(ArrayRef
<unsigned> Elements
) const {
1401 ListRecTy
*T
= dyn_cast
<ListRecTy
>(getType());
1402 if (!T
) return nullptr; // Cannot subscript a non-list variable.
1404 if (Elements
.size() == 1)
1405 return VarListElementInit::get(const_cast<TypedInit
*>(this), Elements
[0]);
1407 SmallVector
<Init
*, 8> ListInits
;
1408 ListInits
.reserve(Elements
.size());
1409 for (unsigned Element
: Elements
)
1410 ListInits
.push_back(VarListElementInit::get(const_cast<TypedInit
*>(this),
1412 return ListInit::get(ListInits
, T
->getElementType());
1416 VarInit
*VarInit::get(StringRef VN
, RecTy
*T
) {
1417 Init
*Value
= StringInit::get(VN
);
1418 return VarInit::get(Value
, T
);
1421 VarInit
*VarInit::get(Init
*VN
, RecTy
*T
) {
1422 using Key
= std::pair
<RecTy
*, Init
*>;
1423 static DenseMap
<Key
, VarInit
*> ThePool
;
1425 Key
TheKey(std::make_pair(T
, VN
));
1427 VarInit
*&I
= ThePool
[TheKey
];
1429 I
= new(Allocator
) VarInit(VN
, T
);
1433 StringRef
VarInit::getName() const {
1434 StringInit
*NameString
= cast
<StringInit
>(getNameInit());
1435 return NameString
->getValue();
1438 Init
*VarInit::getBit(unsigned Bit
) const {
1439 if (getType() == BitRecTy::get())
1440 return const_cast<VarInit
*>(this);
1441 return VarBitInit::get(const_cast<VarInit
*>(this), Bit
);
1444 Init
*VarInit::resolveReferences(Resolver
&R
) const {
1445 if (Init
*Val
= R
.resolve(VarName
))
1447 return const_cast<VarInit
*>(this);
1450 VarBitInit
*VarBitInit::get(TypedInit
*T
, unsigned B
) {
1451 using Key
= std::pair
<TypedInit
*, unsigned>;
1452 static DenseMap
<Key
, VarBitInit
*> ThePool
;
1454 Key
TheKey(std::make_pair(T
, B
));
1456 VarBitInit
*&I
= ThePool
[TheKey
];
1458 I
= new(Allocator
) VarBitInit(T
, B
);
1462 std::string
VarBitInit::getAsString() const {
1463 return TI
->getAsString() + "{" + utostr(Bit
) + "}";
1466 Init
*VarBitInit::resolveReferences(Resolver
&R
) const {
1467 Init
*I
= TI
->resolveReferences(R
);
1469 return I
->getBit(getBitNum());
1471 return const_cast<VarBitInit
*>(this);
1474 VarListElementInit
*VarListElementInit::get(TypedInit
*T
,
1476 using Key
= std::pair
<TypedInit
*, unsigned>;
1477 static DenseMap
<Key
, VarListElementInit
*> ThePool
;
1479 Key
TheKey(std::make_pair(T
, E
));
1481 VarListElementInit
*&I
= ThePool
[TheKey
];
1482 if (!I
) I
= new(Allocator
) VarListElementInit(T
, E
);
1486 std::string
VarListElementInit::getAsString() const {
1487 return TI
->getAsString() + "[" + utostr(Element
) + "]";
1490 Init
*VarListElementInit::resolveReferences(Resolver
&R
) const {
1491 Init
*NewTI
= TI
->resolveReferences(R
);
1492 if (ListInit
*List
= dyn_cast
<ListInit
>(NewTI
)) {
1493 // Leave out-of-bounds array references as-is. This can happen without
1494 // being an error, e.g. in the untaken "branch" of an !if expression.
1495 if (getElementNum() < List
->size())
1496 return List
->getElement(getElementNum());
1498 if (NewTI
!= TI
&& isa
<TypedInit
>(NewTI
))
1499 return VarListElementInit::get(cast
<TypedInit
>(NewTI
), getElementNum());
1500 return const_cast<VarListElementInit
*>(this);
1503 Init
*VarListElementInit::getBit(unsigned Bit
) const {
1504 if (getType() == BitRecTy::get())
1505 return const_cast<VarListElementInit
*>(this);
1506 return VarBitInit::get(const_cast<VarListElementInit
*>(this), Bit
);
1509 DefInit::DefInit(Record
*D
)
1510 : TypedInit(IK_DefInit
, D
->getType()), Def(D
) {}
1512 DefInit
*DefInit::get(Record
*R
) {
1513 return R
->getDefInit();
1516 Init
*DefInit::convertInitializerTo(RecTy
*Ty
) const {
1517 if (auto *RRT
= dyn_cast
<RecordRecTy
>(Ty
))
1518 if (getType()->typeIsConvertibleTo(RRT
))
1519 return const_cast<DefInit
*>(this);
1523 RecTy
*DefInit::getFieldType(StringInit
*FieldName
) const {
1524 if (const RecordVal
*RV
= Def
->getValue(FieldName
))
1525 return RV
->getType();
1529 std::string
DefInit::getAsString() const {
1530 return Def
->getName();
1533 static void ProfileVarDefInit(FoldingSetNodeID
&ID
,
1535 ArrayRef
<Init
*> Args
) {
1536 ID
.AddInteger(Args
.size());
1537 ID
.AddPointer(Class
);
1539 for (Init
*I
: Args
)
1543 VarDefInit
*VarDefInit::get(Record
*Class
, ArrayRef
<Init
*> Args
) {
1544 static FoldingSet
<VarDefInit
> ThePool
;
1546 FoldingSetNodeID ID
;
1547 ProfileVarDefInit(ID
, Class
, Args
);
1550 if (VarDefInit
*I
= ThePool
.FindNodeOrInsertPos(ID
, IP
))
1553 void *Mem
= Allocator
.Allocate(totalSizeToAlloc
<Init
*>(Args
.size()),
1554 alignof(VarDefInit
));
1555 VarDefInit
*I
= new(Mem
) VarDefInit(Class
, Args
.size());
1556 std::uninitialized_copy(Args
.begin(), Args
.end(),
1557 I
->getTrailingObjects
<Init
*>());
1558 ThePool
.InsertNode(I
, IP
);
1562 void VarDefInit::Profile(FoldingSetNodeID
&ID
) const {
1563 ProfileVarDefInit(ID
, Class
, args());
1566 DefInit
*VarDefInit::instantiate() {
1568 RecordKeeper
&Records
= Class
->getRecords();
1569 auto NewRecOwner
= make_unique
<Record
>(Records
.getNewAnonymousName(),
1570 Class
->getLoc(), Records
,
1571 /*IsAnonymous=*/true);
1572 Record
*NewRec
= NewRecOwner
.get();
1574 // Copy values from class to instance
1575 for (const RecordVal
&Val
: Class
->getValues())
1576 NewRec
->addValue(Val
);
1578 // Substitute and resolve template arguments
1579 ArrayRef
<Init
*> TArgs
= Class
->getTemplateArgs();
1580 MapResolver
R(NewRec
);
1582 for (unsigned i
= 0, e
= TArgs
.size(); i
!= e
; ++i
) {
1583 if (i
< args_size())
1584 R
.set(TArgs
[i
], getArg(i
));
1586 R
.set(TArgs
[i
], NewRec
->getValue(TArgs
[i
])->getValue());
1588 NewRec
->removeValue(TArgs
[i
]);
1591 NewRec
->resolveReferences(R
);
1593 // Add superclasses.
1594 ArrayRef
<std::pair
<Record
*, SMRange
>> SCs
= Class
->getSuperClasses();
1595 for (const auto &SCPair
: SCs
)
1596 NewRec
->addSuperClass(SCPair
.first
, SCPair
.second
);
1598 NewRec
->addSuperClass(Class
,
1599 SMRange(Class
->getLoc().back(),
1600 Class
->getLoc().back()));
1602 // Resolve internal references and store in record keeper
1603 NewRec
->resolveReferences();
1604 Records
.addDef(std::move(NewRecOwner
));
1606 Def
= DefInit::get(NewRec
);
1612 Init
*VarDefInit::resolveReferences(Resolver
&R
) const {
1613 TrackUnresolvedResolver
UR(&R
);
1614 bool Changed
= false;
1615 SmallVector
<Init
*, 8> NewArgs
;
1616 NewArgs
.reserve(args_size());
1618 for (Init
*Arg
: args()) {
1619 Init
*NewArg
= Arg
->resolveReferences(UR
);
1620 NewArgs
.push_back(NewArg
);
1621 Changed
|= NewArg
!= Arg
;
1625 auto New
= VarDefInit::get(Class
, NewArgs
);
1626 if (!UR
.foundUnresolved())
1627 return New
->instantiate();
1630 return const_cast<VarDefInit
*>(this);
1633 Init
*VarDefInit::Fold() const {
1637 TrackUnresolvedResolver R
;
1638 for (Init
*Arg
: args())
1639 Arg
->resolveReferences(R
);
1641 if (!R
.foundUnresolved())
1642 return const_cast<VarDefInit
*>(this)->instantiate();
1643 return const_cast<VarDefInit
*>(this);
1646 std::string
VarDefInit::getAsString() const {
1647 std::string Result
= Class
->getNameInitAsString() + "<";
1648 const char *sep
= "";
1649 for (Init
*Arg
: args()) {
1652 Result
+= Arg
->getAsString();
1654 return Result
+ ">";
1657 FieldInit
*FieldInit::get(Init
*R
, StringInit
*FN
) {
1658 using Key
= std::pair
<Init
*, StringInit
*>;
1659 static DenseMap
<Key
, FieldInit
*> ThePool
;
1661 Key
TheKey(std::make_pair(R
, FN
));
1663 FieldInit
*&I
= ThePool
[TheKey
];
1664 if (!I
) I
= new(Allocator
) FieldInit(R
, FN
);
1668 Init
*FieldInit::getBit(unsigned Bit
) const {
1669 if (getType() == BitRecTy::get())
1670 return const_cast<FieldInit
*>(this);
1671 return VarBitInit::get(const_cast<FieldInit
*>(this), Bit
);
1674 Init
*FieldInit::resolveReferences(Resolver
&R
) const {
1675 Init
*NewRec
= Rec
->resolveReferences(R
);
1677 return FieldInit::get(NewRec
, FieldName
)->Fold(R
.getCurrentRecord());
1678 return const_cast<FieldInit
*>(this);
1681 Init
*FieldInit::Fold(Record
*CurRec
) const {
1682 if (DefInit
*DI
= dyn_cast
<DefInit
>(Rec
)) {
1683 Record
*Def
= DI
->getDef();
1685 PrintFatalError(CurRec
->getLoc(),
1686 Twine("Attempting to access field '") +
1687 FieldName
->getAsUnquotedString() + "' of '" +
1688 Rec
->getAsString() + "' is a forbidden self-reference");
1689 Init
*FieldVal
= Def
->getValue(FieldName
)->getValue();
1690 if (FieldVal
->isComplete())
1693 return const_cast<FieldInit
*>(this);
1696 static void ProfileCondOpInit(FoldingSetNodeID
&ID
,
1697 ArrayRef
<Init
*> CondRange
,
1698 ArrayRef
<Init
*> ValRange
,
1699 const RecTy
*ValType
) {
1700 assert(CondRange
.size() == ValRange
.size() &&
1701 "Number of conditions and values must match!");
1702 ID
.AddPointer(ValType
);
1703 ArrayRef
<Init
*>::iterator Case
= CondRange
.begin();
1704 ArrayRef
<Init
*>::iterator Val
= ValRange
.begin();
1706 while (Case
!= CondRange
.end()) {
1707 ID
.AddPointer(*Case
++);
1708 ID
.AddPointer(*Val
++);
1712 void CondOpInit::Profile(FoldingSetNodeID
&ID
) const {
1713 ProfileCondOpInit(ID
,
1714 makeArrayRef(getTrailingObjects
<Init
*>(), NumConds
),
1715 makeArrayRef(getTrailingObjects
<Init
*>() + NumConds
, NumConds
),
1720 CondOpInit::get(ArrayRef
<Init
*> CondRange
,
1721 ArrayRef
<Init
*> ValRange
, RecTy
*Ty
) {
1722 assert(CondRange
.size() == ValRange
.size() &&
1723 "Number of conditions and values must match!");
1725 static FoldingSet
<CondOpInit
> ThePool
;
1726 FoldingSetNodeID ID
;
1727 ProfileCondOpInit(ID
, CondRange
, ValRange
, Ty
);
1730 if (CondOpInit
*I
= ThePool
.FindNodeOrInsertPos(ID
, IP
))
1733 void *Mem
= Allocator
.Allocate(totalSizeToAlloc
<Init
*>(2*CondRange
.size()),
1735 CondOpInit
*I
= new(Mem
) CondOpInit(CondRange
.size(), Ty
);
1737 std::uninitialized_copy(CondRange
.begin(), CondRange
.end(),
1738 I
->getTrailingObjects
<Init
*>());
1739 std::uninitialized_copy(ValRange
.begin(), ValRange
.end(),
1740 I
->getTrailingObjects
<Init
*>()+CondRange
.size());
1741 ThePool
.InsertNode(I
, IP
);
1745 Init
*CondOpInit::resolveReferences(Resolver
&R
) const {
1746 SmallVector
<Init
*, 4> NewConds
;
1747 bool Changed
= false;
1748 for (const Init
*Case
: getConds()) {
1749 Init
*NewCase
= Case
->resolveReferences(R
);
1750 NewConds
.push_back(NewCase
);
1751 Changed
|= NewCase
!= Case
;
1754 SmallVector
<Init
*, 4> NewVals
;
1755 for (const Init
*Val
: getVals()) {
1756 Init
*NewVal
= Val
->resolveReferences(R
);
1757 NewVals
.push_back(NewVal
);
1758 Changed
|= NewVal
!= Val
;
1762 return (CondOpInit::get(NewConds
, NewVals
,
1763 getValType()))->Fold(R
.getCurrentRecord());
1765 return const_cast<CondOpInit
*>(this);
1768 Init
*CondOpInit::Fold(Record
*CurRec
) const {
1769 for ( unsigned i
= 0; i
< NumConds
; ++i
) {
1770 Init
*Cond
= getCond(i
);
1771 Init
*Val
= getVal(i
);
1773 if (IntInit
*CondI
= dyn_cast_or_null
<IntInit
>(
1774 Cond
->convertInitializerTo(IntRecTy::get()))) {
1775 if (CondI
->getValue())
1776 return Val
->convertInitializerTo(getValType());
1778 return const_cast<CondOpInit
*>(this);
1781 PrintFatalError(CurRec
->getLoc(),
1783 " does not have any true condition in:" +
1784 this->getAsString());
1788 bool CondOpInit::isConcrete() const {
1789 for (const Init
*Case
: getConds())
1790 if (!Case
->isConcrete())
1793 for (const Init
*Val
: getVals())
1794 if (!Val
->isConcrete())
1800 bool CondOpInit::isComplete() const {
1801 for (const Init
*Case
: getConds())
1802 if (!Case
->isComplete())
1805 for (const Init
*Val
: getVals())
1806 if (!Val
->isConcrete())
1812 std::string
CondOpInit::getAsString() const {
1813 std::string Result
= "!cond(";
1814 for (unsigned i
= 0; i
< getNumConds(); i
++) {
1815 Result
+= getCond(i
)->getAsString() + ": ";
1816 Result
+= getVal(i
)->getAsString();
1817 if (i
!= getNumConds()-1)
1820 return Result
+ ")";
1823 Init
*CondOpInit::getBit(unsigned Bit
) const {
1824 return VarBitInit::get(const_cast<CondOpInit
*>(this), Bit
);
1827 static void ProfileDagInit(FoldingSetNodeID
&ID
, Init
*V
, StringInit
*VN
,
1828 ArrayRef
<Init
*> ArgRange
,
1829 ArrayRef
<StringInit
*> NameRange
) {
1833 ArrayRef
<Init
*>::iterator Arg
= ArgRange
.begin();
1834 ArrayRef
<StringInit
*>::iterator Name
= NameRange
.begin();
1835 while (Arg
!= ArgRange
.end()) {
1836 assert(Name
!= NameRange
.end() && "Arg name underflow!");
1837 ID
.AddPointer(*Arg
++);
1838 ID
.AddPointer(*Name
++);
1840 assert(Name
== NameRange
.end() && "Arg name overflow!");
1844 DagInit::get(Init
*V
, StringInit
*VN
, ArrayRef
<Init
*> ArgRange
,
1845 ArrayRef
<StringInit
*> NameRange
) {
1846 static FoldingSet
<DagInit
> ThePool
;
1848 FoldingSetNodeID ID
;
1849 ProfileDagInit(ID
, V
, VN
, ArgRange
, NameRange
);
1852 if (DagInit
*I
= ThePool
.FindNodeOrInsertPos(ID
, IP
))
1855 void *Mem
= Allocator
.Allocate(totalSizeToAlloc
<Init
*, StringInit
*>(ArgRange
.size(), NameRange
.size()), alignof(BitsInit
));
1856 DagInit
*I
= new(Mem
) DagInit(V
, VN
, ArgRange
.size(), NameRange
.size());
1857 std::uninitialized_copy(ArgRange
.begin(), ArgRange
.end(),
1858 I
->getTrailingObjects
<Init
*>());
1859 std::uninitialized_copy(NameRange
.begin(), NameRange
.end(),
1860 I
->getTrailingObjects
<StringInit
*>());
1861 ThePool
.InsertNode(I
, IP
);
1866 DagInit::get(Init
*V
, StringInit
*VN
,
1867 ArrayRef
<std::pair
<Init
*, StringInit
*>> args
) {
1868 SmallVector
<Init
*, 8> Args
;
1869 SmallVector
<StringInit
*, 8> Names
;
1871 for (const auto &Arg
: args
) {
1872 Args
.push_back(Arg
.first
);
1873 Names
.push_back(Arg
.second
);
1876 return DagInit::get(V
, VN
, Args
, Names
);
1879 void DagInit::Profile(FoldingSetNodeID
&ID
) const {
1880 ProfileDagInit(ID
, Val
, ValName
, makeArrayRef(getTrailingObjects
<Init
*>(), NumArgs
), makeArrayRef(getTrailingObjects
<StringInit
*>(), NumArgNames
));
1883 Init
*DagInit::resolveReferences(Resolver
&R
) const {
1884 SmallVector
<Init
*, 8> NewArgs
;
1885 NewArgs
.reserve(arg_size());
1886 bool ArgsChanged
= false;
1887 for (const Init
*Arg
: getArgs()) {
1888 Init
*NewArg
= Arg
->resolveReferences(R
);
1889 NewArgs
.push_back(NewArg
);
1890 ArgsChanged
|= NewArg
!= Arg
;
1893 Init
*Op
= Val
->resolveReferences(R
);
1894 if (Op
!= Val
|| ArgsChanged
)
1895 return DagInit::get(Op
, ValName
, NewArgs
, getArgNames());
1897 return const_cast<DagInit
*>(this);
1900 bool DagInit::isConcrete() const {
1901 if (!Val
->isConcrete())
1903 for (const Init
*Elt
: getArgs()) {
1904 if (!Elt
->isConcrete())
1910 std::string
DagInit::getAsString() const {
1911 std::string Result
= "(" + Val
->getAsString();
1913 Result
+= ":" + ValName
->getAsUnquotedString();
1915 Result
+= " " + getArg(0)->getAsString();
1916 if (getArgName(0)) Result
+= ":$" + getArgName(0)->getAsUnquotedString();
1917 for (unsigned i
= 1, e
= getNumArgs(); i
!= e
; ++i
) {
1918 Result
+= ", " + getArg(i
)->getAsString();
1919 if (getArgName(i
)) Result
+= ":$" + getArgName(i
)->getAsUnquotedString();
1922 return Result
+ ")";
1925 //===----------------------------------------------------------------------===//
1926 // Other implementations
1927 //===----------------------------------------------------------------------===//
1929 RecordVal::RecordVal(Init
*N
, RecTy
*T
, bool P
)
1930 : Name(N
), TyAndPrefix(T
, P
) {
1931 setValue(UnsetInit::get());
1932 assert(Value
&& "Cannot create unset value for current type!");
1935 StringRef
RecordVal::getName() const {
1936 return cast
<StringInit
>(getNameInit())->getValue();
1939 bool RecordVal::setValue(Init
*V
) {
1941 Value
= V
->getCastTo(getType());
1943 assert(!isa
<TypedInit
>(Value
) ||
1944 cast
<TypedInit
>(Value
)->getType()->typeIsA(getType()));
1945 if (BitsRecTy
*BTy
= dyn_cast
<BitsRecTy
>(getType())) {
1946 if (!isa
<BitsInit
>(Value
)) {
1947 SmallVector
<Init
*, 64> Bits
;
1948 Bits
.reserve(BTy
->getNumBits());
1949 for (unsigned i
= 0, e
= BTy
->getNumBits(); i
< e
; ++i
)
1950 Bits
.push_back(Value
->getBit(i
));
1951 Value
= BitsInit::get(Bits
);
1955 return Value
== nullptr;
1961 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1962 LLVM_DUMP_METHOD
void RecordVal::dump() const { errs() << *this; }
1965 void RecordVal::print(raw_ostream
&OS
, bool PrintSem
) const {
1966 if (getPrefix()) OS
<< "field ";
1967 OS
<< *getType() << " " << getNameInitAsString();
1970 OS
<< " = " << *getValue();
1972 if (PrintSem
) OS
<< ";\n";
1975 unsigned Record::LastID
= 0;
1977 void Record::checkName() {
1978 // Ensure the record name has string type.
1979 const TypedInit
*TypedName
= cast
<const TypedInit
>(Name
);
1980 if (!isa
<StringRecTy
>(TypedName
->getType()))
1981 PrintFatalError(getLoc(), Twine("Record name '") + Name
->getAsString() +
1982 "' is not a string!");
1985 RecordRecTy
*Record::getType() {
1986 SmallVector
<Record
*, 4> DirectSCs
;
1987 getDirectSuperClasses(DirectSCs
);
1988 return RecordRecTy::get(DirectSCs
);
1991 DefInit
*Record::getDefInit() {
1993 TheInit
= new(Allocator
) DefInit(this);
1997 void Record::setName(Init
*NewName
) {
2000 // DO NOT resolve record values to the name at this point because
2001 // there might be default values for arguments of this def. Those
2002 // arguments might not have been resolved yet so we don't want to
2003 // prematurely assume values for those arguments were not passed to
2006 // Nonetheless, it may be that some of this Record's values
2007 // reference the record name. Indeed, the reason for having the
2008 // record name be an Init is to provide this flexibility. The extra
2009 // resolve steps after completely instantiating defs takes care of
2010 // this. See TGParser::ParseDef and TGParser::ParseDefm.
2013 void Record::getDirectSuperClasses(SmallVectorImpl
<Record
*> &Classes
) const {
2014 ArrayRef
<std::pair
<Record
*, SMRange
>> SCs
= getSuperClasses();
2015 while (!SCs
.empty()) {
2016 // Superclasses are in reverse preorder, so 'back' is a direct superclass,
2017 // and its transitive superclasses are directly preceding it.
2018 Record
*SC
= SCs
.back().first
;
2019 SCs
= SCs
.drop_back(1 + SC
->getSuperClasses().size());
2020 Classes
.push_back(SC
);
2024 void Record::resolveReferences(Resolver
&R
, const RecordVal
*SkipVal
) {
2025 for (RecordVal
&Value
: Values
) {
2026 if (SkipVal
== &Value
) // Skip resolve the same field as the given one
2028 if (Init
*V
= Value
.getValue()) {
2029 Init
*VR
= V
->resolveReferences(R
);
2030 if (Value
.setValue(VR
)) {
2032 if (TypedInit
*VRT
= dyn_cast
<TypedInit
>(VR
))
2034 (Twine("of type '") + VRT
->getType()->getAsString() + "' ").str();
2035 PrintFatalError(getLoc(), Twine("Invalid value ") + Type
+
2036 "is found when setting '" +
2037 Value
.getNameInitAsString() +
2039 Value
.getType()->getAsString() +
2040 "' after resolving references: " +
2041 VR
->getAsUnquotedString() + "\n");
2045 Init
*OldName
= getNameInit();
2046 Init
*NewName
= Name
->resolveReferences(R
);
2047 if (NewName
!= OldName
) {
2048 // Re-register with RecordKeeper.
2053 void Record::resolveReferences() {
2054 RecordResolver
R(*this);
2056 resolveReferences(R
);
2059 void Record::resolveReferencesTo(const RecordVal
*RV
) {
2060 RecordValResolver
R(*this, RV
);
2061 resolveReferences(R
, RV
);
2064 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2065 LLVM_DUMP_METHOD
void Record::dump() const { errs() << *this; }
2068 raw_ostream
&llvm::operator<<(raw_ostream
&OS
, const Record
&R
) {
2069 OS
<< R
.getNameInitAsString();
2071 ArrayRef
<Init
*> TArgs
= R
.getTemplateArgs();
2072 if (!TArgs
.empty()) {
2074 bool NeedComma
= false;
2075 for (const Init
*TA
: TArgs
) {
2076 if (NeedComma
) OS
<< ", ";
2078 const RecordVal
*RV
= R
.getValue(TA
);
2079 assert(RV
&& "Template argument record not found??");
2080 RV
->print(OS
, false);
2086 ArrayRef
<std::pair
<Record
*, SMRange
>> SC
= R
.getSuperClasses();
2089 for (const auto &SuperPair
: SC
)
2090 OS
<< " " << SuperPair
.first
->getNameInitAsString();
2094 for (const RecordVal
&Val
: R
.getValues())
2095 if (Val
.getPrefix() && !R
.isTemplateArg(Val
.getNameInit()))
2097 for (const RecordVal
&Val
: R
.getValues())
2098 if (!Val
.getPrefix() && !R
.isTemplateArg(Val
.getNameInit()))
2104 Init
*Record::getValueInit(StringRef FieldName
) const {
2105 const RecordVal
*R
= getValue(FieldName
);
2106 if (!R
|| !R
->getValue())
2107 PrintFatalError(getLoc(), "Record `" + getName() +
2108 "' does not have a field named `" + FieldName
+ "'!\n");
2109 return R
->getValue();
2112 StringRef
Record::getValueAsString(StringRef FieldName
) const {
2113 const RecordVal
*R
= getValue(FieldName
);
2114 if (!R
|| !R
->getValue())
2115 PrintFatalError(getLoc(), "Record `" + getName() +
2116 "' does not have a field named `" + FieldName
+ "'!\n");
2118 if (StringInit
*SI
= dyn_cast
<StringInit
>(R
->getValue()))
2119 return SI
->getValue();
2120 if (CodeInit
*CI
= dyn_cast
<CodeInit
>(R
->getValue()))
2121 return CI
->getValue();
2123 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2124 FieldName
+ "' does not have a string initializer!");
2127 BitsInit
*Record::getValueAsBitsInit(StringRef FieldName
) const {
2128 const RecordVal
*R
= getValue(FieldName
);
2129 if (!R
|| !R
->getValue())
2130 PrintFatalError(getLoc(), "Record `" + getName() +
2131 "' does not have a field named `" + FieldName
+ "'!\n");
2133 if (BitsInit
*BI
= dyn_cast
<BitsInit
>(R
->getValue()))
2135 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2136 FieldName
+ "' does not have a BitsInit initializer!");
2139 ListInit
*Record::getValueAsListInit(StringRef FieldName
) const {
2140 const RecordVal
*R
= getValue(FieldName
);
2141 if (!R
|| !R
->getValue())
2142 PrintFatalError(getLoc(), "Record `" + getName() +
2143 "' does not have a field named `" + FieldName
+ "'!\n");
2145 if (ListInit
*LI
= dyn_cast
<ListInit
>(R
->getValue()))
2147 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2148 FieldName
+ "' does not have a list initializer!");
2151 std::vector
<Record
*>
2152 Record::getValueAsListOfDefs(StringRef FieldName
) const {
2153 ListInit
*List
= getValueAsListInit(FieldName
);
2154 std::vector
<Record
*> Defs
;
2155 for (Init
*I
: List
->getValues()) {
2156 if (DefInit
*DI
= dyn_cast
<DefInit
>(I
))
2157 Defs
.push_back(DI
->getDef());
2159 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2160 FieldName
+ "' list is not entirely DefInit!");
2165 int64_t Record::getValueAsInt(StringRef FieldName
) const {
2166 const RecordVal
*R
= getValue(FieldName
);
2167 if (!R
|| !R
->getValue())
2168 PrintFatalError(getLoc(), "Record `" + getName() +
2169 "' does not have a field named `" + FieldName
+ "'!\n");
2171 if (IntInit
*II
= dyn_cast
<IntInit
>(R
->getValue()))
2172 return II
->getValue();
2173 PrintFatalError(getLoc(), Twine("Record `") + getName() + "', field `" +
2175 "' does not have an int initializer: " +
2176 R
->getValue()->getAsString());
2179 std::vector
<int64_t>
2180 Record::getValueAsListOfInts(StringRef FieldName
) const {
2181 ListInit
*List
= getValueAsListInit(FieldName
);
2182 std::vector
<int64_t> Ints
;
2183 for (Init
*I
: List
->getValues()) {
2184 if (IntInit
*II
= dyn_cast
<IntInit
>(I
))
2185 Ints
.push_back(II
->getValue());
2187 PrintFatalError(getLoc(),
2188 Twine("Record `") + getName() + "', field `" + FieldName
+
2189 "' does not have a list of ints initializer: " +
2195 std::vector
<StringRef
>
2196 Record::getValueAsListOfStrings(StringRef FieldName
) const {
2197 ListInit
*List
= getValueAsListInit(FieldName
);
2198 std::vector
<StringRef
> Strings
;
2199 for (Init
*I
: List
->getValues()) {
2200 if (StringInit
*SI
= dyn_cast
<StringInit
>(I
))
2201 Strings
.push_back(SI
->getValue());
2203 PrintFatalError(getLoc(),
2204 Twine("Record `") + getName() + "', field `" + FieldName
+
2205 "' does not have a list of strings initializer: " +
2211 Record
*Record::getValueAsDef(StringRef FieldName
) const {
2212 const RecordVal
*R
= getValue(FieldName
);
2213 if (!R
|| !R
->getValue())
2214 PrintFatalError(getLoc(), "Record `" + getName() +
2215 "' does not have a field named `" + FieldName
+ "'!\n");
2217 if (DefInit
*DI
= dyn_cast
<DefInit
>(R
->getValue()))
2218 return DI
->getDef();
2219 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2220 FieldName
+ "' does not have a def initializer!");
2223 bool Record::getValueAsBit(StringRef FieldName
) const {
2224 const RecordVal
*R
= getValue(FieldName
);
2225 if (!R
|| !R
->getValue())
2226 PrintFatalError(getLoc(), "Record `" + getName() +
2227 "' does not have a field named `" + FieldName
+ "'!\n");
2229 if (BitInit
*BI
= dyn_cast
<BitInit
>(R
->getValue()))
2230 return BI
->getValue();
2231 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2232 FieldName
+ "' does not have a bit initializer!");
2235 bool Record::getValueAsBitOrUnset(StringRef FieldName
, bool &Unset
) const {
2236 const RecordVal
*R
= getValue(FieldName
);
2237 if (!R
|| !R
->getValue())
2238 PrintFatalError(getLoc(), "Record `" + getName() +
2239 "' does not have a field named `" + FieldName
.str() + "'!\n");
2241 if (isa
<UnsetInit
>(R
->getValue())) {
2246 if (BitInit
*BI
= dyn_cast
<BitInit
>(R
->getValue()))
2247 return BI
->getValue();
2248 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2249 FieldName
+ "' does not have a bit initializer!");
2252 DagInit
*Record::getValueAsDag(StringRef FieldName
) const {
2253 const RecordVal
*R
= getValue(FieldName
);
2254 if (!R
|| !R
->getValue())
2255 PrintFatalError(getLoc(), "Record `" + getName() +
2256 "' does not have a field named `" + FieldName
+ "'!\n");
2258 if (DagInit
*DI
= dyn_cast
<DagInit
>(R
->getValue()))
2260 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2261 FieldName
+ "' does not have a dag initializer!");
2264 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2265 LLVM_DUMP_METHOD
void RecordKeeper::dump() const { errs() << *this; }
2268 raw_ostream
&llvm::operator<<(raw_ostream
&OS
, const RecordKeeper
&RK
) {
2269 OS
<< "------------- Classes -----------------\n";
2270 for (const auto &C
: RK
.getClasses())
2271 OS
<< "class " << *C
.second
;
2273 OS
<< "------------- Defs -----------------\n";
2274 for (const auto &D
: RK
.getDefs())
2275 OS
<< "def " << *D
.second
;
2279 /// GetNewAnonymousName - Generate a unique anonymous name that can be used as
2281 Init
*RecordKeeper::getNewAnonymousName() {
2282 return StringInit::get("anonymous_" + utostr(AnonCounter
++));
2285 std::vector
<Record
*>
2286 RecordKeeper::getAllDerivedDefinitions(StringRef ClassName
) const {
2287 Record
*Class
= getClass(ClassName
);
2289 PrintFatalError("ERROR: Couldn't find the `" + ClassName
+ "' class!\n");
2291 std::vector
<Record
*> Defs
;
2292 for (const auto &D
: getDefs())
2293 if (D
.second
->isSubClassOf(Class
))
2294 Defs
.push_back(D
.second
.get());
2299 Init
*MapResolver::resolve(Init
*VarName
) {
2300 auto It
= Map
.find(VarName
);
2301 if (It
== Map
.end())
2304 Init
*I
= It
->second
.V
;
2306 if (!It
->second
.Resolved
&& Map
.size() > 1) {
2307 // Resolve mutual references among the mapped variables, but prevent
2308 // infinite recursion.
2310 I
= I
->resolveReferences(*this);
2311 Map
[VarName
] = {I
, true};
2317 Init
*RecordResolver::resolve(Init
*VarName
) {
2318 Init
*Val
= Cache
.lookup(VarName
);
2322 for (Init
*S
: Stack
) {
2324 return nullptr; // prevent infinite recursion
2327 if (RecordVal
*RV
= getCurrentRecord()->getValue(VarName
)) {
2328 if (!isa
<UnsetInit
>(RV
->getValue())) {
2329 Val
= RV
->getValue();
2330 Stack
.push_back(VarName
);
2331 Val
= Val
->resolveReferences(*this);
2336 Cache
[VarName
] = Val
;
2340 Init
*TrackUnresolvedResolver::resolve(Init
*VarName
) {
2344 I
= R
->resolve(VarName
);
2345 if (I
&& !FoundUnresolved
) {
2346 // Do not recurse into the resolved initializer, as that would change
2347 // the behavior of the resolver we're delegating, but do check to see
2348 // if there are unresolved variables remaining.
2349 TrackUnresolvedResolver Sub
;
2350 I
->resolveReferences(Sub
);
2351 FoundUnresolved
|= Sub
.FoundUnresolved
;
2356 FoundUnresolved
= true;
2360 Init
*HasReferenceResolver::resolve(Init
*VarName
)
2362 if (VarName
== VarNameToTrack
)