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