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