blob: fbe120759d3ce2f88a80e82fe3a035b019524a90 [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
David Greene8618f952009-06-08 20:23:18 +0000277/// resolveTypes - Find a common type that T1 and T2 convert to.
278/// 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
287 const std::vector<Record *> &T1SuperClasses = RecTy1->getRecord()->getSuperClasses();
288 for(std::vector<Record *>::const_iterator i = T1SuperClasses.begin(),
289 iend = T1SuperClasses.end();
290 i != iend;
291 ++i) {
292 RecordRecTy *SuperRecTy1 = new RecordRecTy(*i);
293 RecTy *NewType1 = resolveTypes(SuperRecTy1, T2);
294 if (NewType1 != 0) {
295 if (NewType1 != SuperRecTy1) {
296 delete SuperRecTy1;
297 }
298 return NewType1;
299 }
300 }
301 }
302 RecordRecTy *RecTy2 = dynamic_cast<RecordRecTy*>(T2);
303 if (RecTy2) {
304 // See if T1 inherits from a type T2 also inherits from
305 const std::vector<Record *> &T2SuperClasses = RecTy2->getRecord()->getSuperClasses();
306 for(std::vector<Record *>::const_iterator i = T2SuperClasses.begin(),
307 iend = T2SuperClasses.end();
308 i != iend;
309 ++i) {
310 RecordRecTy *SuperRecTy2 = new RecordRecTy(*i);
311 RecTy *NewType2 = resolveTypes(T1, SuperRecTy2);
312 if (NewType2 != 0) {
313 if (NewType2 != SuperRecTy2) {
314 delete SuperRecTy2;
315 }
316 return NewType2;
317 }
318 }
319 }
320 return 0;
321 }
322 return T2;
323 }
324 return T1;
325}
326
327
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000328//===----------------------------------------------------------------------===//
329// Initializer implementations
330//===----------------------------------------------------------------------===//
331
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000332void Init::dump() const { return print(errs()); }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000333
334Init *BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
335 BitsInit *BI = new BitsInit(Bits.size());
336 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
337 if (Bits[i] >= getNumBits()) {
338 delete BI;
339 return 0;
340 }
341 BI->setBit(i, getBit(Bits[i]));
342 }
343 return BI;
344}
345
Chris Lattner695506c2007-11-22 21:05:25 +0000346std::string BitsInit::getAsString() const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000347 //if (!printInHex(OS)) return;
348 //if (!printAsVariable(OS)) return;
349 //if (!printAsUnset(OS)) return;
350
Chris Lattner695506c2007-11-22 21:05:25 +0000351 std::string Result = "{ ";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000352 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
Chris Lattner695506c2007-11-22 21:05:25 +0000353 if (i) Result += ", ";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000354 if (Init *Bit = getBit(e-i-1))
Chris Lattner695506c2007-11-22 21:05:25 +0000355 Result += Bit->getAsString();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000356 else
Chris Lattner695506c2007-11-22 21:05:25 +0000357 Result += "*";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000358 }
Chris Lattner695506c2007-11-22 21:05:25 +0000359 return Result + " }";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000360}
361
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000362bool BitsInit::printInHex(raw_ostream &OS) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000363 // First, attempt to convert the value into an integer value...
Dan Gohmanca0546f2008-10-17 01:33:43 +0000364 int64_t Result = 0;
Misha Brukman650ba8e2005-04-22 00:00:37 +0000365 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000366 if (BitInit *Bit = dynamic_cast<BitInit*>(getBit(i))) {
367 Result |= Bit->getValue() << i;
368 } else {
369 return true;
370 }
371
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000372 OS << format("0x%x", Result);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000373 return false;
374}
375
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000376bool BitsInit::printAsVariable(raw_ostream &OS) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000377 // Get the variable that we may be set equal to...
378 assert(getNumBits() != 0);
379 VarBitInit *FirstBit = dynamic_cast<VarBitInit*>(getBit(0));
380 if (FirstBit == 0) return true;
381 TypedInit *Var = FirstBit->getVariable();
382
383 // Check to make sure the types are compatible.
384 BitsRecTy *Ty = dynamic_cast<BitsRecTy*>(FirstBit->getVariable()->getType());
385 if (Ty == 0) return true;
386 if (Ty->getNumBits() != getNumBits()) return true; // Incompatible types!
387
388 // Check to make sure all bits are referring to the right bits in the variable
389 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
390 VarBitInit *Bit = dynamic_cast<VarBitInit*>(getBit(i));
391 if (Bit == 0 || Bit->getVariable() != Var || Bit->getBitNum() != i)
392 return true;
393 }
394
395 Var->print(OS);
396 return false;
397}
398
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000399bool BitsInit::printAsUnset(raw_ostream &OS) const {
Misha Brukman650ba8e2005-04-22 00:00:37 +0000400 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000401 if (!dynamic_cast<UnsetInit*>(getBit(i)))
402 return true;
403 OS << "?";
404 return false;
405}
406
407// resolveReferences - If there are any field references that refer to fields
408// that have been filled in, we can propagate the values now.
409//
Chris Lattneref943742005-04-19 03:36:21 +0000410Init *BitsInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000411 bool Changed = false;
412 BitsInit *New = new BitsInit(getNumBits());
413
414 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
415 Init *B;
416 Init *CurBit = getBit(i);
417
418 do {
419 B = CurBit;
Chris Lattneref943742005-04-19 03:36:21 +0000420 CurBit = CurBit->resolveReferences(R, RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000421 Changed |= B != CurBit;
422 } while (B != CurBit);
423 New->setBit(i, CurBit);
424 }
425
426 if (Changed)
427 return New;
428 delete New;
429 return this;
430}
431
Chris Lattner695506c2007-11-22 21:05:25 +0000432std::string IntInit::getAsString() const {
433 return itostr(Value);
434}
435
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000436Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
437 BitsInit *BI = new BitsInit(Bits.size());
438
439 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
Dan Gohmanca0546f2008-10-17 01:33:43 +0000440 if (Bits[i] >= 64) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000441 delete BI;
442 return 0;
443 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000444 BI->setBit(i, new BitInit(Value & (INT64_C(1) << Bits[i])));
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000445 }
446 return BI;
447}
448
Chris Lattner8bf9e062004-07-26 23:21:34 +0000449Init *ListInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
450 std::vector<Init*> Vals;
451 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
452 if (Elements[i] >= getSize())
453 return 0;
454 Vals.push_back(getElement(Elements[i]));
455 }
David Greene8618f952009-06-08 20:23:18 +0000456 return new ListInit(Vals, getType());
Chris Lattner8bf9e062004-07-26 23:21:34 +0000457}
458
Chris Lattnercbebe462007-02-27 22:08:27 +0000459Record *ListInit::getElementAsRecord(unsigned i) const {
460 assert(i < Values.size() && "List element index out of range!");
461 DefInit *DI = dynamic_cast<DefInit*>(Values[i]);
462 if (DI == 0) throw "Expected record in list!";
463 return DI->getDef();
464}
465
Chris Lattneref943742005-04-19 03:36:21 +0000466Init *ListInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattner577fc3f2004-07-27 01:01:21 +0000467 std::vector<Init*> Resolved;
468 Resolved.reserve(getSize());
469 bool Changed = false;
470
471 for (unsigned i = 0, e = getSize(); i != e; ++i) {
472 Init *E;
473 Init *CurElt = getElement(i);
474
475 do {
476 E = CurElt;
Chris Lattneref943742005-04-19 03:36:21 +0000477 CurElt = CurElt->resolveReferences(R, RV);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000478 Changed |= E != CurElt;
479 } while (E != CurElt);
480 Resolved.push_back(E);
481 }
482
483 if (Changed)
David Greene8618f952009-06-08 20:23:18 +0000484 return new ListInit(Resolved, getType());
Chris Lattner577fc3f2004-07-27 01:01:21 +0000485 return this;
486}
487
David Greene8618f952009-06-08 20:23:18 +0000488Init *ListInit::resolveListElementReference(Record &R, const RecordVal *IRV,
489 unsigned Elt) {
490 if (Elt >= getSize())
491 return 0; // Out of range reference.
492 Init *E = getElement(Elt);
Bob Wilson67e6cab2009-11-22 03:58:57 +0000493 // If the element is set to some value, or if we are resolving a reference
494 // to a specific variable and that variable is explicitly unset, then
495 // replace the VarListElementInit with it.
496 if (IRV || !dynamic_cast<UnsetInit*>(E))
497 return E;
David Greene8618f952009-06-08 20:23:18 +0000498 return 0;
499}
500
Chris Lattner695506c2007-11-22 21:05:25 +0000501std::string ListInit::getAsString() const {
502 std::string Result = "[";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000503 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
Chris Lattner695506c2007-11-22 21:05:25 +0000504 if (i) Result += ", ";
505 Result += Values[i]->getAsString();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000506 }
Chris Lattner695506c2007-11-22 21:05:25 +0000507 return Result + "]";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000508}
509
David Greene5d0c0512009-05-14 20:54:48 +0000510Init *OpInit::resolveBitReference(Record &R, const RecordVal *IRV,
511 unsigned Bit) {
512 Init *Folded = Fold(&R, 0);
513
514 if (Folded != this) {
515 TypedInit *Typed = dynamic_cast<TypedInit *>(Folded);
516 if (Typed) {
517 return Typed->resolveBitReference(R, IRV, Bit);
518 }
519 }
520
521 return 0;
522}
523
524Init *OpInit::resolveListElementReference(Record &R, const RecordVal *IRV,
525 unsigned Elt) {
526 Init *Folded = Fold(&R, 0);
527
528 if (Folded != this) {
529 TypedInit *Typed = dynamic_cast<TypedInit *>(Folded);
530 if (Typed) {
531 return Typed->resolveListElementReference(R, IRV, Elt);
532 }
533 }
534
535 return 0;
536}
537
David Greenee8f3b272009-05-14 21:22:49 +0000538Init *UnOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
539 switch (getOpcode()) {
540 default: assert(0 && "Unknown unop");
541 case CAST: {
David Greeneefa19612009-06-29 20:05:29 +0000542 if (getType()->getAsString() == "string") {
543 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
544 if (LHSs) {
545 return LHSs;
546 }
David Greene5d0c0512009-05-14 20:54:48 +0000547
David Greeneefa19612009-06-29 20:05:29 +0000548 DefInit *LHSd = dynamic_cast<DefInit*>(LHS);
549 if (LHSd) {
550 return new StringInit(LHSd->getDef()->getName());
551 }
David Greeneefa19612009-06-29 20:05:29 +0000552 }
553 else {
554 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
555 if (LHSs) {
556 std::string Name = LHSs->getValue();
557
558 // From TGParser::ParseIDValue
559 if (CurRec) {
560 if (const RecordVal *RV = CurRec->getValue(Name)) {
561 if (RV->getType() != getType()) {
562 throw "type mismatch in nameconcat";
563 }
564 return new VarInit(Name, RV->getType());
David Greenee8f3b272009-05-14 21:22:49 +0000565 }
David Greeneefa19612009-06-29 20:05:29 +0000566
567 std::string TemplateArgName = CurRec->getName()+":"+Name;
568 if (CurRec->isTemplateArg(TemplateArgName)) {
569 const RecordVal *RV = CurRec->getValue(TemplateArgName);
570 assert(RV && "Template arg doesn't exist??");
571
572 if (RV->getType() != getType()) {
573 throw "type mismatch in nameconcat";
574 }
575
576 return new VarInit(TemplateArgName, RV->getType());
577 }
578 }
579
580 if (CurMultiClass) {
581 std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
582 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
583 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
584 assert(RV && "Template arg doesn't exist??");
585
586 if (RV->getType() != getType()) {
587 throw "type mismatch in nameconcat";
588 }
589
590 return new VarInit(MCName, RV->getType());
591 }
David Greenee8f3b272009-05-14 21:22:49 +0000592 }
David Greene5d0c0512009-05-14 20:54:48 +0000593
David Greeneefa19612009-06-29 20:05:29 +0000594 if (Record *D = Records.getDef(Name))
595 return new DefInit(D);
David Greene5d0c0512009-05-14 20:54:48 +0000596
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000597 errs() << "Variable not defined: '" + Name + "'\n";
David Greeneefa19612009-06-29 20:05:29 +0000598 assert(0 && "Variable not found");
599 return 0;
David Greenee8f3b272009-05-14 21:22:49 +0000600 }
David Greenee8f3b272009-05-14 21:22:49 +0000601 }
602 break;
603 }
David Greened571b3c2009-05-14 22:38:31 +0000604 case CAR: {
605 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
606 if (LHSl) {
607 if (LHSl->getSize() == 0) {
608 assert(0 && "Empty list in car");
609 return 0;
610 }
611 return LHSl->getElement(0);
612 }
613 break;
614 }
615 case CDR: {
616 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
617 if (LHSl) {
618 if (LHSl->getSize() == 0) {
619 assert(0 && "Empty list in cdr");
620 return 0;
621 }
David Greene8618f952009-06-08 20:23:18 +0000622 ListInit *Result = new ListInit(LHSl->begin()+1, LHSl->end(), LHSl->getType());
David Greened571b3c2009-05-14 22:38:31 +0000623 return Result;
624 }
625 break;
626 }
627 case LNULL: {
628 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
629 if (LHSl) {
630 if (LHSl->getSize() == 0) {
631 return new IntInit(1);
632 }
633 else {
634 return new IntInit(0);
635 }
636 }
David Greene8618f952009-06-08 20:23:18 +0000637 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
638 if (LHSs) {
639 if (LHSs->getValue().empty()) {
640 return new IntInit(1);
641 }
642 else {
643 return new IntInit(0);
644 }
645 }
646
David Greened571b3c2009-05-14 22:38:31 +0000647 break;
648 }
David Greenee8f3b272009-05-14 21:22:49 +0000649 }
650 return this;
651}
David Greene5d0c0512009-05-14 20:54:48 +0000652
David Greenee8f3b272009-05-14 21:22:49 +0000653Init *UnOpInit::resolveReferences(Record &R, const RecordVal *RV) {
654 Init *lhs = LHS->resolveReferences(R, RV);
David Greene5d0c0512009-05-14 20:54:48 +0000655
David Greenee8f3b272009-05-14 21:22:49 +0000656 if (LHS != lhs)
657 return (new UnOpInit(getOpcode(), lhs, getType()))->Fold(&R, 0);
658 return Fold(&R, 0);
659}
David Greene5d0c0512009-05-14 20:54:48 +0000660
David Greenee8f3b272009-05-14 21:22:49 +0000661std::string UnOpInit::getAsString() const {
662 std::string Result;
663 switch (Opc) {
664 case CAST: Result = "!cast<" + getType()->getAsString() + ">"; break;
David Greened571b3c2009-05-14 22:38:31 +0000665 case CAR: Result = "!car"; break;
666 case CDR: Result = "!cdr"; break;
667 case LNULL: Result = "!null"; break;
David Greenee8f3b272009-05-14 21:22:49 +0000668 }
669 return Result + "(" + LHS->getAsString() + ")";
670}
David Greene5d0c0512009-05-14 20:54:48 +0000671
David Greeneefa19612009-06-29 20:05:29 +0000672RecTy *UnOpInit::getFieldType(const std::string &FieldName) const {
673 switch (getOpcode()) {
674 default: assert(0 && "Unknown unop");
675 case CAST: {
676 RecordRecTy *RecordType = dynamic_cast<RecordRecTy *>(getType());
677 if (RecordType) {
678 RecordVal *Field = RecordType->getRecord()->getValue(FieldName);
679 if (Field) {
680 return Field->getType();
681 }
682 }
683 break;
684 }
685 }
686 return 0;
687}
688
David Greenea9c6c5d2009-04-22 20:18:10 +0000689Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
Chris Lattner51ffbf12006-03-31 21:53:49 +0000690 switch (getOpcode()) {
691 default: assert(0 && "Unknown binop");
Evan Chenga32dee22007-05-15 01:23:24 +0000692 case CONCAT: {
693 DagInit *LHSs = dynamic_cast<DagInit*>(LHS);
694 DagInit *RHSs = dynamic_cast<DagInit*>(RHS);
695 if (LHSs && RHSs) {
696 DefInit *LOp = dynamic_cast<DefInit*>(LHSs->getOperator());
697 DefInit *ROp = dynamic_cast<DefInit*>(RHSs->getOperator());
Evan Cheng94b5a802007-07-19 01:14:50 +0000698 if (LOp->getDef() != ROp->getDef()) {
699 bool LIsOps =
700 LOp->getDef()->getName() == "outs" ||
701 LOp->getDef()->getName() != "ins" ||
702 LOp->getDef()->getName() != "defs";
703 bool RIsOps =
704 ROp->getDef()->getName() == "outs" ||
705 ROp->getDef()->getName() != "ins" ||
706 ROp->getDef()->getName() != "defs";
707 if (!LIsOps || !RIsOps)
708 throw "Concated Dag operators do not match!";
709 }
Evan Chenga32dee22007-05-15 01:23:24 +0000710 std::vector<Init*> Args;
711 std::vector<std::string> ArgNames;
712 for (unsigned i = 0, e = LHSs->getNumArgs(); i != e; ++i) {
713 Args.push_back(LHSs->getArg(i));
714 ArgNames.push_back(LHSs->getArgName(i));
715 }
716 for (unsigned i = 0, e = RHSs->getNumArgs(); i != e; ++i) {
717 Args.push_back(RHSs->getArg(i));
718 ArgNames.push_back(RHSs->getArgName(i));
719 }
Nate Begemandbe3f772009-03-19 05:21:56 +0000720 return new DagInit(LHSs->getOperator(), "", Args, ArgNames);
Evan Chenga32dee22007-05-15 01:23:24 +0000721 }
722 break;
723 }
Chris Lattner51ffbf12006-03-31 21:53:49 +0000724 case STRCONCAT: {
725 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
726 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
727 if (LHSs && RHSs)
728 return new StringInit(LHSs->getValue() + RHSs->getValue());
729 break;
730 }
David Greenea9c6c5d2009-04-22 20:18:10 +0000731 case NAMECONCAT: {
732 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
733 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
734 if (LHSs && RHSs) {
735 std::string Name(LHSs->getValue() + RHSs->getValue());
736
737 // From TGParser::ParseIDValue
738 if (CurRec) {
David Greene196ac3c2009-04-23 21:25:15 +0000739 if (const RecordVal *RV = CurRec->getValue(Name)) {
740 if (RV->getType() != getType()) {
741 throw "type mismatch in nameconcat";
742 }
David Greenea9c6c5d2009-04-22 20:18:10 +0000743 return new VarInit(Name, RV->getType());
David Greene196ac3c2009-04-23 21:25:15 +0000744 }
745
David Greenea9c6c5d2009-04-22 20:18:10 +0000746 std::string TemplateArgName = CurRec->getName()+":"+Name;
747 if (CurRec->isTemplateArg(TemplateArgName)) {
748 const RecordVal *RV = CurRec->getValue(TemplateArgName);
749 assert(RV && "Template arg doesn't exist??");
David Greene196ac3c2009-04-23 21:25:15 +0000750
751 if (RV->getType() != getType()) {
752 throw "type mismatch in nameconcat";
753 }
754
David Greenea9c6c5d2009-04-22 20:18:10 +0000755 return new VarInit(TemplateArgName, RV->getType());
756 }
757 }
758
759 if (CurMultiClass) {
760 std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
761 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
762 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
763 assert(RV && "Template arg doesn't exist??");
David Greene196ac3c2009-04-23 21:25:15 +0000764
765 if (RV->getType() != getType()) {
766 throw "type mismatch in nameconcat";
767 }
768
David Greenea9c6c5d2009-04-22 20:18:10 +0000769 return new VarInit(MCName, RV->getType());
770 }
771 }
772
773 if (Record *D = Records.getDef(Name))
774 return new DefInit(D);
775
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000776 errs() << "Variable not defined in !nameconcat: '" + Name + "'\n";
David Greene8618f952009-06-08 20:23:18 +0000777 assert(0 && "Variable not found in !nameconcat");
David Greenea9c6c5d2009-04-22 20:18:10 +0000778 return 0;
779 }
780 break;
781 }
Chris Lattner51ffbf12006-03-31 21:53:49 +0000782 case SHL:
783 case SRA:
784 case SRL: {
785 IntInit *LHSi = dynamic_cast<IntInit*>(LHS);
786 IntInit *RHSi = dynamic_cast<IntInit*>(RHS);
787 if (LHSi && RHSi) {
Dan Gohmanca0546f2008-10-17 01:33:43 +0000788 int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
789 int64_t Result;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000790 switch (getOpcode()) {
791 default: assert(0 && "Bad opcode!");
792 case SHL: Result = LHSv << RHSv; break;
793 case SRA: Result = LHSv >> RHSv; break;
Dan Gohmanca0546f2008-10-17 01:33:43 +0000794 case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000795 }
796 return new IntInit(Result);
797 }
798 break;
799 }
800 }
801 return this;
802}
803
804Init *BinOpInit::resolveReferences(Record &R, const RecordVal *RV) {
805 Init *lhs = LHS->resolveReferences(R, RV);
806 Init *rhs = RHS->resolveReferences(R, RV);
807
808 if (LHS != lhs || RHS != rhs)
David Greene196ac3c2009-04-23 21:25:15 +0000809 return (new BinOpInit(getOpcode(), lhs, rhs, getType()))->Fold(&R, 0);
David Greenea9c6c5d2009-04-22 20:18:10 +0000810 return Fold(&R, 0);
Chris Lattner51ffbf12006-03-31 21:53:49 +0000811}
812
Chris Lattner695506c2007-11-22 21:05:25 +0000813std::string BinOpInit::getAsString() const {
814 std::string Result;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000815 switch (Opc) {
Chris Lattner695506c2007-11-22 21:05:25 +0000816 case CONCAT: Result = "!con"; break;
817 case SHL: Result = "!shl"; break;
818 case SRA: Result = "!sra"; break;
819 case SRL: Result = "!srl"; break;
820 case STRCONCAT: Result = "!strconcat"; break;
David Greene196ac3c2009-04-23 21:25:15 +0000821 case NAMECONCAT:
822 Result = "!nameconcat<" + getType()->getAsString() + ">"; break;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000823 }
Chris Lattner695506c2007-11-22 21:05:25 +0000824 return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")";
Chris Lattner51ffbf12006-03-31 21:53:49 +0000825}
826
David Greenee917fff2009-05-14 22:23:47 +0000827static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
828 Record *CurRec, MultiClass *CurMultiClass);
829
830static Init *EvaluateOperation(OpInit *RHSo, Init *LHS, Init *Arg,
831 RecTy *Type, Record *CurRec,
832 MultiClass *CurMultiClass) {
833 std::vector<Init *> NewOperands;
834
835 TypedInit *TArg = dynamic_cast<TypedInit*>(Arg);
836
837 // If this is a dag, recurse
838 if (TArg && TArg->getType()->getAsString() == "dag") {
839 Init *Result = ForeachHelper(LHS, Arg, RHSo, Type,
840 CurRec, CurMultiClass);
841 if (Result != 0) {
842 return Result;
843 }
844 else {
845 return 0;
846 }
847 }
848
849 for (int i = 0; i < RHSo->getNumOperands(); ++i) {
850 OpInit *RHSoo = dynamic_cast<OpInit*>(RHSo->getOperand(i));
851
852 if (RHSoo) {
853 Init *Result = EvaluateOperation(RHSoo, LHS, Arg,
854 Type, CurRec, CurMultiClass);
855 if (Result != 0) {
856 NewOperands.push_back(Result);
857 }
858 else {
859 NewOperands.push_back(Arg);
860 }
861 }
862 else if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
863 NewOperands.push_back(Arg);
864 }
865 else {
866 NewOperands.push_back(RHSo->getOperand(i));
867 }
868 }
869
870 // Now run the operator and use its result as the new leaf
871 OpInit *NewOp = RHSo->clone(NewOperands);
872 Init *NewVal = NewOp->Fold(CurRec, CurMultiClass);
873 if (NewVal != NewOp) {
874 delete NewOp;
875 return NewVal;
876 }
877 return 0;
878}
879
880static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
881 Record *CurRec, MultiClass *CurMultiClass) {
882 DagInit *MHSd = dynamic_cast<DagInit*>(MHS);
883 ListInit *MHSl = dynamic_cast<ListInit*>(MHS);
884
885 DagRecTy *DagType = dynamic_cast<DagRecTy*>(Type);
886 ListRecTy *ListType = dynamic_cast<ListRecTy*>(Type);
887
888 OpInit *RHSo = dynamic_cast<OpInit*>(RHS);
889
890 if (!RHSo) {
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000891 errs() << "!foreach requires an operator\n";
David Greenee917fff2009-05-14 22:23:47 +0000892 assert(0 && "No operator for !foreach");
893 }
894
895 TypedInit *LHSt = dynamic_cast<TypedInit*>(LHS);
896
897 if (!LHSt) {
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000898 errs() << "!foreach requires typed variable\n";
David Greenee917fff2009-05-14 22:23:47 +0000899 assert(0 && "No typed variable for !foreach");
900 }
901
Nick Lewycky94298222009-05-15 03:07:14 +0000902 if ((MHSd && DagType) || (MHSl && ListType)) {
David Greenee917fff2009-05-14 22:23:47 +0000903 if (MHSd) {
904 Init *Val = MHSd->getOperator();
905 Init *Result = EvaluateOperation(RHSo, LHS, Val,
906 Type, CurRec, CurMultiClass);
907 if (Result != 0) {
908 Val = Result;
909 }
910
911 std::vector<std::pair<Init *, std::string> > args;
912 for (unsigned int i = 0; i < MHSd->getNumArgs(); ++i) {
913 Init *Arg;
914 std::string ArgName;
915 Arg = MHSd->getArg(i);
916 ArgName = MHSd->getArgName(i);
917
918 // Process args
919 Init *Result = EvaluateOperation(RHSo, LHS, Arg, Type,
920 CurRec, CurMultiClass);
921 if (Result != 0) {
922 Arg = Result;
923 }
924
925 // TODO: Process arg names
926 args.push_back(std::make_pair(Arg, ArgName));
927 }
928
929 return new DagInit(Val, "", args);
930 }
931 if (MHSl) {
932 std::vector<Init *> NewOperands;
933 std::vector<Init *> NewList(MHSl->begin(), MHSl->end());
934
935 for (ListInit::iterator li = NewList.begin(),
936 liend = NewList.end();
937 li != liend;
938 ++li) {
939 Init *Item = *li;
940 NewOperands.clear();
941 for(int i = 0; i < RHSo->getNumOperands(); ++i) {
942 // First, replace the foreach variable with the list item
943 if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
944 NewOperands.push_back(Item);
945 }
946 else {
947 NewOperands.push_back(RHSo->getOperand(i));
948 }
949 }
950
951 // Now run the operator and use its result as the new list item
952 OpInit *NewOp = RHSo->clone(NewOperands);
953 Init *NewItem = NewOp->Fold(CurRec, CurMultiClass);
954 if (NewItem != NewOp) {
955 *li = NewItem;
956 delete NewOp;
957 }
958 }
David Greene8618f952009-06-08 20:23:18 +0000959 return new ListInit(NewList, MHSl->getType());
David Greenee917fff2009-05-14 22:23:47 +0000960 }
961 }
962 return 0;
963}
964
David Greene98ed3c72009-05-14 21:54:42 +0000965Init *TernOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
966 switch (getOpcode()) {
967 default: assert(0 && "Unknown binop");
968 case SUBST: {
969 DefInit *LHSd = dynamic_cast<DefInit*>(LHS);
970 VarInit *LHSv = dynamic_cast<VarInit*>(LHS);
971 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
David Greene196ac3c2009-04-23 21:25:15 +0000972
David Greene98ed3c72009-05-14 21:54:42 +0000973 DefInit *MHSd = dynamic_cast<DefInit*>(MHS);
974 VarInit *MHSv = dynamic_cast<VarInit*>(MHS);
975 StringInit *MHSs = dynamic_cast<StringInit*>(MHS);
David Greene5d0c0512009-05-14 20:54:48 +0000976
David Greene98ed3c72009-05-14 21:54:42 +0000977 DefInit *RHSd = dynamic_cast<DefInit*>(RHS);
978 VarInit *RHSv = dynamic_cast<VarInit*>(RHS);
979 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
David Greene5d0c0512009-05-14 20:54:48 +0000980
David Greene98ed3c72009-05-14 21:54:42 +0000981 if ((LHSd && MHSd && RHSd)
982 || (LHSv && MHSv && RHSv)
983 || (LHSs && MHSs && RHSs)) {
984 if (RHSd) {
985 Record *Val = RHSd->getDef();
986 if (LHSd->getAsString() == RHSd->getAsString()) {
987 Val = MHSd->getDef();
988 }
989 return new DefInit(Val);
990 }
991 if (RHSv) {
992 std::string Val = RHSv->getName();
993 if (LHSv->getAsString() == RHSv->getAsString()) {
994 Val = MHSv->getName();
995 }
996 return new VarInit(Val, getType());
997 }
998 if (RHSs) {
999 std::string Val = RHSs->getValue();
David Greene5d0c0512009-05-14 20:54:48 +00001000
David Greene98ed3c72009-05-14 21:54:42 +00001001 std::string::size_type found;
1002 do {
1003 found = Val.find(LHSs->getValue());
1004 if (found != std::string::npos) {
1005 Val.replace(found, LHSs->getValue().size(), MHSs->getValue());
1006 }
1007 } while (found != std::string::npos);
David Greene5d0c0512009-05-14 20:54:48 +00001008
David Greene98ed3c72009-05-14 21:54:42 +00001009 return new StringInit(Val);
1010 }
1011 }
1012 break;
1013 }
David Greene5d0c0512009-05-14 20:54:48 +00001014
David Greene98ed3c72009-05-14 21:54:42 +00001015 case FOREACH: {
David Greenee917fff2009-05-14 22:23:47 +00001016 Init *Result = ForeachHelper(LHS, MHS, RHS, getType(),
1017 CurRec, CurMultiClass);
1018 if (Result != 0) {
1019 return Result;
David Greene98ed3c72009-05-14 21:54:42 +00001020 }
1021 break;
1022 }
David Greene3587eed2009-05-14 23:26:46 +00001023
1024 case IF: {
1025 IntInit *LHSi = dynamic_cast<IntInit*>(LHS);
1026 if (LHSi) {
1027 if (LHSi->getValue()) {
1028 return MHS;
1029 }
1030 else {
1031 return RHS;
1032 }
1033 }
1034 break;
1035 }
David Greene98ed3c72009-05-14 21:54:42 +00001036 }
David Greene5d0c0512009-05-14 20:54:48 +00001037
David Greene98ed3c72009-05-14 21:54:42 +00001038 return this;
1039}
David Greene5d0c0512009-05-14 20:54:48 +00001040
David Greene98ed3c72009-05-14 21:54:42 +00001041Init *TernOpInit::resolveReferences(Record &R, const RecordVal *RV) {
1042 Init *lhs = LHS->resolveReferences(R, RV);
David Greeneb0354452009-06-08 19:16:56 +00001043
1044 if (Opc == IF && lhs != LHS) {
1045 IntInit *Value = dynamic_cast<IntInit*>(lhs);
1046 if (Value != 0) {
1047 // Short-circuit
1048 if (Value->getValue()) {
1049 Init *mhs = MHS->resolveReferences(R, RV);
1050 return (new TernOpInit(getOpcode(), lhs, mhs, RHS, getType()))->Fold(&R, 0);
1051 }
1052 else {
1053 Init *rhs = RHS->resolveReferences(R, RV);
1054 return (new TernOpInit(getOpcode(), lhs, MHS, rhs, getType()))->Fold(&R, 0);
1055 }
1056 }
1057 }
1058
David Greene98ed3c72009-05-14 21:54:42 +00001059 Init *mhs = MHS->resolveReferences(R, RV);
1060 Init *rhs = RHS->resolveReferences(R, RV);
David Greeneb0354452009-06-08 19:16:56 +00001061
David Greene98ed3c72009-05-14 21:54:42 +00001062 if (LHS != lhs || MHS != mhs || RHS != rhs)
1063 return (new TernOpInit(getOpcode(), lhs, mhs, rhs, getType()))->Fold(&R, 0);
1064 return Fold(&R, 0);
1065}
David Greene196ac3c2009-04-23 21:25:15 +00001066
David Greene98ed3c72009-05-14 21:54:42 +00001067std::string TernOpInit::getAsString() const {
1068 std::string Result;
1069 switch (Opc) {
1070 case SUBST: Result = "!subst"; break;
1071 case FOREACH: Result = "!foreach"; break;
David Greene3587eed2009-05-14 23:26:46 +00001072 case IF: Result = "!if"; break;
David Greene98ed3c72009-05-14 21:54:42 +00001073 }
1074 return Result + "(" + LHS->getAsString() + ", " + MHS->getAsString() + ", "
1075 + RHS->getAsString() + ")";
1076}
David Greene196ac3c2009-04-23 21:25:15 +00001077
Chris Lattner577fc3f2004-07-27 01:01:21 +00001078Init *TypedInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001079 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
1080 if (T == 0) return 0; // Cannot subscript a non-bits variable...
1081 unsigned NumBits = T->getNumBits();
1082
1083 BitsInit *BI = new BitsInit(Bits.size());
1084 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
1085 if (Bits[i] >= NumBits) {
1086 delete BI;
1087 return 0;
1088 }
1089 BI->setBit(i, new VarBitInit(this, Bits[i]));
1090 }
1091 return BI;
1092}
1093
Chris Lattner577fc3f2004-07-27 01:01:21 +00001094Init *TypedInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
1095 ListRecTy *T = dynamic_cast<ListRecTy*>(getType());
1096 if (T == 0) return 0; // Cannot subscript a non-list variable...
1097
1098 if (Elements.size() == 1)
1099 return new VarListElementInit(this, Elements[0]);
1100
1101 std::vector<Init*> ListInits;
1102 ListInits.reserve(Elements.size());
1103 for (unsigned i = 0, e = Elements.size(); i != e; ++i)
1104 ListInits.push_back(new VarListElementInit(this, Elements[i]));
David Greene8618f952009-06-08 20:23:18 +00001105 return new ListInit(ListInits, T);
Chris Lattner577fc3f2004-07-27 01:01:21 +00001106}
1107
1108
Chris Lattneref943742005-04-19 03:36:21 +00001109Init *VarInit::resolveBitReference(Record &R, const RecordVal *IRV,
1110 unsigned Bit) {
1111 if (R.isTemplateArg(getName())) return 0;
1112 if (IRV && IRV->getName() != getName()) return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001113
1114 RecordVal *RV = R.getValue(getName());
Bob Wilson159905a2009-11-21 22:44:20 +00001115 assert(RV && "Reference to a non-existent variable?");
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001116 assert(dynamic_cast<BitsInit*>(RV->getValue()));
1117 BitsInit *BI = (BitsInit*)RV->getValue();
Misha Brukman650ba8e2005-04-22 00:00:37 +00001118
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001119 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
1120 Init *B = BI->getBit(Bit);
1121
Bob Wilson67e6cab2009-11-22 03:58:57 +00001122 // If the bit is set to some value, or if we are resolving a reference to a
1123 // specific variable and that variable is explicitly unset, then replace the
1124 // VarBitInit with it.
1125 if (IRV || !dynamic_cast<UnsetInit*>(B))
1126 return B;
Chris Lattner577fc3f2004-07-27 01:01:21 +00001127 return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001128}
1129
Chris Lattneref943742005-04-19 03:36:21 +00001130Init *VarInit::resolveListElementReference(Record &R, const RecordVal *IRV,
1131 unsigned Elt) {
1132 if (R.isTemplateArg(getName())) return 0;
1133 if (IRV && IRV->getName() != getName()) return 0;
Chris Lattner577fc3f2004-07-27 01:01:21 +00001134
1135 RecordVal *RV = R.getValue(getName());
Bob Wilson159905a2009-11-21 22:44:20 +00001136 assert(RV && "Reference to a non-existent variable?");
Chris Lattner577fc3f2004-07-27 01:01:21 +00001137 ListInit *LI = dynamic_cast<ListInit*>(RV->getValue());
David Greene9d3febe2009-05-14 20:38:52 +00001138 if (!LI) {
1139 VarInit *VI = dynamic_cast<VarInit*>(RV->getValue());
1140 assert(VI && "Invalid list element!");
1141 return new VarListElementInit(VI, Elt);
1142 }
1143
Chris Lattner577fc3f2004-07-27 01:01:21 +00001144 if (Elt >= LI->getSize())
1145 return 0; // Out of range reference.
1146 Init *E = LI->getElement(Elt);
Bob Wilson67e6cab2009-11-22 03:58:57 +00001147 // If the element is set to some value, or if we are resolving a reference
1148 // to a specific variable and that variable is explicitly unset, then
1149 // replace the VarListElementInit with it.
1150 if (IRV || !dynamic_cast<UnsetInit*>(E))
1151 return E;
Chris Lattner577fc3f2004-07-27 01:01:21 +00001152 return 0;
1153}
1154
1155
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001156RecTy *VarInit::getFieldType(const std::string &FieldName) const {
1157 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
1158 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
1159 return RV->getType();
1160 return 0;
1161}
1162
1163Init *VarInit::getFieldInit(Record &R, const std::string &FieldName) const {
Reid Spencer2a826862006-11-02 20:46:16 +00001164 if (dynamic_cast<RecordRecTy*>(getType()))
Chris Lattnerd959ab92004-02-28 16:31:53 +00001165 if (const RecordVal *RV = R.getValue(VarName)) {
1166 Init *TheInit = RV->getValue();
1167 assert(TheInit != this && "Infinite loop detected!");
1168 if (Init *I = TheInit->getFieldInit(R, FieldName))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001169 return I;
1170 else
1171 return 0;
Chris Lattnerd959ab92004-02-28 16:31:53 +00001172 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001173 return 0;
1174}
1175
1176/// resolveReferences - This method is used by classes that refer to other
Bob Wilson159905a2009-11-21 22:44:20 +00001177/// variables which may not be defined at the time the expression is formed.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001178/// If a value is set for the variable later, this method will be called on
1179/// users of the value to allow the value to propagate out.
1180///
Chris Lattneref943742005-04-19 03:36:21 +00001181Init *VarInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001182 if (RecordVal *Val = R.getValue(VarName))
Chris Lattneref943742005-04-19 03:36:21 +00001183 if (RV == Val || (RV == 0 && !dynamic_cast<UnsetInit*>(Val->getValue())))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001184 return Val->getValue();
1185 return this;
1186}
Misha Brukman650ba8e2005-04-22 00:00:37 +00001187
Chris Lattner695506c2007-11-22 21:05:25 +00001188std::string VarBitInit::getAsString() const {
1189 return TI->getAsString() + "{" + utostr(Bit) + "}";
1190}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001191
Chris Lattneref943742005-04-19 03:36:21 +00001192Init *VarBitInit::resolveReferences(Record &R, const RecordVal *RV) {
1193 if (Init *I = getVariable()->resolveBitReference(R, RV, getBitNum()))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001194 return I;
1195 return this;
1196}
1197
Chris Lattner695506c2007-11-22 21:05:25 +00001198std::string VarListElementInit::getAsString() const {
1199 return TI->getAsString() + "[" + utostr(Element) + "]";
1200}
1201
Chris Lattneref943742005-04-19 03:36:21 +00001202Init *VarListElementInit::resolveReferences(Record &R, const RecordVal *RV) {
1203 if (Init *I = getVariable()->resolveListElementReference(R, RV,
1204 getElementNum()))
Chris Lattner577fc3f2004-07-27 01:01:21 +00001205 return I;
1206 return this;
1207}
1208
Chris Lattneref943742005-04-19 03:36:21 +00001209Init *VarListElementInit::resolveBitReference(Record &R, const RecordVal *RV,
1210 unsigned Bit) {
Chris Lattner577fc3f2004-07-27 01:01:21 +00001211 // FIXME: This should be implemented, to support references like:
1212 // bit B = AA[0]{1};
1213 return 0;
1214}
1215
Chris Lattneref943742005-04-19 03:36:21 +00001216Init *VarListElementInit::
1217resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) {
Chris Lattner577fc3f2004-07-27 01:01:21 +00001218 // FIXME: This should be implemented, to support references like:
1219 // int B = AA[0][1];
1220 return 0;
1221}
1222
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001223RecTy *DefInit::getFieldType(const std::string &FieldName) const {
1224 if (const RecordVal *RV = Def->getValue(FieldName))
1225 return RV->getType();
1226 return 0;
1227}
1228
1229Init *DefInit::getFieldInit(Record &R, const std::string &FieldName) const {
1230 return Def->getValue(FieldName)->getValue();
1231}
1232
1233
Chris Lattner695506c2007-11-22 21:05:25 +00001234std::string DefInit::getAsString() const {
1235 return Def->getName();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001236}
1237
Chris Lattneref943742005-04-19 03:36:21 +00001238Init *FieldInit::resolveBitReference(Record &R, const RecordVal *RV,
1239 unsigned Bit) {
Chris Lattner577fc3f2004-07-27 01:01:21 +00001240 if (Init *BitsVal = Rec->getFieldInit(R, FieldName))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001241 if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
1242 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
1243 Init *B = BI->getBit(Bit);
Misha Brukman650ba8e2005-04-22 00:00:37 +00001244
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001245 if (dynamic_cast<BitInit*>(B)) // If the bit is set...
1246 return B; // Replace the VarBitInit with it.
1247 }
Chris Lattner577fc3f2004-07-27 01:01:21 +00001248 return 0;
1249}
1250
Chris Lattneref943742005-04-19 03:36:21 +00001251Init *FieldInit::resolveListElementReference(Record &R, const RecordVal *RV,
1252 unsigned Elt) {
Chris Lattner577fc3f2004-07-27 01:01:21 +00001253 if (Init *ListVal = Rec->getFieldInit(R, FieldName))
1254 if (ListInit *LI = dynamic_cast<ListInit*>(ListVal)) {
1255 if (Elt >= LI->getSize()) return 0;
1256 Init *E = LI->getElement(Elt);
1257
Bob Wilson67e6cab2009-11-22 03:58:57 +00001258 // If the element is set to some value, or if we are resolving a
1259 // reference to a specific variable and that variable is explicitly
1260 // unset, then replace the VarListElementInit with it.
1261 if (RV || !dynamic_cast<UnsetInit*>(E))
1262 return E;
Chris Lattner577fc3f2004-07-27 01:01:21 +00001263 }
1264 return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001265}
1266
Chris Lattneref943742005-04-19 03:36:21 +00001267Init *FieldInit::resolveReferences(Record &R, const RecordVal *RV) {
1268 Init *NewRec = RV ? Rec->resolveReferences(R, RV) : Rec;
1269
1270 Init *BitsVal = NewRec->getFieldInit(R, FieldName);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001271 if (BitsVal) {
Chris Lattneref943742005-04-19 03:36:21 +00001272 Init *BVR = BitsVal->resolveReferences(R, RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001273 return BVR->isComplete() ? BVR : this;
1274 }
Chris Lattneref943742005-04-19 03:36:21 +00001275
1276 if (NewRec != Rec) {
Chris Lattneref943742005-04-19 03:36:21 +00001277 return new FieldInit(NewRec, FieldName);
1278 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001279 return this;
1280}
1281
Chris Lattner0d3ef402006-01-31 06:02:35 +00001282Init *DagInit::resolveReferences(Record &R, const RecordVal *RV) {
1283 std::vector<Init*> NewArgs;
1284 for (unsigned i = 0, e = Args.size(); i != e; ++i)
1285 NewArgs.push_back(Args[i]->resolveReferences(R, RV));
1286
Chris Lattnerb59cf3c2006-03-30 22:50:40 +00001287 Init *Op = Val->resolveReferences(R, RV);
1288
1289 if (Args != NewArgs || Op != Val)
Nate Begemandbe3f772009-03-19 05:21:56 +00001290 return new DagInit(Op, "", NewArgs, ArgNames);
Chris Lattner0d3ef402006-01-31 06:02:35 +00001291
1292 return this;
1293}
1294
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001295
Chris Lattner695506c2007-11-22 21:05:25 +00001296std::string DagInit::getAsString() const {
1297 std::string Result = "(" + Val->getAsString();
Nate Begemandbe3f772009-03-19 05:21:56 +00001298 if (!ValName.empty())
1299 Result += ":" + ValName;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001300 if (Args.size()) {
Chris Lattner695506c2007-11-22 21:05:25 +00001301 Result += " " + Args[0]->getAsString();
1302 if (!ArgNames[0].empty()) Result += ":$" + ArgNames[0];
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001303 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
Chris Lattner695506c2007-11-22 21:05:25 +00001304 Result += ", " + Args[i]->getAsString();
1305 if (!ArgNames[i].empty()) Result += ":$" + ArgNames[i];
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001306 }
1307 }
Chris Lattner695506c2007-11-22 21:05:25 +00001308 return Result + ")";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001309}
1310
1311
1312//===----------------------------------------------------------------------===//
1313// Other implementations
1314//===----------------------------------------------------------------------===//
1315
1316RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
1317 : Name(N), Ty(T), Prefix(P) {
1318 Value = Ty->convertValue(new UnsetInit());
1319 assert(Value && "Cannot create unset value for current type!");
1320}
1321
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001322void RecordVal::dump() const { errs() << *this; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001323
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001324void RecordVal::print(raw_ostream &OS, bool PrintSem) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001325 if (getPrefix()) OS << "field ";
1326 OS << *getType() << " " << getName();
Chris Lattneref943742005-04-19 03:36:21 +00001327
1328 if (getValue())
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001329 OS << " = " << *getValue();
Chris Lattneref943742005-04-19 03:36:21 +00001330
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001331 if (PrintSem) OS << ";\n";
1332}
1333
Daniel Dunbarced00812009-08-23 09:47:37 +00001334unsigned Record::LastID = 0;
1335
Chris Lattnerac284252005-08-19 17:58:11 +00001336void Record::setName(const std::string &Name) {
1337 if (Records.getDef(getName()) == this) {
1338 Records.removeDef(getName());
1339 this->Name = Name;
1340 Records.addDef(this);
1341 } else {
1342 Records.removeClass(getName());
1343 this->Name = Name;
1344 Records.addClass(this);
1345 }
1346}
1347
Chris Lattneref943742005-04-19 03:36:21 +00001348/// resolveReferencesTo - If anything in this record refers to RV, replace the
1349/// reference to RV with the RHS of RV. If RV is null, we resolve all possible
1350/// references.
1351void Record::resolveReferencesTo(const RecordVal *RV) {
1352 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
1353 if (Init *V = Values[i].getValue())
1354 Values[i].setValue(V->resolveReferences(*this, RV));
1355 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001356}
1357
Chris Lattneref943742005-04-19 03:36:21 +00001358
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001359void Record::dump() const { errs() << *this; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001360
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001361raw_ostream &llvm::operator<<(raw_ostream &OS, const Record &R) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001362 OS << R.getName();
1363
1364 const std::vector<std::string> &TArgs = R.getTemplateArgs();
1365 if (!TArgs.empty()) {
1366 OS << "<";
1367 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1368 if (i) OS << ", ";
1369 const RecordVal *RV = R.getValue(TArgs[i]);
1370 assert(RV && "Template argument record not found??");
1371 RV->print(OS, false);
1372 }
1373 OS << ">";
1374 }
1375
1376 OS << " {";
1377 const std::vector<Record*> &SC = R.getSuperClasses();
1378 if (!SC.empty()) {
1379 OS << "\t//";
1380 for (unsigned i = 0, e = SC.size(); i != e; ++i)
1381 OS << " " << SC[i]->getName();
1382 }
1383 OS << "\n";
1384
1385 const std::vector<RecordVal> &Vals = R.getValues();
1386 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
1387 if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
1388 OS << Vals[i];
1389 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
1390 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
1391 OS << Vals[i];
1392
1393 return OS << "}\n";
1394}
1395
1396/// getValueInit - Return the initializer for a value with the specified name,
1397/// or throw an exception if the field does not exist.
1398///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001399Init *Record::getValueInit(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001400 const RecordVal *R = getValue(FieldName);
1401 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001402 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001403 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001404 return R->getValue();
1405}
1406
1407
1408/// getValueAsString - This method looks up the specified field and returns its
1409/// value as a string, throwing an exception if the field does not exist or if
1410/// the value is not a string.
1411///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001412std::string Record::getValueAsString(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 (const StringInit *SI = dynamic_cast<const StringInit*>(R->getValue()))
1419 return SI->getValue();
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 string initializer!";
1422}
1423
1424/// getValueAsBitsInit - This method looks up the specified field and returns
1425/// its value as a BitsInit, throwing an exception if the field does not exist
1426/// or if the value is not the right type.
1427///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001428BitsInit *Record::getValueAsBitsInit(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001429 const RecordVal *R = getValue(FieldName);
1430 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001431 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001432 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001433
1434 if (BitsInit *BI = dynamic_cast<BitsInit*>(R->getValue()))
1435 return BI;
Chris Lattner42bb0c12009-09-18 18:31:37 +00001436 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001437 "' does not have a BitsInit initializer!";
1438}
1439
1440/// getValueAsListInit - This method looks up the specified field and returns
1441/// its value as a ListInit, throwing an exception if the field does not exist
1442/// or if the value is not the right type.
1443///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001444ListInit *Record::getValueAsListInit(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001445 const RecordVal *R = getValue(FieldName);
1446 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001447 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001448 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001449
1450 if (ListInit *LI = dynamic_cast<ListInit*>(R->getValue()))
1451 return LI;
Chris Lattner42bb0c12009-09-18 18:31:37 +00001452 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001453 "' does not have a list initializer!";
1454}
1455
Chris Lattner7ad0bed2005-10-28 22:49:02 +00001456/// getValueAsListOfDefs - This method looks up the specified field and returns
Jim Laskeyb04feb62005-10-28 21:46:31 +00001457/// its value as a vector of records, throwing an exception if the field does
1458/// not exist or if the value is not the right type.
1459///
Chris Lattner7ad0bed2005-10-28 22:49:02 +00001460std::vector<Record*>
Chris Lattner42bb0c12009-09-18 18:31:37 +00001461Record::getValueAsListOfDefs(StringRef FieldName) const {
Jim Laskeyb04feb62005-10-28 21:46:31 +00001462 ListInit *List = getValueAsListInit(FieldName);
1463 std::vector<Record*> Defs;
1464 for (unsigned i = 0; i < List->getSize(); i++) {
1465 if (DefInit *DI = dynamic_cast<DefInit*>(List->getElement(i))) {
1466 Defs.push_back(DI->getDef());
1467 } else {
Chris Lattner42bb0c12009-09-18 18:31:37 +00001468 throw "Record `" + getName() + "', field `" + FieldName.str() +
Jim Laskeyb04feb62005-10-28 21:46:31 +00001469 "' list is not entirely DefInit!";
1470 }
1471 }
1472 return Defs;
1473}
1474
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001475/// getValueAsInt - This method looks up the specified field and returns its
Dan Gohmanca0546f2008-10-17 01:33:43 +00001476/// value as an int64_t, throwing an exception if the field does not exist or if
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001477/// the value is not the right type.
1478///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001479int64_t Record::getValueAsInt(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001480 const RecordVal *R = getValue(FieldName);
1481 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001482 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001483 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001484
1485 if (IntInit *II = dynamic_cast<IntInit*>(R->getValue()))
1486 return II->getValue();
Chris Lattner42bb0c12009-09-18 18:31:37 +00001487 throw "Record `" + getName() + "', field `" + FieldName.str() +
Nate Begemanf621b332005-11-30 18:37:14 +00001488 "' does not have an int initializer!";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001489}
1490
Anton Korobeynikova468a112007-11-11 11:19:37 +00001491/// getValueAsListOfInts - This method looks up the specified field and returns
1492/// its value as a vector of integers, throwing an exception if the field does
1493/// not exist or if the value is not the right type.
1494///
Dan Gohmanca0546f2008-10-17 01:33:43 +00001495std::vector<int64_t>
Chris Lattner42bb0c12009-09-18 18:31:37 +00001496Record::getValueAsListOfInts(StringRef FieldName) const {
Anton Korobeynikova468a112007-11-11 11:19:37 +00001497 ListInit *List = getValueAsListInit(FieldName);
Dan Gohmanca0546f2008-10-17 01:33:43 +00001498 std::vector<int64_t> Ints;
Anton Korobeynikova468a112007-11-11 11:19:37 +00001499 for (unsigned i = 0; i < List->getSize(); i++) {
1500 if (IntInit *II = dynamic_cast<IntInit*>(List->getElement(i))) {
1501 Ints.push_back(II->getValue());
1502 } else {
Chris Lattner42bb0c12009-09-18 18:31:37 +00001503 throw "Record `" + getName() + "', field `" + FieldName.str() +
Anton Korobeynikova468a112007-11-11 11:19:37 +00001504 "' does not have a list of ints initializer!";
1505 }
1506 }
1507 return Ints;
1508}
1509
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001510/// getValueAsDef - This method looks up the specified field and returns its
1511/// value as a Record, 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 +00001514Record *Record::getValueAsDef(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 (DefInit *DI = dynamic_cast<DefInit*>(R->getValue()))
1521 return DI->getDef();
Chris Lattner42bb0c12009-09-18 18:31:37 +00001522 throw "Record `" + getName() + "', field `" + FieldName.str() +
Nate Begemanf621b332005-11-30 18:37:14 +00001523 "' does not have a def initializer!";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001524}
1525
1526/// getValueAsBit - This method looks up the specified field and returns its
1527/// value as a bit, throwing an exception if the field does not exist or if
1528/// the value is not the right type.
1529///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001530bool Record::getValueAsBit(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001531 const RecordVal *R = getValue(FieldName);
1532 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001533 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001534 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001535
1536 if (BitInit *BI = dynamic_cast<BitInit*>(R->getValue()))
1537 return BI->getValue();
Chris Lattner42bb0c12009-09-18 18:31:37 +00001538 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001539 "' does not have a bit initializer!";
1540}
1541
1542/// getValueAsDag - This method looks up the specified field and returns its
1543/// value as an Dag, throwing an exception if the field does not exist or if
1544/// the value is not the right type.
1545///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001546DagInit *Record::getValueAsDag(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001547 const RecordVal *R = getValue(FieldName);
1548 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001549 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001550 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001551
1552 if (DagInit *DI = dynamic_cast<DagInit*>(R->getValue()))
1553 return DI;
Chris Lattner42bb0c12009-09-18 18:31:37 +00001554 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001555 "' does not have a dag initializer!";
1556}
1557
Chris Lattner42bb0c12009-09-18 18:31:37 +00001558std::string Record::getValueAsCode(StringRef FieldName) const {
Chris Lattnerae939eb2005-09-13 21:44:28 +00001559 const RecordVal *R = getValue(FieldName);
1560 if (R == 0 || R->getValue() == 0)
1561 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001562 FieldName.str() + "'!\n";
Chris Lattnerae939eb2005-09-13 21:44:28 +00001563
1564 if (const CodeInit *CI = dynamic_cast<const CodeInit*>(R->getValue()))
1565 return CI->getValue();
Chris Lattner42bb0c12009-09-18 18:31:37 +00001566 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerae939eb2005-09-13 21:44:28 +00001567 "' does not have a code initializer!";
1568}
1569
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001570
David Greene7049e792009-04-24 16:55:41 +00001571void MultiClass::dump() const {
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001572 errs() << "Record:\n";
David Greene7049e792009-04-24 16:55:41 +00001573 Rec.dump();
1574
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001575 errs() << "Defs:\n";
David Greene7049e792009-04-24 16:55:41 +00001576 for (RecordVector::const_iterator r = DefPrototypes.begin(),
1577 rend = DefPrototypes.end();
1578 r != rend;
1579 ++r) {
1580 (*r)->dump();
1581 }
1582}
1583
1584
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001585void RecordKeeper::dump() const { errs() << *this; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001586
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001587raw_ostream &llvm::operator<<(raw_ostream &OS, const RecordKeeper &RK) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001588 OS << "------------- Classes -----------------\n";
1589 const std::map<std::string, Record*> &Classes = RK.getClasses();
1590 for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001591 E = Classes.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001592 OS << "class " << *I->second;
Misha Brukman650ba8e2005-04-22 00:00:37 +00001593
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001594 OS << "------------- Defs -----------------\n";
1595 const std::map<std::string, Record*> &Defs = RK.getDefs();
1596 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001597 E = Defs.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001598 OS << "def " << *I->second;
1599 return OS;
1600}
1601
1602
1603/// getAllDerivedDefinitions - This method returns all concrete definitions
1604/// that derive from the specified class name. If a class with the specified
1605/// name does not exist, an error is printed and true is returned.
1606std::vector<Record*>
1607RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
1608 Record *Class = Records.getClass(ClassName);
1609 if (!Class)
Misha Brukman41f9f292004-10-08 14:59:05 +00001610 throw "ERROR: Couldn't find the `" + ClassName + "' class!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001611
1612 std::vector<Record*> Defs;
1613 for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(),
1614 E = getDefs().end(); I != E; ++I)
1615 if (I->second->isSubClassOf(Class))
1616 Defs.push_back(I->second);
1617
1618 return Defs;
1619}
Brian Gaeke960707c2003-11-11 22:41:34 +00001620