356d131ae9ee90ccf3e55f58bbd8b12d11d5b78b
[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   /// resolveReferences - This method is used by classes that refer to other
370   /// variables which may not be defined at the time they expression is formed.
371   /// If a value is set for the variable later, this method will be called on
372   /// users of the value to allow the value to propagate out.
373   ///
374   virtual Init *resolveReferences(Record &R);
375   
376   virtual void print(std::ostream &OS) const { OS << VarName; }
377 };
378
379
380 /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
381 ///
382 class VarBitInit : public Init {
383   TypedInit *TI;
384   unsigned Bit;
385 public:
386   VarBitInit(TypedInit *T, unsigned B) : TI(T), Bit(B) {
387     assert(T->getType() && dynamic_cast<BitsRecTy*>(T->getType()) &&
388            ((BitsRecTy*)T->getType())->getNumBits() > B &&
389            "Illegal VarBitInit expression!");
390   }
391
392   virtual Init *convertInitializerTo(RecTy *Ty) {
393     return Ty->convertValue(this);
394   }
395
396   TypedInit *getVariable() const { return TI; }
397   unsigned getBitNum() const { return Bit; }
398   
399   virtual void print(std::ostream &OS) const {
400     TI->print(OS); OS << "{" << Bit << "}";
401   }
402   virtual Init *resolveReferences(Record &R);
403 };
404
405
406 /// DefInit - AL - Represent a reference to a 'def' in the description
407 ///
408 class DefInit : public Init {
409   Record *Def;
410 public:
411   DefInit(Record *D) : Def(D) {}
412   
413   virtual Init *convertInitializerTo(RecTy *Ty) {
414     return Ty->convertValue(this);
415   }
416
417   Record *getDef() const { return Def; }
418
419   //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
420
421   virtual RecTy *getFieldType(const std::string &FieldName) const;
422   virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
423   
424   virtual void print(std::ostream &OS) const;
425 };
426
427
428 /// FieldInit - X.Y - Represent a reference to a subfield of a variable
429 ///
430 class FieldInit : public TypedInit {
431   Init *Rec;                // Record we are referring to
432   std::string FieldName;    // Field we are accessing
433 public:
434   FieldInit(Init *R, const std::string &FN)
435     : TypedInit(R->getFieldType(FN)), Rec(R), FieldName(FN) {
436     assert(getType() && "FieldInit with non-record type!");
437   }
438
439   virtual Init *convertInitializerTo(RecTy *Ty) {
440     return Ty->convertValue(this);
441   }
442
443   virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
444
445   virtual Init *resolveBitReference(Record &R, unsigned Bit);
446
447   virtual void print(std::ostream &OS) const {
448     Rec->print(OS); OS << "." << FieldName;
449   }
450 };
451
452
453 //===----------------------------------------------------------------------===//
454 //  High-Level Classes
455 //===----------------------------------------------------------------------===//
456
457 class RecordVal {
458   std::string Name;
459   RecTy *Ty;
460   unsigned Prefix;
461   Init *Value;
462 public:
463   RecordVal(const std::string &N, RecTy *T, unsigned P);
464
465   const std::string &getName() const { return Name; }
466
467   unsigned getPrefix() const { return Prefix; }
468   RecTy *getType() const { return Ty; }
469   Init *getValue() const { return Value; }
470
471   bool setValue(Init *V) {
472     if (V) {
473       Value = V->convertInitializerTo(Ty);
474       return Value == 0;
475     }
476     Value = 0;
477     return false;
478   }
479
480   void dump() const;
481   void print(std::ostream &OS, bool PrintSem = true) const;
482 };
483
484 inline std::ostream &operator<<(std::ostream &OS, const RecordVal &RV) {
485   RV.print(OS << "  ");
486   return OS;
487 }
488
489 struct Record {
490   const std::string Name;
491   std::vector<std::string> TemplateArgs;
492   std::vector<RecordVal> Values;
493   std::vector<Record*> SuperClasses;
494 public:
495
496   Record(const std::string &N) : Name(N) {}
497   ~Record() {}
498
499   const std::string &getName() const { return Name; }
500   const std::vector<std::string> &getTemplateArgs() const {
501     return TemplateArgs;
502   }
503   const std::vector<RecordVal> &getValues() const { return Values; }
504   const std::vector<Record*>   &getSuperClasses() const { return SuperClasses; }
505
506   bool isTemplateArg(const std::string &Name) const {
507     for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
508       if (TemplateArgs[i] == Name) return true;
509     return false;
510   }
511
512   const RecordVal *getValue(const std::string &Name) const {
513     for (unsigned i = 0, e = Values.size(); i != e; ++i)
514       if (Values[i].getName() == Name) return &Values[i];
515     return 0;
516   }
517   RecordVal *getValue(const std::string &Name) {
518     for (unsigned i = 0, e = Values.size(); i != e; ++i)
519       if (Values[i].getName() == Name) return &Values[i];
520     return 0;
521   }
522
523   void addTemplateArg(const std::string &Name) {
524     assert(!isTemplateArg(Name) && "Template arg already defined!");
525     TemplateArgs.push_back(Name);
526   }
527
528   void addValue(const RecordVal &RV) {
529     assert(getValue(RV.getName()) == 0 && "Value already added!");
530     Values.push_back(RV);
531   }
532
533   bool isSubClassOf(Record *R) const {
534     for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
535       if (SuperClasses[i] == R)
536         return true;
537     return false;
538   }
539
540   void addSuperClass(Record *R) {
541     assert(!isSubClassOf(R) && "Already subclassing record!");
542     SuperClasses.push_back(R);
543   }
544
545   // resolveReferences - If there are any field references that refer to fields
546   // that have been filled in, we can propagate the values now.
547   //
548   void resolveReferences();
549
550   void dump() const;
551 };
552
553 std::ostream &operator<<(std::ostream &OS, const Record &R);
554
555 class RecordKeeper {
556   std::map<std::string, Record*> Classes, Defs;
557 public:
558   ~RecordKeeper() {
559     for (std::map<std::string, Record*>::iterator I = Classes.begin(),
560            E = Classes.end(); I != E; ++I)
561       delete I->second;
562     for (std::map<std::string, Record*>::iterator I = Defs.begin(),
563            E = Defs.end(); I != E; ++I)
564       delete I->second;
565   }
566   
567   const std::map<std::string, Record*> &getClasses() const { return Classes; }
568   const std::map<std::string, Record*> &getDefs() const { return Defs; }
569
570   Record *getClass(const std::string &Name) const {
571     std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
572     return I == Classes.end() ? 0 : I->second;
573   }
574   Record *getDef(const std::string &Name) const {
575     std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
576     return I == Defs.end() ? 0 : I->second;
577   }
578   void addClass(Record *R) {
579     assert(getClass(R->getName()) == 0 && "Class already exists!");
580     Classes.insert(std::make_pair(R->getName(), R));
581   }
582   void addDef(Record *R) {
583     assert(getDef(R->getName()) == 0 && "Def already exists!");
584     Defs.insert(std::make_pair(R->getName(), R));
585   }
586
587   void dump() const;
588 };
589
590 std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK);
591
592 extern RecordKeeper Records;
593
594 #endif