blob: dc793586fbeef1ce30b93ff9d12c2b2eb4930c55 [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"
Chandler Carruth56869f22009-10-26 01:35:46 +000015#include "llvm/System/DataTypes.h"
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000016#include "llvm/Support/Format.h"
Chris Lattner8b9ecda2007-11-20 22:25:16 +000017#include "llvm/ADT/StringExtras.h"
Duraid Madina14492af2005-12-26 05:08:55 +000018
Chris Lattner68478662004-08-01 03:55:39 +000019using namespace llvm;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000020
21//===----------------------------------------------------------------------===//
22// Type implementations
23//===----------------------------------------------------------------------===//
24
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000025void RecTy::dump() const { print(errs()); }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000026
27Init *BitRecTy::convertValue(BitsInit *BI) {
28 if (BI->getNumBits() != 1) return 0; // Only accept if just one bit!
29 return BI->getBit(0);
30}
31
32bool BitRecTy::baseClassOf(const BitsRecTy *RHS) const {
33 return RHS->getNumBits() == 1;
34}
35
36Init *BitRecTy::convertValue(IntInit *II) {
Dan Gohmanca0546f2008-10-17 01:33:43 +000037 int64_t Val = II->getValue();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000038 if (Val != 0 && Val != 1) return 0; // Only accept 0 or 1 for a bit!
Misha Brukman650ba8e2005-04-22 00:00:37 +000039
40 return new BitInit(Val != 0);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000041}
42
43Init *BitRecTy::convertValue(TypedInit *VI) {
44 if (dynamic_cast<BitRecTy*>(VI->getType()))
45 return VI; // Accept variable if it is already of bit type!
46 return 0;
47}
48
Chris Lattner8b9ecda2007-11-20 22:25:16 +000049std::string BitsRecTy::getAsString() const {
50 return "bits<" + utostr(Size) + ">";
51}
52
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000053Init *BitsRecTy::convertValue(UnsetInit *UI) {
54 BitsInit *Ret = new BitsInit(Size);
55
56 for (unsigned i = 0; i != Size; ++i)
57 Ret->setBit(i, new UnsetInit());
58 return Ret;
59}
60
61Init *BitsRecTy::convertValue(BitInit *UI) {
62 if (Size != 1) return 0; // Can only convert single bit...
63 BitsInit *Ret = new BitsInit(1);
64 Ret->setBit(0, UI);
65 return Ret;
66}
67
68// convertValue from Int initializer to bits type: Split the integer up into the
69// appropriate bits...
70//
71Init *BitsRecTy::convertValue(IntInit *II) {
Misha Brukman6752fb52004-06-21 18:01:47 +000072 int64_t Value = II->getValue();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000073 // Make sure this bitfield is large enough to hold the integer value...
74 if (Value >= 0) {
Misha Brukman6752fb52004-06-21 18:01:47 +000075 if (Value & ~((1LL << Size)-1))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000076 return 0;
77 } else {
Jeff Cohen0add83e2006-02-18 03:20:33 +000078 if ((Value >> Size) != -1 || ((Value & (1LL << (Size-1))) == 0))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000079 return 0;
80 }
81
82 BitsInit *Ret = new BitsInit(Size);
83 for (unsigned i = 0; i != Size; ++i)
Jeff Cohen0add83e2006-02-18 03:20:33 +000084 Ret->setBit(i, new BitInit(Value & (1LL << i)));
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000085
86 return Ret;
87}
88
89Init *BitsRecTy::convertValue(BitsInit *BI) {
90 // If the number of bits is right, return it. Otherwise we need to expand or
91 // truncate...
92 if (BI->getNumBits() == Size) return BI;
93 return 0;
94}
95
96Init *BitsRecTy::convertValue(TypedInit *VI) {
97 if (BitsRecTy *BRT = dynamic_cast<BitsRecTy*>(VI->getType()))
98 if (BRT->Size == Size) {
99 BitsInit *Ret = new BitsInit(Size);
100 for (unsigned i = 0; i != Size; ++i)
Jeff Cohen88e7b722005-04-22 04:13:13 +0000101 Ret->setBit(i, new VarBitInit(VI, i));
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000102 return Ret;
103 }
104 if (Size == 1 && dynamic_cast<BitRecTy*>(VI->getType())) {
105 BitsInit *Ret = new BitsInit(1);
106 Ret->setBit(0, VI);
107 return Ret;
108 }
Misha Brukman650ba8e2005-04-22 00:00:37 +0000109
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000110 return 0;
111}
112
113Init *IntRecTy::convertValue(BitInit *BI) {
114 return new IntInit(BI->getValue());
115}
116
117Init *IntRecTy::convertValue(BitsInit *BI) {
Dan Gohmanca0546f2008-10-17 01:33:43 +0000118 int64_t Result = 0;
Misha Brukman650ba8e2005-04-22 00:00:37 +0000119 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000120 if (BitInit *Bit = dynamic_cast<BitInit*>(BI->getBit(i))) {
121 Result |= Bit->getValue() << i;
122 } else {
123 return 0;
124 }
125 return new IntInit(Result);
126}
127
128Init *IntRecTy::convertValue(TypedInit *TI) {
129 if (TI->getType()->typeIsConvertibleTo(this))
130 return TI; // Accept variable if already of the right type!
131 return 0;
132}
133
David Greenee8f3b272009-05-14 21:22:49 +0000134Init *StringRecTy::convertValue(UnOpInit *BO) {
135 if (BO->getOpcode() == UnOpInit::CAST) {
136 Init *L = BO->getOperand()->convertInitializerTo(this);
137 if (L == 0) return 0;
138 if (L != BO->getOperand())
139 return new UnOpInit(UnOpInit::CAST, L, new StringRecTy);
140 return BO;
141 }
David Greene5d0c0512009-05-14 20:54:48 +0000142
David Greenee8f3b272009-05-14 21:22:49 +0000143 return convertValue((TypedInit*)BO);
144}
David Greene5d0c0512009-05-14 20:54:48 +0000145
Chris Lattner51ffbf12006-03-31 21:53:49 +0000146Init *StringRecTy::convertValue(BinOpInit *BO) {
147 if (BO->getOpcode() == BinOpInit::STRCONCAT) {
148 Init *L = BO->getLHS()->convertInitializerTo(this);
149 Init *R = BO->getRHS()->convertInitializerTo(this);
150 if (L == 0 || R == 0) return 0;
151 if (L != BO->getLHS() || R != BO->getRHS())
David Greene196ac3c2009-04-23 21:25:15 +0000152 return new BinOpInit(BinOpInit::STRCONCAT, L, R, new StringRecTy);
Chris Lattner51ffbf12006-03-31 21:53:49 +0000153 return BO;
154 }
David Greene196ac3c2009-04-23 21:25:15 +0000155 if (BO->getOpcode() == BinOpInit::NAMECONCAT) {
156 if (BO->getType()->getAsString() == getAsString()) {
157 Init *L = BO->getLHS()->convertInitializerTo(this);
158 Init *R = BO->getRHS()->convertInitializerTo(this);
159 if (L == 0 || R == 0) return 0;
160 if (L != BO->getLHS() || R != BO->getRHS())
161 return new BinOpInit(BinOpInit::NAMECONCAT, L, R, new StringRecTy);
162 return BO;
163 }
164 }
165
166 return convertValue((TypedInit*)BO);
Chris Lattner51ffbf12006-03-31 21:53:49 +0000167}
168
169
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000170Init *StringRecTy::convertValue(TypedInit *TI) {
171 if (dynamic_cast<StringRecTy*>(TI->getType()))
172 return TI; // Accept variable if already of the right type!
173 return 0;
174}
175
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000176std::string ListRecTy::getAsString() const {
177 return "list<" + Ty->getAsString() + ">";
178}
179
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000180Init *ListRecTy::convertValue(ListInit *LI) {
181 std::vector<Init*> Elements;
182
183 // Verify that all of the elements of the list are subclasses of the
184 // appropriate class!
185 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
186 if (Init *CI = LI->getElement(i)->convertInitializerTo(Ty))
187 Elements.push_back(CI);
188 else
189 return 0;
190
David Greene8618f952009-06-08 20:23:18 +0000191 ListRecTy *LType = dynamic_cast<ListRecTy*>(LI->getType());
192 if (LType == 0) {
193 return 0;
194 }
195
196 return new ListInit(Elements, new ListRecTy(Ty));
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000197}
198
199Init *ListRecTy::convertValue(TypedInit *TI) {
200 // Ensure that TI is compatible with our class.
201 if (ListRecTy *LRT = dynamic_cast<ListRecTy*>(TI->getType()))
202 if (LRT->getElementType()->typeIsConvertibleTo(getElementType()))
203 return TI;
204 return 0;
205}
206
207Init *CodeRecTy::convertValue(TypedInit *TI) {
208 if (TI->getType()->typeIsConvertibleTo(this))
209 return TI;
210 return 0;
211}
212
213Init *DagRecTy::convertValue(TypedInit *TI) {
214 if (TI->getType()->typeIsConvertibleTo(this))
215 return TI;
216 return 0;
217}
218
David Greenee8f3b272009-05-14 21:22:49 +0000219Init *DagRecTy::convertValue(UnOpInit *BO) {
220 if (BO->getOpcode() == UnOpInit::CAST) {
221 Init *L = BO->getOperand()->convertInitializerTo(this);
222 if (L == 0) return 0;
223 if (L != BO->getOperand())
224 return new UnOpInit(UnOpInit::CAST, L, new DagRecTy);
225 return BO;
226 }
227 return 0;
228}
David Greene5d0c0512009-05-14 20:54:48 +0000229
Evan Chenga32dee22007-05-15 01:23:24 +0000230Init *DagRecTy::convertValue(BinOpInit *BO) {
231 if (BO->getOpcode() == BinOpInit::CONCAT) {
232 Init *L = BO->getLHS()->convertInitializerTo(this);
233 Init *R = BO->getRHS()->convertInitializerTo(this);
234 if (L == 0 || R == 0) return 0;
235 if (L != BO->getLHS() || R != BO->getRHS())
David Greene196ac3c2009-04-23 21:25:15 +0000236 return new BinOpInit(BinOpInit::CONCAT, L, R, new DagRecTy);
Evan Chenga32dee22007-05-15 01:23:24 +0000237 return BO;
238 }
David Greene196ac3c2009-04-23 21:25:15 +0000239 if (BO->getOpcode() == BinOpInit::NAMECONCAT) {
240 if (BO->getType()->getAsString() == getAsString()) {
241 Init *L = BO->getLHS()->convertInitializerTo(this);
242 Init *R = BO->getRHS()->convertInitializerTo(this);
243 if (L == 0 || R == 0) return 0;
244 if (L != BO->getLHS() || R != BO->getRHS())
245 return new BinOpInit(BinOpInit::CONCAT, L, R, new DagRecTy);
246 return BO;
247 }
248 }
Evan Chenga32dee22007-05-15 01:23:24 +0000249 return 0;
250}
251
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000252std::string RecordRecTy::getAsString() const {
253 return Rec->getName();
254}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000255
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000256Init *RecordRecTy::convertValue(DefInit *DI) {
257 // Ensure that DI is a subclass of Rec.
258 if (!DI->getDef()->isSubClassOf(Rec))
259 return 0;
260 return DI;
261}
262
263Init *RecordRecTy::convertValue(TypedInit *TI) {
264 // Ensure that TI is compatible with Rec.
265 if (RecordRecTy *RRT = dynamic_cast<RecordRecTy*>(TI->getType()))
266 if (RRT->getRecord()->isSubClassOf(getRecord()) ||
267 RRT->getRecord() == getRecord())
268 return TI;
269 return 0;
270}
271
272bool RecordRecTy::baseClassOf(const RecordRecTy *RHS) const {
Bruno Cardoso Lopesdeb20022010-06-17 23:00:16 +0000273 if (Rec == RHS->getRecord() || RHS->getRecord()->isSubClassOf(Rec))
274 return true;
275
276 const std::vector<Record*> &SC = Rec->getSuperClasses();
277 for (unsigned i = 0, e = SC.size(); i != e; ++i)
278 if (RHS->getRecord()->isSubClassOf(SC[i]))
279 return true;
280
281 return false;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000282}
283
284
Bob Wilson7248f862009-11-22 04:24:42 +0000285/// resolveTypes - Find a common type that T1 and T2 convert to.
David Greene8618f952009-06-08 20:23:18 +0000286/// Return 0 if no such type exists.
287///
288RecTy *llvm::resolveTypes(RecTy *T1, RecTy *T2) {
289 if (!T1->typeIsConvertibleTo(T2)) {
290 if (!T2->typeIsConvertibleTo(T1)) {
291 // If one is a Record type, check superclasses
292 RecordRecTy *RecTy1 = dynamic_cast<RecordRecTy*>(T1);
293 if (RecTy1) {
294 // See if T2 inherits from a type T1 also inherits from
Bob Wilson7248f862009-11-22 04:24:42 +0000295 const std::vector<Record *> &T1SuperClasses =
296 RecTy1->getRecord()->getSuperClasses();
David Greene8618f952009-06-08 20:23:18 +0000297 for(std::vector<Record *>::const_iterator i = T1SuperClasses.begin(),
298 iend = T1SuperClasses.end();
299 i != iend;
300 ++i) {
301 RecordRecTy *SuperRecTy1 = new RecordRecTy(*i);
302 RecTy *NewType1 = resolveTypes(SuperRecTy1, T2);
303 if (NewType1 != 0) {
304 if (NewType1 != SuperRecTy1) {
305 delete SuperRecTy1;
306 }
307 return NewType1;
308 }
309 }
310 }
311 RecordRecTy *RecTy2 = dynamic_cast<RecordRecTy*>(T2);
312 if (RecTy2) {
313 // See if T1 inherits from a type T2 also inherits from
Bob Wilson7248f862009-11-22 04:24:42 +0000314 const std::vector<Record *> &T2SuperClasses =
315 RecTy2->getRecord()->getSuperClasses();
316 for (std::vector<Record *>::const_iterator i = T2SuperClasses.begin(),
David Greene8618f952009-06-08 20:23:18 +0000317 iend = T2SuperClasses.end();
318 i != iend;
319 ++i) {
320 RecordRecTy *SuperRecTy2 = new RecordRecTy(*i);
321 RecTy *NewType2 = resolveTypes(T1, SuperRecTy2);
322 if (NewType2 != 0) {
323 if (NewType2 != SuperRecTy2) {
324 delete SuperRecTy2;
325 }
326 return NewType2;
327 }
328 }
329 }
330 return 0;
331 }
332 return T2;
333 }
334 return T1;
335}
336
337
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000338//===----------------------------------------------------------------------===//
339// Initializer implementations
340//===----------------------------------------------------------------------===//
341
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000342void Init::dump() const { return print(errs()); }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000343
344Init *BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
345 BitsInit *BI = new BitsInit(Bits.size());
346 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
347 if (Bits[i] >= getNumBits()) {
348 delete BI;
349 return 0;
350 }
351 BI->setBit(i, getBit(Bits[i]));
352 }
353 return BI;
354}
355
Chris Lattner695506c2007-11-22 21:05:25 +0000356std::string BitsInit::getAsString() const {
Chris Lattner695506c2007-11-22 21:05:25 +0000357 std::string Result = "{ ";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000358 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
Chris Lattner695506c2007-11-22 21:05:25 +0000359 if (i) Result += ", ";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000360 if (Init *Bit = getBit(e-i-1))
Chris Lattner695506c2007-11-22 21:05:25 +0000361 Result += Bit->getAsString();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000362 else
Chris Lattner695506c2007-11-22 21:05:25 +0000363 Result += "*";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000364 }
Chris Lattner695506c2007-11-22 21:05:25 +0000365 return Result + " }";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000366}
367
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000368// resolveReferences - If there are any field references that refer to fields
369// that have been filled in, we can propagate the values now.
370//
Chris Lattneref943742005-04-19 03:36:21 +0000371Init *BitsInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000372 bool Changed = false;
373 BitsInit *New = new BitsInit(getNumBits());
374
375 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
376 Init *B;
377 Init *CurBit = getBit(i);
378
379 do {
380 B = CurBit;
Chris Lattneref943742005-04-19 03:36:21 +0000381 CurBit = CurBit->resolveReferences(R, RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000382 Changed |= B != CurBit;
383 } while (B != CurBit);
384 New->setBit(i, CurBit);
385 }
386
387 if (Changed)
388 return New;
389 delete New;
390 return this;
391}
392
Chris Lattner695506c2007-11-22 21:05:25 +0000393std::string IntInit::getAsString() const {
394 return itostr(Value);
395}
396
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000397Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
398 BitsInit *BI = new BitsInit(Bits.size());
399
400 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
Dan Gohmanca0546f2008-10-17 01:33:43 +0000401 if (Bits[i] >= 64) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000402 delete BI;
403 return 0;
404 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000405 BI->setBit(i, new BitInit(Value & (INT64_C(1) << Bits[i])));
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000406 }
407 return BI;
408}
409
Chris Lattner8bf9e062004-07-26 23:21:34 +0000410Init *ListInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
411 std::vector<Init*> Vals;
412 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
413 if (Elements[i] >= getSize())
414 return 0;
415 Vals.push_back(getElement(Elements[i]));
416 }
David Greene8618f952009-06-08 20:23:18 +0000417 return new ListInit(Vals, getType());
Chris Lattner8bf9e062004-07-26 23:21:34 +0000418}
419
Chris Lattnercbebe462007-02-27 22:08:27 +0000420Record *ListInit::getElementAsRecord(unsigned i) const {
421 assert(i < Values.size() && "List element index out of range!");
422 DefInit *DI = dynamic_cast<DefInit*>(Values[i]);
423 if (DI == 0) throw "Expected record in list!";
424 return DI->getDef();
425}
426
Chris Lattneref943742005-04-19 03:36:21 +0000427Init *ListInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattner577fc3f2004-07-27 01:01:21 +0000428 std::vector<Init*> Resolved;
429 Resolved.reserve(getSize());
430 bool Changed = false;
431
432 for (unsigned i = 0, e = getSize(); i != e; ++i) {
433 Init *E;
434 Init *CurElt = getElement(i);
435
436 do {
437 E = CurElt;
Chris Lattneref943742005-04-19 03:36:21 +0000438 CurElt = CurElt->resolveReferences(R, RV);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000439 Changed |= E != CurElt;
440 } while (E != CurElt);
441 Resolved.push_back(E);
442 }
443
444 if (Changed)
David Greene8618f952009-06-08 20:23:18 +0000445 return new ListInit(Resolved, getType());
Chris Lattner577fc3f2004-07-27 01:01:21 +0000446 return this;
447}
448
David Greene8618f952009-06-08 20:23:18 +0000449Init *ListInit::resolveListElementReference(Record &R, const RecordVal *IRV,
Bob Wilson7248f862009-11-22 04:24:42 +0000450 unsigned Elt) {
David Greene8618f952009-06-08 20:23:18 +0000451 if (Elt >= getSize())
452 return 0; // Out of range reference.
453 Init *E = getElement(Elt);
Bob Wilson67e6cab2009-11-22 03:58:57 +0000454 // If the element is set to some value, or if we are resolving a reference
455 // to a specific variable and that variable is explicitly unset, then
456 // replace the VarListElementInit with it.
457 if (IRV || !dynamic_cast<UnsetInit*>(E))
458 return E;
David Greene8618f952009-06-08 20:23:18 +0000459 return 0;
460}
461
Chris Lattner695506c2007-11-22 21:05:25 +0000462std::string ListInit::getAsString() const {
463 std::string Result = "[";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000464 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
Chris Lattner695506c2007-11-22 21:05:25 +0000465 if (i) Result += ", ";
466 Result += Values[i]->getAsString();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000467 }
Chris Lattner695506c2007-11-22 21:05:25 +0000468 return Result + "]";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000469}
470
David Greene5d0c0512009-05-14 20:54:48 +0000471Init *OpInit::resolveBitReference(Record &R, const RecordVal *IRV,
Bob Wilson7248f862009-11-22 04:24:42 +0000472 unsigned Bit) {
David Greene5d0c0512009-05-14 20:54:48 +0000473 Init *Folded = Fold(&R, 0);
474
475 if (Folded != this) {
476 TypedInit *Typed = dynamic_cast<TypedInit *>(Folded);
477 if (Typed) {
478 return Typed->resolveBitReference(R, IRV, Bit);
Bob Wilson7248f862009-11-22 04:24:42 +0000479 }
David Greene5d0c0512009-05-14 20:54:48 +0000480 }
Bob Wilson7248f862009-11-22 04:24:42 +0000481
David Greene5d0c0512009-05-14 20:54:48 +0000482 return 0;
483}
484
485Init *OpInit::resolveListElementReference(Record &R, const RecordVal *IRV,
Bob Wilson7248f862009-11-22 04:24:42 +0000486 unsigned Elt) {
David Greene5d0c0512009-05-14 20:54:48 +0000487 Init *Folded = Fold(&R, 0);
488
489 if (Folded != this) {
490 TypedInit *Typed = dynamic_cast<TypedInit *>(Folded);
491 if (Typed) {
492 return Typed->resolveListElementReference(R, IRV, Elt);
Bob Wilson7248f862009-11-22 04:24:42 +0000493 }
David Greene5d0c0512009-05-14 20:54:48 +0000494 }
Bob Wilson7248f862009-11-22 04:24:42 +0000495
David Greene5d0c0512009-05-14 20:54:48 +0000496 return 0;
497}
498
David Greenee8f3b272009-05-14 21:22:49 +0000499Init *UnOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
500 switch (getOpcode()) {
501 default: assert(0 && "Unknown unop");
502 case CAST: {
David Greeneefa19612009-06-29 20:05:29 +0000503 if (getType()->getAsString() == "string") {
504 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
505 if (LHSs) {
506 return LHSs;
507 }
David Greene5d0c0512009-05-14 20:54:48 +0000508
David Greeneefa19612009-06-29 20:05:29 +0000509 DefInit *LHSd = dynamic_cast<DefInit*>(LHS);
510 if (LHSd) {
511 return new StringInit(LHSd->getDef()->getName());
512 }
Bob Wilson7248f862009-11-22 04:24:42 +0000513 } else {
David Greeneefa19612009-06-29 20:05:29 +0000514 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
515 if (LHSs) {
516 std::string Name = LHSs->getValue();
517
518 // From TGParser::ParseIDValue
519 if (CurRec) {
520 if (const RecordVal *RV = CurRec->getValue(Name)) {
521 if (RV->getType() != getType()) {
522 throw "type mismatch in nameconcat";
523 }
524 return new VarInit(Name, RV->getType());
David Greenee8f3b272009-05-14 21:22:49 +0000525 }
David Greeneefa19612009-06-29 20:05:29 +0000526
527 std::string TemplateArgName = CurRec->getName()+":"+Name;
528 if (CurRec->isTemplateArg(TemplateArgName)) {
529 const RecordVal *RV = CurRec->getValue(TemplateArgName);
530 assert(RV && "Template arg doesn't exist??");
531
532 if (RV->getType() != getType()) {
533 throw "type mismatch in nameconcat";
534 }
535
536 return new VarInit(TemplateArgName, RV->getType());
537 }
538 }
539
540 if (CurMultiClass) {
541 std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
542 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
543 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
544 assert(RV && "Template arg doesn't exist??");
Bob Wilson7248f862009-11-22 04:24:42 +0000545
David Greeneefa19612009-06-29 20:05:29 +0000546 if (RV->getType() != getType()) {
547 throw "type mismatch in nameconcat";
548 }
Bob Wilson7248f862009-11-22 04:24:42 +0000549
David Greeneefa19612009-06-29 20:05:29 +0000550 return new VarInit(MCName, RV->getType());
551 }
David Greenee8f3b272009-05-14 21:22:49 +0000552 }
Bob Wilson7248f862009-11-22 04:24:42 +0000553
David Greeneefa19612009-06-29 20:05:29 +0000554 if (Record *D = Records.getDef(Name))
555 return new DefInit(D);
David Greene5d0c0512009-05-14 20:54:48 +0000556
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000557 errs() << "Variable not defined: '" + Name + "'\n";
David Greeneefa19612009-06-29 20:05:29 +0000558 assert(0 && "Variable not found");
559 return 0;
David Greenee8f3b272009-05-14 21:22:49 +0000560 }
David Greenee8f3b272009-05-14 21:22:49 +0000561 }
562 break;
563 }
David Greened571b3c2009-05-14 22:38:31 +0000564 case CAR: {
565 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
566 if (LHSl) {
567 if (LHSl->getSize() == 0) {
568 assert(0 && "Empty list in car");
569 return 0;
570 }
571 return LHSl->getElement(0);
572 }
573 break;
574 }
575 case CDR: {
576 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
577 if (LHSl) {
578 if (LHSl->getSize() == 0) {
579 assert(0 && "Empty list in cdr");
580 return 0;
581 }
Bob Wilson7248f862009-11-22 04:24:42 +0000582 ListInit *Result = new ListInit(LHSl->begin()+1, LHSl->end(),
583 LHSl->getType());
David Greened571b3c2009-05-14 22:38:31 +0000584 return Result;
585 }
586 break;
587 }
588 case LNULL: {
589 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
590 if (LHSl) {
591 if (LHSl->getSize() == 0) {
592 return new IntInit(1);
Bob Wilson7248f862009-11-22 04:24:42 +0000593 } else {
David Greened571b3c2009-05-14 22:38:31 +0000594 return new IntInit(0);
595 }
596 }
David Greene8618f952009-06-08 20:23:18 +0000597 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
598 if (LHSs) {
599 if (LHSs->getValue().empty()) {
600 return new IntInit(1);
Bob Wilson7248f862009-11-22 04:24:42 +0000601 } else {
David Greene8618f952009-06-08 20:23:18 +0000602 return new IntInit(0);
603 }
604 }
Bob Wilson7248f862009-11-22 04:24:42 +0000605
David Greened571b3c2009-05-14 22:38:31 +0000606 break;
607 }
David Greenee8f3b272009-05-14 21:22:49 +0000608 }
609 return this;
610}
David Greene5d0c0512009-05-14 20:54:48 +0000611
David Greenee8f3b272009-05-14 21:22:49 +0000612Init *UnOpInit::resolveReferences(Record &R, const RecordVal *RV) {
613 Init *lhs = LHS->resolveReferences(R, RV);
Bob Wilson7248f862009-11-22 04:24:42 +0000614
David Greenee8f3b272009-05-14 21:22:49 +0000615 if (LHS != lhs)
616 return (new UnOpInit(getOpcode(), lhs, getType()))->Fold(&R, 0);
617 return Fold(&R, 0);
618}
David Greene5d0c0512009-05-14 20:54:48 +0000619
David Greenee8f3b272009-05-14 21:22:49 +0000620std::string UnOpInit::getAsString() const {
621 std::string Result;
622 switch (Opc) {
623 case CAST: Result = "!cast<" + getType()->getAsString() + ">"; break;
David Greened571b3c2009-05-14 22:38:31 +0000624 case CAR: Result = "!car"; break;
625 case CDR: Result = "!cdr"; break;
626 case LNULL: Result = "!null"; break;
David Greenee8f3b272009-05-14 21:22:49 +0000627 }
628 return Result + "(" + LHS->getAsString() + ")";
629}
David Greene5d0c0512009-05-14 20:54:48 +0000630
David Greenea9c6c5d2009-04-22 20:18:10 +0000631Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
Chris Lattner51ffbf12006-03-31 21:53:49 +0000632 switch (getOpcode()) {
633 default: assert(0 && "Unknown binop");
Evan Chenga32dee22007-05-15 01:23:24 +0000634 case CONCAT: {
635 DagInit *LHSs = dynamic_cast<DagInit*>(LHS);
636 DagInit *RHSs = dynamic_cast<DagInit*>(RHS);
637 if (LHSs && RHSs) {
638 DefInit *LOp = dynamic_cast<DefInit*>(LHSs->getOperator());
639 DefInit *ROp = dynamic_cast<DefInit*>(RHSs->getOperator());
Chris Lattner81fd1f22010-03-18 21:07:51 +0000640 if (LOp == 0 || ROp == 0 || LOp->getDef() != ROp->getDef())
641 throw "Concated Dag operators do not match!";
Evan Chenga32dee22007-05-15 01:23:24 +0000642 std::vector<Init*> Args;
643 std::vector<std::string> ArgNames;
644 for (unsigned i = 0, e = LHSs->getNumArgs(); i != e; ++i) {
645 Args.push_back(LHSs->getArg(i));
646 ArgNames.push_back(LHSs->getArgName(i));
647 }
648 for (unsigned i = 0, e = RHSs->getNumArgs(); i != e; ++i) {
649 Args.push_back(RHSs->getArg(i));
650 ArgNames.push_back(RHSs->getArgName(i));
651 }
Nate Begemandbe3f772009-03-19 05:21:56 +0000652 return new DagInit(LHSs->getOperator(), "", Args, ArgNames);
Evan Chenga32dee22007-05-15 01:23:24 +0000653 }
654 break;
655 }
Chris Lattner51ffbf12006-03-31 21:53:49 +0000656 case STRCONCAT: {
657 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
658 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
659 if (LHSs && RHSs)
660 return new StringInit(LHSs->getValue() + RHSs->getValue());
661 break;
662 }
David Greenea9c6c5d2009-04-22 20:18:10 +0000663 case NAMECONCAT: {
664 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
665 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
666 if (LHSs && RHSs) {
667 std::string Name(LHSs->getValue() + RHSs->getValue());
668
669 // From TGParser::ParseIDValue
670 if (CurRec) {
David Greene196ac3c2009-04-23 21:25:15 +0000671 if (const RecordVal *RV = CurRec->getValue(Name)) {
672 if (RV->getType() != getType()) {
673 throw "type mismatch in nameconcat";
674 }
David Greenea9c6c5d2009-04-22 20:18:10 +0000675 return new VarInit(Name, RV->getType());
David Greene196ac3c2009-04-23 21:25:15 +0000676 }
Bob Wilson7248f862009-11-22 04:24:42 +0000677
David Greenea9c6c5d2009-04-22 20:18:10 +0000678 std::string TemplateArgName = CurRec->getName()+":"+Name;
679 if (CurRec->isTemplateArg(TemplateArgName)) {
680 const RecordVal *RV = CurRec->getValue(TemplateArgName);
681 assert(RV && "Template arg doesn't exist??");
David Greene196ac3c2009-04-23 21:25:15 +0000682
683 if (RV->getType() != getType()) {
684 throw "type mismatch in nameconcat";
685 }
686
David Greenea9c6c5d2009-04-22 20:18:10 +0000687 return new VarInit(TemplateArgName, RV->getType());
688 }
689 }
690
691 if (CurMultiClass) {
692 std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
693 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
694 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
695 assert(RV && "Template arg doesn't exist??");
David Greene196ac3c2009-04-23 21:25:15 +0000696
697 if (RV->getType() != getType()) {
698 throw "type mismatch in nameconcat";
699 }
Bob Wilson7248f862009-11-22 04:24:42 +0000700
David Greenea9c6c5d2009-04-22 20:18:10 +0000701 return new VarInit(MCName, RV->getType());
702 }
703 }
704
705 if (Record *D = Records.getDef(Name))
706 return new DefInit(D);
707
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000708 errs() << "Variable not defined in !nameconcat: '" + Name + "'\n";
David Greene8618f952009-06-08 20:23:18 +0000709 assert(0 && "Variable not found in !nameconcat");
David Greenea9c6c5d2009-04-22 20:18:10 +0000710 return 0;
711 }
712 break;
713 }
David Greene297bfe62010-01-05 19:11:42 +0000714 case EQ: {
Bruno Cardoso Lopes77a4a562010-06-16 23:24:12 +0000715 // try to fold eq comparison for 'bit' and 'int', otherwise fallback
716 // to string objects.
717 IntInit* L =
718 dynamic_cast<IntInit*>(LHS->convertInitializerTo(new IntRecTy()));
719 IntInit* R =
720 dynamic_cast<IntInit*>(RHS->convertInitializerTo(new IntRecTy()));
721
722 if (L && R)
723 return new IntInit(L->getValue() == R->getValue());
724
David Greene297bfe62010-01-05 19:11:42 +0000725 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
726 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
Bruno Cardoso Lopes77a4a562010-06-16 23:24:12 +0000727
728 // Make sure we've resolved
David Greene297bfe62010-01-05 19:11:42 +0000729 if (LHSs && RHSs)
730 return new IntInit(LHSs->getValue() == RHSs->getValue());
731
732 break;
733 }
Chris Lattner51ffbf12006-03-31 21:53:49 +0000734 case SHL:
735 case SRA:
736 case SRL: {
737 IntInit *LHSi = dynamic_cast<IntInit*>(LHS);
738 IntInit *RHSi = dynamic_cast<IntInit*>(RHS);
739 if (LHSi && RHSi) {
Dan Gohmanca0546f2008-10-17 01:33:43 +0000740 int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
741 int64_t Result;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000742 switch (getOpcode()) {
743 default: assert(0 && "Bad opcode!");
744 case SHL: Result = LHSv << RHSv; break;
745 case SRA: Result = LHSv >> RHSv; break;
Dan Gohmanca0546f2008-10-17 01:33:43 +0000746 case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000747 }
748 return new IntInit(Result);
749 }
750 break;
751 }
752 }
753 return this;
754}
755
756Init *BinOpInit::resolveReferences(Record &R, const RecordVal *RV) {
757 Init *lhs = LHS->resolveReferences(R, RV);
758 Init *rhs = RHS->resolveReferences(R, RV);
Bob Wilson7248f862009-11-22 04:24:42 +0000759
Chris Lattner51ffbf12006-03-31 21:53:49 +0000760 if (LHS != lhs || RHS != rhs)
David Greene196ac3c2009-04-23 21:25:15 +0000761 return (new BinOpInit(getOpcode(), lhs, rhs, getType()))->Fold(&R, 0);
David Greenea9c6c5d2009-04-22 20:18:10 +0000762 return Fold(&R, 0);
Chris Lattner51ffbf12006-03-31 21:53:49 +0000763}
764
Chris Lattner695506c2007-11-22 21:05:25 +0000765std::string BinOpInit::getAsString() const {
766 std::string Result;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000767 switch (Opc) {
Chris Lattner695506c2007-11-22 21:05:25 +0000768 case CONCAT: Result = "!con"; break;
769 case SHL: Result = "!shl"; break;
770 case SRA: Result = "!sra"; break;
771 case SRL: Result = "!srl"; break;
David Greene297bfe62010-01-05 19:11:42 +0000772 case EQ: Result = "!eq"; break;
Chris Lattner695506c2007-11-22 21:05:25 +0000773 case STRCONCAT: Result = "!strconcat"; break;
Bob Wilson7248f862009-11-22 04:24:42 +0000774 case NAMECONCAT:
David Greene196ac3c2009-04-23 21:25:15 +0000775 Result = "!nameconcat<" + getType()->getAsString() + ">"; break;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000776 }
Chris Lattner695506c2007-11-22 21:05:25 +0000777 return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")";
Chris Lattner51ffbf12006-03-31 21:53:49 +0000778}
779
David Greenee917fff2009-05-14 22:23:47 +0000780static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
781 Record *CurRec, MultiClass *CurMultiClass);
782
783static Init *EvaluateOperation(OpInit *RHSo, Init *LHS, Init *Arg,
784 RecTy *Type, Record *CurRec,
785 MultiClass *CurMultiClass) {
786 std::vector<Init *> NewOperands;
787
788 TypedInit *TArg = dynamic_cast<TypedInit*>(Arg);
789
790 // If this is a dag, recurse
791 if (TArg && TArg->getType()->getAsString() == "dag") {
792 Init *Result = ForeachHelper(LHS, Arg, RHSo, Type,
793 CurRec, CurMultiClass);
794 if (Result != 0) {
795 return Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000796 } else {
David Greenee917fff2009-05-14 22:23:47 +0000797 return 0;
798 }
799 }
800
801 for (int i = 0; i < RHSo->getNumOperands(); ++i) {
802 OpInit *RHSoo = dynamic_cast<OpInit*>(RHSo->getOperand(i));
803
804 if (RHSoo) {
805 Init *Result = EvaluateOperation(RHSoo, LHS, Arg,
806 Type, CurRec, CurMultiClass);
807 if (Result != 0) {
808 NewOperands.push_back(Result);
Bob Wilson7248f862009-11-22 04:24:42 +0000809 } else {
David Greenee917fff2009-05-14 22:23:47 +0000810 NewOperands.push_back(Arg);
811 }
Bob Wilson7248f862009-11-22 04:24:42 +0000812 } else if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
David Greenee917fff2009-05-14 22:23:47 +0000813 NewOperands.push_back(Arg);
Bob Wilson7248f862009-11-22 04:24:42 +0000814 } else {
David Greenee917fff2009-05-14 22:23:47 +0000815 NewOperands.push_back(RHSo->getOperand(i));
816 }
817 }
818
819 // Now run the operator and use its result as the new leaf
820 OpInit *NewOp = RHSo->clone(NewOperands);
821 Init *NewVal = NewOp->Fold(CurRec, CurMultiClass);
822 if (NewVal != NewOp) {
823 delete NewOp;
824 return NewVal;
825 }
826 return 0;
827}
828
829static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
830 Record *CurRec, MultiClass *CurMultiClass) {
831 DagInit *MHSd = dynamic_cast<DagInit*>(MHS);
832 ListInit *MHSl = dynamic_cast<ListInit*>(MHS);
833
834 DagRecTy *DagType = dynamic_cast<DagRecTy*>(Type);
835 ListRecTy *ListType = dynamic_cast<ListRecTy*>(Type);
836
837 OpInit *RHSo = dynamic_cast<OpInit*>(RHS);
838
839 if (!RHSo) {
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000840 errs() << "!foreach requires an operator\n";
David Greenee917fff2009-05-14 22:23:47 +0000841 assert(0 && "No operator for !foreach");
842 }
843
844 TypedInit *LHSt = dynamic_cast<TypedInit*>(LHS);
845
846 if (!LHSt) {
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000847 errs() << "!foreach requires typed variable\n";
David Greenee917fff2009-05-14 22:23:47 +0000848 assert(0 && "No typed variable for !foreach");
849 }
850
Nick Lewycky94298222009-05-15 03:07:14 +0000851 if ((MHSd && DagType) || (MHSl && ListType)) {
David Greenee917fff2009-05-14 22:23:47 +0000852 if (MHSd) {
853 Init *Val = MHSd->getOperator();
854 Init *Result = EvaluateOperation(RHSo, LHS, Val,
855 Type, CurRec, CurMultiClass);
856 if (Result != 0) {
857 Val = Result;
858 }
859
860 std::vector<std::pair<Init *, std::string> > args;
861 for (unsigned int i = 0; i < MHSd->getNumArgs(); ++i) {
862 Init *Arg;
863 std::string ArgName;
864 Arg = MHSd->getArg(i);
865 ArgName = MHSd->getArgName(i);
866
867 // Process args
868 Init *Result = EvaluateOperation(RHSo, LHS, Arg, Type,
869 CurRec, CurMultiClass);
870 if (Result != 0) {
871 Arg = Result;
872 }
873
874 // TODO: Process arg names
875 args.push_back(std::make_pair(Arg, ArgName));
876 }
877
878 return new DagInit(Val, "", args);
879 }
880 if (MHSl) {
881 std::vector<Init *> NewOperands;
882 std::vector<Init *> NewList(MHSl->begin(), MHSl->end());
883
884 for (ListInit::iterator li = NewList.begin(),
885 liend = NewList.end();
886 li != liend;
887 ++li) {
888 Init *Item = *li;
889 NewOperands.clear();
890 for(int i = 0; i < RHSo->getNumOperands(); ++i) {
891 // First, replace the foreach variable with the list item
892 if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
893 NewOperands.push_back(Item);
Bob Wilson7248f862009-11-22 04:24:42 +0000894 } else {
David Greenee917fff2009-05-14 22:23:47 +0000895 NewOperands.push_back(RHSo->getOperand(i));
896 }
897 }
898
899 // Now run the operator and use its result as the new list item
900 OpInit *NewOp = RHSo->clone(NewOperands);
901 Init *NewItem = NewOp->Fold(CurRec, CurMultiClass);
902 if (NewItem != NewOp) {
903 *li = NewItem;
904 delete NewOp;
905 }
906 }
David Greene8618f952009-06-08 20:23:18 +0000907 return new ListInit(NewList, MHSl->getType());
David Greenee917fff2009-05-14 22:23:47 +0000908 }
909 }
910 return 0;
911}
912
David Greene98ed3c72009-05-14 21:54:42 +0000913Init *TernOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
914 switch (getOpcode()) {
915 default: assert(0 && "Unknown binop");
916 case SUBST: {
917 DefInit *LHSd = dynamic_cast<DefInit*>(LHS);
918 VarInit *LHSv = dynamic_cast<VarInit*>(LHS);
919 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
David Greene196ac3c2009-04-23 21:25:15 +0000920
David Greene98ed3c72009-05-14 21:54:42 +0000921 DefInit *MHSd = dynamic_cast<DefInit*>(MHS);
922 VarInit *MHSv = dynamic_cast<VarInit*>(MHS);
923 StringInit *MHSs = dynamic_cast<StringInit*>(MHS);
David Greene5d0c0512009-05-14 20:54:48 +0000924
David Greene98ed3c72009-05-14 21:54:42 +0000925 DefInit *RHSd = dynamic_cast<DefInit*>(RHS);
926 VarInit *RHSv = dynamic_cast<VarInit*>(RHS);
927 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
David Greene5d0c0512009-05-14 20:54:48 +0000928
David Greene98ed3c72009-05-14 21:54:42 +0000929 if ((LHSd && MHSd && RHSd)
930 || (LHSv && MHSv && RHSv)
931 || (LHSs && MHSs && RHSs)) {
932 if (RHSd) {
933 Record *Val = RHSd->getDef();
934 if (LHSd->getAsString() == RHSd->getAsString()) {
935 Val = MHSd->getDef();
936 }
937 return new DefInit(Val);
938 }
939 if (RHSv) {
940 std::string Val = RHSv->getName();
941 if (LHSv->getAsString() == RHSv->getAsString()) {
942 Val = MHSv->getName();
943 }
944 return new VarInit(Val, getType());
945 }
946 if (RHSs) {
947 std::string Val = RHSs->getValue();
David Greene5d0c0512009-05-14 20:54:48 +0000948
David Greene98ed3c72009-05-14 21:54:42 +0000949 std::string::size_type found;
David Greenedbf70742009-12-21 21:21:34 +0000950 std::string::size_type idx = 0;
David Greene98ed3c72009-05-14 21:54:42 +0000951 do {
David Greenedbf70742009-12-21 21:21:34 +0000952 found = Val.find(LHSs->getValue(), idx);
David Greene98ed3c72009-05-14 21:54:42 +0000953 if (found != std::string::npos) {
954 Val.replace(found, LHSs->getValue().size(), MHSs->getValue());
955 }
David Greenedbf70742009-12-21 21:21:34 +0000956 idx = found + MHSs->getValue().size();
David Greene98ed3c72009-05-14 21:54:42 +0000957 } while (found != std::string::npos);
David Greene5d0c0512009-05-14 20:54:48 +0000958
David Greene98ed3c72009-05-14 21:54:42 +0000959 return new StringInit(Val);
960 }
961 }
962 break;
Bob Wilson7248f862009-11-22 04:24:42 +0000963 }
David Greene5d0c0512009-05-14 20:54:48 +0000964
David Greene98ed3c72009-05-14 21:54:42 +0000965 case FOREACH: {
David Greenee917fff2009-05-14 22:23:47 +0000966 Init *Result = ForeachHelper(LHS, MHS, RHS, getType(),
967 CurRec, CurMultiClass);
968 if (Result != 0) {
969 return Result;
David Greene98ed3c72009-05-14 21:54:42 +0000970 }
971 break;
972 }
David Greene3587eed2009-05-14 23:26:46 +0000973
974 case IF: {
Bruno Cardoso Lopes7f4235d2010-06-17 01:50:39 +0000975 IntInit *LHSi = dynamic_cast<IntInit*>(LHS);
976 if (Init *I = LHS->convertInitializerTo(new IntRecTy()))
977 LHSi = dynamic_cast<IntInit*>(I);
David Greene3587eed2009-05-14 23:26:46 +0000978 if (LHSi) {
979 if (LHSi->getValue()) {
980 return MHS;
Bob Wilson7248f862009-11-22 04:24:42 +0000981 } else {
David Greene3587eed2009-05-14 23:26:46 +0000982 return RHS;
983 }
984 }
985 break;
986 }
David Greene98ed3c72009-05-14 21:54:42 +0000987 }
David Greene5d0c0512009-05-14 20:54:48 +0000988
David Greene98ed3c72009-05-14 21:54:42 +0000989 return this;
990}
David Greene5d0c0512009-05-14 20:54:48 +0000991
David Greene98ed3c72009-05-14 21:54:42 +0000992Init *TernOpInit::resolveReferences(Record &R, const RecordVal *RV) {
993 Init *lhs = LHS->resolveReferences(R, RV);
David Greeneb0354452009-06-08 19:16:56 +0000994
995 if (Opc == IF && lhs != LHS) {
Bruno Cardoso Lopes7f4235d2010-06-17 01:50:39 +0000996 IntInit *Value = dynamic_cast<IntInit*>(lhs);
997 if (Init *I = lhs->convertInitializerTo(new IntRecTy()))
998 Value = dynamic_cast<IntInit*>(I);
David Greeneb0354452009-06-08 19:16:56 +0000999 if (Value != 0) {
1000 // Short-circuit
1001 if (Value->getValue()) {
1002 Init *mhs = MHS->resolveReferences(R, RV);
Bob Wilson7248f862009-11-22 04:24:42 +00001003 return (new TernOpInit(getOpcode(), lhs, mhs,
1004 RHS, getType()))->Fold(&R, 0);
1005 } else {
David Greeneb0354452009-06-08 19:16:56 +00001006 Init *rhs = RHS->resolveReferences(R, RV);
Bob Wilson7248f862009-11-22 04:24:42 +00001007 return (new TernOpInit(getOpcode(), lhs, MHS,
1008 rhs, getType()))->Fold(&R, 0);
David Greeneb0354452009-06-08 19:16:56 +00001009 }
1010 }
1011 }
Bob Wilson7248f862009-11-22 04:24:42 +00001012
David Greene98ed3c72009-05-14 21:54:42 +00001013 Init *mhs = MHS->resolveReferences(R, RV);
1014 Init *rhs = RHS->resolveReferences(R, RV);
David Greeneb0354452009-06-08 19:16:56 +00001015
David Greene98ed3c72009-05-14 21:54:42 +00001016 if (LHS != lhs || MHS != mhs || RHS != rhs)
1017 return (new TernOpInit(getOpcode(), lhs, mhs, rhs, getType()))->Fold(&R, 0);
1018 return Fold(&R, 0);
1019}
David Greene196ac3c2009-04-23 21:25:15 +00001020
David Greene98ed3c72009-05-14 21:54:42 +00001021std::string TernOpInit::getAsString() const {
1022 std::string Result;
1023 switch (Opc) {
1024 case SUBST: Result = "!subst"; break;
Bob Wilson7248f862009-11-22 04:24:42 +00001025 case FOREACH: Result = "!foreach"; break;
1026 case IF: Result = "!if"; break;
David Greene98ed3c72009-05-14 21:54:42 +00001027 }
Bob Wilson7248f862009-11-22 04:24:42 +00001028 return Result + "(" + LHS->getAsString() + ", " + MHS->getAsString() + ", "
David Greene98ed3c72009-05-14 21:54:42 +00001029 + RHS->getAsString() + ")";
1030}
David Greene196ac3c2009-04-23 21:25:15 +00001031
David Greene2a9de4d2010-09-03 21:00:49 +00001032RecTy *TypedInit::getFieldType(const std::string &FieldName) const {
1033 RecordRecTy *RecordType = dynamic_cast<RecordRecTy *>(getType());
1034 if (RecordType) {
1035 RecordVal *Field = RecordType->getRecord()->getValue(FieldName);
1036 if (Field) {
1037 return Field->getType();
1038 }
1039 }
1040 return 0;
1041}
1042
Chris Lattner577fc3f2004-07-27 01:01:21 +00001043Init *TypedInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001044 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
1045 if (T == 0) return 0; // Cannot subscript a non-bits variable...
1046 unsigned NumBits = T->getNumBits();
1047
1048 BitsInit *BI = new BitsInit(Bits.size());
1049 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
1050 if (Bits[i] >= NumBits) {
1051 delete BI;
1052 return 0;
1053 }
1054 BI->setBit(i, new VarBitInit(this, Bits[i]));
1055 }
1056 return BI;
1057}
1058
Chris Lattner577fc3f2004-07-27 01:01:21 +00001059Init *TypedInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
1060 ListRecTy *T = dynamic_cast<ListRecTy*>(getType());
1061 if (T == 0) return 0; // Cannot subscript a non-list variable...
1062
1063 if (Elements.size() == 1)
1064 return new VarListElementInit(this, Elements[0]);
1065
1066 std::vector<Init*> ListInits;
1067 ListInits.reserve(Elements.size());
1068 for (unsigned i = 0, e = Elements.size(); i != e; ++i)
1069 ListInits.push_back(new VarListElementInit(this, Elements[i]));
David Greene8618f952009-06-08 20:23:18 +00001070 return new ListInit(ListInits, T);
Chris Lattner577fc3f2004-07-27 01:01:21 +00001071}
1072
1073
Chris Lattneref943742005-04-19 03:36:21 +00001074Init *VarInit::resolveBitReference(Record &R, const RecordVal *IRV,
1075 unsigned Bit) {
1076 if (R.isTemplateArg(getName())) return 0;
1077 if (IRV && IRV->getName() != getName()) return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001078
1079 RecordVal *RV = R.getValue(getName());
Bob Wilson159905a2009-11-21 22:44:20 +00001080 assert(RV && "Reference to a non-existent variable?");
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001081 assert(dynamic_cast<BitsInit*>(RV->getValue()));
1082 BitsInit *BI = (BitsInit*)RV->getValue();
Misha Brukman650ba8e2005-04-22 00:00:37 +00001083
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001084 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
1085 Init *B = BI->getBit(Bit);
1086
Bob Wilson67e6cab2009-11-22 03:58:57 +00001087 // If the bit is set to some value, or if we are resolving a reference to a
1088 // specific variable and that variable is explicitly unset, then replace the
1089 // VarBitInit with it.
1090 if (IRV || !dynamic_cast<UnsetInit*>(B))
1091 return B;
Chris Lattner577fc3f2004-07-27 01:01:21 +00001092 return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001093}
1094
Chris Lattneref943742005-04-19 03:36:21 +00001095Init *VarInit::resolveListElementReference(Record &R, const RecordVal *IRV,
1096 unsigned Elt) {
1097 if (R.isTemplateArg(getName())) return 0;
1098 if (IRV && IRV->getName() != getName()) return 0;
Chris Lattner577fc3f2004-07-27 01:01:21 +00001099
1100 RecordVal *RV = R.getValue(getName());
Bob Wilson159905a2009-11-21 22:44:20 +00001101 assert(RV && "Reference to a non-existent variable?");
Chris Lattner577fc3f2004-07-27 01:01:21 +00001102 ListInit *LI = dynamic_cast<ListInit*>(RV->getValue());
David Greene9d3febe2009-05-14 20:38:52 +00001103 if (!LI) {
1104 VarInit *VI = dynamic_cast<VarInit*>(RV->getValue());
1105 assert(VI && "Invalid list element!");
1106 return new VarListElementInit(VI, Elt);
1107 }
Bob Wilson7248f862009-11-22 04:24:42 +00001108
Chris Lattner577fc3f2004-07-27 01:01:21 +00001109 if (Elt >= LI->getSize())
1110 return 0; // Out of range reference.
1111 Init *E = LI->getElement(Elt);
Bob Wilson67e6cab2009-11-22 03:58:57 +00001112 // If the element is set to some value, or if we are resolving a reference
1113 // to a specific variable and that variable is explicitly unset, then
1114 // replace the VarListElementInit with it.
1115 if (IRV || !dynamic_cast<UnsetInit*>(E))
1116 return E;
Chris Lattner577fc3f2004-07-27 01:01:21 +00001117 return 0;
1118}
1119
1120
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001121RecTy *VarInit::getFieldType(const std::string &FieldName) const {
1122 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
1123 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
1124 return RV->getType();
1125 return 0;
1126}
1127
Jakob Stoklund Olesen0e457622010-03-25 06:23:34 +00001128Init *VarInit::getFieldInit(Record &R, const RecordVal *RV,
1129 const std::string &FieldName) const {
Reid Spencer2a826862006-11-02 20:46:16 +00001130 if (dynamic_cast<RecordRecTy*>(getType()))
Jakob Stoklund Olesen0e457622010-03-25 06:23:34 +00001131 if (const RecordVal *Val = R.getValue(VarName)) {
1132 if (RV != Val && (RV || dynamic_cast<UnsetInit*>(Val->getValue())))
1133 return 0;
1134 Init *TheInit = Val->getValue();
Chris Lattnerd959ab92004-02-28 16:31:53 +00001135 assert(TheInit != this && "Infinite loop detected!");
Jakob Stoklund Olesen0e457622010-03-25 06:23:34 +00001136 if (Init *I = TheInit->getFieldInit(R, RV, FieldName))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001137 return I;
1138 else
1139 return 0;
Chris Lattnerd959ab92004-02-28 16:31:53 +00001140 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001141 return 0;
1142}
1143
1144/// resolveReferences - This method is used by classes that refer to other
Bob Wilson159905a2009-11-21 22:44:20 +00001145/// variables which may not be defined at the time the expression is formed.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001146/// If a value is set for the variable later, this method will be called on
1147/// users of the value to allow the value to propagate out.
1148///
Chris Lattneref943742005-04-19 03:36:21 +00001149Init *VarInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001150 if (RecordVal *Val = R.getValue(VarName))
Chris Lattneref943742005-04-19 03:36:21 +00001151 if (RV == Val || (RV == 0 && !dynamic_cast<UnsetInit*>(Val->getValue())))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001152 return Val->getValue();
1153 return this;
1154}
Misha Brukman650ba8e2005-04-22 00:00:37 +00001155
Chris Lattner695506c2007-11-22 21:05:25 +00001156std::string VarBitInit::getAsString() const {
1157 return TI->getAsString() + "{" + utostr(Bit) + "}";
1158}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001159
Chris Lattneref943742005-04-19 03:36:21 +00001160Init *VarBitInit::resolveReferences(Record &R, const RecordVal *RV) {
1161 if (Init *I = getVariable()->resolveBitReference(R, RV, getBitNum()))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001162 return I;
1163 return this;
1164}
1165
Chris Lattner695506c2007-11-22 21:05:25 +00001166std::string VarListElementInit::getAsString() const {
1167 return TI->getAsString() + "[" + utostr(Element) + "]";
1168}
1169
Chris Lattneref943742005-04-19 03:36:21 +00001170Init *VarListElementInit::resolveReferences(Record &R, const RecordVal *RV) {
1171 if (Init *I = getVariable()->resolveListElementReference(R, RV,
1172 getElementNum()))
Chris Lattner577fc3f2004-07-27 01:01:21 +00001173 return I;
1174 return this;
1175}
1176
Chris Lattneref943742005-04-19 03:36:21 +00001177Init *VarListElementInit::resolveBitReference(Record &R, const RecordVal *RV,
1178 unsigned Bit) {
Chris Lattner577fc3f2004-07-27 01:01:21 +00001179 // FIXME: This should be implemented, to support references like:
1180 // bit B = AA[0]{1};
1181 return 0;
1182}
1183
Chris Lattneref943742005-04-19 03:36:21 +00001184Init *VarListElementInit::
1185resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) {
Chris Lattner577fc3f2004-07-27 01:01:21 +00001186 // FIXME: This should be implemented, to support references like:
1187 // int B = AA[0][1];
1188 return 0;
1189}
1190
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001191RecTy *DefInit::getFieldType(const std::string &FieldName) const {
1192 if (const RecordVal *RV = Def->getValue(FieldName))
1193 return RV->getType();
1194 return 0;
1195}
1196
Jakob Stoklund Olesen0e457622010-03-25 06:23:34 +00001197Init *DefInit::getFieldInit(Record &R, const RecordVal *RV,
1198 const std::string &FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001199 return Def->getValue(FieldName)->getValue();
1200}
1201
1202
Chris Lattner695506c2007-11-22 21:05:25 +00001203std::string DefInit::getAsString() const {
1204 return Def->getName();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001205}
1206
Chris Lattneref943742005-04-19 03:36:21 +00001207Init *FieldInit::resolveBitReference(Record &R, const RecordVal *RV,
1208 unsigned Bit) {
Jakob Stoklund Olesen0e457622010-03-25 06:23:34 +00001209 if (Init *BitsVal = Rec->getFieldInit(R, RV, FieldName))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001210 if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
1211 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
1212 Init *B = BI->getBit(Bit);
Misha Brukman650ba8e2005-04-22 00:00:37 +00001213
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001214 if (dynamic_cast<BitInit*>(B)) // If the bit is set...
1215 return B; // Replace the VarBitInit with it.
1216 }
Chris Lattner577fc3f2004-07-27 01:01:21 +00001217 return 0;
1218}
1219
Chris Lattneref943742005-04-19 03:36:21 +00001220Init *FieldInit::resolveListElementReference(Record &R, const RecordVal *RV,
1221 unsigned Elt) {
Jakob Stoklund Olesen0e457622010-03-25 06:23:34 +00001222 if (Init *ListVal = Rec->getFieldInit(R, RV, FieldName))
Chris Lattner577fc3f2004-07-27 01:01:21 +00001223 if (ListInit *LI = dynamic_cast<ListInit*>(ListVal)) {
1224 if (Elt >= LI->getSize()) return 0;
1225 Init *E = LI->getElement(Elt);
1226
Bob Wilson67e6cab2009-11-22 03:58:57 +00001227 // If the element is set to some value, or if we are resolving a
1228 // reference to a specific variable and that variable is explicitly
1229 // unset, then replace the VarListElementInit with it.
1230 if (RV || !dynamic_cast<UnsetInit*>(E))
1231 return E;
Chris Lattner577fc3f2004-07-27 01:01:21 +00001232 }
1233 return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001234}
1235
Chris Lattneref943742005-04-19 03:36:21 +00001236Init *FieldInit::resolveReferences(Record &R, const RecordVal *RV) {
1237 Init *NewRec = RV ? Rec->resolveReferences(R, RV) : Rec;
1238
Jakob Stoklund Olesen0e457622010-03-25 06:23:34 +00001239 Init *BitsVal = NewRec->getFieldInit(R, RV, FieldName);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001240 if (BitsVal) {
Chris Lattneref943742005-04-19 03:36:21 +00001241 Init *BVR = BitsVal->resolveReferences(R, RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001242 return BVR->isComplete() ? BVR : this;
1243 }
Chris Lattneref943742005-04-19 03:36:21 +00001244
1245 if (NewRec != Rec) {
Chris Lattneref943742005-04-19 03:36:21 +00001246 return new FieldInit(NewRec, FieldName);
1247 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001248 return this;
1249}
1250
Chris Lattner0d3ef402006-01-31 06:02:35 +00001251Init *DagInit::resolveReferences(Record &R, const RecordVal *RV) {
1252 std::vector<Init*> NewArgs;
1253 for (unsigned i = 0, e = Args.size(); i != e; ++i)
1254 NewArgs.push_back(Args[i]->resolveReferences(R, RV));
Bob Wilson7248f862009-11-22 04:24:42 +00001255
Chris Lattnerb59cf3c2006-03-30 22:50:40 +00001256 Init *Op = Val->resolveReferences(R, RV);
Bob Wilson7248f862009-11-22 04:24:42 +00001257
Chris Lattnerb59cf3c2006-03-30 22:50:40 +00001258 if (Args != NewArgs || Op != Val)
Bruno Cardoso Lopes30a28d62010-06-23 19:50:39 +00001259 return new DagInit(Op, ValName, NewArgs, ArgNames);
Bob Wilson7248f862009-11-22 04:24:42 +00001260
Chris Lattner0d3ef402006-01-31 06:02:35 +00001261 return this;
1262}
1263
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001264
Chris Lattner695506c2007-11-22 21:05:25 +00001265std::string DagInit::getAsString() const {
1266 std::string Result = "(" + Val->getAsString();
Nate Begemandbe3f772009-03-19 05:21:56 +00001267 if (!ValName.empty())
1268 Result += ":" + ValName;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001269 if (Args.size()) {
Chris Lattner695506c2007-11-22 21:05:25 +00001270 Result += " " + Args[0]->getAsString();
1271 if (!ArgNames[0].empty()) Result += ":$" + ArgNames[0];
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001272 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
Chris Lattner695506c2007-11-22 21:05:25 +00001273 Result += ", " + Args[i]->getAsString();
1274 if (!ArgNames[i].empty()) Result += ":$" + ArgNames[i];
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001275 }
1276 }
Chris Lattner695506c2007-11-22 21:05:25 +00001277 return Result + ")";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001278}
1279
1280
1281//===----------------------------------------------------------------------===//
1282// Other implementations
1283//===----------------------------------------------------------------------===//
1284
1285RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
1286 : Name(N), Ty(T), Prefix(P) {
1287 Value = Ty->convertValue(new UnsetInit());
1288 assert(Value && "Cannot create unset value for current type!");
1289}
1290
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001291void RecordVal::dump() const { errs() << *this; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001292
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001293void RecordVal::print(raw_ostream &OS, bool PrintSem) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001294 if (getPrefix()) OS << "field ";
1295 OS << *getType() << " " << getName();
Chris Lattneref943742005-04-19 03:36:21 +00001296
1297 if (getValue())
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001298 OS << " = " << *getValue();
Chris Lattneref943742005-04-19 03:36:21 +00001299
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001300 if (PrintSem) OS << ";\n";
1301}
1302
Daniel Dunbarced00812009-08-23 09:47:37 +00001303unsigned Record::LastID = 0;
1304
Chris Lattnerac284252005-08-19 17:58:11 +00001305void Record::setName(const std::string &Name) {
1306 if (Records.getDef(getName()) == this) {
1307 Records.removeDef(getName());
1308 this->Name = Name;
1309 Records.addDef(this);
1310 } else {
1311 Records.removeClass(getName());
1312 this->Name = Name;
1313 Records.addClass(this);
1314 }
1315}
1316
Chris Lattneref943742005-04-19 03:36:21 +00001317/// resolveReferencesTo - If anything in this record refers to RV, replace the
1318/// reference to RV with the RHS of RV. If RV is null, we resolve all possible
1319/// references.
1320void Record::resolveReferencesTo(const RecordVal *RV) {
1321 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
1322 if (Init *V = Values[i].getValue())
1323 Values[i].setValue(V->resolveReferences(*this, RV));
1324 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001325}
1326
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001327void Record::dump() const { errs() << *this; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001328
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001329raw_ostream &llvm::operator<<(raw_ostream &OS, const Record &R) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001330 OS << R.getName();
1331
1332 const std::vector<std::string> &TArgs = R.getTemplateArgs();
1333 if (!TArgs.empty()) {
1334 OS << "<";
1335 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1336 if (i) OS << ", ";
1337 const RecordVal *RV = R.getValue(TArgs[i]);
1338 assert(RV && "Template argument record not found??");
1339 RV->print(OS, false);
1340 }
1341 OS << ">";
1342 }
1343
1344 OS << " {";
1345 const std::vector<Record*> &SC = R.getSuperClasses();
1346 if (!SC.empty()) {
1347 OS << "\t//";
1348 for (unsigned i = 0, e = SC.size(); i != e; ++i)
1349 OS << " " << SC[i]->getName();
1350 }
1351 OS << "\n";
1352
1353 const std::vector<RecordVal> &Vals = R.getValues();
1354 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
1355 if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
1356 OS << Vals[i];
1357 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
1358 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
1359 OS << Vals[i];
1360
1361 return OS << "}\n";
1362}
1363
1364/// getValueInit - Return the initializer for a value with the specified name,
1365/// or throw an exception if the field does not exist.
1366///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001367Init *Record::getValueInit(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001368 const RecordVal *R = getValue(FieldName);
1369 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001370 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001371 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001372 return R->getValue();
1373}
1374
1375
1376/// getValueAsString - This method looks up the specified field and returns its
1377/// value as a string, throwing an exception if the field does not exist or if
1378/// the value is not a string.
1379///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001380std::string Record::getValueAsString(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001381 const RecordVal *R = getValue(FieldName);
1382 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001383 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001384 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001385
1386 if (const StringInit *SI = dynamic_cast<const StringInit*>(R->getValue()))
1387 return SI->getValue();
Chris Lattner42bb0c12009-09-18 18:31:37 +00001388 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001389 "' does not have a string initializer!";
1390}
1391
1392/// getValueAsBitsInit - This method looks up the specified field and returns
1393/// its value as a BitsInit, throwing an exception if the field does not exist
1394/// or if the value is not the right type.
1395///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001396BitsInit *Record::getValueAsBitsInit(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001397 const RecordVal *R = getValue(FieldName);
1398 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001399 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001400 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001401
1402 if (BitsInit *BI = dynamic_cast<BitsInit*>(R->getValue()))
1403 return BI;
Chris Lattner42bb0c12009-09-18 18:31:37 +00001404 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001405 "' does not have a BitsInit initializer!";
1406}
1407
1408/// getValueAsListInit - This method looks up the specified field and returns
1409/// its value as a ListInit, throwing an exception if the field does not exist
1410/// or if the value is not the right type.
1411///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001412ListInit *Record::getValueAsListInit(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001413 const RecordVal *R = getValue(FieldName);
1414 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001415 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001416 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001417
1418 if (ListInit *LI = dynamic_cast<ListInit*>(R->getValue()))
1419 return LI;
Chris Lattner42bb0c12009-09-18 18:31:37 +00001420 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001421 "' does not have a list initializer!";
1422}
1423
Chris Lattner7ad0bed2005-10-28 22:49:02 +00001424/// getValueAsListOfDefs - This method looks up the specified field and returns
Jim Laskeyb04feb62005-10-28 21:46:31 +00001425/// its value as a vector of records, throwing an exception if the field does
1426/// not exist or if the value is not the right type.
1427///
Bob Wilson7248f862009-11-22 04:24:42 +00001428std::vector<Record*>
Chris Lattner42bb0c12009-09-18 18:31:37 +00001429Record::getValueAsListOfDefs(StringRef FieldName) const {
Jim Laskeyb04feb62005-10-28 21:46:31 +00001430 ListInit *List = getValueAsListInit(FieldName);
1431 std::vector<Record*> Defs;
1432 for (unsigned i = 0; i < List->getSize(); i++) {
1433 if (DefInit *DI = dynamic_cast<DefInit*>(List->getElement(i))) {
1434 Defs.push_back(DI->getDef());
1435 } else {
Chris Lattner42bb0c12009-09-18 18:31:37 +00001436 throw "Record `" + getName() + "', field `" + FieldName.str() +
Jim Laskeyb04feb62005-10-28 21:46:31 +00001437 "' list is not entirely DefInit!";
1438 }
1439 }
1440 return Defs;
1441}
1442
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001443/// getValueAsInt - This method looks up the specified field and returns its
Dan Gohmanca0546f2008-10-17 01:33:43 +00001444/// value as an int64_t, throwing an exception if the field does not exist or if
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001445/// the value is not the right type.
1446///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001447int64_t Record::getValueAsInt(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001448 const RecordVal *R = getValue(FieldName);
1449 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001450 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001451 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001452
1453 if (IntInit *II = dynamic_cast<IntInit*>(R->getValue()))
1454 return II->getValue();
Chris Lattner42bb0c12009-09-18 18:31:37 +00001455 throw "Record `" + getName() + "', field `" + FieldName.str() +
Nate Begemanf621b332005-11-30 18:37:14 +00001456 "' does not have an int initializer!";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001457}
1458
Anton Korobeynikova468a112007-11-11 11:19:37 +00001459/// getValueAsListOfInts - This method looks up the specified field and returns
1460/// its value as a vector of integers, throwing an exception if the field does
1461/// not exist or if the value is not the right type.
1462///
Bob Wilson7248f862009-11-22 04:24:42 +00001463std::vector<int64_t>
Chris Lattner42bb0c12009-09-18 18:31:37 +00001464Record::getValueAsListOfInts(StringRef FieldName) const {
Anton Korobeynikova468a112007-11-11 11:19:37 +00001465 ListInit *List = getValueAsListInit(FieldName);
Dan Gohmanca0546f2008-10-17 01:33:43 +00001466 std::vector<int64_t> Ints;
Anton Korobeynikova468a112007-11-11 11:19:37 +00001467 for (unsigned i = 0; i < List->getSize(); i++) {
1468 if (IntInit *II = dynamic_cast<IntInit*>(List->getElement(i))) {
1469 Ints.push_back(II->getValue());
1470 } else {
Chris Lattner42bb0c12009-09-18 18:31:37 +00001471 throw "Record `" + getName() + "', field `" + FieldName.str() +
Anton Korobeynikova468a112007-11-11 11:19:37 +00001472 "' does not have a list of ints initializer!";
1473 }
1474 }
1475 return Ints;
1476}
1477
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001478/// getValueAsDef - This method looks up the specified field and returns its
1479/// value as a Record, throwing an exception if the field does not exist or if
1480/// the value is not the right type.
1481///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001482Record *Record::getValueAsDef(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001483 const RecordVal *R = getValue(FieldName);
1484 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001485 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001486 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001487
1488 if (DefInit *DI = dynamic_cast<DefInit*>(R->getValue()))
1489 return DI->getDef();
Chris Lattner42bb0c12009-09-18 18:31:37 +00001490 throw "Record `" + getName() + "', field `" + FieldName.str() +
Nate Begemanf621b332005-11-30 18:37:14 +00001491 "' does not have a def initializer!";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001492}
1493
1494/// getValueAsBit - This method looks up the specified field and returns its
1495/// value as a bit, throwing an exception if the field does not exist or if
1496/// the value is not the right type.
1497///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001498bool Record::getValueAsBit(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001499 const RecordVal *R = getValue(FieldName);
1500 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001501 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001502 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001503
1504 if (BitInit *BI = dynamic_cast<BitInit*>(R->getValue()))
1505 return BI->getValue();
Chris Lattner42bb0c12009-09-18 18:31:37 +00001506 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001507 "' does not have a bit initializer!";
1508}
1509
1510/// getValueAsDag - This method looks up the specified field and returns its
1511/// value as an Dag, throwing an exception if the field does not exist or if
1512/// the value is not the right type.
1513///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001514DagInit *Record::getValueAsDag(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001515 const RecordVal *R = getValue(FieldName);
1516 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001517 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001518 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001519
1520 if (DagInit *DI = dynamic_cast<DagInit*>(R->getValue()))
1521 return DI;
Chris Lattner42bb0c12009-09-18 18:31:37 +00001522 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001523 "' does not have a dag initializer!";
1524}
1525
Chris Lattner42bb0c12009-09-18 18:31:37 +00001526std::string Record::getValueAsCode(StringRef FieldName) const {
Chris Lattnerae939eb2005-09-13 21:44:28 +00001527 const RecordVal *R = getValue(FieldName);
1528 if (R == 0 || R->getValue() == 0)
1529 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001530 FieldName.str() + "'!\n";
Bob Wilson7248f862009-11-22 04:24:42 +00001531
Chris Lattnerae939eb2005-09-13 21:44:28 +00001532 if (const CodeInit *CI = dynamic_cast<const CodeInit*>(R->getValue()))
1533 return CI->getValue();
Chris Lattner42bb0c12009-09-18 18:31:37 +00001534 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerae939eb2005-09-13 21:44:28 +00001535 "' does not have a code initializer!";
1536}
1537
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001538
David Greene7049e792009-04-24 16:55:41 +00001539void MultiClass::dump() const {
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001540 errs() << "Record:\n";
David Greene7049e792009-04-24 16:55:41 +00001541 Rec.dump();
Bob Wilson7248f862009-11-22 04:24:42 +00001542
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001543 errs() << "Defs:\n";
David Greene7049e792009-04-24 16:55:41 +00001544 for (RecordVector::const_iterator r = DefPrototypes.begin(),
1545 rend = DefPrototypes.end();
1546 r != rend;
1547 ++r) {
1548 (*r)->dump();
1549 }
1550}
1551
1552
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001553void RecordKeeper::dump() const { errs() << *this; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001554
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001555raw_ostream &llvm::operator<<(raw_ostream &OS, const RecordKeeper &RK) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001556 OS << "------------- Classes -----------------\n";
1557 const std::map<std::string, Record*> &Classes = RK.getClasses();
1558 for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001559 E = Classes.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001560 OS << "class " << *I->second;
Misha Brukman650ba8e2005-04-22 00:00:37 +00001561
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001562 OS << "------------- Defs -----------------\n";
1563 const std::map<std::string, Record*> &Defs = RK.getDefs();
1564 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001565 E = Defs.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001566 OS << "def " << *I->second;
1567 return OS;
1568}
1569
1570
1571/// getAllDerivedDefinitions - This method returns all concrete definitions
1572/// that derive from the specified class name. If a class with the specified
1573/// name does not exist, an error is printed and true is returned.
1574std::vector<Record*>
1575RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
1576 Record *Class = Records.getClass(ClassName);
1577 if (!Class)
Misha Brukman41f9f292004-10-08 14:59:05 +00001578 throw "ERROR: Couldn't find the `" + ClassName + "' class!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001579
1580 std::vector<Record*> Defs;
1581 for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(),
1582 E = getDefs().end(); I != E; ++I)
1583 if (I->second->isSubClassOf(Class))
1584 Defs.push_back(I->second);
1585
1586 return Defs;
1587}
Brian Gaeke960707c2003-11-11 22:41:34 +00001588