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