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