LLVM API Documentation

Record.h
Go to the documentation of this file.
00001 //===- llvm/TableGen/Record.h - Classes for Table Records -------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the main TableGen data structures, including the TableGen
00011 // types, values, and high-level data structures.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_TABLEGEN_RECORD_H
00016 #define LLVM_TABLEGEN_RECORD_H
00017 
00018 #include "llvm/ADT/ArrayRef.h"
00019 #include "llvm/ADT/FoldingSet.h"
00020 #include "llvm/Support/Allocator.h"
00021 #include "llvm/Support/Casting.h"
00022 #include "llvm/Support/DataTypes.h"
00023 #include "llvm/Support/ErrorHandling.h"
00024 #include "llvm/Support/SourceMgr.h"
00025 #include "llvm/Support/raw_ostream.h"
00026 #include <map>
00027 
00028 namespace llvm {
00029 class raw_ostream;
00030 
00031 // RecTy subclasses.
00032 class BitRecTy;
00033 class BitsRecTy;
00034 class IntRecTy;
00035 class StringRecTy;
00036 class ListRecTy;
00037 class DagRecTy;
00038 class RecordRecTy;
00039 
00040 // Init subclasses.
00041 class Init;
00042 class UnsetInit;
00043 class BitInit;
00044 class BitsInit;
00045 class IntInit;
00046 class StringInit;
00047 class ListInit;
00048 class UnOpInit;
00049 class BinOpInit;
00050 class TernOpInit;
00051 class DefInit;
00052 class DagInit;
00053 class TypedInit;
00054 class VarInit;
00055 class FieldInit;
00056 class VarBitInit;
00057 class VarListElementInit;
00058 
00059 // Other classes.
00060 class Record;
00061 class RecordVal;
00062 struct MultiClass;
00063 class RecordKeeper;
00064 
00065 //===----------------------------------------------------------------------===//
00066 //  Type Classes
00067 //===----------------------------------------------------------------------===//
00068 
00069 class RecTy {
00070 public:
00071   /// \brief Subclass discriminator (for dyn_cast<> et al.)
00072   enum RecTyKind {
00073     BitRecTyKind,
00074     BitsRecTyKind,
00075     IntRecTyKind,
00076     StringRecTyKind,
00077     ListRecTyKind,
00078     DagRecTyKind,
00079     RecordRecTyKind
00080   };
00081 
00082 private:
00083   RecTyKind Kind;
00084   ListRecTy *ListTy;
00085   virtual void anchor();
00086 
00087 public:
00088   RecTyKind getRecTyKind() const { return Kind; }
00089 
00090   RecTy(RecTyKind K) : Kind(K), ListTy(0) {}
00091   virtual ~RecTy() {}
00092 
00093   virtual std::string getAsString() const = 0;
00094   void print(raw_ostream &OS) const { OS << getAsString(); }
00095   void dump() const;
00096 
00097   /// typeIsConvertibleTo - Return true if all values of 'this' type can be
00098   /// converted to the specified type.
00099   virtual bool typeIsConvertibleTo(const RecTy *RHS) const = 0;
00100 
00101   /// getListTy - Returns the type representing list<this>.
00102   ListRecTy *getListTy();
00103 
00104 public:   // These methods should only be called from subclasses of Init
00105   virtual Init *convertValue( UnsetInit *UI) { return 0; }
00106   virtual Init *convertValue(   BitInit *BI) { return 0; }
00107   virtual Init *convertValue(  BitsInit *BI) { return 0; }
00108   virtual Init *convertValue(   IntInit *II) { return 0; }
00109   virtual Init *convertValue(StringInit *SI) { return 0; }
00110   virtual Init *convertValue(  ListInit *LI) { return 0; }
00111   virtual Init *convertValue( UnOpInit *UI) {
00112     return convertValue((TypedInit*)UI);
00113   }
00114   virtual Init *convertValue( BinOpInit *UI) {
00115     return convertValue((TypedInit*)UI);
00116   }
00117   virtual Init *convertValue( TernOpInit *UI) {
00118     return convertValue((TypedInit*)UI);
00119   }
00120   virtual Init *convertValue(VarBitInit *VB) { return 0; }
00121   virtual Init *convertValue(   DefInit *DI) { return 0; }
00122   virtual Init *convertValue(   DagInit *DI) { return 0; }
00123   virtual Init *convertValue( TypedInit *TI) { return 0; }
00124   virtual Init *convertValue(   VarInit *VI) {
00125     return convertValue((TypedInit*)VI);
00126   }
00127   virtual Init *convertValue( FieldInit *FI) {
00128     return convertValue((TypedInit*)FI);
00129   }
00130 
00131 public:
00132   virtual bool baseClassOf(const RecTy*) const;
00133 };
00134 
00135 inline raw_ostream &operator<<(raw_ostream &OS, const RecTy &Ty) {
00136   Ty.print(OS);
00137   return OS;
00138 }
00139 
00140 
00141 /// BitRecTy - 'bit' - Represent a single bit
00142 ///
00143 class BitRecTy : public RecTy {
00144   static BitRecTy Shared;
00145   BitRecTy() : RecTy(BitRecTyKind) {}
00146 public:
00147   static bool classof(const RecTy *RT) {
00148     return RT->getRecTyKind() == BitRecTyKind;
00149   }
00150 
00151   static BitRecTy *get() { return &Shared; }
00152 
00153   virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
00154   virtual Init *convertValue(   BitInit *BI) { return (Init*)BI; }
00155   virtual Init *convertValue(  BitsInit *BI);
00156   virtual Init *convertValue(   IntInit *II);
00157   virtual Init *convertValue(StringInit *SI) { return 0; }
00158   virtual Init *convertValue(  ListInit *LI) { return 0; }
00159   virtual Init *convertValue(VarBitInit *VB) { return (Init*)VB; }
00160   virtual Init *convertValue(   DefInit *DI) { return 0; }
00161   virtual Init *convertValue(   DagInit *DI) { return 0; }
00162   virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
00163   virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
00164   virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
00165   virtual Init *convertValue( TypedInit *TI);
00166   virtual Init *convertValue(   VarInit *VI) { return RecTy::convertValue(VI);}
00167   virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
00168 
00169   virtual std::string getAsString() const { return "bit"; }
00170 
00171   virtual bool typeIsConvertibleTo(const RecTy *RHS) const {
00172     return RHS->baseClassOf(this);
00173   }
00174   virtual bool baseClassOf(const RecTy*) const;
00175 };
00176 
00177 
00178 /// BitsRecTy - 'bits<n>' - Represent a fixed number of bits
00179 ///
00180 class BitsRecTy : public RecTy {
00181   unsigned Size;
00182   explicit BitsRecTy(unsigned Sz) : RecTy(BitsRecTyKind), Size(Sz) {}
00183 public:
00184   static bool classof(const RecTy *RT) {
00185     return RT->getRecTyKind() == BitsRecTyKind;
00186   }
00187 
00188   static BitsRecTy *get(unsigned Sz);
00189 
00190   unsigned getNumBits() const { return Size; }
00191 
00192   virtual Init *convertValue( UnsetInit *UI);
00193   virtual Init *convertValue(   BitInit *UI);
00194   virtual Init *convertValue(  BitsInit *BI);
00195   virtual Init *convertValue(   IntInit *II);
00196   virtual Init *convertValue(StringInit *SI) { return 0; }
00197   virtual Init *convertValue(  ListInit *LI) { return 0; }
00198   virtual Init *convertValue(VarBitInit *VB) { return 0; }
00199   virtual Init *convertValue(   DefInit *DI) { return 0; }
00200   virtual Init *convertValue(   DagInit *DI) { return 0; }
00201   virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
00202   virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
00203   virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
00204   virtual Init *convertValue( TypedInit *TI);
00205   virtual Init *convertValue(   VarInit *VI) { return RecTy::convertValue(VI);}
00206   virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
00207 
00208   virtual std::string getAsString() const;
00209 
00210   virtual bool typeIsConvertibleTo(const RecTy *RHS) const {
00211     return RHS->baseClassOf(this);
00212   }
00213   virtual bool baseClassOf(const RecTy*) const;
00214 };
00215 
00216 
00217 /// IntRecTy - 'int' - Represent an integer value of no particular size
00218 ///
00219 class IntRecTy : public RecTy {
00220   static IntRecTy Shared;
00221   IntRecTy() : RecTy(IntRecTyKind) {}
00222 public:
00223   static bool classof(const RecTy *RT) {
00224     return RT->getRecTyKind() == IntRecTyKind;
00225   }
00226 
00227   static IntRecTy *get() { return &Shared; }
00228 
00229   virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
00230   virtual Init *convertValue(   BitInit *BI);
00231   virtual Init *convertValue(  BitsInit *BI);
00232   virtual Init *convertValue(   IntInit *II) { return (Init*)II; }
00233   virtual Init *convertValue(StringInit *SI) { return 0; }
00234   virtual Init *convertValue(  ListInit *LI) { return 0; }
00235   virtual Init *convertValue(VarBitInit *VB) { return 0; }
00236   virtual Init *convertValue(   DefInit *DI) { return 0; }
00237   virtual Init *convertValue(   DagInit *DI) { return 0; }
00238   virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
00239   virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
00240   virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
00241   virtual Init *convertValue( TypedInit *TI);
00242   virtual Init *convertValue(   VarInit *VI) { return RecTy::convertValue(VI);}
00243   virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
00244 
00245   virtual std::string getAsString() const { return "int"; }
00246 
00247   virtual bool typeIsConvertibleTo(const RecTy *RHS) const {
00248     return RHS->baseClassOf(this);
00249   }
00250 
00251   virtual bool baseClassOf(const RecTy*) const;
00252 };
00253 
00254 /// StringRecTy - 'string' - Represent an string value
00255 ///
00256 class StringRecTy : public RecTy {
00257   static StringRecTy Shared;
00258   StringRecTy() : RecTy(StringRecTyKind) {}
00259 public:
00260   static bool classof(const RecTy *RT) {
00261     return RT->getRecTyKind() == StringRecTyKind;
00262   }
00263 
00264   static StringRecTy *get() { return &Shared; }
00265 
00266   virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
00267   virtual Init *convertValue(   BitInit *BI) { return 0; }
00268   virtual Init *convertValue(  BitsInit *BI) { return 0; }
00269   virtual Init *convertValue(   IntInit *II) { return 0; }
00270   virtual Init *convertValue(StringInit *SI) { return (Init*)SI; }
00271   virtual Init *convertValue(  ListInit *LI) { return 0; }
00272   virtual Init *convertValue( UnOpInit *BO);
00273   virtual Init *convertValue( BinOpInit *BO);
00274   virtual Init *convertValue( TernOpInit *BO) { return RecTy::convertValue(BO);}
00275 
00276   virtual Init *convertValue(VarBitInit *VB) { return 0; }
00277   virtual Init *convertValue(   DefInit *DI) { return 0; }
00278   virtual Init *convertValue(   DagInit *DI) { return 0; }
00279   virtual Init *convertValue( TypedInit *TI);
00280   virtual Init *convertValue(   VarInit *VI) { return RecTy::convertValue(VI);}
00281   virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
00282 
00283   virtual std::string getAsString() const { return "string"; }
00284 
00285   virtual bool typeIsConvertibleTo(const RecTy *RHS) const {
00286     return RHS->baseClassOf(this);
00287   }
00288 };
00289 
00290 /// ListRecTy - 'list<Ty>' - Represent a list of values, all of which must be of
00291 /// the specified type.
00292 ///
00293 class ListRecTy : public RecTy {
00294   RecTy *Ty;
00295   explicit ListRecTy(RecTy *T) : RecTy(ListRecTyKind), Ty(T) {}
00296   friend ListRecTy *RecTy::getListTy();
00297 public:
00298   static bool classof(const RecTy *RT) {
00299     return RT->getRecTyKind() == ListRecTyKind;
00300   }
00301 
00302   static ListRecTy *get(RecTy *T) { return T->getListTy(); }
00303   RecTy *getElementType() const { return Ty; }
00304 
00305   virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
00306   virtual Init *convertValue(   BitInit *BI) { return 0; }
00307   virtual Init *convertValue(  BitsInit *BI) { return 0; }
00308   virtual Init *convertValue(   IntInit *II) { return 0; }
00309   virtual Init *convertValue(StringInit *SI) { return 0; }
00310   virtual Init *convertValue(  ListInit *LI);
00311   virtual Init *convertValue(VarBitInit *VB) { return 0; }
00312   virtual Init *convertValue(   DefInit *DI) { return 0; }
00313   virtual Init *convertValue(   DagInit *DI) { return 0; }
00314   virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
00315   virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
00316   virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
00317   virtual Init *convertValue( TypedInit *TI);
00318   virtual Init *convertValue(   VarInit *VI) { return RecTy::convertValue(VI);}
00319   virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
00320 
00321   virtual std::string getAsString() const;
00322 
00323   virtual bool typeIsConvertibleTo(const RecTy *RHS) const {
00324     return RHS->baseClassOf(this);
00325   }
00326 
00327   virtual bool baseClassOf(const RecTy*) const;
00328 };
00329 
00330 /// DagRecTy - 'dag' - Represent a dag fragment
00331 ///
00332 class DagRecTy : public RecTy {
00333   static DagRecTy Shared;
00334   DagRecTy() : RecTy(DagRecTyKind) {}
00335 public:
00336   static bool classof(const RecTy *RT) {
00337     return RT->getRecTyKind() == DagRecTyKind;
00338   }
00339 
00340   static DagRecTy *get() { return &Shared; }
00341 
00342   virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
00343   virtual Init *convertValue(   BitInit *BI) { return 0; }
00344   virtual Init *convertValue(  BitsInit *BI) { return 0; }
00345   virtual Init *convertValue(   IntInit *II) { return 0; }
00346   virtual Init *convertValue(StringInit *SI) { return 0; }
00347   virtual Init *convertValue(  ListInit *LI) { return 0; }
00348   virtual Init *convertValue(VarBitInit *VB) { return 0; }
00349   virtual Init *convertValue(   DefInit *DI) { return 0; }
00350   virtual Init *convertValue( UnOpInit *BO);
00351   virtual Init *convertValue( BinOpInit *BO);
00352   virtual Init *convertValue( TernOpInit *BO) { return RecTy::convertValue(BO);}
00353   virtual Init *convertValue(   DagInit *CI) { return (Init*)CI; }
00354   virtual Init *convertValue( TypedInit *TI);
00355   virtual Init *convertValue(   VarInit *VI) { return RecTy::convertValue(VI);}
00356   virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
00357 
00358   virtual std::string getAsString() const { return "dag"; }
00359 
00360   virtual bool typeIsConvertibleTo(const RecTy *RHS) const {
00361     return RHS->baseClassOf(this);
00362   }
00363 };
00364 
00365 
00366 /// RecordRecTy - '[classname]' - Represent an instance of a class, such as:
00367 /// (R32 X = EAX).
00368 ///
00369 class RecordRecTy : public RecTy {
00370   Record *Rec;
00371   explicit RecordRecTy(Record *R) : RecTy(RecordRecTyKind), Rec(R) {}
00372   friend class Record;
00373 public:
00374   static bool classof(const RecTy *RT) {
00375     return RT->getRecTyKind() == RecordRecTyKind;
00376   }
00377 
00378   static RecordRecTy *get(Record *R);
00379 
00380   Record *getRecord() const { return Rec; }
00381 
00382   virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
00383   virtual Init *convertValue(   BitInit *BI) { return 0; }
00384   virtual Init *convertValue(  BitsInit *BI) { return 0; }
00385   virtual Init *convertValue(   IntInit *II) { return 0; }
00386   virtual Init *convertValue(StringInit *SI) { return 0; }
00387   virtual Init *convertValue(  ListInit *LI) { return 0; }
00388   virtual Init *convertValue(VarBitInit *VB) { return 0; }
00389   virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
00390   virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
00391   virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
00392   virtual Init *convertValue(   DefInit *DI);
00393   virtual Init *convertValue(   DagInit *DI) { return 0; }
00394   virtual Init *convertValue( TypedInit *VI);
00395   virtual Init *convertValue(   VarInit *VI) { return RecTy::convertValue(VI);}
00396   virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
00397 
00398   virtual std::string getAsString() const;
00399 
00400   virtual bool typeIsConvertibleTo(const RecTy *RHS) const {
00401     return RHS->baseClassOf(this);
00402   }
00403   virtual bool baseClassOf(const RecTy*) const;
00404 };
00405 
00406 /// resolveTypes - Find a common type that T1 and T2 convert to.
00407 /// Return 0 if no such type exists.
00408 ///
00409 RecTy *resolveTypes(RecTy *T1, RecTy *T2);
00410 
00411 //===----------------------------------------------------------------------===//
00412 //  Initializer Classes
00413 //===----------------------------------------------------------------------===//
00414 
00415 class Init {
00416 protected:
00417   /// \brief Discriminator enum (for isa<>, dyn_cast<>, et al.)
00418   ///
00419   /// This enum is laid out by a preorder traversal of the inheritance
00420   /// hierarchy, and does not contain an entry for abstract classes, as per
00421   /// the recommendation in docs/HowToSetUpLLVMStyleRTTI.rst.
00422   ///
00423   /// We also explicitly include "first" and "last" values for each
00424   /// interior node of the inheritance tree, to make it easier to read the
00425   /// corresponding classof().
00426   ///
00427   /// We could pack these a bit tighter by not having the IK_FirstXXXInit
00428   /// and IK_LastXXXInit be their own values, but that would degrade
00429   /// readability for really no benefit.
00430   enum InitKind {
00431     IK_BitInit,
00432     IK_BitsInit,
00433     IK_FirstTypedInit,
00434     IK_DagInit,
00435     IK_DefInit,
00436     IK_FieldInit,
00437     IK_IntInit,
00438     IK_ListInit,
00439     IK_FirstOpInit,
00440     IK_BinOpInit,
00441     IK_TernOpInit,
00442     IK_UnOpInit,
00443     IK_LastOpInit,
00444     IK_StringInit,
00445     IK_VarInit,
00446     IK_VarListElementInit,
00447     IK_LastTypedInit,
00448     IK_UnsetInit,
00449     IK_VarBitInit
00450   };
00451 
00452 private:
00453   const InitKind Kind;
00454   Init(const Init &) LLVM_DELETED_FUNCTION;
00455   Init &operator=(const Init &) LLVM_DELETED_FUNCTION;
00456   virtual void anchor();
00457 
00458 public:
00459   InitKind getKind() const { return Kind; }
00460 
00461 protected:
00462   explicit Init(InitKind K) : Kind(K) {}
00463 
00464 public:
00465   virtual ~Init() {}
00466 
00467   /// isComplete - This virtual method should be overridden by values that may
00468   /// not be completely specified yet.
00469   virtual bool isComplete() const { return true; }
00470 
00471   /// print - Print out this value.
00472   void print(raw_ostream &OS) const { OS << getAsString(); }
00473 
00474   /// getAsString - Convert this value to a string form.
00475   virtual std::string getAsString() const = 0;
00476   /// getAsUnquotedString - Convert this value to a string form,
00477   /// without adding quote markers.  This primaruly affects
00478   /// StringInits where we will not surround the string value with
00479   /// quotes.
00480   virtual std::string getAsUnquotedString() const { return getAsString(); }
00481 
00482   /// dump - Debugging method that may be called through a debugger, just
00483   /// invokes print on stderr.
00484   void dump() const;
00485 
00486   /// convertInitializerTo - This virtual function is a simple call-back
00487   /// function that should be overridden to call the appropriate
00488   /// RecTy::convertValue method.
00489   ///
00490   virtual Init *convertInitializerTo(RecTy *Ty) const = 0;
00491 
00492   /// convertInitializerBitRange - This method is used to implement the bitrange
00493   /// selection operator.  Given an initializer, it selects the specified bits
00494   /// out, returning them as a new init of bits type.  If it is not legal to use
00495   /// the bit subscript operator on this initializer, return null.
00496   ///
00497   virtual Init *
00498   convertInitializerBitRange(const std::vector<unsigned> &Bits) const {
00499     return 0;
00500   }
00501 
00502   /// convertInitListSlice - This method is used to implement the list slice
00503   /// selection operator.  Given an initializer, it selects the specified list
00504   /// elements, returning them as a new init of list type.  If it is not legal
00505   /// to take a slice of this, return null.
00506   ///
00507   virtual Init *
00508   convertInitListSlice(const std::vector<unsigned> &Elements) const {
00509     return 0;
00510   }
00511 
00512   /// getFieldType - This method is used to implement the FieldInit class.
00513   /// Implementors of this method should return the type of the named field if
00514   /// they are of record type.
00515   ///
00516   virtual RecTy *getFieldType(const std::string &FieldName) const { return 0; }
00517 
00518   /// getFieldInit - This method complements getFieldType to return the
00519   /// initializer for the specified field.  If getFieldType returns non-null
00520   /// this method should return non-null, otherwise it returns null.
00521   ///
00522   virtual Init *getFieldInit(Record &R, const RecordVal *RV,
00523                              const std::string &FieldName) const {
00524     return 0;
00525   }
00526 
00527   /// resolveReferences - This method is used by classes that refer to other
00528   /// variables which may not be defined at the time the expression is formed.
00529   /// If a value is set for the variable later, this method will be called on
00530   /// users of the value to allow the value to propagate out.
00531   ///
00532   virtual Init *resolveReferences(Record &R, const RecordVal *RV) const {
00533     return const_cast<Init *>(this);
00534   }
00535 
00536   /// getBit - This method is used to return the initializer for the specified
00537   /// bit.
00538   virtual Init *getBit(unsigned Bit) const = 0;
00539 
00540   /// getBitVar - This method is used to retrieve the initializer for bit
00541   /// reference. For non-VarBitInit, it simply returns itself.
00542   virtual Init *getBitVar() const { return const_cast<Init*>(this); }
00543 
00544   /// getBitNum - This method is used to retrieve the bit number of a bit
00545   /// reference. For non-VarBitInit, it simply returns 0.
00546   virtual unsigned getBitNum() const { return 0; }
00547 };
00548 
00549 inline raw_ostream &operator<<(raw_ostream &OS, const Init &I) {
00550   I.print(OS); return OS;
00551 }
00552 
00553 /// TypedInit - This is the common super-class of types that have a specific,
00554 /// explicit, type.
00555 ///
00556 class TypedInit : public Init {
00557   RecTy *Ty;
00558 
00559   TypedInit(const TypedInit &Other) LLVM_DELETED_FUNCTION;
00560   TypedInit &operator=(const TypedInit &Other) LLVM_DELETED_FUNCTION;
00561 
00562 protected:
00563   explicit TypedInit(InitKind K, RecTy *T) : Init(K), Ty(T) {}
00564 
00565 public:
00566   static bool classof(const Init *I) {
00567     return I->getKind() >= IK_FirstTypedInit &&
00568            I->getKind() <= IK_LastTypedInit;
00569   }
00570   RecTy *getType() const { return Ty; }
00571 
00572   virtual Init *
00573   convertInitializerBitRange(const std::vector<unsigned> &Bits) const;
00574   virtual Init *
00575   convertInitListSlice(const std::vector<unsigned> &Elements) const;
00576 
00577   /// getFieldType - This method is used to implement the FieldInit class.
00578   /// Implementors of this method should return the type of the named field if
00579   /// they are of record type.
00580   ///
00581   virtual RecTy *getFieldType(const std::string &FieldName) const;
00582 
00583   /// resolveListElementReference - This method is used to implement
00584   /// VarListElementInit::resolveReferences.  If the list element is resolvable
00585   /// now, we return the resolved value, otherwise we return null.
00586   virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
00587                                             unsigned Elt) const = 0;
00588 };
00589 
00590 
00591 /// UnsetInit - ? - Represents an uninitialized value
00592 ///
00593 class UnsetInit : public Init {
00594   UnsetInit() : Init(IK_UnsetInit) {}
00595   UnsetInit(const UnsetInit &) LLVM_DELETED_FUNCTION;
00596   UnsetInit &operator=(const UnsetInit &Other) LLVM_DELETED_FUNCTION;
00597   virtual void anchor();
00598 
00599 public:
00600   static bool classof(const Init *I) {
00601     return I->getKind() == IK_UnsetInit;
00602   }
00603   static UnsetInit *get();
00604 
00605   virtual Init *convertInitializerTo(RecTy *Ty) const {
00606     return Ty->convertValue(const_cast<UnsetInit *>(this));
00607   }
00608 
00609   virtual Init *getBit(unsigned Bit) const {
00610     return const_cast<UnsetInit*>(this);
00611   }
00612 
00613   virtual bool isComplete() const { return false; }
00614   virtual std::string getAsString() const { return "?"; }
00615 };
00616 
00617 
00618 /// BitInit - true/false - Represent a concrete initializer for a bit.
00619 ///
00620 class BitInit : public Init {
00621   bool Value;
00622 
00623   explicit BitInit(bool V) : Init(IK_BitInit), Value(V) {}
00624   BitInit(const BitInit &Other) LLVM_DELETED_FUNCTION;
00625   BitInit &operator=(BitInit &Other) LLVM_DELETED_FUNCTION;
00626   virtual void anchor();
00627 
00628 public:
00629   static bool classof(const Init *I) {
00630     return I->getKind() == IK_BitInit;
00631   }
00632   static BitInit *get(bool V);
00633 
00634   bool getValue() const { return Value; }
00635 
00636   virtual Init *convertInitializerTo(RecTy *Ty) const {
00637     return Ty->convertValue(const_cast<BitInit *>(this));
00638   }
00639 
00640   virtual Init *getBit(unsigned Bit) const {
00641     assert(Bit < 1 && "Bit index out of range!");
00642     return const_cast<BitInit*>(this);
00643   }
00644 
00645   virtual std::string getAsString() const { return Value ? "1" : "0"; }
00646 };
00647 
00648 /// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
00649 /// It contains a vector of bits, whose size is determined by the type.
00650 ///
00651 class BitsInit : public Init, public FoldingSetNode {
00652   std::vector<Init*> Bits;
00653 
00654   BitsInit(ArrayRef<Init *> Range)
00655     : Init(IK_BitsInit), Bits(Range.begin(), Range.end()) {}
00656 
00657   BitsInit(const BitsInit &Other) LLVM_DELETED_FUNCTION;
00658   BitsInit &operator=(const BitsInit &Other) LLVM_DELETED_FUNCTION;
00659 
00660 public:
00661   static bool classof(const Init *I) {
00662     return I->getKind() == IK_BitsInit;
00663   }
00664   static BitsInit *get(ArrayRef<Init *> Range);
00665 
00666   void Profile(FoldingSetNodeID &ID) const;
00667 
00668   unsigned getNumBits() const { return Bits.size(); }
00669 
00670   virtual Init *convertInitializerTo(RecTy *Ty) const {
00671     return Ty->convertValue(const_cast<BitsInit *>(this));
00672   }
00673   virtual Init *
00674   convertInitializerBitRange(const std::vector<unsigned> &Bits) const;
00675 
00676   virtual bool isComplete() const {
00677     for (unsigned i = 0; i != getNumBits(); ++i)
00678       if (!getBit(i)->isComplete()) return false;
00679     return true;
00680   }
00681   bool allInComplete() const {
00682     for (unsigned i = 0; i != getNumBits(); ++i)
00683       if (getBit(i)->isComplete()) return false;
00684     return true;
00685   }
00686   virtual std::string getAsString() const;
00687 
00688   virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
00689 
00690   virtual Init *getBit(unsigned Bit) const {
00691     assert(Bit < Bits.size() && "Bit index out of range!");
00692     return Bits[Bit];
00693   }
00694 };
00695 
00696 
00697 /// IntInit - 7 - Represent an initalization by a literal integer value.
00698 ///
00699 class IntInit : public TypedInit {
00700   int64_t Value;
00701 
00702   explicit IntInit(int64_t V)
00703     : TypedInit(IK_IntInit, IntRecTy::get()), Value(V) {}
00704 
00705   IntInit(const IntInit &Other) LLVM_DELETED_FUNCTION;
00706   IntInit &operator=(const IntInit &Other) LLVM_DELETED_FUNCTION;
00707 
00708 public:
00709   static bool classof(const Init *I) {
00710     return I->getKind() == IK_IntInit;
00711   }
00712   static IntInit *get(int64_t V);
00713 
00714   int64_t getValue() const { return Value; }
00715 
00716   virtual Init *convertInitializerTo(RecTy *Ty) const {
00717     return Ty->convertValue(const_cast<IntInit *>(this));
00718   }
00719   virtual Init *
00720   convertInitializerBitRange(const std::vector<unsigned> &Bits) const;
00721 
00722   virtual std::string getAsString() const;
00723 
00724   /// resolveListElementReference - This method is used to implement
00725   /// VarListElementInit::resolveReferences.  If the list element is resolvable
00726   /// now, we return the resolved value, otherwise we return null.
00727   virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
00728                                             unsigned Elt) const {
00729     llvm_unreachable("Illegal element reference off int");
00730   }
00731 
00732   virtual Init *getBit(unsigned Bit) const {
00733     return BitInit::get((Value & (1ULL << Bit)) != 0);
00734   }
00735 };
00736 
00737 
00738 /// StringInit - "foo" - Represent an initialization by a string value.
00739 ///
00740 class StringInit : public TypedInit {
00741   std::string Value;
00742 
00743   explicit StringInit(const std::string &V)
00744     : TypedInit(IK_StringInit, StringRecTy::get()), Value(V) {}
00745 
00746   StringInit(const StringInit &Other) LLVM_DELETED_FUNCTION;
00747   StringInit &operator=(const StringInit &Other) LLVM_DELETED_FUNCTION;
00748   virtual void anchor();
00749 
00750 public:
00751   static bool classof(const Init *I) {
00752     return I->getKind() == IK_StringInit;
00753   }
00754   static StringInit *get(StringRef);
00755 
00756   const std::string &getValue() const { return Value; }
00757 
00758   virtual Init *convertInitializerTo(RecTy *Ty) const {
00759     return Ty->convertValue(const_cast<StringInit *>(this));
00760   }
00761 
00762   virtual std::string getAsString() const { return "\"" + Value + "\""; }
00763   virtual std::string getAsUnquotedString() const { return Value; }
00764 
00765   /// resolveListElementReference - This method is used to implement
00766   /// VarListElementInit::resolveReferences.  If the list element is resolvable
00767   /// now, we return the resolved value, otherwise we return null.
00768   virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
00769                                             unsigned Elt) const {
00770     llvm_unreachable("Illegal element reference off string");
00771   }
00772 
00773   virtual Init *getBit(unsigned Bit) const {
00774     llvm_unreachable("Illegal bit reference off string");
00775   }
00776 };
00777 
00778 /// ListInit - [AL, AH, CL] - Represent a list of defs
00779 ///
00780 class ListInit : public TypedInit, public FoldingSetNode {
00781   std::vector<Init*> Values;
00782 public:
00783   typedef std::vector<Init*>::const_iterator const_iterator;
00784 
00785 private:
00786   explicit ListInit(ArrayRef<Init *> Range, RecTy *EltTy)
00787     : TypedInit(IK_ListInit, ListRecTy::get(EltTy)),
00788       Values(Range.begin(), Range.end()) {}
00789 
00790   ListInit(const ListInit &Other) LLVM_DELETED_FUNCTION;
00791   ListInit &operator=(const ListInit &Other) LLVM_DELETED_FUNCTION;
00792 
00793 public:
00794   static bool classof(const Init *I) {
00795     return I->getKind() == IK_ListInit;
00796   }
00797   static ListInit *get(ArrayRef<Init *> Range, RecTy *EltTy);
00798 
00799   void Profile(FoldingSetNodeID &ID) const;
00800 
00801   unsigned getSize() const { return Values.size(); }
00802   Init *getElement(unsigned i) const {
00803     assert(i < Values.size() && "List element index out of range!");
00804     return Values[i];
00805   }
00806 
00807   Record *getElementAsRecord(unsigned i) const;
00808 
00809   virtual Init *
00810     convertInitListSlice(const std::vector<unsigned> &Elements) const;
00811 
00812   virtual Init *convertInitializerTo(RecTy *Ty) const {
00813     return Ty->convertValue(const_cast<ListInit *>(this));
00814   }
00815 
00816   /// resolveReferences - This method is used by classes that refer to other
00817   /// variables which may not be defined at the time they expression is formed.
00818   /// If a value is set for the variable later, this method will be called on
00819   /// users of the value to allow the value to propagate out.
00820   ///
00821   virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
00822 
00823   virtual std::string getAsString() const;
00824 
00825   ArrayRef<Init*> getValues() const { return Values; }
00826 
00827   inline const_iterator begin() const { return Values.begin(); }
00828   inline const_iterator end  () const { return Values.end();   }
00829 
00830   inline size_t         size () const { return Values.size();  }
00831   inline bool           empty() const { return Values.empty(); }
00832 
00833   /// resolveListElementReference - This method is used to implement
00834   /// VarListElementInit::resolveReferences.  If the list element is resolvable
00835   /// now, we return the resolved value, otherwise we return null.
00836   virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
00837                                             unsigned Elt) const;
00838 
00839   virtual Init *getBit(unsigned Bit) const {
00840     llvm_unreachable("Illegal bit reference off list");
00841   }
00842 };
00843 
00844 
00845 /// OpInit - Base class for operators
00846 ///
00847 class OpInit : public TypedInit {
00848   OpInit(const OpInit &Other) LLVM_DELETED_FUNCTION;
00849   OpInit &operator=(OpInit &Other) LLVM_DELETED_FUNCTION;
00850 
00851 protected:
00852   explicit OpInit(InitKind K, RecTy *Type) : TypedInit(K, Type) {}
00853 
00854 public:
00855   static bool classof(const Init *I) {
00856     return I->getKind() >= IK_FirstOpInit &&
00857            I->getKind() <= IK_LastOpInit;
00858   }
00859   // Clone - Clone this operator, replacing arguments with the new list
00860   virtual OpInit *clone(std::vector<Init *> &Operands) const = 0;
00861 
00862   virtual int getNumOperands() const = 0;
00863   virtual Init *getOperand(int i) const = 0;
00864 
00865   // Fold - If possible, fold this to a simpler init.  Return this if not
00866   // possible to fold.
00867   virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const = 0;
00868 
00869   virtual Init *convertInitializerTo(RecTy *Ty) const {
00870     return Ty->convertValue(const_cast<OpInit *>(this));
00871   }
00872 
00873   virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
00874                                             unsigned Elt) const;
00875 
00876   virtual Init *getBit(unsigned Bit) const;
00877 };
00878 
00879 
00880 /// UnOpInit - !op (X) - Transform an init.
00881 ///
00882 class UnOpInit : public OpInit {
00883 public:
00884   enum UnaryOp { CAST, HEAD, TAIL, EMPTY };
00885 private:
00886   UnaryOp Opc;
00887   Init *LHS;
00888 
00889   UnOpInit(UnaryOp opc, Init *lhs, RecTy *Type)
00890     : OpInit(IK_UnOpInit, Type), Opc(opc), LHS(lhs) {}
00891 
00892   UnOpInit(const UnOpInit &Other) LLVM_DELETED_FUNCTION;
00893   UnOpInit &operator=(const UnOpInit &Other) LLVM_DELETED_FUNCTION;
00894 
00895 public:
00896   static bool classof(const Init *I) {
00897     return I->getKind() == IK_UnOpInit;
00898   }
00899   static UnOpInit *get(UnaryOp opc, Init *lhs, RecTy *Type);
00900 
00901   // Clone - Clone this operator, replacing arguments with the new list
00902   virtual OpInit *clone(std::vector<Init *> &Operands) const {
00903     assert(Operands.size() == 1 &&
00904            "Wrong number of operands for unary operation");
00905     return UnOpInit::get(getOpcode(), *Operands.begin(), getType());
00906   }
00907 
00908   virtual int getNumOperands() const { return 1; }
00909   virtual Init *getOperand(int i) const {
00910     assert(i == 0 && "Invalid operand id for unary operator");
00911     return getOperand();
00912   }
00913 
00914   UnaryOp getOpcode() const { return Opc; }
00915   Init *getOperand() const { return LHS; }
00916 
00917   // Fold - If possible, fold this to a simpler init.  Return this if not
00918   // possible to fold.
00919   virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const;
00920 
00921   virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
00922 
00923   virtual std::string getAsString() const;
00924 };
00925 
00926 /// BinOpInit - !op (X, Y) - Combine two inits.
00927 ///
00928 class BinOpInit : public OpInit {
00929 public:
00930   enum BinaryOp { ADD, SHL, SRA, SRL, STRCONCAT, CONCAT, EQ };
00931 private:
00932   BinaryOp Opc;
00933   Init *LHS, *RHS;
00934 
00935   BinOpInit(BinaryOp opc, Init *lhs, Init *rhs, RecTy *Type) :
00936       OpInit(IK_BinOpInit, Type), Opc(opc), LHS(lhs), RHS(rhs) {}
00937 
00938   BinOpInit(const BinOpInit &Other) LLVM_DELETED_FUNCTION;
00939   BinOpInit &operator=(const BinOpInit &Other) LLVM_DELETED_FUNCTION;
00940 
00941 public:
00942   static bool classof(const Init *I) {
00943     return I->getKind() == IK_BinOpInit;
00944   }
00945   static BinOpInit *get(BinaryOp opc, Init *lhs, Init *rhs,
00946                         RecTy *Type);
00947 
00948   // Clone - Clone this operator, replacing arguments with the new list
00949   virtual OpInit *clone(std::vector<Init *> &Operands) const {
00950     assert(Operands.size() == 2 &&
00951            "Wrong number of operands for binary operation");
00952     return BinOpInit::get(getOpcode(), Operands[0], Operands[1], getType());
00953   }
00954 
00955   virtual int getNumOperands() const { return 2; }
00956   virtual Init *getOperand(int i) const {
00957     assert((i == 0 || i == 1) && "Invalid operand id for binary operator");
00958     if (i == 0) {
00959       return getLHS();
00960     } else {
00961       return getRHS();
00962     }
00963   }
00964 
00965   BinaryOp getOpcode() const { return Opc; }
00966   Init *getLHS() const { return LHS; }
00967   Init *getRHS() const { return RHS; }
00968 
00969   // Fold - If possible, fold this to a simpler init.  Return this if not
00970   // possible to fold.
00971   virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const;
00972 
00973   virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
00974 
00975   virtual std::string getAsString() const;
00976 };
00977 
00978 /// TernOpInit - !op (X, Y, Z) - Combine two inits.
00979 ///
00980 class TernOpInit : public OpInit {
00981 public:
00982   enum TernaryOp { SUBST, FOREACH, IF };
00983 private:
00984   TernaryOp Opc;
00985   Init *LHS, *MHS, *RHS;
00986 
00987   TernOpInit(TernaryOp opc, Init *lhs, Init *mhs, Init *rhs,
00988              RecTy *Type) :
00989       OpInit(IK_TernOpInit, Type), Opc(opc), LHS(lhs), MHS(mhs), RHS(rhs) {}
00990 
00991   TernOpInit(const TernOpInit &Other) LLVM_DELETED_FUNCTION;
00992   TernOpInit &operator=(const TernOpInit &Other) LLVM_DELETED_FUNCTION;
00993 
00994 public:
00995   static bool classof(const Init *I) {
00996     return I->getKind() == IK_TernOpInit;
00997   }
00998   static TernOpInit *get(TernaryOp opc, Init *lhs,
00999                          Init *mhs, Init *rhs,
01000                          RecTy *Type);
01001 
01002   // Clone - Clone this operator, replacing arguments with the new list
01003   virtual OpInit *clone(std::vector<Init *> &Operands) const {
01004     assert(Operands.size() == 3 &&
01005            "Wrong number of operands for ternary operation");
01006     return TernOpInit::get(getOpcode(), Operands[0], Operands[1], Operands[2],
01007                            getType());
01008   }
01009 
01010   virtual int getNumOperands() const { return 3; }
01011   virtual Init *getOperand(int i) const {
01012     assert((i == 0 || i == 1 || i == 2) &&
01013            "Invalid operand id for ternary operator");
01014     if (i == 0) {
01015       return getLHS();
01016     } else if (i == 1) {
01017       return getMHS();
01018     } else {
01019       return getRHS();
01020     }
01021   }
01022 
01023   TernaryOp getOpcode() const { return Opc; }
01024   Init *getLHS() const { return LHS; }
01025   Init *getMHS() const { return MHS; }
01026   Init *getRHS() const { return RHS; }
01027 
01028   // Fold - If possible, fold this to a simpler init.  Return this if not
01029   // possible to fold.
01030   virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const;
01031 
01032   virtual bool isComplete() const { return false; }
01033 
01034   virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
01035 
01036   virtual std::string getAsString() const;
01037 };
01038 
01039 
01040 /// VarInit - 'Opcode' - Represent a reference to an entire variable object.
01041 ///
01042 class VarInit : public TypedInit {
01043   Init *VarName;
01044 
01045   explicit VarInit(const std::string &VN, RecTy *T)
01046       : TypedInit(IK_VarInit, T), VarName(StringInit::get(VN)) {}
01047   explicit VarInit(Init *VN, RecTy *T)
01048       : TypedInit(IK_VarInit, T), VarName(VN) {}
01049 
01050   VarInit(const VarInit &Other) LLVM_DELETED_FUNCTION;
01051   VarInit &operator=(const VarInit &Other) LLVM_DELETED_FUNCTION;
01052 
01053 public:
01054   static bool classof(const Init *I) {
01055     return I->getKind() == IK_VarInit;
01056   }
01057   static VarInit *get(const std::string &VN, RecTy *T);
01058   static VarInit *get(Init *VN, RecTy *T);
01059 
01060   virtual Init *convertInitializerTo(RecTy *Ty) const {
01061     return Ty->convertValue(const_cast<VarInit *>(this));
01062   }
01063 
01064   const std::string &getName() const;
01065   Init *getNameInit() const { return VarName; }
01066   std::string getNameInitAsString() const {
01067     return getNameInit()->getAsUnquotedString();
01068   }
01069 
01070   virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
01071                                             unsigned Elt) const;
01072 
01073   virtual RecTy *getFieldType(const std::string &FieldName) const;
01074   virtual Init *getFieldInit(Record &R, const RecordVal *RV,
01075                              const std::string &FieldName) const;
01076 
01077   /// resolveReferences - This method is used by classes that refer to other
01078   /// variables which may not be defined at the time they expression is formed.
01079   /// If a value is set for the variable later, this method will be called on
01080   /// users of the value to allow the value to propagate out.
01081   ///
01082   virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
01083 
01084   virtual Init *getBit(unsigned Bit) const;
01085 
01086   virtual std::string getAsString() const { return getName(); }
01087 };
01088 
01089 
01090 /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
01091 ///
01092 class VarBitInit : public Init {
01093   TypedInit *TI;
01094   unsigned Bit;
01095 
01096   VarBitInit(TypedInit *T, unsigned B) : Init(IK_VarBitInit), TI(T), Bit(B) {
01097     assert(T->getType() &&
01098            (isa<IntRecTy>(T->getType()) ||
01099             (isa<BitsRecTy>(T->getType()) &&
01100              cast<BitsRecTy>(T->getType())->getNumBits() > B)) &&
01101            "Illegal VarBitInit expression!");
01102   }
01103 
01104   VarBitInit(const VarBitInit &Other) LLVM_DELETED_FUNCTION;
01105   VarBitInit &operator=(const VarBitInit &Other) LLVM_DELETED_FUNCTION;
01106 
01107 public:
01108   static bool classof(const Init *I) {
01109     return I->getKind() == IK_VarBitInit;
01110   }
01111   static VarBitInit *get(TypedInit *T, unsigned B);
01112 
01113   virtual Init *convertInitializerTo(RecTy *Ty) const {
01114     return Ty->convertValue(const_cast<VarBitInit *>(this));
01115   }
01116 
01117   virtual Init *getBitVar() const { return TI; }
01118   virtual unsigned getBitNum() const { return Bit; }
01119 
01120   virtual std::string getAsString() const;
01121   virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
01122 
01123   virtual Init *getBit(unsigned B) const {
01124     assert(B < 1 && "Bit index out of range!");
01125     return const_cast<VarBitInit*>(this);
01126   }
01127 };
01128 
01129 /// VarListElementInit - List[4] - Represent access to one element of a var or
01130 /// field.
01131 class VarListElementInit : public TypedInit {
01132   TypedInit *TI;
01133   unsigned Element;
01134 
01135   VarListElementInit(TypedInit *T, unsigned E)
01136       : TypedInit(IK_VarListElementInit,
01137                   cast<ListRecTy>(T->getType())->getElementType()),
01138         TI(T), Element(E) {
01139     assert(T->getType() && isa<ListRecTy>(T->getType()) &&
01140            "Illegal VarBitInit expression!");
01141   }
01142 
01143   VarListElementInit(const VarListElementInit &Other) LLVM_DELETED_FUNCTION;
01144   void operator=(const VarListElementInit &Other) LLVM_DELETED_FUNCTION;
01145 
01146 public:
01147   static bool classof(const Init *I) {
01148     return I->getKind() == IK_VarListElementInit;
01149   }
01150   static VarListElementInit *get(TypedInit *T, unsigned E);
01151 
01152   virtual Init *convertInitializerTo(RecTy *Ty) const {
01153     return Ty->convertValue(const_cast<VarListElementInit *>(this));
01154   }
01155 
01156   TypedInit *getVariable() const { return TI; }
01157   unsigned getElementNum() const { return Element; }
01158 
01159   /// resolveListElementReference - This method is used to implement
01160   /// VarListElementInit::resolveReferences.  If the list element is resolvable
01161   /// now, we return the resolved value, otherwise we return null.
01162   virtual Init *resolveListElementReference(Record &R,
01163                                             const RecordVal *RV,
01164                                             unsigned Elt) const;
01165 
01166   virtual std::string getAsString() const;
01167   virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
01168 
01169   virtual Init *getBit(unsigned Bit) const;
01170 };
01171 
01172 /// DefInit - AL - Represent a reference to a 'def' in the description
01173 ///
01174 class DefInit : public TypedInit {
01175   Record *Def;
01176 
01177   DefInit(Record *D, RecordRecTy *T) : TypedInit(IK_DefInit, T), Def(D) {}
01178   friend class Record;
01179 
01180   DefInit(const DefInit &Other) LLVM_DELETED_FUNCTION;
01181   DefInit &operator=(const DefInit &Other) LLVM_DELETED_FUNCTION;
01182 
01183 public:
01184   static bool classof(const Init *I) {
01185     return I->getKind() == IK_DefInit;
01186   }
01187   static DefInit *get(Record*);
01188 
01189   virtual Init *convertInitializerTo(RecTy *Ty) const {
01190     return Ty->convertValue(const_cast<DefInit *>(this));
01191   }
01192 
01193   Record *getDef() const { return Def; }
01194 
01195   //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
01196 
01197   virtual RecTy *getFieldType(const std::string &FieldName) const;
01198   virtual Init *getFieldInit(Record &R, const RecordVal *RV,
01199                              const std::string &FieldName) const;
01200 
01201   virtual std::string getAsString() const;
01202 
01203   virtual Init *getBit(unsigned Bit) const {
01204     llvm_unreachable("Illegal bit reference off def");
01205   }
01206 
01207   /// resolveListElementReference - This method is used to implement
01208   /// VarListElementInit::resolveReferences.  If the list element is resolvable
01209   /// now, we return the resolved value, otherwise we return null.
01210   virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
01211                                             unsigned Elt) const {
01212     llvm_unreachable("Illegal element reference off def");
01213   }
01214 };
01215 
01216 
01217 /// FieldInit - X.Y - Represent a reference to a subfield of a variable
01218 ///
01219 class FieldInit : public TypedInit {
01220   Init *Rec;                // Record we are referring to
01221   std::string FieldName;    // Field we are accessing
01222 
01223   FieldInit(Init *R, const std::string &FN)
01224       : TypedInit(IK_FieldInit, R->getFieldType(FN)), Rec(R), FieldName(FN) {
01225     assert(getType() && "FieldInit with non-record type!");
01226   }
01227 
01228   FieldInit(const FieldInit &Other) LLVM_DELETED_FUNCTION;
01229   FieldInit &operator=(const FieldInit &Other) LLVM_DELETED_FUNCTION;
01230 
01231 public:
01232   static bool classof(const Init *I) {
01233     return I->getKind() == IK_FieldInit;
01234   }
01235   static FieldInit *get(Init *R, const std::string &FN);
01236   static FieldInit *get(Init *R, const Init *FN);
01237 
01238   virtual Init *convertInitializerTo(RecTy *Ty) const {
01239     return Ty->convertValue(const_cast<FieldInit *>(this));
01240   }
01241 
01242   virtual Init *getBit(unsigned Bit) const;
01243 
01244   virtual Init *resolveListElementReference(Record &R,
01245                                             const RecordVal *RV,
01246                                             unsigned Elt) const;
01247 
01248   virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
01249 
01250   virtual std::string getAsString() const {
01251     return Rec->getAsString() + "." + FieldName;
01252   }
01253 };
01254 
01255 /// DagInit - (v a, b) - Represent a DAG tree value.  DAG inits are required
01256 /// to have at least one value then a (possibly empty) list of arguments.  Each
01257 /// argument can have a name associated with it.
01258 ///
01259 class DagInit : public TypedInit, public FoldingSetNode {
01260   Init *Val;
01261   std::string ValName;
01262   std::vector<Init*> Args;
01263   std::vector<std::string> ArgNames;
01264 
01265   DagInit(Init *V, const std::string &VN,
01266           ArrayRef<Init *> ArgRange,
01267           ArrayRef<std::string> NameRange)
01268       : TypedInit(IK_DagInit, DagRecTy::get()), Val(V), ValName(VN),
01269           Args(ArgRange.begin(), ArgRange.end()),
01270           ArgNames(NameRange.begin(), NameRange.end()) {}
01271 
01272   DagInit(const DagInit &Other) LLVM_DELETED_FUNCTION;
01273   DagInit &operator=(const DagInit &Other) LLVM_DELETED_FUNCTION;
01274 
01275 public:
01276   static bool classof(const Init *I) {
01277     return I->getKind() == IK_DagInit;
01278   }
01279   static DagInit *get(Init *V, const std::string &VN,
01280                       ArrayRef<Init *> ArgRange,
01281                       ArrayRef<std::string> NameRange);
01282   static DagInit *get(Init *V, const std::string &VN,
01283                       const std::vector<
01284                         std::pair<Init*, std::string> > &args);
01285 
01286   void Profile(FoldingSetNodeID &ID) const;
01287 
01288   virtual Init *convertInitializerTo(RecTy *Ty) const {
01289     return Ty->convertValue(const_cast<DagInit *>(this));
01290   }
01291 
01292   Init *getOperator() const { return Val; }
01293 
01294   const std::string &getName() const { return ValName; }
01295 
01296   unsigned getNumArgs() const { return Args.size(); }
01297   Init *getArg(unsigned Num) const {
01298     assert(Num < Args.size() && "Arg number out of range!");
01299     return Args[Num];
01300   }
01301   const std::string &getArgName(unsigned Num) const {
01302     assert(Num < ArgNames.size() && "Arg number out of range!");
01303     return ArgNames[Num];
01304   }
01305 
01306   virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
01307 
01308   virtual std::string getAsString() const;
01309 
01310   typedef std::vector<Init*>::const_iterator       const_arg_iterator;
01311   typedef std::vector<std::string>::const_iterator const_name_iterator;
01312 
01313   inline const_arg_iterator  arg_begin() const { return Args.begin(); }
01314   inline const_arg_iterator  arg_end  () const { return Args.end();   }
01315 
01316   inline size_t              arg_size () const { return Args.size();  }
01317   inline bool                arg_empty() const { return Args.empty(); }
01318 
01319   inline const_name_iterator name_begin() const { return ArgNames.begin(); }
01320   inline const_name_iterator name_end  () const { return ArgNames.end();   }
01321 
01322   inline size_t              name_size () const { return ArgNames.size();  }
01323   inline bool                name_empty() const { return ArgNames.empty(); }
01324 
01325   virtual Init *getBit(unsigned Bit) const {
01326     llvm_unreachable("Illegal bit reference off dag");
01327   }
01328 
01329   virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
01330                                             unsigned Elt) const {
01331     llvm_unreachable("Illegal element reference off dag");
01332   }
01333 };
01334 
01335 //===----------------------------------------------------------------------===//
01336 //  High-Level Classes
01337 //===----------------------------------------------------------------------===//
01338 
01339 class RecordVal {
01340   Init *Name;
01341   RecTy *Ty;
01342   unsigned Prefix;
01343   Init *Value;
01344 public:
01345   RecordVal(Init *N, RecTy *T, unsigned P);
01346   RecordVal(const std::string &N, RecTy *T, unsigned P);
01347 
01348   const std::string &getName() const;
01349   const Init *getNameInit() const { return Name; }
01350   std::string getNameInitAsString() const {
01351     return getNameInit()->getAsUnquotedString();
01352   }
01353 
01354   unsigned getPrefix() const { return Prefix; }
01355   RecTy *getType() const { return Ty; }
01356   Init *getValue() const { return Value; }
01357 
01358   bool setValue(Init *V) {
01359     if (V) {
01360       Value = V->convertInitializerTo(Ty);
01361       return Value == 0;
01362     }
01363     Value = 0;
01364     return false;
01365   }
01366 
01367   void dump() const;
01368   void print(raw_ostream &OS, bool PrintSem = true) const;
01369 };
01370 
01371 inline raw_ostream &operator<<(raw_ostream &OS, const RecordVal &RV) {
01372   RV.print(OS << "  ");
01373   return OS;
01374 }
01375 
01376 class Record {
01377   static unsigned LastID;
01378 
01379   // Unique record ID.
01380   unsigned ID;
01381   Init *Name;
01382   // Location where record was instantiated, followed by the location of
01383   // multiclass prototypes used.
01384   SmallVector<SMLoc, 4> Locs;
01385   std::vector<Init *> TemplateArgs;
01386   std::vector<RecordVal> Values;
01387   std::vector<Record *> SuperClasses;
01388   std::vector<SMRange> SuperClassRanges;
01389 
01390   // Tracks Record instances. Not owned by Record.
01391   RecordKeeper &TrackedRecords;
01392 
01393   DefInit *TheInit;
01394   bool IsAnonymous;
01395 
01396   void init();
01397   void checkName();
01398 
01399 public:
01400 
01401   // Constructs a record.
01402   explicit Record(const std::string &N, ArrayRef<SMLoc> locs,
01403                   RecordKeeper &records, bool Anonymous = false) :
01404     ID(LastID++), Name(StringInit::get(N)), Locs(locs.begin(), locs.end()),
01405     TrackedRecords(records), TheInit(0), IsAnonymous(Anonymous) {
01406     init();
01407   }
01408   explicit Record(Init *N, ArrayRef<SMLoc> locs, RecordKeeper &records,
01409                   bool Anonymous = false) :
01410     ID(LastID++), Name(N), Locs(locs.begin(), locs.end()),
01411     TrackedRecords(records), TheInit(0), IsAnonymous(Anonymous) {
01412     init();
01413   }
01414 
01415   // When copy-constructing a Record, we must still guarantee a globally unique
01416   // ID number.  All other fields can be copied normally.
01417   Record(const Record &O) :
01418     ID(LastID++), Name(O.Name), Locs(O.Locs), TemplateArgs(O.TemplateArgs),
01419     Values(O.Values), SuperClasses(O.SuperClasses),
01420     SuperClassRanges(O.SuperClassRanges), TrackedRecords(O.TrackedRecords),
01421     TheInit(O.TheInit), IsAnonymous(O.IsAnonymous) { }
01422 
01423   ~Record() {}
01424 
01425 
01426   static unsigned getNewUID() { return LastID++; }
01427 
01428 
01429   unsigned getID() const { return ID; }
01430 
01431   const std::string &getName() const;
01432   Init *getNameInit() const {
01433     return Name;
01434   }
01435   const std::string getNameInitAsString() const {
01436     return getNameInit()->getAsUnquotedString();
01437   }
01438 
01439   void setName(Init *Name);               // Also updates RecordKeeper.
01440   void setName(const std::string &Name);  // Also updates RecordKeeper.
01441 
01442   ArrayRef<SMLoc> getLoc() const { return Locs; }
01443 
01444   /// get the corresponding DefInit.
01445   DefInit *getDefInit();
01446 
01447   const std::vector<Init *> &getTemplateArgs() const {
01448     return TemplateArgs;
01449   }
01450   const std::vector<RecordVal> &getValues() const { return Values; }
01451   const std::vector<Record*>   &getSuperClasses() const { return SuperClasses; }
01452   ArrayRef<SMRange> getSuperClassRanges() const { return SuperClassRanges; }
01453 
01454   bool isTemplateArg(Init *Name) const {
01455     for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
01456       if (TemplateArgs[i] == Name) return true;
01457     return false;
01458   }
01459   bool isTemplateArg(StringRef Name) const {
01460     return isTemplateArg(StringInit::get(Name.str()));
01461   }
01462 
01463   const RecordVal *getValue(const Init *Name) const {
01464     for (unsigned i = 0, e = Values.size(); i != e; ++i)
01465       if (Values[i].getNameInit() == Name) return &Values[i];
01466     return 0;
01467   }
01468   const RecordVal *getValue(StringRef Name) const {
01469     return getValue(StringInit::get(Name));
01470   }
01471   RecordVal *getValue(const Init *Name) {
01472     for (unsigned i = 0, e = Values.size(); i != e; ++i)
01473       if (Values[i].getNameInit() == Name) return &Values[i];
01474     return 0;
01475   }
01476   RecordVal *getValue(StringRef Name) {
01477     return getValue(StringInit::get(Name));
01478   }
01479 
01480   void addTemplateArg(Init *Name) {
01481     assert(!isTemplateArg(Name) && "Template arg already defined!");
01482     TemplateArgs.push_back(Name);
01483   }
01484   void addTemplateArg(StringRef Name) {
01485     addTemplateArg(StringInit::get(Name.str()));
01486   }
01487 
01488   void addValue(const RecordVal &RV) {
01489     assert(getValue(RV.getNameInit()) == 0 && "Value already added!");
01490     Values.push_back(RV);
01491     if (Values.size() > 1)
01492       // Keep NAME at the end of the list.  It makes record dumps a
01493       // bit prettier and allows TableGen tests to be written more
01494       // naturally.  Tests can use CHECK-NEXT to look for Record
01495       // fields they expect to see after a def.  They can't do that if
01496       // NAME is the first Record field.
01497       std::swap(Values[Values.size() - 2], Values[Values.size() - 1]);
01498   }
01499 
01500   void removeValue(Init *Name) {
01501     for (unsigned i = 0, e = Values.size(); i != e; ++i)
01502       if (Values[i].getNameInit() == Name) {
01503         Values.erase(Values.begin()+i);
01504         return;
01505       }
01506     llvm_unreachable("Cannot remove an entry that does not exist!");
01507   }
01508 
01509   void removeValue(StringRef Name) {
01510     removeValue(StringInit::get(Name.str()));
01511   }
01512 
01513   bool isSubClassOf(const Record *R) const {
01514     for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
01515       if (SuperClasses[i] == R)
01516         return true;
01517     return false;
01518   }
01519 
01520   bool isSubClassOf(StringRef Name) const {
01521     for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
01522       if (SuperClasses[i]->getNameInitAsString() == Name)
01523         return true;
01524     return false;
01525   }
01526 
01527   void addSuperClass(Record *R, SMRange Range) {
01528     assert(!isSubClassOf(R) && "Already subclassing record!");
01529     SuperClasses.push_back(R);
01530     SuperClassRanges.push_back(Range);
01531   }
01532 
01533   /// resolveReferences - If there are any field references that refer to fields
01534   /// that have been filled in, we can propagate the values now.
01535   ///
01536   void resolveReferences() { resolveReferencesTo(0); }
01537 
01538   /// resolveReferencesTo - If anything in this record refers to RV, replace the
01539   /// reference to RV with the RHS of RV.  If RV is null, we resolve all
01540   /// possible references.
01541   void resolveReferencesTo(const RecordVal *RV);
01542 
01543   RecordKeeper &getRecords() const {
01544     return TrackedRecords;
01545   }
01546 
01547   bool isAnonymous() const {
01548     return IsAnonymous;
01549   }
01550 
01551   void dump() const;
01552 
01553   //===--------------------------------------------------------------------===//
01554   // High-level methods useful to tablegen back-ends
01555   //
01556 
01557   /// getValueInit - Return the initializer for a value with the specified name,
01558   /// or throw an exception if the field does not exist.
01559   ///
01560   Init *getValueInit(StringRef FieldName) const;
01561 
01562   /// Return true if the named field is unset.
01563   bool isValueUnset(StringRef FieldName) const {
01564     return getValueInit(FieldName) == UnsetInit::get();
01565   }
01566 
01567   /// getValueAsString - This method looks up the specified field and returns
01568   /// its value as a string, throwing an exception if the field does not exist
01569   /// or if the value is not a string.
01570   ///
01571   std::string getValueAsString(StringRef FieldName) const;
01572 
01573   /// getValueAsBitsInit - This method looks up the specified field and returns
01574   /// its value as a BitsInit, throwing an exception if the field does not exist
01575   /// or if the value is not the right type.
01576   ///
01577   BitsInit *getValueAsBitsInit(StringRef FieldName) const;
01578 
01579   /// getValueAsListInit - This method looks up the specified field and returns
01580   /// its value as a ListInit, throwing an exception if the field does not exist
01581   /// or if the value is not the right type.
01582   ///
01583   ListInit *getValueAsListInit(StringRef FieldName) const;
01584 
01585   /// getValueAsListOfDefs - This method looks up the specified field and
01586   /// returns its value as a vector of records, throwing an exception if the
01587   /// field does not exist or if the value is not the right type.
01588   ///
01589   std::vector<Record*> getValueAsListOfDefs(StringRef FieldName) const;
01590 
01591   /// getValueAsListOfInts - This method looks up the specified field and
01592   /// returns its value as a vector of integers, throwing an exception if the
01593   /// field does not exist or if the value is not the right type.
01594   ///
01595   std::vector<int64_t> getValueAsListOfInts(StringRef FieldName) const;
01596 
01597   /// getValueAsListOfStrings - This method looks up the specified field and
01598   /// returns its value as a vector of strings, throwing an exception if the
01599   /// field does not exist or if the value is not the right type.
01600   ///
01601   std::vector<std::string> getValueAsListOfStrings(StringRef FieldName) const;
01602 
01603   /// getValueAsDef - This method looks up the specified field and returns its
01604   /// value as a Record, throwing an exception if the field does not exist or if
01605   /// the value is not the right type.
01606   ///
01607   Record *getValueAsDef(StringRef FieldName) const;
01608 
01609   /// getValueAsBit - This method looks up the specified field and returns its
01610   /// value as a bit, throwing an exception if the field does not exist or if
01611   /// the value is not the right type.
01612   ///
01613   bool getValueAsBit(StringRef FieldName) const;
01614 
01615   /// getValueAsBitOrUnset - This method looks up the specified field and
01616   /// returns its value as a bit. If the field is unset, sets Unset to true and
01617   /// retunrs false.
01618   ///
01619   bool getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const;
01620 
01621   /// getValueAsInt - This method looks up the specified field and returns its
01622   /// value as an int64_t, throwing an exception if the field does not exist or
01623   /// if the value is not the right type.
01624   ///
01625   int64_t getValueAsInt(StringRef FieldName) const;
01626 
01627   /// getValueAsDag - This method looks up the specified field and returns its
01628   /// value as an Dag, throwing an exception if the field does not exist or if
01629   /// the value is not the right type.
01630   ///
01631   DagInit *getValueAsDag(StringRef FieldName) const;
01632 };
01633 
01634 raw_ostream &operator<<(raw_ostream &OS, const Record &R);
01635 
01636 struct MultiClass {
01637   Record Rec;  // Placeholder for template args and Name.
01638   typedef std::vector<Record*> RecordVector;
01639   RecordVector DefPrototypes;
01640 
01641   void dump() const;
01642 
01643   MultiClass(const std::string &Name, SMLoc Loc, RecordKeeper &Records) :
01644     Rec(Name, Loc, Records) {}
01645 };
01646 
01647 class RecordKeeper {
01648   std::map<std::string, Record*> Classes, Defs;
01649 
01650 public:
01651   ~RecordKeeper() {
01652     for (std::map<std::string, Record*>::iterator I = Classes.begin(),
01653            E = Classes.end(); I != E; ++I)
01654       delete I->second;
01655     for (std::map<std::string, Record*>::iterator I = Defs.begin(),
01656            E = Defs.end(); I != E; ++I)
01657       delete I->second;
01658   }
01659 
01660   const std::map<std::string, Record*> &getClasses() const { return Classes; }
01661   const std::map<std::string, Record*> &getDefs() const { return Defs; }
01662 
01663   Record *getClass(const std::string &Name) const {
01664     std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
01665     return I == Classes.end() ? 0 : I->second;
01666   }
01667   Record *getDef(const std::string &Name) const {
01668     std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
01669     return I == Defs.end() ? 0 : I->second;
01670   }
01671   void addClass(Record *R) {
01672     bool Ins = Classes.insert(std::make_pair(R->getName(), R)).second;
01673     (void)Ins;
01674     assert(Ins && "Class already exists");
01675   }
01676   void addDef(Record *R) {
01677     bool Ins = Defs.insert(std::make_pair(R->getName(), R)).second;
01678     (void)Ins;
01679     assert(Ins && "Record already exists");
01680   }
01681 
01682   /// removeClass - Remove, but do not delete, the specified record.
01683   ///
01684   void removeClass(const std::string &Name) {
01685     assert(Classes.count(Name) && "Class does not exist!");
01686     Classes.erase(Name);
01687   }
01688   /// removeDef - Remove, but do not delete, the specified record.
01689   ///
01690   void removeDef(const std::string &Name) {
01691     assert(Defs.count(Name) && "Def does not exist!");
01692     Defs.erase(Name);
01693   }
01694 
01695   //===--------------------------------------------------------------------===//
01696   // High-level helper methods, useful for tablegen backends...
01697 
01698   /// getAllDerivedDefinitions - This method returns all concrete definitions
01699   /// that derive from the specified class name.  If a class with the specified
01700   /// name does not exist, an exception is thrown.
01701   std::vector<Record*>
01702   getAllDerivedDefinitions(const std::string &ClassName) const;
01703 
01704   void dump() const;
01705 };
01706 
01707 /// LessRecord - Sorting predicate to sort record pointers by name.
01708 ///
01709 struct LessRecord {
01710   bool operator()(const Record *Rec1, const Record *Rec2) const {
01711     return StringRef(Rec1->getName()).compare_numeric(Rec2->getName()) < 0;
01712   }
01713 };
01714 
01715 /// LessRecordByID - Sorting predicate to sort record pointers by their
01716 /// unique ID. If you just need a deterministic order, use this, since it
01717 /// just compares two `unsigned`; the other sorting predicates require
01718 /// string manipulation.
01719 struct LessRecordByID {
01720   bool operator()(const Record *LHS, const Record *RHS) const {
01721     return LHS->getID() < RHS->getID();
01722   }
01723 };
01724 
01725 /// LessRecordFieldName - Sorting predicate to sort record pointers by their
01726 /// name field.
01727 ///
01728 struct LessRecordFieldName {
01729   bool operator()(const Record *Rec1, const Record *Rec2) const {
01730     return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name");
01731   }
01732 };
01733 
01734 raw_ostream &operator<<(raw_ostream &OS, const RecordKeeper &RK);
01735 
01736 /// QualifyName - Return an Init with a qualifier prefix referring
01737 /// to CurRec's name.
01738 Init *QualifyName(Record &CurRec, MultiClass *CurMultiClass,
01739                   Init *Name, const std::string &Scoper);
01740 
01741 /// QualifyName - Return an Init with a qualifier prefix referring
01742 /// to CurRec's name.
01743 Init *QualifyName(Record &CurRec, MultiClass *CurMultiClass,
01744                   const std::string &Name, const std::string &Scoper);
01745 
01746 } // End llvm namespace
01747 
01748 #endif