blob: 3f952b44a902cefe42214b7fe12d8728895e252e [file] [log] [blame]
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001//===- Record.cpp - Record implementation ---------------------------------===//
Misha Brukman650ba8e2005-04-22 00:00:37 +00002//
John Criswelld3032032003-10-20 20:20:30 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner8adcd9f2007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman650ba8e2005-04-22 00:00:37 +00007//
John Criswelld3032032003-10-20 20:20:30 +00008//===----------------------------------------------------------------------===//
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00009//
Chris Lattner51ffbf12006-03-31 21:53:49 +000010// Implement the tablegen record classes.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "Record.h"
Chandler Carruth56869f22009-10-26 01:35:46 +000015#include "llvm/System/DataTypes.h"
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000016#include "llvm/Support/Format.h"
Chris Lattner8b9ecda2007-11-20 22:25:16 +000017#include "llvm/ADT/StringExtras.h"
Duraid Madina14492af2005-12-26 05:08:55 +000018
Chris Lattner68478662004-08-01 03:55:39 +000019using namespace llvm;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000020
21//===----------------------------------------------------------------------===//
22// Type implementations
23//===----------------------------------------------------------------------===//
24
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000025void RecTy::dump() const { print(errs()); }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000026
27Init *BitRecTy::convertValue(BitsInit *BI) {
28 if (BI->getNumBits() != 1) return 0; // Only accept if just one bit!
29 return BI->getBit(0);
30}
31
32bool BitRecTy::baseClassOf(const BitsRecTy *RHS) const {
33 return RHS->getNumBits() == 1;
34}
35
36Init *BitRecTy::convertValue(IntInit *II) {
Dan Gohmanca0546f2008-10-17 01:33:43 +000037 int64_t Val = II->getValue();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000038 if (Val != 0 && Val != 1) return 0; // Only accept 0 or 1 for a bit!
Misha Brukman650ba8e2005-04-22 00:00:37 +000039
40 return new BitInit(Val != 0);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000041}
42
43Init *BitRecTy::convertValue(TypedInit *VI) {
44 if (dynamic_cast<BitRecTy*>(VI->getType()))
45 return VI; // Accept variable if it is already of bit type!
46 return 0;
47}
48
Chris Lattner8b9ecda2007-11-20 22:25:16 +000049std::string BitsRecTy::getAsString() const {
50 return "bits<" + utostr(Size) + ">";
51}
52
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000053Init *BitsRecTy::convertValue(UnsetInit *UI) {
54 BitsInit *Ret = new BitsInit(Size);
55
56 for (unsigned i = 0; i != Size; ++i)
57 Ret->setBit(i, new UnsetInit());
58 return Ret;
59}
60
61Init *BitsRecTy::convertValue(BitInit *UI) {
62 if (Size != 1) return 0; // Can only convert single bit...
63 BitsInit *Ret = new BitsInit(1);
64 Ret->setBit(0, UI);
65 return Ret;
66}
67
68// convertValue from Int initializer to bits type: Split the integer up into the
69// appropriate bits...
70//
71Init *BitsRecTy::convertValue(IntInit *II) {
Misha Brukman6752fb52004-06-21 18:01:47 +000072 int64_t Value = II->getValue();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000073 // Make sure this bitfield is large enough to hold the integer value...
74 if (Value >= 0) {
Misha Brukman6752fb52004-06-21 18:01:47 +000075 if (Value & ~((1LL << Size)-1))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000076 return 0;
77 } else {
Jeff Cohen0add83e2006-02-18 03:20:33 +000078 if ((Value >> Size) != -1 || ((Value & (1LL << (Size-1))) == 0))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000079 return 0;
80 }
81
82 BitsInit *Ret = new BitsInit(Size);
83 for (unsigned i = 0; i != Size; ++i)
Jeff Cohen0add83e2006-02-18 03:20:33 +000084 Ret->setBit(i, new BitInit(Value & (1LL << i)));
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000085
86 return Ret;
87}
88
89Init *BitsRecTy::convertValue(BitsInit *BI) {
90 // If the number of bits is right, return it. Otherwise we need to expand or
91 // truncate...
92 if (BI->getNumBits() == Size) return BI;
93 return 0;
94}
95
96Init *BitsRecTy::convertValue(TypedInit *VI) {
97 if (BitsRecTy *BRT = dynamic_cast<BitsRecTy*>(VI->getType()))
98 if (BRT->Size == Size) {
99 BitsInit *Ret = new BitsInit(Size);
100 for (unsigned i = 0; i != Size; ++i)
Jeff Cohen88e7b722005-04-22 04:13:13 +0000101 Ret->setBit(i, new VarBitInit(VI, i));
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000102 return Ret;
103 }
104 if (Size == 1 && dynamic_cast<BitRecTy*>(VI->getType())) {
105 BitsInit *Ret = new BitsInit(1);
106 Ret->setBit(0, VI);
107 return Ret;
108 }
Misha Brukman650ba8e2005-04-22 00:00:37 +0000109
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000110 return 0;
111}
112
113Init *IntRecTy::convertValue(BitInit *BI) {
114 return new IntInit(BI->getValue());
115}
116
117Init *IntRecTy::convertValue(BitsInit *BI) {
Dan Gohmanca0546f2008-10-17 01:33:43 +0000118 int64_t Result = 0;
Misha Brukman650ba8e2005-04-22 00:00:37 +0000119 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000120 if (BitInit *Bit = dynamic_cast<BitInit*>(BI->getBit(i))) {
121 Result |= Bit->getValue() << i;
122 } else {
123 return 0;
124 }
125 return new IntInit(Result);
126}
127
128Init *IntRecTy::convertValue(TypedInit *TI) {
129 if (TI->getType()->typeIsConvertibleTo(this))
130 return TI; // Accept variable if already of the right type!
131 return 0;
132}
133
David Greenee8f3b272009-05-14 21:22:49 +0000134Init *StringRecTy::convertValue(UnOpInit *BO) {
135 if (BO->getOpcode() == UnOpInit::CAST) {
136 Init *L = BO->getOperand()->convertInitializerTo(this);
137 if (L == 0) return 0;
138 if (L != BO->getOperand())
139 return new UnOpInit(UnOpInit::CAST, L, new StringRecTy);
140 return BO;
141 }
David Greene5d0c0512009-05-14 20:54:48 +0000142
David Greenee8f3b272009-05-14 21:22:49 +0000143 return convertValue((TypedInit*)BO);
144}
David Greene5d0c0512009-05-14 20:54:48 +0000145
Chris Lattner51ffbf12006-03-31 21:53:49 +0000146Init *StringRecTy::convertValue(BinOpInit *BO) {
147 if (BO->getOpcode() == BinOpInit::STRCONCAT) {
148 Init *L = BO->getLHS()->convertInitializerTo(this);
149 Init *R = BO->getRHS()->convertInitializerTo(this);
150 if (L == 0 || R == 0) return 0;
151 if (L != BO->getLHS() || R != BO->getRHS())
David Greene196ac3c2009-04-23 21:25:15 +0000152 return new BinOpInit(BinOpInit::STRCONCAT, L, R, new StringRecTy);
Chris Lattner51ffbf12006-03-31 21:53:49 +0000153 return BO;
154 }
David Greene196ac3c2009-04-23 21:25:15 +0000155 if (BO->getOpcode() == BinOpInit::NAMECONCAT) {
156 if (BO->getType()->getAsString() == getAsString()) {
157 Init *L = BO->getLHS()->convertInitializerTo(this);
158 Init *R = BO->getRHS()->convertInitializerTo(this);
159 if (L == 0 || R == 0) return 0;
160 if (L != BO->getLHS() || R != BO->getRHS())
161 return new BinOpInit(BinOpInit::NAMECONCAT, L, R, new StringRecTy);
162 return BO;
163 }
164 }
165
166 return convertValue((TypedInit*)BO);
Chris Lattner51ffbf12006-03-31 21:53:49 +0000167}
168
169
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000170Init *StringRecTy::convertValue(TypedInit *TI) {
171 if (dynamic_cast<StringRecTy*>(TI->getType()))
172 return TI; // Accept variable if already of the right type!
173 return 0;
174}
175
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000176std::string ListRecTy::getAsString() const {
177 return "list<" + Ty->getAsString() + ">";
178}
179
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000180Init *ListRecTy::convertValue(ListInit *LI) {
181 std::vector<Init*> Elements;
182
183 // Verify that all of the elements of the list are subclasses of the
184 // appropriate class!
185 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
186 if (Init *CI = LI->getElement(i)->convertInitializerTo(Ty))
187 Elements.push_back(CI);
188 else
189 return 0;
190
David Greene8618f952009-06-08 20:23:18 +0000191 ListRecTy *LType = dynamic_cast<ListRecTy*>(LI->getType());
192 if (LType == 0) {
193 return 0;
194 }
195
196 return new ListInit(Elements, new ListRecTy(Ty));
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000197}
198
199Init *ListRecTy::convertValue(TypedInit *TI) {
200 // Ensure that TI is compatible with our class.
201 if (ListRecTy *LRT = dynamic_cast<ListRecTy*>(TI->getType()))
202 if (LRT->getElementType()->typeIsConvertibleTo(getElementType()))
203 return TI;
204 return 0;
205}
206
207Init *CodeRecTy::convertValue(TypedInit *TI) {
208 if (TI->getType()->typeIsConvertibleTo(this))
209 return TI;
210 return 0;
211}
212
213Init *DagRecTy::convertValue(TypedInit *TI) {
214 if (TI->getType()->typeIsConvertibleTo(this))
215 return TI;
216 return 0;
217}
218
David Greenee8f3b272009-05-14 21:22:49 +0000219Init *DagRecTy::convertValue(UnOpInit *BO) {
220 if (BO->getOpcode() == UnOpInit::CAST) {
221 Init *L = BO->getOperand()->convertInitializerTo(this);
222 if (L == 0) return 0;
223 if (L != BO->getOperand())
224 return new UnOpInit(UnOpInit::CAST, L, new DagRecTy);
225 return BO;
226 }
227 return 0;
228}
David Greene5d0c0512009-05-14 20:54:48 +0000229
Evan Chenga32dee22007-05-15 01:23:24 +0000230Init *DagRecTy::convertValue(BinOpInit *BO) {
231 if (BO->getOpcode() == BinOpInit::CONCAT) {
232 Init *L = BO->getLHS()->convertInitializerTo(this);
233 Init *R = BO->getRHS()->convertInitializerTo(this);
234 if (L == 0 || R == 0) return 0;
235 if (L != BO->getLHS() || R != BO->getRHS())
David Greene196ac3c2009-04-23 21:25:15 +0000236 return new BinOpInit(BinOpInit::CONCAT, L, R, new DagRecTy);
Evan Chenga32dee22007-05-15 01:23:24 +0000237 return BO;
238 }
David Greene196ac3c2009-04-23 21:25:15 +0000239 if (BO->getOpcode() == BinOpInit::NAMECONCAT) {
240 if (BO->getType()->getAsString() == getAsString()) {
241 Init *L = BO->getLHS()->convertInitializerTo(this);
242 Init *R = BO->getRHS()->convertInitializerTo(this);
243 if (L == 0 || R == 0) return 0;
244 if (L != BO->getLHS() || R != BO->getRHS())
245 return new BinOpInit(BinOpInit::CONCAT, L, R, new DagRecTy);
246 return BO;
247 }
248 }
Evan Chenga32dee22007-05-15 01:23:24 +0000249 return 0;
250}
251
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000252std::string RecordRecTy::getAsString() const {
253 return Rec->getName();
254}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000255
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000256Init *RecordRecTy::convertValue(DefInit *DI) {
257 // Ensure that DI is a subclass of Rec.
258 if (!DI->getDef()->isSubClassOf(Rec))
259 return 0;
260 return DI;
261}
262
263Init *RecordRecTy::convertValue(TypedInit *TI) {
264 // Ensure that TI is compatible with Rec.
265 if (RecordRecTy *RRT = dynamic_cast<RecordRecTy*>(TI->getType()))
266 if (RRT->getRecord()->isSubClassOf(getRecord()) ||
267 RRT->getRecord() == getRecord())
268 return TI;
269 return 0;
270}
271
272bool RecordRecTy::baseClassOf(const RecordRecTy *RHS) const {
273 return Rec == RHS->getRecord() || RHS->getRecord()->isSubClassOf(Rec);
274}
275
276
Bob Wilson7248f862009-11-22 04:24:42 +0000277/// resolveTypes - Find a common type that T1 and T2 convert to.
David Greene8618f952009-06-08 20:23:18 +0000278/// Return 0 if no such type exists.
279///
280RecTy *llvm::resolveTypes(RecTy *T1, RecTy *T2) {
281 if (!T1->typeIsConvertibleTo(T2)) {
282 if (!T2->typeIsConvertibleTo(T1)) {
283 // If one is a Record type, check superclasses
284 RecordRecTy *RecTy1 = dynamic_cast<RecordRecTy*>(T1);
285 if (RecTy1) {
286 // See if T2 inherits from a type T1 also inherits from
Bob Wilson7248f862009-11-22 04:24:42 +0000287 const std::vector<Record *> &T1SuperClasses =
288 RecTy1->getRecord()->getSuperClasses();
David Greene8618f952009-06-08 20:23:18 +0000289 for(std::vector<Record *>::const_iterator i = T1SuperClasses.begin(),
290 iend = T1SuperClasses.end();
291 i != iend;
292 ++i) {
293 RecordRecTy *SuperRecTy1 = new RecordRecTy(*i);
294 RecTy *NewType1 = resolveTypes(SuperRecTy1, T2);
295 if (NewType1 != 0) {
296 if (NewType1 != SuperRecTy1) {
297 delete SuperRecTy1;
298 }
299 return NewType1;
300 }
301 }
302 }
303 RecordRecTy *RecTy2 = dynamic_cast<RecordRecTy*>(T2);
304 if (RecTy2) {
305 // See if T1 inherits from a type T2 also inherits from
Bob Wilson7248f862009-11-22 04:24:42 +0000306 const std::vector<Record *> &T2SuperClasses =
307 RecTy2->getRecord()->getSuperClasses();
308 for (std::vector<Record *>::const_iterator i = T2SuperClasses.begin(),
David Greene8618f952009-06-08 20:23:18 +0000309 iend = T2SuperClasses.end();
310 i != iend;
311 ++i) {
312 RecordRecTy *SuperRecTy2 = new RecordRecTy(*i);
313 RecTy *NewType2 = resolveTypes(T1, SuperRecTy2);
314 if (NewType2 != 0) {
315 if (NewType2 != SuperRecTy2) {
316 delete SuperRecTy2;
317 }
318 return NewType2;
319 }
320 }
321 }
322 return 0;
323 }
324 return T2;
325 }
326 return T1;
327}
328
329
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000330//===----------------------------------------------------------------------===//
331// Initializer implementations
332//===----------------------------------------------------------------------===//
333
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000334void Init::dump() const { return print(errs()); }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000335
336Init *BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
337 BitsInit *BI = new BitsInit(Bits.size());
338 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
339 if (Bits[i] >= getNumBits()) {
340 delete BI;
341 return 0;
342 }
343 BI->setBit(i, getBit(Bits[i]));
344 }
345 return BI;
346}
347
Chris Lattner695506c2007-11-22 21:05:25 +0000348std::string BitsInit::getAsString() const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000349 //if (!printInHex(OS)) return;
350 //if (!printAsVariable(OS)) return;
351 //if (!printAsUnset(OS)) return;
352
Chris Lattner695506c2007-11-22 21:05:25 +0000353 std::string Result = "{ ";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000354 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
Chris Lattner695506c2007-11-22 21:05:25 +0000355 if (i) Result += ", ";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000356 if (Init *Bit = getBit(e-i-1))
Chris Lattner695506c2007-11-22 21:05:25 +0000357 Result += Bit->getAsString();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000358 else
Chris Lattner695506c2007-11-22 21:05:25 +0000359 Result += "*";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000360 }
Chris Lattner695506c2007-11-22 21:05:25 +0000361 return Result + " }";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000362}
363
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000364bool BitsInit::printInHex(raw_ostream &OS) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000365 // First, attempt to convert the value into an integer value...
Dan Gohmanca0546f2008-10-17 01:33:43 +0000366 int64_t Result = 0;
Misha Brukman650ba8e2005-04-22 00:00:37 +0000367 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000368 if (BitInit *Bit = dynamic_cast<BitInit*>(getBit(i))) {
369 Result |= Bit->getValue() << i;
370 } else {
371 return true;
372 }
373
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000374 OS << format("0x%x", Result);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000375 return false;
376}
377
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000378bool BitsInit::printAsVariable(raw_ostream &OS) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000379 // Get the variable that we may be set equal to...
380 assert(getNumBits() != 0);
381 VarBitInit *FirstBit = dynamic_cast<VarBitInit*>(getBit(0));
382 if (FirstBit == 0) return true;
383 TypedInit *Var = FirstBit->getVariable();
384
385 // Check to make sure the types are compatible.
386 BitsRecTy *Ty = dynamic_cast<BitsRecTy*>(FirstBit->getVariable()->getType());
387 if (Ty == 0) return true;
388 if (Ty->getNumBits() != getNumBits()) return true; // Incompatible types!
389
390 // Check to make sure all bits are referring to the right bits in the variable
391 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
392 VarBitInit *Bit = dynamic_cast<VarBitInit*>(getBit(i));
393 if (Bit == 0 || Bit->getVariable() != Var || Bit->getBitNum() != i)
394 return true;
395 }
396
397 Var->print(OS);
398 return false;
399}
400
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000401bool BitsInit::printAsUnset(raw_ostream &OS) const {
Misha Brukman650ba8e2005-04-22 00:00:37 +0000402 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000403 if (!dynamic_cast<UnsetInit*>(getBit(i)))
404 return true;
405 OS << "?";
406 return false;
407}
408
409// resolveReferences - If there are any field references that refer to fields
410// that have been filled in, we can propagate the values now.
411//
Chris Lattneref943742005-04-19 03:36:21 +0000412Init *BitsInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000413 bool Changed = false;
414 BitsInit *New = new BitsInit(getNumBits());
415
416 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
417 Init *B;
418 Init *CurBit = getBit(i);
419
420 do {
421 B = CurBit;
Chris Lattneref943742005-04-19 03:36:21 +0000422 CurBit = CurBit->resolveReferences(R, RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000423 Changed |= B != CurBit;
424 } while (B != CurBit);
425 New->setBit(i, CurBit);
426 }
427
428 if (Changed)
429 return New;
430 delete New;
431 return this;
432}
433
Chris Lattner695506c2007-11-22 21:05:25 +0000434std::string IntInit::getAsString() const {
435 return itostr(Value);
436}
437
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000438Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
439 BitsInit *BI = new BitsInit(Bits.size());
440
441 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
Dan Gohmanca0546f2008-10-17 01:33:43 +0000442 if (Bits[i] >= 64) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000443 delete BI;
444 return 0;
445 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000446 BI->setBit(i, new BitInit(Value & (INT64_C(1) << Bits[i])));
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000447 }
448 return BI;
449}
450
Chris Lattner8bf9e062004-07-26 23:21:34 +0000451Init *ListInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
452 std::vector<Init*> Vals;
453 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
454 if (Elements[i] >= getSize())
455 return 0;
456 Vals.push_back(getElement(Elements[i]));
457 }
David Greene8618f952009-06-08 20:23:18 +0000458 return new ListInit(Vals, getType());
Chris Lattner8bf9e062004-07-26 23:21:34 +0000459}
460
Chris Lattnercbebe462007-02-27 22:08:27 +0000461Record *ListInit::getElementAsRecord(unsigned i) const {
462 assert(i < Values.size() && "List element index out of range!");
463 DefInit *DI = dynamic_cast<DefInit*>(Values[i]);
464 if (DI == 0) throw "Expected record in list!";
465 return DI->getDef();
466}
467
Chris Lattneref943742005-04-19 03:36:21 +0000468Init *ListInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattner577fc3f2004-07-27 01:01:21 +0000469 std::vector<Init*> Resolved;
470 Resolved.reserve(getSize());
471 bool Changed = false;
472
473 for (unsigned i = 0, e = getSize(); i != e; ++i) {
474 Init *E;
475 Init *CurElt = getElement(i);
476
477 do {
478 E = CurElt;
Chris Lattneref943742005-04-19 03:36:21 +0000479 CurElt = CurElt->resolveReferences(R, RV);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000480 Changed |= E != CurElt;
481 } while (E != CurElt);
482 Resolved.push_back(E);
483 }
484
485 if (Changed)
David Greene8618f952009-06-08 20:23:18 +0000486 return new ListInit(Resolved, getType());
Chris Lattner577fc3f2004-07-27 01:01:21 +0000487 return this;
488}
489
David Greene8618f952009-06-08 20:23:18 +0000490Init *ListInit::resolveListElementReference(Record &R, const RecordVal *IRV,
Bob Wilson7248f862009-11-22 04:24:42 +0000491 unsigned Elt) {
David Greene8618f952009-06-08 20:23:18 +0000492 if (Elt >= getSize())
493 return 0; // Out of range reference.
494 Init *E = getElement(Elt);
Bob Wilson67e6cab2009-11-22 03:58:57 +0000495 // If the element is set to some value, or if we are resolving a reference
496 // to a specific variable and that variable is explicitly unset, then
497 // replace the VarListElementInit with it.
498 if (IRV || !dynamic_cast<UnsetInit*>(E))
499 return E;
David Greene8618f952009-06-08 20:23:18 +0000500 return 0;
501}
502
Chris Lattner695506c2007-11-22 21:05:25 +0000503std::string ListInit::getAsString() const {
504 std::string Result = "[";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000505 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
Chris Lattner695506c2007-11-22 21:05:25 +0000506 if (i) Result += ", ";
507 Result += Values[i]->getAsString();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000508 }
Chris Lattner695506c2007-11-22 21:05:25 +0000509 return Result + "]";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000510}
511
David Greene5d0c0512009-05-14 20:54:48 +0000512Init *OpInit::resolveBitReference(Record &R, const RecordVal *IRV,
Bob Wilson7248f862009-11-22 04:24:42 +0000513 unsigned Bit) {
David Greene5d0c0512009-05-14 20:54:48 +0000514 Init *Folded = Fold(&R, 0);
515
516 if (Folded != this) {
517 TypedInit *Typed = dynamic_cast<TypedInit *>(Folded);
518 if (Typed) {
519 return Typed->resolveBitReference(R, IRV, Bit);
Bob Wilson7248f862009-11-22 04:24:42 +0000520 }
David Greene5d0c0512009-05-14 20:54:48 +0000521 }
Bob Wilson7248f862009-11-22 04:24:42 +0000522
David Greene5d0c0512009-05-14 20:54:48 +0000523 return 0;
524}
525
526Init *OpInit::resolveListElementReference(Record &R, const RecordVal *IRV,
Bob Wilson7248f862009-11-22 04:24:42 +0000527 unsigned Elt) {
David Greene5d0c0512009-05-14 20:54:48 +0000528 Init *Folded = Fold(&R, 0);
529
530 if (Folded != this) {
531 TypedInit *Typed = dynamic_cast<TypedInit *>(Folded);
532 if (Typed) {
533 return Typed->resolveListElementReference(R, IRV, Elt);
Bob Wilson7248f862009-11-22 04:24:42 +0000534 }
David Greene5d0c0512009-05-14 20:54:48 +0000535 }
Bob Wilson7248f862009-11-22 04:24:42 +0000536
David Greene5d0c0512009-05-14 20:54:48 +0000537 return 0;
538}
539
David Greenee8f3b272009-05-14 21:22:49 +0000540Init *UnOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
541 switch (getOpcode()) {
542 default: assert(0 && "Unknown unop");
543 case CAST: {
David Greeneefa19612009-06-29 20:05:29 +0000544 if (getType()->getAsString() == "string") {
545 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
546 if (LHSs) {
547 return LHSs;
548 }
David Greene5d0c0512009-05-14 20:54:48 +0000549
David Greeneefa19612009-06-29 20:05:29 +0000550 DefInit *LHSd = dynamic_cast<DefInit*>(LHS);
551 if (LHSd) {
552 return new StringInit(LHSd->getDef()->getName());
553 }
Bob Wilson7248f862009-11-22 04:24:42 +0000554 } else {
David Greeneefa19612009-06-29 20:05:29 +0000555 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
556 if (LHSs) {
557 std::string Name = LHSs->getValue();
558
559 // From TGParser::ParseIDValue
560 if (CurRec) {
561 if (const RecordVal *RV = CurRec->getValue(Name)) {
562 if (RV->getType() != getType()) {
563 throw "type mismatch in nameconcat";
564 }
565 return new VarInit(Name, RV->getType());
David Greenee8f3b272009-05-14 21:22:49 +0000566 }
David Greeneefa19612009-06-29 20:05:29 +0000567
568 std::string TemplateArgName = CurRec->getName()+":"+Name;
569 if (CurRec->isTemplateArg(TemplateArgName)) {
570 const RecordVal *RV = CurRec->getValue(TemplateArgName);
571 assert(RV && "Template arg doesn't exist??");
572
573 if (RV->getType() != getType()) {
574 throw "type mismatch in nameconcat";
575 }
576
577 return new VarInit(TemplateArgName, RV->getType());
578 }
579 }
580
581 if (CurMultiClass) {
582 std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
583 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
584 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
585 assert(RV && "Template arg doesn't exist??");
Bob Wilson7248f862009-11-22 04:24:42 +0000586
David Greeneefa19612009-06-29 20:05:29 +0000587 if (RV->getType() != getType()) {
588 throw "type mismatch in nameconcat";
589 }
Bob Wilson7248f862009-11-22 04:24:42 +0000590
David Greeneefa19612009-06-29 20:05:29 +0000591 return new VarInit(MCName, RV->getType());
592 }
David Greenee8f3b272009-05-14 21:22:49 +0000593 }
Bob Wilson7248f862009-11-22 04:24:42 +0000594
David Greeneefa19612009-06-29 20:05:29 +0000595 if (Record *D = Records.getDef(Name))
596 return new DefInit(D);
David Greene5d0c0512009-05-14 20:54:48 +0000597
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000598 errs() << "Variable not defined: '" + Name + "'\n";
David Greeneefa19612009-06-29 20:05:29 +0000599 assert(0 && "Variable not found");
600 return 0;
David Greenee8f3b272009-05-14 21:22:49 +0000601 }
David Greenee8f3b272009-05-14 21:22:49 +0000602 }
603 break;
604 }
David Greened571b3c2009-05-14 22:38:31 +0000605 case CAR: {
606 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
607 if (LHSl) {
608 if (LHSl->getSize() == 0) {
609 assert(0 && "Empty list in car");
610 return 0;
611 }
612 return LHSl->getElement(0);
613 }
614 break;
615 }
616 case CDR: {
617 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
618 if (LHSl) {
619 if (LHSl->getSize() == 0) {
620 assert(0 && "Empty list in cdr");
621 return 0;
622 }
Bob Wilson7248f862009-11-22 04:24:42 +0000623 ListInit *Result = new ListInit(LHSl->begin()+1, LHSl->end(),
624 LHSl->getType());
David Greened571b3c2009-05-14 22:38:31 +0000625 return Result;
626 }
627 break;
628 }
629 case LNULL: {
630 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
631 if (LHSl) {
632 if (LHSl->getSize() == 0) {
633 return new IntInit(1);
Bob Wilson7248f862009-11-22 04:24:42 +0000634 } else {
David Greened571b3c2009-05-14 22:38:31 +0000635 return new IntInit(0);
636 }
637 }
David Greene8618f952009-06-08 20:23:18 +0000638 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
639 if (LHSs) {
640 if (LHSs->getValue().empty()) {
641 return new IntInit(1);
Bob Wilson7248f862009-11-22 04:24:42 +0000642 } else {
David Greene8618f952009-06-08 20:23:18 +0000643 return new IntInit(0);
644 }
645 }
Bob Wilson7248f862009-11-22 04:24:42 +0000646
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);
Bob Wilson7248f862009-11-22 04:24:42 +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 }
Bob Wilson7248f862009-11-22 04:24:42 +0000745
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 }
Bob Wilson7248f862009-11-22 04:24:42 +0000768
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);
Bob Wilson7248f862009-11-22 04:24:42 +0000807
Chris Lattner51ffbf12006-03-31 21:53:49 +0000808 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;
Bob Wilson7248f862009-11-22 04:24:42 +0000821 case NAMECONCAT:
David Greene196ac3c2009-04-23 21:25:15 +0000822 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;
Bob Wilson7248f862009-11-22 04:24:42 +0000843 } else {
David Greenee917fff2009-05-14 22:23:47 +0000844 return 0;
845 }
846 }
847
848 for (int i = 0; i < RHSo->getNumOperands(); ++i) {
849 OpInit *RHSoo = dynamic_cast<OpInit*>(RHSo->getOperand(i));
850
851 if (RHSoo) {
852 Init *Result = EvaluateOperation(RHSoo, LHS, Arg,
853 Type, CurRec, CurMultiClass);
854 if (Result != 0) {
855 NewOperands.push_back(Result);
Bob Wilson7248f862009-11-22 04:24:42 +0000856 } else {
David Greenee917fff2009-05-14 22:23:47 +0000857 NewOperands.push_back(Arg);
858 }
Bob Wilson7248f862009-11-22 04:24:42 +0000859 } else if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
David Greenee917fff2009-05-14 22:23:47 +0000860 NewOperands.push_back(Arg);
Bob Wilson7248f862009-11-22 04:24:42 +0000861 } else {
David Greenee917fff2009-05-14 22:23:47 +0000862 NewOperands.push_back(RHSo->getOperand(i));
863 }
864 }
865
866 // Now run the operator and use its result as the new leaf
867 OpInit *NewOp = RHSo->clone(NewOperands);
868 Init *NewVal = NewOp->Fold(CurRec, CurMultiClass);
869 if (NewVal != NewOp) {
870 delete NewOp;
871 return NewVal;
872 }
873 return 0;
874}
875
876static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
877 Record *CurRec, MultiClass *CurMultiClass) {
878 DagInit *MHSd = dynamic_cast<DagInit*>(MHS);
879 ListInit *MHSl = dynamic_cast<ListInit*>(MHS);
880
881 DagRecTy *DagType = dynamic_cast<DagRecTy*>(Type);
882 ListRecTy *ListType = dynamic_cast<ListRecTy*>(Type);
883
884 OpInit *RHSo = dynamic_cast<OpInit*>(RHS);
885
886 if (!RHSo) {
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000887 errs() << "!foreach requires an operator\n";
David Greenee917fff2009-05-14 22:23:47 +0000888 assert(0 && "No operator for !foreach");
889 }
890
891 TypedInit *LHSt = dynamic_cast<TypedInit*>(LHS);
892
893 if (!LHSt) {
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000894 errs() << "!foreach requires typed variable\n";
David Greenee917fff2009-05-14 22:23:47 +0000895 assert(0 && "No typed variable for !foreach");
896 }
897
Nick Lewycky94298222009-05-15 03:07:14 +0000898 if ((MHSd && DagType) || (MHSl && ListType)) {
David Greenee917fff2009-05-14 22:23:47 +0000899 if (MHSd) {
900 Init *Val = MHSd->getOperator();
901 Init *Result = EvaluateOperation(RHSo, LHS, Val,
902 Type, CurRec, CurMultiClass);
903 if (Result != 0) {
904 Val = Result;
905 }
906
907 std::vector<std::pair<Init *, std::string> > args;
908 for (unsigned int i = 0; i < MHSd->getNumArgs(); ++i) {
909 Init *Arg;
910 std::string ArgName;
911 Arg = MHSd->getArg(i);
912 ArgName = MHSd->getArgName(i);
913
914 // Process args
915 Init *Result = EvaluateOperation(RHSo, LHS, Arg, Type,
916 CurRec, CurMultiClass);
917 if (Result != 0) {
918 Arg = Result;
919 }
920
921 // TODO: Process arg names
922 args.push_back(std::make_pair(Arg, ArgName));
923 }
924
925 return new DagInit(Val, "", args);
926 }
927 if (MHSl) {
928 std::vector<Init *> NewOperands;
929 std::vector<Init *> NewList(MHSl->begin(), MHSl->end());
930
931 for (ListInit::iterator li = NewList.begin(),
932 liend = NewList.end();
933 li != liend;
934 ++li) {
935 Init *Item = *li;
936 NewOperands.clear();
937 for(int i = 0; i < RHSo->getNumOperands(); ++i) {
938 // First, replace the foreach variable with the list item
939 if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
940 NewOperands.push_back(Item);
Bob Wilson7248f862009-11-22 04:24:42 +0000941 } else {
David Greenee917fff2009-05-14 22:23:47 +0000942 NewOperands.push_back(RHSo->getOperand(i));
943 }
944 }
945
946 // Now run the operator and use its result as the new list item
947 OpInit *NewOp = RHSo->clone(NewOperands);
948 Init *NewItem = NewOp->Fold(CurRec, CurMultiClass);
949 if (NewItem != NewOp) {
950 *li = NewItem;
951 delete NewOp;
952 }
953 }
David Greene8618f952009-06-08 20:23:18 +0000954 return new ListInit(NewList, MHSl->getType());
David Greenee917fff2009-05-14 22:23:47 +0000955 }
956 }
957 return 0;
958}
959
David Greene98ed3c72009-05-14 21:54:42 +0000960Init *TernOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
961 switch (getOpcode()) {
962 default: assert(0 && "Unknown binop");
963 case SUBST: {
964 DefInit *LHSd = dynamic_cast<DefInit*>(LHS);
965 VarInit *LHSv = dynamic_cast<VarInit*>(LHS);
966 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
David Greene196ac3c2009-04-23 21:25:15 +0000967
David Greene98ed3c72009-05-14 21:54:42 +0000968 DefInit *MHSd = dynamic_cast<DefInit*>(MHS);
969 VarInit *MHSv = dynamic_cast<VarInit*>(MHS);
970 StringInit *MHSs = dynamic_cast<StringInit*>(MHS);
David Greene5d0c0512009-05-14 20:54:48 +0000971
David Greene98ed3c72009-05-14 21:54:42 +0000972 DefInit *RHSd = dynamic_cast<DefInit*>(RHS);
973 VarInit *RHSv = dynamic_cast<VarInit*>(RHS);
974 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
David Greene5d0c0512009-05-14 20:54:48 +0000975
David Greene98ed3c72009-05-14 21:54:42 +0000976 if ((LHSd && MHSd && RHSd)
977 || (LHSv && MHSv && RHSv)
978 || (LHSs && MHSs && RHSs)) {
979 if (RHSd) {
980 Record *Val = RHSd->getDef();
981 if (LHSd->getAsString() == RHSd->getAsString()) {
982 Val = MHSd->getDef();
983 }
984 return new DefInit(Val);
985 }
986 if (RHSv) {
987 std::string Val = RHSv->getName();
988 if (LHSv->getAsString() == RHSv->getAsString()) {
989 Val = MHSv->getName();
990 }
991 return new VarInit(Val, getType());
992 }
993 if (RHSs) {
994 std::string Val = RHSs->getValue();
David Greene5d0c0512009-05-14 20:54:48 +0000995
David Greene98ed3c72009-05-14 21:54:42 +0000996 std::string::size_type found;
997 do {
998 found = Val.find(LHSs->getValue());
999 if (found != std::string::npos) {
1000 Val.replace(found, LHSs->getValue().size(), MHSs->getValue());
1001 }
1002 } while (found != std::string::npos);
David Greene5d0c0512009-05-14 20:54:48 +00001003
David Greene98ed3c72009-05-14 21:54:42 +00001004 return new StringInit(Val);
1005 }
1006 }
1007 break;
Bob Wilson7248f862009-11-22 04:24:42 +00001008 }
David Greene5d0c0512009-05-14 20:54:48 +00001009
David Greene98ed3c72009-05-14 21:54:42 +00001010 case FOREACH: {
David Greenee917fff2009-05-14 22:23:47 +00001011 Init *Result = ForeachHelper(LHS, MHS, RHS, getType(),
1012 CurRec, CurMultiClass);
1013 if (Result != 0) {
1014 return Result;
David Greene98ed3c72009-05-14 21:54:42 +00001015 }
1016 break;
1017 }
David Greene3587eed2009-05-14 23:26:46 +00001018
1019 case IF: {
1020 IntInit *LHSi = dynamic_cast<IntInit*>(LHS);
1021 if (LHSi) {
1022 if (LHSi->getValue()) {
1023 return MHS;
Bob Wilson7248f862009-11-22 04:24:42 +00001024 } else {
David Greene3587eed2009-05-14 23:26:46 +00001025 return RHS;
1026 }
1027 }
1028 break;
1029 }
David Greene98ed3c72009-05-14 21:54:42 +00001030 }
David Greene5d0c0512009-05-14 20:54:48 +00001031
David Greene98ed3c72009-05-14 21:54:42 +00001032 return this;
1033}
David Greene5d0c0512009-05-14 20:54:48 +00001034
David Greene98ed3c72009-05-14 21:54:42 +00001035Init *TernOpInit::resolveReferences(Record &R, const RecordVal *RV) {
1036 Init *lhs = LHS->resolveReferences(R, RV);
David Greeneb0354452009-06-08 19:16:56 +00001037
1038 if (Opc == IF && lhs != LHS) {
1039 IntInit *Value = dynamic_cast<IntInit*>(lhs);
1040 if (Value != 0) {
1041 // Short-circuit
1042 if (Value->getValue()) {
1043 Init *mhs = MHS->resolveReferences(R, RV);
Bob Wilson7248f862009-11-22 04:24:42 +00001044 return (new TernOpInit(getOpcode(), lhs, mhs,
1045 RHS, getType()))->Fold(&R, 0);
1046 } else {
David Greeneb0354452009-06-08 19:16:56 +00001047 Init *rhs = RHS->resolveReferences(R, RV);
Bob Wilson7248f862009-11-22 04:24:42 +00001048 return (new TernOpInit(getOpcode(), lhs, MHS,
1049 rhs, getType()))->Fold(&R, 0);
David Greeneb0354452009-06-08 19:16:56 +00001050 }
1051 }
1052 }
Bob Wilson7248f862009-11-22 04:24:42 +00001053
David Greene98ed3c72009-05-14 21:54:42 +00001054 Init *mhs = MHS->resolveReferences(R, RV);
1055 Init *rhs = RHS->resolveReferences(R, RV);
David Greeneb0354452009-06-08 19:16:56 +00001056
David Greene98ed3c72009-05-14 21:54:42 +00001057 if (LHS != lhs || MHS != mhs || RHS != rhs)
1058 return (new TernOpInit(getOpcode(), lhs, mhs, rhs, getType()))->Fold(&R, 0);
1059 return Fold(&R, 0);
1060}
David Greene196ac3c2009-04-23 21:25:15 +00001061
David Greene98ed3c72009-05-14 21:54:42 +00001062std::string TernOpInit::getAsString() const {
1063 std::string Result;
1064 switch (Opc) {
1065 case SUBST: Result = "!subst"; break;
Bob Wilson7248f862009-11-22 04:24:42 +00001066 case FOREACH: Result = "!foreach"; break;
1067 case IF: Result = "!if"; break;
David Greene98ed3c72009-05-14 21:54:42 +00001068 }
Bob Wilson7248f862009-11-22 04:24:42 +00001069 return Result + "(" + LHS->getAsString() + ", " + MHS->getAsString() + ", "
David Greene98ed3c72009-05-14 21:54:42 +00001070 + RHS->getAsString() + ")";
1071}
David Greene196ac3c2009-04-23 21:25:15 +00001072
Chris Lattner577fc3f2004-07-27 01:01:21 +00001073Init *TypedInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001074 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
1075 if (T == 0) return 0; // Cannot subscript a non-bits variable...
1076 unsigned NumBits = T->getNumBits();
1077
1078 BitsInit *BI = new BitsInit(Bits.size());
1079 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
1080 if (Bits[i] >= NumBits) {
1081 delete BI;
1082 return 0;
1083 }
1084 BI->setBit(i, new VarBitInit(this, Bits[i]));
1085 }
1086 return BI;
1087}
1088
Chris Lattner577fc3f2004-07-27 01:01:21 +00001089Init *TypedInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
1090 ListRecTy *T = dynamic_cast<ListRecTy*>(getType());
1091 if (T == 0) return 0; // Cannot subscript a non-list variable...
1092
1093 if (Elements.size() == 1)
1094 return new VarListElementInit(this, Elements[0]);
1095
1096 std::vector<Init*> ListInits;
1097 ListInits.reserve(Elements.size());
1098 for (unsigned i = 0, e = Elements.size(); i != e; ++i)
1099 ListInits.push_back(new VarListElementInit(this, Elements[i]));
David Greene8618f952009-06-08 20:23:18 +00001100 return new ListInit(ListInits, T);
Chris Lattner577fc3f2004-07-27 01:01:21 +00001101}
1102
1103
Chris Lattneref943742005-04-19 03:36:21 +00001104Init *VarInit::resolveBitReference(Record &R, const RecordVal *IRV,
1105 unsigned Bit) {
1106 if (R.isTemplateArg(getName())) return 0;
1107 if (IRV && IRV->getName() != getName()) return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001108
1109 RecordVal *RV = R.getValue(getName());
Bob Wilson159905a2009-11-21 22:44:20 +00001110 assert(RV && "Reference to a non-existent variable?");
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001111 assert(dynamic_cast<BitsInit*>(RV->getValue()));
1112 BitsInit *BI = (BitsInit*)RV->getValue();
Misha Brukman650ba8e2005-04-22 00:00:37 +00001113
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001114 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
1115 Init *B = BI->getBit(Bit);
1116
Bob Wilson67e6cab2009-11-22 03:58:57 +00001117 // If the bit is set to some value, or if we are resolving a reference to a
1118 // specific variable and that variable is explicitly unset, then replace the
1119 // VarBitInit with it.
1120 if (IRV || !dynamic_cast<UnsetInit*>(B))
1121 return B;
Chris Lattner577fc3f2004-07-27 01:01:21 +00001122 return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001123}
1124
Chris Lattneref943742005-04-19 03:36:21 +00001125Init *VarInit::resolveListElementReference(Record &R, const RecordVal *IRV,
1126 unsigned Elt) {
1127 if (R.isTemplateArg(getName())) return 0;
1128 if (IRV && IRV->getName() != getName()) return 0;
Chris Lattner577fc3f2004-07-27 01:01:21 +00001129
1130 RecordVal *RV = R.getValue(getName());
Bob Wilson159905a2009-11-21 22:44:20 +00001131 assert(RV && "Reference to a non-existent variable?");
Chris Lattner577fc3f2004-07-27 01:01:21 +00001132 ListInit *LI = dynamic_cast<ListInit*>(RV->getValue());
David Greene9d3febe2009-05-14 20:38:52 +00001133 if (!LI) {
1134 VarInit *VI = dynamic_cast<VarInit*>(RV->getValue());
1135 assert(VI && "Invalid list element!");
1136 return new VarListElementInit(VI, Elt);
1137 }
Bob Wilson7248f862009-11-22 04:24:42 +00001138
Chris Lattner577fc3f2004-07-27 01:01:21 +00001139 if (Elt >= LI->getSize())
1140 return 0; // Out of range reference.
1141 Init *E = LI->getElement(Elt);
Bob Wilson67e6cab2009-11-22 03:58:57 +00001142 // If the element is set to some value, or if we are resolving a reference
1143 // to a specific variable and that variable is explicitly unset, then
1144 // replace the VarListElementInit with it.
1145 if (IRV || !dynamic_cast<UnsetInit*>(E))
1146 return E;
Chris Lattner577fc3f2004-07-27 01:01:21 +00001147 return 0;
1148}
1149
1150
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001151RecTy *VarInit::getFieldType(const std::string &FieldName) const {
1152 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
1153 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
1154 return RV->getType();
1155 return 0;
1156}
1157
1158Init *VarInit::getFieldInit(Record &R, const std::string &FieldName) const {
Reid Spencer2a826862006-11-02 20:46:16 +00001159 if (dynamic_cast<RecordRecTy*>(getType()))
Chris Lattnerd959ab92004-02-28 16:31:53 +00001160 if (const RecordVal *RV = R.getValue(VarName)) {
1161 Init *TheInit = RV->getValue();
1162 assert(TheInit != this && "Infinite loop detected!");
1163 if (Init *I = TheInit->getFieldInit(R, FieldName))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001164 return I;
1165 else
1166 return 0;
Chris Lattnerd959ab92004-02-28 16:31:53 +00001167 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001168 return 0;
1169}
1170
1171/// resolveReferences - This method is used by classes that refer to other
Bob Wilson159905a2009-11-21 22:44:20 +00001172/// variables which may not be defined at the time the expression is formed.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001173/// If a value is set for the variable later, this method will be called on
1174/// users of the value to allow the value to propagate out.
1175///
Chris Lattneref943742005-04-19 03:36:21 +00001176Init *VarInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001177 if (RecordVal *Val = R.getValue(VarName))
Chris Lattneref943742005-04-19 03:36:21 +00001178 if (RV == Val || (RV == 0 && !dynamic_cast<UnsetInit*>(Val->getValue())))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001179 return Val->getValue();
1180 return this;
1181}
Misha Brukman650ba8e2005-04-22 00:00:37 +00001182
Chris Lattner695506c2007-11-22 21:05:25 +00001183std::string VarBitInit::getAsString() const {
1184 return TI->getAsString() + "{" + utostr(Bit) + "}";
1185}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001186
Chris Lattneref943742005-04-19 03:36:21 +00001187Init *VarBitInit::resolveReferences(Record &R, const RecordVal *RV) {
1188 if (Init *I = getVariable()->resolveBitReference(R, RV, getBitNum()))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001189 return I;
1190 return this;
1191}
1192
Chris Lattner695506c2007-11-22 21:05:25 +00001193std::string VarListElementInit::getAsString() const {
1194 return TI->getAsString() + "[" + utostr(Element) + "]";
1195}
1196
Chris Lattneref943742005-04-19 03:36:21 +00001197Init *VarListElementInit::resolveReferences(Record &R, const RecordVal *RV) {
1198 if (Init *I = getVariable()->resolveListElementReference(R, RV,
1199 getElementNum()))
Chris Lattner577fc3f2004-07-27 01:01:21 +00001200 return I;
1201 return this;
1202}
1203
Chris Lattneref943742005-04-19 03:36:21 +00001204Init *VarListElementInit::resolveBitReference(Record &R, const RecordVal *RV,
1205 unsigned Bit) {
Chris Lattner577fc3f2004-07-27 01:01:21 +00001206 // FIXME: This should be implemented, to support references like:
1207 // bit B = AA[0]{1};
1208 return 0;
1209}
1210
Chris Lattneref943742005-04-19 03:36:21 +00001211Init *VarListElementInit::
1212resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) {
Chris Lattner577fc3f2004-07-27 01:01:21 +00001213 // FIXME: This should be implemented, to support references like:
1214 // int B = AA[0][1];
1215 return 0;
1216}
1217
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001218RecTy *DefInit::getFieldType(const std::string &FieldName) const {
1219 if (const RecordVal *RV = Def->getValue(FieldName))
1220 return RV->getType();
1221 return 0;
1222}
1223
1224Init *DefInit::getFieldInit(Record &R, const std::string &FieldName) const {
1225 return Def->getValue(FieldName)->getValue();
1226}
1227
1228
Chris Lattner695506c2007-11-22 21:05:25 +00001229std::string DefInit::getAsString() const {
1230 return Def->getName();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001231}
1232
Chris Lattneref943742005-04-19 03:36:21 +00001233Init *FieldInit::resolveBitReference(Record &R, const RecordVal *RV,
1234 unsigned Bit) {
Chris Lattner577fc3f2004-07-27 01:01:21 +00001235 if (Init *BitsVal = Rec->getFieldInit(R, FieldName))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001236 if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
1237 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
1238 Init *B = BI->getBit(Bit);
Misha Brukman650ba8e2005-04-22 00:00:37 +00001239
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001240 if (dynamic_cast<BitInit*>(B)) // If the bit is set...
1241 return B; // Replace the VarBitInit with it.
1242 }
Chris Lattner577fc3f2004-07-27 01:01:21 +00001243 return 0;
1244}
1245
Chris Lattneref943742005-04-19 03:36:21 +00001246Init *FieldInit::resolveListElementReference(Record &R, const RecordVal *RV,
1247 unsigned Elt) {
Chris Lattner577fc3f2004-07-27 01:01:21 +00001248 if (Init *ListVal = Rec->getFieldInit(R, FieldName))
1249 if (ListInit *LI = dynamic_cast<ListInit*>(ListVal)) {
1250 if (Elt >= LI->getSize()) return 0;
1251 Init *E = LI->getElement(Elt);
1252
Bob Wilson67e6cab2009-11-22 03:58:57 +00001253 // If the element is set to some value, or if we are resolving a
1254 // reference to a specific variable and that variable is explicitly
1255 // unset, then replace the VarListElementInit with it.
1256 if (RV || !dynamic_cast<UnsetInit*>(E))
1257 return E;
Chris Lattner577fc3f2004-07-27 01:01:21 +00001258 }
1259 return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001260}
1261
Chris Lattneref943742005-04-19 03:36:21 +00001262Init *FieldInit::resolveReferences(Record &R, const RecordVal *RV) {
1263 Init *NewRec = RV ? Rec->resolveReferences(R, RV) : Rec;
1264
1265 Init *BitsVal = NewRec->getFieldInit(R, FieldName);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001266 if (BitsVal) {
Chris Lattneref943742005-04-19 03:36:21 +00001267 Init *BVR = BitsVal->resolveReferences(R, RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001268 return BVR->isComplete() ? BVR : this;
1269 }
Chris Lattneref943742005-04-19 03:36:21 +00001270
1271 if (NewRec != Rec) {
Chris Lattneref943742005-04-19 03:36:21 +00001272 return new FieldInit(NewRec, FieldName);
1273 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001274 return this;
1275}
1276
Chris Lattner0d3ef402006-01-31 06:02:35 +00001277Init *DagInit::resolveReferences(Record &R, const RecordVal *RV) {
1278 std::vector<Init*> NewArgs;
1279 for (unsigned i = 0, e = Args.size(); i != e; ++i)
1280 NewArgs.push_back(Args[i]->resolveReferences(R, RV));
Bob Wilson7248f862009-11-22 04:24:42 +00001281
Chris Lattnerb59cf3c2006-03-30 22:50:40 +00001282 Init *Op = Val->resolveReferences(R, RV);
Bob Wilson7248f862009-11-22 04:24:42 +00001283
Chris Lattnerb59cf3c2006-03-30 22:50:40 +00001284 if (Args != NewArgs || Op != Val)
Nate Begemandbe3f772009-03-19 05:21:56 +00001285 return new DagInit(Op, "", NewArgs, ArgNames);
Bob Wilson7248f862009-11-22 04:24:42 +00001286
Chris Lattner0d3ef402006-01-31 06:02:35 +00001287 return this;
1288}
1289
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001290
Chris Lattner695506c2007-11-22 21:05:25 +00001291std::string DagInit::getAsString() const {
1292 std::string Result = "(" + Val->getAsString();
Nate Begemandbe3f772009-03-19 05:21:56 +00001293 if (!ValName.empty())
1294 Result += ":" + ValName;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001295 if (Args.size()) {
Chris Lattner695506c2007-11-22 21:05:25 +00001296 Result += " " + Args[0]->getAsString();
1297 if (!ArgNames[0].empty()) Result += ":$" + ArgNames[0];
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001298 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
Chris Lattner695506c2007-11-22 21:05:25 +00001299 Result += ", " + Args[i]->getAsString();
1300 if (!ArgNames[i].empty()) Result += ":$" + ArgNames[i];
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001301 }
1302 }
Chris Lattner695506c2007-11-22 21:05:25 +00001303 return Result + ")";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001304}
1305
1306
1307//===----------------------------------------------------------------------===//
1308// Other implementations
1309//===----------------------------------------------------------------------===//
1310
1311RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
1312 : Name(N), Ty(T), Prefix(P) {
1313 Value = Ty->convertValue(new UnsetInit());
1314 assert(Value && "Cannot create unset value for current type!");
1315}
1316
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001317void RecordVal::dump() const { errs() << *this; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001318
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001319void RecordVal::print(raw_ostream &OS, bool PrintSem) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001320 if (getPrefix()) OS << "field ";
1321 OS << *getType() << " " << getName();
Chris Lattneref943742005-04-19 03:36:21 +00001322
1323 if (getValue())
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001324 OS << " = " << *getValue();
Chris Lattneref943742005-04-19 03:36:21 +00001325
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001326 if (PrintSem) OS << ";\n";
1327}
1328
Daniel Dunbarced00812009-08-23 09:47:37 +00001329unsigned Record::LastID = 0;
1330
Chris Lattnerac284252005-08-19 17:58:11 +00001331void Record::setName(const std::string &Name) {
1332 if (Records.getDef(getName()) == this) {
1333 Records.removeDef(getName());
1334 this->Name = Name;
1335 Records.addDef(this);
1336 } else {
1337 Records.removeClass(getName());
1338 this->Name = Name;
1339 Records.addClass(this);
1340 }
1341}
1342
Chris Lattneref943742005-04-19 03:36:21 +00001343/// resolveReferencesTo - If anything in this record refers to RV, replace the
1344/// reference to RV with the RHS of RV. If RV is null, we resolve all possible
1345/// references.
1346void Record::resolveReferencesTo(const RecordVal *RV) {
1347 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
1348 if (Init *V = Values[i].getValue())
1349 Values[i].setValue(V->resolveReferences(*this, RV));
1350 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001351}
1352
Chris Lattneref943742005-04-19 03:36:21 +00001353
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001354void Record::dump() const { errs() << *this; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001355
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001356raw_ostream &llvm::operator<<(raw_ostream &OS, const Record &R) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001357 OS << R.getName();
1358
1359 const std::vector<std::string> &TArgs = R.getTemplateArgs();
1360 if (!TArgs.empty()) {
1361 OS << "<";
1362 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1363 if (i) OS << ", ";
1364 const RecordVal *RV = R.getValue(TArgs[i]);
1365 assert(RV && "Template argument record not found??");
1366 RV->print(OS, false);
1367 }
1368 OS << ">";
1369 }
1370
1371 OS << " {";
1372 const std::vector<Record*> &SC = R.getSuperClasses();
1373 if (!SC.empty()) {
1374 OS << "\t//";
1375 for (unsigned i = 0, e = SC.size(); i != e; ++i)
1376 OS << " " << SC[i]->getName();
1377 }
1378 OS << "\n";
1379
1380 const std::vector<RecordVal> &Vals = R.getValues();
1381 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
1382 if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
1383 OS << Vals[i];
1384 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
1385 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
1386 OS << Vals[i];
1387
1388 return OS << "}\n";
1389}
1390
1391/// getValueInit - Return the initializer for a value with the specified name,
1392/// or throw an exception if the field does not exist.
1393///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001394Init *Record::getValueInit(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001395 const RecordVal *R = getValue(FieldName);
1396 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001397 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001398 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001399 return R->getValue();
1400}
1401
1402
1403/// getValueAsString - This method looks up the specified field and returns its
1404/// value as a string, throwing an exception if the field does not exist or if
1405/// the value is not a string.
1406///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001407std::string Record::getValueAsString(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001408 const RecordVal *R = getValue(FieldName);
1409 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001410 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001411 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001412
1413 if (const StringInit *SI = dynamic_cast<const StringInit*>(R->getValue()))
1414 return SI->getValue();
Chris Lattner42bb0c12009-09-18 18:31:37 +00001415 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001416 "' does not have a string initializer!";
1417}
1418
1419/// getValueAsBitsInit - This method looks up the specified field and returns
1420/// its value as a BitsInit, throwing an exception if the field does not exist
1421/// or if the value is not the right type.
1422///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001423BitsInit *Record::getValueAsBitsInit(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001424 const RecordVal *R = getValue(FieldName);
1425 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001426 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001427 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001428
1429 if (BitsInit *BI = dynamic_cast<BitsInit*>(R->getValue()))
1430 return BI;
Chris Lattner42bb0c12009-09-18 18:31:37 +00001431 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001432 "' does not have a BitsInit initializer!";
1433}
1434
1435/// getValueAsListInit - This method looks up the specified field and returns
1436/// its value as a ListInit, throwing an exception if the field does not exist
1437/// or if the value is not the right type.
1438///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001439ListInit *Record::getValueAsListInit(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001440 const RecordVal *R = getValue(FieldName);
1441 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001442 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001443 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001444
1445 if (ListInit *LI = dynamic_cast<ListInit*>(R->getValue()))
1446 return LI;
Chris Lattner42bb0c12009-09-18 18:31:37 +00001447 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001448 "' does not have a list initializer!";
1449}
1450
Chris Lattner7ad0bed2005-10-28 22:49:02 +00001451/// getValueAsListOfDefs - This method looks up the specified field and returns
Jim Laskeyb04feb62005-10-28 21:46:31 +00001452/// its value as a vector of records, throwing an exception if the field does
1453/// not exist or if the value is not the right type.
1454///
Bob Wilson7248f862009-11-22 04:24:42 +00001455std::vector<Record*>
Chris Lattner42bb0c12009-09-18 18:31:37 +00001456Record::getValueAsListOfDefs(StringRef FieldName) const {
Jim Laskeyb04feb62005-10-28 21:46:31 +00001457 ListInit *List = getValueAsListInit(FieldName);
1458 std::vector<Record*> Defs;
1459 for (unsigned i = 0; i < List->getSize(); i++) {
1460 if (DefInit *DI = dynamic_cast<DefInit*>(List->getElement(i))) {
1461 Defs.push_back(DI->getDef());
1462 } else {
Chris Lattner42bb0c12009-09-18 18:31:37 +00001463 throw "Record `" + getName() + "', field `" + FieldName.str() +
Jim Laskeyb04feb62005-10-28 21:46:31 +00001464 "' list is not entirely DefInit!";
1465 }
1466 }
1467 return Defs;
1468}
1469
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001470/// getValueAsInt - This method looks up the specified field and returns its
Dan Gohmanca0546f2008-10-17 01:33:43 +00001471/// value as an int64_t, throwing an exception if the field does not exist or if
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001472/// the value is not the right type.
1473///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001474int64_t Record::getValueAsInt(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001475 const RecordVal *R = getValue(FieldName);
1476 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001477 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001478 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001479
1480 if (IntInit *II = dynamic_cast<IntInit*>(R->getValue()))
1481 return II->getValue();
Chris Lattner42bb0c12009-09-18 18:31:37 +00001482 throw "Record `" + getName() + "', field `" + FieldName.str() +
Nate Begemanf621b332005-11-30 18:37:14 +00001483 "' does not have an int initializer!";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001484}
1485
Anton Korobeynikova468a112007-11-11 11:19:37 +00001486/// getValueAsListOfInts - This method looks up the specified field and returns
1487/// its value as a vector of integers, throwing an exception if the field does
1488/// not exist or if the value is not the right type.
1489///
Bob Wilson7248f862009-11-22 04:24:42 +00001490std::vector<int64_t>
Chris Lattner42bb0c12009-09-18 18:31:37 +00001491Record::getValueAsListOfInts(StringRef FieldName) const {
Anton Korobeynikova468a112007-11-11 11:19:37 +00001492 ListInit *List = getValueAsListInit(FieldName);
Dan Gohmanca0546f2008-10-17 01:33:43 +00001493 std::vector<int64_t> Ints;
Anton Korobeynikova468a112007-11-11 11:19:37 +00001494 for (unsigned i = 0; i < List->getSize(); i++) {
1495 if (IntInit *II = dynamic_cast<IntInit*>(List->getElement(i))) {
1496 Ints.push_back(II->getValue());
1497 } else {
Chris Lattner42bb0c12009-09-18 18:31:37 +00001498 throw "Record `" + getName() + "', field `" + FieldName.str() +
Anton Korobeynikova468a112007-11-11 11:19:37 +00001499 "' does not have a list of ints initializer!";
1500 }
1501 }
1502 return Ints;
1503}
1504
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001505/// getValueAsDef - This method looks up the specified field and returns its
1506/// value as a Record, throwing an exception if the field does not exist or if
1507/// the value is not the right type.
1508///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001509Record *Record::getValueAsDef(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001510 const RecordVal *R = getValue(FieldName);
1511 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001512 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001513 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001514
1515 if (DefInit *DI = dynamic_cast<DefInit*>(R->getValue()))
1516 return DI->getDef();
Chris Lattner42bb0c12009-09-18 18:31:37 +00001517 throw "Record `" + getName() + "', field `" + FieldName.str() +
Nate Begemanf621b332005-11-30 18:37:14 +00001518 "' does not have a def initializer!";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001519}
1520
1521/// getValueAsBit - This method looks up the specified field and returns its
1522/// value as a bit, throwing an exception if the field does not exist or if
1523/// the value is not the right type.
1524///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001525bool Record::getValueAsBit(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001526 const RecordVal *R = getValue(FieldName);
1527 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001528 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001529 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001530
1531 if (BitInit *BI = dynamic_cast<BitInit*>(R->getValue()))
1532 return BI->getValue();
Chris Lattner42bb0c12009-09-18 18:31:37 +00001533 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001534 "' does not have a bit initializer!";
1535}
1536
1537/// getValueAsDag - This method looks up the specified field and returns its
1538/// value as an Dag, throwing an exception if the field does not exist or if
1539/// the value is not the right type.
1540///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001541DagInit *Record::getValueAsDag(StringRef FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001542 const RecordVal *R = getValue(FieldName);
1543 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001544 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001545 FieldName.str() + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001546
1547 if (DagInit *DI = dynamic_cast<DagInit*>(R->getValue()))
1548 return DI;
Chris Lattner42bb0c12009-09-18 18:31:37 +00001549 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001550 "' does not have a dag initializer!";
1551}
1552
Chris Lattner42bb0c12009-09-18 18:31:37 +00001553std::string Record::getValueAsCode(StringRef FieldName) const {
Chris Lattnerae939eb2005-09-13 21:44:28 +00001554 const RecordVal *R = getValue(FieldName);
1555 if (R == 0 || R->getValue() == 0)
1556 throw "Record `" + getName() + "' does not have a field named `" +
Chris Lattner42bb0c12009-09-18 18:31:37 +00001557 FieldName.str() + "'!\n";
Bob Wilson7248f862009-11-22 04:24:42 +00001558
Chris Lattnerae939eb2005-09-13 21:44:28 +00001559 if (const CodeInit *CI = dynamic_cast<const CodeInit*>(R->getValue()))
1560 return CI->getValue();
Chris Lattner42bb0c12009-09-18 18:31:37 +00001561 throw "Record `" + getName() + "', field `" + FieldName.str() +
Chris Lattnerae939eb2005-09-13 21:44:28 +00001562 "' does not have a code initializer!";
1563}
1564
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001565
David Greene7049e792009-04-24 16:55:41 +00001566void MultiClass::dump() const {
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001567 errs() << "Record:\n";
David Greene7049e792009-04-24 16:55:41 +00001568 Rec.dump();
Bob Wilson7248f862009-11-22 04:24:42 +00001569
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001570 errs() << "Defs:\n";
David Greene7049e792009-04-24 16:55:41 +00001571 for (RecordVector::const_iterator r = DefPrototypes.begin(),
1572 rend = DefPrototypes.end();
1573 r != rend;
1574 ++r) {
1575 (*r)->dump();
1576 }
1577}
1578
1579
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001580void RecordKeeper::dump() const { errs() << *this; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001581
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001582raw_ostream &llvm::operator<<(raw_ostream &OS, const RecordKeeper &RK) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001583 OS << "------------- Classes -----------------\n";
1584 const std::map<std::string, Record*> &Classes = RK.getClasses();
1585 for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001586 E = Classes.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001587 OS << "class " << *I->second;
Misha Brukman650ba8e2005-04-22 00:00:37 +00001588
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001589 OS << "------------- Defs -----------------\n";
1590 const std::map<std::string, Record*> &Defs = RK.getDefs();
1591 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001592 E = Defs.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001593 OS << "def " << *I->second;
1594 return OS;
1595}
1596
1597
1598/// getAllDerivedDefinitions - This method returns all concrete definitions
1599/// that derive from the specified class name. If a class with the specified
1600/// name does not exist, an error is printed and true is returned.
1601std::vector<Record*>
1602RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
1603 Record *Class = Records.getClass(ClassName);
1604 if (!Class)
Misha Brukman41f9f292004-10-08 14:59:05 +00001605 throw "ERROR: Couldn't find the `" + ClassName + "' class!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001606
1607 std::vector<Record*> Defs;
1608 for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(),
1609 E = getDefs().end(); I != E; ++I)
1610 if (I->second->isSubClassOf(Class))
1611 Defs.push_back(I->second);
1612
1613 return Defs;
1614}
Brian Gaeke960707c2003-11-11 22:41:34 +00001615