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