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