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