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 //===----------------------------------------------------------------------===//
16 //===----------------------------------------------------------------------===//
17 // Type implementations
18 //===----------------------------------------------------------------------===//
20 void RecTy::dump() const { print(std::cerr); }
22 Init *BitRecTy::convertValue(BitsInit *BI) {
23 if (BI->getNumBits() != 1) return 0; // Only accept if just one bit!
27 bool BitRecTy::baseClassOf(const BitsRecTy *RHS) const {
28 return RHS->getNumBits() == 1;
31 Init *BitRecTy::convertValue(IntInit *II) {
32 int Val = II->getValue();
33 if (Val != 0 && Val != 1) return 0; // Only accept 0 or 1 for a bit!
35 return new BitInit(Val != 0);
38 Init *BitRecTy::convertValue(TypedInit *VI) {
39 if (dynamic_cast<BitRecTy*>(VI->getType()))
40 return VI; // Accept variable if it is already of bit type!
44 Init *BitsRecTy::convertValue(UnsetInit *UI) {
45 BitsInit *Ret = new BitsInit(Size);
47 for (unsigned i = 0; i != Size; ++i)
48 Ret->setBit(i, new UnsetInit());
52 Init *BitsRecTy::convertValue(BitInit *UI) {
53 if (Size != 1) return 0; // Can only convert single bit...
54 BitsInit *Ret = new BitsInit(1);
59 // convertValue from Int initializer to bits type: Split the integer up into the
60 // appropriate bits...
62 Init *BitsRecTy::convertValue(IntInit *II) {
63 int64_t Value = II->getValue();
64 // Make sure this bitfield is large enough to hold the integer value...
66 if (Value & ~((1LL << Size)-1))
69 if ((Value >> Size) != -1 || ((Value & (1 << Size-1)) == 0))
73 BitsInit *Ret = new BitsInit(Size);
74 for (unsigned i = 0; i != Size; ++i)
75 Ret->setBit(i, new BitInit(Value & (1 << i)));
80 Init *BitsRecTy::convertValue(BitsInit *BI) {
81 // If the number of bits is right, return it. Otherwise we need to expand or
83 if (BI->getNumBits() == Size) return BI;
87 Init *BitsRecTy::convertValue(TypedInit *VI) {
88 if (BitsRecTy *BRT = dynamic_cast<BitsRecTy*>(VI->getType()))
89 if (BRT->Size == Size) {
90 BitsInit *Ret = new BitsInit(Size);
91 for (unsigned i = 0; i != Size; ++i)
92 Ret->setBit(i, new VarBitInit(VI, i));
95 if (Size == 1 && dynamic_cast<BitRecTy*>(VI->getType())) {
96 BitsInit *Ret = new BitsInit(1);
104 Init *IntRecTy::convertValue(BitInit *BI) {
105 return new IntInit(BI->getValue());
108 Init *IntRecTy::convertValue(BitsInit *BI) {
110 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
111 if (BitInit *Bit = dynamic_cast<BitInit*>(BI->getBit(i))) {
112 Result |= Bit->getValue() << i;
116 return new IntInit(Result);
119 Init *IntRecTy::convertValue(TypedInit *TI) {
120 if (TI->getType()->typeIsConvertibleTo(this))
121 return TI; // Accept variable if already of the right type!
125 Init *StringRecTy::convertValue(TypedInit *TI) {
126 if (dynamic_cast<StringRecTy*>(TI->getType()))
127 return TI; // Accept variable if already of the right type!
131 void ListRecTy::print(std::ostream &OS) const {
132 OS << "list<" << *Ty << ">";
135 Init *ListRecTy::convertValue(ListInit *LI) {
136 std::vector<Init*> Elements;
138 // Verify that all of the elements of the list are subclasses of the
139 // appropriate class!
140 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
141 if (Init *CI = LI->getElement(i)->convertInitializerTo(Ty))
142 Elements.push_back(CI);
146 return new ListInit(Elements);
149 Init *ListRecTy::convertValue(TypedInit *TI) {
150 // Ensure that TI is compatible with our class.
151 if (ListRecTy *LRT = dynamic_cast<ListRecTy*>(TI->getType()))
152 if (LRT->getElementType()->typeIsConvertibleTo(getElementType()))
157 Init *CodeRecTy::convertValue(TypedInit *TI) {
158 if (TI->getType()->typeIsConvertibleTo(this))
163 Init *DagRecTy::convertValue(TypedInit *TI) {
164 if (TI->getType()->typeIsConvertibleTo(this))
170 void RecordRecTy::print(std::ostream &OS) const {
171 OS << Rec->getName();
174 Init *RecordRecTy::convertValue(DefInit *DI) {
175 // Ensure that DI is a subclass of Rec.
176 if (!DI->getDef()->isSubClassOf(Rec))
181 Init *RecordRecTy::convertValue(TypedInit *TI) {
182 // Ensure that TI is compatible with Rec.
183 if (RecordRecTy *RRT = dynamic_cast<RecordRecTy*>(TI->getType()))
184 if (RRT->getRecord()->isSubClassOf(getRecord()) ||
185 RRT->getRecord() == getRecord())
190 bool RecordRecTy::baseClassOf(const RecordRecTy *RHS) const {
191 return Rec == RHS->getRecord() || RHS->getRecord()->isSubClassOf(Rec);
195 //===----------------------------------------------------------------------===//
196 // Initializer implementations
197 //===----------------------------------------------------------------------===//
199 void Init::dump() const { return print(std::cerr); }
201 Init *BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
202 BitsInit *BI = new BitsInit(Bits.size());
203 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
204 if (Bits[i] >= getNumBits()) {
208 BI->setBit(i, getBit(Bits[i]));
213 void BitsInit::print(std::ostream &OS) const {
214 //if (!printInHex(OS)) return;
215 //if (!printAsVariable(OS)) return;
216 //if (!printAsUnset(OS)) return;
219 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
221 if (Init *Bit = getBit(e-i-1))
229 bool BitsInit::printInHex(std::ostream &OS) const {
230 // First, attempt to convert the value into an integer value...
232 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
233 if (BitInit *Bit = dynamic_cast<BitInit*>(getBit(i))) {
234 Result |= Bit->getValue() << i;
239 OS << "0x" << std::hex << Result << std::dec;
243 bool BitsInit::printAsVariable(std::ostream &OS) const {
244 // Get the variable that we may be set equal to...
245 assert(getNumBits() != 0);
246 VarBitInit *FirstBit = dynamic_cast<VarBitInit*>(getBit(0));
247 if (FirstBit == 0) return true;
248 TypedInit *Var = FirstBit->getVariable();
250 // Check to make sure the types are compatible.
251 BitsRecTy *Ty = dynamic_cast<BitsRecTy*>(FirstBit->getVariable()->getType());
252 if (Ty == 0) return true;
253 if (Ty->getNumBits() != getNumBits()) return true; // Incompatible types!
255 // Check to make sure all bits are referring to the right bits in the variable
256 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
257 VarBitInit *Bit = dynamic_cast<VarBitInit*>(getBit(i));
258 if (Bit == 0 || Bit->getVariable() != Var || Bit->getBitNum() != i)
266 bool BitsInit::printAsUnset(std::ostream &OS) const {
267 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
268 if (!dynamic_cast<UnsetInit*>(getBit(i)))
274 // resolveReferences - If there are any field references that refer to fields
275 // that have been filled in, we can propagate the values now.
277 Init *BitsInit::resolveReferences(Record &R) {
278 bool Changed = false;
279 BitsInit *New = new BitsInit(getNumBits());
281 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
283 Init *CurBit = getBit(i);
287 CurBit = CurBit->resolveReferences(R);
288 Changed |= B != CurBit;
289 } while (B != CurBit);
290 New->setBit(i, CurBit);
299 Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
300 BitsInit *BI = new BitsInit(Bits.size());
302 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
307 BI->setBit(i, new BitInit(Value & (1 << Bits[i])));
312 Init *ListInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
313 std::vector<Init*> Vals;
314 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
315 if (Elements[i] >= getSize())
317 Vals.push_back(getElement(Elements[i]));
319 return new ListInit(Vals);
322 Init *ListInit::resolveReferences(Record &R) {
323 std::vector<Init*> Resolved;
324 Resolved.reserve(getSize());
325 bool Changed = false;
327 for (unsigned i = 0, e = getSize(); i != e; ++i) {
329 Init *CurElt = getElement(i);
333 CurElt = CurElt->resolveReferences(R);
334 Changed |= E != CurElt;
335 } while (E != CurElt);
336 Resolved.push_back(E);
340 return new ListInit(Resolved);
344 void ListInit::print(std::ostream &OS) const {
346 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
353 Init *TypedInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
354 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
355 if (T == 0) return 0; // Cannot subscript a non-bits variable...
356 unsigned NumBits = T->getNumBits();
358 BitsInit *BI = new BitsInit(Bits.size());
359 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
360 if (Bits[i] >= NumBits) {
364 BI->setBit(i, new VarBitInit(this, Bits[i]));
369 Init *TypedInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
370 ListRecTy *T = dynamic_cast<ListRecTy*>(getType());
371 if (T == 0) return 0; // Cannot subscript a non-list variable...
373 if (Elements.size() == 1)
374 return new VarListElementInit(this, Elements[0]);
376 std::vector<Init*> ListInits;
377 ListInits.reserve(Elements.size());
378 for (unsigned i = 0, e = Elements.size(); i != e; ++i)
379 ListInits.push_back(new VarListElementInit(this, Elements[i]));
380 return new ListInit(ListInits);
384 Init *VarInit::resolveBitReference(Record &R, unsigned Bit) {
385 if (R.isTemplateArg(getName()))
388 RecordVal *RV = R.getValue(getName());
389 assert(RV && "Reference to a non-existant variable?");
390 assert(dynamic_cast<BitsInit*>(RV->getValue()));
391 BitsInit *BI = (BitsInit*)RV->getValue();
393 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
394 Init *B = BI->getBit(Bit);
396 if (!dynamic_cast<UnsetInit*>(B)) // If the bit is not set...
397 return B; // Replace the VarBitInit with it.
401 Init *VarInit::resolveListElementReference(Record &R, unsigned Elt) {
402 if (R.isTemplateArg(getName()))
405 RecordVal *RV = R.getValue(getName());
406 assert(RV && "Reference to a non-existant variable?");
407 ListInit *LI = dynamic_cast<ListInit*>(RV->getValue());
408 assert(LI && "Invalid list element!");
410 if (Elt >= LI->getSize())
411 return 0; // Out of range reference.
412 Init *E = LI->getElement(Elt);
413 if (!dynamic_cast<UnsetInit*>(E)) // If the element is set
414 return E; // Replace the VarListElementInit with it.
419 RecTy *VarInit::getFieldType(const std::string &FieldName) const {
420 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
421 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
422 return RV->getType();
426 Init *VarInit::getFieldInit(Record &R, const std::string &FieldName) const {
427 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
428 if (const RecordVal *RV = R.getValue(VarName)) {
429 Init *TheInit = RV->getValue();
430 assert(TheInit != this && "Infinite loop detected!");
431 if (Init *I = TheInit->getFieldInit(R, FieldName))
439 /// resolveReferences - This method is used by classes that refer to other
440 /// variables which may not be defined at the time they expression is formed.
441 /// If a value is set for the variable later, this method will be called on
442 /// users of the value to allow the value to propagate out.
444 Init *VarInit::resolveReferences(Record &R) {
445 if (RecordVal *Val = R.getValue(VarName))
446 if (!dynamic_cast<UnsetInit*>(Val->getValue()))
447 return Val->getValue();
452 Init *VarBitInit::resolveReferences(Record &R) {
453 if (Init *I = getVariable()->resolveBitReference(R, getBitNum()))
458 Init *VarListElementInit::resolveReferences(Record &R) {
459 if (Init *I = getVariable()->resolveListElementReference(R, getElementNum()))
464 Init *VarListElementInit::resolveBitReference(Record &R, unsigned Bit) {
465 // FIXME: This should be implemented, to support references like:
470 Init *VarListElementInit::resolveListElementReference(Record &R, unsigned Elt) {
471 // FIXME: This should be implemented, to support references like:
476 RecTy *DefInit::getFieldType(const std::string &FieldName) const {
477 if (const RecordVal *RV = Def->getValue(FieldName))
478 return RV->getType();
482 Init *DefInit::getFieldInit(Record &R, const std::string &FieldName) const {
483 return Def->getValue(FieldName)->getValue();
487 void DefInit::print(std::ostream &OS) const {
488 OS << Def->getName();
491 Init *FieldInit::resolveBitReference(Record &R, unsigned Bit) {
492 if (Init *BitsVal = Rec->getFieldInit(R, FieldName))
493 if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
494 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
495 Init *B = BI->getBit(Bit);
497 if (dynamic_cast<BitInit*>(B)) // If the bit is set...
498 return B; // Replace the VarBitInit with it.
503 Init *FieldInit::resolveListElementReference(Record &R, unsigned Elt) {
504 if (Init *ListVal = Rec->getFieldInit(R, FieldName))
505 if (ListInit *LI = dynamic_cast<ListInit*>(ListVal)) {
506 if (Elt >= LI->getSize()) return 0;
507 Init *E = LI->getElement(Elt);
509 if (!dynamic_cast<UnsetInit*>(E)) // If the bit is set...
510 return E; // Replace the VarListElementInit with it.
515 Init *FieldInit::resolveReferences(Record &R) {
516 Init *BitsVal = Rec->getFieldInit(R, FieldName);
518 Init *BVR = BitsVal->resolveReferences(R);
519 return BVR->isComplete() ? BVR : this;
525 void DagInit::print(std::ostream &OS) const {
526 OS << "(" << NodeTypeDef->getName();
528 OS << " " << *Args[0];
529 if (!ArgNames[0].empty()) OS << ":$" << ArgNames[0];
530 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
531 OS << ", " << *Args[i];
532 if (!ArgNames[i].empty()) OS << ":$" << ArgNames[i];
539 //===----------------------------------------------------------------------===//
540 // Other implementations
541 //===----------------------------------------------------------------------===//
543 RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
544 : Name(N), Ty(T), Prefix(P) {
545 Value = Ty->convertValue(new UnsetInit());
546 assert(Value && "Cannot create unset value for current type!");
549 void RecordVal::dump() const { std::cerr << *this; }
551 void RecordVal::print(std::ostream &OS, bool PrintSem) const {
552 if (getPrefix()) OS << "field ";
553 OS << *getType() << " " << getName();
555 OS << " = " << *getValue();
557 if (PrintSem) OS << ";\n";
560 // resolveReferences - If there are any field references that refer to fields
561 // that have been filled in, we can propagate the values now.
563 void Record::resolveReferences() {
564 for (unsigned i = 0, e = Values.size(); i != e; ++i)
565 Values[i].setValue(Values[i].getValue()->resolveReferences(*this));
568 void Record::dump() const { std::cerr << *this; }
570 std::ostream &llvm::operator<<(std::ostream &OS, const Record &R) {
573 const std::vector<std::string> &TArgs = R.getTemplateArgs();
574 if (!TArgs.empty()) {
576 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
578 const RecordVal *RV = R.getValue(TArgs[i]);
579 assert(RV && "Template argument record not found??");
580 RV->print(OS, false);
586 const std::vector<Record*> &SC = R.getSuperClasses();
589 for (unsigned i = 0, e = SC.size(); i != e; ++i)
590 OS << " " << SC[i]->getName();
594 const std::vector<RecordVal> &Vals = R.getValues();
595 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
596 if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
598 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
599 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
605 /// getValueInit - Return the initializer for a value with the specified name,
606 /// or throw an exception if the field does not exist.
608 Init *Record::getValueInit(const std::string &FieldName) const {
609 const RecordVal *R = getValue(FieldName);
610 if (R == 0 || R->getValue() == 0)
611 throw "Record '" + getName() + "' does not have a field named '" +
613 return R->getValue();
617 /// getValueAsString - This method looks up the specified field and returns its
618 /// value as a string, throwing an exception if the field does not exist or if
619 /// the value is not a string.
621 std::string Record::getValueAsString(const std::string &FieldName) const {
622 const RecordVal *R = getValue(FieldName);
623 if (R == 0 || R->getValue() == 0)
624 throw "Record '" + getName() + "' does not have a field named '" +
627 if (const StringInit *SI = dynamic_cast<const StringInit*>(R->getValue()))
628 return SI->getValue();
629 throw "Record '" + getName() + "', field '" + FieldName +
630 "' does not have a string initializer!";
633 /// getValueAsBitsInit - This method looks up the specified field and returns
634 /// its value as a BitsInit, throwing an exception if the field does not exist
635 /// or if the value is not the right type.
637 BitsInit *Record::getValueAsBitsInit(const std::string &FieldName) const {
638 const RecordVal *R = getValue(FieldName);
639 if (R == 0 || R->getValue() == 0)
640 throw "Record '" + getName() + "' does not have a field named '" +
643 if (BitsInit *BI = dynamic_cast<BitsInit*>(R->getValue()))
645 throw "Record '" + getName() + "', field '" + FieldName +
646 "' does not have a BitsInit initializer!";
649 /// getValueAsListInit - This method looks up the specified field and returns
650 /// its value as a ListInit, throwing an exception if the field does not exist
651 /// or if the value is not the right type.
653 ListInit *Record::getValueAsListInit(const std::string &FieldName) const {
654 const RecordVal *R = getValue(FieldName);
655 if (R == 0 || R->getValue() == 0)
656 throw "Record '" + getName() + "' does not have a field named '" +
659 if (ListInit *LI = dynamic_cast<ListInit*>(R->getValue()))
661 throw "Record '" + getName() + "', field '" + FieldName +
662 "' does not have a list initializer!";
665 /// getValueAsInt - This method looks up the specified field and returns its
666 /// value as an int, throwing an exception if the field does not exist or if
667 /// the value is not the right type.
669 int Record::getValueAsInt(const std::string &FieldName) const {
670 const RecordVal *R = getValue(FieldName);
671 if (R == 0 || R->getValue() == 0)
672 throw "Record '" + getName() + "' does not have a field named '" +
675 if (IntInit *II = dynamic_cast<IntInit*>(R->getValue()))
676 return II->getValue();
677 throw "Record '" + getName() + "', field '" + FieldName +
678 "' does not have a list initializer!";
681 /// getValueAsDef - This method looks up the specified field and returns its
682 /// value as a Record, throwing an exception if the field does not exist or if
683 /// the value is not the right type.
685 Record *Record::getValueAsDef(const std::string &FieldName) const {
686 const RecordVal *R = getValue(FieldName);
687 if (R == 0 || R->getValue() == 0)
688 throw "Record '" + getName() + "' does not have a field named '" +
691 if (DefInit *DI = dynamic_cast<DefInit*>(R->getValue()))
693 throw "Record '" + getName() + "', field '" + FieldName +
694 "' does not have a list initializer!";
697 /// getValueAsBit - This method looks up the specified field and returns its
698 /// value as a bit, throwing an exception if the field does not exist or if
699 /// the value is not the right type.
701 bool Record::getValueAsBit(const std::string &FieldName) const {
702 const RecordVal *R = getValue(FieldName);
703 if (R == 0 || R->getValue() == 0)
704 throw "Record '" + getName() + "' does not have a field named '" +
707 if (BitInit *BI = dynamic_cast<BitInit*>(R->getValue()))
708 return BI->getValue();
709 throw "Record '" + getName() + "', field '" + FieldName +
710 "' does not have a bit initializer!";
713 /// getValueAsDag - This method looks up the specified field and returns its
714 /// value as an Dag, throwing an exception if the field does not exist or if
715 /// the value is not the right type.
717 DagInit *Record::getValueAsDag(const std::string &FieldName) const {
718 const RecordVal *R = getValue(FieldName);
719 if (R == 0 || R->getValue() == 0)
720 throw "Record '" + getName() + "' does not have a field named '" +
723 if (DagInit *DI = dynamic_cast<DagInit*>(R->getValue()))
725 throw "Record '" + getName() + "', field '" + FieldName +
726 "' does not have a dag initializer!";
730 void RecordKeeper::dump() const { std::cerr << *this; }
732 std::ostream &llvm::operator<<(std::ostream &OS, const RecordKeeper &RK) {
733 OS << "------------- Classes -----------------\n";
734 const std::map<std::string, Record*> &Classes = RK.getClasses();
735 for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
736 E = Classes.end(); I != E; ++I)
737 OS << "class " << *I->second;
739 OS << "------------- Defs -----------------\n";
740 const std::map<std::string, Record*> &Defs = RK.getDefs();
741 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
742 E = Defs.end(); I != E; ++I)
743 OS << "def " << *I->second;
748 /// getAllDerivedDefinitions - This method returns all concrete definitions
749 /// that derive from the specified class name. If a class with the specified
750 /// name does not exist, an error is printed and true is returned.
752 RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
753 Record *Class = Records.getClass(ClassName);
755 throw "ERROR: Couldn't find the '" + ClassName + "' class!\n";
757 std::vector<Record*> Defs;
758 for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(),
759 E = getDefs().end(); I != E; ++I)
760 if (I->second->isSubClassOf(Class))
761 Defs.push_back(I->second);