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 void RecordRecTy::print(std::ostream &OS) const {
150 OS << Rec->getName();
153 Init *RecordRecTy::convertValue(DefInit *DI) {
154 // Ensure that DI is a subclass of Rec.
155 if (!DI->getDef()->isSubClassOf(Rec))
160 Init *RecordRecTy::convertValue(TypedInit *TI) {
161 // Ensure that TI is compatible with Rec.
162 if (RecordRecTy *RRT = dynamic_cast<RecordRecTy*>(TI->getType()))
163 if (RRT->getRecord()->isSubClassOf(getRecord()) ||
164 RRT->getRecord() == getRecord())
169 bool RecordRecTy::baseClassOf(const RecordRecTy *RHS) const {
170 return Rec == RHS->getRecord() || RHS->getRecord()->isSubClassOf(Rec);
174 //===----------------------------------------------------------------------===//
175 // Initializer implementations
176 //===----------------------------------------------------------------------===//
178 void Init::dump() const { return print(std::cerr); }
180 Init *BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
181 BitsInit *BI = new BitsInit(Bits.size());
182 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
183 if (Bits[i] >= getNumBits()) {
187 BI->setBit(i, getBit(Bits[i]));
192 void BitsInit::print(std::ostream &OS) const {
193 //if (!printInHex(OS)) return;
194 //if (!printAsVariable(OS)) return;
195 //if (!printAsUnset(OS)) return;
198 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
200 if (Init *Bit = getBit(e-i-1))
208 bool BitsInit::printInHex(std::ostream &OS) const {
209 // First, attempt to convert the value into an integer value...
211 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
212 if (BitInit *Bit = dynamic_cast<BitInit*>(getBit(i))) {
213 Result |= Bit->getValue() << i;
218 OS << "0x" << std::hex << Result << std::dec;
222 bool BitsInit::printAsVariable(std::ostream &OS) const {
223 // Get the variable that we may be set equal to...
224 assert(getNumBits() != 0);
225 VarBitInit *FirstBit = dynamic_cast<VarBitInit*>(getBit(0));
226 if (FirstBit == 0) return true;
227 TypedInit *Var = FirstBit->getVariable();
229 // Check to make sure the types are compatible.
230 BitsRecTy *Ty = dynamic_cast<BitsRecTy*>(FirstBit->getVariable()->getType());
231 if (Ty == 0) return true;
232 if (Ty->getNumBits() != getNumBits()) return true; // Incompatible types!
234 // Check to make sure all bits are referring to the right bits in the variable
235 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
236 VarBitInit *Bit = dynamic_cast<VarBitInit*>(getBit(i));
237 if (Bit == 0 || Bit->getVariable() != Var || Bit->getBitNum() != i)
245 bool BitsInit::printAsUnset(std::ostream &OS) const {
246 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
247 if (!dynamic_cast<UnsetInit*>(getBit(i)))
253 // resolveReferences - If there are any field references that refer to fields
254 // that have been filled in, we can propagate the values now.
256 Init *BitsInit::resolveReferences(Record &R) {
257 bool Changed = false;
258 BitsInit *New = new BitsInit(getNumBits());
260 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
262 Init *CurBit = getBit(i);
266 CurBit = CurBit->resolveReferences(R);
267 Changed |= B != CurBit;
268 } while (B != CurBit);
269 New->setBit(i, CurBit);
278 Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
279 BitsInit *BI = new BitsInit(Bits.size());
281 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
286 BI->setBit(i, new BitInit(Value & (1 << Bits[i])));
291 void ListInit::print(std::ostream &OS) const {
293 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
300 Init *VarInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
301 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
302 if (T == 0) return 0; // Cannot subscript a non-bits variable...
303 unsigned NumBits = T->getNumBits();
305 BitsInit *BI = new BitsInit(Bits.size());
306 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
307 if (Bits[i] >= NumBits) {
311 BI->setBit(i, new VarBitInit(this, Bits[i]));
316 Init *VarInit::resolveBitReference(Record &R, unsigned Bit) {
317 if (R.isTemplateArg(getName()))
320 RecordVal *RV = R.getValue(getName());
321 assert(RV && "Reference to a non-existant variable?");
322 assert(dynamic_cast<BitsInit*>(RV->getValue()));
323 BitsInit *BI = (BitsInit*)RV->getValue();
325 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
326 Init *B = BI->getBit(Bit);
328 if (!dynamic_cast<UnsetInit*>(B)) // If the bit is not set...
329 return B; // Replace the VarBitInit with it.
333 RecTy *VarInit::getFieldType(const std::string &FieldName) const {
334 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
335 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
336 return RV->getType();
340 Init *VarInit::getFieldInit(Record &R, const std::string &FieldName) const {
341 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
342 if (const RecordVal *RV = R.getValue(VarName))
343 if (Init *I = RV->getValue()->getFieldInit(R, FieldName))
350 /// resolveReferences - This method is used by classes that refer to other
351 /// variables which may not be defined at the time they expression is formed.
352 /// If a value is set for the variable later, this method will be called on
353 /// users of the value to allow the value to propagate out.
355 Init *VarInit::resolveReferences(Record &R) {
356 if (RecordVal *Val = R.getValue(VarName))
357 if (!dynamic_cast<UnsetInit*>(Val->getValue()))
358 return Val->getValue();
363 Init *VarBitInit::resolveReferences(Record &R) {
364 Init *I = getVariable()->resolveBitReference(R, getBitNum());
365 if (I != getVariable())
370 RecTy *DefInit::getFieldType(const std::string &FieldName) const {
371 if (const RecordVal *RV = Def->getValue(FieldName))
372 return RV->getType();
376 Init *DefInit::getFieldInit(Record &R, const std::string &FieldName) const {
377 return Def->getValue(FieldName)->getValue();
381 void DefInit::print(std::ostream &OS) const {
382 OS << Def->getName();
385 Init *FieldInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
386 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
387 if (T == 0) return 0; // Cannot subscript a non-bits field...
388 unsigned NumBits = T->getNumBits();
390 BitsInit *BI = new BitsInit(Bits.size());
391 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
392 if (Bits[i] >= NumBits) {
396 BI->setBit(i, new VarBitInit(this, Bits[i]));
401 Init *FieldInit::resolveBitReference(Record &R, unsigned Bit) {
402 Init *BitsVal = Rec->getFieldInit(R, FieldName);
404 if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
405 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
406 Init *B = BI->getBit(Bit);
408 if (dynamic_cast<BitInit*>(B)) // If the bit is set...
409 return B; // Replace the VarBitInit with it.
414 Init *FieldInit::resolveReferences(Record &R) {
415 Init *BitsVal = Rec->getFieldInit(R, FieldName);
417 Init *BVR = BitsVal->resolveReferences(R);
418 return BVR->isComplete() ? BVR : this;
424 //===----------------------------------------------------------------------===//
425 // Other implementations
426 //===----------------------------------------------------------------------===//
428 RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
429 : Name(N), Ty(T), Prefix(P) {
430 Value = Ty->convertValue(new UnsetInit());
431 assert(Value && "Cannot create unset value for current type!");
434 void RecordVal::dump() const { std::cerr << *this; }
436 void RecordVal::print(std::ostream &OS, bool PrintSem) const {
437 if (getPrefix()) OS << "field ";
438 OS << *getType() << " " << getName();
440 OS << " = " << *getValue();
442 if (PrintSem) OS << ";\n";
445 // resolveReferences - If there are any field references that refer to fields
446 // that have been filled in, we can propagate the values now.
448 void Record::resolveReferences() {
449 for (unsigned i = 0, e = Values.size(); i != e; ++i)
450 Values[i].setValue(Values[i].getValue()->resolveReferences(*this));
453 void Record::dump() const { std::cerr << *this; }
455 std::ostream &operator<<(std::ostream &OS, const Record &R) {
458 const std::vector<std::string> &TArgs = R.getTemplateArgs();
459 if (!TArgs.empty()) {
461 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
463 const RecordVal *RV = R.getValue(TArgs[i]);
464 assert(RV && "Template argument record not found??");
465 RV->print(OS, false);
471 const std::vector<Record*> &SC = R.getSuperClasses();
474 for (unsigned i = 0, e = SC.size(); i != e; ++i)
475 OS << " " << SC[i]->getName();
479 const std::vector<RecordVal> &Vals = R.getValues();
480 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
481 if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
483 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
484 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
490 /// getValueInit - Return the initializer for a value with the specified name,
491 /// or throw an exception if the field does not exist.
493 Init *Record::getValueInit(const std::string &FieldName) const {
494 const RecordVal *R = getValue(FieldName);
495 if (R == 0 || R->getValue() == 0)
496 throw "Record '" + R->getName() + "' does not have a field named '" +
498 return R->getValue();
502 /// getValueAsString - This method looks up the specified field and returns its
503 /// value as a string, throwing an exception if the field does not exist or if
504 /// the value is not a string.
506 std::string Record::getValueAsString(const std::string &FieldName) const {
507 const RecordVal *R = getValue(FieldName);
508 if (R == 0 || R->getValue() == 0)
509 throw "Record '" + R->getName() + "' does not have a field named '" +
512 if (const StringInit *SI = dynamic_cast<const StringInit*>(R->getValue()))
513 return SI->getValue();
514 throw "Record '" + R->getName() + "', field '" + FieldName +
515 "' does not have a string initializer!";
518 /// getValueAsBitsInit - This method looks up the specified field and returns
519 /// its value as a BitsInit, throwing an exception if the field does not exist
520 /// or if the value is not the right type.
522 BitsInit *Record::getValueAsBitsInit(const std::string &FieldName) const {
523 const RecordVal *R = getValue(FieldName);
524 if (R == 0 || R->getValue() == 0)
525 throw "Record '" + R->getName() + "' does not have a field named '" +
528 if (BitsInit *BI = dynamic_cast<BitsInit*>(R->getValue()))
530 throw "Record '" + R->getName() + "', field '" + FieldName +
531 "' does not have a BitsInit initializer!";
534 /// getValueAsListInit - This method looks up the specified field and returns
535 /// its value as a ListInit, throwing an exception if the field does not exist
536 /// or if the value is not the right type.
538 ListInit *Record::getValueAsListInit(const std::string &FieldName) const {
539 const RecordVal *R = getValue(FieldName);
540 if (R == 0 || R->getValue() == 0)
541 throw "Record '" + R->getName() + "' does not have a field named '" +
544 if (ListInit *LI = dynamic_cast<ListInit*>(R->getValue()))
546 throw "Record '" + R->getName() + "', field '" + FieldName +
547 "' does not have a list initializer!";
550 /// getValueAsInt - This method looks up the specified field and returns its
551 /// value as an int, throwing an exception if the field does not exist or if
552 /// the value is not the right type.
554 int Record::getValueAsInt(const std::string &FieldName) const {
555 const RecordVal *R = getValue(FieldName);
556 if (R == 0 || R->getValue() == 0)
557 throw "Record '" + R->getName() + "' does not have a field named '" +
560 if (IntInit *II = dynamic_cast<IntInit*>(R->getValue()))
561 return II->getValue();
562 throw "Record '" + R->getName() + "', field '" + FieldName +
563 "' does not have a list initializer!";
566 /// getValueAsDef - This method looks up the specified field and returns its
567 /// value as a Record, throwing an exception if the field does not exist or if
568 /// the value is not the right type.
570 Record *Record::getValueAsDef(const std::string &FieldName) const {
571 const RecordVal *R = getValue(FieldName);
572 if (R == 0 || R->getValue() == 0)
573 throw "Record '" + R->getName() + "' does not have a field named '" +
576 if (DefInit *DI = dynamic_cast<DefInit*>(R->getValue()))
578 throw "Record '" + R->getName() + "', field '" + FieldName +
579 "' does not have a list initializer!";
583 void RecordKeeper::dump() const { std::cerr << *this; }
585 std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK) {
586 OS << "------------- Classes -----------------\n";
587 const std::map<std::string, Record*> &Classes = RK.getClasses();
588 for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
589 E = Classes.end(); I != E; ++I)
590 OS << "class " << *I->second;
592 OS << "------------- Defs -----------------\n";
593 const std::map<std::string, Record*> &Defs = RK.getDefs();
594 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
595 E = Defs.end(); I != E; ++I)
596 OS << "def " << *I->second;
601 /// getAllDerivedDefinitions - This method returns all concrete definitions
602 /// that derive from the specified class name. If a class with the specified
603 /// name does not exist, an error is printed and true is returned.
605 RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
606 Record *Class = Records.getClass(ClassName);
608 throw "ERROR: Couldn't find the '" + ClassName + "' class!\n";
610 std::vector<Record*> Defs;
611 for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(),
612 E = getDefs().end(); I != E; ++I)
613 if (I->second->isSubClassOf(Class))
614 Defs.push_back(I->second);