Initial checkin of TableGen utility
[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 *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
210   BitsInit *BI = new BitsInit(Bits.size());
211
212   for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
213     if (Bits[i] >= 32) {
214       delete BI;
215       return 0;
216     }
217     BI->setBit(i, new BitInit(Value & (1 << Bits[i])));
218   }
219   return BI;
220 }
221
222 void ListInit::print(std::ostream &OS) const {
223   OS << "[";
224   for (unsigned i = 0, e = Records.size(); i != e; ++i) {
225     if (i) OS << ", ";
226     OS << Records[i]->getName();
227   }
228   OS << "]";
229 }
230
231 Init *VarInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
232   BitsRecTy *T = dynamic_cast<BitsRecTy*>(Ty);
233   if (T == 0) return 0;  // Cannot subscript a non-bits variable...
234   unsigned NumBits = T->getNumBits();
235
236   BitsInit *BI = new BitsInit(Bits.size());
237   for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
238     if (Bits[i] >= NumBits) {
239       delete BI;
240       return 0;
241     }
242     BI->setBit(i, new VarBitInit(this, Bits[i]));
243   }
244   return BI;
245 }
246
247 Init *BitsInit::resolveReferences(Record &R) {
248   bool Changed = false;
249   BitsInit *New = new BitsInit(getNumBits());
250
251   for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
252     Init *B;
253     New->setBit(i, getBit(i));
254     do {
255       B = New->getBit(i);
256       New->setBit(i, B->resolveReferences(R));
257       Changed |= B != New->getBit(i);
258     } while (B != New->getBit(i));
259   }
260
261   if (Changed)
262     return New;
263   delete New;
264   return this;
265 }
266
267
268
269 Init *VarBitInit::resolveReferences(Record &R) {
270   if (R.isTemplateArg(getVariable()->getName()))
271     return this;
272
273   RecordVal *RV = R.getValue(getVariable()->getName());
274   assert(RV && "Reference to a non-existant variable?");
275   assert(dynamic_cast<BitsInit*>(RV->getValue()));
276   BitsInit *BI = (BitsInit*)RV->getValue();
277   
278   assert(getBitNum() < BI->getNumBits() && "Bit reference out of range!");
279   Init *B = BI->getBit(getBitNum());
280
281   if (!dynamic_cast<UnsetInit*>(B))  // If the bit is not set...
282     return B;                        // Replace the VarBitInit with it.
283   return this;
284 }
285
286 void DefInit::print(std::ostream &OS) const {
287   OS << Def->getName();
288 }
289
290 //===----------------------------------------------------------------------===//
291 //    Other implementations
292 //===----------------------------------------------------------------------===//
293
294 RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
295   : Name(N), Ty(T), Prefix(P) {
296   Value = Ty->convertValue(new UnsetInit());
297   assert(Value && "Cannot create unset value for current type!");
298 }
299
300 void RecordVal::dump() const { std::cerr << *this; }
301
302 void RecordVal::print(std::ostream &OS, bool PrintSem) const {
303   if (getPrefix()) OS << "field ";
304   OS << *getType() << " " << getName();
305   if (getValue()) {
306     OS << " = " << *getValue();
307   }
308   if (PrintSem) OS << ";\n";
309 }
310
311 // resolveReferences - If there are any field references that refer to fields
312 // that have been filled in, we can propagate the values now.
313 //
314 void Record::resolveReferences() {
315   for (unsigned i = 0, e = Values.size(); i != e; ++i)
316     Values[i].setValue(Values[i].getValue()->resolveReferences(*this));
317 }
318
319 void Record::dump() const { std::cerr << *this; }
320
321 std::ostream &operator<<(std::ostream &OS, const Record &R) {
322   OS << R.getName();
323
324   const std::vector<std::string> &TArgs = R.getTemplateArgs();
325   if (!TArgs.empty()) {
326     OS << "<";
327     for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
328       if (i) OS << ", ";
329       const RecordVal *RV = R.getValue(TArgs[i]);
330       assert(RV && "Template argument record not found??");
331       RV->print(OS, false);
332     }
333     OS << ">";
334   }
335
336   OS << " {";
337   const std::vector<Record*> &SC = R.getSuperClasses();
338   if (!SC.empty()) {
339     OS << "\t//";
340     for (unsigned i = 0, e = SC.size(); i != e; ++i)
341       OS << " " << SC[i]->getName();
342   }
343   OS << "\n";
344
345   const std::vector<RecordVal> &Vals = R.getValues();
346   for (unsigned i = 0, e = Vals.size(); i != e; ++i)
347     if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
348       OS << Vals[i];
349   for (unsigned i = 0, e = Vals.size(); i != e; ++i)
350     if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
351       OS << Vals[i];
352
353   return OS << "}\n";
354 }
355
356 void RecordKeeper::dump() const { std::cerr << *this; }
357
358 std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK) {
359   OS << "------------- Classes -----------------\n";
360   const std::map<std::string, Record*> &Classes = RK.getClasses();
361   for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
362          E = Classes.end(); I != E; ++I)
363     OS << "class " << *I->second;
364   
365   OS << "------------- Defs -----------------\n";
366   const std::map<std::string, Record*> &Defs = RK.getDefs();
367   for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
368          E = Defs.end(); I != E; ++I)
369     OS << "def " << *I->second;
370   return OS;
371 }