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