Home | History | Annotate | Download | only in TableGen
      1 //===- llvm/TableGen/Record.h - Classes for Table Records -------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file defines the main TableGen data structures, including the TableGen
     11 // types, values, and high-level data structures.
     12 //
     13 //===----------------------------------------------------------------------===//
     14 
     15 #ifndef LLVM_TABLEGEN_RECORD_H
     16 #define LLVM_TABLEGEN_RECORD_H
     17 
     18 #include "llvm/ADT/ArrayRef.h"
     19 #include "llvm/ADT/FoldingSet.h"
     20 #include "llvm/Support/Allocator.h"
     21 #include "llvm/Support/Casting.h"
     22 #include "llvm/Support/DataTypes.h"
     23 #include "llvm/Support/ErrorHandling.h"
     24 #include "llvm/Support/SourceMgr.h"
     25 #include "llvm/Support/raw_ostream.h"
     26 #include <map>
     27 
     28 namespace llvm {
     29 class raw_ostream;
     30 
     31 // RecTy subclasses.
     32 class BitRecTy;
     33 class BitsRecTy;
     34 class IntRecTy;
     35 class StringRecTy;
     36 class ListRecTy;
     37 class DagRecTy;
     38 class RecordRecTy;
     39 
     40 // Init subclasses.
     41 class Init;
     42 class UnsetInit;
     43 class BitInit;
     44 class BitsInit;
     45 class IntInit;
     46 class StringInit;
     47 class ListInit;
     48 class UnOpInit;
     49 class BinOpInit;
     50 class TernOpInit;
     51 class DefInit;
     52 class DagInit;
     53 class TypedInit;
     54 class VarInit;
     55 class FieldInit;
     56 class VarBitInit;
     57 class VarListElementInit;
     58 
     59 // Other classes.
     60 class Record;
     61 class RecordVal;
     62 struct MultiClass;
     63 class RecordKeeper;
     64 
     65 //===----------------------------------------------------------------------===//
     66 //  Type Classes
     67 //===----------------------------------------------------------------------===//
     68 
     69 class RecTy {
     70 public:
     71   /// \brief Subclass discriminator (for dyn_cast<> et al.)
     72   enum RecTyKind {
     73     BitRecTyKind,
     74     BitsRecTyKind,
     75     IntRecTyKind,
     76     StringRecTyKind,
     77     ListRecTyKind,
     78     DagRecTyKind,
     79     RecordRecTyKind
     80   };
     81 
     82 private:
     83   RecTyKind Kind;
     84   ListRecTy *ListTy;
     85   virtual void anchor();
     86 
     87 public:
     88   RecTyKind getRecTyKind() const { return Kind; }
     89 
     90   RecTy(RecTyKind K) : Kind(K), ListTy(0) {}
     91   virtual ~RecTy() {}
     92 
     93   virtual std::string getAsString() const = 0;
     94   void print(raw_ostream &OS) const { OS << getAsString(); }
     95   void dump() const;
     96 
     97   /// typeIsConvertibleTo - Return true if all values of 'this' type can be
     98   /// converted to the specified type.
     99   virtual bool typeIsConvertibleTo(const RecTy *RHS) const = 0;
    100 
    101   /// getListTy - Returns the type representing list<this>.
    102   ListRecTy *getListTy();
    103 
    104 public:   // These methods should only be called from subclasses of Init
    105   virtual Init *convertValue( UnsetInit *UI) { return 0; }
    106   virtual Init *convertValue(   BitInit *BI) { return 0; }
    107   virtual Init *convertValue(  BitsInit *BI) { return 0; }
    108   virtual Init *convertValue(   IntInit *II) { return 0; }
    109   virtual Init *convertValue(StringInit *SI) { return 0; }
    110   virtual Init *convertValue(  ListInit *LI) { return 0; }
    111   virtual Init *convertValue( UnOpInit *UI) {
    112     return convertValue((TypedInit*)UI);
    113   }
    114   virtual Init *convertValue( BinOpInit *UI) {
    115     return convertValue((TypedInit*)UI);
    116   }
    117   virtual Init *convertValue( TernOpInit *UI) {
    118     return convertValue((TypedInit*)UI);
    119   }
    120   virtual Init *convertValue(VarBitInit *VB) { return 0; }
    121   virtual Init *convertValue(   DefInit *DI) { return 0; }
    122   virtual Init *convertValue(   DagInit *DI) { return 0; }
    123   virtual Init *convertValue( TypedInit *TI) { return 0; }
    124   virtual Init *convertValue(   VarInit *VI) {
    125     return convertValue((TypedInit*)VI);
    126   }
    127   virtual Init *convertValue( FieldInit *FI) {
    128     return convertValue((TypedInit*)FI);
    129   }
    130 
    131 public:
    132   virtual bool baseClassOf(const RecTy*) const;
    133 };
    134 
    135 inline raw_ostream &operator<<(raw_ostream &OS, const RecTy &Ty) {
    136   Ty.print(OS);
    137   return OS;
    138 }
    139 
    140 
    141 /// BitRecTy - 'bit' - Represent a single bit
    142 ///
    143 class BitRecTy : public RecTy {
    144   static BitRecTy Shared;
    145   BitRecTy() : RecTy(BitRecTyKind) {}
    146 public:
    147   static bool classof(const RecTy *RT) {
    148     return RT->getRecTyKind() == BitRecTyKind;
    149   }
    150 
    151   static BitRecTy *get() { return &Shared; }
    152 
    153   virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
    154   virtual Init *convertValue(   BitInit *BI) { return (Init*)BI; }
    155   virtual Init *convertValue(  BitsInit *BI);
    156   virtual Init *convertValue(   IntInit *II);
    157   virtual Init *convertValue(StringInit *SI) { return 0; }
    158   virtual Init *convertValue(  ListInit *LI) { return 0; }
    159   virtual Init *convertValue(VarBitInit *VB) { return (Init*)VB; }
    160   virtual Init *convertValue(   DefInit *DI) { return 0; }
    161   virtual Init *convertValue(   DagInit *DI) { return 0; }
    162   virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
    163   virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
    164   virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
    165   virtual Init *convertValue( TypedInit *TI);
    166   virtual Init *convertValue(   VarInit *VI) { return RecTy::convertValue(VI);}
    167   virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
    168 
    169   virtual std::string getAsString() const { return "bit"; }
    170 
    171   virtual bool typeIsConvertibleTo(const RecTy *RHS) const {
    172     return RHS->baseClassOf(this);
    173   }
    174   virtual bool baseClassOf(const RecTy*) const;
    175 };
    176 
    177 
    178 /// BitsRecTy - 'bits<n>' - Represent a fixed number of bits
    179 ///
    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   virtual Init *convertValue( UnsetInit *UI);
    193   virtual Init *convertValue(   BitInit *UI);
    194   virtual Init *convertValue(  BitsInit *BI);
    195   virtual Init *convertValue(   IntInit *II);
    196   virtual Init *convertValue(StringInit *SI) { return 0; }
    197   virtual Init *convertValue(  ListInit *LI) { return 0; }
    198   virtual Init *convertValue(VarBitInit *VB) { return 0; }
    199   virtual Init *convertValue(   DefInit *DI) { return 0; }
    200   virtual Init *convertValue(   DagInit *DI) { return 0; }
    201   virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
    202   virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
    203   virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
    204   virtual Init *convertValue( TypedInit *TI);
    205   virtual Init *convertValue(   VarInit *VI) { return RecTy::convertValue(VI);}
    206   virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
    207 
    208   virtual std::string getAsString() const;
    209 
    210   virtual bool typeIsConvertibleTo(const RecTy *RHS) const {
    211     return RHS->baseClassOf(this);
    212   }
    213   virtual bool baseClassOf(const RecTy*) const;
    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   virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
    230   virtual Init *convertValue(   BitInit *BI);
    231   virtual Init *convertValue(  BitsInit *BI);
    232   virtual Init *convertValue(   IntInit *II) { return (Init*)II; }
    233   virtual Init *convertValue(StringInit *SI) { return 0; }
    234   virtual Init *convertValue(  ListInit *LI) { return 0; }
    235   virtual Init *convertValue(VarBitInit *VB) { return 0; }
    236   virtual Init *convertValue(   DefInit *DI) { return 0; }
    237   virtual Init *convertValue(   DagInit *DI) { return 0; }
    238   virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
    239   virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
    240   virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
    241   virtual Init *convertValue( TypedInit *TI);
    242   virtual Init *convertValue(   VarInit *VI) { return RecTy::convertValue(VI);}
    243   virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
    244 
    245   virtual std::string getAsString() const { return "int"; }
    246 
    247   virtual bool typeIsConvertibleTo(const RecTy *RHS) const {
    248     return RHS->baseClassOf(this);
    249   }
    250 
    251   virtual bool baseClassOf(const RecTy*) const;
    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   virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
    267   virtual Init *convertValue(   BitInit *BI) { return 0; }
    268   virtual Init *convertValue(  BitsInit *BI) { return 0; }
    269   virtual Init *convertValue(   IntInit *II) { return 0; }
    270   virtual Init *convertValue(StringInit *SI) { return (Init*)SI; }
    271   virtual Init *convertValue(  ListInit *LI) { return 0; }
    272   virtual Init *convertValue( UnOpInit *BO);
    273   virtual Init *convertValue( BinOpInit *BO);
    274   virtual Init *convertValue( TernOpInit *BO) { return RecTy::convertValue(BO);}
    275 
    276   virtual Init *convertValue(VarBitInit *VB) { return 0; }
    277   virtual Init *convertValue(   DefInit *DI) { return 0; }
    278   virtual Init *convertValue(   DagInit *DI) { return 0; }
    279   virtual Init *convertValue( TypedInit *TI);
    280   virtual Init *convertValue(   VarInit *VI) { return RecTy::convertValue(VI);}
    281   virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
    282 
    283   virtual std::string getAsString() const { return "string"; }
    284 
    285   virtual bool typeIsConvertibleTo(const RecTy *RHS) const {
    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   virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
    306   virtual Init *convertValue(   BitInit *BI) { return 0; }
    307   virtual Init *convertValue(  BitsInit *BI) { return 0; }
    308   virtual Init *convertValue(   IntInit *II) { return 0; }
    309   virtual Init *convertValue(StringInit *SI) { return 0; }
    310   virtual Init *convertValue(  ListInit *LI);
    311   virtual Init *convertValue(VarBitInit *VB) { return 0; }
    312   virtual Init *convertValue(   DefInit *DI) { return 0; }
    313   virtual Init *convertValue(   DagInit *DI) { return 0; }
    314   virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
    315   virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
    316   virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
    317   virtual Init *convertValue( TypedInit *TI);
    318   virtual Init *convertValue(   VarInit *VI) { return RecTy::convertValue(VI);}
    319   virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
    320 
    321   virtual std::string getAsString() const;
    322 
    323   virtual bool typeIsConvertibleTo(const RecTy *RHS) const {
    324     return RHS->baseClassOf(this);
    325   }
    326 
    327   virtual bool baseClassOf(const RecTy*) const;
    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   virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
    343   virtual Init *convertValue(   BitInit *BI) { return 0; }
    344   virtual Init *convertValue(  BitsInit *BI) { return 0; }
    345   virtual Init *convertValue(   IntInit *II) { return 0; }
    346   virtual Init *convertValue(StringInit *SI) { return 0; }
    347   virtual Init *convertValue(  ListInit *LI) { return 0; }
    348   virtual Init *convertValue(VarBitInit *VB) { return 0; }
    349   virtual Init *convertValue(   DefInit *DI) { return 0; }
    350   virtual Init *convertValue( UnOpInit *BO);
    351   virtual Init *convertValue( BinOpInit *BO);
    352   virtual Init *convertValue( TernOpInit *BO) { return RecTy::convertValue(BO);}
    353   virtual Init *convertValue(   DagInit *CI) { return (Init*)CI; }
    354   virtual Init *convertValue( TypedInit *TI);
    355   virtual Init *convertValue(   VarInit *VI) { return RecTy::convertValue(VI);}
    356   virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
    357 
    358   virtual std::string getAsString() const { return "dag"; }
    359 
    360   virtual bool typeIsConvertibleTo(const RecTy *RHS) const {
    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   virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
    383   virtual Init *convertValue(   BitInit *BI) { return 0; }
    384   virtual Init *convertValue(  BitsInit *BI) { return 0; }
    385   virtual Init *convertValue(   IntInit *II) { return 0; }
    386   virtual Init *convertValue(StringInit *SI) { return 0; }
    387   virtual Init *convertValue(  ListInit *LI) { return 0; }
    388   virtual Init *convertValue(VarBitInit *VB) { return 0; }
    389   virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
    390   virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
    391   virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
    392   virtual Init *convertValue(   DefInit *DI);
    393   virtual Init *convertValue(   DagInit *DI) { return 0; }
    394   virtual Init *convertValue( TypedInit *VI);
    395   virtual Init *convertValue(   VarInit *VI) { return RecTy::convertValue(VI);}
    396   virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
    397 
    398   virtual std::string getAsString() const;
    399 
    400   virtual bool typeIsConvertibleTo(const RecTy *RHS) const {
    401     return RHS->baseClassOf(this);
    402   }
    403   virtual bool baseClassOf(const RecTy*) const;
    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 0;
    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 0;
    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 { return 0; }
    517 
    518   /// getFieldInit - This method complements getFieldType to return the
    519   /// initializer for the specified field.  If getFieldType returns non-null
    520   /// this method should return non-null, otherwise it returns null.
    521   ///
    522   virtual Init *getFieldInit(Record &R, const RecordVal *RV,
    523                              const std::string &FieldName) const {
    524     return 0;
    525   }
    526 
    527   /// resolveReferences - This method is used by classes that refer to other
    528   /// variables which may not be defined at the time the expression is formed.
    529   /// If a value is set for the variable later, this method will be called on
    530   /// users of the value to allow the value to propagate out.
    531   ///
    532   virtual Init *resolveReferences(Record &R, const RecordVal *RV) const {
    533     return const_cast<Init *>(this);
    534   }
    535 
    536   /// getBit - This method is used to return the initializer for the specified
    537   /// bit.
    538   virtual Init *getBit(unsigned Bit) const = 0;
    539 
    540   /// getBitVar - This method is used to retrieve the initializer for bit
    541   /// reference. For non-VarBitInit, it simply returns itself.
    542   virtual Init *getBitVar() const { return const_cast<Init*>(this); }
    543 
    544   /// getBitNum - This method is used to retrieve the bit number of a bit
    545   /// reference. For non-VarBitInit, it simply returns 0.
    546   virtual unsigned getBitNum() const { return 0; }
    547 };
    548 
    549 inline raw_ostream &operator<<(raw_ostream &OS, const Init &I) {
    550   I.print(OS); return OS;
    551 }
    552 
    553 /// TypedInit - This is the common super-class of types that have a specific,
    554 /// explicit, type.
    555 ///
    556 class TypedInit : public Init {
    557   RecTy *Ty;
    558 
    559   TypedInit(const TypedInit &Other) LLVM_DELETED_FUNCTION;
    560   TypedInit &operator=(const TypedInit &Other) LLVM_DELETED_FUNCTION;
    561 
    562 protected:
    563   explicit TypedInit(InitKind K, RecTy *T) : Init(K), Ty(T) {}
    564 
    565 public:
    566   static bool classof(const Init *I) {
    567     return I->getKind() >= IK_FirstTypedInit &&
    568            I->getKind() <= IK_LastTypedInit;
    569   }
    570   RecTy *getType() const { return Ty; }
    571 
    572   virtual Init *
    573   convertInitializerBitRange(const std::vector<unsigned> &Bits) const;
    574   virtual Init *
    575   convertInitListSlice(const std::vector<unsigned> &Elements) const;
    576 
    577   /// getFieldType - This method is used to implement the FieldInit class.
    578   /// Implementors of this method should return the type of the named field if
    579   /// they are of record type.
    580   ///
    581   virtual RecTy *getFieldType(const std::string &FieldName) const;
    582 
    583   /// resolveListElementReference - This method is used to implement
    584   /// VarListElementInit::resolveReferences.  If the list element is resolvable
    585   /// now, we return the resolved value, otherwise we return null.
    586   virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
    587                                             unsigned Elt) const = 0;
    588 };
    589 
    590 
    591 /// UnsetInit - ? - Represents an uninitialized value
    592 ///
    593 class UnsetInit : public Init {
    594   UnsetInit() : Init(IK_UnsetInit) {}
    595   UnsetInit(const UnsetInit &) LLVM_DELETED_FUNCTION;
    596   UnsetInit &operator=(const UnsetInit &Other) LLVM_DELETED_FUNCTION;
    597   virtual void anchor();
    598 
    599 public:
    600   static bool classof(const Init *I) {
    601     return I->getKind() == IK_UnsetInit;
    602   }
    603   static UnsetInit *get();
    604 
    605   virtual Init *convertInitializerTo(RecTy *Ty) const {
    606     return Ty->convertValue(const_cast<UnsetInit *>(this));
    607   }
    608 
    609   virtual Init *getBit(unsigned Bit) const {
    610     return const_cast<UnsetInit*>(this);
    611   }
    612 
    613   virtual bool isComplete() const { return false; }
    614   virtual std::string getAsString() const { return "?"; }
    615 };
    616 
    617 
    618 /// BitInit - true/false - Represent a concrete initializer for a bit.
    619 ///
    620 class BitInit : public Init {
    621   bool Value;
    622 
    623   explicit BitInit(bool V) : Init(IK_BitInit), Value(V) {}
    624   BitInit(const BitInit &Other) LLVM_DELETED_FUNCTION;
    625   BitInit &operator=(BitInit &Other) LLVM_DELETED_FUNCTION;
    626   virtual void anchor();
    627 
    628 public:
    629   static bool classof(const Init *I) {
    630     return I->getKind() == IK_BitInit;
    631   }
    632   static BitInit *get(bool V);
    633 
    634   bool getValue() const { return Value; }
    635 
    636   virtual Init *convertInitializerTo(RecTy *Ty) const {
    637     return Ty->convertValue(const_cast<BitInit *>(this));
    638   }
    639 
    640   virtual Init *getBit(unsigned Bit) const {
    641     assert(Bit < 1 && "Bit index out of range!");
    642     return const_cast<BitInit*>(this);
    643   }
    644 
    645   virtual std::string getAsString() const { return Value ? "1" : "0"; }
    646 };
    647 
    648 /// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
    649 /// It contains a vector of bits, whose size is determined by the type.
    650 ///
    651 class BitsInit : public Init, public FoldingSetNode {
    652   std::vector<Init*> Bits;
    653 
    654   BitsInit(ArrayRef<Init *> Range)
    655     : Init(IK_BitsInit), Bits(Range.begin(), Range.end()) {}
    656 
    657   BitsInit(const BitsInit &Other) LLVM_DELETED_FUNCTION;
    658   BitsInit &operator=(const BitsInit &Other) LLVM_DELETED_FUNCTION;
    659 
    660 public:
    661   static bool classof(const Init *I) {
    662     return I->getKind() == IK_BitsInit;
    663   }
    664   static BitsInit *get(ArrayRef<Init *> Range);
    665 
    666   void Profile(FoldingSetNodeID &ID) const;
    667 
    668   unsigned getNumBits() const { return Bits.size(); }
    669 
    670   virtual Init *convertInitializerTo(RecTy *Ty) const {
    671     return Ty->convertValue(const_cast<BitsInit *>(this));
    672   }
    673   virtual Init *
    674   convertInitializerBitRange(const std::vector<unsigned> &Bits) const;
    675 
    676   virtual bool isComplete() const {
    677     for (unsigned i = 0; i != getNumBits(); ++i)
    678       if (!getBit(i)->isComplete()) return false;
    679     return true;
    680   }
    681   bool allInComplete() const {
    682     for (unsigned i = 0; i != getNumBits(); ++i)
    683       if (getBit(i)->isComplete()) return false;
    684     return true;
    685   }
    686   virtual std::string getAsString() const;
    687 
    688   virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
    689 
    690   virtual Init *getBit(unsigned Bit) const {
    691     assert(Bit < Bits.size() && "Bit index out of range!");
    692     return Bits[Bit];
    693   }
    694 };
    695 
    696 
    697 /// IntInit - 7 - Represent an initalization by a literal integer value.
    698 ///
    699 class IntInit : public TypedInit {
    700   int64_t Value;
    701 
    702   explicit IntInit(int64_t V)
    703     : TypedInit(IK_IntInit, IntRecTy::get()), Value(V) {}
    704 
    705   IntInit(const IntInit &Other) LLVM_DELETED_FUNCTION;
    706   IntInit &operator=(const IntInit &Other) LLVM_DELETED_FUNCTION;
    707 
    708 public:
    709   static bool classof(const Init *I) {
    710     return I->getKind() == IK_IntInit;
    711   }
    712   static IntInit *get(int64_t V);
    713 
    714   int64_t getValue() const { return Value; }
    715 
    716   virtual Init *convertInitializerTo(RecTy *Ty) const {
    717     return Ty->convertValue(const_cast<IntInit *>(this));
    718   }
    719   virtual Init *
    720   convertInitializerBitRange(const std::vector<unsigned> &Bits) const;
    721 
    722   virtual std::string getAsString() const;
    723 
    724   /// resolveListElementReference - This method is used to implement
    725   /// VarListElementInit::resolveReferences.  If the list element is resolvable
    726   /// now, we return the resolved value, otherwise we return null.
    727   virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
    728                                             unsigned Elt) const {
    729     llvm_unreachable("Illegal element reference off int");
    730   }
    731 
    732   virtual Init *getBit(unsigned Bit) const {
    733     return BitInit::get((Value & (1ULL << Bit)) != 0);
    734   }
    735 };
    736 
    737 
    738 /// StringInit - "foo" - Represent an initialization by a string value.
    739 ///
    740 class StringInit : public TypedInit {
    741   std::string Value;
    742 
    743   explicit StringInit(const std::string &V)
    744     : TypedInit(IK_StringInit, StringRecTy::get()), Value(V) {}
    745 
    746   StringInit(const StringInit &Other) LLVM_DELETED_FUNCTION;
    747   StringInit &operator=(const StringInit &Other) LLVM_DELETED_FUNCTION;
    748   virtual void anchor();
    749 
    750 public:
    751   static bool classof(const Init *I) {
    752     return I->getKind() == IK_StringInit;
    753   }
    754   static StringInit *get(StringRef);
    755 
    756   const std::string &getValue() const { return Value; }
    757 
    758   virtual Init *convertInitializerTo(RecTy *Ty) const {
    759     return Ty->convertValue(const_cast<StringInit *>(this));
    760   }
    761 
    762   virtual std::string getAsString() const { return "\"" + Value + "\""; }
    763   virtual std::string getAsUnquotedString() const { return Value; }
    764 
    765   /// resolveListElementReference - This method is used to implement
    766   /// VarListElementInit::resolveReferences.  If the list element is resolvable
    767   /// now, we return the resolved value, otherwise we return null.
    768   virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
    769                                             unsigned Elt) const {
    770     llvm_unreachable("Illegal element reference off string");
    771   }
    772 
    773   virtual Init *getBit(unsigned Bit) const {
    774     llvm_unreachable("Illegal bit reference off string");
    775   }
    776 };
    777 
    778 /// ListInit - [AL, AH, CL] - Represent a list of defs
    779 ///
    780 class ListInit : public TypedInit, public FoldingSetNode {
    781   std::vector<Init*> Values;
    782 public:
    783   typedef std::vector<Init*>::const_iterator const_iterator;
    784 
    785 private:
    786   explicit ListInit(ArrayRef<Init *> Range, RecTy *EltTy)
    787     : TypedInit(IK_ListInit, ListRecTy::get(EltTy)),
    788       Values(Range.begin(), Range.end()) {}
    789 
    790   ListInit(const ListInit &Other) LLVM_DELETED_FUNCTION;
    791   ListInit &operator=(const ListInit &Other) LLVM_DELETED_FUNCTION;
    792 
    793 public:
    794   static bool classof(const Init *I) {
    795     return I->getKind() == IK_ListInit;
    796   }
    797   static ListInit *get(ArrayRef<Init *> Range, RecTy *EltTy);
    798 
    799   void Profile(FoldingSetNodeID &ID) const;
    800 
    801   unsigned getSize() const { return Values.size(); }
    802   Init *getElement(unsigned i) const {
    803     assert(i < Values.size() && "List element index out of range!");
    804     return Values[i];
    805   }
    806 
    807   Record *getElementAsRecord(unsigned i) const;
    808 
    809   virtual Init *
    810     convertInitListSlice(const std::vector<unsigned> &Elements) const;
    811 
    812   virtual Init *convertInitializerTo(RecTy *Ty) const {
    813     return Ty->convertValue(const_cast<ListInit *>(this));
    814   }
    815 
    816   /// resolveReferences - This method is used by classes that refer to other
    817   /// variables which may not be defined at the time they expression is formed.
    818   /// If a value is set for the variable later, this method will be called on
    819   /// users of the value to allow the value to propagate out.
    820   ///
    821   virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
    822 
    823   virtual std::string getAsString() const;
    824 
    825   ArrayRef<Init*> getValues() const { return Values; }
    826 
    827   inline const_iterator begin() const { return Values.begin(); }
    828   inline const_iterator end  () const { return Values.end();   }
    829 
    830   inline size_t         size () const { return Values.size();  }
    831   inline bool           empty() const { return Values.empty(); }
    832 
    833   /// resolveListElementReference - This method is used to implement
    834   /// VarListElementInit::resolveReferences.  If the list element is resolvable
    835   /// now, we return the resolved value, otherwise we return null.
    836   virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
    837                                             unsigned Elt) const;
    838 
    839   virtual Init *getBit(unsigned Bit) const {
    840     llvm_unreachable("Illegal bit reference off list");
    841   }
    842 };
    843 
    844 
    845 /// OpInit - Base class for operators
    846 ///
    847 class OpInit : public TypedInit {
    848   OpInit(const OpInit &Other) LLVM_DELETED_FUNCTION;
    849   OpInit &operator=(OpInit &Other) LLVM_DELETED_FUNCTION;
    850 
    851 protected:
    852   explicit OpInit(InitKind K, RecTy *Type) : TypedInit(K, Type) {}
    853 
    854 public:
    855   static bool classof(const Init *I) {
    856     return I->getKind() >= IK_FirstOpInit &&
    857            I->getKind() <= IK_LastOpInit;
    858   }
    859   // Clone - Clone this operator, replacing arguments with the new list
    860   virtual OpInit *clone(std::vector<Init *> &Operands) const = 0;
    861 
    862   virtual int getNumOperands() const = 0;
    863   virtual Init *getOperand(int i) const = 0;
    864 
    865   // Fold - If possible, fold this to a simpler init.  Return this if not
    866   // possible to fold.
    867   virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const = 0;
    868 
    869   virtual Init *convertInitializerTo(RecTy *Ty) const {
    870     return Ty->convertValue(const_cast<OpInit *>(this));
    871   }
    872 
    873   virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
    874                                             unsigned Elt) const;
    875 
    876   virtual Init *getBit(unsigned Bit) const;
    877 };
    878 
    879 
    880 /// UnOpInit - !op (X) - Transform an init.
    881 ///
    882 class UnOpInit : public OpInit {
    883 public:
    884   enum UnaryOp { CAST, HEAD, TAIL, EMPTY };
    885 private:
    886   UnaryOp Opc;
    887   Init *LHS;
    888 
    889   UnOpInit(UnaryOp opc, Init *lhs, RecTy *Type)
    890     : OpInit(IK_UnOpInit, Type), Opc(opc), LHS(lhs) {}
    891 
    892   UnOpInit(const UnOpInit &Other) LLVM_DELETED_FUNCTION;
    893   UnOpInit &operator=(const UnOpInit &Other) LLVM_DELETED_FUNCTION;
    894 
    895 public:
    896   static bool classof(const Init *I) {
    897     return I->getKind() == IK_UnOpInit;
    898   }
    899   static UnOpInit *get(UnaryOp opc, Init *lhs, RecTy *Type);
    900 
    901   // Clone - Clone this operator, replacing arguments with the new list
    902   virtual OpInit *clone(std::vector<Init *> &Operands) const {
    903     assert(Operands.size() == 1 &&
    904            "Wrong number of operands for unary operation");
    905     return UnOpInit::get(getOpcode(), *Operands.begin(), getType());
    906   }
    907 
    908   virtual int getNumOperands() const { return 1; }
    909   virtual Init *getOperand(int i) const {
    910     assert(i == 0 && "Invalid operand id for unary operator");
    911     return getOperand();
    912   }
    913 
    914   UnaryOp getOpcode() const { return Opc; }
    915   Init *getOperand() const { return LHS; }
    916 
    917   // Fold - If possible, fold this to a simpler init.  Return this if not
    918   // possible to fold.
    919   virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const;
    920 
    921   virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
    922 
    923   virtual std::string getAsString() const;
    924 };
    925 
    926 /// BinOpInit - !op (X, Y) - Combine two inits.
    927 ///
    928 class BinOpInit : public OpInit {
    929 public:
    930   enum BinaryOp { ADD, SHL, SRA, SRL, STRCONCAT, CONCAT, EQ };
    931 private:
    932   BinaryOp Opc;
    933   Init *LHS, *RHS;
    934 
    935   BinOpInit(BinaryOp opc, Init *lhs, Init *rhs, RecTy *Type) :
    936       OpInit(IK_BinOpInit, Type), Opc(opc), LHS(lhs), RHS(rhs) {}
    937 
    938   BinOpInit(const BinOpInit &Other) LLVM_DELETED_FUNCTION;
    939   BinOpInit &operator=(const BinOpInit &Other) LLVM_DELETED_FUNCTION;
    940 
    941 public:
    942   static bool classof(const Init *I) {
    943     return I->getKind() == IK_BinOpInit;
    944   }
    945   static BinOpInit *get(BinaryOp opc, Init *lhs, Init *rhs,
    946                         RecTy *Type);
    947 
    948   // Clone - Clone this operator, replacing arguments with the new list
    949   virtual OpInit *clone(std::vector<Init *> &Operands) const {
    950     assert(Operands.size() == 2 &&
    951            "Wrong number of operands for binary operation");
    952     return BinOpInit::get(getOpcode(), Operands[0], Operands[1], getType());
    953   }
    954 
    955   virtual int getNumOperands() const { return 2; }
    956   virtual Init *getOperand(int i) const {
    957     assert((i == 0 || i == 1) && "Invalid operand id for binary operator");
    958     if (i == 0) {
    959       return getLHS();
    960     } else {
    961       return getRHS();
    962     }
    963   }
    964 
    965   BinaryOp getOpcode() const { return Opc; }
    966   Init *getLHS() const { return LHS; }
    967   Init *getRHS() const { return RHS; }
    968 
    969   // Fold - If possible, fold this to a simpler init.  Return this if not
    970   // possible to fold.
    971   virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const;
    972 
    973   virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
    974 
    975   virtual std::string getAsString() const;
    976 };
    977 
    978 /// TernOpInit - !op (X, Y, Z) - Combine two inits.
    979 ///
    980 class TernOpInit : public OpInit {
    981 public:
    982   enum TernaryOp { SUBST, FOREACH, IF };
    983 private:
    984   TernaryOp Opc;
    985   Init *LHS, *MHS, *RHS;
    986 
    987   TernOpInit(TernaryOp opc, Init *lhs, Init *mhs, Init *rhs,
    988              RecTy *Type) :
    989       OpInit(IK_TernOpInit, Type), Opc(opc), LHS(lhs), MHS(mhs), RHS(rhs) {}
    990 
    991   TernOpInit(const TernOpInit &Other) LLVM_DELETED_FUNCTION;
    992   TernOpInit &operator=(const TernOpInit &Other) LLVM_DELETED_FUNCTION;
    993 
    994 public:
    995   static bool classof(const Init *I) {
    996     return I->getKind() == IK_TernOpInit;
    997   }
    998   static TernOpInit *get(TernaryOp opc, Init *lhs,
    999                          Init *mhs, Init *rhs,
   1000                          RecTy *Type);
   1001 
   1002   // Clone - Clone this operator, replacing arguments with the new list
   1003   virtual OpInit *clone(std::vector<Init *> &Operands) const {
   1004     assert(Operands.size() == 3 &&
   1005            "Wrong number of operands for ternary operation");
   1006     return TernOpInit::get(getOpcode(), Operands[0], Operands[1], Operands[2],
   1007                            getType());
   1008   }
   1009 
   1010   virtual int getNumOperands() const { return 3; }
   1011   virtual Init *getOperand(int i) const {
   1012     assert((i == 0 || i == 1 || i == 2) &&
   1013            "Invalid operand id for ternary operator");
   1014     if (i == 0) {
   1015       return getLHS();
   1016     } else if (i == 1) {
   1017       return getMHS();
   1018     } else {
   1019       return getRHS();
   1020     }
   1021   }
   1022 
   1023   TernaryOp getOpcode() const { return Opc; }
   1024   Init *getLHS() const { return LHS; }
   1025   Init *getMHS() const { return MHS; }
   1026   Init *getRHS() const { return RHS; }
   1027 
   1028   // Fold - If possible, fold this to a simpler init.  Return this if not
   1029   // possible to fold.
   1030   virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const;
   1031 
   1032   virtual bool isComplete() const { return false; }
   1033 
   1034   virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
   1035 
   1036   virtual std::string getAsString() const;
   1037 };
   1038 
   1039 
   1040 /// VarInit - 'Opcode' - Represent a reference to an entire variable object.
   1041 ///
   1042 class VarInit : public TypedInit {
   1043   Init *VarName;
   1044 
   1045   explicit VarInit(const std::string &VN, RecTy *T)
   1046       : TypedInit(IK_VarInit, T), VarName(StringInit::get(VN)) {}
   1047   explicit VarInit(Init *VN, RecTy *T)
   1048       : TypedInit(IK_VarInit, T), VarName(VN) {}
   1049 
   1050   VarInit(const VarInit &Other) LLVM_DELETED_FUNCTION;
   1051   VarInit &operator=(const VarInit &Other) LLVM_DELETED_FUNCTION;
   1052 
   1053 public:
   1054   static bool classof(const Init *I) {
   1055     return I->getKind() == IK_VarInit;
   1056   }
   1057   static VarInit *get(const std::string &VN, RecTy *T);
   1058   static VarInit *get(Init *VN, RecTy *T);
   1059 
   1060   virtual Init *convertInitializerTo(RecTy *Ty) const {
   1061     return Ty->convertValue(const_cast<VarInit *>(this));
   1062   }
   1063 
   1064   const std::string &getName() const;
   1065   Init *getNameInit() const { return VarName; }
   1066   std::string getNameInitAsString() const {
   1067     return getNameInit()->getAsUnquotedString();
   1068   }
   1069 
   1070   virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
   1071                                             unsigned Elt) const;
   1072 
   1073   virtual RecTy *getFieldType(const std::string &FieldName) const;
   1074   virtual Init *getFieldInit(Record &R, const RecordVal *RV,
   1075                              const std::string &FieldName) const;
   1076 
   1077   /// resolveReferences - This method is used by classes that refer to other
   1078   /// variables which may not be defined at the time they expression is formed.
   1079   /// If a value is set for the variable later, this method will be called on
   1080   /// users of the value to allow the value to propagate out.
   1081   ///
   1082   virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
   1083 
   1084   virtual Init *getBit(unsigned Bit) const;
   1085 
   1086   virtual std::string getAsString() const { return getName(); }
   1087 };
   1088 
   1089 
   1090 /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
   1091 ///
   1092 class VarBitInit : public Init {
   1093   TypedInit *TI;
   1094   unsigned Bit;
   1095 
   1096   VarBitInit(TypedInit *T, unsigned B) : Init(IK_VarBitInit), TI(T), Bit(B) {
   1097     assert(T->getType() &&
   1098            (isa<IntRecTy>(T->getType()) ||
   1099             (isa<BitsRecTy>(T->getType()) &&
   1100              cast<BitsRecTy>(T->getType())->getNumBits() > B)) &&
   1101            "Illegal VarBitInit expression!");
   1102   }
   1103 
   1104   VarBitInit(const VarBitInit &Other) LLVM_DELETED_FUNCTION;
   1105   VarBitInit &operator=(const VarBitInit &Other) LLVM_DELETED_FUNCTION;
   1106 
   1107 public:
   1108   static bool classof(const Init *I) {
   1109     return I->getKind() == IK_VarBitInit;
   1110   }
   1111   static VarBitInit *get(TypedInit *T, unsigned B);
   1112 
   1113   virtual Init *convertInitializerTo(RecTy *Ty) const {
   1114     return Ty->convertValue(const_cast<VarBitInit *>(this));
   1115   }
   1116 
   1117   virtual Init *getBitVar() const { return TI; }
   1118   virtual unsigned getBitNum() const { return Bit; }
   1119 
   1120   virtual std::string getAsString() const;
   1121   virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
   1122 
   1123   virtual Init *getBit(unsigned B) const {
   1124     assert(B < 1 && "Bit index out of range!");
   1125     return const_cast<VarBitInit*>(this);
   1126   }
   1127 };
   1128 
   1129 /// VarListElementInit - List[4] - Represent access to one element of a var or
   1130 /// field.
   1131 class VarListElementInit : public TypedInit {
   1132   TypedInit *TI;
   1133   unsigned Element;
   1134 
   1135   VarListElementInit(TypedInit *T, unsigned E)
   1136       : TypedInit(IK_VarListElementInit,
   1137                   cast<ListRecTy>(T->getType())->getElementType()),
   1138         TI(T), Element(E) {
   1139     assert(T->getType() && isa<ListRecTy>(T->getType()) &&
   1140            "Illegal VarBitInit expression!");
   1141   }
   1142 
   1143   VarListElementInit(const VarListElementInit &Other) LLVM_DELETED_FUNCTION;
   1144   void operator=(const VarListElementInit &Other) LLVM_DELETED_FUNCTION;
   1145 
   1146 public:
   1147   static bool classof(const Init *I) {
   1148     return I->getKind() == IK_VarListElementInit;
   1149   }
   1150   static VarListElementInit *get(TypedInit *T, unsigned E);
   1151 
   1152   virtual Init *convertInitializerTo(RecTy *Ty) const {
   1153     return Ty->convertValue(const_cast<VarListElementInit *>(this));
   1154   }
   1155 
   1156   TypedInit *getVariable() const { return TI; }
   1157   unsigned getElementNum() const { return Element; }
   1158 
   1159   /// resolveListElementReference - This method is used to implement
   1160   /// VarListElementInit::resolveReferences.  If the list element is resolvable
   1161   /// now, we return the resolved value, otherwise we return null.
   1162   virtual Init *resolveListElementReference(Record &R,
   1163                                             const RecordVal *RV,
   1164                                             unsigned Elt) const;
   1165 
   1166   virtual std::string getAsString() const;
   1167   virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
   1168 
   1169   virtual Init *getBit(unsigned Bit) const;
   1170 };
   1171 
   1172 /// DefInit - AL - Represent a reference to a 'def' in the description
   1173 ///
   1174 class DefInit : public TypedInit {
   1175   Record *Def;
   1176 
   1177   DefInit(Record *D, RecordRecTy *T) : TypedInit(IK_DefInit, T), Def(D) {}
   1178   friend class Record;
   1179 
   1180   DefInit(const DefInit &Other) LLVM_DELETED_FUNCTION;
   1181   DefInit &operator=(const DefInit &Other) LLVM_DELETED_FUNCTION;
   1182 
   1183 public:
   1184   static bool classof(const Init *I) {
   1185     return I->getKind() == IK_DefInit;
   1186   }
   1187   static DefInit *get(Record*);
   1188 
   1189   virtual Init *convertInitializerTo(RecTy *Ty) const {
   1190     return Ty->convertValue(const_cast<DefInit *>(this));
   1191   }
   1192 
   1193   Record *getDef() const { return Def; }
   1194 
   1195   //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
   1196 
   1197   virtual RecTy *getFieldType(const std::string &FieldName) const;
   1198   virtual Init *getFieldInit(Record &R, const RecordVal *RV,
   1199                              const std::string &FieldName) const;
   1200 
   1201   virtual std::string getAsString() const;
   1202 
   1203   virtual Init *getBit(unsigned Bit) const {
   1204     llvm_unreachable("Illegal bit reference off def");
   1205   }
   1206 
   1207   /// resolveListElementReference - This method is used to implement
   1208   /// VarListElementInit::resolveReferences.  If the list element is resolvable
   1209   /// now, we return the resolved value, otherwise we return null.
   1210   virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
   1211                                             unsigned Elt) const {
   1212     llvm_unreachable("Illegal element reference off def");
   1213   }
   1214 };
   1215 
   1216 
   1217 /// FieldInit - X.Y - Represent a reference to a subfield of a variable
   1218 ///
   1219 class FieldInit : public TypedInit {
   1220   Init *Rec;                // Record we are referring to
   1221   std::string FieldName;    // Field we are accessing
   1222 
   1223   FieldInit(Init *R, const std::string &FN)
   1224       : TypedInit(IK_FieldInit, R->getFieldType(FN)), Rec(R), FieldName(FN) {
   1225     assert(getType() && "FieldInit with non-record type!");
   1226   }
   1227 
   1228   FieldInit(const FieldInit &Other) LLVM_DELETED_FUNCTION;
   1229   FieldInit &operator=(const FieldInit &Other) LLVM_DELETED_FUNCTION;
   1230 
   1231 public:
   1232   static bool classof(const Init *I) {
   1233     return I->getKind() == IK_FieldInit;
   1234   }
   1235   static FieldInit *get(Init *R, const std::string &FN);
   1236   static FieldInit *get(Init *R, const Init *FN);
   1237 
   1238   virtual Init *convertInitializerTo(RecTy *Ty) const {
   1239     return Ty->convertValue(const_cast<FieldInit *>(this));
   1240   }
   1241 
   1242   virtual Init *getBit(unsigned Bit) const;
   1243 
   1244   virtual Init *resolveListElementReference(Record &R,
   1245                                             const RecordVal *RV,
   1246                                             unsigned Elt) const;
   1247 
   1248   virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
   1249 
   1250   virtual std::string getAsString() const {
   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   virtual Init *convertInitializerTo(RecTy *Ty) const {
   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   virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
   1307 
   1308   virtual std::string getAsString() const;
   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   virtual Init *getBit(unsigned Bit) const {
   1326     llvm_unreachable("Illegal bit reference off dag");
   1327   }
   1328 
   1329   virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
   1330                                             unsigned Elt) const {
   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 == 0;
   1362     }
   1363     Value = 0;
   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(0), 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(0), 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 0;
   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 0;
   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(0); }
   1537 
   1538   /// resolveReferencesTo - If anything in this record refers to RV, replace the
   1539   /// reference to RV with the RHS of RV.  If RV is null, we resolve all
   1540   /// possible references.
   1541   void resolveReferencesTo(const RecordVal *RV);
   1542 
   1543   RecordKeeper &getRecords() const {
   1544     return TrackedRecords;
   1545   }
   1546 
   1547   bool isAnonymous() const {
   1548     return IsAnonymous;
   1549   }
   1550 
   1551   void dump() const;
   1552 
   1553   //===--------------------------------------------------------------------===//
   1554   // High-level methods useful to tablegen back-ends
   1555   //
   1556 
   1557   /// getValueInit - Return the initializer for a value with the specified name,
   1558   /// or throw an exception if the field does not exist.
   1559   ///
   1560   Init *getValueInit(StringRef FieldName) const;
   1561 
   1562   /// 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   /// retunrs 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() ? 0 : 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() ? 0 : 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
   1829