File: | build/source/llvm/lib/TableGen/Record.cpp |
Warning: | line 486, column 11 Value stored to 'NewBit' during its initialization is never read |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===- Record.cpp - Record implementation ---------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // Implement the tablegen record classes. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #include "llvm/TableGen/Record.h" |
14 | #include "llvm/ADT/ArrayRef.h" |
15 | #include "llvm/ADT/DenseMap.h" |
16 | #include "llvm/ADT/FoldingSet.h" |
17 | #include "llvm/ADT/SmallString.h" |
18 | #include "llvm/ADT/SmallVector.h" |
19 | #include "llvm/ADT/StringExtras.h" |
20 | #include "llvm/ADT/StringMap.h" |
21 | #include "llvm/ADT/StringRef.h" |
22 | #include "llvm/Config/llvm-config.h" |
23 | #include "llvm/Support/Allocator.h" |
24 | #include "llvm/Support/Casting.h" |
25 | #include "llvm/Support/Compiler.h" |
26 | #include "llvm/Support/ErrorHandling.h" |
27 | #include "llvm/Support/MathExtras.h" |
28 | #include "llvm/Support/SMLoc.h" |
29 | #include "llvm/Support/raw_ostream.h" |
30 | #include "llvm/TableGen/Error.h" |
31 | #include <cassert> |
32 | #include <cstdint> |
33 | #include <map> |
34 | #include <memory> |
35 | #include <string> |
36 | #include <utility> |
37 | #include <vector> |
38 | |
39 | using namespace llvm; |
40 | |
41 | #define DEBUG_TYPE"tblgen-records" "tblgen-records" |
42 | |
43 | //===----------------------------------------------------------------------===// |
44 | // Context |
45 | //===----------------------------------------------------------------------===// |
46 | |
47 | namespace llvm { |
48 | namespace detail { |
49 | /// This class represents the internal implementation of the RecordKeeper. |
50 | /// It contains all of the contextual static state of the Record classes. It is |
51 | /// kept out-of-line to simplify dependencies, and also make it easier for |
52 | /// internal classes to access the uniquer state of the keeper. |
53 | struct RecordKeeperImpl { |
54 | RecordKeeperImpl(RecordKeeper &RK) |
55 | : SharedBitRecTy(RK), SharedIntRecTy(RK), SharedStringRecTy(RK), |
56 | SharedDagRecTy(RK), AnyRecord(RK, 0), TheUnsetInit(RK), |
57 | TrueBitInit(true, &SharedBitRecTy), |
58 | FalseBitInit(false, &SharedBitRecTy), StringInitStringPool(Allocator), |
59 | StringInitCodePool(Allocator), AnonCounter(0), LastRecordID(0) {} |
60 | |
61 | BumpPtrAllocator Allocator; |
62 | std::vector<BitsRecTy *> SharedBitsRecTys; |
63 | BitRecTy SharedBitRecTy; |
64 | IntRecTy SharedIntRecTy; |
65 | StringRecTy SharedStringRecTy; |
66 | DagRecTy SharedDagRecTy; |
67 | |
68 | RecordRecTy AnyRecord; |
69 | UnsetInit TheUnsetInit; |
70 | BitInit TrueBitInit; |
71 | BitInit FalseBitInit; |
72 | |
73 | FoldingSet<BitsInit> TheBitsInitPool; |
74 | std::map<int64_t, IntInit *> TheIntInitPool; |
75 | StringMap<StringInit *, BumpPtrAllocator &> StringInitStringPool; |
76 | StringMap<StringInit *, BumpPtrAllocator &> StringInitCodePool; |
77 | FoldingSet<ListInit> TheListInitPool; |
78 | FoldingSet<UnOpInit> TheUnOpInitPool; |
79 | FoldingSet<BinOpInit> TheBinOpInitPool; |
80 | FoldingSet<TernOpInit> TheTernOpInitPool; |
81 | FoldingSet<FoldOpInit> TheFoldOpInitPool; |
82 | FoldingSet<IsAOpInit> TheIsAOpInitPool; |
83 | FoldingSet<ExistsOpInit> TheExistsOpInitPool; |
84 | DenseMap<std::pair<RecTy *, Init *>, VarInit *> TheVarInitPool; |
85 | DenseMap<std::pair<TypedInit *, unsigned>, VarBitInit *> TheVarBitInitPool; |
86 | FoldingSet<VarDefInit> TheVarDefInitPool; |
87 | DenseMap<std::pair<Init *, StringInit *>, FieldInit *> TheFieldInitPool; |
88 | FoldingSet<CondOpInit> TheCondOpInitPool; |
89 | FoldingSet<DagInit> TheDagInitPool; |
90 | FoldingSet<RecordRecTy> RecordTypePool; |
91 | |
92 | unsigned AnonCounter; |
93 | unsigned LastRecordID; |
94 | }; |
95 | } // namespace detail |
96 | } // namespace llvm |
97 | |
98 | //===----------------------------------------------------------------------===// |
99 | // Type implementations |
100 | //===----------------------------------------------------------------------===// |
101 | |
102 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
103 | LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void RecTy::dump() const { print(errs()); } |
104 | #endif |
105 | |
106 | ListRecTy *RecTy::getListTy() { |
107 | if (!ListTy) |
108 | ListTy = new (RK.getImpl().Allocator) ListRecTy(this); |
109 | return ListTy; |
110 | } |
111 | |
112 | bool RecTy::typeIsConvertibleTo(const RecTy *RHS) const { |
113 | assert(RHS && "NULL pointer")(static_cast <bool> (RHS && "NULL pointer") ? void (0) : __assert_fail ("RHS && \"NULL pointer\"", "llvm/lib/TableGen/Record.cpp" , 113, __extension__ __PRETTY_FUNCTION__)); |
114 | return Kind == RHS->getRecTyKind(); |
115 | } |
116 | |
117 | bool RecTy::typeIsA(const RecTy *RHS) const { return this == RHS; } |
118 | |
119 | BitRecTy *BitRecTy::get(RecordKeeper &RK) { |
120 | return &RK.getImpl().SharedBitRecTy; |
121 | } |
122 | |
123 | bool BitRecTy::typeIsConvertibleTo(const RecTy *RHS) const{ |
124 | if (RecTy::typeIsConvertibleTo(RHS) || RHS->getRecTyKind() == IntRecTyKind) |
125 | return true; |
126 | if (const BitsRecTy *BitsTy = dyn_cast<BitsRecTy>(RHS)) |
127 | return BitsTy->getNumBits() == 1; |
128 | return false; |
129 | } |
130 | |
131 | BitsRecTy *BitsRecTy::get(RecordKeeper &RK, unsigned Sz) { |
132 | detail::RecordKeeperImpl &RKImpl = RK.getImpl(); |
133 | if (Sz >= RKImpl.SharedBitsRecTys.size()) |
134 | RKImpl.SharedBitsRecTys.resize(Sz + 1); |
135 | BitsRecTy *&Ty = RKImpl.SharedBitsRecTys[Sz]; |
136 | if (!Ty) |
137 | Ty = new (RKImpl.Allocator) BitsRecTy(RK, Sz); |
138 | return Ty; |
139 | } |
140 | |
141 | std::string BitsRecTy::getAsString() const { |
142 | return "bits<" + utostr(Size) + ">"; |
143 | } |
144 | |
145 | bool BitsRecTy::typeIsConvertibleTo(const RecTy *RHS) const { |
146 | if (RecTy::typeIsConvertibleTo(RHS)) //argument and the sender are same type |
147 | return cast<BitsRecTy>(RHS)->Size == Size; |
148 | RecTyKind kind = RHS->getRecTyKind(); |
149 | return (kind == BitRecTyKind && Size == 1) || (kind == IntRecTyKind); |
150 | } |
151 | |
152 | IntRecTy *IntRecTy::get(RecordKeeper &RK) { |
153 | return &RK.getImpl().SharedIntRecTy; |
154 | } |
155 | |
156 | bool IntRecTy::typeIsConvertibleTo(const RecTy *RHS) const { |
157 | RecTyKind kind = RHS->getRecTyKind(); |
158 | return kind==BitRecTyKind || kind==BitsRecTyKind || kind==IntRecTyKind; |
159 | } |
160 | |
161 | StringRecTy *StringRecTy::get(RecordKeeper &RK) { |
162 | return &RK.getImpl().SharedStringRecTy; |
163 | } |
164 | |
165 | std::string StringRecTy::getAsString() const { |
166 | return "string"; |
167 | } |
168 | |
169 | bool StringRecTy::typeIsConvertibleTo(const RecTy *RHS) const { |
170 | RecTyKind Kind = RHS->getRecTyKind(); |
171 | return Kind == StringRecTyKind; |
172 | } |
173 | |
174 | std::string ListRecTy::getAsString() const { |
175 | return "list<" + ElementTy->getAsString() + ">"; |
176 | } |
177 | |
178 | bool ListRecTy::typeIsConvertibleTo(const RecTy *RHS) const { |
179 | if (const auto *ListTy = dyn_cast<ListRecTy>(RHS)) |
180 | return ElementTy->typeIsConvertibleTo(ListTy->getElementType()); |
181 | return false; |
182 | } |
183 | |
184 | bool ListRecTy::typeIsA(const RecTy *RHS) const { |
185 | if (const ListRecTy *RHSl = dyn_cast<ListRecTy>(RHS)) |
186 | return getElementType()->typeIsA(RHSl->getElementType()); |
187 | return false; |
188 | } |
189 | |
190 | DagRecTy *DagRecTy::get(RecordKeeper &RK) { |
191 | return &RK.getImpl().SharedDagRecTy; |
192 | } |
193 | |
194 | std::string DagRecTy::getAsString() const { |
195 | return "dag"; |
196 | } |
197 | |
198 | static void ProfileRecordRecTy(FoldingSetNodeID &ID, |
199 | ArrayRef<Record *> Classes) { |
200 | ID.AddInteger(Classes.size()); |
201 | for (Record *R : Classes) |
202 | ID.AddPointer(R); |
203 | } |
204 | |
205 | RecordRecTy *RecordRecTy::get(RecordKeeper &RK, |
206 | ArrayRef<Record *> UnsortedClasses) { |
207 | detail::RecordKeeperImpl &RKImpl = RK.getImpl(); |
208 | if (UnsortedClasses.empty()) |
209 | return &RKImpl.AnyRecord; |
210 | |
211 | FoldingSet<RecordRecTy> &ThePool = RKImpl.RecordTypePool; |
212 | |
213 | SmallVector<Record *, 4> Classes(UnsortedClasses.begin(), |
214 | UnsortedClasses.end()); |
215 | llvm::sort(Classes, [](Record *LHS, Record *RHS) { |
216 | return LHS->getNameInitAsString() < RHS->getNameInitAsString(); |
217 | }); |
218 | |
219 | FoldingSetNodeID ID; |
220 | ProfileRecordRecTy(ID, Classes); |
221 | |
222 | void *IP = nullptr; |
223 | if (RecordRecTy *Ty = ThePool.FindNodeOrInsertPos(ID, IP)) |
224 | return Ty; |
225 | |
226 | #ifndef NDEBUG |
227 | // Check for redundancy. |
228 | for (unsigned i = 0; i < Classes.size(); ++i) { |
229 | for (unsigned j = 0; j < Classes.size(); ++j) { |
230 | assert(i == j || !Classes[i]->isSubClassOf(Classes[j]))(static_cast <bool> (i == j || !Classes[i]->isSubClassOf (Classes[j])) ? void (0) : __assert_fail ("i == j || !Classes[i]->isSubClassOf(Classes[j])" , "llvm/lib/TableGen/Record.cpp", 230, __extension__ __PRETTY_FUNCTION__ )); |
231 | } |
232 | assert(&Classes[0]->getRecords() == &Classes[i]->getRecords())(static_cast <bool> (&Classes[0]->getRecords() == &Classes[i]->getRecords()) ? void (0) : __assert_fail ("&Classes[0]->getRecords() == &Classes[i]->getRecords()" , "llvm/lib/TableGen/Record.cpp", 232, __extension__ __PRETTY_FUNCTION__ )); |
233 | } |
234 | #endif |
235 | |
236 | void *Mem = RKImpl.Allocator.Allocate( |
237 | totalSizeToAlloc<Record *>(Classes.size()), alignof(RecordRecTy)); |
238 | RecordRecTy *Ty = new (Mem) RecordRecTy(RK, Classes.size()); |
239 | std::uninitialized_copy(Classes.begin(), Classes.end(), |
240 | Ty->getTrailingObjects<Record *>()); |
241 | ThePool.InsertNode(Ty, IP); |
242 | return Ty; |
243 | } |
244 | RecordRecTy *RecordRecTy::get(Record *Class) { |
245 | assert(Class && "unexpected null class")(static_cast <bool> (Class && "unexpected null class" ) ? void (0) : __assert_fail ("Class && \"unexpected null class\"" , "llvm/lib/TableGen/Record.cpp", 245, __extension__ __PRETTY_FUNCTION__ )); |
246 | return get(Class->getRecords(), Class); |
247 | } |
248 | |
249 | void RecordRecTy::Profile(FoldingSetNodeID &ID) const { |
250 | ProfileRecordRecTy(ID, getClasses()); |
251 | } |
252 | |
253 | std::string RecordRecTy::getAsString() const { |
254 | if (NumClasses == 1) |
255 | return getClasses()[0]->getNameInitAsString(); |
256 | |
257 | std::string Str = "{"; |
258 | bool First = true; |
259 | for (Record *R : getClasses()) { |
260 | if (!First) |
261 | Str += ", "; |
262 | First = false; |
263 | Str += R->getNameInitAsString(); |
264 | } |
265 | Str += "}"; |
266 | return Str; |
267 | } |
268 | |
269 | bool RecordRecTy::isSubClassOf(Record *Class) const { |
270 | return llvm::any_of(getClasses(), [Class](Record *MySuperClass) { |
271 | return MySuperClass == Class || |
272 | MySuperClass->isSubClassOf(Class); |
273 | }); |
274 | } |
275 | |
276 | bool RecordRecTy::typeIsConvertibleTo(const RecTy *RHS) const { |
277 | if (this == RHS) |
278 | return true; |
279 | |
280 | const RecordRecTy *RTy = dyn_cast<RecordRecTy>(RHS); |
281 | if (!RTy) |
282 | return false; |
283 | |
284 | return llvm::all_of(RTy->getClasses(), [this](Record *TargetClass) { |
285 | return isSubClassOf(TargetClass); |
286 | }); |
287 | } |
288 | |
289 | bool RecordRecTy::typeIsA(const RecTy *RHS) const { |
290 | return typeIsConvertibleTo(RHS); |
291 | } |
292 | |
293 | static RecordRecTy *resolveRecordTypes(RecordRecTy *T1, RecordRecTy *T2) { |
294 | SmallVector<Record *, 4> CommonSuperClasses; |
295 | SmallVector<Record *, 4> Stack(T1->classes_begin(), T1->classes_end()); |
296 | |
297 | while (!Stack.empty()) { |
298 | Record *R = Stack.pop_back_val(); |
299 | |
300 | if (T2->isSubClassOf(R)) { |
301 | CommonSuperClasses.push_back(R); |
302 | } else { |
303 | R->getDirectSuperClasses(Stack); |
304 | } |
305 | } |
306 | |
307 | return RecordRecTy::get(T1->getRecordKeeper(), CommonSuperClasses); |
308 | } |
309 | |
310 | RecTy *llvm::resolveTypes(RecTy *T1, RecTy *T2) { |
311 | if (T1 == T2) |
312 | return T1; |
313 | |
314 | if (RecordRecTy *RecTy1 = dyn_cast<RecordRecTy>(T1)) { |
315 | if (RecordRecTy *RecTy2 = dyn_cast<RecordRecTy>(T2)) |
316 | return resolveRecordTypes(RecTy1, RecTy2); |
317 | } |
318 | |
319 | assert(T1 != nullptr && "Invalid record type")(static_cast <bool> (T1 != nullptr && "Invalid record type" ) ? void (0) : __assert_fail ("T1 != nullptr && \"Invalid record type\"" , "llvm/lib/TableGen/Record.cpp", 319, __extension__ __PRETTY_FUNCTION__ )); |
320 | if (T1->typeIsConvertibleTo(T2)) |
321 | return T2; |
322 | |
323 | assert(T2 != nullptr && "Invalid record type")(static_cast <bool> (T2 != nullptr && "Invalid record type" ) ? void (0) : __assert_fail ("T2 != nullptr && \"Invalid record type\"" , "llvm/lib/TableGen/Record.cpp", 323, __extension__ __PRETTY_FUNCTION__ )); |
324 | if (T2->typeIsConvertibleTo(T1)) |
325 | return T1; |
326 | |
327 | if (ListRecTy *ListTy1 = dyn_cast<ListRecTy>(T1)) { |
328 | if (ListRecTy *ListTy2 = dyn_cast<ListRecTy>(T2)) { |
329 | RecTy* NewType = resolveTypes(ListTy1->getElementType(), |
330 | ListTy2->getElementType()); |
331 | if (NewType) |
332 | return NewType->getListTy(); |
333 | } |
334 | } |
335 | |
336 | return nullptr; |
337 | } |
338 | |
339 | //===----------------------------------------------------------------------===// |
340 | // Initializer implementations |
341 | //===----------------------------------------------------------------------===// |
342 | |
343 | void Init::anchor() {} |
344 | |
345 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
346 | LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void Init::dump() const { return print(errs()); } |
347 | #endif |
348 | |
349 | RecordKeeper &Init::getRecordKeeper() const { |
350 | if (auto *TyInit = dyn_cast<TypedInit>(this)) |
351 | return TyInit->getType()->getRecordKeeper(); |
352 | return cast<UnsetInit>(this)->getRecordKeeper(); |
353 | } |
354 | |
355 | UnsetInit *UnsetInit::get(RecordKeeper &RK) { |
356 | return &RK.getImpl().TheUnsetInit; |
357 | } |
358 | |
359 | Init *UnsetInit::getCastTo(RecTy *Ty) const { |
360 | return const_cast<UnsetInit *>(this); |
361 | } |
362 | |
363 | Init *UnsetInit::convertInitializerTo(RecTy *Ty) const { |
364 | return const_cast<UnsetInit *>(this); |
365 | } |
366 | |
367 | BitInit *BitInit::get(RecordKeeper &RK, bool V) { |
368 | return V ? &RK.getImpl().TrueBitInit : &RK.getImpl().FalseBitInit; |
369 | } |
370 | |
371 | Init *BitInit::convertInitializerTo(RecTy *Ty) const { |
372 | if (isa<BitRecTy>(Ty)) |
373 | return const_cast<BitInit *>(this); |
374 | |
375 | if (isa<IntRecTy>(Ty)) |
376 | return IntInit::get(getRecordKeeper(), getValue()); |
377 | |
378 | if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) { |
379 | // Can only convert single bit. |
380 | if (BRT->getNumBits() == 1) |
381 | return BitsInit::get(getRecordKeeper(), const_cast<BitInit *>(this)); |
382 | } |
383 | |
384 | return nullptr; |
385 | } |
386 | |
387 | static void |
388 | ProfileBitsInit(FoldingSetNodeID &ID, ArrayRef<Init *> Range) { |
389 | ID.AddInteger(Range.size()); |
390 | |
391 | for (Init *I : Range) |
392 | ID.AddPointer(I); |
393 | } |
394 | |
395 | BitsInit *BitsInit::get(RecordKeeper &RK, ArrayRef<Init *> Range) { |
396 | FoldingSetNodeID ID; |
397 | ProfileBitsInit(ID, Range); |
398 | |
399 | detail::RecordKeeperImpl &RKImpl = RK.getImpl(); |
400 | void *IP = nullptr; |
401 | if (BitsInit *I = RKImpl.TheBitsInitPool.FindNodeOrInsertPos(ID, IP)) |
402 | return I; |
403 | |
404 | void *Mem = RKImpl.Allocator.Allocate(totalSizeToAlloc<Init *>(Range.size()), |
405 | alignof(BitsInit)); |
406 | BitsInit *I = new (Mem) BitsInit(RK, Range.size()); |
407 | std::uninitialized_copy(Range.begin(), Range.end(), |
408 | I->getTrailingObjects<Init *>()); |
409 | RKImpl.TheBitsInitPool.InsertNode(I, IP); |
410 | return I; |
411 | } |
412 | |
413 | void BitsInit::Profile(FoldingSetNodeID &ID) const { |
414 | ProfileBitsInit(ID, ArrayRef(getTrailingObjects<Init *>(), NumBits)); |
415 | } |
416 | |
417 | Init *BitsInit::convertInitializerTo(RecTy *Ty) const { |
418 | if (isa<BitRecTy>(Ty)) { |
419 | if (getNumBits() != 1) return nullptr; // Only accept if just one bit! |
420 | return getBit(0); |
421 | } |
422 | |
423 | if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) { |
424 | // If the number of bits is right, return it. Otherwise we need to expand |
425 | // or truncate. |
426 | if (getNumBits() != BRT->getNumBits()) return nullptr; |
427 | return const_cast<BitsInit *>(this); |
428 | } |
429 | |
430 | if (isa<IntRecTy>(Ty)) { |
431 | int64_t Result = 0; |
432 | for (unsigned i = 0, e = getNumBits(); i != e; ++i) |
433 | if (auto *Bit = dyn_cast<BitInit>(getBit(i))) |
434 | Result |= static_cast<int64_t>(Bit->getValue()) << i; |
435 | else |
436 | return nullptr; |
437 | return IntInit::get(getRecordKeeper(), Result); |
438 | } |
439 | |
440 | return nullptr; |
441 | } |
442 | |
443 | Init * |
444 | BitsInit::convertInitializerBitRange(ArrayRef<unsigned> Bits) const { |
445 | SmallVector<Init *, 16> NewBits(Bits.size()); |
446 | |
447 | for (unsigned i = 0, e = Bits.size(); i != e; ++i) { |
448 | if (Bits[i] >= getNumBits()) |
449 | return nullptr; |
450 | NewBits[i] = getBit(Bits[i]); |
451 | } |
452 | return BitsInit::get(getRecordKeeper(), NewBits); |
453 | } |
454 | |
455 | bool BitsInit::isConcrete() const { |
456 | for (unsigned i = 0, e = getNumBits(); i != e; ++i) { |
457 | if (!getBit(i)->isConcrete()) |
458 | return false; |
459 | } |
460 | return true; |
461 | } |
462 | |
463 | std::string BitsInit::getAsString() const { |
464 | std::string Result = "{ "; |
465 | for (unsigned i = 0, e = getNumBits(); i != e; ++i) { |
466 | if (i) Result += ", "; |
467 | if (Init *Bit = getBit(e-i-1)) |
468 | Result += Bit->getAsString(); |
469 | else |
470 | Result += "*"; |
471 | } |
472 | return Result + " }"; |
473 | } |
474 | |
475 | // resolveReferences - If there are any field references that refer to fields |
476 | // that have been filled in, we can propagate the values now. |
477 | Init *BitsInit::resolveReferences(Resolver &R) const { |
478 | bool Changed = false; |
479 | SmallVector<Init *, 16> NewBits(getNumBits()); |
480 | |
481 | Init *CachedBitVarRef = nullptr; |
482 | Init *CachedBitVarResolved = nullptr; |
483 | |
484 | for (unsigned i = 0, e = getNumBits(); i != e; ++i) { |
485 | Init *CurBit = getBit(i); |
486 | Init *NewBit = CurBit; |
Value stored to 'NewBit' during its initialization is never read | |
487 | |
488 | if (VarBitInit *CurBitVar = dyn_cast<VarBitInit>(CurBit)) { |
489 | if (CurBitVar->getBitVar() != CachedBitVarRef) { |
490 | CachedBitVarRef = CurBitVar->getBitVar(); |
491 | CachedBitVarResolved = CachedBitVarRef->resolveReferences(R); |
492 | } |
493 | assert(CachedBitVarResolved && "Unresolved bitvar reference")(static_cast <bool> (CachedBitVarResolved && "Unresolved bitvar reference" ) ? void (0) : __assert_fail ("CachedBitVarResolved && \"Unresolved bitvar reference\"" , "llvm/lib/TableGen/Record.cpp", 493, __extension__ __PRETTY_FUNCTION__ )); |
494 | NewBit = CachedBitVarResolved->getBit(CurBitVar->getBitNum()); |
495 | } else { |
496 | // getBit(0) implicitly converts int and bits<1> values to bit. |
497 | NewBit = CurBit->resolveReferences(R)->getBit(0); |
498 | } |
499 | |
500 | if (isa<UnsetInit>(NewBit) && R.keepUnsetBits()) |
501 | NewBit = CurBit; |
502 | NewBits[i] = NewBit; |
503 | Changed |= CurBit != NewBit; |
504 | } |
505 | |
506 | if (Changed) |
507 | return BitsInit::get(getRecordKeeper(), NewBits); |
508 | |
509 | return const_cast<BitsInit *>(this); |
510 | } |
511 | |
512 | IntInit *IntInit::get(RecordKeeper &RK, int64_t V) { |
513 | IntInit *&I = RK.getImpl().TheIntInitPool[V]; |
514 | if (!I) |
515 | I = new (RK.getImpl().Allocator) IntInit(RK, V); |
516 | return I; |
517 | } |
518 | |
519 | std::string IntInit::getAsString() const { |
520 | return itostr(Value); |
521 | } |
522 | |
523 | static bool canFitInBitfield(int64_t Value, unsigned NumBits) { |
524 | // For example, with NumBits == 4, we permit Values from [-7 .. 15]. |
525 | return (NumBits >= sizeof(Value) * 8) || |
526 | (Value >> NumBits == 0) || (Value >> (NumBits-1) == -1); |
527 | } |
528 | |
529 | Init *IntInit::convertInitializerTo(RecTy *Ty) const { |
530 | if (isa<IntRecTy>(Ty)) |
531 | return const_cast<IntInit *>(this); |
532 | |
533 | if (isa<BitRecTy>(Ty)) { |
534 | int64_t Val = getValue(); |
535 | if (Val != 0 && Val != 1) return nullptr; // Only accept 0 or 1 for a bit! |
536 | return BitInit::get(getRecordKeeper(), Val != 0); |
537 | } |
538 | |
539 | if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) { |
540 | int64_t Value = getValue(); |
541 | // Make sure this bitfield is large enough to hold the integer value. |
542 | if (!canFitInBitfield(Value, BRT->getNumBits())) |
543 | return nullptr; |
544 | |
545 | SmallVector<Init *, 16> NewBits(BRT->getNumBits()); |
546 | for (unsigned i = 0; i != BRT->getNumBits(); ++i) |
547 | NewBits[i] = |
548 | BitInit::get(getRecordKeeper(), Value & ((i < 64) ? (1LL << i) : 0)); |
549 | |
550 | return BitsInit::get(getRecordKeeper(), NewBits); |
551 | } |
552 | |
553 | return nullptr; |
554 | } |
555 | |
556 | Init * |
557 | IntInit::convertInitializerBitRange(ArrayRef<unsigned> Bits) const { |
558 | SmallVector<Init *, 16> NewBits(Bits.size()); |
559 | |
560 | for (unsigned i = 0, e = Bits.size(); i != e; ++i) { |
561 | if (Bits[i] >= 64) |
562 | return nullptr; |
563 | |
564 | NewBits[i] = |
565 | BitInit::get(getRecordKeeper(), Value & (INT64_C(1)1L << Bits[i])); |
566 | } |
567 | return BitsInit::get(getRecordKeeper(), NewBits); |
568 | } |
569 | |
570 | AnonymousNameInit *AnonymousNameInit::get(RecordKeeper &RK, unsigned V) { |
571 | return new (RK.getImpl().Allocator) AnonymousNameInit(RK, V); |
572 | } |
573 | |
574 | StringInit *AnonymousNameInit::getNameInit() const { |
575 | return StringInit::get(getRecordKeeper(), getAsString()); |
576 | } |
577 | |
578 | std::string AnonymousNameInit::getAsString() const { |
579 | return "anonymous_" + utostr(Value); |
580 | } |
581 | |
582 | Init *AnonymousNameInit::resolveReferences(Resolver &R) const { |
583 | auto *Old = const_cast<Init *>(static_cast<const Init *>(this)); |
584 | auto *New = R.resolve(Old); |
585 | New = New ? New : Old; |
586 | if (R.isFinal()) |
587 | if (auto *Anonymous = dyn_cast<AnonymousNameInit>(New)) |
588 | return Anonymous->getNameInit(); |
589 | return New; |
590 | } |
591 | |
592 | StringInit *StringInit::get(RecordKeeper &RK, StringRef V, StringFormat Fmt) { |
593 | detail::RecordKeeperImpl &RKImpl = RK.getImpl(); |
594 | auto &InitMap = Fmt == SF_String ? RKImpl.StringInitStringPool |
595 | : RKImpl.StringInitCodePool; |
596 | auto &Entry = *InitMap.insert(std::make_pair(V, nullptr)).first; |
597 | if (!Entry.second) |
598 | Entry.second = new (RKImpl.Allocator) StringInit(RK, Entry.getKey(), Fmt); |
599 | return Entry.second; |
600 | } |
601 | |
602 | Init *StringInit::convertInitializerTo(RecTy *Ty) const { |
603 | if (isa<StringRecTy>(Ty)) |
604 | return const_cast<StringInit *>(this); |
605 | |
606 | return nullptr; |
607 | } |
608 | |
609 | static void ProfileListInit(FoldingSetNodeID &ID, |
610 | ArrayRef<Init *> Range, |
611 | RecTy *EltTy) { |
612 | ID.AddInteger(Range.size()); |
613 | ID.AddPointer(EltTy); |
614 | |
615 | for (Init *I : Range) |
616 | ID.AddPointer(I); |
617 | } |
618 | |
619 | ListInit *ListInit::get(ArrayRef<Init *> Range, RecTy *EltTy) { |
620 | FoldingSetNodeID ID; |
621 | ProfileListInit(ID, Range, EltTy); |
622 | |
623 | detail::RecordKeeperImpl &RK = EltTy->getRecordKeeper().getImpl(); |
624 | void *IP = nullptr; |
625 | if (ListInit *I = RK.TheListInitPool.FindNodeOrInsertPos(ID, IP)) |
626 | return I; |
627 | |
628 | assert(Range.empty() || !isa<TypedInit>(Range[0]) ||(static_cast <bool> (Range.empty() || !isa<TypedInit >(Range[0]) || cast<TypedInit>(Range[0])->getType ()->typeIsConvertibleTo(EltTy)) ? void (0) : __assert_fail ("Range.empty() || !isa<TypedInit>(Range[0]) || cast<TypedInit>(Range[0])->getType()->typeIsConvertibleTo(EltTy)" , "llvm/lib/TableGen/Record.cpp", 629, __extension__ __PRETTY_FUNCTION__ )) |
629 | cast<TypedInit>(Range[0])->getType()->typeIsConvertibleTo(EltTy))(static_cast <bool> (Range.empty() || !isa<TypedInit >(Range[0]) || cast<TypedInit>(Range[0])->getType ()->typeIsConvertibleTo(EltTy)) ? void (0) : __assert_fail ("Range.empty() || !isa<TypedInit>(Range[0]) || cast<TypedInit>(Range[0])->getType()->typeIsConvertibleTo(EltTy)" , "llvm/lib/TableGen/Record.cpp", 629, __extension__ __PRETTY_FUNCTION__ )); |
630 | |
631 | void *Mem = RK.Allocator.Allocate(totalSizeToAlloc<Init *>(Range.size()), |
632 | alignof(ListInit)); |
633 | ListInit *I = new (Mem) ListInit(Range.size(), EltTy); |
634 | std::uninitialized_copy(Range.begin(), Range.end(), |
635 | I->getTrailingObjects<Init *>()); |
636 | RK.TheListInitPool.InsertNode(I, IP); |
637 | return I; |
638 | } |
639 | |
640 | void ListInit::Profile(FoldingSetNodeID &ID) const { |
641 | RecTy *EltTy = cast<ListRecTy>(getType())->getElementType(); |
642 | |
643 | ProfileListInit(ID, getValues(), EltTy); |
644 | } |
645 | |
646 | Init *ListInit::convertInitializerTo(RecTy *Ty) const { |
647 | if (getType() == Ty) |
648 | return const_cast<ListInit*>(this); |
649 | |
650 | if (auto *LRT = dyn_cast<ListRecTy>(Ty)) { |
651 | SmallVector<Init*, 8> Elements; |
652 | Elements.reserve(getValues().size()); |
653 | |
654 | // Verify that all of the elements of the list are subclasses of the |
655 | // appropriate class! |
656 | bool Changed = false; |
657 | RecTy *ElementType = LRT->getElementType(); |
658 | for (Init *I : getValues()) |
659 | if (Init *CI = I->convertInitializerTo(ElementType)) { |
660 | Elements.push_back(CI); |
661 | if (CI != I) |
662 | Changed = true; |
663 | } else |
664 | return nullptr; |
665 | |
666 | if (!Changed) |
667 | return const_cast<ListInit*>(this); |
668 | return ListInit::get(Elements, ElementType); |
669 | } |
670 | |
671 | return nullptr; |
672 | } |
673 | |
674 | Record *ListInit::getElementAsRecord(unsigned i) const { |
675 | assert(i < NumValues && "List element index out of range!")(static_cast <bool> (i < NumValues && "List element index out of range!" ) ? void (0) : __assert_fail ("i < NumValues && \"List element index out of range!\"" , "llvm/lib/TableGen/Record.cpp", 675, __extension__ __PRETTY_FUNCTION__ )); |
676 | DefInit *DI = dyn_cast<DefInit>(getElement(i)); |
677 | if (!DI) |
678 | PrintFatalError("Expected record in list!"); |
679 | return DI->getDef(); |
680 | } |
681 | |
682 | Init *ListInit::resolveReferences(Resolver &R) const { |
683 | SmallVector<Init*, 8> Resolved; |
684 | Resolved.reserve(size()); |
685 | bool Changed = false; |
686 | |
687 | for (Init *CurElt : getValues()) { |
688 | Init *E = CurElt->resolveReferences(R); |
689 | Changed |= E != CurElt; |
690 | Resolved.push_back(E); |
691 | } |
692 | |
693 | if (Changed) |
694 | return ListInit::get(Resolved, getElementType()); |
695 | return const_cast<ListInit *>(this); |
696 | } |
697 | |
698 | bool ListInit::isComplete() const { |
699 | for (Init *Element : *this) { |
700 | if (!Element->isComplete()) |
701 | return false; |
702 | } |
703 | return true; |
704 | } |
705 | |
706 | bool ListInit::isConcrete() const { |
707 | for (Init *Element : *this) { |
708 | if (!Element->isConcrete()) |
709 | return false; |
710 | } |
711 | return true; |
712 | } |
713 | |
714 | std::string ListInit::getAsString() const { |
715 | std::string Result = "["; |
716 | const char *sep = ""; |
717 | for (Init *Element : *this) { |
718 | Result += sep; |
719 | sep = ", "; |
720 | Result += Element->getAsString(); |
721 | } |
722 | return Result + "]"; |
723 | } |
724 | |
725 | Init *OpInit::getBit(unsigned Bit) const { |
726 | if (getType() == BitRecTy::get(getRecordKeeper())) |
727 | return const_cast<OpInit*>(this); |
728 | return VarBitInit::get(const_cast<OpInit*>(this), Bit); |
729 | } |
730 | |
731 | static void |
732 | ProfileUnOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *Op, RecTy *Type) { |
733 | ID.AddInteger(Opcode); |
734 | ID.AddPointer(Op); |
735 | ID.AddPointer(Type); |
736 | } |
737 | |
738 | UnOpInit *UnOpInit::get(UnaryOp Opc, Init *LHS, RecTy *Type) { |
739 | FoldingSetNodeID ID; |
740 | ProfileUnOpInit(ID, Opc, LHS, Type); |
741 | |
742 | detail::RecordKeeperImpl &RK = Type->getRecordKeeper().getImpl(); |
743 | void *IP = nullptr; |
744 | if (UnOpInit *I = RK.TheUnOpInitPool.FindNodeOrInsertPos(ID, IP)) |
745 | return I; |
746 | |
747 | UnOpInit *I = new (RK.Allocator) UnOpInit(Opc, LHS, Type); |
748 | RK.TheUnOpInitPool.InsertNode(I, IP); |
749 | return I; |
750 | } |
751 | |
752 | void UnOpInit::Profile(FoldingSetNodeID &ID) const { |
753 | ProfileUnOpInit(ID, getOpcode(), getOperand(), getType()); |
754 | } |
755 | |
756 | Init *UnOpInit::Fold(Record *CurRec, bool IsFinal) const { |
757 | RecordKeeper &RK = getRecordKeeper(); |
758 | switch (getOpcode()) { |
759 | case TOLOWER: |
760 | if (StringInit *LHSs = dyn_cast<StringInit>(LHS)) |
761 | return StringInit::get(RK, LHSs->getValue().lower()); |
762 | break; |
763 | case TOUPPER: |
764 | if (StringInit *LHSs = dyn_cast<StringInit>(LHS)) |
765 | return StringInit::get(RK, LHSs->getValue().upper()); |
766 | break; |
767 | case CAST: |
768 | if (isa<StringRecTy>(getType())) { |
769 | if (StringInit *LHSs = dyn_cast<StringInit>(LHS)) |
770 | return LHSs; |
771 | |
772 | if (DefInit *LHSd = dyn_cast<DefInit>(LHS)) |
773 | return StringInit::get(RK, LHSd->getAsString()); |
774 | |
775 | if (IntInit *LHSi = dyn_cast_or_null<IntInit>( |
776 | LHS->convertInitializerTo(IntRecTy::get(RK)))) |
777 | return StringInit::get(RK, LHSi->getAsString()); |
778 | |
779 | } else if (isa<RecordRecTy>(getType())) { |
780 | if (StringInit *Name = dyn_cast<StringInit>(LHS)) { |
781 | Record *D = RK.getDef(Name->getValue()); |
782 | if (!D && CurRec) { |
783 | // Self-references are allowed, but their resolution is delayed until |
784 | // the final resolve to ensure that we get the correct type for them. |
785 | auto *Anonymous = dyn_cast<AnonymousNameInit>(CurRec->getNameInit()); |
786 | if (Name == CurRec->getNameInit() || |
787 | (Anonymous && Name == Anonymous->getNameInit())) { |
788 | if (!IsFinal) |
789 | break; |
790 | D = CurRec; |
791 | } |
792 | } |
793 | |
794 | auto PrintFatalErrorHelper = [CurRec](const Twine &T) { |
795 | if (CurRec) |
796 | PrintFatalError(CurRec->getLoc(), T); |
797 | else |
798 | PrintFatalError(T); |
799 | }; |
800 | |
801 | if (!D) { |
802 | if (IsFinal) { |
803 | PrintFatalErrorHelper(Twine("Undefined reference to record: '") + |
804 | Name->getValue() + "'\n"); |
805 | } |
806 | break; |
807 | } |
808 | |
809 | DefInit *DI = DefInit::get(D); |
810 | if (!DI->getType()->typeIsA(getType())) { |
811 | PrintFatalErrorHelper(Twine("Expected type '") + |
812 | getType()->getAsString() + "', got '" + |
813 | DI->getType()->getAsString() + "' in: " + |
814 | getAsString() + "\n"); |
815 | } |
816 | return DI; |
817 | } |
818 | } |
819 | |
820 | if (Init *NewInit = LHS->convertInitializerTo(getType())) |
821 | return NewInit; |
822 | break; |
823 | |
824 | case NOT: |
825 | if (IntInit *LHSi = dyn_cast_or_null<IntInit>( |
826 | LHS->convertInitializerTo(IntRecTy::get(RK)))) |
827 | return IntInit::get(RK, LHSi->getValue() ? 0 : 1); |
828 | break; |
829 | |
830 | case HEAD: |
831 | if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) { |
832 | assert(!LHSl->empty() && "Empty list in head")(static_cast <bool> (!LHSl->empty() && "Empty list in head" ) ? void (0) : __assert_fail ("!LHSl->empty() && \"Empty list in head\"" , "llvm/lib/TableGen/Record.cpp", 832, __extension__ __PRETTY_FUNCTION__ )); |
833 | return LHSl->getElement(0); |
834 | } |
835 | break; |
836 | |
837 | case TAIL: |
838 | if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) { |
839 | assert(!LHSl->empty() && "Empty list in tail")(static_cast <bool> (!LHSl->empty() && "Empty list in tail" ) ? void (0) : __assert_fail ("!LHSl->empty() && \"Empty list in tail\"" , "llvm/lib/TableGen/Record.cpp", 839, __extension__ __PRETTY_FUNCTION__ )); |
840 | // Note the +1. We can't just pass the result of getValues() |
841 | // directly. |
842 | return ListInit::get(LHSl->getValues().slice(1), LHSl->getElementType()); |
843 | } |
844 | break; |
845 | |
846 | case SIZE: |
847 | if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) |
848 | return IntInit::get(RK, LHSl->size()); |
849 | if (DagInit *LHSd = dyn_cast<DagInit>(LHS)) |
850 | return IntInit::get(RK, LHSd->arg_size()); |
851 | if (StringInit *LHSs = dyn_cast<StringInit>(LHS)) |
852 | return IntInit::get(RK, LHSs->getValue().size()); |
853 | break; |
854 | |
855 | case EMPTY: |
856 | if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) |
857 | return IntInit::get(RK, LHSl->empty()); |
858 | if (DagInit *LHSd = dyn_cast<DagInit>(LHS)) |
859 | return IntInit::get(RK, LHSd->arg_empty()); |
860 | if (StringInit *LHSs = dyn_cast<StringInit>(LHS)) |
861 | return IntInit::get(RK, LHSs->getValue().empty()); |
862 | break; |
863 | |
864 | case GETDAGOP: |
865 | if (DagInit *Dag = dyn_cast<DagInit>(LHS)) { |
866 | DefInit *DI = DefInit::get(Dag->getOperatorAsDef({})); |
867 | if (!DI->getType()->typeIsA(getType())) { |
868 | PrintFatalError(CurRec->getLoc(), |
869 | Twine("Expected type '") + |
870 | getType()->getAsString() + "', got '" + |
871 | DI->getType()->getAsString() + "' in: " + |
872 | getAsString() + "\n"); |
873 | } else { |
874 | return DI; |
875 | } |
876 | } |
877 | break; |
878 | |
879 | case LOG2: |
880 | if (IntInit *LHSi = dyn_cast_or_null<IntInit>( |
881 | LHS->convertInitializerTo(IntRecTy::get(RK)))) { |
882 | int64_t LHSv = LHSi->getValue(); |
883 | if (LHSv <= 0) { |
884 | PrintFatalError(CurRec->getLoc(), |
885 | "Illegal operation: logtwo is undefined " |
886 | "on arguments less than or equal to 0"); |
887 | } else { |
888 | uint64_t Log = Log2_64(LHSv); |
889 | assert(Log <= INT64_MAX &&(static_cast <bool> (Log <= (9223372036854775807L) && "Log of an int64_t must be smaller than INT64_MAX") ? void ( 0) : __assert_fail ("Log <= INT64_MAX && \"Log of an int64_t must be smaller than INT64_MAX\"" , "llvm/lib/TableGen/Record.cpp", 890, __extension__ __PRETTY_FUNCTION__ )) |
890 | "Log of an int64_t must be smaller than INT64_MAX")(static_cast <bool> (Log <= (9223372036854775807L) && "Log of an int64_t must be smaller than INT64_MAX") ? void ( 0) : __assert_fail ("Log <= INT64_MAX && \"Log of an int64_t must be smaller than INT64_MAX\"" , "llvm/lib/TableGen/Record.cpp", 890, __extension__ __PRETTY_FUNCTION__ )); |
891 | return IntInit::get(RK, static_cast<int64_t>(Log)); |
892 | } |
893 | } |
894 | break; |
895 | } |
896 | return const_cast<UnOpInit *>(this); |
897 | } |
898 | |
899 | Init *UnOpInit::resolveReferences(Resolver &R) const { |
900 | Init *lhs = LHS->resolveReferences(R); |
901 | |
902 | if (LHS != lhs || (R.isFinal() && getOpcode() == CAST)) |
903 | return (UnOpInit::get(getOpcode(), lhs, getType())) |
904 | ->Fold(R.getCurrentRecord(), R.isFinal()); |
905 | return const_cast<UnOpInit *>(this); |
906 | } |
907 | |
908 | std::string UnOpInit::getAsString() const { |
909 | std::string Result; |
910 | switch (getOpcode()) { |
911 | case CAST: Result = "!cast<" + getType()->getAsString() + ">"; break; |
912 | case NOT: Result = "!not"; break; |
913 | case HEAD: Result = "!head"; break; |
914 | case TAIL: Result = "!tail"; break; |
915 | case SIZE: Result = "!size"; break; |
916 | case EMPTY: Result = "!empty"; break; |
917 | case GETDAGOP: Result = "!getdagop"; break; |
918 | case LOG2 : Result = "!logtwo"; break; |
919 | case TOLOWER: |
920 | Result = "!tolower"; |
921 | break; |
922 | case TOUPPER: |
923 | Result = "!toupper"; |
924 | break; |
925 | } |
926 | return Result + "(" + LHS->getAsString() + ")"; |
927 | } |
928 | |
929 | static void |
930 | ProfileBinOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *LHS, Init *RHS, |
931 | RecTy *Type) { |
932 | ID.AddInteger(Opcode); |
933 | ID.AddPointer(LHS); |
934 | ID.AddPointer(RHS); |
935 | ID.AddPointer(Type); |
936 | } |
937 | |
938 | BinOpInit *BinOpInit::get(BinaryOp Opc, Init *LHS, Init *RHS, RecTy *Type) { |
939 | FoldingSetNodeID ID; |
940 | ProfileBinOpInit(ID, Opc, LHS, RHS, Type); |
941 | |
942 | detail::RecordKeeperImpl &RK = LHS->getRecordKeeper().getImpl(); |
943 | void *IP = nullptr; |
944 | if (BinOpInit *I = RK.TheBinOpInitPool.FindNodeOrInsertPos(ID, IP)) |
945 | return I; |
946 | |
947 | BinOpInit *I = new (RK.Allocator) BinOpInit(Opc, LHS, RHS, Type); |
948 | RK.TheBinOpInitPool.InsertNode(I, IP); |
949 | return I; |
950 | } |
951 | |
952 | void BinOpInit::Profile(FoldingSetNodeID &ID) const { |
953 | ProfileBinOpInit(ID, getOpcode(), getLHS(), getRHS(), getType()); |
954 | } |
955 | |
956 | static StringInit *ConcatStringInits(const StringInit *I0, |
957 | const StringInit *I1) { |
958 | SmallString<80> Concat(I0->getValue()); |
959 | Concat.append(I1->getValue()); |
960 | return StringInit::get( |
961 | I0->getRecordKeeper(), Concat, |
962 | StringInit::determineFormat(I0->getFormat(), I1->getFormat())); |
963 | } |
964 | |
965 | static StringInit *interleaveStringList(const ListInit *List, |
966 | const StringInit *Delim) { |
967 | if (List->size() == 0) |
968 | return StringInit::get(List->getRecordKeeper(), ""); |
969 | StringInit *Element = dyn_cast<StringInit>(List->getElement(0)); |
970 | if (!Element) |
971 | return nullptr; |
972 | SmallString<80> Result(Element->getValue()); |
973 | StringInit::StringFormat Fmt = StringInit::SF_String; |
974 | |
975 | for (unsigned I = 1, E = List->size(); I < E; ++I) { |
976 | Result.append(Delim->getValue()); |
977 | StringInit *Element = dyn_cast<StringInit>(List->getElement(I)); |
978 | if (!Element) |
979 | return nullptr; |
980 | Result.append(Element->getValue()); |
981 | Fmt = StringInit::determineFormat(Fmt, Element->getFormat()); |
982 | } |
983 | return StringInit::get(List->getRecordKeeper(), Result, Fmt); |
984 | } |
985 | |
986 | static StringInit *interleaveIntList(const ListInit *List, |
987 | const StringInit *Delim) { |
988 | RecordKeeper &RK = List->getRecordKeeper(); |
989 | if (List->size() == 0) |
990 | return StringInit::get(RK, ""); |
991 | IntInit *Element = dyn_cast_or_null<IntInit>( |
992 | List->getElement(0)->convertInitializerTo(IntRecTy::get(RK))); |
993 | if (!Element) |
994 | return nullptr; |
995 | SmallString<80> Result(Element->getAsString()); |
996 | |
997 | for (unsigned I = 1, E = List->size(); I < E; ++I) { |
998 | Result.append(Delim->getValue()); |
999 | IntInit *Element = dyn_cast_or_null<IntInit>( |
1000 | List->getElement(I)->convertInitializerTo(IntRecTy::get(RK))); |
1001 | if (!Element) |
1002 | return nullptr; |
1003 | Result.append(Element->getAsString()); |
1004 | } |
1005 | return StringInit::get(RK, Result); |
1006 | } |
1007 | |
1008 | Init *BinOpInit::getStrConcat(Init *I0, Init *I1) { |
1009 | // Shortcut for the common case of concatenating two strings. |
1010 | if (const StringInit *I0s = dyn_cast<StringInit>(I0)) |
1011 | if (const StringInit *I1s = dyn_cast<StringInit>(I1)) |
1012 | return ConcatStringInits(I0s, I1s); |
1013 | return BinOpInit::get(BinOpInit::STRCONCAT, I0, I1, |
1014 | StringRecTy::get(I0->getRecordKeeper())); |
1015 | } |
1016 | |
1017 | static ListInit *ConcatListInits(const ListInit *LHS, |
1018 | const ListInit *RHS) { |
1019 | SmallVector<Init *, 8> Args; |
1020 | llvm::append_range(Args, *LHS); |
1021 | llvm::append_range(Args, *RHS); |
1022 | return ListInit::get(Args, LHS->getElementType()); |
1023 | } |
1024 | |
1025 | Init *BinOpInit::getListConcat(TypedInit *LHS, Init *RHS) { |
1026 | assert(isa<ListRecTy>(LHS->getType()) && "First arg must be a list")(static_cast <bool> (isa<ListRecTy>(LHS->getType ()) && "First arg must be a list") ? void (0) : __assert_fail ("isa<ListRecTy>(LHS->getType()) && \"First arg must be a list\"" , "llvm/lib/TableGen/Record.cpp", 1026, __extension__ __PRETTY_FUNCTION__ )); |
1027 | |
1028 | // Shortcut for the common case of concatenating two lists. |
1029 | if (const ListInit *LHSList = dyn_cast<ListInit>(LHS)) |
1030 | if (const ListInit *RHSList = dyn_cast<ListInit>(RHS)) |
1031 | return ConcatListInits(LHSList, RHSList); |
1032 | return BinOpInit::get(BinOpInit::LISTCONCAT, LHS, RHS, LHS->getType()); |
1033 | } |
1034 | |
1035 | std::optional<bool> BinOpInit::CompareInit(unsigned Opc, Init *LHS, Init *RHS) const { |
1036 | // First see if we have two bit, bits, or int. |
1037 | IntInit *LHSi = dyn_cast_or_null<IntInit>( |
1038 | LHS->convertInitializerTo(IntRecTy::get(getRecordKeeper()))); |
1039 | IntInit *RHSi = dyn_cast_or_null<IntInit>( |
1040 | RHS->convertInitializerTo(IntRecTy::get(getRecordKeeper()))); |
1041 | |
1042 | if (LHSi && RHSi) { |
1043 | bool Result; |
1044 | switch (Opc) { |
1045 | case EQ: |
1046 | Result = LHSi->getValue() == RHSi->getValue(); |
1047 | break; |
1048 | case NE: |
1049 | Result = LHSi->getValue() != RHSi->getValue(); |
1050 | break; |
1051 | case LE: |
1052 | Result = LHSi->getValue() <= RHSi->getValue(); |
1053 | break; |
1054 | case LT: |
1055 | Result = LHSi->getValue() < RHSi->getValue(); |
1056 | break; |
1057 | case GE: |
1058 | Result = LHSi->getValue() >= RHSi->getValue(); |
1059 | break; |
1060 | case GT: |
1061 | Result = LHSi->getValue() > RHSi->getValue(); |
1062 | break; |
1063 | default: |
1064 | llvm_unreachable("unhandled comparison")::llvm::llvm_unreachable_internal("unhandled comparison", "llvm/lib/TableGen/Record.cpp" , 1064); |
1065 | } |
1066 | return Result; |
1067 | } |
1068 | |
1069 | // Next try strings. |
1070 | StringInit *LHSs = dyn_cast<StringInit>(LHS); |
1071 | StringInit *RHSs = dyn_cast<StringInit>(RHS); |
1072 | |
1073 | if (LHSs && RHSs) { |
1074 | bool Result; |
1075 | switch (Opc) { |
1076 | case EQ: |
1077 | Result = LHSs->getValue() == RHSs->getValue(); |
1078 | break; |
1079 | case NE: |
1080 | Result = LHSs->getValue() != RHSs->getValue(); |
1081 | break; |
1082 | case LE: |
1083 | Result = LHSs->getValue() <= RHSs->getValue(); |
1084 | break; |
1085 | case LT: |
1086 | Result = LHSs->getValue() < RHSs->getValue(); |
1087 | break; |
1088 | case GE: |
1089 | Result = LHSs->getValue() >= RHSs->getValue(); |
1090 | break; |
1091 | case GT: |
1092 | Result = LHSs->getValue() > RHSs->getValue(); |
1093 | break; |
1094 | default: |
1095 | llvm_unreachable("unhandled comparison")::llvm::llvm_unreachable_internal("unhandled comparison", "llvm/lib/TableGen/Record.cpp" , 1095); |
1096 | } |
1097 | return Result; |
1098 | } |
1099 | |
1100 | // Finally, !eq and !ne can be used with records. |
1101 | if (Opc == EQ || Opc == NE) { |
1102 | DefInit *LHSd = dyn_cast<DefInit>(LHS); |
1103 | DefInit *RHSd = dyn_cast<DefInit>(RHS); |
1104 | if (LHSd && RHSd) |
1105 | return (Opc == EQ) ? LHSd == RHSd : LHSd != RHSd; |
1106 | } |
1107 | |
1108 | return std::nullopt; |
1109 | } |
1110 | |
1111 | Init *BinOpInit::Fold(Record *CurRec) const { |
1112 | switch (getOpcode()) { |
1113 | case CONCAT: { |
1114 | DagInit *LHSs = dyn_cast<DagInit>(LHS); |
1115 | DagInit *RHSs = dyn_cast<DagInit>(RHS); |
1116 | if (LHSs && RHSs) { |
1117 | DefInit *LOp = dyn_cast<DefInit>(LHSs->getOperator()); |
1118 | DefInit *ROp = dyn_cast<DefInit>(RHSs->getOperator()); |
1119 | if ((!LOp && !isa<UnsetInit>(LHSs->getOperator())) || |
1120 | (!ROp && !isa<UnsetInit>(RHSs->getOperator()))) |
1121 | break; |
1122 | if (LOp && ROp && LOp->getDef() != ROp->getDef()) { |
1123 | PrintFatalError(Twine("Concatenated Dag operators do not match: '") + |
1124 | LHSs->getAsString() + "' vs. '" + RHSs->getAsString() + |
1125 | "'"); |
1126 | } |
1127 | Init *Op = LOp ? LOp : ROp; |
1128 | if (!Op) |
1129 | Op = UnsetInit::get(getRecordKeeper()); |
1130 | |
1131 | SmallVector<Init*, 8> Args; |
1132 | SmallVector<StringInit*, 8> ArgNames; |
1133 | for (unsigned i = 0, e = LHSs->getNumArgs(); i != e; ++i) { |
1134 | Args.push_back(LHSs->getArg(i)); |
1135 | ArgNames.push_back(LHSs->getArgName(i)); |
1136 | } |
1137 | for (unsigned i = 0, e = RHSs->getNumArgs(); i != e; ++i) { |
1138 | Args.push_back(RHSs->getArg(i)); |
1139 | ArgNames.push_back(RHSs->getArgName(i)); |
1140 | } |
1141 | return DagInit::get(Op, nullptr, Args, ArgNames); |
1142 | } |
1143 | break; |
1144 | } |
1145 | case LISTCONCAT: { |
1146 | ListInit *LHSs = dyn_cast<ListInit>(LHS); |
1147 | ListInit *RHSs = dyn_cast<ListInit>(RHS); |
1148 | if (LHSs && RHSs) { |
1149 | SmallVector<Init *, 8> Args; |
1150 | llvm::append_range(Args, *LHSs); |
1151 | llvm::append_range(Args, *RHSs); |
1152 | return ListInit::get(Args, LHSs->getElementType()); |
1153 | } |
1154 | break; |
1155 | } |
1156 | case LISTSPLAT: { |
1157 | TypedInit *Value = dyn_cast<TypedInit>(LHS); |
1158 | IntInit *Size = dyn_cast<IntInit>(RHS); |
1159 | if (Value && Size) { |
1160 | SmallVector<Init *, 8> Args(Size->getValue(), Value); |
1161 | return ListInit::get(Args, Value->getType()); |
1162 | } |
1163 | break; |
1164 | } |
1165 | case LISTREMOVE: { |
1166 | ListInit *LHSs = dyn_cast<ListInit>(LHS); |
1167 | ListInit *RHSs = dyn_cast<ListInit>(RHS); |
1168 | if (LHSs && RHSs) { |
1169 | SmallVector<Init *, 8> Args; |
1170 | for (Init *EltLHS : *LHSs) { |
1171 | bool Found = false; |
1172 | for (Init *EltRHS : *RHSs) { |
1173 | if (std::optional<bool> Result = CompareInit(EQ, EltLHS, EltRHS)) { |
1174 | if (*Result) { |
1175 | Found = true; |
1176 | break; |
1177 | } |
1178 | } |
1179 | } |
1180 | if (!Found) |
1181 | Args.push_back(EltLHS); |
1182 | } |
1183 | return ListInit::get(Args, LHSs->getElementType()); |
1184 | } |
1185 | break; |
1186 | } |
1187 | case LISTELEM: { |
1188 | auto *TheList = dyn_cast<ListInit>(LHS); |
1189 | auto *Idx = dyn_cast<IntInit>(RHS); |
1190 | if (!TheList || !Idx) |
1191 | break; |
1192 | auto i = Idx->getValue(); |
1193 | if (i < 0 || i >= (ssize_t)TheList->size()) |
1194 | break; |
1195 | return TheList->getElement(i); |
1196 | } |
1197 | case LISTSLICE: { |
1198 | auto *TheList = dyn_cast<ListInit>(LHS); |
1199 | auto *SliceIdxs = dyn_cast<ListInit>(RHS); |
1200 | if (!TheList || !SliceIdxs) |
1201 | break; |
1202 | SmallVector<Init *, 8> Args; |
1203 | Args.reserve(SliceIdxs->size()); |
1204 | for (auto *I : *SliceIdxs) { |
1205 | auto *II = dyn_cast<IntInit>(I); |
1206 | if (!II) |
1207 | goto unresolved; |
1208 | auto i = II->getValue(); |
1209 | if (i < 0 || i >= (ssize_t)TheList->size()) |
1210 | goto unresolved; |
1211 | Args.push_back(TheList->getElement(i)); |
1212 | } |
1213 | return ListInit::get(Args, TheList->getElementType()); |
1214 | } |
1215 | case RANGE: |
1216 | case RANGEC: { |
1217 | auto *LHSi = dyn_cast<IntInit>(LHS); |
1218 | auto *RHSi = dyn_cast<IntInit>(RHS); |
1219 | if (!LHSi || !RHSi) |
1220 | break; |
1221 | |
1222 | auto Start = LHSi->getValue(); |
1223 | auto End = RHSi->getValue(); |
1224 | SmallVector<Init *, 8> Args; |
1225 | if (getOpcode() == RANGEC) { |
1226 | // Closed interval |
1227 | if (Start <= End) { |
1228 | // Ascending order |
1229 | Args.reserve(End - Start + 1); |
1230 | for (auto i = Start; i <= End; ++i) |
1231 | Args.push_back(IntInit::get(getRecordKeeper(), i)); |
1232 | } else { |
1233 | // Descending order |
1234 | Args.reserve(Start - End + 1); |
1235 | for (auto i = Start; i >= End; --i) |
1236 | Args.push_back(IntInit::get(getRecordKeeper(), i)); |
1237 | } |
1238 | } else if (Start < End) { |
1239 | // Half-open interval (excludes `End`) |
1240 | Args.reserve(End - Start); |
1241 | for (auto i = Start; i < End; ++i) |
1242 | Args.push_back(IntInit::get(getRecordKeeper(), i)); |
1243 | } else { |
1244 | // Empty set |
1245 | } |
1246 | return ListInit::get(Args, LHSi->getType()); |
1247 | } |
1248 | case STRCONCAT: { |
1249 | StringInit *LHSs = dyn_cast<StringInit>(LHS); |
1250 | StringInit *RHSs = dyn_cast<StringInit>(RHS); |
1251 | if (LHSs && RHSs) |
1252 | return ConcatStringInits(LHSs, RHSs); |
1253 | break; |
1254 | } |
1255 | case INTERLEAVE: { |
1256 | ListInit *List = dyn_cast<ListInit>(LHS); |
1257 | StringInit *Delim = dyn_cast<StringInit>(RHS); |
1258 | if (List && Delim) { |
1259 | StringInit *Result; |
1260 | if (isa<StringRecTy>(List->getElementType())) |
1261 | Result = interleaveStringList(List, Delim); |
1262 | else |
1263 | Result = interleaveIntList(List, Delim); |
1264 | if (Result) |
1265 | return Result; |
1266 | } |
1267 | break; |
1268 | } |
1269 | case EQ: |
1270 | case NE: |
1271 | case LE: |
1272 | case LT: |
1273 | case GE: |
1274 | case GT: { |
1275 | if (std::optional<bool> Result = CompareInit(getOpcode(), LHS, RHS)) |
1276 | return BitInit::get(getRecordKeeper(), *Result); |
1277 | break; |
1278 | } |
1279 | case SETDAGOP: { |
1280 | DagInit *Dag = dyn_cast<DagInit>(LHS); |
1281 | DefInit *Op = dyn_cast<DefInit>(RHS); |
1282 | if (Dag && Op) { |
1283 | SmallVector<Init*, 8> Args; |
1284 | SmallVector<StringInit*, 8> ArgNames; |
1285 | for (unsigned i = 0, e = Dag->getNumArgs(); i != e; ++i) { |
1286 | Args.push_back(Dag->getArg(i)); |
1287 | ArgNames.push_back(Dag->getArgName(i)); |
1288 | } |
1289 | return DagInit::get(Op, nullptr, Args, ArgNames); |
1290 | } |
1291 | break; |
1292 | } |
1293 | case ADD: |
1294 | case SUB: |
1295 | case MUL: |
1296 | case DIV: |
1297 | case AND: |
1298 | case OR: |
1299 | case XOR: |
1300 | case SHL: |
1301 | case SRA: |
1302 | case SRL: { |
1303 | IntInit *LHSi = dyn_cast_or_null<IntInit>( |
1304 | LHS->convertInitializerTo(IntRecTy::get(getRecordKeeper()))); |
1305 | IntInit *RHSi = dyn_cast_or_null<IntInit>( |
1306 | RHS->convertInitializerTo(IntRecTy::get(getRecordKeeper()))); |
1307 | if (LHSi && RHSi) { |
1308 | int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue(); |
1309 | int64_t Result; |
1310 | switch (getOpcode()) { |
1311 | default: llvm_unreachable("Bad opcode!")::llvm::llvm_unreachable_internal("Bad opcode!", "llvm/lib/TableGen/Record.cpp" , 1311); |
1312 | case ADD: Result = LHSv + RHSv; break; |
1313 | case SUB: Result = LHSv - RHSv; break; |
1314 | case MUL: Result = LHSv * RHSv; break; |
1315 | case DIV: |
1316 | if (RHSv == 0) |
1317 | PrintFatalError(CurRec->getLoc(), |
1318 | "Illegal operation: division by zero"); |
1319 | else if (LHSv == INT64_MIN(-9223372036854775807L -1) && RHSv == -1) |
1320 | PrintFatalError(CurRec->getLoc(), |
1321 | "Illegal operation: INT64_MIN / -1"); |
1322 | else |
1323 | Result = LHSv / RHSv; |
1324 | break; |
1325 | case AND: Result = LHSv & RHSv; break; |
1326 | case OR: Result = LHSv | RHSv; break; |
1327 | case XOR: Result = LHSv ^ RHSv; break; |
1328 | case SHL: Result = (uint64_t)LHSv << (uint64_t)RHSv; break; |
1329 | case SRA: Result = LHSv >> RHSv; break; |
1330 | case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break; |
1331 | } |
1332 | return IntInit::get(getRecordKeeper(), Result); |
1333 | } |
1334 | break; |
1335 | } |
1336 | } |
1337 | unresolved: |
1338 | return const_cast<BinOpInit *>(this); |
1339 | } |
1340 | |
1341 | Init *BinOpInit::resolveReferences(Resolver &R) const { |
1342 | Init *lhs = LHS->resolveReferences(R); |
1343 | Init *rhs = RHS->resolveReferences(R); |
1344 | |
1345 | if (LHS != lhs || RHS != rhs) |
1346 | return (BinOpInit::get(getOpcode(), lhs, rhs, getType())) |
1347 | ->Fold(R.getCurrentRecord()); |
1348 | return const_cast<BinOpInit *>(this); |
1349 | } |
1350 | |
1351 | std::string BinOpInit::getAsString() const { |
1352 | std::string Result; |
1353 | switch (getOpcode()) { |
1354 | case LISTELEM: |
1355 | case LISTSLICE: |
1356 | return LHS->getAsString() + "[" + RHS->getAsString() + "]"; |
1357 | case RANGEC: |
1358 | return LHS->getAsString() + "..." + RHS->getAsString(); |
1359 | case CONCAT: Result = "!con"; break; |
1360 | case ADD: Result = "!add"; break; |
1361 | case SUB: Result = "!sub"; break; |
1362 | case MUL: Result = "!mul"; break; |
1363 | case DIV: Result = "!div"; break; |
1364 | case AND: Result = "!and"; break; |
1365 | case OR: Result = "!or"; break; |
1366 | case XOR: Result = "!xor"; break; |
1367 | case SHL: Result = "!shl"; break; |
1368 | case SRA: Result = "!sra"; break; |
1369 | case SRL: Result = "!srl"; break; |
1370 | case EQ: Result = "!eq"; break; |
1371 | case NE: Result = "!ne"; break; |
1372 | case LE: Result = "!le"; break; |
1373 | case LT: Result = "!lt"; break; |
1374 | case GE: Result = "!ge"; break; |
1375 | case GT: Result = "!gt"; break; |
1376 | case LISTCONCAT: Result = "!listconcat"; break; |
1377 | case LISTSPLAT: Result = "!listsplat"; break; |
1378 | case LISTREMOVE: Result = "!listremove"; break; |
1379 | case RANGE: Result = "!range"; break; |
1380 | case STRCONCAT: Result = "!strconcat"; break; |
1381 | case INTERLEAVE: Result = "!interleave"; break; |
1382 | case SETDAGOP: Result = "!setdagop"; break; |
1383 | } |
1384 | return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")"; |
1385 | } |
1386 | |
1387 | static void |
1388 | ProfileTernOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *LHS, Init *MHS, |
1389 | Init *RHS, RecTy *Type) { |
1390 | ID.AddInteger(Opcode); |
1391 | ID.AddPointer(LHS); |
1392 | ID.AddPointer(MHS); |
1393 | ID.AddPointer(RHS); |
1394 | ID.AddPointer(Type); |
1395 | } |
1396 | |
1397 | TernOpInit *TernOpInit::get(TernaryOp Opc, Init *LHS, Init *MHS, Init *RHS, |
1398 | RecTy *Type) { |
1399 | FoldingSetNodeID ID; |
1400 | ProfileTernOpInit(ID, Opc, LHS, MHS, RHS, Type); |
1401 | |
1402 | detail::RecordKeeperImpl &RK = LHS->getRecordKeeper().getImpl(); |
1403 | void *IP = nullptr; |
1404 | if (TernOpInit *I = RK.TheTernOpInitPool.FindNodeOrInsertPos(ID, IP)) |
1405 | return I; |
1406 | |
1407 | TernOpInit *I = new (RK.Allocator) TernOpInit(Opc, LHS, MHS, RHS, Type); |
1408 | RK.TheTernOpInitPool.InsertNode(I, IP); |
1409 | return I; |
1410 | } |
1411 | |
1412 | void TernOpInit::Profile(FoldingSetNodeID &ID) const { |
1413 | ProfileTernOpInit(ID, getOpcode(), getLHS(), getMHS(), getRHS(), getType()); |
1414 | } |
1415 | |
1416 | static Init *ItemApply(Init *LHS, Init *MHSe, Init *RHS, Record *CurRec) { |
1417 | MapResolver R(CurRec); |
1418 | R.set(LHS, MHSe); |
1419 | return RHS->resolveReferences(R); |
1420 | } |
1421 | |
1422 | static Init *ForeachDagApply(Init *LHS, DagInit *MHSd, Init *RHS, |
1423 | Record *CurRec) { |
1424 | bool Change = false; |
1425 | Init *Val = ItemApply(LHS, MHSd->getOperator(), RHS, CurRec); |
1426 | if (Val != MHSd->getOperator()) |
1427 | Change = true; |
1428 | |
1429 | SmallVector<std::pair<Init *, StringInit *>, 8> NewArgs; |
1430 | for (unsigned int i = 0; i < MHSd->getNumArgs(); ++i) { |
1431 | Init *Arg = MHSd->getArg(i); |
1432 | Init *NewArg; |
1433 | StringInit *ArgName = MHSd->getArgName(i); |
1434 | |
1435 | if (DagInit *Argd = dyn_cast<DagInit>(Arg)) |
1436 | NewArg = ForeachDagApply(LHS, Argd, RHS, CurRec); |
1437 | else |
1438 | NewArg = ItemApply(LHS, Arg, RHS, CurRec); |
1439 | |
1440 | NewArgs.push_back(std::make_pair(NewArg, ArgName)); |
1441 | if (Arg != NewArg) |
1442 | Change = true; |
1443 | } |
1444 | |
1445 | if (Change) |
1446 | return DagInit::get(Val, nullptr, NewArgs); |
1447 | return MHSd; |
1448 | } |
1449 | |
1450 | // Applies RHS to all elements of MHS, using LHS as a temp variable. |
1451 | static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type, |
1452 | Record *CurRec) { |
1453 | if (DagInit *MHSd = dyn_cast<DagInit>(MHS)) |
1454 | return ForeachDagApply(LHS, MHSd, RHS, CurRec); |
1455 | |
1456 | if (ListInit *MHSl = dyn_cast<ListInit>(MHS)) { |
1457 | SmallVector<Init *, 8> NewList(MHSl->begin(), MHSl->end()); |
1458 | |
1459 | for (Init *&Item : NewList) { |
1460 | Init *NewItem = ItemApply(LHS, Item, RHS, CurRec); |
1461 | if (NewItem != Item) |
1462 | Item = NewItem; |
1463 | } |
1464 | return ListInit::get(NewList, cast<ListRecTy>(Type)->getElementType()); |
1465 | } |
1466 | |
1467 | return nullptr; |
1468 | } |
1469 | |
1470 | // Evaluates RHS for all elements of MHS, using LHS as a temp variable. |
1471 | // Creates a new list with the elements that evaluated to true. |
1472 | static Init *FilterHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type, |
1473 | Record *CurRec) { |
1474 | if (ListInit *MHSl = dyn_cast<ListInit>(MHS)) { |
1475 | SmallVector<Init *, 8> NewList; |
1476 | |
1477 | for (Init *Item : MHSl->getValues()) { |
1478 | Init *Include = ItemApply(LHS, Item, RHS, CurRec); |
1479 | if (!Include) |
1480 | return nullptr; |
1481 | if (IntInit *IncludeInt = |
1482 | dyn_cast_or_null<IntInit>(Include->convertInitializerTo( |
1483 | IntRecTy::get(LHS->getRecordKeeper())))) { |
1484 | if (IncludeInt->getValue()) |
1485 | NewList.push_back(Item); |
1486 | } else { |
1487 | return nullptr; |
1488 | } |
1489 | } |
1490 | return ListInit::get(NewList, cast<ListRecTy>(Type)->getElementType()); |
1491 | } |
1492 | |
1493 | return nullptr; |
1494 | } |
1495 | |
1496 | Init *TernOpInit::Fold(Record *CurRec) const { |
1497 | RecordKeeper &RK = getRecordKeeper(); |
1498 | switch (getOpcode()) { |
1499 | case SUBST: { |
1500 | DefInit *LHSd = dyn_cast<DefInit>(LHS); |
1501 | VarInit *LHSv = dyn_cast<VarInit>(LHS); |
1502 | StringInit *LHSs = dyn_cast<StringInit>(LHS); |
1503 | |
1504 | DefInit *MHSd = dyn_cast<DefInit>(MHS); |
1505 | VarInit *MHSv = dyn_cast<VarInit>(MHS); |
1506 | StringInit *MHSs = dyn_cast<StringInit>(MHS); |
1507 | |
1508 | DefInit *RHSd = dyn_cast<DefInit>(RHS); |
1509 | VarInit *RHSv = dyn_cast<VarInit>(RHS); |
1510 | StringInit *RHSs = dyn_cast<StringInit>(RHS); |
1511 | |
1512 | if (LHSd && MHSd && RHSd) { |
1513 | Record *Val = RHSd->getDef(); |
1514 | if (LHSd->getAsString() == RHSd->getAsString()) |
1515 | Val = MHSd->getDef(); |
1516 | return DefInit::get(Val); |
1517 | } |
1518 | if (LHSv && MHSv && RHSv) { |
1519 | std::string Val = std::string(RHSv->getName()); |
1520 | if (LHSv->getAsString() == RHSv->getAsString()) |
1521 | Val = std::string(MHSv->getName()); |
1522 | return VarInit::get(Val, getType()); |
1523 | } |
1524 | if (LHSs && MHSs && RHSs) { |
1525 | std::string Val = std::string(RHSs->getValue()); |
1526 | |
1527 | std::string::size_type found; |
1528 | std::string::size_type idx = 0; |
1529 | while (true) { |
1530 | found = Val.find(std::string(LHSs->getValue()), idx); |
1531 | if (found == std::string::npos) |
1532 | break; |
1533 | Val.replace(found, LHSs->getValue().size(), |
1534 | std::string(MHSs->getValue())); |
1535 | idx = found + MHSs->getValue().size(); |
1536 | } |
1537 | |
1538 | return StringInit::get(RK, Val); |
1539 | } |
1540 | break; |
1541 | } |
1542 | |
1543 | case FOREACH: { |
1544 | if (Init *Result = ForeachHelper(LHS, MHS, RHS, getType(), CurRec)) |
1545 | return Result; |
1546 | break; |
1547 | } |
1548 | |
1549 | case FILTER: { |
1550 | if (Init *Result = FilterHelper(LHS, MHS, RHS, getType(), CurRec)) |
1551 | return Result; |
1552 | break; |
1553 | } |
1554 | |
1555 | case IF: { |
1556 | if (IntInit *LHSi = dyn_cast_or_null<IntInit>( |
1557 | LHS->convertInitializerTo(IntRecTy::get(RK)))) { |
1558 | if (LHSi->getValue()) |
1559 | return MHS; |
1560 | return RHS; |
1561 | } |
1562 | break; |
1563 | } |
1564 | |
1565 | case DAG: { |
1566 | ListInit *MHSl = dyn_cast<ListInit>(MHS); |
1567 | ListInit *RHSl = dyn_cast<ListInit>(RHS); |
1568 | bool MHSok = MHSl || isa<UnsetInit>(MHS); |
1569 | bool RHSok = RHSl || isa<UnsetInit>(RHS); |
1570 | |
1571 | if (isa<UnsetInit>(MHS) && isa<UnsetInit>(RHS)) |
1572 | break; // Typically prevented by the parser, but might happen with template args |
1573 | |
1574 | if (MHSok && RHSok && (!MHSl || !RHSl || MHSl->size() == RHSl->size())) { |
1575 | SmallVector<std::pair<Init *, StringInit *>, 8> Children; |
1576 | unsigned Size = MHSl ? MHSl->size() : RHSl->size(); |
1577 | for (unsigned i = 0; i != Size; ++i) { |
1578 | Init *Node = MHSl ? MHSl->getElement(i) : UnsetInit::get(RK); |
1579 | Init *Name = RHSl ? RHSl->getElement(i) : UnsetInit::get(RK); |
1580 | if (!isa<StringInit>(Name) && !isa<UnsetInit>(Name)) |
1581 | return const_cast<TernOpInit *>(this); |
1582 | Children.emplace_back(Node, dyn_cast<StringInit>(Name)); |
1583 | } |
1584 | return DagInit::get(LHS, nullptr, Children); |
1585 | } |
1586 | break; |
1587 | } |
1588 | |
1589 | case SUBSTR: { |
1590 | StringInit *LHSs = dyn_cast<StringInit>(LHS); |
1591 | IntInit *MHSi = dyn_cast<IntInit>(MHS); |
1592 | IntInit *RHSi = dyn_cast<IntInit>(RHS); |
1593 | if (LHSs && MHSi && RHSi) { |
1594 | int64_t StringSize = LHSs->getValue().size(); |
1595 | int64_t Start = MHSi->getValue(); |
1596 | int64_t Length = RHSi->getValue(); |
1597 | if (Start < 0 || Start > StringSize) |
1598 | PrintError(CurRec->getLoc(), |
1599 | Twine("!substr start position is out of range 0...") + |
1600 | std::to_string(StringSize) + ": " + |
1601 | std::to_string(Start)); |
1602 | if (Length < 0) |
1603 | PrintError(CurRec->getLoc(), "!substr length must be nonnegative"); |
1604 | return StringInit::get(RK, LHSs->getValue().substr(Start, Length), |
1605 | LHSs->getFormat()); |
1606 | } |
1607 | break; |
1608 | } |
1609 | |
1610 | case FIND: { |
1611 | StringInit *LHSs = dyn_cast<StringInit>(LHS); |
1612 | StringInit *MHSs = dyn_cast<StringInit>(MHS); |
1613 | IntInit *RHSi = dyn_cast<IntInit>(RHS); |
1614 | if (LHSs && MHSs && RHSi) { |
1615 | int64_t SourceSize = LHSs->getValue().size(); |
1616 | int64_t Start = RHSi->getValue(); |
1617 | if (Start < 0 || Start > SourceSize) |
1618 | PrintError(CurRec->getLoc(), |
1619 | Twine("!find start position is out of range 0...") + |
1620 | std::to_string(SourceSize) + ": " + |
1621 | std::to_string(Start)); |
1622 | auto I = LHSs->getValue().find(MHSs->getValue(), Start); |
1623 | if (I == std::string::npos) |
1624 | return IntInit::get(RK, -1); |
1625 | return IntInit::get(RK, I); |
1626 | } |
1627 | break; |
1628 | } |
1629 | } |
1630 | |
1631 | return const_cast<TernOpInit *>(this); |
1632 | } |
1633 | |
1634 | Init *TernOpInit::resolveReferences(Resolver &R) const { |
1635 | Init *lhs = LHS->resolveReferences(R); |
1636 | |
1637 | if (getOpcode() == IF && lhs != LHS) { |
1638 | if (IntInit *Value = dyn_cast_or_null<IntInit>( |
1639 | lhs->convertInitializerTo(IntRecTy::get(getRecordKeeper())))) { |
1640 | // Short-circuit |
1641 | if (Value->getValue()) |
1642 | return MHS->resolveReferences(R); |
1643 | return RHS->resolveReferences(R); |
1644 | } |
1645 | } |
1646 | |
1647 | Init *mhs = MHS->resolveReferences(R); |
1648 | Init *rhs; |
1649 | |
1650 | if (getOpcode() == FOREACH || getOpcode() == FILTER) { |
1651 | ShadowResolver SR(R); |
1652 | SR.addShadow(lhs); |
1653 | rhs = RHS->resolveReferences(SR); |
1654 | } else { |
1655 | rhs = RHS->resolveReferences(R); |
1656 | } |
1657 | |
1658 | if (LHS != lhs || MHS != mhs || RHS != rhs) |
1659 | return (TernOpInit::get(getOpcode(), lhs, mhs, rhs, getType())) |
1660 | ->Fold(R.getCurrentRecord()); |
1661 | return const_cast<TernOpInit *>(this); |
1662 | } |
1663 | |
1664 | std::string TernOpInit::getAsString() const { |
1665 | std::string Result; |
1666 | bool UnquotedLHS = false; |
1667 | switch (getOpcode()) { |
1668 | case DAG: Result = "!dag"; break; |
1669 | case FILTER: Result = "!filter"; UnquotedLHS = true; break; |
1670 | case FOREACH: Result = "!foreach"; UnquotedLHS = true; break; |
1671 | case IF: Result = "!if"; break; |
1672 | case SUBST: Result = "!subst"; break; |
1673 | case SUBSTR: Result = "!substr"; break; |
1674 | case FIND: Result = "!find"; break; |
1675 | } |
1676 | return (Result + "(" + |
1677 | (UnquotedLHS ? LHS->getAsUnquotedString() : LHS->getAsString()) + |
1678 | ", " + MHS->getAsString() + ", " + RHS->getAsString() + ")"); |
1679 | } |
1680 | |
1681 | static void ProfileFoldOpInit(FoldingSetNodeID &ID, Init *Start, Init *List, |
1682 | Init *A, Init *B, Init *Expr, RecTy *Type) { |
1683 | ID.AddPointer(Start); |
1684 | ID.AddPointer(List); |
1685 | ID.AddPointer(A); |
1686 | ID.AddPointer(B); |
1687 | ID.AddPointer(Expr); |
1688 | ID.AddPointer(Type); |
1689 | } |
1690 | |
1691 | FoldOpInit *FoldOpInit::get(Init *Start, Init *List, Init *A, Init *B, |
1692 | Init *Expr, RecTy *Type) { |
1693 | FoldingSetNodeID ID; |
1694 | ProfileFoldOpInit(ID, Start, List, A, B, Expr, Type); |
1695 | |
1696 | detail::RecordKeeperImpl &RK = Start->getRecordKeeper().getImpl(); |
1697 | void *IP = nullptr; |
1698 | if (FoldOpInit *I = RK.TheFoldOpInitPool.FindNodeOrInsertPos(ID, IP)) |
1699 | return I; |
1700 | |
1701 | FoldOpInit *I = new (RK.Allocator) FoldOpInit(Start, List, A, B, Expr, Type); |
1702 | RK.TheFoldOpInitPool.InsertNode(I, IP); |
1703 | return I; |
1704 | } |
1705 | |
1706 | void FoldOpInit::Profile(FoldingSetNodeID &ID) const { |
1707 | ProfileFoldOpInit(ID, Start, List, A, B, Expr, getType()); |
1708 | } |
1709 | |
1710 | Init *FoldOpInit::Fold(Record *CurRec) const { |
1711 | if (ListInit *LI = dyn_cast<ListInit>(List)) { |
1712 | Init *Accum = Start; |
1713 | for (Init *Elt : *LI) { |
1714 | MapResolver R(CurRec); |
1715 | R.set(A, Accum); |
1716 | R.set(B, Elt); |
1717 | Accum = Expr->resolveReferences(R); |
1718 | } |
1719 | return Accum; |
1720 | } |
1721 | return const_cast<FoldOpInit *>(this); |
1722 | } |
1723 | |
1724 | Init *FoldOpInit::resolveReferences(Resolver &R) const { |
1725 | Init *NewStart = Start->resolveReferences(R); |
1726 | Init *NewList = List->resolveReferences(R); |
1727 | ShadowResolver SR(R); |
1728 | SR.addShadow(A); |
1729 | SR.addShadow(B); |
1730 | Init *NewExpr = Expr->resolveReferences(SR); |
1731 | |
1732 | if (Start == NewStart && List == NewList && Expr == NewExpr) |
1733 | return const_cast<FoldOpInit *>(this); |
1734 | |
1735 | return get(NewStart, NewList, A, B, NewExpr, getType()) |
1736 | ->Fold(R.getCurrentRecord()); |
1737 | } |
1738 | |
1739 | Init *FoldOpInit::getBit(unsigned Bit) const { |
1740 | return VarBitInit::get(const_cast<FoldOpInit *>(this), Bit); |
1741 | } |
1742 | |
1743 | std::string FoldOpInit::getAsString() const { |
1744 | return (Twine("!foldl(") + Start->getAsString() + ", " + List->getAsString() + |
1745 | ", " + A->getAsUnquotedString() + ", " + B->getAsUnquotedString() + |
1746 | ", " + Expr->getAsString() + ")") |
1747 | .str(); |
1748 | } |
1749 | |
1750 | static void ProfileIsAOpInit(FoldingSetNodeID &ID, RecTy *CheckType, |
1751 | Init *Expr) { |
1752 | ID.AddPointer(CheckType); |
1753 | ID.AddPointer(Expr); |
1754 | } |
1755 | |
1756 | IsAOpInit *IsAOpInit::get(RecTy *CheckType, Init *Expr) { |
1757 | |
1758 | FoldingSetNodeID ID; |
1759 | ProfileIsAOpInit(ID, CheckType, Expr); |
1760 | |
1761 | detail::RecordKeeperImpl &RK = Expr->getRecordKeeper().getImpl(); |
1762 | void *IP = nullptr; |
1763 | if (IsAOpInit *I = RK.TheIsAOpInitPool.FindNodeOrInsertPos(ID, IP)) |
1764 | return I; |
1765 | |
1766 | IsAOpInit *I = new (RK.Allocator) IsAOpInit(CheckType, Expr); |
1767 | RK.TheIsAOpInitPool.InsertNode(I, IP); |
1768 | return I; |
1769 | } |
1770 | |
1771 | void IsAOpInit::Profile(FoldingSetNodeID &ID) const { |
1772 | ProfileIsAOpInit(ID, CheckType, Expr); |
1773 | } |
1774 | |
1775 | Init *IsAOpInit::Fold() const { |
1776 | if (TypedInit *TI = dyn_cast<TypedInit>(Expr)) { |
1777 | // Is the expression type known to be (a subclass of) the desired type? |
1778 | if (TI->getType()->typeIsConvertibleTo(CheckType)) |
1779 | return IntInit::get(getRecordKeeper(), 1); |
1780 | |
1781 | if (isa<RecordRecTy>(CheckType)) { |
1782 | // If the target type is not a subclass of the expression type, or if |
1783 | // the expression has fully resolved to a record, we know that it can't |
1784 | // be of the required type. |
1785 | if (!CheckType->typeIsConvertibleTo(TI->getType()) || isa<DefInit>(Expr)) |
1786 | return IntInit::get(getRecordKeeper(), 0); |
1787 | } else { |
1788 | // We treat non-record types as not castable. |
1789 | return IntInit::get(getRecordKeeper(), 0); |
1790 | } |
1791 | } |
1792 | return const_cast<IsAOpInit *>(this); |
1793 | } |
1794 | |
1795 | Init *IsAOpInit::resolveReferences(Resolver &R) const { |
1796 | Init *NewExpr = Expr->resolveReferences(R); |
1797 | if (Expr != NewExpr) |
1798 | return get(CheckType, NewExpr)->Fold(); |
1799 | return const_cast<IsAOpInit *>(this); |
1800 | } |
1801 | |
1802 | Init *IsAOpInit::getBit(unsigned Bit) const { |
1803 | return VarBitInit::get(const_cast<IsAOpInit *>(this), Bit); |
1804 | } |
1805 | |
1806 | std::string IsAOpInit::getAsString() const { |
1807 | return (Twine("!isa<") + CheckType->getAsString() + ">(" + |
1808 | Expr->getAsString() + ")") |
1809 | .str(); |
1810 | } |
1811 | |
1812 | static void ProfileExistsOpInit(FoldingSetNodeID &ID, RecTy *CheckType, |
1813 | Init *Expr) { |
1814 | ID.AddPointer(CheckType); |
1815 | ID.AddPointer(Expr); |
1816 | } |
1817 | |
1818 | ExistsOpInit *ExistsOpInit::get(RecTy *CheckType, Init *Expr) { |
1819 | FoldingSetNodeID ID; |
1820 | ProfileExistsOpInit(ID, CheckType, Expr); |
1821 | |
1822 | detail::RecordKeeperImpl &RK = Expr->getRecordKeeper().getImpl(); |
1823 | void *IP = nullptr; |
1824 | if (ExistsOpInit *I = RK.TheExistsOpInitPool.FindNodeOrInsertPos(ID, IP)) |
1825 | return I; |
1826 | |
1827 | ExistsOpInit *I = new (RK.Allocator) ExistsOpInit(CheckType, Expr); |
1828 | RK.TheExistsOpInitPool.InsertNode(I, IP); |
1829 | return I; |
1830 | } |
1831 | |
1832 | void ExistsOpInit::Profile(FoldingSetNodeID &ID) const { |
1833 | ProfileExistsOpInit(ID, CheckType, Expr); |
1834 | } |
1835 | |
1836 | Init *ExistsOpInit::Fold(Record *CurRec, bool IsFinal) const { |
1837 | if (StringInit *Name = dyn_cast<StringInit>(Expr)) { |
1838 | |
1839 | // Look up all defined records to see if we can find one. |
1840 | Record *D = CheckType->getRecordKeeper().getDef(Name->getValue()); |
1841 | if (D) { |
1842 | // Check if types are compatible. |
1843 | return IntInit::get(getRecordKeeper(), |
1844 | DefInit::get(D)->getType()->typeIsA(CheckType)); |
1845 | } |
1846 | |
1847 | if (CurRec) { |
1848 | // Self-references are allowed, but their resolution is delayed until |
1849 | // the final resolve to ensure that we get the correct type for them. |
1850 | auto *Anonymous = dyn_cast<AnonymousNameInit>(CurRec->getNameInit()); |
1851 | if (Name == CurRec->getNameInit() || |
1852 | (Anonymous && Name == Anonymous->getNameInit())) { |
1853 | if (!IsFinal) |
1854 | return const_cast<ExistsOpInit *>(this); |
1855 | |
1856 | // No doubt that there exists a record, so we should check if types are |
1857 | // compatible. |
1858 | return IntInit::get(getRecordKeeper(), |
1859 | CurRec->getType()->typeIsA(CheckType)); |
1860 | } |
1861 | } |
1862 | |
1863 | if (IsFinal) |
1864 | return IntInit::get(getRecordKeeper(), 0); |
1865 | return const_cast<ExistsOpInit *>(this); |
1866 | } |
1867 | return const_cast<ExistsOpInit *>(this); |
1868 | } |
1869 | |
1870 | Init *ExistsOpInit::resolveReferences(Resolver &R) const { |
1871 | Init *NewExpr = Expr->resolveReferences(R); |
1872 | if (Expr != NewExpr || R.isFinal()) |
1873 | return get(CheckType, NewExpr)->Fold(R.getCurrentRecord(), R.isFinal()); |
1874 | return const_cast<ExistsOpInit *>(this); |
1875 | } |
1876 | |
1877 | Init *ExistsOpInit::getBit(unsigned Bit) const { |
1878 | return VarBitInit::get(const_cast<ExistsOpInit *>(this), Bit); |
1879 | } |
1880 | |
1881 | std::string ExistsOpInit::getAsString() const { |
1882 | return (Twine("!exists<") + CheckType->getAsString() + ">(" + |
1883 | Expr->getAsString() + ")") |
1884 | .str(); |
1885 | } |
1886 | |
1887 | RecTy *TypedInit::getFieldType(StringInit *FieldName) const { |
1888 | if (RecordRecTy *RecordType = dyn_cast<RecordRecTy>(getType())) { |
1889 | for (Record *Rec : RecordType->getClasses()) { |
1890 | if (RecordVal *Field = Rec->getValue(FieldName)) |
1891 | return Field->getType(); |
1892 | } |
1893 | } |
1894 | return nullptr; |
1895 | } |
1896 | |
1897 | Init * |
1898 | TypedInit::convertInitializerTo(RecTy *Ty) const { |
1899 | if (getType() == Ty || getType()->typeIsA(Ty)) |
1900 | return const_cast<TypedInit *>(this); |
1901 | |
1902 | if (isa<BitRecTy>(getType()) && isa<BitsRecTy>(Ty) && |
1903 | cast<BitsRecTy>(Ty)->getNumBits() == 1) |
1904 | return BitsInit::get(getRecordKeeper(), {const_cast<TypedInit *>(this)}); |
1905 | |
1906 | return nullptr; |
1907 | } |
1908 | |
1909 | Init *TypedInit::convertInitializerBitRange(ArrayRef<unsigned> Bits) const { |
1910 | BitsRecTy *T = dyn_cast<BitsRecTy>(getType()); |
1911 | if (!T) return nullptr; // Cannot subscript a non-bits variable. |
1912 | unsigned NumBits = T->getNumBits(); |
1913 | |
1914 | SmallVector<Init *, 16> NewBits; |
1915 | NewBits.reserve(Bits.size()); |
1916 | for (unsigned Bit : Bits) { |
1917 | if (Bit >= NumBits) |
1918 | return nullptr; |
1919 | |
1920 | NewBits.push_back(VarBitInit::get(const_cast<TypedInit *>(this), Bit)); |
1921 | } |
1922 | return BitsInit::get(getRecordKeeper(), NewBits); |
1923 | } |
1924 | |
1925 | Init *TypedInit::getCastTo(RecTy *Ty) const { |
1926 | // Handle the common case quickly |
1927 | if (getType() == Ty || getType()->typeIsA(Ty)) |
1928 | return const_cast<TypedInit *>(this); |
1929 | |
1930 | if (Init *Converted = convertInitializerTo(Ty)) { |
1931 | assert(!isa<TypedInit>(Converted) ||(static_cast <bool> (!isa<TypedInit>(Converted) || cast<TypedInit>(Converted)->getType()->typeIsA(Ty )) ? void (0) : __assert_fail ("!isa<TypedInit>(Converted) || cast<TypedInit>(Converted)->getType()->typeIsA(Ty)" , "llvm/lib/TableGen/Record.cpp", 1932, __extension__ __PRETTY_FUNCTION__ )) |
1932 | cast<TypedInit>(Converted)->getType()->typeIsA(Ty))(static_cast <bool> (!isa<TypedInit>(Converted) || cast<TypedInit>(Converted)->getType()->typeIsA(Ty )) ? void (0) : __assert_fail ("!isa<TypedInit>(Converted) || cast<TypedInit>(Converted)->getType()->typeIsA(Ty)" , "llvm/lib/TableGen/Record.cpp", 1932, __extension__ __PRETTY_FUNCTION__ )); |
1933 | return Converted; |
1934 | } |
1935 | |
1936 | if (!getType()->typeIsConvertibleTo(Ty)) |
1937 | return nullptr; |
1938 | |
1939 | return UnOpInit::get(UnOpInit::CAST, const_cast<TypedInit *>(this), Ty) |
1940 | ->Fold(nullptr); |
1941 | } |
1942 | |
1943 | VarInit *VarInit::get(StringRef VN, RecTy *T) { |
1944 | Init *Value = StringInit::get(T->getRecordKeeper(), VN); |
1945 | return VarInit::get(Value, T); |
1946 | } |
1947 | |
1948 | VarInit *VarInit::get(Init *VN, RecTy *T) { |
1949 | detail::RecordKeeperImpl &RK = T->getRecordKeeper().getImpl(); |
1950 | VarInit *&I = RK.TheVarInitPool[std::make_pair(T, VN)]; |
1951 | if (!I) |
1952 | I = new (RK.Allocator) VarInit(VN, T); |
1953 | return I; |
1954 | } |
1955 | |
1956 | StringRef VarInit::getName() const { |
1957 | StringInit *NameString = cast<StringInit>(getNameInit()); |
1958 | return NameString->getValue(); |
1959 | } |
1960 | |
1961 | Init *VarInit::getBit(unsigned Bit) const { |
1962 | if (getType() == BitRecTy::get(getRecordKeeper())) |
1963 | return const_cast<VarInit*>(this); |
1964 | return VarBitInit::get(const_cast<VarInit*>(this), Bit); |
1965 | } |
1966 | |
1967 | Init *VarInit::resolveReferences(Resolver &R) const { |
1968 | if (Init *Val = R.resolve(VarName)) |
1969 | return Val; |
1970 | return const_cast<VarInit *>(this); |
1971 | } |
1972 | |
1973 | VarBitInit *VarBitInit::get(TypedInit *T, unsigned B) { |
1974 | detail::RecordKeeperImpl &RK = T->getRecordKeeper().getImpl(); |
1975 | VarBitInit *&I = RK.TheVarBitInitPool[std::make_pair(T, B)]; |
1976 | if (!I) |
1977 | I = new (RK.Allocator) VarBitInit(T, B); |
1978 | return I; |
1979 | } |
1980 | |
1981 | std::string VarBitInit::getAsString() const { |
1982 | return TI->getAsString() + "{" + utostr(Bit) + "}"; |
1983 | } |
1984 | |
1985 | Init *VarBitInit::resolveReferences(Resolver &R) const { |
1986 | Init *I = TI->resolveReferences(R); |
1987 | if (TI != I) |
1988 | return I->getBit(getBitNum()); |
1989 | |
1990 | return const_cast<VarBitInit*>(this); |
1991 | } |
1992 | |
1993 | DefInit::DefInit(Record *D) |
1994 | : TypedInit(IK_DefInit, D->getType()), Def(D) {} |
1995 | |
1996 | DefInit *DefInit::get(Record *R) { |
1997 | return R->getDefInit(); |
1998 | } |
1999 | |
2000 | Init *DefInit::convertInitializerTo(RecTy *Ty) const { |
2001 | if (auto *RRT = dyn_cast<RecordRecTy>(Ty)) |
2002 | if (getType()->typeIsConvertibleTo(RRT)) |
2003 | return const_cast<DefInit *>(this); |
2004 | return nullptr; |
2005 | } |
2006 | |
2007 | RecTy *DefInit::getFieldType(StringInit *FieldName) const { |
2008 | if (const RecordVal *RV = Def->getValue(FieldName)) |
2009 | return RV->getType(); |
2010 | return nullptr; |
2011 | } |
2012 | |
2013 | std::string DefInit::getAsString() const { return std::string(Def->getName()); } |
2014 | |
2015 | static void ProfileVarDefInit(FoldingSetNodeID &ID, |
2016 | Record *Class, |
2017 | ArrayRef<Init *> Args) { |
2018 | ID.AddInteger(Args.size()); |
2019 | ID.AddPointer(Class); |
2020 | |
2021 | for (Init *I : Args) |
2022 | ID.AddPointer(I); |
2023 | } |
2024 | |
2025 | VarDefInit::VarDefInit(Record *Class, unsigned N) |
2026 | : TypedInit(IK_VarDefInit, RecordRecTy::get(Class)), Class(Class), |
2027 | NumArgs(N) {} |
2028 | |
2029 | VarDefInit *VarDefInit::get(Record *Class, ArrayRef<Init *> Args) { |
2030 | FoldingSetNodeID ID; |
2031 | ProfileVarDefInit(ID, Class, Args); |
2032 | |
2033 | detail::RecordKeeperImpl &RK = Class->getRecords().getImpl(); |
2034 | void *IP = nullptr; |
2035 | if (VarDefInit *I = RK.TheVarDefInitPool.FindNodeOrInsertPos(ID, IP)) |
2036 | return I; |
2037 | |
2038 | void *Mem = RK.Allocator.Allocate(totalSizeToAlloc<Init *>(Args.size()), |
2039 | alignof(VarDefInit)); |
2040 | VarDefInit *I = new (Mem) VarDefInit(Class, Args.size()); |
2041 | std::uninitialized_copy(Args.begin(), Args.end(), |
2042 | I->getTrailingObjects<Init *>()); |
2043 | RK.TheVarDefInitPool.InsertNode(I, IP); |
2044 | return I; |
2045 | } |
2046 | |
2047 | void VarDefInit::Profile(FoldingSetNodeID &ID) const { |
2048 | ProfileVarDefInit(ID, Class, args()); |
2049 | } |
2050 | |
2051 | DefInit *VarDefInit::instantiate() { |
2052 | if (!Def) { |
2053 | RecordKeeper &Records = Class->getRecords(); |
2054 | auto NewRecOwner = std::make_unique<Record>(Records.getNewAnonymousName(), |
2055 | Class->getLoc(), Records, |
2056 | /*IsAnonymous=*/true); |
2057 | Record *NewRec = NewRecOwner.get(); |
2058 | |
2059 | // Copy values from class to instance |
2060 | for (const RecordVal &Val : Class->getValues()) |
2061 | NewRec->addValue(Val); |
2062 | |
2063 | // Copy assertions from class to instance. |
2064 | NewRec->appendAssertions(Class); |
2065 | |
2066 | // Substitute and resolve template arguments |
2067 | ArrayRef<Init *> TArgs = Class->getTemplateArgs(); |
2068 | MapResolver R(NewRec); |
2069 | |
2070 | for (unsigned i = 0, e = TArgs.size(); i != e; ++i) { |
2071 | if (i < args_size()) |
2072 | R.set(TArgs[i], getArg(i)); |
2073 | else |
2074 | R.set(TArgs[i], NewRec->getValue(TArgs[i])->getValue()); |
2075 | |
2076 | NewRec->removeValue(TArgs[i]); |
2077 | } |
2078 | |
2079 | NewRec->resolveReferences(R); |
2080 | |
2081 | // Add superclasses. |
2082 | ArrayRef<std::pair<Record *, SMRange>> SCs = Class->getSuperClasses(); |
2083 | for (const auto &SCPair : SCs) |
2084 | NewRec->addSuperClass(SCPair.first, SCPair.second); |
2085 | |
2086 | NewRec->addSuperClass(Class, |
2087 | SMRange(Class->getLoc().back(), |
2088 | Class->getLoc().back())); |
2089 | |
2090 | // Resolve internal references and store in record keeper |
2091 | NewRec->resolveReferences(); |
2092 | Records.addDef(std::move(NewRecOwner)); |
2093 | |
2094 | // Check the assertions. |
2095 | NewRec->checkRecordAssertions(); |
2096 | |
2097 | Def = DefInit::get(NewRec); |
2098 | } |
2099 | |
2100 | return Def; |
2101 | } |
2102 | |
2103 | Init *VarDefInit::resolveReferences(Resolver &R) const { |
2104 | TrackUnresolvedResolver UR(&R); |
2105 | bool Changed = false; |
2106 | SmallVector<Init *, 8> NewArgs; |
2107 | NewArgs.reserve(args_size()); |
2108 | |
2109 | for (Init *Arg : args()) { |
2110 | Init *NewArg = Arg->resolveReferences(UR); |
2111 | NewArgs.push_back(NewArg); |
2112 | Changed |= NewArg != Arg; |
2113 | } |
2114 | |
2115 | if (Changed) { |
2116 | auto New = VarDefInit::get(Class, NewArgs); |
2117 | if (!UR.foundUnresolved()) |
2118 | return New->instantiate(); |
2119 | return New; |
2120 | } |
2121 | return const_cast<VarDefInit *>(this); |
2122 | } |
2123 | |
2124 | Init *VarDefInit::Fold() const { |
2125 | if (Def) |
2126 | return Def; |
2127 | |
2128 | TrackUnresolvedResolver R; |
2129 | for (Init *Arg : args()) |
2130 | Arg->resolveReferences(R); |
2131 | |
2132 | if (!R.foundUnresolved()) |
2133 | return const_cast<VarDefInit *>(this)->instantiate(); |
2134 | return const_cast<VarDefInit *>(this); |
2135 | } |
2136 | |
2137 | std::string VarDefInit::getAsString() const { |
2138 | std::string Result = Class->getNameInitAsString() + "<"; |
2139 | const char *sep = ""; |
2140 | for (Init *Arg : args()) { |
2141 | Result += sep; |
2142 | sep = ", "; |
2143 | Result += Arg->getAsString(); |
2144 | } |
2145 | return Result + ">"; |
2146 | } |
2147 | |
2148 | FieldInit *FieldInit::get(Init *R, StringInit *FN) { |
2149 | detail::RecordKeeperImpl &RK = R->getRecordKeeper().getImpl(); |
2150 | FieldInit *&I = RK.TheFieldInitPool[std::make_pair(R, FN)]; |
2151 | if (!I) |
2152 | I = new (RK.Allocator) FieldInit(R, FN); |
2153 | return I; |
2154 | } |
2155 | |
2156 | Init *FieldInit::getBit(unsigned Bit) const { |
2157 | if (getType() == BitRecTy::get(getRecordKeeper())) |
2158 | return const_cast<FieldInit*>(this); |
2159 | return VarBitInit::get(const_cast<FieldInit*>(this), Bit); |
2160 | } |
2161 | |
2162 | Init *FieldInit::resolveReferences(Resolver &R) const { |
2163 | Init *NewRec = Rec->resolveReferences(R); |
2164 | if (NewRec != Rec) |
2165 | return FieldInit::get(NewRec, FieldName)->Fold(R.getCurrentRecord()); |
2166 | return const_cast<FieldInit *>(this); |
2167 | } |
2168 | |
2169 | Init *FieldInit::Fold(Record *CurRec) const { |
2170 | if (DefInit *DI = dyn_cast<DefInit>(Rec)) { |
2171 | Record *Def = DI->getDef(); |
2172 | if (Def == CurRec) |
2173 | PrintFatalError(CurRec->getLoc(), |
2174 | Twine("Attempting to access field '") + |
2175 | FieldName->getAsUnquotedString() + "' of '" + |
2176 | Rec->getAsString() + "' is a forbidden self-reference"); |
2177 | Init *FieldVal = Def->getValue(FieldName)->getValue(); |
2178 | if (FieldVal->isConcrete()) |
2179 | return FieldVal; |
2180 | } |
2181 | return const_cast<FieldInit *>(this); |
2182 | } |
2183 | |
2184 | bool FieldInit::isConcrete() const { |
2185 | if (DefInit *DI = dyn_cast<DefInit>(Rec)) { |
2186 | Init *FieldVal = DI->getDef()->getValue(FieldName)->getValue(); |
2187 | return FieldVal->isConcrete(); |
2188 | } |
2189 | return false; |
2190 | } |
2191 | |
2192 | static void ProfileCondOpInit(FoldingSetNodeID &ID, |
2193 | ArrayRef<Init *> CondRange, |
2194 | ArrayRef<Init *> ValRange, |
2195 | const RecTy *ValType) { |
2196 | assert(CondRange.size() == ValRange.size() &&(static_cast <bool> (CondRange.size() == ValRange.size( ) && "Number of conditions and values must match!") ? void (0) : __assert_fail ("CondRange.size() == ValRange.size() && \"Number of conditions and values must match!\"" , "llvm/lib/TableGen/Record.cpp", 2197, __extension__ __PRETTY_FUNCTION__ )) |
2197 | "Number of conditions and values must match!")(static_cast <bool> (CondRange.size() == ValRange.size( ) && "Number of conditions and values must match!") ? void (0) : __assert_fail ("CondRange.size() == ValRange.size() && \"Number of conditions and values must match!\"" , "llvm/lib/TableGen/Record.cpp", 2197, __extension__ __PRETTY_FUNCTION__ )); |
2198 | ID.AddPointer(ValType); |
2199 | ArrayRef<Init *>::iterator Case = CondRange.begin(); |
2200 | ArrayRef<Init *>::iterator Val = ValRange.begin(); |
2201 | |
2202 | while (Case != CondRange.end()) { |
2203 | ID.AddPointer(*Case++); |
2204 | ID.AddPointer(*Val++); |
2205 | } |
2206 | } |
2207 | |
2208 | void CondOpInit::Profile(FoldingSetNodeID &ID) const { |
2209 | ProfileCondOpInit(ID, ArrayRef(getTrailingObjects<Init *>(), NumConds), |
2210 | ArrayRef(getTrailingObjects<Init *>() + NumConds, NumConds), |
2211 | ValType); |
2212 | } |
2213 | |
2214 | CondOpInit *CondOpInit::get(ArrayRef<Init *> CondRange, |
2215 | ArrayRef<Init *> ValRange, RecTy *Ty) { |
2216 | assert(CondRange.size() == ValRange.size() &&(static_cast <bool> (CondRange.size() == ValRange.size( ) && "Number of conditions and values must match!") ? void (0) : __assert_fail ("CondRange.size() == ValRange.size() && \"Number of conditions and values must match!\"" , "llvm/lib/TableGen/Record.cpp", 2217, __extension__ __PRETTY_FUNCTION__ )) |
2217 | "Number of conditions and values must match!")(static_cast <bool> (CondRange.size() == ValRange.size( ) && "Number of conditions and values must match!") ? void (0) : __assert_fail ("CondRange.size() == ValRange.size() && \"Number of conditions and values must match!\"" , "llvm/lib/TableGen/Record.cpp", 2217, __extension__ __PRETTY_FUNCTION__ )); |
2218 | |
2219 | FoldingSetNodeID ID; |
2220 | ProfileCondOpInit(ID, CondRange, ValRange, Ty); |
2221 | |
2222 | detail::RecordKeeperImpl &RK = Ty->getRecordKeeper().getImpl(); |
2223 | void *IP = nullptr; |
2224 | if (CondOpInit *I = RK.TheCondOpInitPool.FindNodeOrInsertPos(ID, IP)) |
2225 | return I; |
2226 | |
2227 | void *Mem = RK.Allocator.Allocate( |
2228 | totalSizeToAlloc<Init *>(2 * CondRange.size()), alignof(BitsInit)); |
2229 | CondOpInit *I = new(Mem) CondOpInit(CondRange.size(), Ty); |
2230 | |
2231 | std::uninitialized_copy(CondRange.begin(), CondRange.end(), |
2232 | I->getTrailingObjects<Init *>()); |
2233 | std::uninitialized_copy(ValRange.begin(), ValRange.end(), |
2234 | I->getTrailingObjects<Init *>()+CondRange.size()); |
2235 | RK.TheCondOpInitPool.InsertNode(I, IP); |
2236 | return I; |
2237 | } |
2238 | |
2239 | Init *CondOpInit::resolveReferences(Resolver &R) const { |
2240 | SmallVector<Init*, 4> NewConds; |
2241 | bool Changed = false; |
2242 | for (const Init *Case : getConds()) { |
2243 | Init *NewCase = Case->resolveReferences(R); |
2244 | NewConds.push_back(NewCase); |
2245 | Changed |= NewCase != Case; |
2246 | } |
2247 | |
2248 | SmallVector<Init*, 4> NewVals; |
2249 | for (const Init *Val : getVals()) { |
2250 | Init *NewVal = Val->resolveReferences(R); |
2251 | NewVals.push_back(NewVal); |
2252 | Changed |= NewVal != Val; |
2253 | } |
2254 | |
2255 | if (Changed) |
2256 | return (CondOpInit::get(NewConds, NewVals, |
2257 | getValType()))->Fold(R.getCurrentRecord()); |
2258 | |
2259 | return const_cast<CondOpInit *>(this); |
2260 | } |
2261 | |
2262 | Init *CondOpInit::Fold(Record *CurRec) const { |
2263 | RecordKeeper &RK = getRecordKeeper(); |
2264 | for ( unsigned i = 0; i < NumConds; ++i) { |
2265 | Init *Cond = getCond(i); |
2266 | Init *Val = getVal(i); |
2267 | |
2268 | if (IntInit *CondI = dyn_cast_or_null<IntInit>( |
2269 | Cond->convertInitializerTo(IntRecTy::get(RK)))) { |
2270 | if (CondI->getValue()) |
2271 | return Val->convertInitializerTo(getValType()); |
2272 | } else { |
2273 | return const_cast<CondOpInit *>(this); |
2274 | } |
2275 | } |
2276 | |
2277 | PrintFatalError(CurRec->getLoc(), |
2278 | CurRec->getNameInitAsString() + |
2279 | " does not have any true condition in:" + |
2280 | this->getAsString()); |
2281 | return nullptr; |
2282 | } |
2283 | |
2284 | bool CondOpInit::isConcrete() const { |
2285 | for (const Init *Case : getConds()) |
2286 | if (!Case->isConcrete()) |
2287 | return false; |
2288 | |
2289 | for (const Init *Val : getVals()) |
2290 | if (!Val->isConcrete()) |
2291 | return false; |
2292 | |
2293 | return true; |
2294 | } |
2295 | |
2296 | bool CondOpInit::isComplete() const { |
2297 | for (const Init *Case : getConds()) |
2298 | if (!Case->isComplete()) |
2299 | return false; |
2300 | |
2301 | for (const Init *Val : getVals()) |
2302 | if (!Val->isConcrete()) |
2303 | return false; |
2304 | |
2305 | return true; |
2306 | } |
2307 | |
2308 | std::string CondOpInit::getAsString() const { |
2309 | std::string Result = "!cond("; |
2310 | for (unsigned i = 0; i < getNumConds(); i++) { |
2311 | Result += getCond(i)->getAsString() + ": "; |
2312 | Result += getVal(i)->getAsString(); |
2313 | if (i != getNumConds()-1) |
2314 | Result += ", "; |
2315 | } |
2316 | return Result + ")"; |
2317 | } |
2318 | |
2319 | Init *CondOpInit::getBit(unsigned Bit) const { |
2320 | return VarBitInit::get(const_cast<CondOpInit *>(this), Bit); |
2321 | } |
2322 | |
2323 | static void ProfileDagInit(FoldingSetNodeID &ID, Init *V, StringInit *VN, |
2324 | ArrayRef<Init *> ArgRange, |
2325 | ArrayRef<StringInit *> NameRange) { |
2326 | ID.AddPointer(V); |
2327 | ID.AddPointer(VN); |
2328 | |
2329 | ArrayRef<Init *>::iterator Arg = ArgRange.begin(); |
2330 | ArrayRef<StringInit *>::iterator Name = NameRange.begin(); |
2331 | while (Arg != ArgRange.end()) { |
2332 | assert(Name != NameRange.end() && "Arg name underflow!")(static_cast <bool> (Name != NameRange.end() && "Arg name underflow!") ? void (0) : __assert_fail ("Name != NameRange.end() && \"Arg name underflow!\"" , "llvm/lib/TableGen/Record.cpp", 2332, __extension__ __PRETTY_FUNCTION__ )); |
2333 | ID.AddPointer(*Arg++); |
2334 | ID.AddPointer(*Name++); |
2335 | } |
2336 | assert(Name == NameRange.end() && "Arg name overflow!")(static_cast <bool> (Name == NameRange.end() && "Arg name overflow!") ? void (0) : __assert_fail ("Name == NameRange.end() && \"Arg name overflow!\"" , "llvm/lib/TableGen/Record.cpp", 2336, __extension__ __PRETTY_FUNCTION__ )); |
2337 | } |
2338 | |
2339 | DagInit *DagInit::get(Init *V, StringInit *VN, ArrayRef<Init *> ArgRange, |
2340 | ArrayRef<StringInit *> NameRange) { |
2341 | assert(ArgRange.size() == NameRange.size())(static_cast <bool> (ArgRange.size() == NameRange.size( )) ? void (0) : __assert_fail ("ArgRange.size() == NameRange.size()" , "llvm/lib/TableGen/Record.cpp", 2341, __extension__ __PRETTY_FUNCTION__ )); |
2342 | FoldingSetNodeID ID; |
2343 | ProfileDagInit(ID, V, VN, ArgRange, NameRange); |
2344 | |
2345 | detail::RecordKeeperImpl &RK = V->getRecordKeeper().getImpl(); |
2346 | void *IP = nullptr; |
2347 | if (DagInit *I = RK.TheDagInitPool.FindNodeOrInsertPos(ID, IP)) |
2348 | return I; |
2349 | |
2350 | void *Mem = RK.Allocator.Allocate( |
2351 | totalSizeToAlloc<Init *, StringInit *>(ArgRange.size(), NameRange.size()), |
2352 | alignof(BitsInit)); |
2353 | DagInit *I = new (Mem) DagInit(V, VN, ArgRange.size(), NameRange.size()); |
2354 | std::uninitialized_copy(ArgRange.begin(), ArgRange.end(), |
2355 | I->getTrailingObjects<Init *>()); |
2356 | std::uninitialized_copy(NameRange.begin(), NameRange.end(), |
2357 | I->getTrailingObjects<StringInit *>()); |
2358 | RK.TheDagInitPool.InsertNode(I, IP); |
2359 | return I; |
2360 | } |
2361 | |
2362 | DagInit * |
2363 | DagInit::get(Init *V, StringInit *VN, |
2364 | ArrayRef<std::pair<Init*, StringInit*>> args) { |
2365 | SmallVector<Init *, 8> Args; |
2366 | SmallVector<StringInit *, 8> Names; |
2367 | |
2368 | for (const auto &Arg : args) { |
2369 | Args.push_back(Arg.first); |
2370 | Names.push_back(Arg.second); |
2371 | } |
2372 | |
2373 | return DagInit::get(V, VN, Args, Names); |
2374 | } |
2375 | |
2376 | void DagInit::Profile(FoldingSetNodeID &ID) const { |
2377 | ProfileDagInit(ID, Val, ValName, |
2378 | ArrayRef(getTrailingObjects<Init *>(), NumArgs), |
2379 | ArrayRef(getTrailingObjects<StringInit *>(), NumArgNames)); |
2380 | } |
2381 | |
2382 | Record *DagInit::getOperatorAsDef(ArrayRef<SMLoc> Loc) const { |
2383 | if (DefInit *DefI = dyn_cast<DefInit>(Val)) |
2384 | return DefI->getDef(); |
2385 | PrintFatalError(Loc, "Expected record as operator"); |
2386 | return nullptr; |
2387 | } |
2388 | |
2389 | Init *DagInit::resolveReferences(Resolver &R) const { |
2390 | SmallVector<Init*, 8> NewArgs; |
2391 | NewArgs.reserve(arg_size()); |
2392 | bool ArgsChanged = false; |
2393 | for (const Init *Arg : getArgs()) { |
2394 | Init *NewArg = Arg->resolveReferences(R); |
2395 | NewArgs.push_back(NewArg); |
2396 | ArgsChanged |= NewArg != Arg; |
2397 | } |
2398 | |
2399 | Init *Op = Val->resolveReferences(R); |
2400 | if (Op != Val || ArgsChanged) |
2401 | return DagInit::get(Op, ValName, NewArgs, getArgNames()); |
2402 | |
2403 | return const_cast<DagInit *>(this); |
2404 | } |
2405 | |
2406 | bool DagInit::isConcrete() const { |
2407 | if (!Val->isConcrete()) |
2408 | return false; |
2409 | for (const Init *Elt : getArgs()) { |
2410 | if (!Elt->isConcrete()) |
2411 | return false; |
2412 | } |
2413 | return true; |
2414 | } |
2415 | |
2416 | std::string DagInit::getAsString() const { |
2417 | std::string Result = "(" + Val->getAsString(); |
2418 | if (ValName) |
2419 | Result += ":" + ValName->getAsUnquotedString(); |
2420 | if (!arg_empty()) { |
2421 | Result += " " + getArg(0)->getAsString(); |
2422 | if (getArgName(0)) Result += ":$" + getArgName(0)->getAsUnquotedString(); |
2423 | for (unsigned i = 1, e = getNumArgs(); i != e; ++i) { |
2424 | Result += ", " + getArg(i)->getAsString(); |
2425 | if (getArgName(i)) Result += ":$" + getArgName(i)->getAsUnquotedString(); |
2426 | } |
2427 | } |
2428 | return Result + ")"; |
2429 | } |
2430 | |
2431 | //===----------------------------------------------------------------------===// |
2432 | // Other implementations |
2433 | //===----------------------------------------------------------------------===// |
2434 | |
2435 | RecordVal::RecordVal(Init *N, RecTy *T, FieldKind K) |
2436 | : Name(N), TyAndKind(T, K) { |
2437 | setValue(UnsetInit::get(N->getRecordKeeper())); |
2438 | assert(Value && "Cannot create unset value for current type!")(static_cast <bool> (Value && "Cannot create unset value for current type!" ) ? void (0) : __assert_fail ("Value && \"Cannot create unset value for current type!\"" , "llvm/lib/TableGen/Record.cpp", 2438, __extension__ __PRETTY_FUNCTION__ )); |
2439 | } |
2440 | |
2441 | // This constructor accepts the same arguments as the above, but also |
2442 | // a source location. |
2443 | RecordVal::RecordVal(Init *N, SMLoc Loc, RecTy *T, FieldKind K) |
2444 | : Name(N), Loc(Loc), TyAndKind(T, K) { |
2445 | setValue(UnsetInit::get(N->getRecordKeeper())); |
2446 | assert(Value && "Cannot create unset value for current type!")(static_cast <bool> (Value && "Cannot create unset value for current type!" ) ? void (0) : __assert_fail ("Value && \"Cannot create unset value for current type!\"" , "llvm/lib/TableGen/Record.cpp", 2446, __extension__ __PRETTY_FUNCTION__ )); |
2447 | } |
2448 | |
2449 | StringRef RecordVal::getName() const { |
2450 | return cast<StringInit>(getNameInit())->getValue(); |
2451 | } |
2452 | |
2453 | std::string RecordVal::getPrintType() const { |
2454 | if (getType() == StringRecTy::get(getRecordKeeper())) { |
2455 | if (auto *StrInit = dyn_cast<StringInit>(Value)) { |
2456 | if (StrInit->hasCodeFormat()) |
2457 | return "code"; |
2458 | else |
2459 | return "string"; |
2460 | } else { |
2461 | return "string"; |
2462 | } |
2463 | } else { |
2464 | return TyAndKind.getPointer()->getAsString(); |
2465 | } |
2466 | } |
2467 | |
2468 | bool RecordVal::setValue(Init *V) { |
2469 | if (V) { |
2470 | Value = V->getCastTo(getType()); |
2471 | if (Value) { |
2472 | assert(!isa<TypedInit>(Value) ||(static_cast <bool> (!isa<TypedInit>(Value) || cast <TypedInit>(Value)->getType()->typeIsA(getType()) ) ? void (0) : __assert_fail ("!isa<TypedInit>(Value) || cast<TypedInit>(Value)->getType()->typeIsA(getType())" , "llvm/lib/TableGen/Record.cpp", 2473, __extension__ __PRETTY_FUNCTION__ )) |
2473 | cast<TypedInit>(Value)->getType()->typeIsA(getType()))(static_cast <bool> (!isa<TypedInit>(Value) || cast <TypedInit>(Value)->getType()->typeIsA(getType()) ) ? void (0) : __assert_fail ("!isa<TypedInit>(Value) || cast<TypedInit>(Value)->getType()->typeIsA(getType())" , "llvm/lib/TableGen/Record.cpp", 2473, __extension__ __PRETTY_FUNCTION__ )); |
2474 | if (BitsRecTy *BTy = dyn_cast<BitsRecTy>(getType())) { |
2475 | if (!isa<BitsInit>(Value)) { |
2476 | SmallVector<Init *, 64> Bits; |
2477 | Bits.reserve(BTy->getNumBits()); |
2478 | for (unsigned I = 0, E = BTy->getNumBits(); I < E; ++I) |
2479 | Bits.push_back(Value->getBit(I)); |
2480 | Value = BitsInit::get(V->getRecordKeeper(), Bits); |
2481 | } |
2482 | } |
2483 | } |
2484 | return Value == nullptr; |
2485 | } |
2486 | Value = nullptr; |
2487 | return false; |
2488 | } |
2489 | |
2490 | // This version of setValue takes a source location and resets the |
2491 | // location in the RecordVal. |
2492 | bool RecordVal::setValue(Init *V, SMLoc NewLoc) { |
2493 | Loc = NewLoc; |
2494 | if (V) { |
2495 | Value = V->getCastTo(getType()); |
2496 | if (Value) { |
2497 | assert(!isa<TypedInit>(Value) ||(static_cast <bool> (!isa<TypedInit>(Value) || cast <TypedInit>(Value)->getType()->typeIsA(getType()) ) ? void (0) : __assert_fail ("!isa<TypedInit>(Value) || cast<TypedInit>(Value)->getType()->typeIsA(getType())" , "llvm/lib/TableGen/Record.cpp", 2498, __extension__ __PRETTY_FUNCTION__ )) |
2498 | cast<TypedInit>(Value)->getType()->typeIsA(getType()))(static_cast <bool> (!isa<TypedInit>(Value) || cast <TypedInit>(Value)->getType()->typeIsA(getType()) ) ? void (0) : __assert_fail ("!isa<TypedInit>(Value) || cast<TypedInit>(Value)->getType()->typeIsA(getType())" , "llvm/lib/TableGen/Record.cpp", 2498, __extension__ __PRETTY_FUNCTION__ )); |
2499 | if (BitsRecTy *BTy = dyn_cast<BitsRecTy>(getType())) { |
2500 | if (!isa<BitsInit>(Value)) { |
2501 | SmallVector<Init *, 64> Bits; |
2502 | Bits.reserve(BTy->getNumBits()); |
2503 | for (unsigned I = 0, E = BTy->getNumBits(); I < E; ++I) |
2504 | Bits.push_back(Value->getBit(I)); |
2505 | Value = BitsInit::get(getRecordKeeper(), Bits); |
2506 | } |
2507 | } |
2508 | } |
2509 | return Value == nullptr; |
2510 | } |
2511 | Value = nullptr; |
2512 | return false; |
2513 | } |
2514 | |
2515 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
2516 | #include "llvm/TableGen/Record.h" |
2517 | LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void RecordVal::dump() const { errs() << *this; } |
2518 | #endif |
2519 | |
2520 | void RecordVal::print(raw_ostream &OS, bool PrintSem) const { |
2521 | if (isNonconcreteOK()) OS << "field "; |
2522 | OS << getPrintType() << " " << getNameInitAsString(); |
2523 | |
2524 | if (getValue()) |
2525 | OS << " = " << *getValue(); |
2526 | |
2527 | if (PrintSem) OS << ";\n"; |
2528 | } |
2529 | |
2530 | void Record::updateClassLoc(SMLoc Loc) { |
2531 | assert(Locs.size() == 1)(static_cast <bool> (Locs.size() == 1) ? void (0) : __assert_fail ("Locs.size() == 1", "llvm/lib/TableGen/Record.cpp", 2531, __extension__ __PRETTY_FUNCTION__)); |
2532 | ForwardDeclarationLocs.push_back(Locs.front()); |
2533 | |
2534 | Locs.clear(); |
2535 | Locs.push_back(Loc); |
2536 | } |
2537 | |
2538 | void Record::checkName() { |
2539 | // Ensure the record name has string type. |
2540 | const TypedInit *TypedName = cast<const TypedInit>(Name); |
2541 | if (!isa<StringRecTy>(TypedName->getType())) |
2542 | PrintFatalError(getLoc(), Twine("Record name '") + Name->getAsString() + |
2543 | "' is not a string!"); |
2544 | } |
2545 | |
2546 | RecordRecTy *Record::getType() { |
2547 | SmallVector<Record *, 4> DirectSCs; |
2548 | getDirectSuperClasses(DirectSCs); |
2549 | return RecordRecTy::get(TrackedRecords, DirectSCs); |
2550 | } |
2551 | |
2552 | DefInit *Record::getDefInit() { |
2553 | if (!CorrespondingDefInit) { |
2554 | CorrespondingDefInit = |
2555 | new (TrackedRecords.getImpl().Allocator) DefInit(this); |
2556 | } |
2557 | return CorrespondingDefInit; |
2558 | } |
2559 | |
2560 | unsigned Record::getNewUID(RecordKeeper &RK) { |
2561 | return RK.getImpl().LastRecordID++; |
2562 | } |
2563 | |
2564 | void Record::setName(Init *NewName) { |
2565 | Name = NewName; |
2566 | checkName(); |
2567 | // DO NOT resolve record values to the name at this point because |
2568 | // there might be default values for arguments of this def. Those |
2569 | // arguments might not have been resolved yet so we don't want to |
2570 | // prematurely assume values for those arguments were not passed to |
2571 | // this def. |
2572 | // |
2573 | // Nonetheless, it may be that some of this Record's values |
2574 | // reference the record name. Indeed, the reason for having the |
2575 | // record name be an Init is to provide this flexibility. The extra |
2576 | // resolve steps after completely instantiating defs takes care of |
2577 | // this. See TGParser::ParseDef and TGParser::ParseDefm. |
2578 | } |
2579 | |
2580 | // NOTE for the next two functions: |
2581 | // Superclasses are in post-order, so the final one is a direct |
2582 | // superclass. All of its transitive superclases immediately precede it, |
2583 | // so we can step through the direct superclasses in reverse order. |
2584 | |
2585 | bool Record::hasDirectSuperClass(const Record *Superclass) const { |
2586 | ArrayRef<std::pair<Record *, SMRange>> SCs = getSuperClasses(); |
2587 | |
2588 | for (int I = SCs.size() - 1; I >= 0; --I) { |
2589 | const Record *SC = SCs[I].first; |
2590 | if (SC == Superclass) |
2591 | return true; |
2592 | I -= SC->getSuperClasses().size(); |
2593 | } |
2594 | |
2595 | return false; |
2596 | } |
2597 | |
2598 | void Record::getDirectSuperClasses(SmallVectorImpl<Record *> &Classes) const { |
2599 | ArrayRef<std::pair<Record *, SMRange>> SCs = getSuperClasses(); |
2600 | |
2601 | while (!SCs.empty()) { |
2602 | Record *SC = SCs.back().first; |
2603 | SCs = SCs.drop_back(1 + SC->getSuperClasses().size()); |
2604 | Classes.push_back(SC); |
2605 | } |
2606 | } |
2607 | |
2608 | void Record::resolveReferences(Resolver &R, const RecordVal *SkipVal) { |
2609 | Init *OldName = getNameInit(); |
2610 | Init *NewName = Name->resolveReferences(R); |
2611 | if (NewName != OldName) { |
2612 | // Re-register with RecordKeeper. |
2613 | setName(NewName); |
2614 | } |
2615 | |
2616 | // Resolve the field values. |
2617 | for (RecordVal &Value : Values) { |
2618 | if (SkipVal == &Value) // Skip resolve the same field as the given one |
2619 | continue; |
2620 | if (Init *V = Value.getValue()) { |
2621 | Init *VR = V->resolveReferences(R); |
2622 | if (Value.setValue(VR)) { |
2623 | std::string Type; |
2624 | if (TypedInit *VRT = dyn_cast<TypedInit>(VR)) |
2625 | Type = |
2626 | (Twine("of type '") + VRT->getType()->getAsString() + "' ").str(); |
2627 | PrintFatalError( |
2628 | getLoc(), |
2629 | Twine("Invalid value ") + Type + "found when setting field '" + |
2630 | Value.getNameInitAsString() + "' of type '" + |
2631 | Value.getType()->getAsString() + |
2632 | "' after resolving references: " + VR->getAsUnquotedString() + |
2633 | "\n"); |
2634 | } |
2635 | } |
2636 | } |
2637 | |
2638 | // Resolve the assertion expressions. |
2639 | for (auto &Assertion : Assertions) { |
2640 | Init *Value = Assertion.Condition->resolveReferences(R); |
2641 | Assertion.Condition = Value; |
2642 | Value = Assertion.Message->resolveReferences(R); |
2643 | Assertion.Message = Value; |
2644 | } |
2645 | } |
2646 | |
2647 | void Record::resolveReferences(Init *NewName) { |
2648 | RecordResolver R(*this); |
2649 | R.setName(NewName); |
2650 | R.setFinal(true); |
2651 | resolveReferences(R); |
2652 | } |
2653 | |
2654 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
2655 | LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void Record::dump() const { errs() << *this; } |
2656 | #endif |
2657 | |
2658 | raw_ostream &llvm::operator<<(raw_ostream &OS, const Record &R) { |
2659 | OS << R.getNameInitAsString(); |
2660 | |
2661 | ArrayRef<Init *> TArgs = R.getTemplateArgs(); |
2662 | if (!TArgs.empty()) { |
2663 | OS << "<"; |
2664 | bool NeedComma = false; |
2665 | for (const Init *TA : TArgs) { |
2666 | if (NeedComma) OS << ", "; |
2667 | NeedComma = true; |
2668 | const RecordVal *RV = R.getValue(TA); |
2669 | assert(RV && "Template argument record not found??")(static_cast <bool> (RV && "Template argument record not found??" ) ? void (0) : __assert_fail ("RV && \"Template argument record not found??\"" , "llvm/lib/TableGen/Record.cpp", 2669, __extension__ __PRETTY_FUNCTION__ )); |
2670 | RV->print(OS, false); |
2671 | } |
2672 | OS << ">"; |
2673 | } |
2674 | |
2675 | OS << " {"; |
2676 | ArrayRef<std::pair<Record *, SMRange>> SC = R.getSuperClasses(); |
2677 | if (!SC.empty()) { |
2678 | OS << "\t//"; |
2679 | for (const auto &SuperPair : SC) |
2680 | OS << " " << SuperPair.first->getNameInitAsString(); |
2681 | } |
2682 | OS << "\n"; |
2683 | |
2684 | for (const RecordVal &Val : R.getValues()) |
2685 | if (Val.isNonconcreteOK() && !R.isTemplateArg(Val.getNameInit())) |
2686 | OS << Val; |
2687 | for (const RecordVal &Val : R.getValues()) |
2688 | if (!Val.isNonconcreteOK() && !R.isTemplateArg(Val.getNameInit())) |
2689 | OS << Val; |
2690 | |
2691 | return OS << "}\n"; |
2692 | } |
2693 | |
2694 | SMLoc Record::getFieldLoc(StringRef FieldName) const { |
2695 | const RecordVal *R = getValue(FieldName); |
2696 | if (!R) |
2697 | PrintFatalError(getLoc(), "Record `" + getName() + |
2698 | "' does not have a field named `" + FieldName + "'!\n"); |
2699 | return R->getLoc(); |
2700 | } |
2701 | |
2702 | Init *Record::getValueInit(StringRef FieldName) const { |
2703 | const RecordVal *R = getValue(FieldName); |
2704 | if (!R || !R->getValue()) |
2705 | PrintFatalError(getLoc(), "Record `" + getName() + |
2706 | "' does not have a field named `" + FieldName + "'!\n"); |
2707 | return R->getValue(); |
2708 | } |
2709 | |
2710 | StringRef Record::getValueAsString(StringRef FieldName) const { |
2711 | std::optional<StringRef> S = getValueAsOptionalString(FieldName); |
2712 | if (!S) |
2713 | PrintFatalError(getLoc(), "Record `" + getName() + |
2714 | "' does not have a field named `" + FieldName + "'!\n"); |
2715 | return *S; |
2716 | } |
2717 | |
2718 | std::optional<StringRef> |
2719 | Record::getValueAsOptionalString(StringRef FieldName) const { |
2720 | const RecordVal *R = getValue(FieldName); |
2721 | if (!R || !R->getValue()) |
2722 | return std::nullopt; |
2723 | if (isa<UnsetInit>(R->getValue())) |
2724 | return std::nullopt; |
2725 | |
2726 | if (StringInit *SI = dyn_cast<StringInit>(R->getValue())) |
2727 | return SI->getValue(); |
2728 | |
2729 | PrintFatalError(getLoc(), |
2730 | "Record `" + getName() + "', ` field `" + FieldName + |
2731 | "' exists but does not have a string initializer!"); |
2732 | } |
2733 | |
2734 | BitsInit *Record::getValueAsBitsInit(StringRef FieldName) const { |
2735 | const RecordVal *R = getValue(FieldName); |
2736 | if (!R || !R->getValue()) |
2737 | PrintFatalError(getLoc(), "Record `" + getName() + |
2738 | "' does not have a field named `" + FieldName + "'!\n"); |
2739 | |
2740 | if (BitsInit *BI = dyn_cast<BitsInit>(R->getValue())) |
2741 | return BI; |
2742 | PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + FieldName + |
2743 | "' exists but does not have a bits value"); |
2744 | } |
2745 | |
2746 | ListInit *Record::getValueAsListInit(StringRef FieldName) const { |
2747 | const RecordVal *R = getValue(FieldName); |
2748 | if (!R || !R->getValue()) |
2749 | PrintFatalError(getLoc(), "Record `" + getName() + |
2750 | "' does not have a field named `" + FieldName + "'!\n"); |
2751 | |
2752 | if (ListInit *LI = dyn_cast<ListInit>(R->getValue())) |
2753 | return LI; |
2754 | PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + FieldName + |
2755 | "' exists but does not have a list value"); |
2756 | } |
2757 | |
2758 | std::vector<Record*> |
2759 | Record::getValueAsListOfDefs(StringRef FieldName) const { |
2760 | ListInit *List = getValueAsListInit(FieldName); |
2761 | std::vector<Record*> Defs; |
2762 | for (Init *I : List->getValues()) { |
2763 | if (DefInit *DI = dyn_cast<DefInit>(I)) |
2764 | Defs.push_back(DI->getDef()); |
2765 | else |
2766 | PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + |
2767 | FieldName + "' list is not entirely DefInit!"); |
2768 | } |
2769 | return Defs; |
2770 | } |
2771 | |
2772 | int64_t Record::getValueAsInt(StringRef FieldName) const { |
2773 | const RecordVal *R = getValue(FieldName); |
2774 | if (!R || !R->getValue()) |
2775 | PrintFatalError(getLoc(), "Record `" + getName() + |
2776 | "' does not have a field named `" + FieldName + "'!\n"); |
2777 | |
2778 | if (IntInit *II = dyn_cast<IntInit>(R->getValue())) |
2779 | return II->getValue(); |
2780 | PrintFatalError(getLoc(), Twine("Record `") + getName() + "', field `" + |
2781 | FieldName + |
2782 | "' exists but does not have an int value: " + |
2783 | R->getValue()->getAsString()); |
2784 | } |
2785 | |
2786 | std::vector<int64_t> |
2787 | Record::getValueAsListOfInts(StringRef FieldName) const { |
2788 | ListInit *List = getValueAsListInit(FieldName); |
2789 | std::vector<int64_t> Ints; |
2790 | for (Init *I : List->getValues()) { |
2791 | if (IntInit *II = dyn_cast<IntInit>(I)) |
2792 | Ints.push_back(II->getValue()); |
2793 | else |
2794 | PrintFatalError(getLoc(), |
2795 | Twine("Record `") + getName() + "', field `" + FieldName + |
2796 | "' exists but does not have a list of ints value: " + |
2797 | I->getAsString()); |
2798 | } |
2799 | return Ints; |
2800 | } |
2801 | |
2802 | std::vector<StringRef> |
2803 | Record::getValueAsListOfStrings(StringRef FieldName) const { |
2804 | ListInit *List = getValueAsListInit(FieldName); |
2805 | std::vector<StringRef> Strings; |
2806 | for (Init *I : List->getValues()) { |
2807 | if (StringInit *SI = dyn_cast<StringInit>(I)) |
2808 | Strings.push_back(SI->getValue()); |
2809 | else |
2810 | PrintFatalError(getLoc(), |
2811 | Twine("Record `") + getName() + "', field `" + FieldName + |
2812 | "' exists but does not have a list of strings value: " + |
2813 | I->getAsString()); |
2814 | } |
2815 | return Strings; |
2816 | } |
2817 | |
2818 | Record *Record::getValueAsDef(StringRef FieldName) const { |
2819 | const RecordVal *R = getValue(FieldName); |
2820 | if (!R || !R->getValue()) |
2821 | PrintFatalError(getLoc(), "Record `" + getName() + |
2822 | "' does not have a field named `" + FieldName + "'!\n"); |
2823 | |
2824 | if (DefInit *DI = dyn_cast<DefInit>(R->getValue())) |
2825 | return DI->getDef(); |
2826 | PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + |
2827 | FieldName + "' does not have a def initializer!"); |
2828 | } |
2829 | |
2830 | Record *Record::getValueAsOptionalDef(StringRef FieldName) const { |
2831 | const RecordVal *R = getValue(FieldName); |
2832 | if (!R || !R->getValue()) |
2833 | PrintFatalError(getLoc(), "Record `" + getName() + |
2834 | "' does not have a field named `" + FieldName + "'!\n"); |
2835 | |
2836 | if (DefInit *DI = dyn_cast<DefInit>(R->getValue())) |
2837 | return DI->getDef(); |
2838 | if (isa<UnsetInit>(R->getValue())) |
2839 | return nullptr; |
2840 | PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + |
2841 | FieldName + "' does not have either a def initializer or '?'!"); |
2842 | } |
2843 | |
2844 | |
2845 | bool Record::getValueAsBit(StringRef FieldName) const { |
2846 | const RecordVal *R = getValue(FieldName); |
2847 | if (!R || !R->getValue()) |
2848 | PrintFatalError(getLoc(), "Record `" + getName() + |
2849 | "' does not have a field named `" + FieldName + "'!\n"); |
2850 | |
2851 | if (BitInit *BI = dyn_cast<BitInit>(R->getValue())) |
2852 | return BI->getValue(); |
2853 | PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + |
2854 | FieldName + "' does not have a bit initializer!"); |
2855 | } |
2856 | |
2857 | bool Record::getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const { |
2858 | const RecordVal *R = getValue(FieldName); |
2859 | if (!R || !R->getValue()) |
2860 | PrintFatalError(getLoc(), "Record `" + getName() + |
2861 | "' does not have a field named `" + FieldName.str() + "'!\n"); |
2862 | |
2863 | if (isa<UnsetInit>(R->getValue())) { |
2864 | Unset = true; |
2865 | return false; |
2866 | } |
2867 | Unset = false; |
2868 | if (BitInit *BI = dyn_cast<BitInit>(R->getValue())) |
2869 | return BI->getValue(); |
2870 | PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + |
2871 | FieldName + "' does not have a bit initializer!"); |
2872 | } |
2873 | |
2874 | DagInit *Record::getValueAsDag(StringRef FieldName) const { |
2875 | const RecordVal *R = getValue(FieldName); |
2876 | if (!R || !R->getValue()) |
2877 | PrintFatalError(getLoc(), "Record `" + getName() + |
2878 | "' does not have a field named `" + FieldName + "'!\n"); |
2879 | |
2880 | if (DagInit *DI = dyn_cast<DagInit>(R->getValue())) |
2881 | return DI; |
2882 | PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + |
2883 | FieldName + "' does not have a dag initializer!"); |
2884 | } |
2885 | |
2886 | // Check all record assertions: For each one, resolve the condition |
2887 | // and message, then call CheckAssert(). |
2888 | // Note: The condition and message are probably already resolved, |
2889 | // but resolving again allows calls before records are resolved. |
2890 | void Record::checkRecordAssertions() { |
2891 | RecordResolver R(*this); |
2892 | R.setFinal(true); |
2893 | |
2894 | for (const auto &Assertion : getAssertions()) { |
2895 | Init *Condition = Assertion.Condition->resolveReferences(R); |
2896 | Init *Message = Assertion.Message->resolveReferences(R); |
2897 | CheckAssert(Assertion.Loc, Condition, Message); |
2898 | } |
2899 | } |
2900 | |
2901 | // Report a warning if the record has unused template arguments. |
2902 | void Record::checkUnusedTemplateArgs() { |
2903 | for (const Init *TA : getTemplateArgs()) { |
2904 | const RecordVal *Arg = getValue(TA); |
2905 | if (!Arg->isUsed()) |
2906 | PrintWarning(Arg->getLoc(), |
2907 | "unused template argument: " + Twine(Arg->getName())); |
2908 | } |
2909 | } |
2910 | |
2911 | RecordKeeper::RecordKeeper() |
2912 | : Impl(std::make_unique<detail::RecordKeeperImpl>(*this)) {} |
2913 | RecordKeeper::~RecordKeeper() = default; |
2914 | |
2915 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
2916 | LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void RecordKeeper::dump() const { errs() << *this; } |
2917 | #endif |
2918 | |
2919 | raw_ostream &llvm::operator<<(raw_ostream &OS, const RecordKeeper &RK) { |
2920 | OS << "------------- Classes -----------------\n"; |
2921 | for (const auto &C : RK.getClasses()) |
2922 | OS << "class " << *C.second; |
2923 | |
2924 | OS << "------------- Defs -----------------\n"; |
2925 | for (const auto &D : RK.getDefs()) |
2926 | OS << "def " << *D.second; |
2927 | return OS; |
2928 | } |
2929 | |
2930 | /// GetNewAnonymousName - Generate a unique anonymous name that can be used as |
2931 | /// an identifier. |
2932 | Init *RecordKeeper::getNewAnonymousName() { |
2933 | return AnonymousNameInit::get(*this, getImpl().AnonCounter++); |
2934 | } |
2935 | |
2936 | // These functions implement the phase timing facility. Starting a timer |
2937 | // when one is already running stops the running one. |
2938 | |
2939 | void RecordKeeper::startTimer(StringRef Name) { |
2940 | if (TimingGroup) { |
2941 | if (LastTimer && LastTimer->isRunning()) { |
2942 | LastTimer->stopTimer(); |
2943 | if (BackendTimer) { |
2944 | LastTimer->clear(); |
2945 | BackendTimer = false; |
2946 | } |
2947 | } |
2948 | |
2949 | LastTimer = new Timer("", Name, *TimingGroup); |
2950 | LastTimer->startTimer(); |
2951 | } |
2952 | } |
2953 | |
2954 | void RecordKeeper::stopTimer() { |
2955 | if (TimingGroup) { |
2956 | assert(LastTimer && "No phase timer was started")(static_cast <bool> (LastTimer && "No phase timer was started" ) ? void (0) : __assert_fail ("LastTimer && \"No phase timer was started\"" , "llvm/lib/TableGen/Record.cpp", 2956, __extension__ __PRETTY_FUNCTION__ )); |
2957 | LastTimer->stopTimer(); |
2958 | } |
2959 | } |
2960 | |
2961 | void RecordKeeper::startBackendTimer(StringRef Name) { |
2962 | if (TimingGroup) { |
2963 | startTimer(Name); |
2964 | BackendTimer = true; |
2965 | } |
2966 | } |
2967 | |
2968 | void RecordKeeper::stopBackendTimer() { |
2969 | if (TimingGroup) { |
2970 | if (BackendTimer) { |
2971 | stopTimer(); |
2972 | BackendTimer = false; |
2973 | } |
2974 | } |
2975 | } |
2976 | |
2977 | std::vector<Record *> |
2978 | RecordKeeper::getAllDerivedDefinitions(StringRef ClassName) const { |
2979 | // We cache the record vectors for single classes. Many backends request |
2980 | // the same vectors multiple times. |
2981 | auto Pair = ClassRecordsMap.try_emplace(ClassName); |
2982 | if (Pair.second) |
2983 | Pair.first->second = getAllDerivedDefinitions(ArrayRef(ClassName)); |
2984 | |
2985 | return Pair.first->second; |
2986 | } |
2987 | |
2988 | std::vector<Record *> RecordKeeper::getAllDerivedDefinitions( |
2989 | ArrayRef<StringRef> ClassNames) const { |
2990 | SmallVector<Record *, 2> ClassRecs; |
2991 | std::vector<Record *> Defs; |
2992 | |
2993 | assert(ClassNames.size() > 0 && "At least one class must be passed.")(static_cast <bool> (ClassNames.size() > 0 && "At least one class must be passed.") ? void (0) : __assert_fail ("ClassNames.size() > 0 && \"At least one class must be passed.\"" , "llvm/lib/TableGen/Record.cpp", 2993, __extension__ __PRETTY_FUNCTION__ )); |
2994 | for (const auto &ClassName : ClassNames) { |
2995 | Record *Class = getClass(ClassName); |
2996 | if (!Class) |
2997 | PrintFatalError("The class '" + ClassName + "' is not defined\n"); |
2998 | ClassRecs.push_back(Class); |
2999 | } |
3000 | |
3001 | for (const auto &OneDef : getDefs()) { |
3002 | if (all_of(ClassRecs, [&OneDef](const Record *Class) { |
3003 | return OneDef.second->isSubClassOf(Class); |
3004 | })) |
3005 | Defs.push_back(OneDef.second.get()); |
3006 | } |
3007 | |
3008 | llvm::sort(Defs, [](Record *LHS, Record *RHS) { |
3009 | return LHS->getName().compare_numeric(RHS->getName()) < 0; |
3010 | }); |
3011 | |
3012 | return Defs; |
3013 | } |
3014 | |
3015 | std::vector<Record *> |
3016 | RecordKeeper::getAllDerivedDefinitionsIfDefined(StringRef ClassName) const { |
3017 | return getClass(ClassName) ? getAllDerivedDefinitions(ClassName) |
3018 | : std::vector<Record *>(); |
3019 | } |
3020 | |
3021 | Init *MapResolver::resolve(Init *VarName) { |
3022 | auto It = Map.find(VarName); |
3023 | if (It == Map.end()) |
3024 | return nullptr; |
3025 | |
3026 | Init *I = It->second.V; |
3027 | |
3028 | if (!It->second.Resolved && Map.size() > 1) { |
3029 | // Resolve mutual references among the mapped variables, but prevent |
3030 | // infinite recursion. |
3031 | Map.erase(It); |
3032 | I = I->resolveReferences(*this); |
3033 | Map[VarName] = {I, true}; |
3034 | } |
3035 | |
3036 | return I; |
3037 | } |
3038 | |
3039 | Init *RecordResolver::resolve(Init *VarName) { |
3040 | Init *Val = Cache.lookup(VarName); |
3041 | if (Val) |
3042 | return Val; |
3043 | |
3044 | if (llvm::is_contained(Stack, VarName)) |
3045 | return nullptr; // prevent infinite recursion |
3046 | |
3047 | if (RecordVal *RV = getCurrentRecord()->getValue(VarName)) { |
3048 | if (!isa<UnsetInit>(RV->getValue())) { |
3049 | Val = RV->getValue(); |
3050 | Stack.push_back(VarName); |
3051 | Val = Val->resolveReferences(*this); |
3052 | Stack.pop_back(); |
3053 | } |
3054 | } else if (Name && VarName == getCurrentRecord()->getNameInit()) { |
3055 | Stack.push_back(VarName); |
3056 | Val = Name->resolveReferences(*this); |
3057 | Stack.pop_back(); |
3058 | } |
3059 | |
3060 | Cache[VarName] = Val; |
3061 | return Val; |
3062 | } |
3063 | |
3064 | Init *TrackUnresolvedResolver::resolve(Init *VarName) { |
3065 | Init *I = nullptr; |
3066 | |
3067 | if (R) { |
3068 | I = R->resolve(VarName); |
3069 | if (I && !FoundUnresolved) { |
3070 | // Do not recurse into the resolved initializer, as that would change |
3071 | // the behavior of the resolver we're delegating, but do check to see |
3072 | // if there are unresolved variables remaining. |
3073 | TrackUnresolvedResolver Sub; |
3074 | I->resolveReferences(Sub); |
3075 | FoundUnresolved |= Sub.FoundUnresolved; |
3076 | } |
3077 | } |
3078 | |
3079 | if (!I) |
3080 | FoundUnresolved = true; |
3081 | return I; |
3082 | } |
3083 | |
3084 | Init *HasReferenceResolver::resolve(Init *VarName) |
3085 | { |
3086 | if (VarName == VarNameToTrack) |
3087 | Found = true; |
3088 | return nullptr; |
3089 | } |