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 Init *BitRecTy::convertValue(IntInit *II) {
20 int Val = II->getValue();
21 if (Val != 0 && Val != 1) return 0; // Only accept 0 or 1 for a bit!
23 return new BitInit(Val != 0);
26 Init *BitRecTy::convertValue(TypedInit *VI) {
27 if (dynamic_cast<BitRecTy*>(VI->getType()))
28 return VI; // Accept variable if it is already of bit type!
32 Init *BitsRecTy::convertValue(UnsetInit *UI) {
33 BitsInit *Ret = new BitsInit(Size);
35 for (unsigned i = 0; i != Size; ++i)
36 Ret->setBit(i, new UnsetInit());
40 Init *BitsRecTy::convertValue(BitInit *UI) {
41 if (Size != 1) return 0; // Can only convert single bit...
42 BitsInit *Ret = new BitsInit(1);
47 // convertValue from Int initializer to bits type: Split the integer up into the
48 // appropriate bits...
50 Init *BitsRecTy::convertValue(IntInit *II) {
51 int Value = II->getValue();
53 BitsInit *Ret = new BitsInit(Size);
54 for (unsigned i = 0; i != Size; ++i)
55 Ret->setBit(i, new BitInit(Value & (1 << i)));
59 Init *BitsRecTy::convertValue(BitsInit *BI) {
60 // If the number of bits is right, return it. Otherwise we need to expand or
62 if (BI->getNumBits() == Size) return BI;
66 Init *BitsRecTy::convertValue(TypedInit *VI) {
67 if (BitsRecTy *BRT = dynamic_cast<BitsRecTy*>(VI->getType()))
68 if (BRT->Size == Size) {
69 BitsInit *Ret = new BitsInit(Size);
70 for (unsigned i = 0; i != Size; ++i)
71 Ret->setBit(i, new VarBitInit(VI, i));
74 if (Size == 1 && dynamic_cast<BitRecTy*>(VI->getType())) {
75 BitsInit *Ret = new BitsInit(1);
83 Init *IntRecTy::convertValue(BitsInit *BI) {
85 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
86 if (BitInit *Bit = dynamic_cast<BitInit*>(BI->getBit(i))) {
87 Result |= Bit->getValue() << i;
91 return new IntInit(Result);
94 Init *IntRecTy::convertValue(TypedInit *TI) {
95 if (dynamic_cast<IntRecTy*>(TI->getType()))
96 return TI; // Accept variable if already of the right type!
100 Init *StringRecTy::convertValue(TypedInit *TI) {
101 if (dynamic_cast<StringRecTy*>(TI->getType()))
102 return TI; // Accept variable if already of the right type!
106 void ListRecTy::print(std::ostream &OS) const {
107 OS << "list<" << Class->getName() << ">";
110 Init *ListRecTy::convertValue(ListInit *LI) {
111 // Verify that all of the elements of the list are subclasses of the
113 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
114 if (!LI->getElement(i)->isSubClassOf(Class))
119 Init *ListRecTy::convertValue(TypedInit *TI) {
120 // Ensure that TI is compatible with our class.
121 if (ListRecTy *LRT = dynamic_cast<ListRecTy*>(TI->getType()))
122 if (LRT->getElementClass() == getElementClass())
127 void RecordRecTy::print(std::ostream &OS) const {
128 OS << Rec->getName();
131 Init *RecordRecTy::convertValue(DefInit *DI) {
132 // Ensure that DI is a subclass of Rec.
133 if (!DI->getDef()->isSubClassOf(Rec))
138 Init *RecordRecTy::convertValue(TypedInit *TI) {
139 // Ensure that TI is compatible with Rec.
140 if (RecordRecTy *RRT = dynamic_cast<RecordRecTy*>(TI->getType()))
141 if (RRT->getRecord()->isSubClassOf(getRecord()) ||
142 RRT->getRecord() == getRecord())
147 //===----------------------------------------------------------------------===//
148 // Initializer implementations
149 //===----------------------------------------------------------------------===//
151 void Init::dump() const { return print(std::cerr); }
153 Init *BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
154 BitsInit *BI = new BitsInit(Bits.size());
155 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
156 if (Bits[i] >= getNumBits()) {
160 BI->setBit(i, getBit(Bits[i]));
165 void BitsInit::print(std::ostream &OS) const {
166 //if (!printInHex(OS)) return;
167 //if (!printAsVariable(OS)) return;
168 //if (!printAsUnset(OS)) return;
171 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
173 if (Init *Bit = getBit(e-i-1))
181 bool BitsInit::printInHex(std::ostream &OS) const {
182 // First, attempt to convert the value into an integer value...
184 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
185 if (BitInit *Bit = dynamic_cast<BitInit*>(getBit(i))) {
186 Result |= Bit->getValue() << i;
191 OS << "0x" << std::hex << Result << std::dec;
195 bool BitsInit::printAsVariable(std::ostream &OS) const {
196 // Get the variable that we may be set equal to...
197 assert(getNumBits() != 0);
198 VarBitInit *FirstBit = dynamic_cast<VarBitInit*>(getBit(0));
199 if (FirstBit == 0) return true;
200 TypedInit *Var = FirstBit->getVariable();
202 // Check to make sure the types are compatible.
203 BitsRecTy *Ty = dynamic_cast<BitsRecTy*>(FirstBit->getVariable()->getType());
204 if (Ty == 0) return true;
205 if (Ty->getNumBits() != getNumBits()) return true; // Incompatible types!
207 // Check to make sure all bits are referring to the right bits in the variable
208 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
209 VarBitInit *Bit = dynamic_cast<VarBitInit*>(getBit(i));
210 if (Bit == 0 || Bit->getVariable() != Var || Bit->getBitNum() != i)
218 bool BitsInit::printAsUnset(std::ostream &OS) const {
219 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
220 if (!dynamic_cast<UnsetInit*>(getBit(i)))
226 // resolveReferences - If there are any field references that refer to fields
227 // that have been filled in, we can propagate the values now.
229 Init *BitsInit::resolveReferences(Record &R) {
230 bool Changed = false;
231 BitsInit *New = new BitsInit(getNumBits());
233 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
235 Init *CurBit = getBit(i);
239 CurBit = CurBit->resolveReferences(R);
240 Changed |= B != CurBit;
241 } while (B != CurBit);
242 New->setBit(i, CurBit);
251 Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
252 BitsInit *BI = new BitsInit(Bits.size());
254 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
259 BI->setBit(i, new BitInit(Value & (1 << Bits[i])));
264 void ListInit::print(std::ostream &OS) const {
266 for (unsigned i = 0, e = Records.size(); i != e; ++i) {
268 OS << Records[i]->getName();
273 Init *VarInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
274 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
275 if (T == 0) return 0; // Cannot subscript a non-bits variable...
276 unsigned NumBits = T->getNumBits();
278 BitsInit *BI = new BitsInit(Bits.size());
279 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
280 if (Bits[i] >= NumBits) {
284 BI->setBit(i, new VarBitInit(this, Bits[i]));
289 Init *VarInit::resolveBitReference(Record &R, unsigned Bit) {
290 if (R.isTemplateArg(getName()))
293 RecordVal *RV = R.getValue(getName());
294 assert(RV && "Reference to a non-existant variable?");
295 assert(dynamic_cast<BitsInit*>(RV->getValue()));
296 BitsInit *BI = (BitsInit*)RV->getValue();
298 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
299 Init *B = BI->getBit(Bit);
301 if (!dynamic_cast<UnsetInit*>(B)) // If the bit is not set...
302 return B; // Replace the VarBitInit with it.
306 RecTy *VarInit::getFieldType(const std::string &FieldName) const {
307 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
308 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
309 return RV->getType();
313 Init *VarInit::getFieldInit(Record &R, const std::string &FieldName) const {
314 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
315 if (const RecordVal *RV = R.getValue(VarName))
316 if (Init *I = RV->getValue()->getFieldInit(R, FieldName))
323 /// resolveReferences - This method is used by classes that refer to other
324 /// variables which may not be defined at the time they expression is formed.
325 /// If a value is set for the variable later, this method will be called on
326 /// users of the value to allow the value to propagate out.
328 Init *VarInit::resolveReferences(Record &R) {
329 if (RecordVal *Val = R.getValue(VarName))
330 if (!dynamic_cast<UnsetInit*>(Val->getValue()))
331 return Val->getValue();
336 Init *VarBitInit::resolveReferences(Record &R) {
337 Init *I = getVariable()->resolveBitReference(R, getBitNum());
338 if (I != getVariable())
343 RecTy *DefInit::getFieldType(const std::string &FieldName) const {
344 if (const RecordVal *RV = Def->getValue(FieldName))
345 return RV->getType();
349 Init *DefInit::getFieldInit(Record &R, const std::string &FieldName) const {
350 return Def->getValue(FieldName)->getValue();
354 void DefInit::print(std::ostream &OS) const {
355 OS << Def->getName();
358 Init *FieldInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
359 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
360 if (T == 0) return 0; // Cannot subscript a non-bits field...
361 unsigned NumBits = T->getNumBits();
363 BitsInit *BI = new BitsInit(Bits.size());
364 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
365 if (Bits[i] >= NumBits) {
369 BI->setBit(i, new VarBitInit(this, Bits[i]));
374 Init *FieldInit::resolveBitReference(Record &R, unsigned Bit) {
375 Init *BitsVal = Rec->getFieldInit(R, FieldName);
376 assert(BitsVal && "No initializer found!");
378 if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
379 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
380 Init *B = BI->getBit(Bit);
382 if (dynamic_cast<BitInit*>(B)) // If the bit is set...
383 return B; // Replace the VarBitInit with it.
389 //===----------------------------------------------------------------------===//
390 // Other implementations
391 //===----------------------------------------------------------------------===//
393 RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
394 : Name(N), Ty(T), Prefix(P) {
395 Value = Ty->convertValue(new UnsetInit());
396 assert(Value && "Cannot create unset value for current type!");
399 void RecordVal::dump() const { std::cerr << *this; }
401 void RecordVal::print(std::ostream &OS, bool PrintSem) const {
402 if (getPrefix()) OS << "field ";
403 OS << *getType() << " " << getName();
405 OS << " = " << *getValue();
407 if (PrintSem) OS << ";\n";
410 // resolveReferences - If there are any field references that refer to fields
411 // that have been filled in, we can propagate the values now.
413 void Record::resolveReferences() {
414 for (unsigned i = 0, e = Values.size(); i != e; ++i)
415 Values[i].setValue(Values[i].getValue()->resolveReferences(*this));
418 void Record::dump() const { std::cerr << *this; }
420 std::ostream &operator<<(std::ostream &OS, const Record &R) {
423 const std::vector<std::string> &TArgs = R.getTemplateArgs();
424 if (!TArgs.empty()) {
426 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
428 const RecordVal *RV = R.getValue(TArgs[i]);
429 assert(RV && "Template argument record not found??");
430 RV->print(OS, false);
436 const std::vector<Record*> &SC = R.getSuperClasses();
439 for (unsigned i = 0, e = SC.size(); i != e; ++i)
440 OS << " " << SC[i]->getName();
444 const std::vector<RecordVal> &Vals = R.getValues();
445 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
446 if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
448 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
449 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
455 void RecordKeeper::dump() const { std::cerr << *this; }
457 std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK) {
458 OS << "------------- Classes -----------------\n";
459 const std::map<std::string, Record*> &Classes = RK.getClasses();
460 for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
461 E = Classes.end(); I != E; ++I)
462 OS << "class " << *I->second;
464 OS << "------------- Defs -----------------\n";
465 const std::map<std::string, Record*> &Defs = RK.getDefs();
466 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
467 E = Defs.end(); I != E; ++I)
468 OS << "def " << *I->second;
473 /// getAllDerivedDefinitions - This method returns all concrete definitions
474 /// that derive from the specified class name. If a class with the specified
475 /// name does not exist, an error is printed and true is returned.
476 bool RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName,
477 std::vector<Record*> &Defs) const {
478 Record *Class = Records.getClass(ClassName);
480 std::cerr << "ERROR: Couldn't find the '" << ClassName << "' class!\n";
484 for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(),
485 E = getDefs().end(); I != E; ++I)
486 if (I->second->isSubClassOf(Class))
487 Defs.push_back(I->second);