-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathRecord.cpp
3334 lines (2870 loc) · 101 KB
/
Record.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//===- Record.cpp - Record implementation ---------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Implement the tablegen record classes.
//
//===----------------------------------------------------------------------===//
#include "llvm/TableGen/Record.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/FoldingSet.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Config/llvm-config.h"
#include "llvm/Support/Allocator.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/SMLoc.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/TableGen/Error.h"
#include <cassert>
#include <cstdint>
#include <map>
#include <memory>
#include <string>
#include <utility>
#include <vector>
using namespace llvm;
#define DEBUG_TYPE "tblgen-records"
//===----------------------------------------------------------------------===//
// Context
//===----------------------------------------------------------------------===//
namespace llvm {
namespace detail {
/// This class represents the internal implementation of the RecordKeeper.
/// It contains all of the contextual static state of the Record classes. It is
/// kept out-of-line to simplify dependencies, and also make it easier for
/// internal classes to access the uniquer state of the keeper.
struct RecordKeeperImpl {
RecordKeeperImpl(RecordKeeper &RK)
: SharedBitRecTy(RK), SharedIntRecTy(RK), SharedStringRecTy(RK),
SharedDagRecTy(RK), AnyRecord(RK, 0), TheUnsetInit(RK),
TrueBitInit(true, &SharedBitRecTy),
FalseBitInit(false, &SharedBitRecTy), StringInitStringPool(Allocator),
StringInitCodePool(Allocator), AnonCounter(0), LastRecordID(0) {}
BumpPtrAllocator Allocator;
std::vector<BitsRecTy *> SharedBitsRecTys;
BitRecTy SharedBitRecTy;
IntRecTy SharedIntRecTy;
StringRecTy SharedStringRecTy;
DagRecTy SharedDagRecTy;
RecordRecTy AnyRecord;
UnsetInit TheUnsetInit;
BitInit TrueBitInit;
BitInit FalseBitInit;
FoldingSet<ArgumentInit> TheArgumentInitPool;
FoldingSet<BitsInit> TheBitsInitPool;
std::map<int64_t, IntInit *> TheIntInitPool;
StringMap<StringInit *, BumpPtrAllocator &> StringInitStringPool;
StringMap<StringInit *, BumpPtrAllocator &> StringInitCodePool;
FoldingSet<ListInit> TheListInitPool;
FoldingSet<UnOpInit> TheUnOpInitPool;
FoldingSet<BinOpInit> TheBinOpInitPool;
FoldingSet<TernOpInit> TheTernOpInitPool;
FoldingSet<FoldOpInit> TheFoldOpInitPool;
FoldingSet<IsAOpInit> TheIsAOpInitPool;
FoldingSet<ExistsOpInit> TheExistsOpInitPool;
DenseMap<std::pair<RecTy *, Init *>, VarInit *> TheVarInitPool;
DenseMap<std::pair<TypedInit *, unsigned>, VarBitInit *> TheVarBitInitPool;
FoldingSet<VarDefInit> TheVarDefInitPool;
DenseMap<std::pair<Init *, StringInit *>, FieldInit *> TheFieldInitPool;
FoldingSet<CondOpInit> TheCondOpInitPool;
FoldingSet<DagInit> TheDagInitPool;
FoldingSet<RecordRecTy> RecordTypePool;
unsigned AnonCounter;
unsigned LastRecordID;
};
} // namespace detail
} // namespace llvm
//===----------------------------------------------------------------------===//
// Type implementations
//===----------------------------------------------------------------------===//
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
LLVM_DUMP_METHOD void RecTy::dump() const { print(errs()); }
#endif
ListRecTy *RecTy::getListTy() {
if (!ListTy)
ListTy = new (RK.getImpl().Allocator) ListRecTy(this);
return ListTy;
}
bool RecTy::typeIsConvertibleTo(const RecTy *RHS) const {
assert(RHS && "NULL pointer");
return Kind == RHS->getRecTyKind();
}
bool RecTy::typeIsA(const RecTy *RHS) const { return this == RHS; }
BitRecTy *BitRecTy::get(RecordKeeper &RK) {
return &RK.getImpl().SharedBitRecTy;
}
bool BitRecTy::typeIsConvertibleTo(const RecTy *RHS) const{
if (RecTy::typeIsConvertibleTo(RHS) || RHS->getRecTyKind() == IntRecTyKind)
return true;
if (const BitsRecTy *BitsTy = dyn_cast<BitsRecTy>(RHS))
return BitsTy->getNumBits() == 1;
return false;
}
BitsRecTy *BitsRecTy::get(RecordKeeper &RK, unsigned Sz) {
detail::RecordKeeperImpl &RKImpl = RK.getImpl();
if (Sz >= RKImpl.SharedBitsRecTys.size())
RKImpl.SharedBitsRecTys.resize(Sz + 1);
BitsRecTy *&Ty = RKImpl.SharedBitsRecTys[Sz];
if (!Ty)
Ty = new (RKImpl.Allocator) BitsRecTy(RK, Sz);
return Ty;
}
std::string BitsRecTy::getAsString() const {
return "bits<" + utostr(Size) + ">";
}
bool BitsRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
if (RecTy::typeIsConvertibleTo(RHS)) //argument and the sender are same type
return cast<BitsRecTy>(RHS)->Size == Size;
RecTyKind kind = RHS->getRecTyKind();
return (kind == BitRecTyKind && Size == 1) || (kind == IntRecTyKind);
}
IntRecTy *IntRecTy::get(RecordKeeper &RK) {
return &RK.getImpl().SharedIntRecTy;
}
bool IntRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
RecTyKind kind = RHS->getRecTyKind();
return kind==BitRecTyKind || kind==BitsRecTyKind || kind==IntRecTyKind;
}
StringRecTy *StringRecTy::get(RecordKeeper &RK) {
return &RK.getImpl().SharedStringRecTy;
}
std::string StringRecTy::getAsString() const {
return "string";
}
bool StringRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
RecTyKind Kind = RHS->getRecTyKind();
return Kind == StringRecTyKind;
}
std::string ListRecTy::getAsString() const {
return "list<" + ElementTy->getAsString() + ">";
}
bool ListRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
if (const auto *ListTy = dyn_cast<ListRecTy>(RHS))
return ElementTy->typeIsConvertibleTo(ListTy->getElementType());
return false;
}
bool ListRecTy::typeIsA(const RecTy *RHS) const {
if (const ListRecTy *RHSl = dyn_cast<ListRecTy>(RHS))
return getElementType()->typeIsA(RHSl->getElementType());
return false;
}
DagRecTy *DagRecTy::get(RecordKeeper &RK) {
return &RK.getImpl().SharedDagRecTy;
}
std::string DagRecTy::getAsString() const {
return "dag";
}
static void ProfileRecordRecTy(FoldingSetNodeID &ID,
ArrayRef<Record *> Classes) {
ID.AddInteger(Classes.size());
for (Record *R : Classes)
ID.AddPointer(R);
}
RecordRecTy *RecordRecTy::get(RecordKeeper &RK,
ArrayRef<Record *> UnsortedClasses) {
detail::RecordKeeperImpl &RKImpl = RK.getImpl();
if (UnsortedClasses.empty())
return &RKImpl.AnyRecord;
FoldingSet<RecordRecTy> &ThePool = RKImpl.RecordTypePool;
SmallVector<Record *, 4> Classes(UnsortedClasses.begin(),
UnsortedClasses.end());
llvm::sort(Classes, [](Record *LHS, Record *RHS) {
return LHS->getNameInitAsString() < RHS->getNameInitAsString();
});
FoldingSetNodeID ID;
ProfileRecordRecTy(ID, Classes);
void *IP = nullptr;
if (RecordRecTy *Ty = ThePool.FindNodeOrInsertPos(ID, IP))
return Ty;
#ifndef NDEBUG
// Check for redundancy.
for (unsigned i = 0; i < Classes.size(); ++i) {
for (unsigned j = 0; j < Classes.size(); ++j) {
assert(i == j || !Classes[i]->isSubClassOf(Classes[j]));
}
assert(&Classes[0]->getRecords() == &Classes[i]->getRecords());
}
#endif
void *Mem = RKImpl.Allocator.Allocate(
totalSizeToAlloc<Record *>(Classes.size()), alignof(RecordRecTy));
RecordRecTy *Ty = new (Mem) RecordRecTy(RK, Classes.size());
std::uninitialized_copy(Classes.begin(), Classes.end(),
Ty->getTrailingObjects<Record *>());
ThePool.InsertNode(Ty, IP);
return Ty;
}
RecordRecTy *RecordRecTy::get(Record *Class) {
assert(Class && "unexpected null class");
return get(Class->getRecords(), Class);
}
void RecordRecTy::Profile(FoldingSetNodeID &ID) const {
ProfileRecordRecTy(ID, getClasses());
}
std::string RecordRecTy::getAsString() const {
if (NumClasses == 1)
return getClasses()[0]->getNameInitAsString();
std::string Str = "{";
bool First = true;
for (Record *R : getClasses()) {
if (!First)
Str += ", ";
First = false;
Str += R->getNameInitAsString();
}
Str += "}";
return Str;
}
bool RecordRecTy::isSubClassOf(Record *Class) const {
return llvm::any_of(getClasses(), [Class](Record *MySuperClass) {
return MySuperClass == Class ||
MySuperClass->isSubClassOf(Class);
});
}
bool RecordRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
if (this == RHS)
return true;
const RecordRecTy *RTy = dyn_cast<RecordRecTy>(RHS);
if (!RTy)
return false;
return llvm::all_of(RTy->getClasses(), [this](Record *TargetClass) {
return isSubClassOf(TargetClass);
});
}
bool RecordRecTy::typeIsA(const RecTy *RHS) const {
return typeIsConvertibleTo(RHS);
}
static RecordRecTy *resolveRecordTypes(RecordRecTy *T1, RecordRecTy *T2) {
SmallVector<Record *, 4> CommonSuperClasses;
SmallVector<Record *, 4> Stack(T1->classes_begin(), T1->classes_end());
while (!Stack.empty()) {
Record *R = Stack.pop_back_val();
if (T2->isSubClassOf(R)) {
CommonSuperClasses.push_back(R);
} else {
R->getDirectSuperClasses(Stack);
}
}
return RecordRecTy::get(T1->getRecordKeeper(), CommonSuperClasses);
}
RecTy *llvm::resolveTypes(RecTy *T1, RecTy *T2) {
if (T1 == T2)
return T1;
if (RecordRecTy *RecTy1 = dyn_cast<RecordRecTy>(T1)) {
if (RecordRecTy *RecTy2 = dyn_cast<RecordRecTy>(T2))
return resolveRecordTypes(RecTy1, RecTy2);
}
assert(T1 != nullptr && "Invalid record type");
if (T1->typeIsConvertibleTo(T2))
return T2;
assert(T2 != nullptr && "Invalid record type");
if (T2->typeIsConvertibleTo(T1))
return T1;
if (ListRecTy *ListTy1 = dyn_cast<ListRecTy>(T1)) {
if (ListRecTy *ListTy2 = dyn_cast<ListRecTy>(T2)) {
RecTy* NewType = resolveTypes(ListTy1->getElementType(),
ListTy2->getElementType());
if (NewType)
return NewType->getListTy();
}
}
return nullptr;
}
//===----------------------------------------------------------------------===//
// Initializer implementations
//===----------------------------------------------------------------------===//
void Init::anchor() {}
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
LLVM_DUMP_METHOD void Init::dump() const { return print(errs()); }
#endif
RecordKeeper &Init::getRecordKeeper() const {
if (auto *TyInit = dyn_cast<TypedInit>(this))
return TyInit->getType()->getRecordKeeper();
if (auto *ArgInit = dyn_cast<ArgumentInit>(this))
return ArgInit->getRecordKeeper();
return cast<UnsetInit>(this)->getRecordKeeper();
}
UnsetInit *UnsetInit::get(RecordKeeper &RK) {
return &RK.getImpl().TheUnsetInit;
}
Init *UnsetInit::getCastTo(RecTy *Ty) const {
return const_cast<UnsetInit *>(this);
}
Init *UnsetInit::convertInitializerTo(RecTy *Ty) const {
return const_cast<UnsetInit *>(this);
}
static void ProfileArgumentInit(FoldingSetNodeID &ID, Init *Value,
ArgAuxType Aux) {
auto I = Aux.index();
ID.AddInteger(I);
if (I == ArgumentInit::Positional)
ID.AddInteger(std::get<ArgumentInit::Positional>(Aux));
if (I == ArgumentInit::Named)
ID.AddPointer(std::get<ArgumentInit::Named>(Aux));
ID.AddPointer(Value);
}
void ArgumentInit::Profile(FoldingSetNodeID &ID) const {
ProfileArgumentInit(ID, Value, Aux);
}
ArgumentInit *ArgumentInit::get(Init *Value, ArgAuxType Aux) {
FoldingSetNodeID ID;
ProfileArgumentInit(ID, Value, Aux);
RecordKeeper &RK = Value->getRecordKeeper();
detail::RecordKeeperImpl &RKImpl = RK.getImpl();
void *IP = nullptr;
if (ArgumentInit *I = RKImpl.TheArgumentInitPool.FindNodeOrInsertPos(ID, IP))
return I;
ArgumentInit *I = new (RKImpl.Allocator) ArgumentInit(Value, Aux);
RKImpl.TheArgumentInitPool.InsertNode(I, IP);
return I;
}
Init *ArgumentInit::resolveReferences(Resolver &R) const {
Init *NewValue = Value->resolveReferences(R);
if (NewValue != Value)
return cloneWithValue(NewValue);
return const_cast<ArgumentInit *>(this);
}
BitInit *BitInit::get(RecordKeeper &RK, bool V) {
return V ? &RK.getImpl().TrueBitInit : &RK.getImpl().FalseBitInit;
}
Init *BitInit::convertInitializerTo(RecTy *Ty) const {
if (isa<BitRecTy>(Ty))
return const_cast<BitInit *>(this);
if (isa<IntRecTy>(Ty))
return IntInit::get(getRecordKeeper(), getValue());
if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
// Can only convert single bit.
if (BRT->getNumBits() == 1)
return BitsInit::get(getRecordKeeper(), const_cast<BitInit *>(this));
}
return nullptr;
}
static void
ProfileBitsInit(FoldingSetNodeID &ID, ArrayRef<Init *> Range) {
ID.AddInteger(Range.size());
for (Init *I : Range)
ID.AddPointer(I);
}
BitsInit *BitsInit::get(RecordKeeper &RK, ArrayRef<Init *> Range) {
FoldingSetNodeID ID;
ProfileBitsInit(ID, Range);
detail::RecordKeeperImpl &RKImpl = RK.getImpl();
void *IP = nullptr;
if (BitsInit *I = RKImpl.TheBitsInitPool.FindNodeOrInsertPos(ID, IP))
return I;
void *Mem = RKImpl.Allocator.Allocate(totalSizeToAlloc<Init *>(Range.size()),
alignof(BitsInit));
BitsInit *I = new (Mem) BitsInit(RK, Range.size());
std::uninitialized_copy(Range.begin(), Range.end(),
I->getTrailingObjects<Init *>());
RKImpl.TheBitsInitPool.InsertNode(I, IP);
return I;
}
void BitsInit::Profile(FoldingSetNodeID &ID) const {
ProfileBitsInit(ID, ArrayRef(getTrailingObjects<Init *>(), NumBits));
}
Init *BitsInit::convertInitializerTo(RecTy *Ty) const {
if (isa<BitRecTy>(Ty)) {
if (getNumBits() != 1) return nullptr; // Only accept if just one bit!
return getBit(0);
}
if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
// If the number of bits is right, return it. Otherwise we need to expand
// or truncate.
if (getNumBits() != BRT->getNumBits()) return nullptr;
return const_cast<BitsInit *>(this);
}
if (isa<IntRecTy>(Ty)) {
int64_t Result = 0;
for (unsigned i = 0, e = getNumBits(); i != e; ++i)
if (auto *Bit = dyn_cast<BitInit>(getBit(i)))
Result |= static_cast<int64_t>(Bit->getValue()) << i;
else
return nullptr;
return IntInit::get(getRecordKeeper(), Result);
}
return nullptr;
}
Init *
BitsInit::convertInitializerBitRange(ArrayRef<unsigned> Bits) const {
SmallVector<Init *, 16> NewBits(Bits.size());
for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
if (Bits[i] >= getNumBits())
return nullptr;
NewBits[i] = getBit(Bits[i]);
}
return BitsInit::get(getRecordKeeper(), NewBits);
}
bool BitsInit::isConcrete() const {
for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
if (!getBit(i)->isConcrete())
return false;
}
return true;
}
std::string BitsInit::getAsString() const {
std::string Result = "{ ";
for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
if (i) Result += ", ";
if (Init *Bit = getBit(e-i-1))
Result += Bit->getAsString();
else
Result += "*";
}
return Result + " }";
}
// resolveReferences - If there are any field references that refer to fields
// that have been filled in, we can propagate the values now.
Init *BitsInit::resolveReferences(Resolver &R) const {
bool Changed = false;
SmallVector<Init *, 16> NewBits(getNumBits());
Init *CachedBitVarRef = nullptr;
Init *CachedBitVarResolved = nullptr;
for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
Init *CurBit = getBit(i);
Init *NewBit = CurBit;
if (VarBitInit *CurBitVar = dyn_cast<VarBitInit>(CurBit)) {
if (CurBitVar->getBitVar() != CachedBitVarRef) {
CachedBitVarRef = CurBitVar->getBitVar();
CachedBitVarResolved = CachedBitVarRef->resolveReferences(R);
}
assert(CachedBitVarResolved && "Unresolved bitvar reference");
NewBit = CachedBitVarResolved->getBit(CurBitVar->getBitNum());
} else {
// getBit(0) implicitly converts int and bits<1> values to bit.
NewBit = CurBit->resolveReferences(R)->getBit(0);
}
if (isa<UnsetInit>(NewBit) && R.keepUnsetBits())
NewBit = CurBit;
NewBits[i] = NewBit;
Changed |= CurBit != NewBit;
}
if (Changed)
return BitsInit::get(getRecordKeeper(), NewBits);
return const_cast<BitsInit *>(this);
}
IntInit *IntInit::get(RecordKeeper &RK, int64_t V) {
IntInit *&I = RK.getImpl().TheIntInitPool[V];
if (!I)
I = new (RK.getImpl().Allocator) IntInit(RK, V);
return I;
}
std::string IntInit::getAsString() const {
return itostr(Value);
}
static bool canFitInBitfield(int64_t Value, unsigned NumBits) {
// For example, with NumBits == 4, we permit Values from [-7 .. 15].
return (NumBits >= sizeof(Value) * 8) ||
(Value >> NumBits == 0) || (Value >> (NumBits-1) == -1);
}
Init *IntInit::convertInitializerTo(RecTy *Ty) const {
if (isa<IntRecTy>(Ty))
return const_cast<IntInit *>(this);
if (isa<BitRecTy>(Ty)) {
int64_t Val = getValue();
if (Val != 0 && Val != 1) return nullptr; // Only accept 0 or 1 for a bit!
return BitInit::get(getRecordKeeper(), Val != 0);
}
if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
int64_t Value = getValue();
// Make sure this bitfield is large enough to hold the integer value.
if (!canFitInBitfield(Value, BRT->getNumBits()))
return nullptr;
SmallVector<Init *, 16> NewBits(BRT->getNumBits());
for (unsigned i = 0; i != BRT->getNumBits(); ++i)
NewBits[i] =
BitInit::get(getRecordKeeper(), Value & ((i < 64) ? (1LL << i) : 0));
return BitsInit::get(getRecordKeeper(), NewBits);
}
return nullptr;
}
Init *
IntInit::convertInitializerBitRange(ArrayRef<unsigned> Bits) const {
SmallVector<Init *, 16> NewBits(Bits.size());
for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
if (Bits[i] >= 64)
return nullptr;
NewBits[i] =
BitInit::get(getRecordKeeper(), Value & (INT64_C(1) << Bits[i]));
}
return BitsInit::get(getRecordKeeper(), NewBits);
}
AnonymousNameInit *AnonymousNameInit::get(RecordKeeper &RK, unsigned V) {
return new (RK.getImpl().Allocator) AnonymousNameInit(RK, V);
}
StringInit *AnonymousNameInit::getNameInit() const {
return StringInit::get(getRecordKeeper(), getAsString());
}
std::string AnonymousNameInit::getAsString() const {
return "anonymous_" + utostr(Value);
}
Init *AnonymousNameInit::resolveReferences(Resolver &R) const {
auto *Old = const_cast<Init *>(static_cast<const Init *>(this));
auto *New = R.resolve(Old);
New = New ? New : Old;
if (R.isFinal())
if (auto *Anonymous = dyn_cast<AnonymousNameInit>(New))
return Anonymous->getNameInit();
return New;
}
StringInit *StringInit::get(RecordKeeper &RK, StringRef V, StringFormat Fmt) {
detail::RecordKeeperImpl &RKImpl = RK.getImpl();
auto &InitMap = Fmt == SF_String ? RKImpl.StringInitStringPool
: RKImpl.StringInitCodePool;
auto &Entry = *InitMap.insert(std::make_pair(V, nullptr)).first;
if (!Entry.second)
Entry.second = new (RKImpl.Allocator) StringInit(RK, Entry.getKey(), Fmt);
return Entry.second;
}
Init *StringInit::convertInitializerTo(RecTy *Ty) const {
if (isa<StringRecTy>(Ty))
return const_cast<StringInit *>(this);
return nullptr;
}
static void ProfileListInit(FoldingSetNodeID &ID,
ArrayRef<Init *> Range,
RecTy *EltTy) {
ID.AddInteger(Range.size());
ID.AddPointer(EltTy);
for (Init *I : Range)
ID.AddPointer(I);
}
ListInit *ListInit::get(ArrayRef<Init *> Range, RecTy *EltTy) {
FoldingSetNodeID ID;
ProfileListInit(ID, Range, EltTy);
detail::RecordKeeperImpl &RK = EltTy->getRecordKeeper().getImpl();
void *IP = nullptr;
if (ListInit *I = RK.TheListInitPool.FindNodeOrInsertPos(ID, IP))
return I;
assert(Range.empty() || !isa<TypedInit>(Range[0]) ||
cast<TypedInit>(Range[0])->getType()->typeIsConvertibleTo(EltTy));
void *Mem = RK.Allocator.Allocate(totalSizeToAlloc<Init *>(Range.size()),
alignof(ListInit));
ListInit *I = new (Mem) ListInit(Range.size(), EltTy);
std::uninitialized_copy(Range.begin(), Range.end(),
I->getTrailingObjects<Init *>());
RK.TheListInitPool.InsertNode(I, IP);
return I;
}
void ListInit::Profile(FoldingSetNodeID &ID) const {
RecTy *EltTy = cast<ListRecTy>(getType())->getElementType();
ProfileListInit(ID, getValues(), EltTy);
}
Init *ListInit::convertInitializerTo(RecTy *Ty) const {
if (getType() == Ty)
return const_cast<ListInit*>(this);
if (auto *LRT = dyn_cast<ListRecTy>(Ty)) {
SmallVector<Init*, 8> Elements;
Elements.reserve(getValues().size());
// Verify that all of the elements of the list are subclasses of the
// appropriate class!
bool Changed = false;
RecTy *ElementType = LRT->getElementType();
for (Init *I : getValues())
if (Init *CI = I->convertInitializerTo(ElementType)) {
Elements.push_back(CI);
if (CI != I)
Changed = true;
} else
return nullptr;
if (!Changed)
return const_cast<ListInit*>(this);
return ListInit::get(Elements, ElementType);
}
return nullptr;
}
Record *ListInit::getElementAsRecord(unsigned i) const {
assert(i < NumValues && "List element index out of range!");
DefInit *DI = dyn_cast<DefInit>(getElement(i));
if (!DI)
PrintFatalError("Expected record in list!");
return DI->getDef();
}
Init *ListInit::resolveReferences(Resolver &R) const {
SmallVector<Init*, 8> Resolved;
Resolved.reserve(size());
bool Changed = false;
for (Init *CurElt : getValues()) {
Init *E = CurElt->resolveReferences(R);
Changed |= E != CurElt;
Resolved.push_back(E);
}
if (Changed)
return ListInit::get(Resolved, getElementType());
return const_cast<ListInit *>(this);
}
bool ListInit::isComplete() const {
for (Init *Element : *this) {
if (!Element->isComplete())
return false;
}
return true;
}
bool ListInit::isConcrete() const {
for (Init *Element : *this) {
if (!Element->isConcrete())
return false;
}
return true;
}
std::string ListInit::getAsString() const {
std::string Result = "[";
const char *sep = "";
for (Init *Element : *this) {
Result += sep;
sep = ", ";
Result += Element->getAsString();
}
return Result + "]";
}
Init *OpInit::getBit(unsigned Bit) const {
if (getType() == BitRecTy::get(getRecordKeeper()))
return const_cast<OpInit*>(this);
return VarBitInit::get(const_cast<OpInit*>(this), Bit);
}
static void
ProfileUnOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *Op, RecTy *Type) {
ID.AddInteger(Opcode);
ID.AddPointer(Op);
ID.AddPointer(Type);
}
UnOpInit *UnOpInit::get(UnaryOp Opc, Init *LHS, RecTy *Type) {
FoldingSetNodeID ID;
ProfileUnOpInit(ID, Opc, LHS, Type);
detail::RecordKeeperImpl &RK = Type->getRecordKeeper().getImpl();
void *IP = nullptr;
if (UnOpInit *I = RK.TheUnOpInitPool.FindNodeOrInsertPos(ID, IP))
return I;
UnOpInit *I = new (RK.Allocator) UnOpInit(Opc, LHS, Type);
RK.TheUnOpInitPool.InsertNode(I, IP);
return I;
}
void UnOpInit::Profile(FoldingSetNodeID &ID) const {
ProfileUnOpInit(ID, getOpcode(), getOperand(), getType());
}
Init *UnOpInit::Fold(Record *CurRec, bool IsFinal) const {
RecordKeeper &RK = getRecordKeeper();
switch (getOpcode()) {
case REPR:
if (LHS->isConcrete()) {
// If it is a Record, print the full content.
if (const auto *Def = dyn_cast<DefInit>(LHS)) {
std::string S;
raw_string_ostream OS(S);
OS << *Def->getDef();
OS.flush();
return StringInit::get(RK, S);
} else {
// Otherwise, print the value of the variable.
//
// NOTE: we could recursively !repr the elements of a list,
// but that could produce a lot of output when printing a
// defset.
return StringInit::get(RK, LHS->getAsString());
}
}
break;
case TOLOWER:
if (StringInit *LHSs = dyn_cast<StringInit>(LHS))
return StringInit::get(RK, LHSs->getValue().lower());
break;
case TOUPPER:
if (StringInit *LHSs = dyn_cast<StringInit>(LHS))
return StringInit::get(RK, LHSs->getValue().upper());
break;
case CAST:
if (isa<StringRecTy>(getType())) {
if (StringInit *LHSs = dyn_cast<StringInit>(LHS))
return LHSs;
if (DefInit *LHSd = dyn_cast<DefInit>(LHS))
return StringInit::get(RK, LHSd->getAsString());
if (IntInit *LHSi = dyn_cast_or_null<IntInit>(
LHS->convertInitializerTo(IntRecTy::get(RK))))
return StringInit::get(RK, LHSi->getAsString());
} else if (isa<RecordRecTy>(getType())) {
if (StringInit *Name = dyn_cast<StringInit>(LHS)) {
Record *D = RK.getDef(Name->getValue());
if (!D && CurRec) {
// Self-references are allowed, but their resolution is delayed until
// the final resolve to ensure that we get the correct type for them.
auto *Anonymous = dyn_cast<AnonymousNameInit>(CurRec->getNameInit());
if (Name == CurRec->getNameInit() ||
(Anonymous && Name == Anonymous->getNameInit())) {
if (!IsFinal)
break;
D = CurRec;
}
}
auto PrintFatalErrorHelper = [CurRec](const Twine &T) {
if (CurRec)
PrintFatalError(CurRec->getLoc(), T);
else
PrintFatalError(T);
};
if (!D) {
if (IsFinal) {
PrintFatalErrorHelper(Twine("Undefined reference to record: '") +
Name->getValue() + "'\n");
}
break;
}
DefInit *DI = DefInit::get(D);
if (!DI->getType()->typeIsA(getType())) {
PrintFatalErrorHelper(Twine("Expected type '") +
getType()->getAsString() + "', got '" +
DI->getType()->getAsString() + "' in: " +
getAsString() + "\n");
}
return DI;
}
}
if (Init *NewInit = LHS->convertInitializerTo(getType()))
return NewInit;
break;
case NOT:
if (IntInit *LHSi = dyn_cast_or_null<IntInit>(
LHS->convertInitializerTo(IntRecTy::get(RK))))
return IntInit::get(RK, LHSi->getValue() ? 0 : 1);
break;
case HEAD:
if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) {
assert(!LHSl->empty() && "Empty list in head");
return LHSl->getElement(0);
}
break;
case TAIL:
if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) {
assert(!LHSl->empty() && "Empty list in tail");
// Note the +1. We can't just pass the result of getValues()
// directly.
return ListInit::get(LHSl->getValues().slice(1), LHSl->getElementType());
}
break;
case SIZE:
if (ListInit *LHSl = dyn_cast<ListInit>(LHS))
return IntInit::get(RK, LHSl->size());
if (DagInit *LHSd = dyn_cast<DagInit>(LHS))
return IntInit::get(RK, LHSd->arg_size());
if (StringInit *LHSs = dyn_cast<StringInit>(LHS))
return IntInit::get(RK, LHSs->getValue().size());
break;
case EMPTY:
if (ListInit *LHSl = dyn_cast<ListInit>(LHS))
return IntInit::get(RK, LHSl->empty());
if (DagInit *LHSd = dyn_cast<DagInit>(LHS))
return IntInit::get(RK, LHSd->arg_empty());
if (StringInit *LHSs = dyn_cast<StringInit>(LHS))
return IntInit::get(RK, LHSs->getValue().empty());
break;
case GETDAGOP:
if (DagInit *Dag = dyn_cast<DagInit>(LHS)) {
DefInit *DI = DefInit::get(Dag->getOperatorAsDef({}));
if (!DI->getType()->typeIsA(getType())) {
PrintFatalError(CurRec->getLoc(),
Twine("Expected type '") +
getType()->getAsString() + "', got '" +
DI->getType()->getAsString() + "' in: " +
getAsString() + "\n");
} else {
return DI;
}
}
break;
case LOG2:
if (IntInit *LHSi = dyn_cast_or_null<IntInit>(
LHS->convertInitializerTo(IntRecTy::get(RK)))) {
int64_t LHSv = LHSi->getValue();
if (LHSv <= 0) {
PrintFatalError(CurRec->getLoc(),
"Illegal operation: logtwo is undefined "
"on arguments less than or equal to 0");
} else {
uint64_t Log = Log2_64(LHSv);
assert(Log <= INT64_MAX &&
"Log of an int64_t must be smaller than INT64_MAX");
return IntInit::get(RK, static_cast<int64_t>(Log));
}
}
break;
}
return const_cast<UnOpInit *>(this);
}
Init *UnOpInit::resolveReferences(Resolver &R) const {
Init *lhs = LHS->resolveReferences(R);
if (LHS != lhs || (R.isFinal() && getOpcode() == CAST))
return (UnOpInit::get(getOpcode(), lhs, getType()))
->Fold(R.getCurrentRecord(), R.isFinal());
return const_cast<UnOpInit *>(this);
}
std::string UnOpInit::getAsString() const {
std::string Result;
switch (getOpcode()) {
case CAST: Result = "!cast<" + getType()->getAsString() + ">"; break;
case NOT: Result = "!not"; break;
case HEAD: Result = "!head"; break;
case TAIL: Result = "!tail"; break;
case SIZE: Result = "!size"; break;
case EMPTY: Result = "!empty"; break;
case GETDAGOP: Result = "!getdagop"; break;
case LOG2 : Result = "!logtwo"; break;
case REPR:
Result = "!repr";
break;
case TOLOWER:
Result = "!tolower";
break;
case TOUPPER:
Result = "!toupper";
break;
}
return Result + "(" + LHS->getAsString() + ")";
}
static void
ProfileBinOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *LHS, Init *RHS,
RecTy *Type) {
ID.AddInteger(Opcode);
ID.AddPointer(LHS);
ID.AddPointer(RHS);
ID.AddPointer(Type);
}