Bug Summary

File:llvm/lib/TableGen/Record.cpp
Warning:line 796, column 25
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name Record.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mthread-model posix -mframe-pointer=none -fmath-errno -fno-rounding-math -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -dwarf-column-info -fno-split-dwarf-inlining -debugger-tuning=gdb -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-10/lib/clang/10.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/build-llvm/lib/TableGen -I /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/TableGen -I /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/build-llvm/include -I /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/backward -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-10/lib/clang/10.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/build-llvm/lib/TableGen -fdebug-prefix-map=/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd=. -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -o /tmp/scan-build-2020-01-13-084841-49055-1 -x c++ /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/TableGen/Record.cpp
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/ADT/ArrayRef.h"
14#include "llvm/ADT/DenseMap.h"
15#include "llvm/ADT/FoldingSet.h"
16#include "llvm/ADT/SmallString.h"
17#include "llvm/ADT/SmallVector.h"
18#include "llvm/ADT/Statistic.h"
19#include "llvm/ADT/StringExtras.h"
20#include "llvm/ADT/StringMap.h"
21#include "llvm/ADT/StringRef.h"
22#include "llvm/ADT/StringSet.h"
23#include "llvm/Config/llvm-config.h"
24#include "llvm/Support/Allocator.h"
25#include "llvm/Support/Casting.h"
26#include "llvm/Support/Compiler.h"
27#include "llvm/Support/ErrorHandling.h"
28#include "llvm/Support/SMLoc.h"
29#include "llvm/Support/raw_ostream.h"
30#include "llvm/TableGen/Error.h"
31#include "llvm/TableGen/Record.h"
32#include <cassert>
33#include <cstdint>
34#include <memory>
35#include <map>
36#include <string>
37#include <utility>
38#include <vector>
39
40using namespace llvm;
41
42#define DEBUG_TYPE"tblgen-records" "tblgen-records"
43
44static BumpPtrAllocator Allocator;
45
46STATISTIC(CodeInitsConstructed,static llvm::Statistic CodeInitsConstructed = {"tblgen-records"
, "CodeInitsConstructed", "The total number of unique CodeInits constructed"
}
47 "The total number of unique CodeInits constructed")static llvm::Statistic CodeInitsConstructed = {"tblgen-records"
, "CodeInitsConstructed", "The total number of unique CodeInits constructed"
}
;
48
49//===----------------------------------------------------------------------===//
50// Type implementations
51//===----------------------------------------------------------------------===//
52
53BitRecTy BitRecTy::Shared;
54CodeRecTy CodeRecTy::Shared;
55IntRecTy IntRecTy::Shared;
56StringRecTy StringRecTy::Shared;
57DagRecTy DagRecTy::Shared;
58
59#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
60LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void RecTy::dump() const { print(errs()); }
61#endif
62
63ListRecTy *RecTy::getListTy() {
64 if (!ListTy)
65 ListTy = new(Allocator) ListRecTy(this);
66 return ListTy;
67}
68
69bool RecTy::typeIsConvertibleTo(const RecTy *RHS) const {
70 assert(RHS && "NULL pointer")((RHS && "NULL pointer") ? static_cast<void> (0
) : __assert_fail ("RHS && \"NULL pointer\"", "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/TableGen/Record.cpp"
, 70, __PRETTY_FUNCTION__))
;
71 return Kind == RHS->getRecTyKind();
72}
73
74bool RecTy::typeIsA(const RecTy *RHS) const { return this == RHS; }
75
76bool BitRecTy::typeIsConvertibleTo(const RecTy *RHS) const{
77 if (RecTy::typeIsConvertibleTo(RHS) || RHS->getRecTyKind() == IntRecTyKind)
78 return true;
79 if (const BitsRecTy *BitsTy = dyn_cast<BitsRecTy>(RHS))
80 return BitsTy->getNumBits() == 1;
81 return false;
82}
83
84BitsRecTy *BitsRecTy::get(unsigned Sz) {
85 static std::vector<BitsRecTy*> Shared;
86 if (Sz >= Shared.size())
87 Shared.resize(Sz + 1);
88 BitsRecTy *&Ty = Shared[Sz];
89 if (!Ty)
90 Ty = new(Allocator) BitsRecTy(Sz);
91 return Ty;
92}
93
94std::string BitsRecTy::getAsString() const {
95 return "bits<" + utostr(Size) + ">";
96}
97
98bool BitsRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
99 if (RecTy::typeIsConvertibleTo(RHS)) //argument and the sender are same type
100 return cast<BitsRecTy>(RHS)->Size == Size;
101 RecTyKind kind = RHS->getRecTyKind();
102 return (kind == BitRecTyKind && Size == 1) || (kind == IntRecTyKind);
103}
104
105bool BitsRecTy::typeIsA(const RecTy *RHS) const {
106 if (const BitsRecTy *RHSb = dyn_cast<BitsRecTy>(RHS))
107 return RHSb->Size == Size;
108 return false;
109}
110
111bool IntRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
112 RecTyKind kind = RHS->getRecTyKind();
113 return kind==BitRecTyKind || kind==BitsRecTyKind || kind==IntRecTyKind;
114}
115
116bool CodeRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
117 RecTyKind Kind = RHS->getRecTyKind();
118 return Kind == CodeRecTyKind || Kind == StringRecTyKind;
119}
120
121std::string StringRecTy::getAsString() const {
122 return "string";
123}
124
125bool StringRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
126 RecTyKind Kind = RHS->getRecTyKind();
127 return Kind == StringRecTyKind || Kind == CodeRecTyKind;
128}
129
130std::string ListRecTy::getAsString() const {
131 return "list<" + Ty->getAsString() + ">";
132}
133
134bool ListRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
135 if (const auto *ListTy = dyn_cast<ListRecTy>(RHS))
136 return Ty->typeIsConvertibleTo(ListTy->getElementType());
137 return false;
138}
139
140bool ListRecTy::typeIsA(const RecTy *RHS) const {
141 if (const ListRecTy *RHSl = dyn_cast<ListRecTy>(RHS))
142 return getElementType()->typeIsA(RHSl->getElementType());
143 return false;
144}
145
146std::string DagRecTy::getAsString() const {
147 return "dag";
148}
149
150static void ProfileRecordRecTy(FoldingSetNodeID &ID,
151 ArrayRef<Record *> Classes) {
152 ID.AddInteger(Classes.size());
153 for (Record *R : Classes)
154 ID.AddPointer(R);
155}
156
157RecordRecTy *RecordRecTy::get(ArrayRef<Record *> UnsortedClasses) {
158 if (UnsortedClasses.empty()) {
159 static RecordRecTy AnyRecord(0);
160 return &AnyRecord;
161 }
162
163 FoldingSet<RecordRecTy> &ThePool =
164 UnsortedClasses[0]->getRecords().RecordTypePool;
165
166 SmallVector<Record *, 4> Classes(UnsortedClasses.begin(),
167 UnsortedClasses.end());
168 llvm::sort(Classes, [](Record *LHS, Record *RHS) {
169 return LHS->getNameInitAsString() < RHS->getNameInitAsString();
170 });
171
172 FoldingSetNodeID ID;
173 ProfileRecordRecTy(ID, Classes);
174
175 void *IP = nullptr;
176 if (RecordRecTy *Ty = ThePool.FindNodeOrInsertPos(ID, IP))
177 return Ty;
178
179#ifndef NDEBUG
180 // Check for redundancy.
181 for (unsigned i = 0; i < Classes.size(); ++i) {
182 for (unsigned j = 0; j < Classes.size(); ++j) {
183 assert(i == j || !Classes[i]->isSubClassOf(Classes[j]))((i == j || !Classes[i]->isSubClassOf(Classes[j])) ? static_cast
<void> (0) : __assert_fail ("i == j || !Classes[i]->isSubClassOf(Classes[j])"
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/TableGen/Record.cpp"
, 183, __PRETTY_FUNCTION__))
;
184 }
185 assert(&Classes[0]->getRecords() == &Classes[i]->getRecords())((&Classes[0]->getRecords() == &Classes[i]->getRecords
()) ? static_cast<void> (0) : __assert_fail ("&Classes[0]->getRecords() == &Classes[i]->getRecords()"
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/TableGen/Record.cpp"
, 185, __PRETTY_FUNCTION__))
;
186 }
187#endif
188
189 void *Mem = Allocator.Allocate(totalSizeToAlloc<Record *>(Classes.size()),
190 alignof(RecordRecTy));
191 RecordRecTy *Ty = new(Mem) RecordRecTy(Classes.size());
192 std::uninitialized_copy(Classes.begin(), Classes.end(),
193 Ty->getTrailingObjects<Record *>());
194 ThePool.InsertNode(Ty, IP);
195 return Ty;
196}
197
198void RecordRecTy::Profile(FoldingSetNodeID &ID) const {
199 ProfileRecordRecTy(ID, getClasses());
200}
201
202std::string RecordRecTy::getAsString() const {
203 if (NumClasses == 1)
204 return getClasses()[0]->getNameInitAsString();
205
206 std::string Str = "{";
207 bool First = true;
208 for (Record *R : getClasses()) {
209 if (!First)
210 Str += ", ";
211 First = false;
212 Str += R->getNameInitAsString();
213 }
214 Str += "}";
215 return Str;
216}
217
218bool RecordRecTy::isSubClassOf(Record *Class) const {
219 return llvm::any_of(getClasses(), [Class](Record *MySuperClass) {
220 return MySuperClass == Class ||
221 MySuperClass->isSubClassOf(Class);
222 });
223}
224
225bool RecordRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
226 if (this == RHS)
227 return true;
228
229 const RecordRecTy *RTy = dyn_cast<RecordRecTy>(RHS);
230 if (!RTy)
231 return false;
232
233 return llvm::all_of(RTy->getClasses(), [this](Record *TargetClass) {
234 return isSubClassOf(TargetClass);
235 });
236}
237
238bool RecordRecTy::typeIsA(const RecTy *RHS) const {
239 return typeIsConvertibleTo(RHS);
240}
241
242static RecordRecTy *resolveRecordTypes(RecordRecTy *T1, RecordRecTy *T2) {
243 SmallVector<Record *, 4> CommonSuperClasses;
244 SmallVector<Record *, 4> Stack;
245
246 Stack.insert(Stack.end(), T1->classes_begin(), T1->classes_end());
247
248 while (!Stack.empty()) {
249 Record *R = Stack.back();
250 Stack.pop_back();
251
252 if (T2->isSubClassOf(R)) {
253 CommonSuperClasses.push_back(R);
254 } else {
255 R->getDirectSuperClasses(Stack);
256 }
257 }
258
259 return RecordRecTy::get(CommonSuperClasses);
260}
261
262RecTy *llvm::resolveTypes(RecTy *T1, RecTy *T2) {
263 if (T1 == T2)
264 return T1;
265
266 if (RecordRecTy *RecTy1 = dyn_cast<RecordRecTy>(T1)) {
267 if (RecordRecTy *RecTy2 = dyn_cast<RecordRecTy>(T2))
268 return resolveRecordTypes(RecTy1, RecTy2);
269 }
270
271 if (T1->typeIsConvertibleTo(T2))
272 return T2;
273 if (T2->typeIsConvertibleTo(T1))
274 return T1;
275
276 if (ListRecTy *ListTy1 = dyn_cast<ListRecTy>(T1)) {
277 if (ListRecTy *ListTy2 = dyn_cast<ListRecTy>(T2)) {
278 RecTy* NewType = resolveTypes(ListTy1->getElementType(),
279 ListTy2->getElementType());
280 if (NewType)
281 return NewType->getListTy();
282 }
283 }
284
285 return nullptr;
286}
287
288//===----------------------------------------------------------------------===//
289// Initializer implementations
290//===----------------------------------------------------------------------===//
291
292void Init::anchor() {}
293
294#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
295LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void Init::dump() const { return print(errs()); }
296#endif
297
298UnsetInit *UnsetInit::get() {
299 static UnsetInit TheInit;
300 return &TheInit;
301}
302
303Init *UnsetInit::getCastTo(RecTy *Ty) const {
304 return const_cast<UnsetInit *>(this);
305}
306
307Init *UnsetInit::convertInitializerTo(RecTy *Ty) const {
308 return const_cast<UnsetInit *>(this);
309}
310
311BitInit *BitInit::get(bool V) {
312 static BitInit True(true);
313 static BitInit False(false);
314
315 return V ? &True : &False;
316}
317
318Init *BitInit::convertInitializerTo(RecTy *Ty) const {
319 if (isa<BitRecTy>(Ty))
320 return const_cast<BitInit *>(this);
321
322 if (isa<IntRecTy>(Ty))
323 return IntInit::get(getValue());
324
325 if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
326 // Can only convert single bit.
327 if (BRT->getNumBits() == 1)
328 return BitsInit::get(const_cast<BitInit *>(this));
329 }
330
331 return nullptr;
332}
333
334static void
335ProfileBitsInit(FoldingSetNodeID &ID, ArrayRef<Init *> Range) {
336 ID.AddInteger(Range.size());
337
338 for (Init *I : Range)
339 ID.AddPointer(I);
340}
341
342BitsInit *BitsInit::get(ArrayRef<Init *> Range) {
343 static FoldingSet<BitsInit> ThePool;
344
345 FoldingSetNodeID ID;
346 ProfileBitsInit(ID, Range);
347
348 void *IP = nullptr;
349 if (BitsInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
350 return I;
351
352 void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *>(Range.size()),
353 alignof(BitsInit));
354 BitsInit *I = new(Mem) BitsInit(Range.size());
355 std::uninitialized_copy(Range.begin(), Range.end(),
356 I->getTrailingObjects<Init *>());
357 ThePool.InsertNode(I, IP);
358 return I;
359}
360
361void BitsInit::Profile(FoldingSetNodeID &ID) const {
362 ProfileBitsInit(ID, makeArrayRef(getTrailingObjects<Init *>(), NumBits));
363}
364
365Init *BitsInit::convertInitializerTo(RecTy *Ty) const {
366 if (isa<BitRecTy>(Ty)) {
367 if (getNumBits() != 1) return nullptr; // Only accept if just one bit!
368 return getBit(0);
369 }
370
371 if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
372 // If the number of bits is right, return it. Otherwise we need to expand
373 // or truncate.
374 if (getNumBits() != BRT->getNumBits()) return nullptr;
375 return const_cast<BitsInit *>(this);
376 }
377
378 if (isa<IntRecTy>(Ty)) {
379 int64_t Result = 0;
380 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
381 if (auto *Bit = dyn_cast<BitInit>(getBit(i)))
382 Result |= static_cast<int64_t>(Bit->getValue()) << i;
383 else
384 return nullptr;
385 return IntInit::get(Result);
386 }
387
388 return nullptr;
389}
390
391Init *
392BitsInit::convertInitializerBitRange(ArrayRef<unsigned> Bits) const {
393 SmallVector<Init *, 16> NewBits(Bits.size());
394
395 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
396 if (Bits[i] >= getNumBits())
397 return nullptr;
398 NewBits[i] = getBit(Bits[i]);
399 }
400 return BitsInit::get(NewBits);
401}
402
403bool BitsInit::isConcrete() const {
404 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
405 if (!getBit(i)->isConcrete())
406 return false;
407 }
408 return true;
409}
410
411std::string BitsInit::getAsString() const {
412 std::string Result = "{ ";
413 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
414 if (i) Result += ", ";
415 if (Init *Bit = getBit(e-i-1))
416 Result += Bit->getAsString();
417 else
418 Result += "*";
419 }
420 return Result + " }";
421}
422
423// resolveReferences - If there are any field references that refer to fields
424// that have been filled in, we can propagate the values now.
425Init *BitsInit::resolveReferences(Resolver &R) const {
426 bool Changed = false;
427 SmallVector<Init *, 16> NewBits(getNumBits());
428
429 Init *CachedBitVarRef = nullptr;
430 Init *CachedBitVarResolved = nullptr;
431
432 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
433 Init *CurBit = getBit(i);
434 Init *NewBit = CurBit;
435
436 if (VarBitInit *CurBitVar = dyn_cast<VarBitInit>(CurBit)) {
437 if (CurBitVar->getBitVar() != CachedBitVarRef) {
438 CachedBitVarRef = CurBitVar->getBitVar();
439 CachedBitVarResolved = CachedBitVarRef->resolveReferences(R);
440 }
441 assert(CachedBitVarResolved && "Unresolved bitvar reference")((CachedBitVarResolved && "Unresolved bitvar reference"
) ? static_cast<void> (0) : __assert_fail ("CachedBitVarResolved && \"Unresolved bitvar reference\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/TableGen/Record.cpp"
, 441, __PRETTY_FUNCTION__))
;
442 NewBit = CachedBitVarResolved->getBit(CurBitVar->getBitNum());
443 } else {
444 // getBit(0) implicitly converts int and bits<1> values to bit.
445 NewBit = CurBit->resolveReferences(R)->getBit(0);
446 }
447
448 if (isa<UnsetInit>(NewBit) && R.keepUnsetBits())
449 NewBit = CurBit;
450 NewBits[i] = NewBit;
451 Changed |= CurBit != NewBit;
452 }
453
454 if (Changed)
455 return BitsInit::get(NewBits);
456
457 return const_cast<BitsInit *>(this);
458}
459
460IntInit *IntInit::get(int64_t V) {
461 static std::map<int64_t, IntInit*> ThePool;
462
463 IntInit *&I = ThePool[V];
464 if (!I) I = new(Allocator) IntInit(V);
465 return I;
466}
467
468std::string IntInit::getAsString() const {
469 return itostr(Value);
470}
471
472static bool canFitInBitfield(int64_t Value, unsigned NumBits) {
473 // For example, with NumBits == 4, we permit Values from [-7 .. 15].
474 return (NumBits >= sizeof(Value) * 8) ||
475 (Value >> NumBits == 0) || (Value >> (NumBits-1) == -1);
476}
477
478Init *IntInit::convertInitializerTo(RecTy *Ty) const {
479 if (isa<IntRecTy>(Ty))
480 return const_cast<IntInit *>(this);
481
482 if (isa<BitRecTy>(Ty)) {
483 int64_t Val = getValue();
484 if (Val != 0 && Val != 1) return nullptr; // Only accept 0 or 1 for a bit!
485 return BitInit::get(Val != 0);
486 }
487
488 if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
489 int64_t Value = getValue();
490 // Make sure this bitfield is large enough to hold the integer value.
491 if (!canFitInBitfield(Value, BRT->getNumBits()))
492 return nullptr;
493
494 SmallVector<Init *, 16> NewBits(BRT->getNumBits());
495 for (unsigned i = 0; i != BRT->getNumBits(); ++i)
496 NewBits[i] = BitInit::get(Value & ((i < 64) ? (1LL << i) : 0));
497
498 return BitsInit::get(NewBits);
499 }
500
501 return nullptr;
502}
503
504Init *
505IntInit::convertInitializerBitRange(ArrayRef<unsigned> Bits) const {
506 SmallVector<Init *, 16> NewBits(Bits.size());
507
508 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
509 if (Bits[i] >= 64)
510 return nullptr;
511
512 NewBits[i] = BitInit::get(Value & (INT64_C(1)1L << Bits[i]));
513 }
514 return BitsInit::get(NewBits);
515}
516
517CodeInit *CodeInit::get(StringRef V, const SMLoc &Loc) {
518 static StringSet<BumpPtrAllocator &> ThePool(Allocator);
519
520 CodeInitsConstructed++;
521
522 // Unlike StringMap, StringSet doesn't accept empty keys.
523 if (V.empty())
524 return new (Allocator) CodeInit("", Loc);
525
526 // Location tracking prevents us from de-duping CodeInits as we're never
527 // called with the same string and same location twice. However, we can at
528 // least de-dupe the strings for a modest saving.
529 auto &Entry = *ThePool.insert(V).first;
530 return new(Allocator) CodeInit(Entry.getKey(), Loc);
531}
532
533StringInit *StringInit::get(StringRef V) {
534 static StringMap<StringInit*, BumpPtrAllocator &> ThePool(Allocator);
535
536 auto &Entry = *ThePool.insert(std::make_pair(V, nullptr)).first;
537 if (!Entry.second)
538 Entry.second = new(Allocator) StringInit(Entry.getKey());
539 return Entry.second;
540}
541
542Init *StringInit::convertInitializerTo(RecTy *Ty) const {
543 if (isa<StringRecTy>(Ty))
544 return const_cast<StringInit *>(this);
545 if (isa<CodeRecTy>(Ty))
546 return CodeInit::get(getValue(), SMLoc());
547
548 return nullptr;
549}
550
551Init *CodeInit::convertInitializerTo(RecTy *Ty) const {
552 if (isa<CodeRecTy>(Ty))
553 return const_cast<CodeInit *>(this);
554 if (isa<StringRecTy>(Ty))
555 return StringInit::get(getValue());
556
557 return nullptr;
558}
559
560static void ProfileListInit(FoldingSetNodeID &ID,
561 ArrayRef<Init *> Range,
562 RecTy *EltTy) {
563 ID.AddInteger(Range.size());
564 ID.AddPointer(EltTy);
565
566 for (Init *I : Range)
567 ID.AddPointer(I);
568}
569
570ListInit *ListInit::get(ArrayRef<Init *> Range, RecTy *EltTy) {
571 static FoldingSet<ListInit> ThePool;
572
573 FoldingSetNodeID ID;
574 ProfileListInit(ID, Range, EltTy);
575
576 void *IP = nullptr;
577 if (ListInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
578 return I;
579
580 assert(Range.empty() || !isa<TypedInit>(Range[0]) ||((Range.empty() || !isa<TypedInit>(Range[0]) || cast<
TypedInit>(Range[0])->getType()->typeIsConvertibleTo
(EltTy)) ? static_cast<void> (0) : __assert_fail ("Range.empty() || !isa<TypedInit>(Range[0]) || cast<TypedInit>(Range[0])->getType()->typeIsConvertibleTo(EltTy)"
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/TableGen/Record.cpp"
, 581, __PRETTY_FUNCTION__))
581 cast<TypedInit>(Range[0])->getType()->typeIsConvertibleTo(EltTy))((Range.empty() || !isa<TypedInit>(Range[0]) || cast<
TypedInit>(Range[0])->getType()->typeIsConvertibleTo
(EltTy)) ? static_cast<void> (0) : __assert_fail ("Range.empty() || !isa<TypedInit>(Range[0]) || cast<TypedInit>(Range[0])->getType()->typeIsConvertibleTo(EltTy)"
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/TableGen/Record.cpp"
, 581, __PRETTY_FUNCTION__))
;
582
583 void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *>(Range.size()),
584 alignof(ListInit));
585 ListInit *I = new(Mem) ListInit(Range.size(), EltTy);
586 std::uninitialized_copy(Range.begin(), Range.end(),
587 I->getTrailingObjects<Init *>());
588 ThePool.InsertNode(I, IP);
589 return I;
590}
591
592void ListInit::Profile(FoldingSetNodeID &ID) const {
593 RecTy *EltTy = cast<ListRecTy>(getType())->getElementType();
594
595 ProfileListInit(ID, getValues(), EltTy);
596}
597
598Init *ListInit::convertInitializerTo(RecTy *Ty) const {
599 if (getType() == Ty)
600 return const_cast<ListInit*>(this);
601
602 if (auto *LRT = dyn_cast<ListRecTy>(Ty)) {
603 SmallVector<Init*, 8> Elements;
604 Elements.reserve(getValues().size());
605
606 // Verify that all of the elements of the list are subclasses of the
607 // appropriate class!
608 bool Changed = false;
609 RecTy *ElementType = LRT->getElementType();
610 for (Init *I : getValues())
611 if (Init *CI = I->convertInitializerTo(ElementType)) {
612 Elements.push_back(CI);
613 if (CI != I)
614 Changed = true;
615 } else
616 return nullptr;
617
618 if (!Changed)
619 return const_cast<ListInit*>(this);
620 return ListInit::get(Elements, ElementType);
621 }
622
623 return nullptr;
624}
625
626Init *ListInit::convertInitListSlice(ArrayRef<unsigned> Elements) const {
627 SmallVector<Init*, 8> Vals;
628 Vals.reserve(Elements.size());
629 for (unsigned Element : Elements) {
630 if (Element >= size())
631 return nullptr;
632 Vals.push_back(getElement(Element));
633 }
634 return ListInit::get(Vals, getElementType());
635}
636
637Record *ListInit::getElementAsRecord(unsigned i) const {
638 assert(i < NumValues && "List element index out of range!")((i < NumValues && "List element index out of range!"
) ? static_cast<void> (0) : __assert_fail ("i < NumValues && \"List element index out of range!\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/TableGen/Record.cpp"
, 638, __PRETTY_FUNCTION__))
;
639 DefInit *DI = dyn_cast<DefInit>(getElement(i));
640 if (!DI)
641 PrintFatalError("Expected record in list!");
642 return DI->getDef();
643}
644
645Init *ListInit::resolveReferences(Resolver &R) const {
646 SmallVector<Init*, 8> Resolved;
647 Resolved.reserve(size());
648 bool Changed = false;
649
650 for (Init *CurElt : getValues()) {
651 Init *E = CurElt->resolveReferences(R);
652 Changed |= E != CurElt;
653 Resolved.push_back(E);
654 }
655
656 if (Changed)
657 return ListInit::get(Resolved, getElementType());
658 return const_cast<ListInit *>(this);
659}
660
661bool ListInit::isConcrete() const {
662 for (Init *Element : *this) {
663 if (!Element->isConcrete())
664 return false;
665 }
666 return true;
667}
668
669std::string ListInit::getAsString() const {
670 std::string Result = "[";
671 const char *sep = "";
672 for (Init *Element : *this) {
673 Result += sep;
674 sep = ", ";
675 Result += Element->getAsString();
676 }
677 return Result + "]";
678}
679
680Init *OpInit::getBit(unsigned Bit) const {
681 if (getType() == BitRecTy::get())
682 return const_cast<OpInit*>(this);
683 return VarBitInit::get(const_cast<OpInit*>(this), Bit);
684}
685
686static void
687ProfileUnOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *Op, RecTy *Type) {
688 ID.AddInteger(Opcode);
689 ID.AddPointer(Op);
690 ID.AddPointer(Type);
691}
692
693UnOpInit *UnOpInit::get(UnaryOp Opc, Init *LHS, RecTy *Type) {
694 static FoldingSet<UnOpInit> ThePool;
695
696 FoldingSetNodeID ID;
697 ProfileUnOpInit(ID, Opc, LHS, Type);
698
699 void *IP = nullptr;
700 if (UnOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
701 return I;
702
703 UnOpInit *I = new(Allocator) UnOpInit(Opc, LHS, Type);
704 ThePool.InsertNode(I, IP);
705 return I;
706}
707
708void UnOpInit::Profile(FoldingSetNodeID &ID) const {
709 ProfileUnOpInit(ID, getOpcode(), getOperand(), getType());
710}
711
712Init *UnOpInit::Fold(Record *CurRec, bool IsFinal) const {
713 switch (getOpcode()) {
10
Control jumps to 'case GETOP:' at line 792
714 case CAST:
715 if (isa<StringRecTy>(getType())) {
716 if (StringInit *LHSs = dyn_cast<StringInit>(LHS))
717 return LHSs;
718
719 if (DefInit *LHSd = dyn_cast<DefInit>(LHS))
720 return StringInit::get(LHSd->getAsString());
721
722 if (IntInit *LHSi = dyn_cast<IntInit>(LHS))
723 return StringInit::get(LHSi->getAsString());
724 } else if (isa<RecordRecTy>(getType())) {
725 if (StringInit *Name = dyn_cast<StringInit>(LHS)) {
726 if (!CurRec && !IsFinal)
727 break;
728 assert(CurRec && "NULL pointer")((CurRec && "NULL pointer") ? static_cast<void>
(0) : __assert_fail ("CurRec && \"NULL pointer\"", "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/TableGen/Record.cpp"
, 728, __PRETTY_FUNCTION__))
;
729 Record *D;
730
731 // Self-references are allowed, but their resolution is delayed until
732 // the final resolve to ensure that we get the correct type for them.
733 if (Name == CurRec->getNameInit()) {
734 if (!IsFinal)
735 break;
736 D = CurRec;
737 } else {
738 D = CurRec->getRecords().getDef(Name->getValue());
739 if (!D) {
740 if (IsFinal)
741 PrintFatalError(CurRec->getLoc(),
742 Twine("Undefined reference to record: '") +
743 Name->getValue() + "'\n");
744 break;
745 }
746 }
747
748 DefInit *DI = DefInit::get(D);
749 if (!DI->getType()->typeIsA(getType())) {
750 PrintFatalError(CurRec->getLoc(),
751 Twine("Expected type '") +
752 getType()->getAsString() + "', got '" +
753 DI->getType()->getAsString() + "' in: " +
754 getAsString() + "\n");
755 }
756 return DI;
757 }
758 }
759
760 if (Init *NewInit = LHS->convertInitializerTo(getType()))
761 return NewInit;
762 break;
763
764 case HEAD:
765 if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) {
766 assert(!LHSl->empty() && "Empty list in head")((!LHSl->empty() && "Empty list in head") ? static_cast
<void> (0) : __assert_fail ("!LHSl->empty() && \"Empty list in head\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/TableGen/Record.cpp"
, 766, __PRETTY_FUNCTION__))
;
767 return LHSl->getElement(0);
768 }
769 break;
770
771 case TAIL:
772 if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) {
773 assert(!LHSl->empty() && "Empty list in tail")((!LHSl->empty() && "Empty list in tail") ? static_cast
<void> (0) : __assert_fail ("!LHSl->empty() && \"Empty list in tail\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/TableGen/Record.cpp"
, 773, __PRETTY_FUNCTION__))
;
774 // Note the +1. We can't just pass the result of getValues()
775 // directly.
776 return ListInit::get(LHSl->getValues().slice(1), LHSl->getElementType());
777 }
778 break;
779
780 case SIZE:
781 if (ListInit *LHSl = dyn_cast<ListInit>(LHS))
782 return IntInit::get(LHSl->size());
783 break;
784
785 case EMPTY:
786 if (ListInit *LHSl = dyn_cast<ListInit>(LHS))
787 return IntInit::get(LHSl->empty());
788 if (StringInit *LHSs = dyn_cast<StringInit>(LHS))
789 return IntInit::get(LHSs->getValue().empty());
790 break;
791
792 case GETOP:
793 if (DagInit *Dag
11.1
'Dag' is non-null
= dyn_cast<DagInit>(LHS)) {
11
Assuming field 'LHS' is a 'DagInit'
12
Taking true branch
794 DefInit *DI = DefInit::get(Dag->getOperatorAsDef({}));
795 if (!DI->getType()->typeIsA(getType())) {
13
Assuming the condition is true
14
Taking true branch
796 PrintFatalError(CurRec->getLoc(),
15
Called C++ object pointer is null
797 Twine("Expected type '") +
798 getType()->getAsString() + "', got '" +
799 DI->getType()->getAsString() + "' in: " +
800 getAsString() + "\n");
801 } else {
802 return DI;
803 }
804 }
805 break;
806 }
807 return const_cast<UnOpInit *>(this);
808}
809
810Init *UnOpInit::resolveReferences(Resolver &R) const {
811 Init *lhs = LHS->resolveReferences(R);
812
813 if (LHS != lhs || (R.isFinal() && getOpcode() == CAST))
814 return (UnOpInit::get(getOpcode(), lhs, getType()))
815 ->Fold(R.getCurrentRecord(), R.isFinal());
816 return const_cast<UnOpInit *>(this);
817}
818
819std::string UnOpInit::getAsString() const {
820 std::string Result;
821 switch (getOpcode()) {
822 case CAST: Result = "!cast<" + getType()->getAsString() + ">"; break;
823 case HEAD: Result = "!head"; break;
824 case TAIL: Result = "!tail"; break;
825 case SIZE: Result = "!size"; break;
826 case EMPTY: Result = "!empty"; break;
827 case GETOP: Result = "!getop"; break;
828 }
829 return Result + "(" + LHS->getAsString() + ")";
830}
831
832static void
833ProfileBinOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *LHS, Init *RHS,
834 RecTy *Type) {
835 ID.AddInteger(Opcode);
836 ID.AddPointer(LHS);
837 ID.AddPointer(RHS);
838 ID.AddPointer(Type);
839}
840
841BinOpInit *BinOpInit::get(BinaryOp Opc, Init *LHS,
842 Init *RHS, RecTy *Type) {
843 static FoldingSet<BinOpInit> ThePool;
844
845 FoldingSetNodeID ID;
846 ProfileBinOpInit(ID, Opc, LHS, RHS, Type);
847
848 void *IP = nullptr;
849 if (BinOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
850 return I;
851
852 BinOpInit *I = new(Allocator) BinOpInit(Opc, LHS, RHS, Type);
853 ThePool.InsertNode(I, IP);
854 return I;
855}
856
857void BinOpInit::Profile(FoldingSetNodeID &ID) const {
858 ProfileBinOpInit(ID, getOpcode(), getLHS(), getRHS(), getType());
859}
860
861static StringInit *ConcatStringInits(const StringInit *I0,
862 const StringInit *I1) {
863 SmallString<80> Concat(I0->getValue());
864 Concat.append(I1->getValue());
865 return StringInit::get(Concat);
866}
867
868Init *BinOpInit::getStrConcat(Init *I0, Init *I1) {
869 // Shortcut for the common case of concatenating two strings.
870 if (const StringInit *I0s = dyn_cast<StringInit>(I0))
871 if (const StringInit *I1s = dyn_cast<StringInit>(I1))
872 return ConcatStringInits(I0s, I1s);
873 return BinOpInit::get(BinOpInit::STRCONCAT, I0, I1, StringRecTy::get());
874}
875
876static ListInit *ConcatListInits(const ListInit *LHS,
877 const ListInit *RHS) {
878 SmallVector<Init *, 8> Args;
879 Args.insert(Args.end(), LHS->begin(), LHS->end());
880 Args.insert(Args.end(), RHS->begin(), RHS->end());
881 return ListInit::get(Args, LHS->getElementType());
882}
883
884Init *BinOpInit::getListConcat(TypedInit *LHS, Init *RHS) {
885 assert(isa<ListRecTy>(LHS->getType()) && "First arg must be a list")((isa<ListRecTy>(LHS->getType()) && "First arg must be a list"
) ? static_cast<void> (0) : __assert_fail ("isa<ListRecTy>(LHS->getType()) && \"First arg must be a list\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/TableGen/Record.cpp"
, 885, __PRETTY_FUNCTION__))
;
886
887 // Shortcut for the common case of concatenating two lists.
888 if (const ListInit *LHSList = dyn_cast<ListInit>(LHS))
889 if (const ListInit *RHSList = dyn_cast<ListInit>(RHS))
890 return ConcatListInits(LHSList, RHSList);
891 return BinOpInit::get(BinOpInit::LISTCONCAT, LHS, RHS, LHS->getType());
892}
893
894Init *BinOpInit::getListSplat(TypedInit *LHS, Init *RHS) {
895 return BinOpInit::get(BinOpInit::LISTSPLAT, LHS, RHS, LHS->getType());
896}
897
898Init *BinOpInit::Fold(Record *CurRec) const {
899 switch (getOpcode()) {
900 case CONCAT: {
901 DagInit *LHSs = dyn_cast<DagInit>(LHS);
902 DagInit *RHSs = dyn_cast<DagInit>(RHS);
903 if (LHSs && RHSs) {
904 DefInit *LOp = dyn_cast<DefInit>(LHSs->getOperator());
905 DefInit *ROp = dyn_cast<DefInit>(RHSs->getOperator());
906 if ((!LOp && !isa<UnsetInit>(LHSs->getOperator())) ||
907 (!ROp && !isa<UnsetInit>(RHSs->getOperator())))
908 break;
909 if (LOp && ROp && LOp->getDef() != ROp->getDef()) {
910 PrintFatalError(Twine("Concatenated Dag operators do not match: '") +
911 LHSs->getAsString() + "' vs. '" + RHSs->getAsString() +
912 "'");
913 }
914 Init *Op = LOp ? LOp : ROp;
915 if (!Op)
916 Op = UnsetInit::get();
917
918 SmallVector<Init*, 8> Args;
919 SmallVector<StringInit*, 8> ArgNames;
920 for (unsigned i = 0, e = LHSs->getNumArgs(); i != e; ++i) {
921 Args.push_back(LHSs->getArg(i));
922 ArgNames.push_back(LHSs->getArgName(i));
923 }
924 for (unsigned i = 0, e = RHSs->getNumArgs(); i != e; ++i) {
925 Args.push_back(RHSs->getArg(i));
926 ArgNames.push_back(RHSs->getArgName(i));
927 }
928 return DagInit::get(Op, nullptr, Args, ArgNames);
929 }
930 break;
931 }
932 case LISTCONCAT: {
933 ListInit *LHSs = dyn_cast<ListInit>(LHS);
934 ListInit *RHSs = dyn_cast<ListInit>(RHS);
935 if (LHSs && RHSs) {
936 SmallVector<Init *, 8> Args;
937 Args.insert(Args.end(), LHSs->begin(), LHSs->end());
938 Args.insert(Args.end(), RHSs->begin(), RHSs->end());
939 return ListInit::get(Args, LHSs->getElementType());
940 }
941 break;
942 }
943 case LISTSPLAT: {
944 TypedInit *Value = dyn_cast<TypedInit>(LHS);
945 IntInit *Size = dyn_cast<IntInit>(RHS);
946 if (Value && Size) {
947 SmallVector<Init *, 8> Args(Size->getValue(), Value);
948 return ListInit::get(Args, Value->getType());
949 }
950 break;
951 }
952 case STRCONCAT: {
953 StringInit *LHSs = dyn_cast<StringInit>(LHS);
954 StringInit *RHSs = dyn_cast<StringInit>(RHS);
955 if (LHSs && RHSs)
956 return ConcatStringInits(LHSs, RHSs);
957 break;
958 }
959 case EQ:
960 case NE:
961 case LE:
962 case LT:
963 case GE:
964 case GT: {
965 // try to fold eq comparison for 'bit' and 'int', otherwise fallback
966 // to string objects.
967 IntInit *L =
968 dyn_cast_or_null<IntInit>(LHS->convertInitializerTo(IntRecTy::get()));
969 IntInit *R =
970 dyn_cast_or_null<IntInit>(RHS->convertInitializerTo(IntRecTy::get()));
971
972 if (L && R) {
973 bool Result;
974 switch (getOpcode()) {
975 case EQ: Result = L->getValue() == R->getValue(); break;
976 case NE: Result = L->getValue() != R->getValue(); break;
977 case LE: Result = L->getValue() <= R->getValue(); break;
978 case LT: Result = L->getValue() < R->getValue(); break;
979 case GE: Result = L->getValue() >= R->getValue(); break;
980 case GT: Result = L->getValue() > R->getValue(); break;
981 default: llvm_unreachable("unhandled comparison")::llvm::llvm_unreachable_internal("unhandled comparison", "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/TableGen/Record.cpp"
, 981)
;
982 }
983 return BitInit::get(Result);
984 }
985
986 if (getOpcode() == EQ || getOpcode() == NE) {
987 StringInit *LHSs = dyn_cast<StringInit>(LHS);
988 StringInit *RHSs = dyn_cast<StringInit>(RHS);
989
990 // Make sure we've resolved
991 if (LHSs && RHSs) {
992 bool Equal = LHSs->getValue() == RHSs->getValue();
993 return BitInit::get(getOpcode() == EQ ? Equal : !Equal);
994 }
995 }
996
997 break;
998 }
999 case SETOP: {
1000 DagInit *Dag = dyn_cast<DagInit>(LHS);
1001 DefInit *Op = dyn_cast<DefInit>(RHS);
1002 if (Dag && Op) {
1003 SmallVector<Init*, 8> Args;
1004 SmallVector<StringInit*, 8> ArgNames;
1005 for (unsigned i = 0, e = Dag->getNumArgs(); i != e; ++i) {
1006 Args.push_back(Dag->getArg(i));
1007 ArgNames.push_back(Dag->getArgName(i));
1008 }
1009 return DagInit::get(Op, nullptr, Args, ArgNames);
1010 }
1011 break;
1012 }
1013 case ADD:
1014 case MUL:
1015 case AND:
1016 case OR:
1017 case SHL:
1018 case SRA:
1019 case SRL: {
1020 IntInit *LHSi =
1021 dyn_cast_or_null<IntInit>(LHS->convertInitializerTo(IntRecTy::get()));
1022 IntInit *RHSi =
1023 dyn_cast_or_null<IntInit>(RHS->convertInitializerTo(IntRecTy::get()));
1024 if (LHSi && RHSi) {
1025 int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
1026 int64_t Result;
1027 switch (getOpcode()) {
1028 default: llvm_unreachable("Bad opcode!")::llvm::llvm_unreachable_internal("Bad opcode!", "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/TableGen/Record.cpp"
, 1028)
;
1029 case ADD: Result = LHSv + RHSv; break;
1030 case MUL: Result = LHSv * RHSv; break;
1031 case AND: Result = LHSv & RHSv; break;
1032 case OR: Result = LHSv | RHSv; break;
1033 case SHL: Result = LHSv << RHSv; break;
1034 case SRA: Result = LHSv >> RHSv; break;
1035 case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break;
1036 }
1037 return IntInit::get(Result);
1038 }
1039 break;
1040 }
1041 }
1042 return const_cast<BinOpInit *>(this);
1043}
1044
1045Init *BinOpInit::resolveReferences(Resolver &R) const {
1046 Init *lhs = LHS->resolveReferences(R);
1047 Init *rhs = RHS->resolveReferences(R);
1048
1049 if (LHS != lhs || RHS != rhs)
1050 return (BinOpInit::get(getOpcode(), lhs, rhs, getType()))
1051 ->Fold(R.getCurrentRecord());
1052 return const_cast<BinOpInit *>(this);
1053}
1054
1055std::string BinOpInit::getAsString() const {
1056 std::string Result;
1057 switch (getOpcode()) {
1058 case CONCAT: Result = "!con"; break;
1059 case ADD: Result = "!add"; break;
1060 case MUL: Result = "!mul"; break;
1061 case AND: Result = "!and"; break;
1062 case OR: Result = "!or"; break;
1063 case SHL: Result = "!shl"; break;
1064 case SRA: Result = "!sra"; break;
1065 case SRL: Result = "!srl"; break;
1066 case EQ: Result = "!eq"; break;
1067 case NE: Result = "!ne"; break;
1068 case LE: Result = "!le"; break;
1069 case LT: Result = "!lt"; break;
1070 case GE: Result = "!ge"; break;
1071 case GT: Result = "!gt"; break;
1072 case LISTCONCAT: Result = "!listconcat"; break;
1073 case LISTSPLAT: Result = "!listsplat"; break;
1074 case STRCONCAT: Result = "!strconcat"; break;
1075 case SETOP: Result = "!setop"; break;
1076 }
1077 return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")";
1078}
1079
1080static void
1081ProfileTernOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *LHS, Init *MHS,
1082 Init *RHS, RecTy *Type) {
1083 ID.AddInteger(Opcode);
1084 ID.AddPointer(LHS);
1085 ID.AddPointer(MHS);
1086 ID.AddPointer(RHS);
1087 ID.AddPointer(Type);
1088}
1089
1090TernOpInit *TernOpInit::get(TernaryOp Opc, Init *LHS, Init *MHS, Init *RHS,
1091 RecTy *Type) {
1092 static FoldingSet<TernOpInit> ThePool;
1093
1094 FoldingSetNodeID ID;
1095 ProfileTernOpInit(ID, Opc, LHS, MHS, RHS, Type);
1096
1097 void *IP = nullptr;
1098 if (TernOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
1099 return I;
1100
1101 TernOpInit *I = new(Allocator) TernOpInit(Opc, LHS, MHS, RHS, Type);
1102 ThePool.InsertNode(I, IP);
1103 return I;
1104}
1105
1106void TernOpInit::Profile(FoldingSetNodeID &ID) const {
1107 ProfileTernOpInit(ID, getOpcode(), getLHS(), getMHS(), getRHS(), getType());
1108}
1109
1110static Init *ForeachApply(Init *LHS, Init *MHSe, Init *RHS, Record *CurRec) {
1111 MapResolver R(CurRec);
1112 R.set(LHS, MHSe);
1113 return RHS->resolveReferences(R);
1114}
1115
1116static Init *ForeachDagApply(Init *LHS, DagInit *MHSd, Init *RHS,
1117 Record *CurRec) {
1118 bool Change = false;
1119 Init *Val = ForeachApply(LHS, MHSd->getOperator(), RHS, CurRec);
1120 if (Val != MHSd->getOperator())
1121 Change = true;
1122
1123 SmallVector<std::pair<Init *, StringInit *>, 8> NewArgs;
1124 for (unsigned int i = 0; i < MHSd->getNumArgs(); ++i) {
1125 Init *Arg = MHSd->getArg(i);
1126 Init *NewArg;
1127 StringInit *ArgName = MHSd->getArgName(i);
1128
1129 if (DagInit *Argd = dyn_cast<DagInit>(Arg))
1130 NewArg = ForeachDagApply(LHS, Argd, RHS, CurRec);
1131 else
1132 NewArg = ForeachApply(LHS, Arg, RHS, CurRec);
1133
1134 NewArgs.push_back(std::make_pair(NewArg, ArgName));
1135 if (Arg != NewArg)
1136 Change = true;
1137 }
1138
1139 if (Change)
1140 return DagInit::get(Val, nullptr, NewArgs);
1141 return MHSd;
1142}
1143
1144// Applies RHS to all elements of MHS, using LHS as a temp variable.
1145static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
1146 Record *CurRec) {
1147 if (DagInit *MHSd = dyn_cast<DagInit>(MHS))
1148 return ForeachDagApply(LHS, MHSd, RHS, CurRec);
1149
1150 if (ListInit *MHSl = dyn_cast<ListInit>(MHS)) {
1151 SmallVector<Init *, 8> NewList(MHSl->begin(), MHSl->end());
1152
1153 for (Init *&Item : NewList) {
1154 Init *NewItem = ForeachApply(LHS, Item, RHS, CurRec);
1155 if (NewItem != Item)
1156 Item = NewItem;
1157 }
1158 return ListInit::get(NewList, cast<ListRecTy>(Type)->getElementType());
1159 }
1160
1161 return nullptr;
1162}
1163
1164Init *TernOpInit::Fold(Record *CurRec) const {
1165 switch (getOpcode()) {
1166 case SUBST: {
1167 DefInit *LHSd = dyn_cast<DefInit>(LHS);
1168 VarInit *LHSv = dyn_cast<VarInit>(LHS);
1169 StringInit *LHSs = dyn_cast<StringInit>(LHS);
1170
1171 DefInit *MHSd = dyn_cast<DefInit>(MHS);
1172 VarInit *MHSv = dyn_cast<VarInit>(MHS);
1173 StringInit *MHSs = dyn_cast<StringInit>(MHS);
1174
1175 DefInit *RHSd = dyn_cast<DefInit>(RHS);
1176 VarInit *RHSv = dyn_cast<VarInit>(RHS);
1177 StringInit *RHSs = dyn_cast<StringInit>(RHS);
1178
1179 if (LHSd && MHSd && RHSd) {
1180 Record *Val = RHSd->getDef();
1181 if (LHSd->getAsString() == RHSd->getAsString())
1182 Val = MHSd->getDef();
1183 return DefInit::get(Val);
1184 }
1185 if (LHSv && MHSv && RHSv) {
1186 std::string Val = RHSv->getName();
1187 if (LHSv->getAsString() == RHSv->getAsString())
1188 Val = MHSv->getName();
1189 return VarInit::get(Val, getType());
1190 }
1191 if (LHSs && MHSs && RHSs) {
1192 std::string Val = RHSs->getValue();
1193
1194 std::string::size_type found;
1195 std::string::size_type idx = 0;
1196 while (true) {
1197 found = Val.find(LHSs->getValue(), idx);
1198 if (found == std::string::npos)
1199 break;
1200 Val.replace(found, LHSs->getValue().size(), MHSs->getValue());
1201 idx = found + MHSs->getValue().size();
1202 }
1203
1204 return StringInit::get(Val);
1205 }
1206 break;
1207 }
1208
1209 case FOREACH: {
1210 if (Init *Result = ForeachHelper(LHS, MHS, RHS, getType(), CurRec))
1211 return Result;
1212 break;
1213 }
1214
1215 case IF: {
1216 if (IntInit *LHSi = dyn_cast_or_null<IntInit>(
1217 LHS->convertInitializerTo(IntRecTy::get()))) {
1218 if (LHSi->getValue())
1219 return MHS;
1220 return RHS;
1221 }
1222 break;
1223 }
1224
1225 case DAG: {
1226 ListInit *MHSl = dyn_cast<ListInit>(MHS);
1227 ListInit *RHSl = dyn_cast<ListInit>(RHS);
1228 bool MHSok = MHSl || isa<UnsetInit>(MHS);
1229 bool RHSok = RHSl || isa<UnsetInit>(RHS);
1230
1231 if (isa<UnsetInit>(MHS) && isa<UnsetInit>(RHS))
1232 break; // Typically prevented by the parser, but might happen with template args
1233
1234 if (MHSok && RHSok && (!MHSl || !RHSl || MHSl->size() == RHSl->size())) {
1235 SmallVector<std::pair<Init *, StringInit *>, 8> Children;
1236 unsigned Size = MHSl ? MHSl->size() : RHSl->size();
1237 for (unsigned i = 0; i != Size; ++i) {
1238 Init *Node = MHSl ? MHSl->getElement(i) : UnsetInit::get();
1239 Init *Name = RHSl ? RHSl->getElement(i) : UnsetInit::get();
1240 if (!isa<StringInit>(Name) && !isa<UnsetInit>(Name))
1241 return const_cast<TernOpInit *>(this);
1242 Children.emplace_back(Node, dyn_cast<StringInit>(Name));
1243 }
1244 return DagInit::get(LHS, nullptr, Children);
1245 }
1246 break;
1247 }
1248 }
1249
1250 return const_cast<TernOpInit *>(this);
1251}
1252
1253Init *TernOpInit::resolveReferences(Resolver &R) const {
1254 Init *lhs = LHS->resolveReferences(R);
1255
1256 if (getOpcode() == IF && lhs != LHS) {
1257 if (IntInit *Value = dyn_cast_or_null<IntInit>(
1258 lhs->convertInitializerTo(IntRecTy::get()))) {
1259 // Short-circuit
1260 if (Value->getValue())
1261 return MHS->resolveReferences(R);
1262 return RHS->resolveReferences(R);
1263 }
1264 }
1265
1266 Init *mhs = MHS->resolveReferences(R);
1267 Init *rhs;
1268
1269 if (getOpcode() == FOREACH) {
1270 ShadowResolver SR(R);
1271 SR.addShadow(lhs);
1272 rhs = RHS->resolveReferences(SR);
1273 } else {
1274 rhs = RHS->resolveReferences(R);
1275 }
1276
1277 if (LHS != lhs || MHS != mhs || RHS != rhs)
1278 return (TernOpInit::get(getOpcode(), lhs, mhs, rhs, getType()))
1279 ->Fold(R.getCurrentRecord());
1280 return const_cast<TernOpInit *>(this);
1281}
1282
1283std::string TernOpInit::getAsString() const {
1284 std::string Result;
1285 bool UnquotedLHS = false;
1286 switch (getOpcode()) {
1287 case SUBST: Result = "!subst"; break;
1288 case FOREACH: Result = "!foreach"; UnquotedLHS = true; break;
1289 case IF: Result = "!if"; break;
1290 case DAG: Result = "!dag"; break;
1291 }
1292 return (Result + "(" +
1293 (UnquotedLHS ? LHS->getAsUnquotedString() : LHS->getAsString()) +
1294 ", " + MHS->getAsString() + ", " + RHS->getAsString() + ")");
1295}
1296
1297static void ProfileFoldOpInit(FoldingSetNodeID &ID, Init *A, Init *B,
1298 Init *Start, Init *List, Init *Expr,
1299 RecTy *Type) {
1300 ID.AddPointer(Start);
1301 ID.AddPointer(List);
1302 ID.AddPointer(A);
1303 ID.AddPointer(B);
1304 ID.AddPointer(Expr);
1305 ID.AddPointer(Type);
1306}
1307
1308FoldOpInit *FoldOpInit::get(Init *Start, Init *List, Init *A, Init *B,
1309 Init *Expr, RecTy *Type) {
1310 static FoldingSet<FoldOpInit> ThePool;
1311
1312 FoldingSetNodeID ID;
1313 ProfileFoldOpInit(ID, Start, List, A, B, Expr, Type);
1314
1315 void *IP = nullptr;
1316 if (FoldOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
1317 return I;
1318
1319 FoldOpInit *I = new (Allocator) FoldOpInit(Start, List, A, B, Expr, Type);
1320 ThePool.InsertNode(I, IP);
1321 return I;
1322}
1323
1324void FoldOpInit::Profile(FoldingSetNodeID &ID) const {
1325 ProfileFoldOpInit(ID, Start, List, A, B, Expr, getType());
1326}
1327
1328Init *FoldOpInit::Fold(Record *CurRec) const {
1329 if (ListInit *LI = dyn_cast<ListInit>(List)) {
1330 Init *Accum = Start;
1331 for (Init *Elt : *LI) {
1332 MapResolver R(CurRec);
1333 R.set(A, Accum);
1334 R.set(B, Elt);
1335 Accum = Expr->resolveReferences(R);
1336 }
1337 return Accum;
1338 }
1339 return const_cast<FoldOpInit *>(this);
1340}
1341
1342Init *FoldOpInit::resolveReferences(Resolver &R) const {
1343 Init *NewStart = Start->resolveReferences(R);
1344 Init *NewList = List->resolveReferences(R);
1345 ShadowResolver SR(R);
1346 SR.addShadow(A);
1347 SR.addShadow(B);
1348 Init *NewExpr = Expr->resolveReferences(SR);
1349
1350 if (Start == NewStart && List == NewList && Expr == NewExpr)
1351 return const_cast<FoldOpInit *>(this);
1352
1353 return get(NewStart, NewList, A, B, NewExpr, getType())
1354 ->Fold(R.getCurrentRecord());
1355}
1356
1357Init *FoldOpInit::getBit(unsigned Bit) const {
1358 return VarBitInit::get(const_cast<FoldOpInit *>(this), Bit);
1359}
1360
1361std::string FoldOpInit::getAsString() const {
1362 return (Twine("!foldl(") + Start->getAsString() + ", " + List->getAsString() +
1363 ", " + A->getAsUnquotedString() + ", " + B->getAsUnquotedString() +
1364 ", " + Expr->getAsString() + ")")
1365 .str();
1366}
1367
1368static void ProfileIsAOpInit(FoldingSetNodeID &ID, RecTy *CheckType,
1369 Init *Expr) {
1370 ID.AddPointer(CheckType);
1371 ID.AddPointer(Expr);
1372}
1373
1374IsAOpInit *IsAOpInit::get(RecTy *CheckType, Init *Expr) {
1375 static FoldingSet<IsAOpInit> ThePool;
1376
1377 FoldingSetNodeID ID;
1378 ProfileIsAOpInit(ID, CheckType, Expr);
1379
1380 void *IP = nullptr;
1381 if (IsAOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
1382 return I;
1383
1384 IsAOpInit *I = new (Allocator) IsAOpInit(CheckType, Expr);
1385 ThePool.InsertNode(I, IP);
1386 return I;
1387}
1388
1389void IsAOpInit::Profile(FoldingSetNodeID &ID) const {
1390 ProfileIsAOpInit(ID, CheckType, Expr);
1391}
1392
1393Init *IsAOpInit::Fold() const {
1394 if (TypedInit *TI = dyn_cast<TypedInit>(Expr)) {
1395 // Is the expression type known to be (a subclass of) the desired type?
1396 if (TI->getType()->typeIsConvertibleTo(CheckType))
1397 return IntInit::get(1);
1398
1399 if (isa<RecordRecTy>(CheckType)) {
1400 // If the target type is not a subclass of the expression type, or if
1401 // the expression has fully resolved to a record, we know that it can't
1402 // be of the required type.
1403 if (!CheckType->typeIsConvertibleTo(TI->getType()) || isa<DefInit>(Expr))
1404 return IntInit::get(0);
1405 } else {
1406 // We treat non-record types as not castable.
1407 return IntInit::get(0);
1408 }
1409 }
1410 return const_cast<IsAOpInit *>(this);
1411}
1412
1413Init *IsAOpInit::resolveReferences(Resolver &R) const {
1414 Init *NewExpr = Expr->resolveReferences(R);
1415 if (Expr != NewExpr)
1416 return get(CheckType, NewExpr)->Fold();
1417 return const_cast<IsAOpInit *>(this);
1418}
1419
1420Init *IsAOpInit::getBit(unsigned Bit) const {
1421 return VarBitInit::get(const_cast<IsAOpInit *>(this), Bit);
1422}
1423
1424std::string IsAOpInit::getAsString() const {
1425 return (Twine("!isa<") + CheckType->getAsString() + ">(" +
1426 Expr->getAsString() + ")")
1427 .str();
1428}
1429
1430RecTy *TypedInit::getFieldType(StringInit *FieldName) const {
1431 if (RecordRecTy *RecordType = dyn_cast<RecordRecTy>(getType())) {
1432 for (Record *Rec : RecordType->getClasses()) {
1433 if (RecordVal *Field = Rec->getValue(FieldName))
1434 return Field->getType();
1435 }
1436 }
1437 return nullptr;
1438}
1439
1440Init *
1441TypedInit::convertInitializerTo(RecTy *Ty) const {
1442 if (getType() == Ty || getType()->typeIsA(Ty))
1443 return const_cast<TypedInit *>(this);
1444
1445 if (isa<BitRecTy>(getType()) && isa<BitsRecTy>(Ty) &&
1446 cast<BitsRecTy>(Ty)->getNumBits() == 1)
1447 return BitsInit::get({const_cast<TypedInit *>(this)});
1448
1449 return nullptr;
1450}
1451
1452Init *TypedInit::convertInitializerBitRange(ArrayRef<unsigned> Bits) const {
1453 BitsRecTy *T = dyn_cast<BitsRecTy>(getType());
1454 if (!T) return nullptr; // Cannot subscript a non-bits variable.
1455 unsigned NumBits = T->getNumBits();
1456
1457 SmallVector<Init *, 16> NewBits;
1458 NewBits.reserve(Bits.size());
1459 for (unsigned Bit : Bits) {
1460 if (Bit >= NumBits)
1461 return nullptr;
1462
1463 NewBits.push_back(VarBitInit::get(const_cast<TypedInit *>(this), Bit));
1464 }
1465 return BitsInit::get(NewBits);
1466}
1467
1468Init *TypedInit::getCastTo(RecTy *Ty) const {
1469 // Handle the common case quickly
1470 if (getType() == Ty || getType()->typeIsA(Ty))
1
Assuming the condition is false
2
Assuming the condition is false
3
Taking false branch
1471 return const_cast<TypedInit *>(this);
1472
1473 if (Init *Converted = convertInitializerTo(Ty)) {
4
Assuming 'Converted' is null
5
Taking false branch
1474 assert(!isa<TypedInit>(Converted) ||((!isa<TypedInit>(Converted) || cast<TypedInit>(Converted
)->getType()->typeIsA(Ty)) ? static_cast<void> (0
) : __assert_fail ("!isa<TypedInit>(Converted) || cast<TypedInit>(Converted)->getType()->typeIsA(Ty)"
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/TableGen/Record.cpp"
, 1475, __PRETTY_FUNCTION__))
1475 cast<TypedInit>(Converted)->getType()->typeIsA(Ty))((!isa<TypedInit>(Converted) || cast<TypedInit>(Converted
)->getType()->typeIsA(Ty)) ? static_cast<void> (0
) : __assert_fail ("!isa<TypedInit>(Converted) || cast<TypedInit>(Converted)->getType()->typeIsA(Ty)"
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/TableGen/Record.cpp"
, 1475, __PRETTY_FUNCTION__))
;
1476 return Converted;
1477 }
1478
1479 if (!getType()->typeIsConvertibleTo(Ty))
6
Assuming the condition is false
7
Taking false branch
1480 return nullptr;
1481
1482 return UnOpInit::get(UnOpInit::CAST, const_cast<TypedInit *>(this), Ty)
9
Calling 'UnOpInit::Fold'
1483 ->Fold(nullptr);
8
Passing null pointer value via 1st parameter 'CurRec'
1484}
1485
1486Init *TypedInit::convertInitListSlice(ArrayRef<unsigned> Elements) const {
1487 ListRecTy *T = dyn_cast<ListRecTy>(getType());
1488 if (!T) return nullptr; // Cannot subscript a non-list variable.
1489
1490 if (Elements.size() == 1)
1491 return VarListElementInit::get(const_cast<TypedInit *>(this), Elements[0]);
1492
1493 SmallVector<Init*, 8> ListInits;
1494 ListInits.reserve(Elements.size());
1495 for (unsigned Element : Elements)
1496 ListInits.push_back(VarListElementInit::get(const_cast<TypedInit *>(this),
1497 Element));
1498 return ListInit::get(ListInits, T->getElementType());
1499}
1500
1501
1502VarInit *VarInit::get(StringRef VN, RecTy *T) {
1503 Init *Value = StringInit::get(VN);
1504 return VarInit::get(Value, T);
1505}
1506
1507VarInit *VarInit::get(Init *VN, RecTy *T) {
1508 using Key = std::pair<RecTy *, Init *>;
1509 static DenseMap<Key, VarInit*> ThePool;
1510
1511 Key TheKey(std::make_pair(T, VN));
1512
1513 VarInit *&I = ThePool[TheKey];
1514 if (!I)
1515 I = new(Allocator) VarInit(VN, T);
1516 return I;
1517}
1518
1519StringRef VarInit::getName() const {
1520 StringInit *NameString = cast<StringInit>(getNameInit());
1521 return NameString->getValue();
1522}
1523
1524Init *VarInit::getBit(unsigned Bit) const {
1525 if (getType() == BitRecTy::get())
1526 return const_cast<VarInit*>(this);
1527 return VarBitInit::get(const_cast<VarInit*>(this), Bit);
1528}
1529
1530Init *VarInit::resolveReferences(Resolver &R) const {
1531 if (Init *Val = R.resolve(VarName))
1532 return Val;
1533 return const_cast<VarInit *>(this);
1534}
1535
1536VarBitInit *VarBitInit::get(TypedInit *T, unsigned B) {
1537 using Key = std::pair<TypedInit *, unsigned>;
1538 static DenseMap<Key, VarBitInit*> ThePool;
1539
1540 Key TheKey(std::make_pair(T, B));
1541
1542 VarBitInit *&I = ThePool[TheKey];
1543 if (!I)
1544 I = new(Allocator) VarBitInit(T, B);
1545 return I;
1546}
1547
1548std::string VarBitInit::getAsString() const {
1549 return TI->getAsString() + "{" + utostr(Bit) + "}";
1550}
1551
1552Init *VarBitInit::resolveReferences(Resolver &R) const {
1553 Init *I = TI->resolveReferences(R);
1554 if (TI != I)
1555 return I->getBit(getBitNum());
1556
1557 return const_cast<VarBitInit*>(this);
1558}
1559
1560VarListElementInit *VarListElementInit::get(TypedInit *T,
1561 unsigned E) {
1562 using Key = std::pair<TypedInit *, unsigned>;
1563 static DenseMap<Key, VarListElementInit*> ThePool;
1564
1565 Key TheKey(std::make_pair(T, E));
1566
1567 VarListElementInit *&I = ThePool[TheKey];
1568 if (!I) I = new(Allocator) VarListElementInit(T, E);
1569 return I;
1570}
1571
1572std::string VarListElementInit::getAsString() const {
1573 return TI->getAsString() + "[" + utostr(Element) + "]";
1574}
1575
1576Init *VarListElementInit::resolveReferences(Resolver &R) const {
1577 Init *NewTI = TI->resolveReferences(R);
1578 if (ListInit *List = dyn_cast<ListInit>(NewTI)) {
1579 // Leave out-of-bounds array references as-is. This can happen without
1580 // being an error, e.g. in the untaken "branch" of an !if expression.
1581 if (getElementNum() < List->size())
1582 return List->getElement(getElementNum());
1583 }
1584 if (NewTI != TI && isa<TypedInit>(NewTI))
1585 return VarListElementInit::get(cast<TypedInit>(NewTI), getElementNum());
1586 return const_cast<VarListElementInit *>(this);
1587}
1588
1589Init *VarListElementInit::getBit(unsigned Bit) const {
1590 if (getType() == BitRecTy::get())
1591 return const_cast<VarListElementInit*>(this);
1592 return VarBitInit::get(const_cast<VarListElementInit*>(this), Bit);
1593}
1594
1595DefInit::DefInit(Record *D)
1596 : TypedInit(IK_DefInit, D->getType()), Def(D) {}
1597
1598DefInit *DefInit::get(Record *R) {
1599 return R->getDefInit();
1600}
1601
1602Init *DefInit::convertInitializerTo(RecTy *Ty) const {
1603 if (auto *RRT = dyn_cast<RecordRecTy>(Ty))
1604 if (getType()->typeIsConvertibleTo(RRT))
1605 return const_cast<DefInit *>(this);
1606 return nullptr;
1607}
1608
1609RecTy *DefInit::getFieldType(StringInit *FieldName) const {
1610 if (const RecordVal *RV = Def->getValue(FieldName))
1611 return RV->getType();
1612 return nullptr;
1613}
1614
1615std::string DefInit::getAsString() const {
1616 return Def->getName();
1617}
1618
1619static void ProfileVarDefInit(FoldingSetNodeID &ID,
1620 Record *Class,
1621 ArrayRef<Init *> Args) {
1622 ID.AddInteger(Args.size());
1623 ID.AddPointer(Class);
1624
1625 for (Init *I : Args)
1626 ID.AddPointer(I);
1627}
1628
1629VarDefInit *VarDefInit::get(Record *Class, ArrayRef<Init *> Args) {
1630 static FoldingSet<VarDefInit> ThePool;
1631
1632 FoldingSetNodeID ID;
1633 ProfileVarDefInit(ID, Class, Args);
1634
1635 void *IP = nullptr;
1636 if (VarDefInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
1637 return I;
1638
1639 void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *>(Args.size()),
1640 alignof(VarDefInit));
1641 VarDefInit *I = new(Mem) VarDefInit(Class, Args.size());
1642 std::uninitialized_copy(Args.begin(), Args.end(),
1643 I->getTrailingObjects<Init *>());
1644 ThePool.InsertNode(I, IP);
1645 return I;
1646}
1647
1648void VarDefInit::Profile(FoldingSetNodeID &ID) const {
1649 ProfileVarDefInit(ID, Class, args());
1650}
1651
1652DefInit *VarDefInit::instantiate() {
1653 if (!Def) {
1654 RecordKeeper &Records = Class->getRecords();
1655 auto NewRecOwner = std::make_unique<Record>(Records.getNewAnonymousName(),
1656 Class->getLoc(), Records,
1657 /*IsAnonymous=*/true);
1658 Record *NewRec = NewRecOwner.get();
1659
1660 // Copy values from class to instance
1661 for (const RecordVal &Val : Class->getValues())
1662 NewRec->addValue(Val);
1663
1664 // Substitute and resolve template arguments
1665 ArrayRef<Init *> TArgs = Class->getTemplateArgs();
1666 MapResolver R(NewRec);
1667
1668 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1669 if (i < args_size())
1670 R.set(TArgs[i], getArg(i));
1671 else
1672 R.set(TArgs[i], NewRec->getValue(TArgs[i])->getValue());
1673
1674 NewRec->removeValue(TArgs[i]);
1675 }
1676
1677 NewRec->resolveReferences(R);
1678
1679 // Add superclasses.
1680 ArrayRef<std::pair<Record *, SMRange>> SCs = Class->getSuperClasses();
1681 for (const auto &SCPair : SCs)
1682 NewRec->addSuperClass(SCPair.first, SCPair.second);
1683
1684 NewRec->addSuperClass(Class,
1685 SMRange(Class->getLoc().back(),
1686 Class->getLoc().back()));
1687
1688 // Resolve internal references and store in record keeper
1689 NewRec->resolveReferences();
1690 Records.addDef(std::move(NewRecOwner));
1691
1692 Def = DefInit::get(NewRec);
1693 }
1694
1695 return Def;
1696}
1697
1698Init *VarDefInit::resolveReferences(Resolver &R) const {
1699 TrackUnresolvedResolver UR(&R);
1700 bool Changed = false;
1701 SmallVector<Init *, 8> NewArgs;
1702 NewArgs.reserve(args_size());
1703
1704 for (Init *Arg : args()) {
1705 Init *NewArg = Arg->resolveReferences(UR);
1706 NewArgs.push_back(NewArg);
1707 Changed |= NewArg != Arg;
1708 }
1709
1710 if (Changed) {
1711 auto New = VarDefInit::get(Class, NewArgs);
1712 if (!UR.foundUnresolved())
1713 return New->instantiate();
1714 return New;
1715 }
1716 return const_cast<VarDefInit *>(this);
1717}
1718
1719Init *VarDefInit::Fold() const {
1720 if (Def)
1721 return Def;
1722
1723 TrackUnresolvedResolver R;
1724 for (Init *Arg : args())
1725 Arg->resolveReferences(R);
1726
1727 if (!R.foundUnresolved())
1728 return const_cast<VarDefInit *>(this)->instantiate();
1729 return const_cast<VarDefInit *>(this);
1730}
1731
1732std::string VarDefInit::getAsString() const {
1733 std::string Result = Class->getNameInitAsString() + "<";
1734 const char *sep = "";
1735 for (Init *Arg : args()) {
1736 Result += sep;
1737 sep = ", ";
1738 Result += Arg->getAsString();
1739 }
1740 return Result + ">";
1741}
1742
1743FieldInit *FieldInit::get(Init *R, StringInit *FN) {
1744 using Key = std::pair<Init *, StringInit *>;
1745 static DenseMap<Key, FieldInit*> ThePool;
1746
1747 Key TheKey(std::make_pair(R, FN));
1748
1749 FieldInit *&I = ThePool[TheKey];
1750 if (!I) I = new(Allocator) FieldInit(R, FN);
1751 return I;
1752}
1753
1754Init *FieldInit::getBit(unsigned Bit) const {
1755 if (getType() == BitRecTy::get())
1756 return const_cast<FieldInit*>(this);
1757 return VarBitInit::get(const_cast<FieldInit*>(this), Bit);
1758}
1759
1760Init *FieldInit::resolveReferences(Resolver &R) const {
1761 Init *NewRec = Rec->resolveReferences(R);
1762 if (NewRec != Rec)
1763 return FieldInit::get(NewRec, FieldName)->Fold(R.getCurrentRecord());
1764 return const_cast<FieldInit *>(this);
1765}
1766
1767Init *FieldInit::Fold(Record *CurRec) const {
1768 if (DefInit *DI = dyn_cast<DefInit>(Rec)) {
1769 Record *Def = DI->getDef();
1770 if (Def == CurRec)
1771 PrintFatalError(CurRec->getLoc(),
1772 Twine("Attempting to access field '") +
1773 FieldName->getAsUnquotedString() + "' of '" +
1774 Rec->getAsString() + "' is a forbidden self-reference");
1775 Init *FieldVal = Def->getValue(FieldName)->getValue();
1776 if (FieldVal->isComplete())
1777 return FieldVal;
1778 }
1779 return const_cast<FieldInit *>(this);
1780}
1781
1782static void ProfileCondOpInit(FoldingSetNodeID &ID,
1783 ArrayRef<Init *> CondRange,
1784 ArrayRef<Init *> ValRange,
1785 const RecTy *ValType) {
1786 assert(CondRange.size() == ValRange.size() &&((CondRange.size() == ValRange.size() && "Number of conditions and values must match!"
) ? static_cast<void> (0) : __assert_fail ("CondRange.size() == ValRange.size() && \"Number of conditions and values must match!\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/TableGen/Record.cpp"
, 1787, __PRETTY_FUNCTION__))
1787 "Number of conditions and values must match!")((CondRange.size() == ValRange.size() && "Number of conditions and values must match!"
) ? static_cast<void> (0) : __assert_fail ("CondRange.size() == ValRange.size() && \"Number of conditions and values must match!\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/TableGen/Record.cpp"
, 1787, __PRETTY_FUNCTION__))
;
1788 ID.AddPointer(ValType);
1789 ArrayRef<Init *>::iterator Case = CondRange.begin();
1790 ArrayRef<Init *>::iterator Val = ValRange.begin();
1791
1792 while (Case != CondRange.end()) {
1793 ID.AddPointer(*Case++);
1794 ID.AddPointer(*Val++);
1795 }
1796}
1797
1798void CondOpInit::Profile(FoldingSetNodeID &ID) const {
1799 ProfileCondOpInit(ID,
1800 makeArrayRef(getTrailingObjects<Init *>(), NumConds),
1801 makeArrayRef(getTrailingObjects<Init *>() + NumConds, NumConds),
1802 ValType);
1803}
1804
1805CondOpInit *
1806CondOpInit::get(ArrayRef<Init *> CondRange,
1807 ArrayRef<Init *> ValRange, RecTy *Ty) {
1808 assert(CondRange.size() == ValRange.size() &&((CondRange.size() == ValRange.size() && "Number of conditions and values must match!"
) ? static_cast<void> (0) : __assert_fail ("CondRange.size() == ValRange.size() && \"Number of conditions and values must match!\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/TableGen/Record.cpp"
, 1809, __PRETTY_FUNCTION__))
1809 "Number of conditions and values must match!")((CondRange.size() == ValRange.size() && "Number of conditions and values must match!"
) ? static_cast<void> (0) : __assert_fail ("CondRange.size() == ValRange.size() && \"Number of conditions and values must match!\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/TableGen/Record.cpp"
, 1809, __PRETTY_FUNCTION__))
;
1810
1811 static FoldingSet<CondOpInit> ThePool;
1812 FoldingSetNodeID ID;
1813 ProfileCondOpInit(ID, CondRange, ValRange, Ty);
1814
1815 void *IP = nullptr;
1816 if (CondOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
1817 return I;
1818
1819 void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *>(2*CondRange.size()),
1820 alignof(BitsInit));
1821 CondOpInit *I = new(Mem) CondOpInit(CondRange.size(), Ty);
1822
1823 std::uninitialized_copy(CondRange.begin(), CondRange.end(),
1824 I->getTrailingObjects<Init *>());
1825 std::uninitialized_copy(ValRange.begin(), ValRange.end(),
1826 I->getTrailingObjects<Init *>()+CondRange.size());
1827 ThePool.InsertNode(I, IP);
1828 return I;
1829}
1830
1831Init *CondOpInit::resolveReferences(Resolver &R) const {
1832 SmallVector<Init*, 4> NewConds;
1833 bool Changed = false;
1834 for (const Init *Case : getConds()) {
1835 Init *NewCase = Case->resolveReferences(R);
1836 NewConds.push_back(NewCase);
1837 Changed |= NewCase != Case;
1838 }
1839
1840 SmallVector<Init*, 4> NewVals;
1841 for (const Init *Val : getVals()) {
1842 Init *NewVal = Val->resolveReferences(R);
1843 NewVals.push_back(NewVal);
1844 Changed |= NewVal != Val;
1845 }
1846
1847 if (Changed)
1848 return (CondOpInit::get(NewConds, NewVals,
1849 getValType()))->Fold(R.getCurrentRecord());
1850
1851 return const_cast<CondOpInit *>(this);
1852}
1853
1854Init *CondOpInit::Fold(Record *CurRec) const {
1855 for ( unsigned i = 0; i < NumConds; ++i) {
1856 Init *Cond = getCond(i);
1857 Init *Val = getVal(i);
1858
1859 if (IntInit *CondI = dyn_cast_or_null<IntInit>(
1860 Cond->convertInitializerTo(IntRecTy::get()))) {
1861 if (CondI->getValue())
1862 return Val->convertInitializerTo(getValType());
1863 } else
1864 return const_cast<CondOpInit *>(this);
1865 }
1866
1867 PrintFatalError(CurRec->getLoc(),
1868 CurRec->getName() +
1869 " does not have any true condition in:" +
1870 this->getAsString());
1871 return nullptr;
1872}
1873
1874bool CondOpInit::isConcrete() const {
1875 for (const Init *Case : getConds())
1876 if (!Case->isConcrete())
1877 return false;
1878
1879 for (const Init *Val : getVals())
1880 if (!Val->isConcrete())
1881 return false;
1882
1883 return true;
1884}
1885
1886bool CondOpInit::isComplete() const {
1887 for (const Init *Case : getConds())
1888 if (!Case->isComplete())
1889 return false;
1890
1891 for (const Init *Val : getVals())
1892 if (!Val->isConcrete())
1893 return false;
1894
1895 return true;
1896}
1897
1898std::string CondOpInit::getAsString() const {
1899 std::string Result = "!cond(";
1900 for (unsigned i = 0; i < getNumConds(); i++) {
1901 Result += getCond(i)->getAsString() + ": ";
1902 Result += getVal(i)->getAsString();
1903 if (i != getNumConds()-1)
1904 Result += ", ";
1905 }
1906 return Result + ")";
1907}
1908
1909Init *CondOpInit::getBit(unsigned Bit) const {
1910 return VarBitInit::get(const_cast<CondOpInit *>(this), Bit);
1911}
1912
1913static void ProfileDagInit(FoldingSetNodeID &ID, Init *V, StringInit *VN,
1914 ArrayRef<Init *> ArgRange,
1915 ArrayRef<StringInit *> NameRange) {
1916 ID.AddPointer(V);
1917 ID.AddPointer(VN);
1918
1919 ArrayRef<Init *>::iterator Arg = ArgRange.begin();
1920 ArrayRef<StringInit *>::iterator Name = NameRange.begin();
1921 while (Arg != ArgRange.end()) {
1922 assert(Name != NameRange.end() && "Arg name underflow!")((Name != NameRange.end() && "Arg name underflow!") ?
static_cast<void> (0) : __assert_fail ("Name != NameRange.end() && \"Arg name underflow!\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/TableGen/Record.cpp"
, 1922, __PRETTY_FUNCTION__))
;
1923 ID.AddPointer(*Arg++);
1924 ID.AddPointer(*Name++);
1925 }
1926 assert(Name == NameRange.end() && "Arg name overflow!")((Name == NameRange.end() && "Arg name overflow!") ? static_cast
<void> (0) : __assert_fail ("Name == NameRange.end() && \"Arg name overflow!\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/TableGen/Record.cpp"
, 1926, __PRETTY_FUNCTION__))
;
1927}
1928
1929DagInit *
1930DagInit::get(Init *V, StringInit *VN, ArrayRef<Init *> ArgRange,
1931 ArrayRef<StringInit *> NameRange) {
1932 static FoldingSet<DagInit> ThePool;
1933
1934 FoldingSetNodeID ID;
1935 ProfileDagInit(ID, V, VN, ArgRange, NameRange);
1936
1937 void *IP = nullptr;
1938 if (DagInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
1939 return I;
1940
1941 void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *, StringInit *>(ArgRange.size(), NameRange.size()), alignof(BitsInit));
1942 DagInit *I = new(Mem) DagInit(V, VN, ArgRange.size(), NameRange.size());
1943 std::uninitialized_copy(ArgRange.begin(), ArgRange.end(),
1944 I->getTrailingObjects<Init *>());
1945 std::uninitialized_copy(NameRange.begin(), NameRange.end(),
1946 I->getTrailingObjects<StringInit *>());
1947 ThePool.InsertNode(I, IP);
1948 return I;
1949}
1950
1951DagInit *
1952DagInit::get(Init *V, StringInit *VN,
1953 ArrayRef<std::pair<Init*, StringInit*>> args) {
1954 SmallVector<Init *, 8> Args;
1955 SmallVector<StringInit *, 8> Names;
1956
1957 for (const auto &Arg : args) {
1958 Args.push_back(Arg.first);
1959 Names.push_back(Arg.second);
1960 }
1961
1962 return DagInit::get(V, VN, Args, Names);
1963}
1964
1965void DagInit::Profile(FoldingSetNodeID &ID) const {
1966 ProfileDagInit(ID, Val, ValName, makeArrayRef(getTrailingObjects<Init *>(), NumArgs), makeArrayRef(getTrailingObjects<StringInit *>(), NumArgNames));
1967}
1968
1969Record *DagInit::getOperatorAsDef(ArrayRef<SMLoc> Loc) const {
1970 if (DefInit *DefI = dyn_cast<DefInit>(Val))
1971 return DefI->getDef();
1972 PrintFatalError(Loc, "Expected record as operator");
1973 return nullptr;
1974}
1975
1976Init *DagInit::resolveReferences(Resolver &R) const {
1977 SmallVector<Init*, 8> NewArgs;
1978 NewArgs.reserve(arg_size());
1979 bool ArgsChanged = false;
1980 for (const Init *Arg : getArgs()) {
1981 Init *NewArg = Arg->resolveReferences(R);
1982 NewArgs.push_back(NewArg);
1983 ArgsChanged |= NewArg != Arg;
1984 }
1985
1986 Init *Op = Val->resolveReferences(R);
1987 if (Op != Val || ArgsChanged)
1988 return DagInit::get(Op, ValName, NewArgs, getArgNames());
1989
1990 return const_cast<DagInit *>(this);
1991}
1992
1993bool DagInit::isConcrete() const {
1994 if (!Val->isConcrete())
1995 return false;
1996 for (const Init *Elt : getArgs()) {
1997 if (!Elt->isConcrete())
1998 return false;
1999 }
2000 return true;
2001}
2002
2003std::string DagInit::getAsString() const {
2004 std::string Result = "(" + Val->getAsString();
2005 if (ValName)
2006 Result += ":" + ValName->getAsUnquotedString();
2007 if (!arg_empty()) {
2008 Result += " " + getArg(0)->getAsString();
2009 if (getArgName(0)) Result += ":$" + getArgName(0)->getAsUnquotedString();
2010 for (unsigned i = 1, e = getNumArgs(); i != e; ++i) {
2011 Result += ", " + getArg(i)->getAsString();
2012 if (getArgName(i)) Result += ":$" + getArgName(i)->getAsUnquotedString();
2013 }
2014 }
2015 return Result + ")";
2016}
2017
2018//===----------------------------------------------------------------------===//
2019// Other implementations
2020//===----------------------------------------------------------------------===//
2021
2022RecordVal::RecordVal(Init *N, RecTy *T, bool P)
2023 : Name(N), TyAndPrefix(T, P) {
2024 setValue(UnsetInit::get());
2025 assert(Value && "Cannot create unset value for current type!")((Value && "Cannot create unset value for current type!"
) ? static_cast<void> (0) : __assert_fail ("Value && \"Cannot create unset value for current type!\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/TableGen/Record.cpp"
, 2025, __PRETTY_FUNCTION__))
;
2026}
2027
2028StringRef RecordVal::getName() const {
2029 return cast<StringInit>(getNameInit())->getValue();
2030}
2031
2032bool RecordVal::setValue(Init *V) {
2033 if (V) {
2034 Value = V->getCastTo(getType());
2035 if (Value) {
2036 assert(!isa<TypedInit>(Value) ||((!isa<TypedInit>(Value) || cast<TypedInit>(Value
)->getType()->typeIsA(getType())) ? static_cast<void
> (0) : __assert_fail ("!isa<TypedInit>(Value) || cast<TypedInit>(Value)->getType()->typeIsA(getType())"
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/TableGen/Record.cpp"
, 2037, __PRETTY_FUNCTION__))
2037 cast<TypedInit>(Value)->getType()->typeIsA(getType()))((!isa<TypedInit>(Value) || cast<TypedInit>(Value
)->getType()->typeIsA(getType())) ? static_cast<void
> (0) : __assert_fail ("!isa<TypedInit>(Value) || cast<TypedInit>(Value)->getType()->typeIsA(getType())"
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/TableGen/Record.cpp"
, 2037, __PRETTY_FUNCTION__))
;
2038 if (BitsRecTy *BTy = dyn_cast<BitsRecTy>(getType())) {
2039 if (!isa<BitsInit>(Value)) {
2040 SmallVector<Init *, 64> Bits;
2041 Bits.reserve(BTy->getNumBits());
2042 for (unsigned i = 0, e = BTy->getNumBits(); i < e; ++i)
2043 Bits.push_back(Value->getBit(i));
2044 Value = BitsInit::get(Bits);
2045 }
2046 }
2047 }
2048 return Value == nullptr;
2049 }
2050 Value = nullptr;
2051 return false;
2052}
2053
2054#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2055LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void RecordVal::dump() const { errs() << *this; }
2056#endif
2057
2058void RecordVal::print(raw_ostream &OS, bool PrintSem) const {
2059 if (getPrefix()) OS << "field ";
2060 OS << *getType() << " " << getNameInitAsString();
2061
2062 if (getValue())
2063 OS << " = " << *getValue();
2064
2065 if (PrintSem) OS << ";\n";
2066}
2067
2068unsigned Record::LastID = 0;
2069
2070void Record::checkName() {
2071 // Ensure the record name has string type.
2072 const TypedInit *TypedName = cast<const TypedInit>(Name);
2073 if (!isa<StringRecTy>(TypedName->getType()))
2074 PrintFatalError(getLoc(), Twine("Record name '") + Name->getAsString() +
2075 "' is not a string!");
2076}
2077
2078RecordRecTy *Record::getType() {
2079 SmallVector<Record *, 4> DirectSCs;
2080 getDirectSuperClasses(DirectSCs);
2081 return RecordRecTy::get(DirectSCs);
2082}
2083
2084DefInit *Record::getDefInit() {
2085 if (!TheInit)
2086 TheInit = new(Allocator) DefInit(this);
2087 return TheInit;
2088}
2089
2090void Record::setName(Init *NewName) {
2091 Name = NewName;
2092 checkName();
2093 // DO NOT resolve record values to the name at this point because
2094 // there might be default values for arguments of this def. Those
2095 // arguments might not have been resolved yet so we don't want to
2096 // prematurely assume values for those arguments were not passed to
2097 // this def.
2098 //
2099 // Nonetheless, it may be that some of this Record's values
2100 // reference the record name. Indeed, the reason for having the
2101 // record name be an Init is to provide this flexibility. The extra
2102 // resolve steps after completely instantiating defs takes care of
2103 // this. See TGParser::ParseDef and TGParser::ParseDefm.
2104}
2105
2106void Record::getDirectSuperClasses(SmallVectorImpl<Record *> &Classes) const {
2107 ArrayRef<std::pair<Record *, SMRange>> SCs = getSuperClasses();
2108 while (!SCs.empty()) {
2109 // Superclasses are in reverse preorder, so 'back' is a direct superclass,
2110 // and its transitive superclasses are directly preceding it.
2111 Record *SC = SCs.back().first;
2112 SCs = SCs.drop_back(1 + SC->getSuperClasses().size());
2113 Classes.push_back(SC);
2114 }
2115}
2116
2117void Record::resolveReferences(Resolver &R, const RecordVal *SkipVal) {
2118 for (RecordVal &Value : Values) {
2119 if (SkipVal == &Value) // Skip resolve the same field as the given one
2120 continue;
2121 if (Init *V = Value.getValue()) {
2122 Init *VR = V->resolveReferences(R);
2123 if (Value.setValue(VR)) {
2124 std::string Type;
2125 if (TypedInit *VRT = dyn_cast<TypedInit>(VR))
2126 Type =
2127 (Twine("of type '") + VRT->getType()->getAsString() + "' ").str();
2128 PrintFatalError(getLoc(), Twine("Invalid value ") + Type +
2129 "is found when setting '" +
2130 Value.getNameInitAsString() +
2131 "' of type '" +
2132 Value.getType()->getAsString() +
2133 "' after resolving references: " +
2134 VR->getAsUnquotedString() + "\n");
2135 }
2136 }
2137 }
2138 Init *OldName = getNameInit();
2139 Init *NewName = Name->resolveReferences(R);
2140 if (NewName != OldName) {
2141 // Re-register with RecordKeeper.
2142 setName(NewName);
2143 }
2144}
2145
2146void Record::resolveReferences() {
2147 RecordResolver R(*this);
2148 R.setFinal(true);
2149 resolveReferences(R);
2150}
2151
2152void Record::resolveReferencesTo(const RecordVal *RV) {
2153 RecordValResolver R(*this, RV);
2154 resolveReferences(R, RV);
2155}
2156
2157#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2158LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void Record::dump() const { errs() << *this; }
2159#endif
2160
2161raw_ostream &llvm::operator<<(raw_ostream &OS, const Record &R) {
2162 OS << R.getNameInitAsString();
2163
2164 ArrayRef<Init *> TArgs = R.getTemplateArgs();
2165 if (!TArgs.empty()) {
2166 OS << "<";
2167 bool NeedComma = false;
2168 for (const Init *TA : TArgs) {
2169 if (NeedComma) OS << ", ";
2170 NeedComma = true;
2171 const RecordVal *RV = R.getValue(TA);
2172 assert(RV && "Template argument record not found??")((RV && "Template argument record not found??") ? static_cast
<void> (0) : __assert_fail ("RV && \"Template argument record not found??\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/TableGen/Record.cpp"
, 2172, __PRETTY_FUNCTION__))
;
2173 RV->print(OS, false);
2174 }
2175 OS << ">";
2176 }
2177
2178 OS << " {";
2179 ArrayRef<std::pair<Record *, SMRange>> SC = R.getSuperClasses();
2180 if (!SC.empty()) {
2181 OS << "\t//";
2182 for (const auto &SuperPair : SC)
2183 OS << " " << SuperPair.first->getNameInitAsString();
2184 }
2185 OS << "\n";
2186
2187 for (const RecordVal &Val : R.getValues())
2188 if (Val.getPrefix() && !R.isTemplateArg(Val.getNameInit()))
2189 OS << Val;
2190 for (const RecordVal &Val : R.getValues())
2191 if (!Val.getPrefix() && !R.isTemplateArg(Val.getNameInit()))
2192 OS << Val;
2193
2194 return OS << "}\n";
2195}
2196
2197Init *Record::getValueInit(StringRef FieldName) const {
2198 const RecordVal *R = getValue(FieldName);
2199 if (!R || !R->getValue())
2200 PrintFatalError(getLoc(), "Record `" + getName() +
2201 "' does not have a field named `" + FieldName + "'!\n");
2202 return R->getValue();
2203}
2204
2205StringRef Record::getValueAsString(StringRef FieldName) const {
2206 const RecordVal *R = getValue(FieldName);
2207 if (!R || !R->getValue())
2208 PrintFatalError(getLoc(), "Record `" + getName() +
2209 "' does not have a field named `" + FieldName + "'!\n");
2210
2211 if (StringInit *SI = dyn_cast<StringInit>(R->getValue()))
2212 return SI->getValue();
2213 if (CodeInit *CI = dyn_cast<CodeInit>(R->getValue()))
2214 return CI->getValue();
2215
2216 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2217 FieldName + "' does not have a string initializer!");
2218}
2219
2220BitsInit *Record::getValueAsBitsInit(StringRef FieldName) const {
2221 const RecordVal *R = getValue(FieldName);
2222 if (!R || !R->getValue())
2223 PrintFatalError(getLoc(), "Record `" + getName() +
2224 "' does not have a field named `" + FieldName + "'!\n");
2225
2226 if (BitsInit *BI = dyn_cast<BitsInit>(R->getValue()))
2227 return BI;
2228 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2229 FieldName + "' does not have a BitsInit initializer!");
2230}
2231
2232ListInit *Record::getValueAsListInit(StringRef FieldName) const {
2233 const RecordVal *R = getValue(FieldName);
2234 if (!R || !R->getValue())
2235 PrintFatalError(getLoc(), "Record `" + getName() +
2236 "' does not have a field named `" + FieldName + "'!\n");
2237
2238 if (ListInit *LI = dyn_cast<ListInit>(R->getValue()))
2239 return LI;
2240 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2241 FieldName + "' does not have a list initializer!");
2242}
2243
2244std::vector<Record*>
2245Record::getValueAsListOfDefs(StringRef FieldName) const {
2246 ListInit *List = getValueAsListInit(FieldName);
2247 std::vector<Record*> Defs;
2248 for (Init *I : List->getValues()) {
2249 if (DefInit *DI = dyn_cast<DefInit>(I))
2250 Defs.push_back(DI->getDef());
2251 else
2252 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2253 FieldName + "' list is not entirely DefInit!");
2254 }
2255 return Defs;
2256}
2257
2258int64_t Record::getValueAsInt(StringRef FieldName) const {
2259 const RecordVal *R = getValue(FieldName);
2260 if (!R || !R->getValue())
2261 PrintFatalError(getLoc(), "Record `" + getName() +
2262 "' does not have a field named `" + FieldName + "'!\n");
2263
2264 if (IntInit *II = dyn_cast<IntInit>(R->getValue()))
2265 return II->getValue();
2266 PrintFatalError(getLoc(), Twine("Record `") + getName() + "', field `" +
2267 FieldName +
2268 "' does not have an int initializer: " +
2269 R->getValue()->getAsString());
2270}
2271
2272std::vector<int64_t>
2273Record::getValueAsListOfInts(StringRef FieldName) const {
2274 ListInit *List = getValueAsListInit(FieldName);
2275 std::vector<int64_t> Ints;
2276 for (Init *I : List->getValues()) {
2277 if (IntInit *II = dyn_cast<IntInit>(I))
2278 Ints.push_back(II->getValue());
2279 else
2280 PrintFatalError(getLoc(),
2281 Twine("Record `") + getName() + "', field `" + FieldName +
2282 "' does not have a list of ints initializer: " +
2283 I->getAsString());
2284 }
2285 return Ints;
2286}
2287
2288std::vector<StringRef>
2289Record::getValueAsListOfStrings(StringRef FieldName) const {
2290 ListInit *List = getValueAsListInit(FieldName);
2291 std::vector<StringRef> Strings;
2292 for (Init *I : List->getValues()) {
2293 if (StringInit *SI = dyn_cast<StringInit>(I))
2294 Strings.push_back(SI->getValue());
2295 else
2296 PrintFatalError(getLoc(),
2297 Twine("Record `") + getName() + "', field `" + FieldName +
2298 "' does not have a list of strings initializer: " +
2299 I->getAsString());
2300 }
2301 return Strings;
2302}
2303
2304Record *Record::getValueAsDef(StringRef FieldName) const {
2305 const RecordVal *R = getValue(FieldName);
2306 if (!R || !R->getValue())
2307 PrintFatalError(getLoc(), "Record `" + getName() +
2308 "' does not have a field named `" + FieldName + "'!\n");
2309
2310 if (DefInit *DI = dyn_cast<DefInit>(R->getValue()))
2311 return DI->getDef();
2312 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2313 FieldName + "' does not have a def initializer!");
2314}
2315
2316Record *Record::getValueAsOptionalDef(StringRef FieldName) const {
2317 const RecordVal *R = getValue(FieldName);
2318 if (!R || !R->getValue())
2319 PrintFatalError(getLoc(), "Record `" + getName() +
2320 "' does not have a field named `" + FieldName + "'!\n");
2321
2322 if (DefInit *DI = dyn_cast<DefInit>(R->getValue()))
2323 return DI->getDef();
2324 if (isa<UnsetInit>(R->getValue()))
2325 return nullptr;
2326 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2327 FieldName + "' does not have either a def initializer or '?'!");
2328}
2329
2330
2331bool Record::getValueAsBit(StringRef FieldName) const {
2332 const RecordVal *R = getValue(FieldName);
2333 if (!R || !R->getValue())
2334 PrintFatalError(getLoc(), "Record `" + getName() +
2335 "' does not have a field named `" + FieldName + "'!\n");
2336
2337 if (BitInit *BI = dyn_cast<BitInit>(R->getValue()))
2338 return BI->getValue();
2339 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2340 FieldName + "' does not have a bit initializer!");
2341}
2342
2343bool Record::getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const {
2344 const RecordVal *R = getValue(FieldName);
2345 if (!R || !R->getValue())
2346 PrintFatalError(getLoc(), "Record `" + getName() +
2347 "' does not have a field named `" + FieldName.str() + "'!\n");
2348
2349 if (isa<UnsetInit>(R->getValue())) {
2350 Unset = true;
2351 return false;
2352 }
2353 Unset = false;
2354 if (BitInit *BI = dyn_cast<BitInit>(R->getValue()))
2355 return BI->getValue();
2356 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2357 FieldName + "' does not have a bit initializer!");
2358}
2359
2360DagInit *Record::getValueAsDag(StringRef FieldName) const {
2361 const RecordVal *R = getValue(FieldName);
2362 if (!R || !R->getValue())
2363 PrintFatalError(getLoc(), "Record `" + getName() +
2364 "' does not have a field named `" + FieldName + "'!\n");
2365
2366 if (DagInit *DI = dyn_cast<DagInit>(R->getValue()))
2367 return DI;
2368 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
2369 FieldName + "' does not have a dag initializer!");
2370}
2371
2372#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2373LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void RecordKeeper::dump() const { errs() << *this; }
2374#endif
2375
2376raw_ostream &llvm::operator<<(raw_ostream &OS, const RecordKeeper &RK) {
2377 OS << "------------- Classes -----------------\n";
2378 for (const auto &C : RK.getClasses())
2379 OS << "class " << *C.second;
2380
2381 OS << "------------- Defs -----------------\n";
2382 for (const auto &D : RK.getDefs())
2383 OS << "def " << *D.second;
2384 return OS;
2385}
2386
2387/// GetNewAnonymousName - Generate a unique anonymous name that can be used as
2388/// an identifier.
2389Init *RecordKeeper::getNewAnonymousName() {
2390 return StringInit::get("anonymous_" + utostr(AnonCounter++));
2391}
2392
2393std::vector<Record *>
2394RecordKeeper::getAllDerivedDefinitions(StringRef ClassName) const {
2395 Record *Class = getClass(ClassName);
2396 if (!Class)
2397 PrintFatalError("ERROR: Couldn't find the `" + ClassName + "' class!\n");
2398
2399 std::vector<Record*> Defs;
2400 for (const auto &D : getDefs())
2401 if (D.second->isSubClassOf(Class))
2402 Defs.push_back(D.second.get());
2403
2404 return Defs;
2405}
2406
2407Init *MapResolver::resolve(Init *VarName) {
2408 auto It = Map.find(VarName);
2409 if (It == Map.end())
2410 return nullptr;
2411
2412 Init *I = It->second.V;
2413
2414 if (!It->second.Resolved && Map.size() > 1) {
2415 // Resolve mutual references among the mapped variables, but prevent
2416 // infinite recursion.
2417 Map.erase(It);
2418 I = I->resolveReferences(*this);
2419 Map[VarName] = {I, true};
2420 }
2421
2422 return I;
2423}
2424
2425Init *RecordResolver::resolve(Init *VarName) {
2426 Init *Val = Cache.lookup(VarName);
2427 if (Val)
2428 return Val;
2429
2430 for (Init *S : Stack) {
2431 if (S == VarName)
2432 return nullptr; // prevent infinite recursion
2433 }
2434
2435 if (RecordVal *RV = getCurrentRecord()->getValue(VarName)) {
2436 if (!isa<UnsetInit>(RV->getValue())) {
2437 Val = RV->getValue();
2438 Stack.push_back(VarName);
2439 Val = Val->resolveReferences(*this);
2440 Stack.pop_back();
2441 }
2442 }
2443
2444 Cache[VarName] = Val;
2445 return Val;
2446}
2447
2448Init *TrackUnresolvedResolver::resolve(Init *VarName) {
2449 Init *I = nullptr;
2450
2451 if (R) {
2452 I = R->resolve(VarName);
2453 if (I && !FoundUnresolved) {
2454 // Do not recurse into the resolved initializer, as that would change
2455 // the behavior of the resolver we're delegating, but do check to see
2456 // if there are unresolved variables remaining.
2457 TrackUnresolvedResolver Sub;
2458 I->resolveReferences(Sub);
2459 FoundUnresolved |= Sub.FoundUnresolved;
2460 }
2461 }
2462
2463 if (!I)
2464 FoundUnresolved = true;
2465 return I;
2466}
2467
2468Init *HasReferenceResolver::resolve(Init *VarName)
2469{
2470 if (VarName == VarNameToTrack)
2471 Found = true;
2472 return nullptr;
2473}