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 //===----------------------------------------------------------------------===//
15 //===----------------------------------------------------------------------===//
16 // Type implementations
17 //===----------------------------------------------------------------------===//
19 void RecTy::dump() const { print(std::cerr); }
21 Init *BitRecTy::convertValue(BitsInit *BI) {
22 if (BI->getNumBits() != 1) return 0; // Only accept if just one bit!
26 bool BitRecTy::baseClassOf(const BitsRecTy *RHS) const {
27 return RHS->getNumBits() == 1;
30 Init *BitRecTy::convertValue(IntInit *II) {
31 int Val = II->getValue();
32 if (Val != 0 && Val != 1) return 0; // Only accept 0 or 1 for a bit!
34 return new BitInit(Val != 0);
37 Init *BitRecTy::convertValue(TypedInit *VI) {
38 if (dynamic_cast<BitRecTy*>(VI->getType()))
39 return VI; // Accept variable if it is already of bit type!
43 Init *BitsRecTy::convertValue(UnsetInit *UI) {
44 BitsInit *Ret = new BitsInit(Size);
46 for (unsigned i = 0; i != Size; ++i)
47 Ret->setBit(i, new UnsetInit());
51 Init *BitsRecTy::convertValue(BitInit *UI) {
52 if (Size != 1) return 0; // Can only convert single bit...
53 BitsInit *Ret = new BitsInit(1);
58 // convertValue from Int initializer to bits type: Split the integer up into the
59 // appropriate bits...
61 Init *BitsRecTy::convertValue(IntInit *II) {
62 int Value = II->getValue();
63 // Make sure this bitfield is large enough to hold the integer value...
65 if (Value & ~((1 << Size)-1))
68 if ((Value >> Size) != -1 || ((Value & (1 << Size-1)) == 0))
72 BitsInit *Ret = new BitsInit(Size);
73 for (unsigned i = 0; i != Size; ++i)
74 Ret->setBit(i, new BitInit(Value & (1 << i)));
79 Init *BitsRecTy::convertValue(BitsInit *BI) {
80 // If the number of bits is right, return it. Otherwise we need to expand or
82 if (BI->getNumBits() == Size) return BI;
86 Init *BitsRecTy::convertValue(TypedInit *VI) {
87 if (BitsRecTy *BRT = dynamic_cast<BitsRecTy*>(VI->getType()))
88 if (BRT->Size == Size) {
89 BitsInit *Ret = new BitsInit(Size);
90 for (unsigned i = 0; i != Size; ++i)
91 Ret->setBit(i, new VarBitInit(VI, i));
94 if (Size == 1 && dynamic_cast<BitRecTy*>(VI->getType())) {
95 BitsInit *Ret = new BitsInit(1);
103 Init *IntRecTy::convertValue(BitInit *BI) {
104 return new IntInit(BI->getValue());
107 Init *IntRecTy::convertValue(BitsInit *BI) {
109 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
110 if (BitInit *Bit = dynamic_cast<BitInit*>(BI->getBit(i))) {
111 Result |= Bit->getValue() << i;
115 return new IntInit(Result);
118 Init *IntRecTy::convertValue(TypedInit *TI) {
119 if (TI->getType()->typeIsConvertibleTo(this))
120 return TI; // Accept variable if already of the right type!
124 Init *StringRecTy::convertValue(TypedInit *TI) {
125 if (dynamic_cast<StringRecTy*>(TI->getType()))
126 return TI; // Accept variable if already of the right type!
130 void ListRecTy::print(std::ostream &OS) const {
131 OS << "list<" << *Ty << ">";
134 Init *ListRecTy::convertValue(ListInit *LI) {
135 std::vector<Init*> Elements;
137 // Verify that all of the elements of the list are subclasses of the
138 // appropriate class!
139 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
140 if (Init *CI = LI->getElement(i)->convertInitializerTo(Ty))
141 Elements.push_back(CI);
145 return new ListInit(Elements);
148 Init *ListRecTy::convertValue(TypedInit *TI) {
149 // Ensure that TI is compatible with our class.
150 if (ListRecTy *LRT = dynamic_cast<ListRecTy*>(TI->getType()))
151 if (LRT->getElementType()->typeIsConvertibleTo(getElementType()))
156 Init *CodeRecTy::convertValue(TypedInit *TI) {
157 if (TI->getType()->typeIsConvertibleTo(this))
162 Init *DagRecTy::convertValue(TypedInit *TI) {
163 if (TI->getType()->typeIsConvertibleTo(this))
169 void RecordRecTy::print(std::ostream &OS) const {
170 OS << Rec->getName();
173 Init *RecordRecTy::convertValue(DefInit *DI) {
174 // Ensure that DI is a subclass of Rec.
175 if (!DI->getDef()->isSubClassOf(Rec))
180 Init *RecordRecTy::convertValue(TypedInit *TI) {
181 // Ensure that TI is compatible with Rec.
182 if (RecordRecTy *RRT = dynamic_cast<RecordRecTy*>(TI->getType()))
183 if (RRT->getRecord()->isSubClassOf(getRecord()) ||
184 RRT->getRecord() == getRecord())
189 bool RecordRecTy::baseClassOf(const RecordRecTy *RHS) const {
190 return Rec == RHS->getRecord() || RHS->getRecord()->isSubClassOf(Rec);
194 //===----------------------------------------------------------------------===//
195 // Initializer implementations
196 //===----------------------------------------------------------------------===//
198 void Init::dump() const { return print(std::cerr); }
200 Init *BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
201 BitsInit *BI = new BitsInit(Bits.size());
202 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
203 if (Bits[i] >= getNumBits()) {
207 BI->setBit(i, getBit(Bits[i]));
212 void BitsInit::print(std::ostream &OS) const {
213 //if (!printInHex(OS)) return;
214 //if (!printAsVariable(OS)) return;
215 //if (!printAsUnset(OS)) return;
218 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
220 if (Init *Bit = getBit(e-i-1))
228 bool BitsInit::printInHex(std::ostream &OS) const {
229 // First, attempt to convert the value into an integer value...
231 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
232 if (BitInit *Bit = dynamic_cast<BitInit*>(getBit(i))) {
233 Result |= Bit->getValue() << i;
238 OS << "0x" << std::hex << Result << std::dec;
242 bool BitsInit::printAsVariable(std::ostream &OS) const {
243 // Get the variable that we may be set equal to...
244 assert(getNumBits() != 0);
245 VarBitInit *FirstBit = dynamic_cast<VarBitInit*>(getBit(0));
246 if (FirstBit == 0) return true;
247 TypedInit *Var = FirstBit->getVariable();
249 // Check to make sure the types are compatible.
250 BitsRecTy *Ty = dynamic_cast<BitsRecTy*>(FirstBit->getVariable()->getType());
251 if (Ty == 0) return true;
252 if (Ty->getNumBits() != getNumBits()) return true; // Incompatible types!
254 // Check to make sure all bits are referring to the right bits in the variable
255 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
256 VarBitInit *Bit = dynamic_cast<VarBitInit*>(getBit(i));
257 if (Bit == 0 || Bit->getVariable() != Var || Bit->getBitNum() != i)
265 bool BitsInit::printAsUnset(std::ostream &OS) const {
266 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
267 if (!dynamic_cast<UnsetInit*>(getBit(i)))
273 // resolveReferences - If there are any field references that refer to fields
274 // that have been filled in, we can propagate the values now.
276 Init *BitsInit::resolveReferences(Record &R) {
277 bool Changed = false;
278 BitsInit *New = new BitsInit(getNumBits());
280 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
282 Init *CurBit = getBit(i);
286 CurBit = CurBit->resolveReferences(R);
287 Changed |= B != CurBit;
288 } while (B != CurBit);
289 New->setBit(i, CurBit);
298 Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
299 BitsInit *BI = new BitsInit(Bits.size());
301 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
306 BI->setBit(i, new BitInit(Value & (1 << Bits[i])));
311 void ListInit::print(std::ostream &OS) const {
313 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
320 Init *VarInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
321 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
322 if (T == 0) return 0; // Cannot subscript a non-bits variable...
323 unsigned NumBits = T->getNumBits();
325 BitsInit *BI = new BitsInit(Bits.size());
326 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
327 if (Bits[i] >= NumBits) {
331 BI->setBit(i, new VarBitInit(this, Bits[i]));
336 Init *VarInit::resolveBitReference(Record &R, unsigned Bit) {
337 if (R.isTemplateArg(getName()))
340 RecordVal *RV = R.getValue(getName());
341 assert(RV && "Reference to a non-existant variable?");
342 assert(dynamic_cast<BitsInit*>(RV->getValue()));
343 BitsInit *BI = (BitsInit*)RV->getValue();
345 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
346 Init *B = BI->getBit(Bit);
348 if (!dynamic_cast<UnsetInit*>(B)) // If the bit is not set...
349 return B; // Replace the VarBitInit with it.
353 RecTy *VarInit::getFieldType(const std::string &FieldName) const {
354 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
355 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
356 return RV->getType();
360 Init *VarInit::getFieldInit(Record &R, const std::string &FieldName) const {
361 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
362 if (const RecordVal *RV = R.getValue(VarName))
363 if (Init *I = RV->getValue()->getFieldInit(R, FieldName))
370 /// resolveReferences - This method is used by classes that refer to other
371 /// variables which may not be defined at the time they expression is formed.
372 /// If a value is set for the variable later, this method will be called on
373 /// users of the value to allow the value to propagate out.
375 Init *VarInit::resolveReferences(Record &R) {
376 if (RecordVal *Val = R.getValue(VarName))
377 if (!dynamic_cast<UnsetInit*>(Val->getValue()))
378 return Val->getValue();
383 Init *VarBitInit::resolveReferences(Record &R) {
384 Init *I = getVariable()->resolveBitReference(R, getBitNum());
385 if (I != getVariable())
390 RecTy *DefInit::getFieldType(const std::string &FieldName) const {
391 if (const RecordVal *RV = Def->getValue(FieldName))
392 return RV->getType();
396 Init *DefInit::getFieldInit(Record &R, const std::string &FieldName) const {
397 return Def->getValue(FieldName)->getValue();
401 void DefInit::print(std::ostream &OS) const {
402 OS << Def->getName();
405 Init *FieldInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
406 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
407 if (T == 0) return 0; // Cannot subscript a non-bits field...
408 unsigned NumBits = T->getNumBits();
410 BitsInit *BI = new BitsInit(Bits.size());
411 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
412 if (Bits[i] >= NumBits) {
416 BI->setBit(i, new VarBitInit(this, Bits[i]));
421 Init *FieldInit::resolveBitReference(Record &R, unsigned Bit) {
422 Init *BitsVal = Rec->getFieldInit(R, FieldName);
424 if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
425 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
426 Init *B = BI->getBit(Bit);
428 if (dynamic_cast<BitInit*>(B)) // If the bit is set...
429 return B; // Replace the VarBitInit with it.
434 Init *FieldInit::resolveReferences(Record &R) {
435 Init *BitsVal = Rec->getFieldInit(R, FieldName);
437 Init *BVR = BitsVal->resolveReferences(R);
438 return BVR->isComplete() ? BVR : this;
444 void DagInit::print(std::ostream &OS) const {
445 OS << "(" << NodeTypeDef->getName();
447 OS << " " << *Args[0];
448 if (!ArgNames[0].empty()) OS << ":$" << ArgNames[0];
449 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
450 OS << ", " << *Args[i];
451 if (!ArgNames[i].empty()) OS << ":$" << ArgNames[i];
458 //===----------------------------------------------------------------------===//
459 // Other implementations
460 //===----------------------------------------------------------------------===//
462 RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
463 : Name(N), Ty(T), Prefix(P) {
464 Value = Ty->convertValue(new UnsetInit());
465 assert(Value && "Cannot create unset value for current type!");
468 void RecordVal::dump() const { std::cerr << *this; }
470 void RecordVal::print(std::ostream &OS, bool PrintSem) const {
471 if (getPrefix()) OS << "field ";
472 OS << *getType() << " " << getName();
474 OS << " = " << *getValue();
476 if (PrintSem) OS << ";\n";
479 // resolveReferences - If there are any field references that refer to fields
480 // that have been filled in, we can propagate the values now.
482 void Record::resolveReferences() {
483 for (unsigned i = 0, e = Values.size(); i != e; ++i)
484 Values[i].setValue(Values[i].getValue()->resolveReferences(*this));
487 void Record::dump() const { std::cerr << *this; }
489 std::ostream &operator<<(std::ostream &OS, const Record &R) {
492 const std::vector<std::string> &TArgs = R.getTemplateArgs();
493 if (!TArgs.empty()) {
495 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
497 const RecordVal *RV = R.getValue(TArgs[i]);
498 assert(RV && "Template argument record not found??");
499 RV->print(OS, false);
505 const std::vector<Record*> &SC = R.getSuperClasses();
508 for (unsigned i = 0, e = SC.size(); i != e; ++i)
509 OS << " " << SC[i]->getName();
513 const std::vector<RecordVal> &Vals = R.getValues();
514 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
515 if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
517 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
518 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
524 /// getValueInit - Return the initializer for a value with the specified name,
525 /// or throw an exception if the field does not exist.
527 Init *Record::getValueInit(const std::string &FieldName) const {
528 const RecordVal *R = getValue(FieldName);
529 if (R == 0 || R->getValue() == 0)
530 throw "Record '" + getName() + "' does not have a field named '" +
532 return R->getValue();
536 /// getValueAsString - This method looks up the specified field and returns its
537 /// value as a string, throwing an exception if the field does not exist or if
538 /// the value is not a string.
540 std::string Record::getValueAsString(const std::string &FieldName) const {
541 const RecordVal *R = getValue(FieldName);
542 if (R == 0 || R->getValue() == 0)
543 throw "Record '" + getName() + "' does not have a field named '" +
546 if (const StringInit *SI = dynamic_cast<const StringInit*>(R->getValue()))
547 return SI->getValue();
548 throw "Record '" + getName() + "', field '" + FieldName +
549 "' does not have a string initializer!";
552 /// getValueAsBitsInit - This method looks up the specified field and returns
553 /// its value as a BitsInit, throwing an exception if the field does not exist
554 /// or if the value is not the right type.
556 BitsInit *Record::getValueAsBitsInit(const std::string &FieldName) const {
557 const RecordVal *R = getValue(FieldName);
558 if (R == 0 || R->getValue() == 0)
559 throw "Record '" + getName() + "' does not have a field named '" +
562 if (BitsInit *BI = dynamic_cast<BitsInit*>(R->getValue()))
564 throw "Record '" + getName() + "', field '" + FieldName +
565 "' does not have a BitsInit initializer!";
568 /// getValueAsListInit - This method looks up the specified field and returns
569 /// its value as a ListInit, throwing an exception if the field does not exist
570 /// or if the value is not the right type.
572 ListInit *Record::getValueAsListInit(const std::string &FieldName) const {
573 const RecordVal *R = getValue(FieldName);
574 if (R == 0 || R->getValue() == 0)
575 throw "Record '" + getName() + "' does not have a field named '" +
578 if (ListInit *LI = dynamic_cast<ListInit*>(R->getValue()))
580 throw "Record '" + getName() + "', field '" + FieldName +
581 "' does not have a list initializer!";
584 /// getValueAsInt - This method looks up the specified field and returns its
585 /// value as an int, throwing an exception if the field does not exist or if
586 /// the value is not the right type.
588 int Record::getValueAsInt(const std::string &FieldName) const {
589 const RecordVal *R = getValue(FieldName);
590 if (R == 0 || R->getValue() == 0)
591 throw "Record '" + getName() + "' does not have a field named '" +
594 if (IntInit *II = dynamic_cast<IntInit*>(R->getValue()))
595 return II->getValue();
596 throw "Record '" + getName() + "', field '" + FieldName +
597 "' does not have a list initializer!";
600 /// getValueAsDef - This method looks up the specified field and returns its
601 /// value as a Record, throwing an exception if the field does not exist or if
602 /// the value is not the right type.
604 Record *Record::getValueAsDef(const std::string &FieldName) const {
605 const RecordVal *R = getValue(FieldName);
606 if (R == 0 || R->getValue() == 0)
607 throw "Record '" + getName() + "' does not have a field named '" +
610 if (DefInit *DI = dynamic_cast<DefInit*>(R->getValue()))
612 throw "Record '" + getName() + "', field '" + FieldName +
613 "' does not have a list initializer!";
616 /// getValueAsBit - This method looks up the specified field and returns its
617 /// value as a bit, throwing an exception if the field does not exist or if
618 /// the value is not the right type.
620 bool Record::getValueAsBit(const std::string &FieldName) const {
621 const RecordVal *R = getValue(FieldName);
622 if (R == 0 || R->getValue() == 0)
623 throw "Record '" + getName() + "' does not have a field named '" +
626 if (BitInit *BI = dynamic_cast<BitInit*>(R->getValue()))
627 return BI->getValue();
628 throw "Record '" + getName() + "', field '" + FieldName +
629 "' does not have a bit initializer!";
632 /// getValueAsDag - This method looks up the specified field and returns its
633 /// value as an Dag, throwing an exception if the field does not exist or if
634 /// the value is not the right type.
636 DagInit *Record::getValueAsDag(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 (DagInit *DI = dynamic_cast<DagInit*>(R->getValue()))
644 throw "Record '" + getName() + "', field '" + FieldName +
645 "' does not have a dag initializer!";
649 void RecordKeeper::dump() const { std::cerr << *this; }
651 std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK) {
652 OS << "------------- Classes -----------------\n";
653 const std::map<std::string, Record*> &Classes = RK.getClasses();
654 for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
655 E = Classes.end(); I != E; ++I)
656 OS << "class " << *I->second;
658 OS << "------------- Defs -----------------\n";
659 const std::map<std::string, Record*> &Defs = RK.getDefs();
660 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
661 E = Defs.end(); I != E; ++I)
662 OS << "def " << *I->second;
667 /// getAllDerivedDefinitions - This method returns all concrete definitions
668 /// that derive from the specified class name. If a class with the specified
669 /// name does not exist, an error is printed and true is returned.
671 RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
672 Record *Class = Records.getClass(ClassName);
674 throw "ERROR: Couldn't find the '" + ClassName + "' class!\n";
676 std::vector<Record*> Defs;
677 for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(),
678 E = getDefs().end(); I != E; ++I)
679 if (I->second->isSubClassOf(Class))
680 Defs.push_back(I->second);