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