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~svn325874/build-llvm/lib/TableGen -I /build/llvm-toolchain-snapshot-7~svn325874/lib/TableGen -I /build/llvm-toolchain-snapshot-7~svn325874/build-llvm/include -I /build/llvm-toolchain-snapshot-7~svn325874/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~svn325874/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-02-23-163436-368-1 -x c++ /build/llvm-toolchain-snapshot-7~svn325874/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~svn325874/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~svn325874/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~svn325874/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~svn325874/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
567Init *ListInit::resolveListElementReference(Record &R, const RecordVal *IRV,
568 unsigned Elt) const {
569 if (Elt >= size())
570 return nullptr; // Out of range reference.
571 Init *E = getElement(Elt);
572 // If the element is set to some value, or if we are resolving a reference
573 // to a specific variable and that variable is explicitly unset, then
574 // replace the VarListElementInit with it.
575 if (IRV || !isa<UnsetInit>(E))
576 return E;
577 return nullptr;
578}
579
580std::string ListInit::getAsString() const {
581 std::string Result = "[";
582 const char *sep = "";
583 for (Init *Element : *this) {
584 Result += sep;
585 sep = ", ";
586 Result += Element->getAsString();
587 }
588 return Result + "]";
589}
590
591Init *OpInit::resolveListElementReference(Record &R, const RecordVal *IRV,
592 unsigned Elt) const {
593 Init *Resolved = resolveReferences(R, IRV);
594 OpInit *OResolved = dyn_cast<OpInit>(Resolved);
595 if (OResolved) {
596 Resolved = OResolved->Fold(&R, nullptr);
597 }
598
599 if (Resolved != this) {
600 TypedInit *Typed = cast<TypedInit>(Resolved);
601 if (Init *New = Typed->resolveListElementReference(R, IRV, Elt))
602 return New;
603 return VarListElementInit::get(Typed, Elt);
604 }
605
606 return nullptr;
607}
608
609Init *OpInit::getBit(unsigned Bit) const {
610 if (getType() == BitRecTy::get())
611 return const_cast<OpInit*>(this);
612 return VarBitInit::get(const_cast<OpInit*>(this), Bit);
613}
614
615static void
616ProfileUnOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *Op, RecTy *Type) {
617 ID.AddInteger(Opcode);
618 ID.AddPointer(Op);
619 ID.AddPointer(Type);
620}
621
622UnOpInit *UnOpInit::get(UnaryOp Opc, Init *LHS, RecTy *Type) {
623 static FoldingSet<UnOpInit> ThePool;
624
625 FoldingSetNodeID ID;
626 ProfileUnOpInit(ID, Opc, LHS, Type);
627
628 void *IP = nullptr;
629 if (UnOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
630 return I;
631
632 UnOpInit *I = new(Allocator) UnOpInit(Opc, LHS, Type);
633 ThePool.InsertNode(I, IP);
634 return I;
635}
636
637void UnOpInit::Profile(FoldingSetNodeID &ID) const {
638 ProfileUnOpInit(ID, getOpcode(), getOperand(), getType());
639}
640
641Init *UnOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const {
642 switch (getOpcode()) {
643 case CAST:
644 if (isa<StringRecTy>(getType())) {
645 if (StringInit *LHSs = dyn_cast<StringInit>(LHS))
646 return LHSs;
647
648 if (DefInit *LHSd = dyn_cast<DefInit>(LHS))
649 return StringInit::get(LHSd->getAsString());
650
651 if (IntInit *LHSi = dyn_cast<IntInit>(LHS))
652 return StringInit::get(LHSi->getAsString());
653 } else {
654 if (StringInit *Name = dyn_cast<StringInit>(LHS)) {
655 // From TGParser::ParseIDValue
656 if (CurRec) {
657 if (const RecordVal *RV = CurRec->getValue(Name)) {
658 if (RV->getType() != getType())
659 PrintFatalError("type mismatch in cast");
660 return VarInit::get(Name, RV->getType());
661 }
662
663 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name,
664 ":");
665
666 if (CurRec->isTemplateArg(TemplateArgName)) {
667 const RecordVal *RV = CurRec->getValue(TemplateArgName);
668 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~svn325874/lib/TableGen/Record.cpp"
, 668, __extension__ __PRETTY_FUNCTION__))
;
669
670 if (RV->getType() != getType())
671 PrintFatalError("type mismatch in cast");
672
673 return VarInit::get(TemplateArgName, RV->getType());
674 }
675 }
676
677 if (CurMultiClass) {
678 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
679 "::");
680
681 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
682 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
683 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~svn325874/lib/TableGen/Record.cpp"
, 683, __extension__ __PRETTY_FUNCTION__))
;
684
685 if (RV->getType() != getType())
686 PrintFatalError("type mismatch in cast");
687
688 return VarInit::get(MCName, RV->getType());
689 }
690 }
691 assert(CurRec && "NULL pointer")(static_cast <bool> (CurRec && "NULL pointer") ?
void (0) : __assert_fail ("CurRec && \"NULL pointer\""
, "/build/llvm-toolchain-snapshot-7~svn325874/lib/TableGen/Record.cpp"
, 691, __extension__ __PRETTY_FUNCTION__))
;
692 if (Record *D = (CurRec->getRecords()).getDef(Name->getValue()))
693 return DefInit::get(D);
694
695 PrintFatalError(CurRec->getLoc(),
696 "Undefined reference:'" + Name->getValue() + "'\n");
697 }
698
699 if (isa<IntRecTy>(getType())) {
700 if (BitsInit *BI = dyn_cast<BitsInit>(LHS)) {
701 if (Init *NewInit = BI->convertInitializerTo(IntRecTy::get()))
702 return NewInit;
703 break;
704 }
705 }
706 }
707 break;
708
709 case HEAD:
710 if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) {
711 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~svn325874/lib/TableGen/Record.cpp"
, 711, __extension__ __PRETTY_FUNCTION__))
;
712 return LHSl->getElement(0);
713 }
714 break;
715
716 case TAIL:
717 if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) {
718 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~svn325874/lib/TableGen/Record.cpp"
, 718, __extension__ __PRETTY_FUNCTION__))
;
719 // Note the +1. We can't just pass the result of getValues()
720 // directly.
721 return ListInit::get(LHSl->getValues().slice(1), LHSl->getElementType());
722 }
723 break;
724
725 case EMPTY:
726 if (ListInit *LHSl = dyn_cast<ListInit>(LHS))
727 return IntInit::get(LHSl->empty());
728 if (StringInit *LHSs = dyn_cast<StringInit>(LHS))
729 return IntInit::get(LHSs->getValue().empty());
730 break;
731 }
732 return const_cast<UnOpInit *>(this);
733}
734
735Init *UnOpInit::resolveReferences(Record &R, const RecordVal *RV) const {
736 Init *lhs = LHS->resolveReferences(R, RV);
737
738 if (LHS != lhs)
739 return (UnOpInit::get(getOpcode(), lhs, getType()))->Fold(&R, nullptr);
740 return Fold(&R, nullptr);
741}
742
743std::string UnOpInit::getAsString() const {
744 std::string Result;
745 switch (getOpcode()) {
746 case CAST: Result = "!cast<" + getType()->getAsString() + ">"; break;
747 case HEAD: Result = "!head"; break;
748 case TAIL: Result = "!tail"; break;
749 case EMPTY: Result = "!empty"; break;
750 }
751 return Result + "(" + LHS->getAsString() + ")";
752}
753
754static void
755ProfileBinOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *LHS, Init *RHS,
756 RecTy *Type) {
757 ID.AddInteger(Opcode);
758 ID.AddPointer(LHS);
759 ID.AddPointer(RHS);
760 ID.AddPointer(Type);
761}
762
763BinOpInit *BinOpInit::get(BinaryOp Opc, Init *LHS,
764 Init *RHS, RecTy *Type) {
765 static FoldingSet<BinOpInit> ThePool;
766
767 FoldingSetNodeID ID;
768 ProfileBinOpInit(ID, Opc, LHS, RHS, Type);
769
770 void *IP = nullptr;
771 if (BinOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
772 return I;
773
774 BinOpInit *I = new(Allocator) BinOpInit(Opc, LHS, RHS, Type);
775 ThePool.InsertNode(I, IP);
776 return I;
777}
778
779void BinOpInit::Profile(FoldingSetNodeID &ID) const {
780 ProfileBinOpInit(ID, getOpcode(), getLHS(), getRHS(), getType());
781}
782
783static StringInit *ConcatStringInits(const StringInit *I0,
784 const StringInit *I1) {
785 SmallString<80> Concat(I0->getValue());
786 Concat.append(I1->getValue());
787 return StringInit::get(Concat);
788}
789
790Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const {
791 switch (getOpcode()) {
792 case CONCAT: {
793 DagInit *LHSs = dyn_cast<DagInit>(LHS);
794 DagInit *RHSs = dyn_cast<DagInit>(RHS);
795 if (LHSs && RHSs) {
796 DefInit *LOp = dyn_cast<DefInit>(LHSs->getOperator());
797 DefInit *ROp = dyn_cast<DefInit>(RHSs->getOperator());
798 if (!LOp || !ROp || LOp->getDef() != ROp->getDef())
799 PrintFatalError("Concated Dag operators do not match!");
800 SmallVector<Init*, 8> Args;
801 SmallVector<StringInit*, 8> ArgNames;
802 for (unsigned i = 0, e = LHSs->getNumArgs(); i != e; ++i) {
803 Args.push_back(LHSs->getArg(i));
804 ArgNames.push_back(LHSs->getArgName(i));
805 }
806 for (unsigned i = 0, e = RHSs->getNumArgs(); i != e; ++i) {
807 Args.push_back(RHSs->getArg(i));
808 ArgNames.push_back(RHSs->getArgName(i));
809 }
810 return DagInit::get(LHSs->getOperator(), nullptr, Args, ArgNames);
811 }
812 break;
813 }
814 case LISTCONCAT: {
815 ListInit *LHSs = dyn_cast<ListInit>(LHS);
816 ListInit *RHSs = dyn_cast<ListInit>(RHS);
817 if (LHSs && RHSs) {
818 SmallVector<Init *, 8> Args;
819 Args.insert(Args.end(), LHSs->begin(), LHSs->end());
820 Args.insert(Args.end(), RHSs->begin(), RHSs->end());
821 return ListInit::get(Args, LHSs->getElementType());
822 }
823 break;
824 }
825 case STRCONCAT: {
826 StringInit *LHSs = dyn_cast<StringInit>(LHS);
827 StringInit *RHSs = dyn_cast<StringInit>(RHS);
828 if (LHSs && RHSs)
829 return ConcatStringInits(LHSs, RHSs);
830 break;
831 }
832 case EQ: {
833 // try to fold eq comparison for 'bit' and 'int', otherwise fallback
834 // to string objects.
835 IntInit *L =
836 dyn_cast_or_null<IntInit>(LHS->convertInitializerTo(IntRecTy::get()));
837 IntInit *R =
838 dyn_cast_or_null<IntInit>(RHS->convertInitializerTo(IntRecTy::get()));
839
840 if (L && R)
841 return IntInit::get(L->getValue() == R->getValue());
842
843 StringInit *LHSs = dyn_cast<StringInit>(LHS);
844 StringInit *RHSs = dyn_cast<StringInit>(RHS);
845
846 // Make sure we've resolved
847 if (LHSs && RHSs)
848 return IntInit::get(LHSs->getValue() == RHSs->getValue());
849
850 break;
851 }
852 case ADD:
853 case AND:
854 case OR:
855 case SHL:
856 case SRA:
857 case SRL: {
858 IntInit *LHSi =
859 dyn_cast_or_null<IntInit>(LHS->convertInitializerTo(IntRecTy::get()));
860 IntInit *RHSi =
861 dyn_cast_or_null<IntInit>(RHS->convertInitializerTo(IntRecTy::get()));
862 if (LHSi && RHSi) {
863 int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
864 int64_t Result;
865 switch (getOpcode()) {
866 default: llvm_unreachable("Bad opcode!")::llvm::llvm_unreachable_internal("Bad opcode!", "/build/llvm-toolchain-snapshot-7~svn325874/lib/TableGen/Record.cpp"
, 866)
;
867 case ADD: Result = LHSv + RHSv; break;
868 case AND: Result = LHSv & RHSv; break;
869 case OR: Result = LHSv | RHSv; break;
870 case SHL: Result = LHSv << RHSv; break;
871 case SRA: Result = LHSv >> RHSv; break;
872 case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break;
873 }
874 return IntInit::get(Result);
875 }
876 break;
877 }
878 }
879 return const_cast<BinOpInit *>(this);
880}
881
882Init *BinOpInit::resolveReferences(Record &R, const RecordVal *RV) const {
883 Init *lhs = LHS->resolveReferences(R, RV);
884 Init *rhs = RHS->resolveReferences(R, RV);
885
886 if (LHS != lhs || RHS != rhs)
887 return (BinOpInit::get(getOpcode(), lhs, rhs, getType()))->Fold(&R,nullptr);
888 return Fold(&R, nullptr);
889}
890
891std::string BinOpInit::getAsString() const {
892 std::string Result;
893 switch (getOpcode()) {
894 case CONCAT: Result = "!con"; break;
895 case ADD: Result = "!add"; break;
896 case AND: Result = "!and"; break;
897 case OR: Result = "!or"; break;
898 case SHL: Result = "!shl"; break;
899 case SRA: Result = "!sra"; break;
900 case SRL: Result = "!srl"; break;
901 case EQ: Result = "!eq"; break;
902 case LISTCONCAT: Result = "!listconcat"; break;
903 case STRCONCAT: Result = "!strconcat"; break;
904 }
905 return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")";
906}
907
908static void
909ProfileTernOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *LHS, Init *MHS,
910 Init *RHS, RecTy *Type) {
911 ID.AddInteger(Opcode);
912 ID.AddPointer(LHS);
913 ID.AddPointer(MHS);
914 ID.AddPointer(RHS);
915 ID.AddPointer(Type);
916}
917
918TernOpInit *TernOpInit::get(TernaryOp Opc, Init *LHS, Init *MHS, Init *RHS,
919 RecTy *Type) {
920 static FoldingSet<TernOpInit> ThePool;
921
922 FoldingSetNodeID ID;
923 ProfileTernOpInit(ID, Opc, LHS, MHS, RHS, Type);
924
925 void *IP = nullptr;
926 if (TernOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
927 return I;
928
929 TernOpInit *I = new(Allocator) TernOpInit(Opc, LHS, MHS, RHS, Type);
930 ThePool.InsertNode(I, IP);
931 return I;
932}
933
934void TernOpInit::Profile(FoldingSetNodeID &ID) const {
935 ProfileTernOpInit(ID, getOpcode(), getLHS(), getMHS(), getRHS(), getType());
936}
937
938// Evaluates operation RHSo after replacing all operands matching LHS with Arg.
939static Init *EvaluateOperation(OpInit *RHSo, Init *LHS, Init *Arg,
940 Record *CurRec, MultiClass *CurMultiClass) {
941
942 SmallVector<Init *, 8> NewOperands;
943 NewOperands.reserve(RHSo->getNumOperands());
944 for (unsigned i = 0, e = RHSo->getNumOperands(); i < e; ++i) {
945 if (auto *RHSoo = dyn_cast<OpInit>(RHSo->getOperand(i))) {
946 if (Init *Result =
947 EvaluateOperation(RHSoo, LHS, Arg, CurRec, CurMultiClass))
948 NewOperands.push_back(Result);
949 else
950 NewOperands.push_back(RHSoo);
951 } else if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
952 NewOperands.push_back(Arg);
953 } else {
954 NewOperands.push_back(RHSo->getOperand(i));
955 }
956 }
957
958 // Now run the operator and use its result as the new leaf
959 const OpInit *NewOp = RHSo->clone(NewOperands);
960 Init *NewVal = NewOp->Fold(CurRec, CurMultiClass);
961 return (NewVal != NewOp) ? NewVal : nullptr;
962}
963
964// Applies RHS to all elements of MHS, using LHS as a temp variable.
965static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
966 Record *CurRec, MultiClass *CurMultiClass) {
967 OpInit *RHSo = dyn_cast<OpInit>(RHS);
968
969 if (!RHSo)
970 PrintFatalError(CurRec->getLoc(), "!foreach requires an operator\n");
971
972 TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
973
974 if (!LHSt)
975 PrintFatalError(CurRec->getLoc(), "!foreach requires typed variable\n");
976
977 DagInit *MHSd = dyn_cast<DagInit>(MHS);
978 if (MHSd) {
979 Init *Val = MHSd->getOperator();
980 if (Init *Result = EvaluateOperation(RHSo, LHS, Val, CurRec, CurMultiClass))
981 Val = Result;
982
983 SmallVector<std::pair<Init *, StringInit *>, 8> args;
984 for (unsigned int i = 0; i < MHSd->getNumArgs(); ++i) {
985 Init *Arg = MHSd->getArg(i);
986 StringInit *ArgName = MHSd->getArgName(i);
987 // If this is a dag, recurse
988 if (isa<DagInit>(Arg)) {
989 if (Init *Result =
990 ForeachHelper(LHS, Arg, RHSo, Type, CurRec, CurMultiClass))
991 Arg = Result;
992 } else if (Init *Result =
993 EvaluateOperation(RHSo, LHS, Arg, CurRec, CurMultiClass)) {
994 Arg = Result;
995 }
996
997 // TODO: Process arg names
998 args.push_back(std::make_pair(Arg, ArgName));
999 }
1000
1001 return DagInit::get(Val, nullptr, args);
1002 }
1003
1004 ListInit *MHSl = dyn_cast<ListInit>(MHS);
1005 ListRecTy *ListType = dyn_cast<ListRecTy>(Type);
1006 if (MHSl && ListType) {
1007 SmallVector<Init *, 8> NewList(MHSl->begin(), MHSl->end());
1008 for (Init *&Arg : NewList) {
1009 if (Init *Result =
1010 EvaluateOperation(RHSo, LHS, Arg, CurRec, CurMultiClass))
1011 Arg = Result;
1012 }
1013 return ListInit::get(NewList, ListType->getElementType());
1014 }
1015 return nullptr;
1016}
1017
1018Init *TernOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const {
1019 switch (getOpcode()) {
1020 case SUBST: {
1021 DefInit *LHSd = dyn_cast<DefInit>(LHS);
1022 VarInit *LHSv = dyn_cast<VarInit>(LHS);
1023 StringInit *LHSs = dyn_cast<StringInit>(LHS);
1024
1025 DefInit *MHSd = dyn_cast<DefInit>(MHS);
1026 VarInit *MHSv = dyn_cast<VarInit>(MHS);
1027 StringInit *MHSs = dyn_cast<StringInit>(MHS);
1028
1029 DefInit *RHSd = dyn_cast<DefInit>(RHS);
1030 VarInit *RHSv = dyn_cast<VarInit>(RHS);
1031 StringInit *RHSs = dyn_cast<StringInit>(RHS);
1032
1033 if (LHSd && MHSd && RHSd) {
1034 Record *Val = RHSd->getDef();
1035 if (LHSd->getAsString() == RHSd->getAsString())
1036 Val = MHSd->getDef();
1037 return DefInit::get(Val);
1038 }
1039 if (LHSv && MHSv && RHSv) {
1040 std::string Val = RHSv->getName();
1041 if (LHSv->getAsString() == RHSv->getAsString())
1042 Val = MHSv->getName();
1043 return VarInit::get(Val, getType());
1044 }
1045 if (LHSs && MHSs && RHSs) {
1046 std::string Val = RHSs->getValue();
1047
1048 std::string::size_type found;
1049 std::string::size_type idx = 0;
1050 while (true) {
1051 found = Val.find(LHSs->getValue(), idx);
1052 if (found == std::string::npos)
1053 break;
1054 Val.replace(found, LHSs->getValue().size(), MHSs->getValue());
1055 idx = found + MHSs->getValue().size();
1056 }
1057
1058 return StringInit::get(Val);
1059 }
1060 break;
1061 }
1062
1063 case FOREACH: {
1064 if (Init *Result =
1065 ForeachHelper(LHS, MHS, RHS, getType(), CurRec, CurMultiClass))
1066 return Result;
1067 break;
1068 }
1069
1070 case IF: {
1071 IntInit *LHSi = dyn_cast<IntInit>(LHS);
1072 if (Init *I = LHS->convertInitializerTo(IntRecTy::get()))
1073 LHSi = dyn_cast<IntInit>(I);
1074 if (LHSi) {
1075 if (LHSi->getValue())
1076 return MHS;
1077 return RHS;
1078 }
1079 break;
1080 }
1081 }
1082
1083 return const_cast<TernOpInit *>(this);
1084}
1085
1086Init *TernOpInit::resolveReferences(Record &R,
1087 const RecordVal *RV) const {
1088 Init *lhs = LHS->resolveReferences(R, RV);
1089
1090 if (getOpcode() == IF && lhs != LHS) {
1091 IntInit *Value = dyn_cast<IntInit>(lhs);
1092 if (Init *I = lhs->convertInitializerTo(IntRecTy::get()))
1093 Value = dyn_cast<IntInit>(I);
1094 if (Value) {
1095 // Short-circuit
1096 if (Value->getValue()) {
1097 Init *mhs = MHS->resolveReferences(R, RV);
1098 return (TernOpInit::get(getOpcode(), lhs, mhs,
1099 RHS, getType()))->Fold(&R, nullptr);
1100 }
1101 Init *rhs = RHS->resolveReferences(R, RV);
1102 return (TernOpInit::get(getOpcode(), lhs, MHS,
1103 rhs, getType()))->Fold(&R, nullptr);
1104 }
1105 }
1106
1107 Init *mhs = MHS->resolveReferences(R, RV);
1108 Init *rhs = RHS->resolveReferences(R, RV);
1109
1110 if (LHS != lhs || MHS != mhs || RHS != rhs)
1111 return (TernOpInit::get(getOpcode(), lhs, mhs, rhs,
1112 getType()))->Fold(&R, nullptr);
1113 return Fold(&R, nullptr);
1114}
1115
1116std::string TernOpInit::getAsString() const {
1117 std::string Result;
1118 switch (getOpcode()) {
1119 case SUBST: Result = "!subst"; break;
1120 case FOREACH: Result = "!foreach"; break;
1121 case IF: Result = "!if"; break;
1122 }
1123 return Result + "(" + LHS->getAsString() + ", " + MHS->getAsString() + ", " +
1124 RHS->getAsString() + ")";
1125}
1126
1127RecTy *TypedInit::getFieldType(StringInit *FieldName) const {
1128 if (RecordRecTy *RecordType = dyn_cast<RecordRecTy>(getType()))
1129 if (RecordVal *Field = RecordType->getRecord()->getValue(FieldName))
1130 return Field->getType();
1131 return nullptr;
1132}
1133
1134Init *
1135TypedInit::convertInitializerTo(RecTy *Ty) const {
1136 if (isa<IntRecTy>(Ty)) {
1137 if (getType()->typeIsConvertibleTo(Ty))
1138 return const_cast<TypedInit *>(this);
1139 return nullptr;
1140 }
1141
1142 if (isa<StringRecTy>(Ty)) {
1143 if (isa<StringRecTy>(getType()))
1144 return const_cast<TypedInit *>(this);
1145 return nullptr;
1146 }
1147
1148 if (isa<CodeRecTy>(Ty)) {
1149 if (isa<CodeRecTy>(getType()))
1150 return const_cast<TypedInit *>(this);
1151 return nullptr;
1152 }
1153
1154 if (isa<BitRecTy>(Ty)) {
1155 // Accept variable if it is already of bit type!
1156 if (isa<BitRecTy>(getType()))
1157 return const_cast<TypedInit *>(this);
1158 if (auto *BitsTy = dyn_cast<BitsRecTy>(getType())) {
1159 // Accept only bits<1> expression.
1160 if (BitsTy->getNumBits() == 1)
1161 return const_cast<TypedInit *>(this);
1162 return nullptr;
1163 }
1164 // Ternary !if can be converted to bit, but only if both sides are
1165 // convertible to a bit.
1166 if (const auto *TOI = dyn_cast<TernOpInit>(this)) {
1167 if (TOI->getOpcode() == TernOpInit::TernaryOp::IF &&
1168 TOI->getMHS()->convertInitializerTo(BitRecTy::get()) &&
1169 TOI->getRHS()->convertInitializerTo(BitRecTy::get()))
1170 return const_cast<TypedInit *>(this);
1171 return nullptr;
1172 }
1173 return nullptr;
1174 }
1175
1176 if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
1177 if (BRT->getNumBits() == 1 && isa<BitRecTy>(getType()))
1178 return BitsInit::get(const_cast<TypedInit *>(this));
1179
1180 if (getType()->typeIsConvertibleTo(BRT)) {
1181 SmallVector<Init *, 16> NewBits(BRT->getNumBits());
1182
1183 for (unsigned i = 0; i != BRT->getNumBits(); ++i)
1184 NewBits[i] = VarBitInit::get(const_cast<TypedInit *>(this), i);
1185 return BitsInit::get(NewBits);
1186 }
1187
1188 return nullptr;
1189 }
1190
1191 if (auto *DLRT = dyn_cast<ListRecTy>(Ty)) {
1192 if (auto *SLRT = dyn_cast<ListRecTy>(getType()))
1193 if (SLRT->getElementType()->typeIsConvertibleTo(DLRT->getElementType()))
1194 return const_cast<TypedInit *>(this);
1195 return nullptr;
1196 }
1197
1198 if (auto *DRT = dyn_cast<DagRecTy>(Ty)) {
1199 if (getType()->typeIsConvertibleTo(DRT))
1200 return const_cast<TypedInit *>(this);
1201 return nullptr;
1202 }
1203
1204 if (auto *SRRT = dyn_cast<RecordRecTy>(Ty)) {
1205 // Ensure that this is compatible with Rec.
1206 if (RecordRecTy *DRRT = dyn_cast<RecordRecTy>(getType()))
1207 if (DRRT->getRecord()->isSubClassOf(SRRT->getRecord()) ||
1208 DRRT->getRecord() == SRRT->getRecord())
1209 return const_cast<TypedInit *>(this);
1210 return nullptr;
1211 }
1212
1213 return nullptr;
1214}
1215
1216Init *TypedInit::convertInitializerBitRange(ArrayRef<unsigned> Bits) const {
1217 BitsRecTy *T = dyn_cast<BitsRecTy>(getType());
1218 if (!T) return nullptr; // Cannot subscript a non-bits variable.
1219 unsigned NumBits = T->getNumBits();
1220
1221 SmallVector<Init *, 16> NewBits;
1222 NewBits.reserve(Bits.size());
1223 for (unsigned Bit : Bits) {
1224 if (Bit >= NumBits)
1225 return nullptr;
1226
1227 NewBits.push_back(VarBitInit::get(const_cast<TypedInit *>(this), Bit));
1228 }
1229 return BitsInit::get(NewBits);
1230}
1231
1232Init *TypedInit::convertInitListSlice(ArrayRef<unsigned> Elements) const {
1233 ListRecTy *T = dyn_cast<ListRecTy>(getType());
1234 if (!T) return nullptr; // Cannot subscript a non-list variable.
1235
1236 if (Elements.size() == 1)
1237 return VarListElementInit::get(const_cast<TypedInit *>(this), Elements[0]);
1238
1239 SmallVector<Init*, 8> ListInits;
1240 ListInits.reserve(Elements.size());
1241 for (unsigned Element : Elements)
1242 ListInits.push_back(VarListElementInit::get(const_cast<TypedInit *>(this),
1243 Element));
1244 return ListInit::get(ListInits, T->getElementType());
1245}
1246
1247
1248VarInit *VarInit::get(StringRef VN, RecTy *T) {
1249 Init *Value = StringInit::get(VN);
1250 return VarInit::get(Value, T);
1251}
1252
1253VarInit *VarInit::get(Init *VN, RecTy *T) {
1254 using Key = std::pair<RecTy *, Init *>;
1255 static DenseMap<Key, VarInit*> ThePool;
1256
1257 Key TheKey(std::make_pair(T, VN));
1258
1259 VarInit *&I = ThePool[TheKey];
1260 if (!I)
1261 I = new(Allocator) VarInit(VN, T);
1262 return I;
1263}
1264
1265StringRef VarInit::getName() const {
1266 StringInit *NameString = cast<StringInit>(getNameInit());
1267 return NameString->getValue();
1268}
1269
1270Init *VarInit::getBit(unsigned Bit) const {
1271 if (getType() == BitRecTy::get())
1272 return const_cast<VarInit*>(this);
1273 return VarBitInit::get(const_cast<VarInit*>(this), Bit);
1274}
1275
1276Init *VarInit::resolveListElementReference(Record &R,
1277 const RecordVal *IRV,
1278 unsigned Elt) const {
1279 if (R.isTemplateArg(getNameInit())) return nullptr;
1280 if (IRV && IRV->getNameInit() != getNameInit()) return nullptr;
1281
1282 RecordVal *RV = R.getValue(getNameInit());
1283 assert(RV && "Reference to a non-existent variable?")(static_cast <bool> (RV && "Reference to a non-existent variable?"
) ? void (0) : __assert_fail ("RV && \"Reference to a non-existent variable?\""
, "/build/llvm-toolchain-snapshot-7~svn325874/lib/TableGen/Record.cpp"
, 1283, __extension__ __PRETTY_FUNCTION__))
;
1284 ListInit *LI = dyn_cast<ListInit>(RV->getValue());
1285 if (!LI)
1286 return VarListElementInit::get(cast<TypedInit>(RV->getValue()), Elt);
1287
1288 if (Elt >= LI->size())
1289 return nullptr; // Out of range reference.
1290 Init *E = LI->getElement(Elt);
1291 // If the element is set to some value, or if we are resolving a reference
1292 // to a specific variable and that variable is explicitly unset, then
1293 // replace the VarListElementInit with it.
1294 if (IRV || !isa<UnsetInit>(E))
1295 return E;
1296 return nullptr;
1297}
1298
1299RecTy *VarInit::getFieldType(StringInit *FieldName) const {
1300 if (RecordRecTy *RTy = dyn_cast<RecordRecTy>(getType()))
1301 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
1302 return RV->getType();
1303 return nullptr;
1304}
1305
1306Init *VarInit::getFieldInit(Record &R, const RecordVal *RV,
1307 StringInit *FieldName) const {
1308 if (isa<RecordRecTy>(getType()))
1309 if (const RecordVal *Val = R.getValue(VarName)) {
1310 if (RV != Val && (RV || isa<UnsetInit>(Val->getValue())))
1311 return nullptr;
1312 Init *TheInit = Val->getValue();
1313 assert(TheInit != this && "Infinite loop detected!")(static_cast <bool> (TheInit != this && "Infinite loop detected!"
) ? void (0) : __assert_fail ("TheInit != this && \"Infinite loop detected!\""
, "/build/llvm-toolchain-snapshot-7~svn325874/lib/TableGen/Record.cpp"
, 1313, __extension__ __PRETTY_FUNCTION__))
;
1314 if (Init *I = TheInit->getFieldInit(R, RV, FieldName))
1315 return I;
1316 return nullptr;
1317 }
1318 return nullptr;
1319}
1320
1321Init *VarInit::resolveReferences(Record &R, const RecordVal *RV) const {
1322 if (RecordVal *Val = R.getValue(VarName))
1323 if (RV == Val || (!RV && !isa<UnsetInit>(Val->getValue())))
1324 return Val->getValue();
1325 return const_cast<VarInit *>(this);
1326}
1327
1328VarBitInit *VarBitInit::get(TypedInit *T, unsigned B) {
1329 using Key = std::pair<TypedInit *, unsigned>;
1330 static DenseMap<Key, VarBitInit*> ThePool;
1331
1332 Key TheKey(std::make_pair(T, B));
1333
1334 VarBitInit *&I = ThePool[TheKey];
1335 if (!I)
1336 I = new(Allocator) VarBitInit(T, B);
1337 return I;
1338}
1339
1340Init *VarBitInit::convertInitializerTo(RecTy *Ty) const {
1341 if (isa<BitRecTy>(Ty))
1342 return const_cast<VarBitInit *>(this);
1343
1344 return nullptr;
1345}
1346
1347std::string VarBitInit::getAsString() const {
1348 return TI->getAsString() + "{" + utostr(Bit) + "}";
1349}
1350
1351Init *VarBitInit::resolveReferences(Record &R, const RecordVal *RV) const {
1352 Init *I = TI->resolveReferences(R, RV);
1353 if (TI != I)
1354 return I->getBit(getBitNum());
1355
1356 return const_cast<VarBitInit*>(this);
1357}
1358
1359VarListElementInit *VarListElementInit::get(TypedInit *T,
1360 unsigned E) {
1361 using Key = std::pair<TypedInit *, unsigned>;
1362 static DenseMap<Key, VarListElementInit*> ThePool;
1363
1364 Key TheKey(std::make_pair(T, E));
1365
1366 VarListElementInit *&I = ThePool[TheKey];
1367 if (!I) I = new(Allocator) VarListElementInit(T, E);
1368 return I;
1369}
1370
1371std::string VarListElementInit::getAsString() const {
1372 return TI->getAsString() + "[" + utostr(Element) + "]";
1373}
1374
1375Init *
1376VarListElementInit::resolveReferences(Record &R, const RecordVal *RV) const {
1377 if (Init *I = getVariable()->resolveListElementReference(R, RV,
1378 getElementNum()))
1379 return I;
1380 return const_cast<VarListElementInit *>(this);
1381}
1382
1383Init *VarListElementInit::getBit(unsigned Bit) const {
1384 if (getType() == BitRecTy::get())
1385 return const_cast<VarListElementInit*>(this);
1386 return VarBitInit::get(const_cast<VarListElementInit*>(this), Bit);
1387}
1388
1389Init *VarListElementInit:: resolveListElementReference(Record &R,
1390 const RecordVal *RV,
1391 unsigned Elt) const {
1392 if (Init *Result = TI->resolveListElementReference(R, RV, Element)) {
1393 if (TypedInit *TInit = dyn_cast<TypedInit>(Result)) {
1394 if (Init *Result2 = TInit->resolveListElementReference(R, RV, Elt))
1395 return Result2;
1396 return VarListElementInit::get(TInit, Elt);
1397 }
1398 return Result;
1399 }
1400
1401 return nullptr;
1402}
1403
1404DefInit *DefInit::get(Record *R) {
1405 return R->getDefInit();
1406}
1407
1408Init *DefInit::convertInitializerTo(RecTy *Ty) const {
1409 if (auto *RRT = dyn_cast<RecordRecTy>(Ty))
1410 if (getDef()->isSubClassOf(RRT->getRecord()))
1411 return const_cast<DefInit *>(this);
1412 return nullptr;
1413}
1414
1415RecTy *DefInit::getFieldType(StringInit *FieldName) const {
1416 if (const RecordVal *RV = Def->getValue(FieldName))
1417 return RV->getType();
1418 return nullptr;
1419}
1420
1421Init *DefInit::getFieldInit(Record &R, const RecordVal *RV,
1422 StringInit *FieldName) const {
1423 return Def->getValue(FieldName)->getValue();
1424}
1425
1426std::string DefInit::getAsString() const {
1427 return Def->getName();
1428}
1429
1430FieldInit *FieldInit::get(Init *R, StringInit *FN) {
1431 using Key = std::pair<Init *, StringInit *>;
1432 static DenseMap<Key, FieldInit*> ThePool;
1433
1434 Key TheKey(std::make_pair(R, FN));
1435
1436 FieldInit *&I = ThePool[TheKey];
1437 if (!I) I = new(Allocator) FieldInit(R, FN);
1438 return I;
1439}
1440
1441Init *FieldInit::getBit(unsigned Bit) const {
1442 if (getType() == BitRecTy::get())
1443 return const_cast<FieldInit*>(this);
1444 return VarBitInit::get(const_cast<FieldInit*>(this), Bit);
1445}
1446
1447Init *FieldInit::resolveListElementReference(Record &R, const RecordVal *RV,
1448 unsigned Elt) const {
1449 if (Init *ListVal = Rec->getFieldInit(R, RV, FieldName))
1450 if (ListInit *LI = dyn_cast<ListInit>(ListVal)) {
1451 if (Elt >= LI->size()) return nullptr;
1452 Init *E = LI->getElement(Elt);
1453
1454 // If the element is set to some value, or if we are resolving a
1455 // reference to a specific variable and that variable is explicitly
1456 // unset, then replace the VarListElementInit with it.
1457 if (RV || !isa<UnsetInit>(E))
1458 return E;
1459 }
1460 return nullptr;
1461}
1462
1463Init *FieldInit::resolveReferences(Record &R, const RecordVal *RV) const {
1464 Init *NewRec = RV ? Rec->resolveReferences(R, RV) : Rec;
1465
1466 if (Init *BitsVal = NewRec->getFieldInit(R, RV, FieldName)) {
1467 Init *BVR = BitsVal->resolveReferences(R, RV);
1468 return BVR->isComplete() ? BVR : const_cast<FieldInit *>(this);
1469 }
1470
1471 if (NewRec != Rec)
1472 return FieldInit::get(NewRec, FieldName);
1473 return const_cast<FieldInit *>(this);
1474}
1475
1476static void ProfileDagInit(FoldingSetNodeID &ID, Init *V, StringInit *VN,
1477 ArrayRef<Init *> ArgRange,
1478 ArrayRef<StringInit *> NameRange) {
1479 ID.AddPointer(V);
1480 ID.AddPointer(VN);
1481
1482 ArrayRef<Init *>::iterator Arg = ArgRange.begin();
1483 ArrayRef<StringInit *>::iterator Name = NameRange.begin();
1484 while (Arg != ArgRange.end()) {
1485 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~svn325874/lib/TableGen/Record.cpp"
, 1485, __extension__ __PRETTY_FUNCTION__))
;
1486 ID.AddPointer(*Arg++);
1487 ID.AddPointer(*Name++);
1488 }
1489 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~svn325874/lib/TableGen/Record.cpp"
, 1489, __extension__ __PRETTY_FUNCTION__))
;
1490}
1491
1492DagInit *
1493DagInit::get(Init *V, StringInit *VN, ArrayRef<Init *> ArgRange,
1494 ArrayRef<StringInit *> NameRange) {
1495 static FoldingSet<DagInit> ThePool;
1496
1497 FoldingSetNodeID ID;
1498 ProfileDagInit(ID, V, VN, ArgRange, NameRange);
1499
1500 void *IP = nullptr;
1501 if (DagInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
1502 return I;
1503
1504 void *Mem = Allocator.Allocate(totalSizeToAlloc<Init *, StringInit *>(ArgRange.size(), NameRange.size()), alignof(BitsInit));
1505 DagInit *I = new(Mem) DagInit(V, VN, ArgRange.size(), NameRange.size());
1506 std::uninitialized_copy(ArgRange.begin(), ArgRange.end(),
1507 I->getTrailingObjects<Init *>());
1508 std::uninitialized_copy(NameRange.begin(), NameRange.end(),
1509 I->getTrailingObjects<StringInit *>());
1510 ThePool.InsertNode(I, IP);
1511 return I;
1512}
1513
1514DagInit *
1515DagInit::get(Init *V, StringInit *VN,
1516 ArrayRef<std::pair<Init*, StringInit*>> args) {
1517 SmallVector<Init *, 8> Args;
1518 SmallVector<StringInit *, 8> Names;
1519
1520 for (const auto &Arg : args) {
1521 Args.push_back(Arg.first);
1522 Names.push_back(Arg.second);
1523 }
1524
1525 return DagInit::get(V, VN, Args, Names);
1526}
1527
1528void DagInit::Profile(FoldingSetNodeID &ID) const {
1529 ProfileDagInit(ID, Val, ValName, makeArrayRef(getTrailingObjects<Init *>(), NumArgs), makeArrayRef(getTrailingObjects<StringInit *>(), NumArgNames));
1530}
1531
1532Init *DagInit::convertInitializerTo(RecTy *Ty) const {
1533 if (isa<DagRecTy>(Ty))
1534 return const_cast<DagInit *>(this);
1535
1536 return nullptr;
1537}
1538
1539Init *DagInit::resolveReferences(Record &R, const RecordVal *RV) const {
1540 SmallVector<Init*, 8> NewArgs;
1541 NewArgs.reserve(arg_size());
1542 bool ArgsChanged = false;
1543 for (const Init *Arg : getArgs()) {
1544 Init *NewArg = Arg->resolveReferences(R, RV);
1545 NewArgs.push_back(NewArg);
1546 ArgsChanged |= NewArg != Arg;
1547 }
1548
1549 Init *Op = Val->resolveReferences(R, RV);
1550 if (Op != Val || ArgsChanged)
1551 return DagInit::get(Op, ValName, NewArgs, getArgNames());
1552
1553 return const_cast<DagInit *>(this);
1554}
1555
1556std::string DagInit::getAsString() const {
1557 std::string Result = "(" + Val->getAsString();
1558 if (ValName)
1559 Result += ":" + ValName->getAsUnquotedString();
1560 if (!arg_empty()) {
1561 Result += " " + getArg(0)->getAsString();
1562 if (getArgName(0)) Result += ":$" + getArgName(0)->getAsUnquotedString();
1563 for (unsigned i = 1, e = getNumArgs(); i != e; ++i) {
1564 Result += ", " + getArg(i)->getAsString();
1565 if (getArgName(i)) Result += ":$" + getArgName(i)->getAsUnquotedString();
1566 }
1567 }
1568 return Result + ")";
1569}
1570
1571//===----------------------------------------------------------------------===//
1572// Other implementations
1573//===----------------------------------------------------------------------===//
1574
1575RecordVal::RecordVal(Init *N, RecTy *T, bool P)
1576 : Name(N), TyAndPrefix(T, P) {
1577 Value = UnsetInit::get()->convertInitializerTo(T);
1
Calling 'UnsetInit::convertInitializerTo'
1578 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~svn325874/lib/TableGen/Record.cpp"
, 1578, __extension__ __PRETTY_FUNCTION__))
;
1579}
1580
1581StringRef RecordVal::getName() const {
1582 return cast<StringInit>(getNameInit())->getValue();
1583}
1584
1585#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1586LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void RecordVal::dump() const { errs() << *this; }
1587#endif
1588
1589void RecordVal::print(raw_ostream &OS, bool PrintSem) const {
1590 if (getPrefix()) OS << "field ";
1591 OS << *getType() << " " << getNameInitAsString();
1592
1593 if (getValue())
1594 OS << " = " << *getValue();
1595
1596 if (PrintSem) OS << ";\n";
1597}
1598
1599unsigned Record::LastID = 0;
1600
1601void Record::init() {
1602 checkName();
1603
1604 // Every record potentially has a def at the top. This value is
1605 // replaced with the top-level def name at instantiation time.
1606 addValue(RecordVal(StringInit::get("NAME"), StringRecTy::get(), false));
1607}
1608
1609void Record::checkName() {
1610 // Ensure the record name has string type.
1611 const TypedInit *TypedName = cast<const TypedInit>(Name);
1612 if (!isa<StringRecTy>(TypedName->getType()))
1613 PrintFatalError(getLoc(), "Record name is not a string!");
1614}
1615
1616DefInit *Record::getDefInit() {
1617 if (!TheInit)
1618 TheInit = new(Allocator) DefInit(this, new(Allocator) RecordRecTy(this));
1619 return TheInit;
1620}
1621
1622void Record::setName(Init *NewName) {
1623 Name = NewName;
1624 checkName();
1625 // DO NOT resolve record values to the name at this point because
1626 // there might be default values for arguments of this def. Those
1627 // arguments might not have been resolved yet so we don't want to
1628 // prematurely assume values for those arguments were not passed to
1629 // this def.
1630 //
1631 // Nonetheless, it may be that some of this Record's values
1632 // reference the record name. Indeed, the reason for having the
1633 // record name be an Init is to provide this flexibility. The extra
1634 // resolve steps after completely instantiating defs takes care of
1635 // this. See TGParser::ParseDef and TGParser::ParseDefm.
1636}
1637
1638void Record::resolveReferencesTo(const RecordVal *RV) {
1639 for (RecordVal &Value : Values) {
1640 if (RV == &Value) // Skip resolve the same field as the given one
1641 continue;
1642 if (Init *V = Value.getValue())
1643 if (Value.setValue(V->resolveReferences(*this, RV)))
1644 PrintFatalError(getLoc(), "Invalid value is found when setting '" +
1645 Value.getNameInitAsString() +
1646 "' after resolving references" +
1647 (RV ? " against '" + RV->getNameInitAsString() +
1648 "' of (" + RV->getValue()->getAsUnquotedString() +
1649 ")"
1650 : "") + "\n");
1651 }
1652 Init *OldName = getNameInit();
1653 Init *NewName = Name->resolveReferences(*this, RV);
1654 if (NewName != OldName) {
1655 // Re-register with RecordKeeper.
1656 setName(NewName);
1657 }
1658}
1659
1660#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1661LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void Record::dump() const { errs() << *this; }
1662#endif
1663
1664raw_ostream &llvm::operator<<(raw_ostream &OS, const Record &R) {
1665 OS << R.getNameInitAsString();
1666
1667 ArrayRef<Init *> TArgs = R.getTemplateArgs();
1668 if (!TArgs.empty()) {
1669 OS << "<";
1670 bool NeedComma = false;
1671 for (const Init *TA : TArgs) {
1672 if (NeedComma) OS << ", ";
1673 NeedComma = true;
1674 const RecordVal *RV = R.getValue(TA);
1675 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~svn325874/lib/TableGen/Record.cpp"
, 1675, __extension__ __PRETTY_FUNCTION__))
;
1676 RV->print(OS, false);
1677 }
1678 OS << ">";
1679 }
1680
1681 OS << " {";
1682 ArrayRef<std::pair<Record *, SMRange>> SC = R.getSuperClasses();
1683 if (!SC.empty()) {
1684 OS << "\t//";
1685 for (const auto &SuperPair : SC)
1686 OS << " " << SuperPair.first->getNameInitAsString();
1687 }
1688 OS << "\n";
1689
1690 for (const RecordVal &Val : R.getValues())
1691 if (Val.getPrefix() && !R.isTemplateArg(Val.getNameInit()))
1692 OS << Val;
1693 for (const RecordVal &Val : R.getValues())
1694 if (!Val.getPrefix() && !R.isTemplateArg(Val.getNameInit()))
1695 OS << Val;
1696
1697 return OS << "}\n";
1698}
1699
1700Init *Record::getValueInit(StringRef FieldName) const {
1701 const RecordVal *R = getValue(FieldName);
1702 if (!R || !R->getValue())
1703 PrintFatalError(getLoc(), "Record `" + getName() +
1704 "' does not have a field named `" + FieldName + "'!\n");
1705 return R->getValue();
1706}
1707
1708StringRef Record::getValueAsString(StringRef FieldName) const {
1709 const RecordVal *R = getValue(FieldName);
1710 if (!R || !R->getValue())
1711 PrintFatalError(getLoc(), "Record `" + getName() +
1712 "' does not have a field named `" + FieldName + "'!\n");
1713
1714 if (StringInit *SI = dyn_cast<StringInit>(R->getValue()))
1715 return SI->getValue();
1716 if (CodeInit *CI = dyn_cast<CodeInit>(R->getValue()))
1717 return CI->getValue();
1718
1719 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1720 FieldName + "' does not have a string initializer!");
1721}
1722
1723BitsInit *Record::getValueAsBitsInit(StringRef FieldName) const {
1724 const RecordVal *R = getValue(FieldName);
1725 if (!R || !R->getValue())
1726 PrintFatalError(getLoc(), "Record `" + getName() +
1727 "' does not have a field named `" + FieldName + "'!\n");
1728
1729 if (BitsInit *BI = dyn_cast<BitsInit>(R->getValue()))
1730 return BI;
1731 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1732 FieldName + "' does not have a BitsInit initializer!");
1733}
1734
1735ListInit *Record::getValueAsListInit(StringRef FieldName) const {
1736 const RecordVal *R = getValue(FieldName);
1737 if (!R || !R->getValue())
1738 PrintFatalError(getLoc(), "Record `" + getName() +
1739 "' does not have a field named `" + FieldName + "'!\n");
1740
1741 if (ListInit *LI = dyn_cast<ListInit>(R->getValue()))
1742 return LI;
1743 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1744 FieldName + "' does not have a list initializer!");
1745}
1746
1747std::vector<Record*>
1748Record::getValueAsListOfDefs(StringRef FieldName) const {
1749 ListInit *List = getValueAsListInit(FieldName);
1750 std::vector<Record*> Defs;
1751 for (Init *I : List->getValues()) {
1752 if (DefInit *DI = dyn_cast<DefInit>(I))
1753 Defs.push_back(DI->getDef());
1754 else
1755 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1756 FieldName + "' list is not entirely DefInit!");
1757 }
1758 return Defs;
1759}
1760
1761int64_t Record::getValueAsInt(StringRef FieldName) const {
1762 const RecordVal *R = getValue(FieldName);
1763 if (!R || !R->getValue())
1764 PrintFatalError(getLoc(), "Record `" + getName() +
1765 "' does not have a field named `" + FieldName + "'!\n");
1766
1767 if (IntInit *II = dyn_cast<IntInit>(R->getValue()))
1768 return II->getValue();
1769 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1770 FieldName + "' does not have an int initializer!");
1771}
1772
1773std::vector<int64_t>
1774Record::getValueAsListOfInts(StringRef FieldName) const {
1775 ListInit *List = getValueAsListInit(FieldName);
1776 std::vector<int64_t> Ints;
1777 for (Init *I : List->getValues()) {
1778 if (IntInit *II = dyn_cast<IntInit>(I))
1779 Ints.push_back(II->getValue());
1780 else
1781 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1782 FieldName + "' does not have a list of ints initializer!");
1783 }
1784 return Ints;
1785}
1786
1787std::vector<StringRef>
1788Record::getValueAsListOfStrings(StringRef FieldName) const {
1789 ListInit *List = getValueAsListInit(FieldName);
1790 std::vector<StringRef> Strings;
1791 for (Init *I : List->getValues()) {
1792 if (StringInit *SI = dyn_cast<StringInit>(I))
1793 Strings.push_back(SI->getValue());
1794 else
1795 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1796 FieldName + "' does not have a list of strings initializer!");
1797 }
1798 return Strings;
1799}
1800
1801Record *Record::getValueAsDef(StringRef FieldName) const {
1802 const RecordVal *R = getValue(FieldName);
1803 if (!R || !R->getValue())
1804 PrintFatalError(getLoc(), "Record `" + getName() +
1805 "' does not have a field named `" + FieldName + "'!\n");
1806
1807 if (DefInit *DI = dyn_cast<DefInit>(R->getValue()))
1808 return DI->getDef();
1809 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1810 FieldName + "' does not have a def initializer!");
1811}
1812
1813bool Record::getValueAsBit(StringRef FieldName) const {
1814 const RecordVal *R = getValue(FieldName);
1815 if (!R || !R->getValue())
1816 PrintFatalError(getLoc(), "Record `" + getName() +
1817 "' does not have a field named `" + FieldName + "'!\n");
1818
1819 if (BitInit *BI = dyn_cast<BitInit>(R->getValue()))
1820 return BI->getValue();
1821 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1822 FieldName + "' does not have a bit initializer!");
1823}
1824
1825bool Record::getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const {
1826 const RecordVal *R = getValue(FieldName);
1827 if (!R || !R->getValue())
1828 PrintFatalError(getLoc(), "Record `" + getName() +
1829 "' does not have a field named `" + FieldName.str() + "'!\n");
1830
1831 if (isa<UnsetInit>(R->getValue())) {
1832 Unset = true;
1833 return false;
1834 }
1835 Unset = false;
1836 if (BitInit *BI = dyn_cast<BitInit>(R->getValue()))
1837 return BI->getValue();
1838 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1839 FieldName + "' does not have a bit initializer!");
1840}
1841
1842DagInit *Record::getValueAsDag(StringRef FieldName) const {
1843 const RecordVal *R = getValue(FieldName);
1844 if (!R || !R->getValue())
1845 PrintFatalError(getLoc(), "Record `" + getName() +
1846 "' does not have a field named `" + FieldName + "'!\n");
1847
1848 if (DagInit *DI = dyn_cast<DagInit>(R->getValue()))
1849 return DI;
1850 PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1851 FieldName + "' does not have a dag initializer!");
1852}
1853
1854#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1855LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void MultiClass::dump() const {
1856 errs() << "Record:\n";
1857 Rec.dump();
1858
1859 errs() << "Defs:\n";
1860 for (const auto &Proto : DefPrototypes)
1861 Proto->dump();
1862}
1863
1864LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void RecordKeeper::dump() const { errs() << *this; }
1865#endif
1866
1867raw_ostream &llvm::operator<<(raw_ostream &OS, const RecordKeeper &RK) {
1868 OS << "------------- Classes -----------------\n";
1869 for (const auto &C : RK.getClasses())
1870 OS << "class " << *C.second;
1871
1872 OS << "------------- Defs -----------------\n";
1873 for (const auto &D : RK.getDefs())
1874 OS << "def " << *D.second;
1875 return OS;
1876}
1877
1878std::vector<Record *>
1879RecordKeeper::getAllDerivedDefinitions(StringRef ClassName) const {
1880 Record *Class = getClass(ClassName);
1881 if (!Class)
1882 PrintFatalError("ERROR: Couldn't find the `" + ClassName + "' class!\n");
1883
1884 std::vector<Record*> Defs;
1885 for (const auto &D : getDefs())
1886 if (D.second->isSubClassOf(Class))
1887 Defs.push_back(D.second.get());
1888
1889 return Defs;
1890}
1891
1892static Init *GetStrConcat(Init *I0, Init *I1) {
1893 // Shortcut for the common case of concatenating two strings.
1894 if (const StringInit *I0s = dyn_cast<StringInit>(I0))
1895 if (const StringInit *I1s = dyn_cast<StringInit>(I1))
1896 return ConcatStringInits(I0s, I1s);
1897 return BinOpInit::get(BinOpInit::STRCONCAT, I0, I1, StringRecTy::get());
1898}
1899
1900Init *llvm::QualifyName(Record &CurRec, MultiClass *CurMultiClass,
1901 Init *Name, StringRef Scoper) {
1902 Init *NewName = GetStrConcat(CurRec.getNameInit(), StringInit::get(Scoper));
1903 NewName = GetStrConcat(NewName, Name);
1904 if (CurMultiClass && Scoper != "::") {
1905 Init *Prefix = GetStrConcat(CurMultiClass->Rec.getNameInit(),
1906 StringInit::get("::"));
1907 NewName = GetStrConcat(Prefix, NewName);
1908 }
1909
1910 if (BinOpInit *BinOp = dyn_cast<BinOpInit>(NewName))
1911 NewName = BinOp->Fold(&CurRec, CurMultiClass);
1912 return NewName;
1913}