[AVX] Make ListInits Unique
[oota-llvm.git] / utils / TableGen / Record.h
1
2 //===- Record.h - Classes to represent Table Records ------------*- C++ -*-===//
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10 //
11 // This file defines the main TableGen data structures, including the TableGen
12 // types, values, and high-level data structures.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef RECORD_H
17 #define RECORD_H
18
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/FoldingSet.h"
21 #include "llvm/Support/Allocator.h"
22 #include "llvm/Support/SourceMgr.h"
23 #include "llvm/Support/DataTypes.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include <map>
26
27 namespace llvm {
28 class raw_ostream;
29
30 // RecTy subclasses.
31 class BitRecTy;
32 class BitsRecTy;
33 class IntRecTy;
34 class StringRecTy;
35 class ListRecTy;
36 class CodeRecTy;
37 class DagRecTy;
38 class RecordRecTy;
39
40 // Init subclasses.
41 class Init;
42 class UnsetInit;
43 class BitInit;
44 class BitsInit;
45 class IntInit;
46 class StringInit;
47 class CodeInit;
48 class ListInit;
49 class UnOpInit;
50 class BinOpInit;
51 class TernOpInit;
52 class DefInit;
53 class DagInit;
54 class TypedInit;
55 class VarInit;
56 class FieldInit;
57 class VarBitInit;
58 class VarListElementInit;
59
60 // Other classes.
61 class Record;
62 class RecordVal;
63 struct MultiClass;
64 class RecordKeeper;
65
66 //===----------------------------------------------------------------------===//
67 //  Type Classes
68 //===----------------------------------------------------------------------===//
69
70 class RecTy {
71   ListRecTy *ListTy;
72 public:
73   RecTy() : ListTy(0) {}
74   virtual ~RecTy() {}
75
76   virtual std::string getAsString() const = 0;
77   void print(raw_ostream &OS) const { OS << getAsString(); }
78   void dump() const;
79
80   /// typeIsConvertibleTo - Return true if all values of 'this' type can be
81   /// converted to the specified type.
82   virtual bool typeIsConvertibleTo(const RecTy *RHS) const = 0;
83
84   /// getListTy - Returns the type representing list<this>.
85   ListRecTy *getListTy();
86
87 public:   // These methods should only be called from subclasses of Init
88   virtual const Init *convertValue( const UnsetInit *UI) { return 0; }
89   virtual const Init *convertValue(   const BitInit *BI) { return 0; }
90   virtual const Init *convertValue(  const BitsInit *BI) { return 0; }
91   virtual const Init *convertValue(   const IntInit *II) { return 0; }
92   virtual const Init *convertValue(const StringInit *SI) { return 0; }
93   virtual const Init *convertValue(  const ListInit *LI) { return 0; }
94   virtual const Init *convertValue( const UnOpInit *UI) {
95     return convertValue((const TypedInit*)UI);
96   }
97   virtual const Init *convertValue( const BinOpInit *UI) {
98     return convertValue((const TypedInit*)UI);
99   }
100   virtual const Init *convertValue( const TernOpInit *UI) {
101     return convertValue((const TypedInit*)UI);
102   }
103   virtual const Init *convertValue(  const CodeInit *CI) { return 0; }
104   virtual const Init *convertValue(const VarBitInit *VB) { return 0; }
105   virtual const Init *convertValue(   const DefInit *DI) { return 0; }
106   virtual const Init *convertValue(   const DagInit *DI) { return 0; }
107   virtual const Init *convertValue( const TypedInit *TI) { return 0; }
108   virtual const Init *convertValue(   const VarInit *VI) {
109     return convertValue((const TypedInit*)VI);
110   }
111   virtual const Init *convertValue( const FieldInit *FI) {
112     return convertValue((const TypedInit*)FI);
113   }
114
115 public:   // These methods should only be called by subclasses of RecTy.
116   // baseClassOf - These virtual methods should be overloaded to return true iff
117   // all values of type 'RHS' can be converted to the 'this' type.
118   virtual bool baseClassOf(const BitRecTy    *RHS) const { return false; }
119   virtual bool baseClassOf(const BitsRecTy   *RHS) const { return false; }
120   virtual bool baseClassOf(const IntRecTy    *RHS) const { return false; }
121   virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
122   virtual bool baseClassOf(const ListRecTy   *RHS) const { return false; }
123   virtual bool baseClassOf(const CodeRecTy   *RHS) const { return false; }
124   virtual bool baseClassOf(const DagRecTy    *RHS) const { return false; }
125   virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
126 };
127
128 inline raw_ostream &operator<<(raw_ostream &OS, const RecTy &Ty) {
129   Ty.print(OS);
130   return OS;
131 }
132
133
134 /// BitRecTy - 'bit' - Represent a single bit
135 ///
136 class BitRecTy : public RecTy {
137   static BitRecTy Shared;
138   BitRecTy() {}
139 public:
140   static BitRecTy *get() { return &Shared; }
141
142   virtual const Init *convertValue( const UnsetInit *UI) { return (const Init*)UI; }
143   virtual const Init *convertValue(   const BitInit *BI) { return (const Init*)BI; }
144   virtual const Init *convertValue(  const BitsInit *BI);
145   virtual const Init *convertValue(   const IntInit *II);
146   virtual const Init *convertValue(const StringInit *SI) { return 0; }
147   virtual const Init *convertValue(  const ListInit *LI) { return 0; }
148   virtual const Init *convertValue(  const CodeInit *CI) { return 0; }
149   virtual const Init *convertValue(const VarBitInit *VB) { return (const Init*)VB; }
150   virtual const Init *convertValue(   const DefInit *DI) { return 0; }
151   virtual const Init *convertValue(   const DagInit *DI) { return 0; }
152   virtual const Init *convertValue( const UnOpInit *UI) { return RecTy::convertValue(UI);}
153   virtual const Init *convertValue( const BinOpInit *UI) { return RecTy::convertValue(UI);}
154   virtual const Init *convertValue( const TernOpInit *UI) { return RecTy::convertValue(UI);}
155   virtual const Init *convertValue( const TypedInit *TI);
156   virtual const Init *convertValue(   const VarInit *VI) { return RecTy::convertValue(VI);}
157   virtual const Init *convertValue( const FieldInit *FI) { return RecTy::convertValue(FI);}
158
159   std::string getAsString() const { return "bit"; }
160
161   bool typeIsConvertibleTo(const RecTy *RHS) const {
162     return RHS->baseClassOf(this);
163   }
164   virtual bool baseClassOf(const BitRecTy    *RHS) const { return true; }
165   virtual bool baseClassOf(const BitsRecTy   *RHS) const;
166   virtual bool baseClassOf(const IntRecTy    *RHS) const { return true; }
167   virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
168   virtual bool baseClassOf(const ListRecTy   *RHS) const { return false; }
169   virtual bool baseClassOf(const CodeRecTy   *RHS) const { return false; }
170   virtual bool baseClassOf(const DagRecTy    *RHS) const { return false; }
171   virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
172
173 };
174
175
176 // BitsRecTy - 'bits<n>' - Represent a fixed number of bits
177 /// BitsRecTy - 'bits&lt;n&gt;' - Represent a fixed number of bits
178 ///
179 class BitsRecTy : public RecTy {
180   unsigned Size;
181   explicit BitsRecTy(unsigned Sz) : Size(Sz) {}
182 public:
183   static BitsRecTy *get(unsigned Sz);
184
185   unsigned getNumBits() const { return Size; }
186
187   virtual const Init *convertValue( const UnsetInit *UI);
188   virtual const Init *convertValue(   const BitInit *UI);
189   virtual const Init *convertValue(  const BitsInit *BI);
190   virtual const Init *convertValue(   const IntInit *II);
191   virtual const Init *convertValue(const StringInit *SI) { return 0; }
192   virtual const Init *convertValue(  const ListInit *LI) { return 0; }
193   virtual const Init *convertValue(  const CodeInit *CI) { return 0; }
194   virtual const Init *convertValue(const VarBitInit *VB) { return 0; }
195   virtual const Init *convertValue(   const DefInit *DI) { return 0; }
196   virtual const Init *convertValue(   const DagInit *DI) { return 0; }
197   virtual const Init *convertValue( const UnOpInit *UI) { return RecTy::convertValue(UI);}
198   virtual const Init *convertValue( const BinOpInit *UI) { return RecTy::convertValue(UI);}
199   virtual const Init *convertValue( const TernOpInit *UI) { return RecTy::convertValue(UI);}
200   virtual const Init *convertValue( const TypedInit *TI);
201   virtual const Init *convertValue(   const VarInit *VI) { return RecTy::convertValue(VI);}
202   virtual const Init *convertValue( const FieldInit *FI) { return RecTy::convertValue(FI);}
203
204   std::string getAsString() const;
205
206   bool typeIsConvertibleTo(const RecTy *RHS) const {
207     return RHS->baseClassOf(this);
208   }
209   virtual bool baseClassOf(const BitRecTy    *RHS) const { return Size == 1; }
210   virtual bool baseClassOf(const BitsRecTy   *RHS) const {
211     return RHS->Size == Size;
212   }
213   virtual bool baseClassOf(const IntRecTy    *RHS) const { return true; }
214   virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
215   virtual bool baseClassOf(const ListRecTy   *RHS) const { return false; }
216   virtual bool baseClassOf(const CodeRecTy   *RHS) const { return false; }
217   virtual bool baseClassOf(const DagRecTy    *RHS) const { return false; }
218   virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
219
220 };
221
222
223 /// IntRecTy - 'int' - Represent an integer value of no particular size
224 ///
225 class IntRecTy : public RecTy {
226   static IntRecTy Shared;
227   IntRecTy() {}
228 public:
229   static IntRecTy *get() { return &Shared; }
230
231   virtual const Init *convertValue( const UnsetInit *UI) { return (const Init*)UI; }
232   virtual const Init *convertValue(   const BitInit *BI);
233   virtual const Init *convertValue(  const BitsInit *BI);
234   virtual const Init *convertValue(   const IntInit *II) { return (const Init*)II; }
235   virtual const Init *convertValue(const StringInit *SI) { return 0; }
236   virtual const Init *convertValue(  const ListInit *LI) { return 0; }
237   virtual const Init *convertValue(  const CodeInit *CI) { return 0; }
238   virtual const Init *convertValue(const VarBitInit *VB) { return 0; }
239   virtual const Init *convertValue(   const DefInit *DI) { return 0; }
240   virtual const Init *convertValue(   const DagInit *DI) { return 0; }
241   virtual const Init *convertValue( const UnOpInit *UI) { return RecTy::convertValue(UI);}
242   virtual const Init *convertValue( const BinOpInit *UI) { return RecTy::convertValue(UI);}
243   virtual const Init *convertValue( const TernOpInit *UI) { return RecTy::convertValue(UI);}
244   virtual const Init *convertValue( const TypedInit *TI);
245   virtual const Init *convertValue(   const VarInit *VI) { return RecTy::convertValue(VI);}
246   virtual const Init *convertValue( const FieldInit *FI) { return RecTy::convertValue(FI);}
247
248   std::string getAsString() const { return "int"; }
249
250   bool typeIsConvertibleTo(const RecTy *RHS) const {
251     return RHS->baseClassOf(this);
252   }
253
254   virtual bool baseClassOf(const BitRecTy    *RHS) const { return true; }
255   virtual bool baseClassOf(const BitsRecTy   *RHS) const { return true; }
256   virtual bool baseClassOf(const IntRecTy    *RHS) const { return true; }
257   virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
258   virtual bool baseClassOf(const ListRecTy   *RHS) const { return false; }
259   virtual bool baseClassOf(const CodeRecTy   *RHS) const { return false; }
260   virtual bool baseClassOf(const DagRecTy    *RHS) const { return false; }
261   virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
262
263 };
264
265 /// StringRecTy - 'string' - Represent an string value
266 ///
267 class StringRecTy : public RecTy {
268   static StringRecTy Shared;
269   StringRecTy() {}
270 public:
271   static StringRecTy *get() { return &Shared; }
272
273   virtual const Init *convertValue( const UnsetInit *UI) { return (const Init*)UI; }
274   virtual const Init *convertValue(   const BitInit *BI) { return 0; }
275   virtual const Init *convertValue(  const BitsInit *BI) { return 0; }
276   virtual const Init *convertValue(   const IntInit *II) { return 0; }
277   virtual const Init *convertValue(const StringInit *SI) { return (const Init*)SI; }
278   virtual const Init *convertValue(  const ListInit *LI) { return 0; }
279   virtual const Init *convertValue( const UnOpInit *BO);
280   virtual const Init *convertValue( const BinOpInit *BO);
281   virtual const Init *convertValue( const TernOpInit *BO) { return RecTy::convertValue(BO);}
282
283   virtual const Init *convertValue(  const CodeInit *CI) { return 0; }
284   virtual const Init *convertValue(const VarBitInit *VB) { return 0; }
285   virtual const Init *convertValue(   const DefInit *DI) { return 0; }
286   virtual const Init *convertValue(   const DagInit *DI) { return 0; }
287   virtual const Init *convertValue( const TypedInit *TI);
288   virtual const Init *convertValue(   const VarInit *VI) { return RecTy::convertValue(VI);}
289   virtual const Init *convertValue( const FieldInit *FI) { return RecTy::convertValue(FI);}
290
291   std::string getAsString() const { return "string"; }
292
293   bool typeIsConvertibleTo(const RecTy *RHS) const {
294     return RHS->baseClassOf(this);
295   }
296
297   virtual bool baseClassOf(const BitRecTy    *RHS) const { return false; }
298   virtual bool baseClassOf(const BitsRecTy   *RHS) const { return false; }
299   virtual bool baseClassOf(const IntRecTy    *RHS) const { return false; }
300   virtual bool baseClassOf(const StringRecTy *RHS) const { return true; }
301   virtual bool baseClassOf(const ListRecTy   *RHS) const { return false; }
302   virtual bool baseClassOf(const CodeRecTy   *RHS) const { return false; }
303   virtual bool baseClassOf(const DagRecTy    *RHS) const { return false; }
304   virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
305 };
306
307 // ListRecTy - 'list<Ty>' - Represent a list of values, all of which must be of
308 // the specified type.
309 /// ListRecTy - 'list&lt;Ty&gt;' - Represent a list of values, all of which must
310 /// be of the specified type.
311 ///
312 class ListRecTy : public RecTy {
313   RecTy *Ty;
314   explicit ListRecTy(RecTy *T) : Ty(T) {}
315   friend ListRecTy *RecTy::getListTy();
316 public:
317   static ListRecTy *get(RecTy *T) { return T->getListTy(); }
318   RecTy *getElementType() const { return Ty; }
319
320   virtual const Init *convertValue( const UnsetInit *UI) { return (const Init*)UI; }
321   virtual const Init *convertValue(   const BitInit *BI) { return 0; }
322   virtual const Init *convertValue(  const BitsInit *BI) { return 0; }
323   virtual const Init *convertValue(   const IntInit *II) { return 0; }
324   virtual const Init *convertValue(const StringInit *SI) { return 0; }
325   virtual const Init *convertValue(  const ListInit *LI);
326   virtual const Init *convertValue(  const CodeInit *CI) { return 0; }
327   virtual const Init *convertValue(const VarBitInit *VB) { return 0; }
328   virtual const Init *convertValue(   const DefInit *DI) { return 0; }
329   virtual const Init *convertValue(   const DagInit *DI) { return 0; }
330   virtual const Init *convertValue( const UnOpInit *UI) { return RecTy::convertValue(UI);}
331   virtual const Init *convertValue( const BinOpInit *UI) { return RecTy::convertValue(UI);}
332   virtual const Init *convertValue( const TernOpInit *UI) { return RecTy::convertValue(UI);}
333   virtual const Init *convertValue( const TypedInit *TI);
334   virtual const Init *convertValue(   const VarInit *VI) { return RecTy::convertValue(VI);}
335   virtual const Init *convertValue( const FieldInit *FI) { return RecTy::convertValue(FI);}
336
337   std::string getAsString() const;
338
339   bool typeIsConvertibleTo(const RecTy *RHS) const {
340     return RHS->baseClassOf(this);
341   }
342
343   virtual bool baseClassOf(const BitRecTy    *RHS) const { return false; }
344   virtual bool baseClassOf(const BitsRecTy   *RHS) const { return false; }
345   virtual bool baseClassOf(const IntRecTy    *RHS) const { return false; }
346   virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
347   virtual bool baseClassOf(const ListRecTy   *RHS) const {
348     return RHS->getElementType()->typeIsConvertibleTo(Ty);
349   }
350   virtual bool baseClassOf(const CodeRecTy   *RHS) const { return false; }
351   virtual bool baseClassOf(const DagRecTy    *RHS) const { return false; }
352   virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
353 };
354
355 /// CodeRecTy - 'code' - Represent an code fragment, function or method.
356 ///
357 class CodeRecTy : public RecTy {
358   static CodeRecTy Shared;
359   CodeRecTy() {}
360 public:
361   static CodeRecTy *get() { return &Shared; }
362
363   virtual const Init *convertValue( const UnsetInit *UI) { return (const Init*)UI; }
364   virtual const Init *convertValue(   const BitInit *BI) { return 0; }
365   virtual const Init *convertValue(  const BitsInit *BI) { return 0; }
366   virtual const Init *convertValue(   const IntInit *II) { return 0; }
367   virtual const Init *convertValue(const StringInit *SI) { return 0; }
368   virtual const Init *convertValue(  const ListInit *LI) { return 0; }
369   virtual const Init *convertValue(  const CodeInit *CI) { return (const Init*)CI; }
370   virtual const Init *convertValue(const VarBitInit *VB) { return 0; }
371   virtual const Init *convertValue(   const DefInit *DI) { return 0; }
372   virtual const Init *convertValue(   const DagInit *DI) { return 0; }
373   virtual const Init *convertValue( const UnOpInit *UI) { return RecTy::convertValue(UI);}
374   virtual const Init *convertValue( const BinOpInit *UI) { return RecTy::convertValue(UI);}
375   virtual const Init *convertValue( const TernOpInit *UI) { return RecTy::convertValue(UI);}
376   virtual const Init *convertValue( const TypedInit *TI);
377   virtual const Init *convertValue(   const VarInit *VI) { return RecTy::convertValue(VI);}
378   virtual const Init *convertValue( const FieldInit *FI) { return RecTy::convertValue(FI);}
379
380   std::string getAsString() const { return "code"; }
381
382   bool typeIsConvertibleTo(const RecTy *RHS) const {
383     return RHS->baseClassOf(this);
384   }
385   virtual bool baseClassOf(const BitRecTy    *RHS) const { return false; }
386   virtual bool baseClassOf(const BitsRecTy   *RHS) const { return false; }
387   virtual bool baseClassOf(const IntRecTy    *RHS) const { return false; }
388   virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
389   virtual bool baseClassOf(const ListRecTy   *RHS) const { return false; }
390   virtual bool baseClassOf(const CodeRecTy   *RHS) const { return true; }
391   virtual bool baseClassOf(const DagRecTy    *RHS) const { return false; }
392   virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
393 };
394
395 /// DagRecTy - 'dag' - Represent a dag fragment
396 ///
397 class DagRecTy : public RecTy {
398   static DagRecTy Shared;
399   DagRecTy() {}
400 public:
401   static DagRecTy *get() { return &Shared; }
402
403   virtual const Init *convertValue( const UnsetInit *UI) { return (const Init*)UI; }
404   virtual const Init *convertValue(   const BitInit *BI) { return 0; }
405   virtual const Init *convertValue(  const BitsInit *BI) { return 0; }
406   virtual const Init *convertValue(   const IntInit *II) { return 0; }
407   virtual const Init *convertValue(const StringInit *SI) { return 0; }
408   virtual const Init *convertValue(  const ListInit *LI) { return 0; }
409   virtual const Init *convertValue(  const CodeInit *CI) { return 0; }
410   virtual const Init *convertValue(const VarBitInit *VB) { return 0; }
411   virtual const Init *convertValue(   const DefInit *DI) { return 0; }
412   virtual const Init *convertValue( const UnOpInit *BO);
413   virtual const Init *convertValue( const BinOpInit *BO);
414   virtual const Init *convertValue( const TernOpInit *BO) { return RecTy::convertValue(BO);}
415   virtual const Init *convertValue(   const DagInit *CI) { return (const Init*)CI; }
416   virtual const Init *convertValue( const TypedInit *TI);
417   virtual const Init *convertValue(   const VarInit *VI) { return RecTy::convertValue(VI);}
418   virtual const Init *convertValue( const FieldInit *FI) { return RecTy::convertValue(FI);}
419
420   std::string getAsString() const { return "dag"; }
421
422   bool typeIsConvertibleTo(const RecTy *RHS) const {
423     return RHS->baseClassOf(this);
424   }
425
426   virtual bool baseClassOf(const BitRecTy    *RHS) const { return false; }
427   virtual bool baseClassOf(const BitsRecTy   *RHS) const { return false; }
428   virtual bool baseClassOf(const IntRecTy    *RHS) const { return false; }
429   virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
430   virtual bool baseClassOf(const ListRecTy   *RHS) const { return false; }
431   virtual bool baseClassOf(const CodeRecTy   *RHS) const { return false; }
432   virtual bool baseClassOf(const DagRecTy    *RHS) const { return true; }
433   virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
434 };
435
436
437 /// RecordRecTy - '[classname]' - Represent an instance of a class, such as:
438 /// (R32 X = EAX).
439 ///
440 class RecordRecTy : public RecTy {
441   Record *Rec;
442   explicit RecordRecTy(Record *R) : Rec(R) {}
443   friend class Record;
444 public:
445   static RecordRecTy *get(Record *R);
446
447   Record *getRecord() const { return Rec; }
448
449   virtual const Init *convertValue( const UnsetInit *UI) { return (const Init*)UI; }
450   virtual const Init *convertValue(   const BitInit *BI) { return 0; }
451   virtual const Init *convertValue(  const BitsInit *BI) { return 0; }
452   virtual const Init *convertValue(   const IntInit *II) { return 0; }
453   virtual const Init *convertValue(const StringInit *SI) { return 0; }
454   virtual const Init *convertValue(  const ListInit *LI) { return 0; }
455   virtual const Init *convertValue(  const CodeInit *CI) { return 0; }
456   virtual const Init *convertValue(const VarBitInit *VB) { return 0; }
457   virtual const Init *convertValue( const UnOpInit *UI) { return RecTy::convertValue(UI);}
458   virtual const Init *convertValue( const BinOpInit *UI) { return RecTy::convertValue(UI);}
459   virtual const Init *convertValue( const TernOpInit *UI) { return RecTy::convertValue(UI);}
460   virtual const Init *convertValue(   const DefInit *DI);
461   virtual const Init *convertValue(   const DagInit *DI) { return 0; }
462   virtual const Init *convertValue( const TypedInit *VI);
463   virtual const Init *convertValue(   const VarInit *VI) { return RecTy::convertValue(VI);}
464   virtual const Init *convertValue( const FieldInit *FI) { return RecTy::convertValue(FI);}
465
466   std::string getAsString() const;
467
468   bool typeIsConvertibleTo(const RecTy *RHS) const {
469     return RHS->baseClassOf(this);
470   }
471   virtual bool baseClassOf(const BitRecTy    *RHS) const { return false; }
472   virtual bool baseClassOf(const BitsRecTy   *RHS) const { return false; }
473   virtual bool baseClassOf(const IntRecTy    *RHS) const { return false; }
474   virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
475   virtual bool baseClassOf(const ListRecTy   *RHS) const { return false; }
476   virtual bool baseClassOf(const CodeRecTy   *RHS) const { return false; }
477   virtual bool baseClassOf(const DagRecTy    *RHS) const { return false; }
478   virtual bool baseClassOf(const RecordRecTy *RHS) const;
479 };
480
481 /// resolveTypes - Find a common type that T1 and T2 convert to.
482 /// Return 0 if no such type exists.
483 ///
484 RecTy *resolveTypes(RecTy *T1, RecTy *T2);
485
486 //===----------------------------------------------------------------------===//
487 //  Initializer Classes
488 //===----------------------------------------------------------------------===//
489
490 class Init {
491   Init(const Init &);  // Do not define.
492   Init &operator=(const Init &);  // Do not define.
493
494 protected:
495   Init(void) {}
496
497 public:
498   virtual ~Init() {}
499
500   /// isComplete - This virtual method should be overridden by values that may
501   /// not be completely specified yet.
502   virtual bool isComplete() const { return true; }
503
504   /// print - Print out this value.
505   void print(raw_ostream &OS) const { OS << getAsString(); }
506
507   /// getAsString - Convert this value to a string form.
508   virtual std::string getAsString() const = 0;
509
510   /// dump - Debugging method that may be called through a debugger, just
511   /// invokes print on stderr.
512   void dump() const;
513
514   /// convertInitializerTo - This virtual function is a simple call-back
515   /// function that should be overridden to call the appropriate
516   /// RecTy::convertValue method.
517   ///
518   virtual const Init *convertInitializerTo(RecTy *Ty) const = 0;
519
520   /// convertInitializerBitRange - This method is used to implement the bitrange
521   /// selection operator.  Given an initializer, it selects the specified bits
522   /// out, returning them as a new init of bits type.  If it is not legal to use
523   /// the bit subscript operator on this initializer, return null.
524   ///
525   virtual const Init *
526   convertInitializerBitRange(const std::vector<unsigned> &Bits) const {
527     return 0;
528   }
529
530   /// convertInitListSlice - This method is used to implement the list slice
531   /// selection operator.  Given an initializer, it selects the specified list
532   /// elements, returning them as a new init of list type.  If it is not legal
533   /// to take a slice of this, return null.
534   ///
535   virtual const Init *
536   convertInitListSlice(const std::vector<unsigned> &Elements) const {
537     return 0;
538   }
539
540   /// getFieldType - This method is used to implement the FieldInit class.
541   /// Implementors of this method should return the type of the named field if
542   /// they are of record type.
543   ///
544   virtual RecTy *getFieldType(const std::string &FieldName) const { return 0; }
545
546   /// getFieldInit - This method complements getFieldType to return the
547   /// initializer for the specified field.  If getFieldType returns non-null
548   /// this method should return non-null, otherwise it returns null.
549   ///
550   virtual const Init *getFieldInit(Record &R, const RecordVal *RV,
551                                    const std::string &FieldName) const {
552     return 0;
553   }
554
555   /// resolveReferences - This method is used by classes that refer to other
556   /// variables which may not be defined at the time the expression is formed.
557   /// If a value is set for the variable later, this method will be called on
558   /// users of the value to allow the value to propagate out.
559   ///
560   virtual const Init *resolveReferences(Record &R, const RecordVal *RV) const {
561     return this;
562   }
563 };
564
565 inline raw_ostream &operator<<(raw_ostream &OS, const Init &I) {
566   I.print(OS); return OS;
567 }
568
569 /// TypedInit - This is the common super-class of types that have a specific,
570 /// explicit, type.
571 ///
572 class TypedInit : public Init {
573   RecTy *Ty;
574
575   TypedInit(const TypedInit &Other);  // Do not define.
576   TypedInit &operator=(const TypedInit &Other);  // Do not define.
577
578 protected:
579   explicit TypedInit(RecTy *T) : Ty(T) {}
580
581 public:
582   RecTy *getType() const { return Ty; }
583
584   virtual const Init *
585   convertInitializerBitRange(const std::vector<unsigned> &Bits) const;
586   virtual const Init *
587   convertInitListSlice(const std::vector<unsigned> &Elements) const;
588
589   /// getFieldType - This method is used to implement the FieldInit class.
590   /// Implementors of this method should return the type of the named field if
591   /// they are of record type.
592   ///
593   virtual RecTy *getFieldType(const std::string &FieldName) const;
594
595   /// resolveBitReference - This method is used to implement
596   /// VarBitInit::resolveReferences.  If the bit is able to be resolved, we
597   /// simply return the resolved value, otherwise we return null.
598   ///
599   virtual const Init *resolveBitReference(Record &R, const RecordVal *RV,
600                                           unsigned Bit) const = 0;
601
602   /// resolveListElementReference - This method is used to implement
603   /// VarListElementInit::resolveReferences.  If the list element is resolvable
604   /// now, we return the resolved value, otherwise we return null.
605   virtual const Init *resolveListElementReference(Record &R, const RecordVal *RV,
606                                                   unsigned Elt) const = 0;
607 };
608
609
610 /// UnsetInit - ? - Represents an uninitialized value
611 ///
612 class UnsetInit : public Init {
613   UnsetInit() : Init() {}
614   UnsetInit(const UnsetInit &);  // Do not define.
615   UnsetInit &operator=(const UnsetInit &Other);  // Do not define.
616
617 public:
618   static const UnsetInit *get();
619
620   virtual const Init *convertInitializerTo(RecTy *Ty) const {
621     return Ty->convertValue(this);
622   }
623
624   virtual bool isComplete() const { return false; }
625   virtual std::string getAsString() const { return "?"; }
626 };
627
628
629 /// BitInit - true/false - Represent a concrete initializer for a bit.
630 ///
631 class BitInit : public Init {
632   bool Value;
633
634   explicit BitInit(bool V) : Value(V) {}
635   BitInit(const BitInit &Other);  // Do not define.
636   BitInit &operator=(BitInit &Other);  // Do not define.
637
638 public:
639   static const BitInit *get(bool V);
640
641   bool getValue() const { return Value; }
642
643   virtual const Init *convertInitializerTo(RecTy *Ty) const {
644     return Ty->convertValue(this);
645   }
646
647   virtual std::string getAsString() const { return Value ? "1" : "0"; }
648 };
649
650 /// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
651 /// It contains a vector of bits, whose size is determined by the type.
652 ///
653 class BitsInit : public Init, public FoldingSetNode {
654   std::vector<const Init*> Bits;
655
656   BitsInit(ArrayRef<const Init *> Range) : Bits(Range.begin(), Range.end()) {}
657
658   BitsInit(const BitsInit &Other);  // Do not define.
659   BitsInit &operator=(const BitsInit &Other);  // Do not define.
660
661 public:
662   static const BitsInit *get(ArrayRef<const Init *> Range);
663
664   void Profile(FoldingSetNodeID &ID) const;
665
666   unsigned getNumBits() const { return Bits.size(); }
667
668   const Init *getBit(unsigned Bit) const {
669     assert(Bit < Bits.size() && "Bit index out of range!");
670     return Bits[Bit];
671   }
672
673   virtual const Init *convertInitializerTo(RecTy *Ty) const {
674     return Ty->convertValue(this);
675   }
676   virtual const Init *
677   convertInitializerBitRange(const std::vector<unsigned> &Bits) const;
678
679   virtual bool isComplete() const {
680     for (unsigned i = 0; i != getNumBits(); ++i)
681       if (!getBit(i)->isComplete()) return false;
682     return true;
683   }
684   bool allInComplete() const {
685     for (unsigned i = 0; i != getNumBits(); ++i)
686       if (getBit(i)->isComplete()) return false;
687     return true;
688   }
689   virtual std::string getAsString() const;
690
691   virtual const Init *resolveReferences(Record &R, const RecordVal *RV) const;
692 };
693
694
695 /// IntInit - 7 - Represent an initalization by a literal integer value.
696 ///
697 class IntInit : public TypedInit {
698   int64_t Value;
699
700   explicit IntInit(int64_t V) : TypedInit(IntRecTy::get()), Value(V) {}
701
702   IntInit(const IntInit &Other);  // Do not define.
703   IntInit &operator=(const IntInit &Other);  // Do note define.
704
705 public:
706   static const IntInit *get(int64_t V);
707
708   int64_t getValue() const { return Value; }
709
710   virtual const Init *convertInitializerTo(RecTy *Ty) const {
711     return Ty->convertValue(this);
712   }
713   virtual const Init *
714   convertInitializerBitRange(const std::vector<unsigned> &Bits) const;
715
716   virtual std::string getAsString() const;
717
718   /// resolveBitReference - This method is used to implement
719   /// VarBitInit::resolveReferences.  If the bit is able to be resolved, we
720   /// simply return the resolved value, otherwise we return null.
721   ///
722   virtual const Init *resolveBitReference(Record &R, const RecordVal *RV,
723                                           unsigned Bit) const {
724     assert(0 && "Illegal bit reference off int");
725     return 0;
726   }
727
728   /// resolveListElementReference - This method is used to implement
729   /// VarListElementInit::resolveReferences.  If the list element is resolvable
730   /// now, we return the resolved value, otherwise we return null.
731   virtual const Init *resolveListElementReference(Record &R, const RecordVal *RV,
732                                                   unsigned Elt) const {
733     assert(0 && "Illegal element reference off int");
734     return 0;
735   }
736 };
737
738
739 /// StringInit - "foo" - Represent an initialization by a string value.
740 ///
741 class StringInit : public TypedInit {
742   std::string Value;
743
744   explicit StringInit(const std::string &V)
745     : TypedInit(StringRecTy::get()), Value(V) {}
746
747   StringInit(const StringInit &Other);  // Do not define.
748   StringInit &operator=(const StringInit &Other);  // Do not define.
749
750 public:
751   static const StringInit *get(const std::string &V);
752
753   const std::string &getValue() const { return Value; }
754
755   virtual const Init *convertInitializerTo(RecTy *Ty) const {
756     return Ty->convertValue(this);
757   }
758
759   virtual std::string getAsString() const { return "\"" + Value + "\""; }
760
761   /// resolveBitReference - This method is used to implement
762   /// VarBitInit::resolveReferences.  If the bit is able to be resolved, we
763   /// simply return the resolved value, otherwise we return null.
764   ///
765   virtual const Init *resolveBitReference(Record &R, const RecordVal *RV,
766                                           unsigned Bit) const {
767     assert(0 && "Illegal bit reference off string");
768     return 0;
769   }
770
771   /// resolveListElementReference - This method is used to implement
772   /// VarListElementInit::resolveReferences.  If the list element is resolvable
773   /// now, we return the resolved value, otherwise we return null.
774   virtual const Init *resolveListElementReference(Record &R, const RecordVal *RV,
775                                                   unsigned Elt) const {
776     assert(0 && "Illegal element reference off string");
777     return 0;
778   }
779 };
780
781 /// CodeInit - "[{...}]" - Represent a code fragment.
782 ///
783 class CodeInit : public Init {
784   std::string Value;
785
786   explicit CodeInit(const std::string &V) : Value(V) {}
787
788   CodeInit(const CodeInit &Other);  // Do not define.
789   CodeInit &operator=(const CodeInit &Other);  // Do not define.
790
791 public:
792   static const CodeInit *get(const std::string &V);
793
794   const std::string &getValue() const { return Value; }
795
796   virtual const Init *convertInitializerTo(RecTy *Ty) const {
797     return Ty->convertValue(this);
798   }
799
800   virtual std::string getAsString() const { return "[{" + Value + "}]"; }
801 };
802
803 /// ListInit - [AL, AH, CL] - Represent a list of defs
804 ///
805 class ListInit : public TypedInit, public FoldingSetNode {
806   std::vector<const Init*> Values;
807 public:
808   typedef std::vector<const Init*>::const_iterator const_iterator;
809
810 private:
811   explicit ListInit(ArrayRef<const Init *> Range, RecTy *EltTy)
812       : TypedInit(ListRecTy::get(EltTy)), Values(Range.begin(), Range.end()) {}
813
814   ListInit(const ListInit &Other);  // Do not define.
815   ListInit &operator=(const ListInit &Other);  // Do not define.
816
817 public:
818   static const ListInit *get(ArrayRef<const Init *> Range, RecTy *EltTy);
819
820   void Profile(FoldingSetNodeID &ID) const;
821
822   unsigned getSize() const { return Values.size(); }
823   const Init *getElement(unsigned i) const {
824     assert(i < Values.size() && "List element index out of range!");
825     return Values[i];
826   }
827
828   Record *getElementAsRecord(unsigned i) const;
829
830   const Init *convertInitListSlice(const std::vector<unsigned> &Elements) const;
831
832   virtual const Init *convertInitializerTo(RecTy *Ty) const {
833     return Ty->convertValue(this);
834   }
835
836   /// resolveReferences - This method is used by classes that refer to other
837   /// variables which may not be defined at the time they expression is formed.
838   /// If a value is set for the variable later, this method will be called on
839   /// users of the value to allow the value to propagate out.
840   ///
841   virtual const Init *resolveReferences(Record &R, const RecordVal *RV) const;
842
843   virtual std::string getAsString() const;
844
845   ArrayRef<const Init*> getValues() const { return Values; }
846
847   inline const_iterator begin() const { return Values.begin(); }
848   inline const_iterator end  () const { return Values.end();   }
849
850   inline size_t         size () const { return Values.size();  }
851   inline bool           empty() const { return Values.empty(); }
852
853   /// resolveBitReference - This method is used to implement
854   /// VarBitInit::resolveReferences.  If the bit is able to be resolved, we
855   /// simply return the resolved value, otherwise we return null.
856   ///
857   virtual const Init *resolveBitReference(Record &R, const RecordVal *RV,
858                                           unsigned Bit) const {
859     assert(0 && "Illegal bit reference off list");
860     return 0;
861   }
862
863   /// resolveListElementReference - This method is used to implement
864   /// VarListElementInit::resolveReferences.  If the list element is resolvable
865   /// now, we return the resolved value, otherwise we return null.
866   virtual const Init *resolveListElementReference(Record &R, const RecordVal *RV,
867                                                   unsigned Elt) const;
868 };
869
870
871 /// OpInit - Base class for operators
872 ///
873 class OpInit : public TypedInit {
874   OpInit(const OpInit &Other);  // Do not define.
875   OpInit &operator=(OpInit &Other);  // Do not define.
876
877 protected:
878   explicit OpInit(RecTy *Type) : TypedInit(Type) {}
879
880 public:
881   // Clone - Clone this operator, replacing arguments with the new list
882   virtual const OpInit *clone(std::vector<const Init *> &Operands) const = 0;
883
884   virtual int getNumOperands() const = 0;
885   virtual const Init *getOperand(int i) const = 0;
886
887   // Fold - If possible, fold this to a simpler init.  Return this if not
888   // possible to fold.
889   virtual const Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const = 0;
890
891   virtual const Init *convertInitializerTo(RecTy *Ty) const {
892     return Ty->convertValue(this);
893   }
894
895   virtual const Init *resolveBitReference(Record &R, const RecordVal *RV,
896                                           unsigned Bit) const;
897   virtual const Init *resolveListElementReference(Record &R,
898                                                   const RecordVal *RV,
899                                                   unsigned Elt) const;
900 };
901
902
903 /// UnOpInit - !op (X) - Transform an init.
904 ///
905 class UnOpInit : public OpInit {
906 public:
907   enum UnaryOp { CAST, HEAD, TAIL, EMPTY };
908 private:
909   UnaryOp Opc;
910   const Init *LHS;
911
912   UnOpInit(UnaryOp opc, const Init *lhs, RecTy *Type)
913       : OpInit(Type), Opc(opc), LHS(lhs) {}
914
915   UnOpInit(const UnOpInit &Other);  // Do not define.
916   UnOpInit &operator=(const UnOpInit &Other);  // Do not define.
917
918 public:
919   static const UnOpInit *get(UnaryOp opc, const Init *lhs, RecTy *Type);
920
921   // Clone - Clone this operator, replacing arguments with the new list
922   virtual const OpInit *clone(std::vector<const Init *> &Operands) const {
923     assert(Operands.size() == 1 &&
924            "Wrong number of operands for unary operation");
925     return UnOpInit::get(getOpcode(), *Operands.begin(), getType());
926   }
927
928   int getNumOperands() const { return 1; }
929   const Init *getOperand(int i) const {
930     assert(i == 0 && "Invalid operand id for unary operator");
931     return getOperand();
932   }
933
934   UnaryOp getOpcode() const { return Opc; }
935   const Init *getOperand() const { return LHS; }
936
937   // Fold - If possible, fold this to a simpler init.  Return this if not
938   // possible to fold.
939   const Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const;
940
941   virtual const Init *resolveReferences(Record &R, const RecordVal *RV) const;
942
943   virtual std::string getAsString() const;
944 };
945
946 /// BinOpInit - !op (X, Y) - Combine two inits.
947 ///
948 class BinOpInit : public OpInit {
949 public:
950   enum BinaryOp { SHL, SRA, SRL, STRCONCAT, CONCAT, EQ };
951 private:
952   BinaryOp Opc;
953   const Init *LHS, *RHS;
954
955   BinOpInit(BinaryOp opc, const Init *lhs, const Init *rhs, RecTy *Type) :
956       OpInit(Type), Opc(opc), LHS(lhs), RHS(rhs) {}
957
958   BinOpInit(const BinOpInit &Other);  // Do not define.
959   BinOpInit &operator=(const BinOpInit &Other);  // Do not define.
960
961 public:
962   static const BinOpInit *get(BinaryOp opc, const Init *lhs, const Init *rhs,
963                               RecTy *Type);
964
965   // Clone - Clone this operator, replacing arguments with the new list
966   virtual const OpInit *clone(std::vector<const Init *> &Operands) const {
967     assert(Operands.size() == 2 &&
968            "Wrong number of operands for binary operation");
969     return BinOpInit::get(getOpcode(), Operands[0], Operands[1], getType());
970   }
971
972   int getNumOperands() const { return 2; }
973   const Init *getOperand(int i) const {
974     assert((i == 0 || i == 1) && "Invalid operand id for binary operator");
975     if (i == 0) {
976       return getLHS();
977     } else {
978       return getRHS();
979     }
980   }
981
982   BinaryOp getOpcode() const { return Opc; }
983   const Init *getLHS() const { return LHS; }
984   const Init *getRHS() const { return RHS; }
985
986   // Fold - If possible, fold this to a simpler init.  Return this if not
987   // possible to fold.
988   const Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const;
989
990   virtual const Init *resolveReferences(Record &R, const RecordVal *RV) const;
991
992   virtual std::string getAsString() const;
993 };
994
995 /// TernOpInit - !op (X, Y, Z) - Combine two inits.
996 ///
997 class TernOpInit : public OpInit {
998 public:
999   enum TernaryOp { SUBST, FOREACH, IF };
1000 private:
1001   TernaryOp Opc;
1002   const Init *LHS, *MHS, *RHS;
1003
1004   TernOpInit(TernaryOp opc, const Init *lhs, const Init *mhs, const Init *rhs,
1005              RecTy *Type) :
1006       OpInit(Type), Opc(opc), LHS(lhs), MHS(mhs), RHS(rhs) {}
1007
1008   TernOpInit(const TernOpInit &Other);  // Do not define.
1009   TernOpInit &operator=(const TernOpInit &Other);  // Do not define.
1010
1011 public:
1012   static const TernOpInit *get(TernaryOp opc, const Init *lhs,
1013                                const Init *mhs, const Init *rhs,
1014                                RecTy *Type);
1015
1016   // Clone - Clone this operator, replacing arguments with the new list
1017   virtual const OpInit *clone(std::vector<const Init *> &Operands) const {
1018     assert(Operands.size() == 3 &&
1019            "Wrong number of operands for ternary operation");
1020     return TernOpInit::get(getOpcode(), Operands[0], Operands[1], Operands[2],
1021                            getType());
1022   }
1023
1024   int getNumOperands() const { return 3; }
1025   const Init *getOperand(int i) const {
1026     assert((i == 0 || i == 1 || i == 2) &&
1027            "Invalid operand id for ternary operator");
1028     if (i == 0) {
1029       return getLHS();
1030     } else if (i == 1) {
1031       return getMHS();
1032     } else {
1033       return getRHS();
1034     }
1035   }
1036
1037   TernaryOp getOpcode() const { return Opc; }
1038   const Init *getLHS() const { return LHS; }
1039   const Init *getMHS() const { return MHS; }
1040   const Init *getRHS() const { return RHS; }
1041
1042   // Fold - If possible, fold this to a simpler init.  Return this if not
1043   // possible to fold.
1044   const Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const;
1045
1046   virtual bool isComplete() const { return false; }
1047
1048   virtual const Init *resolveReferences(Record &R, const RecordVal *RV) const;
1049
1050   virtual std::string getAsString() const;
1051 };
1052
1053
1054 /// VarInit - 'Opcode' - Represent a reference to an entire variable object.
1055 ///
1056 class VarInit : public TypedInit {
1057   std::string VarName;
1058
1059   explicit VarInit(const std::string &VN, RecTy *T)
1060       : TypedInit(T), VarName(VN) {}
1061
1062   VarInit(const VarInit &Other);  // Do not define.
1063   VarInit &operator=(const VarInit &Other);  // Do not define.
1064
1065 public:
1066   static const VarInit *get(const std::string &VN, RecTy *T);
1067   static const VarInit *get(const Init *VN, RecTy *T);
1068
1069   virtual const Init *convertInitializerTo(RecTy *Ty) const {
1070     return Ty->convertValue(this);
1071   }
1072
1073   const std::string &getName() const { return VarName; }
1074
1075   virtual const Init *resolveBitReference(Record &R, const RecordVal *RV,
1076                                           unsigned Bit) const;
1077   virtual const Init *resolveListElementReference(Record &R, const RecordVal *RV,
1078                                                   unsigned Elt) const;
1079
1080   virtual RecTy *getFieldType(const std::string &FieldName) const;
1081   virtual const Init *getFieldInit(Record &R, const RecordVal *RV,
1082                                    const std::string &FieldName) const;
1083
1084   /// resolveReferences - This method is used by classes that refer to other
1085   /// variables which may not be defined at the time they expression is formed.
1086   /// If a value is set for the variable later, this method will be called on
1087   /// users of the value to allow the value to propagate out.
1088   ///
1089   virtual const Init *resolveReferences(Record &R, const RecordVal *RV) const;
1090
1091   virtual std::string getAsString() const { return VarName; }
1092 };
1093
1094
1095 /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
1096 ///
1097 class VarBitInit : public Init {
1098   const TypedInit *TI;
1099   unsigned Bit;
1100
1101   VarBitInit(const TypedInit *T, unsigned B) : TI(T), Bit(B) {
1102     assert(T->getType() && dynamic_cast<BitsRecTy*>(T->getType()) &&
1103            ((BitsRecTy*)T->getType())->getNumBits() > B &&
1104            "Illegal VarBitInit expression!");
1105   }
1106
1107   VarBitInit(const VarBitInit &Other);  // Do not define.
1108   VarBitInit &operator=(const VarBitInit &Other);  // Do not define.
1109
1110 public:
1111   static const VarBitInit *get(const TypedInit *T, unsigned B);
1112
1113   virtual const Init *convertInitializerTo(RecTy *Ty) const {
1114     return Ty->convertValue(this);
1115   }
1116
1117   const TypedInit *getVariable() const { return TI; }
1118   unsigned getBitNum() const { return Bit; }
1119
1120   virtual std::string getAsString() const;
1121   virtual const Init *resolveReferences(Record &R, const RecordVal *RV) const;
1122 };
1123
1124 /// VarListElementInit - List[4] - Represent access to one element of a var or
1125 /// field.
1126 class VarListElementInit : public TypedInit {
1127   const TypedInit *TI;
1128   unsigned Element;
1129
1130   VarListElementInit(const TypedInit *T, unsigned E)
1131       : TypedInit(dynamic_cast<ListRecTy*>(T->getType())->getElementType()),
1132           TI(T), Element(E) {
1133     assert(T->getType() && dynamic_cast<ListRecTy*>(T->getType()) &&
1134            "Illegal VarBitInit expression!");
1135   }
1136
1137   VarListElementInit(const VarListElementInit &Other);  // Do not define.
1138   VarListElementInit &operator=(const VarListElementInit &Other);  // Do
1139                                                                    // not
1140                                                                    // define.
1141
1142 public:
1143   static const VarListElementInit *get(const TypedInit *T, unsigned E);
1144
1145   virtual const Init *convertInitializerTo(RecTy *Ty) const {
1146     return Ty->convertValue(this);
1147   }
1148
1149   const TypedInit *getVariable() const { return TI; }
1150   unsigned getElementNum() const { return Element; }
1151
1152   virtual const Init *resolveBitReference(Record &R, const RecordVal *RV,
1153                                           unsigned Bit) const;
1154
1155   /// resolveListElementReference - This method is used to implement
1156   /// VarListElementInit::resolveReferences.  If the list element is resolvable
1157   /// now, we return the resolved value, otherwise we return null.
1158   virtual const Init *resolveListElementReference(Record &R,
1159                                                   const RecordVal *RV,
1160                                                   unsigned Elt) const;
1161
1162   virtual std::string getAsString() const;
1163   virtual const Init *resolveReferences(Record &R, const RecordVal *RV) const;
1164 };
1165
1166 /// DefInit - AL - Represent a reference to a 'def' in the description
1167 ///
1168 class DefInit : public TypedInit {
1169   Record *Def;
1170
1171   DefInit(Record *D, RecordRecTy *T) : TypedInit(T), Def(D) {}
1172   friend class Record;
1173
1174   DefInit(const DefInit &Other);  // Do not define.
1175   DefInit &operator=(const DefInit &Other);  // Do not define.
1176
1177 public:
1178   static const DefInit *get(Record*);
1179
1180   virtual const Init *convertInitializerTo(RecTy *Ty) const {
1181     return Ty->convertValue(this);
1182   }
1183
1184   Record *getDef() const { return Def; }
1185
1186   //virtual const Init *
1187   //convertInitializerBitRange(const std::vector<unsigned> &Bits);
1188
1189   virtual RecTy *getFieldType(const std::string &FieldName) const;
1190   virtual const Init *getFieldInit(Record &R, const RecordVal *RV,
1191                                    const std::string &FieldName) const;
1192
1193   virtual std::string getAsString() const;
1194
1195   /// resolveBitReference - This method is used to implement
1196   /// VarBitInit::resolveReferences.  If the bit is able to be resolved, we
1197   /// simply return the resolved value, otherwise we return null.
1198   ///
1199   virtual const Init *resolveBitReference(Record &R, const RecordVal *RV,
1200                                           unsigned Bit) const {
1201     assert(0 && "Illegal bit reference off def");
1202     return 0;
1203   }
1204
1205   /// resolveListElementReference - This method is used to implement
1206   /// VarListElementInit::resolveReferences.  If the list element is resolvable
1207   /// now, we return the resolved value, otherwise we return null.
1208   virtual const Init *resolveListElementReference(Record &R, const RecordVal *RV,
1209                                                   unsigned Elt) const {
1210     assert(0 && "Illegal element reference off def");
1211     return 0;
1212   }
1213 };
1214
1215
1216 /// FieldInit - X.Y - Represent a reference to a subfield of a variable
1217 ///
1218 class FieldInit : public TypedInit {
1219   const Init *Rec;                // Record we are referring to
1220   std::string FieldName;    // Field we are accessing
1221
1222   FieldInit(const Init *R, const std::string &FN)
1223       : TypedInit(R->getFieldType(FN)), Rec(R), FieldName(FN) {
1224     assert(getType() && "FieldInit with non-record type!");
1225   }
1226
1227   FieldInit(const FieldInit &Other);  // Do not define.
1228   FieldInit &operator=(const FieldInit &Other);  // Do not define.
1229
1230 public:
1231   static const FieldInit *get(const Init *R, const std::string &FN);
1232   static const FieldInit *get(const Init *R, const Init *FN);
1233
1234   virtual const Init *convertInitializerTo(RecTy *Ty) const {
1235     return Ty->convertValue(this);
1236   }
1237
1238   virtual const Init *resolveBitReference(Record &R, const RecordVal *RV,
1239                                           unsigned Bit) const;
1240   virtual const Init *resolveListElementReference(Record &R,
1241                                                   const RecordVal *RV,
1242                                                   unsigned Elt) const;
1243
1244   virtual const Init *resolveReferences(Record &R, const RecordVal *RV) const;
1245
1246   virtual std::string getAsString() const {
1247     return Rec->getAsString() + "." + FieldName;
1248   }
1249 };
1250
1251 /// DagInit - (v a, b) - Represent a DAG tree value.  DAG inits are required
1252 /// to have at least one value then a (possibly empty) list of arguments.  Each
1253 /// argument can have a name associated with it.
1254 ///
1255 class DagInit : public TypedInit {
1256   const Init *Val;
1257   std::string ValName;
1258   std::vector<const Init*> Args;
1259   std::vector<std::string> ArgNames;
1260
1261   DagInit(const Init *V, const std::string &VN,
1262           const std::vector<std::pair<const Init*, std::string> > &args)
1263     : TypedInit(DagRecTy::get()), Val(V), ValName(VN) {
1264     Args.reserve(args.size());
1265     ArgNames.reserve(args.size());
1266     for (unsigned i = 0, e = args.size(); i != e; ++i) {
1267       Args.push_back(args[i].first);
1268       ArgNames.push_back(args[i].second);
1269     }
1270   }
1271   DagInit(const Init *V, const std::string &VN,
1272           const std::vector<const Init*> &args,
1273           const std::vector<std::string> &argNames)
1274     : TypedInit(DagRecTy::get()), Val(V), ValName(VN), Args(args),
1275       ArgNames(argNames) { }
1276
1277   DagInit(const DagInit &Other);  // Do not define.
1278   DagInit &operator=(const DagInit &Other);  // Do not define.
1279
1280 public:
1281   static const DagInit *get(const Init *V, const std::string &VN,
1282                             const std::vector<
1283                               std::pair<const Init*, std::string> > &args);
1284
1285   static const DagInit *get(const Init *V, const std::string &VN,
1286                             const std::vector<const Init*> &args,
1287                             const std::vector<std::string> &argNames);
1288
1289   virtual const Init *convertInitializerTo(RecTy *Ty) const {
1290     return Ty->convertValue(this);
1291   }
1292
1293   const Init *getOperator() const { return Val; }
1294
1295   const std::string &getName() const { return ValName; }
1296
1297   unsigned getNumArgs() const { return Args.size(); }
1298   const Init *getArg(unsigned Num) const {
1299     assert(Num < Args.size() && "Arg number out of range!");
1300     return Args[Num];
1301   }
1302   const std::string &getArgName(unsigned Num) const {
1303     assert(Num < ArgNames.size() && "Arg number out of range!");
1304     return ArgNames[Num];
1305   }
1306
1307   virtual const Init *resolveReferences(Record &R, const RecordVal *RV) const;
1308
1309   virtual std::string getAsString() const;
1310
1311   typedef std::vector<const Init*>::const_iterator       const_arg_iterator;
1312   typedef std::vector<std::string>::const_iterator const_name_iterator;
1313
1314   inline const_arg_iterator  arg_begin() const { return Args.begin(); }
1315   inline const_arg_iterator  arg_end  () const { return Args.end();   }
1316
1317   inline size_t              arg_size () const { return Args.size();  }
1318   inline bool                arg_empty() const { return Args.empty(); }
1319
1320   inline const_name_iterator name_begin() const { return ArgNames.begin(); }
1321   inline const_name_iterator name_end  () const { return ArgNames.end();   }
1322
1323   inline size_t              name_size () const { return ArgNames.size();  }
1324   inline bool                name_empty() const { return ArgNames.empty(); }
1325
1326   virtual const Init *resolveBitReference(Record &R, const RecordVal *RV,
1327                                           unsigned Bit) const {
1328     assert(0 && "Illegal bit reference off dag");
1329     return 0;
1330   }
1331
1332   virtual const Init *resolveListElementReference(Record &R,
1333                                                   const RecordVal *RV,
1334                                                   unsigned Elt) const {
1335     assert(0 && "Illegal element reference off dag");
1336     return 0;
1337   }
1338 };
1339
1340 //===----------------------------------------------------------------------===//
1341 //  High-Level Classes
1342 //===----------------------------------------------------------------------===//
1343
1344 class RecordVal {
1345   std::string Name;
1346   RecTy *Ty;
1347   unsigned Prefix;
1348   const Init *Value;
1349 public:
1350   RecordVal(const std::string &N, RecTy *T, unsigned P);
1351
1352   const std::string &getName() const { return Name; }
1353
1354   unsigned getPrefix() const { return Prefix; }
1355   RecTy *getType() const { return Ty; }
1356   const Init *getValue() const { return Value; }
1357
1358   bool setValue(const Init *V) {
1359     if (V) {
1360       Value = V->convertInitializerTo(Ty);
1361       return Value == 0;
1362     }
1363     Value = 0;
1364     return false;
1365   }
1366
1367   void dump() const;
1368   void print(raw_ostream &OS, bool PrintSem = true) const;
1369 };
1370
1371 inline raw_ostream &operator<<(raw_ostream &OS, const RecordVal &RV) {
1372   RV.print(OS << "  ");
1373   return OS;
1374 }
1375
1376 class Record {
1377   static unsigned LastID;
1378
1379   // Unique record ID.
1380   unsigned ID;
1381   std::string Name;
1382   SMLoc Loc;
1383   std::vector<std::string> TemplateArgs;
1384   std::vector<RecordVal> Values;
1385   std::vector<Record*> SuperClasses;
1386
1387   // Tracks Record instances. Not owned by Record.
1388   RecordKeeper &TrackedRecords;
1389
1390   DefInit *TheInit;
1391
1392 public:
1393
1394   // Constructs a record.
1395   explicit Record(const std::string &N, SMLoc loc, RecordKeeper &records) :
1396     ID(LastID++), Name(N), Loc(loc), TrackedRecords(records), TheInit(0) {}
1397   ~Record() {}
1398
1399
1400   static unsigned getNewUID() { return LastID++; }
1401
1402
1403   unsigned getID() const { return ID; }
1404
1405   const std::string &getName() const { return Name; }
1406   void setName(const std::string &Name);  // Also updates RecordKeeper.
1407
1408   SMLoc getLoc() const { return Loc; }
1409
1410   /// get the corresponding DefInit.
1411   DefInit *getDefInit();
1412
1413   const std::vector<std::string> &getTemplateArgs() const {
1414     return TemplateArgs;
1415   }
1416   const std::vector<RecordVal> &getValues() const { return Values; }
1417   const std::vector<Record*>   &getSuperClasses() const { return SuperClasses; }
1418
1419   bool isTemplateArg(StringRef Name) const {
1420     for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
1421       if (TemplateArgs[i] == Name) return true;
1422     return false;
1423   }
1424
1425   const RecordVal *getValue(StringRef Name) const {
1426     for (unsigned i = 0, e = Values.size(); i != e; ++i)
1427       if (Values[i].getName() == Name) return &Values[i];
1428     return 0;
1429   }
1430   RecordVal *getValue(StringRef Name) {
1431     for (unsigned i = 0, e = Values.size(); i != e; ++i)
1432       if (Values[i].getName() == Name) return &Values[i];
1433     return 0;
1434   }
1435
1436   void addTemplateArg(StringRef Name) {
1437     assert(!isTemplateArg(Name) && "Template arg already defined!");
1438     TemplateArgs.push_back(Name);
1439   }
1440
1441   void addValue(const RecordVal &RV) {
1442     assert(getValue(RV.getName()) == 0 && "Value already added!");
1443     Values.push_back(RV);
1444   }
1445
1446   void removeValue(StringRef Name) {
1447     for (unsigned i = 0, e = Values.size(); i != e; ++i)
1448       if (Values[i].getName() == Name) {
1449         Values.erase(Values.begin()+i);
1450         return;
1451       }
1452     assert(0 && "Cannot remove an entry that does not exist!");
1453   }
1454
1455   bool isSubClassOf(const Record *R) const {
1456     for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1457       if (SuperClasses[i] == R)
1458         return true;
1459     return false;
1460   }
1461
1462   bool isSubClassOf(StringRef Name) const {
1463     for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1464       if (SuperClasses[i]->getName() == Name)
1465         return true;
1466     return false;
1467   }
1468
1469   void addSuperClass(Record *R) {
1470     assert(!isSubClassOf(R) && "Already subclassing record!");
1471     SuperClasses.push_back(R);
1472   }
1473
1474   /// resolveReferences - If there are any field references that refer to fields
1475   /// that have been filled in, we can propagate the values now.
1476   ///
1477   void resolveReferences() { resolveReferencesTo(0); }
1478
1479   /// resolveReferencesTo - If anything in this record refers to RV, replace the
1480   /// reference to RV with the RHS of RV.  If RV is null, we resolve all
1481   /// possible references.
1482   void resolveReferencesTo(const RecordVal *RV);
1483
1484   RecordKeeper &getRecords() const {
1485     return TrackedRecords;
1486   }
1487
1488   void dump() const;
1489
1490   //===--------------------------------------------------------------------===//
1491   // High-level methods useful to tablegen back-ends
1492   //
1493
1494   /// getValueInit - Return the initializer for a value with the specified name,
1495   /// or throw an exception if the field does not exist.
1496   ///
1497   const Init *getValueInit(StringRef FieldName) const;
1498
1499   /// getValueAsString - This method looks up the specified field and returns
1500   /// its value as a string, throwing an exception if the field does not exist
1501   /// or if the value is not a string.
1502   ///
1503   std::string getValueAsString(StringRef FieldName) const;
1504
1505   /// getValueAsBitsInit - This method looks up the specified field and returns
1506   /// its value as a BitsInit, throwing an exception if the field does not exist
1507   /// or if the value is not the right type.
1508   ///
1509   const BitsInit *getValueAsBitsInit(StringRef FieldName) const;
1510
1511   /// getValueAsListInit - This method looks up the specified field and returns
1512   /// its value as a ListInit, throwing an exception if the field does not exist
1513   /// or if the value is not the right type.
1514   ///
1515   const ListInit *getValueAsListInit(StringRef FieldName) const;
1516
1517   /// getValueAsListOfDefs - This method looks up the specified field and
1518   /// returns its value as a vector of records, throwing an exception if the
1519   /// field does not exist or if the value is not the right type.
1520   ///
1521   std::vector<Record*> getValueAsListOfDefs(StringRef FieldName) const;
1522
1523   /// getValueAsListOfInts - This method looks up the specified field and
1524   /// returns its value as a vector of integers, throwing an exception if the
1525   /// field does not exist or if the value is not the right type.
1526   ///
1527   std::vector<int64_t> getValueAsListOfInts(StringRef FieldName) const;
1528
1529   /// getValueAsListOfStrings - This method looks up the specified field and
1530   /// returns its value as a vector of strings, throwing an exception if the
1531   /// field does not exist or if the value is not the right type.
1532   ///
1533   std::vector<std::string> getValueAsListOfStrings(StringRef FieldName) const;
1534
1535   /// getValueAsDef - This method looks up the specified field and returns its
1536   /// value as a Record, throwing an exception if the field does not exist or if
1537   /// the value is not the right type.
1538   ///
1539   Record *getValueAsDef(StringRef FieldName) const;
1540
1541   /// getValueAsBit - This method looks up the specified field and returns its
1542   /// value as a bit, throwing an exception if the field does not exist or if
1543   /// the value is not the right type.
1544   ///
1545   bool getValueAsBit(StringRef FieldName) const;
1546
1547   /// getValueAsInt - This method looks up the specified field and returns its
1548   /// value as an int64_t, throwing an exception if the field does not exist or
1549   /// if the value is not the right type.
1550   ///
1551   int64_t getValueAsInt(StringRef FieldName) const;
1552
1553   /// getValueAsDag - This method looks up the specified field and returns its
1554   /// value as an Dag, throwing an exception if the field does not exist or if
1555   /// the value is not the right type.
1556   ///
1557   const DagInit *getValueAsDag(StringRef FieldName) const;
1558
1559   /// getValueAsCode - This method looks up the specified field and returns
1560   /// its value as the string data in a CodeInit, throwing an exception if the
1561   /// field does not exist or if the value is not a code object.
1562   ///
1563   std::string getValueAsCode(StringRef FieldName) const;
1564 };
1565
1566 raw_ostream &operator<<(raw_ostream &OS, const Record &R);
1567
1568 struct MultiClass {
1569   Record Rec;  // Placeholder for template args and Name.
1570   typedef std::vector<Record*> RecordVector;
1571   RecordVector DefPrototypes;
1572
1573   void dump() const;
1574
1575   MultiClass(const std::string &Name, SMLoc Loc, RecordKeeper &Records) : 
1576     Rec(Name, Loc, Records) {}
1577 };
1578
1579 class RecordKeeper {
1580   std::map<std::string, Record*> Classes, Defs;
1581 public:
1582   ~RecordKeeper() {
1583     for (std::map<std::string, Record*>::iterator I = Classes.begin(),
1584            E = Classes.end(); I != E; ++I)
1585       delete I->second;
1586     for (std::map<std::string, Record*>::iterator I = Defs.begin(),
1587            E = Defs.end(); I != E; ++I)
1588       delete I->second;
1589   }
1590
1591   const std::map<std::string, Record*> &getClasses() const { return Classes; }
1592   const std::map<std::string, Record*> &getDefs() const { return Defs; }
1593
1594   Record *getClass(const std::string &Name) const {
1595     std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
1596     return I == Classes.end() ? 0 : I->second;
1597   }
1598   Record *getDef(const std::string &Name) const {
1599     std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
1600     return I == Defs.end() ? 0 : I->second;
1601   }
1602   void addClass(Record *R) {
1603     assert(getClass(R->getName()) == 0 && "Class already exists!");
1604     Classes.insert(std::make_pair(R->getName(), R));
1605   }
1606   void addDef(Record *R) {
1607     assert(getDef(R->getName()) == 0 && "Def already exists!");
1608     Defs.insert(std::make_pair(R->getName(), R));
1609   }
1610
1611   /// removeClass - Remove, but do not delete, the specified record.
1612   ///
1613   void removeClass(const std::string &Name) {
1614     assert(Classes.count(Name) && "Class does not exist!");
1615     Classes.erase(Name);
1616   }
1617   /// removeDef - Remove, but do not delete, the specified record.
1618   ///
1619   void removeDef(const std::string &Name) {
1620     assert(Defs.count(Name) && "Def does not exist!");
1621     Defs.erase(Name);
1622   }
1623
1624   //===--------------------------------------------------------------------===//
1625   // High-level helper methods, useful for tablegen backends...
1626
1627   /// getAllDerivedDefinitions - This method returns all concrete definitions
1628   /// that derive from the specified class name.  If a class with the specified
1629   /// name does not exist, an exception is thrown.
1630   std::vector<Record*>
1631   getAllDerivedDefinitions(const std::string &ClassName) const;
1632
1633   void dump() const;
1634 };
1635
1636 /// LessRecord - Sorting predicate to sort record pointers by name.
1637 ///
1638 struct LessRecord {
1639   bool operator()(const Record *Rec1, const Record *Rec2) const {
1640     return StringRef(Rec1->getName()).compare_numeric(Rec2->getName()) < 0;
1641   }
1642 };
1643
1644 /// LessRecordFieldName - Sorting predicate to sort record pointers by their
1645 /// name field.
1646 ///
1647 struct LessRecordFieldName {
1648   bool operator()(const Record *Rec1, const Record *Rec2) const {
1649     return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name");
1650   }
1651 };
1652
1653 raw_ostream &operator<<(raw_ostream &OS, const RecordKeeper &RK);
1654
1655 } // End llvm namespace
1656
1657 #endif