blob: fc2de1c135ad379331adf4617030b8321f18db03 [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"
Bill Wendling9bfb1e12006-12-07 22:21:48 +000016#include "llvm/Support/Streams.h"
Chris Lattner8b9ecda2007-11-20 22:25:16 +000017#include "llvm/ADT/StringExtras.h"
Duraid Madina14492af2005-12-26 05:08:55 +000018#include <ios>
19
Chris Lattner68478662004-08-01 03:55:39 +000020using namespace llvm;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000021
22//===----------------------------------------------------------------------===//
23// Type implementations
24//===----------------------------------------------------------------------===//
25
Bill Wendling9bfb1e12006-12-07 22:21:48 +000026void RecTy::dump() const { print(*cerr.stream()); }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000027
28Init *BitRecTy::convertValue(BitsInit *BI) {
29 if (BI->getNumBits() != 1) return 0; // Only accept if just one bit!
30 return BI->getBit(0);
31}
32
33bool BitRecTy::baseClassOf(const BitsRecTy *RHS) const {
34 return RHS->getNumBits() == 1;
35}
36
37Init *BitRecTy::convertValue(IntInit *II) {
Dan Gohmanca0546f2008-10-17 01:33:43 +000038 int64_t Val = II->getValue();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000039 if (Val != 0 && Val != 1) return 0; // Only accept 0 or 1 for a bit!
Misha Brukman650ba8e2005-04-22 00:00:37 +000040
41 return new BitInit(Val != 0);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000042}
43
44Init *BitRecTy::convertValue(TypedInit *VI) {
45 if (dynamic_cast<BitRecTy*>(VI->getType()))
46 return VI; // Accept variable if it is already of bit type!
47 return 0;
48}
49
Chris Lattner8b9ecda2007-11-20 22:25:16 +000050std::string BitsRecTy::getAsString() const {
51 return "bits<" + utostr(Size) + ">";
52}
53
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000054Init *BitsRecTy::convertValue(UnsetInit *UI) {
55 BitsInit *Ret = new BitsInit(Size);
56
57 for (unsigned i = 0; i != Size; ++i)
58 Ret->setBit(i, new UnsetInit());
59 return Ret;
60}
61
62Init *BitsRecTy::convertValue(BitInit *UI) {
63 if (Size != 1) return 0; // Can only convert single bit...
64 BitsInit *Ret = new BitsInit(1);
65 Ret->setBit(0, UI);
66 return Ret;
67}
68
69// convertValue from Int initializer to bits type: Split the integer up into the
70// appropriate bits...
71//
72Init *BitsRecTy::convertValue(IntInit *II) {
Misha Brukman6752fb52004-06-21 18:01:47 +000073 int64_t Value = II->getValue();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000074 // Make sure this bitfield is large enough to hold the integer value...
75 if (Value >= 0) {
Misha Brukman6752fb52004-06-21 18:01:47 +000076 if (Value & ~((1LL << Size)-1))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000077 return 0;
78 } else {
Jeff Cohen0add83e2006-02-18 03:20:33 +000079 if ((Value >> Size) != -1 || ((Value & (1LL << (Size-1))) == 0))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000080 return 0;
81 }
82
83 BitsInit *Ret = new BitsInit(Size);
84 for (unsigned i = 0; i != Size; ++i)
Jeff Cohen0add83e2006-02-18 03:20:33 +000085 Ret->setBit(i, new BitInit(Value & (1LL << i)));
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000086
87 return Ret;
88}
89
90Init *BitsRecTy::convertValue(BitsInit *BI) {
91 // If the number of bits is right, return it. Otherwise we need to expand or
92 // truncate...
93 if (BI->getNumBits() == Size) return BI;
94 return 0;
95}
96
97Init *BitsRecTy::convertValue(TypedInit *VI) {
98 if (BitsRecTy *BRT = dynamic_cast<BitsRecTy*>(VI->getType()))
99 if (BRT->Size == Size) {
100 BitsInit *Ret = new BitsInit(Size);
101 for (unsigned i = 0; i != Size; ++i)
Jeff Cohen88e7b722005-04-22 04:13:13 +0000102 Ret->setBit(i, new VarBitInit(VI, i));
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000103 return Ret;
104 }
105 if (Size == 1 && dynamic_cast<BitRecTy*>(VI->getType())) {
106 BitsInit *Ret = new BitsInit(1);
107 Ret->setBit(0, VI);
108 return Ret;
109 }
Misha Brukman650ba8e2005-04-22 00:00:37 +0000110
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000111 return 0;
112}
113
114Init *IntRecTy::convertValue(BitInit *BI) {
115 return new IntInit(BI->getValue());
116}
117
118Init *IntRecTy::convertValue(BitsInit *BI) {
Dan Gohmanca0546f2008-10-17 01:33:43 +0000119 int64_t Result = 0;
Misha Brukman650ba8e2005-04-22 00:00:37 +0000120 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000121 if (BitInit *Bit = dynamic_cast<BitInit*>(BI->getBit(i))) {
122 Result |= Bit->getValue() << i;
123 } else {
124 return 0;
125 }
126 return new IntInit(Result);
127}
128
129Init *IntRecTy::convertValue(TypedInit *TI) {
130 if (TI->getType()->typeIsConvertibleTo(this))
131 return TI; // Accept variable if already of the right type!
132 return 0;
133}
134
David Greenee8f3b272009-05-14 21:22:49 +0000135Init *StringRecTy::convertValue(UnOpInit *BO) {
136 if (BO->getOpcode() == UnOpInit::CAST) {
137 Init *L = BO->getOperand()->convertInitializerTo(this);
138 if (L == 0) return 0;
139 if (L != BO->getOperand())
140 return new UnOpInit(UnOpInit::CAST, L, new StringRecTy);
141 return BO;
142 }
David Greene5d0c0512009-05-14 20:54:48 +0000143
David Greenee8f3b272009-05-14 21:22:49 +0000144 return convertValue((TypedInit*)BO);
145}
David Greene5d0c0512009-05-14 20:54:48 +0000146
Chris Lattner51ffbf12006-03-31 21:53:49 +0000147Init *StringRecTy::convertValue(BinOpInit *BO) {
148 if (BO->getOpcode() == BinOpInit::STRCONCAT) {
149 Init *L = BO->getLHS()->convertInitializerTo(this);
150 Init *R = BO->getRHS()->convertInitializerTo(this);
151 if (L == 0 || R == 0) return 0;
152 if (L != BO->getLHS() || R != BO->getRHS())
David Greene196ac3c2009-04-23 21:25:15 +0000153 return new BinOpInit(BinOpInit::STRCONCAT, L, R, new StringRecTy);
Chris Lattner51ffbf12006-03-31 21:53:49 +0000154 return BO;
155 }
David Greene196ac3c2009-04-23 21:25:15 +0000156 if (BO->getOpcode() == BinOpInit::NAMECONCAT) {
157 if (BO->getType()->getAsString() == getAsString()) {
158 Init *L = BO->getLHS()->convertInitializerTo(this);
159 Init *R = BO->getRHS()->convertInitializerTo(this);
160 if (L == 0 || R == 0) return 0;
161 if (L != BO->getLHS() || R != BO->getRHS())
162 return new BinOpInit(BinOpInit::NAMECONCAT, L, R, new StringRecTy);
163 return BO;
164 }
165 }
166
167 return convertValue((TypedInit*)BO);
Chris Lattner51ffbf12006-03-31 21:53:49 +0000168}
169
170
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000171Init *StringRecTy::convertValue(TypedInit *TI) {
172 if (dynamic_cast<StringRecTy*>(TI->getType()))
173 return TI; // Accept variable if already of the right type!
174 return 0;
175}
176
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000177std::string ListRecTy::getAsString() const {
178 return "list<" + Ty->getAsString() + ">";
179}
180
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000181Init *ListRecTy::convertValue(ListInit *LI) {
182 std::vector<Init*> Elements;
183
184 // Verify that all of the elements of the list are subclasses of the
185 // appropriate class!
186 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
187 if (Init *CI = LI->getElement(i)->convertInitializerTo(Ty))
188 Elements.push_back(CI);
189 else
190 return 0;
191
192 return new ListInit(Elements);
193}
194
195Init *ListRecTy::convertValue(TypedInit *TI) {
196 // Ensure that TI is compatible with our class.
197 if (ListRecTy *LRT = dynamic_cast<ListRecTy*>(TI->getType()))
198 if (LRT->getElementType()->typeIsConvertibleTo(getElementType()))
199 return TI;
200 return 0;
201}
202
203Init *CodeRecTy::convertValue(TypedInit *TI) {
204 if (TI->getType()->typeIsConvertibleTo(this))
205 return TI;
206 return 0;
207}
208
209Init *DagRecTy::convertValue(TypedInit *TI) {
210 if (TI->getType()->typeIsConvertibleTo(this))
211 return TI;
212 return 0;
213}
214
David Greenee8f3b272009-05-14 21:22:49 +0000215Init *DagRecTy::convertValue(UnOpInit *BO) {
216 if (BO->getOpcode() == UnOpInit::CAST) {
217 Init *L = BO->getOperand()->convertInitializerTo(this);
218 if (L == 0) return 0;
219 if (L != BO->getOperand())
220 return new UnOpInit(UnOpInit::CAST, L, new DagRecTy);
221 return BO;
222 }
223 return 0;
224}
David Greene5d0c0512009-05-14 20:54:48 +0000225
Evan Chenga32dee22007-05-15 01:23:24 +0000226Init *DagRecTy::convertValue(BinOpInit *BO) {
227 if (BO->getOpcode() == BinOpInit::CONCAT) {
228 Init *L = BO->getLHS()->convertInitializerTo(this);
229 Init *R = BO->getRHS()->convertInitializerTo(this);
230 if (L == 0 || R == 0) return 0;
231 if (L != BO->getLHS() || R != BO->getRHS())
David Greene196ac3c2009-04-23 21:25:15 +0000232 return new BinOpInit(BinOpInit::CONCAT, L, R, new DagRecTy);
Evan Chenga32dee22007-05-15 01:23:24 +0000233 return BO;
234 }
David Greene196ac3c2009-04-23 21:25:15 +0000235 if (BO->getOpcode() == BinOpInit::NAMECONCAT) {
236 if (BO->getType()->getAsString() == getAsString()) {
237 Init *L = BO->getLHS()->convertInitializerTo(this);
238 Init *R = BO->getRHS()->convertInitializerTo(this);
239 if (L == 0 || R == 0) return 0;
240 if (L != BO->getLHS() || R != BO->getRHS())
241 return new BinOpInit(BinOpInit::CONCAT, L, R, new DagRecTy);
242 return BO;
243 }
244 }
Evan Chenga32dee22007-05-15 01:23:24 +0000245 return 0;
246}
247
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000248std::string RecordRecTy::getAsString() const {
249 return Rec->getName();
250}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000251
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000252Init *RecordRecTy::convertValue(DefInit *DI) {
253 // Ensure that DI is a subclass of Rec.
254 if (!DI->getDef()->isSubClassOf(Rec))
255 return 0;
256 return DI;
257}
258
259Init *RecordRecTy::convertValue(TypedInit *TI) {
260 // Ensure that TI is compatible with Rec.
261 if (RecordRecTy *RRT = dynamic_cast<RecordRecTy*>(TI->getType()))
262 if (RRT->getRecord()->isSubClassOf(getRecord()) ||
263 RRT->getRecord() == getRecord())
264 return TI;
265 return 0;
266}
267
268bool RecordRecTy::baseClassOf(const RecordRecTy *RHS) const {
269 return Rec == RHS->getRecord() || RHS->getRecord()->isSubClassOf(Rec);
270}
271
272
273//===----------------------------------------------------------------------===//
274// Initializer implementations
275//===----------------------------------------------------------------------===//
276
Bill Wendling9bfb1e12006-12-07 22:21:48 +0000277void Init::dump() const { return print(*cerr.stream()); }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000278
279Init *BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
280 BitsInit *BI = new BitsInit(Bits.size());
281 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
282 if (Bits[i] >= getNumBits()) {
283 delete BI;
284 return 0;
285 }
286 BI->setBit(i, getBit(Bits[i]));
287 }
288 return BI;
289}
290
Chris Lattner695506c2007-11-22 21:05:25 +0000291std::string BitsInit::getAsString() const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000292 //if (!printInHex(OS)) return;
293 //if (!printAsVariable(OS)) return;
294 //if (!printAsUnset(OS)) return;
295
Chris Lattner695506c2007-11-22 21:05:25 +0000296 std::string Result = "{ ";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000297 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
Chris Lattner695506c2007-11-22 21:05:25 +0000298 if (i) Result += ", ";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000299 if (Init *Bit = getBit(e-i-1))
Chris Lattner695506c2007-11-22 21:05:25 +0000300 Result += Bit->getAsString();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000301 else
Chris Lattner695506c2007-11-22 21:05:25 +0000302 Result += "*";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000303 }
Chris Lattner695506c2007-11-22 21:05:25 +0000304 return Result + " }";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000305}
306
307bool BitsInit::printInHex(std::ostream &OS) const {
308 // First, attempt to convert the value into an integer value...
Dan Gohmanca0546f2008-10-17 01:33:43 +0000309 int64_t Result = 0;
Misha Brukman650ba8e2005-04-22 00:00:37 +0000310 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000311 if (BitInit *Bit = dynamic_cast<BitInit*>(getBit(i))) {
312 Result |= Bit->getValue() << i;
313 } else {
314 return true;
315 }
316
317 OS << "0x" << std::hex << Result << std::dec;
318 return false;
319}
320
321bool BitsInit::printAsVariable(std::ostream &OS) const {
322 // Get the variable that we may be set equal to...
323 assert(getNumBits() != 0);
324 VarBitInit *FirstBit = dynamic_cast<VarBitInit*>(getBit(0));
325 if (FirstBit == 0) return true;
326 TypedInit *Var = FirstBit->getVariable();
327
328 // Check to make sure the types are compatible.
329 BitsRecTy *Ty = dynamic_cast<BitsRecTy*>(FirstBit->getVariable()->getType());
330 if (Ty == 0) return true;
331 if (Ty->getNumBits() != getNumBits()) return true; // Incompatible types!
332
333 // Check to make sure all bits are referring to the right bits in the variable
334 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
335 VarBitInit *Bit = dynamic_cast<VarBitInit*>(getBit(i));
336 if (Bit == 0 || Bit->getVariable() != Var || Bit->getBitNum() != i)
337 return true;
338 }
339
340 Var->print(OS);
341 return false;
342}
343
344bool BitsInit::printAsUnset(std::ostream &OS) const {
Misha Brukman650ba8e2005-04-22 00:00:37 +0000345 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000346 if (!dynamic_cast<UnsetInit*>(getBit(i)))
347 return true;
348 OS << "?";
349 return false;
350}
351
352// resolveReferences - If there are any field references that refer to fields
353// that have been filled in, we can propagate the values now.
354//
Chris Lattneref943742005-04-19 03:36:21 +0000355Init *BitsInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000356 bool Changed = false;
357 BitsInit *New = new BitsInit(getNumBits());
358
359 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
360 Init *B;
361 Init *CurBit = getBit(i);
362
363 do {
364 B = CurBit;
Chris Lattneref943742005-04-19 03:36:21 +0000365 CurBit = CurBit->resolveReferences(R, RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000366 Changed |= B != CurBit;
367 } while (B != CurBit);
368 New->setBit(i, CurBit);
369 }
370
371 if (Changed)
372 return New;
373 delete New;
374 return this;
375}
376
Chris Lattner695506c2007-11-22 21:05:25 +0000377std::string IntInit::getAsString() const {
378 return itostr(Value);
379}
380
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000381Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
382 BitsInit *BI = new BitsInit(Bits.size());
383
384 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
Dan Gohmanca0546f2008-10-17 01:33:43 +0000385 if (Bits[i] >= 64) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000386 delete BI;
387 return 0;
388 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000389 BI->setBit(i, new BitInit(Value & (INT64_C(1) << Bits[i])));
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000390 }
391 return BI;
392}
393
Chris Lattner8bf9e062004-07-26 23:21:34 +0000394Init *ListInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
395 std::vector<Init*> Vals;
396 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
397 if (Elements[i] >= getSize())
398 return 0;
399 Vals.push_back(getElement(Elements[i]));
400 }
401 return new ListInit(Vals);
402}
403
Chris Lattnercbebe462007-02-27 22:08:27 +0000404Record *ListInit::getElementAsRecord(unsigned i) const {
405 assert(i < Values.size() && "List element index out of range!");
406 DefInit *DI = dynamic_cast<DefInit*>(Values[i]);
407 if (DI == 0) throw "Expected record in list!";
408 return DI->getDef();
409}
410
Chris Lattneref943742005-04-19 03:36:21 +0000411Init *ListInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattner577fc3f2004-07-27 01:01:21 +0000412 std::vector<Init*> Resolved;
413 Resolved.reserve(getSize());
414 bool Changed = false;
415
416 for (unsigned i = 0, e = getSize(); i != e; ++i) {
417 Init *E;
418 Init *CurElt = getElement(i);
419
420 do {
421 E = CurElt;
Chris Lattneref943742005-04-19 03:36:21 +0000422 CurElt = CurElt->resolveReferences(R, RV);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000423 Changed |= E != CurElt;
424 } while (E != CurElt);
425 Resolved.push_back(E);
426 }
427
428 if (Changed)
429 return new ListInit(Resolved);
430 return this;
431}
432
Chris Lattner695506c2007-11-22 21:05:25 +0000433std::string ListInit::getAsString() const {
434 std::string Result = "[";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000435 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
Chris Lattner695506c2007-11-22 21:05:25 +0000436 if (i) Result += ", ";
437 Result += Values[i]->getAsString();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000438 }
Chris Lattner695506c2007-11-22 21:05:25 +0000439 return Result + "]";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000440}
441
David Greene5d0c0512009-05-14 20:54:48 +0000442Init *OpInit::resolveBitReference(Record &R, const RecordVal *IRV,
443 unsigned Bit) {
444 Init *Folded = Fold(&R, 0);
445
446 if (Folded != this) {
447 TypedInit *Typed = dynamic_cast<TypedInit *>(Folded);
448 if (Typed) {
449 return Typed->resolveBitReference(R, IRV, Bit);
450 }
451 }
452
453 return 0;
454}
455
456Init *OpInit::resolveListElementReference(Record &R, const RecordVal *IRV,
457 unsigned Elt) {
458 Init *Folded = Fold(&R, 0);
459
460 if (Folded != this) {
461 TypedInit *Typed = dynamic_cast<TypedInit *>(Folded);
462 if (Typed) {
463 return Typed->resolveListElementReference(R, IRV, Elt);
464 }
465 }
466
467 return 0;
468}
469
David Greenee8f3b272009-05-14 21:22:49 +0000470Init *UnOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
471 switch (getOpcode()) {
472 default: assert(0 && "Unknown unop");
473 case CAST: {
474 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
475 if (LHSs) {
476 std::string Name = LHSs->getValue();
David Greene5d0c0512009-05-14 20:54:48 +0000477
David Greenee8f3b272009-05-14 21:22:49 +0000478 // From TGParser::ParseIDValue
479 if (CurRec) {
480 if (const RecordVal *RV = CurRec->getValue(Name)) {
481 if (RV->getType() != getType()) {
482 throw "type mismatch in nameconcat";
483 }
484 return new VarInit(Name, RV->getType());
485 }
David Greene5d0c0512009-05-14 20:54:48 +0000486
David Greenee8f3b272009-05-14 21:22:49 +0000487 std::string TemplateArgName = CurRec->getName()+":"+Name;
488 if (CurRec->isTemplateArg(TemplateArgName)) {
489 const RecordVal *RV = CurRec->getValue(TemplateArgName);
490 assert(RV && "Template arg doesn't exist??");
David Greene5d0c0512009-05-14 20:54:48 +0000491
David Greenee8f3b272009-05-14 21:22:49 +0000492 if (RV->getType() != getType()) {
493 throw "type mismatch in nameconcat";
494 }
David Greene5d0c0512009-05-14 20:54:48 +0000495
David Greenee8f3b272009-05-14 21:22:49 +0000496 return new VarInit(TemplateArgName, RV->getType());
497 }
498 }
David Greene5d0c0512009-05-14 20:54:48 +0000499
David Greenee8f3b272009-05-14 21:22:49 +0000500 if (CurMultiClass) {
501 std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
502 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
503 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
504 assert(RV && "Template arg doesn't exist??");
David Greene5d0c0512009-05-14 20:54:48 +0000505
David Greenee8f3b272009-05-14 21:22:49 +0000506 if (RV->getType() != getType()) {
507 throw "type mismatch in nameconcat";
508 }
David Greene5d0c0512009-05-14 20:54:48 +0000509
David Greenee8f3b272009-05-14 21:22:49 +0000510 return new VarInit(MCName, RV->getType());
511 }
512 }
David Greene5d0c0512009-05-14 20:54:48 +0000513
David Greenee8f3b272009-05-14 21:22:49 +0000514 if (Record *D = Records.getDef(Name))
515 return new DefInit(D);
David Greene5d0c0512009-05-14 20:54:48 +0000516
David Greenee8f3b272009-05-14 21:22:49 +0000517 cerr << "Variable not defined: '" + Name + "'\n";
518 assert(0 && "Variable not found");
519 return 0;
520 }
521 break;
522 }
David Greened571b3c2009-05-14 22:38:31 +0000523 case CAR: {
524 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
525 if (LHSl) {
526 if (LHSl->getSize() == 0) {
527 assert(0 && "Empty list in car");
528 return 0;
529 }
530 return LHSl->getElement(0);
531 }
532 break;
533 }
534 case CDR: {
535 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
536 if (LHSl) {
537 if (LHSl->getSize() == 0) {
538 assert(0 && "Empty list in cdr");
539 return 0;
540 }
541 ListInit *Result = new ListInit(LHSl->begin()+1, LHSl->end());
542 return Result;
543 }
544 break;
545 }
546 case LNULL: {
547 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
548 if (LHSl) {
549 if (LHSl->getSize() == 0) {
550 return new IntInit(1);
551 }
552 else {
553 return new IntInit(0);
554 }
555 }
556 break;
557 }
David Greenee8f3b272009-05-14 21:22:49 +0000558 }
559 return this;
560}
David Greene5d0c0512009-05-14 20:54:48 +0000561
David Greenee8f3b272009-05-14 21:22:49 +0000562Init *UnOpInit::resolveReferences(Record &R, const RecordVal *RV) {
563 Init *lhs = LHS->resolveReferences(R, RV);
David Greene5d0c0512009-05-14 20:54:48 +0000564
David Greenee8f3b272009-05-14 21:22:49 +0000565 if (LHS != lhs)
566 return (new UnOpInit(getOpcode(), lhs, getType()))->Fold(&R, 0);
567 return Fold(&R, 0);
568}
David Greene5d0c0512009-05-14 20:54:48 +0000569
David Greenee8f3b272009-05-14 21:22:49 +0000570std::string UnOpInit::getAsString() const {
571 std::string Result;
572 switch (Opc) {
573 case CAST: Result = "!cast<" + getType()->getAsString() + ">"; break;
David Greened571b3c2009-05-14 22:38:31 +0000574 case CAR: Result = "!car"; break;
575 case CDR: Result = "!cdr"; break;
576 case LNULL: Result = "!null"; break;
David Greenee8f3b272009-05-14 21:22:49 +0000577 }
578 return Result + "(" + LHS->getAsString() + ")";
579}
David Greene5d0c0512009-05-14 20:54:48 +0000580
David Greenea9c6c5d2009-04-22 20:18:10 +0000581Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
Chris Lattner51ffbf12006-03-31 21:53:49 +0000582 switch (getOpcode()) {
583 default: assert(0 && "Unknown binop");
Evan Chenga32dee22007-05-15 01:23:24 +0000584 case CONCAT: {
585 DagInit *LHSs = dynamic_cast<DagInit*>(LHS);
586 DagInit *RHSs = dynamic_cast<DagInit*>(RHS);
587 if (LHSs && RHSs) {
588 DefInit *LOp = dynamic_cast<DefInit*>(LHSs->getOperator());
589 DefInit *ROp = dynamic_cast<DefInit*>(RHSs->getOperator());
Evan Cheng94b5a802007-07-19 01:14:50 +0000590 if (LOp->getDef() != ROp->getDef()) {
591 bool LIsOps =
592 LOp->getDef()->getName() == "outs" ||
593 LOp->getDef()->getName() != "ins" ||
594 LOp->getDef()->getName() != "defs";
595 bool RIsOps =
596 ROp->getDef()->getName() == "outs" ||
597 ROp->getDef()->getName() != "ins" ||
598 ROp->getDef()->getName() != "defs";
599 if (!LIsOps || !RIsOps)
600 throw "Concated Dag operators do not match!";
601 }
Evan Chenga32dee22007-05-15 01:23:24 +0000602 std::vector<Init*> Args;
603 std::vector<std::string> ArgNames;
604 for (unsigned i = 0, e = LHSs->getNumArgs(); i != e; ++i) {
605 Args.push_back(LHSs->getArg(i));
606 ArgNames.push_back(LHSs->getArgName(i));
607 }
608 for (unsigned i = 0, e = RHSs->getNumArgs(); i != e; ++i) {
609 Args.push_back(RHSs->getArg(i));
610 ArgNames.push_back(RHSs->getArgName(i));
611 }
Nate Begemandbe3f772009-03-19 05:21:56 +0000612 return new DagInit(LHSs->getOperator(), "", Args, ArgNames);
Evan Chenga32dee22007-05-15 01:23:24 +0000613 }
614 break;
615 }
Chris Lattner51ffbf12006-03-31 21:53:49 +0000616 case STRCONCAT: {
617 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
618 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
619 if (LHSs && RHSs)
620 return new StringInit(LHSs->getValue() + RHSs->getValue());
621 break;
622 }
David Greenea9c6c5d2009-04-22 20:18:10 +0000623 case NAMECONCAT: {
624 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
625 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
626 if (LHSs && RHSs) {
627 std::string Name(LHSs->getValue() + RHSs->getValue());
628
629 // From TGParser::ParseIDValue
630 if (CurRec) {
David Greene196ac3c2009-04-23 21:25:15 +0000631 if (const RecordVal *RV = CurRec->getValue(Name)) {
632 if (RV->getType() != getType()) {
633 throw "type mismatch in nameconcat";
634 }
David Greenea9c6c5d2009-04-22 20:18:10 +0000635 return new VarInit(Name, RV->getType());
David Greene196ac3c2009-04-23 21:25:15 +0000636 }
637
David Greenea9c6c5d2009-04-22 20:18:10 +0000638 std::string TemplateArgName = CurRec->getName()+":"+Name;
639 if (CurRec->isTemplateArg(TemplateArgName)) {
640 const RecordVal *RV = CurRec->getValue(TemplateArgName);
641 assert(RV && "Template arg doesn't exist??");
David Greene196ac3c2009-04-23 21:25:15 +0000642
643 if (RV->getType() != getType()) {
644 throw "type mismatch in nameconcat";
645 }
646
David Greenea9c6c5d2009-04-22 20:18:10 +0000647 return new VarInit(TemplateArgName, RV->getType());
648 }
649 }
650
651 if (CurMultiClass) {
652 std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
653 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
654 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
655 assert(RV && "Template arg doesn't exist??");
David Greene196ac3c2009-04-23 21:25:15 +0000656
657 if (RV->getType() != getType()) {
658 throw "type mismatch in nameconcat";
659 }
660
David Greenea9c6c5d2009-04-22 20:18:10 +0000661 return new VarInit(MCName, RV->getType());
662 }
663 }
664
665 if (Record *D = Records.getDef(Name))
666 return new DefInit(D);
667
668 cerr << "Variable not defined: '" + Name + "'\n";
669 assert(0 && "Variable not found");
670 return 0;
671 }
672 break;
673 }
Chris Lattner51ffbf12006-03-31 21:53:49 +0000674 case SHL:
675 case SRA:
676 case SRL: {
677 IntInit *LHSi = dynamic_cast<IntInit*>(LHS);
678 IntInit *RHSi = dynamic_cast<IntInit*>(RHS);
679 if (LHSi && RHSi) {
Dan Gohmanca0546f2008-10-17 01:33:43 +0000680 int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
681 int64_t Result;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000682 switch (getOpcode()) {
683 default: assert(0 && "Bad opcode!");
684 case SHL: Result = LHSv << RHSv; break;
685 case SRA: Result = LHSv >> RHSv; break;
Dan Gohmanca0546f2008-10-17 01:33:43 +0000686 case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000687 }
688 return new IntInit(Result);
689 }
690 break;
691 }
692 }
693 return this;
694}
695
696Init *BinOpInit::resolveReferences(Record &R, const RecordVal *RV) {
697 Init *lhs = LHS->resolveReferences(R, RV);
698 Init *rhs = RHS->resolveReferences(R, RV);
699
700 if (LHS != lhs || RHS != rhs)
David Greene196ac3c2009-04-23 21:25:15 +0000701 return (new BinOpInit(getOpcode(), lhs, rhs, getType()))->Fold(&R, 0);
David Greenea9c6c5d2009-04-22 20:18:10 +0000702 return Fold(&R, 0);
Chris Lattner51ffbf12006-03-31 21:53:49 +0000703}
704
Chris Lattner695506c2007-11-22 21:05:25 +0000705std::string BinOpInit::getAsString() const {
706 std::string Result;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000707 switch (Opc) {
Chris Lattner695506c2007-11-22 21:05:25 +0000708 case CONCAT: Result = "!con"; break;
709 case SHL: Result = "!shl"; break;
710 case SRA: Result = "!sra"; break;
711 case SRL: Result = "!srl"; break;
712 case STRCONCAT: Result = "!strconcat"; break;
David Greene196ac3c2009-04-23 21:25:15 +0000713 case NAMECONCAT:
714 Result = "!nameconcat<" + getType()->getAsString() + ">"; break;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000715 }
Chris Lattner695506c2007-11-22 21:05:25 +0000716 return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")";
Chris Lattner51ffbf12006-03-31 21:53:49 +0000717}
718
David Greenee917fff2009-05-14 22:23:47 +0000719static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
720 Record *CurRec, MultiClass *CurMultiClass);
721
722static Init *EvaluateOperation(OpInit *RHSo, Init *LHS, Init *Arg,
723 RecTy *Type, Record *CurRec,
724 MultiClass *CurMultiClass) {
725 std::vector<Init *> NewOperands;
726
727 TypedInit *TArg = dynamic_cast<TypedInit*>(Arg);
728
729 // If this is a dag, recurse
730 if (TArg && TArg->getType()->getAsString() == "dag") {
731 Init *Result = ForeachHelper(LHS, Arg, RHSo, Type,
732 CurRec, CurMultiClass);
733 if (Result != 0) {
734 return Result;
735 }
736 else {
737 return 0;
738 }
739 }
740
741 for (int i = 0; i < RHSo->getNumOperands(); ++i) {
742 OpInit *RHSoo = dynamic_cast<OpInit*>(RHSo->getOperand(i));
743
744 if (RHSoo) {
745 Init *Result = EvaluateOperation(RHSoo, LHS, Arg,
746 Type, CurRec, CurMultiClass);
747 if (Result != 0) {
748 NewOperands.push_back(Result);
749 }
750 else {
751 NewOperands.push_back(Arg);
752 }
753 }
754 else if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
755 NewOperands.push_back(Arg);
756 }
757 else {
758 NewOperands.push_back(RHSo->getOperand(i));
759 }
760 }
761
762 // Now run the operator and use its result as the new leaf
763 OpInit *NewOp = RHSo->clone(NewOperands);
764 Init *NewVal = NewOp->Fold(CurRec, CurMultiClass);
765 if (NewVal != NewOp) {
766 delete NewOp;
767 return NewVal;
768 }
769 return 0;
770}
771
772static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
773 Record *CurRec, MultiClass *CurMultiClass) {
774 DagInit *MHSd = dynamic_cast<DagInit*>(MHS);
775 ListInit *MHSl = dynamic_cast<ListInit*>(MHS);
776
777 DagRecTy *DagType = dynamic_cast<DagRecTy*>(Type);
778 ListRecTy *ListType = dynamic_cast<ListRecTy*>(Type);
779
780 OpInit *RHSo = dynamic_cast<OpInit*>(RHS);
781
782 if (!RHSo) {
783 cerr << "!foreach requires an operator\n";
784 assert(0 && "No operator for !foreach");
785 }
786
787 TypedInit *LHSt = dynamic_cast<TypedInit*>(LHS);
788
789 if (!LHSt) {
790 cerr << "!foreach requires typed variable\n";
791 assert(0 && "No typed variable for !foreach");
792 }
793
794 if (MHSd && DagType || MHSl && ListType) {
795 if (MHSd) {
796 Init *Val = MHSd->getOperator();
797 Init *Result = EvaluateOperation(RHSo, LHS, Val,
798 Type, CurRec, CurMultiClass);
799 if (Result != 0) {
800 Val = Result;
801 }
802
803 std::vector<std::pair<Init *, std::string> > args;
804 for (unsigned int i = 0; i < MHSd->getNumArgs(); ++i) {
805 Init *Arg;
806 std::string ArgName;
807 Arg = MHSd->getArg(i);
808 ArgName = MHSd->getArgName(i);
809
810 // Process args
811 Init *Result = EvaluateOperation(RHSo, LHS, Arg, Type,
812 CurRec, CurMultiClass);
813 if (Result != 0) {
814 Arg = Result;
815 }
816
817 // TODO: Process arg names
818 args.push_back(std::make_pair(Arg, ArgName));
819 }
820
821 return new DagInit(Val, "", args);
822 }
823 if (MHSl) {
824 std::vector<Init *> NewOperands;
825 std::vector<Init *> NewList(MHSl->begin(), MHSl->end());
826
827 for (ListInit::iterator li = NewList.begin(),
828 liend = NewList.end();
829 li != liend;
830 ++li) {
831 Init *Item = *li;
832 NewOperands.clear();
833 for(int i = 0; i < RHSo->getNumOperands(); ++i) {
834 // First, replace the foreach variable with the list item
835 if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
836 NewOperands.push_back(Item);
837 }
838 else {
839 NewOperands.push_back(RHSo->getOperand(i));
840 }
841 }
842
843 // Now run the operator and use its result as the new list item
844 OpInit *NewOp = RHSo->clone(NewOperands);
845 Init *NewItem = NewOp->Fold(CurRec, CurMultiClass);
846 if (NewItem != NewOp) {
847 *li = NewItem;
848 delete NewOp;
849 }
850 }
851 return new ListInit(NewList);
852 }
853 }
854 return 0;
855}
856
David Greene98ed3c72009-05-14 21:54:42 +0000857Init *TernOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
858 switch (getOpcode()) {
859 default: assert(0 && "Unknown binop");
860 case SUBST: {
861 DefInit *LHSd = dynamic_cast<DefInit*>(LHS);
862 VarInit *LHSv = dynamic_cast<VarInit*>(LHS);
863 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
David Greene196ac3c2009-04-23 21:25:15 +0000864
David Greene98ed3c72009-05-14 21:54:42 +0000865 DefInit *MHSd = dynamic_cast<DefInit*>(MHS);
866 VarInit *MHSv = dynamic_cast<VarInit*>(MHS);
867 StringInit *MHSs = dynamic_cast<StringInit*>(MHS);
David Greene5d0c0512009-05-14 20:54:48 +0000868
David Greene98ed3c72009-05-14 21:54:42 +0000869 DefInit *RHSd = dynamic_cast<DefInit*>(RHS);
870 VarInit *RHSv = dynamic_cast<VarInit*>(RHS);
871 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
David Greene5d0c0512009-05-14 20:54:48 +0000872
David Greene98ed3c72009-05-14 21:54:42 +0000873 if ((LHSd && MHSd && RHSd)
874 || (LHSv && MHSv && RHSv)
875 || (LHSs && MHSs && RHSs)) {
876 if (RHSd) {
877 Record *Val = RHSd->getDef();
878 if (LHSd->getAsString() == RHSd->getAsString()) {
879 Val = MHSd->getDef();
880 }
881 return new DefInit(Val);
882 }
883 if (RHSv) {
884 std::string Val = RHSv->getName();
885 if (LHSv->getAsString() == RHSv->getAsString()) {
886 Val = MHSv->getName();
887 }
888 return new VarInit(Val, getType());
889 }
890 if (RHSs) {
891 std::string Val = RHSs->getValue();
David Greene5d0c0512009-05-14 20:54:48 +0000892
David Greene98ed3c72009-05-14 21:54:42 +0000893 std::string::size_type found;
894 do {
895 found = Val.find(LHSs->getValue());
896 if (found != std::string::npos) {
897 Val.replace(found, LHSs->getValue().size(), MHSs->getValue());
898 }
899 } while (found != std::string::npos);
David Greene5d0c0512009-05-14 20:54:48 +0000900
David Greene98ed3c72009-05-14 21:54:42 +0000901 return new StringInit(Val);
902 }
903 }
904 break;
905 }
David Greene5d0c0512009-05-14 20:54:48 +0000906
David Greene98ed3c72009-05-14 21:54:42 +0000907 case FOREACH: {
David Greenee917fff2009-05-14 22:23:47 +0000908 Init *Result = ForeachHelper(LHS, MHS, RHS, getType(),
909 CurRec, CurMultiClass);
910 if (Result != 0) {
911 return Result;
David Greene98ed3c72009-05-14 21:54:42 +0000912 }
913 break;
914 }
David Greene3587eed2009-05-14 23:26:46 +0000915
916 case IF: {
917 IntInit *LHSi = dynamic_cast<IntInit*>(LHS);
918 if (LHSi) {
919 if (LHSi->getValue()) {
920 return MHS;
921 }
922 else {
923 return RHS;
924 }
925 }
926 break;
927 }
David Greene98ed3c72009-05-14 21:54:42 +0000928 }
David Greene5d0c0512009-05-14 20:54:48 +0000929
David Greene98ed3c72009-05-14 21:54:42 +0000930 return this;
931}
David Greene5d0c0512009-05-14 20:54:48 +0000932
David Greene98ed3c72009-05-14 21:54:42 +0000933Init *TernOpInit::resolveReferences(Record &R, const RecordVal *RV) {
934 Init *lhs = LHS->resolveReferences(R, RV);
935 Init *mhs = MHS->resolveReferences(R, RV);
936 Init *rhs = RHS->resolveReferences(R, RV);
David Greene196ac3c2009-04-23 21:25:15 +0000937
David Greene98ed3c72009-05-14 21:54:42 +0000938 if (LHS != lhs || MHS != mhs || RHS != rhs)
939 return (new TernOpInit(getOpcode(), lhs, mhs, rhs, getType()))->Fold(&R, 0);
940 return Fold(&R, 0);
941}
David Greene196ac3c2009-04-23 21:25:15 +0000942
David Greene98ed3c72009-05-14 21:54:42 +0000943std::string TernOpInit::getAsString() const {
944 std::string Result;
945 switch (Opc) {
946 case SUBST: Result = "!subst"; break;
947 case FOREACH: Result = "!foreach"; break;
David Greene3587eed2009-05-14 23:26:46 +0000948 case IF: Result = "!if"; break;
David Greene98ed3c72009-05-14 21:54:42 +0000949 }
950 return Result + "(" + LHS->getAsString() + ", " + MHS->getAsString() + ", "
951 + RHS->getAsString() + ")";
952}
David Greene196ac3c2009-04-23 21:25:15 +0000953
Chris Lattner577fc3f2004-07-27 01:01:21 +0000954Init *TypedInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000955 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
956 if (T == 0) return 0; // Cannot subscript a non-bits variable...
957 unsigned NumBits = T->getNumBits();
958
959 BitsInit *BI = new BitsInit(Bits.size());
960 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
961 if (Bits[i] >= NumBits) {
962 delete BI;
963 return 0;
964 }
965 BI->setBit(i, new VarBitInit(this, Bits[i]));
966 }
967 return BI;
968}
969
Chris Lattner577fc3f2004-07-27 01:01:21 +0000970Init *TypedInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
971 ListRecTy *T = dynamic_cast<ListRecTy*>(getType());
972 if (T == 0) return 0; // Cannot subscript a non-list variable...
973
974 if (Elements.size() == 1)
975 return new VarListElementInit(this, Elements[0]);
976
977 std::vector<Init*> ListInits;
978 ListInits.reserve(Elements.size());
979 for (unsigned i = 0, e = Elements.size(); i != e; ++i)
980 ListInits.push_back(new VarListElementInit(this, Elements[i]));
981 return new ListInit(ListInits);
982}
983
984
Chris Lattneref943742005-04-19 03:36:21 +0000985Init *VarInit::resolveBitReference(Record &R, const RecordVal *IRV,
986 unsigned Bit) {
987 if (R.isTemplateArg(getName())) return 0;
988 if (IRV && IRV->getName() != getName()) return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000989
990 RecordVal *RV = R.getValue(getName());
991 assert(RV && "Reference to a non-existant variable?");
992 assert(dynamic_cast<BitsInit*>(RV->getValue()));
993 BitsInit *BI = (BitsInit*)RV->getValue();
Misha Brukman650ba8e2005-04-22 00:00:37 +0000994
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000995 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
996 Init *B = BI->getBit(Bit);
997
998 if (!dynamic_cast<UnsetInit*>(B)) // If the bit is not set...
999 return B; // Replace the VarBitInit with it.
Chris Lattner577fc3f2004-07-27 01:01:21 +00001000 return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001001}
1002
Chris Lattneref943742005-04-19 03:36:21 +00001003Init *VarInit::resolveListElementReference(Record &R, const RecordVal *IRV,
1004 unsigned Elt) {
1005 if (R.isTemplateArg(getName())) return 0;
1006 if (IRV && IRV->getName() != getName()) return 0;
Chris Lattner577fc3f2004-07-27 01:01:21 +00001007
1008 RecordVal *RV = R.getValue(getName());
1009 assert(RV && "Reference to a non-existant variable?");
1010 ListInit *LI = dynamic_cast<ListInit*>(RV->getValue());
David Greene9d3febe2009-05-14 20:38:52 +00001011 if (!LI) {
1012 VarInit *VI = dynamic_cast<VarInit*>(RV->getValue());
1013 assert(VI && "Invalid list element!");
1014 return new VarListElementInit(VI, Elt);
1015 }
1016
Chris Lattner577fc3f2004-07-27 01:01:21 +00001017 if (Elt >= LI->getSize())
1018 return 0; // Out of range reference.
1019 Init *E = LI->getElement(Elt);
1020 if (!dynamic_cast<UnsetInit*>(E)) // If the element is set
1021 return E; // Replace the VarListElementInit with it.
1022 return 0;
1023}
1024
1025
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001026RecTy *VarInit::getFieldType(const std::string &FieldName) const {
1027 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
1028 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
1029 return RV->getType();
1030 return 0;
1031}
1032
1033Init *VarInit::getFieldInit(Record &R, const std::string &FieldName) const {
Reid Spencer2a826862006-11-02 20:46:16 +00001034 if (dynamic_cast<RecordRecTy*>(getType()))
Chris Lattnerd959ab92004-02-28 16:31:53 +00001035 if (const RecordVal *RV = R.getValue(VarName)) {
1036 Init *TheInit = RV->getValue();
1037 assert(TheInit != this && "Infinite loop detected!");
1038 if (Init *I = TheInit->getFieldInit(R, FieldName))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001039 return I;
1040 else
1041 return 0;
Chris Lattnerd959ab92004-02-28 16:31:53 +00001042 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001043 return 0;
1044}
1045
1046/// resolveReferences - This method is used by classes that refer to other
1047/// variables which may not be defined at the time they expression is formed.
1048/// If a value is set for the variable later, this method will be called on
1049/// users of the value to allow the value to propagate out.
1050///
Chris Lattneref943742005-04-19 03:36:21 +00001051Init *VarInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001052 if (RecordVal *Val = R.getValue(VarName))
Chris Lattneref943742005-04-19 03:36:21 +00001053 if (RV == Val || (RV == 0 && !dynamic_cast<UnsetInit*>(Val->getValue())))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001054 return Val->getValue();
1055 return this;
1056}
Misha Brukman650ba8e2005-04-22 00:00:37 +00001057
Chris Lattner695506c2007-11-22 21:05:25 +00001058std::string VarBitInit::getAsString() const {
1059 return TI->getAsString() + "{" + utostr(Bit) + "}";
1060}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001061
Chris Lattneref943742005-04-19 03:36:21 +00001062Init *VarBitInit::resolveReferences(Record &R, const RecordVal *RV) {
1063 if (Init *I = getVariable()->resolveBitReference(R, RV, getBitNum()))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001064 return I;
1065 return this;
1066}
1067
Chris Lattner695506c2007-11-22 21:05:25 +00001068std::string VarListElementInit::getAsString() const {
1069 return TI->getAsString() + "[" + utostr(Element) + "]";
1070}
1071
Chris Lattneref943742005-04-19 03:36:21 +00001072Init *VarListElementInit::resolveReferences(Record &R, const RecordVal *RV) {
1073 if (Init *I = getVariable()->resolveListElementReference(R, RV,
1074 getElementNum()))
Chris Lattner577fc3f2004-07-27 01:01:21 +00001075 return I;
1076 return this;
1077}
1078
Chris Lattneref943742005-04-19 03:36:21 +00001079Init *VarListElementInit::resolveBitReference(Record &R, const RecordVal *RV,
1080 unsigned Bit) {
Chris Lattner577fc3f2004-07-27 01:01:21 +00001081 // FIXME: This should be implemented, to support references like:
1082 // bit B = AA[0]{1};
1083 return 0;
1084}
1085
Chris Lattneref943742005-04-19 03:36:21 +00001086Init *VarListElementInit::
1087resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) {
Chris Lattner577fc3f2004-07-27 01:01:21 +00001088 // FIXME: This should be implemented, to support references like:
1089 // int B = AA[0][1];
1090 return 0;
1091}
1092
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001093RecTy *DefInit::getFieldType(const std::string &FieldName) const {
1094 if (const RecordVal *RV = Def->getValue(FieldName))
1095 return RV->getType();
1096 return 0;
1097}
1098
1099Init *DefInit::getFieldInit(Record &R, const std::string &FieldName) const {
1100 return Def->getValue(FieldName)->getValue();
1101}
1102
1103
Chris Lattner695506c2007-11-22 21:05:25 +00001104std::string DefInit::getAsString() const {
1105 return Def->getName();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001106}
1107
Chris Lattneref943742005-04-19 03:36:21 +00001108Init *FieldInit::resolveBitReference(Record &R, const RecordVal *RV,
1109 unsigned Bit) {
Chris Lattner577fc3f2004-07-27 01:01:21 +00001110 if (Init *BitsVal = Rec->getFieldInit(R, FieldName))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001111 if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
1112 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
1113 Init *B = BI->getBit(Bit);
Misha Brukman650ba8e2005-04-22 00:00:37 +00001114
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001115 if (dynamic_cast<BitInit*>(B)) // If the bit is set...
1116 return B; // Replace the VarBitInit with it.
1117 }
Chris Lattner577fc3f2004-07-27 01:01:21 +00001118 return 0;
1119}
1120
Chris Lattneref943742005-04-19 03:36:21 +00001121Init *FieldInit::resolveListElementReference(Record &R, const RecordVal *RV,
1122 unsigned Elt) {
Chris Lattner577fc3f2004-07-27 01:01:21 +00001123 if (Init *ListVal = Rec->getFieldInit(R, FieldName))
1124 if (ListInit *LI = dynamic_cast<ListInit*>(ListVal)) {
1125 if (Elt >= LI->getSize()) return 0;
1126 Init *E = LI->getElement(Elt);
1127
1128 if (!dynamic_cast<UnsetInit*>(E)) // If the bit is set...
1129 return E; // Replace the VarListElementInit with it.
1130 }
1131 return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001132}
1133
Chris Lattneref943742005-04-19 03:36:21 +00001134Init *FieldInit::resolveReferences(Record &R, const RecordVal *RV) {
1135 Init *NewRec = RV ? Rec->resolveReferences(R, RV) : Rec;
1136
1137 Init *BitsVal = NewRec->getFieldInit(R, FieldName);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001138 if (BitsVal) {
Chris Lattneref943742005-04-19 03:36:21 +00001139 Init *BVR = BitsVal->resolveReferences(R, RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001140 return BVR->isComplete() ? BVR : this;
1141 }
Chris Lattneref943742005-04-19 03:36:21 +00001142
1143 if (NewRec != Rec) {
Chris Lattneref943742005-04-19 03:36:21 +00001144 return new FieldInit(NewRec, FieldName);
1145 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001146 return this;
1147}
1148
Chris Lattner0d3ef402006-01-31 06:02:35 +00001149Init *DagInit::resolveReferences(Record &R, const RecordVal *RV) {
1150 std::vector<Init*> NewArgs;
1151 for (unsigned i = 0, e = Args.size(); i != e; ++i)
1152 NewArgs.push_back(Args[i]->resolveReferences(R, RV));
1153
Chris Lattnerb59cf3c2006-03-30 22:50:40 +00001154 Init *Op = Val->resolveReferences(R, RV);
1155
1156 if (Args != NewArgs || Op != Val)
Nate Begemandbe3f772009-03-19 05:21:56 +00001157 return new DagInit(Op, "", NewArgs, ArgNames);
Chris Lattner0d3ef402006-01-31 06:02:35 +00001158
1159 return this;
1160}
1161
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001162
Chris Lattner695506c2007-11-22 21:05:25 +00001163std::string DagInit::getAsString() const {
1164 std::string Result = "(" + Val->getAsString();
Nate Begemandbe3f772009-03-19 05:21:56 +00001165 if (!ValName.empty())
1166 Result += ":" + ValName;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001167 if (Args.size()) {
Chris Lattner695506c2007-11-22 21:05:25 +00001168 Result += " " + Args[0]->getAsString();
1169 if (!ArgNames[0].empty()) Result += ":$" + ArgNames[0];
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001170 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
Chris Lattner695506c2007-11-22 21:05:25 +00001171 Result += ", " + Args[i]->getAsString();
1172 if (!ArgNames[i].empty()) Result += ":$" + ArgNames[i];
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001173 }
1174 }
Chris Lattner695506c2007-11-22 21:05:25 +00001175 return Result + ")";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001176}
1177
1178
1179//===----------------------------------------------------------------------===//
1180// Other implementations
1181//===----------------------------------------------------------------------===//
1182
1183RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
1184 : Name(N), Ty(T), Prefix(P) {
1185 Value = Ty->convertValue(new UnsetInit());
1186 assert(Value && "Cannot create unset value for current type!");
1187}
1188
Bill Wendling9bfb1e12006-12-07 22:21:48 +00001189void RecordVal::dump() const { cerr << *this; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001190
1191void RecordVal::print(std::ostream &OS, bool PrintSem) const {
1192 if (getPrefix()) OS << "field ";
1193 OS << *getType() << " " << getName();
Chris Lattneref943742005-04-19 03:36:21 +00001194
1195 if (getValue())
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001196 OS << " = " << *getValue();
Chris Lattneref943742005-04-19 03:36:21 +00001197
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001198 if (PrintSem) OS << ";\n";
1199}
1200
Chris Lattnerac284252005-08-19 17:58:11 +00001201void Record::setName(const std::string &Name) {
1202 if (Records.getDef(getName()) == this) {
1203 Records.removeDef(getName());
1204 this->Name = Name;
1205 Records.addDef(this);
1206 } else {
1207 Records.removeClass(getName());
1208 this->Name = Name;
1209 Records.addClass(this);
1210 }
1211}
1212
Chris Lattneref943742005-04-19 03:36:21 +00001213/// resolveReferencesTo - If anything in this record refers to RV, replace the
1214/// reference to RV with the RHS of RV. If RV is null, we resolve all possible
1215/// references.
1216void Record::resolveReferencesTo(const RecordVal *RV) {
1217 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
1218 if (Init *V = Values[i].getValue())
1219 Values[i].setValue(V->resolveReferences(*this, RV));
1220 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001221}
1222
Chris Lattneref943742005-04-19 03:36:21 +00001223
Bill Wendling9bfb1e12006-12-07 22:21:48 +00001224void Record::dump() const { cerr << *this; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001225
Chris Lattner68478662004-08-01 03:55:39 +00001226std::ostream &llvm::operator<<(std::ostream &OS, const Record &R) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001227 OS << R.getName();
1228
1229 const std::vector<std::string> &TArgs = R.getTemplateArgs();
1230 if (!TArgs.empty()) {
1231 OS << "<";
1232 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1233 if (i) OS << ", ";
1234 const RecordVal *RV = R.getValue(TArgs[i]);
1235 assert(RV && "Template argument record not found??");
1236 RV->print(OS, false);
1237 }
1238 OS << ">";
1239 }
1240
1241 OS << " {";
1242 const std::vector<Record*> &SC = R.getSuperClasses();
1243 if (!SC.empty()) {
1244 OS << "\t//";
1245 for (unsigned i = 0, e = SC.size(); i != e; ++i)
1246 OS << " " << SC[i]->getName();
1247 }
1248 OS << "\n";
1249
1250 const std::vector<RecordVal> &Vals = R.getValues();
1251 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
1252 if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
1253 OS << Vals[i];
1254 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
1255 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
1256 OS << Vals[i];
1257
1258 return OS << "}\n";
1259}
1260
1261/// getValueInit - Return the initializer for a value with the specified name,
1262/// or throw an exception if the field does not exist.
1263///
1264Init *Record::getValueInit(const std::string &FieldName) const {
1265 const RecordVal *R = getValue(FieldName);
1266 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001267 throw "Record `" + getName() + "' does not have a field named `" +
1268 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001269 return R->getValue();
1270}
1271
1272
1273/// getValueAsString - This method looks up the specified field and returns its
1274/// value as a string, throwing an exception if the field does not exist or if
1275/// the value is not a string.
1276///
1277std::string Record::getValueAsString(const std::string &FieldName) const {
1278 const RecordVal *R = getValue(FieldName);
1279 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001280 throw "Record `" + getName() + "' does not have a field named `" +
1281 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001282
1283 if (const StringInit *SI = dynamic_cast<const StringInit*>(R->getValue()))
1284 return SI->getValue();
Misha Brukman41f9f292004-10-08 14:59:05 +00001285 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001286 "' does not have a string initializer!";
1287}
1288
1289/// getValueAsBitsInit - This method looks up the specified field and returns
1290/// its value as a BitsInit, throwing an exception if the field does not exist
1291/// or if the value is not the right type.
1292///
1293BitsInit *Record::getValueAsBitsInit(const std::string &FieldName) const {
1294 const RecordVal *R = getValue(FieldName);
1295 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001296 throw "Record `" + getName() + "' does not have a field named `" +
1297 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001298
1299 if (BitsInit *BI = dynamic_cast<BitsInit*>(R->getValue()))
1300 return BI;
Misha Brukman41f9f292004-10-08 14:59:05 +00001301 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001302 "' does not have a BitsInit initializer!";
1303}
1304
1305/// getValueAsListInit - This method looks up the specified field and returns
1306/// its value as a ListInit, throwing an exception if the field does not exist
1307/// or if the value is not the right type.
1308///
1309ListInit *Record::getValueAsListInit(const std::string &FieldName) const {
1310 const RecordVal *R = getValue(FieldName);
1311 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001312 throw "Record `" + getName() + "' does not have a field named `" +
1313 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001314
1315 if (ListInit *LI = dynamic_cast<ListInit*>(R->getValue()))
1316 return LI;
Misha Brukman41f9f292004-10-08 14:59:05 +00001317 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001318 "' does not have a list initializer!";
1319}
1320
Chris Lattner7ad0bed2005-10-28 22:49:02 +00001321/// getValueAsListOfDefs - This method looks up the specified field and returns
Jim Laskeyb04feb62005-10-28 21:46:31 +00001322/// its value as a vector of records, throwing an exception if the field does
1323/// not exist or if the value is not the right type.
1324///
Chris Lattner7ad0bed2005-10-28 22:49:02 +00001325std::vector<Record*>
1326Record::getValueAsListOfDefs(const std::string &FieldName) const {
Jim Laskeyb04feb62005-10-28 21:46:31 +00001327 ListInit *List = getValueAsListInit(FieldName);
1328 std::vector<Record*> Defs;
1329 for (unsigned i = 0; i < List->getSize(); i++) {
1330 if (DefInit *DI = dynamic_cast<DefInit*>(List->getElement(i))) {
1331 Defs.push_back(DI->getDef());
1332 } else {
1333 throw "Record `" + getName() + "', field `" + FieldName +
1334 "' list is not entirely DefInit!";
1335 }
1336 }
1337 return Defs;
1338}
1339
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001340/// getValueAsInt - This method looks up the specified field and returns its
Dan Gohmanca0546f2008-10-17 01:33:43 +00001341/// value as an int64_t, throwing an exception if the field does not exist or if
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001342/// the value is not the right type.
1343///
Dan Gohmanca0546f2008-10-17 01:33:43 +00001344int64_t Record::getValueAsInt(const std::string &FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001345 const RecordVal *R = getValue(FieldName);
1346 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001347 throw "Record `" + getName() + "' does not have a field named `" +
1348 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001349
1350 if (IntInit *II = dynamic_cast<IntInit*>(R->getValue()))
1351 return II->getValue();
Misha Brukman41f9f292004-10-08 14:59:05 +00001352 throw "Record `" + getName() + "', field `" + FieldName +
Nate Begemanf621b332005-11-30 18:37:14 +00001353 "' does not have an int initializer!";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001354}
1355
Anton Korobeynikova468a112007-11-11 11:19:37 +00001356/// getValueAsListOfInts - This method looks up the specified field and returns
1357/// its value as a vector of integers, throwing an exception if the field does
1358/// not exist or if the value is not the right type.
1359///
Dan Gohmanca0546f2008-10-17 01:33:43 +00001360std::vector<int64_t>
Anton Korobeynikova468a112007-11-11 11:19:37 +00001361Record::getValueAsListOfInts(const std::string &FieldName) const {
1362 ListInit *List = getValueAsListInit(FieldName);
Dan Gohmanca0546f2008-10-17 01:33:43 +00001363 std::vector<int64_t> Ints;
Anton Korobeynikova468a112007-11-11 11:19:37 +00001364 for (unsigned i = 0; i < List->getSize(); i++) {
1365 if (IntInit *II = dynamic_cast<IntInit*>(List->getElement(i))) {
1366 Ints.push_back(II->getValue());
1367 } else {
1368 throw "Record `" + getName() + "', field `" + FieldName +
1369 "' does not have a list of ints initializer!";
1370 }
1371 }
1372 return Ints;
1373}
1374
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001375/// getValueAsDef - This method looks up the specified field and returns its
1376/// value as a Record, throwing an exception if the field does not exist or if
1377/// the value is not the right type.
1378///
1379Record *Record::getValueAsDef(const std::string &FieldName) const {
1380 const RecordVal *R = getValue(FieldName);
1381 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001382 throw "Record `" + getName() + "' does not have a field named `" +
1383 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001384
1385 if (DefInit *DI = dynamic_cast<DefInit*>(R->getValue()))
1386 return DI->getDef();
Misha Brukman41f9f292004-10-08 14:59:05 +00001387 throw "Record `" + getName() + "', field `" + FieldName +
Nate Begemanf621b332005-11-30 18:37:14 +00001388 "' does not have a def initializer!";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001389}
1390
1391/// getValueAsBit - This method looks up the specified field and returns its
1392/// value as a bit, throwing an exception if the field does not exist or if
1393/// the value is not the right type.
1394///
1395bool Record::getValueAsBit(const std::string &FieldName) const {
1396 const RecordVal *R = getValue(FieldName);
1397 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001398 throw "Record `" + getName() + "' does not have a field named `" +
1399 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001400
1401 if (BitInit *BI = dynamic_cast<BitInit*>(R->getValue()))
1402 return BI->getValue();
Misha Brukman41f9f292004-10-08 14:59:05 +00001403 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001404 "' does not have a bit initializer!";
1405}
1406
1407/// getValueAsDag - This method looks up the specified field and returns its
1408/// value as an Dag, throwing an exception if the field does not exist or if
1409/// the value is not the right type.
1410///
1411DagInit *Record::getValueAsDag(const std::string &FieldName) const {
1412 const RecordVal *R = getValue(FieldName);
1413 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001414 throw "Record `" + getName() + "' does not have a field named `" +
1415 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001416
1417 if (DagInit *DI = dynamic_cast<DagInit*>(R->getValue()))
1418 return DI;
Misha Brukman41f9f292004-10-08 14:59:05 +00001419 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001420 "' does not have a dag initializer!";
1421}
1422
Chris Lattnerae939eb2005-09-13 21:44:28 +00001423std::string Record::getValueAsCode(const std::string &FieldName) const {
1424 const RecordVal *R = getValue(FieldName);
1425 if (R == 0 || R->getValue() == 0)
1426 throw "Record `" + getName() + "' does not have a field named `" +
1427 FieldName + "'!\n";
1428
1429 if (const CodeInit *CI = dynamic_cast<const CodeInit*>(R->getValue()))
1430 return CI->getValue();
1431 throw "Record `" + getName() + "', field `" + FieldName +
1432 "' does not have a code initializer!";
1433}
1434
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001435
David Greene7049e792009-04-24 16:55:41 +00001436void MultiClass::dump() const {
1437 cerr << "Record:\n";
1438 Rec.dump();
1439
1440 cerr << "Defs:\n";
1441 for (RecordVector::const_iterator r = DefPrototypes.begin(),
1442 rend = DefPrototypes.end();
1443 r != rend;
1444 ++r) {
1445 (*r)->dump();
1446 }
1447}
1448
1449
Bill Wendling9bfb1e12006-12-07 22:21:48 +00001450void RecordKeeper::dump() const { cerr << *this; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001451
Chris Lattner68478662004-08-01 03:55:39 +00001452std::ostream &llvm::operator<<(std::ostream &OS, const RecordKeeper &RK) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001453 OS << "------------- Classes -----------------\n";
1454 const std::map<std::string, Record*> &Classes = RK.getClasses();
1455 for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001456 E = Classes.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001457 OS << "class " << *I->second;
Misha Brukman650ba8e2005-04-22 00:00:37 +00001458
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001459 OS << "------------- Defs -----------------\n";
1460 const std::map<std::string, Record*> &Defs = RK.getDefs();
1461 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001462 E = Defs.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001463 OS << "def " << *I->second;
1464 return OS;
1465}
1466
1467
1468/// getAllDerivedDefinitions - This method returns all concrete definitions
1469/// that derive from the specified class name. If a class with the specified
1470/// name does not exist, an error is printed and true is returned.
1471std::vector<Record*>
1472RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
1473 Record *Class = Records.getClass(ClassName);
1474 if (!Class)
Misha Brukman41f9f292004-10-08 14:59:05 +00001475 throw "ERROR: Couldn't find the `" + ClassName + "' class!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001476
1477 std::vector<Record*> Defs;
1478 for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(),
1479 E = getDefs().end(); I != E; ++I)
1480 if (I->second->isSubClassOf(Class))
1481 Defs.push_back(I->second);
1482
1483 return Defs;
1484}
Brian Gaeke960707c2003-11-11 22:41:34 +00001485