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