blob: c427b761d8d64bcf12018d8b1cc482fd6c130ead [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) {
Bill Wendling73a48d82010-12-10 22:54:30 +000062 if (Size != 1) return 0; // Can only convert single bit.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000063 BitsInit *Ret = new BitsInit(1);
64 Ret->setBit(0, UI);
65 return Ret;
66}
67
Bill Wendling73ce4a62010-12-13 01:46:19 +000068/// canFitInBitfield - Return true if the number of bits is large enough to hold
69/// the integer value.
70static bool canFitInBitfield(int64_t Value, unsigned NumBits) {
71 if (Value >= 0) {
72 if (Value & ~((1LL << NumBits) - 1))
73 return false;
74 } else if ((Value >> NumBits) != -1 || (Value & (1LL << (NumBits-1))) == 0) {
75 return false;
76 }
77
78 return true;
79}
80
81/// convertValue from Int initializer to bits type: Split the integer up into the
82/// appropriate bits.
83///
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000084Init *BitsRecTy::convertValue(IntInit *II) {
Misha Brukman6752fb52004-06-21 18:01:47 +000085 int64_t Value = II->getValue();
Bill Wendling73a48d82010-12-10 22:54:30 +000086 // Make sure this bitfield is large enough to hold the integer value.
Bill Wendling73ce4a62010-12-13 01:46:19 +000087 if (!canFitInBitfield(Value, Size))
88 return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000089
90 BitsInit *Ret = new BitsInit(Size);
91 for (unsigned i = 0; i != Size; ++i)
Jeff Cohen0add83e2006-02-18 03:20:33 +000092 Ret->setBit(i, new BitInit(Value & (1LL << i)));
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000093
94 return Ret;
95}
96
97Init *BitsRecTy::convertValue(BitsInit *BI) {
98 // If the number of bits is right, return it. Otherwise we need to expand or
Bill Wendling73a48d82010-12-10 22:54:30 +000099 // truncate.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000100 if (BI->getNumBits() == Size) return BI;
101 return 0;
102}
103
104Init *BitsRecTy::convertValue(TypedInit *VI) {
105 if (BitsRecTy *BRT = dynamic_cast<BitsRecTy*>(VI->getType()))
106 if (BRT->Size == Size) {
107 BitsInit *Ret = new BitsInit(Size);
108 for (unsigned i = 0; i != Size; ++i)
Jeff Cohen88e7b722005-04-22 04:13:13 +0000109 Ret->setBit(i, new VarBitInit(VI, i));
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000110 return Ret;
111 }
Bill Wendling73ce4a62010-12-13 01:46:19 +0000112
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000113 if (Size == 1 && dynamic_cast<BitRecTy*>(VI->getType())) {
114 BitsInit *Ret = new BitsInit(1);
115 Ret->setBit(0, VI);
116 return Ret;
117 }
Misha Brukman650ba8e2005-04-22 00:00:37 +0000118
Bill Wendling73ce4a62010-12-13 01:46:19 +0000119 if (TernOpInit *Tern = dynamic_cast<TernOpInit*>(VI)) {
120 if (Tern->getOpcode() == TernOpInit::IF) {
121 Init *LHS = Tern->getLHS();
122 Init *MHS = Tern->getMHS();
123 Init *RHS = Tern->getRHS();
124
125 IntInit *MHSi = dynamic_cast<IntInit*>(MHS);
126 IntInit *RHSi = dynamic_cast<IntInit*>(RHS);
127
128 if (MHSi && RHSi) {
129 int64_t MHSVal = MHSi->getValue();
130 int64_t RHSVal = RHSi->getValue();
131
132 if (canFitInBitfield(MHSVal, Size) && canFitInBitfield(RHSVal, Size)) {
133 BitsInit *Ret = new BitsInit(Size);
134
135 for (unsigned i = 0; i != Size; ++i)
136 Ret->setBit(i, new TernOpInit(TernOpInit::IF, LHS,
137 new IntInit((MHSVal & (1LL << i)) ? 1 : 0),
138 new IntInit((RHSVal & (1LL << i)) ? 1 : 0),
139 VI->getType()));
140
141 return Ret;
142 }
143 } else {
144 BitsInit *MHSbs = dynamic_cast<BitsInit*>(MHS);
145 BitsInit *RHSbs = dynamic_cast<BitsInit*>(RHS);
146
147 if (MHSbs && RHSbs) {
148 BitsInit *Ret = new BitsInit(Size);
149
150 for (unsigned i = 0; i != Size; ++i)
151 Ret->setBit(i, new TernOpInit(TernOpInit::IF, LHS,
152 MHSbs->getBit(i),
153 RHSbs->getBit(i),
154 VI->getType()));
155
156 return Ret;
157 }
158 }
159 }
160 }
161
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000162 return 0;
163}
164
165Init *IntRecTy::convertValue(BitInit *BI) {
166 return new IntInit(BI->getValue());
167}
168
169Init *IntRecTy::convertValue(BitsInit *BI) {
Dan Gohmanca0546f2008-10-17 01:33:43 +0000170 int64_t Result = 0;
Misha Brukman650ba8e2005-04-22 00:00:37 +0000171 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000172 if (BitInit *Bit = dynamic_cast<BitInit*>(BI->getBit(i))) {
173 Result |= Bit->getValue() << i;
174 } else {
175 return 0;
176 }
177 return new IntInit(Result);
178}
179
180Init *IntRecTy::convertValue(TypedInit *TI) {
181 if (TI->getType()->typeIsConvertibleTo(this))
182 return TI; // Accept variable if already of the right type!
183 return 0;
184}
185
David Greenee8f3b272009-05-14 21:22:49 +0000186Init *StringRecTy::convertValue(UnOpInit *BO) {
187 if (BO->getOpcode() == UnOpInit::CAST) {
188 Init *L = BO->getOperand()->convertInitializerTo(this);
189 if (L == 0) return 0;
190 if (L != BO->getOperand())
191 return new UnOpInit(UnOpInit::CAST, L, new StringRecTy);
192 return BO;
193 }
David Greene5d0c0512009-05-14 20:54:48 +0000194
David Greenee8f3b272009-05-14 21:22:49 +0000195 return convertValue((TypedInit*)BO);
196}
David Greene5d0c0512009-05-14 20:54:48 +0000197
Chris Lattner51ffbf12006-03-31 21:53:49 +0000198Init *StringRecTy::convertValue(BinOpInit *BO) {
199 if (BO->getOpcode() == BinOpInit::STRCONCAT) {
200 Init *L = BO->getLHS()->convertInitializerTo(this);
201 Init *R = BO->getRHS()->convertInitializerTo(this);
202 if (L == 0 || R == 0) return 0;
203 if (L != BO->getLHS() || R != BO->getRHS())
David Greene196ac3c2009-04-23 21:25:15 +0000204 return new BinOpInit(BinOpInit::STRCONCAT, L, R, new StringRecTy);
Chris Lattner51ffbf12006-03-31 21:53:49 +0000205 return BO;
206 }
David Greene196ac3c2009-04-23 21:25:15 +0000207
208 return convertValue((TypedInit*)BO);
Chris Lattner51ffbf12006-03-31 21:53:49 +0000209}
210
211
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000212Init *StringRecTy::convertValue(TypedInit *TI) {
213 if (dynamic_cast<StringRecTy*>(TI->getType()))
214 return TI; // Accept variable if already of the right type!
215 return 0;
216}
217
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000218std::string ListRecTy::getAsString() const {
219 return "list<" + Ty->getAsString() + ">";
220}
221
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000222Init *ListRecTy::convertValue(ListInit *LI) {
223 std::vector<Init*> Elements;
224
225 // Verify that all of the elements of the list are subclasses of the
226 // appropriate class!
227 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
228 if (Init *CI = LI->getElement(i)->convertInitializerTo(Ty))
229 Elements.push_back(CI);
230 else
231 return 0;
232
David Greene8618f952009-06-08 20:23:18 +0000233 ListRecTy *LType = dynamic_cast<ListRecTy*>(LI->getType());
234 if (LType == 0) {
235 return 0;
236 }
237
238 return new ListInit(Elements, new ListRecTy(Ty));
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000239}
240
241Init *ListRecTy::convertValue(TypedInit *TI) {
242 // Ensure that TI is compatible with our class.
243 if (ListRecTy *LRT = dynamic_cast<ListRecTy*>(TI->getType()))
244 if (LRT->getElementType()->typeIsConvertibleTo(getElementType()))
245 return TI;
246 return 0;
247}
248
249Init *CodeRecTy::convertValue(TypedInit *TI) {
250 if (TI->getType()->typeIsConvertibleTo(this))
251 return TI;
252 return 0;
253}
254
255Init *DagRecTy::convertValue(TypedInit *TI) {
256 if (TI->getType()->typeIsConvertibleTo(this))
257 return TI;
258 return 0;
259}
260
David Greenee8f3b272009-05-14 21:22:49 +0000261Init *DagRecTy::convertValue(UnOpInit *BO) {
262 if (BO->getOpcode() == UnOpInit::CAST) {
263 Init *L = BO->getOperand()->convertInitializerTo(this);
264 if (L == 0) return 0;
265 if (L != BO->getOperand())
266 return new UnOpInit(UnOpInit::CAST, L, new DagRecTy);
267 return BO;
268 }
269 return 0;
270}
David Greene5d0c0512009-05-14 20:54:48 +0000271
Evan Chenga32dee22007-05-15 01:23:24 +0000272Init *DagRecTy::convertValue(BinOpInit *BO) {
273 if (BO->getOpcode() == BinOpInit::CONCAT) {
274 Init *L = BO->getLHS()->convertInitializerTo(this);
275 Init *R = BO->getRHS()->convertInitializerTo(this);
276 if (L == 0 || R == 0) return 0;
277 if (L != BO->getLHS() || R != BO->getRHS())
David Greene196ac3c2009-04-23 21:25:15 +0000278 return new BinOpInit(BinOpInit::CONCAT, L, R, new DagRecTy);
Evan Chenga32dee22007-05-15 01:23:24 +0000279 return BO;
280 }
281 return 0;
282}
283
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000284std::string RecordRecTy::getAsString() const {
285 return Rec->getName();
286}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000287
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000288Init *RecordRecTy::convertValue(DefInit *DI) {
289 // Ensure that DI is a subclass of Rec.
290 if (!DI->getDef()->isSubClassOf(Rec))
291 return 0;
292 return DI;
293}
294
295Init *RecordRecTy::convertValue(TypedInit *TI) {
296 // Ensure that TI is compatible with Rec.
297 if (RecordRecTy *RRT = dynamic_cast<RecordRecTy*>(TI->getType()))
298 if (RRT->getRecord()->isSubClassOf(getRecord()) ||
299 RRT->getRecord() == getRecord())
300 return TI;
301 return 0;
302}
303
304bool RecordRecTy::baseClassOf(const RecordRecTy *RHS) const {
Bruno Cardoso Lopesdeb20022010-06-17 23:00:16 +0000305 if (Rec == RHS->getRecord() || RHS->getRecord()->isSubClassOf(Rec))
306 return true;
307
308 const std::vector<Record*> &SC = Rec->getSuperClasses();
309 for (unsigned i = 0, e = SC.size(); i != e; ++i)
310 if (RHS->getRecord()->isSubClassOf(SC[i]))
311 return true;
312
313 return false;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000314}
315
316
Bob Wilson7248f862009-11-22 04:24:42 +0000317/// resolveTypes - Find a common type that T1 and T2 convert to.
David Greene8618f952009-06-08 20:23:18 +0000318/// Return 0 if no such type exists.
319///
320RecTy *llvm::resolveTypes(RecTy *T1, RecTy *T2) {
321 if (!T1->typeIsConvertibleTo(T2)) {
322 if (!T2->typeIsConvertibleTo(T1)) {
323 // If one is a Record type, check superclasses
324 RecordRecTy *RecTy1 = dynamic_cast<RecordRecTy*>(T1);
325 if (RecTy1) {
326 // See if T2 inherits from a type T1 also inherits from
Bob Wilson7248f862009-11-22 04:24:42 +0000327 const std::vector<Record *> &T1SuperClasses =
328 RecTy1->getRecord()->getSuperClasses();
David Greene8618f952009-06-08 20:23:18 +0000329 for(std::vector<Record *>::const_iterator i = T1SuperClasses.begin(),
330 iend = T1SuperClasses.end();
331 i != iend;
332 ++i) {
333 RecordRecTy *SuperRecTy1 = new RecordRecTy(*i);
334 RecTy *NewType1 = resolveTypes(SuperRecTy1, T2);
335 if (NewType1 != 0) {
336 if (NewType1 != SuperRecTy1) {
337 delete SuperRecTy1;
338 }
339 return NewType1;
340 }
341 }
342 }
343 RecordRecTy *RecTy2 = dynamic_cast<RecordRecTy*>(T2);
344 if (RecTy2) {
345 // See if T1 inherits from a type T2 also inherits from
Bob Wilson7248f862009-11-22 04:24:42 +0000346 const std::vector<Record *> &T2SuperClasses =
347 RecTy2->getRecord()->getSuperClasses();
348 for (std::vector<Record *>::const_iterator i = T2SuperClasses.begin(),
David Greene8618f952009-06-08 20:23:18 +0000349 iend = T2SuperClasses.end();
350 i != iend;
351 ++i) {
352 RecordRecTy *SuperRecTy2 = new RecordRecTy(*i);
353 RecTy *NewType2 = resolveTypes(T1, SuperRecTy2);
354 if (NewType2 != 0) {
355 if (NewType2 != SuperRecTy2) {
356 delete SuperRecTy2;
357 }
358 return NewType2;
359 }
360 }
361 }
362 return 0;
363 }
364 return T2;
365 }
366 return T1;
367}
368
369
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000370//===----------------------------------------------------------------------===//
371// Initializer implementations
372//===----------------------------------------------------------------------===//
373
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000374void Init::dump() const { return print(errs()); }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000375
376Init *BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
377 BitsInit *BI = new BitsInit(Bits.size());
378 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
379 if (Bits[i] >= getNumBits()) {
380 delete BI;
381 return 0;
382 }
383 BI->setBit(i, getBit(Bits[i]));
384 }
385 return BI;
386}
387
Chris Lattner695506c2007-11-22 21:05:25 +0000388std::string BitsInit::getAsString() const {
Chris Lattner695506c2007-11-22 21:05:25 +0000389 std::string Result = "{ ";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000390 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
Chris Lattner695506c2007-11-22 21:05:25 +0000391 if (i) Result += ", ";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000392 if (Init *Bit = getBit(e-i-1))
Chris Lattner695506c2007-11-22 21:05:25 +0000393 Result += Bit->getAsString();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000394 else
Chris Lattner695506c2007-11-22 21:05:25 +0000395 Result += "*";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000396 }
Chris Lattner695506c2007-11-22 21:05:25 +0000397 return Result + " }";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000398}
399
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000400// resolveReferences - If there are any field references that refer to fields
401// that have been filled in, we can propagate the values now.
402//
Chris Lattneref943742005-04-19 03:36:21 +0000403Init *BitsInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000404 bool Changed = false;
405 BitsInit *New = new BitsInit(getNumBits());
406
407 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
408 Init *B;
409 Init *CurBit = getBit(i);
410
411 do {
412 B = CurBit;
Chris Lattneref943742005-04-19 03:36:21 +0000413 CurBit = CurBit->resolveReferences(R, RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000414 Changed |= B != CurBit;
415 } while (B != CurBit);
416 New->setBit(i, CurBit);
417 }
418
419 if (Changed)
420 return New;
421 delete New;
422 return this;
423}
424
Chris Lattner695506c2007-11-22 21:05:25 +0000425std::string IntInit::getAsString() const {
426 return itostr(Value);
427}
428
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000429Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
430 BitsInit *BI = new BitsInit(Bits.size());
431
432 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
Dan Gohmanca0546f2008-10-17 01:33:43 +0000433 if (Bits[i] >= 64) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000434 delete BI;
435 return 0;
436 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000437 BI->setBit(i, new BitInit(Value & (INT64_C(1) << Bits[i])));
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000438 }
439 return BI;
440}
441
Chris Lattner8bf9e062004-07-26 23:21:34 +0000442Init *ListInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
443 std::vector<Init*> Vals;
444 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
445 if (Elements[i] >= getSize())
446 return 0;
447 Vals.push_back(getElement(Elements[i]));
448 }
David Greene8618f952009-06-08 20:23:18 +0000449 return new ListInit(Vals, getType());
Chris Lattner8bf9e062004-07-26 23:21:34 +0000450}
451
Chris Lattnercbebe462007-02-27 22:08:27 +0000452Record *ListInit::getElementAsRecord(unsigned i) const {
453 assert(i < Values.size() && "List element index out of range!");
454 DefInit *DI = dynamic_cast<DefInit*>(Values[i]);
455 if (DI == 0) throw "Expected record in list!";
456 return DI->getDef();
457}
458
Chris Lattneref943742005-04-19 03:36:21 +0000459Init *ListInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattner577fc3f2004-07-27 01:01:21 +0000460 std::vector<Init*> Resolved;
461 Resolved.reserve(getSize());
462 bool Changed = false;
463
464 for (unsigned i = 0, e = getSize(); i != e; ++i) {
465 Init *E;
466 Init *CurElt = getElement(i);
467
468 do {
469 E = CurElt;
Chris Lattneref943742005-04-19 03:36:21 +0000470 CurElt = CurElt->resolveReferences(R, RV);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000471 Changed |= E != CurElt;
472 } while (E != CurElt);
473 Resolved.push_back(E);
474 }
475
476 if (Changed)
David Greene8618f952009-06-08 20:23:18 +0000477 return new ListInit(Resolved, getType());
Chris Lattner577fc3f2004-07-27 01:01:21 +0000478 return this;
479}
480
David Greene8618f952009-06-08 20:23:18 +0000481Init *ListInit::resolveListElementReference(Record &R, const RecordVal *IRV,
Bob Wilson7248f862009-11-22 04:24:42 +0000482 unsigned Elt) {
David Greene8618f952009-06-08 20:23:18 +0000483 if (Elt >= getSize())
484 return 0; // Out of range reference.
485 Init *E = getElement(Elt);
Bob Wilson67e6cab2009-11-22 03:58:57 +0000486 // If the element is set to some value, or if we are resolving a reference
487 // to a specific variable and that variable is explicitly unset, then
488 // replace the VarListElementInit with it.
489 if (IRV || !dynamic_cast<UnsetInit*>(E))
490 return E;
David Greene8618f952009-06-08 20:23:18 +0000491 return 0;
492}
493
Chris Lattner695506c2007-11-22 21:05:25 +0000494std::string ListInit::getAsString() const {
495 std::string Result = "[";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000496 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
Chris Lattner695506c2007-11-22 21:05:25 +0000497 if (i) Result += ", ";
498 Result += Values[i]->getAsString();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000499 }
Chris Lattner695506c2007-11-22 21:05:25 +0000500 return Result + "]";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000501}
502
David Greene5d0c0512009-05-14 20:54:48 +0000503Init *OpInit::resolveBitReference(Record &R, const RecordVal *IRV,
Bob Wilson7248f862009-11-22 04:24:42 +0000504 unsigned Bit) {
David Greene5d0c0512009-05-14 20:54:48 +0000505 Init *Folded = Fold(&R, 0);
506
507 if (Folded != this) {
508 TypedInit *Typed = dynamic_cast<TypedInit *>(Folded);
509 if (Typed) {
510 return Typed->resolveBitReference(R, IRV, Bit);
Bob Wilson7248f862009-11-22 04:24:42 +0000511 }
David Greene5d0c0512009-05-14 20:54:48 +0000512 }
Bob Wilson7248f862009-11-22 04:24:42 +0000513
David Greene5d0c0512009-05-14 20:54:48 +0000514 return 0;
515}
516
517Init *OpInit::resolveListElementReference(Record &R, const RecordVal *IRV,
Bob Wilson7248f862009-11-22 04:24:42 +0000518 unsigned Elt) {
David Greene5d0c0512009-05-14 20:54:48 +0000519 Init *Folded = Fold(&R, 0);
520
521 if (Folded != this) {
522 TypedInit *Typed = dynamic_cast<TypedInit *>(Folded);
523 if (Typed) {
524 return Typed->resolveListElementReference(R, IRV, Elt);
Bob Wilson7248f862009-11-22 04:24:42 +0000525 }
David Greene5d0c0512009-05-14 20:54:48 +0000526 }
Bob Wilson7248f862009-11-22 04:24:42 +0000527
David Greene5d0c0512009-05-14 20:54:48 +0000528 return 0;
529}
530
David Greenee8f3b272009-05-14 21:22:49 +0000531Init *UnOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
532 switch (getOpcode()) {
533 default: assert(0 && "Unknown unop");
534 case CAST: {
David Greeneefa19612009-06-29 20:05:29 +0000535 if (getType()->getAsString() == "string") {
536 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
537 if (LHSs) {
538 return LHSs;
539 }
David Greene5d0c0512009-05-14 20:54:48 +0000540
David Greeneefa19612009-06-29 20:05:29 +0000541 DefInit *LHSd = dynamic_cast<DefInit*>(LHS);
542 if (LHSd) {
543 return new StringInit(LHSd->getDef()->getName());
544 }
Bob Wilson7248f862009-11-22 04:24:42 +0000545 } else {
David Greeneefa19612009-06-29 20:05:29 +0000546 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
547 if (LHSs) {
548 std::string Name = LHSs->getValue();
549
550 // From TGParser::ParseIDValue
551 if (CurRec) {
552 if (const RecordVal *RV = CurRec->getValue(Name)) {
Chris Lattner94026332010-10-06 00:19:21 +0000553 if (RV->getType() != getType())
554 throw "type mismatch in cast";
David Greeneefa19612009-06-29 20:05:29 +0000555 return new VarInit(Name, RV->getType());
David Greenee8f3b272009-05-14 21:22:49 +0000556 }
David Greeneefa19612009-06-29 20:05:29 +0000557
558 std::string TemplateArgName = CurRec->getName()+":"+Name;
559 if (CurRec->isTemplateArg(TemplateArgName)) {
560 const RecordVal *RV = CurRec->getValue(TemplateArgName);
561 assert(RV && "Template arg doesn't exist??");
562
Chris Lattner94026332010-10-06 00:19:21 +0000563 if (RV->getType() != getType())
564 throw "type mismatch in cast";
David Greeneefa19612009-06-29 20:05:29 +0000565
566 return new VarInit(TemplateArgName, RV->getType());
567 }
568 }
569
570 if (CurMultiClass) {
571 std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
572 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
573 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
574 assert(RV && "Template arg doesn't exist??");
Bob Wilson7248f862009-11-22 04:24:42 +0000575
Chris Lattner94026332010-10-06 00:19:21 +0000576 if (RV->getType() != getType())
577 throw "type mismatch in cast";
Bob Wilson7248f862009-11-22 04:24:42 +0000578
David Greeneefa19612009-06-29 20:05:29 +0000579 return new VarInit(MCName, RV->getType());
580 }
David Greenee8f3b272009-05-14 21:22:49 +0000581 }
Bob Wilson7248f862009-11-22 04:24:42 +0000582
Chris Lattner77d369c2010-12-13 00:23:57 +0000583 if (Record *D = (CurRec->getRecords()).getDef(Name))
David Greeneefa19612009-06-29 20:05:29 +0000584 return new DefInit(D);
David Greene5d0c0512009-05-14 20:54:48 +0000585
Jim Grosbach59ddb732011-05-06 18:47:45 +0000586 throw TGError(CurRec->getLoc(), "Undefined reference:'" + Name + "'\n");
David Greenee8f3b272009-05-14 21:22:49 +0000587 }
David Greenee8f3b272009-05-14 21:22:49 +0000588 }
589 break;
590 }
David Greene2f7cf7f2011-01-07 17:05:37 +0000591 case HEAD: {
David Greened571b3c2009-05-14 22:38:31 +0000592 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
593 if (LHSl) {
594 if (LHSl->getSize() == 0) {
595 assert(0 && "Empty list in car");
596 return 0;
597 }
598 return LHSl->getElement(0);
599 }
600 break;
601 }
David Greene2f7cf7f2011-01-07 17:05:37 +0000602 case TAIL: {
David Greened571b3c2009-05-14 22:38:31 +0000603 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
604 if (LHSl) {
605 if (LHSl->getSize() == 0) {
606 assert(0 && "Empty list in cdr");
607 return 0;
608 }
Bob Wilson7248f862009-11-22 04:24:42 +0000609 ListInit *Result = new ListInit(LHSl->begin()+1, LHSl->end(),
610 LHSl->getType());
David Greened571b3c2009-05-14 22:38:31 +0000611 return Result;
612 }
613 break;
614 }
David Greene2f7cf7f2011-01-07 17:05:37 +0000615 case EMPTY: {
David Greened571b3c2009-05-14 22:38:31 +0000616 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
617 if (LHSl) {
618 if (LHSl->getSize() == 0) {
619 return new IntInit(1);
Bob Wilson7248f862009-11-22 04:24:42 +0000620 } else {
David Greened571b3c2009-05-14 22:38:31 +0000621 return new IntInit(0);
622 }
623 }
David Greene8618f952009-06-08 20:23:18 +0000624 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
625 if (LHSs) {
626 if (LHSs->getValue().empty()) {
627 return new IntInit(1);
Bob Wilson7248f862009-11-22 04:24:42 +0000628 } else {
David Greene8618f952009-06-08 20:23:18 +0000629 return new IntInit(0);
630 }
631 }
Bob Wilson7248f862009-11-22 04:24:42 +0000632
David Greened571b3c2009-05-14 22:38:31 +0000633 break;
634 }
David Greenee8f3b272009-05-14 21:22:49 +0000635 }
636 return this;
637}
David Greene5d0c0512009-05-14 20:54:48 +0000638
David Greenee8f3b272009-05-14 21:22:49 +0000639Init *UnOpInit::resolveReferences(Record &R, const RecordVal *RV) {
640 Init *lhs = LHS->resolveReferences(R, RV);
Bob Wilson7248f862009-11-22 04:24:42 +0000641
David Greenee8f3b272009-05-14 21:22:49 +0000642 if (LHS != lhs)
643 return (new UnOpInit(getOpcode(), lhs, getType()))->Fold(&R, 0);
644 return Fold(&R, 0);
645}
David Greene5d0c0512009-05-14 20:54:48 +0000646
David Greenee8f3b272009-05-14 21:22:49 +0000647std::string UnOpInit::getAsString() const {
648 std::string Result;
649 switch (Opc) {
650 case CAST: Result = "!cast<" + getType()->getAsString() + ">"; break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000651 case HEAD: Result = "!head"; break;
652 case TAIL: Result = "!tail"; break;
653 case EMPTY: Result = "!empty"; break;
David Greenee8f3b272009-05-14 21:22:49 +0000654 }
655 return Result + "(" + LHS->getAsString() + ")";
656}
David Greene5d0c0512009-05-14 20:54:48 +0000657
David Greenea9c6c5d2009-04-22 20:18:10 +0000658Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
Chris Lattner51ffbf12006-03-31 21:53:49 +0000659 switch (getOpcode()) {
660 default: assert(0 && "Unknown binop");
Evan Chenga32dee22007-05-15 01:23:24 +0000661 case CONCAT: {
662 DagInit *LHSs = dynamic_cast<DagInit*>(LHS);
663 DagInit *RHSs = dynamic_cast<DagInit*>(RHS);
664 if (LHSs && RHSs) {
665 DefInit *LOp = dynamic_cast<DefInit*>(LHSs->getOperator());
666 DefInit *ROp = dynamic_cast<DefInit*>(RHSs->getOperator());
Chris Lattner81fd1f22010-03-18 21:07:51 +0000667 if (LOp == 0 || ROp == 0 || LOp->getDef() != ROp->getDef())
668 throw "Concated Dag operators do not match!";
Evan Chenga32dee22007-05-15 01:23:24 +0000669 std::vector<Init*> Args;
670 std::vector<std::string> ArgNames;
671 for (unsigned i = 0, e = LHSs->getNumArgs(); i != e; ++i) {
672 Args.push_back(LHSs->getArg(i));
673 ArgNames.push_back(LHSs->getArgName(i));
674 }
675 for (unsigned i = 0, e = RHSs->getNumArgs(); i != e; ++i) {
676 Args.push_back(RHSs->getArg(i));
677 ArgNames.push_back(RHSs->getArgName(i));
678 }
Nate Begemandbe3f772009-03-19 05:21:56 +0000679 return new DagInit(LHSs->getOperator(), "", Args, ArgNames);
Evan Chenga32dee22007-05-15 01:23:24 +0000680 }
681 break;
682 }
Chris Lattner51ffbf12006-03-31 21:53:49 +0000683 case STRCONCAT: {
684 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
685 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
686 if (LHSs && RHSs)
687 return new StringInit(LHSs->getValue() + RHSs->getValue());
688 break;
689 }
David Greene297bfe62010-01-05 19:11:42 +0000690 case EQ: {
Bruno Cardoso Lopes77a4a562010-06-16 23:24:12 +0000691 // try to fold eq comparison for 'bit' and 'int', otherwise fallback
692 // to string objects.
693 IntInit* L =
694 dynamic_cast<IntInit*>(LHS->convertInitializerTo(new IntRecTy()));
695 IntInit* R =
696 dynamic_cast<IntInit*>(RHS->convertInitializerTo(new IntRecTy()));
697
698 if (L && R)
699 return new IntInit(L->getValue() == R->getValue());
700
David Greene297bfe62010-01-05 19:11:42 +0000701 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
702 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
Bruno Cardoso Lopes77a4a562010-06-16 23:24:12 +0000703
704 // Make sure we've resolved
David Greene297bfe62010-01-05 19:11:42 +0000705 if (LHSs && RHSs)
706 return new IntInit(LHSs->getValue() == RHSs->getValue());
707
708 break;
709 }
Chris Lattner51ffbf12006-03-31 21:53:49 +0000710 case SHL:
711 case SRA:
712 case SRL: {
713 IntInit *LHSi = dynamic_cast<IntInit*>(LHS);
714 IntInit *RHSi = dynamic_cast<IntInit*>(RHS);
715 if (LHSi && RHSi) {
Dan Gohmanca0546f2008-10-17 01:33:43 +0000716 int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
717 int64_t Result;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000718 switch (getOpcode()) {
719 default: assert(0 && "Bad opcode!");
720 case SHL: Result = LHSv << RHSv; break;
721 case SRA: Result = LHSv >> RHSv; break;
Dan Gohmanca0546f2008-10-17 01:33:43 +0000722 case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000723 }
724 return new IntInit(Result);
725 }
726 break;
727 }
728 }
729 return this;
730}
731
732Init *BinOpInit::resolveReferences(Record &R, const RecordVal *RV) {
733 Init *lhs = LHS->resolveReferences(R, RV);
734 Init *rhs = RHS->resolveReferences(R, RV);
Bob Wilson7248f862009-11-22 04:24:42 +0000735
Chris Lattner51ffbf12006-03-31 21:53:49 +0000736 if (LHS != lhs || RHS != rhs)
David Greene196ac3c2009-04-23 21:25:15 +0000737 return (new BinOpInit(getOpcode(), lhs, rhs, getType()))->Fold(&R, 0);
David Greenea9c6c5d2009-04-22 20:18:10 +0000738 return Fold(&R, 0);
Chris Lattner51ffbf12006-03-31 21:53:49 +0000739}
740
Chris Lattner695506c2007-11-22 21:05:25 +0000741std::string BinOpInit::getAsString() const {
742 std::string Result;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000743 switch (Opc) {
Chris Lattner695506c2007-11-22 21:05:25 +0000744 case CONCAT: Result = "!con"; break;
745 case SHL: Result = "!shl"; break;
746 case SRA: Result = "!sra"; break;
747 case SRL: Result = "!srl"; break;
David Greene297bfe62010-01-05 19:11:42 +0000748 case EQ: Result = "!eq"; break;
Chris Lattner695506c2007-11-22 21:05:25 +0000749 case STRCONCAT: Result = "!strconcat"; break;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000750 }
Chris Lattner695506c2007-11-22 21:05:25 +0000751 return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")";
Chris Lattner51ffbf12006-03-31 21:53:49 +0000752}
753
David Greenee917fff2009-05-14 22:23:47 +0000754static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
755 Record *CurRec, MultiClass *CurMultiClass);
756
757static Init *EvaluateOperation(OpInit *RHSo, Init *LHS, Init *Arg,
758 RecTy *Type, Record *CurRec,
759 MultiClass *CurMultiClass) {
760 std::vector<Init *> NewOperands;
761
762 TypedInit *TArg = dynamic_cast<TypedInit*>(Arg);
763
764 // If this is a dag, recurse
765 if (TArg && TArg->getType()->getAsString() == "dag") {
766 Init *Result = ForeachHelper(LHS, Arg, RHSo, Type,
767 CurRec, CurMultiClass);
768 if (Result != 0) {
769 return Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000770 } else {
David Greenee917fff2009-05-14 22:23:47 +0000771 return 0;
772 }
773 }
774
775 for (int i = 0; i < RHSo->getNumOperands(); ++i) {
776 OpInit *RHSoo = dynamic_cast<OpInit*>(RHSo->getOperand(i));
777
778 if (RHSoo) {
779 Init *Result = EvaluateOperation(RHSoo, LHS, Arg,
780 Type, CurRec, CurMultiClass);
781 if (Result != 0) {
782 NewOperands.push_back(Result);
Bob Wilson7248f862009-11-22 04:24:42 +0000783 } else {
David Greenee917fff2009-05-14 22:23:47 +0000784 NewOperands.push_back(Arg);
785 }
Bob Wilson7248f862009-11-22 04:24:42 +0000786 } else if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
David Greenee917fff2009-05-14 22:23:47 +0000787 NewOperands.push_back(Arg);
Bob Wilson7248f862009-11-22 04:24:42 +0000788 } else {
David Greenee917fff2009-05-14 22:23:47 +0000789 NewOperands.push_back(RHSo->getOperand(i));
790 }
791 }
792
793 // Now run the operator and use its result as the new leaf
794 OpInit *NewOp = RHSo->clone(NewOperands);
795 Init *NewVal = NewOp->Fold(CurRec, CurMultiClass);
796 if (NewVal != NewOp) {
797 delete NewOp;
798 return NewVal;
799 }
800 return 0;
801}
802
803static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
804 Record *CurRec, MultiClass *CurMultiClass) {
805 DagInit *MHSd = dynamic_cast<DagInit*>(MHS);
806 ListInit *MHSl = dynamic_cast<ListInit*>(MHS);
807
808 DagRecTy *DagType = dynamic_cast<DagRecTy*>(Type);
809 ListRecTy *ListType = dynamic_cast<ListRecTy*>(Type);
810
811 OpInit *RHSo = dynamic_cast<OpInit*>(RHS);
812
813 if (!RHSo) {
Jim Grosbach59ddb732011-05-06 18:47:45 +0000814 throw TGError(CurRec->getLoc(), "!foreach requires an operator\n");
David Greenee917fff2009-05-14 22:23:47 +0000815 }
816
817 TypedInit *LHSt = dynamic_cast<TypedInit*>(LHS);
818
819 if (!LHSt) {
Jim Grosbach59ddb732011-05-06 18:47:45 +0000820 throw TGError(CurRec->getLoc(), "!foreach requires typed variable\n");
David Greenee917fff2009-05-14 22:23:47 +0000821 }
822
Nick Lewycky94298222009-05-15 03:07:14 +0000823 if ((MHSd && DagType) || (MHSl && ListType)) {
David Greenee917fff2009-05-14 22:23:47 +0000824 if (MHSd) {
825 Init *Val = MHSd->getOperator();
826 Init *Result = EvaluateOperation(RHSo, LHS, Val,
827 Type, CurRec, CurMultiClass);
828 if (Result != 0) {
829 Val = Result;
830 }
831
832 std::vector<std::pair<Init *, std::string> > args;
833 for (unsigned int i = 0; i < MHSd->getNumArgs(); ++i) {
834 Init *Arg;
835 std::string ArgName;
836 Arg = MHSd->getArg(i);
837 ArgName = MHSd->getArgName(i);
838
839 // Process args
840 Init *Result = EvaluateOperation(RHSo, LHS, Arg, Type,
841 CurRec, CurMultiClass);
842 if (Result != 0) {
843 Arg = Result;
844 }
845
846 // TODO: Process arg names
847 args.push_back(std::make_pair(Arg, ArgName));
848 }
849
850 return new DagInit(Val, "", args);
851 }
852 if (MHSl) {
853 std::vector<Init *> NewOperands;
854 std::vector<Init *> NewList(MHSl->begin(), MHSl->end());
855
856 for (ListInit::iterator li = NewList.begin(),
857 liend = NewList.end();
858 li != liend;
859 ++li) {
860 Init *Item = *li;
861 NewOperands.clear();
862 for(int i = 0; i < RHSo->getNumOperands(); ++i) {
863 // First, replace the foreach variable with the list item
864 if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
865 NewOperands.push_back(Item);
Bob Wilson7248f862009-11-22 04:24:42 +0000866 } else {
David Greenee917fff2009-05-14 22:23:47 +0000867 NewOperands.push_back(RHSo->getOperand(i));
868 }
869 }
870
871 // Now run the operator and use its result as the new list item
872 OpInit *NewOp = RHSo->clone(NewOperands);
873 Init *NewItem = NewOp->Fold(CurRec, CurMultiClass);
874 if (NewItem != NewOp) {
875 *li = NewItem;
876 delete NewOp;
877 }
878 }
David Greene8618f952009-06-08 20:23:18 +0000879 return new ListInit(NewList, MHSl->getType());
David Greenee917fff2009-05-14 22:23:47 +0000880 }
881 }
882 return 0;
883}
884
David Greene98ed3c72009-05-14 21:54:42 +0000885Init *TernOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
886 switch (getOpcode()) {
887 default: assert(0 && "Unknown binop");
888 case SUBST: {
889 DefInit *LHSd = dynamic_cast<DefInit*>(LHS);
890 VarInit *LHSv = dynamic_cast<VarInit*>(LHS);
891 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
David Greene196ac3c2009-04-23 21:25:15 +0000892
David Greene98ed3c72009-05-14 21:54:42 +0000893 DefInit *MHSd = dynamic_cast<DefInit*>(MHS);
894 VarInit *MHSv = dynamic_cast<VarInit*>(MHS);
895 StringInit *MHSs = dynamic_cast<StringInit*>(MHS);
David Greene5d0c0512009-05-14 20:54:48 +0000896
David Greene98ed3c72009-05-14 21:54:42 +0000897 DefInit *RHSd = dynamic_cast<DefInit*>(RHS);
898 VarInit *RHSv = dynamic_cast<VarInit*>(RHS);
899 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
David Greene5d0c0512009-05-14 20:54:48 +0000900
David Greene98ed3c72009-05-14 21:54:42 +0000901 if ((LHSd && MHSd && RHSd)
902 || (LHSv && MHSv && RHSv)
903 || (LHSs && MHSs && RHSs)) {
904 if (RHSd) {
905 Record *Val = RHSd->getDef();
906 if (LHSd->getAsString() == RHSd->getAsString()) {
907 Val = MHSd->getDef();
908 }
909 return new DefInit(Val);
910 }
911 if (RHSv) {
912 std::string Val = RHSv->getName();
913 if (LHSv->getAsString() == RHSv->getAsString()) {
914 Val = MHSv->getName();
915 }
916 return new VarInit(Val, getType());
917 }
918 if (RHSs) {
919 std::string Val = RHSs->getValue();
David Greene5d0c0512009-05-14 20:54:48 +0000920
David Greene98ed3c72009-05-14 21:54:42 +0000921 std::string::size_type found;
David Greenedbf70742009-12-21 21:21:34 +0000922 std::string::size_type idx = 0;
David Greene98ed3c72009-05-14 21:54:42 +0000923 do {
David Greenedbf70742009-12-21 21:21:34 +0000924 found = Val.find(LHSs->getValue(), idx);
David Greene98ed3c72009-05-14 21:54:42 +0000925 if (found != std::string::npos) {
926 Val.replace(found, LHSs->getValue().size(), MHSs->getValue());
927 }
David Greenedbf70742009-12-21 21:21:34 +0000928 idx = found + MHSs->getValue().size();
David Greene98ed3c72009-05-14 21:54:42 +0000929 } while (found != std::string::npos);
David Greene5d0c0512009-05-14 20:54:48 +0000930
David Greene98ed3c72009-05-14 21:54:42 +0000931 return new StringInit(Val);
932 }
933 }
934 break;
Bob Wilson7248f862009-11-22 04:24:42 +0000935 }
David Greene5d0c0512009-05-14 20:54:48 +0000936
David Greene98ed3c72009-05-14 21:54:42 +0000937 case FOREACH: {
David Greenee917fff2009-05-14 22:23:47 +0000938 Init *Result = ForeachHelper(LHS, MHS, RHS, getType(),
939 CurRec, CurMultiClass);
940 if (Result != 0) {
941 return Result;
David Greene98ed3c72009-05-14 21:54:42 +0000942 }
943 break;
944 }
David Greene3587eed2009-05-14 23:26:46 +0000945
946 case IF: {
Bruno Cardoso Lopes7f4235d2010-06-17 01:50:39 +0000947 IntInit *LHSi = dynamic_cast<IntInit*>(LHS);
948 if (Init *I = LHS->convertInitializerTo(new IntRecTy()))
949 LHSi = dynamic_cast<IntInit*>(I);
David Greene3587eed2009-05-14 23:26:46 +0000950 if (LHSi) {
951 if (LHSi->getValue()) {
952 return MHS;
Bob Wilson7248f862009-11-22 04:24:42 +0000953 } else {
David Greene3587eed2009-05-14 23:26:46 +0000954 return RHS;
955 }
956 }
957 break;
958 }
David Greene98ed3c72009-05-14 21:54:42 +0000959 }
David Greene5d0c0512009-05-14 20:54:48 +0000960
David Greene98ed3c72009-05-14 21:54:42 +0000961 return this;
962}
David Greene5d0c0512009-05-14 20:54:48 +0000963
David Greene98ed3c72009-05-14 21:54:42 +0000964Init *TernOpInit::resolveReferences(Record &R, const RecordVal *RV) {
965 Init *lhs = LHS->resolveReferences(R, RV);
David Greeneb0354452009-06-08 19:16:56 +0000966
967 if (Opc == IF && lhs != LHS) {
Bruno Cardoso Lopes7f4235d2010-06-17 01:50:39 +0000968 IntInit *Value = dynamic_cast<IntInit*>(lhs);
969 if (Init *I = lhs->convertInitializerTo(new IntRecTy()))
970 Value = dynamic_cast<IntInit*>(I);
David Greeneb0354452009-06-08 19:16:56 +0000971 if (Value != 0) {
972 // Short-circuit
973 if (Value->getValue()) {
974 Init *mhs = MHS->resolveReferences(R, RV);
Bob Wilson7248f862009-11-22 04:24:42 +0000975 return (new TernOpInit(getOpcode(), lhs, mhs,
976 RHS, getType()))->Fold(&R, 0);
977 } else {
David Greeneb0354452009-06-08 19:16:56 +0000978 Init *rhs = RHS->resolveReferences(R, RV);
Bob Wilson7248f862009-11-22 04:24:42 +0000979 return (new TernOpInit(getOpcode(), lhs, MHS,
980 rhs, getType()))->Fold(&R, 0);
David Greeneb0354452009-06-08 19:16:56 +0000981 }
982 }
983 }
Bob Wilson7248f862009-11-22 04:24:42 +0000984
David Greene98ed3c72009-05-14 21:54:42 +0000985 Init *mhs = MHS->resolveReferences(R, RV);
986 Init *rhs = RHS->resolveReferences(R, RV);
David Greeneb0354452009-06-08 19:16:56 +0000987
David Greene98ed3c72009-05-14 21:54:42 +0000988 if (LHS != lhs || MHS != mhs || RHS != rhs)
989 return (new TernOpInit(getOpcode(), lhs, mhs, rhs, getType()))->Fold(&R, 0);
990 return Fold(&R, 0);
991}
David Greene196ac3c2009-04-23 21:25:15 +0000992
David Greene98ed3c72009-05-14 21:54:42 +0000993std::string TernOpInit::getAsString() const {
994 std::string Result;
995 switch (Opc) {
996 case SUBST: Result = "!subst"; break;
Bob Wilson7248f862009-11-22 04:24:42 +0000997 case FOREACH: Result = "!foreach"; break;
998 case IF: Result = "!if"; break;
David Greene98ed3c72009-05-14 21:54:42 +0000999 }
Bob Wilson7248f862009-11-22 04:24:42 +00001000 return Result + "(" + LHS->getAsString() + ", " + MHS->getAsString() + ", "
David Greene98ed3c72009-05-14 21:54:42 +00001001 + RHS->getAsString() + ")";
1002}
David Greene196ac3c2009-04-23 21:25:15 +00001003
David Greene2a9de4d2010-09-03 21:00:49 +00001004RecTy *TypedInit::getFieldType(const std::string &FieldName) const {
1005 RecordRecTy *RecordType = dynamic_cast<RecordRecTy *>(getType());
1006 if (RecordType) {
1007 RecordVal *Field = RecordType->getRecord()->getValue(FieldName);
1008 if (Field) {
1009 return Field->getType();
1010 }
1011 }
1012 return 0;
1013}
1014
Chris Lattner577fc3f2004-07-27 01:01:21 +00001015Init *TypedInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001016 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
Bill Wendling73a48d82010-12-10 22:54:30 +00001017 if (T == 0) return 0; // Cannot subscript a non-bits variable.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001018 unsigned NumBits = T->getNumBits();
1019
1020 BitsInit *BI = new BitsInit(Bits.size());
1021 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
1022 if (Bits[i] >= NumBits) {
1023 delete BI;
1024 return 0;
1025 }
1026 BI->setBit(i, new VarBitInit(this, Bits[i]));
1027 }
1028 return BI;
1029}
1030
Chris Lattner577fc3f2004-07-27 01:01:21 +00001031Init *TypedInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
1032 ListRecTy *T = dynamic_cast<ListRecTy*>(getType());
Bill Wendling73a48d82010-12-10 22:54:30 +00001033 if (T == 0) return 0; // Cannot subscript a non-list variable.
Chris Lattner577fc3f2004-07-27 01:01:21 +00001034
1035 if (Elements.size() == 1)
1036 return new VarListElementInit(this, Elements[0]);
1037
1038 std::vector<Init*> ListInits;
1039 ListInits.reserve(Elements.size());
1040 for (unsigned i = 0, e = Elements.size(); i != e; ++i)
1041 ListInits.push_back(new VarListElementInit(this, Elements[i]));
David Greene8618f952009-06-08 20:23:18 +00001042 return new ListInit(ListInits, T);
Chris Lattner577fc3f2004-07-27 01:01:21 +00001043}
1044
1045
Chris Lattneref943742005-04-19 03:36:21 +00001046Init *VarInit::resolveBitReference(Record &R, const RecordVal *IRV,
1047 unsigned Bit) {
1048 if (R.isTemplateArg(getName())) return 0;
1049 if (IRV && IRV->getName() != getName()) return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001050
1051 RecordVal *RV = R.getValue(getName());
Bob Wilson159905a2009-11-21 22:44:20 +00001052 assert(RV && "Reference to a non-existent variable?");
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001053 assert(dynamic_cast<BitsInit*>(RV->getValue()));
1054 BitsInit *BI = (BitsInit*)RV->getValue();
Misha Brukman650ba8e2005-04-22 00:00:37 +00001055
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001056 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
1057 Init *B = BI->getBit(Bit);
1058
Bob Wilson67e6cab2009-11-22 03:58:57 +00001059 // If the bit is set to some value, or if we are resolving a reference to a
1060 // specific variable and that variable is explicitly unset, then replace the
1061 // VarBitInit with it.
1062 if (IRV || !dynamic_cast<UnsetInit*>(B))
1063 return B;
Chris Lattner577fc3f2004-07-27 01:01:21 +00001064 return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001065}
1066
Chris Lattneref943742005-04-19 03:36:21 +00001067Init *VarInit::resolveListElementReference(Record &R, const RecordVal *IRV,
1068 unsigned Elt) {
1069 if (R.isTemplateArg(getName())) return 0;
1070 if (IRV && IRV->getName() != getName()) return 0;
Chris Lattner577fc3f2004-07-27 01:01:21 +00001071
1072 RecordVal *RV = R.getValue(getName());
Bob Wilson159905a2009-11-21 22:44:20 +00001073 assert(RV && "Reference to a non-existent variable?");
Chris Lattner577fc3f2004-07-27 01:01:21 +00001074 ListInit *LI = dynamic_cast<ListInit*>(RV->getValue());
David Greene9d3febe2009-05-14 20:38:52 +00001075 if (!LI) {
1076 VarInit *VI = dynamic_cast<VarInit*>(RV->getValue());
1077 assert(VI && "Invalid list element!");
1078 return new VarListElementInit(VI, Elt);
1079 }
Bob Wilson7248f862009-11-22 04:24:42 +00001080
Chris Lattner577fc3f2004-07-27 01:01:21 +00001081 if (Elt >= LI->getSize())
1082 return 0; // Out of range reference.
1083 Init *E = LI->getElement(Elt);
Bob Wilson67e6cab2009-11-22 03:58:57 +00001084 // If the element is set to some value, or if we are resolving a reference
1085 // to a specific variable and that variable is explicitly unset, then
1086 // replace the VarListElementInit with it.
1087 if (IRV || !dynamic_cast<UnsetInit*>(E))
1088 return E;
Chris Lattner577fc3f2004-07-27 01:01:21 +00001089 return 0;
1090}
1091
1092
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001093RecTy *VarInit::getFieldType(const std::string &FieldName) const {
1094 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
1095 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
1096 return RV->getType();
1097 return 0;
1098}
1099
Jakob Stoklund Olesen0e457622010-03-25 06:23:34 +00001100Init *VarInit::getFieldInit(Record &R, const RecordVal *RV,
1101 const std::string &FieldName) const {
Reid Spencer2a826862006-11-02 20:46:16 +00001102 if (dynamic_cast<RecordRecTy*>(getType()))
Jakob Stoklund Olesen0e457622010-03-25 06:23:34 +00001103 if (const RecordVal *Val = R.getValue(VarName)) {
1104 if (RV != Val && (RV || dynamic_cast<UnsetInit*>(Val->getValue())))
1105 return 0;
1106 Init *TheInit = Val->getValue();
Chris Lattnerd959ab92004-02-28 16:31:53 +00001107 assert(TheInit != this && "Infinite loop detected!");
Jakob Stoklund Olesen0e457622010-03-25 06:23:34 +00001108 if (Init *I = TheInit->getFieldInit(R, RV, FieldName))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001109 return I;
1110 else
1111 return 0;
Chris Lattnerd959ab92004-02-28 16:31:53 +00001112 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001113 return 0;
1114}
1115
1116/// resolveReferences - This method is used by classes that refer to other
Bob Wilson159905a2009-11-21 22:44:20 +00001117/// variables which may not be defined at the time the expression is formed.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001118/// If a value is set for the variable later, this method will be called on
1119/// users of the value to allow the value to propagate out.
1120///
Chris Lattneref943742005-04-19 03:36:21 +00001121Init *VarInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001122 if (RecordVal *Val = R.getValue(VarName))
Chris Lattneref943742005-04-19 03:36:21 +00001123 if (RV == Val || (RV == 0 && !dynamic_cast<UnsetInit*>(Val->getValue())))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001124 return Val->getValue();
1125 return this;
1126}
Misha Brukman650ba8e2005-04-22 00:00:37 +00001127
Chris Lattner695506c2007-11-22 21:05:25 +00001128std::string VarBitInit::getAsString() const {
1129 return TI->getAsString() + "{" + utostr(Bit) + "}";
1130}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001131
Chris Lattneref943742005-04-19 03:36:21 +00001132Init *VarBitInit::resolveReferences(Record &R, const RecordVal *RV) {
1133 if (Init *I = getVariable()->resolveBitReference(R, RV, getBitNum()))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001134 return I;
1135 return this;
1136}
1137
Chris Lattner695506c2007-11-22 21:05:25 +00001138std::string VarListElementInit::getAsString() const {
1139 return TI->getAsString() + "[" + utostr(Element) + "]";
1140}
1141
Chris Lattneref943742005-04-19 03:36:21 +00001142Init *VarListElementInit::resolveReferences(Record &R, const RecordVal *RV) {
1143 if (Init *I = getVariable()->resolveListElementReference(R, RV,
1144 getElementNum()))
Chris Lattner577fc3f2004-07-27 01:01:21 +00001145 return I;
1146 return this;
1147}
1148
Chris Lattneref943742005-04-19 03:36:21 +00001149Init *VarListElementInit::resolveBitReference(Record &R, const RecordVal *RV,
1150 unsigned Bit) {
Chris Lattner577fc3f2004-07-27 01:01:21 +00001151 // FIXME: This should be implemented, to support references like:
1152 // bit B = AA[0]{1};
1153 return 0;
1154}
1155
Chris Lattneref943742005-04-19 03:36:21 +00001156Init *VarListElementInit::
1157resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) {
Chris Lattner577fc3f2004-07-27 01:01:21 +00001158 // FIXME: This should be implemented, to support references like:
1159 // int B = AA[0][1];
1160 return 0;
1161}
1162
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001163RecTy *DefInit::getFieldType(const std::string &FieldName) const {
1164 if (const RecordVal *RV = Def->getValue(FieldName))
1165 return RV->getType();
1166 return 0;
1167}
1168
Jakob Stoklund Olesen0e457622010-03-25 06:23:34 +00001169Init *DefInit::getFieldInit(Record &R, const RecordVal *RV,
1170 const std::string &FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001171 return Def->getValue(FieldName)->getValue();
1172}
1173
1174
Chris Lattner695506c2007-11-22 21:05:25 +00001175std::string DefInit::getAsString() const {
1176 return Def->getName();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001177}
1178
Chris Lattneref943742005-04-19 03:36:21 +00001179Init *FieldInit::resolveBitReference(Record &R, const RecordVal *RV,
1180 unsigned Bit) {
Jakob Stoklund Olesen0e457622010-03-25 06:23:34 +00001181 if (Init *BitsVal = Rec->getFieldInit(R, RV, FieldName))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001182 if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
1183 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
1184 Init *B = BI->getBit(Bit);
Misha Brukman650ba8e2005-04-22 00:00:37 +00001185
Bill Wendling73a48d82010-12-10 22:54:30 +00001186 if (dynamic_cast<BitInit*>(B)) // If the bit is set.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001187 return B; // Replace the VarBitInit with it.
1188 }
Chris Lattner577fc3f2004-07-27 01:01:21 +00001189 return 0;
1190}
1191
Chris Lattneref943742005-04-19 03:36:21 +00001192Init *FieldInit::resolveListElementReference(Record &R, const RecordVal *RV,
1193 unsigned Elt) {
Jakob Stoklund Olesen0e457622010-03-25 06:23:34 +00001194 if (Init *ListVal = Rec->getFieldInit(R, RV, FieldName))
Chris Lattner577fc3f2004-07-27 01:01:21 +00001195 if (ListInit *LI = dynamic_cast<ListInit*>(ListVal)) {
1196 if (Elt >= LI->getSize()) return 0;
1197 Init *E = LI->getElement(Elt);
1198
Bob Wilson67e6cab2009-11-22 03:58:57 +00001199 // If the element is set to some value, or if we are resolving a
1200 // reference to a specific variable and that variable is explicitly
1201 // unset, then replace the VarListElementInit with it.
1202 if (RV || !dynamic_cast<UnsetInit*>(E))
1203 return E;
Chris Lattner577fc3f2004-07-27 01:01:21 +00001204 }
1205 return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001206}
1207
Chris Lattneref943742005-04-19 03:36:21 +00001208Init *FieldInit::resolveReferences(Record &R, const RecordVal *RV) {
1209 Init *NewRec = RV ? Rec->resolveReferences(R, RV) : Rec;
1210
Jakob Stoklund Olesen0e457622010-03-25 06:23:34 +00001211 Init *BitsVal = NewRec->getFieldInit(R, RV, FieldName);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001212 if (BitsVal) {
Chris Lattneref943742005-04-19 03:36:21 +00001213 Init *BVR = BitsVal->resolveReferences(R, RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001214 return BVR->isComplete() ? BVR : this;
1215 }
Chris Lattneref943742005-04-19 03:36:21 +00001216
1217 if (NewRec != Rec) {
Chris Lattneref943742005-04-19 03:36:21 +00001218 return new FieldInit(NewRec, FieldName);
1219 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001220 return this;
1221}
1222
Chris Lattner0d3ef402006-01-31 06:02:35 +00001223Init *DagInit::resolveReferences(Record &R, const RecordVal *RV) {
1224 std::vector<Init*> NewArgs;
1225 for (unsigned i = 0, e = Args.size(); i != e; ++i)
1226 NewArgs.push_back(Args[i]->resolveReferences(R, RV));
Bob Wilson7248f862009-11-22 04:24:42 +00001227
Chris Lattnerb59cf3c2006-03-30 22:50:40 +00001228 Init *Op = Val->resolveReferences(R, RV);
Bob Wilson7248f862009-11-22 04:24:42 +00001229
Chris Lattnerb59cf3c2006-03-30 22:50:40 +00001230 if (Args != NewArgs || Op != Val)
Bruno Cardoso Lopes30a28d62010-06-23 19:50:39 +00001231 return new DagInit(Op, ValName, NewArgs, ArgNames);
Bob Wilson7248f862009-11-22 04:24:42 +00001232
Chris Lattner0d3ef402006-01-31 06:02:35 +00001233 return this;
1234}
1235
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001236
Chris Lattner695506c2007-11-22 21:05:25 +00001237std::string DagInit::getAsString() const {
1238 std::string Result = "(" + Val->getAsString();
Nate Begemandbe3f772009-03-19 05:21:56 +00001239 if (!ValName.empty())
1240 Result += ":" + ValName;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001241 if (Args.size()) {
Chris Lattner695506c2007-11-22 21:05:25 +00001242 Result += " " + Args[0]->getAsString();
1243 if (!ArgNames[0].empty()) Result += ":$" + ArgNames[0];
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001244 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
Chris Lattner695506c2007-11-22 21:05:25 +00001245 Result += ", " + Args[i]->getAsString();
1246 if (!ArgNames[i].empty()) Result += ":$" + ArgNames[i];
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001247 }
1248 }
Chris Lattner695506c2007-11-22 21:05:25 +00001249 return Result + ")";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001250}
1251
1252
1253//===----------------------------------------------------------------------===//
1254// Other implementations
1255//===----------------------------------------------------------------------===//
1256
1257RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
1258 : Name(N), Ty(T), Prefix(P) {
1259 Value = Ty->convertValue(new UnsetInit());
1260 assert(Value && "Cannot create unset value for current type!");
1261}
1262
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001263void RecordVal::dump() const { errs() << *this; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001264
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001265void RecordVal::print(raw_ostream &OS, bool PrintSem) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001266 if (getPrefix()) OS << "field ";
1267 OS << *getType() << " " << getName();
Chris Lattneref943742005-04-19 03:36:21 +00001268
1269 if (getValue())
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001270 OS << " = " << *getValue();
Chris Lattneref943742005-04-19 03:36:21 +00001271
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001272 if (PrintSem) OS << ";\n";
1273}
1274
Daniel Dunbarced00812009-08-23 09:47:37 +00001275unsigned Record::LastID = 0;
1276
Chris Lattnerac284252005-08-19 17:58:11 +00001277void Record::setName(const std::string &Name) {
Chris Lattner77d369c2010-12-13 00:23:57 +00001278 if (TrackedRecords.getDef(getName()) == this) {
1279 TrackedRecords.removeDef(getName());
Chris Lattnerac284252005-08-19 17:58:11 +00001280 this->Name = Name;
Chris Lattner77d369c2010-12-13 00:23:57 +00001281 TrackedRecords.addDef(this);
Chris Lattnerac284252005-08-19 17:58:11 +00001282 } else {
Chris Lattner77d369c2010-12-13 00:23:57 +00001283 TrackedRecords.removeClass(getName());
Chris Lattnerac284252005-08-19 17:58:11 +00001284 this->Name = Name;
Chris Lattner77d369c2010-12-13 00:23:57 +00001285 TrackedRecords.addClass(this);
Chris Lattnerac284252005-08-19 17:58:11 +00001286 }
1287}
1288
Chris Lattneref943742005-04-19 03:36:21 +00001289/// resolveReferencesTo - If anything in this record refers to RV, replace the
1290/// reference to RV with the RHS of RV. If RV is null, we resolve all possible
1291/// references.
1292void Record::resolveReferencesTo(const RecordVal *RV) {
1293 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
1294 if (Init *V = Values[i].getValue())
1295 Values[i].setValue(V->resolveReferences(*this, RV));
1296 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001297}
1298
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001299void Record::dump() const { errs() << *this; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001300
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001301raw_ostream &llvm::operator<<(raw_ostream &OS, const Record &R) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001302 OS << R.getName();
1303
1304 const std::vector<std::string> &TArgs = R.getTemplateArgs();
1305 if (!TArgs.empty()) {
1306 OS << "<";
1307 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1308 if (i) OS << ", ";
1309 const RecordVal *RV = R.getValue(TArgs[i]);
1310 assert(RV && "Template argument record not found??");
1311 RV->print(OS, false);
1312 }
1313 OS << ">";
1314 }
1315
1316 OS << " {";
1317 const std::vector<Record*> &SC = R.getSuperClasses();
1318 if (!SC.empty()) {
1319 OS << "\t//";
1320 for (unsigned i = 0, e = SC.size(); i != e; ++i)
1321 OS << " " << SC[i]->getName();
1322 }
1323 OS << "\n";
1324
1325 const std::vector<RecordVal> &Vals = R.getValues();
1326 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
1327 if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
1328 OS << Vals[i];
1329 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
1330 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
1331 OS << Vals[i];
1332
1333 return OS << "}\n";
1334}
1335
1336/// getValueInit - Return the initializer for a value with the specified name,
1337/// or throw an exception if the field does not exist.
1338///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001339Init *Record::getValueInit(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001340 const RecordVal *R = getValue(FieldName);
1341 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001342 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001343 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001344 return R->getValue();
1345}
1346
1347
1348/// getValueAsString - This method looks up the specified field and returns its
1349/// value as a string, throwing an exception if the field does not exist or if
1350/// the value is not a string.
1351///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001352std::string Record::getValueAsString(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001353 const RecordVal *R = getValue(FieldName);
1354 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001355 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001356 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001357
1358 if (const StringInit *SI = dynamic_cast<const StringInit*>(R->getValue()))
1359 return SI->getValue();
Chris Lattner42bb0c12009-09-18 18:31:37 +00001360 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001361 "' does not have a string initializer!";
1362}
1363
1364/// getValueAsBitsInit - This method looks up the specified field and returns
1365/// its value as a BitsInit, throwing an exception if the field does not exist
1366/// or if the value is not the right type.
1367///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001368BitsInit *Record::getValueAsBitsInit(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001369 const RecordVal *R = getValue(FieldName);
1370 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001371 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001372 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001373
1374 if (BitsInit *BI = dynamic_cast<BitsInit*>(R->getValue()))
1375 return BI;
Chris Lattner42bb0c12009-09-18 18:31:37 +00001376 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001377 "' does not have a BitsInit initializer!";
1378}
1379
1380/// getValueAsListInit - This method looks up the specified field and returns
1381/// its value as a ListInit, throwing an exception if the field does not exist
1382/// or if the value is not the right type.
1383///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001384ListInit *Record::getValueAsListInit(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001385 const RecordVal *R = getValue(FieldName);
1386 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001387 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001388 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001389
1390 if (ListInit *LI = dynamic_cast<ListInit*>(R->getValue()))
1391 return LI;
Chris Lattner42bb0c12009-09-18 18:31:37 +00001392 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001393 "' does not have a list initializer!";
1394}
1395
Chris Lattner7ad0bed2005-10-28 22:49:02 +00001396/// getValueAsListOfDefs - This method looks up the specified field and returns
Jim Laskeyb04feb62005-10-28 21:46:31 +00001397/// its value as a vector of records, throwing an exception if the field does
1398/// not exist or if the value is not the right type.
1399///
Bob Wilson7248f862009-11-22 04:24:42 +00001400std::vector<Record*>
Chris Lattner42bb0c12009-09-18 18:31:37 +00001401Record::getValueAsListOfDefs(StringRef FieldName) const {
Jim Laskeyb04feb62005-10-28 21:46:31 +00001402 ListInit *List = getValueAsListInit(FieldName);
1403 std::vector<Record*> Defs;
1404 for (unsigned i = 0; i < List->getSize(); i++) {
1405 if (DefInit *DI = dynamic_cast<DefInit*>(List->getElement(i))) {
1406 Defs.push_back(DI->getDef());
1407 } else {
Chris Lattner42bb0c12009-09-18 18:31:37 +00001408 throw "Record `" + getName() + "', field `" + FieldName.str() +
Jim Laskeyb04feb62005-10-28 21:46:31 +00001409 "' list is not entirely DefInit!";
1410 }
1411 }
1412 return Defs;
1413}
1414
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001415/// getValueAsInt - This method looks up the specified field and returns its
Dan Gohmanca0546f2008-10-17 01:33:43 +00001416/// value as an int64_t, throwing an exception if the field does not exist or if
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001417/// the value is not the right type.
1418///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001419int64_t Record::getValueAsInt(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001420 const RecordVal *R = getValue(FieldName);
1421 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001422 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001423 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001424
1425 if (IntInit *II = dynamic_cast<IntInit*>(R->getValue()))
1426 return II->getValue();
Chris Lattner42bb0c12009-09-18 18:31:37 +00001427 throw "Record `" + getName() + "', field `" + FieldName.str() +
Nate Begemanf621b332005-11-30 18:37:14 +00001428 "' does not have an int initializer!";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001429}
1430
Anton Korobeynikova468a112007-11-11 11:19:37 +00001431/// getValueAsListOfInts - This method looks up the specified field and returns
1432/// its value as a vector of integers, throwing an exception if the field does
1433/// not exist or if the value is not the right type.
1434///
Bob Wilson7248f862009-11-22 04:24:42 +00001435std::vector<int64_t>
Chris Lattner42bb0c12009-09-18 18:31:37 +00001436Record::getValueAsListOfInts(StringRef FieldName) const {
Anton Korobeynikova468a112007-11-11 11:19:37 +00001437 ListInit *List = getValueAsListInit(FieldName);
Dan Gohmanca0546f2008-10-17 01:33:43 +00001438 std::vector<int64_t> Ints;
Anton Korobeynikova468a112007-11-11 11:19:37 +00001439 for (unsigned i = 0; i < List->getSize(); i++) {
1440 if (IntInit *II = dynamic_cast<IntInit*>(List->getElement(i))) {
1441 Ints.push_back(II->getValue());
1442 } else {
Chris Lattner42bb0c12009-09-18 18:31:37 +00001443 throw "Record `" + getName() + "', field `" + FieldName.str() +
Anton Korobeynikova468a112007-11-11 11:19:37 +00001444 "' does not have a list of ints initializer!";
1445 }
1446 }
1447 return Ints;
1448}
1449
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001450/// getValueAsDef - This method looks up the specified field and returns its
1451/// value as a Record, throwing an exception if the field does not exist or if
1452/// the value is not the right type.
1453///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001454Record *Record::getValueAsDef(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001455 const RecordVal *R = getValue(FieldName);
1456 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001457 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001458 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001459
1460 if (DefInit *DI = dynamic_cast<DefInit*>(R->getValue()))
1461 return DI->getDef();
Chris Lattner42bb0c12009-09-18 18:31:37 +00001462 throw "Record `" + getName() + "', field `" + FieldName.str() +
Nate Begemanf621b332005-11-30 18:37:14 +00001463 "' does not have a def initializer!";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001464}
1465
1466/// getValueAsBit - This method looks up the specified field and returns its
1467/// value as a bit, throwing an exception if the field does not exist or if
1468/// the value is not the right type.
1469///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001470bool Record::getValueAsBit(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001471 const RecordVal *R = getValue(FieldName);
1472 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001473 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001474 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001475
1476 if (BitInit *BI = dynamic_cast<BitInit*>(R->getValue()))
1477 return BI->getValue();
Chris Lattner42bb0c12009-09-18 18:31:37 +00001478 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001479 "' does not have a bit initializer!";
1480}
1481
1482/// getValueAsDag - This method looks up the specified field and returns its
1483/// value as an Dag, throwing an exception if the field does not exist or if
1484/// the value is not the right type.
1485///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001486DagInit *Record::getValueAsDag(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001487 const RecordVal *R = getValue(FieldName);
1488 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001489 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001490 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001491
1492 if (DagInit *DI = dynamic_cast<DagInit*>(R->getValue()))
1493 return DI;
Chris Lattner42bb0c12009-09-18 18:31:37 +00001494 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001495 "' does not have a dag initializer!";
1496}
1497
Chris Lattner42bb0c12009-09-18 18:31:37 +00001498std::string Record::getValueAsCode(StringRef FieldName) const {
Chris Lattnerae939eb2005-09-13 21:44:28 +00001499 const RecordVal *R = getValue(FieldName);
1500 if (R == 0 || R->getValue() == 0)
1501 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001502 FieldName.str() + "'!\n";
Bob Wilson7248f862009-11-22 04:24:42 +00001503
Chris Lattnerae939eb2005-09-13 21:44:28 +00001504 if (const CodeInit *CI = dynamic_cast<const CodeInit*>(R->getValue()))
1505 return CI->getValue();
Chris Lattner42bb0c12009-09-18 18:31:37 +00001506 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerae939eb2005-09-13 21:44:28 +00001507 "' does not have a code initializer!";
1508}
1509
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001510
David Greene7049e792009-04-24 16:55:41 +00001511void MultiClass::dump() const {
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001512 errs() << "Record:\n";
David Greene7049e792009-04-24 16:55:41 +00001513 Rec.dump();
Bob Wilson7248f862009-11-22 04:24:42 +00001514
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001515 errs() << "Defs:\n";
David Greene7049e792009-04-24 16:55:41 +00001516 for (RecordVector::const_iterator r = DefPrototypes.begin(),
1517 rend = DefPrototypes.end();
1518 r != rend;
1519 ++r) {
1520 (*r)->dump();
1521 }
1522}
1523
1524
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001525void RecordKeeper::dump() const { errs() << *this; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001526
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001527raw_ostream &llvm::operator<<(raw_ostream &OS, const RecordKeeper &RK) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001528 OS << "------------- Classes -----------------\n";
1529 const std::map<std::string, Record*> &Classes = RK.getClasses();
1530 for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001531 E = Classes.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001532 OS << "class " << *I->second;
Misha Brukman650ba8e2005-04-22 00:00:37 +00001533
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001534 OS << "------------- Defs -----------------\n";
1535 const std::map<std::string, Record*> &Defs = RK.getDefs();
1536 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001537 E = Defs.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001538 OS << "def " << *I->second;
1539 return OS;
1540}
1541
1542
1543/// getAllDerivedDefinitions - This method returns all concrete definitions
1544/// that derive from the specified class name. If a class with the specified
1545/// name does not exist, an error is printed and true is returned.
1546std::vector<Record*>
1547RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
Chris Lattner97049072010-12-13 00:20:52 +00001548 Record *Class = getClass(ClassName);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001549 if (!Class)
Misha Brukman41f9f292004-10-08 14:59:05 +00001550 throw "ERROR: Couldn't find the `" + ClassName + "' class!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001551
1552 std::vector<Record*> Defs;
1553 for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(),
1554 E = getDefs().end(); I != E; ++I)
1555 if (I->second->isSubClassOf(Class))
1556 Defs.push_back(I->second);
1557
1558 return Defs;
1559}
Brian Gaeke960707c2003-11-11 22:41:34 +00001560