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