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