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