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 //===----------------------------------------------------------------------===//
10 // Implement the tablegen record classes.
12 //===----------------------------------------------------------------------===//
15 #include "llvm/Support/DataTypes.h"
16 #include "llvm/Support/Streams.h"
21 //===----------------------------------------------------------------------===//
22 // Type implementations
23 //===----------------------------------------------------------------------===//
25 void RecTy::dump() const { print(*cerr.stream()); }
27 Init *BitRecTy::convertValue(BitsInit *BI) {
28 if (BI->getNumBits() != 1) return 0; // Only accept if just one bit!
32 bool BitRecTy::baseClassOf(const BitsRecTy *RHS) const {
33 return RHS->getNumBits() == 1;
36 Init *BitRecTy::convertValue(IntInit *II) {
37 int Val = II->getValue();
38 if (Val != 0 && Val != 1) return 0; // Only accept 0 or 1 for a bit!
40 return new BitInit(Val != 0);
43 Init *BitRecTy::convertValue(TypedInit *VI) {
44 if (dynamic_cast<BitRecTy*>(VI->getType()))
45 return VI; // Accept variable if it is already of bit type!
49 Init *BitsRecTy::convertValue(UnsetInit *UI) {
50 BitsInit *Ret = new BitsInit(Size);
52 for (unsigned i = 0; i != Size; ++i)
53 Ret->setBit(i, new UnsetInit());
57 Init *BitsRecTy::convertValue(BitInit *UI) {
58 if (Size != 1) return 0; // Can only convert single bit...
59 BitsInit *Ret = new BitsInit(1);
64 // convertValue from Int initializer to bits type: Split the integer up into the
65 // appropriate bits...
67 Init *BitsRecTy::convertValue(IntInit *II) {
68 int64_t Value = II->getValue();
69 // Make sure this bitfield is large enough to hold the integer value...
71 if (Value & ~((1LL << Size)-1))
74 if ((Value >> Size) != -1 || ((Value & (1LL << (Size-1))) == 0))
78 BitsInit *Ret = new BitsInit(Size);
79 for (unsigned i = 0; i != Size; ++i)
80 Ret->setBit(i, new BitInit(Value & (1LL << i)));
85 Init *BitsRecTy::convertValue(BitsInit *BI) {
86 // If the number of bits is right, return it. Otherwise we need to expand or
88 if (BI->getNumBits() == Size) return BI;
92 Init *BitsRecTy::convertValue(TypedInit *VI) {
93 if (BitsRecTy *BRT = dynamic_cast<BitsRecTy*>(VI->getType()))
94 if (BRT->Size == Size) {
95 BitsInit *Ret = new BitsInit(Size);
96 for (unsigned i = 0; i != Size; ++i)
97 Ret->setBit(i, new VarBitInit(VI, i));
100 if (Size == 1 && dynamic_cast<BitRecTy*>(VI->getType())) {
101 BitsInit *Ret = new BitsInit(1);
109 Init *IntRecTy::convertValue(BitInit *BI) {
110 return new IntInit(BI->getValue());
113 Init *IntRecTy::convertValue(BitsInit *BI) {
115 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
116 if (BitInit *Bit = dynamic_cast<BitInit*>(BI->getBit(i))) {
117 Result |= Bit->getValue() << i;
121 return new IntInit(Result);
124 Init *IntRecTy::convertValue(TypedInit *TI) {
125 if (TI->getType()->typeIsConvertibleTo(this))
126 return TI; // Accept variable if already of the right type!
130 Init *StringRecTy::convertValue(BinOpInit *BO) {
131 if (BO->getOpcode() == BinOpInit::STRCONCAT) {
132 Init *L = BO->getLHS()->convertInitializerTo(this);
133 Init *R = BO->getRHS()->convertInitializerTo(this);
134 if (L == 0 || R == 0) return 0;
135 if (L != BO->getLHS() || R != BO->getRHS())
136 return new BinOpInit(BinOpInit::STRCONCAT, L, R);
143 Init *StringRecTy::convertValue(TypedInit *TI) {
144 if (dynamic_cast<StringRecTy*>(TI->getType()))
145 return TI; // Accept variable if already of the right type!
149 void ListRecTy::print(std::ostream &OS) const {
150 OS << "list<" << *Ty << ">";
153 Init *ListRecTy::convertValue(ListInit *LI) {
154 std::vector<Init*> Elements;
156 // Verify that all of the elements of the list are subclasses of the
157 // appropriate class!
158 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
159 if (Init *CI = LI->getElement(i)->convertInitializerTo(Ty))
160 Elements.push_back(CI);
164 return new ListInit(Elements);
167 Init *ListRecTy::convertValue(TypedInit *TI) {
168 // Ensure that TI is compatible with our class.
169 if (ListRecTy *LRT = dynamic_cast<ListRecTy*>(TI->getType()))
170 if (LRT->getElementType()->typeIsConvertibleTo(getElementType()))
175 Init *CodeRecTy::convertValue(TypedInit *TI) {
176 if (TI->getType()->typeIsConvertibleTo(this))
181 Init *DagRecTy::convertValue(TypedInit *TI) {
182 if (TI->getType()->typeIsConvertibleTo(this))
188 void RecordRecTy::print(std::ostream &OS) const {
189 OS << Rec->getName();
192 Init *RecordRecTy::convertValue(DefInit *DI) {
193 // Ensure that DI is a subclass of Rec.
194 if (!DI->getDef()->isSubClassOf(Rec))
199 Init *RecordRecTy::convertValue(TypedInit *TI) {
200 // Ensure that TI is compatible with Rec.
201 if (RecordRecTy *RRT = dynamic_cast<RecordRecTy*>(TI->getType()))
202 if (RRT->getRecord()->isSubClassOf(getRecord()) ||
203 RRT->getRecord() == getRecord())
208 bool RecordRecTy::baseClassOf(const RecordRecTy *RHS) const {
209 return Rec == RHS->getRecord() || RHS->getRecord()->isSubClassOf(Rec);
213 //===----------------------------------------------------------------------===//
214 // Initializer implementations
215 //===----------------------------------------------------------------------===//
217 void Init::dump() const { return print(*cerr.stream()); }
219 Init *BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
220 BitsInit *BI = new BitsInit(Bits.size());
221 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
222 if (Bits[i] >= getNumBits()) {
226 BI->setBit(i, getBit(Bits[i]));
231 void BitsInit::print(std::ostream &OS) const {
232 //if (!printInHex(OS)) return;
233 //if (!printAsVariable(OS)) return;
234 //if (!printAsUnset(OS)) return;
237 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
239 if (Init *Bit = getBit(e-i-1))
247 bool BitsInit::printInHex(std::ostream &OS) const {
248 // First, attempt to convert the value into an integer value...
250 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
251 if (BitInit *Bit = dynamic_cast<BitInit*>(getBit(i))) {
252 Result |= Bit->getValue() << i;
257 OS << "0x" << std::hex << Result << std::dec;
261 bool BitsInit::printAsVariable(std::ostream &OS) const {
262 // Get the variable that we may be set equal to...
263 assert(getNumBits() != 0);
264 VarBitInit *FirstBit = dynamic_cast<VarBitInit*>(getBit(0));
265 if (FirstBit == 0) return true;
266 TypedInit *Var = FirstBit->getVariable();
268 // Check to make sure the types are compatible.
269 BitsRecTy *Ty = dynamic_cast<BitsRecTy*>(FirstBit->getVariable()->getType());
270 if (Ty == 0) return true;
271 if (Ty->getNumBits() != getNumBits()) return true; // Incompatible types!
273 // Check to make sure all bits are referring to the right bits in the variable
274 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
275 VarBitInit *Bit = dynamic_cast<VarBitInit*>(getBit(i));
276 if (Bit == 0 || Bit->getVariable() != Var || Bit->getBitNum() != i)
284 bool BitsInit::printAsUnset(std::ostream &OS) const {
285 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
286 if (!dynamic_cast<UnsetInit*>(getBit(i)))
292 // resolveReferences - If there are any field references that refer to fields
293 // that have been filled in, we can propagate the values now.
295 Init *BitsInit::resolveReferences(Record &R, const RecordVal *RV) {
296 bool Changed = false;
297 BitsInit *New = new BitsInit(getNumBits());
299 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
301 Init *CurBit = getBit(i);
305 CurBit = CurBit->resolveReferences(R, RV);
306 Changed |= B != CurBit;
307 } while (B != CurBit);
308 New->setBit(i, CurBit);
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 *BinOpInit::Fold() {
372 switch (getOpcode()) {
373 default: assert(0 && "Unknown binop");
375 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
376 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
378 return new StringInit(LHSs->getValue() + RHSs->getValue());
384 IntInit *LHSi = dynamic_cast<IntInit*>(LHS);
385 IntInit *RHSi = dynamic_cast<IntInit*>(RHS);
387 int LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
389 switch (getOpcode()) {
390 default: assert(0 && "Bad opcode!");
391 case SHL: Result = LHSv << RHSv; break;
392 case SRA: Result = LHSv >> RHSv; break;
393 case SRL: Result = (unsigned)LHSv >> (unsigned)RHSv; break;
395 return new IntInit(Result);
403 Init *BinOpInit::resolveReferences(Record &R, const RecordVal *RV) {
404 Init *lhs = LHS->resolveReferences(R, RV);
405 Init *rhs = RHS->resolveReferences(R, RV);
407 if (LHS != lhs || RHS != rhs)
408 return (new BinOpInit(getOpcode(), lhs, rhs))->Fold();
412 void BinOpInit::print(std::ostream &OS) const {
414 case SHL: OS << "!shl"; break;
415 case SRA: OS << "!sra"; break;
416 case SRL: OS << "!srl"; break;
417 case STRCONCAT: OS << "!strconcat"; break;
426 Init *TypedInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
427 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
428 if (T == 0) return 0; // Cannot subscript a non-bits variable...
429 unsigned NumBits = T->getNumBits();
431 BitsInit *BI = new BitsInit(Bits.size());
432 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
433 if (Bits[i] >= NumBits) {
437 BI->setBit(i, new VarBitInit(this, Bits[i]));
442 Init *TypedInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
443 ListRecTy *T = dynamic_cast<ListRecTy*>(getType());
444 if (T == 0) return 0; // Cannot subscript a non-list variable...
446 if (Elements.size() == 1)
447 return new VarListElementInit(this, Elements[0]);
449 std::vector<Init*> ListInits;
450 ListInits.reserve(Elements.size());
451 for (unsigned i = 0, e = Elements.size(); i != e; ++i)
452 ListInits.push_back(new VarListElementInit(this, Elements[i]));
453 return new ListInit(ListInits);
457 Init *VarInit::resolveBitReference(Record &R, const RecordVal *IRV,
459 if (R.isTemplateArg(getName())) return 0;
460 if (IRV && IRV->getName() != getName()) return 0;
462 RecordVal *RV = R.getValue(getName());
463 assert(RV && "Reference to a non-existant variable?");
464 assert(dynamic_cast<BitsInit*>(RV->getValue()));
465 BitsInit *BI = (BitsInit*)RV->getValue();
467 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
468 Init *B = BI->getBit(Bit);
470 if (!dynamic_cast<UnsetInit*>(B)) // If the bit is not set...
471 return B; // Replace the VarBitInit with it.
475 Init *VarInit::resolveListElementReference(Record &R, const RecordVal *IRV,
477 if (R.isTemplateArg(getName())) return 0;
478 if (IRV && IRV->getName() != getName()) return 0;
480 RecordVal *RV = R.getValue(getName());
481 assert(RV && "Reference to a non-existant variable?");
482 ListInit *LI = dynamic_cast<ListInit*>(RV->getValue());
483 assert(LI && "Invalid list element!");
485 if (Elt >= LI->getSize())
486 return 0; // Out of range reference.
487 Init *E = LI->getElement(Elt);
488 if (!dynamic_cast<UnsetInit*>(E)) // If the element is set
489 return E; // Replace the VarListElementInit with it.
494 RecTy *VarInit::getFieldType(const std::string &FieldName) const {
495 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
496 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
497 return RV->getType();
501 Init *VarInit::getFieldInit(Record &R, const std::string &FieldName) const {
502 if (dynamic_cast<RecordRecTy*>(getType()))
503 if (const RecordVal *RV = R.getValue(VarName)) {
504 Init *TheInit = RV->getValue();
505 assert(TheInit != this && "Infinite loop detected!");
506 if (Init *I = TheInit->getFieldInit(R, FieldName))
514 /// resolveReferences - This method is used by classes that refer to other
515 /// variables which may not be defined at the time they expression is formed.
516 /// If a value is set for the variable later, this method will be called on
517 /// users of the value to allow the value to propagate out.
519 Init *VarInit::resolveReferences(Record &R, const RecordVal *RV) {
520 if (RecordVal *Val = R.getValue(VarName))
521 if (RV == Val || (RV == 0 && !dynamic_cast<UnsetInit*>(Val->getValue())))
522 return Val->getValue();
527 Init *VarBitInit::resolveReferences(Record &R, const RecordVal *RV) {
528 if (Init *I = getVariable()->resolveBitReference(R, RV, getBitNum()))
533 Init *VarListElementInit::resolveReferences(Record &R, const RecordVal *RV) {
534 if (Init *I = getVariable()->resolveListElementReference(R, RV,
540 Init *VarListElementInit::resolveBitReference(Record &R, const RecordVal *RV,
542 // FIXME: This should be implemented, to support references like:
547 Init *VarListElementInit::
548 resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) {
549 // FIXME: This should be implemented, to support references like:
554 RecTy *DefInit::getFieldType(const std::string &FieldName) const {
555 if (const RecordVal *RV = Def->getValue(FieldName))
556 return RV->getType();
560 Init *DefInit::getFieldInit(Record &R, const std::string &FieldName) const {
561 return Def->getValue(FieldName)->getValue();
565 void DefInit::print(std::ostream &OS) const {
566 OS << Def->getName();
569 Init *FieldInit::resolveBitReference(Record &R, const RecordVal *RV,
571 if (Init *BitsVal = Rec->getFieldInit(R, FieldName))
572 if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
573 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
574 Init *B = BI->getBit(Bit);
576 if (dynamic_cast<BitInit*>(B)) // If the bit is set...
577 return B; // Replace the VarBitInit with it.
582 Init *FieldInit::resolveListElementReference(Record &R, const RecordVal *RV,
584 if (Init *ListVal = Rec->getFieldInit(R, FieldName))
585 if (ListInit *LI = dynamic_cast<ListInit*>(ListVal)) {
586 if (Elt >= LI->getSize()) return 0;
587 Init *E = LI->getElement(Elt);
589 if (!dynamic_cast<UnsetInit*>(E)) // If the bit is set...
590 return E; // Replace the VarListElementInit with it.
595 Init *FieldInit::resolveReferences(Record &R, const RecordVal *RV) {
596 Init *NewRec = RV ? Rec->resolveReferences(R, RV) : Rec;
598 Init *BitsVal = NewRec->getFieldInit(R, FieldName);
600 Init *BVR = BitsVal->resolveReferences(R, RV);
601 return BVR->isComplete() ? BVR : this;
606 NewRec->dump(); cerr << "\n";
607 return new FieldInit(NewRec, FieldName);
612 Init *DagInit::resolveReferences(Record &R, const RecordVal *RV) {
613 std::vector<Init*> NewArgs;
614 for (unsigned i = 0, e = Args.size(); i != e; ++i)
615 NewArgs.push_back(Args[i]->resolveReferences(R, RV));
617 Init *Op = Val->resolveReferences(R, RV);
619 if (Args != NewArgs || Op != Val)
620 return new DagInit(Op, NewArgs, ArgNames);
626 void DagInit::print(std::ostream &OS) const {
629 OS << " " << *Args[0];
630 if (!ArgNames[0].empty()) OS << ":$" << ArgNames[0];
631 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
632 OS << ", " << *Args[i];
633 if (!ArgNames[i].empty()) OS << ":$" << ArgNames[i];
640 //===----------------------------------------------------------------------===//
641 // Other implementations
642 //===----------------------------------------------------------------------===//
644 RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
645 : Name(N), Ty(T), Prefix(P) {
646 Value = Ty->convertValue(new UnsetInit());
647 assert(Value && "Cannot create unset value for current type!");
650 void RecordVal::dump() const { cerr << *this; }
652 void RecordVal::print(std::ostream &OS, bool PrintSem) const {
653 if (getPrefix()) OS << "field ";
654 OS << *getType() << " " << getName();
657 OS << " = " << *getValue();
659 if (PrintSem) OS << ";\n";
662 void Record::setName(const std::string &Name) {
663 if (Records.getDef(getName()) == this) {
664 Records.removeDef(getName());
666 Records.addDef(this);
668 Records.removeClass(getName());
670 Records.addClass(this);
674 /// resolveReferencesTo - If anything in this record refers to RV, replace the
675 /// reference to RV with the RHS of RV. If RV is null, we resolve all possible
677 void Record::resolveReferencesTo(const RecordVal *RV) {
678 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
679 if (Init *V = Values[i].getValue())
680 Values[i].setValue(V->resolveReferences(*this, RV));
685 void Record::dump() const { cerr << *this; }
687 std::ostream &llvm::operator<<(std::ostream &OS, const Record &R) {
690 const std::vector<std::string> &TArgs = R.getTemplateArgs();
691 if (!TArgs.empty()) {
693 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
695 const RecordVal *RV = R.getValue(TArgs[i]);
696 assert(RV && "Template argument record not found??");
697 RV->print(OS, false);
703 const std::vector<Record*> &SC = R.getSuperClasses();
706 for (unsigned i = 0, e = SC.size(); i != e; ++i)
707 OS << " " << SC[i]->getName();
711 const std::vector<RecordVal> &Vals = R.getValues();
712 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
713 if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
715 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
716 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
722 /// getValueInit - Return the initializer for a value with the specified name,
723 /// or throw an exception if the field does not exist.
725 Init *Record::getValueInit(const std::string &FieldName) const {
726 const RecordVal *R = getValue(FieldName);
727 if (R == 0 || R->getValue() == 0)
728 throw "Record `" + getName() + "' does not have a field named `" +
730 return R->getValue();
734 /// getValueAsString - This method looks up the specified field and returns its
735 /// value as a string, throwing an exception if the field does not exist or if
736 /// the value is not a string.
738 std::string Record::getValueAsString(const std::string &FieldName) const {
739 const RecordVal *R = getValue(FieldName);
740 if (R == 0 || R->getValue() == 0)
741 throw "Record `" + getName() + "' does not have a field named `" +
744 if (const StringInit *SI = dynamic_cast<const StringInit*>(R->getValue()))
745 return SI->getValue();
746 throw "Record `" + getName() + "', field `" + FieldName +
747 "' does not have a string initializer!";
750 /// getValueAsBitsInit - This method looks up the specified field and returns
751 /// its value as a BitsInit, throwing an exception if the field does not exist
752 /// or if the value is not the right type.
754 BitsInit *Record::getValueAsBitsInit(const std::string &FieldName) const {
755 const RecordVal *R = getValue(FieldName);
756 if (R == 0 || R->getValue() == 0)
757 throw "Record `" + getName() + "' does not have a field named `" +
760 if (BitsInit *BI = dynamic_cast<BitsInit*>(R->getValue()))
762 throw "Record `" + getName() + "', field `" + FieldName +
763 "' does not have a BitsInit initializer!";
766 /// getValueAsListInit - This method looks up the specified field and returns
767 /// its value as a ListInit, throwing an exception if the field does not exist
768 /// or if the value is not the right type.
770 ListInit *Record::getValueAsListInit(const std::string &FieldName) const {
771 const RecordVal *R = getValue(FieldName);
772 if (R == 0 || R->getValue() == 0)
773 throw "Record `" + getName() + "' does not have a field named `" +
776 if (ListInit *LI = dynamic_cast<ListInit*>(R->getValue()))
778 throw "Record `" + getName() + "', field `" + FieldName +
779 "' does not have a list initializer!";
782 /// getValueAsListOfDefs - This method looks up the specified field and returns
783 /// its value as a vector of records, throwing an exception if the field does
784 /// not exist or if the value is not the right type.
787 Record::getValueAsListOfDefs(const std::string &FieldName) const {
788 ListInit *List = getValueAsListInit(FieldName);
789 std::vector<Record*> Defs;
790 for (unsigned i = 0; i < List->getSize(); i++) {
791 if (DefInit *DI = dynamic_cast<DefInit*>(List->getElement(i))) {
792 Defs.push_back(DI->getDef());
794 throw "Record `" + getName() + "', field `" + FieldName +
795 "' list is not entirely DefInit!";
801 /// getValueAsInt - This method looks up the specified field and returns its
802 /// value as an int, throwing an exception if the field does not exist or if
803 /// the value is not the right type.
805 int Record::getValueAsInt(const std::string &FieldName) const {
806 const RecordVal *R = getValue(FieldName);
807 if (R == 0 || R->getValue() == 0)
808 throw "Record `" + getName() + "' does not have a field named `" +
811 if (IntInit *II = dynamic_cast<IntInit*>(R->getValue()))
812 return II->getValue();
813 throw "Record `" + getName() + "', field `" + FieldName +
814 "' does not have an int initializer!";
817 /// getValueAsDef - This method looks up the specified field and returns its
818 /// value as a Record, throwing an exception if the field does not exist or if
819 /// the value is not the right type.
821 Record *Record::getValueAsDef(const std::string &FieldName) const {
822 const RecordVal *R = getValue(FieldName);
823 if (R == 0 || R->getValue() == 0)
824 throw "Record `" + getName() + "' does not have a field named `" +
827 if (DefInit *DI = dynamic_cast<DefInit*>(R->getValue()))
829 throw "Record `" + getName() + "', field `" + FieldName +
830 "' does not have a def initializer!";
833 /// getValueAsBit - This method looks up the specified field and returns its
834 /// value as a bit, throwing an exception if the field does not exist or if
835 /// the value is not the right type.
837 bool Record::getValueAsBit(const std::string &FieldName) const {
838 const RecordVal *R = getValue(FieldName);
839 if (R == 0 || R->getValue() == 0)
840 throw "Record `" + getName() + "' does not have a field named `" +
843 if (BitInit *BI = dynamic_cast<BitInit*>(R->getValue()))
844 return BI->getValue();
845 throw "Record `" + getName() + "', field `" + FieldName +
846 "' does not have a bit initializer!";
849 /// getValueAsDag - This method looks up the specified field and returns its
850 /// value as an Dag, throwing an exception if the field does not exist or if
851 /// the value is not the right type.
853 DagInit *Record::getValueAsDag(const std::string &FieldName) const {
854 const RecordVal *R = getValue(FieldName);
855 if (R == 0 || R->getValue() == 0)
856 throw "Record `" + getName() + "' does not have a field named `" +
859 if (DagInit *DI = dynamic_cast<DagInit*>(R->getValue()))
861 throw "Record `" + getName() + "', field `" + FieldName +
862 "' does not have a dag initializer!";
865 std::string Record::getValueAsCode(const std::string &FieldName) const {
866 const RecordVal *R = getValue(FieldName);
867 if (R == 0 || R->getValue() == 0)
868 throw "Record `" + getName() + "' does not have a field named `" +
871 if (const CodeInit *CI = dynamic_cast<const CodeInit*>(R->getValue()))
872 return CI->getValue();
873 throw "Record `" + getName() + "', field `" + FieldName +
874 "' does not have a code initializer!";
878 void RecordKeeper::dump() const { cerr << *this; }
880 std::ostream &llvm::operator<<(std::ostream &OS, const RecordKeeper &RK) {
881 OS << "------------- Classes -----------------\n";
882 const std::map<std::string, Record*> &Classes = RK.getClasses();
883 for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
884 E = Classes.end(); I != E; ++I)
885 OS << "class " << *I->second;
887 OS << "------------- Defs -----------------\n";
888 const std::map<std::string, Record*> &Defs = RK.getDefs();
889 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
890 E = Defs.end(); I != E; ++I)
891 OS << "def " << *I->second;
896 /// getAllDerivedDefinitions - This method returns all concrete definitions
897 /// that derive from the specified class name. If a class with the specified
898 /// name does not exist, an error is printed and true is returned.
900 RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
901 Record *Class = Records.getClass(ClassName);
903 throw "ERROR: Couldn't find the `" + ClassName + "' class!\n";
905 std::vector<Record*> Defs;
906 for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(),
907 E = getDefs().end(); I != E; ++I)
908 if (I->second->isSubClassOf(Class))
909 Defs.push_back(I->second);