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