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 void RecordRecTy::print(std::ostream &OS) const {
120 OS << Rec->getName();
123 Init *RecordRecTy::convertValue(DefInit *DI) {
124 // Ensure that DI is a subclass of Rec.
125 if (!DI->getDef()->isSubClassOf(Rec))
130 Init *RecordRecTy::convertValue(TypedInit *VI) {
131 // Ensure that VI is compatible with Rec.
132 if (RecordRecTy *RRT = dynamic_cast<RecordRecTy*>(VI->getType()))
133 if (RRT->getRecord()->isSubClassOf(getRecord()) ||
134 RRT->getRecord() == getRecord())
139 //===----------------------------------------------------------------------===//
140 // Initializer implementations
141 //===----------------------------------------------------------------------===//
143 void Init::dump() const { return print(std::cerr); }
145 Init *BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
146 BitsInit *BI = new BitsInit(Bits.size());
147 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
148 if (Bits[i] >= getNumBits()) {
152 BI->setBit(i, getBit(Bits[i]));
157 void BitsInit::print(std::ostream &OS) const {
158 //if (!printInHex(OS)) return;
159 //if (!printAsVariable(OS)) return;
160 //if (!printAsUnset(OS)) return;
163 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
165 if (Init *Bit = getBit(e-i-1))
173 bool BitsInit::printInHex(std::ostream &OS) const {
174 // First, attempt to convert the value into an integer value...
176 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
177 if (BitInit *Bit = dynamic_cast<BitInit*>(getBit(i))) {
178 Result |= Bit->getValue() << i;
183 OS << "0x" << std::hex << Result << std::dec;
187 bool BitsInit::printAsVariable(std::ostream &OS) const {
188 // Get the variable that we may be set equal to...
189 assert(getNumBits() != 0);
190 VarBitInit *FirstBit = dynamic_cast<VarBitInit*>(getBit(0));
191 if (FirstBit == 0) return true;
192 TypedInit *Var = FirstBit->getVariable();
194 // Check to make sure the types are compatible.
195 BitsRecTy *Ty = dynamic_cast<BitsRecTy*>(FirstBit->getVariable()->getType());
196 if (Ty == 0) return true;
197 if (Ty->getNumBits() != getNumBits()) return true; // Incompatible types!
199 // Check to make sure all bits are referring to the right bits in the variable
200 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
201 VarBitInit *Bit = dynamic_cast<VarBitInit*>(getBit(i));
202 if (Bit == 0 || Bit->getVariable() != Var || Bit->getBitNum() != i)
210 bool BitsInit::printAsUnset(std::ostream &OS) const {
211 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
212 if (!dynamic_cast<UnsetInit*>(getBit(i)))
218 Init *BitsInit::resolveReferences(Record &R) {
219 bool Changed = false;
220 BitsInit *New = new BitsInit(getNumBits());
222 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
224 Init *CurBit = getBit(i);
228 CurBit = CurBit->resolveReferences(R);
229 Changed |= B != CurBit;
230 } while (B != CurBit);
231 New->setBit(i, CurBit);
240 Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
241 BitsInit *BI = new BitsInit(Bits.size());
243 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
248 BI->setBit(i, new BitInit(Value & (1 << Bits[i])));
253 void ListInit::print(std::ostream &OS) const {
255 for (unsigned i = 0, e = Records.size(); i != e; ++i) {
257 OS << Records[i]->getName();
262 Init *VarInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
263 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
264 if (T == 0) return 0; // Cannot subscript a non-bits variable...
265 unsigned NumBits = T->getNumBits();
267 BitsInit *BI = new BitsInit(Bits.size());
268 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
269 if (Bits[i] >= NumBits) {
273 BI->setBit(i, new VarBitInit(this, Bits[i]));
278 Init *VarInit::resolveBitReference(Record &R, unsigned Bit) {
279 if (R.isTemplateArg(getName()))
282 RecordVal *RV = R.getValue(getName());
283 assert(RV && "Reference to a non-existant variable?");
284 assert(dynamic_cast<BitsInit*>(RV->getValue()));
285 BitsInit *BI = (BitsInit*)RV->getValue();
287 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
288 Init *B = BI->getBit(Bit);
290 if (!dynamic_cast<UnsetInit*>(B)) // If the bit is not set...
291 return B; // Replace the VarBitInit with it.
295 RecTy *VarInit::getFieldType(const std::string &FieldName) const {
296 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
297 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
298 return RV->getType();
302 Init *VarInit::getFieldInit(Record &R, const std::string &FieldName) const {
303 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
304 if (const RecordVal *RV = R.getValue(VarName))
305 if (Init *I = RV->getValue()->getFieldInit(R, FieldName))
314 Init *VarBitInit::resolveReferences(Record &R) {
315 Init *I = getVariable()->resolveBitReference(R, getBitNum());
316 if (I != getVariable())
321 RecTy *DefInit::getFieldType(const std::string &FieldName) const {
322 if (const RecordVal *RV = Def->getValue(FieldName))
323 return RV->getType();
327 Init *DefInit::getFieldInit(Record &R, const std::string &FieldName) const {
328 return Def->getValue(FieldName)->getValue();
332 void DefInit::print(std::ostream &OS) const {
333 OS << Def->getName();
336 Init *FieldInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
337 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
338 if (T == 0) return 0; // Cannot subscript a non-bits field...
339 unsigned NumBits = T->getNumBits();
341 BitsInit *BI = new BitsInit(Bits.size());
342 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
343 if (Bits[i] >= NumBits) {
347 BI->setBit(i, new VarBitInit(this, Bits[i]));
352 Init *FieldInit::resolveBitReference(Record &R, unsigned Bit) {
353 Init *BitsVal = Rec->getFieldInit(R, FieldName);
354 assert(BitsVal && "No initializer found!");
356 if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
357 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
358 Init *B = BI->getBit(Bit);
360 if (dynamic_cast<BitInit*>(B)) // If the bit is set...
361 return B; // Replace the VarBitInit with it.
367 //===----------------------------------------------------------------------===//
368 // Other implementations
369 //===----------------------------------------------------------------------===//
371 RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
372 : Name(N), Ty(T), Prefix(P) {
373 Value = Ty->convertValue(new UnsetInit());
374 assert(Value && "Cannot create unset value for current type!");
377 void RecordVal::dump() const { std::cerr << *this; }
379 void RecordVal::print(std::ostream &OS, bool PrintSem) const {
380 if (getPrefix()) OS << "field ";
381 OS << *getType() << " " << getName();
383 OS << " = " << *getValue();
385 if (PrintSem) OS << ";\n";
388 // resolveReferences - If there are any field references that refer to fields
389 // that have been filled in, we can propagate the values now.
391 void Record::resolveReferences() {
392 for (unsigned i = 0, e = Values.size(); i != e; ++i)
393 Values[i].setValue(Values[i].getValue()->resolveReferences(*this));
396 void Record::dump() const { std::cerr << *this; }
398 std::ostream &operator<<(std::ostream &OS, const Record &R) {
401 const std::vector<std::string> &TArgs = R.getTemplateArgs();
402 if (!TArgs.empty()) {
404 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
406 const RecordVal *RV = R.getValue(TArgs[i]);
407 assert(RV && "Template argument record not found??");
408 RV->print(OS, false);
414 const std::vector<Record*> &SC = R.getSuperClasses();
417 for (unsigned i = 0, e = SC.size(); i != e; ++i)
418 OS << " " << SC[i]->getName();
422 const std::vector<RecordVal> &Vals = R.getValues();
423 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
424 if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
426 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
427 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
433 void RecordKeeper::dump() const { std::cerr << *this; }
435 std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK) {
436 OS << "------------- Classes -----------------\n";
437 const std::map<std::string, Record*> &Classes = RK.getClasses();
438 for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
439 E = Classes.end(); I != E; ++I)
440 OS << "class " << *I->second;
442 OS << "------------- Defs -----------------\n";
443 const std::map<std::string, Record*> &Defs = RK.getDefs();
444 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
445 E = Defs.end(); I != E; ++I)
446 OS << "def " << *I->second;