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"
20 //===----------------------------------------------------------------------===//
21 // Type implementations
22 //===----------------------------------------------------------------------===//
24 void RecTy::dump() const { print(std::cerr); }
26 Init *BitRecTy::convertValue(BitsInit *BI) {
27 if (BI->getNumBits() != 1) return 0; // Only accept if just one bit!
31 bool BitRecTy::baseClassOf(const BitsRecTy *RHS) const {
32 return RHS->getNumBits() == 1;
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!
39 return new BitInit(Val != 0);
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!
48 Init *BitsRecTy::convertValue(UnsetInit *UI) {
49 BitsInit *Ret = new BitsInit(Size);
51 for (unsigned i = 0; i != Size; ++i)
52 Ret->setBit(i, new UnsetInit());
56 Init *BitsRecTy::convertValue(BitInit *UI) {
57 if (Size != 1) return 0; // Can only convert single bit...
58 BitsInit *Ret = new BitsInit(1);
63 // convertValue from Int initializer to bits type: Split the integer up into the
64 // appropriate bits...
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...
70 if (Value & ~((1LL << Size)-1))
73 if ((Value >> Size) != -1 || ((Value & (1LL << (Size-1))) == 0))
77 BitsInit *Ret = new BitsInit(Size);
78 for (unsigned i = 0; i != Size; ++i)
79 Ret->setBit(i, new BitInit(Value & (1LL << i)));
84 Init *BitsRecTy::convertValue(BitsInit *BI) {
85 // If the number of bits is right, return it. Otherwise we need to expand or
87 if (BI->getNumBits() == Size) return BI;
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));
99 if (Size == 1 && dynamic_cast<BitRecTy*>(VI->getType())) {
100 BitsInit *Ret = new BitsInit(1);
108 Init *IntRecTy::convertValue(BitInit *BI) {
109 return new IntInit(BI->getValue());
112 Init *IntRecTy::convertValue(BitsInit *BI) {
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;
120 return new IntInit(Result);
123 Init *IntRecTy::convertValue(TypedInit *TI) {
124 if (TI->getType()->typeIsConvertibleTo(this))
125 return TI; // Accept variable if already of the right type!
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);
142 Init *StringRecTy::convertValue(TypedInit *TI) {
143 if (dynamic_cast<StringRecTy*>(TI->getType()))
144 return TI; // Accept variable if already of the right type!
148 void ListRecTy::print(std::ostream &OS) const {
149 OS << "list<" << *Ty << ">";
152 Init *ListRecTy::convertValue(ListInit *LI) {
153 std::vector<Init*> Elements;
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);
163 return new ListInit(Elements);
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()))
174 Init *CodeRecTy::convertValue(TypedInit *TI) {
175 if (TI->getType()->typeIsConvertibleTo(this))
180 Init *DagRecTy::convertValue(TypedInit *TI) {
181 if (TI->getType()->typeIsConvertibleTo(this))
187 void RecordRecTy::print(std::ostream &OS) const {
188 OS << Rec->getName();
191 Init *RecordRecTy::convertValue(DefInit *DI) {
192 // Ensure that DI is a subclass of Rec.
193 if (!DI->getDef()->isSubClassOf(Rec))
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())
207 bool RecordRecTy::baseClassOf(const RecordRecTy *RHS) const {
208 return Rec == RHS->getRecord() || RHS->getRecord()->isSubClassOf(Rec);
212 //===----------------------------------------------------------------------===//
213 // Initializer implementations
214 //===----------------------------------------------------------------------===//
216 void Init::dump() const { return print(std::cerr); }
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()) {
225 BI->setBit(i, getBit(Bits[i]));
230 void BitsInit::print(std::ostream &OS) const {
231 //if (!printInHex(OS)) return;
232 //if (!printAsVariable(OS)) return;
233 //if (!printAsUnset(OS)) return;
236 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
238 if (Init *Bit = getBit(e-i-1))
246 bool BitsInit::printInHex(std::ostream &OS) const {
247 // First, attempt to convert the value into an integer value...
249 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
250 if (BitInit *Bit = dynamic_cast<BitInit*>(getBit(i))) {
251 Result |= Bit->getValue() << i;
256 OS << "0x" << std::hex << Result << std::dec;
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();
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!
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)
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)))
291 // resolveReferences - If there are any field references that refer to fields
292 // that have been filled in, we can propagate the values now.
294 Init *BitsInit::resolveReferences(Record &R, const RecordVal *RV) {
295 bool Changed = false;
296 BitsInit *New = new BitsInit(getNumBits());
298 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
300 Init *CurBit = getBit(i);
304 CurBit = CurBit->resolveReferences(R, RV);
305 Changed |= B != CurBit;
306 } while (B != CurBit);
307 New->setBit(i, CurBit);
316 Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
317 BitsInit *BI = new BitsInit(Bits.size());
319 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
324 BI->setBit(i, new BitInit(Value & (1 << Bits[i])));
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())
334 Vals.push_back(getElement(Elements[i]));
336 return new ListInit(Vals);
339 Init *ListInit::resolveReferences(Record &R, const RecordVal *RV) {
340 std::vector<Init*> Resolved;
341 Resolved.reserve(getSize());
342 bool Changed = false;
344 for (unsigned i = 0, e = getSize(); i != e; ++i) {
346 Init *CurElt = getElement(i);
350 CurElt = CurElt->resolveReferences(R, RV);
351 Changed |= E != CurElt;
352 } while (E != CurElt);
353 Resolved.push_back(E);
357 return new ListInit(Resolved);
361 void ListInit::print(std::ostream &OS) const {
363 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
370 Init *BinOpInit::Fold() {
371 switch (getOpcode()) {
372 default: assert(0 && "Unknown binop");
374 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
375 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
377 return new StringInit(LHSs->getValue() + RHSs->getValue());
383 IntInit *LHSi = dynamic_cast<IntInit*>(LHS);
384 IntInit *RHSi = dynamic_cast<IntInit*>(RHS);
386 int LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
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;
394 return new IntInit(Result);
402 Init *BinOpInit::resolveReferences(Record &R, const RecordVal *RV) {
403 Init *lhs = LHS->resolveReferences(R, RV);
404 Init *rhs = RHS->resolveReferences(R, RV);
406 if (LHS != lhs || RHS != rhs)
407 return (new BinOpInit(getOpcode(), lhs, rhs))->Fold();
411 void BinOpInit::print(std::ostream &OS) const {
413 case SHL: OS << "!shl"; break;
414 case SRA: OS << "!sra"; break;
415 case SRL: OS << "!srl"; break;
416 case STRCONCAT: OS << "!strconcat"; break;
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();
430 BitsInit *BI = new BitsInit(Bits.size());
431 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
432 if (Bits[i] >= NumBits) {
436 BI->setBit(i, new VarBitInit(this, Bits[i]));
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...
445 if (Elements.size() == 1)
446 return new VarListElementInit(this, Elements[0]);
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);
456 Init *VarInit::resolveBitReference(Record &R, const RecordVal *IRV,
458 if (R.isTemplateArg(getName())) return 0;
459 if (IRV && IRV->getName() != getName()) return 0;
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();
466 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
467 Init *B = BI->getBit(Bit);
469 if (!dynamic_cast<UnsetInit*>(B)) // If the bit is not set...
470 return B; // Replace the VarBitInit with it.
474 Init *VarInit::resolveListElementReference(Record &R, const RecordVal *IRV,
476 if (R.isTemplateArg(getName())) return 0;
477 if (IRV && IRV->getName() != getName()) return 0;
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!");
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.
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();
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))
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.
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();
526 Init *VarBitInit::resolveReferences(Record &R, const RecordVal *RV) {
527 if (Init *I = getVariable()->resolveBitReference(R, RV, getBitNum()))
532 Init *VarListElementInit::resolveReferences(Record &R, const RecordVal *RV) {
533 if (Init *I = getVariable()->resolveListElementReference(R, RV,
539 Init *VarListElementInit::resolveBitReference(Record &R, const RecordVal *RV,
541 // FIXME: This should be implemented, to support references like:
546 Init *VarListElementInit::
547 resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) {
548 // FIXME: This should be implemented, to support references like:
553 RecTy *DefInit::getFieldType(const std::string &FieldName) const {
554 if (const RecordVal *RV = Def->getValue(FieldName))
555 return RV->getType();
559 Init *DefInit::getFieldInit(Record &R, const std::string &FieldName) const {
560 return Def->getValue(FieldName)->getValue();
564 void DefInit::print(std::ostream &OS) const {
565 OS << Def->getName();
568 Init *FieldInit::resolveBitReference(Record &R, const RecordVal *RV,
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);
575 if (dynamic_cast<BitInit*>(B)) // If the bit is set...
576 return B; // Replace the VarBitInit with it.
581 Init *FieldInit::resolveListElementReference(Record &R, const RecordVal *RV,
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);
588 if (!dynamic_cast<UnsetInit*>(E)) // If the bit is set...
589 return E; // Replace the VarListElementInit with it.
594 Init *FieldInit::resolveReferences(Record &R, const RecordVal *RV) {
595 Init *NewRec = RV ? Rec->resolveReferences(R, RV) : Rec;
597 Init *BitsVal = NewRec->getFieldInit(R, FieldName);
599 Init *BVR = BitsVal->resolveReferences(R, RV);
600 return BVR->isComplete() ? BVR : this;
605 NewRec->dump(); std::cerr << "\n";
606 return new FieldInit(NewRec, FieldName);
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));
616 Init *Op = Val->resolveReferences(R, RV);
618 if (Args != NewArgs || Op != Val)
619 return new DagInit(Op, NewArgs, ArgNames);
625 void DagInit::print(std::ostream &OS) const {
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];
639 //===----------------------------------------------------------------------===//
640 // Other implementations
641 //===----------------------------------------------------------------------===//
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!");
649 void RecordVal::dump() const { std::cerr << *this; }
651 void RecordVal::print(std::ostream &OS, bool PrintSem) const {
652 if (getPrefix()) OS << "field ";
653 OS << *getType() << " " << getName();
656 OS << " = " << *getValue();
658 if (PrintSem) OS << ";\n";
661 void Record::setName(const std::string &Name) {
662 if (Records.getDef(getName()) == this) {
663 Records.removeDef(getName());
665 Records.addDef(this);
667 Records.removeClass(getName());
669 Records.addClass(this);
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
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));
684 void Record::dump() const { std::cerr << *this; }
686 std::ostream &llvm::operator<<(std::ostream &OS, const Record &R) {
689 const std::vector<std::string> &TArgs = R.getTemplateArgs();
690 if (!TArgs.empty()) {
692 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
694 const RecordVal *RV = R.getValue(TArgs[i]);
695 assert(RV && "Template argument record not found??");
696 RV->print(OS, false);
702 const std::vector<Record*> &SC = R.getSuperClasses();
705 for (unsigned i = 0, e = SC.size(); i != e; ++i)
706 OS << " " << SC[i]->getName();
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()))
714 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
715 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
721 /// getValueInit - Return the initializer for a value with the specified name,
722 /// or throw an exception if the field does not exist.
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 `" +
729 return R->getValue();
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.
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 `" +
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!";
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.
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 `" +
759 if (BitsInit *BI = dynamic_cast<BitsInit*>(R->getValue()))
761 throw "Record `" + getName() + "', field `" + FieldName +
762 "' does not have a BitsInit initializer!";
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.
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 `" +
775 if (ListInit *LI = dynamic_cast<ListInit*>(R->getValue()))
777 throw "Record `" + getName() + "', field `" + FieldName +
778 "' does not have a list initializer!";
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.
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());
793 throw "Record `" + getName() + "', field `" + FieldName +
794 "' list is not entirely DefInit!";
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.
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 `" +
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!";
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.
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 `" +
826 if (DefInit *DI = dynamic_cast<DefInit*>(R->getValue()))
828 throw "Record `" + getName() + "', field `" + FieldName +
829 "' does not have a def initializer!";
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.
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 `" +
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!";
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.
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 `" +
858 if (DagInit *DI = dynamic_cast<DagInit*>(R->getValue()))
860 throw "Record `" + getName() + "', field `" + FieldName +
861 "' does not have a dag initializer!";
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 `" +
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!";
877 void RecordKeeper::dump() const { std::cerr << *this; }
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;
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;
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.
899 RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
900 Record *Class = Records.getClass(ClassName);
902 throw "ERROR: Couldn't find the `" + ClassName + "' class!\n";
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);