Don't delete values that may still be referenced!
[oota-llvm.git] / support / tools / TableGen / Record.cpp
1 //===- Record.cpp - Record implementation ---------------------------------===//
2 //
3 //
4 //===----------------------------------------------------------------------===//
5
6 #include "Record.h"
7
8 //===----------------------------------------------------------------------===//
9 //    Type implementations
10 //===----------------------------------------------------------------------===//
11
12 void RecTy::dump() const { print(std::cerr); }
13
14 Init *BitRecTy::convertValue(BitsInit *BI) {
15   if (BI->getNumBits() != 1) return 0; // Only accept if just one bit!
16   return BI->getBit(0);
17 }
18
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!
22   
23   return new BitInit(Val != 0); 
24 }
25
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!
29   return 0;
30 }
31
32 Init *BitsRecTy::convertValue(UnsetInit *UI) {
33   BitsInit *Ret = new BitsInit(Size);
34
35   for (unsigned i = 0; i != Size; ++i)
36     Ret->setBit(i, new UnsetInit());
37   return Ret;
38 }
39
40 Init *BitsRecTy::convertValue(BitInit *UI) {
41   if (Size != 1) return 0;  // Can only convert single bit...
42   BitsInit *Ret = new BitsInit(1);
43   Ret->setBit(0, UI);
44   return Ret;
45 }
46
47 // convertValue from Int initializer to bits type: Split the integer up into the
48 // appropriate bits...
49 //
50 Init *BitsRecTy::convertValue(IntInit *II) {
51   int Value = II->getValue();
52
53   BitsInit *Ret = new BitsInit(Size);
54   for (unsigned i = 0; i != Size; ++i)
55     Ret->setBit(i, new BitInit(Value & (1 << i)));
56   return Ret;
57 }
58
59 Init *BitsRecTy::convertValue(BitsInit *BI) {
60   // If the number of bits is right, return it.  Otherwise we need to expand or
61   // truncate...
62   if (BI->getNumBits() == Size) return BI;
63   return 0;
64 }
65
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));
72       return Ret;
73     }
74   if (Size == 1 && dynamic_cast<BitRecTy*>(VI->getType())) {
75     BitsInit *Ret = new BitsInit(1);
76     Ret->setBit(0, VI);
77     return Ret;
78   }
79       
80   return 0;
81 }
82
83 Init *IntRecTy::convertValue(BitsInit *BI) {
84   int Result = 0;
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;
88     } else {
89       return 0;
90     }
91   return new IntInit(Result);
92 }
93
94 Init *IntRecTy::convertValue(TypedInit *TI) {
95   if (dynamic_cast<IntRecTy*>(TI->getType()))
96     return TI;  // Accept variable if already of the right type!
97   return 0;
98 }
99
100 Init *StringRecTy::convertValue(TypedInit *TI) {
101   if (dynamic_cast<StringRecTy*>(TI->getType()))
102     return TI;  // Accept variable if already of the right type!
103   return 0;
104 }
105
106 void ListRecTy::print(std::ostream &OS) const {
107   OS << "list<" << Class->getName() << ">";
108 }
109
110 Init *ListRecTy::convertValue(ListInit *LI) {
111   // Verify that all of the elements of the list are subclasses of the
112   // appopriate class!
113   for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
114     if (!LI->getElement(i)->isSubClassOf(Class))
115       return 0;
116   return LI;
117 }
118
119 void RecordRecTy::print(std::ostream &OS) const {
120   OS << Rec->getName();
121 }
122
123 Init *RecordRecTy::convertValue(DefInit *DI) {
124   // Ensure that DI is a subclass of Rec.
125   if (!DI->getDef()->isSubClassOf(Rec))
126     return 0;
127   return DI;
128 }
129
130 //===----------------------------------------------------------------------===//
131 //    Initializer implementations
132 //===----------------------------------------------------------------------===//
133
134 void Init::dump() const { return print(std::cerr); }
135
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()) {
140       delete BI;
141       return 0;
142     }
143     BI->setBit(i, getBit(Bits[i]));
144   }
145   return BI;
146 }
147
148 void BitsInit::print(std::ostream &OS) const {
149   //if (!printInHex(OS)) return;
150   //if (!printAsVariable(OS)) return;
151   //if (!printAsUnset(OS)) return;
152
153   OS << "{ ";
154   for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
155     if (i) OS << ", ";
156     getBit(e-i-1)->print(OS);
157   }
158   OS << " }";
159 }
160
161 bool BitsInit::printInHex(std::ostream &OS) const {
162   // First, attempt to convert the value into an integer value...
163   int Result = 0;
164   for (unsigned i = 0, e = getNumBits(); i != e; ++i) 
165     if (BitInit *Bit = dynamic_cast<BitInit*>(getBit(i))) {
166       Result |= Bit->getValue() << i;
167     } else {
168       return true;
169     }
170
171   OS << "0x" << std::hex << Result << std::dec;
172   return false;
173 }
174
175 bool BitsInit::printAsVariable(std::ostream &OS) const {
176   // Get the variable that we may be set equal to...
177   assert(getNumBits() != 0);
178   VarBitInit *FirstBit = dynamic_cast<VarBitInit*>(getBit(0));
179   if (FirstBit == 0) return true;
180   TypedInit *Var = FirstBit->getVariable();
181
182   // Check to make sure the types are compatible.
183   BitsRecTy *Ty = dynamic_cast<BitsRecTy*>(FirstBit->getVariable()->getType());
184   if (Ty == 0) return true;
185   if (Ty->getNumBits() != getNumBits()) return true; // Incompatible types!
186
187   // Check to make sure all bits are referring to the right bits in the variable
188   for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
189     VarBitInit *Bit = dynamic_cast<VarBitInit*>(getBit(i));
190     if (Bit == 0 || Bit->getVariable() != Var || Bit->getBitNum() != i)
191       return true;
192   }
193
194   Var->print(OS);
195   return false;
196 }
197
198 bool BitsInit::printAsUnset(std::ostream &OS) const {
199   for (unsigned i = 0, e = getNumBits(); i != e; ++i) 
200     if (!dynamic_cast<UnsetInit*>(getBit(i)))
201       return true;
202   OS << "?";
203   return false;
204 }
205
206 Init *BitsInit::resolveReferences(Record &R) {
207   bool Changed = false;
208   BitsInit *New = new BitsInit(getNumBits());
209
210   for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
211     Init *B;
212     Init *CurBit = getBit(i);
213
214     do {
215       B = CurBit;
216       CurBit = CurBit->resolveReferences(R);
217       Changed |= B != CurBit;
218     } while (B != CurBit);
219     New->setBit(i, CurBit);
220   }
221
222   if (Changed)
223     return New;
224   delete New;
225   return this;
226 }
227
228 Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
229   BitsInit *BI = new BitsInit(Bits.size());
230
231   for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
232     if (Bits[i] >= 32) {
233       delete BI;
234       return 0;
235     }
236     BI->setBit(i, new BitInit(Value & (1 << Bits[i])));
237   }
238   return BI;
239 }
240
241 void ListInit::print(std::ostream &OS) const {
242   OS << "[";
243   for (unsigned i = 0, e = Records.size(); i != e; ++i) {
244     if (i) OS << ", ";
245     OS << Records[i]->getName();
246   }
247   OS << "]";
248 }
249
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();
254
255   BitsInit *BI = new BitsInit(Bits.size());
256   for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
257     if (Bits[i] >= NumBits) {
258       delete BI;
259       return 0;
260     }
261     BI->setBit(i, new VarBitInit(this, Bits[i]));
262   }
263   return BI;
264 }
265
266 Init *VarInit::resolveBitReference(Record &R, unsigned Bit) {
267   if (R.isTemplateArg(getName()))
268     return this;
269
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();
274   
275   assert(Bit < BI->getNumBits() && "Bit reference out of range!");
276   Init *B = BI->getBit(Bit);
277
278   if (!dynamic_cast<UnsetInit*>(B))  // If the bit is not set...
279     return B;                        // Replace the VarBitInit with it.
280   return this;
281 }
282
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();
287   return 0;
288 }
289
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))
294         return I;
295       else
296         return (Init*)this;
297   return 0;
298 }
299
300
301
302 Init *VarBitInit::resolveReferences(Record &R) {
303   Init *I = getVariable()->resolveBitReference(R, getBitNum());
304   if (I != getVariable())
305     return I;
306   return this;
307 }
308
309 RecTy *DefInit::getFieldType(const std::string &FieldName) const {
310   if (const RecordVal *RV = Def->getValue(FieldName))
311     return RV->getType();
312   return 0;
313 }
314
315 Init *DefInit::getFieldInit(Record &R, const std::string &FieldName) const {
316   return Def->getValue(FieldName)->getValue();
317 }
318
319
320 void DefInit::print(std::ostream &OS) const {
321   OS << Def->getName();
322 }
323
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();
328
329   BitsInit *BI = new BitsInit(Bits.size());
330   for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
331     if (Bits[i] >= NumBits) {
332       delete BI;
333       return 0;
334     }
335     BI->setBit(i, new VarBitInit(this, Bits[i]));
336   }
337   return BI;
338 }
339
340 Init *FieldInit::resolveBitReference(Record &R, unsigned Bit) {
341   Init *BitsVal = Rec->getFieldInit(R, FieldName);
342   assert(BitsVal && "No initializer found!");
343
344   if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
345     assert(Bit < BI->getNumBits() && "Bit reference out of range!");
346     Init *B = BI->getBit(Bit);
347     
348     if (dynamic_cast<BitInit*>(B))  // If the bit is set...
349       return B;                     // Replace the VarBitInit with it.
350   }
351   return this;
352 }
353
354
355 //===----------------------------------------------------------------------===//
356 //    Other implementations
357 //===----------------------------------------------------------------------===//
358
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!");
363 }
364
365 void RecordVal::dump() const { std::cerr << *this; }
366
367 void RecordVal::print(std::ostream &OS, bool PrintSem) const {
368   if (getPrefix()) OS << "field ";
369   OS << *getType() << " " << getName();
370   if (getValue()) {
371     OS << " = " << *getValue();
372   }
373   if (PrintSem) OS << ";\n";
374 }
375
376 // resolveReferences - If there are any field references that refer to fields
377 // that have been filled in, we can propagate the values now.
378 //
379 void Record::resolveReferences() {
380   for (unsigned i = 0, e = Values.size(); i != e; ++i)
381     Values[i].setValue(Values[i].getValue()->resolveReferences(*this));
382 }
383
384 void Record::dump() const { std::cerr << *this; }
385
386 std::ostream &operator<<(std::ostream &OS, const Record &R) {
387   OS << R.getName();
388
389   const std::vector<std::string> &TArgs = R.getTemplateArgs();
390   if (!TArgs.empty()) {
391     OS << "<";
392     for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
393       if (i) OS << ", ";
394       const RecordVal *RV = R.getValue(TArgs[i]);
395       assert(RV && "Template argument record not found??");
396       RV->print(OS, false);
397     }
398     OS << ">";
399   }
400
401   OS << " {";
402   const std::vector<Record*> &SC = R.getSuperClasses();
403   if (!SC.empty()) {
404     OS << "\t//";
405     for (unsigned i = 0, e = SC.size(); i != e; ++i)
406       OS << " " << SC[i]->getName();
407   }
408   OS << "\n";
409
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()))
413       OS << Vals[i];
414   for (unsigned i = 0, e = Vals.size(); i != e; ++i)
415     if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
416       OS << Vals[i];
417
418   return OS << "}\n";
419 }
420
421 void RecordKeeper::dump() const { std::cerr << *this; }
422
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;
429   
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;
435   return OS;
436 }