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 // resolveReferences - If there are any field references that refer to fields
219 // that have been filled in, we can propagate the values now.
221 Init *BitsInit::resolveReferences(Record &R) {
222 bool Changed = false;
223 BitsInit *New = new BitsInit(getNumBits());
225 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
227 Init *CurBit = getBit(i);
231 CurBit = CurBit->resolveReferences(R);
232 Changed |= B != CurBit;
233 } while (B != CurBit);
234 New->setBit(i, CurBit);
243 Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
244 BitsInit *BI = new BitsInit(Bits.size());
246 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
251 BI->setBit(i, new BitInit(Value & (1 << Bits[i])));
256 void ListInit::print(std::ostream &OS) const {
258 for (unsigned i = 0, e = Records.size(); i != e; ++i) {
260 OS << Records[i]->getName();
265 Init *VarInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
266 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
267 if (T == 0) return 0; // Cannot subscript a non-bits variable...
268 unsigned NumBits = T->getNumBits();
270 BitsInit *BI = new BitsInit(Bits.size());
271 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
272 if (Bits[i] >= NumBits) {
276 BI->setBit(i, new VarBitInit(this, Bits[i]));
281 Init *VarInit::resolveBitReference(Record &R, unsigned Bit) {
282 if (R.isTemplateArg(getName()))
285 RecordVal *RV = R.getValue(getName());
286 assert(RV && "Reference to a non-existant variable?");
287 assert(dynamic_cast<BitsInit*>(RV->getValue()));
288 BitsInit *BI = (BitsInit*)RV->getValue();
290 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
291 Init *B = BI->getBit(Bit);
293 if (!dynamic_cast<UnsetInit*>(B)) // If the bit is not set...
294 return B; // Replace the VarBitInit with it.
298 RecTy *VarInit::getFieldType(const std::string &FieldName) const {
299 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
300 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
301 return RV->getType();
305 Init *VarInit::getFieldInit(Record &R, const std::string &FieldName) const {
306 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
307 if (const RecordVal *RV = R.getValue(VarName))
308 if (Init *I = RV->getValue()->getFieldInit(R, FieldName))
315 /// resolveReferences - This method is used by classes that refer to other
316 /// variables which may not be defined at the time they expression is formed.
317 /// If a value is set for the variable later, this method will be called on
318 /// users of the value to allow the value to propagate out.
320 Init *VarInit::resolveReferences(Record &R) {
321 if (RecordVal *Val = R.getValue(VarName))
322 if (!dynamic_cast<UnsetInit*>(Val->getValue()))
323 return Val->getValue();
328 Init *VarBitInit::resolveReferences(Record &R) {
329 Init *I = getVariable()->resolveBitReference(R, getBitNum());
330 if (I != getVariable())
335 RecTy *DefInit::getFieldType(const std::string &FieldName) const {
336 if (const RecordVal *RV = Def->getValue(FieldName))
337 return RV->getType();
341 Init *DefInit::getFieldInit(Record &R, const std::string &FieldName) const {
342 return Def->getValue(FieldName)->getValue();
346 void DefInit::print(std::ostream &OS) const {
347 OS << Def->getName();
350 Init *FieldInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
351 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
352 if (T == 0) return 0; // Cannot subscript a non-bits field...
353 unsigned NumBits = T->getNumBits();
355 BitsInit *BI = new BitsInit(Bits.size());
356 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
357 if (Bits[i] >= NumBits) {
361 BI->setBit(i, new VarBitInit(this, Bits[i]));
366 Init *FieldInit::resolveBitReference(Record &R, unsigned Bit) {
367 Init *BitsVal = Rec->getFieldInit(R, FieldName);
368 assert(BitsVal && "No initializer found!");
370 if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
371 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
372 Init *B = BI->getBit(Bit);
374 if (dynamic_cast<BitInit*>(B)) // If the bit is set...
375 return B; // Replace the VarBitInit with it.
381 //===----------------------------------------------------------------------===//
382 // Other implementations
383 //===----------------------------------------------------------------------===//
385 RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
386 : Name(N), Ty(T), Prefix(P) {
387 Value = Ty->convertValue(new UnsetInit());
388 assert(Value && "Cannot create unset value for current type!");
391 void RecordVal::dump() const { std::cerr << *this; }
393 void RecordVal::print(std::ostream &OS, bool PrintSem) const {
394 if (getPrefix()) OS << "field ";
395 OS << *getType() << " " << getName();
397 OS << " = " << *getValue();
399 if (PrintSem) OS << ";\n";
402 // resolveReferences - If there are any field references that refer to fields
403 // that have been filled in, we can propagate the values now.
405 void Record::resolveReferences() {
406 for (unsigned i = 0, e = Values.size(); i != e; ++i)
407 Values[i].setValue(Values[i].getValue()->resolveReferences(*this));
410 void Record::dump() const { std::cerr << *this; }
412 std::ostream &operator<<(std::ostream &OS, const Record &R) {
415 const std::vector<std::string> &TArgs = R.getTemplateArgs();
416 if (!TArgs.empty()) {
418 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
420 const RecordVal *RV = R.getValue(TArgs[i]);
421 assert(RV && "Template argument record not found??");
422 RV->print(OS, false);
428 const std::vector<Record*> &SC = R.getSuperClasses();
431 for (unsigned i = 0, e = SC.size(); i != e; ++i)
432 OS << " " << SC[i]->getName();
436 const std::vector<RecordVal> &Vals = R.getValues();
437 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
438 if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
440 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
441 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
447 void RecordKeeper::dump() const { std::cerr << *this; }
449 std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK) {
450 OS << "------------- Classes -----------------\n";
451 const std::map<std::string, Record*> &Classes = RK.getClasses();
452 for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
453 E = Classes.end(); I != E; ++I)
454 OS << "class " << *I->second;
456 OS << "------------- Defs -----------------\n";
457 const std::map<std::string, Record*> &Defs = RK.getDefs();
458 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
459 E = Defs.end(); I != E; ++I)
460 OS << "def " << *I->second;