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(VarInit *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(VarInit *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);
86 Init *IntRecTy::convertValue(BitsInit *BI) {
88 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
89 if (BitInit *Bit = dynamic_cast<BitInit*>(BI->getBit(i))) {
90 Result |= Bit->getValue() << i;
94 return new IntInit(Result);
97 Init *IntRecTy::convertValue(VarInit *VI) {
98 if (dynamic_cast<IntRecTy*>(VI->getType()))
99 return VI; // Accept variable if already of the right type!
103 Init *StringRecTy::convertValue(VarInit *VI) {
104 if (dynamic_cast<StringRecTy*>(VI->getType()))
105 return VI; // Accept variable if already of the right type!
109 void ListRecTy::print(std::ostream &OS) const {
110 OS << "list<" << Class->getName() << ">";
113 Init *ListRecTy::convertValue(ListInit *LI) {
114 // Verify that all of the elements of the list are subclasses of the
116 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
117 if (!LI->getElement(i)->isSubClassOf(Class))
122 void RecordRecTy::print(std::ostream &OS) const {
123 OS << Rec->getName();
126 Init *RecordRecTy::convertValue(DefInit *DI) {
127 // Ensure that DI is a subclass of Rec.
128 if (!DI->getDef()->isSubClassOf(Rec))
133 //===----------------------------------------------------------------------===//
134 // Initializer implementations
135 //===----------------------------------------------------------------------===//
137 void Init::dump() const { return print(std::cerr); }
139 Init *BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
140 BitsInit *BI = new BitsInit(Bits.size());
141 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
142 if (Bits[i] >= getNumBits()) {
146 BI->setBit(i, getBit(Bits[i]));
151 void BitsInit::print(std::ostream &OS) const {
152 //if (!printInHex(OS)) return;
153 if (!printAsVariable(OS)) return;
154 if (!printAsUnset(OS)) return;
157 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
159 getBit(e-i-1)->print(OS);
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 VarInit *Var = FirstBit->getVariable();
185 // Check to make sure the types are compatible.
186 BitsRecTy *Ty = dynamic_cast<BitsRecTy*>(Var->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)
197 OS << Var->getName();
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 *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
210 BitsInit *BI = new BitsInit(Bits.size());
212 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
217 BI->setBit(i, new BitInit(Value & (1 << Bits[i])));
222 void ListInit::print(std::ostream &OS) const {
224 for (unsigned i = 0, e = Records.size(); i != e; ++i) {
226 OS << Records[i]->getName();
231 Init *VarInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
232 BitsRecTy *T = dynamic_cast<BitsRecTy*>(Ty);
233 if (T == 0) return 0; // Cannot subscript a non-bits variable...
234 unsigned NumBits = T->getNumBits();
236 BitsInit *BI = new BitsInit(Bits.size());
237 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
238 if (Bits[i] >= NumBits) {
242 BI->setBit(i, new VarBitInit(this, Bits[i]));
247 Init *BitsInit::resolveReferences(Record &R) {
248 bool Changed = false;
249 BitsInit *New = new BitsInit(getNumBits());
251 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
253 New->setBit(i, getBit(i));
256 New->setBit(i, B->resolveReferences(R));
257 Changed |= B != New->getBit(i);
258 } while (B != New->getBit(i));
269 Init *VarBitInit::resolveReferences(Record &R) {
270 if (R.isTemplateArg(getVariable()->getName()))
273 RecordVal *RV = R.getValue(getVariable()->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(getBitNum() < BI->getNumBits() && "Bit reference out of range!");
279 Init *B = BI->getBit(getBitNum());
281 if (!dynamic_cast<UnsetInit*>(B)) // If the bit is not set...
282 return B; // Replace the VarBitInit with it.
286 void DefInit::print(std::ostream &OS) const {
287 OS << Def->getName();
290 //===----------------------------------------------------------------------===//
291 // Other implementations
292 //===----------------------------------------------------------------------===//
294 RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
295 : Name(N), Ty(T), Prefix(P) {
296 Value = Ty->convertValue(new UnsetInit());
297 assert(Value && "Cannot create unset value for current type!");
300 void RecordVal::dump() const { std::cerr << *this; }
302 void RecordVal::print(std::ostream &OS, bool PrintSem) const {
303 if (getPrefix()) OS << "field ";
304 OS << *getType() << " " << getName();
306 OS << " = " << *getValue();
308 if (PrintSem) OS << ";\n";
311 // resolveReferences - If there are any field references that refer to fields
312 // that have been filled in, we can propagate the values now.
314 void Record::resolveReferences() {
315 for (unsigned i = 0, e = Values.size(); i != e; ++i)
316 Values[i].setValue(Values[i].getValue()->resolveReferences(*this));
319 void Record::dump() const { std::cerr << *this; }
321 std::ostream &operator<<(std::ostream &OS, const Record &R) {
324 const std::vector<std::string> &TArgs = R.getTemplateArgs();
325 if (!TArgs.empty()) {
327 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
329 const RecordVal *RV = R.getValue(TArgs[i]);
330 assert(RV && "Template argument record not found??");
331 RV->print(OS, false);
337 const std::vector<Record*> &SC = R.getSuperClasses();
340 for (unsigned i = 0, e = SC.size(); i != e; ++i)
341 OS << " " << SC[i]->getName();
345 const std::vector<RecordVal> &Vals = R.getValues();
346 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
347 if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
349 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
350 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
356 void RecordKeeper::dump() const { std::cerr << *this; }
358 std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK) {
359 OS << "------------- Classes -----------------\n";
360 const std::map<std::string, Record*> &Classes = RK.getClasses();
361 for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
362 E = Classes.end(); I != E; ++I)
363 OS << "class " << *I->second;
365 OS << "------------- Defs -----------------\n";
366 const std::map<std::string, Record*> &Defs = RK.getDefs();
367 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
368 E = Defs.end(); I != E; ++I)
369 OS << "def " << *I->second;