1 //===- Record.cpp - Record implementation ---------------------------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
11 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/DataTypes.h"
19 //===----------------------------------------------------------------------===//
20 // Type implementations
21 //===----------------------------------------------------------------------===//
23 void RecTy::dump() const { print(std::cerr); }
25 Init *BitRecTy::convertValue(BitsInit *BI) {
26 if (BI->getNumBits() != 1) return 0; // Only accept if just one bit!
30 bool BitRecTy::baseClassOf(const BitsRecTy *RHS) const {
31 return RHS->getNumBits() == 1;
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!
38 return new BitInit(Val != 0);
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!
47 Init *BitsRecTy::convertValue(UnsetInit *UI) {
48 BitsInit *Ret = new BitsInit(Size);
50 for (unsigned i = 0; i != Size; ++i)
51 Ret->setBit(i, new UnsetInit());
55 Init *BitsRecTy::convertValue(BitInit *UI) {
56 if (Size != 1) return 0; // Can only convert single bit...
57 BitsInit *Ret = new BitsInit(1);
62 // convertValue from Int initializer to bits type: Split the integer up into the
63 // appropriate bits...
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...
69 if (Value & ~((1LL << Size)-1))
72 if ((Value >> Size) != -1 || ((Value & (1LL << (Size-1))) == 0))
76 BitsInit *Ret = new BitsInit(Size);
77 for (unsigned i = 0; i != Size; ++i)
78 Ret->setBit(i, new BitInit(Value & (1LL << i)));
83 Init *BitsRecTy::convertValue(BitsInit *BI) {
84 // If the number of bits is right, return it. Otherwise we need to expand or
86 if (BI->getNumBits() == Size) return BI;
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));
98 if (Size == 1 && dynamic_cast<BitRecTy*>(VI->getType())) {
99 BitsInit *Ret = new BitsInit(1);
107 Init *IntRecTy::convertValue(BitInit *BI) {
108 return new IntInit(BI->getValue());
111 Init *IntRecTy::convertValue(BitsInit *BI) {
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;
119 return new IntInit(Result);
122 Init *IntRecTy::convertValue(TypedInit *TI) {
123 if (TI->getType()->typeIsConvertibleTo(this))
124 return TI; // Accept variable if already of the right type!
128 Init *StringRecTy::convertValue(TypedInit *TI) {
129 if (dynamic_cast<StringRecTy*>(TI->getType()))
130 return TI; // Accept variable if already of the right type!
134 void ListRecTy::print(std::ostream &OS) const {
135 OS << "list<" << *Ty << ">";
138 Init *ListRecTy::convertValue(ListInit *LI) {
139 std::vector<Init*> Elements;
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);
149 return new ListInit(Elements);
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()))
160 Init *CodeRecTy::convertValue(TypedInit *TI) {
161 if (TI->getType()->typeIsConvertibleTo(this))
166 Init *DagRecTy::convertValue(TypedInit *TI) {
167 if (TI->getType()->typeIsConvertibleTo(this))
173 void RecordRecTy::print(std::ostream &OS) const {
174 OS << Rec->getName();
177 Init *RecordRecTy::convertValue(DefInit *DI) {
178 // Ensure that DI is a subclass of Rec.
179 if (!DI->getDef()->isSubClassOf(Rec))
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())
193 bool RecordRecTy::baseClassOf(const RecordRecTy *RHS) const {
194 return Rec == RHS->getRecord() || RHS->getRecord()->isSubClassOf(Rec);
198 //===----------------------------------------------------------------------===//
199 // Initializer implementations
200 //===----------------------------------------------------------------------===//
202 void Init::dump() const { return print(std::cerr); }
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()) {
211 BI->setBit(i, getBit(Bits[i]));
216 void BitsInit::print(std::ostream &OS) const {
217 //if (!printInHex(OS)) return;
218 //if (!printAsVariable(OS)) return;
219 //if (!printAsUnset(OS)) return;
222 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
224 if (Init *Bit = getBit(e-i-1))
232 bool BitsInit::printInHex(std::ostream &OS) const {
233 // First, attempt to convert the value into an integer value...
235 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
236 if (BitInit *Bit = dynamic_cast<BitInit*>(getBit(i))) {
237 Result |= Bit->getValue() << i;
242 OS << "0x" << std::hex << Result << std::dec;
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();
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!
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)
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)))
277 // resolveReferences - If there are any field references that refer to fields
278 // that have been filled in, we can propagate the values now.
280 Init *BitsInit::resolveReferences(Record &R, const RecordVal *RV) {
281 bool Changed = false;
282 BitsInit *New = new BitsInit(getNumBits());
284 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
286 Init *CurBit = getBit(i);
290 CurBit = CurBit->resolveReferences(R, RV);
291 Changed |= B != CurBit;
292 } while (B != CurBit);
293 New->setBit(i, CurBit);
302 Init *IntInit::getBinaryOp(BinaryOp Op, Init *RHS) {
303 IntInit *RHSi = dynamic_cast<IntInit*>(RHS);
304 if (RHSi == 0) return 0;
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;
313 return new IntInit(NewValue);
317 Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
318 BitsInit *BI = new BitsInit(Bits.size());
320 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
325 BI->setBit(i, new BitInit(Value & (1 << Bits[i])));
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())
335 Vals.push_back(getElement(Elements[i]));
337 return new ListInit(Vals);
340 Init *ListInit::resolveReferences(Record &R, const RecordVal *RV) {
341 std::vector<Init*> Resolved;
342 Resolved.reserve(getSize());
343 bool Changed = false;
345 for (unsigned i = 0, e = getSize(); i != e; ++i) {
347 Init *CurElt = getElement(i);
351 CurElt = CurElt->resolveReferences(R, RV);
352 Changed |= E != CurElt;
353 } while (E != CurElt);
354 Resolved.push_back(E);
358 return new ListInit(Resolved);
362 void ListInit::print(std::ostream &OS) const {
364 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
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();
376 BitsInit *BI = new BitsInit(Bits.size());
377 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
378 if (Bits[i] >= NumBits) {
382 BI->setBit(i, new VarBitInit(this, Bits[i]));
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...
391 if (Elements.size() == 1)
392 return new VarListElementInit(this, Elements[0]);
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);
402 Init *VarInit::resolveBitReference(Record &R, const RecordVal *IRV,
404 if (R.isTemplateArg(getName())) return 0;
405 if (IRV && IRV->getName() != getName()) return 0;
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();
412 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
413 Init *B = BI->getBit(Bit);
415 if (!dynamic_cast<UnsetInit*>(B)) // If the bit is not set...
416 return B; // Replace the VarBitInit with it.
420 Init *VarInit::resolveListElementReference(Record &R, const RecordVal *IRV,
422 if (R.isTemplateArg(getName())) return 0;
423 if (IRV && IRV->getName() != getName()) return 0;
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!");
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.
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();
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))
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.
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();
472 Init *VarBitInit::resolveReferences(Record &R, const RecordVal *RV) {
473 if (Init *I = getVariable()->resolveBitReference(R, RV, getBitNum()))
478 Init *VarListElementInit::resolveReferences(Record &R, const RecordVal *RV) {
479 if (Init *I = getVariable()->resolveListElementReference(R, RV,
485 Init *VarListElementInit::resolveBitReference(Record &R, const RecordVal *RV,
487 // FIXME: This should be implemented, to support references like:
492 Init *VarListElementInit::
493 resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) {
494 // FIXME: This should be implemented, to support references like:
499 RecTy *DefInit::getFieldType(const std::string &FieldName) const {
500 if (const RecordVal *RV = Def->getValue(FieldName))
501 return RV->getType();
505 Init *DefInit::getFieldInit(Record &R, const std::string &FieldName) const {
506 return Def->getValue(FieldName)->getValue();
510 void DefInit::print(std::ostream &OS) const {
511 OS << Def->getName();
514 Init *FieldInit::resolveBitReference(Record &R, const RecordVal *RV,
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);
521 if (dynamic_cast<BitInit*>(B)) // If the bit is set...
522 return B; // Replace the VarBitInit with it.
527 Init *FieldInit::resolveListElementReference(Record &R, const RecordVal *RV,
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);
534 if (!dynamic_cast<UnsetInit*>(E)) // If the bit is set...
535 return E; // Replace the VarListElementInit with it.
540 Init *FieldInit::resolveReferences(Record &R, const RecordVal *RV) {
541 Init *NewRec = RV ? Rec->resolveReferences(R, RV) : Rec;
543 Init *BitsVal = NewRec->getFieldInit(R, FieldName);
545 Init *BVR = BitsVal->resolveReferences(R, RV);
546 return BVR->isComplete() ? BVR : this;
551 NewRec->dump(); std::cerr << "\n";
552 return new FieldInit(NewRec, FieldName);
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));
563 return new DagInit(NodeTypeDef, NewArgs, ArgNames);
569 void DagInit::print(std::ostream &OS) const {
570 OS << "(" << NodeTypeDef->getName();
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];
583 //===----------------------------------------------------------------------===//
584 // Other implementations
585 //===----------------------------------------------------------------------===//
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!");
593 void RecordVal::dump() const { std::cerr << *this; }
595 void RecordVal::print(std::ostream &OS, bool PrintSem) const {
596 if (getPrefix()) OS << "field ";
597 OS << *getType() << " " << getName();
600 OS << " = " << *getValue();
602 if (PrintSem) OS << ";\n";
605 void Record::setName(const std::string &Name) {
606 if (Records.getDef(getName()) == this) {
607 Records.removeDef(getName());
609 Records.addDef(this);
611 Records.removeClass(getName());
613 Records.addClass(this);
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
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));
628 void Record::dump() const { std::cerr << *this; }
630 std::ostream &llvm::operator<<(std::ostream &OS, const Record &R) {
633 const std::vector<std::string> &TArgs = R.getTemplateArgs();
634 if (!TArgs.empty()) {
636 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
638 const RecordVal *RV = R.getValue(TArgs[i]);
639 assert(RV && "Template argument record not found??");
640 RV->print(OS, false);
646 const std::vector<Record*> &SC = R.getSuperClasses();
649 for (unsigned i = 0, e = SC.size(); i != e; ++i)
650 OS << " " << SC[i]->getName();
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()))
658 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
659 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
665 /// getValueInit - Return the initializer for a value with the specified name,
666 /// or throw an exception if the field does not exist.
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 `" +
673 return R->getValue();
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.
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 `" +
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!";
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.
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 `" +
703 if (BitsInit *BI = dynamic_cast<BitsInit*>(R->getValue()))
705 throw "Record `" + getName() + "', field `" + FieldName +
706 "' does not have a BitsInit initializer!";
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.
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 `" +
719 if (ListInit *LI = dynamic_cast<ListInit*>(R->getValue()))
721 throw "Record `" + getName() + "', field `" + FieldName +
722 "' does not have a list initializer!";
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.
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());
737 throw "Record `" + getName() + "', field `" + FieldName +
738 "' list is not entirely DefInit!";
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.
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 `" +
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!";
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.
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 `" +
770 if (DefInit *DI = dynamic_cast<DefInit*>(R->getValue()))
772 throw "Record `" + getName() + "', field `" + FieldName +
773 "' does not have a def initializer!";
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.
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 `" +
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!";
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.
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 `" +
802 if (DagInit *DI = dynamic_cast<DagInit*>(R->getValue()))
804 throw "Record `" + getName() + "', field `" + FieldName +
805 "' does not have a dag initializer!";
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 `" +
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!";
821 void RecordKeeper::dump() const { std::cerr << *this; }
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;
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;
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.
843 RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
844 Record *Class = Records.getClass(ClassName);
846 throw "ERROR: Couldn't find the `" + ClassName + "' class!\n";
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);