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