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