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