blob: 3aaaa8798994b2a254746a9fde171838f403e51c [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"
Chris Lattner8b9ecda2007-11-20 22:25:16 +000018#include "llvm/ADT/StringExtras.h"
Duraid Madina14492af2005-12-26 05:08:55 +000019
Chris Lattner68478662004-08-01 03:55:39 +000020using namespace llvm;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000021
22//===----------------------------------------------------------------------===//
23// Type implementations
24//===----------------------------------------------------------------------===//
25
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +000026BitRecTy BitRecTy::Shared;
27IntRecTy IntRecTy::Shared;
28StringRecTy StringRecTy::Shared;
29CodeRecTy CodeRecTy::Shared;
30DagRecTy DagRecTy::Shared;
31
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000032void RecTy::dump() const { print(errs()); }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000033
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +000034ListRecTy *RecTy::getListTy() {
35 if (!ListTy)
36 ListTy = new ListRecTy(this);
37 return ListTy;
38}
39
Eric Christopher71520a82011-07-11 23:06:52 +000040Init *BitRecTy::convertValue(BitsInit *BI) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000041 if (BI->getNumBits() != 1) return 0; // Only accept if just one bit!
42 return BI->getBit(0);
43}
44
45bool BitRecTy::baseClassOf(const BitsRecTy *RHS) const {
46 return RHS->getNumBits() == 1;
47}
48
Eric Christopher71520a82011-07-11 23:06:52 +000049Init *BitRecTy::convertValue(IntInit *II) {
Dan Gohmanca0546f2008-10-17 01:33:43 +000050 int64_t Val = II->getValue();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000051 if (Val != 0 && Val != 1) return 0; // Only accept 0 or 1 for a bit!
Misha Brukman650ba8e2005-04-22 00:00:37 +000052
Eric Christopher71520a82011-07-11 23:06:52 +000053 return new BitInit(Val != 0);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000054}
55
Eric Christopher71520a82011-07-11 23:06:52 +000056Init *BitRecTy::convertValue(TypedInit *VI) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000057 if (dynamic_cast<BitRecTy*>(VI->getType()))
58 return VI; // Accept variable if it is already of bit type!
59 return 0;
60}
61
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +000062BitsRecTy *BitsRecTy::get(unsigned Sz) {
63 static std::vector<BitsRecTy*> Shared;
64 if (Sz >= Shared.size())
65 Shared.resize(Sz + 1);
66 BitsRecTy *&Ty = Shared[Sz];
67 if (!Ty)
68 Ty = new BitsRecTy(Sz);
69 return Ty;
70}
71
Chris Lattner8b9ecda2007-11-20 22:25:16 +000072std::string BitsRecTy::getAsString() const {
73 return "bits<" + utostr(Size) + ">";
74}
75
Eric Christopher71520a82011-07-11 23:06:52 +000076Init *BitsRecTy::convertValue(UnsetInit *UI) {
77 BitsInit *Ret = new BitsInit(Size);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000078
79 for (unsigned i = 0; i != Size; ++i)
Eric Christopher71520a82011-07-11 23:06:52 +000080 Ret->setBit(i, new UnsetInit());
81 return Ret;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000082}
83
Eric Christopher71520a82011-07-11 23:06:52 +000084Init *BitsRecTy::convertValue(BitInit *UI) {
Bill Wendling73a48d82010-12-10 22:54:30 +000085 if (Size != 1) return 0; // Can only convert single bit.
Eric Christopher71520a82011-07-11 23:06:52 +000086 BitsInit *Ret = new BitsInit(1);
87 Ret->setBit(0, UI);
88 return Ret;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000089}
90
Bill Wendling73ce4a62010-12-13 01:46:19 +000091/// canFitInBitfield - Return true if the number of bits is large enough to hold
92/// the integer value.
93static bool canFitInBitfield(int64_t Value, unsigned NumBits) {
Nick Lewycky79286bf2011-06-03 08:25:39 +000094 // For example, with NumBits == 4, we permit Values from [-7 .. 15].
95 return (NumBits >= sizeof(Value) * 8) ||
96 (Value >> NumBits == 0) || (Value >> (NumBits-1) == -1);
Bill Wendling73ce4a62010-12-13 01:46:19 +000097}
98
Eric Christopher71520a82011-07-11 23:06:52 +000099/// convertValue from Int initializer to bits type: Split the integer up into the
100/// appropriate bits.
Bill Wendling73ce4a62010-12-13 01:46:19 +0000101///
Eric Christopher71520a82011-07-11 23:06:52 +0000102Init *BitsRecTy::convertValue(IntInit *II) {
Misha Brukman6752fb52004-06-21 18:01:47 +0000103 int64_t Value = II->getValue();
Bill Wendling73a48d82010-12-10 22:54:30 +0000104 // Make sure this bitfield is large enough to hold the integer value.
Bill Wendling73ce4a62010-12-13 01:46:19 +0000105 if (!canFitInBitfield(Value, Size))
106 return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000107
Eric Christopher71520a82011-07-11 23:06:52 +0000108 BitsInit *Ret = new BitsInit(Size);
David Greeneaf973b42011-07-11 18:25:51 +0000109 for (unsigned i = 0; i != Size; ++i)
Eric Christopher71520a82011-07-11 23:06:52 +0000110 Ret->setBit(i, new BitInit(Value & (1LL << i)));
David Greeneaf973b42011-07-11 18:25:51 +0000111
Eric Christopher71520a82011-07-11 23:06:52 +0000112 return Ret;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000113}
114
Eric Christopher71520a82011-07-11 23:06:52 +0000115Init *BitsRecTy::convertValue(BitsInit *BI) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000116 // If the number of bits is right, return it. Otherwise we need to expand or
Bill Wendling73a48d82010-12-10 22:54:30 +0000117 // truncate.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000118 if (BI->getNumBits() == Size) return BI;
119 return 0;
120}
121
Eric Christopher71520a82011-07-11 23:06:52 +0000122Init *BitsRecTy::convertValue(TypedInit *VI) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000123 if (BitsRecTy *BRT = dynamic_cast<BitsRecTy*>(VI->getType()))
124 if (BRT->Size == Size) {
Eric Christopher71520a82011-07-11 23:06:52 +0000125 BitsInit *Ret = new BitsInit(Size);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000126 for (unsigned i = 0; i != Size; ++i)
Eric Christopher71520a82011-07-11 23:06:52 +0000127 Ret->setBit(i, new VarBitInit(VI, i));
128 return Ret;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000129 }
Bill Wendling73ce4a62010-12-13 01:46:19 +0000130
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000131 if (Size == 1 && dynamic_cast<BitRecTy*>(VI->getType())) {
Eric Christopher71520a82011-07-11 23:06:52 +0000132 BitsInit *Ret = new BitsInit(1);
133 Ret->setBit(0, VI);
134 return Ret;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000135 }
Misha Brukman650ba8e2005-04-22 00:00:37 +0000136
Eric Christopher71520a82011-07-11 23:06:52 +0000137 if (TernOpInit *Tern = dynamic_cast<TernOpInit*>(VI)) {
Bill Wendling73ce4a62010-12-13 01:46:19 +0000138 if (Tern->getOpcode() == TernOpInit::IF) {
Eric Christopher71520a82011-07-11 23:06:52 +0000139 Init *LHS = Tern->getLHS();
140 Init *MHS = Tern->getMHS();
141 Init *RHS = Tern->getRHS();
Bill Wendling73ce4a62010-12-13 01:46:19 +0000142
Eric Christopher71520a82011-07-11 23:06:52 +0000143 IntInit *MHSi = dynamic_cast<IntInit*>(MHS);
144 IntInit *RHSi = dynamic_cast<IntInit*>(RHS);
Bill Wendling73ce4a62010-12-13 01:46:19 +0000145
146 if (MHSi && RHSi) {
147 int64_t MHSVal = MHSi->getValue();
148 int64_t RHSVal = RHSi->getValue();
149
150 if (canFitInBitfield(MHSVal, Size) && canFitInBitfield(RHSVal, Size)) {
Eric Christopher71520a82011-07-11 23:06:52 +0000151 BitsInit *Ret = new BitsInit(Size);
Bill Wendling73ce4a62010-12-13 01:46:19 +0000152
153 for (unsigned i = 0; i != Size; ++i)
Eric Christopher71520a82011-07-11 23:06:52 +0000154 Ret->setBit(i, new TernOpInit(TernOpInit::IF, LHS,
155 new IntInit((MHSVal & (1LL << i)) ? 1 : 0),
156 new IntInit((RHSVal & (1LL << i)) ? 1 : 0),
157 VI->getType()));
158
159 return Ret;
Bill Wendling73ce4a62010-12-13 01:46:19 +0000160 }
161 } else {
Eric Christopher71520a82011-07-11 23:06:52 +0000162 BitsInit *MHSbs = dynamic_cast<BitsInit*>(MHS);
163 BitsInit *RHSbs = dynamic_cast<BitsInit*>(RHS);
Bill Wendling73ce4a62010-12-13 01:46:19 +0000164
165 if (MHSbs && RHSbs) {
Eric Christopher71520a82011-07-11 23:06:52 +0000166 BitsInit *Ret = new BitsInit(Size);
Bill Wendling73ce4a62010-12-13 01:46:19 +0000167
168 for (unsigned i = 0; i != Size; ++i)
Eric Christopher71520a82011-07-11 23:06:52 +0000169 Ret->setBit(i, new TernOpInit(TernOpInit::IF, LHS,
170 MHSbs->getBit(i),
171 RHSbs->getBit(i),
172 VI->getType()));
Bill Wendling73ce4a62010-12-13 01:46:19 +0000173
Eric Christopher71520a82011-07-11 23:06:52 +0000174 return Ret;
Bill Wendling73ce4a62010-12-13 01:46:19 +0000175 }
176 }
177 }
178 }
179
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000180 return 0;
181}
182
Eric Christopher71520a82011-07-11 23:06:52 +0000183Init *IntRecTy::convertValue(BitInit *BI) {
184 return new IntInit(BI->getValue());
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000185}
186
Eric Christopher71520a82011-07-11 23:06:52 +0000187Init *IntRecTy::convertValue(BitsInit *BI) {
Dan Gohmanca0546f2008-10-17 01:33:43 +0000188 int64_t Result = 0;
Misha Brukman650ba8e2005-04-22 00:00:37 +0000189 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
Eric Christopher71520a82011-07-11 23:06:52 +0000190 if (BitInit *Bit = dynamic_cast<BitInit*>(BI->getBit(i))) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000191 Result |= Bit->getValue() << i;
192 } else {
193 return 0;
194 }
Eric Christopher71520a82011-07-11 23:06:52 +0000195 return new IntInit(Result);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000196}
197
Eric Christopher71520a82011-07-11 23:06:52 +0000198Init *IntRecTy::convertValue(TypedInit *TI) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000199 if (TI->getType()->typeIsConvertibleTo(this))
200 return TI; // Accept variable if already of the right type!
201 return 0;
202}
203
Eric Christopher71520a82011-07-11 23:06:52 +0000204Init *StringRecTy::convertValue(UnOpInit *BO) {
David Greenee8f3b272009-05-14 21:22:49 +0000205 if (BO->getOpcode() == UnOpInit::CAST) {
Eric Christopher71520a82011-07-11 23:06:52 +0000206 Init *L = BO->getOperand()->convertInitializerTo(this);
David Greenee8f3b272009-05-14 21:22:49 +0000207 if (L == 0) return 0;
208 if (L != BO->getOperand())
Eric Christopher71520a82011-07-11 23:06:52 +0000209 return new UnOpInit(UnOpInit::CAST, L, new StringRecTy);
David Greenee8f3b272009-05-14 21:22:49 +0000210 return BO;
211 }
David Greene5d0c0512009-05-14 20:54:48 +0000212
Eric Christopher71520a82011-07-11 23:06:52 +0000213 return convertValue((TypedInit*)BO);
David Greenee8f3b272009-05-14 21:22:49 +0000214}
David Greene5d0c0512009-05-14 20:54:48 +0000215
Eric Christopher71520a82011-07-11 23:06:52 +0000216Init *StringRecTy::convertValue(BinOpInit *BO) {
Chris Lattner51ffbf12006-03-31 21:53:49 +0000217 if (BO->getOpcode() == BinOpInit::STRCONCAT) {
Eric Christopher71520a82011-07-11 23:06:52 +0000218 Init *L = BO->getLHS()->convertInitializerTo(this);
219 Init *R = BO->getRHS()->convertInitializerTo(this);
Chris Lattner51ffbf12006-03-31 21:53:49 +0000220 if (L == 0 || R == 0) return 0;
221 if (L != BO->getLHS() || R != BO->getRHS())
Eric Christopher71520a82011-07-11 23:06:52 +0000222 return new BinOpInit(BinOpInit::STRCONCAT, L, R, new StringRecTy);
Chris Lattner51ffbf12006-03-31 21:53:49 +0000223 return BO;
224 }
David Greene196ac3c2009-04-23 21:25:15 +0000225
Eric Christopher71520a82011-07-11 23:06:52 +0000226 return convertValue((TypedInit*)BO);
Chris Lattner51ffbf12006-03-31 21:53:49 +0000227}
228
229
Eric Christopher71520a82011-07-11 23:06:52 +0000230Init *StringRecTy::convertValue(TypedInit *TI) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000231 if (dynamic_cast<StringRecTy*>(TI->getType()))
232 return TI; // Accept variable if already of the right type!
233 return 0;
234}
235
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000236std::string ListRecTy::getAsString() const {
237 return "list<" + Ty->getAsString() + ">";
238}
239
Eric Christopher71520a82011-07-11 23:06:52 +0000240Init *ListRecTy::convertValue(ListInit *LI) {
241 std::vector<Init*> Elements;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000242
243 // Verify that all of the elements of the list are subclasses of the
244 // appropriate class!
245 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
Eric Christopher71520a82011-07-11 23:06:52 +0000246 if (Init *CI = LI->getElement(i)->convertInitializerTo(Ty))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000247 Elements.push_back(CI);
248 else
249 return 0;
250
David Greene8618f952009-06-08 20:23:18 +0000251 ListRecTy *LType = dynamic_cast<ListRecTy*>(LI->getType());
252 if (LType == 0) {
253 return 0;
254 }
255
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000256 return new ListInit(Elements, this);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000257}
258
Eric Christopher71520a82011-07-11 23:06:52 +0000259Init *ListRecTy::convertValue(TypedInit *TI) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000260 // Ensure that TI is compatible with our class.
261 if (ListRecTy *LRT = dynamic_cast<ListRecTy*>(TI->getType()))
262 if (LRT->getElementType()->typeIsConvertibleTo(getElementType()))
263 return TI;
264 return 0;
265}
266
Eric Christopher71520a82011-07-11 23:06:52 +0000267Init *CodeRecTy::convertValue(TypedInit *TI) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000268 if (TI->getType()->typeIsConvertibleTo(this))
269 return TI;
270 return 0;
271}
272
Eric Christopher71520a82011-07-11 23:06:52 +0000273Init *DagRecTy::convertValue(TypedInit *TI) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000274 if (TI->getType()->typeIsConvertibleTo(this))
275 return TI;
276 return 0;
277}
278
Eric Christopher71520a82011-07-11 23:06:52 +0000279Init *DagRecTy::convertValue(UnOpInit *BO) {
David Greenee8f3b272009-05-14 21:22:49 +0000280 if (BO->getOpcode() == UnOpInit::CAST) {
Eric Christopher71520a82011-07-11 23:06:52 +0000281 Init *L = BO->getOperand()->convertInitializerTo(this);
David Greenee8f3b272009-05-14 21:22:49 +0000282 if (L == 0) return 0;
283 if (L != BO->getOperand())
Eric Christopher71520a82011-07-11 23:06:52 +0000284 return new UnOpInit(UnOpInit::CAST, L, new DagRecTy);
David Greenee8f3b272009-05-14 21:22:49 +0000285 return BO;
286 }
287 return 0;
288}
David Greene5d0c0512009-05-14 20:54:48 +0000289
Eric Christopher71520a82011-07-11 23:06:52 +0000290Init *DagRecTy::convertValue(BinOpInit *BO) {
Evan Chenga32dee22007-05-15 01:23:24 +0000291 if (BO->getOpcode() == BinOpInit::CONCAT) {
Eric Christopher71520a82011-07-11 23:06:52 +0000292 Init *L = BO->getLHS()->convertInitializerTo(this);
293 Init *R = BO->getRHS()->convertInitializerTo(this);
Evan Chenga32dee22007-05-15 01:23:24 +0000294 if (L == 0 || R == 0) return 0;
295 if (L != BO->getLHS() || R != BO->getRHS())
Eric Christopher71520a82011-07-11 23:06:52 +0000296 return new BinOpInit(BinOpInit::CONCAT, L, R, new DagRecTy);
Evan Chenga32dee22007-05-15 01:23:24 +0000297 return BO;
298 }
299 return 0;
300}
301
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000302RecordRecTy *RecordRecTy::get(Record *R) {
303 return &dynamic_cast<RecordRecTy&>(*R->getDefInit()->getType());
304}
305
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000306std::string RecordRecTy::getAsString() const {
307 return Rec->getName();
308}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000309
Eric Christopher71520a82011-07-11 23:06:52 +0000310Init *RecordRecTy::convertValue(DefInit *DI) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000311 // Ensure that DI is a subclass of Rec.
312 if (!DI->getDef()->isSubClassOf(Rec))
313 return 0;
314 return DI;
315}
316
Eric Christopher71520a82011-07-11 23:06:52 +0000317Init *RecordRecTy::convertValue(TypedInit *TI) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000318 // Ensure that TI is compatible with Rec.
319 if (RecordRecTy *RRT = dynamic_cast<RecordRecTy*>(TI->getType()))
320 if (RRT->getRecord()->isSubClassOf(getRecord()) ||
321 RRT->getRecord() == getRecord())
322 return TI;
323 return 0;
324}
325
326bool RecordRecTy::baseClassOf(const RecordRecTy *RHS) const {
Bruno Cardoso Lopesdeb20022010-06-17 23:00:16 +0000327 if (Rec == RHS->getRecord() || RHS->getRecord()->isSubClassOf(Rec))
328 return true;
329
330 const std::vector<Record*> &SC = Rec->getSuperClasses();
331 for (unsigned i = 0, e = SC.size(); i != e; ++i)
332 if (RHS->getRecord()->isSubClassOf(SC[i]))
333 return true;
334
335 return false;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000336}
337
338
Bob Wilson7248f862009-11-22 04:24:42 +0000339/// resolveTypes - Find a common type that T1 and T2 convert to.
David Greene8618f952009-06-08 20:23:18 +0000340/// Return 0 if no such type exists.
341///
342RecTy *llvm::resolveTypes(RecTy *T1, RecTy *T2) {
343 if (!T1->typeIsConvertibleTo(T2)) {
344 if (!T2->typeIsConvertibleTo(T1)) {
345 // If one is a Record type, check superclasses
346 RecordRecTy *RecTy1 = dynamic_cast<RecordRecTy*>(T1);
347 if (RecTy1) {
348 // See if T2 inherits from a type T1 also inherits from
Bob Wilson7248f862009-11-22 04:24:42 +0000349 const std::vector<Record *> &T1SuperClasses =
350 RecTy1->getRecord()->getSuperClasses();
David Greene8618f952009-06-08 20:23:18 +0000351 for(std::vector<Record *>::const_iterator i = T1SuperClasses.begin(),
352 iend = T1SuperClasses.end();
353 i != iend;
354 ++i) {
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000355 RecordRecTy *SuperRecTy1 = RecordRecTy::get(*i);
David Greene8618f952009-06-08 20:23:18 +0000356 RecTy *NewType1 = resolveTypes(SuperRecTy1, T2);
357 if (NewType1 != 0) {
358 if (NewType1 != SuperRecTy1) {
359 delete SuperRecTy1;
360 }
361 return NewType1;
362 }
363 }
364 }
365 RecordRecTy *RecTy2 = dynamic_cast<RecordRecTy*>(T2);
366 if (RecTy2) {
367 // See if T1 inherits from a type T2 also inherits from
Bob Wilson7248f862009-11-22 04:24:42 +0000368 const std::vector<Record *> &T2SuperClasses =
369 RecTy2->getRecord()->getSuperClasses();
370 for (std::vector<Record *>::const_iterator i = T2SuperClasses.begin(),
David Greene8618f952009-06-08 20:23:18 +0000371 iend = T2SuperClasses.end();
372 i != iend;
373 ++i) {
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000374 RecordRecTy *SuperRecTy2 = RecordRecTy::get(*i);
David Greene8618f952009-06-08 20:23:18 +0000375 RecTy *NewType2 = resolveTypes(T1, SuperRecTy2);
376 if (NewType2 != 0) {
377 if (NewType2 != SuperRecTy2) {
378 delete SuperRecTy2;
379 }
380 return NewType2;
381 }
382 }
383 }
384 return 0;
385 }
386 return T2;
387 }
388 return T1;
389}
390
391
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000392//===----------------------------------------------------------------------===//
393// Initializer implementations
394//===----------------------------------------------------------------------===//
395
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000396void Init::dump() const { return print(errs()); }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000397
Eric Christopher71520a82011-07-11 23:06:52 +0000398Init *BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
399 BitsInit *BI = new BitsInit(Bits.size());
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000400 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
401 if (Bits[i] >= getNumBits()) {
Eric Christopher71520a82011-07-11 23:06:52 +0000402 delete BI;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000403 return 0;
404 }
Eric Christopher71520a82011-07-11 23:06:52 +0000405 BI->setBit(i, getBit(Bits[i]));
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000406 }
Eric Christopher71520a82011-07-11 23:06:52 +0000407 return BI;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000408}
409
Chris Lattner695506c2007-11-22 21:05:25 +0000410std::string BitsInit::getAsString() const {
Chris Lattner695506c2007-11-22 21:05:25 +0000411 std::string Result = "{ ";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000412 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
Chris Lattner695506c2007-11-22 21:05:25 +0000413 if (i) Result += ", ";
Eric Christopher71520a82011-07-11 23:06:52 +0000414 if (Init *Bit = getBit(e-i-1))
Chris Lattner695506c2007-11-22 21:05:25 +0000415 Result += Bit->getAsString();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000416 else
Chris Lattner695506c2007-11-22 21:05:25 +0000417 Result += "*";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000418 }
Chris Lattner695506c2007-11-22 21:05:25 +0000419 return Result + " }";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000420}
421
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000422// resolveReferences - If there are any field references that refer to fields
423// that have been filled in, we can propagate the values now.
424//
Eric Christopher71520a82011-07-11 23:06:52 +0000425Init *BitsInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000426 bool Changed = false;
Eric Christopher71520a82011-07-11 23:06:52 +0000427 BitsInit *New = new BitsInit(getNumBits());
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000428
Eric Christopher71520a82011-07-11 23:06:52 +0000429 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
430 Init *B;
431 Init *CurBit = getBit(i);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000432
433 do {
434 B = CurBit;
Chris Lattneref943742005-04-19 03:36:21 +0000435 CurBit = CurBit->resolveReferences(R, RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000436 Changed |= B != CurBit;
437 } while (B != CurBit);
Eric Christopher71520a82011-07-11 23:06:52 +0000438 New->setBit(i, CurBit);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000439 }
440
441 if (Changed)
Eric Christopher71520a82011-07-11 23:06:52 +0000442 return New;
443 delete New;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000444 return this;
445}
446
Chris Lattner695506c2007-11-22 21:05:25 +0000447std::string IntInit::getAsString() const {
448 return itostr(Value);
449}
450
Eric Christopher71520a82011-07-11 23:06:52 +0000451Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
452 BitsInit *BI = new BitsInit(Bits.size());
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000453
454 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
Eric Christopher71520a82011-07-11 23:06:52 +0000455 if (Bits[i] >= 64) {
456 delete BI;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000457 return 0;
Eric Christopher71520a82011-07-11 23:06:52 +0000458 }
459 BI->setBit(i, new BitInit(Value & (INT64_C(1) << Bits[i])));
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000460 }
Eric Christopher71520a82011-07-11 23:06:52 +0000461 return BI;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000462}
463
Eric Christopher71520a82011-07-11 23:06:52 +0000464Init *ListInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
465 std::vector<Init*> Vals;
Chris Lattner8bf9e062004-07-26 23:21:34 +0000466 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
467 if (Elements[i] >= getSize())
468 return 0;
469 Vals.push_back(getElement(Elements[i]));
470 }
Eric Christopher71520a82011-07-11 23:06:52 +0000471 return new ListInit(Vals, getType());
Chris Lattner8bf9e062004-07-26 23:21:34 +0000472}
473
Chris Lattnercbebe462007-02-27 22:08:27 +0000474Record *ListInit::getElementAsRecord(unsigned i) const {
475 assert(i < Values.size() && "List element index out of range!");
Eric Christopher71520a82011-07-11 23:06:52 +0000476 DefInit *DI = dynamic_cast<DefInit*>(Values[i]);
Chris Lattnercbebe462007-02-27 22:08:27 +0000477 if (DI == 0) throw "Expected record in list!";
478 return DI->getDef();
479}
480
Eric Christopher71520a82011-07-11 23:06:52 +0000481Init *ListInit::resolveReferences(Record &R, const RecordVal *RV) {
482 std::vector<Init*> Resolved;
Chris Lattner577fc3f2004-07-27 01:01:21 +0000483 Resolved.reserve(getSize());
484 bool Changed = false;
485
486 for (unsigned i = 0, e = getSize(); i != e; ++i) {
Eric Christopher71520a82011-07-11 23:06:52 +0000487 Init *E;
488 Init *CurElt = getElement(i);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000489
490 do {
491 E = CurElt;
Chris Lattneref943742005-04-19 03:36:21 +0000492 CurElt = CurElt->resolveReferences(R, RV);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000493 Changed |= E != CurElt;
494 } while (E != CurElt);
495 Resolved.push_back(E);
496 }
497
498 if (Changed)
Eric Christopher71520a82011-07-11 23:06:52 +0000499 return new ListInit(Resolved, getType());
Chris Lattner577fc3f2004-07-27 01:01:21 +0000500 return this;
501}
502
Eric Christopher71520a82011-07-11 23:06:52 +0000503Init *ListInit::resolveListElementReference(Record &R, const RecordVal *IRV,
504 unsigned Elt) {
David Greene8618f952009-06-08 20:23:18 +0000505 if (Elt >= getSize())
506 return 0; // Out of range reference.
Eric Christopher71520a82011-07-11 23:06:52 +0000507 Init *E = getElement(Elt);
Bob Wilson67e6cab2009-11-22 03:58:57 +0000508 // If the element is set to some value, or if we are resolving a reference
509 // to a specific variable and that variable is explicitly unset, then
510 // replace the VarListElementInit with it.
Eric Christopher71520a82011-07-11 23:06:52 +0000511 if (IRV || !dynamic_cast<UnsetInit*>(E))
Bob Wilson67e6cab2009-11-22 03:58:57 +0000512 return E;
David Greene8618f952009-06-08 20:23:18 +0000513 return 0;
514}
515
Chris Lattner695506c2007-11-22 21:05:25 +0000516std::string ListInit::getAsString() const {
517 std::string Result = "[";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000518 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
Chris Lattner695506c2007-11-22 21:05:25 +0000519 if (i) Result += ", ";
520 Result += Values[i]->getAsString();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000521 }
Chris Lattner695506c2007-11-22 21:05:25 +0000522 return Result + "]";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000523}
524
Eric Christopher71520a82011-07-11 23:06:52 +0000525Init *OpInit::resolveBitReference(Record &R, const RecordVal *IRV,
526 unsigned Bit) {
527 Init *Folded = Fold(&R, 0);
David Greene5d0c0512009-05-14 20:54:48 +0000528
529 if (Folded != this) {
Eric Christopher71520a82011-07-11 23:06:52 +0000530 TypedInit *Typed = dynamic_cast<TypedInit *>(Folded);
David Greene5d0c0512009-05-14 20:54:48 +0000531 if (Typed) {
532 return Typed->resolveBitReference(R, IRV, Bit);
Bob Wilson7248f862009-11-22 04:24:42 +0000533 }
David Greene5d0c0512009-05-14 20:54:48 +0000534 }
Bob Wilson7248f862009-11-22 04:24:42 +0000535
David Greene5d0c0512009-05-14 20:54:48 +0000536 return 0;
537}
538
Eric Christopher71520a82011-07-11 23:06:52 +0000539Init *OpInit::resolveListElementReference(Record &R, const RecordVal *IRV,
540 unsigned Elt) {
541 Init *Folded = Fold(&R, 0);
David Greene5d0c0512009-05-14 20:54:48 +0000542
543 if (Folded != this) {
Eric Christopher71520a82011-07-11 23:06:52 +0000544 TypedInit *Typed = dynamic_cast<TypedInit *>(Folded);
David Greene5d0c0512009-05-14 20:54:48 +0000545 if (Typed) {
546 return Typed->resolveListElementReference(R, IRV, Elt);
Bob Wilson7248f862009-11-22 04:24:42 +0000547 }
David Greene5d0c0512009-05-14 20:54:48 +0000548 }
Bob Wilson7248f862009-11-22 04:24:42 +0000549
David Greene5d0c0512009-05-14 20:54:48 +0000550 return 0;
551}
552
Eric Christopher71520a82011-07-11 23:06:52 +0000553Init *UnOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
David Greenee8f3b272009-05-14 21:22:49 +0000554 switch (getOpcode()) {
555 default: assert(0 && "Unknown unop");
556 case CAST: {
David Greeneefa19612009-06-29 20:05:29 +0000557 if (getType()->getAsString() == "string") {
Eric Christopher71520a82011-07-11 23:06:52 +0000558 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
David Greeneefa19612009-06-29 20:05:29 +0000559 if (LHSs) {
560 return LHSs;
561 }
David Greene5d0c0512009-05-14 20:54:48 +0000562
Eric Christopher71520a82011-07-11 23:06:52 +0000563 DefInit *LHSd = dynamic_cast<DefInit*>(LHS);
David Greeneefa19612009-06-29 20:05:29 +0000564 if (LHSd) {
Eric Christopher71520a82011-07-11 23:06:52 +0000565 return new StringInit(LHSd->getDef()->getName());
David Greeneefa19612009-06-29 20:05:29 +0000566 }
Bob Wilson7248f862009-11-22 04:24:42 +0000567 } else {
Eric Christopher71520a82011-07-11 23:06:52 +0000568 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
David Greeneefa19612009-06-29 20:05:29 +0000569 if (LHSs) {
570 std::string Name = LHSs->getValue();
571
572 // From TGParser::ParseIDValue
573 if (CurRec) {
574 if (const RecordVal *RV = CurRec->getValue(Name)) {
Chris Lattner94026332010-10-06 00:19:21 +0000575 if (RV->getType() != getType())
576 throw "type mismatch in cast";
Eric Christopher71520a82011-07-11 23:06:52 +0000577 return new VarInit(Name, RV->getType());
David Greenee8f3b272009-05-14 21:22:49 +0000578 }
David Greeneefa19612009-06-29 20:05:29 +0000579
580 std::string TemplateArgName = CurRec->getName()+":"+Name;
581 if (CurRec->isTemplateArg(TemplateArgName)) {
582 const RecordVal *RV = CurRec->getValue(TemplateArgName);
583 assert(RV && "Template arg doesn't exist??");
584
Chris Lattner94026332010-10-06 00:19:21 +0000585 if (RV->getType() != getType())
586 throw "type mismatch in cast";
David Greeneefa19612009-06-29 20:05:29 +0000587
Eric Christopher71520a82011-07-11 23:06:52 +0000588 return new VarInit(TemplateArgName, RV->getType());
David Greeneefa19612009-06-29 20:05:29 +0000589 }
590 }
591
592 if (CurMultiClass) {
593 std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
594 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
595 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
596 assert(RV && "Template arg doesn't exist??");
Bob Wilson7248f862009-11-22 04:24:42 +0000597
Chris Lattner94026332010-10-06 00:19:21 +0000598 if (RV->getType() != getType())
599 throw "type mismatch in cast";
Bob Wilson7248f862009-11-22 04:24:42 +0000600
Eric Christopher71520a82011-07-11 23:06:52 +0000601 return new VarInit(MCName, RV->getType());
David Greeneefa19612009-06-29 20:05:29 +0000602 }
David Greenee8f3b272009-05-14 21:22:49 +0000603 }
Bob Wilson7248f862009-11-22 04:24:42 +0000604
Chris Lattner77d369c2010-12-13 00:23:57 +0000605 if (Record *D = (CurRec->getRecords()).getDef(Name))
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000606 return DefInit::get(D);
David Greene5d0c0512009-05-14 20:54:48 +0000607
Jim Grosbach59ddb732011-05-06 18:47:45 +0000608 throw TGError(CurRec->getLoc(), "Undefined reference:'" + Name + "'\n");
David Greenee8f3b272009-05-14 21:22:49 +0000609 }
David Greenee8f3b272009-05-14 21:22:49 +0000610 }
611 break;
612 }
David Greene2f7cf7f2011-01-07 17:05:37 +0000613 case HEAD: {
Eric Christopher71520a82011-07-11 23:06:52 +0000614 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
David Greened571b3c2009-05-14 22:38:31 +0000615 if (LHSl) {
616 if (LHSl->getSize() == 0) {
617 assert(0 && "Empty list in car");
618 return 0;
619 }
620 return LHSl->getElement(0);
621 }
622 break;
623 }
David Greene2f7cf7f2011-01-07 17:05:37 +0000624 case TAIL: {
Eric Christopher71520a82011-07-11 23:06:52 +0000625 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
David Greened571b3c2009-05-14 22:38:31 +0000626 if (LHSl) {
627 if (LHSl->getSize() == 0) {
628 assert(0 && "Empty list in cdr");
629 return 0;
630 }
Eric Christopher71520a82011-07-11 23:06:52 +0000631 ListInit *Result = new ListInit(LHSl->begin()+1, LHSl->end(),
632 LHSl->getType());
David Greened571b3c2009-05-14 22:38:31 +0000633 return Result;
634 }
635 break;
636 }
David Greene2f7cf7f2011-01-07 17:05:37 +0000637 case EMPTY: {
Eric Christopher71520a82011-07-11 23:06:52 +0000638 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
David Greened571b3c2009-05-14 22:38:31 +0000639 if (LHSl) {
640 if (LHSl->getSize() == 0) {
Eric Christopher71520a82011-07-11 23:06:52 +0000641 return new IntInit(1);
Bob Wilson7248f862009-11-22 04:24:42 +0000642 } else {
Eric Christopher71520a82011-07-11 23:06:52 +0000643 return new IntInit(0);
David Greened571b3c2009-05-14 22:38:31 +0000644 }
645 }
Eric Christopher71520a82011-07-11 23:06:52 +0000646 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
David Greene8618f952009-06-08 20:23:18 +0000647 if (LHSs) {
648 if (LHSs->getValue().empty()) {
Eric Christopher71520a82011-07-11 23:06:52 +0000649 return new IntInit(1);
Bob Wilson7248f862009-11-22 04:24:42 +0000650 } else {
Eric Christopher71520a82011-07-11 23:06:52 +0000651 return new IntInit(0);
David Greene8618f952009-06-08 20:23:18 +0000652 }
653 }
Bob Wilson7248f862009-11-22 04:24:42 +0000654
David Greened571b3c2009-05-14 22:38:31 +0000655 break;
656 }
David Greenee8f3b272009-05-14 21:22:49 +0000657 }
658 return this;
659}
David Greene5d0c0512009-05-14 20:54:48 +0000660
Eric Christopher71520a82011-07-11 23:06:52 +0000661Init *UnOpInit::resolveReferences(Record &R, const RecordVal *RV) {
662 Init *lhs = LHS->resolveReferences(R, RV);
Bob Wilson7248f862009-11-22 04:24:42 +0000663
David Greenee8f3b272009-05-14 21:22:49 +0000664 if (LHS != lhs)
Eric Christopher71520a82011-07-11 23:06:52 +0000665 return (new UnOpInit(getOpcode(), lhs, getType()))->Fold(&R, 0);
David Greenee8f3b272009-05-14 21:22:49 +0000666 return Fold(&R, 0);
667}
David Greene5d0c0512009-05-14 20:54:48 +0000668
David Greenee8f3b272009-05-14 21:22:49 +0000669std::string UnOpInit::getAsString() const {
670 std::string Result;
671 switch (Opc) {
672 case CAST: Result = "!cast<" + getType()->getAsString() + ">"; break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000673 case HEAD: Result = "!head"; break;
674 case TAIL: Result = "!tail"; break;
675 case EMPTY: Result = "!empty"; break;
David Greenee8f3b272009-05-14 21:22:49 +0000676 }
677 return Result + "(" + LHS->getAsString() + ")";
678}
David Greene5d0c0512009-05-14 20:54:48 +0000679
Eric Christopher71520a82011-07-11 23:06:52 +0000680Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
Chris Lattner51ffbf12006-03-31 21:53:49 +0000681 switch (getOpcode()) {
682 default: assert(0 && "Unknown binop");
Evan Chenga32dee22007-05-15 01:23:24 +0000683 case CONCAT: {
Eric Christopher71520a82011-07-11 23:06:52 +0000684 DagInit *LHSs = dynamic_cast<DagInit*>(LHS);
685 DagInit *RHSs = dynamic_cast<DagInit*>(RHS);
Evan Chenga32dee22007-05-15 01:23:24 +0000686 if (LHSs && RHSs) {
Eric Christopher71520a82011-07-11 23:06:52 +0000687 DefInit *LOp = dynamic_cast<DefInit*>(LHSs->getOperator());
688 DefInit *ROp = dynamic_cast<DefInit*>(RHSs->getOperator());
Chris Lattner81fd1f22010-03-18 21:07:51 +0000689 if (LOp == 0 || ROp == 0 || LOp->getDef() != ROp->getDef())
690 throw "Concated Dag operators do not match!";
Eric Christopher71520a82011-07-11 23:06:52 +0000691 std::vector<Init*> Args;
Evan Chenga32dee22007-05-15 01:23:24 +0000692 std::vector<std::string> ArgNames;
693 for (unsigned i = 0, e = LHSs->getNumArgs(); i != e; ++i) {
694 Args.push_back(LHSs->getArg(i));
695 ArgNames.push_back(LHSs->getArgName(i));
696 }
697 for (unsigned i = 0, e = RHSs->getNumArgs(); i != e; ++i) {
698 Args.push_back(RHSs->getArg(i));
699 ArgNames.push_back(RHSs->getArgName(i));
700 }
Eric Christopher71520a82011-07-11 23:06:52 +0000701 return new DagInit(LHSs->getOperator(), "", Args, ArgNames);
Evan Chenga32dee22007-05-15 01:23:24 +0000702 }
703 break;
704 }
Chris Lattner51ffbf12006-03-31 21:53:49 +0000705 case STRCONCAT: {
Eric Christopher71520a82011-07-11 23:06:52 +0000706 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
707 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
Chris Lattner51ffbf12006-03-31 21:53:49 +0000708 if (LHSs && RHSs)
Eric Christopher71520a82011-07-11 23:06:52 +0000709 return new StringInit(LHSs->getValue() + RHSs->getValue());
Chris Lattner51ffbf12006-03-31 21:53:49 +0000710 break;
711 }
David Greene297bfe62010-01-05 19:11:42 +0000712 case EQ: {
Bruno Cardoso Lopes77a4a562010-06-16 23:24:12 +0000713 // try to fold eq comparison for 'bit' and 'int', otherwise fallback
714 // to string objects.
Eric Christopher71520a82011-07-11 23:06:52 +0000715 IntInit* L =
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000716 dynamic_cast<IntInit*>(LHS->convertInitializerTo(IntRecTy::get()));
Eric Christopher71520a82011-07-11 23:06:52 +0000717 IntInit* R =
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000718 dynamic_cast<IntInit*>(RHS->convertInitializerTo(IntRecTy::get()));
Bruno Cardoso Lopes77a4a562010-06-16 23:24:12 +0000719
720 if (L && R)
Eric Christopher71520a82011-07-11 23:06:52 +0000721 return new IntInit(L->getValue() == R->getValue());
Bruno Cardoso Lopes77a4a562010-06-16 23:24:12 +0000722
Eric Christopher71520a82011-07-11 23:06:52 +0000723 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
724 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
Bruno Cardoso Lopes77a4a562010-06-16 23:24:12 +0000725
726 // Make sure we've resolved
David Greene297bfe62010-01-05 19:11:42 +0000727 if (LHSs && RHSs)
Eric Christopher71520a82011-07-11 23:06:52 +0000728 return new IntInit(LHSs->getValue() == RHSs->getValue());
David Greene297bfe62010-01-05 19:11:42 +0000729
730 break;
731 }
Chris Lattner51ffbf12006-03-31 21:53:49 +0000732 case SHL:
733 case SRA:
734 case SRL: {
Eric Christopher71520a82011-07-11 23:06:52 +0000735 IntInit *LHSi = dynamic_cast<IntInit*>(LHS);
736 IntInit *RHSi = dynamic_cast<IntInit*>(RHS);
Chris Lattner51ffbf12006-03-31 21:53:49 +0000737 if (LHSi && RHSi) {
Dan Gohmanca0546f2008-10-17 01:33:43 +0000738 int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
739 int64_t Result;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000740 switch (getOpcode()) {
741 default: assert(0 && "Bad opcode!");
742 case SHL: Result = LHSv << RHSv; break;
743 case SRA: Result = LHSv >> RHSv; break;
Dan Gohmanca0546f2008-10-17 01:33:43 +0000744 case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000745 }
Eric Christopher71520a82011-07-11 23:06:52 +0000746 return new IntInit(Result);
Chris Lattner51ffbf12006-03-31 21:53:49 +0000747 }
748 break;
749 }
750 }
751 return this;
752}
753
Eric Christopher71520a82011-07-11 23:06:52 +0000754Init *BinOpInit::resolveReferences(Record &R, const RecordVal *RV) {
755 Init *lhs = LHS->resolveReferences(R, RV);
756 Init *rhs = RHS->resolveReferences(R, RV);
Bob Wilson7248f862009-11-22 04:24:42 +0000757
Chris Lattner51ffbf12006-03-31 21:53:49 +0000758 if (LHS != lhs || RHS != rhs)
Eric Christopher71520a82011-07-11 23:06:52 +0000759 return (new BinOpInit(getOpcode(), lhs, rhs, getType()))->Fold(&R, 0);
David Greenea9c6c5d2009-04-22 20:18:10 +0000760 return Fold(&R, 0);
Chris Lattner51ffbf12006-03-31 21:53:49 +0000761}
762
Chris Lattner695506c2007-11-22 21:05:25 +0000763std::string BinOpInit::getAsString() const {
764 std::string Result;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000765 switch (Opc) {
Chris Lattner695506c2007-11-22 21:05:25 +0000766 case CONCAT: Result = "!con"; break;
767 case SHL: Result = "!shl"; break;
768 case SRA: Result = "!sra"; break;
769 case SRL: Result = "!srl"; break;
David Greene297bfe62010-01-05 19:11:42 +0000770 case EQ: Result = "!eq"; break;
Chris Lattner695506c2007-11-22 21:05:25 +0000771 case STRCONCAT: Result = "!strconcat"; break;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000772 }
Chris Lattner695506c2007-11-22 21:05:25 +0000773 return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")";
Chris Lattner51ffbf12006-03-31 21:53:49 +0000774}
775
Eric Christopher71520a82011-07-11 23:06:52 +0000776static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
777 Record *CurRec, MultiClass *CurMultiClass);
David Greenee917fff2009-05-14 22:23:47 +0000778
Eric Christopher71520a82011-07-11 23:06:52 +0000779static Init *EvaluateOperation(OpInit *RHSo, Init *LHS, Init *Arg,
780 RecTy *Type, Record *CurRec,
781 MultiClass *CurMultiClass) {
782 std::vector<Init *> NewOperands;
David Greenee917fff2009-05-14 22:23:47 +0000783
Eric Christopher71520a82011-07-11 23:06:52 +0000784 TypedInit *TArg = dynamic_cast<TypedInit*>(Arg);
David Greenee917fff2009-05-14 22:23:47 +0000785
786 // If this is a dag, recurse
787 if (TArg && TArg->getType()->getAsString() == "dag") {
Eric Christopher71520a82011-07-11 23:06:52 +0000788 Init *Result = ForeachHelper(LHS, Arg, RHSo, Type,
789 CurRec, CurMultiClass);
David Greenee917fff2009-05-14 22:23:47 +0000790 if (Result != 0) {
791 return Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000792 } else {
David Greenee917fff2009-05-14 22:23:47 +0000793 return 0;
794 }
795 }
796
797 for (int i = 0; i < RHSo->getNumOperands(); ++i) {
Eric Christopher71520a82011-07-11 23:06:52 +0000798 OpInit *RHSoo = dynamic_cast<OpInit*>(RHSo->getOperand(i));
David Greenee917fff2009-05-14 22:23:47 +0000799
800 if (RHSoo) {
Eric Christopher71520a82011-07-11 23:06:52 +0000801 Init *Result = EvaluateOperation(RHSoo, LHS, Arg,
802 Type, CurRec, CurMultiClass);
David Greenee917fff2009-05-14 22:23:47 +0000803 if (Result != 0) {
804 NewOperands.push_back(Result);
Bob Wilson7248f862009-11-22 04:24:42 +0000805 } else {
David Greenee917fff2009-05-14 22:23:47 +0000806 NewOperands.push_back(Arg);
807 }
Bob Wilson7248f862009-11-22 04:24:42 +0000808 } else if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
David Greenee917fff2009-05-14 22:23:47 +0000809 NewOperands.push_back(Arg);
Bob Wilson7248f862009-11-22 04:24:42 +0000810 } else {
David Greenee917fff2009-05-14 22:23:47 +0000811 NewOperands.push_back(RHSo->getOperand(i));
812 }
813 }
814
815 // Now run the operator and use its result as the new leaf
Eric Christopher71520a82011-07-11 23:06:52 +0000816 OpInit *NewOp = RHSo->clone(NewOperands);
817 Init *NewVal = NewOp->Fold(CurRec, CurMultiClass);
818 if (NewVal != NewOp) {
819 delete NewOp;
David Greenee917fff2009-05-14 22:23:47 +0000820 return NewVal;
821 }
822 return 0;
823}
824
Eric Christopher71520a82011-07-11 23:06:52 +0000825static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
826 Record *CurRec, MultiClass *CurMultiClass) {
827 DagInit *MHSd = dynamic_cast<DagInit*>(MHS);
828 ListInit *MHSl = dynamic_cast<ListInit*>(MHS);
David Greenee917fff2009-05-14 22:23:47 +0000829
830 DagRecTy *DagType = dynamic_cast<DagRecTy*>(Type);
831 ListRecTy *ListType = dynamic_cast<ListRecTy*>(Type);
832
Eric Christopher71520a82011-07-11 23:06:52 +0000833 OpInit *RHSo = dynamic_cast<OpInit*>(RHS);
David Greenee917fff2009-05-14 22:23:47 +0000834
835 if (!RHSo) {
Jim Grosbach59ddb732011-05-06 18:47:45 +0000836 throw TGError(CurRec->getLoc(), "!foreach requires an operator\n");
David Greenee917fff2009-05-14 22:23:47 +0000837 }
838
Eric Christopher71520a82011-07-11 23:06:52 +0000839 TypedInit *LHSt = dynamic_cast<TypedInit*>(LHS);
David Greenee917fff2009-05-14 22:23:47 +0000840
841 if (!LHSt) {
Jim Grosbach59ddb732011-05-06 18:47:45 +0000842 throw TGError(CurRec->getLoc(), "!foreach requires typed variable\n");
David Greenee917fff2009-05-14 22:23:47 +0000843 }
844
Nick Lewycky94298222009-05-15 03:07:14 +0000845 if ((MHSd && DagType) || (MHSl && ListType)) {
David Greenee917fff2009-05-14 22:23:47 +0000846 if (MHSd) {
Eric Christopher71520a82011-07-11 23:06:52 +0000847 Init *Val = MHSd->getOperator();
848 Init *Result = EvaluateOperation(RHSo, LHS, Val,
849 Type, CurRec, CurMultiClass);
David Greenee917fff2009-05-14 22:23:47 +0000850 if (Result != 0) {
851 Val = Result;
852 }
853
Eric Christopher71520a82011-07-11 23:06:52 +0000854 std::vector<std::pair<Init *, std::string> > args;
David Greenee917fff2009-05-14 22:23:47 +0000855 for (unsigned int i = 0; i < MHSd->getNumArgs(); ++i) {
Eric Christopher71520a82011-07-11 23:06:52 +0000856 Init *Arg;
David Greenee917fff2009-05-14 22:23:47 +0000857 std::string ArgName;
858 Arg = MHSd->getArg(i);
859 ArgName = MHSd->getArgName(i);
860
861 // Process args
Eric Christopher71520a82011-07-11 23:06:52 +0000862 Init *Result = EvaluateOperation(RHSo, LHS, Arg, Type,
863 CurRec, CurMultiClass);
David Greenee917fff2009-05-14 22:23:47 +0000864 if (Result != 0) {
865 Arg = Result;
866 }
867
868 // TODO: Process arg names
869 args.push_back(std::make_pair(Arg, ArgName));
870 }
871
Eric Christopher71520a82011-07-11 23:06:52 +0000872 return new DagInit(Val, "", args);
David Greenee917fff2009-05-14 22:23:47 +0000873 }
874 if (MHSl) {
Eric Christopher71520a82011-07-11 23:06:52 +0000875 std::vector<Init *> NewOperands;
876 std::vector<Init *> NewList(MHSl->begin(), MHSl->end());
David Greenee917fff2009-05-14 22:23:47 +0000877
Eric Christopher71520a82011-07-11 23:06:52 +0000878 for (ListInit::iterator li = NewList.begin(),
David Greenee917fff2009-05-14 22:23:47 +0000879 liend = NewList.end();
880 li != liend;
881 ++li) {
Eric Christopher71520a82011-07-11 23:06:52 +0000882 Init *Item = *li;
David Greenee917fff2009-05-14 22:23:47 +0000883 NewOperands.clear();
884 for(int i = 0; i < RHSo->getNumOperands(); ++i) {
885 // First, replace the foreach variable with the list item
886 if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
887 NewOperands.push_back(Item);
Bob Wilson7248f862009-11-22 04:24:42 +0000888 } else {
David Greenee917fff2009-05-14 22:23:47 +0000889 NewOperands.push_back(RHSo->getOperand(i));
890 }
891 }
892
893 // Now run the operator and use its result as the new list item
Eric Christopher71520a82011-07-11 23:06:52 +0000894 OpInit *NewOp = RHSo->clone(NewOperands);
895 Init *NewItem = NewOp->Fold(CurRec, CurMultiClass);
David Greenee917fff2009-05-14 22:23:47 +0000896 if (NewItem != NewOp) {
897 *li = NewItem;
Eric Christopher71520a82011-07-11 23:06:52 +0000898 delete NewOp;
David Greenee917fff2009-05-14 22:23:47 +0000899 }
900 }
Eric Christopher71520a82011-07-11 23:06:52 +0000901 return new ListInit(NewList, MHSl->getType());
David Greenee917fff2009-05-14 22:23:47 +0000902 }
903 }
904 return 0;
905}
906
Eric Christopher71520a82011-07-11 23:06:52 +0000907Init *TernOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
David Greene98ed3c72009-05-14 21:54:42 +0000908 switch (getOpcode()) {
909 default: assert(0 && "Unknown binop");
910 case SUBST: {
Eric Christopher71520a82011-07-11 23:06:52 +0000911 DefInit *LHSd = dynamic_cast<DefInit*>(LHS);
912 VarInit *LHSv = dynamic_cast<VarInit*>(LHS);
913 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
David Greene196ac3c2009-04-23 21:25:15 +0000914
Eric Christopher71520a82011-07-11 23:06:52 +0000915 DefInit *MHSd = dynamic_cast<DefInit*>(MHS);
916 VarInit *MHSv = dynamic_cast<VarInit*>(MHS);
917 StringInit *MHSs = dynamic_cast<StringInit*>(MHS);
David Greene5d0c0512009-05-14 20:54:48 +0000918
Eric Christopher71520a82011-07-11 23:06:52 +0000919 DefInit *RHSd = dynamic_cast<DefInit*>(RHS);
920 VarInit *RHSv = dynamic_cast<VarInit*>(RHS);
921 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
David Greene5d0c0512009-05-14 20:54:48 +0000922
David Greene98ed3c72009-05-14 21:54:42 +0000923 if ((LHSd && MHSd && RHSd)
924 || (LHSv && MHSv && RHSv)
925 || (LHSs && MHSs && RHSs)) {
926 if (RHSd) {
927 Record *Val = RHSd->getDef();
928 if (LHSd->getAsString() == RHSd->getAsString()) {
929 Val = MHSd->getDef();
930 }
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000931 return DefInit::get(Val);
David Greene98ed3c72009-05-14 21:54:42 +0000932 }
933 if (RHSv) {
934 std::string Val = RHSv->getName();
935 if (LHSv->getAsString() == RHSv->getAsString()) {
936 Val = MHSv->getName();
937 }
Eric Christopher71520a82011-07-11 23:06:52 +0000938 return new VarInit(Val, getType());
David Greene98ed3c72009-05-14 21:54:42 +0000939 }
940 if (RHSs) {
941 std::string Val = RHSs->getValue();
David Greene5d0c0512009-05-14 20:54:48 +0000942
David Greene98ed3c72009-05-14 21:54:42 +0000943 std::string::size_type found;
David Greenedbf70742009-12-21 21:21:34 +0000944 std::string::size_type idx = 0;
David Greene98ed3c72009-05-14 21:54:42 +0000945 do {
David Greenedbf70742009-12-21 21:21:34 +0000946 found = Val.find(LHSs->getValue(), idx);
David Greene98ed3c72009-05-14 21:54:42 +0000947 if (found != std::string::npos) {
948 Val.replace(found, LHSs->getValue().size(), MHSs->getValue());
949 }
David Greenedbf70742009-12-21 21:21:34 +0000950 idx = found + MHSs->getValue().size();
David Greene98ed3c72009-05-14 21:54:42 +0000951 } while (found != std::string::npos);
David Greene5d0c0512009-05-14 20:54:48 +0000952
Eric Christopher71520a82011-07-11 23:06:52 +0000953 return new StringInit(Val);
David Greene98ed3c72009-05-14 21:54:42 +0000954 }
955 }
956 break;
Bob Wilson7248f862009-11-22 04:24:42 +0000957 }
David Greene5d0c0512009-05-14 20:54:48 +0000958
David Greene98ed3c72009-05-14 21:54:42 +0000959 case FOREACH: {
Eric Christopher71520a82011-07-11 23:06:52 +0000960 Init *Result = ForeachHelper(LHS, MHS, RHS, getType(),
David Greenee917fff2009-05-14 22:23:47 +0000961 CurRec, CurMultiClass);
962 if (Result != 0) {
963 return Result;
David Greene98ed3c72009-05-14 21:54:42 +0000964 }
965 break;
966 }
David Greene3587eed2009-05-14 23:26:46 +0000967
968 case IF: {
Eric Christopher71520a82011-07-11 23:06:52 +0000969 IntInit *LHSi = dynamic_cast<IntInit*>(LHS);
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000970 if (Init *I = LHS->convertInitializerTo(IntRecTy::get()))
Eric Christopher71520a82011-07-11 23:06:52 +0000971 LHSi = dynamic_cast<IntInit*>(I);
David Greene3587eed2009-05-14 23:26:46 +0000972 if (LHSi) {
973 if (LHSi->getValue()) {
974 return MHS;
Bob Wilson7248f862009-11-22 04:24:42 +0000975 } else {
David Greene3587eed2009-05-14 23:26:46 +0000976 return RHS;
977 }
978 }
979 break;
980 }
David Greene98ed3c72009-05-14 21:54:42 +0000981 }
David Greene5d0c0512009-05-14 20:54:48 +0000982
David Greene98ed3c72009-05-14 21:54:42 +0000983 return this;
984}
David Greene5d0c0512009-05-14 20:54:48 +0000985
Eric Christopher71520a82011-07-11 23:06:52 +0000986Init *TernOpInit::resolveReferences(Record &R, const RecordVal *RV) {
987 Init *lhs = LHS->resolveReferences(R, RV);
David Greeneb0354452009-06-08 19:16:56 +0000988
989 if (Opc == IF && lhs != LHS) {
Eric Christopher71520a82011-07-11 23:06:52 +0000990 IntInit *Value = dynamic_cast<IntInit*>(lhs);
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000991 if (Init *I = lhs->convertInitializerTo(IntRecTy::get()))
Eric Christopher71520a82011-07-11 23:06:52 +0000992 Value = dynamic_cast<IntInit*>(I);
David Greeneb0354452009-06-08 19:16:56 +0000993 if (Value != 0) {
994 // Short-circuit
995 if (Value->getValue()) {
Eric Christopher71520a82011-07-11 23:06:52 +0000996 Init *mhs = MHS->resolveReferences(R, RV);
997 return (new TernOpInit(getOpcode(), lhs, mhs,
998 RHS, getType()))->Fold(&R, 0);
Bob Wilson7248f862009-11-22 04:24:42 +0000999 } else {
Eric Christopher71520a82011-07-11 23:06:52 +00001000 Init *rhs = RHS->resolveReferences(R, RV);
1001 return (new TernOpInit(getOpcode(), lhs, MHS,
1002 rhs, getType()))->Fold(&R, 0);
David Greeneb0354452009-06-08 19:16:56 +00001003 }
1004 }
1005 }
Bob Wilson7248f862009-11-22 04:24:42 +00001006
Eric Christopher71520a82011-07-11 23:06:52 +00001007 Init *mhs = MHS->resolveReferences(R, RV);
1008 Init *rhs = RHS->resolveReferences(R, RV);
David Greeneb0354452009-06-08 19:16:56 +00001009
David Greene98ed3c72009-05-14 21:54:42 +00001010 if (LHS != lhs || MHS != mhs || RHS != rhs)
Eric Christopher71520a82011-07-11 23:06:52 +00001011 return (new TernOpInit(getOpcode(), lhs, mhs, rhs, getType()))->Fold(&R, 0);
David Greene98ed3c72009-05-14 21:54:42 +00001012 return Fold(&R, 0);
1013}
David Greene196ac3c2009-04-23 21:25:15 +00001014
David Greene98ed3c72009-05-14 21:54:42 +00001015std::string TernOpInit::getAsString() const {
1016 std::string Result;
1017 switch (Opc) {
1018 case SUBST: Result = "!subst"; break;
Bob Wilson7248f862009-11-22 04:24:42 +00001019 case FOREACH: Result = "!foreach"; break;
1020 case IF: Result = "!if"; break;
David Greene98ed3c72009-05-14 21:54:42 +00001021 }
Bob Wilson7248f862009-11-22 04:24:42 +00001022 return Result + "(" + LHS->getAsString() + ", " + MHS->getAsString() + ", "
David Greene98ed3c72009-05-14 21:54:42 +00001023 + RHS->getAsString() + ")";
1024}
David Greene196ac3c2009-04-23 21:25:15 +00001025
David Greene2a9de4d2010-09-03 21:00:49 +00001026RecTy *TypedInit::getFieldType(const std::string &FieldName) const {
1027 RecordRecTy *RecordType = dynamic_cast<RecordRecTy *>(getType());
1028 if (RecordType) {
1029 RecordVal *Field = RecordType->getRecord()->getValue(FieldName);
1030 if (Field) {
1031 return Field->getType();
1032 }
1033 }
1034 return 0;
1035}
1036
Eric Christopher71520a82011-07-11 23:06:52 +00001037Init *TypedInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001038 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
Bill Wendling73a48d82010-12-10 22:54:30 +00001039 if (T == 0) return 0; // Cannot subscript a non-bits variable.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001040 unsigned NumBits = T->getNumBits();
1041
Eric Christopher71520a82011-07-11 23:06:52 +00001042 BitsInit *BI = new BitsInit(Bits.size());
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001043 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
1044 if (Bits[i] >= NumBits) {
Eric Christopher71520a82011-07-11 23:06:52 +00001045 delete BI;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001046 return 0;
1047 }
Eric Christopher71520a82011-07-11 23:06:52 +00001048 BI->setBit(i, new VarBitInit(this, Bits[i]));
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001049 }
Eric Christopher71520a82011-07-11 23:06:52 +00001050 return BI;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001051}
1052
Eric Christopher71520a82011-07-11 23:06:52 +00001053Init *TypedInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
Chris Lattner577fc3f2004-07-27 01:01:21 +00001054 ListRecTy *T = dynamic_cast<ListRecTy*>(getType());
Bill Wendling73a48d82010-12-10 22:54:30 +00001055 if (T == 0) return 0; // Cannot subscript a non-list variable.
Chris Lattner577fc3f2004-07-27 01:01:21 +00001056
1057 if (Elements.size() == 1)
Eric Christopher71520a82011-07-11 23:06:52 +00001058 return new VarListElementInit(this, Elements[0]);
Chris Lattner577fc3f2004-07-27 01:01:21 +00001059
Eric Christopher71520a82011-07-11 23:06:52 +00001060 std::vector<Init*> ListInits;
Chris Lattner577fc3f2004-07-27 01:01:21 +00001061 ListInits.reserve(Elements.size());
1062 for (unsigned i = 0, e = Elements.size(); i != e; ++i)
Eric Christopher71520a82011-07-11 23:06:52 +00001063 ListInits.push_back(new VarListElementInit(this, Elements[i]));
1064 return new ListInit(ListInits, T);
Chris Lattner577fc3f2004-07-27 01:01:21 +00001065}
1066
1067
Eric Christopher71520a82011-07-11 23:06:52 +00001068Init *VarInit::resolveBitReference(Record &R, const RecordVal *IRV,
1069 unsigned Bit) {
Chris Lattneref943742005-04-19 03:36:21 +00001070 if (R.isTemplateArg(getName())) return 0;
1071 if (IRV && IRV->getName() != getName()) return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001072
1073 RecordVal *RV = R.getValue(getName());
Bob Wilson159905a2009-11-21 22:44:20 +00001074 assert(RV && "Reference to a non-existent variable?");
Eric Christopher71520a82011-07-11 23:06:52 +00001075 assert(dynamic_cast<BitsInit*>(RV->getValue()));
1076 BitsInit *BI = (BitsInit*)RV->getValue();
Misha Brukman650ba8e2005-04-22 00:00:37 +00001077
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001078 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
Eric Christopher71520a82011-07-11 23:06:52 +00001079 Init *B = BI->getBit(Bit);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001080
Bob Wilson67e6cab2009-11-22 03:58:57 +00001081 // If the bit is set to some value, or if we are resolving a reference to a
1082 // specific variable and that variable is explicitly unset, then replace the
1083 // VarBitInit with it.
Eric Christopher71520a82011-07-11 23:06:52 +00001084 if (IRV || !dynamic_cast<UnsetInit*>(B))
Bob Wilson67e6cab2009-11-22 03:58:57 +00001085 return B;
Chris Lattner577fc3f2004-07-27 01:01:21 +00001086 return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001087}
1088
Eric Christopher71520a82011-07-11 23:06:52 +00001089Init *VarInit::resolveListElementReference(Record &R, const RecordVal *IRV,
1090 unsigned Elt) {
Chris Lattneref943742005-04-19 03:36:21 +00001091 if (R.isTemplateArg(getName())) return 0;
1092 if (IRV && IRV->getName() != getName()) return 0;
Chris Lattner577fc3f2004-07-27 01:01:21 +00001093
1094 RecordVal *RV = R.getValue(getName());
Bob Wilson159905a2009-11-21 22:44:20 +00001095 assert(RV && "Reference to a non-existent variable?");
Eric Christopher71520a82011-07-11 23:06:52 +00001096 ListInit *LI = dynamic_cast<ListInit*>(RV->getValue());
David Greene9d3febe2009-05-14 20:38:52 +00001097 if (!LI) {
Eric Christopher71520a82011-07-11 23:06:52 +00001098 VarInit *VI = dynamic_cast<VarInit*>(RV->getValue());
David Greene9d3febe2009-05-14 20:38:52 +00001099 assert(VI && "Invalid list element!");
Eric Christopher71520a82011-07-11 23:06:52 +00001100 return new VarListElementInit(VI, Elt);
David Greene9d3febe2009-05-14 20:38:52 +00001101 }
Bob Wilson7248f862009-11-22 04:24:42 +00001102
Chris Lattner577fc3f2004-07-27 01:01:21 +00001103 if (Elt >= LI->getSize())
1104 return 0; // Out of range reference.
Eric Christopher71520a82011-07-11 23:06:52 +00001105 Init *E = LI->getElement(Elt);
Bob Wilson67e6cab2009-11-22 03:58:57 +00001106 // If the element is set to some value, or if we are resolving a reference
1107 // to a specific variable and that variable is explicitly unset, then
1108 // replace the VarListElementInit with it.
Eric Christopher71520a82011-07-11 23:06:52 +00001109 if (IRV || !dynamic_cast<UnsetInit*>(E))
Bob Wilson67e6cab2009-11-22 03:58:57 +00001110 return E;
Chris Lattner577fc3f2004-07-27 01:01:21 +00001111 return 0;
1112}
1113
1114
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001115RecTy *VarInit::getFieldType(const std::string &FieldName) const {
1116 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
1117 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
1118 return RV->getType();
1119 return 0;
1120}
1121
Eric Christopher71520a82011-07-11 23:06:52 +00001122Init *VarInit::getFieldInit(Record &R, const RecordVal *RV,
1123 const std::string &FieldName) const {
Reid Spencer2a826862006-11-02 20:46:16 +00001124 if (dynamic_cast<RecordRecTy*>(getType()))
Jakob Stoklund Olesen0e457622010-03-25 06:23:34 +00001125 if (const RecordVal *Val = R.getValue(VarName)) {
Eric Christopher71520a82011-07-11 23:06:52 +00001126 if (RV != Val && (RV || dynamic_cast<UnsetInit*>(Val->getValue())))
Jakob Stoklund Olesen0e457622010-03-25 06:23:34 +00001127 return 0;
Eric Christopher71520a82011-07-11 23:06:52 +00001128 Init *TheInit = Val->getValue();
Chris Lattnerd959ab92004-02-28 16:31:53 +00001129 assert(TheInit != this && "Infinite loop detected!");
Eric Christopher71520a82011-07-11 23:06:52 +00001130 if (Init *I = TheInit->getFieldInit(R, RV, FieldName))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001131 return I;
1132 else
1133 return 0;
Chris Lattnerd959ab92004-02-28 16:31:53 +00001134 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001135 return 0;
1136}
1137
1138/// resolveReferences - This method is used by classes that refer to other
Bob Wilson159905a2009-11-21 22:44:20 +00001139/// variables which may not be defined at the time the expression is formed.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001140/// If a value is set for the variable later, this method will be called on
1141/// users of the value to allow the value to propagate out.
1142///
Eric Christopher71520a82011-07-11 23:06:52 +00001143Init *VarInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001144 if (RecordVal *Val = R.getValue(VarName))
Eric Christopher71520a82011-07-11 23:06:52 +00001145 if (RV == Val || (RV == 0 && !dynamic_cast<UnsetInit*>(Val->getValue())))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001146 return Val->getValue();
1147 return this;
1148}
Misha Brukman650ba8e2005-04-22 00:00:37 +00001149
Chris Lattner695506c2007-11-22 21:05:25 +00001150std::string VarBitInit::getAsString() const {
1151 return TI->getAsString() + "{" + utostr(Bit) + "}";
1152}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001153
Eric Christopher71520a82011-07-11 23:06:52 +00001154Init *VarBitInit::resolveReferences(Record &R, const RecordVal *RV) {
1155 if (Init *I = getVariable()->resolveBitReference(R, RV, getBitNum()))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001156 return I;
1157 return this;
1158}
1159
Chris Lattner695506c2007-11-22 21:05:25 +00001160std::string VarListElementInit::getAsString() const {
1161 return TI->getAsString() + "[" + utostr(Element) + "]";
1162}
1163
Eric Christopher71520a82011-07-11 23:06:52 +00001164Init *VarListElementInit::resolveReferences(Record &R, const RecordVal *RV) {
1165 if (Init *I = getVariable()->resolveListElementReference(R, RV,
1166 getElementNum()))
Chris Lattner577fc3f2004-07-27 01:01:21 +00001167 return I;
1168 return this;
1169}
1170
Eric Christopher71520a82011-07-11 23:06:52 +00001171Init *VarListElementInit::resolveBitReference(Record &R, const RecordVal *RV,
1172 unsigned Bit) {
Chris Lattner577fc3f2004-07-27 01:01:21 +00001173 // FIXME: This should be implemented, to support references like:
1174 // bit B = AA[0]{1};
1175 return 0;
1176}
1177
Eric Christopher71520a82011-07-11 23:06:52 +00001178Init *VarListElementInit::
1179resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) {
Chris Lattner577fc3f2004-07-27 01:01:21 +00001180 // FIXME: This should be implemented, to support references like:
1181 // int B = AA[0][1];
1182 return 0;
1183}
1184
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +00001185DefInit *DefInit::get(Record *R) {
1186 return R->getDefInit();
1187}
1188
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001189RecTy *DefInit::getFieldType(const std::string &FieldName) const {
1190 if (const RecordVal *RV = Def->getValue(FieldName))
1191 return RV->getType();
1192 return 0;
1193}
1194
Eric Christopher71520a82011-07-11 23:06:52 +00001195Init *DefInit::getFieldInit(Record &R, const RecordVal *RV,
1196 const std::string &FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001197 return Def->getValue(FieldName)->getValue();
1198}
1199
1200
Chris Lattner695506c2007-11-22 21:05:25 +00001201std::string DefInit::getAsString() const {
1202 return Def->getName();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001203}
1204
Eric Christopher71520a82011-07-11 23:06:52 +00001205Init *FieldInit::resolveBitReference(Record &R, const RecordVal *RV,
1206 unsigned Bit) {
1207 if (Init *BitsVal = Rec->getFieldInit(R, RV, FieldName))
1208 if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
David Greeneaf973b42011-07-11 18:25:51 +00001209 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
Eric Christopher71520a82011-07-11 23:06:52 +00001210 Init *B = BI->getBit(Bit);
David Greeneaf973b42011-07-11 18:25:51 +00001211
Eric Christopher71520a82011-07-11 23:06:52 +00001212 if (dynamic_cast<BitInit*>(B)) // If the bit is set.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001213 return B; // Replace the VarBitInit with it.
1214 }
Chris Lattner577fc3f2004-07-27 01:01:21 +00001215 return 0;
1216}
1217
Eric Christopher71520a82011-07-11 23:06:52 +00001218Init *FieldInit::resolveListElementReference(Record &R, const RecordVal *RV,
1219 unsigned Elt) {
1220 if (Init *ListVal = Rec->getFieldInit(R, RV, FieldName))
1221 if (ListInit *LI = dynamic_cast<ListInit*>(ListVal)) {
Chris Lattner577fc3f2004-07-27 01:01:21 +00001222 if (Elt >= LI->getSize()) return 0;
Eric Christopher71520a82011-07-11 23:06:52 +00001223 Init *E = LI->getElement(Elt);
Chris Lattner577fc3f2004-07-27 01:01:21 +00001224
Bob Wilson67e6cab2009-11-22 03:58:57 +00001225 // If the element is set to some value, or if we are resolving a
1226 // reference to a specific variable and that variable is explicitly
1227 // unset, then replace the VarListElementInit with it.
Eric Christopher71520a82011-07-11 23:06:52 +00001228 if (RV || !dynamic_cast<UnsetInit*>(E))
Bob Wilson67e6cab2009-11-22 03:58:57 +00001229 return E;
Chris Lattner577fc3f2004-07-27 01:01:21 +00001230 }
1231 return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001232}
1233
Eric Christopher71520a82011-07-11 23:06:52 +00001234Init *FieldInit::resolveReferences(Record &R, const RecordVal *RV) {
1235 Init *NewRec = RV ? Rec->resolveReferences(R, RV) : Rec;
Chris Lattneref943742005-04-19 03:36:21 +00001236
Eric Christopher71520a82011-07-11 23:06:52 +00001237 Init *BitsVal = NewRec->getFieldInit(R, RV, FieldName);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001238 if (BitsVal) {
Eric Christopher71520a82011-07-11 23:06:52 +00001239 Init *BVR = BitsVal->resolveReferences(R, RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001240 return BVR->isComplete() ? BVR : this;
1241 }
Chris Lattneref943742005-04-19 03:36:21 +00001242
1243 if (NewRec != Rec) {
Eric Christopher71520a82011-07-11 23:06:52 +00001244 return new FieldInit(NewRec, FieldName);
Chris Lattneref943742005-04-19 03:36:21 +00001245 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001246 return this;
1247}
1248
Eric Christopher71520a82011-07-11 23:06:52 +00001249Init *DagInit::resolveReferences(Record &R, const RecordVal *RV) {
1250 std::vector<Init*> NewArgs;
Chris Lattner0d3ef402006-01-31 06:02:35 +00001251 for (unsigned i = 0, e = Args.size(); i != e; ++i)
1252 NewArgs.push_back(Args[i]->resolveReferences(R, RV));
Bob Wilson7248f862009-11-22 04:24:42 +00001253
Eric Christopher71520a82011-07-11 23:06:52 +00001254 Init *Op = Val->resolveReferences(R, RV);
Bob Wilson7248f862009-11-22 04:24:42 +00001255
Chris Lattnerb59cf3c2006-03-30 22:50:40 +00001256 if (Args != NewArgs || Op != Val)
Eric Christopher71520a82011-07-11 23:06:52 +00001257 return new DagInit(Op, ValName, NewArgs, ArgNames);
Bob Wilson7248f862009-11-22 04:24:42 +00001258
Chris Lattner0d3ef402006-01-31 06:02:35 +00001259 return this;
1260}
1261
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001262
Chris Lattner695506c2007-11-22 21:05:25 +00001263std::string DagInit::getAsString() const {
1264 std::string Result = "(" + Val->getAsString();
Nate Begemandbe3f772009-03-19 05:21:56 +00001265 if (!ValName.empty())
1266 Result += ":" + ValName;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001267 if (Args.size()) {
Chris Lattner695506c2007-11-22 21:05:25 +00001268 Result += " " + Args[0]->getAsString();
1269 if (!ArgNames[0].empty()) Result += ":$" + ArgNames[0];
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001270 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
Chris Lattner695506c2007-11-22 21:05:25 +00001271 Result += ", " + Args[i]->getAsString();
1272 if (!ArgNames[i].empty()) Result += ":$" + ArgNames[i];
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001273 }
1274 }
Chris Lattner695506c2007-11-22 21:05:25 +00001275 return Result + ")";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001276}
1277
1278
1279//===----------------------------------------------------------------------===//
1280// Other implementations
1281//===----------------------------------------------------------------------===//
1282
1283RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
1284 : Name(N), Ty(T), Prefix(P) {
Eric Christopher71520a82011-07-11 23:06:52 +00001285 Value = Ty->convertValue(new UnsetInit());
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001286 assert(Value && "Cannot create unset value for current type!");
1287}
1288
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001289void RecordVal::dump() const { errs() << *this; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001290
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001291void RecordVal::print(raw_ostream &OS, bool PrintSem) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001292 if (getPrefix()) OS << "field ";
1293 OS << *getType() << " " << getName();
Chris Lattneref943742005-04-19 03:36:21 +00001294
1295 if (getValue())
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001296 OS << " = " << *getValue();
Chris Lattneref943742005-04-19 03:36:21 +00001297
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001298 if (PrintSem) OS << ";\n";
1299}
1300
Daniel Dunbarced00812009-08-23 09:47:37 +00001301unsigned Record::LastID = 0;
1302
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +00001303DefInit *Record::getDefInit() {
1304 if (!TheInit)
1305 TheInit = new DefInit(this, new RecordRecTy(this));
1306 return TheInit;
1307}
1308
Chris Lattnerac284252005-08-19 17:58:11 +00001309void Record::setName(const std::string &Name) {
Chris Lattner77d369c2010-12-13 00:23:57 +00001310 if (TrackedRecords.getDef(getName()) == this) {
1311 TrackedRecords.removeDef(getName());
Chris Lattnerac284252005-08-19 17:58:11 +00001312 this->Name = Name;
Chris Lattner77d369c2010-12-13 00:23:57 +00001313 TrackedRecords.addDef(this);
Chris Lattnerac284252005-08-19 17:58:11 +00001314 } else {
Chris Lattner77d369c2010-12-13 00:23:57 +00001315 TrackedRecords.removeClass(getName());
Chris Lattnerac284252005-08-19 17:58:11 +00001316 this->Name = Name;
Chris Lattner77d369c2010-12-13 00:23:57 +00001317 TrackedRecords.addClass(this);
Chris Lattnerac284252005-08-19 17:58:11 +00001318 }
1319}
1320
Chris Lattneref943742005-04-19 03:36:21 +00001321/// resolveReferencesTo - If anything in this record refers to RV, replace the
1322/// reference to RV with the RHS of RV. If RV is null, we resolve all possible
1323/// references.
1324void Record::resolveReferencesTo(const RecordVal *RV) {
1325 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
Eric Christopher71520a82011-07-11 23:06:52 +00001326 if (Init *V = Values[i].getValue())
Chris Lattneref943742005-04-19 03:36:21 +00001327 Values[i].setValue(V->resolveReferences(*this, RV));
1328 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001329}
1330
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001331void Record::dump() const { errs() << *this; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001332
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001333raw_ostream &llvm::operator<<(raw_ostream &OS, const Record &R) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001334 OS << R.getName();
1335
1336 const std::vector<std::string> &TArgs = R.getTemplateArgs();
1337 if (!TArgs.empty()) {
1338 OS << "<";
1339 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1340 if (i) OS << ", ";
1341 const RecordVal *RV = R.getValue(TArgs[i]);
1342 assert(RV && "Template argument record not found??");
1343 RV->print(OS, false);
1344 }
1345 OS << ">";
1346 }
1347
1348 OS << " {";
1349 const std::vector<Record*> &SC = R.getSuperClasses();
1350 if (!SC.empty()) {
1351 OS << "\t//";
1352 for (unsigned i = 0, e = SC.size(); i != e; ++i)
1353 OS << " " << SC[i]->getName();
1354 }
1355 OS << "\n";
1356
1357 const std::vector<RecordVal> &Vals = R.getValues();
1358 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
1359 if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
1360 OS << Vals[i];
1361 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
1362 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
1363 OS << Vals[i];
1364
1365 return OS << "}\n";
1366}
1367
1368/// getValueInit - Return the initializer for a value with the specified name,
1369/// or throw an exception if the field does not exist.
1370///
Eric Christopher71520a82011-07-11 23:06:52 +00001371Init *Record::getValueInit(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001372 const RecordVal *R = getValue(FieldName);
1373 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001374 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001375 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001376 return R->getValue();
1377}
1378
1379
1380/// getValueAsString - This method looks up the specified field and returns its
1381/// value as a string, throwing an exception if the field does not exist or if
1382/// the value is not a string.
1383///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001384std::string Record::getValueAsString(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001385 const RecordVal *R = getValue(FieldName);
1386 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001387 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001388 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001389
1390 if (const StringInit *SI = dynamic_cast<const StringInit*>(R->getValue()))
1391 return SI->getValue();
Chris Lattner42bb0c12009-09-18 18:31:37 +00001392 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001393 "' does not have a string initializer!";
1394}
1395
1396/// getValueAsBitsInit - This method looks up the specified field and returns
1397/// its value as a BitsInit, throwing an exception if the field does not exist
1398/// or if the value is not the right type.
1399///
Eric Christopher71520a82011-07-11 23:06:52 +00001400BitsInit *Record::getValueAsBitsInit(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001401 const RecordVal *R = getValue(FieldName);
1402 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001403 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001404 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001405
Eric Christopher71520a82011-07-11 23:06:52 +00001406 if (BitsInit *BI = dynamic_cast<BitsInit*>(R->getValue()))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001407 return BI;
Chris Lattner42bb0c12009-09-18 18:31:37 +00001408 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001409 "' does not have a BitsInit initializer!";
1410}
1411
1412/// getValueAsListInit - This method looks up the specified field and returns
1413/// its value as a ListInit, throwing an exception if the field does not exist
1414/// or if the value is not the right type.
1415///
Eric Christopher71520a82011-07-11 23:06:52 +00001416ListInit *Record::getValueAsListInit(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001417 const RecordVal *R = getValue(FieldName);
1418 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001419 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001420 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001421
Eric Christopher71520a82011-07-11 23:06:52 +00001422 if (ListInit *LI = dynamic_cast<ListInit*>(R->getValue()))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001423 return LI;
Chris Lattner42bb0c12009-09-18 18:31:37 +00001424 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001425 "' does not have a list initializer!";
1426}
1427
Chris Lattner7ad0bed2005-10-28 22:49:02 +00001428/// getValueAsListOfDefs - This method looks up the specified field and returns
Jim Laskeyb04feb62005-10-28 21:46:31 +00001429/// its value as a vector of records, throwing an exception if the field does
1430/// not exist or if the value is not the right type.
1431///
Bob Wilson7248f862009-11-22 04:24:42 +00001432std::vector<Record*>
Chris Lattner42bb0c12009-09-18 18:31:37 +00001433Record::getValueAsListOfDefs(StringRef FieldName) const {
Eric Christopher71520a82011-07-11 23:06:52 +00001434 ListInit *List = getValueAsListInit(FieldName);
Jim Laskeyb04feb62005-10-28 21:46:31 +00001435 std::vector<Record*> Defs;
1436 for (unsigned i = 0; i < List->getSize(); i++) {
Eric Christopher71520a82011-07-11 23:06:52 +00001437 if (DefInit *DI = dynamic_cast<DefInit*>(List->getElement(i))) {
Jim Laskeyb04feb62005-10-28 21:46:31 +00001438 Defs.push_back(DI->getDef());
1439 } else {
Chris Lattner42bb0c12009-09-18 18:31:37 +00001440 throw "Record `" + getName() + "', field `" + FieldName.str() +
Jim Laskeyb04feb62005-10-28 21:46:31 +00001441 "' list is not entirely DefInit!";
1442 }
1443 }
1444 return Defs;
1445}
1446
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001447/// getValueAsInt - This method looks up the specified field and returns its
Dan Gohmanca0546f2008-10-17 01:33:43 +00001448/// value as an int64_t, throwing an exception if the field does not exist or if
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001449/// the value is not the right type.
1450///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001451int64_t Record::getValueAsInt(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001452 const RecordVal *R = getValue(FieldName);
1453 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001454 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001455 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001456
Eric Christopher71520a82011-07-11 23:06:52 +00001457 if (IntInit *II = dynamic_cast<IntInit*>(R->getValue()))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001458 return II->getValue();
Chris Lattner42bb0c12009-09-18 18:31:37 +00001459 throw "Record `" + getName() + "', field `" + FieldName.str() +
Nate Begemanf621b332005-11-30 18:37:14 +00001460 "' does not have an int initializer!";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001461}
1462
Anton Korobeynikova468a112007-11-11 11:19:37 +00001463/// getValueAsListOfInts - This method looks up the specified field and returns
1464/// its value as a vector of integers, throwing an exception if the field does
1465/// not exist or if the value is not the right type.
1466///
Bob Wilson7248f862009-11-22 04:24:42 +00001467std::vector<int64_t>
Chris Lattner42bb0c12009-09-18 18:31:37 +00001468Record::getValueAsListOfInts(StringRef FieldName) const {
Eric Christopher71520a82011-07-11 23:06:52 +00001469 ListInit *List = getValueAsListInit(FieldName);
Dan Gohmanca0546f2008-10-17 01:33:43 +00001470 std::vector<int64_t> Ints;
Anton Korobeynikova468a112007-11-11 11:19:37 +00001471 for (unsigned i = 0; i < List->getSize(); i++) {
Eric Christopher71520a82011-07-11 23:06:52 +00001472 if (IntInit *II = dynamic_cast<IntInit*>(List->getElement(i))) {
Anton Korobeynikova468a112007-11-11 11:19:37 +00001473 Ints.push_back(II->getValue());
1474 } else {
Chris Lattner42bb0c12009-09-18 18:31:37 +00001475 throw "Record `" + getName() + "', field `" + FieldName.str() +
Anton Korobeynikova468a112007-11-11 11:19:37 +00001476 "' does not have a list of ints initializer!";
1477 }
1478 }
1479 return Ints;
1480}
1481
Owen Andersona84be6c2011-06-27 21:06:21 +00001482/// getValueAsListOfStrings - This method looks up the specified field and
1483/// returns its value as a vector of strings, throwing an exception if the
1484/// field does not exist or if the value is not the right type.
1485///
1486std::vector<std::string>
1487Record::getValueAsListOfStrings(StringRef FieldName) const {
Eric Christopher71520a82011-07-11 23:06:52 +00001488 ListInit *List = getValueAsListInit(FieldName);
Owen Andersona84be6c2011-06-27 21:06:21 +00001489 std::vector<std::string> Strings;
1490 for (unsigned i = 0; i < List->getSize(); i++) {
Eric Christopher71520a82011-07-11 23:06:52 +00001491 if (StringInit *II = dynamic_cast<StringInit*>(List->getElement(i))) {
Owen Andersona84be6c2011-06-27 21:06:21 +00001492 Strings.push_back(II->getValue());
1493 } else {
1494 throw "Record `" + getName() + "', field `" + FieldName.str() +
1495 "' does not have a list of strings initializer!";
1496 }
1497 }
1498 return Strings;
1499}
1500
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001501/// getValueAsDef - This method looks up the specified field and returns its
1502/// value as a Record, throwing an exception if the field does not exist or if
1503/// the value is not the right type.
1504///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001505Record *Record::getValueAsDef(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001506 const RecordVal *R = getValue(FieldName);
1507 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001508 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001509 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001510
Eric Christopher71520a82011-07-11 23:06:52 +00001511 if (DefInit *DI = dynamic_cast<DefInit*>(R->getValue()))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001512 return DI->getDef();
Chris Lattner42bb0c12009-09-18 18:31:37 +00001513 throw "Record `" + getName() + "', field `" + FieldName.str() +
Nate Begemanf621b332005-11-30 18:37:14 +00001514 "' does not have a def initializer!";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001515}
1516
1517/// getValueAsBit - This method looks up the specified field and returns its
1518/// value as a bit, throwing an exception if the field does not exist or if
1519/// the value is not the right type.
1520///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001521bool Record::getValueAsBit(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001522 const RecordVal *R = getValue(FieldName);
1523 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001524 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001525 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001526
Eric Christopher71520a82011-07-11 23:06:52 +00001527 if (BitInit *BI = dynamic_cast<BitInit*>(R->getValue()))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001528 return BI->getValue();
Chris Lattner42bb0c12009-09-18 18:31:37 +00001529 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001530 "' does not have a bit initializer!";
1531}
1532
1533/// getValueAsDag - This method looks up the specified field and returns its
1534/// value as an Dag, throwing an exception if the field does not exist or if
1535/// the value is not the right type.
1536///
Eric Christopher71520a82011-07-11 23:06:52 +00001537DagInit *Record::getValueAsDag(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001538 const RecordVal *R = getValue(FieldName);
1539 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001540 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001541 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001542
Eric Christopher71520a82011-07-11 23:06:52 +00001543 if (DagInit *DI = dynamic_cast<DagInit*>(R->getValue()))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001544 return DI;
Chris Lattner42bb0c12009-09-18 18:31:37 +00001545 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001546 "' does not have a dag initializer!";
1547}
1548
Chris Lattner42bb0c12009-09-18 18:31:37 +00001549std::string Record::getValueAsCode(StringRef FieldName) const {
Chris Lattnerae939eb2005-09-13 21:44:28 +00001550 const RecordVal *R = getValue(FieldName);
1551 if (R == 0 || R->getValue() == 0)
1552 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001553 FieldName.str() + "'!\n";
Bob Wilson7248f862009-11-22 04:24:42 +00001554
Chris Lattnerae939eb2005-09-13 21:44:28 +00001555 if (const CodeInit *CI = dynamic_cast<const CodeInit*>(R->getValue()))
1556 return CI->getValue();
Chris Lattner42bb0c12009-09-18 18:31:37 +00001557 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerae939eb2005-09-13 21:44:28 +00001558 "' does not have a code initializer!";
1559}
1560
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001561
David Greene7049e792009-04-24 16:55:41 +00001562void MultiClass::dump() const {
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001563 errs() << "Record:\n";
David Greene7049e792009-04-24 16:55:41 +00001564 Rec.dump();
Bob Wilson7248f862009-11-22 04:24:42 +00001565
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001566 errs() << "Defs:\n";
David Greene7049e792009-04-24 16:55:41 +00001567 for (RecordVector::const_iterator r = DefPrototypes.begin(),
1568 rend = DefPrototypes.end();
1569 r != rend;
1570 ++r) {
1571 (*r)->dump();
1572 }
1573}
1574
1575
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001576void RecordKeeper::dump() const { errs() << *this; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001577
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001578raw_ostream &llvm::operator<<(raw_ostream &OS, const RecordKeeper &RK) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001579 OS << "------------- Classes -----------------\n";
1580 const std::map<std::string, Record*> &Classes = RK.getClasses();
1581 for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001582 E = Classes.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001583 OS << "class " << *I->second;
Misha Brukman650ba8e2005-04-22 00:00:37 +00001584
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001585 OS << "------------- Defs -----------------\n";
1586 const std::map<std::string, Record*> &Defs = RK.getDefs();
1587 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001588 E = Defs.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001589 OS << "def " << *I->second;
1590 return OS;
1591}
1592
1593
1594/// getAllDerivedDefinitions - This method returns all concrete definitions
1595/// that derive from the specified class name. If a class with the specified
1596/// name does not exist, an error is printed and true is returned.
1597std::vector<Record*>
1598RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
Chris Lattner97049072010-12-13 00:20:52 +00001599 Record *Class = getClass(ClassName);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001600 if (!Class)
Misha Brukman41f9f292004-10-08 14:59:05 +00001601 throw "ERROR: Couldn't find the `" + ClassName + "' class!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001602
1603 std::vector<Record*> Defs;
1604 for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(),
1605 E = getDefs().end(); I != E; ++I)
1606 if (I->second->isSubClassOf(Class))
1607 Defs.push_back(I->second);
1608
1609 return Defs;
1610}
Brian Gaeke960707c2003-11-11 22:41:34 +00001611