717962794a074f476b4216a80e3708dde5962eae
[oota-llvm.git] / utils / TableGen / Record.cpp
1 //===- Record.cpp - Record implementation ---------------------------------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "Record.h"
14
15 //===----------------------------------------------------------------------===//
16 //    Type implementations
17 //===----------------------------------------------------------------------===//
18
19 namespace llvm {
20
21 void RecTy::dump() const { print(std::cerr); }
22
23 Init *BitRecTy::convertValue(BitsInit *BI) {
24   if (BI->getNumBits() != 1) return 0; // Only accept if just one bit!
25   return BI->getBit(0);
26 }
27
28 bool BitRecTy::baseClassOf(const BitsRecTy *RHS) const {
29   return RHS->getNumBits() == 1;
30 }
31
32 Init *BitRecTy::convertValue(IntInit *II) {
33   int Val = II->getValue();
34   if (Val != 0 && Val != 1) return 0;  // Only accept 0 or 1 for a bit!
35   
36   return new BitInit(Val != 0); 
37 }
38
39 Init *BitRecTy::convertValue(TypedInit *VI) {
40   if (dynamic_cast<BitRecTy*>(VI->getType()))
41     return VI;  // Accept variable if it is already of bit type!
42   return 0;
43 }
44
45 Init *BitsRecTy::convertValue(UnsetInit *UI) {
46   BitsInit *Ret = new BitsInit(Size);
47
48   for (unsigned i = 0; i != Size; ++i)
49     Ret->setBit(i, new UnsetInit());
50   return Ret;
51 }
52
53 Init *BitsRecTy::convertValue(BitInit *UI) {
54   if (Size != 1) return 0;  // Can only convert single bit...
55   BitsInit *Ret = new BitsInit(1);
56   Ret->setBit(0, UI);
57   return Ret;
58 }
59
60 // convertValue from Int initializer to bits type: Split the integer up into the
61 // appropriate bits...
62 //
63 Init *BitsRecTy::convertValue(IntInit *II) {
64   int64_t Value = II->getValue();
65   // Make sure this bitfield is large enough to hold the integer value...
66   if (Value >= 0) {
67     if (Value & ~((1LL << Size)-1))
68       return 0;
69   } else {
70     if ((Value >> Size) != -1 || ((Value & (1 << Size-1)) == 0))
71       return 0;
72   }
73
74   BitsInit *Ret = new BitsInit(Size);
75   for (unsigned i = 0; i != Size; ++i)
76     Ret->setBit(i, new BitInit(Value & (1 << i)));
77
78   return Ret;
79 }
80
81 Init *BitsRecTy::convertValue(BitsInit *BI) {
82   // If the number of bits is right, return it.  Otherwise we need to expand or
83   // truncate...
84   if (BI->getNumBits() == Size) return BI;
85   return 0;
86 }
87
88 Init *BitsRecTy::convertValue(TypedInit *VI) {
89   if (BitsRecTy *BRT = dynamic_cast<BitsRecTy*>(VI->getType()))
90     if (BRT->Size == Size) {
91       BitsInit *Ret = new BitsInit(Size);
92       for (unsigned i = 0; i != Size; ++i)
93         Ret->setBit(i, new VarBitInit(VI, i));
94       return Ret;
95     }
96   if (Size == 1 && dynamic_cast<BitRecTy*>(VI->getType())) {
97     BitsInit *Ret = new BitsInit(1);
98     Ret->setBit(0, VI);
99     return Ret;
100   }
101       
102   return 0;
103 }
104
105 Init *IntRecTy::convertValue(BitInit *BI) {
106   return new IntInit(BI->getValue());
107 }
108
109 Init *IntRecTy::convertValue(BitsInit *BI) {
110   int Result = 0;
111   for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) 
112     if (BitInit *Bit = dynamic_cast<BitInit*>(BI->getBit(i))) {
113       Result |= Bit->getValue() << i;
114     } else {
115       return 0;
116     }
117   return new IntInit(Result);
118 }
119
120 Init *IntRecTy::convertValue(TypedInit *TI) {
121   if (TI->getType()->typeIsConvertibleTo(this))
122     return TI;  // Accept variable if already of the right type!
123   return 0;
124 }
125
126 Init *StringRecTy::convertValue(TypedInit *TI) {
127   if (dynamic_cast<StringRecTy*>(TI->getType()))
128     return TI;  // Accept variable if already of the right type!
129   return 0;
130 }
131
132 void ListRecTy::print(std::ostream &OS) const {
133   OS << "list<" << *Ty << ">";
134 }
135
136 Init *ListRecTy::convertValue(ListInit *LI) {
137   std::vector<Init*> Elements;
138
139   // Verify that all of the elements of the list are subclasses of the
140   // appropriate class!
141   for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
142     if (Init *CI = LI->getElement(i)->convertInitializerTo(Ty))
143       Elements.push_back(CI);
144     else
145       return 0;
146
147   return new ListInit(Elements);
148 }
149
150 Init *ListRecTy::convertValue(TypedInit *TI) {
151   // Ensure that TI is compatible with our class.
152   if (ListRecTy *LRT = dynamic_cast<ListRecTy*>(TI->getType()))
153     if (LRT->getElementType()->typeIsConvertibleTo(getElementType()))
154       return TI;
155   return 0;
156 }
157
158 Init *CodeRecTy::convertValue(TypedInit *TI) {
159   if (TI->getType()->typeIsConvertibleTo(this))
160     return TI;
161   return 0;
162 }
163
164 Init *DagRecTy::convertValue(TypedInit *TI) {
165   if (TI->getType()->typeIsConvertibleTo(this))
166     return TI;
167   return 0;
168 }
169
170
171 void RecordRecTy::print(std::ostream &OS) const {
172   OS << Rec->getName();
173 }
174
175 Init *RecordRecTy::convertValue(DefInit *DI) {
176   // Ensure that DI is a subclass of Rec.
177   if (!DI->getDef()->isSubClassOf(Rec))
178     return 0;
179   return DI;
180 }
181
182 Init *RecordRecTy::convertValue(TypedInit *TI) {
183   // Ensure that TI is compatible with Rec.
184   if (RecordRecTy *RRT = dynamic_cast<RecordRecTy*>(TI->getType()))
185     if (RRT->getRecord()->isSubClassOf(getRecord()) ||
186         RRT->getRecord() == getRecord())
187       return TI;
188   return 0;
189 }
190
191 bool RecordRecTy::baseClassOf(const RecordRecTy *RHS) const {
192   return Rec == RHS->getRecord() || RHS->getRecord()->isSubClassOf(Rec);
193 }
194
195
196 //===----------------------------------------------------------------------===//
197 //    Initializer implementations
198 //===----------------------------------------------------------------------===//
199
200 void Init::dump() const { return print(std::cerr); }
201
202 Init *BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
203   BitsInit *BI = new BitsInit(Bits.size());
204   for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
205     if (Bits[i] >= getNumBits()) {
206       delete BI;
207       return 0;
208     }
209     BI->setBit(i, getBit(Bits[i]));
210   }
211   return BI;
212 }
213
214 void BitsInit::print(std::ostream &OS) const {
215   //if (!printInHex(OS)) return;
216   //if (!printAsVariable(OS)) return;
217   //if (!printAsUnset(OS)) return;
218
219   OS << "{ ";
220   for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
221     if (i) OS << ", ";
222     if (Init *Bit = getBit(e-i-1))
223       Bit->print(OS);
224     else
225       OS << "*";
226   }
227   OS << " }";
228 }
229
230 bool BitsInit::printInHex(std::ostream &OS) const {
231   // First, attempt to convert the value into an integer value...
232   int Result = 0;
233   for (unsigned i = 0, e = getNumBits(); i != e; ++i) 
234     if (BitInit *Bit = dynamic_cast<BitInit*>(getBit(i))) {
235       Result |= Bit->getValue() << i;
236     } else {
237       return true;
238     }
239
240   OS << "0x" << std::hex << Result << std::dec;
241   return false;
242 }
243
244 bool BitsInit::printAsVariable(std::ostream &OS) const {
245   // Get the variable that we may be set equal to...
246   assert(getNumBits() != 0);
247   VarBitInit *FirstBit = dynamic_cast<VarBitInit*>(getBit(0));
248   if (FirstBit == 0) return true;
249   TypedInit *Var = FirstBit->getVariable();
250
251   // Check to make sure the types are compatible.
252   BitsRecTy *Ty = dynamic_cast<BitsRecTy*>(FirstBit->getVariable()->getType());
253   if (Ty == 0) return true;
254   if (Ty->getNumBits() != getNumBits()) return true; // Incompatible types!
255
256   // Check to make sure all bits are referring to the right bits in the variable
257   for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
258     VarBitInit *Bit = dynamic_cast<VarBitInit*>(getBit(i));
259     if (Bit == 0 || Bit->getVariable() != Var || Bit->getBitNum() != i)
260       return true;
261   }
262
263   Var->print(OS);
264   return false;
265 }
266
267 bool BitsInit::printAsUnset(std::ostream &OS) const {
268   for (unsigned i = 0, e = getNumBits(); i != e; ++i) 
269     if (!dynamic_cast<UnsetInit*>(getBit(i)))
270       return true;
271   OS << "?";
272   return false;
273 }
274
275 // resolveReferences - If there are any field references that refer to fields
276 // that have been filled in, we can propagate the values now.
277 //
278 Init *BitsInit::resolveReferences(Record &R) {
279   bool Changed = false;
280   BitsInit *New = new BitsInit(getNumBits());
281
282   for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
283     Init *B;
284     Init *CurBit = getBit(i);
285
286     do {
287       B = CurBit;
288       CurBit = CurBit->resolveReferences(R);
289       Changed |= B != CurBit;
290     } while (B != CurBit);
291     New->setBit(i, CurBit);
292   }
293
294   if (Changed)
295     return New;
296   delete New;
297   return this;
298 }
299
300 Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
301   BitsInit *BI = new BitsInit(Bits.size());
302
303   for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
304     if (Bits[i] >= 32) {
305       delete BI;
306       return 0;
307     }
308     BI->setBit(i, new BitInit(Value & (1 << Bits[i])));
309   }
310   return BI;
311 }
312
313 Init *ListInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
314   std::vector<Init*> Vals;
315   for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
316     if (Elements[i] >= getSize())
317       return 0;
318     Vals.push_back(getElement(Elements[i]));
319   }
320   return new ListInit(Vals);
321 }
322
323 Init *ListInit::resolveReferences(Record &R) {
324   std::vector<Init*> Resolved;
325   Resolved.reserve(getSize());
326   bool Changed = false;
327
328   for (unsigned i = 0, e = getSize(); i != e; ++i) {
329     Init *E;
330     Init *CurElt = getElement(i);
331
332     do {
333       E = CurElt;
334       CurElt = CurElt->resolveReferences(R);
335       Changed |= E != CurElt;
336     } while (E != CurElt);
337     Resolved.push_back(E);
338   }
339
340   if (Changed)
341     return new ListInit(Resolved);
342   return this;
343 }
344
345 void ListInit::print(std::ostream &OS) const {
346   OS << "[";
347   for (unsigned i = 0, e = Values.size(); i != e; ++i) {
348     if (i) OS << ", ";
349     OS << *Values[i];
350   }
351   OS << "]";
352 }
353
354 Init *TypedInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
355   BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
356   if (T == 0) return 0;  // Cannot subscript a non-bits variable...
357   unsigned NumBits = T->getNumBits();
358
359   BitsInit *BI = new BitsInit(Bits.size());
360   for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
361     if (Bits[i] >= NumBits) {
362       delete BI;
363       return 0;
364     }
365     BI->setBit(i, new VarBitInit(this, Bits[i]));
366   }
367   return BI;
368 }
369
370 Init *TypedInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
371   ListRecTy *T = dynamic_cast<ListRecTy*>(getType());
372   if (T == 0) return 0;  // Cannot subscript a non-list variable...
373
374   if (Elements.size() == 1)
375     return new VarListElementInit(this, Elements[0]);
376
377   std::vector<Init*> ListInits;
378   ListInits.reserve(Elements.size());
379   for (unsigned i = 0, e = Elements.size(); i != e; ++i)
380     ListInits.push_back(new VarListElementInit(this, Elements[i]));
381   return new ListInit(ListInits);
382 }
383
384
385 Init *VarInit::resolveBitReference(Record &R, unsigned Bit) {
386   if (R.isTemplateArg(getName()))
387     return 0;
388
389   RecordVal *RV = R.getValue(getName());
390   assert(RV && "Reference to a non-existant variable?");
391   assert(dynamic_cast<BitsInit*>(RV->getValue()));
392   BitsInit *BI = (BitsInit*)RV->getValue();
393   
394   assert(Bit < BI->getNumBits() && "Bit reference out of range!");
395   Init *B = BI->getBit(Bit);
396
397   if (!dynamic_cast<UnsetInit*>(B))  // If the bit is not set...
398     return B;                        // Replace the VarBitInit with it.
399   return 0;
400 }
401
402 Init *VarInit::resolveListElementReference(Record &R, unsigned Elt) {
403   if (R.isTemplateArg(getName()))
404     return 0;
405
406   RecordVal *RV = R.getValue(getName());
407   assert(RV && "Reference to a non-existant variable?");
408   ListInit *LI = dynamic_cast<ListInit*>(RV->getValue());
409   assert(LI && "Invalid list element!");
410
411   if (Elt >= LI->getSize())
412     return 0;  // Out of range reference.
413   Init *E = LI->getElement(Elt);
414   if (!dynamic_cast<UnsetInit*>(E))  // If the element is set
415     return E;                        // Replace the VarListElementInit with it.
416   return 0;
417 }
418
419
420 RecTy *VarInit::getFieldType(const std::string &FieldName) const {
421   if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
422     if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
423       return RV->getType();
424   return 0;
425 }
426
427 Init *VarInit::getFieldInit(Record &R, const std::string &FieldName) const {
428   if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
429     if (const RecordVal *RV = R.getValue(VarName)) {
430       Init *TheInit = RV->getValue();
431       assert(TheInit != this && "Infinite loop detected!");
432       if (Init *I = TheInit->getFieldInit(R, FieldName))
433         return I;
434       else
435         return 0;
436     }
437   return 0;
438 }
439
440 /// resolveReferences - This method is used by classes that refer to other
441 /// variables which may not be defined at the time they expression is formed.
442 /// If a value is set for the variable later, this method will be called on
443 /// users of the value to allow the value to propagate out.
444 ///
445 Init *VarInit::resolveReferences(Record &R) {
446   if (RecordVal *Val = R.getValue(VarName))
447     if (!dynamic_cast<UnsetInit*>(Val->getValue()))
448       return Val->getValue();
449   return this;
450 }
451   
452
453 Init *VarBitInit::resolveReferences(Record &R) {
454   if (Init *I = getVariable()->resolveBitReference(R, getBitNum()))
455     return I;
456   return this;
457 }
458
459 Init *VarListElementInit::resolveReferences(Record &R) {
460   if (Init *I = getVariable()->resolveListElementReference(R, getElementNum()))
461     return I;
462   return this;
463 }
464
465 Init *VarListElementInit::resolveBitReference(Record &R, unsigned Bit) {
466   // FIXME: This should be implemented, to support references like:
467   // bit B = AA[0]{1};
468   return 0;
469 }
470
471 Init *VarListElementInit::resolveListElementReference(Record &R, unsigned Elt) {
472   // FIXME: This should be implemented, to support references like:
473   // int B = AA[0][1];
474   return 0;
475 }
476
477 RecTy *DefInit::getFieldType(const std::string &FieldName) const {
478   if (const RecordVal *RV = Def->getValue(FieldName))
479     return RV->getType();
480   return 0;
481 }
482
483 Init *DefInit::getFieldInit(Record &R, const std::string &FieldName) const {
484   return Def->getValue(FieldName)->getValue();
485 }
486
487
488 void DefInit::print(std::ostream &OS) const {
489   OS << Def->getName();
490 }
491
492 Init *FieldInit::resolveBitReference(Record &R, unsigned Bit) {
493   if (Init *BitsVal = Rec->getFieldInit(R, FieldName))
494     if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
495       assert(Bit < BI->getNumBits() && "Bit reference out of range!");
496       Init *B = BI->getBit(Bit);
497       
498       if (dynamic_cast<BitInit*>(B))  // If the bit is set...
499         return B;                     // Replace the VarBitInit with it.
500     }
501   return 0;
502 }
503
504 Init *FieldInit::resolveListElementReference(Record &R, unsigned Elt) {
505   if (Init *ListVal = Rec->getFieldInit(R, FieldName))
506     if (ListInit *LI = dynamic_cast<ListInit*>(ListVal)) {
507       if (Elt >= LI->getSize()) return 0;
508       Init *E = LI->getElement(Elt);
509
510       if (!dynamic_cast<UnsetInit*>(E))  // If the bit is set...
511         return E;                  // Replace the VarListElementInit with it.
512     }
513   return 0;
514 }
515
516 Init *FieldInit::resolveReferences(Record &R) {
517   Init *BitsVal = Rec->getFieldInit(R, FieldName);
518   if (BitsVal) {
519     Init *BVR = BitsVal->resolveReferences(R);
520     return BVR->isComplete() ? BVR : this;
521   }
522   return this;
523 }
524
525
526 void DagInit::print(std::ostream &OS) const {
527   OS << "(" << NodeTypeDef->getName();
528   if (Args.size()) {
529     OS << " " << *Args[0];
530     if (!ArgNames[0].empty()) OS << ":$" << ArgNames[0];
531     for (unsigned i = 1, e = Args.size(); i != e; ++i) {
532       OS << ", " << *Args[i];
533       if (!ArgNames[i].empty()) OS << ":$" << ArgNames[i];
534     }
535   }
536   OS << ")";
537 }
538
539
540 //===----------------------------------------------------------------------===//
541 //    Other implementations
542 //===----------------------------------------------------------------------===//
543
544 RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
545   : Name(N), Ty(T), Prefix(P) {
546   Value = Ty->convertValue(new UnsetInit());
547   assert(Value && "Cannot create unset value for current type!");
548 }
549
550 void RecordVal::dump() const { std::cerr << *this; }
551
552 void RecordVal::print(std::ostream &OS, bool PrintSem) const {
553   if (getPrefix()) OS << "field ";
554   OS << *getType() << " " << getName();
555   if (getValue()) {
556     OS << " = " << *getValue();
557   }
558   if (PrintSem) OS << ";\n";
559 }
560
561 // resolveReferences - If there are any field references that refer to fields
562 // that have been filled in, we can propagate the values now.
563 //
564 void Record::resolveReferences() {
565   for (unsigned i = 0, e = Values.size(); i != e; ++i)
566     Values[i].setValue(Values[i].getValue()->resolveReferences(*this));
567 }
568
569 void Record::dump() const { std::cerr << *this; }
570
571 std::ostream &operator<<(std::ostream &OS, const Record &R) {
572   OS << R.getName();
573
574   const std::vector<std::string> &TArgs = R.getTemplateArgs();
575   if (!TArgs.empty()) {
576     OS << "<";
577     for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
578       if (i) OS << ", ";
579       const RecordVal *RV = R.getValue(TArgs[i]);
580       assert(RV && "Template argument record not found??");
581       RV->print(OS, false);
582     }
583     OS << ">";
584   }
585
586   OS << " {";
587   const std::vector<Record*> &SC = R.getSuperClasses();
588   if (!SC.empty()) {
589     OS << "\t//";
590     for (unsigned i = 0, e = SC.size(); i != e; ++i)
591       OS << " " << SC[i]->getName();
592   }
593   OS << "\n";
594
595   const std::vector<RecordVal> &Vals = R.getValues();
596   for (unsigned i = 0, e = Vals.size(); i != e; ++i)
597     if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
598       OS << Vals[i];
599   for (unsigned i = 0, e = Vals.size(); i != e; ++i)
600     if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
601       OS << Vals[i];
602
603   return OS << "}\n";
604 }
605
606 /// getValueInit - Return the initializer for a value with the specified name,
607 /// or throw an exception if the field does not exist.
608 ///
609 Init *Record::getValueInit(const std::string &FieldName) const {
610   const RecordVal *R = getValue(FieldName);
611   if (R == 0 || R->getValue() == 0)
612     throw "Record '" + getName() + "' does not have a field named '" +
613       FieldName + "!\n";
614   return R->getValue();
615 }
616
617
618 /// getValueAsString - This method looks up the specified field and returns its
619 /// value as a string, throwing an exception if the field does not exist or if
620 /// the value is not a string.
621 ///
622 std::string Record::getValueAsString(const std::string &FieldName) const {
623   const RecordVal *R = getValue(FieldName);
624   if (R == 0 || R->getValue() == 0)
625     throw "Record '" + getName() + "' does not have a field named '" +
626           FieldName + "!\n";
627
628   if (const StringInit *SI = dynamic_cast<const StringInit*>(R->getValue()))
629     return SI->getValue();
630   throw "Record '" + getName() + "', field '" + FieldName +
631         "' does not have a string initializer!";
632 }
633
634 /// getValueAsBitsInit - This method looks up the specified field and returns
635 /// its value as a BitsInit, throwing an exception if the field does not exist
636 /// or if the value is not the right type.
637 ///
638 BitsInit *Record::getValueAsBitsInit(const std::string &FieldName) const {
639   const RecordVal *R = getValue(FieldName);
640   if (R == 0 || R->getValue() == 0)
641     throw "Record '" + getName() + "' does not have a field named '" +
642           FieldName + "!\n";
643
644   if (BitsInit *BI = dynamic_cast<BitsInit*>(R->getValue()))
645     return BI;
646   throw "Record '" + getName() + "', field '" + FieldName +
647         "' does not have a BitsInit initializer!";
648 }
649
650 /// getValueAsListInit - This method looks up the specified field and returns
651 /// its value as a ListInit, throwing an exception if the field does not exist
652 /// or if the value is not the right type.
653 ///
654 ListInit *Record::getValueAsListInit(const std::string &FieldName) const {
655   const RecordVal *R = getValue(FieldName);
656   if (R == 0 || R->getValue() == 0)
657     throw "Record '" + getName() + "' does not have a field named '" +
658           FieldName + "!\n";
659
660   if (ListInit *LI = dynamic_cast<ListInit*>(R->getValue()))
661     return LI;
662   throw "Record '" + getName() + "', field '" + FieldName +
663         "' does not have a list initializer!";
664 }
665
666 /// getValueAsInt - This method looks up the specified field and returns its
667 /// value as an int, throwing an exception if the field does not exist or if
668 /// the value is not the right type.
669 ///
670 int Record::getValueAsInt(const std::string &FieldName) const {
671   const RecordVal *R = getValue(FieldName);
672   if (R == 0 || R->getValue() == 0)
673     throw "Record '" + getName() + "' does not have a field named '" +
674           FieldName + "!\n";
675
676   if (IntInit *II = dynamic_cast<IntInit*>(R->getValue()))
677     return II->getValue();
678   throw "Record '" + getName() + "', field '" + FieldName +
679         "' does not have a list initializer!";
680 }
681
682 /// getValueAsDef - This method looks up the specified field and returns its
683 /// value as a Record, throwing an exception if the field does not exist or if
684 /// the value is not the right type.
685 ///
686 Record *Record::getValueAsDef(const std::string &FieldName) const {
687   const RecordVal *R = getValue(FieldName);
688   if (R == 0 || R->getValue() == 0)
689     throw "Record '" + getName() + "' does not have a field named '" +
690       FieldName + "!\n";
691
692   if (DefInit *DI = dynamic_cast<DefInit*>(R->getValue()))
693     return DI->getDef();
694   throw "Record '" + getName() + "', field '" + FieldName +
695         "' does not have a list initializer!";
696 }
697
698 /// getValueAsBit - This method looks up the specified field and returns its
699 /// value as a bit, throwing an exception if the field does not exist or if
700 /// the value is not the right type.
701 ///
702 bool Record::getValueAsBit(const std::string &FieldName) const {
703   const RecordVal *R = getValue(FieldName);
704   if (R == 0 || R->getValue() == 0)
705     throw "Record '" + getName() + "' does not have a field named '" +
706       FieldName + "!\n";
707
708   if (BitInit *BI = dynamic_cast<BitInit*>(R->getValue()))
709     return BI->getValue();
710   throw "Record '" + getName() + "', field '" + FieldName +
711         "' does not have a bit initializer!";
712 }
713
714 /// getValueAsDag - This method looks up the specified field and returns its
715 /// value as an Dag, throwing an exception if the field does not exist or if
716 /// the value is not the right type.
717 ///
718 DagInit *Record::getValueAsDag(const std::string &FieldName) const {
719   const RecordVal *R = getValue(FieldName);
720   if (R == 0 || R->getValue() == 0)
721     throw "Record '" + getName() + "' does not have a field named '" +
722       FieldName + "!\n";
723
724   if (DagInit *DI = dynamic_cast<DagInit*>(R->getValue()))
725     return DI;
726   throw "Record '" + getName() + "', field '" + FieldName +
727         "' does not have a dag initializer!";
728 }
729
730
731 void RecordKeeper::dump() const { std::cerr << *this; }
732
733 std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK) {
734   OS << "------------- Classes -----------------\n";
735   const std::map<std::string, Record*> &Classes = RK.getClasses();
736   for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
737          E = Classes.end(); I != E; ++I)
738     OS << "class " << *I->second;
739   
740   OS << "------------- Defs -----------------\n";
741   const std::map<std::string, Record*> &Defs = RK.getDefs();
742   for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
743          E = Defs.end(); I != E; ++I)
744     OS << "def " << *I->second;
745   return OS;
746 }
747
748
749 /// getAllDerivedDefinitions - This method returns all concrete definitions
750 /// that derive from the specified class name.  If a class with the specified
751 /// name does not exist, an error is printed and true is returned.
752 std::vector<Record*>
753 RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
754   Record *Class = Records.getClass(ClassName);
755   if (!Class)
756     throw "ERROR: Couldn't find the '" + ClassName + "' class!\n";
757
758   std::vector<Record*> Defs;
759   for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(),
760          E = getDefs().end(); I != E; ++I)
761     if (I->second->isSubClassOf(Class))
762       Defs.push_back(I->second);
763
764   return Defs;
765 }
766
767 } // End llvm namespace