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))
187 Init *DagRecTy::convertValue(BinOpInit *BO) {
188 if (BO->getOpcode() == BinOpInit::CONCAT) {
189 Init *L = BO->getLHS()->convertInitializerTo(this);
190 Init *R = BO->getRHS()->convertInitializerTo(this);
191 if (L == 0 || R == 0) return 0;
192 if (L != BO->getLHS() || R != BO->getRHS())
193 return new BinOpInit(BinOpInit::CONCAT, L, R);
200 void RecordRecTy::print(std::ostream &OS) const {
201 OS << Rec->getName();
204 Init *RecordRecTy::convertValue(DefInit *DI) {
205 // Ensure that DI is a subclass of Rec.
206 if (!DI->getDef()->isSubClassOf(Rec))
211 Init *RecordRecTy::convertValue(TypedInit *TI) {
212 // Ensure that TI is compatible with Rec.
213 if (RecordRecTy *RRT = dynamic_cast<RecordRecTy*>(TI->getType()))
214 if (RRT->getRecord()->isSubClassOf(getRecord()) ||
215 RRT->getRecord() == getRecord())
220 bool RecordRecTy::baseClassOf(const RecordRecTy *RHS) const {
221 return Rec == RHS->getRecord() || RHS->getRecord()->isSubClassOf(Rec);
225 //===----------------------------------------------------------------------===//
226 // Initializer implementations
227 //===----------------------------------------------------------------------===//
229 void Init::dump() const { return print(*cerr.stream()); }
231 Init *BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
232 BitsInit *BI = new BitsInit(Bits.size());
233 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
234 if (Bits[i] >= getNumBits()) {
238 BI->setBit(i, getBit(Bits[i]));
243 void BitsInit::print(std::ostream &OS) const {
244 //if (!printInHex(OS)) return;
245 //if (!printAsVariable(OS)) return;
246 //if (!printAsUnset(OS)) return;
249 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
251 if (Init *Bit = getBit(e-i-1))
259 bool BitsInit::printInHex(std::ostream &OS) const {
260 // First, attempt to convert the value into an integer value...
262 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
263 if (BitInit *Bit = dynamic_cast<BitInit*>(getBit(i))) {
264 Result |= Bit->getValue() << i;
269 OS << "0x" << std::hex << Result << std::dec;
273 bool BitsInit::printAsVariable(std::ostream &OS) const {
274 // Get the variable that we may be set equal to...
275 assert(getNumBits() != 0);
276 VarBitInit *FirstBit = dynamic_cast<VarBitInit*>(getBit(0));
277 if (FirstBit == 0) return true;
278 TypedInit *Var = FirstBit->getVariable();
280 // Check to make sure the types are compatible.
281 BitsRecTy *Ty = dynamic_cast<BitsRecTy*>(FirstBit->getVariable()->getType());
282 if (Ty == 0) return true;
283 if (Ty->getNumBits() != getNumBits()) return true; // Incompatible types!
285 // Check to make sure all bits are referring to the right bits in the variable
286 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
287 VarBitInit *Bit = dynamic_cast<VarBitInit*>(getBit(i));
288 if (Bit == 0 || Bit->getVariable() != Var || Bit->getBitNum() != i)
296 bool BitsInit::printAsUnset(std::ostream &OS) const {
297 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
298 if (!dynamic_cast<UnsetInit*>(getBit(i)))
304 // resolveReferences - If there are any field references that refer to fields
305 // that have been filled in, we can propagate the values now.
307 Init *BitsInit::resolveReferences(Record &R, const RecordVal *RV) {
308 bool Changed = false;
309 BitsInit *New = new BitsInit(getNumBits());
311 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
313 Init *CurBit = getBit(i);
317 CurBit = CurBit->resolveReferences(R, RV);
318 Changed |= B != CurBit;
319 } while (B != CurBit);
320 New->setBit(i, CurBit);
329 Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
330 BitsInit *BI = new BitsInit(Bits.size());
332 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
337 BI->setBit(i, new BitInit(Value & (1 << Bits[i])));
342 Init *ListInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
343 std::vector<Init*> Vals;
344 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
345 if (Elements[i] >= getSize())
347 Vals.push_back(getElement(Elements[i]));
349 return new ListInit(Vals);
352 Record *ListInit::getElementAsRecord(unsigned i) const {
353 assert(i < Values.size() && "List element index out of range!");
354 DefInit *DI = dynamic_cast<DefInit*>(Values[i]);
355 if (DI == 0) throw "Expected record in list!";
359 Init *ListInit::resolveReferences(Record &R, const RecordVal *RV) {
360 std::vector<Init*> Resolved;
361 Resolved.reserve(getSize());
362 bool Changed = false;
364 for (unsigned i = 0, e = getSize(); i != e; ++i) {
366 Init *CurElt = getElement(i);
370 CurElt = CurElt->resolveReferences(R, RV);
371 Changed |= E != CurElt;
372 } while (E != CurElt);
373 Resolved.push_back(E);
377 return new ListInit(Resolved);
381 void ListInit::print(std::ostream &OS) const {
383 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
390 Init *BinOpInit::Fold() {
391 switch (getOpcode()) {
392 default: assert(0 && "Unknown binop");
394 DagInit *LHSs = dynamic_cast<DagInit*>(LHS);
395 DagInit *RHSs = dynamic_cast<DagInit*>(RHS);
397 DefInit *LOp = dynamic_cast<DefInit*>(LHSs->getOperator());
398 DefInit *ROp = dynamic_cast<DefInit*>(RHSs->getOperator());
399 if (LOp->getDef() != ROp->getDef()) {
401 LOp->getDef()->getName() == "outs" ||
402 LOp->getDef()->getName() != "ins" ||
403 LOp->getDef()->getName() != "defs";
405 ROp->getDef()->getName() == "outs" ||
406 ROp->getDef()->getName() != "ins" ||
407 ROp->getDef()->getName() != "defs";
408 if (!LIsOps || !RIsOps)
409 throw "Concated Dag operators do not match!";
411 std::vector<Init*> Args;
412 std::vector<std::string> ArgNames;
413 for (unsigned i = 0, e = LHSs->getNumArgs(); i != e; ++i) {
414 Args.push_back(LHSs->getArg(i));
415 ArgNames.push_back(LHSs->getArgName(i));
417 for (unsigned i = 0, e = RHSs->getNumArgs(); i != e; ++i) {
418 Args.push_back(RHSs->getArg(i));
419 ArgNames.push_back(RHSs->getArgName(i));
421 return new DagInit(LHSs->getOperator(), Args, ArgNames);
426 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
427 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
429 return new StringInit(LHSs->getValue() + RHSs->getValue());
435 IntInit *LHSi = dynamic_cast<IntInit*>(LHS);
436 IntInit *RHSi = dynamic_cast<IntInit*>(RHS);
438 int LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
440 switch (getOpcode()) {
441 default: assert(0 && "Bad opcode!");
442 case SHL: Result = LHSv << RHSv; break;
443 case SRA: Result = LHSv >> RHSv; break;
444 case SRL: Result = (unsigned)LHSv >> (unsigned)RHSv; break;
446 return new IntInit(Result);
454 Init *BinOpInit::resolveReferences(Record &R, const RecordVal *RV) {
455 Init *lhs = LHS->resolveReferences(R, RV);
456 Init *rhs = RHS->resolveReferences(R, RV);
458 if (LHS != lhs || RHS != rhs)
459 return (new BinOpInit(getOpcode(), lhs, rhs))->Fold();
463 void BinOpInit::print(std::ostream &OS) const {
465 case CONCAT: OS << "!con"; break;
466 case SHL: OS << "!shl"; break;
467 case SRA: OS << "!sra"; break;
468 case SRL: OS << "!srl"; break;
469 case STRCONCAT: OS << "!strconcat"; break;
478 Init *TypedInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
479 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
480 if (T == 0) return 0; // Cannot subscript a non-bits variable...
481 unsigned NumBits = T->getNumBits();
483 BitsInit *BI = new BitsInit(Bits.size());
484 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
485 if (Bits[i] >= NumBits) {
489 BI->setBit(i, new VarBitInit(this, Bits[i]));
494 Init *TypedInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
495 ListRecTy *T = dynamic_cast<ListRecTy*>(getType());
496 if (T == 0) return 0; // Cannot subscript a non-list variable...
498 if (Elements.size() == 1)
499 return new VarListElementInit(this, Elements[0]);
501 std::vector<Init*> ListInits;
502 ListInits.reserve(Elements.size());
503 for (unsigned i = 0, e = Elements.size(); i != e; ++i)
504 ListInits.push_back(new VarListElementInit(this, Elements[i]));
505 return new ListInit(ListInits);
509 Init *VarInit::resolveBitReference(Record &R, const RecordVal *IRV,
511 if (R.isTemplateArg(getName())) return 0;
512 if (IRV && IRV->getName() != getName()) return 0;
514 RecordVal *RV = R.getValue(getName());
515 assert(RV && "Reference to a non-existant variable?");
516 assert(dynamic_cast<BitsInit*>(RV->getValue()));
517 BitsInit *BI = (BitsInit*)RV->getValue();
519 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
520 Init *B = BI->getBit(Bit);
522 if (!dynamic_cast<UnsetInit*>(B)) // If the bit is not set...
523 return B; // Replace the VarBitInit with it.
527 Init *VarInit::resolveListElementReference(Record &R, const RecordVal *IRV,
529 if (R.isTemplateArg(getName())) return 0;
530 if (IRV && IRV->getName() != getName()) return 0;
532 RecordVal *RV = R.getValue(getName());
533 assert(RV && "Reference to a non-existant variable?");
534 ListInit *LI = dynamic_cast<ListInit*>(RV->getValue());
535 assert(LI && "Invalid list element!");
537 if (Elt >= LI->getSize())
538 return 0; // Out of range reference.
539 Init *E = LI->getElement(Elt);
540 if (!dynamic_cast<UnsetInit*>(E)) // If the element is set
541 return E; // Replace the VarListElementInit with it.
546 RecTy *VarInit::getFieldType(const std::string &FieldName) const {
547 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
548 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
549 return RV->getType();
553 Init *VarInit::getFieldInit(Record &R, const std::string &FieldName) const {
554 if (dynamic_cast<RecordRecTy*>(getType()))
555 if (const RecordVal *RV = R.getValue(VarName)) {
556 Init *TheInit = RV->getValue();
557 assert(TheInit != this && "Infinite loop detected!");
558 if (Init *I = TheInit->getFieldInit(R, FieldName))
566 /// resolveReferences - This method is used by classes that refer to other
567 /// variables which may not be defined at the time they expression is formed.
568 /// If a value is set for the variable later, this method will be called on
569 /// users of the value to allow the value to propagate out.
571 Init *VarInit::resolveReferences(Record &R, const RecordVal *RV) {
572 if (RecordVal *Val = R.getValue(VarName))
573 if (RV == Val || (RV == 0 && !dynamic_cast<UnsetInit*>(Val->getValue())))
574 return Val->getValue();
579 Init *VarBitInit::resolveReferences(Record &R, const RecordVal *RV) {
580 if (Init *I = getVariable()->resolveBitReference(R, RV, getBitNum()))
585 Init *VarListElementInit::resolveReferences(Record &R, const RecordVal *RV) {
586 if (Init *I = getVariable()->resolveListElementReference(R, RV,
592 Init *VarListElementInit::resolveBitReference(Record &R, const RecordVal *RV,
594 // FIXME: This should be implemented, to support references like:
599 Init *VarListElementInit::
600 resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) {
601 // FIXME: This should be implemented, to support references like:
606 RecTy *DefInit::getFieldType(const std::string &FieldName) const {
607 if (const RecordVal *RV = Def->getValue(FieldName))
608 return RV->getType();
612 Init *DefInit::getFieldInit(Record &R, const std::string &FieldName) const {
613 return Def->getValue(FieldName)->getValue();
617 void DefInit::print(std::ostream &OS) const {
618 OS << Def->getName();
621 Init *FieldInit::resolveBitReference(Record &R, const RecordVal *RV,
623 if (Init *BitsVal = Rec->getFieldInit(R, FieldName))
624 if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
625 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
626 Init *B = BI->getBit(Bit);
628 if (dynamic_cast<BitInit*>(B)) // If the bit is set...
629 return B; // Replace the VarBitInit with it.
634 Init *FieldInit::resolveListElementReference(Record &R, const RecordVal *RV,
636 if (Init *ListVal = Rec->getFieldInit(R, FieldName))
637 if (ListInit *LI = dynamic_cast<ListInit*>(ListVal)) {
638 if (Elt >= LI->getSize()) return 0;
639 Init *E = LI->getElement(Elt);
641 if (!dynamic_cast<UnsetInit*>(E)) // If the bit is set...
642 return E; // Replace the VarListElementInit with it.
647 Init *FieldInit::resolveReferences(Record &R, const RecordVal *RV) {
648 Init *NewRec = RV ? Rec->resolveReferences(R, RV) : Rec;
650 Init *BitsVal = NewRec->getFieldInit(R, FieldName);
652 Init *BVR = BitsVal->resolveReferences(R, RV);
653 return BVR->isComplete() ? BVR : this;
658 NewRec->dump(); cerr << "\n";
659 return new FieldInit(NewRec, FieldName);
664 Init *DagInit::resolveReferences(Record &R, const RecordVal *RV) {
665 std::vector<Init*> NewArgs;
666 for (unsigned i = 0, e = Args.size(); i != e; ++i)
667 NewArgs.push_back(Args[i]->resolveReferences(R, RV));
669 Init *Op = Val->resolveReferences(R, RV);
671 if (Args != NewArgs || Op != Val)
672 return new DagInit(Op, NewArgs, ArgNames);
678 void DagInit::print(std::ostream &OS) const {
681 OS << " " << *Args[0];
682 if (!ArgNames[0].empty()) OS << ":$" << ArgNames[0];
683 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
684 OS << ", " << *Args[i];
685 if (!ArgNames[i].empty()) OS << ":$" << ArgNames[i];
692 //===----------------------------------------------------------------------===//
693 // Other implementations
694 //===----------------------------------------------------------------------===//
696 RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
697 : Name(N), Ty(T), Prefix(P) {
698 Value = Ty->convertValue(new UnsetInit());
699 assert(Value && "Cannot create unset value for current type!");
702 void RecordVal::dump() const { cerr << *this; }
704 void RecordVal::print(std::ostream &OS, bool PrintSem) const {
705 if (getPrefix()) OS << "field ";
706 OS << *getType() << " " << getName();
709 OS << " = " << *getValue();
711 if (PrintSem) OS << ";\n";
714 void Record::setName(const std::string &Name) {
715 if (Records.getDef(getName()) == this) {
716 Records.removeDef(getName());
718 Records.addDef(this);
720 Records.removeClass(getName());
722 Records.addClass(this);
726 /// resolveReferencesTo - If anything in this record refers to RV, replace the
727 /// reference to RV with the RHS of RV. If RV is null, we resolve all possible
729 void Record::resolveReferencesTo(const RecordVal *RV) {
730 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
731 if (Init *V = Values[i].getValue())
732 Values[i].setValue(V->resolveReferences(*this, RV));
737 void Record::dump() const { cerr << *this; }
739 std::ostream &llvm::operator<<(std::ostream &OS, const Record &R) {
742 const std::vector<std::string> &TArgs = R.getTemplateArgs();
743 if (!TArgs.empty()) {
745 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
747 const RecordVal *RV = R.getValue(TArgs[i]);
748 assert(RV && "Template argument record not found??");
749 RV->print(OS, false);
755 const std::vector<Record*> &SC = R.getSuperClasses();
758 for (unsigned i = 0, e = SC.size(); i != e; ++i)
759 OS << " " << SC[i]->getName();
763 const std::vector<RecordVal> &Vals = R.getValues();
764 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
765 if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
767 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
768 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
774 /// getValueInit - Return the initializer for a value with the specified name,
775 /// or throw an exception if the field does not exist.
777 Init *Record::getValueInit(const std::string &FieldName) const {
778 const RecordVal *R = getValue(FieldName);
779 if (R == 0 || R->getValue() == 0)
780 throw "Record `" + getName() + "' does not have a field named `" +
782 return R->getValue();
786 /// getValueAsString - This method looks up the specified field and returns its
787 /// value as a string, throwing an exception if the field does not exist or if
788 /// the value is not a string.
790 std::string Record::getValueAsString(const std::string &FieldName) const {
791 const RecordVal *R = getValue(FieldName);
792 if (R == 0 || R->getValue() == 0)
793 throw "Record `" + getName() + "' does not have a field named `" +
796 if (const StringInit *SI = dynamic_cast<const StringInit*>(R->getValue()))
797 return SI->getValue();
798 throw "Record `" + getName() + "', field `" + FieldName +
799 "' does not have a string initializer!";
802 /// getValueAsBitsInit - This method looks up the specified field and returns
803 /// its value as a BitsInit, throwing an exception if the field does not exist
804 /// or if the value is not the right type.
806 BitsInit *Record::getValueAsBitsInit(const std::string &FieldName) const {
807 const RecordVal *R = getValue(FieldName);
808 if (R == 0 || R->getValue() == 0)
809 throw "Record `" + getName() + "' does not have a field named `" +
812 if (BitsInit *BI = dynamic_cast<BitsInit*>(R->getValue()))
814 throw "Record `" + getName() + "', field `" + FieldName +
815 "' does not have a BitsInit initializer!";
818 /// getValueAsListInit - This method looks up the specified field and returns
819 /// its value as a ListInit, throwing an exception if the field does not exist
820 /// or if the value is not the right type.
822 ListInit *Record::getValueAsListInit(const std::string &FieldName) const {
823 const RecordVal *R = getValue(FieldName);
824 if (R == 0 || R->getValue() == 0)
825 throw "Record `" + getName() + "' does not have a field named `" +
828 if (ListInit *LI = dynamic_cast<ListInit*>(R->getValue()))
830 throw "Record `" + getName() + "', field `" + FieldName +
831 "' does not have a list initializer!";
834 /// getValueAsListOfDefs - This method looks up the specified field and returns
835 /// its value as a vector of records, throwing an exception if the field does
836 /// not exist or if the value is not the right type.
839 Record::getValueAsListOfDefs(const std::string &FieldName) const {
840 ListInit *List = getValueAsListInit(FieldName);
841 std::vector<Record*> Defs;
842 for (unsigned i = 0; i < List->getSize(); i++) {
843 if (DefInit *DI = dynamic_cast<DefInit*>(List->getElement(i))) {
844 Defs.push_back(DI->getDef());
846 throw "Record `" + getName() + "', field `" + FieldName +
847 "' list is not entirely DefInit!";
853 /// getValueAsInt - This method looks up the specified field and returns its
854 /// value as an int, throwing an exception if the field does not exist or if
855 /// the value is not the right type.
857 int Record::getValueAsInt(const std::string &FieldName) const {
858 const RecordVal *R = getValue(FieldName);
859 if (R == 0 || R->getValue() == 0)
860 throw "Record `" + getName() + "' does not have a field named `" +
863 if (IntInit *II = dynamic_cast<IntInit*>(R->getValue()))
864 return II->getValue();
865 throw "Record `" + getName() + "', field `" + FieldName +
866 "' does not have an int initializer!";
869 /// getValueAsListOfInts - This method looks up the specified field and returns
870 /// its value as a vector of integers, throwing an exception if the field does
871 /// not exist or if the value is not the right type.
874 Record::getValueAsListOfInts(const std::string &FieldName) const {
875 ListInit *List = getValueAsListInit(FieldName);
876 std::vector<int> Ints;
877 for (unsigned i = 0; i < List->getSize(); i++) {
878 if (IntInit *II = dynamic_cast<IntInit*>(List->getElement(i))) {
879 Ints.push_back(II->getValue());
881 throw "Record `" + getName() + "', field `" + FieldName +
882 "' does not have a list of ints initializer!";
888 /// getValueAsDef - This method looks up the specified field and returns its
889 /// value as a Record, throwing an exception if the field does not exist or if
890 /// the value is not the right type.
892 Record *Record::getValueAsDef(const std::string &FieldName) const {
893 const RecordVal *R = getValue(FieldName);
894 if (R == 0 || R->getValue() == 0)
895 throw "Record `" + getName() + "' does not have a field named `" +
898 if (DefInit *DI = dynamic_cast<DefInit*>(R->getValue()))
900 throw "Record `" + getName() + "', field `" + FieldName +
901 "' does not have a def initializer!";
904 /// getValueAsBit - This method looks up the specified field and returns its
905 /// value as a bit, throwing an exception if the field does not exist or if
906 /// the value is not the right type.
908 bool Record::getValueAsBit(const std::string &FieldName) const {
909 const RecordVal *R = getValue(FieldName);
910 if (R == 0 || R->getValue() == 0)
911 throw "Record `" + getName() + "' does not have a field named `" +
914 if (BitInit *BI = dynamic_cast<BitInit*>(R->getValue()))
915 return BI->getValue();
916 throw "Record `" + getName() + "', field `" + FieldName +
917 "' does not have a bit initializer!";
920 /// getValueAsDag - This method looks up the specified field and returns its
921 /// value as an Dag, throwing an exception if the field does not exist or if
922 /// the value is not the right type.
924 DagInit *Record::getValueAsDag(const std::string &FieldName) const {
925 const RecordVal *R = getValue(FieldName);
926 if (R == 0 || R->getValue() == 0)
927 throw "Record `" + getName() + "' does not have a field named `" +
930 if (DagInit *DI = dynamic_cast<DagInit*>(R->getValue()))
932 throw "Record `" + getName() + "', field `" + FieldName +
933 "' does not have a dag initializer!";
936 std::string Record::getValueAsCode(const std::string &FieldName) const {
937 const RecordVal *R = getValue(FieldName);
938 if (R == 0 || R->getValue() == 0)
939 throw "Record `" + getName() + "' does not have a field named `" +
942 if (const CodeInit *CI = dynamic_cast<const CodeInit*>(R->getValue()))
943 return CI->getValue();
944 throw "Record `" + getName() + "', field `" + FieldName +
945 "' does not have a code initializer!";
949 void RecordKeeper::dump() const { cerr << *this; }
951 std::ostream &llvm::operator<<(std::ostream &OS, const RecordKeeper &RK) {
952 OS << "------------- Classes -----------------\n";
953 const std::map<std::string, Record*> &Classes = RK.getClasses();
954 for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
955 E = Classes.end(); I != E; ++I)
956 OS << "class " << *I->second;
958 OS << "------------- Defs -----------------\n";
959 const std::map<std::string, Record*> &Defs = RK.getDefs();
960 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
961 E = Defs.end(); I != E; ++I)
962 OS << "def " << *I->second;
967 /// getAllDerivedDefinitions - This method returns all concrete definitions
968 /// that derive from the specified class name. If a class with the specified
969 /// name does not exist, an error is printed and true is returned.
971 RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
972 Record *Class = Records.getClass(ClassName);
974 throw "ERROR: Couldn't find the `" + ClassName + "' class!\n";
976 std::vector<Record*> Defs;
977 for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(),
978 E = getDefs().end(); I != E; ++I)
979 if (I->second->isSubClassOf(Class))
980 Defs.push_back(I->second);