Bug Summary

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