Allow initializing variable initializers with variables
[oota-llvm.git] / utils / 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 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())
135       return VI;
136   return 0;
137 }
138
139 //===----------------------------------------------------------------------===//
140 //    Initializer implementations
141 //===----------------------------------------------------------------------===//
142
143 void Init::dump() const { return print(std::cerr); }
144
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()) {
149       delete BI;
150       return 0;
151     }
152     BI->setBit(i, getBit(Bits[i]));
153   }
154   return BI;
155 }
156
157 void BitsInit::print(std::ostream &OS) const {
158   //if (!printInHex(OS)) return;
159   //if (!printAsVariable(OS)) return;
160   //if (!printAsUnset(OS)) return;
161
162   OS << "{ ";
163   for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
164     if (i) OS << ", ";
165     if (Init *Bit = getBit(e-i-1))
166       Bit->print(OS);
167     else
168       OS << "*";
169   }
170   OS << " }";
171 }
172
173 bool BitsInit::printInHex(std::ostream &OS) const {
174   // First, attempt to convert the value into an integer value...
175   int Result = 0;
176   for (unsigned i = 0, e = getNumBits(); i != e; ++i) 
177     if (BitInit *Bit = dynamic_cast<BitInit*>(getBit(i))) {
178       Result |= Bit->getValue() << i;
179     } else {
180       return true;
181     }
182
183   OS << "0x" << std::hex << Result << std::dec;
184   return false;
185 }
186
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();
193
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!
198
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)
203       return true;
204   }
205
206   Var->print(OS);
207   return false;
208 }
209
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)))
213       return true;
214   OS << "?";
215   return false;
216 }
217
218 Init *BitsInit::resolveReferences(Record &R) {
219   bool Changed = false;
220   BitsInit *New = new BitsInit(getNumBits());
221
222   for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
223     Init *B;
224     Init *CurBit = getBit(i);
225
226     do {
227       B = CurBit;
228       CurBit = CurBit->resolveReferences(R);
229       Changed |= B != CurBit;
230     } while (B != CurBit);
231     New->setBit(i, CurBit);
232   }
233
234   if (Changed)
235     return New;
236   delete New;
237   return this;
238 }
239
240 Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
241   BitsInit *BI = new BitsInit(Bits.size());
242
243   for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
244     if (Bits[i] >= 32) {
245       delete BI;
246       return 0;
247     }
248     BI->setBit(i, new BitInit(Value & (1 << Bits[i])));
249   }
250   return BI;
251 }
252
253 void ListInit::print(std::ostream &OS) const {
254   OS << "[";
255   for (unsigned i = 0, e = Records.size(); i != e; ++i) {
256     if (i) OS << ", ";
257     OS << Records[i]->getName();
258   }
259   OS << "]";
260 }
261
262 Init *VarInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
263   BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
264   if (T == 0) return 0;  // Cannot subscript a non-bits variable...
265   unsigned NumBits = T->getNumBits();
266
267   BitsInit *BI = new BitsInit(Bits.size());
268   for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
269     if (Bits[i] >= NumBits) {
270       delete BI;
271       return 0;
272     }
273     BI->setBit(i, new VarBitInit(this, Bits[i]));
274   }
275   return BI;
276 }
277
278 Init *VarInit::resolveBitReference(Record &R, unsigned Bit) {
279   if (R.isTemplateArg(getName()))
280     return this;
281
282   RecordVal *RV = R.getValue(getName());
283   assert(RV && "Reference to a non-existant variable?");
284   assert(dynamic_cast<BitsInit*>(RV->getValue()));
285   BitsInit *BI = (BitsInit*)RV->getValue();
286   
287   assert(Bit < BI->getNumBits() && "Bit reference out of range!");
288   Init *B = BI->getBit(Bit);
289
290   if (!dynamic_cast<UnsetInit*>(B))  // If the bit is not set...
291     return B;                        // Replace the VarBitInit with it.
292   return this;
293 }
294
295 RecTy *VarInit::getFieldType(const std::string &FieldName) const {
296   if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
297     if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
298       return RV->getType();
299   return 0;
300 }
301
302 Init *VarInit::getFieldInit(Record &R, const std::string &FieldName) const {
303   if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
304     if (const RecordVal *RV = R.getValue(VarName))
305       if (Init *I = RV->getValue()->getFieldInit(R, FieldName))
306         return I;
307       else
308         return (Init*)this;
309   return 0;
310 }
311
312
313
314 Init *VarBitInit::resolveReferences(Record &R) {
315   Init *I = getVariable()->resolveBitReference(R, getBitNum());
316   if (I != getVariable())
317     return I;
318   return this;
319 }
320
321 RecTy *DefInit::getFieldType(const std::string &FieldName) const {
322   if (const RecordVal *RV = Def->getValue(FieldName))
323     return RV->getType();
324   return 0;
325 }
326
327 Init *DefInit::getFieldInit(Record &R, const std::string &FieldName) const {
328   return Def->getValue(FieldName)->getValue();
329 }
330
331
332 void DefInit::print(std::ostream &OS) const {
333   OS << Def->getName();
334 }
335
336 Init *FieldInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
337   BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
338   if (T == 0) return 0;  // Cannot subscript a non-bits field...
339   unsigned NumBits = T->getNumBits();
340
341   BitsInit *BI = new BitsInit(Bits.size());
342   for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
343     if (Bits[i] >= NumBits) {
344       delete BI;
345       return 0;
346     }
347     BI->setBit(i, new VarBitInit(this, Bits[i]));
348   }
349   return BI;
350 }
351
352 Init *FieldInit::resolveBitReference(Record &R, unsigned Bit) {
353   Init *BitsVal = Rec->getFieldInit(R, FieldName);
354   assert(BitsVal && "No initializer found!");
355
356   if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
357     assert(Bit < BI->getNumBits() && "Bit reference out of range!");
358     Init *B = BI->getBit(Bit);
359     
360     if (dynamic_cast<BitInit*>(B))  // If the bit is set...
361       return B;                     // Replace the VarBitInit with it.
362   }
363   return this;
364 }
365
366
367 //===----------------------------------------------------------------------===//
368 //    Other implementations
369 //===----------------------------------------------------------------------===//
370
371 RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
372   : Name(N), Ty(T), Prefix(P) {
373   Value = Ty->convertValue(new UnsetInit());
374   assert(Value && "Cannot create unset value for current type!");
375 }
376
377 void RecordVal::dump() const { std::cerr << *this; }
378
379 void RecordVal::print(std::ostream &OS, bool PrintSem) const {
380   if (getPrefix()) OS << "field ";
381   OS << *getType() << " " << getName();
382   if (getValue()) {
383     OS << " = " << *getValue();
384   }
385   if (PrintSem) OS << ";\n";
386 }
387
388 // resolveReferences - If there are any field references that refer to fields
389 // that have been filled in, we can propagate the values now.
390 //
391 void Record::resolveReferences() {
392   for (unsigned i = 0, e = Values.size(); i != e; ++i)
393     Values[i].setValue(Values[i].getValue()->resolveReferences(*this));
394 }
395
396 void Record::dump() const { std::cerr << *this; }
397
398 std::ostream &operator<<(std::ostream &OS, const Record &R) {
399   OS << R.getName();
400
401   const std::vector<std::string> &TArgs = R.getTemplateArgs();
402   if (!TArgs.empty()) {
403     OS << "<";
404     for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
405       if (i) OS << ", ";
406       const RecordVal *RV = R.getValue(TArgs[i]);
407       assert(RV && "Template argument record not found??");
408       RV->print(OS, false);
409     }
410     OS << ">";
411   }
412
413   OS << " {";
414   const std::vector<Record*> &SC = R.getSuperClasses();
415   if (!SC.empty()) {
416     OS << "\t//";
417     for (unsigned i = 0, e = SC.size(); i != e; ++i)
418       OS << " " << SC[i]->getName();
419   }
420   OS << "\n";
421
422   const std::vector<RecordVal> &Vals = R.getValues();
423   for (unsigned i = 0, e = Vals.size(); i != e; ++i)
424     if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
425       OS << Vals[i];
426   for (unsigned i = 0, e = Vals.size(); i != e; ++i)
427     if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
428       OS << Vals[i];
429
430   return OS << "}\n";
431 }
432
433 void RecordKeeper::dump() const { std::cerr << *this; }
434
435 std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK) {
436   OS << "------------- Classes -----------------\n";
437   const std::map<std::string, Record*> &Classes = RK.getClasses();
438   for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
439          E = Classes.end(); I != E; ++I)
440     OS << "class " << *I->second;
441   
442   OS << "------------- Defs -----------------\n";
443   const std::map<std::string, Record*> &Defs = RK.getDefs();
444   for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
445          E = Defs.end(); I != E; ++I)
446     OS << "def " << *I->second;
447   return OS;
448 }