blob: 4f33638eaa7bee168e288256fc06f3aa9620e54d [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"
Jim Grosbach797cff02011-06-21 22:55:50 +000015#include "Error.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000016#include "llvm/Support/DataTypes.h"
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000017#include "llvm/Support/Format.h"
David Greeneb3da8122011-07-29 19:07:00 +000018#include "llvm/ADT/SmallVector.h"
19#include "llvm/ADT/STLExtras.h"
Chris Lattner8b9ecda2007-11-20 22:25:16 +000020#include "llvm/ADT/StringExtras.h"
Duraid Madina14492af2005-12-26 05:08:55 +000021
Chris Lattner68478662004-08-01 03:55:39 +000022using namespace llvm;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000023
24//===----------------------------------------------------------------------===//
David Greeneea844f02011-07-29 19:06:58 +000025// std::string wrapper for DenseMap purposes
26//===----------------------------------------------------------------------===//
27
28/// TableGenStringKey - This is a wrapper for std::string suitable for
29/// using as a key to a DenseMap. Because there isn't a particularly
30/// good way to indicate tombstone or empty keys for strings, we want
31/// to wrap std::string to indicate that this is a "special" string
32/// not expected to take on certain values (those of the tombstone and
33/// empty keys). This makes things a little safer as it clarifies
34/// that DenseMap is really not appropriate for general strings.
35
36class TableGenStringKey {
37public:
38 TableGenStringKey(const std::string &str) : data(str) {}
39 TableGenStringKey(const char *str) : data(str) {}
40
41 const std::string &str() const { return data; }
42
43private:
44 std::string data;
45};
46
47/// Specialize DenseMapInfo for TableGenStringKey.
48namespace llvm {
49
50template<> struct DenseMapInfo<TableGenStringKey> {
51 static inline TableGenStringKey getEmptyKey() {
52 TableGenStringKey Empty("<<<EMPTY KEY>>>");
53 return Empty;
54 }
55 static inline TableGenStringKey getTombstoneKey() {
56 TableGenStringKey Tombstone("<<<TOMBSTONE KEY>>>");
57 return Tombstone;
58 }
59 static unsigned getHashValue(const TableGenStringKey& Val) {
60 return HashString(Val.str());
61 }
62 static bool isEqual(const TableGenStringKey& LHS,
63 const TableGenStringKey& RHS) {
64 return LHS.str() == RHS.str();
65 }
66};
67
68}
69
70//===----------------------------------------------------------------------===//
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000071// Type implementations
72//===----------------------------------------------------------------------===//
73
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +000074BitRecTy BitRecTy::Shared;
75IntRecTy IntRecTy::Shared;
76StringRecTy StringRecTy::Shared;
77CodeRecTy CodeRecTy::Shared;
78DagRecTy DagRecTy::Shared;
79
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000080void RecTy::dump() const { print(errs()); }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000081
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +000082ListRecTy *RecTy::getListTy() {
83 if (!ListTy)
84 ListTy = new ListRecTy(this);
85 return ListTy;
86}
87
Eric Christopher71520a82011-07-11 23:06:52 +000088Init *BitRecTy::convertValue(BitsInit *BI) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000089 if (BI->getNumBits() != 1) return 0; // Only accept if just one bit!
90 return BI->getBit(0);
91}
92
93bool BitRecTy::baseClassOf(const BitsRecTy *RHS) const {
94 return RHS->getNumBits() == 1;
95}
96
Eric Christopher71520a82011-07-11 23:06:52 +000097Init *BitRecTy::convertValue(IntInit *II) {
Dan Gohmanca0546f2008-10-17 01:33:43 +000098 int64_t Val = II->getValue();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000099 if (Val != 0 && Val != 1) return 0; // Only accept 0 or 1 for a bit!
Misha Brukman650ba8e2005-04-22 00:00:37 +0000100
Eric Christopher71520a82011-07-11 23:06:52 +0000101 return new BitInit(Val != 0);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000102}
103
Eric Christopher71520a82011-07-11 23:06:52 +0000104Init *BitRecTy::convertValue(TypedInit *VI) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000105 if (dynamic_cast<BitRecTy*>(VI->getType()))
106 return VI; // Accept variable if it is already of bit type!
107 return 0;
108}
109
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000110BitsRecTy *BitsRecTy::get(unsigned Sz) {
111 static std::vector<BitsRecTy*> Shared;
112 if (Sz >= Shared.size())
113 Shared.resize(Sz + 1);
114 BitsRecTy *&Ty = Shared[Sz];
115 if (!Ty)
116 Ty = new BitsRecTy(Sz);
117 return Ty;
118}
119
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000120std::string BitsRecTy::getAsString() const {
121 return "bits<" + utostr(Size) + ">";
122}
123
Eric Christopher71520a82011-07-11 23:06:52 +0000124Init *BitsRecTy::convertValue(UnsetInit *UI) {
David Greeneb3da8122011-07-29 19:07:00 +0000125 SmallVector<Init *, 16> NewBits(Size);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000126
127 for (unsigned i = 0; i != Size; ++i)
David Greeneb3da8122011-07-29 19:07:00 +0000128 NewBits[i] = new UnsetInit();
129
130 return new BitsInit(ArrayRef<Init *>(NewBits));
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000131}
132
Eric Christopher71520a82011-07-11 23:06:52 +0000133Init *BitsRecTy::convertValue(BitInit *UI) {
Bill Wendling73a48d82010-12-10 22:54:30 +0000134 if (Size != 1) return 0; // Can only convert single bit.
David Greeneb3da8122011-07-29 19:07:00 +0000135 return new BitsInit(ArrayRef<Init *>(UI));
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000136}
137
Bill Wendling73ce4a62010-12-13 01:46:19 +0000138/// canFitInBitfield - Return true if the number of bits is large enough to hold
139/// the integer value.
140static bool canFitInBitfield(int64_t Value, unsigned NumBits) {
Nick Lewycky79286bf2011-06-03 08:25:39 +0000141 // For example, with NumBits == 4, we permit Values from [-7 .. 15].
142 return (NumBits >= sizeof(Value) * 8) ||
143 (Value >> NumBits == 0) || (Value >> (NumBits-1) == -1);
Bill Wendling73ce4a62010-12-13 01:46:19 +0000144}
145
Eric Christopher71520a82011-07-11 23:06:52 +0000146/// convertValue from Int initializer to bits type: Split the integer up into the
147/// appropriate bits.
Bill Wendling73ce4a62010-12-13 01:46:19 +0000148///
Eric Christopher71520a82011-07-11 23:06:52 +0000149Init *BitsRecTy::convertValue(IntInit *II) {
Misha Brukman6752fb52004-06-21 18:01:47 +0000150 int64_t Value = II->getValue();
Bill Wendling73a48d82010-12-10 22:54:30 +0000151 // Make sure this bitfield is large enough to hold the integer value.
Bill Wendling73ce4a62010-12-13 01:46:19 +0000152 if (!canFitInBitfield(Value, Size))
153 return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000154
David Greeneb3da8122011-07-29 19:07:00 +0000155 SmallVector<Init *, 16> NewBits(Size);
David Greeneaf973b42011-07-11 18:25:51 +0000156
David Greeneb3da8122011-07-29 19:07:00 +0000157 for (unsigned i = 0; i != Size; ++i)
158 NewBits[i] = new BitInit(Value & (1LL << i));
159
160 return new BitsInit(ArrayRef<Init *>(NewBits));
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000161}
162
Eric Christopher71520a82011-07-11 23:06:52 +0000163Init *BitsRecTy::convertValue(BitsInit *BI) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000164 // If the number of bits is right, return it. Otherwise we need to expand or
Bill Wendling73a48d82010-12-10 22:54:30 +0000165 // truncate.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000166 if (BI->getNumBits() == Size) return BI;
167 return 0;
168}
169
Eric Christopher71520a82011-07-11 23:06:52 +0000170Init *BitsRecTy::convertValue(TypedInit *VI) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000171 if (BitsRecTy *BRT = dynamic_cast<BitsRecTy*>(VI->getType()))
172 if (BRT->Size == Size) {
David Greeneb3da8122011-07-29 19:07:00 +0000173 SmallVector<Init *, 16> NewBits(Size);
174
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000175 for (unsigned i = 0; i != Size; ++i)
David Greeneb3da8122011-07-29 19:07:00 +0000176 NewBits[i] = new VarBitInit(VI, i);
177 return new BitsInit(ArrayRef<Init *>(NewBits));
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000178 }
Bill Wendling73ce4a62010-12-13 01:46:19 +0000179
David Greeneb3da8122011-07-29 19:07:00 +0000180 if (Size == 1 && dynamic_cast<BitRecTy*>(VI->getType()))
181 return new BitsInit(VI);
Misha Brukman650ba8e2005-04-22 00:00:37 +0000182
Eric Christopher71520a82011-07-11 23:06:52 +0000183 if (TernOpInit *Tern = dynamic_cast<TernOpInit*>(VI)) {
Bill Wendling73ce4a62010-12-13 01:46:19 +0000184 if (Tern->getOpcode() == TernOpInit::IF) {
Eric Christopher71520a82011-07-11 23:06:52 +0000185 Init *LHS = Tern->getLHS();
186 Init *MHS = Tern->getMHS();
187 Init *RHS = Tern->getRHS();
Bill Wendling73ce4a62010-12-13 01:46:19 +0000188
Eric Christopher71520a82011-07-11 23:06:52 +0000189 IntInit *MHSi = dynamic_cast<IntInit*>(MHS);
190 IntInit *RHSi = dynamic_cast<IntInit*>(RHS);
Bill Wendling73ce4a62010-12-13 01:46:19 +0000191
192 if (MHSi && RHSi) {
193 int64_t MHSVal = MHSi->getValue();
194 int64_t RHSVal = RHSi->getValue();
195
196 if (canFitInBitfield(MHSVal, Size) && canFitInBitfield(RHSVal, Size)) {
David Greeneb3da8122011-07-29 19:07:00 +0000197 SmallVector<Init *, 16> NewBits(Size);
Bill Wendling73ce4a62010-12-13 01:46:19 +0000198
199 for (unsigned i = 0; i != Size; ++i)
David Greeneb3da8122011-07-29 19:07:00 +0000200 NewBits[i] =
201 new TernOpInit(TernOpInit::IF, LHS,
202 new IntInit((MHSVal & (1LL << i)) ? 1 : 0),
203 new IntInit((RHSVal & (1LL << i)) ? 1 : 0),
204 VI->getType());
Eric Christopher71520a82011-07-11 23:06:52 +0000205
David Greeneb3da8122011-07-29 19:07:00 +0000206 return new BitsInit(ArrayRef<Init *>(NewBits));
Bill Wendling73ce4a62010-12-13 01:46:19 +0000207 }
208 } else {
Eric Christopher71520a82011-07-11 23:06:52 +0000209 BitsInit *MHSbs = dynamic_cast<BitsInit*>(MHS);
210 BitsInit *RHSbs = dynamic_cast<BitsInit*>(RHS);
Bill Wendling73ce4a62010-12-13 01:46:19 +0000211
212 if (MHSbs && RHSbs) {
David Greeneb3da8122011-07-29 19:07:00 +0000213 SmallVector<Init *, 16> NewBits(Size);
Bill Wendling73ce4a62010-12-13 01:46:19 +0000214
215 for (unsigned i = 0; i != Size; ++i)
David Greeneb3da8122011-07-29 19:07:00 +0000216 NewBits[i] = new TernOpInit(TernOpInit::IF, LHS,
217 MHSbs->getBit(i),
218 RHSbs->getBit(i),
219 VI->getType());
Bill Wendling73ce4a62010-12-13 01:46:19 +0000220
David Greeneb3da8122011-07-29 19:07:00 +0000221 return new BitsInit(ArrayRef<Init *>(NewBits));
Bill Wendling73ce4a62010-12-13 01:46:19 +0000222 }
223 }
224 }
225 }
226
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000227 return 0;
228}
229
Eric Christopher71520a82011-07-11 23:06:52 +0000230Init *IntRecTy::convertValue(BitInit *BI) {
231 return new IntInit(BI->getValue());
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000232}
233
Eric Christopher71520a82011-07-11 23:06:52 +0000234Init *IntRecTy::convertValue(BitsInit *BI) {
Dan Gohmanca0546f2008-10-17 01:33:43 +0000235 int64_t Result = 0;
Misha Brukman650ba8e2005-04-22 00:00:37 +0000236 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
Eric Christopher71520a82011-07-11 23:06:52 +0000237 if (BitInit *Bit = dynamic_cast<BitInit*>(BI->getBit(i))) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000238 Result |= Bit->getValue() << i;
239 } else {
240 return 0;
241 }
Eric Christopher71520a82011-07-11 23:06:52 +0000242 return new IntInit(Result);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000243}
244
Eric Christopher71520a82011-07-11 23:06:52 +0000245Init *IntRecTy::convertValue(TypedInit *TI) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000246 if (TI->getType()->typeIsConvertibleTo(this))
247 return TI; // Accept variable if already of the right type!
248 return 0;
249}
250
Eric Christopher71520a82011-07-11 23:06:52 +0000251Init *StringRecTy::convertValue(UnOpInit *BO) {
David Greenee8f3b272009-05-14 21:22:49 +0000252 if (BO->getOpcode() == UnOpInit::CAST) {
Eric Christopher71520a82011-07-11 23:06:52 +0000253 Init *L = BO->getOperand()->convertInitializerTo(this);
David Greenee8f3b272009-05-14 21:22:49 +0000254 if (L == 0) return 0;
255 if (L != BO->getOperand())
Eric Christopher71520a82011-07-11 23:06:52 +0000256 return new UnOpInit(UnOpInit::CAST, L, new StringRecTy);
David Greenee8f3b272009-05-14 21:22:49 +0000257 return BO;
258 }
David Greene5d0c0512009-05-14 20:54:48 +0000259
Eric Christopher71520a82011-07-11 23:06:52 +0000260 return convertValue((TypedInit*)BO);
David Greenee8f3b272009-05-14 21:22:49 +0000261}
David Greene5d0c0512009-05-14 20:54:48 +0000262
Eric Christopher71520a82011-07-11 23:06:52 +0000263Init *StringRecTy::convertValue(BinOpInit *BO) {
Chris Lattner51ffbf12006-03-31 21:53:49 +0000264 if (BO->getOpcode() == BinOpInit::STRCONCAT) {
Eric Christopher71520a82011-07-11 23:06:52 +0000265 Init *L = BO->getLHS()->convertInitializerTo(this);
266 Init *R = BO->getRHS()->convertInitializerTo(this);
Chris Lattner51ffbf12006-03-31 21:53:49 +0000267 if (L == 0 || R == 0) return 0;
268 if (L != BO->getLHS() || R != BO->getRHS())
Eric Christopher71520a82011-07-11 23:06:52 +0000269 return new BinOpInit(BinOpInit::STRCONCAT, L, R, new StringRecTy);
Chris Lattner51ffbf12006-03-31 21:53:49 +0000270 return BO;
271 }
David Greene196ac3c2009-04-23 21:25:15 +0000272
Eric Christopher71520a82011-07-11 23:06:52 +0000273 return convertValue((TypedInit*)BO);
Chris Lattner51ffbf12006-03-31 21:53:49 +0000274}
275
276
Eric Christopher71520a82011-07-11 23:06:52 +0000277Init *StringRecTy::convertValue(TypedInit *TI) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000278 if (dynamic_cast<StringRecTy*>(TI->getType()))
279 return TI; // Accept variable if already of the right type!
280 return 0;
281}
282
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000283std::string ListRecTy::getAsString() const {
284 return "list<" + Ty->getAsString() + ">";
285}
286
Eric Christopher71520a82011-07-11 23:06:52 +0000287Init *ListRecTy::convertValue(ListInit *LI) {
288 std::vector<Init*> Elements;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000289
290 // Verify that all of the elements of the list are subclasses of the
291 // appropriate class!
292 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
Eric Christopher71520a82011-07-11 23:06:52 +0000293 if (Init *CI = LI->getElement(i)->convertInitializerTo(Ty))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000294 Elements.push_back(CI);
295 else
296 return 0;
297
David Greene8618f952009-06-08 20:23:18 +0000298 ListRecTy *LType = dynamic_cast<ListRecTy*>(LI->getType());
299 if (LType == 0) {
300 return 0;
301 }
302
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000303 return new ListInit(Elements, this);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000304}
305
Eric Christopher71520a82011-07-11 23:06:52 +0000306Init *ListRecTy::convertValue(TypedInit *TI) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000307 // Ensure that TI is compatible with our class.
308 if (ListRecTy *LRT = dynamic_cast<ListRecTy*>(TI->getType()))
309 if (LRT->getElementType()->typeIsConvertibleTo(getElementType()))
310 return TI;
311 return 0;
312}
313
Eric Christopher71520a82011-07-11 23:06:52 +0000314Init *CodeRecTy::convertValue(TypedInit *TI) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000315 if (TI->getType()->typeIsConvertibleTo(this))
316 return TI;
317 return 0;
318}
319
Eric Christopher71520a82011-07-11 23:06:52 +0000320Init *DagRecTy::convertValue(TypedInit *TI) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000321 if (TI->getType()->typeIsConvertibleTo(this))
322 return TI;
323 return 0;
324}
325
Eric Christopher71520a82011-07-11 23:06:52 +0000326Init *DagRecTy::convertValue(UnOpInit *BO) {
David Greenee8f3b272009-05-14 21:22:49 +0000327 if (BO->getOpcode() == UnOpInit::CAST) {
Eric Christopher71520a82011-07-11 23:06:52 +0000328 Init *L = BO->getOperand()->convertInitializerTo(this);
David Greenee8f3b272009-05-14 21:22:49 +0000329 if (L == 0) return 0;
330 if (L != BO->getOperand())
Eric Christopher71520a82011-07-11 23:06:52 +0000331 return new UnOpInit(UnOpInit::CAST, L, new DagRecTy);
David Greenee8f3b272009-05-14 21:22:49 +0000332 return BO;
333 }
334 return 0;
335}
David Greene5d0c0512009-05-14 20:54:48 +0000336
Eric Christopher71520a82011-07-11 23:06:52 +0000337Init *DagRecTy::convertValue(BinOpInit *BO) {
Evan Chenga32dee22007-05-15 01:23:24 +0000338 if (BO->getOpcode() == BinOpInit::CONCAT) {
Eric Christopher71520a82011-07-11 23:06:52 +0000339 Init *L = BO->getLHS()->convertInitializerTo(this);
340 Init *R = BO->getRHS()->convertInitializerTo(this);
Evan Chenga32dee22007-05-15 01:23:24 +0000341 if (L == 0 || R == 0) return 0;
342 if (L != BO->getLHS() || R != BO->getRHS())
Eric Christopher71520a82011-07-11 23:06:52 +0000343 return new BinOpInit(BinOpInit::CONCAT, L, R, new DagRecTy);
Evan Chenga32dee22007-05-15 01:23:24 +0000344 return BO;
345 }
346 return 0;
347}
348
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000349RecordRecTy *RecordRecTy::get(Record *R) {
350 return &dynamic_cast<RecordRecTy&>(*R->getDefInit()->getType());
351}
352
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000353std::string RecordRecTy::getAsString() const {
354 return Rec->getName();
355}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000356
Eric Christopher71520a82011-07-11 23:06:52 +0000357Init *RecordRecTy::convertValue(DefInit *DI) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000358 // Ensure that DI is a subclass of Rec.
359 if (!DI->getDef()->isSubClassOf(Rec))
360 return 0;
361 return DI;
362}
363
Eric Christopher71520a82011-07-11 23:06:52 +0000364Init *RecordRecTy::convertValue(TypedInit *TI) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000365 // Ensure that TI is compatible with Rec.
366 if (RecordRecTy *RRT = dynamic_cast<RecordRecTy*>(TI->getType()))
367 if (RRT->getRecord()->isSubClassOf(getRecord()) ||
368 RRT->getRecord() == getRecord())
369 return TI;
370 return 0;
371}
372
373bool RecordRecTy::baseClassOf(const RecordRecTy *RHS) const {
Bruno Cardoso Lopesdeb20022010-06-17 23:00:16 +0000374 if (Rec == RHS->getRecord() || RHS->getRecord()->isSubClassOf(Rec))
375 return true;
376
377 const std::vector<Record*> &SC = Rec->getSuperClasses();
378 for (unsigned i = 0, e = SC.size(); i != e; ++i)
379 if (RHS->getRecord()->isSubClassOf(SC[i]))
380 return true;
381
382 return false;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000383}
384
385
Bob Wilson7248f862009-11-22 04:24:42 +0000386/// resolveTypes - Find a common type that T1 and T2 convert to.
David Greene8618f952009-06-08 20:23:18 +0000387/// Return 0 if no such type exists.
388///
389RecTy *llvm::resolveTypes(RecTy *T1, RecTy *T2) {
390 if (!T1->typeIsConvertibleTo(T2)) {
391 if (!T2->typeIsConvertibleTo(T1)) {
392 // If one is a Record type, check superclasses
393 RecordRecTy *RecTy1 = dynamic_cast<RecordRecTy*>(T1);
394 if (RecTy1) {
395 // See if T2 inherits from a type T1 also inherits from
Bob Wilson7248f862009-11-22 04:24:42 +0000396 const std::vector<Record *> &T1SuperClasses =
397 RecTy1->getRecord()->getSuperClasses();
David Greene8618f952009-06-08 20:23:18 +0000398 for(std::vector<Record *>::const_iterator i = T1SuperClasses.begin(),
399 iend = T1SuperClasses.end();
400 i != iend;
401 ++i) {
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000402 RecordRecTy *SuperRecTy1 = RecordRecTy::get(*i);
David Greene8618f952009-06-08 20:23:18 +0000403 RecTy *NewType1 = resolveTypes(SuperRecTy1, T2);
404 if (NewType1 != 0) {
405 if (NewType1 != SuperRecTy1) {
406 delete SuperRecTy1;
407 }
408 return NewType1;
409 }
410 }
411 }
412 RecordRecTy *RecTy2 = dynamic_cast<RecordRecTy*>(T2);
413 if (RecTy2) {
414 // See if T1 inherits from a type T2 also inherits from
Bob Wilson7248f862009-11-22 04:24:42 +0000415 const std::vector<Record *> &T2SuperClasses =
416 RecTy2->getRecord()->getSuperClasses();
417 for (std::vector<Record *>::const_iterator i = T2SuperClasses.begin(),
David Greene8618f952009-06-08 20:23:18 +0000418 iend = T2SuperClasses.end();
419 i != iend;
420 ++i) {
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000421 RecordRecTy *SuperRecTy2 = RecordRecTy::get(*i);
David Greene8618f952009-06-08 20:23:18 +0000422 RecTy *NewType2 = resolveTypes(T1, SuperRecTy2);
423 if (NewType2 != 0) {
424 if (NewType2 != SuperRecTy2) {
425 delete SuperRecTy2;
426 }
427 return NewType2;
428 }
429 }
430 }
431 return 0;
432 }
433 return T2;
434 }
435 return T1;
436}
437
438
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000439//===----------------------------------------------------------------------===//
440// Initializer implementations
441//===----------------------------------------------------------------------===//
442
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000443void Init::dump() const { return print(errs()); }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000444
Eric Christopher71520a82011-07-11 23:06:52 +0000445Init *BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
David Greeneb3da8122011-07-29 19:07:00 +0000446 SmallVector<Init *, 16> NewBits(Bits.size());
447
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000448 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
David Greeneb3da8122011-07-29 19:07:00 +0000449 if (Bits[i] >= getNumBits())
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000450 return 0;
David Greeneb3da8122011-07-29 19:07:00 +0000451 NewBits[i] = getBit(Bits[i]);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000452 }
David Greeneb3da8122011-07-29 19:07:00 +0000453 return new BitsInit(ArrayRef<Init *>(NewBits));
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000454}
455
Chris Lattner695506c2007-11-22 21:05:25 +0000456std::string BitsInit::getAsString() const {
Chris Lattner695506c2007-11-22 21:05:25 +0000457 std::string Result = "{ ";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000458 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
Chris Lattner695506c2007-11-22 21:05:25 +0000459 if (i) Result += ", ";
Eric Christopher71520a82011-07-11 23:06:52 +0000460 if (Init *Bit = getBit(e-i-1))
Chris Lattner695506c2007-11-22 21:05:25 +0000461 Result += Bit->getAsString();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000462 else
Chris Lattner695506c2007-11-22 21:05:25 +0000463 Result += "*";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000464 }
Chris Lattner695506c2007-11-22 21:05:25 +0000465 return Result + " }";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000466}
467
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000468// resolveReferences - If there are any field references that refer to fields
469// that have been filled in, we can propagate the values now.
470//
Eric Christopher71520a82011-07-11 23:06:52 +0000471Init *BitsInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000472 bool Changed = false;
David Greeneb3da8122011-07-29 19:07:00 +0000473 SmallVector<Init *, 16> NewBits(getNumBits());
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000474
Eric Christopher71520a82011-07-11 23:06:52 +0000475 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
476 Init *B;
477 Init *CurBit = getBit(i);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000478
479 do {
480 B = CurBit;
Chris Lattneref943742005-04-19 03:36:21 +0000481 CurBit = CurBit->resolveReferences(R, RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000482 Changed |= B != CurBit;
483 } while (B != CurBit);
David Greeneb3da8122011-07-29 19:07:00 +0000484 NewBits[i] = CurBit;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000485 }
486
487 if (Changed)
David Greeneb3da8122011-07-29 19:07:00 +0000488 return new BitsInit(ArrayRef<Init *>(NewBits));
489
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000490 return this;
491}
492
Chris Lattner695506c2007-11-22 21:05:25 +0000493std::string IntInit::getAsString() const {
494 return itostr(Value);
495}
496
Eric Christopher71520a82011-07-11 23:06:52 +0000497Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
David Greeneb3da8122011-07-29 19:07:00 +0000498 SmallVector<Init *, 16> NewBits(Bits.size());
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000499
500 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
David Greeneb3da8122011-07-29 19:07:00 +0000501 if (Bits[i] >= 64)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000502 return 0;
David Greeneb3da8122011-07-29 19:07:00 +0000503
504 NewBits[i] = new BitInit(Value & (INT64_C(1) << Bits[i]));
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000505 }
David Greeneb3da8122011-07-29 19:07:00 +0000506 return new BitsInit(ArrayRef<Init *>(NewBits));
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000507}
508
Eric Christopher71520a82011-07-11 23:06:52 +0000509Init *ListInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
510 std::vector<Init*> Vals;
Chris Lattner8bf9e062004-07-26 23:21:34 +0000511 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
512 if (Elements[i] >= getSize())
513 return 0;
514 Vals.push_back(getElement(Elements[i]));
515 }
Eric Christopher71520a82011-07-11 23:06:52 +0000516 return new ListInit(Vals, getType());
Chris Lattner8bf9e062004-07-26 23:21:34 +0000517}
518
Chris Lattnercbebe462007-02-27 22:08:27 +0000519Record *ListInit::getElementAsRecord(unsigned i) const {
520 assert(i < Values.size() && "List element index out of range!");
Eric Christopher71520a82011-07-11 23:06:52 +0000521 DefInit *DI = dynamic_cast<DefInit*>(Values[i]);
Chris Lattnercbebe462007-02-27 22:08:27 +0000522 if (DI == 0) throw "Expected record in list!";
523 return DI->getDef();
524}
525
Eric Christopher71520a82011-07-11 23:06:52 +0000526Init *ListInit::resolveReferences(Record &R, const RecordVal *RV) {
527 std::vector<Init*> Resolved;
Chris Lattner577fc3f2004-07-27 01:01:21 +0000528 Resolved.reserve(getSize());
529 bool Changed = false;
530
531 for (unsigned i = 0, e = getSize(); i != e; ++i) {
Eric Christopher71520a82011-07-11 23:06:52 +0000532 Init *E;
533 Init *CurElt = getElement(i);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000534
535 do {
536 E = CurElt;
Chris Lattneref943742005-04-19 03:36:21 +0000537 CurElt = CurElt->resolveReferences(R, RV);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000538 Changed |= E != CurElt;
539 } while (E != CurElt);
540 Resolved.push_back(E);
541 }
542
543 if (Changed)
Eric Christopher71520a82011-07-11 23:06:52 +0000544 return new ListInit(Resolved, getType());
Chris Lattner577fc3f2004-07-27 01:01:21 +0000545 return this;
546}
547
Eric Christopher71520a82011-07-11 23:06:52 +0000548Init *ListInit::resolveListElementReference(Record &R, const RecordVal *IRV,
549 unsigned Elt) {
David Greene8618f952009-06-08 20:23:18 +0000550 if (Elt >= getSize())
551 return 0; // Out of range reference.
Eric Christopher71520a82011-07-11 23:06:52 +0000552 Init *E = getElement(Elt);
Bob Wilson67e6cab2009-11-22 03:58:57 +0000553 // If the element is set to some value, or if we are resolving a reference
554 // to a specific variable and that variable is explicitly unset, then
555 // replace the VarListElementInit with it.
Eric Christopher71520a82011-07-11 23:06:52 +0000556 if (IRV || !dynamic_cast<UnsetInit*>(E))
Bob Wilson67e6cab2009-11-22 03:58:57 +0000557 return E;
David Greene8618f952009-06-08 20:23:18 +0000558 return 0;
559}
560
Chris Lattner695506c2007-11-22 21:05:25 +0000561std::string ListInit::getAsString() const {
562 std::string Result = "[";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000563 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
Chris Lattner695506c2007-11-22 21:05:25 +0000564 if (i) Result += ", ";
565 Result += Values[i]->getAsString();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000566 }
Chris Lattner695506c2007-11-22 21:05:25 +0000567 return Result + "]";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000568}
569
Eric Christopher71520a82011-07-11 23:06:52 +0000570Init *OpInit::resolveBitReference(Record &R, const RecordVal *IRV,
571 unsigned Bit) {
572 Init *Folded = Fold(&R, 0);
David Greene5d0c0512009-05-14 20:54:48 +0000573
574 if (Folded != this) {
Eric Christopher71520a82011-07-11 23:06:52 +0000575 TypedInit *Typed = dynamic_cast<TypedInit *>(Folded);
David Greene5d0c0512009-05-14 20:54:48 +0000576 if (Typed) {
577 return Typed->resolveBitReference(R, IRV, Bit);
Bob Wilson7248f862009-11-22 04:24:42 +0000578 }
David Greene5d0c0512009-05-14 20:54:48 +0000579 }
Bob Wilson7248f862009-11-22 04:24:42 +0000580
David Greene5d0c0512009-05-14 20:54:48 +0000581 return 0;
582}
583
Eric Christopher71520a82011-07-11 23:06:52 +0000584Init *OpInit::resolveListElementReference(Record &R, const RecordVal *IRV,
585 unsigned Elt) {
586 Init *Folded = Fold(&R, 0);
David Greene5d0c0512009-05-14 20:54:48 +0000587
588 if (Folded != this) {
Eric Christopher71520a82011-07-11 23:06:52 +0000589 TypedInit *Typed = dynamic_cast<TypedInit *>(Folded);
David Greene5d0c0512009-05-14 20:54:48 +0000590 if (Typed) {
591 return Typed->resolveListElementReference(R, IRV, Elt);
Bob Wilson7248f862009-11-22 04:24:42 +0000592 }
David Greene5d0c0512009-05-14 20:54:48 +0000593 }
Bob Wilson7248f862009-11-22 04:24:42 +0000594
David Greene5d0c0512009-05-14 20:54:48 +0000595 return 0;
596}
597
Eric Christopher71520a82011-07-11 23:06:52 +0000598Init *UnOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
David Greenee8f3b272009-05-14 21:22:49 +0000599 switch (getOpcode()) {
600 default: assert(0 && "Unknown unop");
601 case CAST: {
David Greeneefa19612009-06-29 20:05:29 +0000602 if (getType()->getAsString() == "string") {
Eric Christopher71520a82011-07-11 23:06:52 +0000603 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
David Greeneefa19612009-06-29 20:05:29 +0000604 if (LHSs) {
605 return LHSs;
606 }
David Greene5d0c0512009-05-14 20:54:48 +0000607
Eric Christopher71520a82011-07-11 23:06:52 +0000608 DefInit *LHSd = dynamic_cast<DefInit*>(LHS);
David Greeneefa19612009-06-29 20:05:29 +0000609 if (LHSd) {
Eric Christopher71520a82011-07-11 23:06:52 +0000610 return new StringInit(LHSd->getDef()->getName());
David Greeneefa19612009-06-29 20:05:29 +0000611 }
Bob Wilson7248f862009-11-22 04:24:42 +0000612 } else {
Eric Christopher71520a82011-07-11 23:06:52 +0000613 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
David Greeneefa19612009-06-29 20:05:29 +0000614 if (LHSs) {
615 std::string Name = LHSs->getValue();
616
617 // From TGParser::ParseIDValue
618 if (CurRec) {
619 if (const RecordVal *RV = CurRec->getValue(Name)) {
Chris Lattner94026332010-10-06 00:19:21 +0000620 if (RV->getType() != getType())
621 throw "type mismatch in cast";
Eric Christopher71520a82011-07-11 23:06:52 +0000622 return new VarInit(Name, RV->getType());
David Greenee8f3b272009-05-14 21:22:49 +0000623 }
David Greeneefa19612009-06-29 20:05:29 +0000624
625 std::string TemplateArgName = CurRec->getName()+":"+Name;
626 if (CurRec->isTemplateArg(TemplateArgName)) {
627 const RecordVal *RV = CurRec->getValue(TemplateArgName);
628 assert(RV && "Template arg doesn't exist??");
629
Chris Lattner94026332010-10-06 00:19:21 +0000630 if (RV->getType() != getType())
631 throw "type mismatch in cast";
David Greeneefa19612009-06-29 20:05:29 +0000632
Eric Christopher71520a82011-07-11 23:06:52 +0000633 return new VarInit(TemplateArgName, RV->getType());
David Greeneefa19612009-06-29 20:05:29 +0000634 }
635 }
636
637 if (CurMultiClass) {
638 std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
639 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
640 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
641 assert(RV && "Template arg doesn't exist??");
Bob Wilson7248f862009-11-22 04:24:42 +0000642
Chris Lattner94026332010-10-06 00:19:21 +0000643 if (RV->getType() != getType())
644 throw "type mismatch in cast";
Bob Wilson7248f862009-11-22 04:24:42 +0000645
Eric Christopher71520a82011-07-11 23:06:52 +0000646 return new VarInit(MCName, RV->getType());
David Greeneefa19612009-06-29 20:05:29 +0000647 }
David Greenee8f3b272009-05-14 21:22:49 +0000648 }
Bob Wilson7248f862009-11-22 04:24:42 +0000649
Chris Lattner77d369c2010-12-13 00:23:57 +0000650 if (Record *D = (CurRec->getRecords()).getDef(Name))
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000651 return DefInit::get(D);
David Greene5d0c0512009-05-14 20:54:48 +0000652
Jim Grosbach59ddb732011-05-06 18:47:45 +0000653 throw TGError(CurRec->getLoc(), "Undefined reference:'" + Name + "'\n");
David Greenee8f3b272009-05-14 21:22:49 +0000654 }
David Greenee8f3b272009-05-14 21:22:49 +0000655 }
656 break;
657 }
David Greene2f7cf7f2011-01-07 17:05:37 +0000658 case HEAD: {
Eric Christopher71520a82011-07-11 23:06:52 +0000659 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
David Greened571b3c2009-05-14 22:38:31 +0000660 if (LHSl) {
661 if (LHSl->getSize() == 0) {
662 assert(0 && "Empty list in car");
663 return 0;
664 }
665 return LHSl->getElement(0);
666 }
667 break;
668 }
David Greene2f7cf7f2011-01-07 17:05:37 +0000669 case TAIL: {
Eric Christopher71520a82011-07-11 23:06:52 +0000670 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
David Greened571b3c2009-05-14 22:38:31 +0000671 if (LHSl) {
672 if (LHSl->getSize() == 0) {
673 assert(0 && "Empty list in cdr");
674 return 0;
675 }
David Greenecdd64322011-07-29 19:07:02 +0000676 ListInit::const_iterator begin = LHSl->begin()+1;
677 ListInit::const_iterator end = LHSl->end();
678 // We can't pass these iterators directly to ArrayRef because
679 // they are not convertible to Init **. Fortunately,
680 // RandomAccessIterator::operator * is guaranteed to return an
681 // lvalue.
682 ListInit *Result = new ListInit(ArrayRef<Init *>(&*begin, end - begin),
Eric Christopher71520a82011-07-11 23:06:52 +0000683 LHSl->getType());
David Greened571b3c2009-05-14 22:38:31 +0000684 return Result;
685 }
686 break;
687 }
David Greene2f7cf7f2011-01-07 17:05:37 +0000688 case EMPTY: {
Eric Christopher71520a82011-07-11 23:06:52 +0000689 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
David Greened571b3c2009-05-14 22:38:31 +0000690 if (LHSl) {
691 if (LHSl->getSize() == 0) {
Eric Christopher71520a82011-07-11 23:06:52 +0000692 return new IntInit(1);
Bob Wilson7248f862009-11-22 04:24:42 +0000693 } else {
Eric Christopher71520a82011-07-11 23:06:52 +0000694 return new IntInit(0);
David Greened571b3c2009-05-14 22:38:31 +0000695 }
696 }
Eric Christopher71520a82011-07-11 23:06:52 +0000697 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
David Greene8618f952009-06-08 20:23:18 +0000698 if (LHSs) {
699 if (LHSs->getValue().empty()) {
Eric Christopher71520a82011-07-11 23:06:52 +0000700 return new IntInit(1);
Bob Wilson7248f862009-11-22 04:24:42 +0000701 } else {
Eric Christopher71520a82011-07-11 23:06:52 +0000702 return new IntInit(0);
David Greene8618f952009-06-08 20:23:18 +0000703 }
704 }
Bob Wilson7248f862009-11-22 04:24:42 +0000705
David Greened571b3c2009-05-14 22:38:31 +0000706 break;
707 }
David Greenee8f3b272009-05-14 21:22:49 +0000708 }
709 return this;
710}
David Greene5d0c0512009-05-14 20:54:48 +0000711
Eric Christopher71520a82011-07-11 23:06:52 +0000712Init *UnOpInit::resolveReferences(Record &R, const RecordVal *RV) {
713 Init *lhs = LHS->resolveReferences(R, RV);
Bob Wilson7248f862009-11-22 04:24:42 +0000714
David Greenee8f3b272009-05-14 21:22:49 +0000715 if (LHS != lhs)
Eric Christopher71520a82011-07-11 23:06:52 +0000716 return (new UnOpInit(getOpcode(), lhs, getType()))->Fold(&R, 0);
David Greenee8f3b272009-05-14 21:22:49 +0000717 return Fold(&R, 0);
718}
David Greene5d0c0512009-05-14 20:54:48 +0000719
David Greenee8f3b272009-05-14 21:22:49 +0000720std::string UnOpInit::getAsString() const {
721 std::string Result;
722 switch (Opc) {
723 case CAST: Result = "!cast<" + getType()->getAsString() + ">"; break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000724 case HEAD: Result = "!head"; break;
725 case TAIL: Result = "!tail"; break;
726 case EMPTY: Result = "!empty"; break;
David Greenee8f3b272009-05-14 21:22:49 +0000727 }
728 return Result + "(" + LHS->getAsString() + ")";
729}
David Greene5d0c0512009-05-14 20:54:48 +0000730
Eric Christopher71520a82011-07-11 23:06:52 +0000731Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
Chris Lattner51ffbf12006-03-31 21:53:49 +0000732 switch (getOpcode()) {
733 default: assert(0 && "Unknown binop");
Evan Chenga32dee22007-05-15 01:23:24 +0000734 case CONCAT: {
Eric Christopher71520a82011-07-11 23:06:52 +0000735 DagInit *LHSs = dynamic_cast<DagInit*>(LHS);
736 DagInit *RHSs = dynamic_cast<DagInit*>(RHS);
Evan Chenga32dee22007-05-15 01:23:24 +0000737 if (LHSs && RHSs) {
Eric Christopher71520a82011-07-11 23:06:52 +0000738 DefInit *LOp = dynamic_cast<DefInit*>(LHSs->getOperator());
739 DefInit *ROp = dynamic_cast<DefInit*>(RHSs->getOperator());
Chris Lattner81fd1f22010-03-18 21:07:51 +0000740 if (LOp == 0 || ROp == 0 || LOp->getDef() != ROp->getDef())
741 throw "Concated Dag operators do not match!";
Eric Christopher71520a82011-07-11 23:06:52 +0000742 std::vector<Init*> Args;
Evan Chenga32dee22007-05-15 01:23:24 +0000743 std::vector<std::string> ArgNames;
744 for (unsigned i = 0, e = LHSs->getNumArgs(); i != e; ++i) {
745 Args.push_back(LHSs->getArg(i));
746 ArgNames.push_back(LHSs->getArgName(i));
747 }
748 for (unsigned i = 0, e = RHSs->getNumArgs(); i != e; ++i) {
749 Args.push_back(RHSs->getArg(i));
750 ArgNames.push_back(RHSs->getArgName(i));
751 }
Eric Christopher71520a82011-07-11 23:06:52 +0000752 return new DagInit(LHSs->getOperator(), "", Args, ArgNames);
Evan Chenga32dee22007-05-15 01:23:24 +0000753 }
754 break;
755 }
Chris Lattner51ffbf12006-03-31 21:53:49 +0000756 case STRCONCAT: {
Eric Christopher71520a82011-07-11 23:06:52 +0000757 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
758 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
Chris Lattner51ffbf12006-03-31 21:53:49 +0000759 if (LHSs && RHSs)
Eric Christopher71520a82011-07-11 23:06:52 +0000760 return new StringInit(LHSs->getValue() + RHSs->getValue());
Chris Lattner51ffbf12006-03-31 21:53:49 +0000761 break;
762 }
David Greene297bfe62010-01-05 19:11:42 +0000763 case EQ: {
Bruno Cardoso Lopes77a4a562010-06-16 23:24:12 +0000764 // try to fold eq comparison for 'bit' and 'int', otherwise fallback
765 // to string objects.
Eric Christopher71520a82011-07-11 23:06:52 +0000766 IntInit* L =
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000767 dynamic_cast<IntInit*>(LHS->convertInitializerTo(IntRecTy::get()));
Eric Christopher71520a82011-07-11 23:06:52 +0000768 IntInit* R =
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000769 dynamic_cast<IntInit*>(RHS->convertInitializerTo(IntRecTy::get()));
Bruno Cardoso Lopes77a4a562010-06-16 23:24:12 +0000770
771 if (L && R)
Eric Christopher71520a82011-07-11 23:06:52 +0000772 return new IntInit(L->getValue() == R->getValue());
Bruno Cardoso Lopes77a4a562010-06-16 23:24:12 +0000773
Eric Christopher71520a82011-07-11 23:06:52 +0000774 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
775 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
Bruno Cardoso Lopes77a4a562010-06-16 23:24:12 +0000776
777 // Make sure we've resolved
David Greene297bfe62010-01-05 19:11:42 +0000778 if (LHSs && RHSs)
Eric Christopher71520a82011-07-11 23:06:52 +0000779 return new IntInit(LHSs->getValue() == RHSs->getValue());
David Greene297bfe62010-01-05 19:11:42 +0000780
781 break;
782 }
Chris Lattner51ffbf12006-03-31 21:53:49 +0000783 case SHL:
784 case SRA:
785 case SRL: {
Eric Christopher71520a82011-07-11 23:06:52 +0000786 IntInit *LHSi = dynamic_cast<IntInit*>(LHS);
787 IntInit *RHSi = dynamic_cast<IntInit*>(RHS);
Chris Lattner51ffbf12006-03-31 21:53:49 +0000788 if (LHSi && RHSi) {
Dan Gohmanca0546f2008-10-17 01:33:43 +0000789 int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
790 int64_t Result;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000791 switch (getOpcode()) {
792 default: assert(0 && "Bad opcode!");
793 case SHL: Result = LHSv << RHSv; break;
794 case SRA: Result = LHSv >> RHSv; break;
Dan Gohmanca0546f2008-10-17 01:33:43 +0000795 case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000796 }
Eric Christopher71520a82011-07-11 23:06:52 +0000797 return new IntInit(Result);
Chris Lattner51ffbf12006-03-31 21:53:49 +0000798 }
799 break;
800 }
801 }
802 return this;
803}
804
Eric Christopher71520a82011-07-11 23:06:52 +0000805Init *BinOpInit::resolveReferences(Record &R, const RecordVal *RV) {
806 Init *lhs = LHS->resolveReferences(R, RV);
807 Init *rhs = RHS->resolveReferences(R, RV);
Bob Wilson7248f862009-11-22 04:24:42 +0000808
Chris Lattner51ffbf12006-03-31 21:53:49 +0000809 if (LHS != lhs || RHS != rhs)
Eric Christopher71520a82011-07-11 23:06:52 +0000810 return (new BinOpInit(getOpcode(), lhs, rhs, getType()))->Fold(&R, 0);
David Greenea9c6c5d2009-04-22 20:18:10 +0000811 return Fold(&R, 0);
Chris Lattner51ffbf12006-03-31 21:53:49 +0000812}
813
Chris Lattner695506c2007-11-22 21:05:25 +0000814std::string BinOpInit::getAsString() const {
815 std::string Result;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000816 switch (Opc) {
Chris Lattner695506c2007-11-22 21:05:25 +0000817 case CONCAT: Result = "!con"; break;
818 case SHL: Result = "!shl"; break;
819 case SRA: Result = "!sra"; break;
820 case SRL: Result = "!srl"; break;
David Greene297bfe62010-01-05 19:11:42 +0000821 case EQ: Result = "!eq"; break;
Chris Lattner695506c2007-11-22 21:05:25 +0000822 case STRCONCAT: Result = "!strconcat"; break;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000823 }
Chris Lattner695506c2007-11-22 21:05:25 +0000824 return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")";
Chris Lattner51ffbf12006-03-31 21:53:49 +0000825}
826
Eric Christopher71520a82011-07-11 23:06:52 +0000827static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
828 Record *CurRec, MultiClass *CurMultiClass);
David Greenee917fff2009-05-14 22:23:47 +0000829
Eric Christopher71520a82011-07-11 23:06:52 +0000830static Init *EvaluateOperation(OpInit *RHSo, Init *LHS, Init *Arg,
831 RecTy *Type, Record *CurRec,
832 MultiClass *CurMultiClass) {
833 std::vector<Init *> NewOperands;
David Greenee917fff2009-05-14 22:23:47 +0000834
Eric Christopher71520a82011-07-11 23:06:52 +0000835 TypedInit *TArg = dynamic_cast<TypedInit*>(Arg);
David Greenee917fff2009-05-14 22:23:47 +0000836
837 // If this is a dag, recurse
838 if (TArg && TArg->getType()->getAsString() == "dag") {
Eric Christopher71520a82011-07-11 23:06:52 +0000839 Init *Result = ForeachHelper(LHS, Arg, RHSo, Type,
840 CurRec, CurMultiClass);
David Greenee917fff2009-05-14 22:23:47 +0000841 if (Result != 0) {
842 return Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000843 } else {
David Greenee917fff2009-05-14 22:23:47 +0000844 return 0;
845 }
846 }
847
848 for (int i = 0; i < RHSo->getNumOperands(); ++i) {
Eric Christopher71520a82011-07-11 23:06:52 +0000849 OpInit *RHSoo = dynamic_cast<OpInit*>(RHSo->getOperand(i));
David Greenee917fff2009-05-14 22:23:47 +0000850
851 if (RHSoo) {
Eric Christopher71520a82011-07-11 23:06:52 +0000852 Init *Result = EvaluateOperation(RHSoo, LHS, Arg,
853 Type, CurRec, CurMultiClass);
David Greenee917fff2009-05-14 22:23:47 +0000854 if (Result != 0) {
855 NewOperands.push_back(Result);
Bob Wilson7248f862009-11-22 04:24:42 +0000856 } else {
David Greenee917fff2009-05-14 22:23:47 +0000857 NewOperands.push_back(Arg);
858 }
Bob Wilson7248f862009-11-22 04:24:42 +0000859 } else if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
David Greenee917fff2009-05-14 22:23:47 +0000860 NewOperands.push_back(Arg);
Bob Wilson7248f862009-11-22 04:24:42 +0000861 } else {
David Greenee917fff2009-05-14 22:23:47 +0000862 NewOperands.push_back(RHSo->getOperand(i));
863 }
864 }
865
866 // Now run the operator and use its result as the new leaf
Eric Christopher71520a82011-07-11 23:06:52 +0000867 OpInit *NewOp = RHSo->clone(NewOperands);
868 Init *NewVal = NewOp->Fold(CurRec, CurMultiClass);
869 if (NewVal != NewOp) {
870 delete NewOp;
David Greenee917fff2009-05-14 22:23:47 +0000871 return NewVal;
872 }
873 return 0;
874}
875
Eric Christopher71520a82011-07-11 23:06:52 +0000876static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
877 Record *CurRec, MultiClass *CurMultiClass) {
878 DagInit *MHSd = dynamic_cast<DagInit*>(MHS);
879 ListInit *MHSl = dynamic_cast<ListInit*>(MHS);
David Greenee917fff2009-05-14 22:23:47 +0000880
881 DagRecTy *DagType = dynamic_cast<DagRecTy*>(Type);
882 ListRecTy *ListType = dynamic_cast<ListRecTy*>(Type);
883
Eric Christopher71520a82011-07-11 23:06:52 +0000884 OpInit *RHSo = dynamic_cast<OpInit*>(RHS);
David Greenee917fff2009-05-14 22:23:47 +0000885
886 if (!RHSo) {
Jim Grosbach59ddb732011-05-06 18:47:45 +0000887 throw TGError(CurRec->getLoc(), "!foreach requires an operator\n");
David Greenee917fff2009-05-14 22:23:47 +0000888 }
889
Eric Christopher71520a82011-07-11 23:06:52 +0000890 TypedInit *LHSt = dynamic_cast<TypedInit*>(LHS);
David Greenee917fff2009-05-14 22:23:47 +0000891
892 if (!LHSt) {
Jim Grosbach59ddb732011-05-06 18:47:45 +0000893 throw TGError(CurRec->getLoc(), "!foreach requires typed variable\n");
David Greenee917fff2009-05-14 22:23:47 +0000894 }
895
Nick Lewycky94298222009-05-15 03:07:14 +0000896 if ((MHSd && DagType) || (MHSl && ListType)) {
David Greenee917fff2009-05-14 22:23:47 +0000897 if (MHSd) {
Eric Christopher71520a82011-07-11 23:06:52 +0000898 Init *Val = MHSd->getOperator();
899 Init *Result = EvaluateOperation(RHSo, LHS, Val,
900 Type, CurRec, CurMultiClass);
David Greenee917fff2009-05-14 22:23:47 +0000901 if (Result != 0) {
902 Val = Result;
903 }
904
Eric Christopher71520a82011-07-11 23:06:52 +0000905 std::vector<std::pair<Init *, std::string> > args;
David Greenee917fff2009-05-14 22:23:47 +0000906 for (unsigned int i = 0; i < MHSd->getNumArgs(); ++i) {
Eric Christopher71520a82011-07-11 23:06:52 +0000907 Init *Arg;
David Greenee917fff2009-05-14 22:23:47 +0000908 std::string ArgName;
909 Arg = MHSd->getArg(i);
910 ArgName = MHSd->getArgName(i);
911
912 // Process args
Eric Christopher71520a82011-07-11 23:06:52 +0000913 Init *Result = EvaluateOperation(RHSo, LHS, Arg, Type,
914 CurRec, CurMultiClass);
David Greenee917fff2009-05-14 22:23:47 +0000915 if (Result != 0) {
916 Arg = Result;
917 }
918
919 // TODO: Process arg names
920 args.push_back(std::make_pair(Arg, ArgName));
921 }
922
Eric Christopher71520a82011-07-11 23:06:52 +0000923 return new DagInit(Val, "", args);
David Greenee917fff2009-05-14 22:23:47 +0000924 }
925 if (MHSl) {
Eric Christopher71520a82011-07-11 23:06:52 +0000926 std::vector<Init *> NewOperands;
927 std::vector<Init *> NewList(MHSl->begin(), MHSl->end());
David Greenee917fff2009-05-14 22:23:47 +0000928
David Greenecdd64322011-07-29 19:07:02 +0000929 for (std::vector<Init *>::iterator li = NewList.begin(),
David Greenee917fff2009-05-14 22:23:47 +0000930 liend = NewList.end();
931 li != liend;
932 ++li) {
Eric Christopher71520a82011-07-11 23:06:52 +0000933 Init *Item = *li;
David Greenee917fff2009-05-14 22:23:47 +0000934 NewOperands.clear();
935 for(int i = 0; i < RHSo->getNumOperands(); ++i) {
936 // First, replace the foreach variable with the list item
937 if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
938 NewOperands.push_back(Item);
Bob Wilson7248f862009-11-22 04:24:42 +0000939 } else {
David Greenee917fff2009-05-14 22:23:47 +0000940 NewOperands.push_back(RHSo->getOperand(i));
941 }
942 }
943
944 // Now run the operator and use its result as the new list item
Eric Christopher71520a82011-07-11 23:06:52 +0000945 OpInit *NewOp = RHSo->clone(NewOperands);
946 Init *NewItem = NewOp->Fold(CurRec, CurMultiClass);
David Greenee917fff2009-05-14 22:23:47 +0000947 if (NewItem != NewOp) {
948 *li = NewItem;
Eric Christopher71520a82011-07-11 23:06:52 +0000949 delete NewOp;
David Greenee917fff2009-05-14 22:23:47 +0000950 }
951 }
Eric Christopher71520a82011-07-11 23:06:52 +0000952 return new ListInit(NewList, MHSl->getType());
David Greenee917fff2009-05-14 22:23:47 +0000953 }
954 }
955 return 0;
956}
957
Eric Christopher71520a82011-07-11 23:06:52 +0000958Init *TernOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
David Greene98ed3c72009-05-14 21:54:42 +0000959 switch (getOpcode()) {
960 default: assert(0 && "Unknown binop");
961 case SUBST: {
Eric Christopher71520a82011-07-11 23:06:52 +0000962 DefInit *LHSd = dynamic_cast<DefInit*>(LHS);
963 VarInit *LHSv = dynamic_cast<VarInit*>(LHS);
964 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
David Greene196ac3c2009-04-23 21:25:15 +0000965
Eric Christopher71520a82011-07-11 23:06:52 +0000966 DefInit *MHSd = dynamic_cast<DefInit*>(MHS);
967 VarInit *MHSv = dynamic_cast<VarInit*>(MHS);
968 StringInit *MHSs = dynamic_cast<StringInit*>(MHS);
David Greene5d0c0512009-05-14 20:54:48 +0000969
Eric Christopher71520a82011-07-11 23:06:52 +0000970 DefInit *RHSd = dynamic_cast<DefInit*>(RHS);
971 VarInit *RHSv = dynamic_cast<VarInit*>(RHS);
972 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
David Greene5d0c0512009-05-14 20:54:48 +0000973
David Greene98ed3c72009-05-14 21:54:42 +0000974 if ((LHSd && MHSd && RHSd)
975 || (LHSv && MHSv && RHSv)
976 || (LHSs && MHSs && RHSs)) {
977 if (RHSd) {
978 Record *Val = RHSd->getDef();
979 if (LHSd->getAsString() == RHSd->getAsString()) {
980 Val = MHSd->getDef();
981 }
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000982 return DefInit::get(Val);
David Greene98ed3c72009-05-14 21:54:42 +0000983 }
984 if (RHSv) {
985 std::string Val = RHSv->getName();
986 if (LHSv->getAsString() == RHSv->getAsString()) {
987 Val = MHSv->getName();
988 }
Eric Christopher71520a82011-07-11 23:06:52 +0000989 return new VarInit(Val, getType());
David Greene98ed3c72009-05-14 21:54:42 +0000990 }
991 if (RHSs) {
992 std::string Val = RHSs->getValue();
David Greene5d0c0512009-05-14 20:54:48 +0000993
David Greene98ed3c72009-05-14 21:54:42 +0000994 std::string::size_type found;
David Greenedbf70742009-12-21 21:21:34 +0000995 std::string::size_type idx = 0;
David Greene98ed3c72009-05-14 21:54:42 +0000996 do {
David Greenedbf70742009-12-21 21:21:34 +0000997 found = Val.find(LHSs->getValue(), idx);
David Greene98ed3c72009-05-14 21:54:42 +0000998 if (found != std::string::npos) {
999 Val.replace(found, LHSs->getValue().size(), MHSs->getValue());
1000 }
David Greenedbf70742009-12-21 21:21:34 +00001001 idx = found + MHSs->getValue().size();
David Greene98ed3c72009-05-14 21:54:42 +00001002 } while (found != std::string::npos);
David Greene5d0c0512009-05-14 20:54:48 +00001003
Eric Christopher71520a82011-07-11 23:06:52 +00001004 return new StringInit(Val);
David Greene98ed3c72009-05-14 21:54:42 +00001005 }
1006 }
1007 break;
Bob Wilson7248f862009-11-22 04:24:42 +00001008 }
David Greene5d0c0512009-05-14 20:54:48 +00001009
David Greene98ed3c72009-05-14 21:54:42 +00001010 case FOREACH: {
Eric Christopher71520a82011-07-11 23:06:52 +00001011 Init *Result = ForeachHelper(LHS, MHS, RHS, getType(),
David Greenee917fff2009-05-14 22:23:47 +00001012 CurRec, CurMultiClass);
1013 if (Result != 0) {
1014 return Result;
David Greene98ed3c72009-05-14 21:54:42 +00001015 }
1016 break;
1017 }
David Greene3587eed2009-05-14 23:26:46 +00001018
1019 case IF: {
Eric Christopher71520a82011-07-11 23:06:52 +00001020 IntInit *LHSi = dynamic_cast<IntInit*>(LHS);
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +00001021 if (Init *I = LHS->convertInitializerTo(IntRecTy::get()))
Eric Christopher71520a82011-07-11 23:06:52 +00001022 LHSi = dynamic_cast<IntInit*>(I);
David Greene3587eed2009-05-14 23:26:46 +00001023 if (LHSi) {
1024 if (LHSi->getValue()) {
1025 return MHS;
Bob Wilson7248f862009-11-22 04:24:42 +00001026 } else {
David Greene3587eed2009-05-14 23:26:46 +00001027 return RHS;
1028 }
1029 }
1030 break;
1031 }
David Greene98ed3c72009-05-14 21:54:42 +00001032 }
David Greene5d0c0512009-05-14 20:54:48 +00001033
David Greene98ed3c72009-05-14 21:54:42 +00001034 return this;
1035}
David Greene5d0c0512009-05-14 20:54:48 +00001036
Eric Christopher71520a82011-07-11 23:06:52 +00001037Init *TernOpInit::resolveReferences(Record &R, const RecordVal *RV) {
1038 Init *lhs = LHS->resolveReferences(R, RV);
David Greeneb0354452009-06-08 19:16:56 +00001039
1040 if (Opc == IF && lhs != LHS) {
Eric Christopher71520a82011-07-11 23:06:52 +00001041 IntInit *Value = dynamic_cast<IntInit*>(lhs);
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +00001042 if (Init *I = lhs->convertInitializerTo(IntRecTy::get()))
Eric Christopher71520a82011-07-11 23:06:52 +00001043 Value = dynamic_cast<IntInit*>(I);
David Greeneb0354452009-06-08 19:16:56 +00001044 if (Value != 0) {
1045 // Short-circuit
1046 if (Value->getValue()) {
Eric Christopher71520a82011-07-11 23:06:52 +00001047 Init *mhs = MHS->resolveReferences(R, RV);
1048 return (new TernOpInit(getOpcode(), lhs, mhs,
1049 RHS, getType()))->Fold(&R, 0);
Bob Wilson7248f862009-11-22 04:24:42 +00001050 } else {
Eric Christopher71520a82011-07-11 23:06:52 +00001051 Init *rhs = RHS->resolveReferences(R, RV);
1052 return (new TernOpInit(getOpcode(), lhs, MHS,
1053 rhs, getType()))->Fold(&R, 0);
David Greeneb0354452009-06-08 19:16:56 +00001054 }
1055 }
1056 }
Bob Wilson7248f862009-11-22 04:24:42 +00001057
Eric Christopher71520a82011-07-11 23:06:52 +00001058 Init *mhs = MHS->resolveReferences(R, RV);
1059 Init *rhs = RHS->resolveReferences(R, RV);
David Greeneb0354452009-06-08 19:16:56 +00001060
David Greene98ed3c72009-05-14 21:54:42 +00001061 if (LHS != lhs || MHS != mhs || RHS != rhs)
Eric Christopher71520a82011-07-11 23:06:52 +00001062 return (new TernOpInit(getOpcode(), lhs, mhs, rhs, getType()))->Fold(&R, 0);
David Greene98ed3c72009-05-14 21:54:42 +00001063 return Fold(&R, 0);
1064}
David Greene196ac3c2009-04-23 21:25:15 +00001065
David Greene98ed3c72009-05-14 21:54:42 +00001066std::string TernOpInit::getAsString() const {
1067 std::string Result;
1068 switch (Opc) {
1069 case SUBST: Result = "!subst"; break;
Bob Wilson7248f862009-11-22 04:24:42 +00001070 case FOREACH: Result = "!foreach"; break;
1071 case IF: Result = "!if"; break;
David Greene98ed3c72009-05-14 21:54:42 +00001072 }
Bob Wilson7248f862009-11-22 04:24:42 +00001073 return Result + "(" + LHS->getAsString() + ", " + MHS->getAsString() + ", "
David Greene98ed3c72009-05-14 21:54:42 +00001074 + RHS->getAsString() + ")";
1075}
David Greene196ac3c2009-04-23 21:25:15 +00001076
David Greene2a9de4d2010-09-03 21:00:49 +00001077RecTy *TypedInit::getFieldType(const std::string &FieldName) const {
1078 RecordRecTy *RecordType = dynamic_cast<RecordRecTy *>(getType());
1079 if (RecordType) {
1080 RecordVal *Field = RecordType->getRecord()->getValue(FieldName);
1081 if (Field) {
1082 return Field->getType();
1083 }
1084 }
1085 return 0;
1086}
1087
Eric Christopher71520a82011-07-11 23:06:52 +00001088Init *TypedInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001089 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
Bill Wendling73a48d82010-12-10 22:54:30 +00001090 if (T == 0) return 0; // Cannot subscript a non-bits variable.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001091 unsigned NumBits = T->getNumBits();
1092
David Greeneb3da8122011-07-29 19:07:00 +00001093 SmallVector<Init *, 16> NewBits(Bits.size());
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001094 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
David Greeneb3da8122011-07-29 19:07:00 +00001095 if (Bits[i] >= NumBits)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001096 return 0;
David Greeneb3da8122011-07-29 19:07:00 +00001097
1098 NewBits[i] = new VarBitInit(this, Bits[i]);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001099 }
David Greeneb3da8122011-07-29 19:07:00 +00001100 return new BitsInit(ArrayRef<Init *>(NewBits));
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001101}
1102
Eric Christopher71520a82011-07-11 23:06:52 +00001103Init *TypedInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
Chris Lattner577fc3f2004-07-27 01:01:21 +00001104 ListRecTy *T = dynamic_cast<ListRecTy*>(getType());
Bill Wendling73a48d82010-12-10 22:54:30 +00001105 if (T == 0) return 0; // Cannot subscript a non-list variable.
Chris Lattner577fc3f2004-07-27 01:01:21 +00001106
1107 if (Elements.size() == 1)
Eric Christopher71520a82011-07-11 23:06:52 +00001108 return new VarListElementInit(this, Elements[0]);
Chris Lattner577fc3f2004-07-27 01:01:21 +00001109
Eric Christopher71520a82011-07-11 23:06:52 +00001110 std::vector<Init*> ListInits;
Chris Lattner577fc3f2004-07-27 01:01:21 +00001111 ListInits.reserve(Elements.size());
1112 for (unsigned i = 0, e = Elements.size(); i != e; ++i)
Eric Christopher71520a82011-07-11 23:06:52 +00001113 ListInits.push_back(new VarListElementInit(this, Elements[i]));
1114 return new ListInit(ListInits, T);
Chris Lattner577fc3f2004-07-27 01:01:21 +00001115}
1116
1117
Eric Christopher71520a82011-07-11 23:06:52 +00001118Init *VarInit::resolveBitReference(Record &R, const RecordVal *IRV,
1119 unsigned Bit) {
Chris Lattneref943742005-04-19 03:36:21 +00001120 if (R.isTemplateArg(getName())) return 0;
1121 if (IRV && IRV->getName() != getName()) return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001122
1123 RecordVal *RV = R.getValue(getName());
Bob Wilson159905a2009-11-21 22:44:20 +00001124 assert(RV && "Reference to a non-existent variable?");
Eric Christopher71520a82011-07-11 23:06:52 +00001125 assert(dynamic_cast<BitsInit*>(RV->getValue()));
1126 BitsInit *BI = (BitsInit*)RV->getValue();
Misha Brukman650ba8e2005-04-22 00:00:37 +00001127
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001128 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
Eric Christopher71520a82011-07-11 23:06:52 +00001129 Init *B = BI->getBit(Bit);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001130
Bob Wilson67e6cab2009-11-22 03:58:57 +00001131 // If the bit is set to some value, or if we are resolving a reference to a
1132 // specific variable and that variable is explicitly unset, then replace the
1133 // VarBitInit with it.
Eric Christopher71520a82011-07-11 23:06:52 +00001134 if (IRV || !dynamic_cast<UnsetInit*>(B))
Bob Wilson67e6cab2009-11-22 03:58:57 +00001135 return B;
Chris Lattner577fc3f2004-07-27 01:01:21 +00001136 return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001137}
1138
Eric Christopher71520a82011-07-11 23:06:52 +00001139Init *VarInit::resolveListElementReference(Record &R, const RecordVal *IRV,
1140 unsigned Elt) {
Chris Lattneref943742005-04-19 03:36:21 +00001141 if (R.isTemplateArg(getName())) return 0;
1142 if (IRV && IRV->getName() != getName()) return 0;
Chris Lattner577fc3f2004-07-27 01:01:21 +00001143
1144 RecordVal *RV = R.getValue(getName());
Bob Wilson159905a2009-11-21 22:44:20 +00001145 assert(RV && "Reference to a non-existent variable?");
Eric Christopher71520a82011-07-11 23:06:52 +00001146 ListInit *LI = dynamic_cast<ListInit*>(RV->getValue());
David Greene9d3febe2009-05-14 20:38:52 +00001147 if (!LI) {
Eric Christopher71520a82011-07-11 23:06:52 +00001148 VarInit *VI = dynamic_cast<VarInit*>(RV->getValue());
David Greene9d3febe2009-05-14 20:38:52 +00001149 assert(VI && "Invalid list element!");
Eric Christopher71520a82011-07-11 23:06:52 +00001150 return new VarListElementInit(VI, Elt);
David Greene9d3febe2009-05-14 20:38:52 +00001151 }
Bob Wilson7248f862009-11-22 04:24:42 +00001152
Chris Lattner577fc3f2004-07-27 01:01:21 +00001153 if (Elt >= LI->getSize())
1154 return 0; // Out of range reference.
Eric Christopher71520a82011-07-11 23:06:52 +00001155 Init *E = LI->getElement(Elt);
Bob Wilson67e6cab2009-11-22 03:58:57 +00001156 // If the element is set to some value, or if we are resolving a reference
1157 // to a specific variable and that variable is explicitly unset, then
1158 // replace the VarListElementInit with it.
Eric Christopher71520a82011-07-11 23:06:52 +00001159 if (IRV || !dynamic_cast<UnsetInit*>(E))
Bob Wilson67e6cab2009-11-22 03:58:57 +00001160 return E;
Chris Lattner577fc3f2004-07-27 01:01:21 +00001161 return 0;
1162}
1163
1164
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001165RecTy *VarInit::getFieldType(const std::string &FieldName) const {
1166 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
1167 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
1168 return RV->getType();
1169 return 0;
1170}
1171
Eric Christopher71520a82011-07-11 23:06:52 +00001172Init *VarInit::getFieldInit(Record &R, const RecordVal *RV,
1173 const std::string &FieldName) const {
Reid Spencer2a826862006-11-02 20:46:16 +00001174 if (dynamic_cast<RecordRecTy*>(getType()))
Jakob Stoklund Olesen0e457622010-03-25 06:23:34 +00001175 if (const RecordVal *Val = R.getValue(VarName)) {
Eric Christopher71520a82011-07-11 23:06:52 +00001176 if (RV != Val && (RV || dynamic_cast<UnsetInit*>(Val->getValue())))
Jakob Stoklund Olesen0e457622010-03-25 06:23:34 +00001177 return 0;
Eric Christopher71520a82011-07-11 23:06:52 +00001178 Init *TheInit = Val->getValue();
Chris Lattnerd959ab92004-02-28 16:31:53 +00001179 assert(TheInit != this && "Infinite loop detected!");
Eric Christopher71520a82011-07-11 23:06:52 +00001180 if (Init *I = TheInit->getFieldInit(R, RV, FieldName))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001181 return I;
1182 else
1183 return 0;
Chris Lattnerd959ab92004-02-28 16:31:53 +00001184 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001185 return 0;
1186}
1187
1188/// resolveReferences - This method is used by classes that refer to other
Bob Wilson159905a2009-11-21 22:44:20 +00001189/// variables which may not be defined at the time the expression is formed.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001190/// If a value is set for the variable later, this method will be called on
1191/// users of the value to allow the value to propagate out.
1192///
Eric Christopher71520a82011-07-11 23:06:52 +00001193Init *VarInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001194 if (RecordVal *Val = R.getValue(VarName))
Eric Christopher71520a82011-07-11 23:06:52 +00001195 if (RV == Val || (RV == 0 && !dynamic_cast<UnsetInit*>(Val->getValue())))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001196 return Val->getValue();
1197 return this;
1198}
Misha Brukman650ba8e2005-04-22 00:00:37 +00001199
Chris Lattner695506c2007-11-22 21:05:25 +00001200std::string VarBitInit::getAsString() const {
1201 return TI->getAsString() + "{" + utostr(Bit) + "}";
1202}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001203
Eric Christopher71520a82011-07-11 23:06:52 +00001204Init *VarBitInit::resolveReferences(Record &R, const RecordVal *RV) {
1205 if (Init *I = getVariable()->resolveBitReference(R, RV, getBitNum()))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001206 return I;
1207 return this;
1208}
1209
Chris Lattner695506c2007-11-22 21:05:25 +00001210std::string VarListElementInit::getAsString() const {
1211 return TI->getAsString() + "[" + utostr(Element) + "]";
1212}
1213
Eric Christopher71520a82011-07-11 23:06:52 +00001214Init *VarListElementInit::resolveReferences(Record &R, const RecordVal *RV) {
1215 if (Init *I = getVariable()->resolveListElementReference(R, RV,
1216 getElementNum()))
Chris Lattner577fc3f2004-07-27 01:01:21 +00001217 return I;
1218 return this;
1219}
1220
Eric Christopher71520a82011-07-11 23:06:52 +00001221Init *VarListElementInit::resolveBitReference(Record &R, const RecordVal *RV,
1222 unsigned Bit) {
Chris Lattner577fc3f2004-07-27 01:01:21 +00001223 // FIXME: This should be implemented, to support references like:
1224 // bit B = AA[0]{1};
1225 return 0;
1226}
1227
Eric Christopher71520a82011-07-11 23:06:52 +00001228Init *VarListElementInit::
1229resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) {
Chris Lattner577fc3f2004-07-27 01:01:21 +00001230 // FIXME: This should be implemented, to support references like:
1231 // int B = AA[0][1];
1232 return 0;
1233}
1234
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +00001235DefInit *DefInit::get(Record *R) {
1236 return R->getDefInit();
1237}
1238
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001239RecTy *DefInit::getFieldType(const std::string &FieldName) const {
1240 if (const RecordVal *RV = Def->getValue(FieldName))
1241 return RV->getType();
1242 return 0;
1243}
1244
Eric Christopher71520a82011-07-11 23:06:52 +00001245Init *DefInit::getFieldInit(Record &R, const RecordVal *RV,
1246 const std::string &FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001247 return Def->getValue(FieldName)->getValue();
1248}
1249
1250
Chris Lattner695506c2007-11-22 21:05:25 +00001251std::string DefInit::getAsString() const {
1252 return Def->getName();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001253}
1254
Eric Christopher71520a82011-07-11 23:06:52 +00001255Init *FieldInit::resolveBitReference(Record &R, const RecordVal *RV,
1256 unsigned Bit) {
1257 if (Init *BitsVal = Rec->getFieldInit(R, RV, FieldName))
1258 if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
David Greeneaf973b42011-07-11 18:25:51 +00001259 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
Eric Christopher71520a82011-07-11 23:06:52 +00001260 Init *B = BI->getBit(Bit);
David Greeneaf973b42011-07-11 18:25:51 +00001261
Eric Christopher71520a82011-07-11 23:06:52 +00001262 if (dynamic_cast<BitInit*>(B)) // If the bit is set.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001263 return B; // Replace the VarBitInit with it.
1264 }
Chris Lattner577fc3f2004-07-27 01:01:21 +00001265 return 0;
1266}
1267
Eric Christopher71520a82011-07-11 23:06:52 +00001268Init *FieldInit::resolveListElementReference(Record &R, const RecordVal *RV,
1269 unsigned Elt) {
1270 if (Init *ListVal = Rec->getFieldInit(R, RV, FieldName))
1271 if (ListInit *LI = dynamic_cast<ListInit*>(ListVal)) {
Chris Lattner577fc3f2004-07-27 01:01:21 +00001272 if (Elt >= LI->getSize()) return 0;
Eric Christopher71520a82011-07-11 23:06:52 +00001273 Init *E = LI->getElement(Elt);
Chris Lattner577fc3f2004-07-27 01:01:21 +00001274
Bob Wilson67e6cab2009-11-22 03:58:57 +00001275 // If the element is set to some value, or if we are resolving a
1276 // reference to a specific variable and that variable is explicitly
1277 // unset, then replace the VarListElementInit with it.
Eric Christopher71520a82011-07-11 23:06:52 +00001278 if (RV || !dynamic_cast<UnsetInit*>(E))
Bob Wilson67e6cab2009-11-22 03:58:57 +00001279 return E;
Chris Lattner577fc3f2004-07-27 01:01:21 +00001280 }
1281 return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001282}
1283
Eric Christopher71520a82011-07-11 23:06:52 +00001284Init *FieldInit::resolveReferences(Record &R, const RecordVal *RV) {
1285 Init *NewRec = RV ? Rec->resolveReferences(R, RV) : Rec;
Chris Lattneref943742005-04-19 03:36:21 +00001286
Eric Christopher71520a82011-07-11 23:06:52 +00001287 Init *BitsVal = NewRec->getFieldInit(R, RV, FieldName);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001288 if (BitsVal) {
Eric Christopher71520a82011-07-11 23:06:52 +00001289 Init *BVR = BitsVal->resolveReferences(R, RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001290 return BVR->isComplete() ? BVR : this;
1291 }
Chris Lattneref943742005-04-19 03:36:21 +00001292
1293 if (NewRec != Rec) {
Eric Christopher71520a82011-07-11 23:06:52 +00001294 return new FieldInit(NewRec, FieldName);
Chris Lattneref943742005-04-19 03:36:21 +00001295 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001296 return this;
1297}
1298
Eric Christopher71520a82011-07-11 23:06:52 +00001299Init *DagInit::resolveReferences(Record &R, const RecordVal *RV) {
1300 std::vector<Init*> NewArgs;
Chris Lattner0d3ef402006-01-31 06:02:35 +00001301 for (unsigned i = 0, e = Args.size(); i != e; ++i)
1302 NewArgs.push_back(Args[i]->resolveReferences(R, RV));
Bob Wilson7248f862009-11-22 04:24:42 +00001303
Eric Christopher71520a82011-07-11 23:06:52 +00001304 Init *Op = Val->resolveReferences(R, RV);
Bob Wilson7248f862009-11-22 04:24:42 +00001305
Chris Lattnerb59cf3c2006-03-30 22:50:40 +00001306 if (Args != NewArgs || Op != Val)
Eric Christopher71520a82011-07-11 23:06:52 +00001307 return new DagInit(Op, ValName, NewArgs, ArgNames);
Bob Wilson7248f862009-11-22 04:24:42 +00001308
Chris Lattner0d3ef402006-01-31 06:02:35 +00001309 return this;
1310}
1311
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001312
Chris Lattner695506c2007-11-22 21:05:25 +00001313std::string DagInit::getAsString() const {
1314 std::string Result = "(" + Val->getAsString();
Nate Begemandbe3f772009-03-19 05:21:56 +00001315 if (!ValName.empty())
1316 Result += ":" + ValName;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001317 if (Args.size()) {
Chris Lattner695506c2007-11-22 21:05:25 +00001318 Result += " " + Args[0]->getAsString();
1319 if (!ArgNames[0].empty()) Result += ":$" + ArgNames[0];
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001320 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
Chris Lattner695506c2007-11-22 21:05:25 +00001321 Result += ", " + Args[i]->getAsString();
1322 if (!ArgNames[i].empty()) Result += ":$" + ArgNames[i];
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001323 }
1324 }
Chris Lattner695506c2007-11-22 21:05:25 +00001325 return Result + ")";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001326}
1327
1328
1329//===----------------------------------------------------------------------===//
1330// Other implementations
1331//===----------------------------------------------------------------------===//
1332
1333RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
1334 : Name(N), Ty(T), Prefix(P) {
Eric Christopher71520a82011-07-11 23:06:52 +00001335 Value = Ty->convertValue(new UnsetInit());
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001336 assert(Value && "Cannot create unset value for current type!");
1337}
1338
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001339void RecordVal::dump() const { errs() << *this; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001340
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001341void RecordVal::print(raw_ostream &OS, bool PrintSem) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001342 if (getPrefix()) OS << "field ";
1343 OS << *getType() << " " << getName();
Chris Lattneref943742005-04-19 03:36:21 +00001344
1345 if (getValue())
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001346 OS << " = " << *getValue();
Chris Lattneref943742005-04-19 03:36:21 +00001347
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001348 if (PrintSem) OS << ";\n";
1349}
1350
Daniel Dunbarced00812009-08-23 09:47:37 +00001351unsigned Record::LastID = 0;
1352
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +00001353DefInit *Record::getDefInit() {
1354 if (!TheInit)
1355 TheInit = new DefInit(this, new RecordRecTy(this));
1356 return TheInit;
1357}
1358
Chris Lattnerac284252005-08-19 17:58:11 +00001359void Record::setName(const std::string &Name) {
Chris Lattner77d369c2010-12-13 00:23:57 +00001360 if (TrackedRecords.getDef(getName()) == this) {
1361 TrackedRecords.removeDef(getName());
Chris Lattnerac284252005-08-19 17:58:11 +00001362 this->Name = Name;
Chris Lattner77d369c2010-12-13 00:23:57 +00001363 TrackedRecords.addDef(this);
Chris Lattnerac284252005-08-19 17:58:11 +00001364 } else {
Chris Lattner77d369c2010-12-13 00:23:57 +00001365 TrackedRecords.removeClass(getName());
Chris Lattnerac284252005-08-19 17:58:11 +00001366 this->Name = Name;
Chris Lattner77d369c2010-12-13 00:23:57 +00001367 TrackedRecords.addClass(this);
Chris Lattnerac284252005-08-19 17:58:11 +00001368 }
1369}
1370
Chris Lattneref943742005-04-19 03:36:21 +00001371/// resolveReferencesTo - If anything in this record refers to RV, replace the
1372/// reference to RV with the RHS of RV. If RV is null, we resolve all possible
1373/// references.
1374void Record::resolveReferencesTo(const RecordVal *RV) {
1375 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
Eric Christopher71520a82011-07-11 23:06:52 +00001376 if (Init *V = Values[i].getValue())
Chris Lattneref943742005-04-19 03:36:21 +00001377 Values[i].setValue(V->resolveReferences(*this, RV));
1378 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001379}
1380
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001381void Record::dump() const { errs() << *this; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001382
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001383raw_ostream &llvm::operator<<(raw_ostream &OS, const Record &R) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001384 OS << R.getName();
1385
1386 const std::vector<std::string> &TArgs = R.getTemplateArgs();
1387 if (!TArgs.empty()) {
1388 OS << "<";
1389 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1390 if (i) OS << ", ";
1391 const RecordVal *RV = R.getValue(TArgs[i]);
1392 assert(RV && "Template argument record not found??");
1393 RV->print(OS, false);
1394 }
1395 OS << ">";
1396 }
1397
1398 OS << " {";
1399 const std::vector<Record*> &SC = R.getSuperClasses();
1400 if (!SC.empty()) {
1401 OS << "\t//";
1402 for (unsigned i = 0, e = SC.size(); i != e; ++i)
1403 OS << " " << SC[i]->getName();
1404 }
1405 OS << "\n";
1406
1407 const std::vector<RecordVal> &Vals = R.getValues();
1408 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
1409 if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
1410 OS << Vals[i];
1411 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
1412 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
1413 OS << Vals[i];
1414
1415 return OS << "}\n";
1416}
1417
1418/// getValueInit - Return the initializer for a value with the specified name,
1419/// or throw an exception if the field does not exist.
1420///
Eric Christopher71520a82011-07-11 23:06:52 +00001421Init *Record::getValueInit(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001422 const RecordVal *R = getValue(FieldName);
1423 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001424 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001425 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001426 return R->getValue();
1427}
1428
1429
1430/// getValueAsString - This method looks up the specified field and returns its
1431/// value as a string, throwing an exception if the field does not exist or if
1432/// the value is not a string.
1433///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001434std::string Record::getValueAsString(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001435 const RecordVal *R = getValue(FieldName);
1436 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001437 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001438 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001439
1440 if (const StringInit *SI = dynamic_cast<const StringInit*>(R->getValue()))
1441 return SI->getValue();
Chris Lattner42bb0c12009-09-18 18:31:37 +00001442 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001443 "' does not have a string initializer!";
1444}
1445
1446/// getValueAsBitsInit - This method looks up the specified field and returns
1447/// its value as a BitsInit, throwing an exception if the field does not exist
1448/// or if the value is not the right type.
1449///
Eric Christopher71520a82011-07-11 23:06:52 +00001450BitsInit *Record::getValueAsBitsInit(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001451 const RecordVal *R = getValue(FieldName);
1452 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001453 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001454 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001455
Eric Christopher71520a82011-07-11 23:06:52 +00001456 if (BitsInit *BI = dynamic_cast<BitsInit*>(R->getValue()))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001457 return BI;
Chris Lattner42bb0c12009-09-18 18:31:37 +00001458 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001459 "' does not have a BitsInit initializer!";
1460}
1461
1462/// getValueAsListInit - This method looks up the specified field and returns
1463/// its value as a ListInit, throwing an exception if the field does not exist
1464/// or if the value is not the right type.
1465///
Eric Christopher71520a82011-07-11 23:06:52 +00001466ListInit *Record::getValueAsListInit(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001467 const RecordVal *R = getValue(FieldName);
1468 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001469 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001470 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001471
Eric Christopher71520a82011-07-11 23:06:52 +00001472 if (ListInit *LI = dynamic_cast<ListInit*>(R->getValue()))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001473 return LI;
Chris Lattner42bb0c12009-09-18 18:31:37 +00001474 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001475 "' does not have a list initializer!";
1476}
1477
Chris Lattner7ad0bed2005-10-28 22:49:02 +00001478/// getValueAsListOfDefs - This method looks up the specified field and returns
Jim Laskeyb04feb62005-10-28 21:46:31 +00001479/// its value as a vector of records, throwing an exception if the field does
1480/// not exist or if the value is not the right type.
1481///
Bob Wilson7248f862009-11-22 04:24:42 +00001482std::vector<Record*>
Chris Lattner42bb0c12009-09-18 18:31:37 +00001483Record::getValueAsListOfDefs(StringRef FieldName) const {
Eric Christopher71520a82011-07-11 23:06:52 +00001484 ListInit *List = getValueAsListInit(FieldName);
Jim Laskeyb04feb62005-10-28 21:46:31 +00001485 std::vector<Record*> Defs;
1486 for (unsigned i = 0; i < List->getSize(); i++) {
Eric Christopher71520a82011-07-11 23:06:52 +00001487 if (DefInit *DI = dynamic_cast<DefInit*>(List->getElement(i))) {
Jim Laskeyb04feb62005-10-28 21:46:31 +00001488 Defs.push_back(DI->getDef());
1489 } else {
Chris Lattner42bb0c12009-09-18 18:31:37 +00001490 throw "Record `" + getName() + "', field `" + FieldName.str() +
Jim Laskeyb04feb62005-10-28 21:46:31 +00001491 "' list is not entirely DefInit!";
1492 }
1493 }
1494 return Defs;
1495}
1496
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001497/// getValueAsInt - This method looks up the specified field and returns its
Dan Gohmanca0546f2008-10-17 01:33:43 +00001498/// value as an int64_t, throwing an exception if the field does not exist or if
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001499/// the value is not the right type.
1500///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001501int64_t Record::getValueAsInt(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001502 const RecordVal *R = getValue(FieldName);
1503 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001504 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001505 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001506
Eric Christopher71520a82011-07-11 23:06:52 +00001507 if (IntInit *II = dynamic_cast<IntInit*>(R->getValue()))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001508 return II->getValue();
Chris Lattner42bb0c12009-09-18 18:31:37 +00001509 throw "Record `" + getName() + "', field `" + FieldName.str() +
Nate Begemanf621b332005-11-30 18:37:14 +00001510 "' does not have an int initializer!";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001511}
1512
Anton Korobeynikova468a112007-11-11 11:19:37 +00001513/// getValueAsListOfInts - This method looks up the specified field and returns
1514/// its value as a vector of integers, throwing an exception if the field does
1515/// not exist or if the value is not the right type.
1516///
Bob Wilson7248f862009-11-22 04:24:42 +00001517std::vector<int64_t>
Chris Lattner42bb0c12009-09-18 18:31:37 +00001518Record::getValueAsListOfInts(StringRef FieldName) const {
Eric Christopher71520a82011-07-11 23:06:52 +00001519 ListInit *List = getValueAsListInit(FieldName);
Dan Gohmanca0546f2008-10-17 01:33:43 +00001520 std::vector<int64_t> Ints;
Anton Korobeynikova468a112007-11-11 11:19:37 +00001521 for (unsigned i = 0; i < List->getSize(); i++) {
Eric Christopher71520a82011-07-11 23:06:52 +00001522 if (IntInit *II = dynamic_cast<IntInit*>(List->getElement(i))) {
Anton Korobeynikova468a112007-11-11 11:19:37 +00001523 Ints.push_back(II->getValue());
1524 } else {
Chris Lattner42bb0c12009-09-18 18:31:37 +00001525 throw "Record `" + getName() + "', field `" + FieldName.str() +
Anton Korobeynikova468a112007-11-11 11:19:37 +00001526 "' does not have a list of ints initializer!";
1527 }
1528 }
1529 return Ints;
1530}
1531
Owen Andersona84be6c2011-06-27 21:06:21 +00001532/// getValueAsListOfStrings - This method looks up the specified field and
1533/// returns its value as a vector of strings, throwing an exception if the
1534/// field does not exist or if the value is not the right type.
1535///
1536std::vector<std::string>
1537Record::getValueAsListOfStrings(StringRef FieldName) const {
Eric Christopher71520a82011-07-11 23:06:52 +00001538 ListInit *List = getValueAsListInit(FieldName);
Owen Andersona84be6c2011-06-27 21:06:21 +00001539 std::vector<std::string> Strings;
1540 for (unsigned i = 0; i < List->getSize(); i++) {
Eric Christopher71520a82011-07-11 23:06:52 +00001541 if (StringInit *II = dynamic_cast<StringInit*>(List->getElement(i))) {
Owen Andersona84be6c2011-06-27 21:06:21 +00001542 Strings.push_back(II->getValue());
1543 } else {
1544 throw "Record `" + getName() + "', field `" + FieldName.str() +
1545 "' does not have a list of strings initializer!";
1546 }
1547 }
1548 return Strings;
1549}
1550
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001551/// getValueAsDef - This method looks up the specified field and returns its
1552/// value as a Record, throwing an exception if the field does not exist or if
1553/// the value is not the right type.
1554///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001555Record *Record::getValueAsDef(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001556 const RecordVal *R = getValue(FieldName);
1557 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001558 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001559 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001560
Eric Christopher71520a82011-07-11 23:06:52 +00001561 if (DefInit *DI = dynamic_cast<DefInit*>(R->getValue()))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001562 return DI->getDef();
Chris Lattner42bb0c12009-09-18 18:31:37 +00001563 throw "Record `" + getName() + "', field `" + FieldName.str() +
Nate Begemanf621b332005-11-30 18:37:14 +00001564 "' does not have a def initializer!";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001565}
1566
1567/// getValueAsBit - This method looks up the specified field and returns its
1568/// value as a bit, throwing an exception if the field does not exist or if
1569/// the value is not the right type.
1570///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001571bool Record::getValueAsBit(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001572 const RecordVal *R = getValue(FieldName);
1573 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001574 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001575 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001576
Eric Christopher71520a82011-07-11 23:06:52 +00001577 if (BitInit *BI = dynamic_cast<BitInit*>(R->getValue()))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001578 return BI->getValue();
Chris Lattner42bb0c12009-09-18 18:31:37 +00001579 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001580 "' does not have a bit initializer!";
1581}
1582
1583/// getValueAsDag - This method looks up the specified field and returns its
1584/// value as an Dag, throwing an exception if the field does not exist or if
1585/// the value is not the right type.
1586///
Eric Christopher71520a82011-07-11 23:06:52 +00001587DagInit *Record::getValueAsDag(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001588 const RecordVal *R = getValue(FieldName);
1589 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001590 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001591 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001592
Eric Christopher71520a82011-07-11 23:06:52 +00001593 if (DagInit *DI = dynamic_cast<DagInit*>(R->getValue()))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001594 return DI;
Chris Lattner42bb0c12009-09-18 18:31:37 +00001595 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001596 "' does not have a dag initializer!";
1597}
1598
Chris Lattner42bb0c12009-09-18 18:31:37 +00001599std::string Record::getValueAsCode(StringRef FieldName) const {
Chris Lattnerae939eb2005-09-13 21:44:28 +00001600 const RecordVal *R = getValue(FieldName);
1601 if (R == 0 || R->getValue() == 0)
1602 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001603 FieldName.str() + "'!\n";
Bob Wilson7248f862009-11-22 04:24:42 +00001604
Chris Lattnerae939eb2005-09-13 21:44:28 +00001605 if (const CodeInit *CI = dynamic_cast<const CodeInit*>(R->getValue()))
1606 return CI->getValue();
Chris Lattner42bb0c12009-09-18 18:31:37 +00001607 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerae939eb2005-09-13 21:44:28 +00001608 "' does not have a code initializer!";
1609}
1610
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001611
David Greene7049e792009-04-24 16:55:41 +00001612void MultiClass::dump() const {
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001613 errs() << "Record:\n";
David Greene7049e792009-04-24 16:55:41 +00001614 Rec.dump();
Bob Wilson7248f862009-11-22 04:24:42 +00001615
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001616 errs() << "Defs:\n";
David Greene7049e792009-04-24 16:55:41 +00001617 for (RecordVector::const_iterator r = DefPrototypes.begin(),
1618 rend = DefPrototypes.end();
1619 r != rend;
1620 ++r) {
1621 (*r)->dump();
1622 }
1623}
1624
1625
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001626void RecordKeeper::dump() const { errs() << *this; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001627
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001628raw_ostream &llvm::operator<<(raw_ostream &OS, const RecordKeeper &RK) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001629 OS << "------------- Classes -----------------\n";
1630 const std::map<std::string, Record*> &Classes = RK.getClasses();
1631 for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001632 E = Classes.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001633 OS << "class " << *I->second;
Misha Brukman650ba8e2005-04-22 00:00:37 +00001634
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001635 OS << "------------- Defs -----------------\n";
1636 const std::map<std::string, Record*> &Defs = RK.getDefs();
1637 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001638 E = Defs.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001639 OS << "def " << *I->second;
1640 return OS;
1641}
1642
1643
1644/// getAllDerivedDefinitions - This method returns all concrete definitions
1645/// that derive from the specified class name. If a class with the specified
1646/// name does not exist, an error is printed and true is returned.
1647std::vector<Record*>
1648RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
Chris Lattner97049072010-12-13 00:20:52 +00001649 Record *Class = getClass(ClassName);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001650 if (!Class)
Misha Brukman41f9f292004-10-08 14:59:05 +00001651 throw "ERROR: Couldn't find the `" + ClassName + "' class!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001652
1653 std::vector<Record*> Defs;
1654 for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(),
1655 E = getDefs().end(); I != E; ++I)
1656 if (I->second->isSubClassOf(Class))
1657 Defs.push_back(I->second);
1658
1659 return Defs;
1660}
Brian Gaeke960707c2003-11-11 22:41:34 +00001661