64711bfaa4c6fd17e8e87640bc4b391dab749ad1
[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(VarInit *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(VarInit *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 *BitsRecTy::convertValue(FieldInit *VI) {
86   return 0;
87 }
88
89
90 Init *IntRecTy::convertValue(BitsInit *BI) {
91   int Result = 0;
92   for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) 
93     if (BitInit *Bit = dynamic_cast<BitInit*>(BI->getBit(i))) {
94       Result |= Bit->getValue() << i;
95     } else {
96       return 0;
97     }
98   return new IntInit(Result);
99 }
100
101 Init *IntRecTy::convertValue(VarInit *VI) {
102   if (dynamic_cast<IntRecTy*>(VI->getType()))
103     return VI;  // Accept variable if already of the right type!
104   return 0;
105 }
106
107 Init *StringRecTy::convertValue(VarInit *VI) {
108   if (dynamic_cast<StringRecTy*>(VI->getType()))
109     return VI;  // Accept variable if already of the right type!
110   return 0;
111 }
112
113 void ListRecTy::print(std::ostream &OS) const {
114   OS << "list<" << Class->getName() << ">";
115 }
116
117 Init *ListRecTy::convertValue(ListInit *LI) {
118   // Verify that all of the elements of the list are subclasses of the
119   // appopriate class!
120   for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
121     if (!LI->getElement(i)->isSubClassOf(Class))
122       return 0;
123   return LI;
124 }
125
126 void RecordRecTy::print(std::ostream &OS) const {
127   OS << Rec->getName();
128 }
129
130 Init *RecordRecTy::convertValue(DefInit *DI) {
131   // Ensure that DI is a subclass of Rec.
132   if (!DI->getDef()->isSubClassOf(Rec))
133     return 0;
134   return DI;
135 }
136
137 //===----------------------------------------------------------------------===//
138 //    Initializer implementations
139 //===----------------------------------------------------------------------===//
140
141 void Init::dump() const { return print(std::cerr); }
142
143 Init *BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
144   BitsInit *BI = new BitsInit(Bits.size());
145   for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
146     if (Bits[i] >= getNumBits()) {
147       delete BI;
148       return 0;
149     }
150     BI->setBit(i, getBit(Bits[i]));
151   }
152   return BI;
153 }
154
155 void BitsInit::print(std::ostream &OS) const {
156   //if (!printInHex(OS)) return;
157   if (!printAsVariable(OS)) return;
158   if (!printAsUnset(OS)) return;
159
160   OS << "{ ";
161   for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
162     if (i) OS << ", ";
163     getBit(e-i-1)->print(OS);
164   }
165   OS << " }";
166 }
167
168 bool BitsInit::printInHex(std::ostream &OS) const {
169   // First, attempt to convert the value into an integer value...
170   int Result = 0;
171   for (unsigned i = 0, e = getNumBits(); i != e; ++i) 
172     if (BitInit *Bit = dynamic_cast<BitInit*>(getBit(i))) {
173       Result |= Bit->getValue() << i;
174     } else {
175       return true;
176     }
177
178   OS << "0x" << std::hex << Result << std::dec;
179   return false;
180 }
181
182 bool BitsInit::printAsVariable(std::ostream &OS) const {
183   // Get the variable that we may be set equal to...
184   assert(getNumBits() != 0);
185   VarBitInit *FirstBit = dynamic_cast<VarBitInit*>(getBit(0));
186   if (FirstBit == 0) return true;
187   TypedInit *Var = FirstBit->getVariable();
188
189   // Check to make sure the types are compatible.
190   BitsRecTy *Ty = dynamic_cast<BitsRecTy*>(FirstBit->getVariable()->getType());
191   if (Ty == 0) return true;
192   if (Ty->getNumBits() != getNumBits()) return true; // Incompatible types!
193
194   // Check to make sure all bits are referring to the right bits in the variable
195   for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
196     VarBitInit *Bit = dynamic_cast<VarBitInit*>(getBit(i));
197     if (Bit == 0 || Bit->getVariable() != Var || Bit->getBitNum() != i)
198       return true;
199   }
200
201   Var->print(OS);
202   return false;
203 }
204
205 bool BitsInit::printAsUnset(std::ostream &OS) const {
206   for (unsigned i = 0, e = getNumBits(); i != e; ++i) 
207     if (!dynamic_cast<UnsetInit*>(getBit(i)))
208       return true;
209   OS << "?";
210   return false;
211 }
212
213 Init *BitsInit::resolveReferences(Record &R) {
214   bool Changed = false;
215   BitsInit *New = new BitsInit(getNumBits());
216
217   for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
218     Init *B;
219     New->setBit(i, getBit(i));
220     do {
221       B = New->getBit(i);
222       New->setBit(i, B->resolveReferences(R));
223       Changed |= B != New->getBit(i);
224     } while (B != New->getBit(i));
225   }
226
227   if (Changed)
228     return New;
229   delete New;
230   return this;
231 }
232
233 Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
234   BitsInit *BI = new BitsInit(Bits.size());
235
236   for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
237     if (Bits[i] >= 32) {
238       delete BI;
239       return 0;
240     }
241     BI->setBit(i, new BitInit(Value & (1 << Bits[i])));
242   }
243   return BI;
244 }
245
246 void ListInit::print(std::ostream &OS) const {
247   OS << "[";
248   for (unsigned i = 0, e = Records.size(); i != e; ++i) {
249     if (i) OS << ", ";
250     OS << Records[i]->getName();
251   }
252   OS << "]";
253 }
254
255 Init *VarInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
256   BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
257   if (T == 0) return 0;  // Cannot subscript a non-bits variable...
258   unsigned NumBits = T->getNumBits();
259
260   BitsInit *BI = new BitsInit(Bits.size());
261   for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
262     if (Bits[i] >= NumBits) {
263       delete BI;
264       return 0;
265     }
266     BI->setBit(i, new VarBitInit(this, Bits[i]));
267   }
268   return BI;
269 }
270
271 RecTy *VarInit::getFieldType(const std::string &FieldName) const {
272   if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
273     if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
274       return RV->getType();
275   return 0;
276 }
277
278 Init *VarInit::resolveBitReference(Record &R, unsigned Bit) {
279   if (R.isTemplateArg(getName()))
280     return this;
281
282   RecordVal *RV = R.getValue(getName());
283   assert(RV && "Reference to a non-existant variable?");
284   assert(dynamic_cast<BitsInit*>(RV->getValue()));
285   BitsInit *BI = (BitsInit*)RV->getValue();
286   
287   assert(Bit < BI->getNumBits() && "Bit reference out of range!");
288   Init *B = BI->getBit(Bit);
289
290   if (!dynamic_cast<UnsetInit*>(B))  // If the bit is not set...
291     return B;                        // Replace the VarBitInit with it.
292   return this;
293 }
294
295
296
297 Init *VarBitInit::resolveReferences(Record &R) {
298   Init *I = getVariable()->resolveBitReference(R, getBitNum());
299
300   if (I != getVariable())
301     return I;
302   return this;
303 }
304
305 void DefInit::print(std::ostream &OS) const {
306   OS << Def->getName();
307 }
308
309 Init *FieldInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
310   BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
311   if (T == 0) return 0;  // Cannot subscript a non-bits field...
312   unsigned NumBits = T->getNumBits();
313
314   BitsInit *BI = new BitsInit(Bits.size());
315   for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
316     if (Bits[i] >= NumBits) {
317       delete BI;
318       return 0;
319     }
320     BI->setBit(i, new VarBitInit(this, Bits[i]));
321   }
322   return BI;
323 }
324
325 Init *FieldInit::resolveBitReference(Record &R, unsigned Bit) {
326   // Can never be resolved yet.
327   return this;
328 }
329
330
331 //===----------------------------------------------------------------------===//
332 //    Other implementations
333 //===----------------------------------------------------------------------===//
334
335 RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
336   : Name(N), Ty(T), Prefix(P) {
337   Value = Ty->convertValue(new UnsetInit());
338   assert(Value && "Cannot create unset value for current type!");
339 }
340
341 void RecordVal::dump() const { std::cerr << *this; }
342
343 void RecordVal::print(std::ostream &OS, bool PrintSem) const {
344   if (getPrefix()) OS << "field ";
345   OS << *getType() << " " << getName();
346   if (getValue()) {
347     OS << " = " << *getValue();
348   }
349   if (PrintSem) OS << ";\n";
350 }
351
352 // resolveReferences - If there are any field references that refer to fields
353 // that have been filled in, we can propagate the values now.
354 //
355 void Record::resolveReferences() {
356   for (unsigned i = 0, e = Values.size(); i != e; ++i)
357     Values[i].setValue(Values[i].getValue()->resolveReferences(*this));
358 }
359
360 void Record::dump() const { std::cerr << *this; }
361
362 std::ostream &operator<<(std::ostream &OS, const Record &R) {
363   OS << R.getName();
364
365   const std::vector<std::string> &TArgs = R.getTemplateArgs();
366   if (!TArgs.empty()) {
367     OS << "<";
368     for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
369       if (i) OS << ", ";
370       const RecordVal *RV = R.getValue(TArgs[i]);
371       assert(RV && "Template argument record not found??");
372       RV->print(OS, false);
373     }
374     OS << ">";
375   }
376
377   OS << " {";
378   const std::vector<Record*> &SC = R.getSuperClasses();
379   if (!SC.empty()) {
380     OS << "\t//";
381     for (unsigned i = 0, e = SC.size(); i != e; ++i)
382       OS << " " << SC[i]->getName();
383   }
384   OS << "\n";
385
386   const std::vector<RecordVal> &Vals = R.getValues();
387   for (unsigned i = 0, e = Vals.size(); i != e; ++i)
388     if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
389       OS << Vals[i];
390   for (unsigned i = 0, e = Vals.size(); i != e; ++i)
391     if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
392       OS << Vals[i];
393
394   return OS << "}\n";
395 }
396
397 void RecordKeeper::dump() const { std::cerr << *this; }
398
399 std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK) {
400   OS << "------------- Classes -----------------\n";
401   const std::map<std::string, Record*> &Classes = RK.getClasses();
402   for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
403          E = Classes.end(); I != E; ++I)
404     OS << "class " << *I->second;
405   
406   OS << "------------- Defs -----------------\n";
407   const std::map<std::string, Record*> &Defs = RK.getDefs();
408   for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
409          E = Defs.end(); I != E; ++I)
410     OS << "def " << *I->second;
411   return OS;
412 }