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 if (!ArgNames[0].empty()) OS << ":$" << ArgNames[0];
442 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
443 OS << ", " << *Args[i];
444 if (!ArgNames[i].empty()) OS << ":$" << ArgNames[i];
451 //===----------------------------------------------------------------------===//
452 // Other implementations
453 //===----------------------------------------------------------------------===//
455 RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
456 : Name(N), Ty(T), Prefix(P) {
457 Value = Ty->convertValue(new UnsetInit());
458 assert(Value && "Cannot create unset value for current type!");
461 void RecordVal::dump() const { std::cerr << *this; }
463 void RecordVal::print(std::ostream &OS, bool PrintSem) const {
464 if (getPrefix()) OS << "field ";
465 OS << *getType() << " " << getName();
467 OS << " = " << *getValue();
469 if (PrintSem) OS << ";\n";
472 // resolveReferences - If there are any field references that refer to fields
473 // that have been filled in, we can propagate the values now.
475 void Record::resolveReferences() {
476 for (unsigned i = 0, e = Values.size(); i != e; ++i)
477 Values[i].setValue(Values[i].getValue()->resolveReferences(*this));
480 void Record::dump() const { std::cerr << *this; }
482 std::ostream &operator<<(std::ostream &OS, const Record &R) {
485 const std::vector<std::string> &TArgs = R.getTemplateArgs();
486 if (!TArgs.empty()) {
488 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
490 const RecordVal *RV = R.getValue(TArgs[i]);
491 assert(RV && "Template argument record not found??");
492 RV->print(OS, false);
498 const std::vector<Record*> &SC = R.getSuperClasses();
501 for (unsigned i = 0, e = SC.size(); i != e; ++i)
502 OS << " " << SC[i]->getName();
506 const std::vector<RecordVal> &Vals = R.getValues();
507 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
508 if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
510 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
511 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
517 /// getValueInit - Return the initializer for a value with the specified name,
518 /// or throw an exception if the field does not exist.
520 Init *Record::getValueInit(const std::string &FieldName) const {
521 const RecordVal *R = getValue(FieldName);
522 if (R == 0 || R->getValue() == 0)
523 throw "Record '" + getName() + "' does not have a field named '" +
525 return R->getValue();
529 /// getValueAsString - This method looks up the specified field and returns its
530 /// value as a string, throwing an exception if the field does not exist or if
531 /// the value is not a string.
533 std::string Record::getValueAsString(const std::string &FieldName) const {
534 const RecordVal *R = getValue(FieldName);
535 if (R == 0 || R->getValue() == 0)
536 throw "Record '" + getName() + "' does not have a field named '" +
539 if (const StringInit *SI = dynamic_cast<const StringInit*>(R->getValue()))
540 return SI->getValue();
541 throw "Record '" + getName() + "', field '" + FieldName +
542 "' does not have a string initializer!";
545 /// getValueAsBitsInit - This method looks up the specified field and returns
546 /// its value as a BitsInit, throwing an exception if the field does not exist
547 /// or if the value is not the right type.
549 BitsInit *Record::getValueAsBitsInit(const std::string &FieldName) const {
550 const RecordVal *R = getValue(FieldName);
551 if (R == 0 || R->getValue() == 0)
552 throw "Record '" + getName() + "' does not have a field named '" +
555 if (BitsInit *BI = dynamic_cast<BitsInit*>(R->getValue()))
557 throw "Record '" + getName() + "', field '" + FieldName +
558 "' does not have a BitsInit initializer!";
561 /// getValueAsListInit - This method looks up the specified field and returns
562 /// its value as a ListInit, throwing an exception if the field does not exist
563 /// or if the value is not the right type.
565 ListInit *Record::getValueAsListInit(const std::string &FieldName) const {
566 const RecordVal *R = getValue(FieldName);
567 if (R == 0 || R->getValue() == 0)
568 throw "Record '" + getName() + "' does not have a field named '" +
571 if (ListInit *LI = dynamic_cast<ListInit*>(R->getValue()))
573 throw "Record '" + getName() + "', field '" + FieldName +
574 "' does not have a list initializer!";
577 /// getValueAsInt - This method looks up the specified field and returns its
578 /// value as an int, throwing an exception if the field does not exist or if
579 /// the value is not the right type.
581 int Record::getValueAsInt(const std::string &FieldName) const {
582 const RecordVal *R = getValue(FieldName);
583 if (R == 0 || R->getValue() == 0)
584 throw "Record '" + getName() + "' does not have a field named '" +
587 if (IntInit *II = dynamic_cast<IntInit*>(R->getValue()))
588 return II->getValue();
589 throw "Record '" + getName() + "', field '" + FieldName +
590 "' does not have a list initializer!";
593 /// getValueAsDef - This method looks up the specified field and returns its
594 /// value as a Record, throwing an exception if the field does not exist or if
595 /// the value is not the right type.
597 Record *Record::getValueAsDef(const std::string &FieldName) const {
598 const RecordVal *R = getValue(FieldName);
599 if (R == 0 || R->getValue() == 0)
600 throw "Record '" + getName() + "' does not have a field named '" +
603 if (DefInit *DI = dynamic_cast<DefInit*>(R->getValue()))
605 throw "Record '" + getName() + "', field '" + FieldName +
606 "' does not have a list initializer!";
609 /// getValueAsBit - This method looks up the specified field and returns its
610 /// value as a bit, throwing an exception if the field does not exist or if
611 /// the value is not the right type.
613 bool Record::getValueAsBit(const std::string &FieldName) const {
614 const RecordVal *R = getValue(FieldName);
615 if (R == 0 || R->getValue() == 0)
616 throw "Record '" + getName() + "' does not have a field named '" +
619 if (BitInit *BI = dynamic_cast<BitInit*>(R->getValue()))
620 return BI->getValue();
621 throw "Record '" + getName() + "', field '" + FieldName +
622 "' does not have a bit initializer!";
625 /// getValueAsDag - This method looks up the specified field and returns its
626 /// value as an Dag, throwing an exception if the field does not exist or if
627 /// the value is not the right type.
629 DagInit *Record::getValueAsDag(const std::string &FieldName) const {
630 const RecordVal *R = getValue(FieldName);
631 if (R == 0 || R->getValue() == 0)
632 throw "Record '" + getName() + "' does not have a field named '" +
635 if (DagInit *DI = dynamic_cast<DagInit*>(R->getValue()))
637 throw "Record '" + getName() + "', field '" + FieldName +
638 "' does not have a dag initializer!";
642 void RecordKeeper::dump() const { std::cerr << *this; }
644 std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK) {
645 OS << "------------- Classes -----------------\n";
646 const std::map<std::string, Record*> &Classes = RK.getClasses();
647 for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
648 E = Classes.end(); I != E; ++I)
649 OS << "class " << *I->second;
651 OS << "------------- Defs -----------------\n";
652 const std::map<std::string, Record*> &Defs = RK.getDefs();
653 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
654 E = Defs.end(); I != E; ++I)
655 OS << "def " << *I->second;
660 /// getAllDerivedDefinitions - This method returns all concrete definitions
661 /// that derive from the specified class name. If a class with the specified
662 /// name does not exist, an error is printed and true is returned.
664 RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
665 Record *Class = Records.getClass(ClassName);
667 throw "ERROR: Couldn't find the '" + ClassName + "' class!\n";
669 std::vector<Record*> Defs;
670 for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(),
671 E = getDefs().end(); I != E; ++I)
672 if (I->second->isSubClassOf(Class))
673 Defs.push_back(I->second);