Tighten up assertion checking
[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   delete II;
23   
24   return new BitInit(Val != 0); 
25 }
26
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!
30   return 0;
31 }
32
33 Init *BitsRecTy::convertValue(UnsetInit *UI) {
34   BitsInit *Ret = new BitsInit(Size);
35
36   for (unsigned i = 0; i != Size; ++i)
37     Ret->setBit(i, new UnsetInit());
38   return Ret;
39 }
40
41 Init *BitsRecTy::convertValue(BitInit *UI) {
42   if (Size != 1) return 0;  // Can only convert single bit...
43   BitsInit *Ret = new BitsInit(1);
44   Ret->setBit(0, UI);
45   return Ret;
46 }
47
48 // convertValue from Int initializer to bits type: Split the integer up into the
49 // appropriate bits...
50 //
51 Init *BitsRecTy::convertValue(IntInit *II) {
52   int Value = II->getValue();
53   delete II;
54
55   BitsInit *Ret = new BitsInit(Size);
56   for (unsigned i = 0; i != Size; ++i)
57     Ret->setBit(i, new BitInit(Value & (1 << i)));
58   return Ret;
59 }
60
61 Init *BitsRecTy::convertValue(BitsInit *BI) {
62   // If the number of bits is right, return it.  Otherwise we need to expand or
63   // truncate...
64   if (BI->getNumBits() == Size) return BI;
65   return 0;
66 }
67
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));
74       return Ret;
75     }
76   if (Size == 1 && dynamic_cast<BitRecTy*>(VI->getType())) {
77     BitsInit *Ret = new BitsInit(1);
78     Ret->setBit(0, VI);
79     return Ret;
80   }
81       
82   return 0;
83 }
84
85 Init *IntRecTy::convertValue(BitsInit *BI) {
86   int Result = 0;
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;
90     } else {
91       return 0;
92     }
93   return new IntInit(Result);
94 }
95
96 Init *IntRecTy::convertValue(TypedInit *TI) {
97   if (dynamic_cast<IntRecTy*>(TI->getType()))
98     return TI;  // Accept variable if already of the right type!
99   return 0;
100 }
101
102 Init *StringRecTy::convertValue(TypedInit *TI) {
103   if (dynamic_cast<StringRecTy*>(TI->getType()))
104     return TI;  // Accept variable if already of the right type!
105   return 0;
106 }
107
108 void ListRecTy::print(std::ostream &OS) const {
109   OS << "list<" << Class->getName() << ">";
110 }
111
112 Init *ListRecTy::convertValue(ListInit *LI) {
113   // Verify that all of the elements of the list are subclasses of the
114   // appopriate class!
115   for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
116     if (!LI->getElement(i)->isSubClassOf(Class))
117       return 0;
118   return LI;
119 }
120
121 void RecordRecTy::print(std::ostream &OS) const {
122   OS << Rec->getName();
123 }
124
125 Init *RecordRecTy::convertValue(DefInit *DI) {
126   // Ensure that DI is a subclass of Rec.
127   if (!DI->getDef()->isSubClassOf(Rec))
128     return 0;
129   return DI;
130 }
131
132 //===----------------------------------------------------------------------===//
133 //    Initializer implementations
134 //===----------------------------------------------------------------------===//
135
136 void Init::dump() const { return print(std::cerr); }
137
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()) {
142       delete BI;
143       return 0;
144     }
145     BI->setBit(i, getBit(Bits[i]));
146   }
147   return BI;
148 }
149
150 void BitsInit::print(std::ostream &OS) const {
151   //if (!printInHex(OS)) return;
152   //if (!printAsVariable(OS)) return;
153   //if (!printAsUnset(OS)) return;
154
155   OS << "{ ";
156   for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
157     if (i) OS << ", ";
158     getBit(e-i-1)->print(OS);
159   }
160   OS << " }";
161 }
162
163 bool BitsInit::printInHex(std::ostream &OS) const {
164   // First, attempt to convert the value into an integer value...
165   int Result = 0;
166   for (unsigned i = 0, e = getNumBits(); i != e; ++i) 
167     if (BitInit *Bit = dynamic_cast<BitInit*>(getBit(i))) {
168       Result |= Bit->getValue() << i;
169     } else {
170       return true;
171     }
172
173   OS << "0x" << std::hex << Result << std::dec;
174   return false;
175 }
176
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();
183
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!
188
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)
193       return true;
194   }
195
196   Var->print(OS);
197   return false;
198 }
199
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)))
203       return true;
204   OS << "?";
205   return false;
206 }
207
208 Init *BitsInit::resolveReferences(Record &R) {
209   bool Changed = false;
210   BitsInit *New = new BitsInit(getNumBits());
211
212   for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
213     Init *B;
214     Init *CurBit = getBit(i);
215
216     do {
217       B = CurBit;
218       CurBit = CurBit->resolveReferences(R);
219       Changed |= B != CurBit;
220     } while (B != CurBit);
221     New->setBit(i, CurBit);
222   }
223
224   if (Changed)
225     return New;
226   delete New;
227   return this;
228 }
229
230 Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
231   BitsInit *BI = new BitsInit(Bits.size());
232
233   for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
234     if (Bits[i] >= 32) {
235       delete BI;
236       return 0;
237     }
238     BI->setBit(i, new BitInit(Value & (1 << Bits[i])));
239   }
240   return BI;
241 }
242
243 void ListInit::print(std::ostream &OS) const {
244   OS << "[";
245   for (unsigned i = 0, e = Records.size(); i != e; ++i) {
246     if (i) OS << ", ";
247     OS << Records[i]->getName();
248   }
249   OS << "]";
250 }
251
252 Init *VarInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
253   BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
254   if (T == 0) return 0;  // Cannot subscript a non-bits variable...
255   unsigned NumBits = T->getNumBits();
256
257   BitsInit *BI = new BitsInit(Bits.size());
258   for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
259     if (Bits[i] >= NumBits) {
260       delete BI;
261       return 0;
262     }
263     BI->setBit(i, new VarBitInit(this, Bits[i]));
264   }
265   return BI;
266 }
267
268 Init *VarInit::resolveBitReference(Record &R, unsigned Bit) {
269   if (R.isTemplateArg(getName()))
270     return this;
271
272   RecordVal *RV = R.getValue(getName());
273   assert(RV && "Reference to a non-existant variable?");
274   assert(dynamic_cast<BitsInit*>(RV->getValue()));
275   BitsInit *BI = (BitsInit*)RV->getValue();
276   
277   assert(Bit < BI->getNumBits() && "Bit reference out of range!");
278   Init *B = BI->getBit(Bit);
279
280   if (!dynamic_cast<UnsetInit*>(B))  // If the bit is not set...
281     return B;                        // Replace the VarBitInit with it.
282   return this;
283 }
284
285 RecTy *VarInit::getFieldType(const std::string &FieldName) const {
286   if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
287     if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
288       return RV->getType();
289   return 0;
290 }
291
292 Init *VarInit::getFieldInit(Record &R, const std::string &FieldName) const {
293   if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
294     if (const RecordVal *RV = R.getValue(VarName))
295       if (Init *I = RV->getValue()->getFieldInit(R, FieldName))
296         return I;
297       else
298         return (Init*)this;
299   return 0;
300 }
301
302
303
304 Init *VarBitInit::resolveReferences(Record &R) {
305   Init *I = getVariable()->resolveBitReference(R, getBitNum());
306   if (I != getVariable())
307     return I;
308   return this;
309 }
310
311 RecTy *DefInit::getFieldType(const std::string &FieldName) const {
312   if (const RecordVal *RV = Def->getValue(FieldName))
313     return RV->getType();
314   return 0;
315 }
316
317 Init *DefInit::getFieldInit(Record &R, const std::string &FieldName) const {
318   return Def->getValue(FieldName)->getValue();
319 }
320
321
322 void DefInit::print(std::ostream &OS) const {
323   OS << Def->getName();
324 }
325
326 Init *FieldInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
327   BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
328   if (T == 0) return 0;  // Cannot subscript a non-bits field...
329   unsigned NumBits = T->getNumBits();
330
331   BitsInit *BI = new BitsInit(Bits.size());
332   for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
333     if (Bits[i] >= NumBits) {
334       delete BI;
335       return 0;
336     }
337     BI->setBit(i, new VarBitInit(this, Bits[i]));
338   }
339   return BI;
340 }
341
342 Init *FieldInit::resolveBitReference(Record &R, unsigned Bit) {
343   Init *BitsVal = Rec->getFieldInit(R, FieldName);
344   assert(BitsVal && "No initializer found!");
345
346   if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
347     assert(Bit < BI->getNumBits() && "Bit reference out of range!");
348     Init *B = BI->getBit(Bit);
349     
350     if (dynamic_cast<BitInit*>(B))  // If the bit is set...
351       return B;                     // Replace the VarBitInit with it.
352   }
353   return this;
354 }
355
356
357 //===----------------------------------------------------------------------===//
358 //    Other implementations
359 //===----------------------------------------------------------------------===//
360
361 RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
362   : Name(N), Ty(T), Prefix(P) {
363   Value = Ty->convertValue(new UnsetInit());
364   assert(Value && "Cannot create unset value for current type!");
365 }
366
367 void RecordVal::dump() const { std::cerr << *this; }
368
369 void RecordVal::print(std::ostream &OS, bool PrintSem) const {
370   if (getPrefix()) OS << "field ";
371   OS << *getType() << " " << getName();
372   if (getValue()) {
373     OS << " = " << *getValue();
374   }
375   if (PrintSem) OS << ";\n";
376 }
377
378 // resolveReferences - If there are any field references that refer to fields
379 // that have been filled in, we can propagate the values now.
380 //
381 void Record::resolveReferences() {
382   for (unsigned i = 0, e = Values.size(); i != e; ++i)
383     Values[i].setValue(Values[i].getValue()->resolveReferences(*this));
384 }
385
386 void Record::dump() const { std::cerr << *this; }
387
388 std::ostream &operator<<(std::ostream &OS, const Record &R) {
389   OS << R.getName();
390
391   const std::vector<std::string> &TArgs = R.getTemplateArgs();
392   if (!TArgs.empty()) {
393     OS << "<";
394     for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
395       if (i) OS << ", ";
396       const RecordVal *RV = R.getValue(TArgs[i]);
397       assert(RV && "Template argument record not found??");
398       RV->print(OS, false);
399     }
400     OS << ">";
401   }
402
403   OS << " {";
404   const std::vector<Record*> &SC = R.getSuperClasses();
405   if (!SC.empty()) {
406     OS << "\t//";
407     for (unsigned i = 0, e = SC.size(); i != e; ++i)
408       OS << " " << SC[i]->getName();
409   }
410   OS << "\n";
411
412   const std::vector<RecordVal> &Vals = R.getValues();
413   for (unsigned i = 0, e = Vals.size(); i != e; ++i)
414     if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
415       OS << Vals[i];
416   for (unsigned i = 0, e = Vals.size(); i != e; ++i)
417     if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
418       OS << Vals[i];
419
420   return OS << "}\n";
421 }
422
423 void RecordKeeper::dump() const { std::cerr << *this; }
424
425 std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK) {
426   OS << "------------- Classes -----------------\n";
427   const std::map<std::string, Record*> &Classes = RK.getClasses();
428   for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
429          E = Classes.end(); I != E; ++I)
430     OS << "class " << *I->second;
431   
432   OS << "------------- Defs -----------------\n";
433   const std::map<std::string, Record*> &Defs = RK.getDefs();
434   for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
435          E = Defs.end(); I != E; ++I)
436     OS << "def " << *I->second;
437   return OS;
438 }