Revert 46556 and 46585. Dan please fix the PseudoSourceValue problem and re-commit.
[oota-llvm.git] / utils / TableGen / Record.h
1 //===- Record.h - Classes to represent Table Records ------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the main TableGen data structures, including the TableGen
11 // types, values, and high-level data structures.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef RECORD_H
16 #define RECORD_H
17
18 #include <string>
19 #include <vector>
20 #include <map>
21 #include <ostream>
22 #include <cassert>
23
24 namespace llvm {
25
26 // RecTy subclasses.
27 class BitRecTy;
28 class BitsRecTy;
29 class IntRecTy;
30 class StringRecTy;
31 class ListRecTy;
32 class CodeRecTy;
33 class DagRecTy;
34 class RecordRecTy;
35
36 // Init subclasses.
37 struct Init;
38 class UnsetInit;
39 class BitInit;
40 class BitsInit;
41 class IntInit;
42 class StringInit;
43 class CodeInit;
44 class ListInit;
45 class BinOpInit;
46 class DefInit;
47 class DagInit;
48 class TypedInit;
49 class VarInit;
50 class FieldInit;
51 class VarBitInit;
52 class VarListElementInit;
53
54 // Other classes.
55 class Record;
56 class RecordVal;
57
58 //===----------------------------------------------------------------------===//
59 //  Type Classes
60 //===----------------------------------------------------------------------===//
61
62 struct RecTy {
63   virtual ~RecTy() {}
64
65   virtual std::string getAsString() const = 0;
66   void print(std::ostream &OS) const { OS << getAsString(); }
67   void dump() const;
68
69   /// typeIsConvertibleTo - Return true if all values of 'this' type can be
70   /// converted to the specified type.
71   virtual bool typeIsConvertibleTo(const RecTy *RHS) const = 0;
72
73 public:   // These methods should only be called from subclasses of Init
74   virtual Init *convertValue( UnsetInit *UI) { return 0; }
75   virtual Init *convertValue(   BitInit *BI) { return 0; }
76   virtual Init *convertValue(  BitsInit *BI) { return 0; }
77   virtual Init *convertValue(   IntInit *II) { return 0; }
78   virtual Init *convertValue(StringInit *SI) { return 0; }
79   virtual Init *convertValue(  ListInit *LI) { return 0; }
80   virtual Init *convertValue( BinOpInit *UI) { return 0; }
81   virtual Init *convertValue(  CodeInit *CI) { return 0; }
82   virtual Init *convertValue(VarBitInit *VB) { return 0; }
83   virtual Init *convertValue(   DefInit *DI) { return 0; }
84   virtual Init *convertValue(   DagInit *DI) { return 0; }
85   virtual Init *convertValue( TypedInit *TI) { return 0; }
86   virtual Init *convertValue(   VarInit *VI) {
87     return convertValue((TypedInit*)VI);
88   }
89   virtual Init *convertValue( FieldInit *FI) {
90     return convertValue((TypedInit*)FI);
91   }
92
93 public:   // These methods should only be called by subclasses of RecTy.
94   // baseClassOf - These virtual methods should be overloaded to return true iff
95   // all values of type 'RHS' can be converted to the 'this' type.
96   virtual bool baseClassOf(const BitRecTy    *RHS) const { return false; }
97   virtual bool baseClassOf(const BitsRecTy   *RHS) const { return false; }
98   virtual bool baseClassOf(const IntRecTy    *RHS) const { return false; }
99   virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
100   virtual bool baseClassOf(const ListRecTy   *RHS) const { return false; }
101   virtual bool baseClassOf(const CodeRecTy   *RHS) const { return false; }
102   virtual bool baseClassOf(const DagRecTy    *RHS) const { return false; }
103   virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
104 };
105
106 inline std::ostream &operator<<(std::ostream &OS, const RecTy &Ty) {
107   Ty.print(OS);
108   return OS;
109 }
110
111
112 /// BitRecTy - 'bit' - Represent a single bit
113 ///
114 class BitRecTy : public RecTy {
115 public:
116   virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
117   virtual Init *convertValue(   BitInit *BI) { return (Init*)BI; }
118   virtual Init *convertValue(  BitsInit *BI);
119   virtual Init *convertValue(   IntInit *II);
120   virtual Init *convertValue(StringInit *SI) { return 0; }
121   virtual Init *convertValue(  ListInit *LI) { return 0; }
122   virtual Init *convertValue(  CodeInit *CI) { return 0; }
123   virtual Init *convertValue(VarBitInit *VB) { return (Init*)VB; }
124   virtual Init *convertValue(   DefInit *DI) { return 0; }
125   virtual Init *convertValue(   DagInit *DI) { return 0; }
126   virtual Init *convertValue( BinOpInit *UI) { return 0; }
127   virtual Init *convertValue( TypedInit *TI);
128   virtual Init *convertValue(   VarInit *VI) { return RecTy::convertValue(VI);}
129   virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
130
131   std::string getAsString() const { return "bit"; }
132
133   bool typeIsConvertibleTo(const RecTy *RHS) const {
134     return RHS->baseClassOf(this);
135   }
136   virtual bool baseClassOf(const BitRecTy    *RHS) const { return true; }
137   virtual bool baseClassOf(const BitsRecTy   *RHS) const;
138   virtual bool baseClassOf(const IntRecTy    *RHS) const { return true; }
139   virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
140   virtual bool baseClassOf(const ListRecTy   *RHS) const { return false; }
141   virtual bool baseClassOf(const CodeRecTy   *RHS) const { return false; }
142   virtual bool baseClassOf(const DagRecTy    *RHS) const { return false; }
143   virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
144
145 };
146
147
148 // BitsRecTy - 'bits<n>' - Represent a fixed number of bits
149 /// BitsRecTy - 'bits&lt;n&gt;' - Represent a fixed number of bits
150 ///
151 class BitsRecTy : public RecTy {
152   unsigned Size;
153 public:
154   BitsRecTy(unsigned Sz) : Size(Sz) {}
155
156   unsigned getNumBits() const { return Size; }
157
158   virtual Init *convertValue( UnsetInit *UI);
159   virtual Init *convertValue(   BitInit *UI);
160   virtual Init *convertValue(  BitsInit *BI);
161   virtual Init *convertValue(   IntInit *II);
162   virtual Init *convertValue(StringInit *SI) { return 0; }
163   virtual Init *convertValue(  ListInit *LI) { return 0; }
164   virtual Init *convertValue(  CodeInit *CI) { return 0; }
165   virtual Init *convertValue(VarBitInit *VB) { return 0; }
166   virtual Init *convertValue(   DefInit *DI) { return 0; }
167   virtual Init *convertValue(   DagInit *DI) { return 0; }
168   virtual Init *convertValue( BinOpInit *UI) { return 0; }
169   virtual Init *convertValue( TypedInit *TI);
170   virtual Init *convertValue(   VarInit *VI) { return RecTy::convertValue(VI);}
171   virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
172
173   std::string getAsString() const;
174
175   bool typeIsConvertibleTo(const RecTy *RHS) const {
176     return RHS->baseClassOf(this);
177   }
178   virtual bool baseClassOf(const BitRecTy    *RHS) const { return Size == 1; }
179   virtual bool baseClassOf(const BitsRecTy   *RHS) const {
180     return RHS->Size == Size;
181   }
182   virtual bool baseClassOf(const IntRecTy    *RHS) const { return true; }
183   virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
184   virtual bool baseClassOf(const ListRecTy   *RHS) const { return false; }
185   virtual bool baseClassOf(const CodeRecTy   *RHS) const { return false; }
186   virtual bool baseClassOf(const DagRecTy    *RHS) const { return false; }
187   virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
188
189 };
190
191
192 /// IntRecTy - 'int' - Represent an integer value of no particular size
193 ///
194 class IntRecTy : public RecTy {
195 public:
196   virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
197   virtual Init *convertValue(   BitInit *BI);
198   virtual Init *convertValue(  BitsInit *BI);
199   virtual Init *convertValue(   IntInit *II) { return (Init*)II; }
200   virtual Init *convertValue(StringInit *SI) { return 0; }
201   virtual Init *convertValue(  ListInit *LI) { return 0; }
202   virtual Init *convertValue(  CodeInit *CI) { return 0; }
203   virtual Init *convertValue(VarBitInit *VB) { return 0; }
204   virtual Init *convertValue(   DefInit *DI) { return 0; }
205   virtual Init *convertValue(   DagInit *DI) { return 0; }
206   virtual Init *convertValue( BinOpInit *UI) { return 0; }
207   virtual Init *convertValue( TypedInit *TI);
208   virtual Init *convertValue(   VarInit *VI) { return RecTy::convertValue(VI);}
209   virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
210
211   std::string getAsString() const { return "int"; }
212
213   bool typeIsConvertibleTo(const RecTy *RHS) const {
214     return RHS->baseClassOf(this);
215   }
216
217   virtual bool baseClassOf(const BitRecTy    *RHS) const { return true; }
218   virtual bool baseClassOf(const BitsRecTy   *RHS) const { return true; }
219   virtual bool baseClassOf(const IntRecTy    *RHS) const { return true; }
220   virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
221   virtual bool baseClassOf(const ListRecTy   *RHS) const { return false; }
222   virtual bool baseClassOf(const CodeRecTy   *RHS) const { return false; }
223   virtual bool baseClassOf(const DagRecTy    *RHS) const { return false; }
224   virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
225
226 };
227
228 /// StringRecTy - 'string' - Represent an string value
229 ///
230 class StringRecTy : public RecTy {
231 public:
232   virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
233   virtual Init *convertValue(   BitInit *BI) { return 0; }
234   virtual Init *convertValue(  BitsInit *BI) { return 0; }
235   virtual Init *convertValue(   IntInit *II) { return 0; }
236   virtual Init *convertValue(StringInit *SI) { return (Init*)SI; }
237   virtual Init *convertValue(  ListInit *LI) { return 0; }
238   virtual Init *convertValue( BinOpInit *BO);
239   virtual Init *convertValue(  CodeInit *CI) { return 0; }
240   virtual Init *convertValue(VarBitInit *VB) { return 0; }
241   virtual Init *convertValue(   DefInit *DI) { return 0; }
242   virtual Init *convertValue(   DagInit *DI) { return 0; }
243   virtual Init *convertValue( TypedInit *TI);
244   virtual Init *convertValue(   VarInit *VI) { return RecTy::convertValue(VI);}
245   virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
246
247   std::string getAsString() const { return "string"; }
248
249   bool typeIsConvertibleTo(const RecTy *RHS) const {
250     return RHS->baseClassOf(this);
251   }
252
253   virtual bool baseClassOf(const BitRecTy    *RHS) const { return false; }
254   virtual bool baseClassOf(const BitsRecTy   *RHS) const { return false; }
255   virtual bool baseClassOf(const IntRecTy    *RHS) const { return false; }
256   virtual bool baseClassOf(const StringRecTy *RHS) const { return true; }
257   virtual bool baseClassOf(const ListRecTy   *RHS) const { return false; }
258   virtual bool baseClassOf(const CodeRecTy   *RHS) const { return false; }
259   virtual bool baseClassOf(const DagRecTy    *RHS) const { return false; }
260   virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
261 };
262
263 // ListRecTy - 'list<Ty>' - Represent a list of values, all of which must be of
264 // the specified type.
265 /// ListRecTy - 'list&lt;Ty&gt;' - Represent a list of values, all of which must
266 /// be of the specified type.
267 ///
268 class ListRecTy : public RecTy {
269   RecTy *Ty;
270 public:
271   ListRecTy(RecTy *T) : Ty(T) {}
272
273   RecTy *getElementType() const { return Ty; }
274
275   virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
276   virtual Init *convertValue(   BitInit *BI) { return 0; }
277   virtual Init *convertValue(  BitsInit *BI) { return 0; }
278   virtual Init *convertValue(   IntInit *II) { return 0; }
279   virtual Init *convertValue(StringInit *SI) { return 0; }
280   virtual Init *convertValue(  ListInit *LI);
281   virtual Init *convertValue(  CodeInit *CI) { return 0; }
282   virtual Init *convertValue(VarBitInit *VB) { return 0; }
283   virtual Init *convertValue(   DefInit *DI) { return 0; }
284   virtual Init *convertValue(   DagInit *DI) { return 0; }
285   virtual Init *convertValue( BinOpInit *UI) { return 0; }
286   virtual Init *convertValue( TypedInit *TI);
287   virtual Init *convertValue(   VarInit *VI) { return RecTy::convertValue(VI);}
288   virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
289
290   std::string getAsString() const;
291
292   bool typeIsConvertibleTo(const RecTy *RHS) const {
293     return RHS->baseClassOf(this);
294   }
295
296   virtual bool baseClassOf(const BitRecTy    *RHS) const { return false; }
297   virtual bool baseClassOf(const BitsRecTy   *RHS) const { return false; }
298   virtual bool baseClassOf(const IntRecTy    *RHS) const { return false; }
299   virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
300   virtual bool baseClassOf(const ListRecTy   *RHS) const {
301     return RHS->getElementType()->typeIsConvertibleTo(Ty);
302   }
303   virtual bool baseClassOf(const CodeRecTy   *RHS) const { return false; }
304   virtual bool baseClassOf(const DagRecTy    *RHS) const { return false; }
305   virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
306 };
307
308 /// CodeRecTy - 'code' - Represent an code fragment, function or method.
309 ///
310 class CodeRecTy : public RecTy {
311 public:
312   virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
313   virtual Init *convertValue(   BitInit *BI) { return 0; }
314   virtual Init *convertValue(  BitsInit *BI) { return 0; }
315   virtual Init *convertValue(   IntInit *II) { return 0; }
316   virtual Init *convertValue(StringInit *SI) { return 0; }
317   virtual Init *convertValue(  ListInit *LI) { return 0; }
318   virtual Init *convertValue(  CodeInit *CI) { return (Init*)CI; }
319   virtual Init *convertValue(VarBitInit *VB) { return 0; }
320   virtual Init *convertValue(   DefInit *DI) { return 0; }
321   virtual Init *convertValue(   DagInit *DI) { return 0; }
322   virtual Init *convertValue( BinOpInit *UI) { return 0; }
323   virtual Init *convertValue( TypedInit *TI);
324   virtual Init *convertValue(   VarInit *VI) { return RecTy::convertValue(VI);}
325   virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
326
327   std::string getAsString() const { return "code"; }
328
329   bool typeIsConvertibleTo(const RecTy *RHS) const {
330     return RHS->baseClassOf(this);
331   }
332   virtual bool baseClassOf(const BitRecTy    *RHS) const { return false; }
333   virtual bool baseClassOf(const BitsRecTy   *RHS) const { return false; }
334   virtual bool baseClassOf(const IntRecTy    *RHS) const { return false; }
335   virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
336   virtual bool baseClassOf(const ListRecTy   *RHS) const { return false; }
337   virtual bool baseClassOf(const CodeRecTy   *RHS) const { return true; }
338   virtual bool baseClassOf(const DagRecTy    *RHS) const { return false; }
339   virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
340 };
341
342 /// DagRecTy - 'dag' - Represent a dag fragment
343 ///
344 class DagRecTy : public RecTy {
345 public:
346   virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
347   virtual Init *convertValue(   BitInit *BI) { return 0; }
348   virtual Init *convertValue(  BitsInit *BI) { return 0; }
349   virtual Init *convertValue(   IntInit *II) { return 0; }
350   virtual Init *convertValue(StringInit *SI) { return 0; }
351   virtual Init *convertValue(  ListInit *LI) { return 0; }
352   virtual Init *convertValue(  CodeInit *CI) { return 0; }
353   virtual Init *convertValue(VarBitInit *VB) { return 0; }
354   virtual Init *convertValue(   DefInit *DI) { return 0; }
355   virtual Init *convertValue( BinOpInit *BO);
356   virtual Init *convertValue(   DagInit *CI) { return (Init*)CI; }
357   virtual Init *convertValue( TypedInit *TI);
358   virtual Init *convertValue(   VarInit *VI) { return RecTy::convertValue(VI);}
359   virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
360
361   std::string getAsString() const { return "dag"; }
362
363   bool typeIsConvertibleTo(const RecTy *RHS) const {
364     return RHS->baseClassOf(this);
365   }
366
367   virtual bool baseClassOf(const BitRecTy    *RHS) const { return false; }
368   virtual bool baseClassOf(const BitsRecTy   *RHS) const { return false; }
369   virtual bool baseClassOf(const IntRecTy    *RHS) const { return false; }
370   virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
371   virtual bool baseClassOf(const ListRecTy   *RHS) const { return false; }
372   virtual bool baseClassOf(const CodeRecTy   *RHS) const { return false; }
373   virtual bool baseClassOf(const DagRecTy    *RHS) const { return true; }
374   virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
375 };
376
377
378 /// RecordRecTy - '[classname]' - Represent an instance of a class, such as:
379 /// (R32 X = EAX).
380 ///
381 class RecordRecTy : public RecTy {
382   Record *Rec;
383 public:
384   RecordRecTy(Record *R) : Rec(R) {}
385
386   Record *getRecord() const { return Rec; }
387
388   virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
389   virtual Init *convertValue(   BitInit *BI) { return 0; }
390   virtual Init *convertValue(  BitsInit *BI) { return 0; }
391   virtual Init *convertValue(   IntInit *II) { return 0; }
392   virtual Init *convertValue(StringInit *SI) { return 0; }
393   virtual Init *convertValue(  ListInit *LI) { return 0; }
394   virtual Init *convertValue(  CodeInit *CI) { return 0; }
395   virtual Init *convertValue(VarBitInit *VB) { return 0; }
396   virtual Init *convertValue( BinOpInit *UI) { return 0; }
397   virtual Init *convertValue(   DefInit *DI);
398   virtual Init *convertValue(   DagInit *DI) { return 0; }
399   virtual Init *convertValue( TypedInit *VI);
400   virtual Init *convertValue(   VarInit *VI) { return RecTy::convertValue(VI);}
401   virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
402
403   std::string getAsString() const;
404
405   bool typeIsConvertibleTo(const RecTy *RHS) const {
406     return RHS->baseClassOf(this);
407   }
408   virtual bool baseClassOf(const BitRecTy    *RHS) const { return false; }
409   virtual bool baseClassOf(const BitsRecTy   *RHS) const { return false; }
410   virtual bool baseClassOf(const IntRecTy    *RHS) const { return false; }
411   virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
412   virtual bool baseClassOf(const ListRecTy   *RHS) const { return false; }
413   virtual bool baseClassOf(const CodeRecTy   *RHS) const { return false; }
414   virtual bool baseClassOf(const DagRecTy    *RHS) const { return false; }
415   virtual bool baseClassOf(const RecordRecTy *RHS) const;
416 };
417
418
419
420 //===----------------------------------------------------------------------===//
421 //  Initializer Classes
422 //===----------------------------------------------------------------------===//
423
424 struct Init {
425   virtual ~Init() {}
426
427   /// isComplete - This virtual method should be overridden by values that may
428   /// not be completely specified yet.
429   virtual bool isComplete() const { return true; }
430
431   /// print - Print out this value.
432   void print(std::ostream &OS) const { OS << getAsString(); }
433
434   /// getAsString - Convert this value to a string form.
435   virtual std::string getAsString() const = 0;
436
437   /// dump - Debugging method that may be called through a debugger, just
438   /// invokes print on cerr.
439   void dump() const;
440
441   /// convertInitializerTo - This virtual function is a simple call-back
442   /// function that should be overridden to call the appropriate
443   /// RecTy::convertValue method.
444   ///
445   virtual Init *convertInitializerTo(RecTy *Ty) = 0;
446
447   /// convertInitializerBitRange - This method is used to implement the bitrange
448   /// selection operator.  Given an initializer, it selects the specified bits
449   /// out, returning them as a new init of bits type.  If it is not legal to use
450   /// the bit subscript operator on this initializer, return null.
451   ///
452   virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits) {
453     return 0;
454   }
455
456   /// convertInitListSlice - This method is used to implement the list slice
457   /// selection operator.  Given an initializer, it selects the specified list
458   /// elements, returning them as a new init of list type.  If it is not legal
459   /// to take a slice of this, return null.
460   ///
461   virtual Init *convertInitListSlice(const std::vector<unsigned> &Elements) {
462     return 0;
463   }
464
465   /// getFieldType - This method is used to implement the FieldInit class.
466   /// Implementors of this method should return the type of the named field if
467   /// they are of record type.
468   ///
469   virtual RecTy *getFieldType(const std::string &FieldName) const { return 0; }
470
471   /// getFieldInit - This method complements getFieldType to return the
472   /// initializer for the specified field.  If getFieldType returns non-null
473   /// this method should return non-null, otherwise it returns null.
474   ///
475   virtual Init *getFieldInit(Record &R, const std::string &FieldName) const {
476     return 0;
477   }
478
479   /// resolveReferences - This method is used by classes that refer to other
480   /// variables which may not be defined at the time they expression is formed.
481   /// If a value is set for the variable later, this method will be called on
482   /// users of the value to allow the value to propagate out.
483   ///
484   virtual Init *resolveReferences(Record &R, const RecordVal *RV) {
485     return this;
486   }
487 };
488
489 inline std::ostream &operator<<(std::ostream &OS, const Init &I) {
490   I.print(OS); return OS;
491 }
492
493
494 /// UnsetInit - ? - Represents an uninitialized value
495 ///
496 class UnsetInit : public Init {
497 public:
498   virtual Init *convertInitializerTo(RecTy *Ty) {
499     return Ty->convertValue(this);
500   }
501
502   virtual bool isComplete() const { return false; }
503   virtual std::string getAsString() const { return "?"; }
504 };
505
506
507 /// BitInit - true/false - Represent a concrete initializer for a bit.
508 ///
509 class BitInit : public Init {
510   bool Value;
511 public:
512   BitInit(bool V) : Value(V) {}
513
514   bool getValue() const { return Value; }
515
516   virtual Init *convertInitializerTo(RecTy *Ty) {
517     return Ty->convertValue(this);
518   }
519
520   virtual std::string getAsString() const { return Value ? "1" : "0"; }
521 };
522
523 /// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
524 /// It contains a vector of bits, whose size is determined by the type.
525 ///
526 class BitsInit : public Init {
527   std::vector<Init*> Bits;
528 public:
529   BitsInit(unsigned Size) : Bits(Size) {}
530
531   unsigned getNumBits() const { return Bits.size(); }
532
533   Init *getBit(unsigned Bit) const {
534     assert(Bit < Bits.size() && "Bit index out of range!");
535     return Bits[Bit];
536   }
537   void setBit(unsigned Bit, Init *V) {
538     assert(Bit < Bits.size() && "Bit index out of range!");
539     assert(Bits[Bit] == 0 && "Bit already set!");
540     Bits[Bit] = V;
541   }
542
543   virtual Init *convertInitializerTo(RecTy *Ty) {
544     return Ty->convertValue(this);
545   }
546   virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
547
548   virtual bool isComplete() const {
549     for (unsigned i = 0; i != getNumBits(); ++i)
550       if (!getBit(i)->isComplete()) return false;
551     return true;
552   }
553   virtual std::string getAsString() const;
554
555   virtual Init *resolveReferences(Record &R, const RecordVal *RV);
556
557   // printXX - Print this bitstream with the specified format, returning true if
558   // it is not possible.
559   bool printInHex(std::ostream &OS) const;
560   bool printAsVariable(std::ostream &OS) const;
561   bool printAsUnset(std::ostream &OS) const;
562 };
563
564
565 /// IntInit - 7 - Represent an initalization by a literal integer value.
566 ///
567 class IntInit : public Init {
568   int Value;
569 public:
570   IntInit(int V) : Value(V) {}
571
572   int getValue() const { return Value; }
573
574   virtual Init *convertInitializerTo(RecTy *Ty) {
575     return Ty->convertValue(this);
576   }
577   virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
578
579   virtual std::string getAsString() const;
580 };
581
582
583 /// StringInit - "foo" - Represent an initialization by a string value.
584 ///
585 class StringInit : public Init {
586   std::string Value;
587 public:
588   StringInit(const std::string &V) : Value(V) {}
589
590   const std::string &getValue() const { return Value; }
591
592   virtual Init *convertInitializerTo(RecTy *Ty) {
593     return Ty->convertValue(this);
594   }
595
596   virtual std::string getAsString() const { return "\"" + Value + "\""; }
597 };
598
599 /// CodeInit - "[{...}]" - Represent a code fragment.
600 ///
601 class CodeInit : public Init {
602   std::string Value;
603 public:
604   CodeInit(const std::string &V) : Value(V) {}
605
606   const std::string getValue() const { return Value; }
607
608   virtual Init *convertInitializerTo(RecTy *Ty) {
609     return Ty->convertValue(this);
610   }
611
612   virtual std::string getAsString() const { return "[{" + Value + "}]"; }
613 };
614
615 /// ListInit - [AL, AH, CL] - Represent a list of defs
616 ///
617 class ListInit : public Init {
618   std::vector<Init*> Values;
619 public:
620   ListInit(std::vector<Init*> &Vs) {
621     Values.swap(Vs);
622   }
623
624   unsigned getSize() const { return Values.size(); }
625   Init *getElement(unsigned i) const {
626     assert(i < Values.size() && "List element index out of range!");
627     return Values[i];
628   }
629
630   Record *getElementAsRecord(unsigned i) const;
631   
632   Init *convertInitListSlice(const std::vector<unsigned> &Elements);
633
634   virtual Init *convertInitializerTo(RecTy *Ty) {
635     return Ty->convertValue(this);
636   }
637
638   /// resolveReferences - This method is used by classes that refer to other
639   /// variables which may not be defined at the time they expression is formed.
640   /// If a value is set for the variable later, this method will be called on
641   /// users of the value to allow the value to propagate out.
642   ///
643   virtual Init *resolveReferences(Record &R, const RecordVal *RV);
644
645   virtual std::string getAsString() const;
646
647   typedef std::vector<Init*>::iterator       iterator;
648   typedef std::vector<Init*>::const_iterator const_iterator;
649
650   inline iterator       begin()       { return Values.begin(); }
651   inline const_iterator begin() const { return Values.begin(); }
652   inline iterator       end  ()       { return Values.end();   }
653   inline const_iterator end  () const { return Values.end();   }
654
655   inline size_t         size () const { return Values.size();  }
656   inline bool           empty() const { return Values.empty(); }
657 };
658
659 /// BinOpInit - !op (X, Y) - Combine two inits.
660 ///
661 class BinOpInit : public Init {
662 public:
663   enum BinaryOp { SHL, SRA, SRL, STRCONCAT, CONCAT };
664 private:
665   BinaryOp Opc;
666   Init *LHS, *RHS;
667 public:
668   BinOpInit(BinaryOp opc, Init *lhs, Init *rhs) : Opc(opc), LHS(lhs), RHS(rhs) {
669   }
670   
671   BinaryOp getOpcode() const { return Opc; }
672   Init *getLHS() const { return LHS; }
673   Init *getRHS() const { return RHS; }
674
675   // Fold - If possible, fold this to a simpler init.  Return this if not
676   // possible to fold.
677   Init *Fold();
678
679   virtual Init *convertInitializerTo(RecTy *Ty) {
680     return Ty->convertValue(this);
681   }
682   
683   virtual Init *resolveReferences(Record &R, const RecordVal *RV);
684   
685   virtual std::string getAsString() const;
686 };
687
688
689
690 /// TypedInit - This is the common super-class of types that have a specific,
691 /// explicit, type.
692 ///
693 class TypedInit : public Init {
694   RecTy *Ty;
695 public:
696   TypedInit(RecTy *T) : Ty(T) {}
697
698   RecTy *getType() const { return Ty; }
699
700   virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
701   virtual Init *convertInitListSlice(const std::vector<unsigned> &Elements);
702
703   /// resolveBitReference - This method is used to implement
704   /// VarBitInit::resolveReferences.  If the bit is able to be resolved, we
705   /// simply return the resolved value, otherwise we return null.
706   ///
707   virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
708                                     unsigned Bit) = 0;
709
710   /// resolveListElementReference - This method is used to implement
711   /// VarListElementInit::resolveReferences.  If the list element is resolvable
712   /// now, we return the resolved value, otherwise we return null.
713   virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
714                                             unsigned Elt) = 0;
715 };
716
717 /// VarInit - 'Opcode' - Represent a reference to an entire variable object.
718 ///
719 class VarInit : public TypedInit {
720   std::string VarName;
721 public:
722   VarInit(const std::string &VN, RecTy *T) : TypedInit(T), VarName(VN) {}
723
724   virtual Init *convertInitializerTo(RecTy *Ty) {
725     return Ty->convertValue(this);
726   }
727
728   const std::string &getName() const { return VarName; }
729
730   virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
731                                     unsigned Bit);
732   virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
733                                             unsigned Elt);
734
735   virtual RecTy *getFieldType(const std::string &FieldName) const;
736   virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
737
738   /// resolveReferences - This method is used by classes that refer to other
739   /// variables which may not be defined at the time they expression is formed.
740   /// If a value is set for the variable later, this method will be called on
741   /// users of the value to allow the value to propagate out.
742   ///
743   virtual Init *resolveReferences(Record &R, const RecordVal *RV);
744
745   virtual std::string getAsString() const { return VarName; }
746 };
747
748
749 /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
750 ///
751 class VarBitInit : public Init {
752   TypedInit *TI;
753   unsigned Bit;
754 public:
755   VarBitInit(TypedInit *T, unsigned B) : TI(T), Bit(B) {
756     assert(T->getType() && dynamic_cast<BitsRecTy*>(T->getType()) &&
757            ((BitsRecTy*)T->getType())->getNumBits() > B &&
758            "Illegal VarBitInit expression!");
759   }
760
761   virtual Init *convertInitializerTo(RecTy *Ty) {
762     return Ty->convertValue(this);
763   }
764
765   TypedInit *getVariable() const { return TI; }
766   unsigned getBitNum() const { return Bit; }
767
768   virtual std::string getAsString() const;
769   virtual Init *resolveReferences(Record &R, const RecordVal *RV);
770 };
771
772 /// VarListElementInit - List[4] - Represent access to one element of a var or
773 /// field.
774 class VarListElementInit : public TypedInit {
775   TypedInit *TI;
776   unsigned Element;
777 public:
778   VarListElementInit(TypedInit *T, unsigned E)
779     : TypedInit(dynamic_cast<ListRecTy*>(T->getType())->getElementType()),
780                 TI(T), Element(E) {
781     assert(T->getType() && dynamic_cast<ListRecTy*>(T->getType()) &&
782            "Illegal VarBitInit expression!");
783   }
784
785   virtual Init *convertInitializerTo(RecTy *Ty) {
786     return Ty->convertValue(this);
787   }
788
789   TypedInit *getVariable() const { return TI; }
790   unsigned getElementNum() const { return Element; }
791
792   virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
793                                     unsigned Bit);
794
795   /// resolveListElementReference - This method is used to implement
796   /// VarListElementInit::resolveReferences.  If the list element is resolvable
797   /// now, we return the resolved value, otherwise we return null.
798   virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
799                                             unsigned Elt);
800
801   virtual std::string getAsString() const;
802   virtual Init *resolveReferences(Record &R, const RecordVal *RV);
803 };
804
805 /// DefInit - AL - Represent a reference to a 'def' in the description
806 ///
807 class DefInit : public Init {
808   Record *Def;
809 public:
810   DefInit(Record *D) : Def(D) {}
811
812   virtual Init *convertInitializerTo(RecTy *Ty) {
813     return Ty->convertValue(this);
814   }
815
816   Record *getDef() const { return Def; }
817
818   //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
819
820   virtual RecTy *getFieldType(const std::string &FieldName) const;
821   virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
822
823   virtual std::string getAsString() const;
824 };
825
826
827 /// FieldInit - X.Y - Represent a reference to a subfield of a variable
828 ///
829 class FieldInit : public TypedInit {
830   Init *Rec;                // Record we are referring to
831   std::string FieldName;    // Field we are accessing
832 public:
833   FieldInit(Init *R, const std::string &FN)
834     : TypedInit(R->getFieldType(FN)), Rec(R), FieldName(FN) {
835     assert(getType() && "FieldInit with non-record type!");
836   }
837
838   virtual Init *convertInitializerTo(RecTy *Ty) {
839     return Ty->convertValue(this);
840   }
841
842   virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
843                                     unsigned Bit);
844   virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
845                                             unsigned Elt);
846
847   virtual Init *resolveReferences(Record &R, const RecordVal *RV);
848
849   virtual std::string getAsString() const {
850     return Rec->getAsString() + "." + FieldName;
851   }
852 };
853
854 /// DagInit - (v a, b) - Represent a DAG tree value.  DAG inits are required
855 /// to have at least one value then a (possibly empty) list of arguments.  Each
856 /// argument can have a name associated with it.
857 ///
858 class DagInit : public Init {
859   Init *Val;
860   std::vector<Init*> Args;
861   std::vector<std::string> ArgNames;
862 public:
863   DagInit(Init *V, const std::vector<std::pair<Init*, std::string> > &args)
864     : Val(V) {
865     Args.reserve(args.size());
866     ArgNames.reserve(args.size());
867     for (unsigned i = 0, e = args.size(); i != e; ++i) {
868       Args.push_back(args[i].first);
869       ArgNames.push_back(args[i].second);
870     }
871   }
872   DagInit(Init *V, const std::vector<Init*> &args, 
873           const std::vector<std::string> &argNames)
874   : Val(V), Args(args), ArgNames(argNames) {
875   }
876   
877   virtual Init *convertInitializerTo(RecTy *Ty) {
878     return Ty->convertValue(this);
879   }
880
881   Init *getOperator() const { return Val; }
882
883   unsigned getNumArgs() const { return Args.size(); }
884   Init *getArg(unsigned Num) const {
885     assert(Num < Args.size() && "Arg number out of range!");
886     return Args[Num];
887   }
888   const std::string &getArgName(unsigned Num) const {
889     assert(Num < ArgNames.size() && "Arg number out of range!");
890     return ArgNames[Num];
891   }
892
893   void setArg(unsigned Num, Init *I) {
894     assert(Num < Args.size() && "Arg number out of range!");
895     Args[Num] = I;
896   }
897   
898   virtual Init *resolveReferences(Record &R, const RecordVal *RV);
899
900   virtual std::string getAsString() const;
901
902   typedef std::vector<Init*>::iterator             arg_iterator;
903   typedef std::vector<Init*>::const_iterator       const_arg_iterator;
904   typedef std::vector<std::string>::iterator       name_iterator;
905   typedef std::vector<std::string>::const_iterator const_name_iterator;
906
907   inline arg_iterator        arg_begin()       { return Args.begin(); }
908   inline const_arg_iterator  arg_begin() const { return Args.begin(); }
909   inline arg_iterator        arg_end  ()       { return Args.end();   }
910   inline const_arg_iterator  arg_end  () const { return Args.end();   }
911
912   inline size_t              arg_size () const { return Args.size();  }
913   inline bool                arg_empty() const { return Args.empty(); }
914
915   inline name_iterator       name_begin()       { return ArgNames.begin(); }
916   inline const_name_iterator name_begin() const { return ArgNames.begin(); }
917   inline name_iterator       name_end  ()       { return ArgNames.end();   }
918   inline const_name_iterator name_end  () const { return ArgNames.end();   }
919
920   inline size_t              name_size () const { return ArgNames.size();  }
921   inline bool                name_empty() const { return ArgNames.empty(); }
922
923 };
924
925 //===----------------------------------------------------------------------===//
926 //  High-Level Classes
927 //===----------------------------------------------------------------------===//
928
929 class RecordVal {
930   std::string Name;
931   RecTy *Ty;
932   unsigned Prefix;
933   Init *Value;
934 public:
935   RecordVal(const std::string &N, RecTy *T, unsigned P);
936
937   const std::string &getName() const { return Name; }
938
939   unsigned getPrefix() const { return Prefix; }
940   RecTy *getType() const { return Ty; }
941   Init *getValue() const { return Value; }
942
943   bool setValue(Init *V) {
944     if (V) {
945       Value = V->convertInitializerTo(Ty);
946       return Value == 0;
947     }
948     Value = 0;
949     return false;
950   }
951
952   void dump() const;
953   void print(std::ostream &OS, bool PrintSem = true) const;
954 };
955
956 inline std::ostream &operator<<(std::ostream &OS, const RecordVal &RV) {
957   RV.print(OS << "  ");
958   return OS;
959 }
960
961 class Record {
962   std::string Name;
963   std::vector<std::string> TemplateArgs;
964   std::vector<RecordVal> Values;
965   std::vector<Record*> SuperClasses;
966 public:
967
968   Record(const std::string &N) : Name(N) {}
969   ~Record() {}
970
971   const std::string &getName() const { return Name; }
972   void setName(const std::string &Name);  // Also updates RecordKeeper.
973   const std::vector<std::string> &getTemplateArgs() const {
974     return TemplateArgs;
975   }
976   const std::vector<RecordVal> &getValues() const { return Values; }
977   const std::vector<Record*>   &getSuperClasses() const { return SuperClasses; }
978
979   bool isTemplateArg(const std::string &Name) const {
980     for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
981       if (TemplateArgs[i] == Name) return true;
982     return false;
983   }
984
985   const RecordVal *getValue(const std::string &Name) const {
986     for (unsigned i = 0, e = Values.size(); i != e; ++i)
987       if (Values[i].getName() == Name) return &Values[i];
988     return 0;
989   }
990   RecordVal *getValue(const std::string &Name) {
991     for (unsigned i = 0, e = Values.size(); i != e; ++i)
992       if (Values[i].getName() == Name) return &Values[i];
993     return 0;
994   }
995
996   void addTemplateArg(const std::string &Name) {
997     assert(!isTemplateArg(Name) && "Template arg already defined!");
998     TemplateArgs.push_back(Name);
999   }
1000
1001   void addValue(const RecordVal &RV) {
1002     assert(getValue(RV.getName()) == 0 && "Value already added!");
1003     Values.push_back(RV);
1004   }
1005
1006   void removeValue(const std::string &Name) {
1007     assert(getValue(Name) && "Cannot remove an entry that does not exist!");
1008     for (unsigned i = 0, e = Values.size(); i != e; ++i)
1009       if (Values[i].getName() == Name) {
1010         Values.erase(Values.begin()+i);
1011         return;
1012       }
1013     assert(0 && "Name does not exist in record!");
1014   }
1015
1016   bool isSubClassOf(Record *R) const {
1017     for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1018       if (SuperClasses[i] == R)
1019         return true;
1020     return false;
1021   }
1022
1023   bool isSubClassOf(const std::string &Name) const {
1024     for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1025       if (SuperClasses[i]->getName() == Name)
1026         return true;
1027     return false;
1028   }
1029
1030   void addSuperClass(Record *R) {
1031     assert(!isSubClassOf(R) && "Already subclassing record!");
1032     SuperClasses.push_back(R);
1033   }
1034
1035   /// resolveReferences - If there are any field references that refer to fields
1036   /// that have been filled in, we can propagate the values now.
1037   ///
1038   void resolveReferences() { resolveReferencesTo(0); }
1039
1040   /// resolveReferencesTo - If anything in this record refers to RV, replace the
1041   /// reference to RV with the RHS of RV.  If RV is null, we resolve all
1042   /// possible references.
1043   void resolveReferencesTo(const RecordVal *RV);
1044
1045   void dump() const;
1046
1047   //===--------------------------------------------------------------------===//
1048   // High-level methods useful to tablegen back-ends
1049   //
1050
1051   /// getValueInit - Return the initializer for a value with the specified name,
1052   /// or throw an exception if the field does not exist.
1053   ///
1054   Init *getValueInit(const std::string &FieldName) const;
1055
1056   /// getValueAsString - This method looks up the specified field and returns
1057   /// its value as a string, throwing an exception if the field does not exist
1058   /// or if the value is not a string.
1059   ///
1060   std::string getValueAsString(const std::string &FieldName) const;
1061
1062   /// getValueAsBitsInit - This method looks up the specified field and returns
1063   /// its value as a BitsInit, throwing an exception if the field does not exist
1064   /// or if the value is not the right type.
1065   ///
1066   BitsInit *getValueAsBitsInit(const std::string &FieldName) const;
1067
1068   /// getValueAsListInit - This method looks up the specified field and returns
1069   /// its value as a ListInit, throwing an exception if the field does not exist
1070   /// or if the value is not the right type.
1071   ///
1072   ListInit *getValueAsListInit(const std::string &FieldName) const;
1073
1074   /// getValueAsListOfDefs - This method looks up the specified field and
1075   /// returns its value as a vector of records, throwing an exception if the
1076   /// field does not exist or if the value is not the right type.
1077   ///
1078   std::vector<Record*> getValueAsListOfDefs(const std::string &FieldName) const;
1079
1080   /// getValueAsListOfInts - This method looks up the specified field and returns
1081   /// its value as a vector of integers, throwing an exception if the field does
1082   /// not exist or if the value is not the right type.
1083   ///
1084   std::vector<int> getValueAsListOfInts(const std::string &FieldName) const;
1085   
1086   /// getValueAsDef - This method looks up the specified field and returns its
1087   /// value as a Record, throwing an exception if the field does not exist or if
1088   /// the value is not the right type.
1089   ///
1090   Record *getValueAsDef(const std::string &FieldName) const;
1091
1092   /// getValueAsBit - This method looks up the specified field and returns its
1093   /// value as a bit, throwing an exception if the field does not exist or if
1094   /// the value is not the right type.
1095   ///
1096   bool getValueAsBit(const std::string &FieldName) const;
1097
1098   /// getValueAsInt - This method looks up the specified field and returns its
1099   /// value as an int, throwing an exception if the field does not exist or if
1100   /// the value is not the right type.
1101   ///
1102   int getValueAsInt(const std::string &FieldName) const;
1103
1104   /// getValueAsDag - This method looks up the specified field and returns its
1105   /// value as an Dag, throwing an exception if the field does not exist or if
1106   /// the value is not the right type.
1107   ///
1108   DagInit *getValueAsDag(const std::string &FieldName) const;
1109   
1110   /// getValueAsCode - This method looks up the specified field and returns
1111   /// its value as the string data in a CodeInit, throwing an exception if the
1112   /// field does not exist or if the value is not a code object.
1113   ///
1114   std::string getValueAsCode(const std::string &FieldName) const;
1115 };
1116
1117 std::ostream &operator<<(std::ostream &OS, const Record &R);
1118
1119 class RecordKeeper {
1120   std::map<std::string, Record*> Classes, Defs;
1121 public:
1122   ~RecordKeeper() {
1123     for (std::map<std::string, Record*>::iterator I = Classes.begin(),
1124            E = Classes.end(); I != E; ++I)
1125       delete I->second;
1126     for (std::map<std::string, Record*>::iterator I = Defs.begin(),
1127            E = Defs.end(); I != E; ++I)
1128       delete I->second;
1129   }
1130
1131   const std::map<std::string, Record*> &getClasses() const { return Classes; }
1132   const std::map<std::string, Record*> &getDefs() const { return Defs; }
1133
1134   Record *getClass(const std::string &Name) const {
1135     std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
1136     return I == Classes.end() ? 0 : I->second;
1137   }
1138   Record *getDef(const std::string &Name) const {
1139     std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
1140     return I == Defs.end() ? 0 : I->second;
1141   }
1142   void addClass(Record *R) {
1143     assert(getClass(R->getName()) == 0 && "Class already exists!");
1144     Classes.insert(std::make_pair(R->getName(), R));
1145   }
1146   void addDef(Record *R) {
1147     assert(getDef(R->getName()) == 0 && "Def already exists!");
1148     Defs.insert(std::make_pair(R->getName(), R));
1149   }
1150
1151   /// removeClass - Remove, but do not delete, the specified record.
1152   ///
1153   void removeClass(const std::string &Name) {
1154     assert(Classes.count(Name) && "Class does not exist!");
1155     Classes.erase(Name);
1156   }
1157   /// removeDef - Remove, but do not delete, the specified record.
1158   ///
1159   void removeDef(const std::string &Name) {
1160     assert(Defs.count(Name) && "Def does not exist!");
1161     Defs.erase(Name);
1162   }
1163   
1164   //===--------------------------------------------------------------------===//
1165   // High-level helper methods, useful for tablegen backends...
1166
1167   /// getAllDerivedDefinitions - This method returns all concrete definitions
1168   /// that derive from the specified class name.  If a class with the specified
1169   /// name does not exist, an exception is thrown.
1170   std::vector<Record*>
1171   getAllDerivedDefinitions(const std::string &ClassName) const;
1172
1173
1174   void dump() const;
1175 };
1176
1177 std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK);
1178
1179 extern RecordKeeper Records;
1180
1181 } // End llvm namespace
1182
1183 #endif