1 //===- Record.cpp - Record implementation ---------------------------------===//
4 //===----------------------------------------------------------------------===//
8 //===----------------------------------------------------------------------===//
9 // Type implementations
10 //===----------------------------------------------------------------------===//
12 void RecTy::dump() const { print(std::cerr); }
14 Init *BitRecTy::convertValue(BitsInit *BI) {
15 if (BI->getNumBits() != 1) return 0; // Only accept if just one bit!
19 bool BitRecTy::baseClassOf(const BitsRecTy *RHS) const {
20 return RHS->getNumBits() == 1;
23 Init *BitRecTy::convertValue(IntInit *II) {
24 int Val = II->getValue();
25 if (Val != 0 && Val != 1) return 0; // Only accept 0 or 1 for a bit!
27 return new BitInit(Val != 0);
30 Init *BitRecTy::convertValue(TypedInit *VI) {
31 if (dynamic_cast<BitRecTy*>(VI->getType()))
32 return VI; // Accept variable if it is already of bit type!
36 Init *BitsRecTy::convertValue(UnsetInit *UI) {
37 BitsInit *Ret = new BitsInit(Size);
39 for (unsigned i = 0; i != Size; ++i)
40 Ret->setBit(i, new UnsetInit());
44 Init *BitsRecTy::convertValue(BitInit *UI) {
45 if (Size != 1) return 0; // Can only convert single bit...
46 BitsInit *Ret = new BitsInit(1);
51 // convertValue from Int initializer to bits type: Split the integer up into the
52 // appropriate bits...
54 Init *BitsRecTy::convertValue(IntInit *II) {
55 int Value = II->getValue();
56 // Make sure this bitfield is large enough to hold the integer value...
58 if (Value & ~((1 << Size)-1))
61 if ((Value >> Size) != -1 || ((Value & (1 << Size-1)) == 0))
65 BitsInit *Ret = new BitsInit(Size);
66 for (unsigned i = 0; i != Size; ++i)
67 Ret->setBit(i, new BitInit(Value & (1 << i)));
72 Init *BitsRecTy::convertValue(BitsInit *BI) {
73 // If the number of bits is right, return it. Otherwise we need to expand or
75 if (BI->getNumBits() == Size) return BI;
79 Init *BitsRecTy::convertValue(TypedInit *VI) {
80 if (BitsRecTy *BRT = dynamic_cast<BitsRecTy*>(VI->getType()))
81 if (BRT->Size == Size) {
82 BitsInit *Ret = new BitsInit(Size);
83 for (unsigned i = 0; i != Size; ++i)
84 Ret->setBit(i, new VarBitInit(VI, i));
87 if (Size == 1 && dynamic_cast<BitRecTy*>(VI->getType())) {
88 BitsInit *Ret = new BitsInit(1);
96 Init *IntRecTy::convertValue(BitInit *BI) {
97 return new IntInit(BI->getValue());
100 Init *IntRecTy::convertValue(BitsInit *BI) {
102 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
103 if (BitInit *Bit = dynamic_cast<BitInit*>(BI->getBit(i))) {
104 Result |= Bit->getValue() << i;
108 return new IntInit(Result);
111 Init *IntRecTy::convertValue(TypedInit *TI) {
112 if (TI->getType()->typeIsConvertibleTo(this))
113 return TI; // Accept variable if already of the right type!
117 Init *StringRecTy::convertValue(TypedInit *TI) {
118 if (dynamic_cast<StringRecTy*>(TI->getType()))
119 return TI; // Accept variable if already of the right type!
123 void ListRecTy::print(std::ostream &OS) const {
124 OS << "list<" << *Ty << ">";
127 Init *ListRecTy::convertValue(ListInit *LI) {
128 std::vector<Init*> Elements;
130 // Verify that all of the elements of the list are subclasses of the
131 // appropriate class!
132 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
133 if (Init *CI = LI->getElement(i)->convertInitializerTo(Ty))
134 Elements.push_back(CI);
138 return new ListInit(Elements);
141 Init *ListRecTy::convertValue(TypedInit *TI) {
142 // Ensure that TI is compatible with our class.
143 if (ListRecTy *LRT = dynamic_cast<ListRecTy*>(TI->getType()))
144 if (LRT->getElementType()->typeIsConvertibleTo(getElementType()))
149 Init *CodeRecTy::convertValue(TypedInit *TI) {
150 if (TI->getType()->typeIsConvertibleTo(this))
155 Init *DagRecTy::convertValue(TypedInit *TI) {
156 if (TI->getType()->typeIsConvertibleTo(this))
162 void RecordRecTy::print(std::ostream &OS) const {
163 OS << Rec->getName();
166 Init *RecordRecTy::convertValue(DefInit *DI) {
167 // Ensure that DI is a subclass of Rec.
168 if (!DI->getDef()->isSubClassOf(Rec))
173 Init *RecordRecTy::convertValue(TypedInit *TI) {
174 // Ensure that TI is compatible with Rec.
175 if (RecordRecTy *RRT = dynamic_cast<RecordRecTy*>(TI->getType()))
176 if (RRT->getRecord()->isSubClassOf(getRecord()) ||
177 RRT->getRecord() == getRecord())
182 bool RecordRecTy::baseClassOf(const RecordRecTy *RHS) const {
183 return Rec == RHS->getRecord() || RHS->getRecord()->isSubClassOf(Rec);
187 //===----------------------------------------------------------------------===//
188 // Initializer implementations
189 //===----------------------------------------------------------------------===//
191 void Init::dump() const { return print(std::cerr); }
193 Init *BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
194 BitsInit *BI = new BitsInit(Bits.size());
195 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
196 if (Bits[i] >= getNumBits()) {
200 BI->setBit(i, getBit(Bits[i]));
205 void BitsInit::print(std::ostream &OS) const {
206 //if (!printInHex(OS)) return;
207 //if (!printAsVariable(OS)) return;
208 //if (!printAsUnset(OS)) return;
211 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
213 if (Init *Bit = getBit(e-i-1))
221 bool BitsInit::printInHex(std::ostream &OS) const {
222 // First, attempt to convert the value into an integer value...
224 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
225 if (BitInit *Bit = dynamic_cast<BitInit*>(getBit(i))) {
226 Result |= Bit->getValue() << i;
231 OS << "0x" << std::hex << Result << std::dec;
235 bool BitsInit::printAsVariable(std::ostream &OS) const {
236 // Get the variable that we may be set equal to...
237 assert(getNumBits() != 0);
238 VarBitInit *FirstBit = dynamic_cast<VarBitInit*>(getBit(0));
239 if (FirstBit == 0) return true;
240 TypedInit *Var = FirstBit->getVariable();
242 // Check to make sure the types are compatible.
243 BitsRecTy *Ty = dynamic_cast<BitsRecTy*>(FirstBit->getVariable()->getType());
244 if (Ty == 0) return true;
245 if (Ty->getNumBits() != getNumBits()) return true; // Incompatible types!
247 // Check to make sure all bits are referring to the right bits in the variable
248 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
249 VarBitInit *Bit = dynamic_cast<VarBitInit*>(getBit(i));
250 if (Bit == 0 || Bit->getVariable() != Var || Bit->getBitNum() != i)
258 bool BitsInit::printAsUnset(std::ostream &OS) const {
259 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
260 if (!dynamic_cast<UnsetInit*>(getBit(i)))
266 // resolveReferences - If there are any field references that refer to fields
267 // that have been filled in, we can propagate the values now.
269 Init *BitsInit::resolveReferences(Record &R) {
270 bool Changed = false;
271 BitsInit *New = new BitsInit(getNumBits());
273 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
275 Init *CurBit = getBit(i);
279 CurBit = CurBit->resolveReferences(R);
280 Changed |= B != CurBit;
281 } while (B != CurBit);
282 New->setBit(i, CurBit);
291 Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
292 BitsInit *BI = new BitsInit(Bits.size());
294 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
299 BI->setBit(i, new BitInit(Value & (1 << Bits[i])));
304 void ListInit::print(std::ostream &OS) const {
306 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
313 Init *VarInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
314 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
315 if (T == 0) return 0; // Cannot subscript a non-bits variable...
316 unsigned NumBits = T->getNumBits();
318 BitsInit *BI = new BitsInit(Bits.size());
319 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
320 if (Bits[i] >= NumBits) {
324 BI->setBit(i, new VarBitInit(this, Bits[i]));
329 Init *VarInit::resolveBitReference(Record &R, unsigned Bit) {
330 if (R.isTemplateArg(getName()))
333 RecordVal *RV = R.getValue(getName());
334 assert(RV && "Reference to a non-existant variable?");
335 assert(dynamic_cast<BitsInit*>(RV->getValue()));
336 BitsInit *BI = (BitsInit*)RV->getValue();
338 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
339 Init *B = BI->getBit(Bit);
341 if (!dynamic_cast<UnsetInit*>(B)) // If the bit is not set...
342 return B; // Replace the VarBitInit with it.
346 RecTy *VarInit::getFieldType(const std::string &FieldName) const {
347 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
348 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
349 return RV->getType();
353 Init *VarInit::getFieldInit(Record &R, const std::string &FieldName) const {
354 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
355 if (const RecordVal *RV = R.getValue(VarName))
356 if (Init *I = RV->getValue()->getFieldInit(R, FieldName))
363 /// resolveReferences - This method is used by classes that refer to other
364 /// variables which may not be defined at the time they expression is formed.
365 /// If a value is set for the variable later, this method will be called on
366 /// users of the value to allow the value to propagate out.
368 Init *VarInit::resolveReferences(Record &R) {
369 if (RecordVal *Val = R.getValue(VarName))
370 if (!dynamic_cast<UnsetInit*>(Val->getValue()))
371 return Val->getValue();
376 Init *VarBitInit::resolveReferences(Record &R) {
377 Init *I = getVariable()->resolveBitReference(R, getBitNum());
378 if (I != getVariable())
383 RecTy *DefInit::getFieldType(const std::string &FieldName) const {
384 if (const RecordVal *RV = Def->getValue(FieldName))
385 return RV->getType();
389 Init *DefInit::getFieldInit(Record &R, const std::string &FieldName) const {
390 return Def->getValue(FieldName)->getValue();
394 void DefInit::print(std::ostream &OS) const {
395 OS << Def->getName();
398 Init *FieldInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
399 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
400 if (T == 0) return 0; // Cannot subscript a non-bits field...
401 unsigned NumBits = T->getNumBits();
403 BitsInit *BI = new BitsInit(Bits.size());
404 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
405 if (Bits[i] >= NumBits) {
409 BI->setBit(i, new VarBitInit(this, Bits[i]));
414 Init *FieldInit::resolveBitReference(Record &R, unsigned Bit) {
415 Init *BitsVal = Rec->getFieldInit(R, FieldName);
417 if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
418 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
419 Init *B = BI->getBit(Bit);
421 if (dynamic_cast<BitInit*>(B)) // If the bit is set...
422 return B; // Replace the VarBitInit with it.
427 Init *FieldInit::resolveReferences(Record &R) {
428 Init *BitsVal = Rec->getFieldInit(R, FieldName);
430 Init *BVR = BitsVal->resolveReferences(R);
431 return BVR->isComplete() ? BVR : this;
437 void DagInit::print(std::ostream &OS) const {
438 OS << "(" << NodeTypeDef->getName();
440 OS << " " << *Args[0];
441 for (unsigned i = 1, e = Args.size(); i != e; ++i)
442 OS << ", " << *Args[i];
448 //===----------------------------------------------------------------------===//
449 // Other implementations
450 //===----------------------------------------------------------------------===//
452 RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
453 : Name(N), Ty(T), Prefix(P) {
454 Value = Ty->convertValue(new UnsetInit());
455 assert(Value && "Cannot create unset value for current type!");
458 void RecordVal::dump() const { std::cerr << *this; }
460 void RecordVal::print(std::ostream &OS, bool PrintSem) const {
461 if (getPrefix()) OS << "field ";
462 OS << *getType() << " " << getName();
464 OS << " = " << *getValue();
466 if (PrintSem) OS << ";\n";
469 // resolveReferences - If there are any field references that refer to fields
470 // that have been filled in, we can propagate the values now.
472 void Record::resolveReferences() {
473 for (unsigned i = 0, e = Values.size(); i != e; ++i)
474 Values[i].setValue(Values[i].getValue()->resolveReferences(*this));
477 void Record::dump() const { std::cerr << *this; }
479 std::ostream &operator<<(std::ostream &OS, const Record &R) {
482 const std::vector<std::string> &TArgs = R.getTemplateArgs();
483 if (!TArgs.empty()) {
485 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
487 const RecordVal *RV = R.getValue(TArgs[i]);
488 assert(RV && "Template argument record not found??");
489 RV->print(OS, false);
495 const std::vector<Record*> &SC = R.getSuperClasses();
498 for (unsigned i = 0, e = SC.size(); i != e; ++i)
499 OS << " " << SC[i]->getName();
503 const std::vector<RecordVal> &Vals = R.getValues();
504 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
505 if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
507 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
508 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
514 /// getValueInit - Return the initializer for a value with the specified name,
515 /// or throw an exception if the field does not exist.
517 Init *Record::getValueInit(const std::string &FieldName) const {
518 const RecordVal *R = getValue(FieldName);
519 if (R == 0 || R->getValue() == 0)
520 throw "Record '" + getName() + "' does not have a field named '" +
522 return R->getValue();
526 /// getValueAsString - This method looks up the specified field and returns its
527 /// value as a string, throwing an exception if the field does not exist or if
528 /// the value is not a string.
530 std::string Record::getValueAsString(const std::string &FieldName) const {
531 const RecordVal *R = getValue(FieldName);
532 if (R == 0 || R->getValue() == 0)
533 throw "Record '" + getName() + "' does not have a field named '" +
536 if (const StringInit *SI = dynamic_cast<const StringInit*>(R->getValue()))
537 return SI->getValue();
538 throw "Record '" + getName() + "', field '" + FieldName +
539 "' does not have a string initializer!";
542 /// getValueAsBitsInit - This method looks up the specified field and returns
543 /// its value as a BitsInit, throwing an exception if the field does not exist
544 /// or if the value is not the right type.
546 BitsInit *Record::getValueAsBitsInit(const std::string &FieldName) const {
547 const RecordVal *R = getValue(FieldName);
548 if (R == 0 || R->getValue() == 0)
549 throw "Record '" + getName() + "' does not have a field named '" +
552 if (BitsInit *BI = dynamic_cast<BitsInit*>(R->getValue()))
554 throw "Record '" + getName() + "', field '" + FieldName +
555 "' does not have a BitsInit initializer!";
558 /// getValueAsListInit - This method looks up the specified field and returns
559 /// its value as a ListInit, throwing an exception if the field does not exist
560 /// or if the value is not the right type.
562 ListInit *Record::getValueAsListInit(const std::string &FieldName) const {
563 const RecordVal *R = getValue(FieldName);
564 if (R == 0 || R->getValue() == 0)
565 throw "Record '" + getName() + "' does not have a field named '" +
568 if (ListInit *LI = dynamic_cast<ListInit*>(R->getValue()))
570 throw "Record '" + getName() + "', field '" + FieldName +
571 "' does not have a list initializer!";
574 /// getValueAsInt - This method looks up the specified field and returns its
575 /// value as an int, throwing an exception if the field does not exist or if
576 /// the value is not the right type.
578 int Record::getValueAsInt(const std::string &FieldName) const {
579 const RecordVal *R = getValue(FieldName);
580 if (R == 0 || R->getValue() == 0)
581 throw "Record '" + getName() + "' does not have a field named '" +
584 if (IntInit *II = dynamic_cast<IntInit*>(R->getValue()))
585 return II->getValue();
586 throw "Record '" + getName() + "', field '" + FieldName +
587 "' does not have a list initializer!";
590 /// getValueAsDef - This method looks up the specified field and returns its
591 /// value as a Record, throwing an exception if the field does not exist or if
592 /// the value is not the right type.
594 Record *Record::getValueAsDef(const std::string &FieldName) const {
595 const RecordVal *R = getValue(FieldName);
596 if (R == 0 || R->getValue() == 0)
597 throw "Record '" + getName() + "' does not have a field named '" +
600 if (DefInit *DI = dynamic_cast<DefInit*>(R->getValue()))
602 throw "Record '" + getName() + "', field '" + FieldName +
603 "' does not have a list initializer!";
606 /// getValueAsBit - This method looks up the specified field and returns its
607 /// value as a bit, throwing an exception if the field does not exist or if
608 /// the value is not the right type.
610 bool Record::getValueAsBit(const std::string &FieldName) const {
611 const RecordVal *R = getValue(FieldName);
612 if (R == 0 || R->getValue() == 0)
613 throw "Record '" + getName() + "' does not have a field named '" +
616 if (BitInit *BI = dynamic_cast<BitInit*>(R->getValue()))
617 return BI->getValue();
618 throw "Record '" + getName() + "', field '" + FieldName +
619 "' does not have a bit initializer!";
622 /// getValueAsDag - This method looks up the specified field and returns its
623 /// value as an Dag, throwing an exception if the field does not exist or if
624 /// the value is not the right type.
626 DagInit *Record::getValueAsDag(const std::string &FieldName) const {
627 const RecordVal *R = getValue(FieldName);
628 if (R == 0 || R->getValue() == 0)
629 throw "Record '" + getName() + "' does not have a field named '" +
632 if (DagInit *DI = dynamic_cast<DagInit*>(R->getValue()))
634 throw "Record '" + getName() + "', field '" + FieldName +
635 "' does not have a dag initializer!";
639 void RecordKeeper::dump() const { std::cerr << *this; }
641 std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK) {
642 OS << "------------- Classes -----------------\n";
643 const std::map<std::string, Record*> &Classes = RK.getClasses();
644 for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
645 E = Classes.end(); I != E; ++I)
646 OS << "class " << *I->second;
648 OS << "------------- Defs -----------------\n";
649 const std::map<std::string, Record*> &Defs = RK.getDefs();
650 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
651 E = Defs.end(); I != E; ++I)
652 OS << "def " << *I->second;
657 /// getAllDerivedDefinitions - This method returns all concrete definitions
658 /// that derive from the specified class name. If a class with the specified
659 /// name does not exist, an error is printed and true is returned.
661 RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
662 Record *Class = Records.getClass(ClassName);
664 throw "ERROR: Couldn't find the '" + ClassName + "' class!\n";
666 std::vector<Record*> Defs;
667 for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(),
668 E = getDefs().end(); I != E; ++I)
669 if (I->second->isSubClassOf(Class))
670 Defs.push_back(I->second);