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