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