1 //===- Record.cpp - Record implementation ---------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Implement the tablegen record classes.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/TableGen/Record.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/FoldingSet.h"
17 #include "llvm/ADT/Hashing.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/ADT/StringMap.h"
22 #include "llvm/Support/DataTypes.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/Format.h"
25 #include "llvm/TableGen/Error.h"
26
27 using namespace llvm;
28
29 //===----------------------------------------------------------------------===//
30 // std::string wrapper for DenseMap purposes
31 //===----------------------------------------------------------------------===//
32
33 namespace llvm {
34
35 /// This is a wrapper for std::string suitable for using as a key to a DenseMap.
36 /// Because there isn't a particularly
37 /// good way to indicate tombstone or empty keys for strings, we want
38 /// to wrap std::string to indicate that this is a "special" string
39 /// not expected to take on certain values (those of the tombstone and
40 /// empty keys). This makes things a little safer as it clarifies
41 /// that DenseMap is really not appropriate for general strings.
42
43 class TableGenStringKey {
44 public:
TableGenStringKey(const std::string & str)45 TableGenStringKey(const std::string &str) : data(str) {}
TableGenStringKey(const char * str)46 TableGenStringKey(const char *str) : data(str) {}
47
str() const48 const std::string &str() const { return data; }
49
hash_value(const TableGenStringKey & Value)50 friend hash_code hash_value(const TableGenStringKey &Value) {
51 using llvm::hash_value;
52 return hash_value(Value.str());
53 }
54 private:
55 std::string data;
56 };
57
58 /// Specialize DenseMapInfo for TableGenStringKey.
59 template<> struct DenseMapInfo<TableGenStringKey> {
getEmptyKeyllvm::DenseMapInfo60 static inline TableGenStringKey getEmptyKey() {
61 TableGenStringKey Empty("<<<EMPTY KEY>>>");
62 return Empty;
63 }
getTombstoneKeyllvm::DenseMapInfo64 static inline TableGenStringKey getTombstoneKey() {
65 TableGenStringKey Tombstone("<<<TOMBSTONE KEY>>>");
66 return Tombstone;
67 }
getHashValuellvm::DenseMapInfo68 static unsigned getHashValue(const TableGenStringKey& Val) {
69 using llvm::hash_value;
70 return hash_value(Val);
71 }
isEqualllvm::DenseMapInfo72 static bool isEqual(const TableGenStringKey& LHS,
73 const TableGenStringKey& RHS) {
74 return LHS.str() == RHS.str();
75 }
76 };
77
78 } // namespace llvm
79
80 //===----------------------------------------------------------------------===//
81 // Type implementations
82 //===----------------------------------------------------------------------===//
83
84 BitRecTy BitRecTy::Shared;
85 CodeRecTy CodeRecTy::Shared;
86 IntRecTy IntRecTy::Shared;
87 StringRecTy StringRecTy::Shared;
88 DagRecTy DagRecTy::Shared;
89
dump() const90 LLVM_DUMP_METHOD void RecTy::dump() const { print(errs()); }
91
getListTy()92 ListRecTy *RecTy::getListTy() {
93 if (!ListTy)
94 ListTy.reset(new ListRecTy(this));
95 return ListTy.get();
96 }
97
typeIsConvertibleTo(const RecTy * RHS) const98 bool RecTy::typeIsConvertibleTo(const RecTy *RHS) const {
99 assert(RHS && "NULL pointer");
100 return Kind == RHS->getRecTyKind();
101 }
102
typeIsConvertibleTo(const RecTy * RHS) const103 bool BitRecTy::typeIsConvertibleTo(const RecTy *RHS) const{
104 if (RecTy::typeIsConvertibleTo(RHS) || RHS->getRecTyKind() == IntRecTyKind)
105 return true;
106 if (const BitsRecTy *BitsTy = dyn_cast<BitsRecTy>(RHS))
107 return BitsTy->getNumBits() == 1;
108 return false;
109 }
110
get(unsigned Sz)111 BitsRecTy *BitsRecTy::get(unsigned Sz) {
112 static std::vector<std::unique_ptr<BitsRecTy>> Shared;
113 if (Sz >= Shared.size())
114 Shared.resize(Sz + 1);
115 std::unique_ptr<BitsRecTy> &Ty = Shared[Sz];
116 if (!Ty)
117 Ty.reset(new BitsRecTy(Sz));
118 return Ty.get();
119 }
120
getAsString() const121 std::string BitsRecTy::getAsString() const {
122 return "bits<" + utostr(Size) + ">";
123 }
124
typeIsConvertibleTo(const RecTy * RHS) const125 bool BitsRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
126 if (RecTy::typeIsConvertibleTo(RHS)) //argument and the sender are same type
127 return cast<BitsRecTy>(RHS)->Size == Size;
128 RecTyKind kind = RHS->getRecTyKind();
129 return (kind == BitRecTyKind && Size == 1) || (kind == IntRecTyKind);
130 }
131
typeIsConvertibleTo(const RecTy * RHS) const132 bool IntRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
133 RecTyKind kind = RHS->getRecTyKind();
134 return kind==BitRecTyKind || kind==BitsRecTyKind || kind==IntRecTyKind;
135 }
136
getAsString() const137 std::string StringRecTy::getAsString() const {
138 return "string";
139 }
140
getAsString() const141 std::string ListRecTy::getAsString() const {
142 return "list<" + Ty->getAsString() + ">";
143 }
144
typeIsConvertibleTo(const RecTy * RHS) const145 bool ListRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
146 if (const auto *ListTy = dyn_cast<ListRecTy>(RHS))
147 return Ty->typeIsConvertibleTo(ListTy->getElementType());
148 return false;
149 }
150
getAsString() const151 std::string DagRecTy::getAsString() const {
152 return "dag";
153 }
154
get(Record * R)155 RecordRecTy *RecordRecTy::get(Record *R) {
156 return dyn_cast<RecordRecTy>(R->getDefInit()->getType());
157 }
158
getAsString() const159 std::string RecordRecTy::getAsString() const {
160 return Rec->getName();
161 }
162
typeIsConvertibleTo(const RecTy * RHS) const163 bool RecordRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
164 const RecordRecTy *RTy = dyn_cast<RecordRecTy>(RHS);
165 if (!RTy)
166 return false;
167
168 if (RTy->getRecord() == Rec || Rec->isSubClassOf(RTy->getRecord()))
169 return true;
170
171 for (const auto &SCPair : RTy->getRecord()->getSuperClasses())
172 if (Rec->isSubClassOf(SCPair.first))
173 return true;
174
175 return false;
176 }
177
resolveTypes(RecTy * T1,RecTy * T2)178 RecTy *llvm::resolveTypes(RecTy *T1, RecTy *T2) {
179 if (T1->typeIsConvertibleTo(T2))
180 return T2;
181 if (T2->typeIsConvertibleTo(T1))
182 return T1;
183
184 // If one is a Record type, check superclasses
185 if (RecordRecTy *RecTy1 = dyn_cast<RecordRecTy>(T1)) {
186 // See if T2 inherits from a type T1 also inherits from
187 for (const auto &SuperPair1 : RecTy1->getRecord()->getSuperClasses()) {
188 RecordRecTy *SuperRecTy1 = RecordRecTy::get(SuperPair1.first);
189 RecTy *NewType1 = resolveTypes(SuperRecTy1, T2);
190 if (NewType1)
191 return NewType1;
192 }
193 }
194 if (RecordRecTy *RecTy2 = dyn_cast<RecordRecTy>(T2)) {
195 // See if T1 inherits from a type T2 also inherits from
196 for (const auto &SuperPair2 : RecTy2->getRecord()->getSuperClasses()) {
197 RecordRecTy *SuperRecTy2 = RecordRecTy::get(SuperPair2.first);
198 RecTy *NewType2 = resolveTypes(T1, SuperRecTy2);
199 if (NewType2)
200 return NewType2;
201 }
202 }
203 return nullptr;
204 }
205
206
207 //===----------------------------------------------------------------------===//
208 // Initializer implementations
209 //===----------------------------------------------------------------------===//
210
anchor()211 void Init::anchor() { }
dump() const212 LLVM_DUMP_METHOD void Init::dump() const { return print(errs()); }
213
get()214 UnsetInit *UnsetInit::get() {
215 static UnsetInit TheInit;
216 return &TheInit;
217 }
218
convertInitializerTo(RecTy * Ty) const219 Init *UnsetInit::convertInitializerTo(RecTy *Ty) const {
220 if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
221 SmallVector<Init *, 16> NewBits(BRT->getNumBits());
222
223 for (unsigned i = 0; i != BRT->getNumBits(); ++i)
224 NewBits[i] = UnsetInit::get();
225
226 return BitsInit::get(NewBits);
227 }
228
229 // All other types can just be returned.
230 return const_cast<UnsetInit *>(this);
231 }
232
get(bool V)233 BitInit *BitInit::get(bool V) {
234 static BitInit True(true);
235 static BitInit False(false);
236
237 return V ? &True : &False;
238 }
239
convertInitializerTo(RecTy * Ty) const240 Init *BitInit::convertInitializerTo(RecTy *Ty) const {
241 if (isa<BitRecTy>(Ty))
242 return const_cast<BitInit *>(this);
243
244 if (isa<IntRecTy>(Ty))
245 return IntInit::get(getValue());
246
247 if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
248 // Can only convert single bit.
249 if (BRT->getNumBits() == 1)
250 return BitsInit::get(const_cast<BitInit *>(this));
251 }
252
253 return nullptr;
254 }
255
256 static void
ProfileBitsInit(FoldingSetNodeID & ID,ArrayRef<Init * > Range)257 ProfileBitsInit(FoldingSetNodeID &ID, ArrayRef<Init *> Range) {
258 ID.AddInteger(Range.size());
259
260 for (Init *I : Range)
261 ID.AddPointer(I);
262 }
263
get(ArrayRef<Init * > Range)264 BitsInit *BitsInit::get(ArrayRef<Init *> Range) {
265 static FoldingSet<BitsInit> ThePool;
266 static std::vector<std::unique_ptr<BitsInit>> TheActualPool;
267
268 FoldingSetNodeID ID;
269 ProfileBitsInit(ID, Range);
270
271 void *IP = nullptr;
272 if (BitsInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
273 return I;
274
275 void *Mem = ::operator new (totalSizeToAlloc<Init *>(Range.size()));
276 BitsInit *I = new (Mem) BitsInit(Range.size());
277 std::uninitialized_copy(Range.begin(), Range.end(),
278 I->getTrailingObjects<Init *>());
279 ThePool.InsertNode(I, IP);
280 TheActualPool.push_back(std::unique_ptr<BitsInit>(I));
281 return I;
282 }
283
Profile(FoldingSetNodeID & ID) const284 void BitsInit::Profile(FoldingSetNodeID &ID) const {
285 ProfileBitsInit(ID, makeArrayRef(getTrailingObjects<Init *>(), NumBits));
286 }
287
convertInitializerTo(RecTy * Ty) const288 Init *BitsInit::convertInitializerTo(RecTy *Ty) const {
289 if (isa<BitRecTy>(Ty)) {
290 if (getNumBits() != 1) return nullptr; // Only accept if just one bit!
291 return getBit(0);
292 }
293
294 if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
295 // If the number of bits is right, return it. Otherwise we need to expand
296 // or truncate.
297 if (getNumBits() != BRT->getNumBits()) return nullptr;
298 return const_cast<BitsInit *>(this);
299 }
300
301 if (isa<IntRecTy>(Ty)) {
302 int64_t Result = 0;
303 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
304 if (auto *Bit = dyn_cast<BitInit>(getBit(i)))
305 Result |= static_cast<int64_t>(Bit->getValue()) << i;
306 else
307 return nullptr;
308 return IntInit::get(Result);
309 }
310
311 return nullptr;
312 }
313
314 Init *
convertInitializerBitRange(const std::vector<unsigned> & Bits) const315 BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) const {
316 SmallVector<Init *, 16> NewBits(Bits.size());
317
318 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
319 if (Bits[i] >= getNumBits())
320 return nullptr;
321 NewBits[i] = getBit(Bits[i]);
322 }
323 return BitsInit::get(NewBits);
324 }
325
getAsString() const326 std::string BitsInit::getAsString() const {
327 std::string Result = "{ ";
328 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
329 if (i) Result += ", ";
330 if (Init *Bit = getBit(e-i-1))
331 Result += Bit->getAsString();
332 else
333 Result += "*";
334 }
335 return Result + " }";
336 }
337
338 // Fix bit initializer to preserve the behavior that bit reference from a unset
339 // bits initializer will resolve into VarBitInit to keep the field name and bit
340 // number used in targets with fixed insn length.
fixBitInit(const RecordVal * RV,Init * Before,Init * After)341 static Init *fixBitInit(const RecordVal *RV, Init *Before, Init *After) {
342 if (RV || !isa<UnsetInit>(After))
343 return After;
344 return Before;
345 }
346
347 // resolveReferences - If there are any field references that refer to fields
348 // that have been filled in, we can propagate the values now.
349 //
resolveReferences(Record & R,const RecordVal * RV) const350 Init *BitsInit::resolveReferences(Record &R, const RecordVal *RV) const {
351 bool Changed = false;
352 SmallVector<Init *, 16> NewBits(getNumBits());
353
354 Init *CachedInit = nullptr;
355 Init *CachedBitVar = nullptr;
356 bool CachedBitVarChanged = false;
357
358 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
359 Init *CurBit = getBit(i);
360 Init *CurBitVar = CurBit->getBitVar();
361
362 NewBits[i] = CurBit;
363
364 if (CurBitVar == CachedBitVar) {
365 if (CachedBitVarChanged) {
366 Init *Bit = CachedInit->getBit(CurBit->getBitNum());
367 NewBits[i] = fixBitInit(RV, CurBit, Bit);
368 }
369 continue;
370 }
371 CachedBitVar = CurBitVar;
372 CachedBitVarChanged = false;
373
374 Init *B;
375 do {
376 B = CurBitVar;
377 CurBitVar = CurBitVar->resolveReferences(R, RV);
378 CachedBitVarChanged |= B != CurBitVar;
379 Changed |= B != CurBitVar;
380 } while (B != CurBitVar);
381 CachedInit = CurBitVar;
382
383 if (CachedBitVarChanged) {
384 Init *Bit = CurBitVar->getBit(CurBit->getBitNum());
385 NewBits[i] = fixBitInit(RV, CurBit, Bit);
386 }
387 }
388
389 if (Changed)
390 return BitsInit::get(NewBits);
391
392 return const_cast<BitsInit *>(this);
393 }
394
get(int64_t V)395 IntInit *IntInit::get(int64_t V) {
396 static DenseMap<int64_t, std::unique_ptr<IntInit>> ThePool;
397
398 std::unique_ptr<IntInit> &I = ThePool[V];
399 if (!I) I.reset(new IntInit(V));
400 return I.get();
401 }
402
getAsString() const403 std::string IntInit::getAsString() const {
404 return itostr(Value);
405 }
406
canFitInBitfield(int64_t Value,unsigned NumBits)407 static bool canFitInBitfield(int64_t Value, unsigned NumBits) {
408 // For example, with NumBits == 4, we permit Values from [-7 .. 15].
409 return (NumBits >= sizeof(Value) * 8) ||
410 (Value >> NumBits == 0) || (Value >> (NumBits-1) == -1);
411 }
412
convertInitializerTo(RecTy * Ty) const413 Init *IntInit::convertInitializerTo(RecTy *Ty) const {
414 if (isa<IntRecTy>(Ty))
415 return const_cast<IntInit *>(this);
416
417 if (isa<BitRecTy>(Ty)) {
418 int64_t Val = getValue();
419 if (Val != 0 && Val != 1) return nullptr; // Only accept 0 or 1 for a bit!
420 return BitInit::get(Val != 0);
421 }
422
423 if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
424 int64_t Value = getValue();
425 // Make sure this bitfield is large enough to hold the integer value.
426 if (!canFitInBitfield(Value, BRT->getNumBits()))
427 return nullptr;
428
429 SmallVector<Init *, 16> NewBits(BRT->getNumBits());
430 for (unsigned i = 0; i != BRT->getNumBits(); ++i)
431 NewBits[i] = BitInit::get(Value & (1LL << i));
432
433 return BitsInit::get(NewBits);
434 }
435
436 return nullptr;
437 }
438
439 Init *
convertInitializerBitRange(const std::vector<unsigned> & Bits) const440 IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) const {
441 SmallVector<Init *, 16> NewBits(Bits.size());
442
443 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
444 if (Bits[i] >= 64)
445 return nullptr;
446
447 NewBits[i] = BitInit::get(Value & (INT64_C(1) << Bits[i]));
448 }
449 return BitsInit::get(NewBits);
450 }
451
get(StringRef V)452 CodeInit *CodeInit::get(StringRef V) {
453 static StringMap<std::unique_ptr<CodeInit>> ThePool;
454
455 std::unique_ptr<CodeInit> &I = ThePool[V];
456 if (!I) I.reset(new CodeInit(V));
457 return I.get();
458 }
459
get(StringRef V)460 StringInit *StringInit::get(StringRef V) {
461 static StringMap<std::unique_ptr<StringInit>> ThePool;
462
463 std::unique_ptr<StringInit> &I = ThePool[V];
464 if (!I) I.reset(new StringInit(V));
465 return I.get();
466 }
467
convertInitializerTo(RecTy * Ty) const468 Init *StringInit::convertInitializerTo(RecTy *Ty) const {
469 if (isa<StringRecTy>(Ty))
470 return const_cast<StringInit *>(this);
471
472 return nullptr;
473 }
474
convertInitializerTo(RecTy * Ty) const475 Init *CodeInit::convertInitializerTo(RecTy *Ty) const {
476 if (isa<CodeRecTy>(Ty))
477 return const_cast<CodeInit *>(this);
478
479 return nullptr;
480 }
481
ProfileListInit(FoldingSetNodeID & ID,ArrayRef<Init * > Range,RecTy * EltTy)482 static void ProfileListInit(FoldingSetNodeID &ID,
483 ArrayRef<Init *> Range,
484 RecTy *EltTy) {
485 ID.AddInteger(Range.size());
486 ID.AddPointer(EltTy);
487
488 for (Init *I : Range)
489 ID.AddPointer(I);
490 }
491
get(ArrayRef<Init * > Range,RecTy * EltTy)492 ListInit *ListInit::get(ArrayRef<Init *> Range, RecTy *EltTy) {
493 static FoldingSet<ListInit> ThePool;
494 static std::vector<std::unique_ptr<ListInit>> TheActualPool;
495
496 FoldingSetNodeID ID;
497 ProfileListInit(ID, Range, EltTy);
498
499 void *IP = nullptr;
500 if (ListInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
501 return I;
502
503 void *Mem = ::operator new (totalSizeToAlloc<Init *>(Range.size()));
504 ListInit *I = new (Mem) ListInit(Range.size(), EltTy);
505 std::uninitialized_copy(Range.begin(), Range.end(),
506 I->getTrailingObjects<Init *>());
507 ThePool.InsertNode(I, IP);
508 TheActualPool.push_back(std::unique_ptr<ListInit>(I));
509 return I;
510 }
511
Profile(FoldingSetNodeID & ID) const512 void ListInit::Profile(FoldingSetNodeID &ID) const {
513 RecTy *EltTy = cast<ListRecTy>(getType())->getElementType();
514
515 ProfileListInit(ID, getValues(), EltTy);
516 }
517
convertInitializerTo(RecTy * Ty) const518 Init *ListInit::convertInitializerTo(RecTy *Ty) const {
519 if (auto *LRT = dyn_cast<ListRecTy>(Ty)) {
520 std::vector<Init*> Elements;
521
522 // Verify that all of the elements of the list are subclasses of the
523 // appropriate class!
524 for (Init *I : getValues())
525 if (Init *CI = I->convertInitializerTo(LRT->getElementType()))
526 Elements.push_back(CI);
527 else
528 return nullptr;
529
530 if (isa<ListRecTy>(getType()))
531 return ListInit::get(Elements, Ty);
532 }
533
534 return nullptr;
535 }
536
537 Init *
convertInitListSlice(const std::vector<unsigned> & Elements) const538 ListInit::convertInitListSlice(const std::vector<unsigned> &Elements) const {
539 std::vector<Init*> Vals;
540 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
541 if (Elements[i] >= size())
542 return nullptr;
543 Vals.push_back(getElement(Elements[i]));
544 }
545 return ListInit::get(Vals, getType());
546 }
547
getElementAsRecord(unsigned i) const548 Record *ListInit::getElementAsRecord(unsigned i) const {
549 assert(i < NumValues && "List element index out of range!");
550 DefInit *DI = dyn_cast<DefInit>(getElement(i));
551 if (!DI)
552 PrintFatalError("Expected record in list!");
553 return DI->getDef();
554 }
555
resolveReferences(Record & R,const RecordVal * RV) const556 Init *ListInit::resolveReferences(Record &R, const RecordVal *RV) const {
557 std::vector<Init*> Resolved;
558 Resolved.reserve(size());
559 bool Changed = false;
560
561 for (Init *CurElt : getValues()) {
562 Init *E;
563
564 do {
565 E = CurElt;
566 CurElt = CurElt->resolveReferences(R, RV);
567 Changed |= E != CurElt;
568 } while (E != CurElt);
569 Resolved.push_back(E);
570 }
571
572 if (Changed)
573 return ListInit::get(Resolved, getType());
574 return const_cast<ListInit *>(this);
575 }
576
resolveListElementReference(Record & R,const RecordVal * IRV,unsigned Elt) const577 Init *ListInit::resolveListElementReference(Record &R, const RecordVal *IRV,
578 unsigned Elt) const {
579 if (Elt >= size())
580 return nullptr; // Out of range reference.
581 Init *E = getElement(Elt);
582 // If the element is set to some value, or if we are resolving a reference
583 // to a specific variable and that variable is explicitly unset, then
584 // replace the VarListElementInit with it.
585 if (IRV || !isa<UnsetInit>(E))
586 return E;
587 return nullptr;
588 }
589
getAsString() const590 std::string ListInit::getAsString() const {
591 std::string Result = "[";
592 for (unsigned i = 0, e = NumValues; i != e; ++i) {
593 if (i) Result += ", ";
594 Result += getElement(i)->getAsString();
595 }
596 return Result + "]";
597 }
598
resolveListElementReference(Record & R,const RecordVal * IRV,unsigned Elt) const599 Init *OpInit::resolveListElementReference(Record &R, const RecordVal *IRV,
600 unsigned Elt) const {
601 Init *Resolved = resolveReferences(R, IRV);
602 OpInit *OResolved = dyn_cast<OpInit>(Resolved);
603 if (OResolved) {
604 Resolved = OResolved->Fold(&R, nullptr);
605 }
606
607 if (Resolved != this) {
608 TypedInit *Typed = cast<TypedInit>(Resolved);
609 if (Init *New = Typed->resolveListElementReference(R, IRV, Elt))
610 return New;
611 return VarListElementInit::get(Typed, Elt);
612 }
613
614 return nullptr;
615 }
616
getBit(unsigned Bit) const617 Init *OpInit::getBit(unsigned Bit) const {
618 if (getType() == BitRecTy::get())
619 return const_cast<OpInit*>(this);
620 return VarBitInit::get(const_cast<OpInit*>(this), Bit);
621 }
622
623 static void
ProfileUnOpInit(FoldingSetNodeID & ID,unsigned Opcode,Init * Op,RecTy * Type)624 ProfileUnOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *Op, RecTy *Type) {
625 ID.AddInteger(Opcode);
626 ID.AddPointer(Op);
627 ID.AddPointer(Type);
628 }
629
get(UnaryOp Opc,Init * LHS,RecTy * Type)630 UnOpInit *UnOpInit::get(UnaryOp Opc, Init *LHS, RecTy *Type) {
631 static FoldingSet<UnOpInit> ThePool;
632 static std::vector<std::unique_ptr<UnOpInit>> TheActualPool;
633
634 FoldingSetNodeID ID;
635 ProfileUnOpInit(ID, Opc, LHS, Type);
636
637 void *IP = nullptr;
638 if (UnOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
639 return I;
640
641 UnOpInit *I = new UnOpInit(Opc, LHS, Type);
642 ThePool.InsertNode(I, IP);
643 TheActualPool.push_back(std::unique_ptr<UnOpInit>(I));
644 return I;
645 }
646
Profile(FoldingSetNodeID & ID) const647 void UnOpInit::Profile(FoldingSetNodeID &ID) const {
648 ProfileUnOpInit(ID, getOpcode(), getOperand(), getType());
649 }
650
Fold(Record * CurRec,MultiClass * CurMultiClass) const651 Init *UnOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const {
652 switch (getOpcode()) {
653 case CAST: {
654 if (isa<StringRecTy>(getType())) {
655 if (StringInit *LHSs = dyn_cast<StringInit>(LHS))
656 return LHSs;
657
658 if (DefInit *LHSd = dyn_cast<DefInit>(LHS))
659 return StringInit::get(LHSd->getAsString());
660
661 if (IntInit *LHSi = dyn_cast<IntInit>(LHS))
662 return StringInit::get(LHSi->getAsString());
663 } else {
664 if (StringInit *LHSs = dyn_cast<StringInit>(LHS)) {
665 const std::string &Name = LHSs->getValue();
666
667 // From TGParser::ParseIDValue
668 if (CurRec) {
669 if (const RecordVal *RV = CurRec->getValue(Name)) {
670 if (RV->getType() != getType())
671 PrintFatalError("type mismatch in cast");
672 return VarInit::get(Name, RV->getType());
673 }
674
675 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name,
676 ":");
677
678 if (CurRec->isTemplateArg(TemplateArgName)) {
679 const RecordVal *RV = CurRec->getValue(TemplateArgName);
680 assert(RV && "Template arg doesn't exist??");
681
682 if (RV->getType() != getType())
683 PrintFatalError("type mismatch in cast");
684
685 return VarInit::get(TemplateArgName, RV->getType());
686 }
687 }
688
689 if (CurMultiClass) {
690 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
691 "::");
692
693 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
694 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
695 assert(RV && "Template arg doesn't exist??");
696
697 if (RV->getType() != getType())
698 PrintFatalError("type mismatch in cast");
699
700 return VarInit::get(MCName, RV->getType());
701 }
702 }
703 assert(CurRec && "NULL pointer");
704 if (Record *D = (CurRec->getRecords()).getDef(Name))
705 return DefInit::get(D);
706
707 PrintFatalError(CurRec->getLoc(),
708 "Undefined reference:'" + Name + "'\n");
709 }
710
711 if (isa<IntRecTy>(getType())) {
712 if (BitsInit *BI = dyn_cast<BitsInit>(LHS)) {
713 if (Init *NewInit = BI->convertInitializerTo(IntRecTy::get()))
714 return NewInit;
715 break;
716 }
717 }
718 }
719 break;
720 }
721 case HEAD: {
722 if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) {
723 assert(!LHSl->empty() && "Empty list in head");
724 return LHSl->getElement(0);
725 }
726 break;
727 }
728 case TAIL: {
729 if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) {
730 assert(!LHSl->empty() && "Empty list in tail");
731 // Note the +1. We can't just pass the result of getValues()
732 // directly.
733 return ListInit::get(LHSl->getValues().slice(1), LHSl->getType());
734 }
735 break;
736 }
737 case EMPTY: {
738 if (ListInit *LHSl = dyn_cast<ListInit>(LHS))
739 return IntInit::get(LHSl->empty());
740 if (StringInit *LHSs = dyn_cast<StringInit>(LHS))
741 return IntInit::get(LHSs->getValue().empty());
742
743 break;
744 }
745 }
746 return const_cast<UnOpInit *>(this);
747 }
748
resolveReferences(Record & R,const RecordVal * RV) const749 Init *UnOpInit::resolveReferences(Record &R, const RecordVal *RV) const {
750 Init *lhs = LHS->resolveReferences(R, RV);
751
752 if (LHS != lhs)
753 return (UnOpInit::get(getOpcode(), lhs, getType()))->Fold(&R, nullptr);
754 return Fold(&R, nullptr);
755 }
756
getAsString() const757 std::string UnOpInit::getAsString() const {
758 std::string Result;
759 switch (getOpcode()) {
760 case CAST: Result = "!cast<" + getType()->getAsString() + ">"; break;
761 case HEAD: Result = "!head"; break;
762 case TAIL: Result = "!tail"; break;
763 case EMPTY: Result = "!empty"; break;
764 }
765 return Result + "(" + LHS->getAsString() + ")";
766 }
767
768 static void
ProfileBinOpInit(FoldingSetNodeID & ID,unsigned Opcode,Init * LHS,Init * RHS,RecTy * Type)769 ProfileBinOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *LHS, Init *RHS,
770 RecTy *Type) {
771 ID.AddInteger(Opcode);
772 ID.AddPointer(LHS);
773 ID.AddPointer(RHS);
774 ID.AddPointer(Type);
775 }
776
get(BinaryOp Opc,Init * LHS,Init * RHS,RecTy * Type)777 BinOpInit *BinOpInit::get(BinaryOp Opc, Init *LHS,
778 Init *RHS, RecTy *Type) {
779 static FoldingSet<BinOpInit> ThePool;
780 static std::vector<std::unique_ptr<BinOpInit>> TheActualPool;
781
782 FoldingSetNodeID ID;
783 ProfileBinOpInit(ID, Opc, LHS, RHS, Type);
784
785 void *IP = nullptr;
786 if (BinOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
787 return I;
788
789 BinOpInit *I = new BinOpInit(Opc, LHS, RHS, Type);
790 ThePool.InsertNode(I, IP);
791 TheActualPool.push_back(std::unique_ptr<BinOpInit>(I));
792 return I;
793 }
794
Profile(FoldingSetNodeID & ID) const795 void BinOpInit::Profile(FoldingSetNodeID &ID) const {
796 ProfileBinOpInit(ID, getOpcode(), getLHS(), getRHS(), getType());
797 }
798
Fold(Record * CurRec,MultiClass * CurMultiClass) const799 Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const {
800 switch (getOpcode()) {
801 case CONCAT: {
802 DagInit *LHSs = dyn_cast<DagInit>(LHS);
803 DagInit *RHSs = dyn_cast<DagInit>(RHS);
804 if (LHSs && RHSs) {
805 DefInit *LOp = dyn_cast<DefInit>(LHSs->getOperator());
806 DefInit *ROp = dyn_cast<DefInit>(RHSs->getOperator());
807 if (!LOp || !ROp || LOp->getDef() != ROp->getDef())
808 PrintFatalError("Concated Dag operators do not match!");
809 std::vector<Init*> Args;
810 std::vector<std::string> ArgNames;
811 for (unsigned i = 0, e = LHSs->getNumArgs(); i != e; ++i) {
812 Args.push_back(LHSs->getArg(i));
813 ArgNames.push_back(LHSs->getArgName(i));
814 }
815 for (unsigned i = 0, e = RHSs->getNumArgs(); i != e; ++i) {
816 Args.push_back(RHSs->getArg(i));
817 ArgNames.push_back(RHSs->getArgName(i));
818 }
819 return DagInit::get(LHSs->getOperator(), "", Args, ArgNames);
820 }
821 break;
822 }
823 case LISTCONCAT: {
824 ListInit *LHSs = dyn_cast<ListInit>(LHS);
825 ListInit *RHSs = dyn_cast<ListInit>(RHS);
826 if (LHSs && RHSs) {
827 std::vector<Init *> Args;
828 Args.insert(Args.end(), LHSs->begin(), LHSs->end());
829 Args.insert(Args.end(), RHSs->begin(), RHSs->end());
830 return ListInit::get(
831 Args, cast<ListRecTy>(LHSs->getType())->getElementType());
832 }
833 break;
834 }
835 case STRCONCAT: {
836 StringInit *LHSs = dyn_cast<StringInit>(LHS);
837 StringInit *RHSs = dyn_cast<StringInit>(RHS);
838 if (LHSs && RHSs)
839 return StringInit::get(LHSs->getValue() + RHSs->getValue());
840 break;
841 }
842 case EQ: {
843 // try to fold eq comparison for 'bit' and 'int', otherwise fallback
844 // to string objects.
845 IntInit *L =
846 dyn_cast_or_null<IntInit>(LHS->convertInitializerTo(IntRecTy::get()));
847 IntInit *R =
848 dyn_cast_or_null<IntInit>(RHS->convertInitializerTo(IntRecTy::get()));
849
850 if (L && R)
851 return IntInit::get(L->getValue() == R->getValue());
852
853 StringInit *LHSs = dyn_cast<StringInit>(LHS);
854 StringInit *RHSs = dyn_cast<StringInit>(RHS);
855
856 // Make sure we've resolved
857 if (LHSs && RHSs)
858 return IntInit::get(LHSs->getValue() == RHSs->getValue());
859
860 break;
861 }
862 case ADD:
863 case AND:
864 case SHL:
865 case SRA:
866 case SRL: {
867 IntInit *LHSi =
868 dyn_cast_or_null<IntInit>(LHS->convertInitializerTo(IntRecTy::get()));
869 IntInit *RHSi =
870 dyn_cast_or_null<IntInit>(RHS->convertInitializerTo(IntRecTy::get()));
871 if (LHSi && RHSi) {
872 int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
873 int64_t Result;
874 switch (getOpcode()) {
875 default: llvm_unreachable("Bad opcode!");
876 case ADD: Result = LHSv + RHSv; break;
877 case AND: Result = LHSv & RHSv; break;
878 case SHL: Result = LHSv << RHSv; break;
879 case SRA: Result = LHSv >> RHSv; break;
880 case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break;
881 }
882 return IntInit::get(Result);
883 }
884 break;
885 }
886 }
887 return const_cast<BinOpInit *>(this);
888 }
889
resolveReferences(Record & R,const RecordVal * RV) const890 Init *BinOpInit::resolveReferences(Record &R, const RecordVal *RV) const {
891 Init *lhs = LHS->resolveReferences(R, RV);
892 Init *rhs = RHS->resolveReferences(R, RV);
893
894 if (LHS != lhs || RHS != rhs)
895 return (BinOpInit::get(getOpcode(), lhs, rhs, getType()))->Fold(&R,nullptr);
896 return Fold(&R, nullptr);
897 }
898
getAsString() const899 std::string BinOpInit::getAsString() const {
900 std::string Result;
901 switch (getOpcode()) {
902 case CONCAT: Result = "!con"; break;
903 case ADD: Result = "!add"; break;
904 case AND: Result = "!and"; break;
905 case SHL: Result = "!shl"; break;
906 case SRA: Result = "!sra"; break;
907 case SRL: Result = "!srl"; break;
908 case EQ: Result = "!eq"; break;
909 case LISTCONCAT: Result = "!listconcat"; break;
910 case STRCONCAT: Result = "!strconcat"; break;
911 }
912 return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")";
913 }
914
915 static void
ProfileTernOpInit(FoldingSetNodeID & ID,unsigned Opcode,Init * LHS,Init * MHS,Init * RHS,RecTy * Type)916 ProfileTernOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *LHS, Init *MHS,
917 Init *RHS, RecTy *Type) {
918 ID.AddInteger(Opcode);
919 ID.AddPointer(LHS);
920 ID.AddPointer(MHS);
921 ID.AddPointer(RHS);
922 ID.AddPointer(Type);
923 }
924
get(TernaryOp Opc,Init * LHS,Init * MHS,Init * RHS,RecTy * Type)925 TernOpInit *TernOpInit::get(TernaryOp Opc, Init *LHS, Init *MHS, Init *RHS,
926 RecTy *Type) {
927 static FoldingSet<TernOpInit> ThePool;
928 static std::vector<std::unique_ptr<TernOpInit>> TheActualPool;
929
930 FoldingSetNodeID ID;
931 ProfileTernOpInit(ID, Opc, LHS, MHS, RHS, Type);
932
933 void *IP = nullptr;
934 if (TernOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
935 return I;
936
937 TernOpInit *I = new TernOpInit(Opc, LHS, MHS, RHS, Type);
938 ThePool.InsertNode(I, IP);
939 TheActualPool.push_back(std::unique_ptr<TernOpInit>(I));
940 return I;
941 }
942
Profile(FoldingSetNodeID & ID) const943 void TernOpInit::Profile(FoldingSetNodeID &ID) const {
944 ProfileTernOpInit(ID, getOpcode(), getLHS(), getMHS(), getRHS(), getType());
945 }
946
947 static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
948 Record *CurRec, MultiClass *CurMultiClass);
949
EvaluateOperation(OpInit * RHSo,Init * LHS,Init * Arg,RecTy * Type,Record * CurRec,MultiClass * CurMultiClass)950 static Init *EvaluateOperation(OpInit *RHSo, Init *LHS, Init *Arg,
951 RecTy *Type, Record *CurRec,
952 MultiClass *CurMultiClass) {
953 // If this is a dag, recurse
954 if (auto *TArg = dyn_cast<TypedInit>(Arg))
955 if (isa<DagRecTy>(TArg->getType()))
956 return ForeachHelper(LHS, Arg, RHSo, Type, CurRec, CurMultiClass);
957
958 std::vector<Init *> NewOperands;
959 for (unsigned i = 0; i < RHSo->getNumOperands(); ++i) {
960 if (auto *RHSoo = dyn_cast<OpInit>(RHSo->getOperand(i))) {
961 if (Init *Result = EvaluateOperation(RHSoo, LHS, Arg,
962 Type, CurRec, CurMultiClass))
963 NewOperands.push_back(Result);
964 else
965 NewOperands.push_back(Arg);
966 } else if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
967 NewOperands.push_back(Arg);
968 } else {
969 NewOperands.push_back(RHSo->getOperand(i));
970 }
971 }
972
973 // Now run the operator and use its result as the new leaf
974 const OpInit *NewOp = RHSo->clone(NewOperands);
975 Init *NewVal = NewOp->Fold(CurRec, CurMultiClass);
976 return (NewVal != NewOp) ? NewVal : nullptr;
977 }
978
ForeachHelper(Init * LHS,Init * MHS,Init * RHS,RecTy * Type,Record * CurRec,MultiClass * CurMultiClass)979 static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
980 Record *CurRec, MultiClass *CurMultiClass) {
981
982 OpInit *RHSo = dyn_cast<OpInit>(RHS);
983
984 if (!RHSo)
985 PrintFatalError(CurRec->getLoc(), "!foreach requires an operator\n");
986
987 TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
988
989 if (!LHSt)
990 PrintFatalError(CurRec->getLoc(), "!foreach requires typed variable\n");
991
992 DagInit *MHSd = dyn_cast<DagInit>(MHS);
993 if (MHSd && isa<DagRecTy>(Type)) {
994 Init *Val = MHSd->getOperator();
995 if (Init *Result = EvaluateOperation(RHSo, LHS, Val,
996 Type, CurRec, CurMultiClass))
997 Val = Result;
998
999 std::vector<std::pair<Init *, std::string> > args;
1000 for (unsigned int i = 0; i < MHSd->getNumArgs(); ++i) {
1001 Init *Arg = MHSd->getArg(i);
1002 std::string ArgName = MHSd->getArgName(i);
1003
1004 // Process args
1005 if (Init *Result = EvaluateOperation(RHSo, LHS, Arg, Type,
1006 CurRec, CurMultiClass))
1007 Arg = Result;
1008
1009 // TODO: Process arg names
1010 args.push_back(std::make_pair(Arg, ArgName));
1011 }
1012
1013 return DagInit::get(Val, "", args);
1014 }
1015
1016 ListInit *MHSl = dyn_cast<ListInit>(MHS);
1017 if (MHSl && isa<ListRecTy>(Type)) {
1018 std::vector<Init *> NewOperands;
1019 std::vector<Init *> NewList(MHSl->begin(), MHSl->end());
1020
1021 for (Init *&Item : NewList) {
1022 NewOperands.clear();
1023 for(unsigned i = 0; i < RHSo->getNumOperands(); ++i) {
1024 // First, replace the foreach variable with the list item
1025 if (LHS->getAsString() == RHSo->getOperand(i)->getAsString())
1026 NewOperands.push_back(Item);
1027 else
1028 NewOperands.push_back(RHSo->getOperand(i));
1029 }
1030
1031 // Now run the operator and use its result as the new list item
1032 const OpInit *NewOp = RHSo->clone(NewOperands);
1033 Init *NewItem = NewOp->Fold(CurRec, CurMultiClass);
1034 if (NewItem != NewOp)
1035 Item = NewItem;
1036 }
1037 return ListInit::get(NewList, MHSl->getType());
1038 }
1039 return nullptr;
1040 }
1041
Fold(Record * CurRec,MultiClass * CurMultiClass) const1042 Init *TernOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const {
1043 switch (getOpcode()) {
1044 case SUBST: {
1045 DefInit *LHSd = dyn_cast<DefInit>(LHS);
1046 VarInit *LHSv = dyn_cast<VarInit>(LHS);
1047 StringInit *LHSs = dyn_cast<StringInit>(LHS);
1048
1049 DefInit *MHSd = dyn_cast<DefInit>(MHS);
1050 VarInit *MHSv = dyn_cast<VarInit>(MHS);
1051 StringInit *MHSs = dyn_cast<StringInit>(MHS);
1052
1053 DefInit *RHSd = dyn_cast<DefInit>(RHS);
1054 VarInit *RHSv = dyn_cast<VarInit>(RHS);
1055 StringInit *RHSs = dyn_cast<StringInit>(RHS);
1056
1057 if (LHSd && MHSd && RHSd) {
1058 Record *Val = RHSd->getDef();
1059 if (LHSd->getAsString() == RHSd->getAsString())
1060 Val = MHSd->getDef();
1061 return DefInit::get(Val);
1062 }
1063 if (LHSv && MHSv && RHSv) {
1064 std::string Val = RHSv->getName();
1065 if (LHSv->getAsString() == RHSv->getAsString())
1066 Val = MHSv->getName();
1067 return VarInit::get(Val, getType());
1068 }
1069 if (LHSs && MHSs && RHSs) {
1070 std::string Val = RHSs->getValue();
1071
1072 std::string::size_type found;
1073 std::string::size_type idx = 0;
1074 while (true) {
1075 found = Val.find(LHSs->getValue(), idx);
1076 if (found == std::string::npos)
1077 break;
1078 Val.replace(found, LHSs->getValue().size(), MHSs->getValue());
1079 idx = found + MHSs->getValue().size();
1080 }
1081
1082 return StringInit::get(Val);
1083 }
1084 break;
1085 }
1086
1087 case FOREACH: {
1088 if (Init *Result = ForeachHelper(LHS, MHS, RHS, getType(),
1089 CurRec, CurMultiClass))
1090 return Result;
1091 break;
1092 }
1093
1094 case IF: {
1095 IntInit *LHSi = dyn_cast<IntInit>(LHS);
1096 if (Init *I = LHS->convertInitializerTo(IntRecTy::get()))
1097 LHSi = dyn_cast<IntInit>(I);
1098 if (LHSi) {
1099 if (LHSi->getValue())
1100 return MHS;
1101 return RHS;
1102 }
1103 break;
1104 }
1105 }
1106
1107 return const_cast<TernOpInit *>(this);
1108 }
1109
resolveReferences(Record & R,const RecordVal * RV) const1110 Init *TernOpInit::resolveReferences(Record &R,
1111 const RecordVal *RV) const {
1112 Init *lhs = LHS->resolveReferences(R, RV);
1113
1114 if (getOpcode() == IF && lhs != LHS) {
1115 IntInit *Value = dyn_cast<IntInit>(lhs);
1116 if (Init *I = lhs->convertInitializerTo(IntRecTy::get()))
1117 Value = dyn_cast<IntInit>(I);
1118 if (Value) {
1119 // Short-circuit
1120 if (Value->getValue()) {
1121 Init *mhs = MHS->resolveReferences(R, RV);
1122 return (TernOpInit::get(getOpcode(), lhs, mhs,
1123 RHS, getType()))->Fold(&R, nullptr);
1124 }
1125 Init *rhs = RHS->resolveReferences(R, RV);
1126 return (TernOpInit::get(getOpcode(), lhs, MHS,
1127 rhs, getType()))->Fold(&R, nullptr);
1128 }
1129 }
1130
1131 Init *mhs = MHS->resolveReferences(R, RV);
1132 Init *rhs = RHS->resolveReferences(R, RV);
1133
1134 if (LHS != lhs || MHS != mhs || RHS != rhs)
1135 return (TernOpInit::get(getOpcode(), lhs, mhs, rhs,
1136 getType()))->Fold(&R, nullptr);
1137 return Fold(&R, nullptr);
1138 }
1139
getAsString() const1140 std::string TernOpInit::getAsString() const {
1141 std::string Result;
1142 switch (getOpcode()) {
1143 case SUBST: Result = "!subst"; break;
1144 case FOREACH: Result = "!foreach"; break;
1145 case IF: Result = "!if"; break;
1146 }
1147 return Result + "(" + LHS->getAsString() + ", " + MHS->getAsString() + ", " +
1148 RHS->getAsString() + ")";
1149 }
1150
getFieldType(const std::string & FieldName) const1151 RecTy *TypedInit::getFieldType(const std::string &FieldName) const {
1152 if (RecordRecTy *RecordType = dyn_cast<RecordRecTy>(getType()))
1153 if (RecordVal *Field = RecordType->getRecord()->getValue(FieldName))
1154 return Field->getType();
1155 return nullptr;
1156 }
1157
1158 Init *
convertInitializerTo(RecTy * Ty) const1159 TypedInit::convertInitializerTo(RecTy *Ty) const {
1160 if (isa<IntRecTy>(Ty)) {
1161 if (getType()->typeIsConvertibleTo(Ty))
1162 return const_cast<TypedInit *>(this);
1163 return nullptr;
1164 }
1165
1166 if (isa<StringRecTy>(Ty)) {
1167 if (isa<StringRecTy>(getType()))
1168 return const_cast<TypedInit *>(this);
1169 return nullptr;
1170 }
1171
1172 if (isa<CodeRecTy>(Ty)) {
1173 if (isa<CodeRecTy>(getType()))
1174 return const_cast<TypedInit *>(this);
1175 return nullptr;
1176 }
1177
1178 if (isa<BitRecTy>(Ty)) {
1179 // Accept variable if it is already of bit type!
1180 if (isa<BitRecTy>(getType()))
1181 return const_cast<TypedInit *>(this);
1182 if (auto *BitsTy = dyn_cast<BitsRecTy>(getType())) {
1183 // Accept only bits<1> expression.
1184 if (BitsTy->getNumBits() == 1)
1185 return const_cast<TypedInit *>(this);
1186 return nullptr;
1187 }
1188 // Ternary !if can be converted to bit, but only if both sides are
1189 // convertible to a bit.
1190 if (const auto *TOI = dyn_cast<TernOpInit>(this)) {
1191 if (TOI->getOpcode() == TernOpInit::TernaryOp::IF &&
1192 TOI->getMHS()->convertInitializerTo(BitRecTy::get()) &&
1193 TOI->getRHS()->convertInitializerTo(BitRecTy::get()))
1194 return const_cast<TypedInit *>(this);
1195 return nullptr;
1196 }
1197 return nullptr;
1198 }
1199
1200 if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
1201 if (BRT->getNumBits() == 1 && isa<BitRecTy>(getType()))
1202 return BitsInit::get(const_cast<TypedInit *>(this));
1203
1204 if (getType()->typeIsConvertibleTo(BRT)) {
1205 SmallVector<Init *, 16> NewBits(BRT->getNumBits());
1206
1207 for (unsigned i = 0; i != BRT->getNumBits(); ++i)
1208 NewBits[i] = VarBitInit::get(const_cast<TypedInit *>(this), i);
1209 return BitsInit::get(NewBits);
1210 }
1211
1212 return nullptr;
1213 }
1214
1215 if (auto *DLRT = dyn_cast<ListRecTy>(Ty)) {
1216 if (auto *SLRT = dyn_cast<ListRecTy>(getType()))
1217 if (SLRT->getElementType()->typeIsConvertibleTo(DLRT->getElementType()))
1218 return const_cast<TypedInit *>(this);
1219 return nullptr;
1220 }
1221
1222 if (auto *DRT = dyn_cast<DagRecTy>(Ty)) {
1223 if (getType()->typeIsConvertibleTo(DRT))
1224 return const_cast<TypedInit *>(this);
1225 return nullptr;
1226 }
1227
1228 if (auto *SRRT = dyn_cast<RecordRecTy>(Ty)) {
1229 // Ensure that this is compatible with Rec.
1230 if (RecordRecTy *DRRT = dyn_cast<RecordRecTy>(getType()))
1231 if (DRRT->getRecord()->isSubClassOf(SRRT->getRecord()) ||
1232 DRRT->getRecord() == SRRT->getRecord())
1233 return const_cast<TypedInit *>(this);
1234 return nullptr;
1235 }
1236
1237 return nullptr;
1238 }
1239
1240 Init *
convertInitializerBitRange(const std::vector<unsigned> & Bits) const1241 TypedInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) const {
1242 BitsRecTy *T = dyn_cast<BitsRecTy>(getType());
1243 if (!T) return nullptr; // Cannot subscript a non-bits variable.
1244 unsigned NumBits = T->getNumBits();
1245
1246 SmallVector<Init *, 16> NewBits(Bits.size());
1247 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
1248 if (Bits[i] >= NumBits)
1249 return nullptr;
1250
1251 NewBits[i] = VarBitInit::get(const_cast<TypedInit *>(this), Bits[i]);
1252 }
1253 return BitsInit::get(NewBits);
1254 }
1255
1256 Init *
convertInitListSlice(const std::vector<unsigned> & Elements) const1257 TypedInit::convertInitListSlice(const std::vector<unsigned> &Elements) const {
1258 ListRecTy *T = dyn_cast<ListRecTy>(getType());
1259 if (!T) return nullptr; // Cannot subscript a non-list variable.
1260
1261 if (Elements.size() == 1)
1262 return VarListElementInit::get(const_cast<TypedInit *>(this), Elements[0]);
1263
1264 std::vector<Init*> ListInits;
1265 ListInits.reserve(Elements.size());
1266 for (unsigned i = 0, e = Elements.size(); i != e; ++i)
1267 ListInits.push_back(VarListElementInit::get(const_cast<TypedInit *>(this),
1268 Elements[i]));
1269 return ListInit::get(ListInits, T);
1270 }
1271
1272
get(const std::string & VN,RecTy * T)1273 VarInit *VarInit::get(const std::string &VN, RecTy *T) {
1274 Init *Value = StringInit::get(VN);
1275 return VarInit::get(Value, T);
1276 }
1277
get(Init * VN,RecTy * T)1278 VarInit *VarInit::get(Init *VN, RecTy *T) {
1279 typedef std::pair<RecTy *, Init *> Key;
1280 static DenseMap<Key, std::unique_ptr<VarInit>> ThePool;
1281
1282 Key TheKey(std::make_pair(T, VN));
1283
1284 std::unique_ptr<VarInit> &I = ThePool[TheKey];
1285 if (!I) I.reset(new VarInit(VN, T));
1286 return I.get();
1287 }
1288
getName() const1289 const std::string &VarInit::getName() const {
1290 StringInit *NameString = cast<StringInit>(getNameInit());
1291 return NameString->getValue();
1292 }
1293
getBit(unsigned Bit) const1294 Init *VarInit::getBit(unsigned Bit) const {
1295 if (getType() == BitRecTy::get())
1296 return const_cast<VarInit*>(this);
1297 return VarBitInit::get(const_cast<VarInit*>(this), Bit);
1298 }
1299
resolveListElementReference(Record & R,const RecordVal * IRV,unsigned Elt) const1300 Init *VarInit::resolveListElementReference(Record &R,
1301 const RecordVal *IRV,
1302 unsigned Elt) const {
1303 if (R.isTemplateArg(getNameInit())) return nullptr;
1304 if (IRV && IRV->getNameInit() != getNameInit()) return nullptr;
1305
1306 RecordVal *RV = R.getValue(getNameInit());
1307 assert(RV && "Reference to a non-existent variable?");
1308 ListInit *LI = dyn_cast<ListInit>(RV->getValue());
1309 if (!LI)
1310 return VarListElementInit::get(cast<TypedInit>(RV->getValue()), Elt);
1311
1312 if (Elt >= LI->size())
1313 return nullptr; // Out of range reference.
1314 Init *E = LI->getElement(Elt);
1315 // If the element is set to some value, or if we are resolving a reference
1316 // to a specific variable and that variable is explicitly unset, then
1317 // replace the VarListElementInit with it.
1318 if (IRV || !isa<UnsetInit>(E))
1319 return E;
1320 return nullptr;
1321 }
1322
1323
getFieldType(const std::string & FieldName) const1324 RecTy *VarInit::getFieldType(const std::string &FieldName) const {
1325 if (RecordRecTy *RTy = dyn_cast<RecordRecTy>(getType()))
1326 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
1327 return RV->getType();
1328 return nullptr;
1329 }
1330
getFieldInit(Record & R,const RecordVal * RV,const std::string & FieldName) const1331 Init *VarInit::getFieldInit(Record &R, const RecordVal *RV,
1332 const std::string &FieldName) const {
1333 if (isa<RecordRecTy>(getType()))
1334 if (const RecordVal *Val = R.getValue(VarName)) {
1335 if (RV != Val && (RV || isa<UnsetInit>(Val->getValue())))
1336 return nullptr;
1337 Init *TheInit = Val->getValue();
1338 assert(TheInit != this && "Infinite loop detected!");
1339 if (Init *I = TheInit->getFieldInit(R, RV, FieldName))
1340 return I;
1341 return nullptr;
1342 }
1343 return nullptr;
1344 }
1345
resolveReferences(Record & R,const RecordVal * RV) const1346 Init *VarInit::resolveReferences(Record &R, const RecordVal *RV) const {
1347 if (RecordVal *Val = R.getValue(VarName))
1348 if (RV == Val || (!RV && !isa<UnsetInit>(Val->getValue())))
1349 return Val->getValue();
1350 return const_cast<VarInit *>(this);
1351 }
1352
get(TypedInit * T,unsigned B)1353 VarBitInit *VarBitInit::get(TypedInit *T, unsigned B) {
1354 typedef std::pair<TypedInit *, unsigned> Key;
1355 static DenseMap<Key, std::unique_ptr<VarBitInit>> ThePool;
1356
1357 Key TheKey(std::make_pair(T, B));
1358
1359 std::unique_ptr<VarBitInit> &I = ThePool[TheKey];
1360 if (!I) I.reset(new VarBitInit(T, B));
1361 return I.get();
1362 }
1363
convertInitializerTo(RecTy * Ty) const1364 Init *VarBitInit::convertInitializerTo(RecTy *Ty) const {
1365 if (isa<BitRecTy>(Ty))
1366 return const_cast<VarBitInit *>(this);
1367
1368 return nullptr;
1369 }
1370
getAsString() const1371 std::string VarBitInit::getAsString() const {
1372 return TI->getAsString() + "{" + utostr(Bit) + "}";
1373 }
1374
resolveReferences(Record & R,const RecordVal * RV) const1375 Init *VarBitInit::resolveReferences(Record &R, const RecordVal *RV) const {
1376 Init *I = TI->resolveReferences(R, RV);
1377 if (TI != I)
1378 return I->getBit(getBitNum());
1379
1380 return const_cast<VarBitInit*>(this);
1381 }
1382
get(TypedInit * T,unsigned E)1383 VarListElementInit *VarListElementInit::get(TypedInit *T,
1384 unsigned E) {
1385 typedef std::pair<TypedInit *, unsigned> Key;
1386 static DenseMap<Key, std::unique_ptr<VarListElementInit>> ThePool;
1387
1388 Key TheKey(std::make_pair(T, E));
1389
1390 std::unique_ptr<VarListElementInit> &I = ThePool[TheKey];
1391 if (!I) I.reset(new VarListElementInit(T, E));
1392 return I.get();
1393 }
1394
getAsString() const1395 std::string VarListElementInit::getAsString() const {
1396 return TI->getAsString() + "[" + utostr(Element) + "]";
1397 }
1398
1399 Init *
resolveReferences(Record & R,const RecordVal * RV) const1400 VarListElementInit::resolveReferences(Record &R, const RecordVal *RV) const {
1401 if (Init *I = getVariable()->resolveListElementReference(R, RV,
1402 getElementNum()))
1403 return I;
1404 return const_cast<VarListElementInit *>(this);
1405 }
1406
getBit(unsigned Bit) const1407 Init *VarListElementInit::getBit(unsigned Bit) const {
1408 if (getType() == BitRecTy::get())
1409 return const_cast<VarListElementInit*>(this);
1410 return VarBitInit::get(const_cast<VarListElementInit*>(this), Bit);
1411 }
1412
resolveListElementReference(Record & R,const RecordVal * RV,unsigned Elt) const1413 Init *VarListElementInit:: resolveListElementReference(Record &R,
1414 const RecordVal *RV,
1415 unsigned Elt) const {
1416 if (Init *Result = TI->resolveListElementReference(R, RV, Element)) {
1417 if (TypedInit *TInit = dyn_cast<TypedInit>(Result)) {
1418 if (Init *Result2 = TInit->resolveListElementReference(R, RV, Elt))
1419 return Result2;
1420 return VarListElementInit::get(TInit, Elt);
1421 }
1422 return Result;
1423 }
1424
1425 return nullptr;
1426 }
1427
get(Record * R)1428 DefInit *DefInit::get(Record *R) {
1429 return R->getDefInit();
1430 }
1431
convertInitializerTo(RecTy * Ty) const1432 Init *DefInit::convertInitializerTo(RecTy *Ty) const {
1433 if (auto *RRT = dyn_cast<RecordRecTy>(Ty))
1434 if (getDef()->isSubClassOf(RRT->getRecord()))
1435 return const_cast<DefInit *>(this);
1436 return nullptr;
1437 }
1438
getFieldType(const std::string & FieldName) const1439 RecTy *DefInit::getFieldType(const std::string &FieldName) const {
1440 if (const RecordVal *RV = Def->getValue(FieldName))
1441 return RV->getType();
1442 return nullptr;
1443 }
1444
getFieldInit(Record & R,const RecordVal * RV,const std::string & FieldName) const1445 Init *DefInit::getFieldInit(Record &R, const RecordVal *RV,
1446 const std::string &FieldName) const {
1447 return Def->getValue(FieldName)->getValue();
1448 }
1449
1450
getAsString() const1451 std::string DefInit::getAsString() const {
1452 return Def->getName();
1453 }
1454
get(Init * R,const std::string & FN)1455 FieldInit *FieldInit::get(Init *R, const std::string &FN) {
1456 typedef std::pair<Init *, TableGenStringKey> Key;
1457 static DenseMap<Key, std::unique_ptr<FieldInit>> ThePool;
1458
1459 Key TheKey(std::make_pair(R, FN));
1460
1461 std::unique_ptr<FieldInit> &I = ThePool[TheKey];
1462 if (!I) I.reset(new FieldInit(R, FN));
1463 return I.get();
1464 }
1465
getBit(unsigned Bit) const1466 Init *FieldInit::getBit(unsigned Bit) const {
1467 if (getType() == BitRecTy::get())
1468 return const_cast<FieldInit*>(this);
1469 return VarBitInit::get(const_cast<FieldInit*>(this), Bit);
1470 }
1471
resolveListElementReference(Record & R,const RecordVal * RV,unsigned Elt) const1472 Init *FieldInit::resolveListElementReference(Record &R, const RecordVal *RV,
1473 unsigned Elt) const {
1474 if (Init *ListVal = Rec->getFieldInit(R, RV, FieldName))
1475 if (ListInit *LI = dyn_cast<ListInit>(ListVal)) {
1476 if (Elt >= LI->size()) return nullptr;
1477 Init *E = LI->getElement(Elt);
1478
1479 // If the element is set to some value, or if we are resolving a
1480 // reference to a specific variable and that variable is explicitly
1481 // unset, then replace the VarListElementInit with it.
1482 if (RV || !isa<UnsetInit>(E))
1483 return E;
1484 }
1485 return nullptr;
1486 }
1487
resolveReferences(Record & R,const RecordVal * RV) const1488 Init *FieldInit::resolveReferences(Record &R, const RecordVal *RV) const {
1489 Init *NewRec = RV ? Rec->resolveReferences(R, RV) : Rec;
1490
1491 if (Init *BitsVal = NewRec->getFieldInit(R, RV, FieldName)) {
1492 Init *BVR = BitsVal->resolveReferences(R, RV);
1493 return BVR->isComplete() ? BVR : const_cast<FieldInit *>(this);
1494 }
1495
1496 if (NewRec != Rec)
1497 return FieldInit::get(NewRec, FieldName);
1498 return const_cast<FieldInit *>(this);
1499 }
1500
ProfileDagInit(FoldingSetNodeID & ID,Init * V,const std::string & VN,ArrayRef<Init * > ArgRange,ArrayRef<std::string> NameRange)1501 static void ProfileDagInit(FoldingSetNodeID &ID, Init *V, const std::string &VN,
1502 ArrayRef<Init *> ArgRange,
1503 ArrayRef<std::string> NameRange) {
1504 ID.AddPointer(V);
1505 ID.AddString(VN);
1506
1507 ArrayRef<Init *>::iterator Arg = ArgRange.begin();
1508 ArrayRef<std::string>::iterator Name = NameRange.begin();
1509 while (Arg != ArgRange.end()) {
1510 assert(Name != NameRange.end() && "Arg name underflow!");
1511 ID.AddPointer(*Arg++);
1512 ID.AddString(*Name++);
1513 }
1514 assert(Name == NameRange.end() && "Arg name overflow!");
1515 }
1516
1517 DagInit *
get(Init * V,const std::string & VN,ArrayRef<Init * > ArgRange,ArrayRef<std::string> NameRange)1518 DagInit::get(Init *V, const std::string &VN,
1519 ArrayRef<Init *> ArgRange,
1520 ArrayRef<std::string> NameRange) {
1521 static FoldingSet<DagInit> ThePool;
1522 static std::vector<std::unique_ptr<DagInit>> TheActualPool;
1523
1524 FoldingSetNodeID ID;
1525 ProfileDagInit(ID, V, VN, ArgRange, NameRange);
1526
1527 void *IP = nullptr;
1528 if (DagInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
1529 return I;
1530
1531 DagInit *I = new DagInit(V, VN, ArgRange, NameRange);
1532 ThePool.InsertNode(I, IP);
1533 TheActualPool.push_back(std::unique_ptr<DagInit>(I));
1534 return I;
1535 }
1536
1537 DagInit *
get(Init * V,const std::string & VN,const std::vector<std::pair<Init *,std::string>> & args)1538 DagInit::get(Init *V, const std::string &VN,
1539 const std::vector<std::pair<Init*, std::string> > &args) {
1540 std::vector<Init *> Args;
1541 std::vector<std::string> Names;
1542
1543 for (const auto &Arg : args) {
1544 Args.push_back(Arg.first);
1545 Names.push_back(Arg.second);
1546 }
1547
1548 return DagInit::get(V, VN, Args, Names);
1549 }
1550
Profile(FoldingSetNodeID & ID) const1551 void DagInit::Profile(FoldingSetNodeID &ID) const {
1552 ProfileDagInit(ID, Val, ValName, Args, ArgNames);
1553 }
1554
convertInitializerTo(RecTy * Ty) const1555 Init *DagInit::convertInitializerTo(RecTy *Ty) const {
1556 if (isa<DagRecTy>(Ty))
1557 return const_cast<DagInit *>(this);
1558
1559 return nullptr;
1560 }
1561
resolveReferences(Record & R,const RecordVal * RV) const1562 Init *DagInit::resolveReferences(Record &R, const RecordVal *RV) const {
1563 std::vector<Init*> NewArgs;
1564 for (unsigned i = 0, e = Args.size(); i != e; ++i)
1565 NewArgs.push_back(Args[i]->resolveReferences(R, RV));
1566
1567 Init *Op = Val->resolveReferences(R, RV);
1568
1569 if (Args != NewArgs || Op != Val)
1570 return DagInit::get(Op, ValName, NewArgs, ArgNames);
1571
1572 return const_cast<DagInit *>(this);
1573 }
1574
1575
getAsString() const1576 std::string DagInit::getAsString() const {
1577 std::string Result = "(" + Val->getAsString();
1578 if (!ValName.empty())
1579 Result += ":" + ValName;
1580 if (!Args.empty()) {
1581 Result += " " + Args[0]->getAsString();
1582 if (!ArgNames[0].empty()) Result += ":$" + ArgNames[0];
1583 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
1584 Result += ", " + Args[i]->getAsString();
1585 if (!ArgNames[i].empty()) Result += ":$" + ArgNames[i];
1586 }
1587 }
1588 return Result + ")";
1589 }
1590
1591
1592 //===----------------------------------------------------------------------===//
1593 // Other implementations
1594 //===----------------------------------------------------------------------===//
1595
RecordVal(Init * N,RecTy * T,bool P)1596 RecordVal::RecordVal(Init *N, RecTy *T, bool P)
1597 : NameAndPrefix(N, P), Ty(T) {
1598 Value = UnsetInit::get()->convertInitializerTo(Ty);
1599 assert(Value && "Cannot create unset value for current type!");
1600 }
1601
RecordVal(const std::string & N,RecTy * T,bool P)1602 RecordVal::RecordVal(const std::string &N, RecTy *T, bool P)
1603 : NameAndPrefix(StringInit::get(N), P), Ty(T) {
1604 Value = UnsetInit::get()->convertInitializerTo(Ty);
1605 assert(Value && "Cannot create unset value for current type!");
1606 }
1607
getName() const1608 const std::string &RecordVal::getName() const {
1609 return cast<StringInit>(getNameInit())->getValue();
1610 }
1611
dump() const1612 LLVM_DUMP_METHOD void RecordVal::dump() const { errs() << *this; }
1613
print(raw_ostream & OS,bool PrintSem) const1614 void RecordVal::print(raw_ostream &OS, bool PrintSem) const {
1615 if (getPrefix()) OS << "field ";
1616 OS << *getType() << " " << getNameInitAsString();
1617
1618 if (getValue())
1619 OS << " = " << *getValue();
1620
1621 if (PrintSem) OS << ";\n";
1622 }
1623
1624 unsigned Record::LastID = 0;
1625
init()1626 void Record::init() {
1627 checkName();
1628
1629 // Every record potentially has a def at the top. This value is
1630 // replaced with the top-level def name at instantiation time.
1631 RecordVal DN("NAME", StringRecTy::get(), 0);
1632 addValue(DN);
1633 }
1634
checkName()1635 void Record::checkName() {
1636 // Ensure the record name has string type.
1637 const TypedInit *TypedName = cast<const TypedInit>(Name);
1638 if (!isa<StringRecTy>(TypedName->getType()))
1639 PrintFatalError(getLoc(), "Record name is not a string!");
1640 }
1641
getDefInit()1642 DefInit *Record::getDefInit() {
1643 if (!TheInit)
1644 TheInit.reset(new DefInit(this, new RecordRecTy(this)));
1645 return TheInit.get();
1646 }
1647
getName() const1648 const std::string &Record::getName() const {
1649 return cast<StringInit>(Name)->getValue();
1650 }
1651
setName(Init * NewName)1652 void Record::setName(Init *NewName) {
1653 Name = NewName;
1654 checkName();
1655 // DO NOT resolve record values to the name at this point because
1656 // there might be default values for arguments of this def. Those
1657 // arguments might not have been resolved yet so we don't want to
1658 // prematurely assume values for those arguments were not passed to
1659 // this def.
1660 //
1661 // Nonetheless, it may be that some of this Record's values
1662 // reference the record name. Indeed, the reason for having the
1663 // record name be an Init is to provide this flexibility. The extra
1664 // resolve steps after completely instantiating defs takes care of
1665 // this. See TGParser::ParseDef and TGParser::ParseDefm.
1666 }
1667
setName(const std::string & Name)1668 void Record::setName(const std::string &Name) {
1669 setName(StringInit::get(Name));
1670 }
1671
resolveReferencesTo(const RecordVal * RV)1672 void Record::resolveReferencesTo(const RecordVal *RV) {
1673 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
1674 if (RV == &Values[i]) // Skip resolve the same field as the given one
1675 continue;
1676 if (Init *V = Values[i].getValue())
1677 if (Values[i].setValue(V->resolveReferences(*this, RV)))
1678 PrintFatalError(getLoc(), "Invalid value is found when setting '" +
1679 Values[i].getNameInitAsString() +
1680 "' after resolving references" +
1681 (RV ? " against '" + RV->getNameInitAsString() +
1682 "' of (" + RV->getValue()->getAsUnquotedString() +
1683 ")"
1684 : "") + "\n");
1685 }
1686 Init *OldName = getNameInit();
1687 Init *NewName = Name->resolveReferences(*this, RV);
1688 if (NewName != OldName) {
1689 // Re-register with RecordKeeper.
1690 setName(NewName);
1691 }
1692 }
1693
dump() const1694 LLVM_DUMP_METHOD void Record::dump() const { errs() << *this; }
1695
operator <<(raw_ostream & OS,const Record & R)1696 raw_ostream &llvm::operator<<(raw_ostream &OS, const Record &R) {
1697 OS << R.getNameInitAsString();
1698
1699 ArrayRef<Init *> TArgs = R.getTemplateArgs();
1700 if (!TArgs.empty()) {
1701 OS << "<";
1702 bool NeedComma = false;
1703 for (const Init *TA : TArgs) {
1704 if (NeedComma) OS << ", ";
1705 NeedComma = true;
1706 const RecordVal *RV = R.getValue(TA);
1707 assert(RV && "Template argument record not found??");
1708 RV->print(OS, false);
1709 }
1710 OS << ">";
1711 }
1712
1713 OS << " {";
1714 ArrayRef<std::pair<Record *, SMRange>> SC = R.getSuperClasses();
1715 if (!SC.empty()) {
1716 OS << "\t//";
1717 for (const auto &SuperPair : SC)
1718 OS << " " << SuperPair.first->getNameInitAsString();
1719 }
1720 OS << "\n";
1721
1722 for (const RecordVal &Val : R.getValues())
1723 if (Val.getPrefix() && !R.isTemplateArg(Val.getName()))
1724 OS << Val;
1725 for (const RecordVal &Val : R.getValues())
1726 if (!Val.getPrefix() && !R.isTemplateArg(Val.getName()))
1727 OS << Val;
1728
1729 return OS << "}\n";
1730 }
1731
getValueInit(StringRef FieldName) const1732 Init *Record::getValueInit(StringRef FieldName) const {
1733 const RecordVal *R = getValue(FieldName);
1734 if (!R || !R->getValue())
1735 PrintFatalError(getLoc(), "Record `" + getName() +
1736 "' does not have a field named `" + FieldName + "'!\n");
1737 return R->getValue();
1738 }
1739
1740
getValueAsString(StringRef FieldName) const1741 std::string Record::getValueAsString(StringRef FieldName) const {
1742 const RecordVal *R = getValue(FieldName);
1743 if (!R || !R->getValue())
1744 PrintFatalError(getLoc(), "Record `" + getName() +
1745 "' does not have a field named `" + FieldName + "'!\n");
1746
1747 if (StringInit *SI = dyn_cast<StringInit>(R->getValue()))
1748 return SI->getValue();
1749 if (CodeInit *CI = dyn_cast<CodeInit>(R->getValue()))
1750 return CI->getValue();
1751
1752 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1753 FieldName + "' does not have a string initializer!");
1754 }
1755
getValueAsBitsInit(StringRef FieldName) const1756 BitsInit *Record::getValueAsBitsInit(StringRef FieldName) const {
1757 const RecordVal *R = getValue(FieldName);
1758 if (!R || !R->getValue())
1759 PrintFatalError(getLoc(), "Record `" + getName() +
1760 "' does not have a field named `" + FieldName + "'!\n");
1761
1762 if (BitsInit *BI = dyn_cast<BitsInit>(R->getValue()))
1763 return BI;
1764 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1765 FieldName + "' does not have a BitsInit initializer!");
1766 }
1767
getValueAsListInit(StringRef FieldName) const1768 ListInit *Record::getValueAsListInit(StringRef FieldName) const {
1769 const RecordVal *R = getValue(FieldName);
1770 if (!R || !R->getValue())
1771 PrintFatalError(getLoc(), "Record `" + getName() +
1772 "' does not have a field named `" + FieldName + "'!\n");
1773
1774 if (ListInit *LI = dyn_cast<ListInit>(R->getValue()))
1775 return LI;
1776 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1777 FieldName + "' does not have a list initializer!");
1778 }
1779
1780 std::vector<Record*>
getValueAsListOfDefs(StringRef FieldName) const1781 Record::getValueAsListOfDefs(StringRef FieldName) const {
1782 ListInit *List = getValueAsListInit(FieldName);
1783 std::vector<Record*> Defs;
1784 for (Init *I : List->getValues()) {
1785 if (DefInit *DI = dyn_cast<DefInit>(I))
1786 Defs.push_back(DI->getDef());
1787 else
1788 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1789 FieldName + "' list is not entirely DefInit!");
1790 }
1791 return Defs;
1792 }
1793
getValueAsInt(StringRef FieldName) const1794 int64_t Record::getValueAsInt(StringRef FieldName) const {
1795 const RecordVal *R = getValue(FieldName);
1796 if (!R || !R->getValue())
1797 PrintFatalError(getLoc(), "Record `" + getName() +
1798 "' does not have a field named `" + FieldName + "'!\n");
1799
1800 if (IntInit *II = dyn_cast<IntInit>(R->getValue()))
1801 return II->getValue();
1802 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1803 FieldName + "' does not have an int initializer!");
1804 }
1805
1806 std::vector<int64_t>
getValueAsListOfInts(StringRef FieldName) const1807 Record::getValueAsListOfInts(StringRef FieldName) const {
1808 ListInit *List = getValueAsListInit(FieldName);
1809 std::vector<int64_t> Ints;
1810 for (Init *I : List->getValues()) {
1811 if (IntInit *II = dyn_cast<IntInit>(I))
1812 Ints.push_back(II->getValue());
1813 else
1814 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1815 FieldName + "' does not have a list of ints initializer!");
1816 }
1817 return Ints;
1818 }
1819
1820 std::vector<std::string>
getValueAsListOfStrings(StringRef FieldName) const1821 Record::getValueAsListOfStrings(StringRef FieldName) const {
1822 ListInit *List = getValueAsListInit(FieldName);
1823 std::vector<std::string> Strings;
1824 for (Init *I : List->getValues()) {
1825 if (StringInit *SI = dyn_cast<StringInit>(I))
1826 Strings.push_back(SI->getValue());
1827 else
1828 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1829 FieldName + "' does not have a list of strings initializer!");
1830 }
1831 return Strings;
1832 }
1833
getValueAsDef(StringRef FieldName) const1834 Record *Record::getValueAsDef(StringRef FieldName) const {
1835 const RecordVal *R = getValue(FieldName);
1836 if (!R || !R->getValue())
1837 PrintFatalError(getLoc(), "Record `" + getName() +
1838 "' does not have a field named `" + FieldName + "'!\n");
1839
1840 if (DefInit *DI = dyn_cast<DefInit>(R->getValue()))
1841 return DI->getDef();
1842 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1843 FieldName + "' does not have a def initializer!");
1844 }
1845
getValueAsBit(StringRef FieldName) const1846 bool Record::getValueAsBit(StringRef FieldName) const {
1847 const RecordVal *R = getValue(FieldName);
1848 if (!R || !R->getValue())
1849 PrintFatalError(getLoc(), "Record `" + getName() +
1850 "' does not have a field named `" + FieldName + "'!\n");
1851
1852 if (BitInit *BI = dyn_cast<BitInit>(R->getValue()))
1853 return BI->getValue();
1854 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1855 FieldName + "' does not have a bit initializer!");
1856 }
1857
getValueAsBitOrUnset(StringRef FieldName,bool & Unset) const1858 bool Record::getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const {
1859 const RecordVal *R = getValue(FieldName);
1860 if (!R || !R->getValue())
1861 PrintFatalError(getLoc(), "Record `" + getName() +
1862 "' does not have a field named `" + FieldName.str() + "'!\n");
1863
1864 if (isa<UnsetInit>(R->getValue())) {
1865 Unset = true;
1866 return false;
1867 }
1868 Unset = false;
1869 if (BitInit *BI = dyn_cast<BitInit>(R->getValue()))
1870 return BI->getValue();
1871 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1872 FieldName + "' does not have a bit initializer!");
1873 }
1874
getValueAsDag(StringRef FieldName) const1875 DagInit *Record::getValueAsDag(StringRef FieldName) const {
1876 const RecordVal *R = getValue(FieldName);
1877 if (!R || !R->getValue())
1878 PrintFatalError(getLoc(), "Record `" + getName() +
1879 "' does not have a field named `" + FieldName + "'!\n");
1880
1881 if (DagInit *DI = dyn_cast<DagInit>(R->getValue()))
1882 return DI;
1883 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1884 FieldName + "' does not have a dag initializer!");
1885 }
1886
1887
dump() const1888 LLVM_DUMP_METHOD void MultiClass::dump() const {
1889 errs() << "Record:\n";
1890 Rec.dump();
1891
1892 errs() << "Defs:\n";
1893 for (const auto &Proto : DefPrototypes)
1894 Proto->dump();
1895 }
1896
1897
dump() const1898 LLVM_DUMP_METHOD void RecordKeeper::dump() const { errs() << *this; }
1899
operator <<(raw_ostream & OS,const RecordKeeper & RK)1900 raw_ostream &llvm::operator<<(raw_ostream &OS, const RecordKeeper &RK) {
1901 OS << "------------- Classes -----------------\n";
1902 for (const auto &C : RK.getClasses())
1903 OS << "class " << *C.second;
1904
1905 OS << "------------- Defs -----------------\n";
1906 for (const auto &D : RK.getDefs())
1907 OS << "def " << *D.second;
1908 return OS;
1909 }
1910
1911 std::vector<Record *>
getAllDerivedDefinitions(const std::string & ClassName) const1912 RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
1913 Record *Class = getClass(ClassName);
1914 if (!Class)
1915 PrintFatalError("ERROR: Couldn't find the `" + ClassName + "' class!\n");
1916
1917 std::vector<Record*> Defs;
1918 for (const auto &D : getDefs())
1919 if (D.second->isSubClassOf(Class))
1920 Defs.push_back(D.second.get());
1921
1922 return Defs;
1923 }
1924
QualifyName(Record & CurRec,MultiClass * CurMultiClass,Init * Name,const std::string & Scoper)1925 Init *llvm::QualifyName(Record &CurRec, MultiClass *CurMultiClass,
1926 Init *Name, const std::string &Scoper) {
1927 RecTy *Type = cast<TypedInit>(Name)->getType();
1928
1929 BinOpInit *NewName =
1930 BinOpInit::get(BinOpInit::STRCONCAT,
1931 BinOpInit::get(BinOpInit::STRCONCAT,
1932 CurRec.getNameInit(),
1933 StringInit::get(Scoper),
1934 Type)->Fold(&CurRec, CurMultiClass),
1935 Name,
1936 Type);
1937
1938 if (CurMultiClass && Scoper != "::") {
1939 NewName =
1940 BinOpInit::get(BinOpInit::STRCONCAT,
1941 BinOpInit::get(BinOpInit::STRCONCAT,
1942 CurMultiClass->Rec.getNameInit(),
1943 StringInit::get("::"),
1944 Type)->Fold(&CurRec, CurMultiClass),
1945 NewName->Fold(&CurRec, CurMultiClass),
1946 Type);
1947 }
1948
1949 return NewName->Fold(&CurRec, CurMultiClass);
1950 }
1951
QualifyName(Record & CurRec,MultiClass * CurMultiClass,const std::string & Name,const std::string & Scoper)1952 Init *llvm::QualifyName(Record &CurRec, MultiClass *CurMultiClass,
1953 const std::string &Name,
1954 const std::string &Scoper) {
1955 return QualifyName(CurRec, CurMultiClass, StringInit::get(Name), Scoper);
1956 }
1957