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 //===----------------------------------------------------------------------===//
131 // Initializer implementations
132 //===----------------------------------------------------------------------===//
134 void Init::dump() const { return print(std::cerr); }
136 Init *BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
137 BitsInit *BI = new BitsInit(Bits.size());
138 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
139 if (Bits[i] >= getNumBits()) {
143 BI->setBit(i, getBit(Bits[i]));
148 void BitsInit::print(std::ostream &OS) const {
149 //if (!printInHex(OS)) return;
150 //if (!printAsVariable(OS)) return;
151 //if (!printAsUnset(OS)) return;
154 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
156 if (Init *Bit = getBit(e-i-1))
164 bool BitsInit::printInHex(std::ostream &OS) const {
165 // First, attempt to convert the value into an integer value...
167 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
168 if (BitInit *Bit = dynamic_cast<BitInit*>(getBit(i))) {
169 Result |= Bit->getValue() << i;
174 OS << "0x" << std::hex << Result << std::dec;
178 bool BitsInit::printAsVariable(std::ostream &OS) const {
179 // Get the variable that we may be set equal to...
180 assert(getNumBits() != 0);
181 VarBitInit *FirstBit = dynamic_cast<VarBitInit*>(getBit(0));
182 if (FirstBit == 0) return true;
183 TypedInit *Var = FirstBit->getVariable();
185 // Check to make sure the types are compatible.
186 BitsRecTy *Ty = dynamic_cast<BitsRecTy*>(FirstBit->getVariable()->getType());
187 if (Ty == 0) return true;
188 if (Ty->getNumBits() != getNumBits()) return true; // Incompatible types!
190 // Check to make sure all bits are referring to the right bits in the variable
191 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
192 VarBitInit *Bit = dynamic_cast<VarBitInit*>(getBit(i));
193 if (Bit == 0 || Bit->getVariable() != Var || Bit->getBitNum() != i)
201 bool BitsInit::printAsUnset(std::ostream &OS) const {
202 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
203 if (!dynamic_cast<UnsetInit*>(getBit(i)))
209 Init *BitsInit::resolveReferences(Record &R) {
210 bool Changed = false;
211 BitsInit *New = new BitsInit(getNumBits());
213 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
215 Init *CurBit = getBit(i);
219 CurBit = CurBit->resolveReferences(R);
220 Changed |= B != CurBit;
221 } while (B != CurBit);
222 New->setBit(i, CurBit);
231 Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
232 BitsInit *BI = new BitsInit(Bits.size());
234 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
239 BI->setBit(i, new BitInit(Value & (1 << Bits[i])));
244 void ListInit::print(std::ostream &OS) const {
246 for (unsigned i = 0, e = Records.size(); i != e; ++i) {
248 OS << Records[i]->getName();
253 Init *VarInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
254 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
255 if (T == 0) return 0; // Cannot subscript a non-bits variable...
256 unsigned NumBits = T->getNumBits();
258 BitsInit *BI = new BitsInit(Bits.size());
259 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
260 if (Bits[i] >= NumBits) {
264 BI->setBit(i, new VarBitInit(this, Bits[i]));
269 Init *VarInit::resolveBitReference(Record &R, unsigned Bit) {
270 if (R.isTemplateArg(getName()))
273 RecordVal *RV = R.getValue(getName());
274 assert(RV && "Reference to a non-existant variable?");
275 assert(dynamic_cast<BitsInit*>(RV->getValue()));
276 BitsInit *BI = (BitsInit*)RV->getValue();
278 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
279 Init *B = BI->getBit(Bit);
281 if (!dynamic_cast<UnsetInit*>(B)) // If the bit is not set...
282 return B; // Replace the VarBitInit with it.
286 RecTy *VarInit::getFieldType(const std::string &FieldName) const {
287 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
288 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
289 return RV->getType();
293 Init *VarInit::getFieldInit(Record &R, const std::string &FieldName) const {
294 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
295 if (const RecordVal *RV = R.getValue(VarName))
296 if (Init *I = RV->getValue()->getFieldInit(R, FieldName))
305 Init *VarBitInit::resolveReferences(Record &R) {
306 Init *I = getVariable()->resolveBitReference(R, getBitNum());
307 if (I != getVariable())
312 RecTy *DefInit::getFieldType(const std::string &FieldName) const {
313 if (const RecordVal *RV = Def->getValue(FieldName))
314 return RV->getType();
318 Init *DefInit::getFieldInit(Record &R, const std::string &FieldName) const {
319 return Def->getValue(FieldName)->getValue();
323 void DefInit::print(std::ostream &OS) const {
324 OS << Def->getName();
327 Init *FieldInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
328 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
329 if (T == 0) return 0; // Cannot subscript a non-bits field...
330 unsigned NumBits = T->getNumBits();
332 BitsInit *BI = new BitsInit(Bits.size());
333 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
334 if (Bits[i] >= NumBits) {
338 BI->setBit(i, new VarBitInit(this, Bits[i]));
343 Init *FieldInit::resolveBitReference(Record &R, unsigned Bit) {
344 Init *BitsVal = Rec->getFieldInit(R, FieldName);
345 assert(BitsVal && "No initializer found!");
347 if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
348 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
349 Init *B = BI->getBit(Bit);
351 if (dynamic_cast<BitInit*>(B)) // If the bit is set...
352 return B; // Replace the VarBitInit with it.
358 //===----------------------------------------------------------------------===//
359 // Other implementations
360 //===----------------------------------------------------------------------===//
362 RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
363 : Name(N), Ty(T), Prefix(P) {
364 Value = Ty->convertValue(new UnsetInit());
365 assert(Value && "Cannot create unset value for current type!");
368 void RecordVal::dump() const { std::cerr << *this; }
370 void RecordVal::print(std::ostream &OS, bool PrintSem) const {
371 if (getPrefix()) OS << "field ";
372 OS << *getType() << " " << getName();
374 OS << " = " << *getValue();
376 if (PrintSem) OS << ";\n";
379 // resolveReferences - If there are any field references that refer to fields
380 // that have been filled in, we can propagate the values now.
382 void Record::resolveReferences() {
383 for (unsigned i = 0, e = Values.size(); i != e; ++i)
384 Values[i].setValue(Values[i].getValue()->resolveReferences(*this));
387 void Record::dump() const { std::cerr << *this; }
389 std::ostream &operator<<(std::ostream &OS, const Record &R) {
392 const std::vector<std::string> &TArgs = R.getTemplateArgs();
393 if (!TArgs.empty()) {
395 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
397 const RecordVal *RV = R.getValue(TArgs[i]);
398 assert(RV && "Template argument record not found??");
399 RV->print(OS, false);
405 const std::vector<Record*> &SC = R.getSuperClasses();
408 for (unsigned i = 0, e = SC.size(); i != e; ++i)
409 OS << " " << SC[i]->getName();
413 const std::vector<RecordVal> &Vals = R.getValues();
414 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
415 if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
417 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
418 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
424 void RecordKeeper::dump() const { std::cerr << *this; }
426 std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK) {
427 OS << "------------- Classes -----------------\n";
428 const std::map<std::string, Record*> &Classes = RK.getClasses();
429 for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
430 E = Classes.end(); I != E; ++I)
431 OS << "class " << *I->second;
433 OS << "------------- Defs -----------------\n";
434 const std::map<std::string, Record*> &Defs = RK.getDefs();
435 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
436 E = Defs.end(); I != E; ++I)
437 OS << "def " << *I->second;