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