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(BitsInit *BI) {
98 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
99 if (BitInit *Bit = dynamic_cast<BitInit*>(BI->getBit(i))) {
100 Result |= Bit->getValue() << i;
104 return new IntInit(Result);
107 Init *IntRecTy::convertValue(TypedInit *TI) {
108 if (dynamic_cast<IntRecTy*>(TI->getType()))
109 return TI; // Accept variable if already of the right type!
113 Init *StringRecTy::convertValue(TypedInit *TI) {
114 if (dynamic_cast<StringRecTy*>(TI->getType()))
115 return TI; // Accept variable if already of the right type!
119 void ListRecTy::print(std::ostream &OS) const {
120 OS << "list<" << *Ty << ">";
123 Init *ListRecTy::convertValue(ListInit *LI) {
124 std::vector<Init*> Elements;
126 // Verify that all of the elements of the list are subclasses of the
127 // appropriate class!
128 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
129 if (Init *CI = LI->getElement(i)->convertInitializerTo(Ty))
130 Elements.push_back(CI);
134 return new ListInit(Elements);
137 Init *ListRecTy::convertValue(TypedInit *TI) {
138 // Ensure that TI is compatible with our class.
139 if (ListRecTy *LRT = dynamic_cast<ListRecTy*>(TI->getType()))
140 if (LRT->getElementType()->typeIsConvertibleTo(getElementType()))
145 void RecordRecTy::print(std::ostream &OS) const {
146 OS << Rec->getName();
149 Init *RecordRecTy::convertValue(DefInit *DI) {
150 // Ensure that DI is a subclass of Rec.
151 if (!DI->getDef()->isSubClassOf(Rec))
156 Init *RecordRecTy::convertValue(TypedInit *TI) {
157 // Ensure that TI is compatible with Rec.
158 if (RecordRecTy *RRT = dynamic_cast<RecordRecTy*>(TI->getType()))
159 if (RRT->getRecord()->isSubClassOf(getRecord()) ||
160 RRT->getRecord() == getRecord())
165 bool RecordRecTy::baseClassOf(const RecordRecTy *RHS) const {
166 return Rec == RHS->getRecord() || RHS->getRecord()->isSubClassOf(Rec);
170 //===----------------------------------------------------------------------===//
171 // Initializer implementations
172 //===----------------------------------------------------------------------===//
174 void Init::dump() const { return print(std::cerr); }
176 Init *BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
177 BitsInit *BI = new BitsInit(Bits.size());
178 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
179 if (Bits[i] >= getNumBits()) {
183 BI->setBit(i, getBit(Bits[i]));
188 void BitsInit::print(std::ostream &OS) const {
189 //if (!printInHex(OS)) return;
190 //if (!printAsVariable(OS)) return;
191 //if (!printAsUnset(OS)) return;
194 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
196 if (Init *Bit = getBit(e-i-1))
204 bool BitsInit::printInHex(std::ostream &OS) const {
205 // First, attempt to convert the value into an integer value...
207 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
208 if (BitInit *Bit = dynamic_cast<BitInit*>(getBit(i))) {
209 Result |= Bit->getValue() << i;
214 OS << "0x" << std::hex << Result << std::dec;
218 bool BitsInit::printAsVariable(std::ostream &OS) const {
219 // Get the variable that we may be set equal to...
220 assert(getNumBits() != 0);
221 VarBitInit *FirstBit = dynamic_cast<VarBitInit*>(getBit(0));
222 if (FirstBit == 0) return true;
223 TypedInit *Var = FirstBit->getVariable();
225 // Check to make sure the types are compatible.
226 BitsRecTy *Ty = dynamic_cast<BitsRecTy*>(FirstBit->getVariable()->getType());
227 if (Ty == 0) return true;
228 if (Ty->getNumBits() != getNumBits()) return true; // Incompatible types!
230 // Check to make sure all bits are referring to the right bits in the variable
231 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
232 VarBitInit *Bit = dynamic_cast<VarBitInit*>(getBit(i));
233 if (Bit == 0 || Bit->getVariable() != Var || Bit->getBitNum() != i)
241 bool BitsInit::printAsUnset(std::ostream &OS) const {
242 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
243 if (!dynamic_cast<UnsetInit*>(getBit(i)))
249 // resolveReferences - If there are any field references that refer to fields
250 // that have been filled in, we can propagate the values now.
252 Init *BitsInit::resolveReferences(Record &R) {
253 bool Changed = false;
254 BitsInit *New = new BitsInit(getNumBits());
256 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
258 Init *CurBit = getBit(i);
262 CurBit = CurBit->resolveReferences(R);
263 Changed |= B != CurBit;
264 } while (B != CurBit);
265 New->setBit(i, CurBit);
274 Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
275 BitsInit *BI = new BitsInit(Bits.size());
277 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
282 BI->setBit(i, new BitInit(Value & (1 << Bits[i])));
287 void ListInit::print(std::ostream &OS) const {
289 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
296 Init *VarInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
297 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
298 if (T == 0) return 0; // Cannot subscript a non-bits variable...
299 unsigned NumBits = T->getNumBits();
301 BitsInit *BI = new BitsInit(Bits.size());
302 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
303 if (Bits[i] >= NumBits) {
307 BI->setBit(i, new VarBitInit(this, Bits[i]));
312 Init *VarInit::resolveBitReference(Record &R, unsigned Bit) {
313 if (R.isTemplateArg(getName()))
316 RecordVal *RV = R.getValue(getName());
317 assert(RV && "Reference to a non-existant variable?");
318 assert(dynamic_cast<BitsInit*>(RV->getValue()));
319 BitsInit *BI = (BitsInit*)RV->getValue();
321 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
322 Init *B = BI->getBit(Bit);
324 if (!dynamic_cast<UnsetInit*>(B)) // If the bit is not set...
325 return B; // Replace the VarBitInit with it.
329 RecTy *VarInit::getFieldType(const std::string &FieldName) const {
330 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
331 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
332 return RV->getType();
336 Init *VarInit::getFieldInit(Record &R, const std::string &FieldName) const {
337 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
338 if (const RecordVal *RV = R.getValue(VarName))
339 if (Init *I = RV->getValue()->getFieldInit(R, FieldName))
346 /// resolveReferences - This method is used by classes that refer to other
347 /// variables which may not be defined at the time they expression is formed.
348 /// If a value is set for the variable later, this method will be called on
349 /// users of the value to allow the value to propagate out.
351 Init *VarInit::resolveReferences(Record &R) {
352 if (RecordVal *Val = R.getValue(VarName))
353 if (!dynamic_cast<UnsetInit*>(Val->getValue()))
354 return Val->getValue();
359 Init *VarBitInit::resolveReferences(Record &R) {
360 Init *I = getVariable()->resolveBitReference(R, getBitNum());
361 if (I != getVariable())
366 RecTy *DefInit::getFieldType(const std::string &FieldName) const {
367 if (const RecordVal *RV = Def->getValue(FieldName))
368 return RV->getType();
372 Init *DefInit::getFieldInit(Record &R, const std::string &FieldName) const {
373 return Def->getValue(FieldName)->getValue();
377 void DefInit::print(std::ostream &OS) const {
378 OS << Def->getName();
381 Init *FieldInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
382 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
383 if (T == 0) return 0; // Cannot subscript a non-bits field...
384 unsigned NumBits = T->getNumBits();
386 BitsInit *BI = new BitsInit(Bits.size());
387 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
388 if (Bits[i] >= NumBits) {
392 BI->setBit(i, new VarBitInit(this, Bits[i]));
397 Init *FieldInit::resolveBitReference(Record &R, unsigned Bit) {
398 Init *BitsVal = Rec->getFieldInit(R, FieldName);
400 if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
401 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
402 Init *B = BI->getBit(Bit);
404 if (dynamic_cast<BitInit*>(B)) // If the bit is set...
405 return B; // Replace the VarBitInit with it.
410 Init *FieldInit::resolveReferences(Record &R) {
411 Init *BitsVal = Rec->getFieldInit(R, FieldName);
413 Init *BVR = BitsVal->resolveReferences(R);
414 return BVR->isComplete() ? BVR : this;
420 //===----------------------------------------------------------------------===//
421 // Other implementations
422 //===----------------------------------------------------------------------===//
424 RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
425 : Name(N), Ty(T), Prefix(P) {
426 Value = Ty->convertValue(new UnsetInit());
427 assert(Value && "Cannot create unset value for current type!");
430 void RecordVal::dump() const { std::cerr << *this; }
432 void RecordVal::print(std::ostream &OS, bool PrintSem) const {
433 if (getPrefix()) OS << "field ";
434 OS << *getType() << " " << getName();
436 OS << " = " << *getValue();
438 if (PrintSem) OS << ";\n";
441 // resolveReferences - If there are any field references that refer to fields
442 // that have been filled in, we can propagate the values now.
444 void Record::resolveReferences() {
445 for (unsigned i = 0, e = Values.size(); i != e; ++i)
446 Values[i].setValue(Values[i].getValue()->resolveReferences(*this));
449 void Record::dump() const { std::cerr << *this; }
451 std::ostream &operator<<(std::ostream &OS, const Record &R) {
454 const std::vector<std::string> &TArgs = R.getTemplateArgs();
455 if (!TArgs.empty()) {
457 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
459 const RecordVal *RV = R.getValue(TArgs[i]);
460 assert(RV && "Template argument record not found??");
461 RV->print(OS, false);
467 const std::vector<Record*> &SC = R.getSuperClasses();
470 for (unsigned i = 0, e = SC.size(); i != e; ++i)
471 OS << " " << SC[i]->getName();
475 const std::vector<RecordVal> &Vals = R.getValues();
476 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
477 if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
479 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
480 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
486 /// getValueInit - Return the initializer for a value with the specified name,
487 /// or throw an exception if the field does not exist.
489 Init *Record::getValueInit(const std::string &FieldName) const {
490 const RecordVal *R = getValue(FieldName);
491 if (R == 0 || R->getValue() == 0)
492 throw "Record '" + R->getName() + "' does not have a field named '" +
494 return R->getValue();
498 /// getValueAsString - This method looks up the specified field and returns its
499 /// value as a string, throwing an exception if the field does not exist or if
500 /// the value is not a string.
502 std::string Record::getValueAsString(const std::string &FieldName) const {
503 const RecordVal *R = getValue(FieldName);
504 if (R == 0 || R->getValue() == 0)
505 throw "Record '" + R->getName() + "' does not have a field named '" +
508 if (const StringInit *SI = dynamic_cast<const StringInit*>(R->getValue()))
509 return SI->getValue();
510 throw "Record '" + R->getName() + "', field '" + FieldName +
511 "' does not have a string initializer!";
514 /// getValueAsBitsInit - This method looks up the specified field and returns
515 /// its value as a BitsInit, throwing an exception if the field does not exist
516 /// or if the value is not the right type.
518 BitsInit *Record::getValueAsBitsInit(const std::string &FieldName) const {
519 const RecordVal *R = getValue(FieldName);
520 if (R == 0 || R->getValue() == 0)
521 throw "Record '" + R->getName() + "' does not have a field named '" +
524 if (BitsInit *BI = dynamic_cast<BitsInit*>(R->getValue()))
526 throw "Record '" + R->getName() + "', field '" + FieldName +
527 "' does not have a BitsInit initializer!";
530 /// getValueAsListInit - This method looks up the specified field and returns
531 /// its value as a ListInit, throwing an exception if the field does not exist
532 /// or if the value is not the right type.
534 ListInit *Record::getValueAsListInit(const std::string &FieldName) const {
535 const RecordVal *R = getValue(FieldName);
536 if (R == 0 || R->getValue() == 0)
537 throw "Record '" + R->getName() + "' does not have a field named '" +
540 if (ListInit *LI = dynamic_cast<ListInit*>(R->getValue()))
542 throw "Record '" + R->getName() + "', field '" + FieldName +
543 "' does not have a list initializer!";
546 /// getValueAsInt - This method looks up the specified field and returns its
547 /// value as an int, throwing an exception if the field does not exist or if
548 /// the value is not the right type.
550 int Record::getValueAsInt(const std::string &FieldName) const {
551 const RecordVal *R = getValue(FieldName);
552 if (R == 0 || R->getValue() == 0)
553 throw "Record '" + R->getName() + "' does not have a field named '" +
556 if (IntInit *II = dynamic_cast<IntInit*>(R->getValue()))
557 return II->getValue();
558 throw "Record '" + R->getName() + "', field '" + FieldName +
559 "' does not have a list initializer!";
562 /// getValueAsDef - This method looks up the specified field and returns its
563 /// value as a Record, throwing an exception if the field does not exist or if
564 /// the value is not the right type.
566 Record *Record::getValueAsDef(const std::string &FieldName) const {
567 const RecordVal *R = getValue(FieldName);
568 if (R == 0 || R->getValue() == 0)
569 throw "Record '" + R->getName() + "' does not have a field named '" +
572 if (DefInit *DI = dynamic_cast<DefInit*>(R->getValue()))
574 throw "Record '" + R->getName() + "', field '" + FieldName +
575 "' does not have a list initializer!";
579 void RecordKeeper::dump() const { std::cerr << *this; }
581 std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK) {
582 OS << "------------- Classes -----------------\n";
583 const std::map<std::string, Record*> &Classes = RK.getClasses();
584 for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
585 E = Classes.end(); I != E; ++I)
586 OS << "class " << *I->second;
588 OS << "------------- Defs -----------------\n";
589 const std::map<std::string, Record*> &Defs = RK.getDefs();
590 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
591 E = Defs.end(); I != E; ++I)
592 OS << "def " << *I->second;
597 /// getAllDerivedDefinitions - This method returns all concrete definitions
598 /// that derive from the specified class name. If a class with the specified
599 /// name does not exist, an error is printed and true is returned.
601 RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
602 Record *Class = Records.getClass(ClassName);
604 throw "ERROR: Couldn't find the '" + ClassName + "' class!\n";
606 std::vector<Record*> Defs;
607 for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(),
608 E = getDefs().end(); I != E; ++I)
609 if (I->second->isSubClassOf(Class))
610 Defs.push_back(I->second);