Add new method
[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   const std::string getValue() const { return Value; }
339
340   virtual Init *convertInitializerTo(RecTy *Ty) {
341     return Ty->convertValue(this);
342   }
343
344   virtual void print(std::ostream &OS) const { OS << "[{" << Value << "}]"; }
345 };
346
347 /// ListInit - [AL, AH, CL] - Represent a list of defs
348 ///
349 class ListInit : public Init {
350   std::vector<Record*> Records;
351 public:
352   ListInit(std::vector<Record*> &Rs) {
353     Records.swap(Rs);
354   }
355
356   unsigned getSize() const { return Records.size(); }
357   Record  *getElement(unsigned i) const {
358     assert(i < Records.size() && "List element index out of range!");
359     return Records[i];
360   }
361
362   virtual Init *convertInitializerTo(RecTy *Ty) {
363     return Ty->convertValue(this);
364   }
365
366   virtual void print(std::ostream &OS) const;
367 };
368
369
370 /// TypedInit - This is the common super-class of types that have a specific,
371 /// explicit, type.
372 ///
373 class TypedInit : public Init {
374   RecTy *Ty;
375 public:  
376   TypedInit(RecTy *T) : Ty(T) {}
377
378   RecTy *getType() const { return Ty; }
379
380   /// resolveBitReference - This method is used to implement
381   /// VarBitInit::resolveReferences.  If the bit is able to be resolved, we
382   /// simply return the resolved value, otherwise we return this.
383   ///
384   virtual Init *resolveBitReference(Record &R, unsigned Bit) = 0;
385 };
386
387 /// VarInit - 'Opcode' - Represent a reference to an entire variable object.
388 ///
389 class VarInit : public TypedInit {
390   std::string VarName;
391 public:
392   VarInit(const std::string &VN, RecTy *T) : TypedInit(T), VarName(VN) {}
393   
394   virtual Init *convertInitializerTo(RecTy *Ty) {
395     return Ty->convertValue(this);
396   }
397
398   const std::string &getName() const { return VarName; }
399
400   virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
401
402   virtual Init *resolveBitReference(Record &R, unsigned Bit);
403
404   virtual RecTy *getFieldType(const std::string &FieldName) const;
405   virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
406
407   /// resolveReferences - This method is used by classes that refer to other
408   /// variables which may not be defined at the time they expression is formed.
409   /// If a value is set for the variable later, this method will be called on
410   /// users of the value to allow the value to propagate out.
411   ///
412   virtual Init *resolveReferences(Record &R);
413   
414   virtual void print(std::ostream &OS) const { OS << VarName; }
415 };
416
417
418 /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
419 ///
420 class VarBitInit : public Init {
421   TypedInit *TI;
422   unsigned Bit;
423 public:
424   VarBitInit(TypedInit *T, unsigned B) : TI(T), Bit(B) {
425     assert(T->getType() && dynamic_cast<BitsRecTy*>(T->getType()) &&
426            ((BitsRecTy*)T->getType())->getNumBits() > B &&
427            "Illegal VarBitInit expression!");
428   }
429
430   virtual Init *convertInitializerTo(RecTy *Ty) {
431     return Ty->convertValue(this);
432   }
433
434   TypedInit *getVariable() const { return TI; }
435   unsigned getBitNum() const { return Bit; }
436   
437   virtual void print(std::ostream &OS) const {
438     TI->print(OS); OS << "{" << Bit << "}";
439   }
440   virtual Init *resolveReferences(Record &R);
441 };
442
443
444 /// DefInit - AL - Represent a reference to a 'def' in the description
445 ///
446 class DefInit : public Init {
447   Record *Def;
448 public:
449   DefInit(Record *D) : Def(D) {}
450   
451   virtual Init *convertInitializerTo(RecTy *Ty) {
452     return Ty->convertValue(this);
453   }
454
455   Record *getDef() const { return Def; }
456
457   //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
458
459   virtual RecTy *getFieldType(const std::string &FieldName) const;
460   virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
461   
462   virtual void print(std::ostream &OS) const;
463 };
464
465
466 /// FieldInit - X.Y - Represent a reference to a subfield of a variable
467 ///
468 class FieldInit : public TypedInit {
469   Init *Rec;                // Record we are referring to
470   std::string FieldName;    // Field we are accessing
471 public:
472   FieldInit(Init *R, const std::string &FN)
473     : TypedInit(R->getFieldType(FN)), Rec(R), FieldName(FN) {
474     assert(getType() && "FieldInit with non-record type!");
475   }
476
477   virtual Init *convertInitializerTo(RecTy *Ty) {
478     return Ty->convertValue(this);
479   }
480
481   virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
482
483   virtual Init *resolveBitReference(Record &R, unsigned Bit);
484
485   virtual Init *resolveReferences(Record &R);
486
487   virtual void print(std::ostream &OS) const {
488     Rec->print(OS); OS << "." << FieldName;
489   }
490 };
491
492
493 //===----------------------------------------------------------------------===//
494 //  High-Level Classes
495 //===----------------------------------------------------------------------===//
496
497 class RecordVal {
498   std::string Name;
499   RecTy *Ty;
500   unsigned Prefix;
501   Init *Value;
502 public:
503   RecordVal(const std::string &N, RecTy *T, unsigned P);
504
505   const std::string &getName() const { return Name; }
506
507   unsigned getPrefix() const { return Prefix; }
508   RecTy *getType() const { return Ty; }
509   Init *getValue() const { return Value; }
510
511   bool setValue(Init *V) {
512     if (V) {
513       Value = V->convertInitializerTo(Ty);
514       return Value == 0;
515     }
516     Value = 0;
517     return false;
518   }
519
520   void dump() const;
521   void print(std::ostream &OS, bool PrintSem = true) const;
522 };
523
524 inline std::ostream &operator<<(std::ostream &OS, const RecordVal &RV) {
525   RV.print(OS << "  ");
526   return OS;
527 }
528
529 struct Record {
530   const std::string Name;
531   std::vector<std::string> TemplateArgs;
532   std::vector<RecordVal> Values;
533   std::vector<Record*> SuperClasses;
534 public:
535
536   Record(const std::string &N) : Name(N) {}
537   ~Record() {}
538
539   const std::string &getName() const { return Name; }
540   const std::vector<std::string> &getTemplateArgs() const {
541     return TemplateArgs;
542   }
543   const std::vector<RecordVal> &getValues() const { return Values; }
544   const std::vector<Record*>   &getSuperClasses() const { return SuperClasses; }
545
546   bool isTemplateArg(const std::string &Name) const {
547     for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
548       if (TemplateArgs[i] == Name) return true;
549     return false;
550   }
551
552   const RecordVal *getValue(const std::string &Name) const {
553     for (unsigned i = 0, e = Values.size(); i != e; ++i)
554       if (Values[i].getName() == Name) return &Values[i];
555     return 0;
556   }
557   RecordVal *getValue(const std::string &Name) {
558     for (unsigned i = 0, e = Values.size(); i != e; ++i)
559       if (Values[i].getName() == Name) return &Values[i];
560     return 0;
561   }
562
563   void addTemplateArg(const std::string &Name) {
564     assert(!isTemplateArg(Name) && "Template arg already defined!");
565     TemplateArgs.push_back(Name);
566   }
567
568   void addValue(const RecordVal &RV) {
569     assert(getValue(RV.getName()) == 0 && "Value already added!");
570     Values.push_back(RV);
571   }
572
573   void removeValue(const std::string &Name) {
574     assert(getValue(Name) && "Cannot remove an entry that does not exist!");
575     for (unsigned i = 0, e = Values.size(); i != e; ++i)
576       if (Values[i].getName() == Name) {
577         Values.erase(Values.begin()+i);
578         return;
579       }
580     assert(0 && "Name does not exist in record!");
581   }
582
583   bool isSubClassOf(Record *R) const {
584     for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
585       if (SuperClasses[i] == R)
586         return true;
587     return false;
588   }
589
590   void addSuperClass(Record *R) {
591     assert(!isSubClassOf(R) && "Already subclassing record!");
592     SuperClasses.push_back(R);
593   }
594
595   // resolveReferences - If there are any field references that refer to fields
596   // that have been filled in, we can propagate the values now.
597   //
598   void resolveReferences();
599
600   void dump() const;
601
602   //===--------------------------------------------------------------------===//
603   // High-level methods useful to tablegen back-ends
604   //
605
606   /// getValueInit - Return the initializer for a value with the specified name,
607   /// or throw an exception if the field does not exist.
608   ///
609   Init *getValueInit(const std::string &FieldName) const;
610
611   /// getValueAsString - This method looks up the specified field and returns
612   /// its value as a string, throwing an exception if the field does not exist
613   /// or if the value is not a string.
614   ///
615   std::string getValueAsString(const std::string &FieldName) const;
616
617   /// getValueAsBitsInit - This method looks up the specified field and returns
618   /// its value as a BitsInit, throwing an exception if the field does not exist
619   /// or if the value is not the right type.
620   ///
621   BitsInit *getValueAsBitsInit(const std::string &FieldName) const;
622
623   /// getValueAsListInit - This method looks up the specified field and returns
624   /// its value as a ListInit, throwing an exception if the field does not exist
625   /// or if the value is not the right type.
626   ///
627   ListInit *getValueAsListInit(const std::string &FieldName) const;
628
629   /// getValueAsDef - This method looks up the specified field and returns its
630   /// value as a Record, throwing an exception if the field does not exist or if
631   /// the value is not the right type.
632   ///
633   Record *getValueAsDef(const std::string &FieldName) const;
634
635   /// getValueAsInt - This method looks up the specified field and returns its
636   /// value as an int, throwing an exception if the field does not exist or if
637   /// the value is not the right type.
638   ///
639   int getValueAsInt(const std::string &FieldName) const;
640 };
641
642 std::ostream &operator<<(std::ostream &OS, const Record &R);
643
644 class RecordKeeper {
645   std::map<std::string, Record*> Classes, Defs;
646 public:
647   ~RecordKeeper() {
648     for (std::map<std::string, Record*>::iterator I = Classes.begin(),
649            E = Classes.end(); I != E; ++I)
650       delete I->second;
651     for (std::map<std::string, Record*>::iterator I = Defs.begin(),
652            E = Defs.end(); I != E; ++I)
653       delete I->second;
654   }
655   
656   const std::map<std::string, Record*> &getClasses() const { return Classes; }
657   const std::map<std::string, Record*> &getDefs() const { return Defs; }
658
659   Record *getClass(const std::string &Name) const {
660     std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
661     return I == Classes.end() ? 0 : I->second;
662   }
663   Record *getDef(const std::string &Name) const {
664     std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
665     return I == Defs.end() ? 0 : I->second;
666   }
667   void addClass(Record *R) {
668     assert(getClass(R->getName()) == 0 && "Class already exists!");
669     Classes.insert(std::make_pair(R->getName(), R));
670   }
671   void addDef(Record *R) {
672     assert(getDef(R->getName()) == 0 && "Def already exists!");
673     Defs.insert(std::make_pair(R->getName(), R));
674   }
675
676   //===--------------------------------------------------------------------===//
677   // High-level helper methods, useful for tablegen backends...
678
679   /// getAllDerivedDefinitions - This method returns all concrete definitions
680   /// that derive from the specified class name.  If a class with the specified
681   /// name does not exist, an exception is thrown.
682   std::vector<Record*>
683   getAllDerivedDefinitions(const std::string &ClassName) const;
684
685
686   void dump() const;
687 };
688
689 std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK);
690
691 extern RecordKeeper Records;
692
693 #endif