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