blob: dae0c391be6121486476562c0ee15f65c7d4e8fc [file] [log] [blame]
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001//===- Record.cpp - Record implementation ---------------------------------===//
Misha Brukman650ba8e2005-04-22 00:00:37 +00002//
John Criswelld3032032003-10-20 20:20:30 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner8adcd9f2007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman650ba8e2005-04-22 00:00:37 +00007//
John Criswelld3032032003-10-20 20:20:30 +00008//===----------------------------------------------------------------------===//
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00009//
Chris Lattner51ffbf12006-03-31 21:53:49 +000010// Implement the tablegen record classes.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "Record.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000015#include "llvm/Support/DataTypes.h"
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000016#include "llvm/Support/Format.h"
Chris Lattner8b9ecda2007-11-20 22:25:16 +000017#include "llvm/ADT/StringExtras.h"
Duraid Madina14492af2005-12-26 05:08:55 +000018
Chris Lattner68478662004-08-01 03:55:39 +000019using namespace llvm;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000020
21//===----------------------------------------------------------------------===//
22// Type implementations
23//===----------------------------------------------------------------------===//
24
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000025void RecTy::dump() const { print(errs()); }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000026
27Init *BitRecTy::convertValue(BitsInit *BI) {
28 if (BI->getNumBits() != 1) return 0; // Only accept if just one bit!
29 return BI->getBit(0);
30}
31
32bool BitRecTy::baseClassOf(const BitsRecTy *RHS) const {
33 return RHS->getNumBits() == 1;
34}
35
36Init *BitRecTy::convertValue(IntInit *II) {
Dan Gohmanca0546f2008-10-17 01:33:43 +000037 int64_t Val = II->getValue();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000038 if (Val != 0 && Val != 1) return 0; // Only accept 0 or 1 for a bit!
Misha Brukman650ba8e2005-04-22 00:00:37 +000039
40 return new BitInit(Val != 0);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000041}
42
43Init *BitRecTy::convertValue(TypedInit *VI) {
44 if (dynamic_cast<BitRecTy*>(VI->getType()))
45 return VI; // Accept variable if it is already of bit type!
46 return 0;
47}
48
Chris Lattner8b9ecda2007-11-20 22:25:16 +000049std::string BitsRecTy::getAsString() const {
50 return "bits<" + utostr(Size) + ">";
51}
52
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000053Init *BitsRecTy::convertValue(UnsetInit *UI) {
54 BitsInit *Ret = new BitsInit(Size);
55
56 for (unsigned i = 0; i != Size; ++i)
57 Ret->setBit(i, new UnsetInit());
58 return Ret;
59}
60
61Init *BitsRecTy::convertValue(BitInit *UI) {
62 if (Size != 1) return 0; // Can only convert single bit...
63 BitsInit *Ret = new BitsInit(1);
64 Ret->setBit(0, UI);
65 return Ret;
66}
67
68// convertValue from Int initializer to bits type: Split the integer up into the
69// appropriate bits...
70//
71Init *BitsRecTy::convertValue(IntInit *II) {
Misha Brukman6752fb52004-06-21 18:01:47 +000072 int64_t Value = II->getValue();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000073 // Make sure this bitfield is large enough to hold the integer value...
74 if (Value >= 0) {
Misha Brukman6752fb52004-06-21 18:01:47 +000075 if (Value & ~((1LL << Size)-1))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000076 return 0;
77 } else {
Jeff Cohen0add83e2006-02-18 03:20:33 +000078 if ((Value >> Size) != -1 || ((Value & (1LL << (Size-1))) == 0))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000079 return 0;
80 }
81
82 BitsInit *Ret = new BitsInit(Size);
83 for (unsigned i = 0; i != Size; ++i)
Jeff Cohen0add83e2006-02-18 03:20:33 +000084 Ret->setBit(i, new BitInit(Value & (1LL << i)));
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000085
86 return Ret;
87}
88
89Init *BitsRecTy::convertValue(BitsInit *BI) {
90 // If the number of bits is right, return it. Otherwise we need to expand or
91 // truncate...
92 if (BI->getNumBits() == Size) return BI;
93 return 0;
94}
95
96Init *BitsRecTy::convertValue(TypedInit *VI) {
97 if (BitsRecTy *BRT = dynamic_cast<BitsRecTy*>(VI->getType()))
98 if (BRT->Size == Size) {
99 BitsInit *Ret = new BitsInit(Size);
100 for (unsigned i = 0; i != Size; ++i)
Jeff Cohen88e7b722005-04-22 04:13:13 +0000101 Ret->setBit(i, new VarBitInit(VI, i));
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000102 return Ret;
103 }
104 if (Size == 1 && dynamic_cast<BitRecTy*>(VI->getType())) {
105 BitsInit *Ret = new BitsInit(1);
106 Ret->setBit(0, VI);
107 return Ret;
108 }
Misha Brukman650ba8e2005-04-22 00:00:37 +0000109
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000110 return 0;
111}
112
113Init *IntRecTy::convertValue(BitInit *BI) {
114 return new IntInit(BI->getValue());
115}
116
117Init *IntRecTy::convertValue(BitsInit *BI) {
Dan Gohmanca0546f2008-10-17 01:33:43 +0000118 int64_t Result = 0;
Misha Brukman650ba8e2005-04-22 00:00:37 +0000119 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000120 if (BitInit *Bit = dynamic_cast<BitInit*>(BI->getBit(i))) {
121 Result |= Bit->getValue() << i;
122 } else {
123 return 0;
124 }
125 return new IntInit(Result);
126}
127
128Init *IntRecTy::convertValue(TypedInit *TI) {
129 if (TI->getType()->typeIsConvertibleTo(this))
130 return TI; // Accept variable if already of the right type!
131 return 0;
132}
133
David Greenee8f3b272009-05-14 21:22:49 +0000134Init *StringRecTy::convertValue(UnOpInit *BO) {
135 if (BO->getOpcode() == UnOpInit::CAST) {
136 Init *L = BO->getOperand()->convertInitializerTo(this);
137 if (L == 0) return 0;
138 if (L != BO->getOperand())
139 return new UnOpInit(UnOpInit::CAST, L, new StringRecTy);
140 return BO;
141 }
David Greene5d0c0512009-05-14 20:54:48 +0000142
David Greenee8f3b272009-05-14 21:22:49 +0000143 return convertValue((TypedInit*)BO);
144}
David Greene5d0c0512009-05-14 20:54:48 +0000145
Chris Lattner51ffbf12006-03-31 21:53:49 +0000146Init *StringRecTy::convertValue(BinOpInit *BO) {
147 if (BO->getOpcode() == BinOpInit::STRCONCAT) {
148 Init *L = BO->getLHS()->convertInitializerTo(this);
149 Init *R = BO->getRHS()->convertInitializerTo(this);
150 if (L == 0 || R == 0) return 0;
151 if (L != BO->getLHS() || R != BO->getRHS())
David Greene196ac3c2009-04-23 21:25:15 +0000152 return new BinOpInit(BinOpInit::STRCONCAT, L, R, new StringRecTy);
Chris Lattner51ffbf12006-03-31 21:53:49 +0000153 return BO;
154 }
David Greene196ac3c2009-04-23 21:25:15 +0000155
156 return convertValue((TypedInit*)BO);
Chris Lattner51ffbf12006-03-31 21:53:49 +0000157}
158
159
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000160Init *StringRecTy::convertValue(TypedInit *TI) {
161 if (dynamic_cast<StringRecTy*>(TI->getType()))
162 return TI; // Accept variable if already of the right type!
163 return 0;
164}
165
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000166std::string ListRecTy::getAsString() const {
167 return "list<" + Ty->getAsString() + ">";
168}
169
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000170Init *ListRecTy::convertValue(ListInit *LI) {
171 std::vector<Init*> Elements;
172
173 // Verify that all of the elements of the list are subclasses of the
174 // appropriate class!
175 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
176 if (Init *CI = LI->getElement(i)->convertInitializerTo(Ty))
177 Elements.push_back(CI);
178 else
179 return 0;
180
David Greene8618f952009-06-08 20:23:18 +0000181 ListRecTy *LType = dynamic_cast<ListRecTy*>(LI->getType());
182 if (LType == 0) {
183 return 0;
184 }
185
186 return new ListInit(Elements, new ListRecTy(Ty));
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000187}
188
189Init *ListRecTy::convertValue(TypedInit *TI) {
190 // Ensure that TI is compatible with our class.
191 if (ListRecTy *LRT = dynamic_cast<ListRecTy*>(TI->getType()))
192 if (LRT->getElementType()->typeIsConvertibleTo(getElementType()))
193 return TI;
194 return 0;
195}
196
197Init *CodeRecTy::convertValue(TypedInit *TI) {
198 if (TI->getType()->typeIsConvertibleTo(this))
199 return TI;
200 return 0;
201}
202
203Init *DagRecTy::convertValue(TypedInit *TI) {
204 if (TI->getType()->typeIsConvertibleTo(this))
205 return TI;
206 return 0;
207}
208
David Greenee8f3b272009-05-14 21:22:49 +0000209Init *DagRecTy::convertValue(UnOpInit *BO) {
210 if (BO->getOpcode() == UnOpInit::CAST) {
211 Init *L = BO->getOperand()->convertInitializerTo(this);
212 if (L == 0) return 0;
213 if (L != BO->getOperand())
214 return new UnOpInit(UnOpInit::CAST, L, new DagRecTy);
215 return BO;
216 }
217 return 0;
218}
David Greene5d0c0512009-05-14 20:54:48 +0000219
Evan Chenga32dee22007-05-15 01:23:24 +0000220Init *DagRecTy::convertValue(BinOpInit *BO) {
221 if (BO->getOpcode() == BinOpInit::CONCAT) {
222 Init *L = BO->getLHS()->convertInitializerTo(this);
223 Init *R = BO->getRHS()->convertInitializerTo(this);
224 if (L == 0 || R == 0) return 0;
225 if (L != BO->getLHS() || R != BO->getRHS())
David Greene196ac3c2009-04-23 21:25:15 +0000226 return new BinOpInit(BinOpInit::CONCAT, L, R, new DagRecTy);
Evan Chenga32dee22007-05-15 01:23:24 +0000227 return BO;
228 }
229 return 0;
230}
231
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000232std::string RecordRecTy::getAsString() const {
233 return Rec->getName();
234}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000235
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000236Init *RecordRecTy::convertValue(DefInit *DI) {
237 // Ensure that DI is a subclass of Rec.
238 if (!DI->getDef()->isSubClassOf(Rec))
239 return 0;
240 return DI;
241}
242
243Init *RecordRecTy::convertValue(TypedInit *TI) {
244 // Ensure that TI is compatible with Rec.
245 if (RecordRecTy *RRT = dynamic_cast<RecordRecTy*>(TI->getType()))
246 if (RRT->getRecord()->isSubClassOf(getRecord()) ||
247 RRT->getRecord() == getRecord())
248 return TI;
249 return 0;
250}
251
252bool RecordRecTy::baseClassOf(const RecordRecTy *RHS) const {
Bruno Cardoso Lopesdeb20022010-06-17 23:00:16 +0000253 if (Rec == RHS->getRecord() || RHS->getRecord()->isSubClassOf(Rec))
254 return true;
255
256 const std::vector<Record*> &SC = Rec->getSuperClasses();
257 for (unsigned i = 0, e = SC.size(); i != e; ++i)
258 if (RHS->getRecord()->isSubClassOf(SC[i]))
259 return true;
260
261 return false;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000262}
263
264
Bob Wilson7248f862009-11-22 04:24:42 +0000265/// resolveTypes - Find a common type that T1 and T2 convert to.
David Greene8618f952009-06-08 20:23:18 +0000266/// Return 0 if no such type exists.
267///
268RecTy *llvm::resolveTypes(RecTy *T1, RecTy *T2) {
269 if (!T1->typeIsConvertibleTo(T2)) {
270 if (!T2->typeIsConvertibleTo(T1)) {
271 // If one is a Record type, check superclasses
272 RecordRecTy *RecTy1 = dynamic_cast<RecordRecTy*>(T1);
273 if (RecTy1) {
274 // See if T2 inherits from a type T1 also inherits from
Bob Wilson7248f862009-11-22 04:24:42 +0000275 const std::vector<Record *> &T1SuperClasses =
276 RecTy1->getRecord()->getSuperClasses();
David Greene8618f952009-06-08 20:23:18 +0000277 for(std::vector<Record *>::const_iterator i = T1SuperClasses.begin(),
278 iend = T1SuperClasses.end();
279 i != iend;
280 ++i) {
281 RecordRecTy *SuperRecTy1 = new RecordRecTy(*i);
282 RecTy *NewType1 = resolveTypes(SuperRecTy1, T2);
283 if (NewType1 != 0) {
284 if (NewType1 != SuperRecTy1) {
285 delete SuperRecTy1;
286 }
287 return NewType1;
288 }
289 }
290 }
291 RecordRecTy *RecTy2 = dynamic_cast<RecordRecTy*>(T2);
292 if (RecTy2) {
293 // See if T1 inherits from a type T2 also inherits from
Bob Wilson7248f862009-11-22 04:24:42 +0000294 const std::vector<Record *> &T2SuperClasses =
295 RecTy2->getRecord()->getSuperClasses();
296 for (std::vector<Record *>::const_iterator i = T2SuperClasses.begin(),
David Greene8618f952009-06-08 20:23:18 +0000297 iend = T2SuperClasses.end();
298 i != iend;
299 ++i) {
300 RecordRecTy *SuperRecTy2 = new RecordRecTy(*i);
301 RecTy *NewType2 = resolveTypes(T1, SuperRecTy2);
302 if (NewType2 != 0) {
303 if (NewType2 != SuperRecTy2) {
304 delete SuperRecTy2;
305 }
306 return NewType2;
307 }
308 }
309 }
310 return 0;
311 }
312 return T2;
313 }
314 return T1;
315}
316
317
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000318//===----------------------------------------------------------------------===//
319// Initializer implementations
320//===----------------------------------------------------------------------===//
321
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000322void Init::dump() const { return print(errs()); }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000323
324Init *BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
325 BitsInit *BI = new BitsInit(Bits.size());
326 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
327 if (Bits[i] >= getNumBits()) {
328 delete BI;
329 return 0;
330 }
331 BI->setBit(i, getBit(Bits[i]));
332 }
333 return BI;
334}
335
Chris Lattner695506c2007-11-22 21:05:25 +0000336std::string BitsInit::getAsString() const {
Chris Lattner695506c2007-11-22 21:05:25 +0000337 std::string Result = "{ ";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000338 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
Chris Lattner695506c2007-11-22 21:05:25 +0000339 if (i) Result += ", ";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000340 if (Init *Bit = getBit(e-i-1))
Chris Lattner695506c2007-11-22 21:05:25 +0000341 Result += Bit->getAsString();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000342 else
Chris Lattner695506c2007-11-22 21:05:25 +0000343 Result += "*";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000344 }
Chris Lattner695506c2007-11-22 21:05:25 +0000345 return Result + " }";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000346}
347
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000348// resolveReferences - If there are any field references that refer to fields
349// that have been filled in, we can propagate the values now.
350//
Chris Lattneref943742005-04-19 03:36:21 +0000351Init *BitsInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000352 bool Changed = false;
353 BitsInit *New = new BitsInit(getNumBits());
354
355 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
356 Init *B;
357 Init *CurBit = getBit(i);
358
359 do {
360 B = CurBit;
Chris Lattneref943742005-04-19 03:36:21 +0000361 CurBit = CurBit->resolveReferences(R, RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000362 Changed |= B != CurBit;
363 } while (B != CurBit);
364 New->setBit(i, CurBit);
365 }
366
367 if (Changed)
368 return New;
369 delete New;
370 return this;
371}
372
Chris Lattner695506c2007-11-22 21:05:25 +0000373std::string IntInit::getAsString() const {
374 return itostr(Value);
375}
376
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000377Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
378 BitsInit *BI = new BitsInit(Bits.size());
379
380 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
Dan Gohmanca0546f2008-10-17 01:33:43 +0000381 if (Bits[i] >= 64) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000382 delete BI;
383 return 0;
384 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000385 BI->setBit(i, new BitInit(Value & (INT64_C(1) << Bits[i])));
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000386 }
387 return BI;
388}
389
Chris Lattner8bf9e062004-07-26 23:21:34 +0000390Init *ListInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
391 std::vector<Init*> Vals;
392 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
393 if (Elements[i] >= getSize())
394 return 0;
395 Vals.push_back(getElement(Elements[i]));
396 }
David Greene8618f952009-06-08 20:23:18 +0000397 return new ListInit(Vals, getType());
Chris Lattner8bf9e062004-07-26 23:21:34 +0000398}
399
Chris Lattnercbebe462007-02-27 22:08:27 +0000400Record *ListInit::getElementAsRecord(unsigned i) const {
401 assert(i < Values.size() && "List element index out of range!");
402 DefInit *DI = dynamic_cast<DefInit*>(Values[i]);
403 if (DI == 0) throw "Expected record in list!";
404 return DI->getDef();
405}
406
Chris Lattneref943742005-04-19 03:36:21 +0000407Init *ListInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattner577fc3f2004-07-27 01:01:21 +0000408 std::vector<Init*> Resolved;
409 Resolved.reserve(getSize());
410 bool Changed = false;
411
412 for (unsigned i = 0, e = getSize(); i != e; ++i) {
413 Init *E;
414 Init *CurElt = getElement(i);
415
416 do {
417 E = CurElt;
Chris Lattneref943742005-04-19 03:36:21 +0000418 CurElt = CurElt->resolveReferences(R, RV);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000419 Changed |= E != CurElt;
420 } while (E != CurElt);
421 Resolved.push_back(E);
422 }
423
424 if (Changed)
David Greene8618f952009-06-08 20:23:18 +0000425 return new ListInit(Resolved, getType());
Chris Lattner577fc3f2004-07-27 01:01:21 +0000426 return this;
427}
428
David Greene8618f952009-06-08 20:23:18 +0000429Init *ListInit::resolveListElementReference(Record &R, const RecordVal *IRV,
Bob Wilson7248f862009-11-22 04:24:42 +0000430 unsigned Elt) {
David Greene8618f952009-06-08 20:23:18 +0000431 if (Elt >= getSize())
432 return 0; // Out of range reference.
433 Init *E = getElement(Elt);
Bob Wilson67e6cab2009-11-22 03:58:57 +0000434 // If the element is set to some value, or if we are resolving a reference
435 // to a specific variable and that variable is explicitly unset, then
436 // replace the VarListElementInit with it.
437 if (IRV || !dynamic_cast<UnsetInit*>(E))
438 return E;
David Greene8618f952009-06-08 20:23:18 +0000439 return 0;
440}
441
Chris Lattner695506c2007-11-22 21:05:25 +0000442std::string ListInit::getAsString() const {
443 std::string Result = "[";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000444 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
Chris Lattner695506c2007-11-22 21:05:25 +0000445 if (i) Result += ", ";
446 Result += Values[i]->getAsString();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000447 }
Chris Lattner695506c2007-11-22 21:05:25 +0000448 return Result + "]";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000449}
450
David Greene5d0c0512009-05-14 20:54:48 +0000451Init *OpInit::resolveBitReference(Record &R, const RecordVal *IRV,
Bob Wilson7248f862009-11-22 04:24:42 +0000452 unsigned Bit) {
David Greene5d0c0512009-05-14 20:54:48 +0000453 Init *Folded = Fold(&R, 0);
454
455 if (Folded != this) {
456 TypedInit *Typed = dynamic_cast<TypedInit *>(Folded);
457 if (Typed) {
458 return Typed->resolveBitReference(R, IRV, Bit);
Bob Wilson7248f862009-11-22 04:24:42 +0000459 }
David Greene5d0c0512009-05-14 20:54:48 +0000460 }
Bob Wilson7248f862009-11-22 04:24:42 +0000461
David Greene5d0c0512009-05-14 20:54:48 +0000462 return 0;
463}
464
465Init *OpInit::resolveListElementReference(Record &R, const RecordVal *IRV,
Bob Wilson7248f862009-11-22 04:24:42 +0000466 unsigned Elt) {
David Greene5d0c0512009-05-14 20:54:48 +0000467 Init *Folded = Fold(&R, 0);
468
469 if (Folded != this) {
470 TypedInit *Typed = dynamic_cast<TypedInit *>(Folded);
471 if (Typed) {
472 return Typed->resolveListElementReference(R, IRV, Elt);
Bob Wilson7248f862009-11-22 04:24:42 +0000473 }
David Greene5d0c0512009-05-14 20:54:48 +0000474 }
Bob Wilson7248f862009-11-22 04:24:42 +0000475
David Greene5d0c0512009-05-14 20:54:48 +0000476 return 0;
477}
478
David Greenee8f3b272009-05-14 21:22:49 +0000479Init *UnOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
480 switch (getOpcode()) {
481 default: assert(0 && "Unknown unop");
482 case CAST: {
David Greeneefa19612009-06-29 20:05:29 +0000483 if (getType()->getAsString() == "string") {
484 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
485 if (LHSs) {
486 return LHSs;
487 }
David Greene5d0c0512009-05-14 20:54:48 +0000488
David Greeneefa19612009-06-29 20:05:29 +0000489 DefInit *LHSd = dynamic_cast<DefInit*>(LHS);
490 if (LHSd) {
491 return new StringInit(LHSd->getDef()->getName());
492 }
Bob Wilson7248f862009-11-22 04:24:42 +0000493 } else {
David Greeneefa19612009-06-29 20:05:29 +0000494 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
495 if (LHSs) {
496 std::string Name = LHSs->getValue();
497
498 // From TGParser::ParseIDValue
499 if (CurRec) {
500 if (const RecordVal *RV = CurRec->getValue(Name)) {
Chris Lattner94026332010-10-06 00:19:21 +0000501 if (RV->getType() != getType())
502 throw "type mismatch in cast";
David Greeneefa19612009-06-29 20:05:29 +0000503 return new VarInit(Name, RV->getType());
David Greenee8f3b272009-05-14 21:22:49 +0000504 }
David Greeneefa19612009-06-29 20:05:29 +0000505
506 std::string TemplateArgName = CurRec->getName()+":"+Name;
507 if (CurRec->isTemplateArg(TemplateArgName)) {
508 const RecordVal *RV = CurRec->getValue(TemplateArgName);
509 assert(RV && "Template arg doesn't exist??");
510
Chris Lattner94026332010-10-06 00:19:21 +0000511 if (RV->getType() != getType())
512 throw "type mismatch in cast";
David Greeneefa19612009-06-29 20:05:29 +0000513
514 return new VarInit(TemplateArgName, RV->getType());
515 }
516 }
517
518 if (CurMultiClass) {
519 std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
520 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
521 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
522 assert(RV && "Template arg doesn't exist??");
Bob Wilson7248f862009-11-22 04:24:42 +0000523
Chris Lattner94026332010-10-06 00:19:21 +0000524 if (RV->getType() != getType())
525 throw "type mismatch in cast";
Bob Wilson7248f862009-11-22 04:24:42 +0000526
David Greeneefa19612009-06-29 20:05:29 +0000527 return new VarInit(MCName, RV->getType());
528 }
David Greenee8f3b272009-05-14 21:22:49 +0000529 }
Bob Wilson7248f862009-11-22 04:24:42 +0000530
David Greeneefa19612009-06-29 20:05:29 +0000531 if (Record *D = Records.getDef(Name))
532 return new DefInit(D);
David Greene5d0c0512009-05-14 20:54:48 +0000533
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000534 errs() << "Variable not defined: '" + Name + "'\n";
David Greeneefa19612009-06-29 20:05:29 +0000535 assert(0 && "Variable not found");
536 return 0;
David Greenee8f3b272009-05-14 21:22:49 +0000537 }
David Greenee8f3b272009-05-14 21:22:49 +0000538 }
539 break;
540 }
David Greened571b3c2009-05-14 22:38:31 +0000541 case CAR: {
542 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
543 if (LHSl) {
544 if (LHSl->getSize() == 0) {
545 assert(0 && "Empty list in car");
546 return 0;
547 }
548 return LHSl->getElement(0);
549 }
550 break;
551 }
552 case CDR: {
553 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
554 if (LHSl) {
555 if (LHSl->getSize() == 0) {
556 assert(0 && "Empty list in cdr");
557 return 0;
558 }
Bob Wilson7248f862009-11-22 04:24:42 +0000559 ListInit *Result = new ListInit(LHSl->begin()+1, LHSl->end(),
560 LHSl->getType());
David Greened571b3c2009-05-14 22:38:31 +0000561 return Result;
562 }
563 break;
564 }
565 case LNULL: {
566 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
567 if (LHSl) {
568 if (LHSl->getSize() == 0) {
569 return new IntInit(1);
Bob Wilson7248f862009-11-22 04:24:42 +0000570 } else {
David Greened571b3c2009-05-14 22:38:31 +0000571 return new IntInit(0);
572 }
573 }
David Greene8618f952009-06-08 20:23:18 +0000574 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
575 if (LHSs) {
576 if (LHSs->getValue().empty()) {
577 return new IntInit(1);
Bob Wilson7248f862009-11-22 04:24:42 +0000578 } else {
David Greene8618f952009-06-08 20:23:18 +0000579 return new IntInit(0);
580 }
581 }
Bob Wilson7248f862009-11-22 04:24:42 +0000582
David Greened571b3c2009-05-14 22:38:31 +0000583 break;
584 }
David Greenee8f3b272009-05-14 21:22:49 +0000585 }
586 return this;
587}
David Greene5d0c0512009-05-14 20:54:48 +0000588
David Greenee8f3b272009-05-14 21:22:49 +0000589Init *UnOpInit::resolveReferences(Record &R, const RecordVal *RV) {
590 Init *lhs = LHS->resolveReferences(R, RV);
Bob Wilson7248f862009-11-22 04:24:42 +0000591
David Greenee8f3b272009-05-14 21:22:49 +0000592 if (LHS != lhs)
593 return (new UnOpInit(getOpcode(), lhs, getType()))->Fold(&R, 0);
594 return Fold(&R, 0);
595}
David Greene5d0c0512009-05-14 20:54:48 +0000596
David Greenee8f3b272009-05-14 21:22:49 +0000597std::string UnOpInit::getAsString() const {
598 std::string Result;
599 switch (Opc) {
600 case CAST: Result = "!cast<" + getType()->getAsString() + ">"; break;
David Greened571b3c2009-05-14 22:38:31 +0000601 case CAR: Result = "!car"; break;
602 case CDR: Result = "!cdr"; break;
603 case LNULL: Result = "!null"; break;
David Greenee8f3b272009-05-14 21:22:49 +0000604 }
605 return Result + "(" + LHS->getAsString() + ")";
606}
David Greene5d0c0512009-05-14 20:54:48 +0000607
David Greenea9c6c5d2009-04-22 20:18:10 +0000608Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
Chris Lattner51ffbf12006-03-31 21:53:49 +0000609 switch (getOpcode()) {
610 default: assert(0 && "Unknown binop");
Evan Chenga32dee22007-05-15 01:23:24 +0000611 case CONCAT: {
612 DagInit *LHSs = dynamic_cast<DagInit*>(LHS);
613 DagInit *RHSs = dynamic_cast<DagInit*>(RHS);
614 if (LHSs && RHSs) {
615 DefInit *LOp = dynamic_cast<DefInit*>(LHSs->getOperator());
616 DefInit *ROp = dynamic_cast<DefInit*>(RHSs->getOperator());
Chris Lattner81fd1f22010-03-18 21:07:51 +0000617 if (LOp == 0 || ROp == 0 || LOp->getDef() != ROp->getDef())
618 throw "Concated Dag operators do not match!";
Evan Chenga32dee22007-05-15 01:23:24 +0000619 std::vector<Init*> Args;
620 std::vector<std::string> ArgNames;
621 for (unsigned i = 0, e = LHSs->getNumArgs(); i != e; ++i) {
622 Args.push_back(LHSs->getArg(i));
623 ArgNames.push_back(LHSs->getArgName(i));
624 }
625 for (unsigned i = 0, e = RHSs->getNumArgs(); i != e; ++i) {
626 Args.push_back(RHSs->getArg(i));
627 ArgNames.push_back(RHSs->getArgName(i));
628 }
Nate Begemandbe3f772009-03-19 05:21:56 +0000629 return new DagInit(LHSs->getOperator(), "", Args, ArgNames);
Evan Chenga32dee22007-05-15 01:23:24 +0000630 }
631 break;
632 }
Chris Lattner51ffbf12006-03-31 21:53:49 +0000633 case STRCONCAT: {
634 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
635 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
636 if (LHSs && RHSs)
637 return new StringInit(LHSs->getValue() + RHSs->getValue());
638 break;
639 }
David Greene297bfe62010-01-05 19:11:42 +0000640 case EQ: {
Bruno Cardoso Lopes77a4a562010-06-16 23:24:12 +0000641 // try to fold eq comparison for 'bit' and 'int', otherwise fallback
642 // to string objects.
643 IntInit* L =
644 dynamic_cast<IntInit*>(LHS->convertInitializerTo(new IntRecTy()));
645 IntInit* R =
646 dynamic_cast<IntInit*>(RHS->convertInitializerTo(new IntRecTy()));
647
648 if (L && R)
649 return new IntInit(L->getValue() == R->getValue());
650
David Greene297bfe62010-01-05 19:11:42 +0000651 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
652 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
Bruno Cardoso Lopes77a4a562010-06-16 23:24:12 +0000653
654 // Make sure we've resolved
David Greene297bfe62010-01-05 19:11:42 +0000655 if (LHSs && RHSs)
656 return new IntInit(LHSs->getValue() == RHSs->getValue());
657
658 break;
659 }
Chris Lattner51ffbf12006-03-31 21:53:49 +0000660 case SHL:
661 case SRA:
662 case SRL: {
663 IntInit *LHSi = dynamic_cast<IntInit*>(LHS);
664 IntInit *RHSi = dynamic_cast<IntInit*>(RHS);
665 if (LHSi && RHSi) {
Dan Gohmanca0546f2008-10-17 01:33:43 +0000666 int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
667 int64_t Result;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000668 switch (getOpcode()) {
669 default: assert(0 && "Bad opcode!");
670 case SHL: Result = LHSv << RHSv; break;
671 case SRA: Result = LHSv >> RHSv; break;
Dan Gohmanca0546f2008-10-17 01:33:43 +0000672 case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000673 }
674 return new IntInit(Result);
675 }
676 break;
677 }
678 }
679 return this;
680}
681
682Init *BinOpInit::resolveReferences(Record &R, const RecordVal *RV) {
683 Init *lhs = LHS->resolveReferences(R, RV);
684 Init *rhs = RHS->resolveReferences(R, RV);
Bob Wilson7248f862009-11-22 04:24:42 +0000685
Chris Lattner51ffbf12006-03-31 21:53:49 +0000686 if (LHS != lhs || RHS != rhs)
David Greene196ac3c2009-04-23 21:25:15 +0000687 return (new BinOpInit(getOpcode(), lhs, rhs, getType()))->Fold(&R, 0);
David Greenea9c6c5d2009-04-22 20:18:10 +0000688 return Fold(&R, 0);
Chris Lattner51ffbf12006-03-31 21:53:49 +0000689}
690
Chris Lattner695506c2007-11-22 21:05:25 +0000691std::string BinOpInit::getAsString() const {
692 std::string Result;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000693 switch (Opc) {
Chris Lattner695506c2007-11-22 21:05:25 +0000694 case CONCAT: Result = "!con"; break;
695 case SHL: Result = "!shl"; break;
696 case SRA: Result = "!sra"; break;
697 case SRL: Result = "!srl"; break;
David Greene297bfe62010-01-05 19:11:42 +0000698 case EQ: Result = "!eq"; break;
Chris Lattner695506c2007-11-22 21:05:25 +0000699 case STRCONCAT: Result = "!strconcat"; break;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000700 }
Chris Lattner695506c2007-11-22 21:05:25 +0000701 return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")";
Chris Lattner51ffbf12006-03-31 21:53:49 +0000702}
703
David Greenee917fff2009-05-14 22:23:47 +0000704static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
705 Record *CurRec, MultiClass *CurMultiClass);
706
707static Init *EvaluateOperation(OpInit *RHSo, Init *LHS, Init *Arg,
708 RecTy *Type, Record *CurRec,
709 MultiClass *CurMultiClass) {
710 std::vector<Init *> NewOperands;
711
712 TypedInit *TArg = dynamic_cast<TypedInit*>(Arg);
713
714 // If this is a dag, recurse
715 if (TArg && TArg->getType()->getAsString() == "dag") {
716 Init *Result = ForeachHelper(LHS, Arg, RHSo, Type,
717 CurRec, CurMultiClass);
718 if (Result != 0) {
719 return Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000720 } else {
David Greenee917fff2009-05-14 22:23:47 +0000721 return 0;
722 }
723 }
724
725 for (int i = 0; i < RHSo->getNumOperands(); ++i) {
726 OpInit *RHSoo = dynamic_cast<OpInit*>(RHSo->getOperand(i));
727
728 if (RHSoo) {
729 Init *Result = EvaluateOperation(RHSoo, LHS, Arg,
730 Type, CurRec, CurMultiClass);
731 if (Result != 0) {
732 NewOperands.push_back(Result);
Bob Wilson7248f862009-11-22 04:24:42 +0000733 } else {
David Greenee917fff2009-05-14 22:23:47 +0000734 NewOperands.push_back(Arg);
735 }
Bob Wilson7248f862009-11-22 04:24:42 +0000736 } else if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
David Greenee917fff2009-05-14 22:23:47 +0000737 NewOperands.push_back(Arg);
Bob Wilson7248f862009-11-22 04:24:42 +0000738 } else {
David Greenee917fff2009-05-14 22:23:47 +0000739 NewOperands.push_back(RHSo->getOperand(i));
740 }
741 }
742
743 // Now run the operator and use its result as the new leaf
744 OpInit *NewOp = RHSo->clone(NewOperands);
745 Init *NewVal = NewOp->Fold(CurRec, CurMultiClass);
746 if (NewVal != NewOp) {
747 delete NewOp;
748 return NewVal;
749 }
750 return 0;
751}
752
753static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
754 Record *CurRec, MultiClass *CurMultiClass) {
755 DagInit *MHSd = dynamic_cast<DagInit*>(MHS);
756 ListInit *MHSl = dynamic_cast<ListInit*>(MHS);
757
758 DagRecTy *DagType = dynamic_cast<DagRecTy*>(Type);
759 ListRecTy *ListType = dynamic_cast<ListRecTy*>(Type);
760
761 OpInit *RHSo = dynamic_cast<OpInit*>(RHS);
762
763 if (!RHSo) {
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000764 errs() << "!foreach requires an operator\n";
David Greenee917fff2009-05-14 22:23:47 +0000765 assert(0 && "No operator for !foreach");
766 }
767
768 TypedInit *LHSt = dynamic_cast<TypedInit*>(LHS);
769
770 if (!LHSt) {
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000771 errs() << "!foreach requires typed variable\n";
David Greenee917fff2009-05-14 22:23:47 +0000772 assert(0 && "No typed variable for !foreach");
773 }
774
Nick Lewycky94298222009-05-15 03:07:14 +0000775 if ((MHSd && DagType) || (MHSl && ListType)) {
David Greenee917fff2009-05-14 22:23:47 +0000776 if (MHSd) {
777 Init *Val = MHSd->getOperator();
778 Init *Result = EvaluateOperation(RHSo, LHS, Val,
779 Type, CurRec, CurMultiClass);
780 if (Result != 0) {
781 Val = Result;
782 }
783
784 std::vector<std::pair<Init *, std::string> > args;
785 for (unsigned int i = 0; i < MHSd->getNumArgs(); ++i) {
786 Init *Arg;
787 std::string ArgName;
788 Arg = MHSd->getArg(i);
789 ArgName = MHSd->getArgName(i);
790
791 // Process args
792 Init *Result = EvaluateOperation(RHSo, LHS, Arg, Type,
793 CurRec, CurMultiClass);
794 if (Result != 0) {
795 Arg = Result;
796 }
797
798 // TODO: Process arg names
799 args.push_back(std::make_pair(Arg, ArgName));
800 }
801
802 return new DagInit(Val, "", args);
803 }
804 if (MHSl) {
805 std::vector<Init *> NewOperands;
806 std::vector<Init *> NewList(MHSl->begin(), MHSl->end());
807
808 for (ListInit::iterator li = NewList.begin(),
809 liend = NewList.end();
810 li != liend;
811 ++li) {
812 Init *Item = *li;
813 NewOperands.clear();
814 for(int i = 0; i < RHSo->getNumOperands(); ++i) {
815 // First, replace the foreach variable with the list item
816 if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
817 NewOperands.push_back(Item);
Bob Wilson7248f862009-11-22 04:24:42 +0000818 } else {
David Greenee917fff2009-05-14 22:23:47 +0000819 NewOperands.push_back(RHSo->getOperand(i));
820 }
821 }
822
823 // Now run the operator and use its result as the new list item
824 OpInit *NewOp = RHSo->clone(NewOperands);
825 Init *NewItem = NewOp->Fold(CurRec, CurMultiClass);
826 if (NewItem != NewOp) {
827 *li = NewItem;
828 delete NewOp;
829 }
830 }
David Greene8618f952009-06-08 20:23:18 +0000831 return new ListInit(NewList, MHSl->getType());
David Greenee917fff2009-05-14 22:23:47 +0000832 }
833 }
834 return 0;
835}
836
David Greene98ed3c72009-05-14 21:54:42 +0000837Init *TernOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
838 switch (getOpcode()) {
839 default: assert(0 && "Unknown binop");
840 case SUBST: {
841 DefInit *LHSd = dynamic_cast<DefInit*>(LHS);
842 VarInit *LHSv = dynamic_cast<VarInit*>(LHS);
843 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
David Greene196ac3c2009-04-23 21:25:15 +0000844
David Greene98ed3c72009-05-14 21:54:42 +0000845 DefInit *MHSd = dynamic_cast<DefInit*>(MHS);
846 VarInit *MHSv = dynamic_cast<VarInit*>(MHS);
847 StringInit *MHSs = dynamic_cast<StringInit*>(MHS);
David Greene5d0c0512009-05-14 20:54:48 +0000848
David Greene98ed3c72009-05-14 21:54:42 +0000849 DefInit *RHSd = dynamic_cast<DefInit*>(RHS);
850 VarInit *RHSv = dynamic_cast<VarInit*>(RHS);
851 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
David Greene5d0c0512009-05-14 20:54:48 +0000852
David Greene98ed3c72009-05-14 21:54:42 +0000853 if ((LHSd && MHSd && RHSd)
854 || (LHSv && MHSv && RHSv)
855 || (LHSs && MHSs && RHSs)) {
856 if (RHSd) {
857 Record *Val = RHSd->getDef();
858 if (LHSd->getAsString() == RHSd->getAsString()) {
859 Val = MHSd->getDef();
860 }
861 return new DefInit(Val);
862 }
863 if (RHSv) {
864 std::string Val = RHSv->getName();
865 if (LHSv->getAsString() == RHSv->getAsString()) {
866 Val = MHSv->getName();
867 }
868 return new VarInit(Val, getType());
869 }
870 if (RHSs) {
871 std::string Val = RHSs->getValue();
David Greene5d0c0512009-05-14 20:54:48 +0000872
David Greene98ed3c72009-05-14 21:54:42 +0000873 std::string::size_type found;
David Greenedbf70742009-12-21 21:21:34 +0000874 std::string::size_type idx = 0;
David Greene98ed3c72009-05-14 21:54:42 +0000875 do {
David Greenedbf70742009-12-21 21:21:34 +0000876 found = Val.find(LHSs->getValue(), idx);
David Greene98ed3c72009-05-14 21:54:42 +0000877 if (found != std::string::npos) {
878 Val.replace(found, LHSs->getValue().size(), MHSs->getValue());
879 }
David Greenedbf70742009-12-21 21:21:34 +0000880 idx = found + MHSs->getValue().size();
David Greene98ed3c72009-05-14 21:54:42 +0000881 } while (found != std::string::npos);
David Greene5d0c0512009-05-14 20:54:48 +0000882
David Greene98ed3c72009-05-14 21:54:42 +0000883 return new StringInit(Val);
884 }
885 }
886 break;
Bob Wilson7248f862009-11-22 04:24:42 +0000887 }
David Greene5d0c0512009-05-14 20:54:48 +0000888
David Greene98ed3c72009-05-14 21:54:42 +0000889 case FOREACH: {
David Greenee917fff2009-05-14 22:23:47 +0000890 Init *Result = ForeachHelper(LHS, MHS, RHS, getType(),
891 CurRec, CurMultiClass);
892 if (Result != 0) {
893 return Result;
David Greene98ed3c72009-05-14 21:54:42 +0000894 }
895 break;
896 }
David Greene3587eed2009-05-14 23:26:46 +0000897
898 case IF: {
Bruno Cardoso Lopes7f4235d2010-06-17 01:50:39 +0000899 IntInit *LHSi = dynamic_cast<IntInit*>(LHS);
900 if (Init *I = LHS->convertInitializerTo(new IntRecTy()))
901 LHSi = dynamic_cast<IntInit*>(I);
David Greene3587eed2009-05-14 23:26:46 +0000902 if (LHSi) {
903 if (LHSi->getValue()) {
904 return MHS;
Bob Wilson7248f862009-11-22 04:24:42 +0000905 } else {
David Greene3587eed2009-05-14 23:26:46 +0000906 return RHS;
907 }
908 }
909 break;
910 }
David Greene98ed3c72009-05-14 21:54:42 +0000911 }
David Greene5d0c0512009-05-14 20:54:48 +0000912
David Greene98ed3c72009-05-14 21:54:42 +0000913 return this;
914}
David Greene5d0c0512009-05-14 20:54:48 +0000915
David Greene98ed3c72009-05-14 21:54:42 +0000916Init *TernOpInit::resolveReferences(Record &R, const RecordVal *RV) {
917 Init *lhs = LHS->resolveReferences(R, RV);
David Greeneb0354452009-06-08 19:16:56 +0000918
919 if (Opc == IF && lhs != LHS) {
Bruno Cardoso Lopes7f4235d2010-06-17 01:50:39 +0000920 IntInit *Value = dynamic_cast<IntInit*>(lhs);
921 if (Init *I = lhs->convertInitializerTo(new IntRecTy()))
922 Value = dynamic_cast<IntInit*>(I);
David Greeneb0354452009-06-08 19:16:56 +0000923 if (Value != 0) {
924 // Short-circuit
925 if (Value->getValue()) {
926 Init *mhs = MHS->resolveReferences(R, RV);
Bob Wilson7248f862009-11-22 04:24:42 +0000927 return (new TernOpInit(getOpcode(), lhs, mhs,
928 RHS, getType()))->Fold(&R, 0);
929 } else {
David Greeneb0354452009-06-08 19:16:56 +0000930 Init *rhs = RHS->resolveReferences(R, RV);
Bob Wilson7248f862009-11-22 04:24:42 +0000931 return (new TernOpInit(getOpcode(), lhs, MHS,
932 rhs, getType()))->Fold(&R, 0);
David Greeneb0354452009-06-08 19:16:56 +0000933 }
934 }
935 }
Bob Wilson7248f862009-11-22 04:24:42 +0000936
David Greene98ed3c72009-05-14 21:54:42 +0000937 Init *mhs = MHS->resolveReferences(R, RV);
938 Init *rhs = RHS->resolveReferences(R, RV);
David Greeneb0354452009-06-08 19:16:56 +0000939
David Greene98ed3c72009-05-14 21:54:42 +0000940 if (LHS != lhs || MHS != mhs || RHS != rhs)
941 return (new TernOpInit(getOpcode(), lhs, mhs, rhs, getType()))->Fold(&R, 0);
942 return Fold(&R, 0);
943}
David Greene196ac3c2009-04-23 21:25:15 +0000944
David Greene98ed3c72009-05-14 21:54:42 +0000945std::string TernOpInit::getAsString() const {
946 std::string Result;
947 switch (Opc) {
948 case SUBST: Result = "!subst"; break;
Bob Wilson7248f862009-11-22 04:24:42 +0000949 case FOREACH: Result = "!foreach"; break;
950 case IF: Result = "!if"; break;
David Greene98ed3c72009-05-14 21:54:42 +0000951 }
Bob Wilson7248f862009-11-22 04:24:42 +0000952 return Result + "(" + LHS->getAsString() + ", " + MHS->getAsString() + ", "
David Greene98ed3c72009-05-14 21:54:42 +0000953 + RHS->getAsString() + ")";
954}
David Greene196ac3c2009-04-23 21:25:15 +0000955
David Greene2a9de4d2010-09-03 21:00:49 +0000956RecTy *TypedInit::getFieldType(const std::string &FieldName) const {
957 RecordRecTy *RecordType = dynamic_cast<RecordRecTy *>(getType());
958 if (RecordType) {
959 RecordVal *Field = RecordType->getRecord()->getValue(FieldName);
960 if (Field) {
961 return Field->getType();
962 }
963 }
964 return 0;
965}
966
Chris Lattner577fc3f2004-07-27 01:01:21 +0000967Init *TypedInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000968 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
969 if (T == 0) return 0; // Cannot subscript a non-bits variable...
970 unsigned NumBits = T->getNumBits();
971
972 BitsInit *BI = new BitsInit(Bits.size());
973 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
974 if (Bits[i] >= NumBits) {
975 delete BI;
976 return 0;
977 }
978 BI->setBit(i, new VarBitInit(this, Bits[i]));
979 }
980 return BI;
981}
982
Chris Lattner577fc3f2004-07-27 01:01:21 +0000983Init *TypedInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
984 ListRecTy *T = dynamic_cast<ListRecTy*>(getType());
985 if (T == 0) return 0; // Cannot subscript a non-list variable...
986
987 if (Elements.size() == 1)
988 return new VarListElementInit(this, Elements[0]);
989
990 std::vector<Init*> ListInits;
991 ListInits.reserve(Elements.size());
992 for (unsigned i = 0, e = Elements.size(); i != e; ++i)
993 ListInits.push_back(new VarListElementInit(this, Elements[i]));
David Greene8618f952009-06-08 20:23:18 +0000994 return new ListInit(ListInits, T);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000995}
996
997
Chris Lattneref943742005-04-19 03:36:21 +0000998Init *VarInit::resolveBitReference(Record &R, const RecordVal *IRV,
999 unsigned Bit) {
1000 if (R.isTemplateArg(getName())) return 0;
1001 if (IRV && IRV->getName() != getName()) return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001002
1003 RecordVal *RV = R.getValue(getName());
Bob Wilson159905a2009-11-21 22:44:20 +00001004 assert(RV && "Reference to a non-existent variable?");
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001005 assert(dynamic_cast<BitsInit*>(RV->getValue()));
1006 BitsInit *BI = (BitsInit*)RV->getValue();
Misha Brukman650ba8e2005-04-22 00:00:37 +00001007
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001008 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
1009 Init *B = BI->getBit(Bit);
1010
Bob Wilson67e6cab2009-11-22 03:58:57 +00001011 // If the bit is set to some value, or if we are resolving a reference to a
1012 // specific variable and that variable is explicitly unset, then replace the
1013 // VarBitInit with it.
1014 if (IRV || !dynamic_cast<UnsetInit*>(B))
1015 return B;
Chris Lattner577fc3f2004-07-27 01:01:21 +00001016 return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001017}
1018
Chris Lattneref943742005-04-19 03:36:21 +00001019Init *VarInit::resolveListElementReference(Record &R, const RecordVal *IRV,
1020 unsigned Elt) {
1021 if (R.isTemplateArg(getName())) return 0;
1022 if (IRV && IRV->getName() != getName()) return 0;
Chris Lattner577fc3f2004-07-27 01:01:21 +00001023
1024 RecordVal *RV = R.getValue(getName());
Bob Wilson159905a2009-11-21 22:44:20 +00001025 assert(RV && "Reference to a non-existent variable?");
Chris Lattner577fc3f2004-07-27 01:01:21 +00001026 ListInit *LI = dynamic_cast<ListInit*>(RV->getValue());
David Greene9d3febe2009-05-14 20:38:52 +00001027 if (!LI) {
1028 VarInit *VI = dynamic_cast<VarInit*>(RV->getValue());
1029 assert(VI && "Invalid list element!");
1030 return new VarListElementInit(VI, Elt);
1031 }
Bob Wilson7248f862009-11-22 04:24:42 +00001032
Chris Lattner577fc3f2004-07-27 01:01:21 +00001033 if (Elt >= LI->getSize())
1034 return 0; // Out of range reference.
1035 Init *E = LI->getElement(Elt);
Bob Wilson67e6cab2009-11-22 03:58:57 +00001036 // If the element is set to some value, or if we are resolving a reference
1037 // to a specific variable and that variable is explicitly unset, then
1038 // replace the VarListElementInit with it.
1039 if (IRV || !dynamic_cast<UnsetInit*>(E))
1040 return E;
Chris Lattner577fc3f2004-07-27 01:01:21 +00001041 return 0;
1042}
1043
1044
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001045RecTy *VarInit::getFieldType(const std::string &FieldName) const {
1046 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
1047 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
1048 return RV->getType();
1049 return 0;
1050}
1051
Jakob Stoklund Olesen0e457622010-03-25 06:23:34 +00001052Init *VarInit::getFieldInit(Record &R, const RecordVal *RV,
1053 const std::string &FieldName) const {
Reid Spencer2a826862006-11-02 20:46:16 +00001054 if (dynamic_cast<RecordRecTy*>(getType()))
Jakob Stoklund Olesen0e457622010-03-25 06:23:34 +00001055 if (const RecordVal *Val = R.getValue(VarName)) {
1056 if (RV != Val && (RV || dynamic_cast<UnsetInit*>(Val->getValue())))
1057 return 0;
1058 Init *TheInit = Val->getValue();
Chris Lattnerd959ab92004-02-28 16:31:53 +00001059 assert(TheInit != this && "Infinite loop detected!");
Jakob Stoklund Olesen0e457622010-03-25 06:23:34 +00001060 if (Init *I = TheInit->getFieldInit(R, RV, FieldName))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001061 return I;
1062 else
1063 return 0;
Chris Lattnerd959ab92004-02-28 16:31:53 +00001064 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001065 return 0;
1066}
1067
1068/// resolveReferences - This method is used by classes that refer to other
Bob Wilson159905a2009-11-21 22:44:20 +00001069/// variables which may not be defined at the time the expression is formed.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001070/// If a value is set for the variable later, this method will be called on
1071/// users of the value to allow the value to propagate out.
1072///
Chris Lattneref943742005-04-19 03:36:21 +00001073Init *VarInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001074 if (RecordVal *Val = R.getValue(VarName))
Chris Lattneref943742005-04-19 03:36:21 +00001075 if (RV == Val || (RV == 0 && !dynamic_cast<UnsetInit*>(Val->getValue())))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001076 return Val->getValue();
1077 return this;
1078}
Misha Brukman650ba8e2005-04-22 00:00:37 +00001079
Chris Lattner695506c2007-11-22 21:05:25 +00001080std::string VarBitInit::getAsString() const {
1081 return TI->getAsString() + "{" + utostr(Bit) + "}";
1082}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001083
Chris Lattneref943742005-04-19 03:36:21 +00001084Init *VarBitInit::resolveReferences(Record &R, const RecordVal *RV) {
1085 if (Init *I = getVariable()->resolveBitReference(R, RV, getBitNum()))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001086 return I;
1087 return this;
1088}
1089
Chris Lattner695506c2007-11-22 21:05:25 +00001090std::string VarListElementInit::getAsString() const {
1091 return TI->getAsString() + "[" + utostr(Element) + "]";
1092}
1093
Chris Lattneref943742005-04-19 03:36:21 +00001094Init *VarListElementInit::resolveReferences(Record &R, const RecordVal *RV) {
1095 if (Init *I = getVariable()->resolveListElementReference(R, RV,
1096 getElementNum()))
Chris Lattner577fc3f2004-07-27 01:01:21 +00001097 return I;
1098 return this;
1099}
1100
Chris Lattneref943742005-04-19 03:36:21 +00001101Init *VarListElementInit::resolveBitReference(Record &R, const RecordVal *RV,
1102 unsigned Bit) {
Chris Lattner577fc3f2004-07-27 01:01:21 +00001103 // FIXME: This should be implemented, to support references like:
1104 // bit B = AA[0]{1};
1105 return 0;
1106}
1107
Chris Lattneref943742005-04-19 03:36:21 +00001108Init *VarListElementInit::
1109resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) {
Chris Lattner577fc3f2004-07-27 01:01:21 +00001110 // FIXME: This should be implemented, to support references like:
1111 // int B = AA[0][1];
1112 return 0;
1113}
1114
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001115RecTy *DefInit::getFieldType(const std::string &FieldName) const {
1116 if (const RecordVal *RV = Def->getValue(FieldName))
1117 return RV->getType();
1118 return 0;
1119}
1120
Jakob Stoklund Olesen0e457622010-03-25 06:23:34 +00001121Init *DefInit::getFieldInit(Record &R, const RecordVal *RV,
1122 const std::string &FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001123 return Def->getValue(FieldName)->getValue();
1124}
1125
1126
Chris Lattner695506c2007-11-22 21:05:25 +00001127std::string DefInit::getAsString() const {
1128 return Def->getName();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001129}
1130
Chris Lattneref943742005-04-19 03:36:21 +00001131Init *FieldInit::resolveBitReference(Record &R, const RecordVal *RV,
1132 unsigned Bit) {
Jakob Stoklund Olesen0e457622010-03-25 06:23:34 +00001133 if (Init *BitsVal = Rec->getFieldInit(R, RV, FieldName))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001134 if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
1135 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
1136 Init *B = BI->getBit(Bit);
Misha Brukman650ba8e2005-04-22 00:00:37 +00001137
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001138 if (dynamic_cast<BitInit*>(B)) // If the bit is set...
1139 return B; // Replace the VarBitInit with it.
1140 }
Chris Lattner577fc3f2004-07-27 01:01:21 +00001141 return 0;
1142}
1143
Chris Lattneref943742005-04-19 03:36:21 +00001144Init *FieldInit::resolveListElementReference(Record &R, const RecordVal *RV,
1145 unsigned Elt) {
Jakob Stoklund Olesen0e457622010-03-25 06:23:34 +00001146 if (Init *ListVal = Rec->getFieldInit(R, RV, FieldName))
Chris Lattner577fc3f2004-07-27 01:01:21 +00001147 if (ListInit *LI = dynamic_cast<ListInit*>(ListVal)) {
1148 if (Elt >= LI->getSize()) return 0;
1149 Init *E = LI->getElement(Elt);
1150
Bob Wilson67e6cab2009-11-22 03:58:57 +00001151 // If the element is set to some value, or if we are resolving a
1152 // reference to a specific variable and that variable is explicitly
1153 // unset, then replace the VarListElementInit with it.
1154 if (RV || !dynamic_cast<UnsetInit*>(E))
1155 return E;
Chris Lattner577fc3f2004-07-27 01:01:21 +00001156 }
1157 return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001158}
1159
Chris Lattneref943742005-04-19 03:36:21 +00001160Init *FieldInit::resolveReferences(Record &R, const RecordVal *RV) {
1161 Init *NewRec = RV ? Rec->resolveReferences(R, RV) : Rec;
1162
Jakob Stoklund Olesen0e457622010-03-25 06:23:34 +00001163 Init *BitsVal = NewRec->getFieldInit(R, RV, FieldName);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001164 if (BitsVal) {
Chris Lattneref943742005-04-19 03:36:21 +00001165 Init *BVR = BitsVal->resolveReferences(R, RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001166 return BVR->isComplete() ? BVR : this;
1167 }
Chris Lattneref943742005-04-19 03:36:21 +00001168
1169 if (NewRec != Rec) {
Chris Lattneref943742005-04-19 03:36:21 +00001170 return new FieldInit(NewRec, FieldName);
1171 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001172 return this;
1173}
1174
Chris Lattner0d3ef402006-01-31 06:02:35 +00001175Init *DagInit::resolveReferences(Record &R, const RecordVal *RV) {
1176 std::vector<Init*> NewArgs;
1177 for (unsigned i = 0, e = Args.size(); i != e; ++i)
1178 NewArgs.push_back(Args[i]->resolveReferences(R, RV));
Bob Wilson7248f862009-11-22 04:24:42 +00001179
Chris Lattnerb59cf3c2006-03-30 22:50:40 +00001180 Init *Op = Val->resolveReferences(R, RV);
Bob Wilson7248f862009-11-22 04:24:42 +00001181
Chris Lattnerb59cf3c2006-03-30 22:50:40 +00001182 if (Args != NewArgs || Op != Val)
Bruno Cardoso Lopes30a28d62010-06-23 19:50:39 +00001183 return new DagInit(Op, ValName, NewArgs, ArgNames);
Bob Wilson7248f862009-11-22 04:24:42 +00001184
Chris Lattner0d3ef402006-01-31 06:02:35 +00001185 return this;
1186}
1187
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001188
Chris Lattner695506c2007-11-22 21:05:25 +00001189std::string DagInit::getAsString() const {
1190 std::string Result = "(" + Val->getAsString();
Nate Begemandbe3f772009-03-19 05:21:56 +00001191 if (!ValName.empty())
1192 Result += ":" + ValName;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001193 if (Args.size()) {
Chris Lattner695506c2007-11-22 21:05:25 +00001194 Result += " " + Args[0]->getAsString();
1195 if (!ArgNames[0].empty()) Result += ":$" + ArgNames[0];
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001196 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
Chris Lattner695506c2007-11-22 21:05:25 +00001197 Result += ", " + Args[i]->getAsString();
1198 if (!ArgNames[i].empty()) Result += ":$" + ArgNames[i];
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001199 }
1200 }
Chris Lattner695506c2007-11-22 21:05:25 +00001201 return Result + ")";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001202}
1203
1204
1205//===----------------------------------------------------------------------===//
1206// Other implementations
1207//===----------------------------------------------------------------------===//
1208
1209RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
1210 : Name(N), Ty(T), Prefix(P) {
1211 Value = Ty->convertValue(new UnsetInit());
1212 assert(Value && "Cannot create unset value for current type!");
1213}
1214
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001215void RecordVal::dump() const { errs() << *this; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001216
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001217void RecordVal::print(raw_ostream &OS, bool PrintSem) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001218 if (getPrefix()) OS << "field ";
1219 OS << *getType() << " " << getName();
Chris Lattneref943742005-04-19 03:36:21 +00001220
1221 if (getValue())
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001222 OS << " = " << *getValue();
Chris Lattneref943742005-04-19 03:36:21 +00001223
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001224 if (PrintSem) OS << ";\n";
1225}
1226
Daniel Dunbarced00812009-08-23 09:47:37 +00001227unsigned Record::LastID = 0;
1228
Chris Lattnerac284252005-08-19 17:58:11 +00001229void Record::setName(const std::string &Name) {
1230 if (Records.getDef(getName()) == this) {
1231 Records.removeDef(getName());
1232 this->Name = Name;
1233 Records.addDef(this);
1234 } else {
1235 Records.removeClass(getName());
1236 this->Name = Name;
1237 Records.addClass(this);
1238 }
1239}
1240
Chris Lattneref943742005-04-19 03:36:21 +00001241/// resolveReferencesTo - If anything in this record refers to RV, replace the
1242/// reference to RV with the RHS of RV. If RV is null, we resolve all possible
1243/// references.
1244void Record::resolveReferencesTo(const RecordVal *RV) {
1245 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
1246 if (Init *V = Values[i].getValue())
1247 Values[i].setValue(V->resolveReferences(*this, RV));
1248 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001249}
1250
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001251void Record::dump() const { errs() << *this; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001252
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001253raw_ostream &llvm::operator<<(raw_ostream &OS, const Record &R) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001254 OS << R.getName();
1255
1256 const std::vector<std::string> &TArgs = R.getTemplateArgs();
1257 if (!TArgs.empty()) {
1258 OS << "<";
1259 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1260 if (i) OS << ", ";
1261 const RecordVal *RV = R.getValue(TArgs[i]);
1262 assert(RV && "Template argument record not found??");
1263 RV->print(OS, false);
1264 }
1265 OS << ">";
1266 }
1267
1268 OS << " {";
1269 const std::vector<Record*> &SC = R.getSuperClasses();
1270 if (!SC.empty()) {
1271 OS << "\t//";
1272 for (unsigned i = 0, e = SC.size(); i != e; ++i)
1273 OS << " " << SC[i]->getName();
1274 }
1275 OS << "\n";
1276
1277 const std::vector<RecordVal> &Vals = R.getValues();
1278 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
1279 if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
1280 OS << Vals[i];
1281 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
1282 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
1283 OS << Vals[i];
1284
1285 return OS << "}\n";
1286}
1287
1288/// getValueInit - Return the initializer for a value with the specified name,
1289/// or throw an exception if the field does not exist.
1290///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001291Init *Record::getValueInit(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001292 const RecordVal *R = getValue(FieldName);
1293 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001294 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001295 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001296 return R->getValue();
1297}
1298
1299
1300/// getValueAsString - This method looks up the specified field and returns its
1301/// value as a string, throwing an exception if the field does not exist or if
1302/// the value is not a string.
1303///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001304std::string Record::getValueAsString(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001305 const RecordVal *R = getValue(FieldName);
1306 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001307 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001308 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001309
1310 if (const StringInit *SI = dynamic_cast<const StringInit*>(R->getValue()))
1311 return SI->getValue();
Chris Lattner42bb0c12009-09-18 18:31:37 +00001312 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001313 "' does not have a string initializer!";
1314}
1315
1316/// getValueAsBitsInit - This method looks up the specified field and returns
1317/// its value as a BitsInit, throwing an exception if the field does not exist
1318/// or if the value is not the right type.
1319///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001320BitsInit *Record::getValueAsBitsInit(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001321 const RecordVal *R = getValue(FieldName);
1322 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001323 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001324 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001325
1326 if (BitsInit *BI = dynamic_cast<BitsInit*>(R->getValue()))
1327 return BI;
Chris Lattner42bb0c12009-09-18 18:31:37 +00001328 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001329 "' does not have a BitsInit initializer!";
1330}
1331
1332/// getValueAsListInit - This method looks up the specified field and returns
1333/// its value as a ListInit, throwing an exception if the field does not exist
1334/// or if the value is not the right type.
1335///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001336ListInit *Record::getValueAsListInit(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001337 const RecordVal *R = getValue(FieldName);
1338 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001339 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001340 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001341
1342 if (ListInit *LI = dynamic_cast<ListInit*>(R->getValue()))
1343 return LI;
Chris Lattner42bb0c12009-09-18 18:31:37 +00001344 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001345 "' does not have a list initializer!";
1346}
1347
Chris Lattner7ad0bed2005-10-28 22:49:02 +00001348/// getValueAsListOfDefs - This method looks up the specified field and returns
Jim Laskeyb04feb62005-10-28 21:46:31 +00001349/// its value as a vector of records, throwing an exception if the field does
1350/// not exist or if the value is not the right type.
1351///
Bob Wilson7248f862009-11-22 04:24:42 +00001352std::vector<Record*>
Chris Lattner42bb0c12009-09-18 18:31:37 +00001353Record::getValueAsListOfDefs(StringRef FieldName) const {
Jim Laskeyb04feb62005-10-28 21:46:31 +00001354 ListInit *List = getValueAsListInit(FieldName);
1355 std::vector<Record*> Defs;
1356 for (unsigned i = 0; i < List->getSize(); i++) {
1357 if (DefInit *DI = dynamic_cast<DefInit*>(List->getElement(i))) {
1358 Defs.push_back(DI->getDef());
1359 } else {
Chris Lattner42bb0c12009-09-18 18:31:37 +00001360 throw "Record `" + getName() + "', field `" + FieldName.str() +
Jim Laskeyb04feb62005-10-28 21:46:31 +00001361 "' list is not entirely DefInit!";
1362 }
1363 }
1364 return Defs;
1365}
1366
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001367/// getValueAsInt - This method looks up the specified field and returns its
Dan Gohmanca0546f2008-10-17 01:33:43 +00001368/// value as an int64_t, throwing an exception if the field does not exist or if
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001369/// the value is not the right type.
1370///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001371int64_t Record::getValueAsInt(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001372 const RecordVal *R = getValue(FieldName);
1373 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001374 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001375 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001376
1377 if (IntInit *II = dynamic_cast<IntInit*>(R->getValue()))
1378 return II->getValue();
Chris Lattner42bb0c12009-09-18 18:31:37 +00001379 throw "Record `" + getName() + "', field `" + FieldName.str() +
Nate Begemanf621b332005-11-30 18:37:14 +00001380 "' does not have an int initializer!";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001381}
1382
Anton Korobeynikova468a112007-11-11 11:19:37 +00001383/// getValueAsListOfInts - This method looks up the specified field and returns
1384/// its value as a vector of integers, throwing an exception if the field does
1385/// not exist or if the value is not the right type.
1386///
Bob Wilson7248f862009-11-22 04:24:42 +00001387std::vector<int64_t>
Chris Lattner42bb0c12009-09-18 18:31:37 +00001388Record::getValueAsListOfInts(StringRef FieldName) const {
Anton Korobeynikova468a112007-11-11 11:19:37 +00001389 ListInit *List = getValueAsListInit(FieldName);
Dan Gohmanca0546f2008-10-17 01:33:43 +00001390 std::vector<int64_t> Ints;
Anton Korobeynikova468a112007-11-11 11:19:37 +00001391 for (unsigned i = 0; i < List->getSize(); i++) {
1392 if (IntInit *II = dynamic_cast<IntInit*>(List->getElement(i))) {
1393 Ints.push_back(II->getValue());
1394 } else {
Chris Lattner42bb0c12009-09-18 18:31:37 +00001395 throw "Record `" + getName() + "', field `" + FieldName.str() +
Anton Korobeynikova468a112007-11-11 11:19:37 +00001396 "' does not have a list of ints initializer!";
1397 }
1398 }
1399 return Ints;
1400}
1401
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001402/// getValueAsDef - This method looks up the specified field and returns its
1403/// value as a Record, throwing an exception if the field does not exist or if
1404/// the value is not the right type.
1405///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001406Record *Record::getValueAsDef(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001407 const RecordVal *R = getValue(FieldName);
1408 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001409 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001410 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001411
1412 if (DefInit *DI = dynamic_cast<DefInit*>(R->getValue()))
1413 return DI->getDef();
Chris Lattner42bb0c12009-09-18 18:31:37 +00001414 throw "Record `" + getName() + "', field `" + FieldName.str() +
Nate Begemanf621b332005-11-30 18:37:14 +00001415 "' does not have a def initializer!";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001416}
1417
1418/// getValueAsBit - This method looks up the specified field and returns its
1419/// value as a bit, throwing an exception if the field does not exist or if
1420/// the value is not the right type.
1421///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001422bool Record::getValueAsBit(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001423 const RecordVal *R = getValue(FieldName);
1424 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001425 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001426 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001427
1428 if (BitInit *BI = dynamic_cast<BitInit*>(R->getValue()))
1429 return BI->getValue();
Chris Lattner42bb0c12009-09-18 18:31:37 +00001430 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001431 "' does not have a bit initializer!";
1432}
1433
1434/// getValueAsDag - This method looks up the specified field and returns its
1435/// value as an Dag, throwing an exception if the field does not exist or if
1436/// the value is not the right type.
1437///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001438DagInit *Record::getValueAsDag(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001439 const RecordVal *R = getValue(FieldName);
1440 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001441 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001442 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001443
1444 if (DagInit *DI = dynamic_cast<DagInit*>(R->getValue()))
1445 return DI;
Chris Lattner42bb0c12009-09-18 18:31:37 +00001446 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001447 "' does not have a dag initializer!";
1448}
1449
Chris Lattner42bb0c12009-09-18 18:31:37 +00001450std::string Record::getValueAsCode(StringRef FieldName) const {
Chris Lattnerae939eb2005-09-13 21:44:28 +00001451 const RecordVal *R = getValue(FieldName);
1452 if (R == 0 || R->getValue() == 0)
1453 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001454 FieldName.str() + "'!\n";
Bob Wilson7248f862009-11-22 04:24:42 +00001455
Chris Lattnerae939eb2005-09-13 21:44:28 +00001456 if (const CodeInit *CI = dynamic_cast<const CodeInit*>(R->getValue()))
1457 return CI->getValue();
Chris Lattner42bb0c12009-09-18 18:31:37 +00001458 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerae939eb2005-09-13 21:44:28 +00001459 "' does not have a code initializer!";
1460}
1461
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001462
David Greene7049e792009-04-24 16:55:41 +00001463void MultiClass::dump() const {
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001464 errs() << "Record:\n";
David Greene7049e792009-04-24 16:55:41 +00001465 Rec.dump();
Bob Wilson7248f862009-11-22 04:24:42 +00001466
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001467 errs() << "Defs:\n";
David Greene7049e792009-04-24 16:55:41 +00001468 for (RecordVector::const_iterator r = DefPrototypes.begin(),
1469 rend = DefPrototypes.end();
1470 r != rend;
1471 ++r) {
1472 (*r)->dump();
1473 }
1474}
1475
1476
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001477void RecordKeeper::dump() const { errs() << *this; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001478
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001479raw_ostream &llvm::operator<<(raw_ostream &OS, const RecordKeeper &RK) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001480 OS << "------------- Classes -----------------\n";
1481 const std::map<std::string, Record*> &Classes = RK.getClasses();
1482 for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001483 E = Classes.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001484 OS << "class " << *I->second;
Misha Brukman650ba8e2005-04-22 00:00:37 +00001485
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001486 OS << "------------- Defs -----------------\n";
1487 const std::map<std::string, Record*> &Defs = RK.getDefs();
1488 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001489 E = Defs.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001490 OS << "def " << *I->second;
1491 return OS;
1492}
1493
1494
1495/// getAllDerivedDefinitions - This method returns all concrete definitions
1496/// that derive from the specified class name. If a class with the specified
1497/// name does not exist, an error is printed and true is returned.
1498std::vector<Record*>
1499RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
1500 Record *Class = Records.getClass(ClassName);
1501 if (!Class)
Misha Brukman41f9f292004-10-08 14:59:05 +00001502 throw "ERROR: Couldn't find the `" + ClassName + "' class!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001503
1504 std::vector<Record*> Defs;
1505 for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(),
1506 E = getDefs().end(); I != E; ++I)
1507 if (I->second->isSubClassOf(Class))
1508 Defs.push_back(I->second);
1509
1510 return Defs;
1511}
Brian Gaeke960707c2003-11-11 22:41:34 +00001512