File: | lib/TableGen/Record.cpp |
Location: | line 816, column 26 |
Description: | Called C++ object pointer is null |
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/TableGen/Record.h" | |||
15 | #include "llvm/ADT/DenseMap.h" | |||
16 | #include "llvm/ADT/FoldingSet.h" | |||
17 | #include "llvm/ADT/Hashing.h" | |||
18 | #include "llvm/ADT/STLExtras.h" | |||
19 | #include "llvm/ADT/SmallVector.h" | |||
20 | #include "llvm/ADT/StringExtras.h" | |||
21 | #include "llvm/ADT/StringMap.h" | |||
22 | #include "llvm/Support/DataTypes.h" | |||
23 | #include "llvm/Support/ErrorHandling.h" | |||
24 | #include "llvm/Support/Format.h" | |||
25 | #include "llvm/TableGen/Error.h" | |||
26 | ||||
27 | using namespace llvm; | |||
28 | ||||
29 | //===----------------------------------------------------------------------===// | |||
30 | // std::string wrapper for DenseMap purposes | |||
31 | //===----------------------------------------------------------------------===// | |||
32 | ||||
33 | namespace llvm { | |||
34 | ||||
35 | /// TableGenStringKey - This is a wrapper for std::string suitable for | |||
36 | /// using as a key to a DenseMap. Because there isn't a particularly | |||
37 | /// good way to indicate tombstone or empty keys for strings, we want | |||
38 | /// to wrap std::string to indicate that this is a "special" string | |||
39 | /// not expected to take on certain values (those of the tombstone and | |||
40 | /// empty keys). This makes things a little safer as it clarifies | |||
41 | /// that DenseMap is really not appropriate for general strings. | |||
42 | ||||
43 | class TableGenStringKey { | |||
44 | public: | |||
45 | TableGenStringKey(const std::string &str) : data(str) {} | |||
46 | TableGenStringKey(const char *str) : data(str) {} | |||
47 | ||||
48 | const std::string &str() const { return data; } | |||
49 | ||||
50 | friend hash_code hash_value(const TableGenStringKey &Value) { | |||
51 | using llvm::hash_value; | |||
52 | return hash_value(Value.str()); | |||
53 | } | |||
54 | private: | |||
55 | std::string data; | |||
56 | }; | |||
57 | ||||
58 | /// Specialize DenseMapInfo for TableGenStringKey. | |||
59 | template<> struct DenseMapInfo<TableGenStringKey> { | |||
60 | static inline TableGenStringKey getEmptyKey() { | |||
61 | TableGenStringKey Empty("<<<EMPTY KEY>>>"); | |||
62 | return Empty; | |||
63 | } | |||
64 | static inline TableGenStringKey getTombstoneKey() { | |||
65 | TableGenStringKey Tombstone("<<<TOMBSTONE KEY>>>"); | |||
66 | return Tombstone; | |||
67 | } | |||
68 | static unsigned getHashValue(const TableGenStringKey& Val) { | |||
69 | using llvm::hash_value; | |||
70 | return hash_value(Val); | |||
71 | } | |||
72 | static bool isEqual(const TableGenStringKey& LHS, | |||
73 | const TableGenStringKey& RHS) { | |||
74 | return LHS.str() == RHS.str(); | |||
75 | } | |||
76 | }; | |||
77 | ||||
78 | } // namespace llvm | |||
79 | ||||
80 | //===----------------------------------------------------------------------===// | |||
81 | // Type implementations | |||
82 | //===----------------------------------------------------------------------===// | |||
83 | ||||
84 | BitRecTy BitRecTy::Shared; | |||
85 | IntRecTy IntRecTy::Shared; | |||
86 | StringRecTy StringRecTy::Shared; | |||
87 | DagRecTy DagRecTy::Shared; | |||
88 | ||||
89 | void RecTy::anchor() { } | |||
90 | void RecTy::dump() const { print(errs()); } | |||
91 | ||||
92 | ListRecTy *RecTy::getListTy() { | |||
93 | if (!ListTy) | |||
94 | ListTy = new ListRecTy(this); | |||
95 | return ListTy; | |||
96 | } | |||
97 | ||||
98 | bool RecTy::baseClassOf(const RecTy *RHS) const{ | |||
99 | assert (RHS && "NULL pointer")((RHS && "NULL pointer") ? static_cast<void> (0 ) : __assert_fail ("RHS && \"NULL pointer\"", "/tmp/buildd/llvm-toolchain-snapshot-3.6~svn224007/lib/TableGen/Record.cpp" , 99, __PRETTY_FUNCTION__)); | |||
100 | return Kind == RHS->getRecTyKind(); | |||
101 | } | |||
102 | ||||
103 | Init *BitRecTy::convertValue(BitsInit *BI) { | |||
104 | if (BI->getNumBits() != 1) return nullptr; // Only accept if just one bit! | |||
105 | return BI->getBit(0); | |||
106 | } | |||
107 | ||||
108 | Init *BitRecTy::convertValue(IntInit *II) { | |||
109 | int64_t Val = II->getValue(); | |||
110 | if (Val != 0 && Val != 1) return nullptr; // Only accept 0 or 1 for a bit! | |||
111 | ||||
112 | return BitInit::get(Val != 0); | |||
113 | } | |||
114 | ||||
115 | Init *BitRecTy::convertValue(TypedInit *VI) { | |||
116 | RecTy *Ty = VI->getType(); | |||
117 | if (isa<BitRecTy>(Ty)) | |||
118 | return VI; // Accept variable if it is already of bit type! | |||
119 | if (auto *BitsTy = dyn_cast<BitsRecTy>(Ty)) | |||
120 | // Accept only bits<1> expression. | |||
121 | return BitsTy->getNumBits() == 1 ? VI : nullptr; | |||
122 | // Ternary !if can be converted to bit, but only if both sides are | |||
123 | // convertible to a bit. | |||
124 | if (TernOpInit *TOI = dyn_cast<TernOpInit>(VI)) { | |||
125 | if (TOI->getOpcode() != TernOpInit::TernaryOp::IF) | |||
126 | return nullptr; | |||
127 | if (!TOI->getMHS()->convertInitializerTo(BitRecTy::get()) || | |||
128 | !TOI->getRHS()->convertInitializerTo(BitRecTy::get())) | |||
129 | return nullptr; | |||
130 | return TOI; | |||
131 | } | |||
132 | return nullptr; | |||
133 | } | |||
134 | ||||
135 | bool BitRecTy::baseClassOf(const RecTy *RHS) const{ | |||
136 | if(RecTy::baseClassOf(RHS) || getRecTyKind() == IntRecTyKind) | |||
137 | return true; | |||
138 | if(const BitsRecTy *BitsTy = dyn_cast<BitsRecTy>(RHS)) | |||
139 | return BitsTy->getNumBits() == 1; | |||
140 | return false; | |||
141 | } | |||
142 | ||||
143 | BitsRecTy *BitsRecTy::get(unsigned Sz) { | |||
144 | static std::vector<BitsRecTy*> Shared; | |||
145 | if (Sz >= Shared.size()) | |||
146 | Shared.resize(Sz + 1); | |||
147 | BitsRecTy *&Ty = Shared[Sz]; | |||
148 | if (!Ty) | |||
149 | Ty = new BitsRecTy(Sz); | |||
150 | return Ty; | |||
151 | } | |||
152 | ||||
153 | std::string BitsRecTy::getAsString() const { | |||
154 | return "bits<" + utostr(Size) + ">"; | |||
155 | } | |||
156 | ||||
157 | Init *BitsRecTy::convertValue(UnsetInit *UI) { | |||
158 | SmallVector<Init *, 16> NewBits(Size); | |||
159 | ||||
160 | for (unsigned i = 0; i != Size; ++i) | |||
161 | NewBits[i] = UnsetInit::get(); | |||
162 | ||||
163 | return BitsInit::get(NewBits); | |||
164 | } | |||
165 | ||||
166 | Init *BitsRecTy::convertValue(BitInit *UI) { | |||
167 | if (Size != 1) return nullptr; // Can only convert single bit. | |||
168 | return BitsInit::get(UI); | |||
169 | } | |||
170 | ||||
171 | /// canFitInBitfield - Return true if the number of bits is large enough to hold | |||
172 | /// the integer value. | |||
173 | static bool canFitInBitfield(int64_t Value, unsigned NumBits) { | |||
174 | // For example, with NumBits == 4, we permit Values from [-7 .. 15]. | |||
175 | return (NumBits >= sizeof(Value) * 8) || | |||
176 | (Value >> NumBits == 0) || (Value >> (NumBits-1) == -1); | |||
177 | } | |||
178 | ||||
179 | /// convertValue from Int initializer to bits type: Split the integer up into the | |||
180 | /// appropriate bits. | |||
181 | /// | |||
182 | Init *BitsRecTy::convertValue(IntInit *II) { | |||
183 | int64_t Value = II->getValue(); | |||
184 | // Make sure this bitfield is large enough to hold the integer value. | |||
185 | if (!canFitInBitfield(Value, Size)) | |||
186 | return nullptr; | |||
187 | ||||
188 | SmallVector<Init *, 16> NewBits(Size); | |||
189 | ||||
190 | for (unsigned i = 0; i != Size; ++i) | |||
191 | NewBits[i] = BitInit::get(Value & (1LL << i)); | |||
192 | ||||
193 | return BitsInit::get(NewBits); | |||
194 | } | |||
195 | ||||
196 | Init *BitsRecTy::convertValue(BitsInit *BI) { | |||
197 | // If the number of bits is right, return it. Otherwise we need to expand or | |||
198 | // truncate. | |||
199 | if (BI->getNumBits() == Size) return BI; | |||
200 | return nullptr; | |||
201 | } | |||
202 | ||||
203 | Init *BitsRecTy::convertValue(TypedInit *VI) { | |||
204 | if (Size == 1 && isa<BitRecTy>(VI->getType())) | |||
205 | return BitsInit::get(VI); | |||
206 | ||||
207 | if (VI->getType()->typeIsConvertibleTo(this)) { | |||
208 | SmallVector<Init *, 16> NewBits(Size); | |||
209 | ||||
210 | for (unsigned i = 0; i != Size; ++i) | |||
211 | NewBits[i] = VarBitInit::get(VI, i); | |||
212 | return BitsInit::get(NewBits); | |||
213 | } | |||
214 | ||||
215 | return nullptr; | |||
216 | } | |||
217 | ||||
218 | bool BitsRecTy::baseClassOf(const RecTy *RHS) const{ | |||
219 | if (RecTy::baseClassOf(RHS)) //argument and the receiver are the same type | |||
220 | return cast<BitsRecTy>(RHS)->Size == Size; | |||
221 | RecTyKind kind = RHS->getRecTyKind(); | |||
222 | return (kind == BitRecTyKind && Size == 1) || (kind == IntRecTyKind); | |||
223 | } | |||
224 | ||||
225 | Init *IntRecTy::convertValue(BitInit *BI) { | |||
226 | return IntInit::get(BI->getValue()); | |||
227 | } | |||
228 | ||||
229 | Init *IntRecTy::convertValue(BitsInit *BI) { | |||
230 | int64_t Result = 0; | |||
231 | for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) | |||
232 | if (BitInit *Bit = dyn_cast<BitInit>(BI->getBit(i))) { | |||
233 | Result |= Bit->getValue() << i; | |||
234 | } else { | |||
235 | return nullptr; | |||
236 | } | |||
237 | return IntInit::get(Result); | |||
238 | } | |||
239 | ||||
240 | Init *IntRecTy::convertValue(TypedInit *TI) { | |||
241 | if (TI->getType()->typeIsConvertibleTo(this)) | |||
242 | return TI; // Accept variable if already of the right type! | |||
243 | return nullptr; | |||
244 | } | |||
245 | ||||
246 | bool IntRecTy::baseClassOf(const RecTy *RHS) const{ | |||
247 | RecTyKind kind = RHS->getRecTyKind(); | |||
248 | return kind==BitRecTyKind || kind==BitsRecTyKind || kind==IntRecTyKind; | |||
249 | } | |||
250 | ||||
251 | Init *StringRecTy::convertValue(UnOpInit *BO) { | |||
252 | if (BO->getOpcode() == UnOpInit::CAST) { | |||
253 | Init *L = BO->getOperand()->convertInitializerTo(this); | |||
254 | if (!L) return nullptr; | |||
255 | if (L != BO->getOperand()) | |||
256 | return UnOpInit::get(UnOpInit::CAST, L, new StringRecTy); | |||
257 | return BO; | |||
258 | } | |||
259 | ||||
260 | return convertValue((TypedInit*)BO); | |||
261 | } | |||
262 | ||||
263 | Init *StringRecTy::convertValue(BinOpInit *BO) { | |||
264 | if (BO->getOpcode() == BinOpInit::STRCONCAT) { | |||
265 | Init *L = BO->getLHS()->convertInitializerTo(this); | |||
266 | Init *R = BO->getRHS()->convertInitializerTo(this); | |||
267 | if (!L || !R) return nullptr; | |||
268 | if (L != BO->getLHS() || R != BO->getRHS()) | |||
269 | return BinOpInit::get(BinOpInit::STRCONCAT, L, R, new StringRecTy); | |||
270 | return BO; | |||
271 | } | |||
272 | ||||
273 | return convertValue((TypedInit*)BO); | |||
274 | } | |||
275 | ||||
276 | ||||
277 | Init *StringRecTy::convertValue(TypedInit *TI) { | |||
278 | if (isa<StringRecTy>(TI->getType())) | |||
279 | return TI; // Accept variable if already of the right type! | |||
280 | return nullptr; | |||
281 | } | |||
282 | ||||
283 | std::string ListRecTy::getAsString() const { | |||
284 | return "list<" + Ty->getAsString() + ">"; | |||
285 | } | |||
286 | ||||
287 | Init *ListRecTy::convertValue(ListInit *LI) { | |||
288 | std::vector<Init*> Elements; | |||
289 | ||||
290 | // Verify that all of the elements of the list are subclasses of the | |||
291 | // appropriate class! | |||
292 | for (unsigned i = 0, e = LI->getSize(); i != e; ++i) | |||
293 | if (Init *CI = LI->getElement(i)->convertInitializerTo(Ty)) | |||
294 | Elements.push_back(CI); | |||
295 | else | |||
296 | return nullptr; | |||
297 | ||||
298 | if (!isa<ListRecTy>(LI->getType())) | |||
299 | return nullptr; | |||
300 | ||||
301 | return ListInit::get(Elements, this); | |||
302 | } | |||
303 | ||||
304 | Init *ListRecTy::convertValue(TypedInit *TI) { | |||
305 | // Ensure that TI is compatible with our class. | |||
306 | if (ListRecTy *LRT = dyn_cast<ListRecTy>(TI->getType())) | |||
307 | if (LRT->getElementType()->typeIsConvertibleTo(getElementType())) | |||
308 | return TI; | |||
309 | return nullptr; | |||
310 | } | |||
311 | ||||
312 | bool ListRecTy::baseClassOf(const RecTy *RHS) const{ | |||
313 | if(const ListRecTy* ListTy = dyn_cast<ListRecTy>(RHS)) | |||
314 | return ListTy->getElementType()->typeIsConvertibleTo(Ty); | |||
315 | return false; | |||
316 | } | |||
317 | ||||
318 | Init *DagRecTy::convertValue(TypedInit *TI) { | |||
319 | if (TI->getType()->typeIsConvertibleTo(this)) | |||
320 | return TI; | |||
321 | return nullptr; | |||
322 | } | |||
323 | ||||
324 | Init *DagRecTy::convertValue(UnOpInit *BO) { | |||
325 | if (BO->getOpcode() == UnOpInit::CAST) { | |||
326 | Init *L = BO->getOperand()->convertInitializerTo(this); | |||
327 | if (!L) return nullptr; | |||
328 | if (L != BO->getOperand()) | |||
329 | return UnOpInit::get(UnOpInit::CAST, L, new DagRecTy); | |||
330 | return BO; | |||
331 | } | |||
332 | return nullptr; | |||
333 | } | |||
334 | ||||
335 | Init *DagRecTy::convertValue(BinOpInit *BO) { | |||
336 | if (BO->getOpcode() == BinOpInit::CONCAT) { | |||
337 | Init *L = BO->getLHS()->convertInitializerTo(this); | |||
338 | Init *R = BO->getRHS()->convertInitializerTo(this); | |||
339 | if (!L || !R) return nullptr; | |||
340 | if (L != BO->getLHS() || R != BO->getRHS()) | |||
341 | return BinOpInit::get(BinOpInit::CONCAT, L, R, new DagRecTy); | |||
342 | return BO; | |||
343 | } | |||
344 | return nullptr; | |||
345 | } | |||
346 | ||||
347 | RecordRecTy *RecordRecTy::get(Record *R) { | |||
348 | return dyn_cast<RecordRecTy>(R->getDefInit()->getType()); | |||
349 | } | |||
350 | ||||
351 | std::string RecordRecTy::getAsString() const { | |||
352 | return Rec->getName(); | |||
353 | } | |||
354 | ||||
355 | Init *RecordRecTy::convertValue(DefInit *DI) { | |||
356 | // Ensure that DI is a subclass of Rec. | |||
357 | if (!DI->getDef()->isSubClassOf(Rec)) | |||
358 | return nullptr; | |||
359 | return DI; | |||
360 | } | |||
361 | ||||
362 | Init *RecordRecTy::convertValue(TypedInit *TI) { | |||
363 | // Ensure that TI is compatible with Rec. | |||
364 | if (RecordRecTy *RRT = dyn_cast<RecordRecTy>(TI->getType())) | |||
365 | if (RRT->getRecord()->isSubClassOf(getRecord()) || | |||
366 | RRT->getRecord() == getRecord()) | |||
367 | return TI; | |||
368 | return nullptr; | |||
369 | } | |||
370 | ||||
371 | bool RecordRecTy::baseClassOf(const RecTy *RHS) const{ | |||
372 | const RecordRecTy *RTy = dyn_cast<RecordRecTy>(RHS); | |||
373 | if (!RTy) | |||
374 | return false; | |||
375 | ||||
376 | if (Rec == RTy->getRecord() || RTy->getRecord()->isSubClassOf(Rec)) | |||
377 | return true; | |||
378 | ||||
379 | const std::vector<Record*> &SC = Rec->getSuperClasses(); | |||
380 | for (unsigned i = 0, e = SC.size(); i != e; ++i) | |||
381 | if (RTy->getRecord()->isSubClassOf(SC[i])) | |||
382 | return true; | |||
383 | ||||
384 | return false; | |||
385 | } | |||
386 | ||||
387 | /// resolveTypes - Find a common type that T1 and T2 convert to. | |||
388 | /// Return 0 if no such type exists. | |||
389 | /// | |||
390 | RecTy *llvm::resolveTypes(RecTy *T1, RecTy *T2) { | |||
391 | if (T1->typeIsConvertibleTo(T2)) | |||
392 | return T2; | |||
393 | if (T2->typeIsConvertibleTo(T1)) | |||
394 | return T1; | |||
395 | ||||
396 | // If one is a Record type, check superclasses | |||
397 | if (RecordRecTy *RecTy1 = dyn_cast<RecordRecTy>(T1)) { | |||
398 | // See if T2 inherits from a type T1 also inherits from | |||
399 | const std::vector<Record *> &T1SuperClasses = | |||
400 | RecTy1->getRecord()->getSuperClasses(); | |||
401 | for(std::vector<Record *>::const_iterator i = T1SuperClasses.begin(), | |||
402 | iend = T1SuperClasses.end(); | |||
403 | i != iend; | |||
404 | ++i) { | |||
405 | RecordRecTy *SuperRecTy1 = RecordRecTy::get(*i); | |||
406 | RecTy *NewType1 = resolveTypes(SuperRecTy1, T2); | |||
407 | if (NewType1) { | |||
408 | if (NewType1 != SuperRecTy1) { | |||
409 | delete SuperRecTy1; | |||
410 | } | |||
411 | return NewType1; | |||
412 | } | |||
413 | } | |||
414 | } | |||
415 | if (RecordRecTy *RecTy2 = dyn_cast<RecordRecTy>(T2)) { | |||
416 | // See if T1 inherits from a type T2 also inherits from | |||
417 | const std::vector<Record *> &T2SuperClasses = | |||
418 | RecTy2->getRecord()->getSuperClasses(); | |||
419 | for (std::vector<Record *>::const_iterator i = T2SuperClasses.begin(), | |||
420 | iend = T2SuperClasses.end(); | |||
421 | i != iend; | |||
422 | ++i) { | |||
423 | RecordRecTy *SuperRecTy2 = RecordRecTy::get(*i); | |||
424 | RecTy *NewType2 = resolveTypes(T1, SuperRecTy2); | |||
425 | if (NewType2) { | |||
426 | if (NewType2 != SuperRecTy2) { | |||
427 | delete SuperRecTy2; | |||
428 | } | |||
429 | return NewType2; | |||
430 | } | |||
431 | } | |||
432 | } | |||
433 | return nullptr; | |||
434 | } | |||
435 | ||||
436 | ||||
437 | //===----------------------------------------------------------------------===// | |||
438 | // Initializer implementations | |||
439 | //===----------------------------------------------------------------------===// | |||
440 | ||||
441 | void Init::anchor() { } | |||
442 | void Init::dump() const { return print(errs()); } | |||
443 | ||||
444 | void UnsetInit::anchor() { } | |||
445 | ||||
446 | UnsetInit *UnsetInit::get() { | |||
447 | static UnsetInit TheInit; | |||
448 | return &TheInit; | |||
449 | } | |||
450 | ||||
451 | void BitInit::anchor() { } | |||
452 | ||||
453 | BitInit *BitInit::get(bool V) { | |||
454 | static BitInit True(true); | |||
455 | static BitInit False(false); | |||
456 | ||||
457 | return V ? &True : &False; | |||
458 | } | |||
459 | ||||
460 | static void | |||
461 | ProfileBitsInit(FoldingSetNodeID &ID, ArrayRef<Init *> Range) { | |||
462 | ID.AddInteger(Range.size()); | |||
463 | ||||
464 | for (ArrayRef<Init *>::iterator i = Range.begin(), | |||
465 | iend = Range.end(); | |||
466 | i != iend; | |||
467 | ++i) | |||
468 | ID.AddPointer(*i); | |||
469 | } | |||
470 | ||||
471 | BitsInit *BitsInit::get(ArrayRef<Init *> Range) { | |||
472 | typedef FoldingSet<BitsInit> Pool; | |||
473 | static Pool ThePool; | |||
474 | ||||
475 | FoldingSetNodeID ID; | |||
476 | ProfileBitsInit(ID, Range); | |||
477 | ||||
478 | void *IP = nullptr; | |||
479 | if (BitsInit *I = ThePool.FindNodeOrInsertPos(ID, IP)) | |||
480 | return I; | |||
481 | ||||
482 | BitsInit *I = new BitsInit(Range); | |||
483 | ThePool.InsertNode(I, IP); | |||
484 | ||||
485 | return I; | |||
486 | } | |||
487 | ||||
488 | void BitsInit::Profile(FoldingSetNodeID &ID) const { | |||
489 | ProfileBitsInit(ID, Bits); | |||
490 | } | |||
491 | ||||
492 | Init * | |||
493 | BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) const { | |||
494 | SmallVector<Init *, 16> NewBits(Bits.size()); | |||
495 | ||||
496 | for (unsigned i = 0, e = Bits.size(); i != e; ++i) { | |||
497 | if (Bits[i] >= getNumBits()) | |||
498 | return nullptr; | |||
499 | NewBits[i] = getBit(Bits[i]); | |||
500 | } | |||
501 | return BitsInit::get(NewBits); | |||
502 | } | |||
503 | ||||
504 | std::string BitsInit::getAsString() const { | |||
505 | std::string Result = "{ "; | |||
506 | for (unsigned i = 0, e = getNumBits(); i != e; ++i) { | |||
507 | if (i) Result += ", "; | |||
508 | if (Init *Bit = getBit(e-i-1)) | |||
509 | Result += Bit->getAsString(); | |||
510 | else | |||
511 | Result += "*"; | |||
512 | } | |||
513 | return Result + " }"; | |||
514 | } | |||
515 | ||||
516 | // Fix bit initializer to preserve the behavior that bit reference from a unset | |||
517 | // bits initializer will resolve into VarBitInit to keep the field name and bit | |||
518 | // number used in targets with fixed insn length. | |||
519 | static Init *fixBitInit(const RecordVal *RV, Init *Before, Init *After) { | |||
520 | if (RV || After != UnsetInit::get()) | |||
521 | return After; | |||
522 | return Before; | |||
523 | } | |||
524 | ||||
525 | // resolveReferences - If there are any field references that refer to fields | |||
526 | // that have been filled in, we can propagate the values now. | |||
527 | // | |||
528 | Init *BitsInit::resolveReferences(Record &R, const RecordVal *RV) const { | |||
529 | bool Changed = false; | |||
530 | SmallVector<Init *, 16> NewBits(getNumBits()); | |||
531 | ||||
532 | Init *CachedInit = nullptr; | |||
533 | Init *CachedBitVar = nullptr; | |||
534 | bool CachedBitVarChanged = false; | |||
535 | ||||
536 | for (unsigned i = 0, e = getNumBits(); i != e; ++i) { | |||
537 | Init *CurBit = Bits[i]; | |||
538 | Init *CurBitVar = CurBit->getBitVar(); | |||
539 | ||||
540 | NewBits[i] = CurBit; | |||
541 | ||||
542 | if (CurBitVar == CachedBitVar) { | |||
543 | if (CachedBitVarChanged) { | |||
544 | Init *Bit = CachedInit->getBit(CurBit->getBitNum()); | |||
545 | NewBits[i] = fixBitInit(RV, CurBit, Bit); | |||
546 | } | |||
547 | continue; | |||
548 | } | |||
549 | CachedBitVar = CurBitVar; | |||
550 | CachedBitVarChanged = false; | |||
551 | ||||
552 | Init *B; | |||
553 | do { | |||
554 | B = CurBitVar; | |||
555 | CurBitVar = CurBitVar->resolveReferences(R, RV); | |||
556 | CachedBitVarChanged |= B != CurBitVar; | |||
557 | Changed |= B != CurBitVar; | |||
558 | } while (B != CurBitVar); | |||
559 | CachedInit = CurBitVar; | |||
560 | ||||
561 | if (CachedBitVarChanged) { | |||
562 | Init *Bit = CurBitVar->getBit(CurBit->getBitNum()); | |||
563 | NewBits[i] = fixBitInit(RV, CurBit, Bit); | |||
564 | } | |||
565 | } | |||
566 | ||||
567 | if (Changed) | |||
568 | return BitsInit::get(NewBits); | |||
569 | ||||
570 | return const_cast<BitsInit *>(this); | |||
571 | } | |||
572 | ||||
573 | namespace { | |||
574 | template<typename T> | |||
575 | class Pool : public T { | |||
576 | public: | |||
577 | ~Pool(); | |||
578 | }; | |||
579 | template<typename T> | |||
580 | Pool<T>::~Pool() { | |||
581 | for (typename T::iterator I = this->begin(), E = this->end(); I != E; ++I) { | |||
582 | typename T::value_type &Item = *I; | |||
583 | delete Item.second; | |||
584 | } | |||
585 | } | |||
586 | } | |||
587 | ||||
588 | IntInit *IntInit::get(int64_t V) { | |||
589 | static Pool<DenseMap<int64_t, IntInit *> > ThePool; | |||
590 | ||||
591 | IntInit *&I = ThePool[V]; | |||
592 | if (!I) I = new IntInit(V); | |||
593 | return I; | |||
594 | } | |||
595 | ||||
596 | std::string IntInit::getAsString() const { | |||
597 | return itostr(Value); | |||
598 | } | |||
599 | ||||
600 | Init * | |||
601 | IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) const { | |||
602 | SmallVector<Init *, 16> NewBits(Bits.size()); | |||
603 | ||||
604 | for (unsigned i = 0, e = Bits.size(); i != e; ++i) { | |||
605 | if (Bits[i] >= 64) | |||
606 | return nullptr; | |||
607 | ||||
608 | NewBits[i] = BitInit::get(Value & (INT64_C(1)1L << Bits[i])); | |||
609 | } | |||
610 | return BitsInit::get(NewBits); | |||
611 | } | |||
612 | ||||
613 | void StringInit::anchor() { } | |||
614 | ||||
615 | StringInit *StringInit::get(StringRef V) { | |||
616 | static Pool<StringMap<StringInit *> > ThePool; | |||
617 | ||||
618 | StringInit *&I = ThePool[V]; | |||
619 | if (!I) I = new StringInit(V); | |||
620 | return I; | |||
621 | } | |||
622 | ||||
623 | static void ProfileListInit(FoldingSetNodeID &ID, | |||
624 | ArrayRef<Init *> Range, | |||
625 | RecTy *EltTy) { | |||
626 | ID.AddInteger(Range.size()); | |||
627 | ID.AddPointer(EltTy); | |||
628 | ||||
629 | for (ArrayRef<Init *>::iterator i = Range.begin(), | |||
630 | iend = Range.end(); | |||
631 | i != iend; | |||
632 | ++i) | |||
633 | ID.AddPointer(*i); | |||
634 | } | |||
635 | ||||
636 | ListInit *ListInit::get(ArrayRef<Init *> Range, RecTy *EltTy) { | |||
637 | typedef FoldingSet<ListInit> Pool; | |||
638 | static Pool ThePool; | |||
639 | static std::vector<std::unique_ptr<ListInit>> TheActualPool; | |||
640 | ||||
641 | FoldingSetNodeID ID; | |||
642 | ProfileListInit(ID, Range, EltTy); | |||
643 | ||||
644 | void *IP = nullptr; | |||
645 | if (ListInit *I = ThePool.FindNodeOrInsertPos(ID, IP)) | |||
646 | return I; | |||
647 | ||||
648 | ListInit *I = new ListInit(Range, EltTy); | |||
649 | ThePool.InsertNode(I, IP); | |||
650 | TheActualPool.push_back(std::unique_ptr<ListInit>(I)); | |||
651 | return I; | |||
652 | } | |||
653 | ||||
654 | void ListInit::Profile(FoldingSetNodeID &ID) const { | |||
655 | ListRecTy *ListType = dyn_cast<ListRecTy>(getType()); | |||
656 | assert(ListType && "Bad type for ListInit!")((ListType && "Bad type for ListInit!") ? static_cast <void> (0) : __assert_fail ("ListType && \"Bad type for ListInit!\"" , "/tmp/buildd/llvm-toolchain-snapshot-3.6~svn224007/lib/TableGen/Record.cpp" , 656, __PRETTY_FUNCTION__)); | |||
657 | RecTy *EltTy = ListType->getElementType(); | |||
658 | ||||
659 | ProfileListInit(ID, Values, EltTy); | |||
660 | } | |||
661 | ||||
662 | Init * | |||
663 | ListInit::convertInitListSlice(const std::vector<unsigned> &Elements) const { | |||
664 | std::vector<Init*> Vals; | |||
665 | for (unsigned i = 0, e = Elements.size(); i != e; ++i) { | |||
666 | if (Elements[i] >= getSize()) | |||
667 | return nullptr; | |||
668 | Vals.push_back(getElement(Elements[i])); | |||
669 | } | |||
670 | return ListInit::get(Vals, getType()); | |||
671 | } | |||
672 | ||||
673 | Record *ListInit::getElementAsRecord(unsigned i) const { | |||
674 | assert(i < Values.size() && "List element index out of range!")((i < Values.size() && "List element index out of range!" ) ? static_cast<void> (0) : __assert_fail ("i < Values.size() && \"List element index out of range!\"" , "/tmp/buildd/llvm-toolchain-snapshot-3.6~svn224007/lib/TableGen/Record.cpp" , 674, __PRETTY_FUNCTION__)); | |||
675 | DefInit *DI = dyn_cast<DefInit>(Values[i]); | |||
676 | if (!DI) | |||
677 | PrintFatalError("Expected record in list!"); | |||
678 | return DI->getDef(); | |||
679 | } | |||
680 | ||||
681 | Init *ListInit::resolveReferences(Record &R, const RecordVal *RV) const { | |||
682 | std::vector<Init*> Resolved; | |||
683 | Resolved.reserve(getSize()); | |||
684 | bool Changed = false; | |||
685 | ||||
686 | for (unsigned i = 0, e = getSize(); i != e; ++i) { | |||
687 | Init *E; | |||
688 | Init *CurElt = getElement(i); | |||
689 | ||||
690 | do { | |||
691 | E = CurElt; | |||
692 | CurElt = CurElt->resolveReferences(R, RV); | |||
693 | Changed |= E != CurElt; | |||
694 | } while (E != CurElt); | |||
695 | Resolved.push_back(E); | |||
696 | } | |||
697 | ||||
698 | if (Changed) | |||
699 | return ListInit::get(Resolved, getType()); | |||
700 | return const_cast<ListInit *>(this); | |||
701 | } | |||
702 | ||||
703 | Init *ListInit::resolveListElementReference(Record &R, const RecordVal *IRV, | |||
704 | unsigned Elt) const { | |||
705 | if (Elt >= getSize()) | |||
706 | return nullptr; // Out of range reference. | |||
707 | Init *E = getElement(Elt); | |||
708 | // If the element is set to some value, or if we are resolving a reference | |||
709 | // to a specific variable and that variable is explicitly unset, then | |||
710 | // replace the VarListElementInit with it. | |||
711 | if (IRV || !isa<UnsetInit>(E)) | |||
712 | return E; | |||
713 | return nullptr; | |||
714 | } | |||
715 | ||||
716 | std::string ListInit::getAsString() const { | |||
717 | std::string Result = "["; | |||
718 | for (unsigned i = 0, e = Values.size(); i != e; ++i) { | |||
719 | if (i) Result += ", "; | |||
720 | Result += Values[i]->getAsString(); | |||
721 | } | |||
722 | return Result + "]"; | |||
723 | } | |||
724 | ||||
725 | Init *OpInit::resolveListElementReference(Record &R, const RecordVal *IRV, | |||
726 | unsigned Elt) const { | |||
727 | Init *Resolved = resolveReferences(R, IRV); | |||
728 | OpInit *OResolved = dyn_cast<OpInit>(Resolved); | |||
729 | if (OResolved) { | |||
730 | Resolved = OResolved->Fold(&R, nullptr); | |||
731 | } | |||
732 | ||||
733 | if (Resolved != this) { | |||
734 | TypedInit *Typed = dyn_cast<TypedInit>(Resolved); | |||
735 | assert(Typed && "Expected typed init for list reference")((Typed && "Expected typed init for list reference") ? static_cast<void> (0) : __assert_fail ("Typed && \"Expected typed init for list reference\"" , "/tmp/buildd/llvm-toolchain-snapshot-3.6~svn224007/lib/TableGen/Record.cpp" , 735, __PRETTY_FUNCTION__)); | |||
736 | if (Typed) { | |||
737 | Init *New = Typed->resolveListElementReference(R, IRV, Elt); | |||
738 | if (New) | |||
739 | return New; | |||
740 | return VarListElementInit::get(Typed, Elt); | |||
741 | } | |||
742 | } | |||
743 | ||||
744 | return nullptr; | |||
745 | } | |||
746 | ||||
747 | Init *OpInit::getBit(unsigned Bit) const { | |||
748 | if (getType() == BitRecTy::get()) | |||
749 | return const_cast<OpInit*>(this); | |||
750 | return VarBitInit::get(const_cast<OpInit*>(this), Bit); | |||
751 | } | |||
752 | ||||
753 | UnOpInit *UnOpInit::get(UnaryOp opc, Init *lhs, RecTy *Type) { | |||
754 | typedef std::pair<std::pair<unsigned, Init *>, RecTy *> Key; | |||
755 | static Pool<DenseMap<Key, UnOpInit *> > ThePool; | |||
756 | ||||
757 | Key TheKey(std::make_pair(std::make_pair(opc, lhs), Type)); | |||
758 | ||||
759 | UnOpInit *&I = ThePool[TheKey]; | |||
760 | if (!I) I = new UnOpInit(opc, lhs, Type); | |||
761 | return I; | |||
762 | } | |||
763 | ||||
764 | Init *UnOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const { | |||
765 | switch (getOpcode()) { | |||
| ||||
766 | case CAST: { | |||
767 | if (getType()->getAsString() == "string") { | |||
768 | if (StringInit *LHSs = dyn_cast<StringInit>(LHS)) | |||
769 | return LHSs; | |||
770 | ||||
771 | if (DefInit *LHSd = dyn_cast<DefInit>(LHS)) | |||
772 | return StringInit::get(LHSd->getDef()->getName()); | |||
773 | ||||
774 | if (IntInit *LHSi = dyn_cast<IntInit>(LHS)) | |||
775 | return StringInit::get(LHSi->getAsString()); | |||
776 | } else { | |||
777 | if (StringInit *LHSs = dyn_cast<StringInit>(LHS)) { | |||
778 | std::string Name = LHSs->getValue(); | |||
779 | ||||
780 | // From TGParser::ParseIDValue | |||
781 | if (CurRec) { | |||
782 | if (const RecordVal *RV = CurRec->getValue(Name)) { | |||
783 | if (RV->getType() != getType()) | |||
784 | PrintFatalError("type mismatch in cast"); | |||
785 | return VarInit::get(Name, RV->getType()); | |||
786 | } | |||
787 | ||||
788 | Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, | |||
789 | ":"); | |||
790 | ||||
791 | if (CurRec->isTemplateArg(TemplateArgName)) { | |||
792 | const RecordVal *RV = CurRec->getValue(TemplateArgName); | |||
793 | assert(RV && "Template arg doesn't exist??")((RV && "Template arg doesn't exist??") ? static_cast <void> (0) : __assert_fail ("RV && \"Template arg doesn't exist??\"" , "/tmp/buildd/llvm-toolchain-snapshot-3.6~svn224007/lib/TableGen/Record.cpp" , 793, __PRETTY_FUNCTION__)); | |||
794 | ||||
795 | if (RV->getType() != getType()) | |||
796 | PrintFatalError("type mismatch in cast"); | |||
797 | ||||
798 | return VarInit::get(TemplateArgName, RV->getType()); | |||
799 | } | |||
800 | } | |||
801 | ||||
802 | if (CurMultiClass) { | |||
803 | Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name, "::"); | |||
804 | ||||
805 | if (CurMultiClass->Rec.isTemplateArg(MCName)) { | |||
806 | const RecordVal *RV = CurMultiClass->Rec.getValue(MCName); | |||
807 | assert(RV && "Template arg doesn't exist??")((RV && "Template arg doesn't exist??") ? static_cast <void> (0) : __assert_fail ("RV && \"Template arg doesn't exist??\"" , "/tmp/buildd/llvm-toolchain-snapshot-3.6~svn224007/lib/TableGen/Record.cpp" , 807, __PRETTY_FUNCTION__)); | |||
808 | ||||
809 | if (RV->getType() != getType()) | |||
810 | PrintFatalError("type mismatch in cast"); | |||
811 | ||||
812 | return VarInit::get(MCName, RV->getType()); | |||
813 | } | |||
814 | } | |||
815 | ||||
816 | if (Record *D = (CurRec->getRecords()).getDef(Name)) | |||
| ||||
817 | return DefInit::get(D); | |||
818 | ||||
819 | PrintFatalError(CurRec->getLoc(), | |||
820 | "Undefined reference:'" + Name + "'\n"); | |||
821 | } | |||
822 | } | |||
823 | break; | |||
824 | } | |||
825 | case HEAD: { | |||
826 | if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) { | |||
827 | assert(LHSl->getSize() != 0 && "Empty list in car")((LHSl->getSize() != 0 && "Empty list in car") ? static_cast <void> (0) : __assert_fail ("LHSl->getSize() != 0 && \"Empty list in car\"" , "/tmp/buildd/llvm-toolchain-snapshot-3.6~svn224007/lib/TableGen/Record.cpp" , 827, __PRETTY_FUNCTION__)); | |||
828 | return LHSl->getElement(0); | |||
829 | } | |||
830 | break; | |||
831 | } | |||
832 | case TAIL: { | |||
833 | if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) { | |||
834 | assert(LHSl->getSize() != 0 && "Empty list in cdr")((LHSl->getSize() != 0 && "Empty list in cdr") ? static_cast <void> (0) : __assert_fail ("LHSl->getSize() != 0 && \"Empty list in cdr\"" , "/tmp/buildd/llvm-toolchain-snapshot-3.6~svn224007/lib/TableGen/Record.cpp" , 834, __PRETTY_FUNCTION__)); | |||
835 | // Note the +1. We can't just pass the result of getValues() | |||
836 | // directly. | |||
837 | ArrayRef<Init *>::iterator begin = LHSl->getValues().begin()+1; | |||
838 | ArrayRef<Init *>::iterator end = LHSl->getValues().end(); | |||
839 | ListInit *Result = | |||
840 | ListInit::get(ArrayRef<Init *>(begin, end - begin), | |||
841 | LHSl->getType()); | |||
842 | return Result; | |||
843 | } | |||
844 | break; | |||
845 | } | |||
846 | case EMPTY: { | |||
847 | if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) { | |||
848 | if (LHSl->getSize() == 0) { | |||
849 | return IntInit::get(1); | |||
850 | } else { | |||
851 | return IntInit::get(0); | |||
852 | } | |||
853 | } | |||
854 | if (StringInit *LHSs = dyn_cast<StringInit>(LHS)) { | |||
855 | if (LHSs->getValue().empty()) { | |||
856 | return IntInit::get(1); | |||
857 | } else { | |||
858 | return IntInit::get(0); | |||
859 | } | |||
860 | } | |||
861 | ||||
862 | break; | |||
863 | } | |||
864 | } | |||
865 | return const_cast<UnOpInit *>(this); | |||
866 | } | |||
867 | ||||
868 | Init *UnOpInit::resolveReferences(Record &R, const RecordVal *RV) const { | |||
869 | Init *lhs = LHS->resolveReferences(R, RV); | |||
870 | ||||
871 | if (LHS != lhs) | |||
872 | return (UnOpInit::get(getOpcode(), lhs, getType()))->Fold(&R, nullptr); | |||
873 | return Fold(&R, nullptr); | |||
874 | } | |||
875 | ||||
876 | std::string UnOpInit::getAsString() const { | |||
877 | std::string Result; | |||
878 | switch (Opc) { | |||
879 | case CAST: Result = "!cast<" + getType()->getAsString() + ">"; break; | |||
880 | case HEAD: Result = "!head"; break; | |||
881 | case TAIL: Result = "!tail"; break; | |||
882 | case EMPTY: Result = "!empty"; break; | |||
883 | } | |||
884 | return Result + "(" + LHS->getAsString() + ")"; | |||
885 | } | |||
886 | ||||
887 | BinOpInit *BinOpInit::get(BinaryOp opc, Init *lhs, | |||
888 | Init *rhs, RecTy *Type) { | |||
889 | typedef std::pair< | |||
890 | std::pair<std::pair<unsigned, Init *>, Init *>, | |||
891 | RecTy * | |||
892 | > Key; | |||
893 | ||||
894 | static Pool<DenseMap<Key, BinOpInit *> > ThePool; | |||
895 | ||||
896 | Key TheKey(std::make_pair(std::make_pair(std::make_pair(opc, lhs), rhs), | |||
897 | Type)); | |||
898 | ||||
899 | BinOpInit *&I = ThePool[TheKey]; | |||
900 | if (!I) I = new BinOpInit(opc, lhs, rhs, Type); | |||
901 | return I; | |||
902 | } | |||
903 | ||||
904 | Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const { | |||
905 | switch (getOpcode()) { | |||
906 | case CONCAT: { | |||
907 | DagInit *LHSs = dyn_cast<DagInit>(LHS); | |||
908 | DagInit *RHSs = dyn_cast<DagInit>(RHS); | |||
909 | if (LHSs && RHSs) { | |||
910 | DefInit *LOp = dyn_cast<DefInit>(LHSs->getOperator()); | |||
911 | DefInit *ROp = dyn_cast<DefInit>(RHSs->getOperator()); | |||
912 | if (!LOp || !ROp || LOp->getDef() != ROp->getDef()) | |||
913 | PrintFatalError("Concated Dag operators do not match!"); | |||
914 | std::vector<Init*> Args; | |||
915 | std::vector<std::string> ArgNames; | |||
916 | for (unsigned i = 0, e = LHSs->getNumArgs(); i != e; ++i) { | |||
917 | Args.push_back(LHSs->getArg(i)); | |||
918 | ArgNames.push_back(LHSs->getArgName(i)); | |||
919 | } | |||
920 | for (unsigned i = 0, e = RHSs->getNumArgs(); i != e; ++i) { | |||
921 | Args.push_back(RHSs->getArg(i)); | |||
922 | ArgNames.push_back(RHSs->getArgName(i)); | |||
923 | } | |||
924 | return DagInit::get(LHSs->getOperator(), "", Args, ArgNames); | |||
925 | } | |||
926 | break; | |||
927 | } | |||
928 | case LISTCONCAT: { | |||
929 | ListInit *LHSs = dyn_cast<ListInit>(LHS); | |||
930 | ListInit *RHSs = dyn_cast<ListInit>(RHS); | |||
931 | if (LHSs && RHSs) { | |||
932 | std::vector<Init *> Args; | |||
933 | Args.insert(Args.end(), LHSs->begin(), LHSs->end()); | |||
934 | Args.insert(Args.end(), RHSs->begin(), RHSs->end()); | |||
935 | return ListInit::get( | |||
936 | Args, static_cast<ListRecTy *>(LHSs->getType())->getElementType()); | |||
937 | } | |||
938 | break; | |||
939 | } | |||
940 | case STRCONCAT: { | |||
941 | StringInit *LHSs = dyn_cast<StringInit>(LHS); | |||
942 | StringInit *RHSs = dyn_cast<StringInit>(RHS); | |||
943 | if (LHSs && RHSs) | |||
944 | return StringInit::get(LHSs->getValue() + RHSs->getValue()); | |||
945 | break; | |||
946 | } | |||
947 | case EQ: { | |||
948 | // try to fold eq comparison for 'bit' and 'int', otherwise fallback | |||
949 | // to string objects. | |||
950 | IntInit *L = | |||
951 | dyn_cast_or_null<IntInit>(LHS->convertInitializerTo(IntRecTy::get())); | |||
952 | IntInit *R = | |||
953 | dyn_cast_or_null<IntInit>(RHS->convertInitializerTo(IntRecTy::get())); | |||
954 | ||||
955 | if (L && R) | |||
956 | return IntInit::get(L->getValue() == R->getValue()); | |||
957 | ||||
958 | StringInit *LHSs = dyn_cast<StringInit>(LHS); | |||
959 | StringInit *RHSs = dyn_cast<StringInit>(RHS); | |||
960 | ||||
961 | // Make sure we've resolved | |||
962 | if (LHSs && RHSs) | |||
963 | return IntInit::get(LHSs->getValue() == RHSs->getValue()); | |||
964 | ||||
965 | break; | |||
966 | } | |||
967 | case ADD: | |||
968 | case AND: | |||
969 | case SHL: | |||
970 | case SRA: | |||
971 | case SRL: { | |||
972 | IntInit *LHSi = | |||
973 | dyn_cast_or_null<IntInit>(LHS->convertInitializerTo(IntRecTy::get())); | |||
974 | IntInit *RHSi = | |||
975 | dyn_cast_or_null<IntInit>(RHS->convertInitializerTo(IntRecTy::get())); | |||
976 | if (LHSi && RHSi) { | |||
977 | int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue(); | |||
978 | int64_t Result; | |||
979 | switch (getOpcode()) { | |||
980 | default: llvm_unreachable("Bad opcode!")::llvm::llvm_unreachable_internal("Bad opcode!", "/tmp/buildd/llvm-toolchain-snapshot-3.6~svn224007/lib/TableGen/Record.cpp" , 980); | |||
981 | case ADD: Result = LHSv + RHSv; break; | |||
982 | case AND: Result = LHSv & RHSv; break; | |||
983 | case SHL: Result = LHSv << RHSv; break; | |||
984 | case SRA: Result = LHSv >> RHSv; break; | |||
985 | case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break; | |||
986 | } | |||
987 | return IntInit::get(Result); | |||
988 | } | |||
989 | break; | |||
990 | } | |||
991 | } | |||
992 | return const_cast<BinOpInit *>(this); | |||
993 | } | |||
994 | ||||
995 | Init *BinOpInit::resolveReferences(Record &R, const RecordVal *RV) const { | |||
996 | Init *lhs = LHS->resolveReferences(R, RV); | |||
997 | Init *rhs = RHS->resolveReferences(R, RV); | |||
998 | ||||
999 | if (LHS != lhs || RHS != rhs) | |||
1000 | return (BinOpInit::get(getOpcode(), lhs, rhs, getType()))->Fold(&R,nullptr); | |||
1001 | return Fold(&R, nullptr); | |||
1002 | } | |||
1003 | ||||
1004 | std::string BinOpInit::getAsString() const { | |||
1005 | std::string Result; | |||
1006 | switch (Opc) { | |||
1007 | case CONCAT: Result = "!con"; break; | |||
1008 | case ADD: Result = "!add"; break; | |||
1009 | case AND: Result = "!and"; break; | |||
1010 | case SHL: Result = "!shl"; break; | |||
1011 | case SRA: Result = "!sra"; break; | |||
1012 | case SRL: Result = "!srl"; break; | |||
1013 | case EQ: Result = "!eq"; break; | |||
1014 | case LISTCONCAT: Result = "!listconcat"; break; | |||
1015 | case STRCONCAT: Result = "!strconcat"; break; | |||
1016 | } | |||
1017 | return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")"; | |||
1018 | } | |||
1019 | ||||
1020 | TernOpInit *TernOpInit::get(TernaryOp opc, Init *lhs, | |||
1021 | Init *mhs, Init *rhs, | |||
1022 | RecTy *Type) { | |||
1023 | typedef std::pair< | |||
1024 | std::pair< | |||
1025 | std::pair<std::pair<unsigned, RecTy *>, Init *>, | |||
1026 | Init * | |||
1027 | >, | |||
1028 | Init * | |||
1029 | > Key; | |||
1030 | ||||
1031 | typedef DenseMap<Key, TernOpInit *> Pool; | |||
1032 | static Pool ThePool; | |||
1033 | ||||
1034 | Key TheKey(std::make_pair(std::make_pair(std::make_pair(std::make_pair(opc, | |||
1035 | Type), | |||
1036 | lhs), | |||
1037 | mhs), | |||
1038 | rhs)); | |||
1039 | ||||
1040 | TernOpInit *&I = ThePool[TheKey]; | |||
1041 | if (!I) I = new TernOpInit(opc, lhs, mhs, rhs, Type); | |||
1042 | return I; | |||
1043 | } | |||
1044 | ||||
1045 | static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type, | |||
1046 | Record *CurRec, MultiClass *CurMultiClass); | |||
1047 | ||||
1048 | static Init *EvaluateOperation(OpInit *RHSo, Init *LHS, Init *Arg, | |||
1049 | RecTy *Type, Record *CurRec, | |||
1050 | MultiClass *CurMultiClass) { | |||
1051 | std::vector<Init *> NewOperands; | |||
1052 | ||||
1053 | TypedInit *TArg = dyn_cast<TypedInit>(Arg); | |||
1054 | ||||
1055 | // If this is a dag, recurse | |||
1056 | if (TArg && TArg->getType()->getAsString() == "dag") { | |||
1057 | Init *Result = ForeachHelper(LHS, Arg, RHSo, Type, | |||
1058 | CurRec, CurMultiClass); | |||
1059 | return Result; | |||
1060 | } | |||
1061 | ||||
1062 | for (int i = 0; i < RHSo->getNumOperands(); ++i) { | |||
1063 | OpInit *RHSoo = dyn_cast<OpInit>(RHSo->getOperand(i)); | |||
1064 | ||||
1065 | if (RHSoo) { | |||
1066 | Init *Result = EvaluateOperation(RHSoo, LHS, Arg, | |||
1067 | Type, CurRec, CurMultiClass); | |||
1068 | if (Result) { | |||
1069 | NewOperands.push_back(Result); | |||
1070 | } else { | |||
1071 | NewOperands.push_back(Arg); | |||
1072 | } | |||
1073 | } else if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) { | |||
1074 | NewOperands.push_back(Arg); | |||
1075 | } else { | |||
1076 | NewOperands.push_back(RHSo->getOperand(i)); | |||
1077 | } | |||
1078 | } | |||
1079 | ||||
1080 | // Now run the operator and use its result as the new leaf | |||
1081 | const OpInit *NewOp = RHSo->clone(NewOperands); | |||
1082 | Init *NewVal = NewOp->Fold(CurRec, CurMultiClass); | |||
1083 | return (NewVal != NewOp) ? NewVal : nullptr; | |||
1084 | } | |||
1085 | ||||
1086 | static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type, | |||
1087 | Record *CurRec, MultiClass *CurMultiClass) { | |||
1088 | DagInit *MHSd = dyn_cast<DagInit>(MHS); | |||
1089 | ListInit *MHSl = dyn_cast<ListInit>(MHS); | |||
1090 | ||||
1091 | OpInit *RHSo = dyn_cast<OpInit>(RHS); | |||
1092 | ||||
1093 | if (!RHSo) { | |||
1094 | PrintFatalError(CurRec->getLoc(), "!foreach requires an operator\n"); | |||
1095 | } | |||
1096 | ||||
1097 | TypedInit *LHSt = dyn_cast<TypedInit>(LHS); | |||
1098 | ||||
1099 | if (!LHSt) | |||
1100 | PrintFatalError(CurRec->getLoc(), "!foreach requires typed variable\n"); | |||
1101 | ||||
1102 | if ((MHSd && isa<DagRecTy>(Type)) || (MHSl && isa<ListRecTy>(Type))) { | |||
1103 | if (MHSd) { | |||
1104 | Init *Val = MHSd->getOperator(); | |||
1105 | Init *Result = EvaluateOperation(RHSo, LHS, Val, | |||
1106 | Type, CurRec, CurMultiClass); | |||
1107 | if (Result) { | |||
1108 | Val = Result; | |||
1109 | } | |||
1110 | ||||
1111 | std::vector<std::pair<Init *, std::string> > args; | |||
1112 | for (unsigned int i = 0; i < MHSd->getNumArgs(); ++i) { | |||
1113 | Init *Arg; | |||
1114 | std::string ArgName; | |||
1115 | Arg = MHSd->getArg(i); | |||
1116 | ArgName = MHSd->getArgName(i); | |||
1117 | ||||
1118 | // Process args | |||
1119 | Init *Result = EvaluateOperation(RHSo, LHS, Arg, Type, | |||
1120 | CurRec, CurMultiClass); | |||
1121 | if (Result) { | |||
1122 | Arg = Result; | |||
1123 | } | |||
1124 | ||||
1125 | // TODO: Process arg names | |||
1126 | args.push_back(std::make_pair(Arg, ArgName)); | |||
1127 | } | |||
1128 | ||||
1129 | return DagInit::get(Val, "", args); | |||
1130 | } | |||
1131 | if (MHSl) { | |||
1132 | std::vector<Init *> NewOperands; | |||
1133 | std::vector<Init *> NewList(MHSl->begin(), MHSl->end()); | |||
1134 | ||||
1135 | for (std::vector<Init *>::iterator li = NewList.begin(), | |||
1136 | liend = NewList.end(); | |||
1137 | li != liend; | |||
1138 | ++li) { | |||
1139 | Init *Item = *li; | |||
1140 | NewOperands.clear(); | |||
1141 | for(int i = 0; i < RHSo->getNumOperands(); ++i) { | |||
1142 | // First, replace the foreach variable with the list item | |||
1143 | if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) { | |||
1144 | NewOperands.push_back(Item); | |||
1145 | } else { | |||
1146 | NewOperands.push_back(RHSo->getOperand(i)); | |||
1147 | } | |||
1148 | } | |||
1149 | ||||
1150 | // Now run the operator and use its result as the new list item | |||
1151 | const OpInit *NewOp = RHSo->clone(NewOperands); | |||
1152 | Init *NewItem = NewOp->Fold(CurRec, CurMultiClass); | |||
1153 | if (NewItem != NewOp) | |||
1154 | *li = NewItem; | |||
1155 | } | |||
1156 | return ListInit::get(NewList, MHSl->getType()); | |||
1157 | } | |||
1158 | } | |||
1159 | return nullptr; | |||
1160 | } | |||
1161 | ||||
1162 | Init *TernOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const { | |||
1163 | switch (getOpcode()) { | |||
1164 | case SUBST: { | |||
1165 | DefInit *LHSd = dyn_cast<DefInit>(LHS); | |||
1166 | VarInit *LHSv = dyn_cast<VarInit>(LHS); | |||
1167 | StringInit *LHSs = dyn_cast<StringInit>(LHS); | |||
1168 | ||||
1169 | DefInit *MHSd = dyn_cast<DefInit>(MHS); | |||
1170 | VarInit *MHSv = dyn_cast<VarInit>(MHS); | |||
1171 | StringInit *MHSs = dyn_cast<StringInit>(MHS); | |||
1172 | ||||
1173 | DefInit *RHSd = dyn_cast<DefInit>(RHS); | |||
1174 | VarInit *RHSv = dyn_cast<VarInit>(RHS); | |||
1175 | StringInit *RHSs = dyn_cast<StringInit>(RHS); | |||
1176 | ||||
1177 | if ((LHSd && MHSd && RHSd) | |||
1178 | || (LHSv && MHSv && RHSv) | |||
1179 | || (LHSs && MHSs && RHSs)) { | |||
1180 | if (RHSd) { | |||
1181 | Record *Val = RHSd->getDef(); | |||
1182 | if (LHSd->getAsString() == RHSd->getAsString()) { | |||
1183 | Val = MHSd->getDef(); | |||
1184 | } | |||
1185 | return DefInit::get(Val); | |||
1186 | } | |||
1187 | if (RHSv) { | |||
1188 | std::string Val = RHSv->getName(); | |||
1189 | if (LHSv->getAsString() == RHSv->getAsString()) { | |||
1190 | Val = MHSv->getName(); | |||
1191 | } | |||
1192 | return VarInit::get(Val, getType()); | |||
1193 | } | |||
1194 | if (RHSs) { | |||
1195 | std::string Val = RHSs->getValue(); | |||
1196 | ||||
1197 | std::string::size_type found; | |||
1198 | std::string::size_type idx = 0; | |||
1199 | do { | |||
1200 | found = Val.find(LHSs->getValue(), idx); | |||
1201 | if (found != std::string::npos) { | |||
1202 | Val.replace(found, LHSs->getValue().size(), MHSs->getValue()); | |||
1203 | } | |||
1204 | idx = found + MHSs->getValue().size(); | |||
1205 | } while (found != std::string::npos); | |||
1206 | ||||
1207 | return StringInit::get(Val); | |||
1208 | } | |||
1209 | } | |||
1210 | break; | |||
1211 | } | |||
1212 | ||||
1213 | case FOREACH: { | |||
1214 | Init *Result = ForeachHelper(LHS, MHS, RHS, getType(), | |||
1215 | CurRec, CurMultiClass); | |||
1216 | if (Result) { | |||
1217 | return Result; | |||
1218 | } | |||
1219 | break; | |||
1220 | } | |||
1221 | ||||
1222 | case IF: { | |||
1223 | IntInit *LHSi = dyn_cast<IntInit>(LHS); | |||
1224 | if (Init *I = LHS->convertInitializerTo(IntRecTy::get())) | |||
1225 | LHSi = dyn_cast<IntInit>(I); | |||
1226 | if (LHSi) { | |||
1227 | if (LHSi->getValue()) { | |||
1228 | return MHS; | |||
1229 | } else { | |||
1230 | return RHS; | |||
1231 | } | |||
1232 | } | |||
1233 | break; | |||
1234 | } | |||
1235 | } | |||
1236 | ||||
1237 | return const_cast<TernOpInit *>(this); | |||
1238 | } | |||
1239 | ||||
1240 | Init *TernOpInit::resolveReferences(Record &R, | |||
1241 | const RecordVal *RV) const { | |||
1242 | Init *lhs = LHS->resolveReferences(R, RV); | |||
1243 | ||||
1244 | if (Opc == IF && lhs != LHS) { | |||
1245 | IntInit *Value = dyn_cast<IntInit>(lhs); | |||
1246 | if (Init *I = lhs->convertInitializerTo(IntRecTy::get())) | |||
1247 | Value = dyn_cast<IntInit>(I); | |||
1248 | if (Value) { | |||
1249 | // Short-circuit | |||
1250 | if (Value->getValue()) { | |||
1251 | Init *mhs = MHS->resolveReferences(R, RV); | |||
1252 | return (TernOpInit::get(getOpcode(), lhs, mhs, | |||
1253 | RHS, getType()))->Fold(&R, nullptr); | |||
1254 | } else { | |||
1255 | Init *rhs = RHS->resolveReferences(R, RV); | |||
1256 | return (TernOpInit::get(getOpcode(), lhs, MHS, | |||
1257 | rhs, getType()))->Fold(&R, nullptr); | |||
1258 | } | |||
1259 | } | |||
1260 | } | |||
1261 | ||||
1262 | Init *mhs = MHS->resolveReferences(R, RV); | |||
1263 | Init *rhs = RHS->resolveReferences(R, RV); | |||
1264 | ||||
1265 | if (LHS != lhs || MHS != mhs || RHS != rhs) | |||
1266 | return (TernOpInit::get(getOpcode(), lhs, mhs, rhs, | |||
1267 | getType()))->Fold(&R, nullptr); | |||
1268 | return Fold(&R, nullptr); | |||
1269 | } | |||
1270 | ||||
1271 | std::string TernOpInit::getAsString() const { | |||
1272 | std::string Result; | |||
1273 | switch (Opc) { | |||
1274 | case SUBST: Result = "!subst"; break; | |||
1275 | case FOREACH: Result = "!foreach"; break; | |||
1276 | case IF: Result = "!if"; break; | |||
1277 | } | |||
1278 | return Result + "(" + LHS->getAsString() + ", " + MHS->getAsString() + ", " | |||
1279 | + RHS->getAsString() + ")"; | |||
1280 | } | |||
1281 | ||||
1282 | RecTy *TypedInit::getFieldType(const std::string &FieldName) const { | |||
1283 | if (RecordRecTy *RecordType = dyn_cast<RecordRecTy>(getType())) | |||
1284 | if (RecordVal *Field = RecordType->getRecord()->getValue(FieldName)) | |||
1285 | return Field->getType(); | |||
1286 | return nullptr; | |||
1287 | } | |||
1288 | ||||
1289 | Init * | |||
1290 | TypedInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) const { | |||
1291 | BitsRecTy *T = dyn_cast<BitsRecTy>(getType()); | |||
1292 | if (!T) return nullptr; // Cannot subscript a non-bits variable. | |||
1293 | unsigned NumBits = T->getNumBits(); | |||
1294 | ||||
1295 | SmallVector<Init *, 16> NewBits(Bits.size()); | |||
1296 | for (unsigned i = 0, e = Bits.size(); i != e; ++i) { | |||
1297 | if (Bits[i] >= NumBits) | |||
1298 | return nullptr; | |||
1299 | ||||
1300 | NewBits[i] = VarBitInit::get(const_cast<TypedInit *>(this), Bits[i]); | |||
1301 | } | |||
1302 | return BitsInit::get(NewBits); | |||
1303 | } | |||
1304 | ||||
1305 | Init * | |||
1306 | TypedInit::convertInitListSlice(const std::vector<unsigned> &Elements) const { | |||
1307 | ListRecTy *T = dyn_cast<ListRecTy>(getType()); | |||
1308 | if (!T) return nullptr; // Cannot subscript a non-list variable. | |||
1309 | ||||
1310 | if (Elements.size() == 1) | |||
1311 | return VarListElementInit::get(const_cast<TypedInit *>(this), Elements[0]); | |||
1312 | ||||
1313 | std::vector<Init*> ListInits; | |||
1314 | ListInits.reserve(Elements.size()); | |||
1315 | for (unsigned i = 0, e = Elements.size(); i != e; ++i) | |||
1316 | ListInits.push_back(VarListElementInit::get(const_cast<TypedInit *>(this), | |||
1317 | Elements[i])); | |||
1318 | return ListInit::get(ListInits, T); | |||
1319 | } | |||
1320 | ||||
1321 | ||||
1322 | VarInit *VarInit::get(const std::string &VN, RecTy *T) { | |||
1323 | Init *Value = StringInit::get(VN); | |||
1324 | return VarInit::get(Value, T); | |||
1325 | } | |||
1326 | ||||
1327 | VarInit *VarInit::get(Init *VN, RecTy *T) { | |||
1328 | typedef std::pair<RecTy *, Init *> Key; | |||
1329 | static Pool<DenseMap<Key, VarInit *> > ThePool; | |||
1330 | ||||
1331 | Key TheKey(std::make_pair(T, VN)); | |||
1332 | ||||
1333 | VarInit *&I = ThePool[TheKey]; | |||
1334 | if (!I) I = new VarInit(VN, T); | |||
1335 | return I; | |||
1336 | } | |||
1337 | ||||
1338 | const std::string &VarInit::getName() const { | |||
1339 | StringInit *NameString = dyn_cast<StringInit>(getNameInit()); | |||
1340 | assert(NameString && "VarInit name is not a string!")((NameString && "VarInit name is not a string!") ? static_cast <void> (0) : __assert_fail ("NameString && \"VarInit name is not a string!\"" , "/tmp/buildd/llvm-toolchain-snapshot-3.6~svn224007/lib/TableGen/Record.cpp" , 1340, __PRETTY_FUNCTION__)); | |||
1341 | return NameString->getValue(); | |||
1342 | } | |||
1343 | ||||
1344 | Init *VarInit::getBit(unsigned Bit) const { | |||
1345 | if (getType() == BitRecTy::get()) | |||
1346 | return const_cast<VarInit*>(this); | |||
1347 | return VarBitInit::get(const_cast<VarInit*>(this), Bit); | |||
1348 | } | |||
1349 | ||||
1350 | Init *VarInit::resolveListElementReference(Record &R, | |||
1351 | const RecordVal *IRV, | |||
1352 | unsigned Elt) const { | |||
1353 | if (R.isTemplateArg(getNameInit())) return nullptr; | |||
1354 | if (IRV && IRV->getNameInit() != getNameInit()) return nullptr; | |||
1355 | ||||
1356 | RecordVal *RV = R.getValue(getNameInit()); | |||
1357 | assert(RV && "Reference to a non-existent variable?")((RV && "Reference to a non-existent variable?") ? static_cast <void> (0) : __assert_fail ("RV && \"Reference to a non-existent variable?\"" , "/tmp/buildd/llvm-toolchain-snapshot-3.6~svn224007/lib/TableGen/Record.cpp" , 1357, __PRETTY_FUNCTION__)); | |||
1358 | ListInit *LI = dyn_cast<ListInit>(RV->getValue()); | |||
1359 | if (!LI) { | |||
1360 | TypedInit *VI = dyn_cast<TypedInit>(RV->getValue()); | |||
1361 | assert(VI && "Invalid list element!")((VI && "Invalid list element!") ? static_cast<void > (0) : __assert_fail ("VI && \"Invalid list element!\"" , "/tmp/buildd/llvm-toolchain-snapshot-3.6~svn224007/lib/TableGen/Record.cpp" , 1361, __PRETTY_FUNCTION__)); | |||
1362 | return VarListElementInit::get(VI, Elt); | |||
1363 | } | |||
1364 | ||||
1365 | if (Elt >= LI->getSize()) | |||
1366 | return nullptr; // Out of range reference. | |||
1367 | Init *E = LI->getElement(Elt); | |||
1368 | // If the element is set to some value, or if we are resolving a reference | |||
1369 | // to a specific variable and that variable is explicitly unset, then | |||
1370 | // replace the VarListElementInit with it. | |||
1371 | if (IRV || !isa<UnsetInit>(E)) | |||
1372 | return E; | |||
1373 | return nullptr; | |||
1374 | } | |||
1375 | ||||
1376 | ||||
1377 | RecTy *VarInit::getFieldType(const std::string &FieldName) const { | |||
1378 | if (RecordRecTy *RTy = dyn_cast<RecordRecTy>(getType())) | |||
1379 | if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName)) | |||
1380 | return RV->getType(); | |||
1381 | return nullptr; | |||
1382 | } | |||
1383 | ||||
1384 | Init *VarInit::getFieldInit(Record &R, const RecordVal *RV, | |||
1385 | const std::string &FieldName) const { | |||
1386 | if (isa<RecordRecTy>(getType())) | |||
1387 | if (const RecordVal *Val = R.getValue(VarName)) { | |||
1388 | if (RV != Val && (RV || isa<UnsetInit>(Val->getValue()))) | |||
1389 | return nullptr; | |||
1390 | Init *TheInit = Val->getValue(); | |||
1391 | assert(TheInit != this && "Infinite loop detected!")((TheInit != this && "Infinite loop detected!") ? static_cast <void> (0) : __assert_fail ("TheInit != this && \"Infinite loop detected!\"" , "/tmp/buildd/llvm-toolchain-snapshot-3.6~svn224007/lib/TableGen/Record.cpp" , 1391, __PRETTY_FUNCTION__)); | |||
1392 | if (Init *I = TheInit->getFieldInit(R, RV, FieldName)) | |||
1393 | return I; | |||
1394 | else | |||
1395 | return nullptr; | |||
1396 | } | |||
1397 | return nullptr; | |||
1398 | } | |||
1399 | ||||
1400 | /// resolveReferences - This method is used by classes that refer to other | |||
1401 | /// variables which may not be defined at the time the expression is formed. | |||
1402 | /// If a value is set for the variable later, this method will be called on | |||
1403 | /// users of the value to allow the value to propagate out. | |||
1404 | /// | |||
1405 | Init *VarInit::resolveReferences(Record &R, const RecordVal *RV) const { | |||
1406 | if (RecordVal *Val = R.getValue(VarName)) | |||
1407 | if (RV == Val || (!RV && !isa<UnsetInit>(Val->getValue()))) | |||
1408 | return Val->getValue(); | |||
1409 | return const_cast<VarInit *>(this); | |||
1410 | } | |||
1411 | ||||
1412 | VarBitInit *VarBitInit::get(TypedInit *T, unsigned B) { | |||
1413 | typedef std::pair<TypedInit *, unsigned> Key; | |||
1414 | typedef DenseMap<Key, VarBitInit *> Pool; | |||
1415 | ||||
1416 | static Pool ThePool; | |||
1417 | ||||
1418 | Key TheKey(std::make_pair(T, B)); | |||
1419 | ||||
1420 | VarBitInit *&I = ThePool[TheKey]; | |||
1421 | if (!I) I = new VarBitInit(T, B); | |||
1422 | return I; | |||
1423 | } | |||
1424 | ||||
1425 | std::string VarBitInit::getAsString() const { | |||
1426 | return TI->getAsString() + "{" + utostr(Bit) + "}"; | |||
1427 | } | |||
1428 | ||||
1429 | Init *VarBitInit::resolveReferences(Record &R, const RecordVal *RV) const { | |||
1430 | Init *I = TI->resolveReferences(R, RV); | |||
1431 | if (TI != I) | |||
1432 | return I->getBit(getBitNum()); | |||
1433 | ||||
1434 | return const_cast<VarBitInit*>(this); | |||
1435 | } | |||
1436 | ||||
1437 | VarListElementInit *VarListElementInit::get(TypedInit *T, | |||
1438 | unsigned E) { | |||
1439 | typedef std::pair<TypedInit *, unsigned> Key; | |||
1440 | typedef DenseMap<Key, VarListElementInit *> Pool; | |||
1441 | ||||
1442 | static Pool ThePool; | |||
1443 | ||||
1444 | Key TheKey(std::make_pair(T, E)); | |||
1445 | ||||
1446 | VarListElementInit *&I = ThePool[TheKey]; | |||
1447 | if (!I) I = new VarListElementInit(T, E); | |||
1448 | return I; | |||
1449 | } | |||
1450 | ||||
1451 | std::string VarListElementInit::getAsString() const { | |||
1452 | return TI->getAsString() + "[" + utostr(Element) + "]"; | |||
1453 | } | |||
1454 | ||||
1455 | Init * | |||
1456 | VarListElementInit::resolveReferences(Record &R, const RecordVal *RV) const { | |||
1457 | if (Init *I = getVariable()->resolveListElementReference(R, RV, | |||
1458 | getElementNum())) | |||
1459 | return I; | |||
1460 | return const_cast<VarListElementInit *>(this); | |||
1461 | } | |||
1462 | ||||
1463 | Init *VarListElementInit::getBit(unsigned Bit) const { | |||
1464 | if (getType() == BitRecTy::get()) | |||
1465 | return const_cast<VarListElementInit*>(this); | |||
1466 | return VarBitInit::get(const_cast<VarListElementInit*>(this), Bit); | |||
1467 | } | |||
1468 | ||||
1469 | Init *VarListElementInit:: resolveListElementReference(Record &R, | |||
1470 | const RecordVal *RV, | |||
1471 | unsigned Elt) const { | |||
1472 | Init *Result = TI->resolveListElementReference(R, RV, Element); | |||
1473 | ||||
1474 | if (Result) { | |||
1475 | if (TypedInit *TInit = dyn_cast<TypedInit>(Result)) { | |||
1476 | Init *Result2 = TInit->resolveListElementReference(R, RV, Elt); | |||
1477 | if (Result2) return Result2; | |||
1478 | return new VarListElementInit(TInit, Elt); | |||
1479 | } | |||
1480 | return Result; | |||
1481 | } | |||
1482 | ||||
1483 | return nullptr; | |||
1484 | } | |||
1485 | ||||
1486 | DefInit *DefInit::get(Record *R) { | |||
1487 | return R->getDefInit(); | |||
1488 | } | |||
1489 | ||||
1490 | RecTy *DefInit::getFieldType(const std::string &FieldName) const { | |||
1491 | if (const RecordVal *RV = Def->getValue(FieldName)) | |||
1492 | return RV->getType(); | |||
1493 | return nullptr; | |||
1494 | } | |||
1495 | ||||
1496 | Init *DefInit::getFieldInit(Record &R, const RecordVal *RV, | |||
1497 | const std::string &FieldName) const { | |||
1498 | return Def->getValue(FieldName)->getValue(); | |||
1499 | } | |||
1500 | ||||
1501 | ||||
1502 | std::string DefInit::getAsString() const { | |||
1503 | return Def->getName(); | |||
1504 | } | |||
1505 | ||||
1506 | FieldInit *FieldInit::get(Init *R, const std::string &FN) { | |||
1507 | typedef std::pair<Init *, TableGenStringKey> Key; | |||
1508 | typedef DenseMap<Key, FieldInit *> Pool; | |||
1509 | static Pool ThePool; | |||
1510 | ||||
1511 | Key TheKey(std::make_pair(R, FN)); | |||
1512 | ||||
1513 | FieldInit *&I = ThePool[TheKey]; | |||
1514 | if (!I) I = new FieldInit(R, FN); | |||
1515 | return I; | |||
1516 | } | |||
1517 | ||||
1518 | Init *FieldInit::getBit(unsigned Bit) const { | |||
1519 | if (getType() == BitRecTy::get()) | |||
1520 | return const_cast<FieldInit*>(this); | |||
1521 | return VarBitInit::get(const_cast<FieldInit*>(this), Bit); | |||
1522 | } | |||
1523 | ||||
1524 | Init *FieldInit::resolveListElementReference(Record &R, const RecordVal *RV, | |||
1525 | unsigned Elt) const { | |||
1526 | if (Init *ListVal = Rec->getFieldInit(R, RV, FieldName)) | |||
1527 | if (ListInit *LI = dyn_cast<ListInit>(ListVal)) { | |||
1528 | if (Elt >= LI->getSize()) return nullptr; | |||
1529 | Init *E = LI->getElement(Elt); | |||
1530 | ||||
1531 | // If the element is set to some value, or if we are resolving a | |||
1532 | // reference to a specific variable and that variable is explicitly | |||
1533 | // unset, then replace the VarListElementInit with it. | |||
1534 | if (RV || !isa<UnsetInit>(E)) | |||
1535 | return E; | |||
1536 | } | |||
1537 | return nullptr; | |||
1538 | } | |||
1539 | ||||
1540 | Init *FieldInit::resolveReferences(Record &R, const RecordVal *RV) const { | |||
1541 | Init *NewRec = RV ? Rec->resolveReferences(R, RV) : Rec; | |||
1542 | ||||
1543 | Init *BitsVal = NewRec->getFieldInit(R, RV, FieldName); | |||
1544 | if (BitsVal) { | |||
1545 | Init *BVR = BitsVal->resolveReferences(R, RV); | |||
1546 | return BVR->isComplete() ? BVR : const_cast<FieldInit *>(this); | |||
1547 | } | |||
1548 | ||||
1549 | if (NewRec != Rec) { | |||
1550 | return FieldInit::get(NewRec, FieldName); | |||
1551 | } | |||
1552 | return const_cast<FieldInit *>(this); | |||
1553 | } | |||
1554 | ||||
1555 | static void ProfileDagInit(FoldingSetNodeID &ID, Init *V, const std::string &VN, | |||
1556 | ArrayRef<Init *> ArgRange, | |||
1557 | ArrayRef<std::string> NameRange) { | |||
1558 | ID.AddPointer(V); | |||
1559 | ID.AddString(VN); | |||
1560 | ||||
1561 | ArrayRef<Init *>::iterator Arg = ArgRange.begin(); | |||
1562 | ArrayRef<std::string>::iterator Name = NameRange.begin(); | |||
1563 | while (Arg != ArgRange.end()) { | |||
1564 | assert(Name != NameRange.end() && "Arg name underflow!")((Name != NameRange.end() && "Arg name underflow!") ? static_cast<void> (0) : __assert_fail ("Name != NameRange.end() && \"Arg name underflow!\"" , "/tmp/buildd/llvm-toolchain-snapshot-3.6~svn224007/lib/TableGen/Record.cpp" , 1564, __PRETTY_FUNCTION__)); | |||
1565 | ID.AddPointer(*Arg++); | |||
1566 | ID.AddString(*Name++); | |||
1567 | } | |||
1568 | assert(Name == NameRange.end() && "Arg name overflow!")((Name == NameRange.end() && "Arg name overflow!") ? static_cast <void> (0) : __assert_fail ("Name == NameRange.end() && \"Arg name overflow!\"" , "/tmp/buildd/llvm-toolchain-snapshot-3.6~svn224007/lib/TableGen/Record.cpp" , 1568, __PRETTY_FUNCTION__)); | |||
1569 | } | |||
1570 | ||||
1571 | DagInit * | |||
1572 | DagInit::get(Init *V, const std::string &VN, | |||
1573 | ArrayRef<Init *> ArgRange, | |||
1574 | ArrayRef<std::string> NameRange) { | |||
1575 | typedef FoldingSet<DagInit> Pool; | |||
1576 | static Pool ThePool; | |||
1577 | ||||
1578 | FoldingSetNodeID ID; | |||
1579 | ProfileDagInit(ID, V, VN, ArgRange, NameRange); | |||
1580 | ||||
1581 | void *IP = nullptr; | |||
1582 | if (DagInit *I = ThePool.FindNodeOrInsertPos(ID, IP)) | |||
1583 | return I; | |||
1584 | ||||
1585 | DagInit *I = new DagInit(V, VN, ArgRange, NameRange); | |||
1586 | ThePool.InsertNode(I, IP); | |||
1587 | ||||
1588 | return I; | |||
1589 | } | |||
1590 | ||||
1591 | DagInit * | |||
1592 | DagInit::get(Init *V, const std::string &VN, | |||
1593 | const std::vector<std::pair<Init*, std::string> > &args) { | |||
1594 | typedef std::pair<Init*, std::string> PairType; | |||
1595 | ||||
1596 | std::vector<Init *> Args; | |||
1597 | std::vector<std::string> Names; | |||
1598 | ||||
1599 | for (std::vector<PairType>::const_iterator i = args.begin(), | |||
1600 | iend = args.end(); | |||
1601 | i != iend; | |||
1602 | ++i) { | |||
1603 | Args.push_back(i->first); | |||
1604 | Names.push_back(i->second); | |||
1605 | } | |||
1606 | ||||
1607 | return DagInit::get(V, VN, Args, Names); | |||
1608 | } | |||
1609 | ||||
1610 | void DagInit::Profile(FoldingSetNodeID &ID) const { | |||
1611 | ProfileDagInit(ID, Val, ValName, Args, ArgNames); | |||
1612 | } | |||
1613 | ||||
1614 | Init *DagInit::resolveReferences(Record &R, const RecordVal *RV) const { | |||
1615 | std::vector<Init*> NewArgs; | |||
1616 | for (unsigned i = 0, e = Args.size(); i != e; ++i) | |||
1617 | NewArgs.push_back(Args[i]->resolveReferences(R, RV)); | |||
1618 | ||||
1619 | Init *Op = Val->resolveReferences(R, RV); | |||
1620 | ||||
1621 | if (Args != NewArgs || Op != Val) | |||
1622 | return DagInit::get(Op, ValName, NewArgs, ArgNames); | |||
1623 | ||||
1624 | return const_cast<DagInit *>(this); | |||
1625 | } | |||
1626 | ||||
1627 | ||||
1628 | std::string DagInit::getAsString() const { | |||
1629 | std::string Result = "(" + Val->getAsString(); | |||
1630 | if (!ValName.empty()) | |||
1631 | Result += ":" + ValName; | |||
1632 | if (Args.size()) { | |||
1633 | Result += " " + Args[0]->getAsString(); | |||
1634 | if (!ArgNames[0].empty()) Result += ":$" + ArgNames[0]; | |||
1635 | for (unsigned i = 1, e = Args.size(); i != e; ++i) { | |||
1636 | Result += ", " + Args[i]->getAsString(); | |||
1637 | if (!ArgNames[i].empty()) Result += ":$" + ArgNames[i]; | |||
1638 | } | |||
1639 | } | |||
1640 | return Result + ")"; | |||
1641 | } | |||
1642 | ||||
1643 | ||||
1644 | //===----------------------------------------------------------------------===// | |||
1645 | // Other implementations | |||
1646 | //===----------------------------------------------------------------------===// | |||
1647 | ||||
1648 | RecordVal::RecordVal(Init *N, RecTy *T, unsigned P) | |||
1649 | : Name(N), Ty(T), Prefix(P) { | |||
1650 | Value = Ty->convertValue(UnsetInit::get()); | |||
1651 | assert(Value && "Cannot create unset value for current type!")((Value && "Cannot create unset value for current type!" ) ? static_cast<void> (0) : __assert_fail ("Value && \"Cannot create unset value for current type!\"" , "/tmp/buildd/llvm-toolchain-snapshot-3.6~svn224007/lib/TableGen/Record.cpp" , 1651, __PRETTY_FUNCTION__)); | |||
1652 | } | |||
1653 | ||||
1654 | RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P) | |||
1655 | : Name(StringInit::get(N)), Ty(T), Prefix(P) { | |||
1656 | Value = Ty->convertValue(UnsetInit::get()); | |||
1657 | assert(Value && "Cannot create unset value for current type!")((Value && "Cannot create unset value for current type!" ) ? static_cast<void> (0) : __assert_fail ("Value && \"Cannot create unset value for current type!\"" , "/tmp/buildd/llvm-toolchain-snapshot-3.6~svn224007/lib/TableGen/Record.cpp" , 1657, __PRETTY_FUNCTION__)); | |||
1658 | } | |||
1659 | ||||
1660 | const std::string &RecordVal::getName() const { | |||
1661 | StringInit *NameString = dyn_cast<StringInit>(Name); | |||
1662 | assert(NameString && "RecordVal name is not a string!")((NameString && "RecordVal name is not a string!") ? static_cast <void> (0) : __assert_fail ("NameString && \"RecordVal name is not a string!\"" , "/tmp/buildd/llvm-toolchain-snapshot-3.6~svn224007/lib/TableGen/Record.cpp" , 1662, __PRETTY_FUNCTION__)); | |||
1663 | return NameString->getValue(); | |||
1664 | } | |||
1665 | ||||
1666 | void RecordVal::dump() const { errs() << *this; } | |||
1667 | ||||
1668 | void RecordVal::print(raw_ostream &OS, bool PrintSem) const { | |||
1669 | if (getPrefix()) OS << "field "; | |||
1670 | OS << *getType() << " " << getNameInitAsString(); | |||
1671 | ||||
1672 | if (getValue()) | |||
1673 | OS << " = " << *getValue(); | |||
1674 | ||||
1675 | if (PrintSem) OS << ";\n"; | |||
1676 | } | |||
1677 | ||||
1678 | unsigned Record::LastID = 0; | |||
1679 | ||||
1680 | void Record::init() { | |||
1681 | checkName(); | |||
1682 | ||||
1683 | // Every record potentially has a def at the top. This value is | |||
1684 | // replaced with the top-level def name at instantiation time. | |||
1685 | RecordVal DN("NAME", StringRecTy::get(), 0); | |||
1686 | addValue(DN); | |||
1687 | } | |||
1688 | ||||
1689 | void Record::checkName() { | |||
1690 | // Ensure the record name has string type. | |||
1691 | const TypedInit *TypedName = dyn_cast<const TypedInit>(Name); | |||
1692 | assert(TypedName && "Record name is not typed!")((TypedName && "Record name is not typed!") ? static_cast <void> (0) : __assert_fail ("TypedName && \"Record name is not typed!\"" , "/tmp/buildd/llvm-toolchain-snapshot-3.6~svn224007/lib/TableGen/Record.cpp" , 1692, __PRETTY_FUNCTION__)); | |||
1693 | RecTy *Type = TypedName->getType(); | |||
1694 | if (!isa<StringRecTy>(Type)) | |||
1695 | PrintFatalError(getLoc(), "Record name is not a string!"); | |||
1696 | } | |||
1697 | ||||
1698 | DefInit *Record::getDefInit() { | |||
1699 | if (!TheInit) | |||
1700 | TheInit = new DefInit(this, new RecordRecTy(this)); | |||
1701 | return TheInit; | |||
1702 | } | |||
1703 | ||||
1704 | const std::string &Record::getName() const { | |||
1705 | const StringInit *NameString = dyn_cast<StringInit>(Name); | |||
1706 | assert(NameString && "Record name is not a string!")((NameString && "Record name is not a string!") ? static_cast <void> (0) : __assert_fail ("NameString && \"Record name is not a string!\"" , "/tmp/buildd/llvm-toolchain-snapshot-3.6~svn224007/lib/TableGen/Record.cpp" , 1706, __PRETTY_FUNCTION__)); | |||
1707 | return NameString->getValue(); | |||
1708 | } | |||
1709 | ||||
1710 | void Record::setName(Init *NewName) { | |||
1711 | Name = NewName; | |||
1712 | checkName(); | |||
1713 | // DO NOT resolve record values to the name at this point because | |||
1714 | // there might be default values for arguments of this def. Those | |||
1715 | // arguments might not have been resolved yet so we don't want to | |||
1716 | // prematurely assume values for those arguments were not passed to | |||
1717 | // this def. | |||
1718 | // | |||
1719 | // Nonetheless, it may be that some of this Record's values | |||
1720 | // reference the record name. Indeed, the reason for having the | |||
1721 | // record name be an Init is to provide this flexibility. The extra | |||
1722 | // resolve steps after completely instantiating defs takes care of | |||
1723 | // this. See TGParser::ParseDef and TGParser::ParseDefm. | |||
1724 | } | |||
1725 | ||||
1726 | void Record::setName(const std::string &Name) { | |||
1727 | setName(StringInit::get(Name)); | |||
1728 | } | |||
1729 | ||||
1730 | /// resolveReferencesTo - If anything in this record refers to RV, replace the | |||
1731 | /// reference to RV with the RHS of RV. If RV is null, we resolve all possible | |||
1732 | /// references. | |||
1733 | void Record::resolveReferencesTo(const RecordVal *RV) { | |||
1734 | for (unsigned i = 0, e = Values.size(); i != e; ++i) { | |||
1735 | if (RV == &Values[i]) // Skip resolve the same field as the given one | |||
1736 | continue; | |||
1737 | if (Init *V = Values[i].getValue()) | |||
1738 | if (Values[i].setValue(V->resolveReferences(*this, RV))) | |||
1739 | PrintFatalError(getLoc(), "Invalid value is found when setting '" | |||
1740 | + Values[i].getNameInitAsString() | |||
1741 | + "' after resolving references" | |||
1742 | + (RV ? " against '" + RV->getNameInitAsString() | |||
1743 | + "' of (" | |||
1744 | + RV->getValue()->getAsUnquotedString() + ")" | |||
1745 | : "") | |||
1746 | + "\n"); | |||
1747 | } | |||
1748 | Init *OldName = getNameInit(); | |||
1749 | Init *NewName = Name->resolveReferences(*this, RV); | |||
1750 | if (NewName != OldName) { | |||
1751 | // Re-register with RecordKeeper. | |||
1752 | setName(NewName); | |||
1753 | } | |||
1754 | } | |||
1755 | ||||
1756 | void Record::dump() const { errs() << *this; } | |||
1757 | ||||
1758 | raw_ostream &llvm::operator<<(raw_ostream &OS, const Record &R) { | |||
1759 | OS << R.getNameInitAsString(); | |||
1760 | ||||
1761 | const std::vector<Init *> &TArgs = R.getTemplateArgs(); | |||
1762 | if (!TArgs.empty()) { | |||
1763 | OS << "<"; | |||
1764 | for (unsigned i = 0, e = TArgs.size(); i != e; ++i) { | |||
1765 | if (i) OS << ", "; | |||
1766 | const RecordVal *RV = R.getValue(TArgs[i]); | |||
1767 | assert(RV && "Template argument record not found??")((RV && "Template argument record not found??") ? static_cast <void> (0) : __assert_fail ("RV && \"Template argument record not found??\"" , "/tmp/buildd/llvm-toolchain-snapshot-3.6~svn224007/lib/TableGen/Record.cpp" , 1767, __PRETTY_FUNCTION__)); | |||
1768 | RV->print(OS, false); | |||
1769 | } | |||
1770 | OS << ">"; | |||
1771 | } | |||
1772 | ||||
1773 | OS << " {"; | |||
1774 | const std::vector<Record*> &SC = R.getSuperClasses(); | |||
1775 | if (!SC.empty()) { | |||
1776 | OS << "\t//"; | |||
1777 | for (unsigned i = 0, e = SC.size(); i != e; ++i) | |||
1778 | OS << " " << SC[i]->getNameInitAsString(); | |||
1779 | } | |||
1780 | OS << "\n"; | |||
1781 | ||||
1782 | const std::vector<RecordVal> &Vals = R.getValues(); | |||
1783 | for (unsigned i = 0, e = Vals.size(); i != e; ++i) | |||
1784 | if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName())) | |||
1785 | OS << Vals[i]; | |||
1786 | for (unsigned i = 0, e = Vals.size(); i != e; ++i) | |||
1787 | if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName())) | |||
1788 | OS << Vals[i]; | |||
1789 | ||||
1790 | return OS << "}\n"; | |||
1791 | } | |||
1792 | ||||
1793 | /// getValueInit - Return the initializer for a value with the specified name, | |||
1794 | /// or abort if the field does not exist. | |||
1795 | /// | |||
1796 | Init *Record::getValueInit(StringRef FieldName) const { | |||
1797 | const RecordVal *R = getValue(FieldName); | |||
1798 | if (!R || !R->getValue()) | |||
1799 | PrintFatalError(getLoc(), "Record `" + getName() + | |||
1800 | "' does not have a field named `" + FieldName + "'!\n"); | |||
1801 | return R->getValue(); | |||
1802 | } | |||
1803 | ||||
1804 | ||||
1805 | /// getValueAsString - This method looks up the specified field and returns its | |||
1806 | /// value as a string, aborts if the field does not exist or if | |||
1807 | /// the value is not a string. | |||
1808 | /// | |||
1809 | std::string Record::getValueAsString(StringRef FieldName) const { | |||
1810 | const RecordVal *R = getValue(FieldName); | |||
1811 | if (!R || !R->getValue()) | |||
1812 | PrintFatalError(getLoc(), "Record `" + getName() + | |||
1813 | "' does not have a field named `" + FieldName + "'!\n"); | |||
1814 | ||||
1815 | if (StringInit *SI = dyn_cast<StringInit>(R->getValue())) | |||
1816 | return SI->getValue(); | |||
1817 | PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + | |||
1818 | FieldName + "' does not have a string initializer!"); | |||
1819 | } | |||
1820 | ||||
1821 | /// getValueAsBitsInit - This method looks up the specified field and returns | |||
1822 | /// its value as a BitsInit, aborts if the field does not exist or if | |||
1823 | /// the value is not the right type. | |||
1824 | /// | |||
1825 | BitsInit *Record::getValueAsBitsInit(StringRef FieldName) 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 + "'!\n"); | |||
1830 | ||||
1831 | if (BitsInit *BI = dyn_cast<BitsInit>(R->getValue())) | |||
1832 | return BI; | |||
1833 | PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + | |||
1834 | FieldName + "' does not have a BitsInit initializer!"); | |||
1835 | } | |||
1836 | ||||
1837 | /// getValueAsListInit - This method looks up the specified field and returns | |||
1838 | /// its value as a ListInit, aborting if the field does not exist or if | |||
1839 | /// the value is not the right type. | |||
1840 | /// | |||
1841 | ListInit *Record::getValueAsListInit(StringRef FieldName) const { | |||
1842 | const RecordVal *R = getValue(FieldName); | |||
1843 | if (!R || !R->getValue()) | |||
1844 | PrintFatalError(getLoc(), "Record `" + getName() + | |||
1845 | "' does not have a field named `" + FieldName + "'!\n"); | |||
1846 | ||||
1847 | if (ListInit *LI = dyn_cast<ListInit>(R->getValue())) | |||
1848 | return LI; | |||
1849 | PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + | |||
1850 | FieldName + "' does not have a list initializer!"); | |||
1851 | } | |||
1852 | ||||
1853 | /// getValueAsListOfDefs - This method looks up the specified field and returns | |||
1854 | /// its value as a vector of records, aborting if the field does not exist | |||
1855 | /// or if the value is not the right type. | |||
1856 | /// | |||
1857 | std::vector<Record*> | |||
1858 | Record::getValueAsListOfDefs(StringRef FieldName) const { | |||
1859 | ListInit *List = getValueAsListInit(FieldName); | |||
1860 | std::vector<Record*> Defs; | |||
1861 | for (unsigned i = 0; i < List->getSize(); i++) { | |||
1862 | if (DefInit *DI = dyn_cast<DefInit>(List->getElement(i))) { | |||
1863 | Defs.push_back(DI->getDef()); | |||
1864 | } else { | |||
1865 | PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + | |||
1866 | FieldName + "' list is not entirely DefInit!"); | |||
1867 | } | |||
1868 | } | |||
1869 | return Defs; | |||
1870 | } | |||
1871 | ||||
1872 | /// getValueAsInt - This method looks up the specified field and returns its | |||
1873 | /// value as an int64_t, aborting if the field does not exist or if the value | |||
1874 | /// is not the right type. | |||
1875 | /// | |||
1876 | int64_t Record::getValueAsInt(StringRef FieldName) const { | |||
1877 | const RecordVal *R = getValue(FieldName); | |||
1878 | if (!R || !R->getValue()) | |||
1879 | PrintFatalError(getLoc(), "Record `" + getName() + | |||
1880 | "' does not have a field named `" + FieldName + "'!\n"); | |||
1881 | ||||
1882 | if (IntInit *II = dyn_cast<IntInit>(R->getValue())) | |||
1883 | return II->getValue(); | |||
1884 | PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + | |||
1885 | FieldName + "' does not have an int initializer!"); | |||
1886 | } | |||
1887 | ||||
1888 | /// getValueAsListOfInts - This method looks up the specified field and returns | |||
1889 | /// its value as a vector of integers, aborting if the field does not exist or | |||
1890 | /// if the value is not the right type. | |||
1891 | /// | |||
1892 | std::vector<int64_t> | |||
1893 | Record::getValueAsListOfInts(StringRef FieldName) const { | |||
1894 | ListInit *List = getValueAsListInit(FieldName); | |||
1895 | std::vector<int64_t> Ints; | |||
1896 | for (unsigned i = 0; i < List->getSize(); i++) { | |||
1897 | if (IntInit *II = dyn_cast<IntInit>(List->getElement(i))) { | |||
1898 | Ints.push_back(II->getValue()); | |||
1899 | } else { | |||
1900 | PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + | |||
1901 | FieldName + "' does not have a list of ints initializer!"); | |||
1902 | } | |||
1903 | } | |||
1904 | return Ints; | |||
1905 | } | |||
1906 | ||||
1907 | /// getValueAsListOfStrings - This method looks up the specified field and | |||
1908 | /// returns its value as a vector of strings, aborting if the field does not | |||
1909 | /// exist or if the value is not the right type. | |||
1910 | /// | |||
1911 | std::vector<std::string> | |||
1912 | Record::getValueAsListOfStrings(StringRef FieldName) const { | |||
1913 | ListInit *List = getValueAsListInit(FieldName); | |||
1914 | std::vector<std::string> Strings; | |||
1915 | for (unsigned i = 0; i < List->getSize(); i++) { | |||
1916 | if (StringInit *II = dyn_cast<StringInit>(List->getElement(i))) { | |||
1917 | Strings.push_back(II->getValue()); | |||
1918 | } else { | |||
1919 | PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + | |||
1920 | FieldName + "' does not have a list of strings initializer!"); | |||
1921 | } | |||
1922 | } | |||
1923 | return Strings; | |||
1924 | } | |||
1925 | ||||
1926 | /// getValueAsDef - This method looks up the specified field and returns its | |||
1927 | /// value as a Record, aborting if the field does not exist or if the value | |||
1928 | /// is not the right type. | |||
1929 | /// | |||
1930 | Record *Record::getValueAsDef(StringRef FieldName) const { | |||
1931 | const RecordVal *R = getValue(FieldName); | |||
1932 | if (!R || !R->getValue()) | |||
1933 | PrintFatalError(getLoc(), "Record `" + getName() + | |||
1934 | "' does not have a field named `" + FieldName + "'!\n"); | |||
1935 | ||||
1936 | if (DefInit *DI = dyn_cast<DefInit>(R->getValue())) | |||
1937 | return DI->getDef(); | |||
1938 | PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + | |||
1939 | FieldName + "' does not have a def initializer!"); | |||
1940 | } | |||
1941 | ||||
1942 | /// getValueAsBit - This method looks up the specified field and returns its | |||
1943 | /// value as a bit, aborting if the field does not exist or if the value is | |||
1944 | /// not the right type. | |||
1945 | /// | |||
1946 | bool Record::getValueAsBit(StringRef FieldName) const { | |||
1947 | const RecordVal *R = getValue(FieldName); | |||
1948 | if (!R || !R->getValue()) | |||
1949 | PrintFatalError(getLoc(), "Record `" + getName() + | |||
1950 | "' does not have a field named `" + FieldName + "'!\n"); | |||
1951 | ||||
1952 | if (BitInit *BI = dyn_cast<BitInit>(R->getValue())) | |||
1953 | return BI->getValue(); | |||
1954 | PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + | |||
1955 | FieldName + "' does not have a bit initializer!"); | |||
1956 | } | |||
1957 | ||||
1958 | bool Record::getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const { | |||
1959 | const RecordVal *R = getValue(FieldName); | |||
1960 | if (!R || !R->getValue()) | |||
1961 | PrintFatalError(getLoc(), "Record `" + getName() + | |||
1962 | "' does not have a field named `" + FieldName.str() + "'!\n"); | |||
1963 | ||||
1964 | if (R->getValue() == UnsetInit::get()) { | |||
1965 | Unset = true; | |||
1966 | return false; | |||
1967 | } | |||
1968 | Unset = false; | |||
1969 | if (BitInit *BI = dyn_cast<BitInit>(R->getValue())) | |||
1970 | return BI->getValue(); | |||
1971 | PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + | |||
1972 | FieldName + "' does not have a bit initializer!"); | |||
1973 | } | |||
1974 | ||||
1975 | /// getValueAsDag - This method looks up the specified field and returns its | |||
1976 | /// value as an Dag, aborting if the field does not exist or if the value is | |||
1977 | /// not the right type. | |||
1978 | /// | |||
1979 | DagInit *Record::getValueAsDag(StringRef FieldName) const { | |||
1980 | const RecordVal *R = getValue(FieldName); | |||
1981 | if (!R || !R->getValue()) | |||
1982 | PrintFatalError(getLoc(), "Record `" + getName() + | |||
1983 | "' does not have a field named `" + FieldName + "'!\n"); | |||
1984 | ||||
1985 | if (DagInit *DI = dyn_cast<DagInit>(R->getValue())) | |||
1986 | return DI; | |||
1987 | PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + | |||
1988 | FieldName + "' does not have a dag initializer!"); | |||
1989 | } | |||
1990 | ||||
1991 | ||||
1992 | void MultiClass::dump() const { | |||
1993 | errs() << "Record:\n"; | |||
1994 | Rec.dump(); | |||
1995 | ||||
1996 | errs() << "Defs:\n"; | |||
1997 | for (RecordVector::const_iterator r = DefPrototypes.begin(), | |||
1998 | rend = DefPrototypes.end(); | |||
1999 | r != rend; | |||
2000 | ++r) { | |||
2001 | (*r)->dump(); | |||
2002 | } | |||
2003 | } | |||
2004 | ||||
2005 | ||||
2006 | void RecordKeeper::dump() const { errs() << *this; } | |||
2007 | ||||
2008 | raw_ostream &llvm::operator<<(raw_ostream &OS, const RecordKeeper &RK) { | |||
2009 | OS << "------------- Classes -----------------\n"; | |||
2010 | const auto &Classes = RK.getClasses(); | |||
2011 | for (const auto &C : Classes) | |||
2012 | OS << "class " << *C.second; | |||
2013 | ||||
2014 | OS << "------------- Defs -----------------\n"; | |||
2015 | const auto &Defs = RK.getDefs(); | |||
2016 | for (const auto &D : Defs) | |||
2017 | OS << "def " << *D.second; | |||
2018 | return OS; | |||
2019 | } | |||
2020 | ||||
2021 | ||||
2022 | /// getAllDerivedDefinitions - This method returns all concrete definitions | |||
2023 | /// that derive from the specified class name. If a class with the specified | |||
2024 | /// name does not exist, an error is printed and true is returned. | |||
2025 | std::vector<Record*> | |||
2026 | RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const { | |||
2027 | Record *Class = getClass(ClassName); | |||
2028 | if (!Class) | |||
2029 | PrintFatalError("ERROR: Couldn't find the `" + ClassName + "' class!\n"); | |||
2030 | ||||
2031 | std::vector<Record*> Defs; | |||
2032 | for (const auto &D : getDefs()) | |||
2033 | if (D.second->isSubClassOf(Class)) | |||
2034 | Defs.push_back(D.second.get()); | |||
2035 | ||||
2036 | return Defs; | |||
2037 | } | |||
2038 | ||||
2039 | /// QualifyName - Return an Init with a qualifier prefix referring | |||
2040 | /// to CurRec's name. | |||
2041 | Init *llvm::QualifyName(Record &CurRec, MultiClass *CurMultiClass, | |||
2042 | Init *Name, const std::string &Scoper) { | |||
2043 | RecTy *Type = dyn_cast<TypedInit>(Name)->getType(); | |||
2044 | ||||
2045 | BinOpInit *NewName = | |||
2046 | BinOpInit::get(BinOpInit::STRCONCAT, | |||
2047 | BinOpInit::get(BinOpInit::STRCONCAT, | |||
2048 | CurRec.getNameInit(), | |||
2049 | StringInit::get(Scoper), | |||
2050 | Type)->Fold(&CurRec, CurMultiClass), | |||
2051 | Name, | |||
2052 | Type); | |||
2053 | ||||
2054 | if (CurMultiClass && Scoper != "::") { | |||
2055 | NewName = | |||
2056 | BinOpInit::get(BinOpInit::STRCONCAT, | |||
2057 | BinOpInit::get(BinOpInit::STRCONCAT, | |||
2058 | CurMultiClass->Rec.getNameInit(), | |||
2059 | StringInit::get("::"), | |||
2060 | Type)->Fold(&CurRec, CurMultiClass), | |||
2061 | NewName->Fold(&CurRec, CurMultiClass), | |||
2062 | Type); | |||
2063 | } | |||
2064 | ||||
2065 | return NewName->Fold(&CurRec, CurMultiClass); | |||
2066 | } | |||
2067 | ||||
2068 | /// QualifyName - Return an Init with a qualifier prefix referring | |||
2069 | /// to CurRec's name. | |||
2070 | Init *llvm::QualifyName(Record &CurRec, MultiClass *CurMultiClass, | |||
2071 | const std::string &Name, | |||
2072 | const std::string &Scoper) { | |||
2073 | return QualifyName(CurRec, CurMultiClass, StringInit::get(Name), Scoper); | |||
2074 | } |