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