1 //===- Record.cpp - Record implementation ---------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/DataTypes.h"
17 //===----------------------------------------------------------------------===//
18 // Type implementations
19 //===----------------------------------------------------------------------===//
21 void RecTy::dump() const { print(std::cerr); }
23 Init *BitRecTy::convertValue(BitsInit *BI) {
24 if (BI->getNumBits() != 1) return 0; // Only accept if just one bit!
28 bool BitRecTy::baseClassOf(const BitsRecTy *RHS) const {
29 return RHS->getNumBits() == 1;
32 Init *BitRecTy::convertValue(IntInit *II) {
33 int Val = II->getValue();
34 if (Val != 0 && Val != 1) return 0; // Only accept 0 or 1 for a bit!
36 return new BitInit(Val != 0);
39 Init *BitRecTy::convertValue(TypedInit *VI) {
40 if (dynamic_cast<BitRecTy*>(VI->getType()))
41 return VI; // Accept variable if it is already of bit type!
45 Init *BitsRecTy::convertValue(UnsetInit *UI) {
46 BitsInit *Ret = new BitsInit(Size);
48 for (unsigned i = 0; i != Size; ++i)
49 Ret->setBit(i, new UnsetInit());
53 Init *BitsRecTy::convertValue(BitInit *UI) {
54 if (Size != 1) return 0; // Can only convert single bit...
55 BitsInit *Ret = new BitsInit(1);
60 // convertValue from Int initializer to bits type: Split the integer up into the
61 // appropriate bits...
63 Init *BitsRecTy::convertValue(IntInit *II) {
64 int64_t Value = II->getValue();
65 // Make sure this bitfield is large enough to hold the integer value...
67 if (Value & ~((1LL << Size)-1))
70 if ((Value >> Size) != -1 || ((Value & (1 << (Size-1))) == 0))
74 BitsInit *Ret = new BitsInit(Size);
75 for (unsigned i = 0; i != Size; ++i)
76 Ret->setBit(i, new BitInit(Value & (1 << i)));
81 Init *BitsRecTy::convertValue(BitsInit *BI) {
82 // If the number of bits is right, return it. Otherwise we need to expand or
84 if (BI->getNumBits() == Size) return BI;
88 Init *BitsRecTy::convertValue(TypedInit *VI) {
89 if (BitsRecTy *BRT = dynamic_cast<BitsRecTy*>(VI->getType()))
90 if (BRT->Size == Size) {
91 BitsInit *Ret = new BitsInit(Size);
92 for (unsigned i = 0; i != Size; ++i)
93 Ret->setBit(i, new VarBitInit(VI, i));
96 if (Size == 1 && dynamic_cast<BitRecTy*>(VI->getType())) {
97 BitsInit *Ret = new BitsInit(1);
105 Init *IntRecTy::convertValue(BitInit *BI) {
106 return new IntInit(BI->getValue());
109 Init *IntRecTy::convertValue(BitsInit *BI) {
111 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
112 if (BitInit *Bit = dynamic_cast<BitInit*>(BI->getBit(i))) {
113 Result |= Bit->getValue() << i;
117 return new IntInit(Result);
120 Init *IntRecTy::convertValue(TypedInit *TI) {
121 if (TI->getType()->typeIsConvertibleTo(this))
122 return TI; // Accept variable if already of the right type!
126 Init *StringRecTy::convertValue(TypedInit *TI) {
127 if (dynamic_cast<StringRecTy*>(TI->getType()))
128 return TI; // Accept variable if already of the right type!
132 void ListRecTy::print(std::ostream &OS) const {
133 OS << "list<" << *Ty << ">";
136 Init *ListRecTy::convertValue(ListInit *LI) {
137 std::vector<Init*> Elements;
139 // Verify that all of the elements of the list are subclasses of the
140 // appropriate class!
141 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
142 if (Init *CI = LI->getElement(i)->convertInitializerTo(Ty))
143 Elements.push_back(CI);
147 return new ListInit(Elements);
150 Init *ListRecTy::convertValue(TypedInit *TI) {
151 // Ensure that TI is compatible with our class.
152 if (ListRecTy *LRT = dynamic_cast<ListRecTy*>(TI->getType()))
153 if (LRT->getElementType()->typeIsConvertibleTo(getElementType()))
158 Init *CodeRecTy::convertValue(TypedInit *TI) {
159 if (TI->getType()->typeIsConvertibleTo(this))
164 Init *DagRecTy::convertValue(TypedInit *TI) {
165 if (TI->getType()->typeIsConvertibleTo(this))
171 void RecordRecTy::print(std::ostream &OS) const {
172 OS << Rec->getName();
175 Init *RecordRecTy::convertValue(DefInit *DI) {
176 // Ensure that DI is a subclass of Rec.
177 if (!DI->getDef()->isSubClassOf(Rec))
182 Init *RecordRecTy::convertValue(TypedInit *TI) {
183 // Ensure that TI is compatible with Rec.
184 if (RecordRecTy *RRT = dynamic_cast<RecordRecTy*>(TI->getType()))
185 if (RRT->getRecord()->isSubClassOf(getRecord()) ||
186 RRT->getRecord() == getRecord())
191 bool RecordRecTy::baseClassOf(const RecordRecTy *RHS) const {
192 return Rec == RHS->getRecord() || RHS->getRecord()->isSubClassOf(Rec);
196 //===----------------------------------------------------------------------===//
197 // Initializer implementations
198 //===----------------------------------------------------------------------===//
200 void Init::dump() const { return print(std::cerr); }
202 Init *BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
203 BitsInit *BI = new BitsInit(Bits.size());
204 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
205 if (Bits[i] >= getNumBits()) {
209 BI->setBit(i, getBit(Bits[i]));
214 void BitsInit::print(std::ostream &OS) const {
215 //if (!printInHex(OS)) return;
216 //if (!printAsVariable(OS)) return;
217 //if (!printAsUnset(OS)) return;
220 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
222 if (Init *Bit = getBit(e-i-1))
230 bool BitsInit::printInHex(std::ostream &OS) const {
231 // First, attempt to convert the value into an integer value...
233 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
234 if (BitInit *Bit = dynamic_cast<BitInit*>(getBit(i))) {
235 Result |= Bit->getValue() << i;
240 OS << "0x" << std::hex << Result << std::dec;
244 bool BitsInit::printAsVariable(std::ostream &OS) const {
245 // Get the variable that we may be set equal to...
246 assert(getNumBits() != 0);
247 VarBitInit *FirstBit = dynamic_cast<VarBitInit*>(getBit(0));
248 if (FirstBit == 0) return true;
249 TypedInit *Var = FirstBit->getVariable();
251 // Check to make sure the types are compatible.
252 BitsRecTy *Ty = dynamic_cast<BitsRecTy*>(FirstBit->getVariable()->getType());
253 if (Ty == 0) return true;
254 if (Ty->getNumBits() != getNumBits()) return true; // Incompatible types!
256 // Check to make sure all bits are referring to the right bits in the variable
257 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
258 VarBitInit *Bit = dynamic_cast<VarBitInit*>(getBit(i));
259 if (Bit == 0 || Bit->getVariable() != Var || Bit->getBitNum() != i)
267 bool BitsInit::printAsUnset(std::ostream &OS) const {
268 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
269 if (!dynamic_cast<UnsetInit*>(getBit(i)))
275 // resolveReferences - If there are any field references that refer to fields
276 // that have been filled in, we can propagate the values now.
278 Init *BitsInit::resolveReferences(Record &R) {
279 bool Changed = false;
280 BitsInit *New = new BitsInit(getNumBits());
282 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
284 Init *CurBit = getBit(i);
288 CurBit = CurBit->resolveReferences(R);
289 Changed |= B != CurBit;
290 } while (B != CurBit);
291 New->setBit(i, CurBit);
300 Init *IntInit::getBinaryOp(BinaryOp Op, Init *RHS) {
301 IntInit *RHSi = dynamic_cast<IntInit*>(RHS);
302 if (RHSi == 0) return 0;
306 case SHL: NewValue = Value << RHSi->getValue(); break;
307 case SRA: NewValue = Value >> RHSi->getValue(); break;
308 case SRL: NewValue = (unsigned)Value >> (unsigned)RHSi->getValue(); break;
310 return new IntInit(NewValue);
314 Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
315 BitsInit *BI = new BitsInit(Bits.size());
317 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
322 BI->setBit(i, new BitInit(Value & (1 << Bits[i])));
327 Init *ListInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
328 std::vector<Init*> Vals;
329 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
330 if (Elements[i] >= getSize())
332 Vals.push_back(getElement(Elements[i]));
334 return new ListInit(Vals);
337 Init *ListInit::resolveReferences(Record &R) {
338 std::vector<Init*> Resolved;
339 Resolved.reserve(getSize());
340 bool Changed = false;
342 for (unsigned i = 0, e = getSize(); i != e; ++i) {
344 Init *CurElt = getElement(i);
348 CurElt = CurElt->resolveReferences(R);
349 Changed |= E != CurElt;
350 } while (E != CurElt);
351 Resolved.push_back(E);
355 return new ListInit(Resolved);
359 void ListInit::print(std::ostream &OS) const {
361 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
368 Init *TypedInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
369 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
370 if (T == 0) return 0; // Cannot subscript a non-bits variable...
371 unsigned NumBits = T->getNumBits();
373 BitsInit *BI = new BitsInit(Bits.size());
374 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
375 if (Bits[i] >= NumBits) {
379 BI->setBit(i, new VarBitInit(this, Bits[i]));
384 Init *TypedInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
385 ListRecTy *T = dynamic_cast<ListRecTy*>(getType());
386 if (T == 0) return 0; // Cannot subscript a non-list variable...
388 if (Elements.size() == 1)
389 return new VarListElementInit(this, Elements[0]);
391 std::vector<Init*> ListInits;
392 ListInits.reserve(Elements.size());
393 for (unsigned i = 0, e = Elements.size(); i != e; ++i)
394 ListInits.push_back(new VarListElementInit(this, Elements[i]));
395 return new ListInit(ListInits);
399 Init *VarInit::resolveBitReference(Record &R, unsigned Bit) {
400 if (R.isTemplateArg(getName()))
403 RecordVal *RV = R.getValue(getName());
404 assert(RV && "Reference to a non-existant variable?");
405 assert(dynamic_cast<BitsInit*>(RV->getValue()));
406 BitsInit *BI = (BitsInit*)RV->getValue();
408 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
409 Init *B = BI->getBit(Bit);
411 if (!dynamic_cast<UnsetInit*>(B)) // If the bit is not set...
412 return B; // Replace the VarBitInit with it.
416 Init *VarInit::resolveListElementReference(Record &R, unsigned Elt) {
417 if (R.isTemplateArg(getName()))
420 RecordVal *RV = R.getValue(getName());
421 assert(RV && "Reference to a non-existant variable?");
422 ListInit *LI = dynamic_cast<ListInit*>(RV->getValue());
423 assert(LI && "Invalid list element!");
425 if (Elt >= LI->getSize())
426 return 0; // Out of range reference.
427 Init *E = LI->getElement(Elt);
428 if (!dynamic_cast<UnsetInit*>(E)) // If the element is set
429 return E; // Replace the VarListElementInit with it.
434 RecTy *VarInit::getFieldType(const std::string &FieldName) const {
435 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
436 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
437 return RV->getType();
441 Init *VarInit::getFieldInit(Record &R, const std::string &FieldName) const {
442 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
443 if (const RecordVal *RV = R.getValue(VarName)) {
444 Init *TheInit = RV->getValue();
445 assert(TheInit != this && "Infinite loop detected!");
446 if (Init *I = TheInit->getFieldInit(R, FieldName))
454 /// resolveReferences - This method is used by classes that refer to other
455 /// variables which may not be defined at the time they expression is formed.
456 /// If a value is set for the variable later, this method will be called on
457 /// users of the value to allow the value to propagate out.
459 Init *VarInit::resolveReferences(Record &R) {
460 if (RecordVal *Val = R.getValue(VarName))
461 if (!dynamic_cast<UnsetInit*>(Val->getValue()))
462 return Val->getValue();
467 Init *VarBitInit::resolveReferences(Record &R) {
468 if (Init *I = getVariable()->resolveBitReference(R, getBitNum()))
473 Init *VarListElementInit::resolveReferences(Record &R) {
474 if (Init *I = getVariable()->resolveListElementReference(R, getElementNum()))
479 Init *VarListElementInit::resolveBitReference(Record &R, unsigned Bit) {
480 // FIXME: This should be implemented, to support references like:
485 Init *VarListElementInit::resolveListElementReference(Record &R, unsigned Elt) {
486 // FIXME: This should be implemented, to support references like:
491 RecTy *DefInit::getFieldType(const std::string &FieldName) const {
492 if (const RecordVal *RV = Def->getValue(FieldName))
493 return RV->getType();
497 Init *DefInit::getFieldInit(Record &R, const std::string &FieldName) const {
498 return Def->getValue(FieldName)->getValue();
502 void DefInit::print(std::ostream &OS) const {
503 OS << Def->getName();
506 Init *FieldInit::resolveBitReference(Record &R, unsigned Bit) {
507 if (Init *BitsVal = Rec->getFieldInit(R, FieldName))
508 if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
509 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
510 Init *B = BI->getBit(Bit);
512 if (dynamic_cast<BitInit*>(B)) // If the bit is set...
513 return B; // Replace the VarBitInit with it.
518 Init *FieldInit::resolveListElementReference(Record &R, unsigned Elt) {
519 if (Init *ListVal = Rec->getFieldInit(R, FieldName))
520 if (ListInit *LI = dynamic_cast<ListInit*>(ListVal)) {
521 if (Elt >= LI->getSize()) return 0;
522 Init *E = LI->getElement(Elt);
524 if (!dynamic_cast<UnsetInit*>(E)) // If the bit is set...
525 return E; // Replace the VarListElementInit with it.
530 Init *FieldInit::resolveReferences(Record &R) {
531 Init *BitsVal = Rec->getFieldInit(R, FieldName);
533 Init *BVR = BitsVal->resolveReferences(R);
534 return BVR->isComplete() ? BVR : this;
540 void DagInit::print(std::ostream &OS) const {
541 OS << "(" << NodeTypeDef->getName();
543 OS << " " << *Args[0];
544 if (!ArgNames[0].empty()) OS << ":$" << ArgNames[0];
545 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
546 OS << ", " << *Args[i];
547 if (!ArgNames[i].empty()) OS << ":$" << ArgNames[i];
554 //===----------------------------------------------------------------------===//
555 // Other implementations
556 //===----------------------------------------------------------------------===//
558 RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
559 : Name(N), Ty(T), Prefix(P) {
560 Value = Ty->convertValue(new UnsetInit());
561 assert(Value && "Cannot create unset value for current type!");
564 void RecordVal::dump() const { std::cerr << *this; }
566 void RecordVal::print(std::ostream &OS, bool PrintSem) const {
567 if (getPrefix()) OS << "field ";
568 OS << *getType() << " " << getName();
570 OS << " = " << *getValue();
572 if (PrintSem) OS << ";\n";
575 // resolveReferences - If there are any field references that refer to fields
576 // that have been filled in, we can propagate the values now.
578 void Record::resolveReferences() {
579 for (unsigned i = 0, e = Values.size(); i != e; ++i)
580 Values[i].setValue(Values[i].getValue()->resolveReferences(*this));
583 void Record::dump() const { std::cerr << *this; }
585 std::ostream &llvm::operator<<(std::ostream &OS, const Record &R) {
588 const std::vector<std::string> &TArgs = R.getTemplateArgs();
589 if (!TArgs.empty()) {
591 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
593 const RecordVal *RV = R.getValue(TArgs[i]);
594 assert(RV && "Template argument record not found??");
595 RV->print(OS, false);
601 const std::vector<Record*> &SC = R.getSuperClasses();
604 for (unsigned i = 0, e = SC.size(); i != e; ++i)
605 OS << " " << SC[i]->getName();
609 const std::vector<RecordVal> &Vals = R.getValues();
610 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
611 if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
613 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
614 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
620 /// getValueInit - Return the initializer for a value with the specified name,
621 /// or throw an exception if the field does not exist.
623 Init *Record::getValueInit(const std::string &FieldName) const {
624 const RecordVal *R = getValue(FieldName);
625 if (R == 0 || R->getValue() == 0)
626 throw "Record `" + getName() + "' does not have a field named `" +
628 return R->getValue();
632 /// getValueAsString - This method looks up the specified field and returns its
633 /// value as a string, throwing an exception if the field does not exist or if
634 /// the value is not a string.
636 std::string Record::getValueAsString(const std::string &FieldName) const {
637 const RecordVal *R = getValue(FieldName);
638 if (R == 0 || R->getValue() == 0)
639 throw "Record `" + getName() + "' does not have a field named `" +
642 if (const StringInit *SI = dynamic_cast<const StringInit*>(R->getValue()))
643 return SI->getValue();
644 throw "Record `" + getName() + "', field `" + FieldName +
645 "' does not have a string initializer!";
648 /// getValueAsBitsInit - This method looks up the specified field and returns
649 /// its value as a BitsInit, throwing an exception if the field does not exist
650 /// or if the value is not the right type.
652 BitsInit *Record::getValueAsBitsInit(const std::string &FieldName) const {
653 const RecordVal *R = getValue(FieldName);
654 if (R == 0 || R->getValue() == 0)
655 throw "Record `" + getName() + "' does not have a field named `" +
658 if (BitsInit *BI = dynamic_cast<BitsInit*>(R->getValue()))
660 throw "Record `" + getName() + "', field `" + FieldName +
661 "' does not have a BitsInit initializer!";
664 /// getValueAsListInit - This method looks up the specified field and returns
665 /// its value as a ListInit, throwing an exception if the field does not exist
666 /// or if the value is not the right type.
668 ListInit *Record::getValueAsListInit(const std::string &FieldName) const {
669 const RecordVal *R = getValue(FieldName);
670 if (R == 0 || R->getValue() == 0)
671 throw "Record `" + getName() + "' does not have a field named `" +
674 if (ListInit *LI = dynamic_cast<ListInit*>(R->getValue()))
676 throw "Record `" + getName() + "', field `" + FieldName +
677 "' does not have a list initializer!";
680 /// getValueAsInt - This method looks up the specified field and returns its
681 /// value as an int, throwing an exception if the field does not exist or if
682 /// the value is not the right type.
684 int Record::getValueAsInt(const std::string &FieldName) const {
685 const RecordVal *R = getValue(FieldName);
686 if (R == 0 || R->getValue() == 0)
687 throw "Record `" + getName() + "' does not have a field named `" +
690 if (IntInit *II = dynamic_cast<IntInit*>(R->getValue()))
691 return II->getValue();
692 throw "Record `" + getName() + "', field `" + FieldName +
693 "' does not have a list initializer!";
696 /// getValueAsDef - This method looks up the specified field and returns its
697 /// value as a Record, throwing an exception if the field does not exist or if
698 /// the value is not the right type.
700 Record *Record::getValueAsDef(const std::string &FieldName) const {
701 const RecordVal *R = getValue(FieldName);
702 if (R == 0 || R->getValue() == 0)
703 throw "Record `" + getName() + "' does not have a field named `" +
706 if (DefInit *DI = dynamic_cast<DefInit*>(R->getValue()))
708 throw "Record `" + getName() + "', field `" + FieldName +
709 "' does not have a list initializer!";
712 /// getValueAsBit - This method looks up the specified field and returns its
713 /// value as a bit, throwing an exception if the field does not exist or if
714 /// the value is not the right type.
716 bool Record::getValueAsBit(const std::string &FieldName) const {
717 const RecordVal *R = getValue(FieldName);
718 if (R == 0 || R->getValue() == 0)
719 throw "Record `" + getName() + "' does not have a field named `" +
722 if (BitInit *BI = dynamic_cast<BitInit*>(R->getValue()))
723 return BI->getValue();
724 throw "Record `" + getName() + "', field `" + FieldName +
725 "' does not have a bit initializer!";
728 /// getValueAsDag - This method looks up the specified field and returns its
729 /// value as an Dag, throwing an exception if the field does not exist or if
730 /// the value is not the right type.
732 DagInit *Record::getValueAsDag(const std::string &FieldName) const {
733 const RecordVal *R = getValue(FieldName);
734 if (R == 0 || R->getValue() == 0)
735 throw "Record `" + getName() + "' does not have a field named `" +
738 if (DagInit *DI = dynamic_cast<DagInit*>(R->getValue()))
740 throw "Record `" + getName() + "', field `" + FieldName +
741 "' does not have a dag initializer!";
745 void RecordKeeper::dump() const { std::cerr << *this; }
747 std::ostream &llvm::operator<<(std::ostream &OS, const RecordKeeper &RK) {
748 OS << "------------- Classes -----------------\n";
749 const std::map<std::string, Record*> &Classes = RK.getClasses();
750 for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
751 E = Classes.end(); I != E; ++I)
752 OS << "class " << *I->second;
754 OS << "------------- Defs -----------------\n";
755 const std::map<std::string, Record*> &Defs = RK.getDefs();
756 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
757 E = Defs.end(); I != E; ++I)
758 OS << "def " << *I->second;
763 /// getAllDerivedDefinitions - This method returns all concrete definitions
764 /// that derive from the specified class name. If a class with the specified
765 /// name does not exist, an error is printed and true is returned.
767 RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
768 Record *Class = Records.getClass(ClassName);
770 throw "ERROR: Couldn't find the `" + ClassName + "' class!\n";
772 std::vector<Record*> Defs;
773 for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(),
774 E = getDefs().end(); I != E; ++I)
775 if (I->second->isSubClassOf(Class))
776 Defs.push_back(I->second);