Continue implementing field initializers
[oota-llvm.git] / utils / TableGen / Record.h
1 //===- Record.h - Classes to represent Table Records ------------*- C++ -*-===//
2 //
3 //
4 //===----------------------------------------------------------------------===//
5
6 #ifndef RECORD_H
7 #define RECORD_H
8
9 #include <string>
10 #include <vector>
11 #include <map>
12 #include <iostream>
13 class Init;
14 class UnsetInit;
15 class BitInit;
16 class BitsInit;
17 class IntInit;
18 class StringInit;
19 class ListInit;
20 class DefInit;
21 class TypedInit;
22 class VarInit;
23 class FieldInit;
24 class VarBitInit;
25 class Record;
26
27 //===----------------------------------------------------------------------===//
28 //  Type Classes
29 //===----------------------------------------------------------------------===//
30
31 struct RecTy {
32   virtual ~RecTy() {}
33
34   virtual Init *convertValue( UnsetInit *UI) { return 0; }
35   virtual Init *convertValue(   BitInit *BI) { return 0; }
36   virtual Init *convertValue(  BitsInit *BI) { return 0; }
37   virtual Init *convertValue(   IntInit *II) { return 0; }
38   virtual Init *convertValue(StringInit *SI) { return 0; }
39   virtual Init *convertValue(  ListInit *LI) { return 0; }
40   virtual Init *convertValue(VarBitInit *VB) { return 0; }
41   virtual Init *convertValue(   DefInit *DI) { return 0; }
42   virtual Init *convertValue( TypedInit *TI) { return 0; }
43   virtual Init *convertValue(   VarInit *VI) {
44     return convertValue((TypedInit*)VI);
45   }
46   virtual Init *convertValue( FieldInit *FI) {
47     return convertValue((TypedInit*)FI);
48   }
49
50   virtual void print(std::ostream &OS) const = 0;
51   void dump() const;
52 };
53
54 inline std::ostream &operator<<(std::ostream &OS, const RecTy &Ty) {
55   Ty.print(OS);
56   return OS;
57 }
58
59
60 /// BitRecTy - 'bit' - Represent a single bit
61 ///
62 struct BitRecTy : public RecTy {
63   Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
64   Init *convertValue(BitInit *BI) { return (Init*)BI; }
65   Init *convertValue(BitsInit *BI);
66   Init *convertValue(IntInit *II);
67   Init *convertValue(TypedInit *VI);
68   Init *convertValue(VarBitInit *VB) { return (Init*)VB; }
69
70   void print(std::ostream &OS) const { OS << "bit"; }
71 };
72
73
74 /// BitsRecTy - 'bits<n>' - Represent a fixed number of bits
75 ///
76 class BitsRecTy : public RecTy {
77   unsigned Size;
78 public:
79   BitsRecTy(unsigned Sz) : Size(Sz) {}
80
81   unsigned getNumBits() const { return Size; }
82
83   Init *convertValue(UnsetInit *UI);
84   Init *convertValue(BitInit *UI);
85   Init *convertValue(BitsInit *BI);
86   Init *convertValue(IntInit *II);
87   Init *convertValue(TypedInit *VI);
88
89   void print(std::ostream &OS) const { OS << "bits<" << Size << ">"; }
90 };
91
92
93 /// IntRecTy - 'int' - Represent an integer value of no particular size
94 ///
95 struct IntRecTy : public RecTy {
96   Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
97   Init *convertValue(IntInit *II) { return (Init*)II; }
98   Init *convertValue(BitsInit *BI);
99   Init *convertValue(TypedInit *TI);
100
101   void print(std::ostream &OS) const { OS << "int"; }
102 };
103
104 /// StringRecTy - 'string' - Represent an string value
105 ///
106 struct StringRecTy : public RecTy {
107   Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
108   Init *convertValue(StringInit *SI) { return (Init*)SI; }
109   Init *convertValue(TypedInit *VI);
110   void print(std::ostream &OS) const { OS << "string"; }
111 };
112
113 /// ListRecTy - 'list<class>' - Represent a list defs, all of which must be
114 /// derived from the specified class.
115 ///
116 class ListRecTy : public RecTy {
117   Record *Class;
118 public:
119   ListRecTy(Record *C) : Class(C) {}
120   Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
121   Init *convertValue(ListInit *LI);
122   
123   void print(std::ostream &OS) const;
124 };
125
126 /// RecordRecTy - '<classname>' - Represent an instance of a class, such as:
127 /// (R32 X = EAX).
128 ///
129 class RecordRecTy : public RecTy {
130   Record *Rec;
131 public:
132   RecordRecTy(Record *R) : Rec(R) {}
133
134   Record *getRecord() const { return Rec; }
135
136   Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
137   Init *convertValue(   DefInit *DI);
138
139   void print(std::ostream &OS) const;
140 };
141
142
143
144 //===----------------------------------------------------------------------===//
145 //  Initializer Classes
146 //===----------------------------------------------------------------------===//
147
148 struct Init {
149   virtual ~Init() {}
150
151   /// isComplete - This virtual method should be overridden by values that may
152   /// not be completely specified yet.
153   virtual bool isComplete() const { return true; }
154
155   /// print - Print out this value.
156   virtual void print(std::ostream &OS) const = 0;
157
158   /// dump - Debugging method that may be called through a debugger, just
159   /// invokes print on cerr.
160   void dump() const;
161
162   /// convertInitializerTo - This virtual function is a simple call-back
163   /// function that should be overridden to call the appropriate
164   /// RecTy::convertValue method.
165   ///
166   virtual Init *convertInitializerTo(RecTy *Ty) = 0;
167
168   /// convertInitializerBitRange - This method is used to implement the bitrange
169   /// selection operator.  Given an initializer, it selects the specified bits
170   /// out, returning them as a new init of bits type.  If it is not legal to use
171   /// the bit subscript operator on this initializer, return null.
172   ///
173   virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits) {
174     return 0;
175   }
176
177   /// getFieldType - This method is used to implement the FieldInit class.
178   /// Implementors of this method should return the type of the named field if
179   /// they are of record type.
180   ///
181   virtual RecTy *getFieldType(const std::string &FieldName) const { return 0; }
182
183   /// getFieldInit - This method complements getFieldType to return the
184   /// initializer for the specified field.  If getFieldType returns non-null
185   /// this method should return non-null, otherwise it returns null.
186   ///
187   virtual Init *getFieldInit(Record &R, const std::string &FieldName) const {
188     return 0;
189   }
190
191   /// resolveReferences - This method is used by classes that refer to other
192   /// variables which may not be defined at the time they expression is formed.
193   /// If a value is set for the variable later, this method will be called on
194   /// users of the value to allow the value to propagate out.
195   ///
196   virtual Init *resolveReferences(Record &R) { return this; }
197 };
198
199 inline std::ostream &operator<<(std::ostream &OS, const Init &I) {
200   I.print(OS); return OS;
201 }
202
203
204 /// UnsetInit - ? - Represents an uninitialized value
205 ///
206 struct UnsetInit : public Init {
207   virtual Init *convertInitializerTo(RecTy *Ty) {
208     return Ty->convertValue(this);
209   }
210
211   virtual bool isComplete() const { return false; }
212   virtual void print(std::ostream &OS) const { OS << "?"; }
213 };
214
215
216 /// BitInit - true/false - Represent a concrete initializer for a bit.
217 ///
218 class BitInit : public Init {
219   bool Value;
220 public:
221   BitInit(bool V) : Value(V) {}
222
223   bool getValue() const { return Value; }
224
225   virtual Init *convertInitializerTo(RecTy *Ty) {
226     return Ty->convertValue(this);
227   }
228
229   virtual void print(std::ostream &OS) const { OS << (Value ? "1" : "0"); }
230 };
231
232 /// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
233 /// It contains a vector of bits, whose size is determined by the type.
234 ///
235 class BitsInit : public Init {
236   std::vector<Init*> Bits;
237 public:
238   BitsInit(unsigned Size) : Bits(Size) {}
239
240   unsigned getNumBits() const { return Bits.size(); }
241
242   Init *getBit(unsigned Bit) const {
243     assert(Bit < Bits.size() && "Bit index out of range!");
244     return Bits[Bit];
245   }
246   void setBit(unsigned Bit, Init *V) {
247     assert(Bit < Bits.size() && "Bit index out of range!");
248     Bits[Bit] = V;
249   }
250
251   virtual Init *convertInitializerTo(RecTy *Ty) {
252     return Ty->convertValue(this);
253   }
254   virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
255
256   virtual bool isComplete() const {
257     for (unsigned i = 0; i != getNumBits(); ++i)
258       if (!getBit(i)->isComplete()) return false;
259     return true;
260   }
261   virtual void print(std::ostream &OS) const;
262
263   virtual Init *resolveReferences(Record &R);
264
265   // printXX - Print this bitstream with the specified format, returning true if
266   // it is not possible.
267   bool printInHex(std::ostream &OS) const;
268   bool printAsVariable(std::ostream &OS) const;
269   bool printAsUnset(std::ostream &OS) const;
270 };
271
272
273 /// IntInit - 7 - Represent an initalization by a literal integer value.
274 ///
275 class IntInit : public Init {
276   int Value;
277 public:
278   IntInit(int V) : Value(V) {}
279
280   int getValue() const { return Value; }
281
282   virtual Init *convertInitializerTo(RecTy *Ty) {
283     return Ty->convertValue(this);
284   }
285   virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
286
287   virtual void print(std::ostream &OS) const { OS << Value; }
288 };
289
290
291 /// StringInit - "foo" - Represent an initialization by a string value.
292 ///
293 class StringInit : public Init {
294   std::string Value;
295 public:
296   StringInit(const std::string &V) : Value(V) {}
297
298   virtual Init *convertInitializerTo(RecTy *Ty) {
299     return Ty->convertValue(this);
300   }
301
302   virtual void print(std::ostream &OS) const { OS << "\"" << Value << "\""; }
303 };
304
305 /// ListInit - [AL, AH, CL] - Represent a list of defs
306 ///
307 class ListInit : public Init {
308   std::vector<Record*> Records;
309 public:
310   ListInit(std::vector<Record*> &Rs) {
311     Records.swap(Rs);
312   }
313
314   unsigned getSize() const { return Records.size(); }
315   Record  *getElement(unsigned i) const {
316     assert(i < Records.size() && "List element index out of range!");
317     return Records[i];
318   }
319
320   virtual Init *convertInitializerTo(RecTy *Ty) {
321     return Ty->convertValue(this);
322   }
323
324   virtual void print(std::ostream &OS) const;
325 };
326
327
328 /// TypedInit - This is the common super-class of types that have a specific,
329 /// explicit, type.
330 ///
331 class TypedInit : public Init {
332   RecTy *Ty;
333 public:  
334   TypedInit(RecTy *T) : Ty(T) {}
335
336   RecTy *getType() const { return Ty; }
337
338   /// resolveBitReference - This method is used to implement
339   /// VarBitInit::resolveReferences.  If the bit is able to be resolved, we
340   /// simply return the resolved value, otherwise we return this.
341   ///
342   virtual Init *resolveBitReference(Record &R, unsigned Bit) = 0;
343 };
344
345 /// VarInit - 'Opcode' - Represent a reference to an entire variable object.
346 ///
347 class VarInit : public TypedInit {
348   std::string VarName;
349 public:
350   VarInit(const std::string &VN, RecTy *T) : TypedInit(T), VarName(VN) {}
351   
352   virtual Init *convertInitializerTo(RecTy *Ty) {
353     return Ty->convertValue(this);
354   }
355
356   const std::string &getName() const { return VarName; }
357
358   virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
359
360   virtual Init *resolveBitReference(Record &R, unsigned Bit);
361
362   virtual RecTy *getFieldType(const std::string &FieldName) const;
363   virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
364   
365   virtual void print(std::ostream &OS) const { OS << VarName; }
366 };
367
368
369 /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
370 ///
371 class VarBitInit : public Init {
372   TypedInit *TI;
373   unsigned Bit;
374 public:
375   VarBitInit(TypedInit *T, unsigned B) : TI(T), Bit(B) {
376     assert(T->getType() && dynamic_cast<BitsRecTy*>(T->getType()) &&
377            ((BitsRecTy*)T->getType())->getNumBits() > B &&
378            "Illegal VarBitInit expression!");
379   }
380
381   virtual Init *convertInitializerTo(RecTy *Ty) {
382     return Ty->convertValue(this);
383   }
384
385   TypedInit *getVariable() const { return TI; }
386   unsigned getBitNum() const { return Bit; }
387   
388   virtual void print(std::ostream &OS) const {
389     TI->print(OS); OS << "{" << Bit << "}";
390   }
391   virtual Init *resolveReferences(Record &R);
392 };
393
394
395 /// DefInit - AL - Represent a reference to a 'def' in the description
396 ///
397 class DefInit : public Init {
398   Record *Def;
399 public:
400   DefInit(Record *D) : Def(D) {}
401   
402   virtual Init *convertInitializerTo(RecTy *Ty) {
403     return Ty->convertValue(this);
404   }
405
406   Record *getDef() const { return Def; }
407
408   //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
409
410   virtual RecTy *getFieldType(const std::string &FieldName) const;
411   virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
412   
413   virtual void print(std::ostream &OS) const;
414 };
415
416
417 /// FieldInit - X.Y - Represent a reference to a subfield of a variable
418 ///
419 class FieldInit : public TypedInit {
420   Init *Rec;                // Record we are referring to
421   std::string FieldName;    // Field we are accessing
422 public:
423   FieldInit(Init *R, const std::string &FN)
424     : TypedInit(R->getFieldType(FN)), Rec(R), FieldName(FN) {
425     assert(getType() && "FieldInit with non-record type!");
426   }
427
428   virtual Init *convertInitializerTo(RecTy *Ty) {
429     return Ty->convertValue(this);
430   }
431
432   virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
433
434   virtual Init *resolveBitReference(Record &R, unsigned Bit);
435
436   virtual void print(std::ostream &OS) const {
437     Rec->print(OS); OS << "." << FieldName;
438   }
439 };
440
441
442 //===----------------------------------------------------------------------===//
443 //  High-Level Classes
444 //===----------------------------------------------------------------------===//
445
446 class RecordVal {
447   std::string Name;
448   RecTy *Ty;
449   unsigned Prefix;
450   Init *Value;
451 public:
452   RecordVal(const std::string &N, RecTy *T, unsigned P);
453
454   const std::string &getName() const { return Name; }
455
456   unsigned getPrefix() const { return Prefix; }
457   RecTy *getType() const { return Ty; }
458   Init *getValue() const { return Value; }
459
460   bool setValue(Init *V) {
461     if (V) {
462       Value = V->convertInitializerTo(Ty);
463       return Value == 0;
464     }
465     Value = 0;
466     return false;
467   }
468
469   void dump() const;
470   void print(std::ostream &OS, bool PrintSem = true) const;
471 };
472
473 inline std::ostream &operator<<(std::ostream &OS, const RecordVal &RV) {
474   RV.print(OS << "  ");
475   return OS;
476 }
477
478 struct Record {
479   const std::string Name;
480   std::vector<std::string> TemplateArgs;
481   std::vector<RecordVal> Values;
482   std::vector<Record*> SuperClasses;
483 public:
484
485   Record(const std::string &N) : Name(N) {}
486   ~Record() {}
487
488   const std::string &getName() const { return Name; }
489   const std::vector<std::string> &getTemplateArgs() const {
490     return TemplateArgs;
491   }
492   const std::vector<RecordVal> &getValues() const { return Values; }
493   const std::vector<Record*>   &getSuperClasses() const { return SuperClasses; }
494
495   bool isTemplateArg(const std::string &Name) const {
496     for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
497       if (TemplateArgs[i] == Name) return true;
498     return false;
499   }
500
501   const RecordVal *getValue(const std::string &Name) const {
502     for (unsigned i = 0, e = Values.size(); i != e; ++i)
503       if (Values[i].getName() == Name) return &Values[i];
504     return 0;
505   }
506   RecordVal *getValue(const std::string &Name) {
507     for (unsigned i = 0, e = Values.size(); i != e; ++i)
508       if (Values[i].getName() == Name) return &Values[i];
509     return 0;
510   }
511
512   void addTemplateArg(const std::string &Name) {
513     assert(!isTemplateArg(Name) && "Template arg already defined!");
514     TemplateArgs.push_back(Name);
515   }
516
517   void addValue(const RecordVal &RV) {
518     assert(getValue(RV.getName()) == 0 && "Value already added!");
519     Values.push_back(RV);
520   }
521
522   bool isSubClassOf(Record *R) const {
523     for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
524       if (SuperClasses[i] == R)
525         return true;
526     return false;
527   }
528
529   void addSuperClass(Record *R) {
530     assert(!isSubClassOf(R) && "Already subclassing record!");
531     SuperClasses.push_back(R);
532   }
533
534   // resolveReferences - If there are any field references that refer to fields
535   // that have been filled in, we can propagate the values now.
536   //
537   void resolveReferences();
538
539   void dump() const;
540 };
541
542 std::ostream &operator<<(std::ostream &OS, const Record &R);
543
544 class RecordKeeper {
545   std::map<std::string, Record*> Classes, Defs;
546 public:
547   ~RecordKeeper() {
548     for (std::map<std::string, Record*>::iterator I = Classes.begin(),
549            E = Classes.end(); I != E; ++I)
550       delete I->second;
551     for (std::map<std::string, Record*>::iterator I = Defs.begin(),
552            E = Defs.end(); I != E; ++I)
553       delete I->second;
554   }
555   
556   const std::map<std::string, Record*> &getClasses() const { return Classes; }
557   const std::map<std::string, Record*> &getDefs() const { return Defs; }
558
559   Record *getClass(const std::string &Name) const {
560     std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
561     return I == Classes.end() ? 0 : I->second;
562   }
563   Record *getDef(const std::string &Name) const {
564     std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
565     return I == Defs.end() ? 0 : I->second;
566   }
567   void addClass(Record *R) {
568     assert(getClass(R->getName()) == 0 && "Class already exists!");
569     Classes.insert(std::make_pair(R->getName(), R));
570   }
571   void addDef(Record *R) {
572     assert(getDef(R->getName()) == 0 && "Def already exists!");
573     Defs.insert(std::make_pair(R->getName(), R));
574   }
575
576   void dump() const;
577 };
578
579 std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK);
580
581 extern RecordKeeper Records;
582
583 #endif