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