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