blob: f0147d328b8ea32cb45e7e62927797ce81ca2a15 [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 {
273 return Rec == RHS->getRecord() || RHS->getRecord()->isSubClassOf(Rec);
274}
275
276
Bob Wilson7248f862009-11-22 04:24:42 +0000277/// resolveTypes - Find a common type that T1 and T2 convert to.
David Greene8618f952009-06-08 20:23:18 +0000278/// Return 0 if no such type exists.
279///
280RecTy *llvm::resolveTypes(RecTy *T1, RecTy *T2) {
281 if (!T1->typeIsConvertibleTo(T2)) {
282 if (!T2->typeIsConvertibleTo(T1)) {
283 // If one is a Record type, check superclasses
284 RecordRecTy *RecTy1 = dynamic_cast<RecordRecTy*>(T1);
285 if (RecTy1) {
286 // See if T2 inherits from a type T1 also inherits from
Bob Wilson7248f862009-11-22 04:24:42 +0000287 const std::vector<Record *> &T1SuperClasses =
288 RecTy1->getRecord()->getSuperClasses();
David Greene8618f952009-06-08 20:23:18 +0000289 for(std::vector<Record *>::const_iterator i = T1SuperClasses.begin(),
290 iend = T1SuperClasses.end();
291 i != iend;
292 ++i) {
293 RecordRecTy *SuperRecTy1 = new RecordRecTy(*i);
294 RecTy *NewType1 = resolveTypes(SuperRecTy1, T2);
295 if (NewType1 != 0) {
296 if (NewType1 != SuperRecTy1) {
297 delete SuperRecTy1;
298 }
299 return NewType1;
300 }
301 }
302 }
303 RecordRecTy *RecTy2 = dynamic_cast<RecordRecTy*>(T2);
304 if (RecTy2) {
305 // See if T1 inherits from a type T2 also inherits from
Bob Wilson7248f862009-11-22 04:24:42 +0000306 const std::vector<Record *> &T2SuperClasses =
307 RecTy2->getRecord()->getSuperClasses();
308 for (std::vector<Record *>::const_iterator i = T2SuperClasses.begin(),
David Greene8618f952009-06-08 20:23:18 +0000309 iend = T2SuperClasses.end();
310 i != iend;
311 ++i) {
312 RecordRecTy *SuperRecTy2 = new RecordRecTy(*i);
313 RecTy *NewType2 = resolveTypes(T1, SuperRecTy2);
314 if (NewType2 != 0) {
315 if (NewType2 != SuperRecTy2) {
316 delete SuperRecTy2;
317 }
318 return NewType2;
319 }
320 }
321 }
322 return 0;
323 }
324 return T2;
325 }
326 return T1;
327}
328
329
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000330//===----------------------------------------------------------------------===//
331// Initializer implementations
332//===----------------------------------------------------------------------===//
333
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000334void Init::dump() const { return print(errs()); }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000335
336Init *BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
337 BitsInit *BI = new BitsInit(Bits.size());
338 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
339 if (Bits[i] >= getNumBits()) {
340 delete BI;
341 return 0;
342 }
343 BI->setBit(i, getBit(Bits[i]));
344 }
345 return BI;
346}
347
Chris Lattner695506c2007-11-22 21:05:25 +0000348std::string BitsInit::getAsString() const {
Chris Lattner695506c2007-11-22 21:05:25 +0000349 std::string Result = "{ ";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000350 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
Chris Lattner695506c2007-11-22 21:05:25 +0000351 if (i) Result += ", ";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000352 if (Init *Bit = getBit(e-i-1))
Chris Lattner695506c2007-11-22 21:05:25 +0000353 Result += Bit->getAsString();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000354 else
Chris Lattner695506c2007-11-22 21:05:25 +0000355 Result += "*";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000356 }
Chris Lattner695506c2007-11-22 21:05:25 +0000357 return Result + " }";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000358}
359
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000360// resolveReferences - If there are any field references that refer to fields
361// that have been filled in, we can propagate the values now.
362//
Chris Lattneref943742005-04-19 03:36:21 +0000363Init *BitsInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000364 bool Changed = false;
365 BitsInit *New = new BitsInit(getNumBits());
366
367 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
368 Init *B;
369 Init *CurBit = getBit(i);
370
371 do {
372 B = CurBit;
Chris Lattneref943742005-04-19 03:36:21 +0000373 CurBit = CurBit->resolveReferences(R, RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000374 Changed |= B != CurBit;
375 } while (B != CurBit);
376 New->setBit(i, CurBit);
377 }
378
379 if (Changed)
380 return New;
381 delete New;
382 return this;
383}
384
Chris Lattner695506c2007-11-22 21:05:25 +0000385std::string IntInit::getAsString() const {
386 return itostr(Value);
387}
388
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000389Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
390 BitsInit *BI = new BitsInit(Bits.size());
391
392 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
Dan Gohmanca0546f2008-10-17 01:33:43 +0000393 if (Bits[i] >= 64) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000394 delete BI;
395 return 0;
396 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000397 BI->setBit(i, new BitInit(Value & (INT64_C(1) << Bits[i])));
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000398 }
399 return BI;
400}
401
Chris Lattner8bf9e062004-07-26 23:21:34 +0000402Init *ListInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
403 std::vector<Init*> Vals;
404 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
405 if (Elements[i] >= getSize())
406 return 0;
407 Vals.push_back(getElement(Elements[i]));
408 }
David Greene8618f952009-06-08 20:23:18 +0000409 return new ListInit(Vals, getType());
Chris Lattner8bf9e062004-07-26 23:21:34 +0000410}
411
Chris Lattnercbebe462007-02-27 22:08:27 +0000412Record *ListInit::getElementAsRecord(unsigned i) const {
413 assert(i < Values.size() && "List element index out of range!");
414 DefInit *DI = dynamic_cast<DefInit*>(Values[i]);
415 if (DI == 0) throw "Expected record in list!";
416 return DI->getDef();
417}
418
Chris Lattneref943742005-04-19 03:36:21 +0000419Init *ListInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattner577fc3f2004-07-27 01:01:21 +0000420 std::vector<Init*> Resolved;
421 Resolved.reserve(getSize());
422 bool Changed = false;
423
424 for (unsigned i = 0, e = getSize(); i != e; ++i) {
425 Init *E;
426 Init *CurElt = getElement(i);
427
428 do {
429 E = CurElt;
Chris Lattneref943742005-04-19 03:36:21 +0000430 CurElt = CurElt->resolveReferences(R, RV);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000431 Changed |= E != CurElt;
432 } while (E != CurElt);
433 Resolved.push_back(E);
434 }
435
436 if (Changed)
David Greene8618f952009-06-08 20:23:18 +0000437 return new ListInit(Resolved, getType());
Chris Lattner577fc3f2004-07-27 01:01:21 +0000438 return this;
439}
440
David Greene8618f952009-06-08 20:23:18 +0000441Init *ListInit::resolveListElementReference(Record &R, const RecordVal *IRV,
Bob Wilson7248f862009-11-22 04:24:42 +0000442 unsigned Elt) {
David Greene8618f952009-06-08 20:23:18 +0000443 if (Elt >= getSize())
444 return 0; // Out of range reference.
445 Init *E = getElement(Elt);
Bob Wilson67e6cab2009-11-22 03:58:57 +0000446 // If the element is set to some value, or if we are resolving a reference
447 // to a specific variable and that variable is explicitly unset, then
448 // replace the VarListElementInit with it.
449 if (IRV || !dynamic_cast<UnsetInit*>(E))
450 return E;
David Greene8618f952009-06-08 20:23:18 +0000451 return 0;
452}
453
Chris Lattner695506c2007-11-22 21:05:25 +0000454std::string ListInit::getAsString() const {
455 std::string Result = "[";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000456 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
Chris Lattner695506c2007-11-22 21:05:25 +0000457 if (i) Result += ", ";
458 Result += Values[i]->getAsString();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000459 }
Chris Lattner695506c2007-11-22 21:05:25 +0000460 return Result + "]";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000461}
462
David Greene5d0c0512009-05-14 20:54:48 +0000463Init *OpInit::resolveBitReference(Record &R, const RecordVal *IRV,
Bob Wilson7248f862009-11-22 04:24:42 +0000464 unsigned Bit) {
David Greene5d0c0512009-05-14 20:54:48 +0000465 Init *Folded = Fold(&R, 0);
466
467 if (Folded != this) {
468 TypedInit *Typed = dynamic_cast<TypedInit *>(Folded);
469 if (Typed) {
470 return Typed->resolveBitReference(R, IRV, Bit);
Bob Wilson7248f862009-11-22 04:24:42 +0000471 }
David Greene5d0c0512009-05-14 20:54:48 +0000472 }
Bob Wilson7248f862009-11-22 04:24:42 +0000473
David Greene5d0c0512009-05-14 20:54:48 +0000474 return 0;
475}
476
477Init *OpInit::resolveListElementReference(Record &R, const RecordVal *IRV,
Bob Wilson7248f862009-11-22 04:24:42 +0000478 unsigned Elt) {
David Greene5d0c0512009-05-14 20:54:48 +0000479 Init *Folded = Fold(&R, 0);
480
481 if (Folded != this) {
482 TypedInit *Typed = dynamic_cast<TypedInit *>(Folded);
483 if (Typed) {
484 return Typed->resolveListElementReference(R, IRV, Elt);
Bob Wilson7248f862009-11-22 04:24:42 +0000485 }
David Greene5d0c0512009-05-14 20:54:48 +0000486 }
Bob Wilson7248f862009-11-22 04:24:42 +0000487
David Greene5d0c0512009-05-14 20:54:48 +0000488 return 0;
489}
490
David Greenee8f3b272009-05-14 21:22:49 +0000491Init *UnOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
492 switch (getOpcode()) {
493 default: assert(0 && "Unknown unop");
494 case CAST: {
David Greeneefa19612009-06-29 20:05:29 +0000495 if (getType()->getAsString() == "string") {
496 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
497 if (LHSs) {
498 return LHSs;
499 }
David Greene5d0c0512009-05-14 20:54:48 +0000500
David Greeneefa19612009-06-29 20:05:29 +0000501 DefInit *LHSd = dynamic_cast<DefInit*>(LHS);
502 if (LHSd) {
503 return new StringInit(LHSd->getDef()->getName());
504 }
Bob Wilson7248f862009-11-22 04:24:42 +0000505 } else {
David Greeneefa19612009-06-29 20:05:29 +0000506 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
507 if (LHSs) {
508 std::string Name = LHSs->getValue();
509
510 // From TGParser::ParseIDValue
511 if (CurRec) {
512 if (const RecordVal *RV = CurRec->getValue(Name)) {
513 if (RV->getType() != getType()) {
514 throw "type mismatch in nameconcat";
515 }
516 return new VarInit(Name, RV->getType());
David Greenee8f3b272009-05-14 21:22:49 +0000517 }
David Greeneefa19612009-06-29 20:05:29 +0000518
519 std::string TemplateArgName = CurRec->getName()+":"+Name;
520 if (CurRec->isTemplateArg(TemplateArgName)) {
521 const RecordVal *RV = CurRec->getValue(TemplateArgName);
522 assert(RV && "Template arg doesn't exist??");
523
524 if (RV->getType() != getType()) {
525 throw "type mismatch in nameconcat";
526 }
527
528 return new VarInit(TemplateArgName, RV->getType());
529 }
530 }
531
532 if (CurMultiClass) {
533 std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
534 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
535 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
536 assert(RV && "Template arg doesn't exist??");
Bob Wilson7248f862009-11-22 04:24:42 +0000537
David Greeneefa19612009-06-29 20:05:29 +0000538 if (RV->getType() != getType()) {
539 throw "type mismatch in nameconcat";
540 }
Bob Wilson7248f862009-11-22 04:24:42 +0000541
David Greeneefa19612009-06-29 20:05:29 +0000542 return new VarInit(MCName, RV->getType());
543 }
David Greenee8f3b272009-05-14 21:22:49 +0000544 }
Bob Wilson7248f862009-11-22 04:24:42 +0000545
David Greeneefa19612009-06-29 20:05:29 +0000546 if (Record *D = Records.getDef(Name))
547 return new DefInit(D);
David Greene5d0c0512009-05-14 20:54:48 +0000548
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000549 errs() << "Variable not defined: '" + Name + "'\n";
David Greeneefa19612009-06-29 20:05:29 +0000550 assert(0 && "Variable not found");
551 return 0;
David Greenee8f3b272009-05-14 21:22:49 +0000552 }
David Greenee8f3b272009-05-14 21:22:49 +0000553 }
554 break;
555 }
David Greened571b3c2009-05-14 22:38:31 +0000556 case CAR: {
557 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
558 if (LHSl) {
559 if (LHSl->getSize() == 0) {
560 assert(0 && "Empty list in car");
561 return 0;
562 }
563 return LHSl->getElement(0);
564 }
565 break;
566 }
567 case CDR: {
568 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
569 if (LHSl) {
570 if (LHSl->getSize() == 0) {
571 assert(0 && "Empty list in cdr");
572 return 0;
573 }
Bob Wilson7248f862009-11-22 04:24:42 +0000574 ListInit *Result = new ListInit(LHSl->begin()+1, LHSl->end(),
575 LHSl->getType());
David Greened571b3c2009-05-14 22:38:31 +0000576 return Result;
577 }
578 break;
579 }
580 case LNULL: {
581 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
582 if (LHSl) {
583 if (LHSl->getSize() == 0) {
584 return new IntInit(1);
Bob Wilson7248f862009-11-22 04:24:42 +0000585 } else {
David Greened571b3c2009-05-14 22:38:31 +0000586 return new IntInit(0);
587 }
588 }
David Greene8618f952009-06-08 20:23:18 +0000589 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
590 if (LHSs) {
591 if (LHSs->getValue().empty()) {
592 return new IntInit(1);
Bob Wilson7248f862009-11-22 04:24:42 +0000593 } else {
David Greene8618f952009-06-08 20:23:18 +0000594 return new IntInit(0);
595 }
596 }
Bob Wilson7248f862009-11-22 04:24:42 +0000597
David Greened571b3c2009-05-14 22:38:31 +0000598 break;
599 }
David Greenee8f3b272009-05-14 21:22:49 +0000600 }
601 return this;
602}
David Greene5d0c0512009-05-14 20:54:48 +0000603
David Greenee8f3b272009-05-14 21:22:49 +0000604Init *UnOpInit::resolveReferences(Record &R, const RecordVal *RV) {
605 Init *lhs = LHS->resolveReferences(R, RV);
Bob Wilson7248f862009-11-22 04:24:42 +0000606
David Greenee8f3b272009-05-14 21:22:49 +0000607 if (LHS != lhs)
608 return (new UnOpInit(getOpcode(), lhs, getType()))->Fold(&R, 0);
609 return Fold(&R, 0);
610}
David Greene5d0c0512009-05-14 20:54:48 +0000611
David Greenee8f3b272009-05-14 21:22:49 +0000612std::string UnOpInit::getAsString() const {
613 std::string Result;
614 switch (Opc) {
615 case CAST: Result = "!cast<" + getType()->getAsString() + ">"; break;
David Greened571b3c2009-05-14 22:38:31 +0000616 case CAR: Result = "!car"; break;
617 case CDR: Result = "!cdr"; break;
618 case LNULL: Result = "!null"; break;
David Greenee8f3b272009-05-14 21:22:49 +0000619 }
620 return Result + "(" + LHS->getAsString() + ")";
621}
David Greene5d0c0512009-05-14 20:54:48 +0000622
David Greeneefa19612009-06-29 20:05:29 +0000623RecTy *UnOpInit::getFieldType(const std::string &FieldName) const {
624 switch (getOpcode()) {
625 default: assert(0 && "Unknown unop");
626 case CAST: {
627 RecordRecTy *RecordType = dynamic_cast<RecordRecTy *>(getType());
628 if (RecordType) {
629 RecordVal *Field = RecordType->getRecord()->getValue(FieldName);
630 if (Field) {
631 return Field->getType();
632 }
633 }
634 break;
635 }
636 }
637 return 0;
638}
639
David Greenea9c6c5d2009-04-22 20:18:10 +0000640Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
Chris Lattner51ffbf12006-03-31 21:53:49 +0000641 switch (getOpcode()) {
642 default: assert(0 && "Unknown binop");
Evan Chenga32dee22007-05-15 01:23:24 +0000643 case CONCAT: {
644 DagInit *LHSs = dynamic_cast<DagInit*>(LHS);
645 DagInit *RHSs = dynamic_cast<DagInit*>(RHS);
646 if (LHSs && RHSs) {
647 DefInit *LOp = dynamic_cast<DefInit*>(LHSs->getOperator());
648 DefInit *ROp = dynamic_cast<DefInit*>(RHSs->getOperator());
Chris Lattner81fd1f22010-03-18 21:07:51 +0000649 if (LOp == 0 || ROp == 0 || LOp->getDef() != ROp->getDef())
650 throw "Concated Dag operators do not match!";
Evan Chenga32dee22007-05-15 01:23:24 +0000651 std::vector<Init*> Args;
652 std::vector<std::string> ArgNames;
653 for (unsigned i = 0, e = LHSs->getNumArgs(); i != e; ++i) {
654 Args.push_back(LHSs->getArg(i));
655 ArgNames.push_back(LHSs->getArgName(i));
656 }
657 for (unsigned i = 0, e = RHSs->getNumArgs(); i != e; ++i) {
658 Args.push_back(RHSs->getArg(i));
659 ArgNames.push_back(RHSs->getArgName(i));
660 }
Nate Begemandbe3f772009-03-19 05:21:56 +0000661 return new DagInit(LHSs->getOperator(), "", Args, ArgNames);
Evan Chenga32dee22007-05-15 01:23:24 +0000662 }
663 break;
664 }
Chris Lattner51ffbf12006-03-31 21:53:49 +0000665 case STRCONCAT: {
666 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
667 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
668 if (LHSs && RHSs)
669 return new StringInit(LHSs->getValue() + RHSs->getValue());
670 break;
671 }
David Greenea9c6c5d2009-04-22 20:18:10 +0000672 case NAMECONCAT: {
673 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
674 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
675 if (LHSs && RHSs) {
676 std::string Name(LHSs->getValue() + RHSs->getValue());
677
678 // From TGParser::ParseIDValue
679 if (CurRec) {
David Greene196ac3c2009-04-23 21:25:15 +0000680 if (const RecordVal *RV = CurRec->getValue(Name)) {
681 if (RV->getType() != getType()) {
682 throw "type mismatch in nameconcat";
683 }
David Greenea9c6c5d2009-04-22 20:18:10 +0000684 return new VarInit(Name, RV->getType());
David Greene196ac3c2009-04-23 21:25:15 +0000685 }
Bob Wilson7248f862009-11-22 04:24:42 +0000686
David Greenea9c6c5d2009-04-22 20:18:10 +0000687 std::string TemplateArgName = CurRec->getName()+":"+Name;
688 if (CurRec->isTemplateArg(TemplateArgName)) {
689 const RecordVal *RV = CurRec->getValue(TemplateArgName);
690 assert(RV && "Template arg doesn't exist??");
David Greene196ac3c2009-04-23 21:25:15 +0000691
692 if (RV->getType() != getType()) {
693 throw "type mismatch in nameconcat";
694 }
695
David Greenea9c6c5d2009-04-22 20:18:10 +0000696 return new VarInit(TemplateArgName, RV->getType());
697 }
698 }
699
700 if (CurMultiClass) {
701 std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
702 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
703 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
704 assert(RV && "Template arg doesn't exist??");
David Greene196ac3c2009-04-23 21:25:15 +0000705
706 if (RV->getType() != getType()) {
707 throw "type mismatch in nameconcat";
708 }
Bob Wilson7248f862009-11-22 04:24:42 +0000709
David Greenea9c6c5d2009-04-22 20:18:10 +0000710 return new VarInit(MCName, RV->getType());
711 }
712 }
713
714 if (Record *D = Records.getDef(Name))
715 return new DefInit(D);
716
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000717 errs() << "Variable not defined in !nameconcat: '" + Name + "'\n";
David Greene8618f952009-06-08 20:23:18 +0000718 assert(0 && "Variable not found in !nameconcat");
David Greenea9c6c5d2009-04-22 20:18:10 +0000719 return 0;
720 }
721 break;
722 }
David Greene297bfe62010-01-05 19:11:42 +0000723 case EQ: {
Bruno Cardoso Lopes77a4a562010-06-16 23:24:12 +0000724 // try to fold eq comparison for 'bit' and 'int', otherwise fallback
725 // to string objects.
726 IntInit* L =
727 dynamic_cast<IntInit*>(LHS->convertInitializerTo(new IntRecTy()));
728 IntInit* R =
729 dynamic_cast<IntInit*>(RHS->convertInitializerTo(new IntRecTy()));
730
731 if (L && R)
732 return new IntInit(L->getValue() == R->getValue());
733
David Greene297bfe62010-01-05 19:11:42 +0000734 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
735 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
Bruno Cardoso Lopes77a4a562010-06-16 23:24:12 +0000736
737 // Make sure we've resolved
David Greene297bfe62010-01-05 19:11:42 +0000738 if (LHSs && RHSs)
739 return new IntInit(LHSs->getValue() == RHSs->getValue());
740
741 break;
742 }
Chris Lattner51ffbf12006-03-31 21:53:49 +0000743 case SHL:
744 case SRA:
745 case SRL: {
746 IntInit *LHSi = dynamic_cast<IntInit*>(LHS);
747 IntInit *RHSi = dynamic_cast<IntInit*>(RHS);
748 if (LHSi && RHSi) {
Dan Gohmanca0546f2008-10-17 01:33:43 +0000749 int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
750 int64_t Result;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000751 switch (getOpcode()) {
752 default: assert(0 && "Bad opcode!");
753 case SHL: Result = LHSv << RHSv; break;
754 case SRA: Result = LHSv >> RHSv; break;
Dan Gohmanca0546f2008-10-17 01:33:43 +0000755 case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000756 }
757 return new IntInit(Result);
758 }
759 break;
760 }
761 }
762 return this;
763}
764
765Init *BinOpInit::resolveReferences(Record &R, const RecordVal *RV) {
766 Init *lhs = LHS->resolveReferences(R, RV);
767 Init *rhs = RHS->resolveReferences(R, RV);
Bob Wilson7248f862009-11-22 04:24:42 +0000768
Chris Lattner51ffbf12006-03-31 21:53:49 +0000769 if (LHS != lhs || RHS != rhs)
David Greene196ac3c2009-04-23 21:25:15 +0000770 return (new BinOpInit(getOpcode(), lhs, rhs, getType()))->Fold(&R, 0);
David Greenea9c6c5d2009-04-22 20:18:10 +0000771 return Fold(&R, 0);
Chris Lattner51ffbf12006-03-31 21:53:49 +0000772}
773
Chris Lattner695506c2007-11-22 21:05:25 +0000774std::string BinOpInit::getAsString() const {
775 std::string Result;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000776 switch (Opc) {
Chris Lattner695506c2007-11-22 21:05:25 +0000777 case CONCAT: Result = "!con"; break;
778 case SHL: Result = "!shl"; break;
779 case SRA: Result = "!sra"; break;
780 case SRL: Result = "!srl"; break;
David Greene297bfe62010-01-05 19:11:42 +0000781 case EQ: Result = "!eq"; break;
Chris Lattner695506c2007-11-22 21:05:25 +0000782 case STRCONCAT: Result = "!strconcat"; break;
Bob Wilson7248f862009-11-22 04:24:42 +0000783 case NAMECONCAT:
David Greene196ac3c2009-04-23 21:25:15 +0000784 Result = "!nameconcat<" + getType()->getAsString() + ">"; break;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000785 }
Chris Lattner695506c2007-11-22 21:05:25 +0000786 return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")";
Chris Lattner51ffbf12006-03-31 21:53:49 +0000787}
788
David Greenee917fff2009-05-14 22:23:47 +0000789static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
790 Record *CurRec, MultiClass *CurMultiClass);
791
792static Init *EvaluateOperation(OpInit *RHSo, Init *LHS, Init *Arg,
793 RecTy *Type, Record *CurRec,
794 MultiClass *CurMultiClass) {
795 std::vector<Init *> NewOperands;
796
797 TypedInit *TArg = dynamic_cast<TypedInit*>(Arg);
798
799 // If this is a dag, recurse
800 if (TArg && TArg->getType()->getAsString() == "dag") {
801 Init *Result = ForeachHelper(LHS, Arg, RHSo, Type,
802 CurRec, CurMultiClass);
803 if (Result != 0) {
804 return Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000805 } else {
David Greenee917fff2009-05-14 22:23:47 +0000806 return 0;
807 }
808 }
809
810 for (int i = 0; i < RHSo->getNumOperands(); ++i) {
811 OpInit *RHSoo = dynamic_cast<OpInit*>(RHSo->getOperand(i));
812
813 if (RHSoo) {
814 Init *Result = EvaluateOperation(RHSoo, LHS, Arg,
815 Type, CurRec, CurMultiClass);
816 if (Result != 0) {
817 NewOperands.push_back(Result);
Bob Wilson7248f862009-11-22 04:24:42 +0000818 } else {
David Greenee917fff2009-05-14 22:23:47 +0000819 NewOperands.push_back(Arg);
820 }
Bob Wilson7248f862009-11-22 04:24:42 +0000821 } else if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
David Greenee917fff2009-05-14 22:23:47 +0000822 NewOperands.push_back(Arg);
Bob Wilson7248f862009-11-22 04:24:42 +0000823 } else {
David Greenee917fff2009-05-14 22:23:47 +0000824 NewOperands.push_back(RHSo->getOperand(i));
825 }
826 }
827
828 // Now run the operator and use its result as the new leaf
829 OpInit *NewOp = RHSo->clone(NewOperands);
830 Init *NewVal = NewOp->Fold(CurRec, CurMultiClass);
831 if (NewVal != NewOp) {
832 delete NewOp;
833 return NewVal;
834 }
835 return 0;
836}
837
838static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
839 Record *CurRec, MultiClass *CurMultiClass) {
840 DagInit *MHSd = dynamic_cast<DagInit*>(MHS);
841 ListInit *MHSl = dynamic_cast<ListInit*>(MHS);
842
843 DagRecTy *DagType = dynamic_cast<DagRecTy*>(Type);
844 ListRecTy *ListType = dynamic_cast<ListRecTy*>(Type);
845
846 OpInit *RHSo = dynamic_cast<OpInit*>(RHS);
847
848 if (!RHSo) {
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000849 errs() << "!foreach requires an operator\n";
David Greenee917fff2009-05-14 22:23:47 +0000850 assert(0 && "No operator for !foreach");
851 }
852
853 TypedInit *LHSt = dynamic_cast<TypedInit*>(LHS);
854
855 if (!LHSt) {
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000856 errs() << "!foreach requires typed variable\n";
David Greenee917fff2009-05-14 22:23:47 +0000857 assert(0 && "No typed variable for !foreach");
858 }
859
Nick Lewycky94298222009-05-15 03:07:14 +0000860 if ((MHSd && DagType) || (MHSl && ListType)) {
David Greenee917fff2009-05-14 22:23:47 +0000861 if (MHSd) {
862 Init *Val = MHSd->getOperator();
863 Init *Result = EvaluateOperation(RHSo, LHS, Val,
864 Type, CurRec, CurMultiClass);
865 if (Result != 0) {
866 Val = Result;
867 }
868
869 std::vector<std::pair<Init *, std::string> > args;
870 for (unsigned int i = 0; i < MHSd->getNumArgs(); ++i) {
871 Init *Arg;
872 std::string ArgName;
873 Arg = MHSd->getArg(i);
874 ArgName = MHSd->getArgName(i);
875
876 // Process args
877 Init *Result = EvaluateOperation(RHSo, LHS, Arg, Type,
878 CurRec, CurMultiClass);
879 if (Result != 0) {
880 Arg = Result;
881 }
882
883 // TODO: Process arg names
884 args.push_back(std::make_pair(Arg, ArgName));
885 }
886
887 return new DagInit(Val, "", args);
888 }
889 if (MHSl) {
890 std::vector<Init *> NewOperands;
891 std::vector<Init *> NewList(MHSl->begin(), MHSl->end());
892
893 for (ListInit::iterator li = NewList.begin(),
894 liend = NewList.end();
895 li != liend;
896 ++li) {
897 Init *Item = *li;
898 NewOperands.clear();
899 for(int i = 0; i < RHSo->getNumOperands(); ++i) {
900 // First, replace the foreach variable with the list item
901 if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
902 NewOperands.push_back(Item);
Bob Wilson7248f862009-11-22 04:24:42 +0000903 } else {
David Greenee917fff2009-05-14 22:23:47 +0000904 NewOperands.push_back(RHSo->getOperand(i));
905 }
906 }
907
908 // Now run the operator and use its result as the new list item
909 OpInit *NewOp = RHSo->clone(NewOperands);
910 Init *NewItem = NewOp->Fold(CurRec, CurMultiClass);
911 if (NewItem != NewOp) {
912 *li = NewItem;
913 delete NewOp;
914 }
915 }
David Greene8618f952009-06-08 20:23:18 +0000916 return new ListInit(NewList, MHSl->getType());
David Greenee917fff2009-05-14 22:23:47 +0000917 }
918 }
919 return 0;
920}
921
David Greene98ed3c72009-05-14 21:54:42 +0000922Init *TernOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
923 switch (getOpcode()) {
924 default: assert(0 && "Unknown binop");
925 case SUBST: {
926 DefInit *LHSd = dynamic_cast<DefInit*>(LHS);
927 VarInit *LHSv = dynamic_cast<VarInit*>(LHS);
928 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
David Greene196ac3c2009-04-23 21:25:15 +0000929
David Greene98ed3c72009-05-14 21:54:42 +0000930 DefInit *MHSd = dynamic_cast<DefInit*>(MHS);
931 VarInit *MHSv = dynamic_cast<VarInit*>(MHS);
932 StringInit *MHSs = dynamic_cast<StringInit*>(MHS);
David Greene5d0c0512009-05-14 20:54:48 +0000933
David Greene98ed3c72009-05-14 21:54:42 +0000934 DefInit *RHSd = dynamic_cast<DefInit*>(RHS);
935 VarInit *RHSv = dynamic_cast<VarInit*>(RHS);
936 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
David Greene5d0c0512009-05-14 20:54:48 +0000937
David Greene98ed3c72009-05-14 21:54:42 +0000938 if ((LHSd && MHSd && RHSd)
939 || (LHSv && MHSv && RHSv)
940 || (LHSs && MHSs && RHSs)) {
941 if (RHSd) {
942 Record *Val = RHSd->getDef();
943 if (LHSd->getAsString() == RHSd->getAsString()) {
944 Val = MHSd->getDef();
945 }
946 return new DefInit(Val);
947 }
948 if (RHSv) {
949 std::string Val = RHSv->getName();
950 if (LHSv->getAsString() == RHSv->getAsString()) {
951 Val = MHSv->getName();
952 }
953 return new VarInit(Val, getType());
954 }
955 if (RHSs) {
956 std::string Val = RHSs->getValue();
David Greene5d0c0512009-05-14 20:54:48 +0000957
David Greene98ed3c72009-05-14 21:54:42 +0000958 std::string::size_type found;
David Greenedbf70742009-12-21 21:21:34 +0000959 std::string::size_type idx = 0;
David Greene98ed3c72009-05-14 21:54:42 +0000960 do {
David Greenedbf70742009-12-21 21:21:34 +0000961 found = Val.find(LHSs->getValue(), idx);
David Greene98ed3c72009-05-14 21:54:42 +0000962 if (found != std::string::npos) {
963 Val.replace(found, LHSs->getValue().size(), MHSs->getValue());
964 }
David Greenedbf70742009-12-21 21:21:34 +0000965 idx = found + MHSs->getValue().size();
David Greene98ed3c72009-05-14 21:54:42 +0000966 } while (found != std::string::npos);
David Greene5d0c0512009-05-14 20:54:48 +0000967
David Greene98ed3c72009-05-14 21:54:42 +0000968 return new StringInit(Val);
969 }
970 }
971 break;
Bob Wilson7248f862009-11-22 04:24:42 +0000972 }
David Greene5d0c0512009-05-14 20:54:48 +0000973
David Greene98ed3c72009-05-14 21:54:42 +0000974 case FOREACH: {
David Greenee917fff2009-05-14 22:23:47 +0000975 Init *Result = ForeachHelper(LHS, MHS, RHS, getType(),
976 CurRec, CurMultiClass);
977 if (Result != 0) {
978 return Result;
David Greene98ed3c72009-05-14 21:54:42 +0000979 }
980 break;
981 }
David Greene3587eed2009-05-14 23:26:46 +0000982
983 case IF: {
Bruno Cardoso Lopes7f4235d2010-06-17 01:50:39 +0000984 IntInit *LHSi = dynamic_cast<IntInit*>(LHS);
985 if (Init *I = LHS->convertInitializerTo(new IntRecTy()))
986 LHSi = dynamic_cast<IntInit*>(I);
David Greene3587eed2009-05-14 23:26:46 +0000987 if (LHSi) {
988 if (LHSi->getValue()) {
989 return MHS;
Bob Wilson7248f862009-11-22 04:24:42 +0000990 } else {
David Greene3587eed2009-05-14 23:26:46 +0000991 return RHS;
992 }
993 }
994 break;
995 }
David Greene98ed3c72009-05-14 21:54:42 +0000996 }
David Greene5d0c0512009-05-14 20:54:48 +0000997
David Greene98ed3c72009-05-14 21:54:42 +0000998 return this;
999}
David Greene5d0c0512009-05-14 20:54:48 +00001000
David Greene98ed3c72009-05-14 21:54:42 +00001001Init *TernOpInit::resolveReferences(Record &R, const RecordVal *RV) {
1002 Init *lhs = LHS->resolveReferences(R, RV);
David Greeneb0354452009-06-08 19:16:56 +00001003
1004 if (Opc == IF && lhs != LHS) {
Bruno Cardoso Lopes7f4235d2010-06-17 01:50:39 +00001005 IntInit *Value = dynamic_cast<IntInit*>(lhs);
1006 if (Init *I = lhs->convertInitializerTo(new IntRecTy()))
1007 Value = dynamic_cast<IntInit*>(I);
David Greeneb0354452009-06-08 19:16:56 +00001008 if (Value != 0) {
1009 // Short-circuit
1010 if (Value->getValue()) {
1011 Init *mhs = MHS->resolveReferences(R, RV);
Bob Wilson7248f862009-11-22 04:24:42 +00001012 return (new TernOpInit(getOpcode(), lhs, mhs,
1013 RHS, getType()))->Fold(&R, 0);
1014 } else {
David Greeneb0354452009-06-08 19:16:56 +00001015 Init *rhs = RHS->resolveReferences(R, RV);
Bob Wilson7248f862009-11-22 04:24:42 +00001016 return (new TernOpInit(getOpcode(), lhs, MHS,
1017 rhs, getType()))->Fold(&R, 0);
David Greeneb0354452009-06-08 19:16:56 +00001018 }
1019 }
1020 }
Bob Wilson7248f862009-11-22 04:24:42 +00001021
David Greene98ed3c72009-05-14 21:54:42 +00001022 Init *mhs = MHS->resolveReferences(R, RV);
1023 Init *rhs = RHS->resolveReferences(R, RV);
David Greeneb0354452009-06-08 19:16:56 +00001024
David Greene98ed3c72009-05-14 21:54:42 +00001025 if (LHS != lhs || MHS != mhs || RHS != rhs)
1026 return (new TernOpInit(getOpcode(), lhs, mhs, rhs, getType()))->Fold(&R, 0);
1027 return Fold(&R, 0);
1028}
David Greene196ac3c2009-04-23 21:25:15 +00001029
David Greene98ed3c72009-05-14 21:54:42 +00001030std::string TernOpInit::getAsString() const {
1031 std::string Result;
1032 switch (Opc) {
1033 case SUBST: Result = "!subst"; break;
Bob Wilson7248f862009-11-22 04:24:42 +00001034 case FOREACH: Result = "!foreach"; break;
1035 case IF: Result = "!if"; break;
David Greene98ed3c72009-05-14 21:54:42 +00001036 }
Bob Wilson7248f862009-11-22 04:24:42 +00001037 return Result + "(" + LHS->getAsString() + ", " + MHS->getAsString() + ", "
David Greene98ed3c72009-05-14 21:54:42 +00001038 + RHS->getAsString() + ")";
1039}
David Greene196ac3c2009-04-23 21:25:15 +00001040
Chris Lattner577fc3f2004-07-27 01:01:21 +00001041Init *TypedInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001042 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
1043 if (T == 0) return 0; // Cannot subscript a non-bits variable...
1044 unsigned NumBits = T->getNumBits();
1045
1046 BitsInit *BI = new BitsInit(Bits.size());
1047 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
1048 if (Bits[i] >= NumBits) {
1049 delete BI;
1050 return 0;
1051 }
1052 BI->setBit(i, new VarBitInit(this, Bits[i]));
1053 }
1054 return BI;
1055}
1056
Chris Lattner577fc3f2004-07-27 01:01:21 +00001057Init *TypedInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
1058 ListRecTy *T = dynamic_cast<ListRecTy*>(getType());
1059 if (T == 0) return 0; // Cannot subscript a non-list variable...
1060
1061 if (Elements.size() == 1)
1062 return new VarListElementInit(this, Elements[0]);
1063
1064 std::vector<Init*> ListInits;
1065 ListInits.reserve(Elements.size());
1066 for (unsigned i = 0, e = Elements.size(); i != e; ++i)
1067 ListInits.push_back(new VarListElementInit(this, Elements[i]));
David Greene8618f952009-06-08 20:23:18 +00001068 return new ListInit(ListInits, T);
Chris Lattner577fc3f2004-07-27 01:01:21 +00001069}
1070
1071
Chris Lattneref943742005-04-19 03:36:21 +00001072Init *VarInit::resolveBitReference(Record &R, const RecordVal *IRV,
1073 unsigned Bit) {
1074 if (R.isTemplateArg(getName())) return 0;
1075 if (IRV && IRV->getName() != getName()) return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001076
1077 RecordVal *RV = R.getValue(getName());
Bob Wilson159905a2009-11-21 22:44:20 +00001078 assert(RV && "Reference to a non-existent variable?");
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001079 assert(dynamic_cast<BitsInit*>(RV->getValue()));
1080 BitsInit *BI = (BitsInit*)RV->getValue();
Misha Brukman650ba8e2005-04-22 00:00:37 +00001081
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001082 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
1083 Init *B = BI->getBit(Bit);
1084
Bob Wilson67e6cab2009-11-22 03:58:57 +00001085 // If the bit is set to some value, or if we are resolving a reference to a
1086 // specific variable and that variable is explicitly unset, then replace the
1087 // VarBitInit with it.
1088 if (IRV || !dynamic_cast<UnsetInit*>(B))
1089 return B;
Chris Lattner577fc3f2004-07-27 01:01:21 +00001090 return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001091}
1092
Chris Lattneref943742005-04-19 03:36:21 +00001093Init *VarInit::resolveListElementReference(Record &R, const RecordVal *IRV,
1094 unsigned Elt) {
1095 if (R.isTemplateArg(getName())) return 0;
1096 if (IRV && IRV->getName() != getName()) return 0;
Chris Lattner577fc3f2004-07-27 01:01:21 +00001097
1098 RecordVal *RV = R.getValue(getName());
Bob Wilson159905a2009-11-21 22:44:20 +00001099 assert(RV && "Reference to a non-existent variable?");
Chris Lattner577fc3f2004-07-27 01:01:21 +00001100 ListInit *LI = dynamic_cast<ListInit*>(RV->getValue());
David Greene9d3febe2009-05-14 20:38:52 +00001101 if (!LI) {
1102 VarInit *VI = dynamic_cast<VarInit*>(RV->getValue());
1103 assert(VI && "Invalid list element!");
1104 return new VarListElementInit(VI, Elt);
1105 }
Bob Wilson7248f862009-11-22 04:24:42 +00001106
Chris Lattner577fc3f2004-07-27 01:01:21 +00001107 if (Elt >= LI->getSize())
1108 return 0; // Out of range reference.
1109 Init *E = LI->getElement(Elt);
Bob Wilson67e6cab2009-11-22 03:58:57 +00001110 // If the element is set to some value, or if we are resolving a reference
1111 // to a specific variable and that variable is explicitly unset, then
1112 // replace the VarListElementInit with it.
1113 if (IRV || !dynamic_cast<UnsetInit*>(E))
1114 return E;
Chris Lattner577fc3f2004-07-27 01:01:21 +00001115 return 0;
1116}
1117
1118
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001119RecTy *VarInit::getFieldType(const std::string &FieldName) const {
1120 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
1121 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
1122 return RV->getType();
1123 return 0;
1124}
1125
Jakob Stoklund Olesen0e457622010-03-25 06:23:34 +00001126Init *VarInit::getFieldInit(Record &R, const RecordVal *RV,
1127 const std::string &FieldName) const {
Reid Spencer2a826862006-11-02 20:46:16 +00001128 if (dynamic_cast<RecordRecTy*>(getType()))
Jakob Stoklund Olesen0e457622010-03-25 06:23:34 +00001129 if (const RecordVal *Val = R.getValue(VarName)) {
1130 if (RV != Val && (RV || dynamic_cast<UnsetInit*>(Val->getValue())))
1131 return 0;
1132 Init *TheInit = Val->getValue();
Chris Lattnerd959ab92004-02-28 16:31:53 +00001133 assert(TheInit != this && "Infinite loop detected!");
Jakob Stoklund Olesen0e457622010-03-25 06:23:34 +00001134 if (Init *I = TheInit->getFieldInit(R, RV, FieldName))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001135 return I;
1136 else
1137 return 0;
Chris Lattnerd959ab92004-02-28 16:31:53 +00001138 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001139 return 0;
1140}
1141
1142/// resolveReferences - This method is used by classes that refer to other
Bob Wilson159905a2009-11-21 22:44:20 +00001143/// variables which may not be defined at the time the expression is formed.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001144/// If a value is set for the variable later, this method will be called on
1145/// users of the value to allow the value to propagate out.
1146///
Chris Lattneref943742005-04-19 03:36:21 +00001147Init *VarInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001148 if (RecordVal *Val = R.getValue(VarName))
Chris Lattneref943742005-04-19 03:36:21 +00001149 if (RV == Val || (RV == 0 && !dynamic_cast<UnsetInit*>(Val->getValue())))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001150 return Val->getValue();
1151 return this;
1152}
Misha Brukman650ba8e2005-04-22 00:00:37 +00001153
Chris Lattner695506c2007-11-22 21:05:25 +00001154std::string VarBitInit::getAsString() const {
1155 return TI->getAsString() + "{" + utostr(Bit) + "}";
1156}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001157
Chris Lattneref943742005-04-19 03:36:21 +00001158Init *VarBitInit::resolveReferences(Record &R, const RecordVal *RV) {
1159 if (Init *I = getVariable()->resolveBitReference(R, RV, getBitNum()))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001160 return I;
1161 return this;
1162}
1163
Chris Lattner695506c2007-11-22 21:05:25 +00001164std::string VarListElementInit::getAsString() const {
1165 return TI->getAsString() + "[" + utostr(Element) + "]";
1166}
1167
Chris Lattneref943742005-04-19 03:36:21 +00001168Init *VarListElementInit::resolveReferences(Record &R, const RecordVal *RV) {
1169 if (Init *I = getVariable()->resolveListElementReference(R, RV,
1170 getElementNum()))
Chris Lattner577fc3f2004-07-27 01:01:21 +00001171 return I;
1172 return this;
1173}
1174
Chris Lattneref943742005-04-19 03:36:21 +00001175Init *VarListElementInit::resolveBitReference(Record &R, const RecordVal *RV,
1176 unsigned Bit) {
Chris Lattner577fc3f2004-07-27 01:01:21 +00001177 // FIXME: This should be implemented, to support references like:
1178 // bit B = AA[0]{1};
1179 return 0;
1180}
1181
Chris Lattneref943742005-04-19 03:36:21 +00001182Init *VarListElementInit::
1183resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) {
Chris Lattner577fc3f2004-07-27 01:01:21 +00001184 // FIXME: This should be implemented, to support references like:
1185 // int B = AA[0][1];
1186 return 0;
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
Jakob Stoklund Olesen0e457622010-03-25 06:23:34 +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
Chris Lattneref943742005-04-19 03:36:21 +00001205Init *FieldInit::resolveBitReference(Record &R, const RecordVal *RV,
1206 unsigned Bit) {
Jakob Stoklund Olesen0e457622010-03-25 06:23:34 +00001207 if (Init *BitsVal = Rec->getFieldInit(R, RV, FieldName))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001208 if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
1209 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
1210 Init *B = BI->getBit(Bit);
Misha Brukman650ba8e2005-04-22 00:00:37 +00001211
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001212 if (dynamic_cast<BitInit*>(B)) // If the bit is set...
1213 return B; // Replace the VarBitInit with it.
1214 }
Chris Lattner577fc3f2004-07-27 01:01:21 +00001215 return 0;
1216}
1217
Chris Lattneref943742005-04-19 03:36:21 +00001218Init *FieldInit::resolveListElementReference(Record &R, const RecordVal *RV,
1219 unsigned Elt) {
Jakob Stoklund Olesen0e457622010-03-25 06:23:34 +00001220 if (Init *ListVal = Rec->getFieldInit(R, RV, FieldName))
Chris Lattner577fc3f2004-07-27 01:01:21 +00001221 if (ListInit *LI = dynamic_cast<ListInit*>(ListVal)) {
1222 if (Elt >= LI->getSize()) return 0;
1223 Init *E = LI->getElement(Elt);
1224
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.
1228 if (RV || !dynamic_cast<UnsetInit*>(E))
1229 return E;
Chris Lattner577fc3f2004-07-27 01:01:21 +00001230 }
1231 return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001232}
1233
Chris Lattneref943742005-04-19 03:36:21 +00001234Init *FieldInit::resolveReferences(Record &R, const RecordVal *RV) {
1235 Init *NewRec = RV ? Rec->resolveReferences(R, RV) : Rec;
1236
Jakob Stoklund Olesen0e457622010-03-25 06:23:34 +00001237 Init *BitsVal = NewRec->getFieldInit(R, RV, FieldName);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001238 if (BitsVal) {
Chris Lattneref943742005-04-19 03:36:21 +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) {
Chris Lattneref943742005-04-19 03:36:21 +00001244 return new FieldInit(NewRec, FieldName);
1245 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001246 return this;
1247}
1248
Chris Lattner0d3ef402006-01-31 06:02:35 +00001249Init *DagInit::resolveReferences(Record &R, const RecordVal *RV) {
1250 std::vector<Init*> NewArgs;
1251 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
Chris Lattnerb59cf3c2006-03-30 22:50:40 +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)
Nate Begemandbe3f772009-03-19 05:21:56 +00001257 return new DagInit(Op, "", 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) {
1285 Value = Ty->convertValue(new UnsetInit());
1286 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
Chris Lattnerac284252005-08-19 17:58:11 +00001303void Record::setName(const std::string &Name) {
1304 if (Records.getDef(getName()) == this) {
1305 Records.removeDef(getName());
1306 this->Name = Name;
1307 Records.addDef(this);
1308 } else {
1309 Records.removeClass(getName());
1310 this->Name = Name;
1311 Records.addClass(this);
1312 }
1313}
1314
Chris Lattneref943742005-04-19 03:36:21 +00001315/// resolveReferencesTo - If anything in this record refers to RV, replace the
1316/// reference to RV with the RHS of RV. If RV is null, we resolve all possible
1317/// references.
1318void Record::resolveReferencesTo(const RecordVal *RV) {
1319 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
1320 if (Init *V = Values[i].getValue())
1321 Values[i].setValue(V->resolveReferences(*this, RV));
1322 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001323}
1324
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001325void Record::dump() const { errs() << *this; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001326
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001327raw_ostream &llvm::operator<<(raw_ostream &OS, const Record &R) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001328 OS << R.getName();
1329
1330 const std::vector<std::string> &TArgs = R.getTemplateArgs();
1331 if (!TArgs.empty()) {
1332 OS << "<";
1333 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1334 if (i) OS << ", ";
1335 const RecordVal *RV = R.getValue(TArgs[i]);
1336 assert(RV && "Template argument record not found??");
1337 RV->print(OS, false);
1338 }
1339 OS << ">";
1340 }
1341
1342 OS << " {";
1343 const std::vector<Record*> &SC = R.getSuperClasses();
1344 if (!SC.empty()) {
1345 OS << "\t//";
1346 for (unsigned i = 0, e = SC.size(); i != e; ++i)
1347 OS << " " << SC[i]->getName();
1348 }
1349 OS << "\n";
1350
1351 const std::vector<RecordVal> &Vals = R.getValues();
1352 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
1353 if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
1354 OS << Vals[i];
1355 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
1356 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
1357 OS << Vals[i];
1358
1359 return OS << "}\n";
1360}
1361
1362/// getValueInit - Return the initializer for a value with the specified name,
1363/// or throw an exception if the field does not exist.
1364///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001365Init *Record::getValueInit(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001366 const RecordVal *R = getValue(FieldName);
1367 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001368 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001369 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001370 return R->getValue();
1371}
1372
1373
1374/// getValueAsString - This method looks up the specified field and returns its
1375/// value as a string, throwing an exception if the field does not exist or if
1376/// the value is not a string.
1377///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001378std::string Record::getValueAsString(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001379 const RecordVal *R = getValue(FieldName);
1380 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001381 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001382 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001383
1384 if (const StringInit *SI = dynamic_cast<const StringInit*>(R->getValue()))
1385 return SI->getValue();
Chris Lattner42bb0c12009-09-18 18:31:37 +00001386 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001387 "' does not have a string initializer!";
1388}
1389
1390/// getValueAsBitsInit - This method looks up the specified field and returns
1391/// its value as a BitsInit, throwing an exception if the field does not exist
1392/// or if the value is not the right type.
1393///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001394BitsInit *Record::getValueAsBitsInit(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001395 const RecordVal *R = getValue(FieldName);
1396 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001397 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001398 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001399
1400 if (BitsInit *BI = dynamic_cast<BitsInit*>(R->getValue()))
1401 return BI;
Chris Lattner42bb0c12009-09-18 18:31:37 +00001402 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001403 "' does not have a BitsInit initializer!";
1404}
1405
1406/// getValueAsListInit - This method looks up the specified field and returns
1407/// its value as a ListInit, throwing an exception if the field does not exist
1408/// or if the value is not the right type.
1409///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001410ListInit *Record::getValueAsListInit(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001411 const RecordVal *R = getValue(FieldName);
1412 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001413 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001414 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001415
1416 if (ListInit *LI = dynamic_cast<ListInit*>(R->getValue()))
1417 return LI;
Chris Lattner42bb0c12009-09-18 18:31:37 +00001418 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001419 "' does not have a list initializer!";
1420}
1421
Chris Lattner7ad0bed2005-10-28 22:49:02 +00001422/// getValueAsListOfDefs - This method looks up the specified field and returns
Jim Laskeyb04feb62005-10-28 21:46:31 +00001423/// its value as a vector of records, throwing an exception if the field does
1424/// not exist or if the value is not the right type.
1425///
Bob Wilson7248f862009-11-22 04:24:42 +00001426std::vector<Record*>
Chris Lattner42bb0c12009-09-18 18:31:37 +00001427Record::getValueAsListOfDefs(StringRef FieldName) const {
Jim Laskeyb04feb62005-10-28 21:46:31 +00001428 ListInit *List = getValueAsListInit(FieldName);
1429 std::vector<Record*> Defs;
1430 for (unsigned i = 0; i < List->getSize(); i++) {
1431 if (DefInit *DI = dynamic_cast<DefInit*>(List->getElement(i))) {
1432 Defs.push_back(DI->getDef());
1433 } else {
Chris Lattner42bb0c12009-09-18 18:31:37 +00001434 throw "Record `" + getName() + "', field `" + FieldName.str() +
Jim Laskeyb04feb62005-10-28 21:46:31 +00001435 "' list is not entirely DefInit!";
1436 }
1437 }
1438 return Defs;
1439}
1440
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001441/// getValueAsInt - This method looks up the specified field and returns its
Dan Gohmanca0546f2008-10-17 01:33:43 +00001442/// value as an int64_t, throwing an exception if the field does not exist or if
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001443/// the value is not the right type.
1444///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001445int64_t Record::getValueAsInt(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001446 const RecordVal *R = getValue(FieldName);
1447 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001448 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001449 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001450
1451 if (IntInit *II = dynamic_cast<IntInit*>(R->getValue()))
1452 return II->getValue();
Chris Lattner42bb0c12009-09-18 18:31:37 +00001453 throw "Record `" + getName() + "', field `" + FieldName.str() +
Nate Begemanf621b332005-11-30 18:37:14 +00001454 "' does not have an int initializer!";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001455}
1456
Anton Korobeynikova468a112007-11-11 11:19:37 +00001457/// getValueAsListOfInts - This method looks up the specified field and returns
1458/// its value as a vector of integers, throwing an exception if the field does
1459/// not exist or if the value is not the right type.
1460///
Bob Wilson7248f862009-11-22 04:24:42 +00001461std::vector<int64_t>
Chris Lattner42bb0c12009-09-18 18:31:37 +00001462Record::getValueAsListOfInts(StringRef FieldName) const {
Anton Korobeynikova468a112007-11-11 11:19:37 +00001463 ListInit *List = getValueAsListInit(FieldName);
Dan Gohmanca0546f2008-10-17 01:33:43 +00001464 std::vector<int64_t> Ints;
Anton Korobeynikova468a112007-11-11 11:19:37 +00001465 for (unsigned i = 0; i < List->getSize(); i++) {
1466 if (IntInit *II = dynamic_cast<IntInit*>(List->getElement(i))) {
1467 Ints.push_back(II->getValue());
1468 } else {
Chris Lattner42bb0c12009-09-18 18:31:37 +00001469 throw "Record `" + getName() + "', field `" + FieldName.str() +
Anton Korobeynikova468a112007-11-11 11:19:37 +00001470 "' does not have a list of ints initializer!";
1471 }
1472 }
1473 return Ints;
1474}
1475
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001476/// getValueAsDef - This method looks up the specified field and returns its
1477/// value as a Record, throwing an exception if the field does not exist or if
1478/// the value is not the right type.
1479///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001480Record *Record::getValueAsDef(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001481 const RecordVal *R = getValue(FieldName);
1482 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001483 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001484 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001485
1486 if (DefInit *DI = dynamic_cast<DefInit*>(R->getValue()))
1487 return DI->getDef();
Chris Lattner42bb0c12009-09-18 18:31:37 +00001488 throw "Record `" + getName() + "', field `" + FieldName.str() +
Nate Begemanf621b332005-11-30 18:37:14 +00001489 "' does not have a def initializer!";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001490}
1491
1492/// getValueAsBit - This method looks up the specified field and returns its
1493/// value as a bit, throwing an exception if the field does not exist or if
1494/// the value is not the right type.
1495///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001496bool Record::getValueAsBit(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001497 const RecordVal *R = getValue(FieldName);
1498 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001499 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001500 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001501
1502 if (BitInit *BI = dynamic_cast<BitInit*>(R->getValue()))
1503 return BI->getValue();
Chris Lattner42bb0c12009-09-18 18:31:37 +00001504 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001505 "' does not have a bit initializer!";
1506}
1507
1508/// getValueAsDag - This method looks up the specified field and returns its
1509/// value as an Dag, throwing an exception if the field does not exist or if
1510/// the value is not the right type.
1511///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001512DagInit *Record::getValueAsDag(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001513 const RecordVal *R = getValue(FieldName);
1514 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001515 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001516 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001517
1518 if (DagInit *DI = dynamic_cast<DagInit*>(R->getValue()))
1519 return DI;
Chris Lattner42bb0c12009-09-18 18:31:37 +00001520 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001521 "' does not have a dag initializer!";
1522}
1523
Chris Lattner42bb0c12009-09-18 18:31:37 +00001524std::string Record::getValueAsCode(StringRef FieldName) const {
Chris Lattnerae939eb2005-09-13 21:44:28 +00001525 const RecordVal *R = getValue(FieldName);
1526 if (R == 0 || R->getValue() == 0)
1527 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001528 FieldName.str() + "'!\n";
Bob Wilson7248f862009-11-22 04:24:42 +00001529
Chris Lattnerae939eb2005-09-13 21:44:28 +00001530 if (const CodeInit *CI = dynamic_cast<const CodeInit*>(R->getValue()))
1531 return CI->getValue();
Chris Lattner42bb0c12009-09-18 18:31:37 +00001532 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerae939eb2005-09-13 21:44:28 +00001533 "' does not have a code initializer!";
1534}
1535
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001536
David Greene7049e792009-04-24 16:55:41 +00001537void MultiClass::dump() const {
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001538 errs() << "Record:\n";
David Greene7049e792009-04-24 16:55:41 +00001539 Rec.dump();
Bob Wilson7248f862009-11-22 04:24:42 +00001540
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001541 errs() << "Defs:\n";
David Greene7049e792009-04-24 16:55:41 +00001542 for (RecordVector::const_iterator r = DefPrototypes.begin(),
1543 rend = DefPrototypes.end();
1544 r != rend;
1545 ++r) {
1546 (*r)->dump();
1547 }
1548}
1549
1550
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001551void RecordKeeper::dump() const { errs() << *this; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001552
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001553raw_ostream &llvm::operator<<(raw_ostream &OS, const RecordKeeper &RK) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001554 OS << "------------- Classes -----------------\n";
1555 const std::map<std::string, Record*> &Classes = RK.getClasses();
1556 for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001557 E = Classes.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001558 OS << "class " << *I->second;
Misha Brukman650ba8e2005-04-22 00:00:37 +00001559
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001560 OS << "------------- Defs -----------------\n";
1561 const std::map<std::string, Record*> &Defs = RK.getDefs();
1562 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001563 E = Defs.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001564 OS << "def " << *I->second;
1565 return OS;
1566}
1567
1568
1569/// getAllDerivedDefinitions - This method returns all concrete definitions
1570/// that derive from the specified class name. If a class with the specified
1571/// name does not exist, an error is printed and true is returned.
1572std::vector<Record*>
1573RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
1574 Record *Class = Records.getClass(ClassName);
1575 if (!Class)
Misha Brukman41f9f292004-10-08 14:59:05 +00001576 throw "ERROR: Couldn't find the `" + ClassName + "' class!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001577
1578 std::vector<Record*> Defs;
1579 for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(),
1580 E = getDefs().end(); I != E; ++I)
1581 if (I->second->isSubClassOf(Class))
1582 Defs.push_back(I->second);
1583
1584 return Defs;
1585}
Brian Gaeke960707c2003-11-11 22:41:34 +00001586