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