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!
24 return new BitInit(Val != 0);
27 Init *BitRecTy::convertValue(TypedInit *VI) {
28 if (dynamic_cast<BitRecTy*>(VI->getType()))
29 return VI; // Accept variable if it is already of bit type!
33 Init *BitsRecTy::convertValue(UnsetInit *UI) {
34 BitsInit *Ret = new BitsInit(Size);
36 for (unsigned i = 0; i != Size; ++i)
37 Ret->setBit(i, new UnsetInit());
41 Init *BitsRecTy::convertValue(BitInit *UI) {
42 if (Size != 1) return 0; // Can only convert single bit...
43 BitsInit *Ret = new BitsInit(1);
48 // convertValue from Int initializer to bits type: Split the integer up into the
49 // appropriate bits...
51 Init *BitsRecTy::convertValue(IntInit *II) {
52 int Value = II->getValue();
55 BitsInit *Ret = new BitsInit(Size);
56 for (unsigned i = 0; i != Size; ++i)
57 Ret->setBit(i, new BitInit(Value & (1 << i)));
61 Init *BitsRecTy::convertValue(BitsInit *BI) {
62 // If the number of bits is right, return it. Otherwise we need to expand or
64 if (BI->getNumBits() == Size) return BI;
68 Init *BitsRecTy::convertValue(TypedInit *VI) {
69 if (BitsRecTy *BRT = dynamic_cast<BitsRecTy*>(VI->getType()))
70 if (BRT->Size == Size) {
71 BitsInit *Ret = new BitsInit(Size);
72 for (unsigned i = 0; i != Size; ++i)
73 Ret->setBit(i, new VarBitInit(VI, i));
76 if (Size == 1 && dynamic_cast<BitRecTy*>(VI->getType())) {
77 BitsInit *Ret = new BitsInit(1);
85 Init *IntRecTy::convertValue(BitsInit *BI) {
87 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
88 if (BitInit *Bit = dynamic_cast<BitInit*>(BI->getBit(i))) {
89 Result |= Bit->getValue() << i;
93 return new IntInit(Result);
96 Init *IntRecTy::convertValue(TypedInit *TI) {
97 if (dynamic_cast<IntRecTy*>(TI->getType()))
98 return TI; // Accept variable if already of the right type!
102 Init *StringRecTy::convertValue(TypedInit *TI) {
103 if (dynamic_cast<StringRecTy*>(TI->getType()))
104 return TI; // Accept variable if already of the right type!
108 void ListRecTy::print(std::ostream &OS) const {
109 OS << "list<" << Class->getName() << ">";
112 Init *ListRecTy::convertValue(ListInit *LI) {
113 // Verify that all of the elements of the list are subclasses of the
115 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
116 if (!LI->getElement(i)->isSubClassOf(Class))
121 void RecordRecTy::print(std::ostream &OS) const {
122 OS << Rec->getName();
125 Init *RecordRecTy::convertValue(DefInit *DI) {
126 // Ensure that DI is a subclass of Rec.
127 if (!DI->getDef()->isSubClassOf(Rec))
132 //===----------------------------------------------------------------------===//
133 // Initializer implementations
134 //===----------------------------------------------------------------------===//
136 void Init::dump() const { return print(std::cerr); }
138 Init *BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
139 BitsInit *BI = new BitsInit(Bits.size());
140 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
141 if (Bits[i] >= getNumBits()) {
145 BI->setBit(i, getBit(Bits[i]));
150 void BitsInit::print(std::ostream &OS) const {
151 //if (!printInHex(OS)) return;
152 //if (!printAsVariable(OS)) return;
153 //if (!printAsUnset(OS)) return;
156 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
158 getBit(e-i-1)->print(OS);
163 bool BitsInit::printInHex(std::ostream &OS) const {
164 // First, attempt to convert the value into an integer value...
166 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
167 if (BitInit *Bit = dynamic_cast<BitInit*>(getBit(i))) {
168 Result |= Bit->getValue() << i;
173 OS << "0x" << std::hex << Result << std::dec;
177 bool BitsInit::printAsVariable(std::ostream &OS) const {
178 // Get the variable that we may be set equal to...
179 assert(getNumBits() != 0);
180 VarBitInit *FirstBit = dynamic_cast<VarBitInit*>(getBit(0));
181 if (FirstBit == 0) return true;
182 TypedInit *Var = FirstBit->getVariable();
184 // Check to make sure the types are compatible.
185 BitsRecTy *Ty = dynamic_cast<BitsRecTy*>(FirstBit->getVariable()->getType());
186 if (Ty == 0) return true;
187 if (Ty->getNumBits() != getNumBits()) return true; // Incompatible types!
189 // Check to make sure all bits are referring to the right bits in the variable
190 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
191 VarBitInit *Bit = dynamic_cast<VarBitInit*>(getBit(i));
192 if (Bit == 0 || Bit->getVariable() != Var || Bit->getBitNum() != i)
200 bool BitsInit::printAsUnset(std::ostream &OS) const {
201 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
202 if (!dynamic_cast<UnsetInit*>(getBit(i)))
208 Init *BitsInit::resolveReferences(Record &R) {
209 bool Changed = false;
210 BitsInit *New = new BitsInit(getNumBits());
212 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
214 New->setBit(i, getBit(i));
217 New->setBit(i, B->resolveReferences(R));
218 Changed |= B != New->getBit(i);
219 } while (B != New->getBit(i));
228 Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
229 BitsInit *BI = new BitsInit(Bits.size());
231 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
236 BI->setBit(i, new BitInit(Value & (1 << Bits[i])));
241 void ListInit::print(std::ostream &OS) const {
243 for (unsigned i = 0, e = Records.size(); i != e; ++i) {
245 OS << Records[i]->getName();
250 Init *VarInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
251 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
252 if (T == 0) return 0; // Cannot subscript a non-bits variable...
253 unsigned NumBits = T->getNumBits();
255 BitsInit *BI = new BitsInit(Bits.size());
256 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
257 if (Bits[i] >= NumBits) {
261 BI->setBit(i, new VarBitInit(this, Bits[i]));
266 Init *VarInit::resolveBitReference(Record &R, unsigned Bit) {
267 if (R.isTemplateArg(getName()))
270 RecordVal *RV = R.getValue(getName());
271 assert(RV && "Reference to a non-existant variable?");
272 assert(dynamic_cast<BitsInit*>(RV->getValue()));
273 BitsInit *BI = (BitsInit*)RV->getValue();
275 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
276 Init *B = BI->getBit(Bit);
278 if (!dynamic_cast<UnsetInit*>(B)) // If the bit is not set...
279 return B; // Replace the VarBitInit with it.
283 RecTy *VarInit::getFieldType(const std::string &FieldName) const {
284 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
285 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
286 return RV->getType();
290 Init *VarInit::getFieldInit(Record &R, const std::string &FieldName) const {
291 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
292 if (const RecordVal *RV = R.getValue(VarName))
293 if (Init *I = RV->getValue()->getFieldInit(R, FieldName))
302 Init *VarBitInit::resolveReferences(Record &R) {
303 Init *I = getVariable()->resolveBitReference(R, getBitNum());
304 if (I != getVariable())
309 RecTy *DefInit::getFieldType(const std::string &FieldName) const {
310 if (const RecordVal *RV = Def->getValue(FieldName))
311 return RV->getType();
315 Init *DefInit::getFieldInit(Record &R, const std::string &FieldName) const {
316 return Def->getValue(FieldName)->getValue();
320 void DefInit::print(std::ostream &OS) const {
321 OS << Def->getName();
324 Init *FieldInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
325 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
326 if (T == 0) return 0; // Cannot subscript a non-bits field...
327 unsigned NumBits = T->getNumBits();
329 BitsInit *BI = new BitsInit(Bits.size());
330 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
331 if (Bits[i] >= NumBits) {
335 BI->setBit(i, new VarBitInit(this, Bits[i]));
340 Init *FieldInit::resolveBitReference(Record &R, unsigned Bit) {
341 Init *BitsVal = Rec->getFieldInit(R, FieldName);
342 assert(BitsVal && "No initializer found!");
344 if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
345 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
346 Init *B = BI->getBit(Bit);
348 if (dynamic_cast<BitInit*>(B)) // If the bit is set...
349 return B; // Replace the VarBitInit with it.
355 //===----------------------------------------------------------------------===//
356 // Other implementations
357 //===----------------------------------------------------------------------===//
359 RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
360 : Name(N), Ty(T), Prefix(P) {
361 Value = Ty->convertValue(new UnsetInit());
362 assert(Value && "Cannot create unset value for current type!");
365 void RecordVal::dump() const { std::cerr << *this; }
367 void RecordVal::print(std::ostream &OS, bool PrintSem) const {
368 if (getPrefix()) OS << "field ";
369 OS << *getType() << " " << getName();
371 OS << " = " << *getValue();
373 if (PrintSem) OS << ";\n";
376 // resolveReferences - If there are any field references that refer to fields
377 // that have been filled in, we can propagate the values now.
379 void Record::resolveReferences() {
380 for (unsigned i = 0, e = Values.size(); i != e; ++i)
381 Values[i].setValue(Values[i].getValue()->resolveReferences(*this));
384 void Record::dump() const { std::cerr << *this; }
386 std::ostream &operator<<(std::ostream &OS, const Record &R) {
389 const std::vector<std::string> &TArgs = R.getTemplateArgs();
390 if (!TArgs.empty()) {
392 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
394 const RecordVal *RV = R.getValue(TArgs[i]);
395 assert(RV && "Template argument record not found??");
396 RV->print(OS, false);
402 const std::vector<Record*> &SC = R.getSuperClasses();
405 for (unsigned i = 0, e = SC.size(); i != e; ++i)
406 OS << " " << SC[i]->getName();
410 const std::vector<RecordVal> &Vals = R.getValues();
411 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
412 if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
414 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
415 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
421 void RecordKeeper::dump() const { std::cerr << *this; }
423 std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK) {
424 OS << "------------- Classes -----------------\n";
425 const std::map<std::string, Record*> &Classes = RK.getClasses();
426 for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
427 E = Classes.end(); I != E; ++I)
428 OS << "class " << *I->second;
430 OS << "------------- Defs -----------------\n";
431 const std::map<std::string, Record*> &Defs = RK.getDefs();
432 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
433 E = Defs.end(); I != E; ++I)
434 OS << "def " << *I->second;