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