Bug Summary

File:lib/TableGen/Record.cpp
Warning:line 253, column 27
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-eagerly-assume -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 -mrelocation-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-7/lib/clang/7.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-7~svn326551/build-llvm/lib/TableGen -I /build/llvm-toolchain-snapshot-7~svn326551/lib/TableGen -I /build/llvm-toolchain-snapshot-7~svn326551/build-llvm/include -I /build/llvm-toolchain-snapshot-7~svn326551/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/x86_64-linux-gnu/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/x86_64-linux-gnu/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0/backward -internal-isystem /usr/include/clang/7.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-7/lib/clang/7.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++11 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-7~svn326551/build-llvm/lib/TableGen -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-checker optin.performance.Padding -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2018-03-02-155150-1477-1 -x c++ /build/llvm-toolchain-snapshot-7~svn326551/lib/TableGen/Record.cpp
1//===- Record.cpp - Record implementation ---------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Implement the tablegen record classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/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/Support/Allocator.h"
23#include "llvm/Support/Casting.h"
24#include "llvm/Support/Compiler.h"
25#include "llvm/Support/ErrorHandling.h"
26#include "llvm/Support/SMLoc.h"
27#include "llvm/Support/raw_ostream.h"
28#include "llvm/TableGen/Error.h"
29#include "llvm/TableGen/Record.h"
30#include <cassert>
31#include <cstdint>
32#include <memory>
33#include <string>
34#include <utility>
35#include <vector>
36
37using namespace llvm;
38
39static BumpPtrAllocator Allocator;
40
41//===----------------------------------------------------------------------===//
42// Type implementations
43//===----------------------------------------------------------------------===//
44
45BitRecTy BitRecTy::Shared;
46CodeRecTy CodeRecTy::Shared;
47IntRecTy IntRecTy::Shared;
48StringRecTy StringRecTy::Shared;
49DagRecTy DagRecTy::Shared;
50
51#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
52LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void RecTy::dump() const { print(errs()); }
53#endif
54
55ListRecTy *RecTy::getListTy() {
56 if (!ListTy)
57 ListTy = new(Allocator) ListRecTy(this);
58 return ListTy;
59}
60
61bool RecTy::typeIsConvertibleTo(const RecTy *RHS) const {
62 assert(RHS && "NULL pointer")(static_cast <bool> (RHS && "NULL pointer") ? void
(0) : __assert_fail ("RHS && \"NULL pointer\"", "/build/llvm-toolchain-snapshot-7~svn326551/lib/TableGen/Record.cpp"
, 62, __extension__ __PRETTY_FUNCTION__))
;
63 return Kind == RHS->getRecTyKind();
64}
65
66bool BitRecTy::typeIsConvertibleTo(const RecTy *RHS) const{
67 if (RecTy::typeIsConvertibleTo(RHS) || RHS->getRecTyKind() == IntRecTyKind)
68 return true;
69 if (const BitsRecTy *BitsTy = dyn_cast<BitsRecTy>(RHS))
70 return BitsTy->getNumBits() == 1;
71 return false;
72}
73
74BitsRecTy *BitsRecTy::get(unsigned Sz) {
75 static std::vector<BitsRecTy*> Shared;
76 if (Sz >= Shared.size())
77 Shared.resize(Sz + 1);
78 BitsRecTy *&Ty = Shared[Sz];
79 if (!Ty)
80 Ty = new(Allocator) BitsRecTy(Sz);
81 return Ty;
82}
83
84std::string BitsRecTy::getAsString() const {
85 return "bits<" + utostr(Size) + ">";
86}
87
88bool BitsRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
89 if (RecTy::typeIsConvertibleTo(RHS)) //argument and the sender are same type
90 return cast<BitsRecTy>(RHS)->Size == Size;
91 RecTyKind kind = RHS->getRecTyKind();
92 return (kind == BitRecTyKind && Size == 1) || (kind == IntRecTyKind);
93}
94
95bool IntRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
96 RecTyKind kind = RHS->getRecTyKind();
97 return kind==BitRecTyKind || kind==BitsRecTyKind || kind==IntRecTyKind;
98}
99
100bool CodeRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
101 RecTyKind Kind = RHS->getRecTyKind();
102 return Kind == CodeRecTyKind || Kind == StringRecTyKind;
103}
104
105std::string StringRecTy::getAsString() const {
106 return "string";
107}
108
109bool StringRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
110 RecTyKind Kind = RHS->getRecTyKind();
111 return Kind == StringRecTyKind || Kind == CodeRecTyKind;
112}
113
114std::string ListRecTy::getAsString() const {
115 return "list<" + Ty->getAsString() + ">";
116}
117
118bool ListRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
119 if (const auto *ListTy = dyn_cast<ListRecTy>(RHS))
120 return Ty->typeIsConvertibleTo(ListTy->getElementType());
121 return false;
122}
123
124std::string DagRecTy::getAsString() const {
125 return "dag";
126}
127
128RecordRecTy *RecordRecTy::get(Record *R) {
129 return dyn_cast<RecordRecTy>(R->getDefInit()->getType());
130}
131
132std::string RecordRecTy::getAsString() const {
133 return Rec->getName();
134}
135
136bool RecordRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
137 const RecordRecTy *RTy = dyn_cast<RecordRecTy>(RHS);
138 if (!RTy)
139 return false;
140
141 if (RTy->getRecord() == Rec || Rec->isSubClassOf(RTy->getRecord()))
142 return true;
143
144 for (const auto &SCPair : RTy->getRecord()->getSuperClasses())
145 if (Rec->isSubClassOf(SCPair.first))
146 return true;
147
148 return false;
149}
150
151RecTy *llvm::resolveTypes(RecTy *T1, RecTy *T2) {
152 if (T1->typeIsConvertibleTo(T2))
153 return T2;
154 if (T2->typeIsConvertibleTo(T1))
155 return T1;
156
157 // If one is a Record type, check superclasses
158 if (RecordRecTy *RecTy1 = dyn_cast<RecordRecTy>(T1)) {
159 // See if T2 inherits from a type T1 also inherits from
160 for (const auto &SuperPair1 : RecTy1->getRecord()->getSuperClasses()) {
161 RecordRecTy *SuperRecTy1 = RecordRecTy::get(SuperPair1.first);
162 RecTy *NewType1 = resolveTypes(SuperRecTy1, T2);
163 if (NewType1)
164 return NewType1;
165 }
166 }
167 if (RecordRecTy *RecTy2 = dyn_cast<RecordRecTy>(T2)) {
168 // See if T1 inherits from a type T2 also inherits from
169 for (const auto &SuperPair2 : RecTy2->getRecord()->getSuperClasses()) {
170 RecordRecTy *SuperRecTy2 = RecordRecTy::get(SuperPair2.first);
171 RecTy *NewType2 = resolveTypes(T1, SuperRecTy2);
172 if (NewType2)
173 return NewType2;
174 }
175 }
176 return nullptr;
177}
178
179//===----------------------------------------------------------------------===//
180// Initializer implementations
181//===----------------------------------------------------------------------===//
182
183void Init::anchor() {}
184
185#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
186LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void Init::dump() const { return print(errs()); }
187#endif
188
189UnsetInit *UnsetInit::get() {
190 static UnsetInit TheInit;
191 return &TheInit;
192}
193
194Init *UnsetInit::convertInitializerTo(RecTy *Ty) const {
195 if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
2
Assuming 'BRT' is non-null
3
Taking true branch
196 SmallVector<Init *, 16> NewBits(BRT->getNumBits());
197
198 for (unsigned i = 0; i != BRT->getNumBits(); ++i)
4
Assuming the condition is false
5
Loop condition is false. Execution continues on line 201
199 NewBits[i] = UnsetInit::get();
200
201 return BitsInit::get(NewBits);
6
Calling 'BitsInit::get'
202 }
203
204 // All other types can just be returned.
205 return const_cast<UnsetInit *>(this);
206}
207
208BitInit *BitInit::get(bool V) {
209 static BitInit True(true);
210 static BitInit False(false);
211
212 return V ? &True : &False;
213}
214
215Init *BitInit::convertInitializerTo(RecTy *Ty) const {
216 if (isa<BitRecTy>(Ty))
217 return const_cast<BitInit *>(this);
218
219 if (isa<IntRecTy>(Ty))
220 return IntInit::get(getValue());
221
222 if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
223 // Can only convert single bit.
224 if (BRT->getNumBits() == 1)
225 return BitsInit::get(const_cast<BitInit *>(this));
226 }
227
228 return nullptr;
229}
230
231static void
232ProfileBitsInit(FoldingSetNodeID &ID, ArrayRef<Init *> Range) {
233 ID.AddInteger(Range.size());
234
235 for (Init *I : Range)
236 ID.AddPointer(I);
237}
238
239BitsInit *BitsInit::get(ArrayRef<Init *> Range) {
240 static FoldingSet<BitsInit> ThePool;
241
242 FoldingSetNodeID ID;
243 ProfileBitsInit(ID, Range);
244
245 void *IP = nullptr;
246 if (BitsInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
7
Assuming 'I' is null
8
Taking false branch
247 return I;
248
249 void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *>(Range.size()),
250 alignof(BitsInit));
251 BitsInit *I = new(Mem) BitsInit(Range.size());
9
'I' initialized to a null pointer value
252 std::uninitialized_copy(Range.begin(), Range.end(),
253 I->getTrailingObjects<Init *>());
10
Called C++ object pointer is null
254 ThePool.InsertNode(I, IP);
255 return I;
256}
257
258void BitsInit::Profile(FoldingSetNodeID &ID) const {
259 ProfileBitsInit(ID, makeArrayRef(getTrailingObjects<Init *>(), NumBits));
260}
261
262Init *BitsInit::convertInitializerTo(RecTy *Ty) const {
263 if (isa<BitRecTy>(Ty)) {
264 if (getNumBits() != 1) return nullptr; // Only accept if just one bit!
265 return getBit(0);
266 }
267
268 if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
269 // If the number of bits is right, return it. Otherwise we need to expand
270 // or truncate.
271 if (getNumBits() != BRT->getNumBits()) return nullptr;
272 return const_cast<BitsInit *>(this);
273 }
274
275 if (isa<IntRecTy>(Ty)) {
276 int64_t Result = 0;
277 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
278 if (auto *Bit = dyn_cast<BitInit>(getBit(i)))
279 Result |= static_cast<int64_t>(Bit->getValue()) << i;
280 else
281 return nullptr;
282 return IntInit::get(Result);
283 }
284
285 return nullptr;
286}
287
288Init *
289BitsInit::convertInitializerBitRange(ArrayRef<unsigned> Bits) const {
290 SmallVector<Init *, 16> NewBits(Bits.size());
291
292 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
293 if (Bits[i] >= getNumBits())
294 return nullptr;
295 NewBits[i] = getBit(Bits[i]);
296 }
297 return BitsInit::get(NewBits);
298}
299
300std::string BitsInit::getAsString() const {
301 std::string Result = "{ ";
302 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
303 if (i) Result += ", ";
304 if (Init *Bit = getBit(e-i-1))
305 Result += Bit->getAsString();
306 else
307 Result += "*";
308 }
309 return Result + " }";
310}
311
312// Fix bit initializer to preserve the behavior that bit reference from a unset
313// bits initializer will resolve into VarBitInit to keep the field name and bit
314// number used in targets with fixed insn length.
315static Init *fixBitInit(const RecordVal *RV, Init *Before, Init *After) {
316 if (RV || !isa<UnsetInit>(After))
317 return After;
318 return Before;
319}
320
321// resolveReferences - If there are any field references that refer to fields
322// that have been filled in, we can propagate the values now.
323Init *BitsInit::resolveReferences(Record &R, const RecordVal *RV) const {
324 bool Changed = false;
325 SmallVector<Init *, 16> NewBits(getNumBits());
326
327 Init *CachedInit = nullptr;
328 Init *CachedBitVar = nullptr;
329 bool CachedBitVarChanged = false;
330
331 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
332 Init *CurBit = getBit(i);
333 Init *CurBitVar = CurBit->getBitVar();
334
335 NewBits[i] = CurBit;
336
337 if (CurBitVar == CachedBitVar) {
338 if (CachedBitVarChanged) {
339 Init *Bit = CachedInit->getBit(CurBit->getBitNum());
340 NewBits[i] = fixBitInit(RV, CurBit, Bit);
341 }
342 continue;
343 }
344 CachedBitVar = CurBitVar;
345 CachedBitVarChanged = false;
346
347 Init *B;
348 do {
349 B = CurBitVar;
350 CurBitVar = CurBitVar->resolveReferences(R, RV);
351 CachedBitVarChanged |= B != CurBitVar;
352 Changed |= B != CurBitVar;
353 } while (B != CurBitVar);
354 CachedInit = CurBitVar;
355
356 if (CachedBitVarChanged) {
357 Init *Bit = CurBitVar->getBit(CurBit->getBitNum());
358 NewBits[i] = fixBitInit(RV, CurBit, Bit);
359 }
360 }
361
362 if (Changed)
363 return BitsInit::get(NewBits);
364
365 return const_cast<BitsInit *>(this);
366}
367
368IntInit *IntInit::get(int64_t V) {
369 static DenseMap<int64_t, IntInit*> ThePool;
370
371 IntInit *&I = ThePool[V];
372 if (!I) I = new(Allocator) IntInit(V);
373 return I;
374}
375
376std::string IntInit::getAsString() const {
377 return itostr(Value);
378}
379
380static bool canFitInBitfield(int64_t Value, unsigned NumBits) {
381 // For example, with NumBits == 4, we permit Values from [-7 .. 15].
382 return (NumBits >= sizeof(Value) * 8) ||
383 (Value >> NumBits == 0) || (Value >> (NumBits-1) == -1);
384}
385
386Init *IntInit::convertInitializerTo(RecTy *Ty) const {
387 if (isa<IntRecTy>(Ty))
388 return const_cast<IntInit *>(this);
389
390 if (isa<BitRecTy>(Ty)) {
391 int64_t Val = getValue();
392 if (Val != 0 && Val != 1) return nullptr; // Only accept 0 or 1 for a bit!
393 return BitInit::get(Val != 0);
394 }
395
396 if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
397 int64_t Value = getValue();
398 // Make sure this bitfield is large enough to hold the integer value.
399 if (!canFitInBitfield(Value, BRT->getNumBits()))
400 return nullptr;
401
402 SmallVector<Init *, 16> NewBits(BRT->getNumBits());
403 for (unsigned i = 0; i != BRT->getNumBits(); ++i)
404 NewBits[i] = BitInit::get(Value & (1LL << i));
405
406 return BitsInit::get(NewBits);
407 }
408
409 return nullptr;
410}
411
412Init *
413IntInit::convertInitializerBitRange(ArrayRef<unsigned> Bits) const {
414 SmallVector<Init *, 16> NewBits(Bits.size());
415
416 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
417 if (Bits[i] >= 64)
418 return nullptr;
419
420 NewBits[i] = BitInit::get(Value & (INT64_C(1)1L << Bits[i]));
421 }
422 return BitsInit::get(NewBits);
423}
424
425CodeInit *CodeInit::get(StringRef V) {
426 static StringMap<CodeInit*, BumpPtrAllocator &> ThePool(Allocator);
427
428 auto &Entry = *ThePool.insert(std::make_pair(V, nullptr)).first;
429 if (!Entry.second)
430 Entry.second = new(Allocator) CodeInit(Entry.getKey());
431 return Entry.second;
432}
433
434StringInit *StringInit::get(StringRef V) {
435 static StringMap<StringInit*, BumpPtrAllocator &> ThePool(Allocator);
436
437 auto &Entry = *ThePool.insert(std::make_pair(V, nullptr)).first;
438 if (!Entry.second)
439 Entry.second = new(Allocator) StringInit(Entry.getKey());
440 return Entry.second;
441}
442
443Init *StringInit::convertInitializerTo(RecTy *Ty) const {
444 if (isa<StringRecTy>(Ty))
445 return const_cast<StringInit *>(this);
446 if (isa<CodeRecTy>(Ty))
447 return CodeInit::get(getValue());
448
449 return nullptr;
450}
451
452Init *CodeInit::convertInitializerTo(RecTy *Ty) const {
453 if (isa<CodeRecTy>(Ty))
454 return const_cast<CodeInit *>(this);
455 if (isa<StringRecTy>(Ty))
456 return StringInit::get(getValue());
457
458 return nullptr;
459}
460
461static void ProfileListInit(FoldingSetNodeID &ID,
462 ArrayRef<Init *> Range,
463 RecTy *EltTy) {
464 ID.AddInteger(Range.size());
465 ID.AddPointer(EltTy);
466
467 for (Init *I : Range)
468 ID.AddPointer(I);
469}
470
471ListInit *ListInit::get(ArrayRef<Init *> Range, RecTy *EltTy) {
472 static FoldingSet<ListInit> ThePool;
473
474 FoldingSetNodeID ID;
475 ProfileListInit(ID, Range, EltTy);
476
477 void *IP = nullptr;
478 if (ListInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
479 return I;
480
481 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)"
, "/build/llvm-toolchain-snapshot-7~svn326551/lib/TableGen/Record.cpp"
, 482, __extension__ __PRETTY_FUNCTION__))
482 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)"
, "/build/llvm-toolchain-snapshot-7~svn326551/lib/TableGen/Record.cpp"
, 482, __extension__ __PRETTY_FUNCTION__))
;
483
484 void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *>(Range.size()),
485 alignof(ListInit));
486 ListInit *I = new(Mem) ListInit(Range.size(), EltTy);
487 std::uninitialized_copy(Range.begin(), Range.end(),
488 I->getTrailingObjects<Init *>());
489 ThePool.InsertNode(I, IP);
490 return I;
491}
492
493void ListInit::Profile(FoldingSetNodeID &ID) const {
494 RecTy *EltTy = cast<ListRecTy>(getType())->getElementType();
495
496 ProfileListInit(ID, getValues(), EltTy);
497}
498
499Init *ListInit::convertInitializerTo(RecTy *Ty) const {
500 if (getType() == Ty)
501 return const_cast<ListInit*>(this);
502
503 if (auto *LRT = dyn_cast<ListRecTy>(Ty)) {
504 SmallVector<Init*, 8> Elements;
505 Elements.reserve(getValues().size());
506
507 // Verify that all of the elements of the list are subclasses of the
508 // appropriate class!
509 bool Changed = false;
510 RecTy *ElementType = LRT->getElementType();
511 for (Init *I : getValues())
512 if (Init *CI = I->convertInitializerTo(ElementType)) {
513 Elements.push_back(CI);
514 if (CI != I)
515 Changed = true;
516 } else
517 return nullptr;
518
519 if (!Changed)
520 return const_cast<ListInit*>(this);
521 return ListInit::get(Elements, ElementType);
522 }
523
524 return nullptr;
525}
526
527Init *ListInit::convertInitListSlice(ArrayRef<unsigned> Elements) const {
528 SmallVector<Init*, 8> Vals;
529 Vals.reserve(Elements.size());
530 for (unsigned Element : Elements) {
531 if (Element >= size())
532 return nullptr;
533 Vals.push_back(getElement(Element));
534 }
535 return ListInit::get(Vals, getElementType());
536}
537
538Record *ListInit::getElementAsRecord(unsigned i) const {
539 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!\""
, "/build/llvm-toolchain-snapshot-7~svn326551/lib/TableGen/Record.cpp"
, 539, __extension__ __PRETTY_FUNCTION__))
;
540 DefInit *DI = dyn_cast<DefInit>(getElement(i));
541 if (!DI)
542 PrintFatalError("Expected record in list!");
543 return DI->getDef();
544}
545
546Init *ListInit::resolveReferences(Record &R, const RecordVal *RV) const {
547 SmallVector<Init*, 8> Resolved;
548 Resolved.reserve(size());
549 bool Changed = false;
550
551 for (Init *CurElt : getValues()) {
552 Init *E;
553
554 do {
555 E = CurElt;
556 CurElt = CurElt->resolveReferences(R, RV);
557 Changed |= E != CurElt;
558 } while (E != CurElt);
559 Resolved.push_back(E);
560 }
561
562 if (Changed)
563 return ListInit::get(Resolved, getElementType());
564 return const_cast<ListInit *>(this);
565}
566
567std::string ListInit::getAsString() const {
568 std::string Result = "[";
569 const char *sep = "";
570 for (Init *Element : *this) {
571 Result += sep;
572 sep = ", ";
573 Result += Element->getAsString();
574 }
575 return Result + "]";
576}
577
578Init *OpInit::getBit(unsigned Bit) const {
579 if (getType() == BitRecTy::get())
580 return const_cast<OpInit*>(this);
581 return VarBitInit::get(const_cast<OpInit*>(this), Bit);
582}
583
584static void
585ProfileUnOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *Op, RecTy *Type) {
586 ID.AddInteger(Opcode);
587 ID.AddPointer(Op);
588 ID.AddPointer(Type);
589}
590
591UnOpInit *UnOpInit::get(UnaryOp Opc, Init *LHS, RecTy *Type) {
592 static FoldingSet<UnOpInit> ThePool;
593
594 FoldingSetNodeID ID;
595 ProfileUnOpInit(ID, Opc, LHS, Type);
596
597 void *IP = nullptr;
598 if (UnOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
599 return I;
600
601 UnOpInit *I = new(Allocator) UnOpInit(Opc, LHS, Type);
602 ThePool.InsertNode(I, IP);
603 return I;
604}
605
606void UnOpInit::Profile(FoldingSetNodeID &ID) const {
607 ProfileUnOpInit(ID, getOpcode(), getOperand(), getType());
608}
609
610Init *UnOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const {
611 switch (getOpcode()) {
612 case CAST:
613 if (isa<StringRecTy>(getType())) {
614 if (StringInit *LHSs = dyn_cast<StringInit>(LHS))
615 return LHSs;
616
617 if (DefInit *LHSd = dyn_cast<DefInit>(LHS))
618 return StringInit::get(LHSd->getAsString());
619
620 if (IntInit *LHSi = dyn_cast<IntInit>(LHS))
621 return StringInit::get(LHSi->getAsString());
622 } else {
623 if (StringInit *Name = dyn_cast<StringInit>(LHS)) {
624 // From TGParser::ParseIDValue
625 if (CurRec) {
626 if (const RecordVal *RV = CurRec->getValue(Name)) {
627 if (RV->getType() != getType())
628 PrintFatalError("type mismatch in cast");
629 return VarInit::get(Name, RV->getType());
630 }
631
632 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name,
633 ":");
634
635 if (CurRec->isTemplateArg(TemplateArgName)) {
636 const RecordVal *RV = CurRec->getValue(TemplateArgName);
637 assert(RV && "Template arg doesn't exist??")(static_cast <bool> (RV && "Template arg doesn't exist??"
) ? void (0) : __assert_fail ("RV && \"Template arg doesn't exist??\""
, "/build/llvm-toolchain-snapshot-7~svn326551/lib/TableGen/Record.cpp"
, 637, __extension__ __PRETTY_FUNCTION__))
;
638
639 if (RV->getType() != getType())
640 PrintFatalError("type mismatch in cast");
641
642 return VarInit::get(TemplateArgName, RV->getType());
643 }
644 }
645
646 if (CurMultiClass) {
647 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
648 "::");
649
650 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
651 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
652 assert(RV && "Template arg doesn't exist??")(static_cast <bool> (RV && "Template arg doesn't exist??"
) ? void (0) : __assert_fail ("RV && \"Template arg doesn't exist??\""
, "/build/llvm-toolchain-snapshot-7~svn326551/lib/TableGen/Record.cpp"
, 652, __extension__ __PRETTY_FUNCTION__))
;
653
654 if (RV->getType() != getType())
655 PrintFatalError("type mismatch in cast");
656
657 return VarInit::get(MCName, RV->getType());
658 }
659 }
660 assert(CurRec && "NULL pointer")(static_cast <bool> (CurRec && "NULL pointer") ?
void (0) : __assert_fail ("CurRec && \"NULL pointer\""
, "/build/llvm-toolchain-snapshot-7~svn326551/lib/TableGen/Record.cpp"
, 660, __extension__ __PRETTY_FUNCTION__))
;
661 if (Record *D = (CurRec->getRecords()).getDef(Name->getValue()))
662 return DefInit::get(D);
663
664 PrintFatalError(CurRec->getLoc(),
665 "Undefined reference:'" + Name->getValue() + "'\n");
666 }
667
668 if (isa<IntRecTy>(getType())) {
669 if (BitsInit *BI = dyn_cast<BitsInit>(LHS)) {
670 if (Init *NewInit = BI->convertInitializerTo(IntRecTy::get()))
671 return NewInit;
672 break;
673 }
674 }
675 }
676 break;
677
678 case HEAD:
679 if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) {
680 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\""
, "/build/llvm-toolchain-snapshot-7~svn326551/lib/TableGen/Record.cpp"
, 680, __extension__ __PRETTY_FUNCTION__))
;
681 return LHSl->getElement(0);
682 }
683 break;
684
685 case TAIL:
686 if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) {
687 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\""
, "/build/llvm-toolchain-snapshot-7~svn326551/lib/TableGen/Record.cpp"
, 687, __extension__ __PRETTY_FUNCTION__))
;
688 // Note the +1. We can't just pass the result of getValues()
689 // directly.
690 return ListInit::get(LHSl->getValues().slice(1), LHSl->getElementType());
691 }
692 break;
693
694 case SIZE:
695 if (ListInit *LHSl = dyn_cast<ListInit>(LHS))
696 return IntInit::get(LHSl->size());
697 break;
698
699 case EMPTY:
700 if (ListInit *LHSl = dyn_cast<ListInit>(LHS))
701 return IntInit::get(LHSl->empty());
702 if (StringInit *LHSs = dyn_cast<StringInit>(LHS))
703 return IntInit::get(LHSs->getValue().empty());
704 break;
705 }
706 return const_cast<UnOpInit *>(this);
707}
708
709Init *UnOpInit::resolveReferences(Record &R, const RecordVal *RV) const {
710 Init *lhs = LHS->resolveReferences(R, RV);
711
712 if (LHS != lhs)
713 return (UnOpInit::get(getOpcode(), lhs, getType()))->Fold(&R, nullptr);
714 return Fold(&R, nullptr);
715}
716
717std::string UnOpInit::getAsString() const {
718 std::string Result;
719 switch (getOpcode()) {
720 case CAST: Result = "!cast<" + getType()->getAsString() + ">"; break;
721 case HEAD: Result = "!head"; break;
722 case TAIL: Result = "!tail"; break;
723 case SIZE: Result = "!size"; break;
724 case EMPTY: Result = "!empty"; break;
725 }
726 return Result + "(" + LHS->getAsString() + ")";
727}
728
729static void
730ProfileBinOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *LHS, Init *RHS,
731 RecTy *Type) {
732 ID.AddInteger(Opcode);
733 ID.AddPointer(LHS);
734 ID.AddPointer(RHS);
735 ID.AddPointer(Type);
736}
737
738BinOpInit *BinOpInit::get(BinaryOp Opc, Init *LHS,
739 Init *RHS, RecTy *Type) {
740 static FoldingSet<BinOpInit> ThePool;
741
742 FoldingSetNodeID ID;
743 ProfileBinOpInit(ID, Opc, LHS, RHS, Type);
744
745 void *IP = nullptr;
746 if (BinOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
747 return I;
748
749 BinOpInit *I = new(Allocator) BinOpInit(Opc, LHS, RHS, Type);
750 ThePool.InsertNode(I, IP);
751 return I;
752}
753
754void BinOpInit::Profile(FoldingSetNodeID &ID) const {
755 ProfileBinOpInit(ID, getOpcode(), getLHS(), getRHS(), getType());
756}
757
758static StringInit *ConcatStringInits(const StringInit *I0,
759 const StringInit *I1) {
760 SmallString<80> Concat(I0->getValue());
761 Concat.append(I1->getValue());
762 return StringInit::get(Concat);
763}
764
765Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const {
766 switch (getOpcode()) {
767 case CONCAT: {
768 DagInit *LHSs = dyn_cast<DagInit>(LHS);
769 DagInit *RHSs = dyn_cast<DagInit>(RHS);
770 if (LHSs && RHSs) {
771 DefInit *LOp = dyn_cast<DefInit>(LHSs->getOperator());
772 DefInit *ROp = dyn_cast<DefInit>(RHSs->getOperator());
773 if (!LOp || !ROp || LOp->getDef() != ROp->getDef())
774 PrintFatalError("Concated Dag operators do not match!");
775 SmallVector<Init*, 8> Args;
776 SmallVector<StringInit*, 8> ArgNames;
777 for (unsigned i = 0, e = LHSs->getNumArgs(); i != e; ++i) {
778 Args.push_back(LHSs->getArg(i));
779 ArgNames.push_back(LHSs->getArgName(i));
780 }
781 for (unsigned i = 0, e = RHSs->getNumArgs(); i != e; ++i) {
782 Args.push_back(RHSs->getArg(i));
783 ArgNames.push_back(RHSs->getArgName(i));
784 }
785 return DagInit::get(LHSs->getOperator(), nullptr, Args, ArgNames);
786 }
787 break;
788 }
789 case LISTCONCAT: {
790 ListInit *LHSs = dyn_cast<ListInit>(LHS);
791 ListInit *RHSs = dyn_cast<ListInit>(RHS);
792 if (LHSs && RHSs) {
793 SmallVector<Init *, 8> Args;
794 Args.insert(Args.end(), LHSs->begin(), LHSs->end());
795 Args.insert(Args.end(), RHSs->begin(), RHSs->end());
796 return ListInit::get(Args, LHSs->getElementType());
797 }
798 break;
799 }
800 case STRCONCAT: {
801 StringInit *LHSs = dyn_cast<StringInit>(LHS);
802 StringInit *RHSs = dyn_cast<StringInit>(RHS);
803 if (LHSs && RHSs)
804 return ConcatStringInits(LHSs, RHSs);
805 break;
806 }
807 case EQ: {
808 // try to fold eq comparison for 'bit' and 'int', otherwise fallback
809 // to string objects.
810 IntInit *L =
811 dyn_cast_or_null<IntInit>(LHS->convertInitializerTo(IntRecTy::get()));
812 IntInit *R =
813 dyn_cast_or_null<IntInit>(RHS->convertInitializerTo(IntRecTy::get()));
814
815 if (L && R)
816 return IntInit::get(L->getValue() == R->getValue());
817
818 StringInit *LHSs = dyn_cast<StringInit>(LHS);
819 StringInit *RHSs = dyn_cast<StringInit>(RHS);
820
821 // Make sure we've resolved
822 if (LHSs && RHSs)
823 return IntInit::get(LHSs->getValue() == RHSs->getValue());
824
825 break;
826 }
827 case ADD:
828 case AND:
829 case OR:
830 case SHL:
831 case SRA:
832 case SRL: {
833 IntInit *LHSi =
834 dyn_cast_or_null<IntInit>(LHS->convertInitializerTo(IntRecTy::get()));
835 IntInit *RHSi =
836 dyn_cast_or_null<IntInit>(RHS->convertInitializerTo(IntRecTy::get()));
837 if (LHSi && RHSi) {
838 int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
839 int64_t Result;
840 switch (getOpcode()) {
841 default: llvm_unreachable("Bad opcode!")::llvm::llvm_unreachable_internal("Bad opcode!", "/build/llvm-toolchain-snapshot-7~svn326551/lib/TableGen/Record.cpp"
, 841)
;
842 case ADD: Result = LHSv + RHSv; break;
843 case AND: Result = LHSv & RHSv; break;
844 case OR: Result = LHSv | RHSv; break;
845 case SHL: Result = LHSv << RHSv; break;
846 case SRA: Result = LHSv >> RHSv; break;
847 case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break;
848 }
849 return IntInit::get(Result);
850 }
851 break;
852 }
853 }
854 return const_cast<BinOpInit *>(this);
855}
856
857Init *BinOpInit::resolveReferences(Record &R, const RecordVal *RV) const {
858 Init *lhs = LHS->resolveReferences(R, RV);
859 Init *rhs = RHS->resolveReferences(R, RV);
860
861 if (LHS != lhs || RHS != rhs)
862 return (BinOpInit::get(getOpcode(), lhs, rhs, getType()))->Fold(&R,nullptr);
863 return Fold(&R, nullptr);
864}
865
866std::string BinOpInit::getAsString() const {
867 std::string Result;
868 switch (getOpcode()) {
869 case CONCAT: Result = "!con"; break;
870 case ADD: Result = "!add"; break;
871 case AND: Result = "!and"; break;
872 case OR: Result = "!or"; break;
873 case SHL: Result = "!shl"; break;
874 case SRA: Result = "!sra"; break;
875 case SRL: Result = "!srl"; break;
876 case EQ: Result = "!eq"; break;
877 case LISTCONCAT: Result = "!listconcat"; break;
878 case STRCONCAT: Result = "!strconcat"; break;
879 }
880 return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")";
881}
882
883static void
884ProfileTernOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *LHS, Init *MHS,
885 Init *RHS, RecTy *Type) {
886 ID.AddInteger(Opcode);
887 ID.AddPointer(LHS);
888 ID.AddPointer(MHS);
889 ID.AddPointer(RHS);
890 ID.AddPointer(Type);
891}
892
893TernOpInit *TernOpInit::get(TernaryOp Opc, Init *LHS, Init *MHS, Init *RHS,
894 RecTy *Type) {
895 static FoldingSet<TernOpInit> ThePool;
896
897 FoldingSetNodeID ID;
898 ProfileTernOpInit(ID, Opc, LHS, MHS, RHS, Type);
899
900 void *IP = nullptr;
901 if (TernOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
902 return I;
903
904 TernOpInit *I = new(Allocator) TernOpInit(Opc, LHS, MHS, RHS, Type);
905 ThePool.InsertNode(I, IP);
906 return I;
907}
908
909void TernOpInit::Profile(FoldingSetNodeID &ID) const {
910 ProfileTernOpInit(ID, getOpcode(), getLHS(), getMHS(), getRHS(), getType());
911}
912
913// Evaluates operation RHSo after replacing all operands matching LHS with Arg.
914static Init *EvaluateOperation(OpInit *RHSo, Init *LHS, Init *Arg,
915 Record *CurRec, MultiClass *CurMultiClass) {
916
917 SmallVector<Init *, 8> NewOperands;
918 NewOperands.reserve(RHSo->getNumOperands());
919 for (unsigned i = 0, e = RHSo->getNumOperands(); i < e; ++i) {
920 if (auto *RHSoo = dyn_cast<OpInit>(RHSo->getOperand(i))) {
921 if (Init *Result =
922 EvaluateOperation(RHSoo, LHS, Arg, CurRec, CurMultiClass))
923 NewOperands.push_back(Result);
924 else
925 NewOperands.push_back(RHSoo);
926 } else if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
927 NewOperands.push_back(Arg);
928 } else {
929 NewOperands.push_back(RHSo->getOperand(i));
930 }
931 }
932
933 // Now run the operator and use its result as the new leaf
934 const OpInit *NewOp = RHSo->clone(NewOperands);
935 Init *NewVal = NewOp->Fold(CurRec, CurMultiClass);
936 return (NewVal != NewOp) ? NewVal : nullptr;
937}
938
939// Applies RHS to all elements of MHS, using LHS as a temp variable.
940static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
941 Record *CurRec, MultiClass *CurMultiClass) {
942 OpInit *RHSo = dyn_cast<OpInit>(RHS);
943
944 if (!RHSo)
945 PrintFatalError(CurRec->getLoc(), "!foreach requires an operator\n");
946
947 TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
948
949 if (!LHSt)
950 PrintFatalError(CurRec->getLoc(), "!foreach requires typed variable\n");
951
952 DagInit *MHSd = dyn_cast<DagInit>(MHS);
953 if (MHSd) {
954 Init *Val = MHSd->getOperator();
955 if (Init *Result = EvaluateOperation(RHSo, LHS, Val, CurRec, CurMultiClass))
956 Val = Result;
957
958 SmallVector<std::pair<Init *, StringInit *>, 8> args;
959 for (unsigned int i = 0; i < MHSd->getNumArgs(); ++i) {
960 Init *Arg = MHSd->getArg(i);
961 StringInit *ArgName = MHSd->getArgName(i);
962 // If this is a dag, recurse
963 if (isa<DagInit>(Arg)) {
964 if (Init *Result =
965 ForeachHelper(LHS, Arg, RHSo, Type, CurRec, CurMultiClass))
966 Arg = Result;
967 } else if (Init *Result =
968 EvaluateOperation(RHSo, LHS, Arg, CurRec, CurMultiClass)) {
969 Arg = Result;
970 }
971
972 // TODO: Process arg names
973 args.push_back(std::make_pair(Arg, ArgName));
974 }
975
976 return DagInit::get(Val, nullptr, args);
977 }
978
979 ListInit *MHSl = dyn_cast<ListInit>(MHS);
980 ListRecTy *ListType = dyn_cast<ListRecTy>(Type);
981 if (MHSl && ListType) {
982 SmallVector<Init *, 8> NewList(MHSl->begin(), MHSl->end());
983 for (Init *&Arg : NewList) {
984 if (Init *Result =
985 EvaluateOperation(RHSo, LHS, Arg, CurRec, CurMultiClass))
986 Arg = Result;
987 }
988 return ListInit::get(NewList, ListType->getElementType());
989 }
990 return nullptr;
991}
992
993Init *TernOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const {
994 switch (getOpcode()) {
995 case SUBST: {
996 DefInit *LHSd = dyn_cast<DefInit>(LHS);
997 VarInit *LHSv = dyn_cast<VarInit>(LHS);
998 StringInit *LHSs = dyn_cast<StringInit>(LHS);
999
1000 DefInit *MHSd = dyn_cast<DefInit>(MHS);
1001 VarInit *MHSv = dyn_cast<VarInit>(MHS);
1002 StringInit *MHSs = dyn_cast<StringInit>(MHS);
1003
1004 DefInit *RHSd = dyn_cast<DefInit>(RHS);
1005 VarInit *RHSv = dyn_cast<VarInit>(RHS);
1006 StringInit *RHSs = dyn_cast<StringInit>(RHS);
1007
1008 if (LHSd && MHSd && RHSd) {
1009 Record *Val = RHSd->getDef();
1010 if (LHSd->getAsString() == RHSd->getAsString())
1011 Val = MHSd->getDef();
1012 return DefInit::get(Val);
1013 }
1014 if (LHSv && MHSv && RHSv) {
1015 std::string Val = RHSv->getName();
1016 if (LHSv->getAsString() == RHSv->getAsString())
1017 Val = MHSv->getName();
1018 return VarInit::get(Val, getType());
1019 }
1020 if (LHSs && MHSs && RHSs) {
1021 std::string Val = RHSs->getValue();
1022
1023 std::string::size_type found;
1024 std::string::size_type idx = 0;
1025 while (true) {
1026 found = Val.find(LHSs->getValue(), idx);
1027 if (found == std::string::npos)
1028 break;
1029 Val.replace(found, LHSs->getValue().size(), MHSs->getValue());
1030 idx = found + MHSs->getValue().size();
1031 }
1032
1033 return StringInit::get(Val);
1034 }
1035 break;
1036 }
1037
1038 case FOREACH: {
1039 if (Init *Result =
1040 ForeachHelper(LHS, MHS, RHS, getType(), CurRec, CurMultiClass))
1041 return Result;
1042 break;
1043 }
1044
1045 case IF: {
1046 IntInit *LHSi = dyn_cast<IntInit>(LHS);
1047 if (Init *I = LHS->convertInitializerTo(IntRecTy::get()))
1048 LHSi = dyn_cast<IntInit>(I);
1049 if (LHSi) {
1050 if (LHSi->getValue())
1051 return MHS;
1052 return RHS;
1053 }
1054 break;
1055 }
1056 }
1057
1058 return const_cast<TernOpInit *>(this);
1059}
1060
1061Init *TernOpInit::resolveReferences(Record &R,
1062 const RecordVal *RV) const {
1063 Init *lhs = LHS->resolveReferences(R, RV);
1064
1065 if (getOpcode() == IF && lhs != LHS) {
1066 IntInit *Value = dyn_cast<IntInit>(lhs);
1067 if (Init *I = lhs->convertInitializerTo(IntRecTy::get()))
1068 Value = dyn_cast<IntInit>(I);
1069 if (Value) {
1070 // Short-circuit
1071 if (Value->getValue()) {
1072 Init *mhs = MHS->resolveReferences(R, RV);
1073 return (TernOpInit::get(getOpcode(), lhs, mhs,
1074 RHS, getType()))->Fold(&R, nullptr);
1075 }
1076 Init *rhs = RHS->resolveReferences(R, RV);
1077 return (TernOpInit::get(getOpcode(), lhs, MHS,
1078 rhs, getType()))->Fold(&R, nullptr);
1079 }
1080 }
1081
1082 Init *mhs = MHS->resolveReferences(R, RV);
1083 Init *rhs = RHS->resolveReferences(R, RV);
1084
1085 if (LHS != lhs || MHS != mhs || RHS != rhs)
1086 return (TernOpInit::get(getOpcode(), lhs, mhs, rhs,
1087 getType()))->Fold(&R, nullptr);
1088 return Fold(&R, nullptr);
1089}
1090
1091std::string TernOpInit::getAsString() const {
1092 std::string Result;
1093 switch (getOpcode()) {
1094 case SUBST: Result = "!subst"; break;
1095 case FOREACH: Result = "!foreach"; break;
1096 case IF: Result = "!if"; break;
1097 }
1098 return Result + "(" + LHS->getAsString() + ", " + MHS->getAsString() + ", " +
1099 RHS->getAsString() + ")";
1100}
1101
1102RecTy *TypedInit::getFieldType(StringInit *FieldName) const {
1103 if (RecordRecTy *RecordType = dyn_cast<RecordRecTy>(getType()))
1104 if (RecordVal *Field = RecordType->getRecord()->getValue(FieldName))
1105 return Field->getType();
1106 return nullptr;
1107}
1108
1109Init *
1110TypedInit::convertInitializerTo(RecTy *Ty) const {
1111 if (isa<IntRecTy>(Ty)) {
1112 if (getType()->typeIsConvertibleTo(Ty))
1113 return const_cast<TypedInit *>(this);
1114 return nullptr;
1115 }
1116
1117 if (isa<StringRecTy>(Ty)) {
1118 if (isa<StringRecTy>(getType()))
1119 return const_cast<TypedInit *>(this);
1120 return nullptr;
1121 }
1122
1123 if (isa<CodeRecTy>(Ty)) {
1124 if (isa<CodeRecTy>(getType()))
1125 return const_cast<TypedInit *>(this);
1126 return nullptr;
1127 }
1128
1129 if (isa<BitRecTy>(Ty)) {
1130 // Accept variable if it is already of bit type!
1131 if (isa<BitRecTy>(getType()))
1132 return const_cast<TypedInit *>(this);
1133 if (auto *BitsTy = dyn_cast<BitsRecTy>(getType())) {
1134 // Accept only bits<1> expression.
1135 if (BitsTy->getNumBits() == 1)
1136 return const_cast<TypedInit *>(this);
1137 return nullptr;
1138 }
1139 // Ternary !if can be converted to bit, but only if both sides are
1140 // convertible to a bit.
1141 if (const auto *TOI = dyn_cast<TernOpInit>(this)) {
1142 if (TOI->getOpcode() == TernOpInit::TernaryOp::IF &&
1143 TOI->getMHS()->convertInitializerTo(BitRecTy::get()) &&
1144 TOI->getRHS()->convertInitializerTo(BitRecTy::get()))
1145 return const_cast<TypedInit *>(this);
1146 return nullptr;
1147 }
1148 return nullptr;
1149 }
1150
1151 if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
1152 if (BRT->getNumBits() == 1 && isa<BitRecTy>(getType()))
1153 return BitsInit::get(const_cast<TypedInit *>(this));
1154
1155 if (getType()->typeIsConvertibleTo(BRT)) {
1156 SmallVector<Init *, 16> NewBits(BRT->getNumBits());
1157
1158 for (unsigned i = 0; i != BRT->getNumBits(); ++i)
1159 NewBits[i] = VarBitInit::get(const_cast<TypedInit *>(this), i);
1160 return BitsInit::get(NewBits);
1161 }
1162
1163 return nullptr;
1164 }
1165
1166 if (auto *DLRT = dyn_cast<ListRecTy>(Ty)) {
1167 if (auto *SLRT = dyn_cast<ListRecTy>(getType()))
1168 if (SLRT->getElementType()->typeIsConvertibleTo(DLRT->getElementType()))
1169 return const_cast<TypedInit *>(this);
1170 return nullptr;
1171 }
1172
1173 if (auto *DRT = dyn_cast<DagRecTy>(Ty)) {
1174 if (getType()->typeIsConvertibleTo(DRT))
1175 return const_cast<TypedInit *>(this);
1176 return nullptr;
1177 }
1178
1179 if (auto *SRRT = dyn_cast<RecordRecTy>(Ty)) {
1180 // Ensure that this is compatible with Rec.
1181 if (RecordRecTy *DRRT = dyn_cast<RecordRecTy>(getType()))
1182 if (DRRT->getRecord()->isSubClassOf(SRRT->getRecord()) ||
1183 DRRT->getRecord() == SRRT->getRecord())
1184 return const_cast<TypedInit *>(this);
1185 return nullptr;
1186 }
1187
1188 return nullptr;
1189}
1190
1191Init *TypedInit::convertInitializerBitRange(ArrayRef<unsigned> Bits) const {
1192 BitsRecTy *T = dyn_cast<BitsRecTy>(getType());
1193 if (!T) return nullptr; // Cannot subscript a non-bits variable.
1194 unsigned NumBits = T->getNumBits();
1195
1196 SmallVector<Init *, 16> NewBits;
1197 NewBits.reserve(Bits.size());
1198 for (unsigned Bit : Bits) {
1199 if (Bit >= NumBits)
1200 return nullptr;
1201
1202 NewBits.push_back(VarBitInit::get(const_cast<TypedInit *>(this), Bit));
1203 }
1204 return BitsInit::get(NewBits);
1205}
1206
1207Init *TypedInit::convertInitListSlice(ArrayRef<unsigned> Elements) const {
1208 ListRecTy *T = dyn_cast<ListRecTy>(getType());
1209 if (!T) return nullptr; // Cannot subscript a non-list variable.
1210
1211 if (Elements.size() == 1)
1212 return VarListElementInit::get(const_cast<TypedInit *>(this), Elements[0]);
1213
1214 SmallVector<Init*, 8> ListInits;
1215 ListInits.reserve(Elements.size());
1216 for (unsigned Element : Elements)
1217 ListInits.push_back(VarListElementInit::get(const_cast<TypedInit *>(this),
1218 Element));
1219 return ListInit::get(ListInits, T->getElementType());
1220}
1221
1222
1223VarInit *VarInit::get(StringRef VN, RecTy *T) {
1224 Init *Value = StringInit::get(VN);
1225 return VarInit::get(Value, T);
1226}
1227
1228VarInit *VarInit::get(Init *VN, RecTy *T) {
1229 using Key = std::pair<RecTy *, Init *>;
1230 static DenseMap<Key, VarInit*> ThePool;
1231
1232 Key TheKey(std::make_pair(T, VN));
1233
1234 VarInit *&I = ThePool[TheKey];
1235 if (!I)
1236 I = new(Allocator) VarInit(VN, T);
1237 return I;
1238}
1239
1240StringRef VarInit::getName() const {
1241 StringInit *NameString = cast<StringInit>(getNameInit());
1242 return NameString->getValue();
1243}
1244
1245Init *VarInit::getBit(unsigned Bit) const {
1246 if (getType() == BitRecTy::get())
1247 return const_cast<VarInit*>(this);
1248 return VarBitInit::get(const_cast<VarInit*>(this), Bit);
1249}
1250
1251Init *VarInit::resolveReferences(Record &R, const RecordVal *RV) const {
1252 if (RecordVal *Val = R.getValue(VarName))
1253 if (RV == Val || (!RV && !isa<UnsetInit>(Val->getValue())))
1254 return Val->getValue();
1255 return const_cast<VarInit *>(this);
1256}
1257
1258VarBitInit *VarBitInit::get(TypedInit *T, unsigned B) {
1259 using Key = std::pair<TypedInit *, unsigned>;
1260 static DenseMap<Key, VarBitInit*> ThePool;
1261
1262 Key TheKey(std::make_pair(T, B));
1263
1264 VarBitInit *&I = ThePool[TheKey];
1265 if (!I)
1266 I = new(Allocator) VarBitInit(T, B);
1267 return I;
1268}
1269
1270Init *VarBitInit::convertInitializerTo(RecTy *Ty) const {
1271 if (isa<BitRecTy>(Ty))
1272 return const_cast<VarBitInit *>(this);
1273
1274 return nullptr;
1275}
1276
1277std::string VarBitInit::getAsString() const {
1278 return TI->getAsString() + "{" + utostr(Bit) + "}";
1279}
1280
1281Init *VarBitInit::resolveReferences(Record &R, const RecordVal *RV) const {
1282 Init *I = TI->resolveReferences(R, RV);
1283 if (TI != I)
1284 return I->getBit(getBitNum());
1285
1286 return const_cast<VarBitInit*>(this);
1287}
1288
1289VarListElementInit *VarListElementInit::get(TypedInit *T,
1290 unsigned E) {
1291 using Key = std::pair<TypedInit *, unsigned>;
1292 static DenseMap<Key, VarListElementInit*> ThePool;
1293
1294 Key TheKey(std::make_pair(T, E));
1295
1296 VarListElementInit *&I = ThePool[TheKey];
1297 if (!I) I = new(Allocator) VarListElementInit(T, E);
1298 return I;
1299}
1300
1301std::string VarListElementInit::getAsString() const {
1302 return TI->getAsString() + "[" + utostr(Element) + "]";
1303}
1304
1305Init *
1306VarListElementInit::resolveReferences(Record &R, const RecordVal *RV) const {
1307 Init *NewTI = TI->resolveReferences(R, RV);
1308 if (ListInit *List = dyn_cast<ListInit>(NewTI)) {
1309 // Leave out-of-bounds array references as-is. This can happen without
1310 // being an error, e.g. in the untaken "branch" of an !if expression.
1311 if (getElementNum() < List->size())
1312 return List->getElement(getElementNum());
1313 }
1314 if (NewTI != TI && isa<TypedInit>(NewTI))
1315 return VarListElementInit::get(cast<TypedInit>(NewTI), getElementNum());
1316 return const_cast<VarListElementInit *>(this);
1317}
1318
1319Init *VarListElementInit::getBit(unsigned Bit) const {
1320 if (getType() == BitRecTy::get())
1321 return const_cast<VarListElementInit*>(this);
1322 return VarBitInit::get(const_cast<VarListElementInit*>(this), Bit);
1323}
1324
1325DefInit *DefInit::get(Record *R) {
1326 return R->getDefInit();
1327}
1328
1329Init *DefInit::convertInitializerTo(RecTy *Ty) const {
1330 if (auto *RRT = dyn_cast<RecordRecTy>(Ty))
1331 if (getDef()->isSubClassOf(RRT->getRecord()))
1332 return const_cast<DefInit *>(this);
1333 return nullptr;
1334}
1335
1336RecTy *DefInit::getFieldType(StringInit *FieldName) const {
1337 if (const RecordVal *RV = Def->getValue(FieldName))
1338 return RV->getType();
1339 return nullptr;
1340}
1341
1342std::string DefInit::getAsString() const {
1343 return Def->getName();
1344}
1345
1346FieldInit *FieldInit::get(Init *R, StringInit *FN) {
1347 using Key = std::pair<Init *, StringInit *>;
1348 static DenseMap<Key, FieldInit*> ThePool;
1349
1350 Key TheKey(std::make_pair(R, FN));
1351
1352 FieldInit *&I = ThePool[TheKey];
1353 if (!I) I = new(Allocator) FieldInit(R, FN);
1354 return I;
1355}
1356
1357Init *FieldInit::getBit(unsigned Bit) const {
1358 if (getType() == BitRecTy::get())
1359 return const_cast<FieldInit*>(this);
1360 return VarBitInit::get(const_cast<FieldInit*>(this), Bit);
1361}
1362
1363Init *FieldInit::resolveReferences(Record &R, const RecordVal *RV) const {
1364 Init *NewRec = Rec->resolveReferences(R, RV);
1365
1366 if (DefInit *DI = dyn_cast<DefInit>(NewRec)) {
1367 Init *FieldVal = DI->getDef()->getValue(FieldName)->getValue();
1368 Init *BVR = FieldVal->resolveReferences(R, RV);
1369 if (BVR->isComplete())
1370 return BVR;
1371 }
1372
1373 if (NewRec != Rec)
1374 return FieldInit::get(NewRec, FieldName);
1375 return const_cast<FieldInit *>(this);
1376}
1377
1378static void ProfileDagInit(FoldingSetNodeID &ID, Init *V, StringInit *VN,
1379 ArrayRef<Init *> ArgRange,
1380 ArrayRef<StringInit *> NameRange) {
1381 ID.AddPointer(V);
1382 ID.AddPointer(VN);
1383
1384 ArrayRef<Init *>::iterator Arg = ArgRange.begin();
1385 ArrayRef<StringInit *>::iterator Name = NameRange.begin();
1386 while (Arg != ArgRange.end()) {
1387 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!\""
, "/build/llvm-toolchain-snapshot-7~svn326551/lib/TableGen/Record.cpp"
, 1387, __extension__ __PRETTY_FUNCTION__))
;
1388 ID.AddPointer(*Arg++);
1389 ID.AddPointer(*Name++);
1390 }
1391 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!\""
, "/build/llvm-toolchain-snapshot-7~svn326551/lib/TableGen/Record.cpp"
, 1391, __extension__ __PRETTY_FUNCTION__))
;
1392}
1393
1394DagInit *
1395DagInit::get(Init *V, StringInit *VN, ArrayRef<Init *> ArgRange,
1396 ArrayRef<StringInit *> NameRange) {
1397 static FoldingSet<DagInit> ThePool;
1398
1399 FoldingSetNodeID ID;
1400 ProfileDagInit(ID, V, VN, ArgRange, NameRange);
1401
1402 void *IP = nullptr;
1403 if (DagInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
1404 return I;
1405
1406 void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *, StringInit *>(ArgRange.size(), NameRange.size()), alignof(BitsInit));
1407 DagInit *I = new(Mem) DagInit(V, VN, ArgRange.size(), NameRange.size());
1408 std::uninitialized_copy(ArgRange.begin(), ArgRange.end(),
1409 I->getTrailingObjects<Init *>());
1410 std::uninitialized_copy(NameRange.begin(), NameRange.end(),
1411 I->getTrailingObjects<StringInit *>());
1412 ThePool.InsertNode(I, IP);
1413 return I;
1414}
1415
1416DagInit *
1417DagInit::get(Init *V, StringInit *VN,
1418 ArrayRef<std::pair<Init*, StringInit*>> args) {
1419 SmallVector<Init *, 8> Args;
1420 SmallVector<StringInit *, 8> Names;
1421
1422 for (const auto &Arg : args) {
1423 Args.push_back(Arg.first);
1424 Names.push_back(Arg.second);
1425 }
1426
1427 return DagInit::get(V, VN, Args, Names);
1428}
1429
1430void DagInit::Profile(FoldingSetNodeID &ID) const {
1431 ProfileDagInit(ID, Val, ValName, makeArrayRef(getTrailingObjects<Init *>(), NumArgs), makeArrayRef(getTrailingObjects<StringInit *>(), NumArgNames));
1432}
1433
1434Init *DagInit::convertInitializerTo(RecTy *Ty) const {
1435 if (isa<DagRecTy>(Ty))
1436 return const_cast<DagInit *>(this);
1437
1438 return nullptr;
1439}
1440
1441Init *DagInit::resolveReferences(Record &R, const RecordVal *RV) const {
1442 SmallVector<Init*, 8> NewArgs;
1443 NewArgs.reserve(arg_size());
1444 bool ArgsChanged = false;
1445 for (const Init *Arg : getArgs()) {
1446 Init *NewArg = Arg->resolveReferences(R, RV);
1447 NewArgs.push_back(NewArg);
1448 ArgsChanged |= NewArg != Arg;
1449 }
1450
1451 Init *Op = Val->resolveReferences(R, RV);
1452 if (Op != Val || ArgsChanged)
1453 return DagInit::get(Op, ValName, NewArgs, getArgNames());
1454
1455 return const_cast<DagInit *>(this);
1456}
1457
1458std::string DagInit::getAsString() const {
1459 std::string Result = "(" + Val->getAsString();
1460 if (ValName)
1461 Result += ":" + ValName->getAsUnquotedString();
1462 if (!arg_empty()) {
1463 Result += " " + getArg(0)->getAsString();
1464 if (getArgName(0)) Result += ":$" + getArgName(0)->getAsUnquotedString();
1465 for (unsigned i = 1, e = getNumArgs(); i != e; ++i) {
1466 Result += ", " + getArg(i)->getAsString();
1467 if (getArgName(i)) Result += ":$" + getArgName(i)->getAsUnquotedString();
1468 }
1469 }
1470 return Result + ")";
1471}
1472
1473//===----------------------------------------------------------------------===//
1474// Other implementations
1475//===----------------------------------------------------------------------===//
1476
1477RecordVal::RecordVal(Init *N, RecTy *T, bool P)
1478 : Name(N), TyAndPrefix(T, P) {
1479 Value = UnsetInit::get()->convertInitializerTo(T);
1
Calling 'UnsetInit::convertInitializerTo'
1480 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!\""
, "/build/llvm-toolchain-snapshot-7~svn326551/lib/TableGen/Record.cpp"
, 1480, __extension__ __PRETTY_FUNCTION__))
;
1481}
1482
1483StringRef RecordVal::getName() const {
1484 return cast<StringInit>(getNameInit())->getValue();
1485}
1486
1487#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1488LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void RecordVal::dump() const { errs() << *this; }
1489#endif
1490
1491void RecordVal::print(raw_ostream &OS, bool PrintSem) const {
1492 if (getPrefix()) OS << "field ";
1493 OS << *getType() << " " << getNameInitAsString();
1494
1495 if (getValue())
1496 OS << " = " << *getValue();
1497
1498 if (PrintSem) OS << ";\n";
1499}
1500
1501unsigned Record::LastID = 0;
1502
1503void Record::init() {
1504 checkName();
1505
1506 // Every record potentially has a def at the top. This value is
1507 // replaced with the top-level def name at instantiation time.
1508 addValue(RecordVal(StringInit::get("NAME"), StringRecTy::get(), false));
1509}
1510
1511void Record::checkName() {
1512 // Ensure the record name has string type.
1513 const TypedInit *TypedName = cast<const TypedInit>(Name);
1514 if (!isa<StringRecTy>(TypedName->getType()))
1515 PrintFatalError(getLoc(), "Record name is not a string!");
1516}
1517
1518DefInit *Record::getDefInit() {
1519 if (!TheInit)
1520 TheInit = new(Allocator) DefInit(this, new(Allocator) RecordRecTy(this));
1521 return TheInit;
1522}
1523
1524void Record::setName(Init *NewName) {
1525 Name = NewName;
1526 checkName();
1527 // DO NOT resolve record values to the name at this point because
1528 // there might be default values for arguments of this def. Those
1529 // arguments might not have been resolved yet so we don't want to
1530 // prematurely assume values for those arguments were not passed to
1531 // this def.
1532 //
1533 // Nonetheless, it may be that some of this Record's values
1534 // reference the record name. Indeed, the reason for having the
1535 // record name be an Init is to provide this flexibility. The extra
1536 // resolve steps after completely instantiating defs takes care of
1537 // this. See TGParser::ParseDef and TGParser::ParseDefm.
1538}
1539
1540void Record::resolveReferencesTo(const RecordVal *RV) {
1541 for (RecordVal &Value : Values) {
1542 if (RV == &Value) // Skip resolve the same field as the given one
1543 continue;
1544 if (Init *V = Value.getValue())
1545 if (Value.setValue(V->resolveReferences(*this, RV)))
1546 PrintFatalError(getLoc(), "Invalid value is found when setting '" +
1547 Value.getNameInitAsString() +
1548 "' after resolving references" +
1549 (RV ? " against '" + RV->getNameInitAsString() +
1550 "' of (" + RV->getValue()->getAsUnquotedString() +
1551 ")"
1552 : "") + "\n");
1553 }
1554 Init *OldName = getNameInit();
1555 Init *NewName = Name->resolveReferences(*this, RV);
1556 if (NewName != OldName) {
1557 // Re-register with RecordKeeper.
1558 setName(NewName);
1559 }
1560}
1561
1562#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1563LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void Record::dump() const { errs() << *this; }
1564#endif
1565
1566raw_ostream &llvm::operator<<(raw_ostream &OS, const Record &R) {
1567 OS << R.getNameInitAsString();
1568
1569 ArrayRef<Init *> TArgs = R.getTemplateArgs();
1570 if (!TArgs.empty()) {
1571 OS << "<";
1572 bool NeedComma = false;
1573 for (const Init *TA : TArgs) {
1574 if (NeedComma) OS << ", ";
1575 NeedComma = true;
1576 const RecordVal *RV = R.getValue(TA);
1577 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??\""
, "/build/llvm-toolchain-snapshot-7~svn326551/lib/TableGen/Record.cpp"
, 1577, __extension__ __PRETTY_FUNCTION__))
;
1578 RV->print(OS, false);
1579 }
1580 OS << ">";
1581 }
1582
1583 OS << " {";
1584 ArrayRef<std::pair<Record *, SMRange>> SC = R.getSuperClasses();
1585 if (!SC.empty()) {
1586 OS << "\t//";
1587 for (const auto &SuperPair : SC)
1588 OS << " " << SuperPair.first->getNameInitAsString();
1589 }
1590 OS << "\n";
1591
1592 for (const RecordVal &Val : R.getValues())
1593 if (Val.getPrefix() && !R.isTemplateArg(Val.getNameInit()))
1594 OS << Val;
1595 for (const RecordVal &Val : R.getValues())
1596 if (!Val.getPrefix() && !R.isTemplateArg(Val.getNameInit()))
1597 OS << Val;
1598
1599 return OS << "}\n";
1600}
1601
1602Init *Record::getValueInit(StringRef FieldName) const {
1603 const RecordVal *R = getValue(FieldName);
1604 if (!R || !R->getValue())
1605 PrintFatalError(getLoc(), "Record `" + getName() +
1606 "' does not have a field named `" + FieldName + "'!\n");
1607 return R->getValue();
1608}
1609
1610StringRef Record::getValueAsString(StringRef FieldName) const {
1611 const RecordVal *R = getValue(FieldName);
1612 if (!R || !R->getValue())
1613 PrintFatalError(getLoc(), "Record `" + getName() +
1614 "' does not have a field named `" + FieldName + "'!\n");
1615
1616 if (StringInit *SI = dyn_cast<StringInit>(R->getValue()))
1617 return SI->getValue();
1618 if (CodeInit *CI = dyn_cast<CodeInit>(R->getValue()))
1619 return CI->getValue();
1620
1621 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1622 FieldName + "' does not have a string initializer!");
1623}
1624
1625BitsInit *Record::getValueAsBitsInit(StringRef FieldName) const {
1626 const RecordVal *R = getValue(FieldName);
1627 if (!R || !R->getValue())
1628 PrintFatalError(getLoc(), "Record `" + getName() +
1629 "' does not have a field named `" + FieldName + "'!\n");
1630
1631 if (BitsInit *BI = dyn_cast<BitsInit>(R->getValue()))
1632 return BI;
1633 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1634 FieldName + "' does not have a BitsInit initializer!");
1635}
1636
1637ListInit *Record::getValueAsListInit(StringRef FieldName) const {
1638 const RecordVal *R = getValue(FieldName);
1639 if (!R || !R->getValue())
1640 PrintFatalError(getLoc(), "Record `" + getName() +
1641 "' does not have a field named `" + FieldName + "'!\n");
1642
1643 if (ListInit *LI = dyn_cast<ListInit>(R->getValue()))
1644 return LI;
1645 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1646 FieldName + "' does not have a list initializer!");
1647}
1648
1649std::vector<Record*>
1650Record::getValueAsListOfDefs(StringRef FieldName) const {
1651 ListInit *List = getValueAsListInit(FieldName);
1652 std::vector<Record*> Defs;
1653 for (Init *I : List->getValues()) {
1654 if (DefInit *DI = dyn_cast<DefInit>(I))
1655 Defs.push_back(DI->getDef());
1656 else
1657 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1658 FieldName + "' list is not entirely DefInit!");
1659 }
1660 return Defs;
1661}
1662
1663int64_t Record::getValueAsInt(StringRef FieldName) const {
1664 const RecordVal *R = getValue(FieldName);
1665 if (!R || !R->getValue())
1666 PrintFatalError(getLoc(), "Record `" + getName() +
1667 "' does not have a field named `" + FieldName + "'!\n");
1668
1669 if (IntInit *II = dyn_cast<IntInit>(R->getValue()))
1670 return II->getValue();
1671 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1672 FieldName + "' does not have an int initializer!");
1673}
1674
1675std::vector<int64_t>
1676Record::getValueAsListOfInts(StringRef FieldName) const {
1677 ListInit *List = getValueAsListInit(FieldName);
1678 std::vector<int64_t> Ints;
1679 for (Init *I : List->getValues()) {
1680 if (IntInit *II = dyn_cast<IntInit>(I))
1681 Ints.push_back(II->getValue());
1682 else
1683 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1684 FieldName + "' does not have a list of ints initializer!");
1685 }
1686 return Ints;
1687}
1688
1689std::vector<StringRef>
1690Record::getValueAsListOfStrings(StringRef FieldName) const {
1691 ListInit *List = getValueAsListInit(FieldName);
1692 std::vector<StringRef> Strings;
1693 for (Init *I : List->getValues()) {
1694 if (StringInit *SI = dyn_cast<StringInit>(I))
1695 Strings.push_back(SI->getValue());
1696 else
1697 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1698 FieldName + "' does not have a list of strings initializer!");
1699 }
1700 return Strings;
1701}
1702
1703Record *Record::getValueAsDef(StringRef FieldName) const {
1704 const RecordVal *R = getValue(FieldName);
1705 if (!R || !R->getValue())
1706 PrintFatalError(getLoc(), "Record `" + getName() +
1707 "' does not have a field named `" + FieldName + "'!\n");
1708
1709 if (DefInit *DI = dyn_cast<DefInit>(R->getValue()))
1710 return DI->getDef();
1711 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1712 FieldName + "' does not have a def initializer!");
1713}
1714
1715bool Record::getValueAsBit(StringRef FieldName) const {
1716 const RecordVal *R = getValue(FieldName);
1717 if (!R || !R->getValue())
1718 PrintFatalError(getLoc(), "Record `" + getName() +
1719 "' does not have a field named `" + FieldName + "'!\n");
1720
1721 if (BitInit *BI = dyn_cast<BitInit>(R->getValue()))
1722 return BI->getValue();
1723 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1724 FieldName + "' does not have a bit initializer!");
1725}
1726
1727bool Record::getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const {
1728 const RecordVal *R = getValue(FieldName);
1729 if (!R || !R->getValue())
1730 PrintFatalError(getLoc(), "Record `" + getName() +
1731 "' does not have a field named `" + FieldName.str() + "'!\n");
1732
1733 if (isa<UnsetInit>(R->getValue())) {
1734 Unset = true;
1735 return false;
1736 }
1737 Unset = false;
1738 if (BitInit *BI = dyn_cast<BitInit>(R->getValue()))
1739 return BI->getValue();
1740 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1741 FieldName + "' does not have a bit initializer!");
1742}
1743
1744DagInit *Record::getValueAsDag(StringRef FieldName) const {
1745 const RecordVal *R = getValue(FieldName);
1746 if (!R || !R->getValue())
1747 PrintFatalError(getLoc(), "Record `" + getName() +
1748 "' does not have a field named `" + FieldName + "'!\n");
1749
1750 if (DagInit *DI = dyn_cast<DagInit>(R->getValue()))
1751 return DI;
1752 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1753 FieldName + "' does not have a dag initializer!");
1754}
1755
1756#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1757LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void MultiClass::dump() const {
1758 errs() << "Record:\n";
1759 Rec.dump();
1760
1761 errs() << "Defs:\n";
1762 for (const auto &Proto : DefPrototypes)
1763 Proto->dump();
1764}
1765
1766LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void RecordKeeper::dump() const { errs() << *this; }
1767#endif
1768
1769raw_ostream &llvm::operator<<(raw_ostream &OS, const RecordKeeper &RK) {
1770 OS << "------------- Classes -----------------\n";
1771 for (const auto &C : RK.getClasses())
1772 OS << "class " << *C.second;
1773
1774 OS << "------------- Defs -----------------\n";
1775 for (const auto &D : RK.getDefs())
1776 OS << "def " << *D.second;
1777 return OS;
1778}
1779
1780std::vector<Record *>
1781RecordKeeper::getAllDerivedDefinitions(StringRef ClassName) const {
1782 Record *Class = getClass(ClassName);
1783 if (!Class)
1784 PrintFatalError("ERROR: Couldn't find the `" + ClassName + "' class!\n");
1785
1786 std::vector<Record*> Defs;
1787 for (const auto &D : getDefs())
1788 if (D.second->isSubClassOf(Class))
1789 Defs.push_back(D.second.get());
1790
1791 return Defs;
1792}
1793
1794static Init *GetStrConcat(Init *I0, Init *I1) {
1795 // Shortcut for the common case of concatenating two strings.
1796 if (const StringInit *I0s = dyn_cast<StringInit>(I0))
1797 if (const StringInit *I1s = dyn_cast<StringInit>(I1))
1798 return ConcatStringInits(I0s, I1s);
1799 return BinOpInit::get(BinOpInit::STRCONCAT, I0, I1, StringRecTy::get());
1800}
1801
1802Init *llvm::QualifyName(Record &CurRec, MultiClass *CurMultiClass,
1803 Init *Name, StringRef Scoper) {
1804 Init *NewName = GetStrConcat(CurRec.getNameInit(), StringInit::get(Scoper));
1805 NewName = GetStrConcat(NewName, Name);
1806 if (CurMultiClass && Scoper != "::") {
1807 Init *Prefix = GetStrConcat(CurMultiClass->Rec.getNameInit(),
1808 StringInit::get("::"));
1809 NewName = GetStrConcat(Prefix, NewName);
1810 }
1811
1812 if (BinOpInit *BinOp = dyn_cast<BinOpInit>(NewName))
1813 NewName = BinOp->Fold(&CurRec, CurMultiClass);
1814 return NewName;
1815}