blob: 00bb2a734ba2408d88a6711e4a7665b5c94c4aaf [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
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000586 errs() << "Variable not defined: '" + Name + "'\n";
David Greeneefa19612009-06-29 20:05:29 +0000587 assert(0 && "Variable not found");
588 return 0;
David Greenee8f3b272009-05-14 21:22:49 +0000589 }
David Greenee8f3b272009-05-14 21:22:49 +0000590 }
591 break;
592 }
David Greened571b3c2009-05-14 22:38:31 +0000593 case CAR: {
594 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
595 if (LHSl) {
596 if (LHSl->getSize() == 0) {
597 assert(0 && "Empty list in car");
598 return 0;
599 }
600 return LHSl->getElement(0);
601 }
602 break;
603 }
604 case CDR: {
605 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
606 if (LHSl) {
607 if (LHSl->getSize() == 0) {
608 assert(0 && "Empty list in cdr");
609 return 0;
610 }
Bob Wilson7248f862009-11-22 04:24:42 +0000611 ListInit *Result = new ListInit(LHSl->begin()+1, LHSl->end(),
612 LHSl->getType());
David Greened571b3c2009-05-14 22:38:31 +0000613 return Result;
614 }
615 break;
616 }
617 case LNULL: {
618 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
619 if (LHSl) {
620 if (LHSl->getSize() == 0) {
621 return new IntInit(1);
Bob Wilson7248f862009-11-22 04:24:42 +0000622 } else {
David Greened571b3c2009-05-14 22:38:31 +0000623 return new IntInit(0);
624 }
625 }
David Greene8618f952009-06-08 20:23:18 +0000626 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
627 if (LHSs) {
628 if (LHSs->getValue().empty()) {
629 return new IntInit(1);
Bob Wilson7248f862009-11-22 04:24:42 +0000630 } else {
David Greene8618f952009-06-08 20:23:18 +0000631 return new IntInit(0);
632 }
633 }
Bob Wilson7248f862009-11-22 04:24:42 +0000634
David Greened571b3c2009-05-14 22:38:31 +0000635 break;
636 }
David Greenee8f3b272009-05-14 21:22:49 +0000637 }
638 return this;
639}
David Greene5d0c0512009-05-14 20:54:48 +0000640
David Greenee8f3b272009-05-14 21:22:49 +0000641Init *UnOpInit::resolveReferences(Record &R, const RecordVal *RV) {
642 Init *lhs = LHS->resolveReferences(R, RV);
Bob Wilson7248f862009-11-22 04:24:42 +0000643
David Greenee8f3b272009-05-14 21:22:49 +0000644 if (LHS != lhs)
645 return (new UnOpInit(getOpcode(), lhs, getType()))->Fold(&R, 0);
646 return Fold(&R, 0);
647}
David Greene5d0c0512009-05-14 20:54:48 +0000648
David Greenee8f3b272009-05-14 21:22:49 +0000649std::string UnOpInit::getAsString() const {
650 std::string Result;
651 switch (Opc) {
652 case CAST: Result = "!cast<" + getType()->getAsString() + ">"; break;
David Greened571b3c2009-05-14 22:38:31 +0000653 case CAR: Result = "!car"; break;
654 case CDR: Result = "!cdr"; break;
655 case LNULL: Result = "!null"; break;
David Greenee8f3b272009-05-14 21:22:49 +0000656 }
657 return Result + "(" + LHS->getAsString() + ")";
658}
David Greene5d0c0512009-05-14 20:54:48 +0000659
David Greenea9c6c5d2009-04-22 20:18:10 +0000660Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
Chris Lattner51ffbf12006-03-31 21:53:49 +0000661 switch (getOpcode()) {
662 default: assert(0 && "Unknown binop");
Evan Chenga32dee22007-05-15 01:23:24 +0000663 case CONCAT: {
664 DagInit *LHSs = dynamic_cast<DagInit*>(LHS);
665 DagInit *RHSs = dynamic_cast<DagInit*>(RHS);
666 if (LHSs && RHSs) {
667 DefInit *LOp = dynamic_cast<DefInit*>(LHSs->getOperator());
668 DefInit *ROp = dynamic_cast<DefInit*>(RHSs->getOperator());
Chris Lattner81fd1f22010-03-18 21:07:51 +0000669 if (LOp == 0 || ROp == 0 || LOp->getDef() != ROp->getDef())
670 throw "Concated Dag operators do not match!";
Evan Chenga32dee22007-05-15 01:23:24 +0000671 std::vector<Init*> Args;
672 std::vector<std::string> ArgNames;
673 for (unsigned i = 0, e = LHSs->getNumArgs(); i != e; ++i) {
674 Args.push_back(LHSs->getArg(i));
675 ArgNames.push_back(LHSs->getArgName(i));
676 }
677 for (unsigned i = 0, e = RHSs->getNumArgs(); i != e; ++i) {
678 Args.push_back(RHSs->getArg(i));
679 ArgNames.push_back(RHSs->getArgName(i));
680 }
Nate Begemandbe3f772009-03-19 05:21:56 +0000681 return new DagInit(LHSs->getOperator(), "", Args, ArgNames);
Evan Chenga32dee22007-05-15 01:23:24 +0000682 }
683 break;
684 }
Chris Lattner51ffbf12006-03-31 21:53:49 +0000685 case STRCONCAT: {
686 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
687 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
688 if (LHSs && RHSs)
689 return new StringInit(LHSs->getValue() + RHSs->getValue());
690 break;
691 }
David Greene297bfe62010-01-05 19:11:42 +0000692 case EQ: {
Bruno Cardoso Lopes77a4a562010-06-16 23:24:12 +0000693 // try to fold eq comparison for 'bit' and 'int', otherwise fallback
694 // to string objects.
695 IntInit* L =
696 dynamic_cast<IntInit*>(LHS->convertInitializerTo(new IntRecTy()));
697 IntInit* R =
698 dynamic_cast<IntInit*>(RHS->convertInitializerTo(new IntRecTy()));
699
700 if (L && R)
701 return new IntInit(L->getValue() == R->getValue());
702
David Greene297bfe62010-01-05 19:11:42 +0000703 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
704 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
Bruno Cardoso Lopes77a4a562010-06-16 23:24:12 +0000705
706 // Make sure we've resolved
David Greene297bfe62010-01-05 19:11:42 +0000707 if (LHSs && RHSs)
708 return new IntInit(LHSs->getValue() == RHSs->getValue());
709
710 break;
711 }
Chris Lattner51ffbf12006-03-31 21:53:49 +0000712 case SHL:
713 case SRA:
714 case SRL: {
715 IntInit *LHSi = dynamic_cast<IntInit*>(LHS);
716 IntInit *RHSi = dynamic_cast<IntInit*>(RHS);
717 if (LHSi && RHSi) {
Dan Gohmanca0546f2008-10-17 01:33:43 +0000718 int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
719 int64_t Result;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000720 switch (getOpcode()) {
721 default: assert(0 && "Bad opcode!");
722 case SHL: Result = LHSv << RHSv; break;
723 case SRA: Result = LHSv >> RHSv; break;
Dan Gohmanca0546f2008-10-17 01:33:43 +0000724 case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000725 }
726 return new IntInit(Result);
727 }
728 break;
729 }
730 }
731 return this;
732}
733
734Init *BinOpInit::resolveReferences(Record &R, const RecordVal *RV) {
735 Init *lhs = LHS->resolveReferences(R, RV);
736 Init *rhs = RHS->resolveReferences(R, RV);
Bob Wilson7248f862009-11-22 04:24:42 +0000737
Chris Lattner51ffbf12006-03-31 21:53:49 +0000738 if (LHS != lhs || RHS != rhs)
David Greene196ac3c2009-04-23 21:25:15 +0000739 return (new BinOpInit(getOpcode(), lhs, rhs, getType()))->Fold(&R, 0);
David Greenea9c6c5d2009-04-22 20:18:10 +0000740 return Fold(&R, 0);
Chris Lattner51ffbf12006-03-31 21:53:49 +0000741}
742
Chris Lattner695506c2007-11-22 21:05:25 +0000743std::string BinOpInit::getAsString() const {
744 std::string Result;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000745 switch (Opc) {
Chris Lattner695506c2007-11-22 21:05:25 +0000746 case CONCAT: Result = "!con"; break;
747 case SHL: Result = "!shl"; break;
748 case SRA: Result = "!sra"; break;
749 case SRL: Result = "!srl"; break;
David Greene297bfe62010-01-05 19:11:42 +0000750 case EQ: Result = "!eq"; break;
Chris Lattner695506c2007-11-22 21:05:25 +0000751 case STRCONCAT: Result = "!strconcat"; break;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000752 }
Chris Lattner695506c2007-11-22 21:05:25 +0000753 return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")";
Chris Lattner51ffbf12006-03-31 21:53:49 +0000754}
755
David Greenee917fff2009-05-14 22:23:47 +0000756static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
757 Record *CurRec, MultiClass *CurMultiClass);
758
759static Init *EvaluateOperation(OpInit *RHSo, Init *LHS, Init *Arg,
760 RecTy *Type, Record *CurRec,
761 MultiClass *CurMultiClass) {
762 std::vector<Init *> NewOperands;
763
764 TypedInit *TArg = dynamic_cast<TypedInit*>(Arg);
765
766 // If this is a dag, recurse
767 if (TArg && TArg->getType()->getAsString() == "dag") {
768 Init *Result = ForeachHelper(LHS, Arg, RHSo, Type,
769 CurRec, CurMultiClass);
770 if (Result != 0) {
771 return Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000772 } else {
David Greenee917fff2009-05-14 22:23:47 +0000773 return 0;
774 }
775 }
776
777 for (int i = 0; i < RHSo->getNumOperands(); ++i) {
778 OpInit *RHSoo = dynamic_cast<OpInit*>(RHSo->getOperand(i));
779
780 if (RHSoo) {
781 Init *Result = EvaluateOperation(RHSoo, LHS, Arg,
782 Type, CurRec, CurMultiClass);
783 if (Result != 0) {
784 NewOperands.push_back(Result);
Bob Wilson7248f862009-11-22 04:24:42 +0000785 } else {
David Greenee917fff2009-05-14 22:23:47 +0000786 NewOperands.push_back(Arg);
787 }
Bob Wilson7248f862009-11-22 04:24:42 +0000788 } else if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
David Greenee917fff2009-05-14 22:23:47 +0000789 NewOperands.push_back(Arg);
Bob Wilson7248f862009-11-22 04:24:42 +0000790 } else {
David Greenee917fff2009-05-14 22:23:47 +0000791 NewOperands.push_back(RHSo->getOperand(i));
792 }
793 }
794
795 // Now run the operator and use its result as the new leaf
796 OpInit *NewOp = RHSo->clone(NewOperands);
797 Init *NewVal = NewOp->Fold(CurRec, CurMultiClass);
798 if (NewVal != NewOp) {
799 delete NewOp;
800 return NewVal;
801 }
802 return 0;
803}
804
805static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
806 Record *CurRec, MultiClass *CurMultiClass) {
807 DagInit *MHSd = dynamic_cast<DagInit*>(MHS);
808 ListInit *MHSl = dynamic_cast<ListInit*>(MHS);
809
810 DagRecTy *DagType = dynamic_cast<DagRecTy*>(Type);
811 ListRecTy *ListType = dynamic_cast<ListRecTy*>(Type);
812
813 OpInit *RHSo = dynamic_cast<OpInit*>(RHS);
814
815 if (!RHSo) {
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000816 errs() << "!foreach requires an operator\n";
David Greenee917fff2009-05-14 22:23:47 +0000817 assert(0 && "No operator for !foreach");
818 }
819
820 TypedInit *LHSt = dynamic_cast<TypedInit*>(LHS);
821
822 if (!LHSt) {
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000823 errs() << "!foreach requires typed variable\n";
David Greenee917fff2009-05-14 22:23:47 +0000824 assert(0 && "No typed variable for !foreach");
825 }
826
Nick Lewycky94298222009-05-15 03:07:14 +0000827 if ((MHSd && DagType) || (MHSl && ListType)) {
David Greenee917fff2009-05-14 22:23:47 +0000828 if (MHSd) {
829 Init *Val = MHSd->getOperator();
830 Init *Result = EvaluateOperation(RHSo, LHS, Val,
831 Type, CurRec, CurMultiClass);
832 if (Result != 0) {
833 Val = Result;
834 }
835
836 std::vector<std::pair<Init *, std::string> > args;
837 for (unsigned int i = 0; i < MHSd->getNumArgs(); ++i) {
838 Init *Arg;
839 std::string ArgName;
840 Arg = MHSd->getArg(i);
841 ArgName = MHSd->getArgName(i);
842
843 // Process args
844 Init *Result = EvaluateOperation(RHSo, LHS, Arg, Type,
845 CurRec, CurMultiClass);
846 if (Result != 0) {
847 Arg = Result;
848 }
849
850 // TODO: Process arg names
851 args.push_back(std::make_pair(Arg, ArgName));
852 }
853
854 return new DagInit(Val, "", args);
855 }
856 if (MHSl) {
857 std::vector<Init *> NewOperands;
858 std::vector<Init *> NewList(MHSl->begin(), MHSl->end());
859
860 for (ListInit::iterator li = NewList.begin(),
861 liend = NewList.end();
862 li != liend;
863 ++li) {
864 Init *Item = *li;
865 NewOperands.clear();
866 for(int i = 0; i < RHSo->getNumOperands(); ++i) {
867 // First, replace the foreach variable with the list item
868 if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
869 NewOperands.push_back(Item);
Bob Wilson7248f862009-11-22 04:24:42 +0000870 } else {
David Greenee917fff2009-05-14 22:23:47 +0000871 NewOperands.push_back(RHSo->getOperand(i));
872 }
873 }
874
875 // Now run the operator and use its result as the new list item
876 OpInit *NewOp = RHSo->clone(NewOperands);
877 Init *NewItem = NewOp->Fold(CurRec, CurMultiClass);
878 if (NewItem != NewOp) {
879 *li = NewItem;
880 delete NewOp;
881 }
882 }
David Greene8618f952009-06-08 20:23:18 +0000883 return new ListInit(NewList, MHSl->getType());
David Greenee917fff2009-05-14 22:23:47 +0000884 }
885 }
886 return 0;
887}
888
David Greene98ed3c72009-05-14 21:54:42 +0000889Init *TernOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
890 switch (getOpcode()) {
891 default: assert(0 && "Unknown binop");
892 case SUBST: {
893 DefInit *LHSd = dynamic_cast<DefInit*>(LHS);
894 VarInit *LHSv = dynamic_cast<VarInit*>(LHS);
895 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
David Greene196ac3c2009-04-23 21:25:15 +0000896
David Greene98ed3c72009-05-14 21:54:42 +0000897 DefInit *MHSd = dynamic_cast<DefInit*>(MHS);
898 VarInit *MHSv = dynamic_cast<VarInit*>(MHS);
899 StringInit *MHSs = dynamic_cast<StringInit*>(MHS);
David Greene5d0c0512009-05-14 20:54:48 +0000900
David Greene98ed3c72009-05-14 21:54:42 +0000901 DefInit *RHSd = dynamic_cast<DefInit*>(RHS);
902 VarInit *RHSv = dynamic_cast<VarInit*>(RHS);
903 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
David Greene5d0c0512009-05-14 20:54:48 +0000904
David Greene98ed3c72009-05-14 21:54:42 +0000905 if ((LHSd && MHSd && RHSd)
906 || (LHSv && MHSv && RHSv)
907 || (LHSs && MHSs && RHSs)) {
908 if (RHSd) {
909 Record *Val = RHSd->getDef();
910 if (LHSd->getAsString() == RHSd->getAsString()) {
911 Val = MHSd->getDef();
912 }
913 return new DefInit(Val);
914 }
915 if (RHSv) {
916 std::string Val = RHSv->getName();
917 if (LHSv->getAsString() == RHSv->getAsString()) {
918 Val = MHSv->getName();
919 }
920 return new VarInit(Val, getType());
921 }
922 if (RHSs) {
923 std::string Val = RHSs->getValue();
David Greene5d0c0512009-05-14 20:54:48 +0000924
David Greene98ed3c72009-05-14 21:54:42 +0000925 std::string::size_type found;
David Greenedbf70742009-12-21 21:21:34 +0000926 std::string::size_type idx = 0;
David Greene98ed3c72009-05-14 21:54:42 +0000927 do {
David Greenedbf70742009-12-21 21:21:34 +0000928 found = Val.find(LHSs->getValue(), idx);
David Greene98ed3c72009-05-14 21:54:42 +0000929 if (found != std::string::npos) {
930 Val.replace(found, LHSs->getValue().size(), MHSs->getValue());
931 }
David Greenedbf70742009-12-21 21:21:34 +0000932 idx = found + MHSs->getValue().size();
David Greene98ed3c72009-05-14 21:54:42 +0000933 } while (found != std::string::npos);
David Greene5d0c0512009-05-14 20:54:48 +0000934
David Greene98ed3c72009-05-14 21:54:42 +0000935 return new StringInit(Val);
936 }
937 }
938 break;
Bob Wilson7248f862009-11-22 04:24:42 +0000939 }
David Greene5d0c0512009-05-14 20:54:48 +0000940
David Greene98ed3c72009-05-14 21:54:42 +0000941 case FOREACH: {
David Greenee917fff2009-05-14 22:23:47 +0000942 Init *Result = ForeachHelper(LHS, MHS, RHS, getType(),
943 CurRec, CurMultiClass);
944 if (Result != 0) {
945 return Result;
David Greene98ed3c72009-05-14 21:54:42 +0000946 }
947 break;
948 }
David Greene3587eed2009-05-14 23:26:46 +0000949
950 case IF: {
Bruno Cardoso Lopes7f4235d2010-06-17 01:50:39 +0000951 IntInit *LHSi = dynamic_cast<IntInit*>(LHS);
952 if (Init *I = LHS->convertInitializerTo(new IntRecTy()))
953 LHSi = dynamic_cast<IntInit*>(I);
David Greene3587eed2009-05-14 23:26:46 +0000954 if (LHSi) {
955 if (LHSi->getValue()) {
956 return MHS;
Bob Wilson7248f862009-11-22 04:24:42 +0000957 } else {
David Greene3587eed2009-05-14 23:26:46 +0000958 return RHS;
959 }
960 }
961 break;
962 }
David Greene98ed3c72009-05-14 21:54:42 +0000963 }
David Greene5d0c0512009-05-14 20:54:48 +0000964
David Greene98ed3c72009-05-14 21:54:42 +0000965 return this;
966}
David Greene5d0c0512009-05-14 20:54:48 +0000967
David Greene98ed3c72009-05-14 21:54:42 +0000968Init *TernOpInit::resolveReferences(Record &R, const RecordVal *RV) {
969 Init *lhs = LHS->resolveReferences(R, RV);
David Greeneb0354452009-06-08 19:16:56 +0000970
971 if (Opc == IF && lhs != LHS) {
Bruno Cardoso Lopes7f4235d2010-06-17 01:50:39 +0000972 IntInit *Value = dynamic_cast<IntInit*>(lhs);
973 if (Init *I = lhs->convertInitializerTo(new IntRecTy()))
974 Value = dynamic_cast<IntInit*>(I);
David Greeneb0354452009-06-08 19:16:56 +0000975 if (Value != 0) {
976 // Short-circuit
977 if (Value->getValue()) {
978 Init *mhs = MHS->resolveReferences(R, RV);
Bob Wilson7248f862009-11-22 04:24:42 +0000979 return (new TernOpInit(getOpcode(), lhs, mhs,
980 RHS, getType()))->Fold(&R, 0);
981 } else {
David Greeneb0354452009-06-08 19:16:56 +0000982 Init *rhs = RHS->resolveReferences(R, RV);
Bob Wilson7248f862009-11-22 04:24:42 +0000983 return (new TernOpInit(getOpcode(), lhs, MHS,
984 rhs, getType()))->Fold(&R, 0);
David Greeneb0354452009-06-08 19:16:56 +0000985 }
986 }
987 }
Bob Wilson7248f862009-11-22 04:24:42 +0000988
David Greene98ed3c72009-05-14 21:54:42 +0000989 Init *mhs = MHS->resolveReferences(R, RV);
990 Init *rhs = RHS->resolveReferences(R, RV);
David Greeneb0354452009-06-08 19:16:56 +0000991
David Greene98ed3c72009-05-14 21:54:42 +0000992 if (LHS != lhs || MHS != mhs || RHS != rhs)
993 return (new TernOpInit(getOpcode(), lhs, mhs, rhs, getType()))->Fold(&R, 0);
994 return Fold(&R, 0);
995}
David Greene196ac3c2009-04-23 21:25:15 +0000996
David Greene98ed3c72009-05-14 21:54:42 +0000997std::string TernOpInit::getAsString() const {
998 std::string Result;
999 switch (Opc) {
1000 case SUBST: Result = "!subst"; break;
Bob Wilson7248f862009-11-22 04:24:42 +00001001 case FOREACH: Result = "!foreach"; break;
1002 case IF: Result = "!if"; break;
David Greene98ed3c72009-05-14 21:54:42 +00001003 }
Bob Wilson7248f862009-11-22 04:24:42 +00001004 return Result + "(" + LHS->getAsString() + ", " + MHS->getAsString() + ", "
David Greene98ed3c72009-05-14 21:54:42 +00001005 + RHS->getAsString() + ")";
1006}
David Greene196ac3c2009-04-23 21:25:15 +00001007
David Greene2a9de4d2010-09-03 21:00:49 +00001008RecTy *TypedInit::getFieldType(const std::string &FieldName) const {
1009 RecordRecTy *RecordType = dynamic_cast<RecordRecTy *>(getType());
1010 if (RecordType) {
1011 RecordVal *Field = RecordType->getRecord()->getValue(FieldName);
1012 if (Field) {
1013 return Field->getType();
1014 }
1015 }
1016 return 0;
1017}
1018
Chris Lattner577fc3f2004-07-27 01:01:21 +00001019Init *TypedInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001020 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
Bill Wendling73a48d82010-12-10 22:54:30 +00001021 if (T == 0) return 0; // Cannot subscript a non-bits variable.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001022 unsigned NumBits = T->getNumBits();
1023
1024 BitsInit *BI = new BitsInit(Bits.size());
1025 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
1026 if (Bits[i] >= NumBits) {
1027 delete BI;
1028 return 0;
1029 }
1030 BI->setBit(i, new VarBitInit(this, Bits[i]));
1031 }
1032 return BI;
1033}
1034
Chris Lattner577fc3f2004-07-27 01:01:21 +00001035Init *TypedInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
1036 ListRecTy *T = dynamic_cast<ListRecTy*>(getType());
Bill Wendling73a48d82010-12-10 22:54:30 +00001037 if (T == 0) return 0; // Cannot subscript a non-list variable.
Chris Lattner577fc3f2004-07-27 01:01:21 +00001038
1039 if (Elements.size() == 1)
1040 return new VarListElementInit(this, Elements[0]);
1041
1042 std::vector<Init*> ListInits;
1043 ListInits.reserve(Elements.size());
1044 for (unsigned i = 0, e = Elements.size(); i != e; ++i)
1045 ListInits.push_back(new VarListElementInit(this, Elements[i]));
David Greene8618f952009-06-08 20:23:18 +00001046 return new ListInit(ListInits, T);
Chris Lattner577fc3f2004-07-27 01:01:21 +00001047}
1048
1049
Chris Lattneref943742005-04-19 03:36:21 +00001050Init *VarInit::resolveBitReference(Record &R, const RecordVal *IRV,
1051 unsigned Bit) {
1052 if (R.isTemplateArg(getName())) return 0;
1053 if (IRV && IRV->getName() != getName()) return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001054
1055 RecordVal *RV = R.getValue(getName());
Bob Wilson159905a2009-11-21 22:44:20 +00001056 assert(RV && "Reference to a non-existent variable?");
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001057 assert(dynamic_cast<BitsInit*>(RV->getValue()));
1058 BitsInit *BI = (BitsInit*)RV->getValue();
Misha Brukman650ba8e2005-04-22 00:00:37 +00001059
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001060 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
1061 Init *B = BI->getBit(Bit);
1062
Bob Wilson67e6cab2009-11-22 03:58:57 +00001063 // If the bit is set to some value, or if we are resolving a reference to a
1064 // specific variable and that variable is explicitly unset, then replace the
1065 // VarBitInit with it.
1066 if (IRV || !dynamic_cast<UnsetInit*>(B))
1067 return B;
Chris Lattner577fc3f2004-07-27 01:01:21 +00001068 return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001069}
1070
Chris Lattneref943742005-04-19 03:36:21 +00001071Init *VarInit::resolveListElementReference(Record &R, const RecordVal *IRV,
1072 unsigned Elt) {
1073 if (R.isTemplateArg(getName())) return 0;
1074 if (IRV && IRV->getName() != getName()) return 0;
Chris Lattner577fc3f2004-07-27 01:01:21 +00001075
1076 RecordVal *RV = R.getValue(getName());
Bob Wilson159905a2009-11-21 22:44:20 +00001077 assert(RV && "Reference to a non-existent variable?");
Chris Lattner577fc3f2004-07-27 01:01:21 +00001078 ListInit *LI = dynamic_cast<ListInit*>(RV->getValue());
David Greene9d3febe2009-05-14 20:38:52 +00001079 if (!LI) {
1080 VarInit *VI = dynamic_cast<VarInit*>(RV->getValue());
1081 assert(VI && "Invalid list element!");
1082 return new VarListElementInit(VI, Elt);
1083 }
Bob Wilson7248f862009-11-22 04:24:42 +00001084
Chris Lattner577fc3f2004-07-27 01:01:21 +00001085 if (Elt >= LI->getSize())
1086 return 0; // Out of range reference.
1087 Init *E = LI->getElement(Elt);
Bob Wilson67e6cab2009-11-22 03:58:57 +00001088 // If the element is set to some value, or if we are resolving a reference
1089 // to a specific variable and that variable is explicitly unset, then
1090 // replace the VarListElementInit with it.
1091 if (IRV || !dynamic_cast<UnsetInit*>(E))
1092 return E;
Chris Lattner577fc3f2004-07-27 01:01:21 +00001093 return 0;
1094}
1095
1096
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001097RecTy *VarInit::getFieldType(const std::string &FieldName) const {
1098 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
1099 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
1100 return RV->getType();
1101 return 0;
1102}
1103
Jakob Stoklund Olesen0e457622010-03-25 06:23:34 +00001104Init *VarInit::getFieldInit(Record &R, const RecordVal *RV,
1105 const std::string &FieldName) const {
Reid Spencer2a826862006-11-02 20:46:16 +00001106 if (dynamic_cast<RecordRecTy*>(getType()))
Jakob Stoklund Olesen0e457622010-03-25 06:23:34 +00001107 if (const RecordVal *Val = R.getValue(VarName)) {
1108 if (RV != Val && (RV || dynamic_cast<UnsetInit*>(Val->getValue())))
1109 return 0;
1110 Init *TheInit = Val->getValue();
Chris Lattnerd959ab92004-02-28 16:31:53 +00001111 assert(TheInit != this && "Infinite loop detected!");
Jakob Stoklund Olesen0e457622010-03-25 06:23:34 +00001112 if (Init *I = TheInit->getFieldInit(R, RV, FieldName))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001113 return I;
1114 else
1115 return 0;
Chris Lattnerd959ab92004-02-28 16:31:53 +00001116 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001117 return 0;
1118}
1119
1120/// resolveReferences - This method is used by classes that refer to other
Bob Wilson159905a2009-11-21 22:44:20 +00001121/// variables which may not be defined at the time the expression is formed.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001122/// If a value is set for the variable later, this method will be called on
1123/// users of the value to allow the value to propagate out.
1124///
Chris Lattneref943742005-04-19 03:36:21 +00001125Init *VarInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001126 if (RecordVal *Val = R.getValue(VarName))
Chris Lattneref943742005-04-19 03:36:21 +00001127 if (RV == Val || (RV == 0 && !dynamic_cast<UnsetInit*>(Val->getValue())))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001128 return Val->getValue();
1129 return this;
1130}
Misha Brukman650ba8e2005-04-22 00:00:37 +00001131
Chris Lattner695506c2007-11-22 21:05:25 +00001132std::string VarBitInit::getAsString() const {
1133 return TI->getAsString() + "{" + utostr(Bit) + "}";
1134}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001135
Chris Lattneref943742005-04-19 03:36:21 +00001136Init *VarBitInit::resolveReferences(Record &R, const RecordVal *RV) {
1137 if (Init *I = getVariable()->resolveBitReference(R, RV, getBitNum()))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001138 return I;
1139 return this;
1140}
1141
Chris Lattner695506c2007-11-22 21:05:25 +00001142std::string VarListElementInit::getAsString() const {
1143 return TI->getAsString() + "[" + utostr(Element) + "]";
1144}
1145
Chris Lattneref943742005-04-19 03:36:21 +00001146Init *VarListElementInit::resolveReferences(Record &R, const RecordVal *RV) {
1147 if (Init *I = getVariable()->resolveListElementReference(R, RV,
1148 getElementNum()))
Chris Lattner577fc3f2004-07-27 01:01:21 +00001149 return I;
1150 return this;
1151}
1152
Chris Lattneref943742005-04-19 03:36:21 +00001153Init *VarListElementInit::resolveBitReference(Record &R, const RecordVal *RV,
1154 unsigned Bit) {
Chris Lattner577fc3f2004-07-27 01:01:21 +00001155 // FIXME: This should be implemented, to support references like:
1156 // bit B = AA[0]{1};
1157 return 0;
1158}
1159
Chris Lattneref943742005-04-19 03:36:21 +00001160Init *VarListElementInit::
1161resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) {
Chris Lattner577fc3f2004-07-27 01:01:21 +00001162 // FIXME: This should be implemented, to support references like:
1163 // int B = AA[0][1];
1164 return 0;
1165}
1166
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001167RecTy *DefInit::getFieldType(const std::string &FieldName) const {
1168 if (const RecordVal *RV = Def->getValue(FieldName))
1169 return RV->getType();
1170 return 0;
1171}
1172
Jakob Stoklund Olesen0e457622010-03-25 06:23:34 +00001173Init *DefInit::getFieldInit(Record &R, const RecordVal *RV,
1174 const std::string &FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001175 return Def->getValue(FieldName)->getValue();
1176}
1177
1178
Chris Lattner695506c2007-11-22 21:05:25 +00001179std::string DefInit::getAsString() const {
1180 return Def->getName();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001181}
1182
Chris Lattneref943742005-04-19 03:36:21 +00001183Init *FieldInit::resolveBitReference(Record &R, const RecordVal *RV,
1184 unsigned Bit) {
Jakob Stoklund Olesen0e457622010-03-25 06:23:34 +00001185 if (Init *BitsVal = Rec->getFieldInit(R, RV, FieldName))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001186 if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
1187 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
1188 Init *B = BI->getBit(Bit);
Misha Brukman650ba8e2005-04-22 00:00:37 +00001189
Bill Wendling73a48d82010-12-10 22:54:30 +00001190 if (dynamic_cast<BitInit*>(B)) // If the bit is set.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001191 return B; // Replace the VarBitInit with it.
1192 }
Chris Lattner577fc3f2004-07-27 01:01:21 +00001193 return 0;
1194}
1195
Chris Lattneref943742005-04-19 03:36:21 +00001196Init *FieldInit::resolveListElementReference(Record &R, const RecordVal *RV,
1197 unsigned Elt) {
Jakob Stoklund Olesen0e457622010-03-25 06:23:34 +00001198 if (Init *ListVal = Rec->getFieldInit(R, RV, FieldName))
Chris Lattner577fc3f2004-07-27 01:01:21 +00001199 if (ListInit *LI = dynamic_cast<ListInit*>(ListVal)) {
1200 if (Elt >= LI->getSize()) return 0;
1201 Init *E = LI->getElement(Elt);
1202
Bob Wilson67e6cab2009-11-22 03:58:57 +00001203 // If the element is set to some value, or if we are resolving a
1204 // reference to a specific variable and that variable is explicitly
1205 // unset, then replace the VarListElementInit with it.
1206 if (RV || !dynamic_cast<UnsetInit*>(E))
1207 return E;
Chris Lattner577fc3f2004-07-27 01:01:21 +00001208 }
1209 return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001210}
1211
Chris Lattneref943742005-04-19 03:36:21 +00001212Init *FieldInit::resolveReferences(Record &R, const RecordVal *RV) {
1213 Init *NewRec = RV ? Rec->resolveReferences(R, RV) : Rec;
1214
Jakob Stoklund Olesen0e457622010-03-25 06:23:34 +00001215 Init *BitsVal = NewRec->getFieldInit(R, RV, FieldName);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001216 if (BitsVal) {
Chris Lattneref943742005-04-19 03:36:21 +00001217 Init *BVR = BitsVal->resolveReferences(R, RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001218 return BVR->isComplete() ? BVR : this;
1219 }
Chris Lattneref943742005-04-19 03:36:21 +00001220
1221 if (NewRec != Rec) {
Chris Lattneref943742005-04-19 03:36:21 +00001222 return new FieldInit(NewRec, FieldName);
1223 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001224 return this;
1225}
1226
Chris Lattner0d3ef402006-01-31 06:02:35 +00001227Init *DagInit::resolveReferences(Record &R, const RecordVal *RV) {
1228 std::vector<Init*> NewArgs;
1229 for (unsigned i = 0, e = Args.size(); i != e; ++i)
1230 NewArgs.push_back(Args[i]->resolveReferences(R, RV));
Bob Wilson7248f862009-11-22 04:24:42 +00001231
Chris Lattnerb59cf3c2006-03-30 22:50:40 +00001232 Init *Op = Val->resolveReferences(R, RV);
Bob Wilson7248f862009-11-22 04:24:42 +00001233
Chris Lattnerb59cf3c2006-03-30 22:50:40 +00001234 if (Args != NewArgs || Op != Val)
Bruno Cardoso Lopes30a28d62010-06-23 19:50:39 +00001235 return new DagInit(Op, ValName, NewArgs, ArgNames);
Bob Wilson7248f862009-11-22 04:24:42 +00001236
Chris Lattner0d3ef402006-01-31 06:02:35 +00001237 return this;
1238}
1239
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001240
Chris Lattner695506c2007-11-22 21:05:25 +00001241std::string DagInit::getAsString() const {
1242 std::string Result = "(" + Val->getAsString();
Nate Begemandbe3f772009-03-19 05:21:56 +00001243 if (!ValName.empty())
1244 Result += ":" + ValName;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001245 if (Args.size()) {
Chris Lattner695506c2007-11-22 21:05:25 +00001246 Result += " " + Args[0]->getAsString();
1247 if (!ArgNames[0].empty()) Result += ":$" + ArgNames[0];
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001248 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
Chris Lattner695506c2007-11-22 21:05:25 +00001249 Result += ", " + Args[i]->getAsString();
1250 if (!ArgNames[i].empty()) Result += ":$" + ArgNames[i];
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001251 }
1252 }
Chris Lattner695506c2007-11-22 21:05:25 +00001253 return Result + ")";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001254}
1255
1256
1257//===----------------------------------------------------------------------===//
1258// Other implementations
1259//===----------------------------------------------------------------------===//
1260
1261RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
1262 : Name(N), Ty(T), Prefix(P) {
1263 Value = Ty->convertValue(new UnsetInit());
1264 assert(Value && "Cannot create unset value for current type!");
1265}
1266
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001267void RecordVal::dump() const { errs() << *this; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001268
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001269void RecordVal::print(raw_ostream &OS, bool PrintSem) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001270 if (getPrefix()) OS << "field ";
1271 OS << *getType() << " " << getName();
Chris Lattneref943742005-04-19 03:36:21 +00001272
1273 if (getValue())
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001274 OS << " = " << *getValue();
Chris Lattneref943742005-04-19 03:36:21 +00001275
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001276 if (PrintSem) OS << ";\n";
1277}
1278
Daniel Dunbarced00812009-08-23 09:47:37 +00001279unsigned Record::LastID = 0;
1280
Chris Lattnerac284252005-08-19 17:58:11 +00001281void Record::setName(const std::string &Name) {
Chris Lattner77d369c2010-12-13 00:23:57 +00001282 if (TrackedRecords.getDef(getName()) == this) {
1283 TrackedRecords.removeDef(getName());
Chris Lattnerac284252005-08-19 17:58:11 +00001284 this->Name = Name;
Chris Lattner77d369c2010-12-13 00:23:57 +00001285 TrackedRecords.addDef(this);
Chris Lattnerac284252005-08-19 17:58:11 +00001286 } else {
Chris Lattner77d369c2010-12-13 00:23:57 +00001287 TrackedRecords.removeClass(getName());
Chris Lattnerac284252005-08-19 17:58:11 +00001288 this->Name = Name;
Chris Lattner77d369c2010-12-13 00:23:57 +00001289 TrackedRecords.addClass(this);
Chris Lattnerac284252005-08-19 17:58:11 +00001290 }
1291}
1292
Chris Lattneref943742005-04-19 03:36:21 +00001293/// resolveReferencesTo - If anything in this record refers to RV, replace the
1294/// reference to RV with the RHS of RV. If RV is null, we resolve all possible
1295/// references.
1296void Record::resolveReferencesTo(const RecordVal *RV) {
1297 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
1298 if (Init *V = Values[i].getValue())
1299 Values[i].setValue(V->resolveReferences(*this, RV));
1300 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001301}
1302
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001303void Record::dump() const { errs() << *this; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001304
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001305raw_ostream &llvm::operator<<(raw_ostream &OS, const Record &R) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001306 OS << R.getName();
1307
1308 const std::vector<std::string> &TArgs = R.getTemplateArgs();
1309 if (!TArgs.empty()) {
1310 OS << "<";
1311 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1312 if (i) OS << ", ";
1313 const RecordVal *RV = R.getValue(TArgs[i]);
1314 assert(RV && "Template argument record not found??");
1315 RV->print(OS, false);
1316 }
1317 OS << ">";
1318 }
1319
1320 OS << " {";
1321 const std::vector<Record*> &SC = R.getSuperClasses();
1322 if (!SC.empty()) {
1323 OS << "\t//";
1324 for (unsigned i = 0, e = SC.size(); i != e; ++i)
1325 OS << " " << SC[i]->getName();
1326 }
1327 OS << "\n";
1328
1329 const std::vector<RecordVal> &Vals = R.getValues();
1330 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
1331 if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
1332 OS << Vals[i];
1333 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
1334 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
1335 OS << Vals[i];
1336
1337 return OS << "}\n";
1338}
1339
1340/// getValueInit - Return the initializer for a value with the specified name,
1341/// or throw an exception if the field does not exist.
1342///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001343Init *Record::getValueInit(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001344 const RecordVal *R = getValue(FieldName);
1345 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001346 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001347 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001348 return R->getValue();
1349}
1350
1351
1352/// getValueAsString - This method looks up the specified field and returns its
1353/// value as a string, throwing an exception if the field does not exist or if
1354/// the value is not a string.
1355///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001356std::string Record::getValueAsString(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001357 const RecordVal *R = getValue(FieldName);
1358 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001359 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001360 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001361
1362 if (const StringInit *SI = dynamic_cast<const StringInit*>(R->getValue()))
1363 return SI->getValue();
Chris Lattner42bb0c12009-09-18 18:31:37 +00001364 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001365 "' does not have a string initializer!";
1366}
1367
1368/// getValueAsBitsInit - This method looks up the specified field and returns
1369/// its value as a BitsInit, throwing an exception if the field does not exist
1370/// or if the value is not the right type.
1371///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001372BitsInit *Record::getValueAsBitsInit(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001373 const RecordVal *R = getValue(FieldName);
1374 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001375 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001376 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001377
1378 if (BitsInit *BI = dynamic_cast<BitsInit*>(R->getValue()))
1379 return BI;
Chris Lattner42bb0c12009-09-18 18:31:37 +00001380 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001381 "' does not have a BitsInit initializer!";
1382}
1383
1384/// getValueAsListInit - This method looks up the specified field and returns
1385/// its value as a ListInit, throwing an exception if the field does not exist
1386/// or if the value is not the right type.
1387///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001388ListInit *Record::getValueAsListInit(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001389 const RecordVal *R = getValue(FieldName);
1390 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001391 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001392 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001393
1394 if (ListInit *LI = dynamic_cast<ListInit*>(R->getValue()))
1395 return LI;
Chris Lattner42bb0c12009-09-18 18:31:37 +00001396 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001397 "' does not have a list initializer!";
1398}
1399
Chris Lattner7ad0bed2005-10-28 22:49:02 +00001400/// getValueAsListOfDefs - This method looks up the specified field and returns
Jim Laskeyb04feb62005-10-28 21:46:31 +00001401/// its value as a vector of records, throwing an exception if the field does
1402/// not exist or if the value is not the right type.
1403///
Bob Wilson7248f862009-11-22 04:24:42 +00001404std::vector<Record*>
Chris Lattner42bb0c12009-09-18 18:31:37 +00001405Record::getValueAsListOfDefs(StringRef FieldName) const {
Jim Laskeyb04feb62005-10-28 21:46:31 +00001406 ListInit *List = getValueAsListInit(FieldName);
1407 std::vector<Record*> Defs;
1408 for (unsigned i = 0; i < List->getSize(); i++) {
1409 if (DefInit *DI = dynamic_cast<DefInit*>(List->getElement(i))) {
1410 Defs.push_back(DI->getDef());
1411 } else {
Chris Lattner42bb0c12009-09-18 18:31:37 +00001412 throw "Record `" + getName() + "', field `" + FieldName.str() +
Jim Laskeyb04feb62005-10-28 21:46:31 +00001413 "' list is not entirely DefInit!";
1414 }
1415 }
1416 return Defs;
1417}
1418
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001419/// getValueAsInt - This method looks up the specified field and returns its
Dan Gohmanca0546f2008-10-17 01:33:43 +00001420/// value as an int64_t, throwing an exception if the field does not exist or if
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001421/// the value is not the right type.
1422///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001423int64_t Record::getValueAsInt(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001424 const RecordVal *R = getValue(FieldName);
1425 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001426 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001427 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001428
1429 if (IntInit *II = dynamic_cast<IntInit*>(R->getValue()))
1430 return II->getValue();
Chris Lattner42bb0c12009-09-18 18:31:37 +00001431 throw "Record `" + getName() + "', field `" + FieldName.str() +
Nate Begemanf621b332005-11-30 18:37:14 +00001432 "' does not have an int initializer!";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001433}
1434
Anton Korobeynikova468a112007-11-11 11:19:37 +00001435/// getValueAsListOfInts - This method looks up the specified field and returns
1436/// its value as a vector of integers, throwing an exception if the field does
1437/// not exist or if the value is not the right type.
1438///
Bob Wilson7248f862009-11-22 04:24:42 +00001439std::vector<int64_t>
Chris Lattner42bb0c12009-09-18 18:31:37 +00001440Record::getValueAsListOfInts(StringRef FieldName) const {
Anton Korobeynikova468a112007-11-11 11:19:37 +00001441 ListInit *List = getValueAsListInit(FieldName);
Dan Gohmanca0546f2008-10-17 01:33:43 +00001442 std::vector<int64_t> Ints;
Anton Korobeynikova468a112007-11-11 11:19:37 +00001443 for (unsigned i = 0; i < List->getSize(); i++) {
1444 if (IntInit *II = dynamic_cast<IntInit*>(List->getElement(i))) {
1445 Ints.push_back(II->getValue());
1446 } else {
Chris Lattner42bb0c12009-09-18 18:31:37 +00001447 throw "Record `" + getName() + "', field `" + FieldName.str() +
Anton Korobeynikova468a112007-11-11 11:19:37 +00001448 "' does not have a list of ints initializer!";
1449 }
1450 }
1451 return Ints;
1452}
1453
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001454/// getValueAsDef - This method looks up the specified field and returns its
1455/// value as a Record, throwing an exception if the field does not exist or if
1456/// the value is not the right type.
1457///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001458Record *Record::getValueAsDef(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001459 const RecordVal *R = getValue(FieldName);
1460 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001461 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001462 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001463
1464 if (DefInit *DI = dynamic_cast<DefInit*>(R->getValue()))
1465 return DI->getDef();
Chris Lattner42bb0c12009-09-18 18:31:37 +00001466 throw "Record `" + getName() + "', field `" + FieldName.str() +
Nate Begemanf621b332005-11-30 18:37:14 +00001467 "' does not have a def initializer!";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001468}
1469
1470/// getValueAsBit - This method looks up the specified field and returns its
1471/// value as a bit, throwing an exception if the field does not exist or if
1472/// the value is not the right type.
1473///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001474bool Record::getValueAsBit(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001475 const RecordVal *R = getValue(FieldName);
1476 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001477 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001478 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001479
1480 if (BitInit *BI = dynamic_cast<BitInit*>(R->getValue()))
1481 return BI->getValue();
Chris Lattner42bb0c12009-09-18 18:31:37 +00001482 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001483 "' does not have a bit initializer!";
1484}
1485
1486/// getValueAsDag - This method looks up the specified field and returns its
1487/// value as an Dag, throwing an exception if the field does not exist or if
1488/// the value is not the right type.
1489///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001490DagInit *Record::getValueAsDag(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001491 const RecordVal *R = getValue(FieldName);
1492 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001493 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001494 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001495
1496 if (DagInit *DI = dynamic_cast<DagInit*>(R->getValue()))
1497 return DI;
Chris Lattner42bb0c12009-09-18 18:31:37 +00001498 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001499 "' does not have a dag initializer!";
1500}
1501
Chris Lattner42bb0c12009-09-18 18:31:37 +00001502std::string Record::getValueAsCode(StringRef FieldName) const {
Chris Lattnerae939eb2005-09-13 21:44:28 +00001503 const RecordVal *R = getValue(FieldName);
1504 if (R == 0 || R->getValue() == 0)
1505 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001506 FieldName.str() + "'!\n";
Bob Wilson7248f862009-11-22 04:24:42 +00001507
Chris Lattnerae939eb2005-09-13 21:44:28 +00001508 if (const CodeInit *CI = dynamic_cast<const CodeInit*>(R->getValue()))
1509 return CI->getValue();
Chris Lattner42bb0c12009-09-18 18:31:37 +00001510 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerae939eb2005-09-13 21:44:28 +00001511 "' does not have a code initializer!";
1512}
1513
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001514
David Greene7049e792009-04-24 16:55:41 +00001515void MultiClass::dump() const {
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001516 errs() << "Record:\n";
David Greene7049e792009-04-24 16:55:41 +00001517 Rec.dump();
Bob Wilson7248f862009-11-22 04:24:42 +00001518
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001519 errs() << "Defs:\n";
David Greene7049e792009-04-24 16:55:41 +00001520 for (RecordVector::const_iterator r = DefPrototypes.begin(),
1521 rend = DefPrototypes.end();
1522 r != rend;
1523 ++r) {
1524 (*r)->dump();
1525 }
1526}
1527
1528
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001529void RecordKeeper::dump() const { errs() << *this; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001530
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001531raw_ostream &llvm::operator<<(raw_ostream &OS, const RecordKeeper &RK) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001532 OS << "------------- Classes -----------------\n";
1533 const std::map<std::string, Record*> &Classes = RK.getClasses();
1534 for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001535 E = Classes.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001536 OS << "class " << *I->second;
Misha Brukman650ba8e2005-04-22 00:00:37 +00001537
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001538 OS << "------------- Defs -----------------\n";
1539 const std::map<std::string, Record*> &Defs = RK.getDefs();
1540 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001541 E = Defs.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001542 OS << "def " << *I->second;
1543 return OS;
1544}
1545
1546
1547/// getAllDerivedDefinitions - This method returns all concrete definitions
1548/// that derive from the specified class name. If a class with the specified
1549/// name does not exist, an error is printed and true is returned.
1550std::vector<Record*>
1551RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
Chris Lattner97049072010-12-13 00:20:52 +00001552 Record *Class = getClass(ClassName);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001553 if (!Class)
Misha Brukman41f9f292004-10-08 14:59:05 +00001554 throw "ERROR: Couldn't find the `" + ClassName + "' class!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001555
1556 std::vector<Record*> Defs;
1557 for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(),
1558 E = getDefs().end(); I != E; ++I)
1559 if (I->second->isSubClassOf(Class))
1560 Defs.push_back(I->second);
1561
1562 return Defs;
1563}
Brian Gaeke960707c2003-11-11 22:41:34 +00001564