204929e0244c04d4e67df5acb171bdf01dc641d7
[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 bool BitRecTy::baseClassOf(const BitsRecTy *RHS) const {
20   return RHS->getNumBits() == 1;
21 }
22
23 Init *BitRecTy::convertValue(IntInit *II) {
24   int Val = II->getValue();
25   if (Val != 0 && Val != 1) return 0;  // Only accept 0 or 1 for a bit!
26   
27   return new BitInit(Val != 0); 
28 }
29
30 Init *BitRecTy::convertValue(TypedInit *VI) {
31   if (dynamic_cast<BitRecTy*>(VI->getType()))
32     return VI;  // Accept variable if it is already of bit type!
33   return 0;
34 }
35
36 Init *BitsRecTy::convertValue(UnsetInit *UI) {
37   BitsInit *Ret = new BitsInit(Size);
38
39   for (unsigned i = 0; i != Size; ++i)
40     Ret->setBit(i, new UnsetInit());
41   return Ret;
42 }
43
44 Init *BitsRecTy::convertValue(BitInit *UI) {
45   if (Size != 1) return 0;  // Can only convert single bit...
46   BitsInit *Ret = new BitsInit(1);
47   Ret->setBit(0, UI);
48   return Ret;
49 }
50
51 // convertValue from Int initializer to bits type: Split the integer up into the
52 // appropriate bits...
53 //
54 Init *BitsRecTy::convertValue(IntInit *II) {
55   int Value = II->getValue();
56   // Make sure this bitfield is large enough to hold the integer value...
57   if (Value >= 0) {
58     if (Value & ~((1 << Size)-1))
59       return 0;
60   } else {
61     if ((Value >> Size) != -1 || ((Value & (1 << Size-1)) == 0))
62       return 0;
63   }
64
65   BitsInit *Ret = new BitsInit(Size);
66   for (unsigned i = 0; i != Size; ++i)
67     Ret->setBit(i, new BitInit(Value & (1 << i)));
68
69   return Ret;
70 }
71
72 Init *BitsRecTy::convertValue(BitsInit *BI) {
73   // If the number of bits is right, return it.  Otherwise we need to expand or
74   // truncate...
75   if (BI->getNumBits() == Size) return BI;
76   return 0;
77 }
78
79 Init *BitsRecTy::convertValue(TypedInit *VI) {
80   if (BitsRecTy *BRT = dynamic_cast<BitsRecTy*>(VI->getType()))
81     if (BRT->Size == Size) {
82       BitsInit *Ret = new BitsInit(Size);
83       for (unsigned i = 0; i != Size; ++i)
84         Ret->setBit(i, new VarBitInit(VI, i));
85       return Ret;
86     }
87   if (Size == 1 && dynamic_cast<BitRecTy*>(VI->getType())) {
88     BitsInit *Ret = new BitsInit(1);
89     Ret->setBit(0, VI);
90     return Ret;
91   }
92       
93   return 0;
94 }
95
96 Init *IntRecTy::convertValue(BitsInit *BI) {
97   int Result = 0;
98   for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) 
99     if (BitInit *Bit = dynamic_cast<BitInit*>(BI->getBit(i))) {
100       Result |= Bit->getValue() << i;
101     } else {
102       return 0;
103     }
104   return new IntInit(Result);
105 }
106
107 Init *IntRecTy::convertValue(TypedInit *TI) {
108   if (dynamic_cast<IntRecTy*>(TI->getType()))
109     return TI;  // Accept variable if already of the right type!
110   return 0;
111 }
112
113 Init *StringRecTy::convertValue(TypedInit *TI) {
114   if (dynamic_cast<StringRecTy*>(TI->getType()))
115     return TI;  // Accept variable if already of the right type!
116   return 0;
117 }
118
119 void ListRecTy::print(std::ostream &OS) const {
120   OS << "list<" << *Ty << ">";
121 }
122
123 Init *ListRecTy::convertValue(ListInit *LI) {
124   std::vector<Init*> Elements;
125
126   // Verify that all of the elements of the list are subclasses of the
127   // appropriate class!
128   for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
129     if (Init *CI = LI->getElement(i)->convertInitializerTo(Ty))
130       Elements.push_back(CI);
131     else
132       return 0;
133
134   return new ListInit(Elements);
135 }
136
137 Init *ListRecTy::convertValue(TypedInit *TI) {
138   // Ensure that TI is compatible with our class.
139   if (ListRecTy *LRT = dynamic_cast<ListRecTy*>(TI->getType()))
140     if (LRT->getElementType()->typeIsConvertibleTo(getElementType()))
141       return TI;
142   return 0;
143 }
144
145 void RecordRecTy::print(std::ostream &OS) const {
146   OS << Rec->getName();
147 }
148
149 Init *RecordRecTy::convertValue(DefInit *DI) {
150   // Ensure that DI is a subclass of Rec.
151   if (!DI->getDef()->isSubClassOf(Rec))
152     return 0;
153   return DI;
154 }
155
156 Init *RecordRecTy::convertValue(TypedInit *TI) {
157   // Ensure that TI is compatible with Rec.
158   if (RecordRecTy *RRT = dynamic_cast<RecordRecTy*>(TI->getType()))
159     if (RRT->getRecord()->isSubClassOf(getRecord()) ||
160         RRT->getRecord() == getRecord())
161       return TI;
162   return 0;
163 }
164
165 bool RecordRecTy::baseClassOf(const RecordRecTy *RHS) const {
166   return Rec == RHS->getRecord() || RHS->getRecord()->isSubClassOf(Rec);
167 }
168
169
170 //===----------------------------------------------------------------------===//
171 //    Initializer implementations
172 //===----------------------------------------------------------------------===//
173
174 void Init::dump() const { return print(std::cerr); }
175
176 Init *BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
177   BitsInit *BI = new BitsInit(Bits.size());
178   for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
179     if (Bits[i] >= getNumBits()) {
180       delete BI;
181       return 0;
182     }
183     BI->setBit(i, getBit(Bits[i]));
184   }
185   return BI;
186 }
187
188 void BitsInit::print(std::ostream &OS) const {
189   //if (!printInHex(OS)) return;
190   //if (!printAsVariable(OS)) return;
191   //if (!printAsUnset(OS)) return;
192
193   OS << "{ ";
194   for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
195     if (i) OS << ", ";
196     if (Init *Bit = getBit(e-i-1))
197       Bit->print(OS);
198     else
199       OS << "*";
200   }
201   OS << " }";
202 }
203
204 bool BitsInit::printInHex(std::ostream &OS) const {
205   // First, attempt to convert the value into an integer value...
206   int Result = 0;
207   for (unsigned i = 0, e = getNumBits(); i != e; ++i) 
208     if (BitInit *Bit = dynamic_cast<BitInit*>(getBit(i))) {
209       Result |= Bit->getValue() << i;
210     } else {
211       return true;
212     }
213
214   OS << "0x" << std::hex << Result << std::dec;
215   return false;
216 }
217
218 bool BitsInit::printAsVariable(std::ostream &OS) const {
219   // Get the variable that we may be set equal to...
220   assert(getNumBits() != 0);
221   VarBitInit *FirstBit = dynamic_cast<VarBitInit*>(getBit(0));
222   if (FirstBit == 0) return true;
223   TypedInit *Var = FirstBit->getVariable();
224
225   // Check to make sure the types are compatible.
226   BitsRecTy *Ty = dynamic_cast<BitsRecTy*>(FirstBit->getVariable()->getType());
227   if (Ty == 0) return true;
228   if (Ty->getNumBits() != getNumBits()) return true; // Incompatible types!
229
230   // Check to make sure all bits are referring to the right bits in the variable
231   for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
232     VarBitInit *Bit = dynamic_cast<VarBitInit*>(getBit(i));
233     if (Bit == 0 || Bit->getVariable() != Var || Bit->getBitNum() != i)
234       return true;
235   }
236
237   Var->print(OS);
238   return false;
239 }
240
241 bool BitsInit::printAsUnset(std::ostream &OS) const {
242   for (unsigned i = 0, e = getNumBits(); i != e; ++i) 
243     if (!dynamic_cast<UnsetInit*>(getBit(i)))
244       return true;
245   OS << "?";
246   return false;
247 }
248
249 // resolveReferences - If there are any field references that refer to fields
250 // that have been filled in, we can propagate the values now.
251 //
252 Init *BitsInit::resolveReferences(Record &R) {
253   bool Changed = false;
254   BitsInit *New = new BitsInit(getNumBits());
255
256   for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
257     Init *B;
258     Init *CurBit = getBit(i);
259
260     do {
261       B = CurBit;
262       CurBit = CurBit->resolveReferences(R);
263       Changed |= B != CurBit;
264     } while (B != CurBit);
265     New->setBit(i, CurBit);
266   }
267
268   if (Changed)
269     return New;
270   delete New;
271   return this;
272 }
273
274 Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
275   BitsInit *BI = new BitsInit(Bits.size());
276
277   for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
278     if (Bits[i] >= 32) {
279       delete BI;
280       return 0;
281     }
282     BI->setBit(i, new BitInit(Value & (1 << Bits[i])));
283   }
284   return BI;
285 }
286
287 void ListInit::print(std::ostream &OS) const {
288   OS << "[";
289   for (unsigned i = 0, e = Values.size(); i != e; ++i) {
290     if (i) OS << ", ";
291     OS << *Values[i];
292   }
293   OS << "]";
294 }
295
296 Init *VarInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
297   BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
298   if (T == 0) return 0;  // Cannot subscript a non-bits variable...
299   unsigned NumBits = T->getNumBits();
300
301   BitsInit *BI = new BitsInit(Bits.size());
302   for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
303     if (Bits[i] >= NumBits) {
304       delete BI;
305       return 0;
306     }
307     BI->setBit(i, new VarBitInit(this, Bits[i]));
308   }
309   return BI;
310 }
311
312 Init *VarInit::resolveBitReference(Record &R, unsigned Bit) {
313   if (R.isTemplateArg(getName()))
314     return this;
315
316   RecordVal *RV = R.getValue(getName());
317   assert(RV && "Reference to a non-existant variable?");
318   assert(dynamic_cast<BitsInit*>(RV->getValue()));
319   BitsInit *BI = (BitsInit*)RV->getValue();
320   
321   assert(Bit < BI->getNumBits() && "Bit reference out of range!");
322   Init *B = BI->getBit(Bit);
323
324   if (!dynamic_cast<UnsetInit*>(B))  // If the bit is not set...
325     return B;                        // Replace the VarBitInit with it.
326   return this;
327 }
328
329 RecTy *VarInit::getFieldType(const std::string &FieldName) const {
330   if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
331     if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
332       return RV->getType();
333   return 0;
334 }
335
336 Init *VarInit::getFieldInit(Record &R, const std::string &FieldName) const {
337   if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
338     if (const RecordVal *RV = R.getValue(VarName))
339       if (Init *I = RV->getValue()->getFieldInit(R, FieldName))
340         return I;
341       else
342         return 0;
343   return 0;
344 }
345
346 /// resolveReferences - This method is used by classes that refer to other
347 /// variables which may not be defined at the time they expression is formed.
348 /// If a value is set for the variable later, this method will be called on
349 /// users of the value to allow the value to propagate out.
350 ///
351 Init *VarInit::resolveReferences(Record &R) {
352   if (RecordVal *Val = R.getValue(VarName))
353     if (!dynamic_cast<UnsetInit*>(Val->getValue()))
354       return Val->getValue();
355   return this;
356 }
357   
358
359 Init *VarBitInit::resolveReferences(Record &R) {
360   Init *I = getVariable()->resolveBitReference(R, getBitNum());
361   if (I != getVariable())
362     return I;
363   return this;
364 }
365
366 RecTy *DefInit::getFieldType(const std::string &FieldName) const {
367   if (const RecordVal *RV = Def->getValue(FieldName))
368     return RV->getType();
369   return 0;
370 }
371
372 Init *DefInit::getFieldInit(Record &R, const std::string &FieldName) const {
373   return Def->getValue(FieldName)->getValue();
374 }
375
376
377 void DefInit::print(std::ostream &OS) const {
378   OS << Def->getName();
379 }
380
381 Init *FieldInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
382   BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
383   if (T == 0) return 0;  // Cannot subscript a non-bits field...
384   unsigned NumBits = T->getNumBits();
385
386   BitsInit *BI = new BitsInit(Bits.size());
387   for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
388     if (Bits[i] >= NumBits) {
389       delete BI;
390       return 0;
391     }
392     BI->setBit(i, new VarBitInit(this, Bits[i]));
393   }
394   return BI;
395 }
396
397 Init *FieldInit::resolveBitReference(Record &R, unsigned Bit) {
398   Init *BitsVal = Rec->getFieldInit(R, FieldName);
399   if (BitsVal)
400     if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
401       assert(Bit < BI->getNumBits() && "Bit reference out of range!");
402       Init *B = BI->getBit(Bit);
403       
404       if (dynamic_cast<BitInit*>(B))  // If the bit is set...
405         return B;                     // Replace the VarBitInit with it.
406     }
407   return this;
408 }
409
410 Init *FieldInit::resolveReferences(Record &R) {
411   Init *BitsVal = Rec->getFieldInit(R, FieldName);
412   if (BitsVal) {
413     Init *BVR = BitsVal->resolveReferences(R);
414     return BVR->isComplete() ? BVR : this;
415   }
416   return this;
417 }
418
419
420 //===----------------------------------------------------------------------===//
421 //    Other implementations
422 //===----------------------------------------------------------------------===//
423
424 RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
425   : Name(N), Ty(T), Prefix(P) {
426   Value = Ty->convertValue(new UnsetInit());
427   assert(Value && "Cannot create unset value for current type!");
428 }
429
430 void RecordVal::dump() const { std::cerr << *this; }
431
432 void RecordVal::print(std::ostream &OS, bool PrintSem) const {
433   if (getPrefix()) OS << "field ";
434   OS << *getType() << " " << getName();
435   if (getValue()) {
436     OS << " = " << *getValue();
437   }
438   if (PrintSem) OS << ";\n";
439 }
440
441 // resolveReferences - If there are any field references that refer to fields
442 // that have been filled in, we can propagate the values now.
443 //
444 void Record::resolveReferences() {
445   for (unsigned i = 0, e = Values.size(); i != e; ++i)
446     Values[i].setValue(Values[i].getValue()->resolveReferences(*this));
447 }
448
449 void Record::dump() const { std::cerr << *this; }
450
451 std::ostream &operator<<(std::ostream &OS, const Record &R) {
452   OS << R.getName();
453
454   const std::vector<std::string> &TArgs = R.getTemplateArgs();
455   if (!TArgs.empty()) {
456     OS << "<";
457     for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
458       if (i) OS << ", ";
459       const RecordVal *RV = R.getValue(TArgs[i]);
460       assert(RV && "Template argument record not found??");
461       RV->print(OS, false);
462     }
463     OS << ">";
464   }
465
466   OS << " {";
467   const std::vector<Record*> &SC = R.getSuperClasses();
468   if (!SC.empty()) {
469     OS << "\t//";
470     for (unsigned i = 0, e = SC.size(); i != e; ++i)
471       OS << " " << SC[i]->getName();
472   }
473   OS << "\n";
474
475   const std::vector<RecordVal> &Vals = R.getValues();
476   for (unsigned i = 0, e = Vals.size(); i != e; ++i)
477     if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
478       OS << Vals[i];
479   for (unsigned i = 0, e = Vals.size(); i != e; ++i)
480     if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
481       OS << Vals[i];
482
483   return OS << "}\n";
484 }
485
486 /// getValueInit - Return the initializer for a value with the specified name,
487 /// or throw an exception if the field does not exist.
488 ///
489 Init *Record::getValueInit(const std::string &FieldName) const {
490   const RecordVal *R = getValue(FieldName);
491   if (R == 0 || R->getValue() == 0)
492     throw "Record '" + R->getName() + "' does not have a field named '" +
493       FieldName + "!\n";
494   return R->getValue();
495 }
496
497
498 /// getValueAsString - This method looks up the specified field and returns its
499 /// value as a string, throwing an exception if the field does not exist or if
500 /// the value is not a string.
501 ///
502 std::string Record::getValueAsString(const std::string &FieldName) const {
503   const RecordVal *R = getValue(FieldName);
504   if (R == 0 || R->getValue() == 0)
505     throw "Record '" + R->getName() + "' does not have a field named '" +
506           FieldName + "!\n";
507
508   if (const StringInit *SI = dynamic_cast<const StringInit*>(R->getValue()))
509     return SI->getValue();
510   throw "Record '" + R->getName() + "', field '" + FieldName +
511         "' does not have a string initializer!";
512 }
513
514 /// getValueAsBitsInit - This method looks up the specified field and returns
515 /// its value as a BitsInit, throwing an exception if the field does not exist
516 /// or if the value is not the right type.
517 ///
518 BitsInit *Record::getValueAsBitsInit(const std::string &FieldName) const {
519   const RecordVal *R = getValue(FieldName);
520   if (R == 0 || R->getValue() == 0)
521     throw "Record '" + R->getName() + "' does not have a field named '" +
522           FieldName + "!\n";
523
524   if (BitsInit *BI = dynamic_cast<BitsInit*>(R->getValue()))
525     return BI;
526   throw "Record '" + R->getName() + "', field '" + FieldName +
527         "' does not have a BitsInit initializer!";
528 }
529
530 /// getValueAsListInit - This method looks up the specified field and returns
531 /// its value as a ListInit, throwing an exception if the field does not exist
532 /// or if the value is not the right type.
533 ///
534 ListInit *Record::getValueAsListInit(const std::string &FieldName) const {
535   const RecordVal *R = getValue(FieldName);
536   if (R == 0 || R->getValue() == 0)
537     throw "Record '" + R->getName() + "' does not have a field named '" +
538           FieldName + "!\n";
539
540   if (ListInit *LI = dynamic_cast<ListInit*>(R->getValue()))
541     return LI;
542   throw "Record '" + R->getName() + "', field '" + FieldName +
543         "' does not have a list initializer!";
544 }
545
546 /// getValueAsInt - This method looks up the specified field and returns its
547 /// value as an int, throwing an exception if the field does not exist or if
548 /// the value is not the right type.
549 ///
550 int Record::getValueAsInt(const std::string &FieldName) const {
551   const RecordVal *R = getValue(FieldName);
552   if (R == 0 || R->getValue() == 0)
553     throw "Record '" + R->getName() + "' does not have a field named '" +
554           FieldName + "!\n";
555
556   if (IntInit *II = dynamic_cast<IntInit*>(R->getValue()))
557     return II->getValue();
558   throw "Record '" + R->getName() + "', field '" + FieldName +
559         "' does not have a list initializer!";
560 }
561
562 /// getValueAsDef - This method looks up the specified field and returns its
563 /// value as a Record, throwing an exception if the field does not exist or if
564 /// the value is not the right type.
565 ///
566 Record *Record::getValueAsDef(const std::string &FieldName) const {
567   const RecordVal *R = getValue(FieldName);
568   if (R == 0 || R->getValue() == 0)
569     throw "Record '" + R->getName() + "' does not have a field named '" +
570       FieldName + "!\n";
571
572   if (DefInit *DI = dynamic_cast<DefInit*>(R->getValue()))
573     return DI->getDef();
574   throw "Record '" + R->getName() + "', field '" + FieldName +
575         "' does not have a list initializer!";
576 }
577
578
579 void RecordKeeper::dump() const { std::cerr << *this; }
580
581 std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK) {
582   OS << "------------- Classes -----------------\n";
583   const std::map<std::string, Record*> &Classes = RK.getClasses();
584   for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
585          E = Classes.end(); I != E; ++I)
586     OS << "class " << *I->second;
587   
588   OS << "------------- Defs -----------------\n";
589   const std::map<std::string, Record*> &Defs = RK.getDefs();
590   for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
591          E = Defs.end(); I != E; ++I)
592     OS << "def " << *I->second;
593   return OS;
594 }
595
596
597 /// getAllDerivedDefinitions - This method returns all concrete definitions
598 /// that derive from the specified class name.  If a class with the specified
599 /// name does not exist, an error is printed and true is returned.
600 std::vector<Record*>
601 RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
602   Record *Class = Records.getClass(ClassName);
603   if (!Class)
604     throw "ERROR: Couldn't find the '" + ClassName + "' class!\n";
605
606   std::vector<Record*> Defs;
607   for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(),
608          E = getDefs().end(); I != E; ++I)
609     if (I->second->isSubClassOf(Class))
610       Defs.push_back(I->second);
611
612   return Defs;
613 }