blob: d594c9aa09aad70298b4f1ea44a78e9ad049537e [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"
Misha Brukman46cee7d2004-09-30 18:27:39 +000015#include "llvm/Support/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);
493 if (!dynamic_cast<UnsetInit*>(E)) // If the element is set
494 return E; // Replace the VarListElementInit with it.
495 return 0;
496}
497
Chris Lattner695506c2007-11-22 21:05:25 +0000498std::string ListInit::getAsString() const {
499 std::string Result = "[";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000500 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
Chris Lattner695506c2007-11-22 21:05:25 +0000501 if (i) Result += ", ";
502 Result += Values[i]->getAsString();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000503 }
Chris Lattner695506c2007-11-22 21:05:25 +0000504 return Result + "]";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000505}
506
David Greene5d0c0512009-05-14 20:54:48 +0000507Init *OpInit::resolveBitReference(Record &R, const RecordVal *IRV,
508 unsigned Bit) {
509 Init *Folded = Fold(&R, 0);
510
511 if (Folded != this) {
512 TypedInit *Typed = dynamic_cast<TypedInit *>(Folded);
513 if (Typed) {
514 return Typed->resolveBitReference(R, IRV, Bit);
515 }
516 }
517
518 return 0;
519}
520
521Init *OpInit::resolveListElementReference(Record &R, const RecordVal *IRV,
522 unsigned Elt) {
523 Init *Folded = Fold(&R, 0);
524
525 if (Folded != this) {
526 TypedInit *Typed = dynamic_cast<TypedInit *>(Folded);
527 if (Typed) {
528 return Typed->resolveListElementReference(R, IRV, Elt);
529 }
530 }
531
532 return 0;
533}
534
David Greenee8f3b272009-05-14 21:22:49 +0000535Init *UnOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
536 switch (getOpcode()) {
537 default: assert(0 && "Unknown unop");
538 case CAST: {
David Greeneefa19612009-06-29 20:05:29 +0000539 if (getType()->getAsString() == "string") {
540 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
541 if (LHSs) {
542 return LHSs;
543 }
David Greene5d0c0512009-05-14 20:54:48 +0000544
David Greeneefa19612009-06-29 20:05:29 +0000545 DefInit *LHSd = dynamic_cast<DefInit*>(LHS);
546 if (LHSd) {
547 return new StringInit(LHSd->getDef()->getName());
548 }
David Greeneefa19612009-06-29 20:05:29 +0000549 }
550 else {
551 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
552 if (LHSs) {
553 std::string Name = LHSs->getValue();
554
555 // From TGParser::ParseIDValue
556 if (CurRec) {
557 if (const RecordVal *RV = CurRec->getValue(Name)) {
558 if (RV->getType() != getType()) {
559 throw "type mismatch in nameconcat";
560 }
561 return new VarInit(Name, RV->getType());
David Greenee8f3b272009-05-14 21:22:49 +0000562 }
David Greeneefa19612009-06-29 20:05:29 +0000563
564 std::string TemplateArgName = CurRec->getName()+":"+Name;
565 if (CurRec->isTemplateArg(TemplateArgName)) {
566 const RecordVal *RV = CurRec->getValue(TemplateArgName);
567 assert(RV && "Template arg doesn't exist??");
568
569 if (RV->getType() != getType()) {
570 throw "type mismatch in nameconcat";
571 }
572
573 return new VarInit(TemplateArgName, RV->getType());
574 }
575 }
576
577 if (CurMultiClass) {
578 std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
579 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
580 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
581 assert(RV && "Template arg doesn't exist??");
582
583 if (RV->getType() != getType()) {
584 throw "type mismatch in nameconcat";
585 }
586
587 return new VarInit(MCName, RV->getType());
588 }
David Greenee8f3b272009-05-14 21:22:49 +0000589 }
David Greene5d0c0512009-05-14 20:54:48 +0000590
David Greeneefa19612009-06-29 20:05:29 +0000591 if (Record *D = Records.getDef(Name))
592 return new DefInit(D);
David Greene5d0c0512009-05-14 20:54:48 +0000593
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000594 errs() << "Variable not defined: '" + Name + "'\n";
David Greeneefa19612009-06-29 20:05:29 +0000595 assert(0 && "Variable not found");
596 return 0;
David Greenee8f3b272009-05-14 21:22:49 +0000597 }
David Greenee8f3b272009-05-14 21:22:49 +0000598 }
599 break;
600 }
David Greened571b3c2009-05-14 22:38:31 +0000601 case CAR: {
602 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
603 if (LHSl) {
604 if (LHSl->getSize() == 0) {
605 assert(0 && "Empty list in car");
606 return 0;
607 }
608 return LHSl->getElement(0);
609 }
610 break;
611 }
612 case CDR: {
613 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
614 if (LHSl) {
615 if (LHSl->getSize() == 0) {
616 assert(0 && "Empty list in cdr");
617 return 0;
618 }
David Greene8618f952009-06-08 20:23:18 +0000619 ListInit *Result = new ListInit(LHSl->begin()+1, LHSl->end(), LHSl->getType());
David Greened571b3c2009-05-14 22:38:31 +0000620 return Result;
621 }
622 break;
623 }
624 case LNULL: {
625 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
626 if (LHSl) {
627 if (LHSl->getSize() == 0) {
628 return new IntInit(1);
629 }
630 else {
631 return new IntInit(0);
632 }
633 }
David Greene8618f952009-06-08 20:23:18 +0000634 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
635 if (LHSs) {
636 if (LHSs->getValue().empty()) {
637 return new IntInit(1);
638 }
639 else {
640 return new IntInit(0);
641 }
642 }
643
David Greened571b3c2009-05-14 22:38:31 +0000644 break;
645 }
David Greenee8f3b272009-05-14 21:22:49 +0000646 }
647 return this;
648}
David Greene5d0c0512009-05-14 20:54:48 +0000649
David Greenee8f3b272009-05-14 21:22:49 +0000650Init *UnOpInit::resolveReferences(Record &R, const RecordVal *RV) {
651 Init *lhs = LHS->resolveReferences(R, RV);
David Greene5d0c0512009-05-14 20:54:48 +0000652
David Greenee8f3b272009-05-14 21:22:49 +0000653 if (LHS != lhs)
654 return (new UnOpInit(getOpcode(), lhs, getType()))->Fold(&R, 0);
655 return Fold(&R, 0);
656}
David Greene5d0c0512009-05-14 20:54:48 +0000657
David Greenee8f3b272009-05-14 21:22:49 +0000658std::string UnOpInit::getAsString() const {
659 std::string Result;
660 switch (Opc) {
661 case CAST: Result = "!cast<" + getType()->getAsString() + ">"; break;
David Greened571b3c2009-05-14 22:38:31 +0000662 case CAR: Result = "!car"; break;
663 case CDR: Result = "!cdr"; break;
664 case LNULL: Result = "!null"; break;
David Greenee8f3b272009-05-14 21:22:49 +0000665 }
666 return Result + "(" + LHS->getAsString() + ")";
667}
David Greene5d0c0512009-05-14 20:54:48 +0000668
David Greeneefa19612009-06-29 20:05:29 +0000669RecTy *UnOpInit::getFieldType(const std::string &FieldName) const {
670 switch (getOpcode()) {
671 default: assert(0 && "Unknown unop");
672 case CAST: {
673 RecordRecTy *RecordType = dynamic_cast<RecordRecTy *>(getType());
674 if (RecordType) {
675 RecordVal *Field = RecordType->getRecord()->getValue(FieldName);
676 if (Field) {
677 return Field->getType();
678 }
679 }
680 break;
681 }
682 }
683 return 0;
684}
685
David Greenea9c6c5d2009-04-22 20:18:10 +0000686Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
Chris Lattner51ffbf12006-03-31 21:53:49 +0000687 switch (getOpcode()) {
688 default: assert(0 && "Unknown binop");
Evan Chenga32dee22007-05-15 01:23:24 +0000689 case CONCAT: {
690 DagInit *LHSs = dynamic_cast<DagInit*>(LHS);
691 DagInit *RHSs = dynamic_cast<DagInit*>(RHS);
692 if (LHSs && RHSs) {
693 DefInit *LOp = dynamic_cast<DefInit*>(LHSs->getOperator());
694 DefInit *ROp = dynamic_cast<DefInit*>(RHSs->getOperator());
Evan Cheng94b5a802007-07-19 01:14:50 +0000695 if (LOp->getDef() != ROp->getDef()) {
696 bool LIsOps =
697 LOp->getDef()->getName() == "outs" ||
698 LOp->getDef()->getName() != "ins" ||
699 LOp->getDef()->getName() != "defs";
700 bool RIsOps =
701 ROp->getDef()->getName() == "outs" ||
702 ROp->getDef()->getName() != "ins" ||
703 ROp->getDef()->getName() != "defs";
704 if (!LIsOps || !RIsOps)
705 throw "Concated Dag operators do not match!";
706 }
Evan Chenga32dee22007-05-15 01:23:24 +0000707 std::vector<Init*> Args;
708 std::vector<std::string> ArgNames;
709 for (unsigned i = 0, e = LHSs->getNumArgs(); i != e; ++i) {
710 Args.push_back(LHSs->getArg(i));
711 ArgNames.push_back(LHSs->getArgName(i));
712 }
713 for (unsigned i = 0, e = RHSs->getNumArgs(); i != e; ++i) {
714 Args.push_back(RHSs->getArg(i));
715 ArgNames.push_back(RHSs->getArgName(i));
716 }
Nate Begemandbe3f772009-03-19 05:21:56 +0000717 return new DagInit(LHSs->getOperator(), "", Args, ArgNames);
Evan Chenga32dee22007-05-15 01:23:24 +0000718 }
719 break;
720 }
Chris Lattner51ffbf12006-03-31 21:53:49 +0000721 case STRCONCAT: {
722 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
723 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
724 if (LHSs && RHSs)
725 return new StringInit(LHSs->getValue() + RHSs->getValue());
726 break;
727 }
David Greenea9c6c5d2009-04-22 20:18:10 +0000728 case NAMECONCAT: {
729 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
730 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
731 if (LHSs && RHSs) {
732 std::string Name(LHSs->getValue() + RHSs->getValue());
733
734 // From TGParser::ParseIDValue
735 if (CurRec) {
David Greene196ac3c2009-04-23 21:25:15 +0000736 if (const RecordVal *RV = CurRec->getValue(Name)) {
737 if (RV->getType() != getType()) {
738 throw "type mismatch in nameconcat";
739 }
David Greenea9c6c5d2009-04-22 20:18:10 +0000740 return new VarInit(Name, RV->getType());
David Greene196ac3c2009-04-23 21:25:15 +0000741 }
742
David Greenea9c6c5d2009-04-22 20:18:10 +0000743 std::string TemplateArgName = CurRec->getName()+":"+Name;
744 if (CurRec->isTemplateArg(TemplateArgName)) {
745 const RecordVal *RV = CurRec->getValue(TemplateArgName);
746 assert(RV && "Template arg doesn't exist??");
David Greene196ac3c2009-04-23 21:25:15 +0000747
748 if (RV->getType() != getType()) {
749 throw "type mismatch in nameconcat";
750 }
751
David Greenea9c6c5d2009-04-22 20:18:10 +0000752 return new VarInit(TemplateArgName, RV->getType());
753 }
754 }
755
756 if (CurMultiClass) {
757 std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
758 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
759 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
760 assert(RV && "Template arg doesn't exist??");
David Greene196ac3c2009-04-23 21:25:15 +0000761
762 if (RV->getType() != getType()) {
763 throw "type mismatch in nameconcat";
764 }
765
David Greenea9c6c5d2009-04-22 20:18:10 +0000766 return new VarInit(MCName, RV->getType());
767 }
768 }
769
770 if (Record *D = Records.getDef(Name))
771 return new DefInit(D);
772
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000773 errs() << "Variable not defined in !nameconcat: '" + Name + "'\n";
David Greene8618f952009-06-08 20:23:18 +0000774 assert(0 && "Variable not found in !nameconcat");
David Greenea9c6c5d2009-04-22 20:18:10 +0000775 return 0;
776 }
777 break;
778 }
Chris Lattner51ffbf12006-03-31 21:53:49 +0000779 case SHL:
780 case SRA:
781 case SRL: {
782 IntInit *LHSi = dynamic_cast<IntInit*>(LHS);
783 IntInit *RHSi = dynamic_cast<IntInit*>(RHS);
784 if (LHSi && RHSi) {
Dan Gohmanca0546f2008-10-17 01:33:43 +0000785 int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
786 int64_t Result;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000787 switch (getOpcode()) {
788 default: assert(0 && "Bad opcode!");
789 case SHL: Result = LHSv << RHSv; break;
790 case SRA: Result = LHSv >> RHSv; break;
Dan Gohmanca0546f2008-10-17 01:33:43 +0000791 case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000792 }
793 return new IntInit(Result);
794 }
795 break;
796 }
797 }
798 return this;
799}
800
801Init *BinOpInit::resolveReferences(Record &R, const RecordVal *RV) {
802 Init *lhs = LHS->resolveReferences(R, RV);
803 Init *rhs = RHS->resolveReferences(R, RV);
804
805 if (LHS != lhs || RHS != rhs)
David Greene196ac3c2009-04-23 21:25:15 +0000806 return (new BinOpInit(getOpcode(), lhs, rhs, getType()))->Fold(&R, 0);
David Greenea9c6c5d2009-04-22 20:18:10 +0000807 return Fold(&R, 0);
Chris Lattner51ffbf12006-03-31 21:53:49 +0000808}
809
Chris Lattner695506c2007-11-22 21:05:25 +0000810std::string BinOpInit::getAsString() const {
811 std::string Result;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000812 switch (Opc) {
Chris Lattner695506c2007-11-22 21:05:25 +0000813 case CONCAT: Result = "!con"; break;
814 case SHL: Result = "!shl"; break;
815 case SRA: Result = "!sra"; break;
816 case SRL: Result = "!srl"; break;
817 case STRCONCAT: Result = "!strconcat"; break;
David Greene196ac3c2009-04-23 21:25:15 +0000818 case NAMECONCAT:
819 Result = "!nameconcat<" + getType()->getAsString() + ">"; break;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000820 }
Chris Lattner695506c2007-11-22 21:05:25 +0000821 return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")";
Chris Lattner51ffbf12006-03-31 21:53:49 +0000822}
823
David Greenee917fff2009-05-14 22:23:47 +0000824static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
825 Record *CurRec, MultiClass *CurMultiClass);
826
827static Init *EvaluateOperation(OpInit *RHSo, Init *LHS, Init *Arg,
828 RecTy *Type, Record *CurRec,
829 MultiClass *CurMultiClass) {
830 std::vector<Init *> NewOperands;
831
832 TypedInit *TArg = dynamic_cast<TypedInit*>(Arg);
833
834 // If this is a dag, recurse
835 if (TArg && TArg->getType()->getAsString() == "dag") {
836 Init *Result = ForeachHelper(LHS, Arg, RHSo, Type,
837 CurRec, CurMultiClass);
838 if (Result != 0) {
839 return Result;
840 }
841 else {
842 return 0;
843 }
844 }
845
846 for (int i = 0; i < RHSo->getNumOperands(); ++i) {
847 OpInit *RHSoo = dynamic_cast<OpInit*>(RHSo->getOperand(i));
848
849 if (RHSoo) {
850 Init *Result = EvaluateOperation(RHSoo, LHS, Arg,
851 Type, CurRec, CurMultiClass);
852 if (Result != 0) {
853 NewOperands.push_back(Result);
854 }
855 else {
856 NewOperands.push_back(Arg);
857 }
858 }
859 else if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
860 NewOperands.push_back(Arg);
861 }
862 else {
863 NewOperands.push_back(RHSo->getOperand(i));
864 }
865 }
866
867 // Now run the operator and use its result as the new leaf
868 OpInit *NewOp = RHSo->clone(NewOperands);
869 Init *NewVal = NewOp->Fold(CurRec, CurMultiClass);
870 if (NewVal != NewOp) {
871 delete NewOp;
872 return NewVal;
873 }
874 return 0;
875}
876
877static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
878 Record *CurRec, MultiClass *CurMultiClass) {
879 DagInit *MHSd = dynamic_cast<DagInit*>(MHS);
880 ListInit *MHSl = dynamic_cast<ListInit*>(MHS);
881
882 DagRecTy *DagType = dynamic_cast<DagRecTy*>(Type);
883 ListRecTy *ListType = dynamic_cast<ListRecTy*>(Type);
884
885 OpInit *RHSo = dynamic_cast<OpInit*>(RHS);
886
887 if (!RHSo) {
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000888 errs() << "!foreach requires an operator\n";
David Greenee917fff2009-05-14 22:23:47 +0000889 assert(0 && "No operator for !foreach");
890 }
891
892 TypedInit *LHSt = dynamic_cast<TypedInit*>(LHS);
893
894 if (!LHSt) {
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000895 errs() << "!foreach requires typed variable\n";
David Greenee917fff2009-05-14 22:23:47 +0000896 assert(0 && "No typed variable for !foreach");
897 }
898
Nick Lewycky94298222009-05-15 03:07:14 +0000899 if ((MHSd && DagType) || (MHSl && ListType)) {
David Greenee917fff2009-05-14 22:23:47 +0000900 if (MHSd) {
901 Init *Val = MHSd->getOperator();
902 Init *Result = EvaluateOperation(RHSo, LHS, Val,
903 Type, CurRec, CurMultiClass);
904 if (Result != 0) {
905 Val = Result;
906 }
907
908 std::vector<std::pair<Init *, std::string> > args;
909 for (unsigned int i = 0; i < MHSd->getNumArgs(); ++i) {
910 Init *Arg;
911 std::string ArgName;
912 Arg = MHSd->getArg(i);
913 ArgName = MHSd->getArgName(i);
914
915 // Process args
916 Init *Result = EvaluateOperation(RHSo, LHS, Arg, Type,
917 CurRec, CurMultiClass);
918 if (Result != 0) {
919 Arg = Result;
920 }
921
922 // TODO: Process arg names
923 args.push_back(std::make_pair(Arg, ArgName));
924 }
925
926 return new DagInit(Val, "", args);
927 }
928 if (MHSl) {
929 std::vector<Init *> NewOperands;
930 std::vector<Init *> NewList(MHSl->begin(), MHSl->end());
931
932 for (ListInit::iterator li = NewList.begin(),
933 liend = NewList.end();
934 li != liend;
935 ++li) {
936 Init *Item = *li;
937 NewOperands.clear();
938 for(int i = 0; i < RHSo->getNumOperands(); ++i) {
939 // First, replace the foreach variable with the list item
940 if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
941 NewOperands.push_back(Item);
942 }
943 else {
944 NewOperands.push_back(RHSo->getOperand(i));
945 }
946 }
947
948 // Now run the operator and use its result as the new list item
949 OpInit *NewOp = RHSo->clone(NewOperands);
950 Init *NewItem = NewOp->Fold(CurRec, CurMultiClass);
951 if (NewItem != NewOp) {
952 *li = NewItem;
953 delete NewOp;
954 }
955 }
David Greene8618f952009-06-08 20:23:18 +0000956 return new ListInit(NewList, MHSl->getType());
David Greenee917fff2009-05-14 22:23:47 +0000957 }
958 }
959 return 0;
960}
961
David Greene98ed3c72009-05-14 21:54:42 +0000962Init *TernOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
963 switch (getOpcode()) {
964 default: assert(0 && "Unknown binop");
965 case SUBST: {
966 DefInit *LHSd = dynamic_cast<DefInit*>(LHS);
967 VarInit *LHSv = dynamic_cast<VarInit*>(LHS);
968 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
David Greene196ac3c2009-04-23 21:25:15 +0000969
David Greene98ed3c72009-05-14 21:54:42 +0000970 DefInit *MHSd = dynamic_cast<DefInit*>(MHS);
971 VarInit *MHSv = dynamic_cast<VarInit*>(MHS);
972 StringInit *MHSs = dynamic_cast<StringInit*>(MHS);
David Greene5d0c0512009-05-14 20:54:48 +0000973
David Greene98ed3c72009-05-14 21:54:42 +0000974 DefInit *RHSd = dynamic_cast<DefInit*>(RHS);
975 VarInit *RHSv = dynamic_cast<VarInit*>(RHS);
976 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
David Greene5d0c0512009-05-14 20:54:48 +0000977
David Greene98ed3c72009-05-14 21:54:42 +0000978 if ((LHSd && MHSd && RHSd)
979 || (LHSv && MHSv && RHSv)
980 || (LHSs && MHSs && RHSs)) {
981 if (RHSd) {
982 Record *Val = RHSd->getDef();
983 if (LHSd->getAsString() == RHSd->getAsString()) {
984 Val = MHSd->getDef();
985 }
986 return new DefInit(Val);
987 }
988 if (RHSv) {
989 std::string Val = RHSv->getName();
990 if (LHSv->getAsString() == RHSv->getAsString()) {
991 Val = MHSv->getName();
992 }
993 return new VarInit(Val, getType());
994 }
995 if (RHSs) {
996 std::string Val = RHSs->getValue();
David Greene5d0c0512009-05-14 20:54:48 +0000997
David Greene98ed3c72009-05-14 21:54:42 +0000998 std::string::size_type found;
999 do {
1000 found = Val.find(LHSs->getValue());
1001 if (found != std::string::npos) {
1002 Val.replace(found, LHSs->getValue().size(), MHSs->getValue());
1003 }
1004 } while (found != std::string::npos);
David Greene5d0c0512009-05-14 20:54:48 +00001005
David Greene98ed3c72009-05-14 21:54:42 +00001006 return new StringInit(Val);
1007 }
1008 }
1009 break;
1010 }
David Greene5d0c0512009-05-14 20:54:48 +00001011
David Greene98ed3c72009-05-14 21:54:42 +00001012 case FOREACH: {
David Greenee917fff2009-05-14 22:23:47 +00001013 Init *Result = ForeachHelper(LHS, MHS, RHS, getType(),
1014 CurRec, CurMultiClass);
1015 if (Result != 0) {
1016 return Result;
David Greene98ed3c72009-05-14 21:54:42 +00001017 }
1018 break;
1019 }
David Greene3587eed2009-05-14 23:26:46 +00001020
1021 case IF: {
1022 IntInit *LHSi = dynamic_cast<IntInit*>(LHS);
1023 if (LHSi) {
1024 if (LHSi->getValue()) {
1025 return MHS;
1026 }
1027 else {
1028 return RHS;
1029 }
1030 }
1031 break;
1032 }
David Greene98ed3c72009-05-14 21:54:42 +00001033 }
David Greene5d0c0512009-05-14 20:54:48 +00001034
David Greene98ed3c72009-05-14 21:54:42 +00001035 return this;
1036}
David Greene5d0c0512009-05-14 20:54:48 +00001037
David Greene98ed3c72009-05-14 21:54:42 +00001038Init *TernOpInit::resolveReferences(Record &R, const RecordVal *RV) {
1039 Init *lhs = LHS->resolveReferences(R, RV);
David Greeneb0354452009-06-08 19:16:56 +00001040
1041 if (Opc == IF && lhs != LHS) {
1042 IntInit *Value = dynamic_cast<IntInit*>(lhs);
1043 if (Value != 0) {
1044 // Short-circuit
1045 if (Value->getValue()) {
1046 Init *mhs = MHS->resolveReferences(R, RV);
1047 return (new TernOpInit(getOpcode(), lhs, mhs, RHS, getType()))->Fold(&R, 0);
1048 }
1049 else {
1050 Init *rhs = RHS->resolveReferences(R, RV);
1051 return (new TernOpInit(getOpcode(), lhs, MHS, rhs, getType()))->Fold(&R, 0);
1052 }
1053 }
1054 }
1055
David Greene98ed3c72009-05-14 21:54:42 +00001056 Init *mhs = MHS->resolveReferences(R, RV);
1057 Init *rhs = RHS->resolveReferences(R, RV);
David Greeneb0354452009-06-08 19:16:56 +00001058
David Greene98ed3c72009-05-14 21:54:42 +00001059 if (LHS != lhs || MHS != mhs || RHS != rhs)
1060 return (new TernOpInit(getOpcode(), lhs, mhs, rhs, getType()))->Fold(&R, 0);
1061 return Fold(&R, 0);
1062}
David Greene196ac3c2009-04-23 21:25:15 +00001063
David Greene98ed3c72009-05-14 21:54:42 +00001064std::string TernOpInit::getAsString() const {
1065 std::string Result;
1066 switch (Opc) {
1067 case SUBST: Result = "!subst"; break;
1068 case FOREACH: Result = "!foreach"; break;
David Greene3587eed2009-05-14 23:26:46 +00001069 case IF: Result = "!if"; break;
David Greene98ed3c72009-05-14 21:54:42 +00001070 }
1071 return Result + "(" + LHS->getAsString() + ", " + MHS->getAsString() + ", "
1072 + RHS->getAsString() + ")";
1073}
David Greene196ac3c2009-04-23 21:25:15 +00001074
Chris Lattner577fc3f2004-07-27 01:01:21 +00001075Init *TypedInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001076 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
1077 if (T == 0) return 0; // Cannot subscript a non-bits variable...
1078 unsigned NumBits = T->getNumBits();
1079
1080 BitsInit *BI = new BitsInit(Bits.size());
1081 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
1082 if (Bits[i] >= NumBits) {
1083 delete BI;
1084 return 0;
1085 }
1086 BI->setBit(i, new VarBitInit(this, Bits[i]));
1087 }
1088 return BI;
1089}
1090
Chris Lattner577fc3f2004-07-27 01:01:21 +00001091Init *TypedInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
1092 ListRecTy *T = dynamic_cast<ListRecTy*>(getType());
1093 if (T == 0) return 0; // Cannot subscript a non-list variable...
1094
1095 if (Elements.size() == 1)
1096 return new VarListElementInit(this, Elements[0]);
1097
1098 std::vector<Init*> ListInits;
1099 ListInits.reserve(Elements.size());
1100 for (unsigned i = 0, e = Elements.size(); i != e; ++i)
1101 ListInits.push_back(new VarListElementInit(this, Elements[i]));
David Greene8618f952009-06-08 20:23:18 +00001102 return new ListInit(ListInits, T);
Chris Lattner577fc3f2004-07-27 01:01:21 +00001103}
1104
1105
Chris Lattneref943742005-04-19 03:36:21 +00001106Init *VarInit::resolveBitReference(Record &R, const RecordVal *IRV,
1107 unsigned Bit) {
1108 if (R.isTemplateArg(getName())) return 0;
1109 if (IRV && IRV->getName() != getName()) return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001110
1111 RecordVal *RV = R.getValue(getName());
1112 assert(RV && "Reference to a non-existant variable?");
1113 assert(dynamic_cast<BitsInit*>(RV->getValue()));
1114 BitsInit *BI = (BitsInit*)RV->getValue();
Misha Brukman650ba8e2005-04-22 00:00:37 +00001115
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001116 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
1117 Init *B = BI->getBit(Bit);
1118
1119 if (!dynamic_cast<UnsetInit*>(B)) // If the bit is not set...
1120 return B; // Replace the VarBitInit with it.
Chris Lattner577fc3f2004-07-27 01:01:21 +00001121 return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001122}
1123
Chris Lattneref943742005-04-19 03:36:21 +00001124Init *VarInit::resolveListElementReference(Record &R, const RecordVal *IRV,
1125 unsigned Elt) {
1126 if (R.isTemplateArg(getName())) return 0;
1127 if (IRV && IRV->getName() != getName()) return 0;
Chris Lattner577fc3f2004-07-27 01:01:21 +00001128
1129 RecordVal *RV = R.getValue(getName());
1130 assert(RV && "Reference to a non-existant variable?");
1131 ListInit *LI = dynamic_cast<ListInit*>(RV->getValue());
David Greene9d3febe2009-05-14 20:38:52 +00001132 if (!LI) {
1133 VarInit *VI = dynamic_cast<VarInit*>(RV->getValue());
1134 assert(VI && "Invalid list element!");
1135 return new VarListElementInit(VI, Elt);
1136 }
1137
Chris Lattner577fc3f2004-07-27 01:01:21 +00001138 if (Elt >= LI->getSize())
1139 return 0; // Out of range reference.
1140 Init *E = LI->getElement(Elt);
1141 if (!dynamic_cast<UnsetInit*>(E)) // If the element is set
1142 return E; // Replace the VarListElementInit with it.
1143 return 0;
1144}
1145
1146
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001147RecTy *VarInit::getFieldType(const std::string &FieldName) const {
1148 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
1149 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
1150 return RV->getType();
1151 return 0;
1152}
1153
1154Init *VarInit::getFieldInit(Record &R, const std::string &FieldName) const {
Reid Spencer2a826862006-11-02 20:46:16 +00001155 if (dynamic_cast<RecordRecTy*>(getType()))
Chris Lattnerd959ab92004-02-28 16:31:53 +00001156 if (const RecordVal *RV = R.getValue(VarName)) {
1157 Init *TheInit = RV->getValue();
1158 assert(TheInit != this && "Infinite loop detected!");
1159 if (Init *I = TheInit->getFieldInit(R, FieldName))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001160 return I;
1161 else
1162 return 0;
Chris Lattnerd959ab92004-02-28 16:31:53 +00001163 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001164 return 0;
1165}
1166
1167/// resolveReferences - This method is used by classes that refer to other
1168/// variables which may not be defined at the time they expression is formed.
1169/// If a value is set for the variable later, this method will be called on
1170/// users of the value to allow the value to propagate out.
1171///
Chris Lattneref943742005-04-19 03:36:21 +00001172Init *VarInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001173 if (RecordVal *Val = R.getValue(VarName))
Chris Lattneref943742005-04-19 03:36:21 +00001174 if (RV == Val || (RV == 0 && !dynamic_cast<UnsetInit*>(Val->getValue())))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001175 return Val->getValue();
1176 return this;
1177}
Misha Brukman650ba8e2005-04-22 00:00:37 +00001178
Chris Lattner695506c2007-11-22 21:05:25 +00001179std::string VarBitInit::getAsString() const {
1180 return TI->getAsString() + "{" + utostr(Bit) + "}";
1181}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001182
Chris Lattneref943742005-04-19 03:36:21 +00001183Init *VarBitInit::resolveReferences(Record &R, const RecordVal *RV) {
1184 if (Init *I = getVariable()->resolveBitReference(R, RV, getBitNum()))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001185 return I;
1186 return this;
1187}
1188
Chris Lattner695506c2007-11-22 21:05:25 +00001189std::string VarListElementInit::getAsString() const {
1190 return TI->getAsString() + "[" + utostr(Element) + "]";
1191}
1192
Chris Lattneref943742005-04-19 03:36:21 +00001193Init *VarListElementInit::resolveReferences(Record &R, const RecordVal *RV) {
1194 if (Init *I = getVariable()->resolveListElementReference(R, RV,
1195 getElementNum()))
Chris Lattner577fc3f2004-07-27 01:01:21 +00001196 return I;
1197 return this;
1198}
1199
Chris Lattneref943742005-04-19 03:36:21 +00001200Init *VarListElementInit::resolveBitReference(Record &R, const RecordVal *RV,
1201 unsigned Bit) {
Chris Lattner577fc3f2004-07-27 01:01:21 +00001202 // FIXME: This should be implemented, to support references like:
1203 // bit B = AA[0]{1};
1204 return 0;
1205}
1206
Chris Lattneref943742005-04-19 03:36:21 +00001207Init *VarListElementInit::
1208resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) {
Chris Lattner577fc3f2004-07-27 01:01:21 +00001209 // FIXME: This should be implemented, to support references like:
1210 // int B = AA[0][1];
1211 return 0;
1212}
1213
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001214RecTy *DefInit::getFieldType(const std::string &FieldName) const {
1215 if (const RecordVal *RV = Def->getValue(FieldName))
1216 return RV->getType();
1217 return 0;
1218}
1219
1220Init *DefInit::getFieldInit(Record &R, const std::string &FieldName) const {
1221 return Def->getValue(FieldName)->getValue();
1222}
1223
1224
Chris Lattner695506c2007-11-22 21:05:25 +00001225std::string DefInit::getAsString() const {
1226 return Def->getName();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001227}
1228
Chris Lattneref943742005-04-19 03:36:21 +00001229Init *FieldInit::resolveBitReference(Record &R, const RecordVal *RV,
1230 unsigned Bit) {
Chris Lattner577fc3f2004-07-27 01:01:21 +00001231 if (Init *BitsVal = Rec->getFieldInit(R, FieldName))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001232 if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
1233 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
1234 Init *B = BI->getBit(Bit);
Misha Brukman650ba8e2005-04-22 00:00:37 +00001235
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001236 if (dynamic_cast<BitInit*>(B)) // If the bit is set...
1237 return B; // Replace the VarBitInit with it.
1238 }
Chris Lattner577fc3f2004-07-27 01:01:21 +00001239 return 0;
1240}
1241
Chris Lattneref943742005-04-19 03:36:21 +00001242Init *FieldInit::resolveListElementReference(Record &R, const RecordVal *RV,
1243 unsigned Elt) {
Chris Lattner577fc3f2004-07-27 01:01:21 +00001244 if (Init *ListVal = Rec->getFieldInit(R, FieldName))
1245 if (ListInit *LI = dynamic_cast<ListInit*>(ListVal)) {
1246 if (Elt >= LI->getSize()) return 0;
1247 Init *E = LI->getElement(Elt);
1248
1249 if (!dynamic_cast<UnsetInit*>(E)) // If the bit is set...
1250 return E; // Replace the VarListElementInit with it.
1251 }
1252 return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001253}
1254
Chris Lattneref943742005-04-19 03:36:21 +00001255Init *FieldInit::resolveReferences(Record &R, const RecordVal *RV) {
1256 Init *NewRec = RV ? Rec->resolveReferences(R, RV) : Rec;
1257
1258 Init *BitsVal = NewRec->getFieldInit(R, FieldName);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001259 if (BitsVal) {
Chris Lattneref943742005-04-19 03:36:21 +00001260 Init *BVR = BitsVal->resolveReferences(R, RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001261 return BVR->isComplete() ? BVR : this;
1262 }
Chris Lattneref943742005-04-19 03:36:21 +00001263
1264 if (NewRec != Rec) {
Chris Lattneref943742005-04-19 03:36:21 +00001265 return new FieldInit(NewRec, FieldName);
1266 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001267 return this;
1268}
1269
Chris Lattner0d3ef402006-01-31 06:02:35 +00001270Init *DagInit::resolveReferences(Record &R, const RecordVal *RV) {
1271 std::vector<Init*> NewArgs;
1272 for (unsigned i = 0, e = Args.size(); i != e; ++i)
1273 NewArgs.push_back(Args[i]->resolveReferences(R, RV));
1274
Chris Lattnerb59cf3c2006-03-30 22:50:40 +00001275 Init *Op = Val->resolveReferences(R, RV);
1276
1277 if (Args != NewArgs || Op != Val)
Nate Begemandbe3f772009-03-19 05:21:56 +00001278 return new DagInit(Op, "", NewArgs, ArgNames);
Chris Lattner0d3ef402006-01-31 06:02:35 +00001279
1280 return this;
1281}
1282
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001283
Chris Lattner695506c2007-11-22 21:05:25 +00001284std::string DagInit::getAsString() const {
1285 std::string Result = "(" + Val->getAsString();
Nate Begemandbe3f772009-03-19 05:21:56 +00001286 if (!ValName.empty())
1287 Result += ":" + ValName;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001288 if (Args.size()) {
Chris Lattner695506c2007-11-22 21:05:25 +00001289 Result += " " + Args[0]->getAsString();
1290 if (!ArgNames[0].empty()) Result += ":$" + ArgNames[0];
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001291 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
Chris Lattner695506c2007-11-22 21:05:25 +00001292 Result += ", " + Args[i]->getAsString();
1293 if (!ArgNames[i].empty()) Result += ":$" + ArgNames[i];
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001294 }
1295 }
Chris Lattner695506c2007-11-22 21:05:25 +00001296 return Result + ")";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001297}
1298
1299
1300//===----------------------------------------------------------------------===//
1301// Other implementations
1302//===----------------------------------------------------------------------===//
1303
1304RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
1305 : Name(N), Ty(T), Prefix(P) {
1306 Value = Ty->convertValue(new UnsetInit());
1307 assert(Value && "Cannot create unset value for current type!");
1308}
1309
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001310void RecordVal::dump() const { errs() << *this; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001311
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001312void RecordVal::print(raw_ostream &OS, bool PrintSem) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001313 if (getPrefix()) OS << "field ";
1314 OS << *getType() << " " << getName();
Chris Lattneref943742005-04-19 03:36:21 +00001315
1316 if (getValue())
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001317 OS << " = " << *getValue();
Chris Lattneref943742005-04-19 03:36:21 +00001318
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001319 if (PrintSem) OS << ";\n";
1320}
1321
Daniel Dunbarced00812009-08-23 09:47:37 +00001322unsigned Record::LastID = 0;
1323
Chris Lattnerac284252005-08-19 17:58:11 +00001324void Record::setName(const std::string &Name) {
1325 if (Records.getDef(getName()) == this) {
1326 Records.removeDef(getName());
1327 this->Name = Name;
1328 Records.addDef(this);
1329 } else {
1330 Records.removeClass(getName());
1331 this->Name = Name;
1332 Records.addClass(this);
1333 }
1334}
1335
Chris Lattneref943742005-04-19 03:36:21 +00001336/// resolveReferencesTo - If anything in this record refers to RV, replace the
1337/// reference to RV with the RHS of RV. If RV is null, we resolve all possible
1338/// references.
1339void Record::resolveReferencesTo(const RecordVal *RV) {
1340 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
1341 if (Init *V = Values[i].getValue())
1342 Values[i].setValue(V->resolveReferences(*this, RV));
1343 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001344}
1345
Chris Lattneref943742005-04-19 03:36:21 +00001346
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001347void Record::dump() const { errs() << *this; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001348
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001349raw_ostream &llvm::operator<<(raw_ostream &OS, const Record &R) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001350 OS << R.getName();
1351
1352 const std::vector<std::string> &TArgs = R.getTemplateArgs();
1353 if (!TArgs.empty()) {
1354 OS << "<";
1355 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1356 if (i) OS << ", ";
1357 const RecordVal *RV = R.getValue(TArgs[i]);
1358 assert(RV && "Template argument record not found??");
1359 RV->print(OS, false);
1360 }
1361 OS << ">";
1362 }
1363
1364 OS << " {";
1365 const std::vector<Record*> &SC = R.getSuperClasses();
1366 if (!SC.empty()) {
1367 OS << "\t//";
1368 for (unsigned i = 0, e = SC.size(); i != e; ++i)
1369 OS << " " << SC[i]->getName();
1370 }
1371 OS << "\n";
1372
1373 const std::vector<RecordVal> &Vals = R.getValues();
1374 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
1375 if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
1376 OS << Vals[i];
1377 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
1378 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
1379 OS << Vals[i];
1380
1381 return OS << "}\n";
1382}
1383
1384/// getValueInit - Return the initializer for a value with the specified name,
1385/// or throw an exception if the field does not exist.
1386///
1387Init *Record::getValueInit(const std::string &FieldName) const {
1388 const RecordVal *R = getValue(FieldName);
1389 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001390 throw "Record `" + getName() + "' does not have a field named `" +
1391 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001392 return R->getValue();
1393}
1394
1395
1396/// getValueAsString - This method looks up the specified field and returns its
1397/// value as a string, throwing an exception if the field does not exist or if
1398/// the value is not a string.
1399///
1400std::string Record::getValueAsString(const std::string &FieldName) const {
1401 const RecordVal *R = getValue(FieldName);
1402 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001403 throw "Record `" + getName() + "' does not have a field named `" +
1404 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001405
1406 if (const StringInit *SI = dynamic_cast<const StringInit*>(R->getValue()))
1407 return SI->getValue();
Misha Brukman41f9f292004-10-08 14:59:05 +00001408 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001409 "' does not have a string initializer!";
1410}
1411
1412/// getValueAsBitsInit - This method looks up the specified field and returns
1413/// its value as a BitsInit, throwing an exception if the field does not exist
1414/// or if the value is not the right type.
1415///
1416BitsInit *Record::getValueAsBitsInit(const std::string &FieldName) const {
1417 const RecordVal *R = getValue(FieldName);
1418 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001419 throw "Record `" + getName() + "' does not have a field named `" +
1420 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001421
1422 if (BitsInit *BI = dynamic_cast<BitsInit*>(R->getValue()))
1423 return BI;
Misha Brukman41f9f292004-10-08 14:59:05 +00001424 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001425 "' does not have a BitsInit initializer!";
1426}
1427
1428/// getValueAsListInit - This method looks up the specified field and returns
1429/// its value as a ListInit, throwing an exception if the field does not exist
1430/// or if the value is not the right type.
1431///
1432ListInit *Record::getValueAsListInit(const std::string &FieldName) const {
1433 const RecordVal *R = getValue(FieldName);
1434 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001435 throw "Record `" + getName() + "' does not have a field named `" +
1436 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001437
1438 if (ListInit *LI = dynamic_cast<ListInit*>(R->getValue()))
1439 return LI;
Misha Brukman41f9f292004-10-08 14:59:05 +00001440 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001441 "' does not have a list initializer!";
1442}
1443
Chris Lattner7ad0bed2005-10-28 22:49:02 +00001444/// getValueAsListOfDefs - This method looks up the specified field and returns
Jim Laskeyb04feb62005-10-28 21:46:31 +00001445/// its value as a vector of records, throwing an exception if the field does
1446/// not exist or if the value is not the right type.
1447///
Chris Lattner7ad0bed2005-10-28 22:49:02 +00001448std::vector<Record*>
1449Record::getValueAsListOfDefs(const std::string &FieldName) const {
Jim Laskeyb04feb62005-10-28 21:46:31 +00001450 ListInit *List = getValueAsListInit(FieldName);
1451 std::vector<Record*> Defs;
1452 for (unsigned i = 0; i < List->getSize(); i++) {
1453 if (DefInit *DI = dynamic_cast<DefInit*>(List->getElement(i))) {
1454 Defs.push_back(DI->getDef());
1455 } else {
1456 throw "Record `" + getName() + "', field `" + FieldName +
1457 "' list is not entirely DefInit!";
1458 }
1459 }
1460 return Defs;
1461}
1462
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001463/// getValueAsInt - This method looks up the specified field and returns its
Dan Gohmanca0546f2008-10-17 01:33:43 +00001464/// value as an int64_t, throwing an exception if the field does not exist or if
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001465/// the value is not the right type.
1466///
Dan Gohmanca0546f2008-10-17 01:33:43 +00001467int64_t Record::getValueAsInt(const std::string &FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001468 const RecordVal *R = getValue(FieldName);
1469 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001470 throw "Record `" + getName() + "' does not have a field named `" +
1471 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001472
1473 if (IntInit *II = dynamic_cast<IntInit*>(R->getValue()))
1474 return II->getValue();
Misha Brukman41f9f292004-10-08 14:59:05 +00001475 throw "Record `" + getName() + "', field `" + FieldName +
Nate Begemanf621b332005-11-30 18:37:14 +00001476 "' does not have an int initializer!";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001477}
1478
Anton Korobeynikova468a112007-11-11 11:19:37 +00001479/// getValueAsListOfInts - This method looks up the specified field and returns
1480/// its value as a vector of integers, throwing an exception if the field does
1481/// not exist or if the value is not the right type.
1482///
Dan Gohmanca0546f2008-10-17 01:33:43 +00001483std::vector<int64_t>
Anton Korobeynikova468a112007-11-11 11:19:37 +00001484Record::getValueAsListOfInts(const std::string &FieldName) const {
1485 ListInit *List = getValueAsListInit(FieldName);
Dan Gohmanca0546f2008-10-17 01:33:43 +00001486 std::vector<int64_t> Ints;
Anton Korobeynikova468a112007-11-11 11:19:37 +00001487 for (unsigned i = 0; i < List->getSize(); i++) {
1488 if (IntInit *II = dynamic_cast<IntInit*>(List->getElement(i))) {
1489 Ints.push_back(II->getValue());
1490 } else {
1491 throw "Record `" + getName() + "', field `" + FieldName +
1492 "' does not have a list of ints initializer!";
1493 }
1494 }
1495 return Ints;
1496}
1497
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001498/// getValueAsDef - This method looks up the specified field and returns its
1499/// value as a Record, throwing an exception if the field does not exist or if
1500/// the value is not the right type.
1501///
1502Record *Record::getValueAsDef(const std::string &FieldName) const {
1503 const RecordVal *R = getValue(FieldName);
1504 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001505 throw "Record `" + getName() + "' does not have a field named `" +
1506 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001507
1508 if (DefInit *DI = dynamic_cast<DefInit*>(R->getValue()))
1509 return DI->getDef();
Misha Brukman41f9f292004-10-08 14:59:05 +00001510 throw "Record `" + getName() + "', field `" + FieldName +
Nate Begemanf621b332005-11-30 18:37:14 +00001511 "' does not have a def initializer!";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001512}
1513
1514/// getValueAsBit - This method looks up the specified field and returns its
1515/// value as a bit, throwing an exception if the field does not exist or if
1516/// the value is not the right type.
1517///
1518bool Record::getValueAsBit(const std::string &FieldName) const {
1519 const RecordVal *R = getValue(FieldName);
1520 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001521 throw "Record `" + getName() + "' does not have a field named `" +
1522 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001523
1524 if (BitInit *BI = dynamic_cast<BitInit*>(R->getValue()))
1525 return BI->getValue();
Misha Brukman41f9f292004-10-08 14:59:05 +00001526 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001527 "' does not have a bit initializer!";
1528}
1529
1530/// getValueAsDag - This method looks up the specified field and returns its
1531/// value as an Dag, throwing an exception if the field does not exist or if
1532/// the value is not the right type.
1533///
1534DagInit *Record::getValueAsDag(const std::string &FieldName) const {
1535 const RecordVal *R = getValue(FieldName);
1536 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001537 throw "Record `" + getName() + "' does not have a field named `" +
1538 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001539
1540 if (DagInit *DI = dynamic_cast<DagInit*>(R->getValue()))
1541 return DI;
Misha Brukman41f9f292004-10-08 14:59:05 +00001542 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001543 "' does not have a dag initializer!";
1544}
1545
Chris Lattnerae939eb2005-09-13 21:44:28 +00001546std::string Record::getValueAsCode(const std::string &FieldName) const {
1547 const RecordVal *R = getValue(FieldName);
1548 if (R == 0 || R->getValue() == 0)
1549 throw "Record `" + getName() + "' does not have a field named `" +
1550 FieldName + "'!\n";
1551
1552 if (const CodeInit *CI = dynamic_cast<const CodeInit*>(R->getValue()))
1553 return CI->getValue();
1554 throw "Record `" + getName() + "', field `" + FieldName +
1555 "' does not have a code initializer!";
1556}
1557
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001558
David Greene7049e792009-04-24 16:55:41 +00001559void MultiClass::dump() const {
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001560 errs() << "Record:\n";
David Greene7049e792009-04-24 16:55:41 +00001561 Rec.dump();
1562
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001563 errs() << "Defs:\n";
David Greene7049e792009-04-24 16:55:41 +00001564 for (RecordVector::const_iterator r = DefPrototypes.begin(),
1565 rend = DefPrototypes.end();
1566 r != rend;
1567 ++r) {
1568 (*r)->dump();
1569 }
1570}
1571
1572
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001573void RecordKeeper::dump() const { errs() << *this; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001574
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001575raw_ostream &llvm::operator<<(raw_ostream &OS, const RecordKeeper &RK) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001576 OS << "------------- Classes -----------------\n";
1577 const std::map<std::string, Record*> &Classes = RK.getClasses();
1578 for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001579 E = Classes.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001580 OS << "class " << *I->second;
Misha Brukman650ba8e2005-04-22 00:00:37 +00001581
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001582 OS << "------------- Defs -----------------\n";
1583 const std::map<std::string, Record*> &Defs = RK.getDefs();
1584 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001585 E = Defs.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001586 OS << "def " << *I->second;
1587 return OS;
1588}
1589
1590
1591/// getAllDerivedDefinitions - This method returns all concrete definitions
1592/// that derive from the specified class name. If a class with the specified
1593/// name does not exist, an error is printed and true is returned.
1594std::vector<Record*>
1595RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
1596 Record *Class = Records.getClass(ClassName);
1597 if (!Class)
Misha Brukman41f9f292004-10-08 14:59:05 +00001598 throw "ERROR: Couldn't find the `" + ClassName + "' class!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001599
1600 std::vector<Record*> Defs;
1601 for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(),
1602 E = getDefs().end(); I != E; ++I)
1603 if (I->second->isSubClassOf(Class))
1604 Defs.push_back(I->second);
1605
1606 return Defs;
1607}
Brian Gaeke960707c2003-11-11 22:41:34 +00001608