1 //===- llvm/TableGen/Record.h - Classes for Table Records -------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines the main TableGen data structures, including the TableGen 10 // types, values, and high-level data structures. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_TABLEGEN_RECORD_H 15 #define LLVM_TABLEGEN_RECORD_H 16 17 #include "llvm/ADT/ArrayRef.h" 18 #include "llvm/ADT/DenseMap.h" 19 #include "llvm/ADT/DenseSet.h" 20 #include "llvm/ADT/FoldingSet.h" 21 #include "llvm/ADT/PointerIntPair.h" 22 #include "llvm/ADT/SmallVector.h" 23 #include "llvm/ADT/StringRef.h" 24 #include "llvm/Support/Casting.h" 25 #include "llvm/Support/ErrorHandling.h" 26 #include "llvm/Support/SMLoc.h" 27 #include "llvm/Support/TrailingObjects.h" 28 #include "llvm/Support/raw_ostream.h" 29 #include <algorithm> 30 #include <cassert> 31 #include <cstddef> 32 #include <cstdint> 33 #include <map> 34 #include <memory> 35 #include <string> 36 #include <utility> 37 #include <vector> 38 39 namespace llvm { 40 41 class ListRecTy; 42 struct MultiClass; 43 class Record; 44 class RecordKeeper; 45 class RecordVal; 46 class Resolver; 47 class StringInit; 48 class TypedInit; 49 50 //===----------------------------------------------------------------------===// 51 // Type Classes 52 //===----------------------------------------------------------------------===// 53 54 class RecTy { 55 public: 56 /// Subclass discriminator (for dyn_cast<> et al.) 57 enum RecTyKind { 58 BitRecTyKind, 59 BitsRecTyKind, 60 CodeRecTyKind, 61 IntRecTyKind, 62 StringRecTyKind, 63 ListRecTyKind, 64 DagRecTyKind, 65 RecordRecTyKind 66 }; 67 68 private: 69 RecTyKind Kind; 70 /// ListRecTy of the list that has elements of this type. 71 ListRecTy *ListTy = nullptr; 72 73 public: RecTy(RecTyKind K)74 RecTy(RecTyKind K) : Kind(K) {} 75 virtual ~RecTy() = default; 76 getRecTyKind()77 RecTyKind getRecTyKind() const { return Kind; } 78 79 virtual std::string getAsString() const = 0; print(raw_ostream & OS)80 void print(raw_ostream &OS) const { OS << getAsString(); } 81 void dump() const; 82 83 /// Return true if all values of 'this' type can be converted to the specified 84 /// type. 85 virtual bool typeIsConvertibleTo(const RecTy *RHS) const; 86 87 /// Return true if 'this' type is equal to or a subtype of RHS. For example, 88 /// a bit set is not an int, but they are convertible. 89 virtual bool typeIsA(const RecTy *RHS) const; 90 91 /// Returns the type representing list<thistype>. 92 ListRecTy *getListTy(); 93 }; 94 95 inline raw_ostream &operator<<(raw_ostream &OS, const RecTy &Ty) { 96 Ty.print(OS); 97 return OS; 98 } 99 100 /// 'bit' - Represent a single bit 101 class BitRecTy : public RecTy { 102 static BitRecTy Shared; 103 BitRecTy()104 BitRecTy() : RecTy(BitRecTyKind) {} 105 106 public: classof(const RecTy * RT)107 static bool classof(const RecTy *RT) { 108 return RT->getRecTyKind() == BitRecTyKind; 109 } 110 get()111 static BitRecTy *get() { return &Shared; } 112 getAsString()113 std::string getAsString() const override { return "bit"; } 114 115 bool typeIsConvertibleTo(const RecTy *RHS) const override; 116 }; 117 118 /// 'bits<n>' - Represent a fixed number of bits 119 class BitsRecTy : public RecTy { 120 unsigned Size; 121 BitsRecTy(unsigned Sz)122 explicit BitsRecTy(unsigned Sz) : RecTy(BitsRecTyKind), Size(Sz) {} 123 124 public: classof(const RecTy * RT)125 static bool classof(const RecTy *RT) { 126 return RT->getRecTyKind() == BitsRecTyKind; 127 } 128 129 static BitsRecTy *get(unsigned Sz); 130 getNumBits()131 unsigned getNumBits() const { return Size; } 132 133 std::string getAsString() const override; 134 135 bool typeIsConvertibleTo(const RecTy *RHS) const override; 136 137 bool typeIsA(const RecTy *RHS) const override; 138 }; 139 140 /// 'code' - Represent a code fragment 141 class CodeRecTy : public RecTy { 142 static CodeRecTy Shared; 143 CodeRecTy()144 CodeRecTy() : RecTy(CodeRecTyKind) {} 145 146 public: classof(const RecTy * RT)147 static bool classof(const RecTy *RT) { 148 return RT->getRecTyKind() == CodeRecTyKind; 149 } 150 get()151 static CodeRecTy *get() { return &Shared; } 152 getAsString()153 std::string getAsString() const override { return "code"; } 154 155 bool typeIsConvertibleTo(const RecTy *RHS) const override; 156 }; 157 158 /// 'int' - Represent an integer value of no particular size 159 class IntRecTy : public RecTy { 160 static IntRecTy Shared; 161 IntRecTy()162 IntRecTy() : RecTy(IntRecTyKind) {} 163 164 public: classof(const RecTy * RT)165 static bool classof(const RecTy *RT) { 166 return RT->getRecTyKind() == IntRecTyKind; 167 } 168 get()169 static IntRecTy *get() { return &Shared; } 170 getAsString()171 std::string getAsString() const override { return "int"; } 172 173 bool typeIsConvertibleTo(const RecTy *RHS) const override; 174 }; 175 176 /// 'string' - Represent an string value 177 class StringRecTy : public RecTy { 178 static StringRecTy Shared; 179 StringRecTy()180 StringRecTy() : RecTy(StringRecTyKind) {} 181 182 public: classof(const RecTy * RT)183 static bool classof(const RecTy *RT) { 184 return RT->getRecTyKind() == StringRecTyKind; 185 } 186 get()187 static StringRecTy *get() { return &Shared; } 188 189 std::string getAsString() const override; 190 191 bool typeIsConvertibleTo(const RecTy *RHS) const override; 192 }; 193 194 /// 'list<Ty>' - Represent a list of element values, all of which must be of 195 /// the specified type. The type is stored in ElementTy. 196 class ListRecTy : public RecTy { 197 friend ListRecTy *RecTy::getListTy(); 198 199 RecTy *ElementTy; 200 ListRecTy(RecTy * T)201 explicit ListRecTy(RecTy *T) : RecTy(ListRecTyKind), ElementTy(T) {} 202 203 public: classof(const RecTy * RT)204 static bool classof(const RecTy *RT) { 205 return RT->getRecTyKind() == ListRecTyKind; 206 } 207 get(RecTy * T)208 static ListRecTy *get(RecTy *T) { return T->getListTy(); } getElementType()209 RecTy *getElementType() const { return ElementTy; } 210 211 std::string getAsString() const override; 212 213 bool typeIsConvertibleTo(const RecTy *RHS) const override; 214 215 bool typeIsA(const RecTy *RHS) const override; 216 }; 217 218 /// 'dag' - Represent a dag fragment 219 class DagRecTy : public RecTy { 220 static DagRecTy Shared; 221 DagRecTy()222 DagRecTy() : RecTy(DagRecTyKind) {} 223 224 public: classof(const RecTy * RT)225 static bool classof(const RecTy *RT) { 226 return RT->getRecTyKind() == DagRecTyKind; 227 } 228 get()229 static DagRecTy *get() { return &Shared; } 230 231 std::string getAsString() const override; 232 }; 233 234 /// '[classname]' - Type of record values that have zero or more superclasses. 235 /// 236 /// The list of superclasses is non-redundant, i.e. only contains classes that 237 /// are not the superclass of some other listed class. 238 class RecordRecTy final : public RecTy, public FoldingSetNode, 239 public TrailingObjects<RecordRecTy, Record *> { 240 friend class Record; 241 242 unsigned NumClasses; 243 RecordRecTy(unsigned Num)244 explicit RecordRecTy(unsigned Num) 245 : RecTy(RecordRecTyKind), NumClasses(Num) {} 246 247 public: 248 RecordRecTy(const RecordRecTy &) = delete; 249 RecordRecTy &operator=(const RecordRecTy &) = delete; 250 251 // Do not use sized deallocation due to trailing objects. delete(void * p)252 void operator delete(void *p) { ::operator delete(p); } 253 classof(const RecTy * RT)254 static bool classof(const RecTy *RT) { 255 return RT->getRecTyKind() == RecordRecTyKind; 256 } 257 258 /// Get the record type with the given non-redundant list of superclasses. 259 static RecordRecTy *get(ArrayRef<Record *> Classes); 260 261 void Profile(FoldingSetNodeID &ID) const; 262 getClasses()263 ArrayRef<Record *> getClasses() const { 264 return makeArrayRef(getTrailingObjects<Record *>(), NumClasses); 265 } 266 267 using const_record_iterator = Record * const *; 268 classes_begin()269 const_record_iterator classes_begin() const { return getClasses().begin(); } classes_end()270 const_record_iterator classes_end() const { return getClasses().end(); } 271 272 std::string getAsString() const override; 273 274 bool isSubClassOf(Record *Class) const; 275 bool typeIsConvertibleTo(const RecTy *RHS) const override; 276 277 bool typeIsA(const RecTy *RHS) const override; 278 }; 279 280 /// Find a common type that T1 and T2 convert to. 281 /// Return 0 if no such type exists. 282 RecTy *resolveTypes(RecTy *T1, RecTy *T2); 283 284 //===----------------------------------------------------------------------===// 285 // Initializer Classes 286 //===----------------------------------------------------------------------===// 287 288 class Init { 289 protected: 290 /// Discriminator enum (for isa<>, dyn_cast<>, et al.) 291 /// 292 /// This enum is laid out by a preorder traversal of the inheritance 293 /// hierarchy, and does not contain an entry for abstract classes, as per 294 /// the recommendation in docs/HowToSetUpLLVMStyleRTTI.rst. 295 /// 296 /// We also explicitly include "first" and "last" values for each 297 /// interior node of the inheritance tree, to make it easier to read the 298 /// corresponding classof(). 299 /// 300 /// We could pack these a bit tighter by not having the IK_FirstXXXInit 301 /// and IK_LastXXXInit be their own values, but that would degrade 302 /// readability for really no benefit. 303 enum InitKind : uint8_t { 304 IK_First, // unused; silence a spurious warning 305 IK_FirstTypedInit, 306 IK_BitInit, 307 IK_BitsInit, 308 IK_CodeInit, 309 IK_DagInit, 310 IK_DefInit, 311 IK_FieldInit, 312 IK_IntInit, 313 IK_ListInit, 314 IK_FirstOpInit, 315 IK_BinOpInit, 316 IK_TernOpInit, 317 IK_UnOpInit, 318 IK_LastOpInit, 319 IK_CondOpInit, 320 IK_FoldOpInit, 321 IK_IsAOpInit, 322 IK_StringInit, 323 IK_VarInit, 324 IK_VarListElementInit, 325 IK_VarBitInit, 326 IK_VarDefInit, 327 IK_LastTypedInit, 328 IK_UnsetInit 329 }; 330 331 private: 332 const InitKind Kind; 333 334 protected: 335 uint8_t Opc; // Used by UnOpInit, BinOpInit, and TernOpInit 336 337 private: 338 virtual void anchor(); 339 340 public: 341 /// Get the kind (type) of the value. getKind()342 InitKind getKind() const { return Kind; } 343 344 protected: Kind(K)345 explicit Init(InitKind K, uint8_t Opc = 0) : Kind(K), Opc(Opc) {} 346 347 public: 348 Init(const Init &) = delete; 349 Init &operator=(const Init &) = delete; 350 virtual ~Init() = default; 351 352 /// Is this a complete value with no unset (uninitialized) subvalues? isComplete()353 virtual bool isComplete() const { return true; } 354 355 /// Is this a concrete and fully resolved value without any references or 356 /// stuck operations? Unset values are concrete. isConcrete()357 virtual bool isConcrete() const { return false; } 358 359 /// Print this value. print(raw_ostream & OS)360 void print(raw_ostream &OS) const { OS << getAsString(); } 361 362 /// Convert this value to a literal form. 363 virtual std::string getAsString() const = 0; 364 365 /// Convert this value to a literal form, 366 /// without adding quotes around a string. getAsUnquotedString()367 virtual std::string getAsUnquotedString() const { return getAsString(); } 368 369 /// Debugging method that may be called through a debugger; just 370 /// invokes print on stderr. 371 void dump() const; 372 373 /// If this value is convertible to type \p Ty, return a value whose 374 /// type is \p Ty, generating a !cast operation if required. 375 /// Otherwise, return null. 376 virtual Init *getCastTo(RecTy *Ty) const = 0; 377 378 /// Convert to a value whose type is \p Ty, or return null if this 379 /// is not possible. This can happen if the value's type is convertible 380 /// to \p Ty, but there are unresolved references. 381 virtual Init *convertInitializerTo(RecTy *Ty) const = 0; 382 383 /// This function is used to implement the bit range 384 /// selection operator. Given a value, it selects the specified bits, 385 /// returning them as a new \p Init of type \p bits. If it is not legal 386 /// to use the bit selection operator on this value, null is returned. convertInitializerBitRange(ArrayRef<unsigned> Bits)387 virtual Init *convertInitializerBitRange(ArrayRef<unsigned> Bits) const { 388 return nullptr; 389 } 390 391 /// This function is used to implement the list slice 392 /// selection operator. Given a value, it selects the specified list 393 /// elements, returning them as a new \p Init of type \p list. If it 394 /// is not legal to use the slice operator, null is returned. convertInitListSlice(ArrayRef<unsigned> Elements)395 virtual Init *convertInitListSlice(ArrayRef<unsigned> Elements) const { 396 return nullptr; 397 } 398 399 /// This function is used to implement the FieldInit class. 400 /// Implementors of this method should return the type of the named 401 /// field if they are of type record. getFieldType(StringInit * FieldName)402 virtual RecTy *getFieldType(StringInit *FieldName) const { 403 return nullptr; 404 } 405 406 /// This function is used by classes that refer to other 407 /// variables which may not be defined at the time the expression is formed. 408 /// If a value is set for the variable later, this method will be called on 409 /// users of the value to allow the value to propagate out. resolveReferences(Resolver & R)410 virtual Init *resolveReferences(Resolver &R) const { 411 return const_cast<Init *>(this); 412 } 413 414 /// Get the \p Init value of the specified bit. 415 virtual Init *getBit(unsigned Bit) const = 0; 416 }; 417 418 inline raw_ostream &operator<<(raw_ostream &OS, const Init &I) { 419 I.print(OS); return OS; 420 } 421 422 /// This is the common superclass of types that have a specific, 423 /// explicit type, stored in ValueTy. 424 class TypedInit : public Init { 425 RecTy *ValueTy; 426 427 protected: 428 explicit TypedInit(InitKind K, RecTy *T, uint8_t Opc = 0) Init(K,Opc)429 : Init(K, Opc), ValueTy(T) {} 430 431 public: 432 TypedInit(const TypedInit &) = delete; 433 TypedInit &operator=(const TypedInit &) = delete; 434 classof(const Init * I)435 static bool classof(const Init *I) { 436 return I->getKind() >= IK_FirstTypedInit && 437 I->getKind() <= IK_LastTypedInit; 438 } 439 440 /// Get the type of the Init as a RecTy. getType()441 RecTy *getType() const { return ValueTy; } 442 443 Init *getCastTo(RecTy *Ty) const override; 444 Init *convertInitializerTo(RecTy *Ty) const override; 445 446 Init *convertInitializerBitRange(ArrayRef<unsigned> Bits) const override; 447 Init *convertInitListSlice(ArrayRef<unsigned> Elements) const override; 448 449 /// This method is used to implement the FieldInit class. 450 /// Implementors of this method should return the type of the named field if 451 /// they are of type record. 452 RecTy *getFieldType(StringInit *FieldName) const override; 453 }; 454 455 /// '?' - Represents an uninitialized value. 456 class UnsetInit : public Init { UnsetInit()457 UnsetInit() : Init(IK_UnsetInit) {} 458 459 public: 460 UnsetInit(const UnsetInit &) = delete; 461 UnsetInit &operator=(const UnsetInit &) = delete; 462 classof(const Init * I)463 static bool classof(const Init *I) { 464 return I->getKind() == IK_UnsetInit; 465 } 466 467 /// Get the singleton unset Init. 468 static UnsetInit *get(); 469 470 Init *getCastTo(RecTy *Ty) const override; 471 Init *convertInitializerTo(RecTy *Ty) const override; 472 getBit(unsigned Bit)473 Init *getBit(unsigned Bit) const override { 474 return const_cast<UnsetInit*>(this); 475 } 476 477 /// Is this a complete value with no unset (uninitialized) subvalues? isComplete()478 bool isComplete() const override { return false; } 479 isConcrete()480 bool isConcrete() const override { return true; } 481 482 /// Get the string representation of the Init. getAsString()483 std::string getAsString() const override { return "?"; } 484 }; 485 486 /// 'true'/'false' - Represent a concrete initializer for a bit. 487 class BitInit final : public TypedInit { 488 bool Value; 489 BitInit(bool V)490 explicit BitInit(bool V) : TypedInit(IK_BitInit, BitRecTy::get()), Value(V) {} 491 492 public: 493 BitInit(const BitInit &) = delete; 494 BitInit &operator=(BitInit &) = delete; 495 classof(const Init * I)496 static bool classof(const Init *I) { 497 return I->getKind() == IK_BitInit; 498 } 499 500 static BitInit *get(bool V); 501 getValue()502 bool getValue() const { return Value; } 503 504 Init *convertInitializerTo(RecTy *Ty) const override; 505 getBit(unsigned Bit)506 Init *getBit(unsigned Bit) const override { 507 assert(Bit < 1 && "Bit index out of range!"); 508 return const_cast<BitInit*>(this); 509 } 510 isConcrete()511 bool isConcrete() const override { return true; } getAsString()512 std::string getAsString() const override { return Value ? "1" : "0"; } 513 }; 514 515 /// '{ a, b, c }' - Represents an initializer for a BitsRecTy value. 516 /// It contains a vector of bits, whose size is determined by the type. 517 class BitsInit final : public TypedInit, public FoldingSetNode, 518 public TrailingObjects<BitsInit, Init *> { 519 unsigned NumBits; 520 BitsInit(unsigned N)521 BitsInit(unsigned N) 522 : TypedInit(IK_BitsInit, BitsRecTy::get(N)), NumBits(N) {} 523 524 public: 525 BitsInit(const BitsInit &) = delete; 526 BitsInit &operator=(const BitsInit &) = delete; 527 528 // Do not use sized deallocation due to trailing objects. delete(void * p)529 void operator delete(void *p) { ::operator delete(p); } 530 classof(const Init * I)531 static bool classof(const Init *I) { 532 return I->getKind() == IK_BitsInit; 533 } 534 535 static BitsInit *get(ArrayRef<Init *> Range); 536 537 void Profile(FoldingSetNodeID &ID) const; 538 getNumBits()539 unsigned getNumBits() const { return NumBits; } 540 541 Init *convertInitializerTo(RecTy *Ty) const override; 542 Init *convertInitializerBitRange(ArrayRef<unsigned> Bits) const override; 543 isComplete()544 bool isComplete() const override { 545 for (unsigned i = 0; i != getNumBits(); ++i) 546 if (!getBit(i)->isComplete()) return false; 547 return true; 548 } 549 allInComplete()550 bool allInComplete() const { 551 for (unsigned i = 0; i != getNumBits(); ++i) 552 if (getBit(i)->isComplete()) return false; 553 return true; 554 } 555 556 bool isConcrete() const override; 557 std::string getAsString() const override; 558 559 Init *resolveReferences(Resolver &R) const override; 560 getBit(unsigned Bit)561 Init *getBit(unsigned Bit) const override { 562 assert(Bit < NumBits && "Bit index out of range!"); 563 return getTrailingObjects<Init *>()[Bit]; 564 } 565 }; 566 567 /// '7' - Represent an initialization by a literal integer value. 568 class IntInit : public TypedInit { 569 int64_t Value; 570 IntInit(int64_t V)571 explicit IntInit(int64_t V) 572 : TypedInit(IK_IntInit, IntRecTy::get()), Value(V) {} 573 574 public: 575 IntInit(const IntInit &) = delete; 576 IntInit &operator=(const IntInit &) = delete; 577 classof(const Init * I)578 static bool classof(const Init *I) { 579 return I->getKind() == IK_IntInit; 580 } 581 582 static IntInit *get(int64_t V); 583 getValue()584 int64_t getValue() const { return Value; } 585 586 Init *convertInitializerTo(RecTy *Ty) const override; 587 Init *convertInitializerBitRange(ArrayRef<unsigned> Bits) const override; 588 isConcrete()589 bool isConcrete() const override { return true; } 590 std::string getAsString() const override; 591 getBit(unsigned Bit)592 Init *getBit(unsigned Bit) const override { 593 return BitInit::get((Value & (1ULL << Bit)) != 0); 594 } 595 }; 596 597 /// "foo" - Represent an initialization by a string value. 598 class StringInit : public TypedInit { 599 StringRef Value; 600 StringInit(StringRef V)601 explicit StringInit(StringRef V) 602 : TypedInit(IK_StringInit, StringRecTy::get()), Value(V) {} 603 604 public: 605 StringInit(const StringInit &) = delete; 606 StringInit &operator=(const StringInit &) = delete; 607 classof(const Init * I)608 static bool classof(const Init *I) { 609 return I->getKind() == IK_StringInit; 610 } 611 612 static StringInit *get(StringRef); 613 getValue()614 StringRef getValue() const { return Value; } 615 616 Init *convertInitializerTo(RecTy *Ty) const override; 617 isConcrete()618 bool isConcrete() const override { return true; } getAsString()619 std::string getAsString() const override { return "\"" + Value.str() + "\""; } 620 getAsUnquotedString()621 std::string getAsUnquotedString() const override { 622 return std::string(Value); 623 } 624 getBit(unsigned Bit)625 Init *getBit(unsigned Bit) const override { 626 llvm_unreachable("Illegal bit reference off string"); 627 } 628 }; 629 630 class CodeInit : public TypedInit { 631 StringRef Value; 632 SMLoc Loc; 633 CodeInit(StringRef V,const SMLoc & Loc)634 explicit CodeInit(StringRef V, const SMLoc &Loc) 635 : TypedInit(IK_CodeInit, static_cast<RecTy *>(CodeRecTy::get())), 636 Value(V), Loc(Loc) {} 637 638 public: 639 CodeInit(const StringInit &) = delete; 640 CodeInit &operator=(const StringInit &) = delete; 641 classof(const Init * I)642 static bool classof(const Init *I) { 643 return I->getKind() == IK_CodeInit; 644 } 645 646 static CodeInit *get(StringRef, const SMLoc &Loc); 647 getValue()648 StringRef getValue() const { return Value; } getLoc()649 const SMLoc &getLoc() const { return Loc; } 650 651 Init *convertInitializerTo(RecTy *Ty) const override; 652 isConcrete()653 bool isConcrete() const override { return true; } getAsString()654 std::string getAsString() const override { 655 return "[{" + Value.str() + "}]"; 656 } 657 getAsUnquotedString()658 std::string getAsUnquotedString() const override { 659 return std::string(Value); 660 } 661 getBit(unsigned Bit)662 Init *getBit(unsigned Bit) const override { 663 llvm_unreachable("Illegal bit reference off string"); 664 } 665 }; 666 667 /// [AL, AH, CL] - Represent a list of defs 668 /// 669 class ListInit final : public TypedInit, public FoldingSetNode, 670 public TrailingObjects<ListInit, Init *> { 671 unsigned NumValues; 672 673 public: 674 using const_iterator = Init *const *; 675 676 private: ListInit(unsigned N,RecTy * EltTy)677 explicit ListInit(unsigned N, RecTy *EltTy) 678 : TypedInit(IK_ListInit, ListRecTy::get(EltTy)), NumValues(N) {} 679 680 public: 681 ListInit(const ListInit &) = delete; 682 ListInit &operator=(const ListInit &) = delete; 683 684 // Do not use sized deallocation due to trailing objects. delete(void * p)685 void operator delete(void *p) { ::operator delete(p); } 686 classof(const Init * I)687 static bool classof(const Init *I) { 688 return I->getKind() == IK_ListInit; 689 } 690 static ListInit *get(ArrayRef<Init *> Range, RecTy *EltTy); 691 692 void Profile(FoldingSetNodeID &ID) const; 693 getElement(unsigned i)694 Init *getElement(unsigned i) const { 695 assert(i < NumValues && "List element index out of range!"); 696 return getTrailingObjects<Init *>()[i]; 697 } getElementType()698 RecTy *getElementType() const { 699 return cast<ListRecTy>(getType())->getElementType(); 700 } 701 702 Record *getElementAsRecord(unsigned i) const; 703 704 Init *convertInitListSlice(ArrayRef<unsigned> Elements) const override; 705 706 Init *convertInitializerTo(RecTy *Ty) const override; 707 708 /// This method is used by classes that refer to other 709 /// variables which may not be defined at the time they expression is formed. 710 /// If a value is set for the variable later, this method will be called on 711 /// users of the value to allow the value to propagate out. 712 /// 713 Init *resolveReferences(Resolver &R) const override; 714 715 bool isConcrete() const override; 716 std::string getAsString() const override; 717 getValues()718 ArrayRef<Init*> getValues() const { 719 return makeArrayRef(getTrailingObjects<Init *>(), NumValues); 720 } 721 begin()722 const_iterator begin() const { return getTrailingObjects<Init *>(); } end()723 const_iterator end () const { return begin() + NumValues; } 724 size()725 size_t size () const { return NumValues; } empty()726 bool empty() const { return NumValues == 0; } 727 getBit(unsigned Bit)728 Init *getBit(unsigned Bit) const override { 729 llvm_unreachable("Illegal bit reference off list"); 730 } 731 }; 732 733 /// Base class for operators 734 /// 735 class OpInit : public TypedInit { 736 protected: OpInit(InitKind K,RecTy * Type,uint8_t Opc)737 explicit OpInit(InitKind K, RecTy *Type, uint8_t Opc) 738 : TypedInit(K, Type, Opc) {} 739 740 public: 741 OpInit(const OpInit &) = delete; 742 OpInit &operator=(OpInit &) = delete; 743 classof(const Init * I)744 static bool classof(const Init *I) { 745 return I->getKind() >= IK_FirstOpInit && 746 I->getKind() <= IK_LastOpInit; 747 } 748 749 // Clone - Clone this operator, replacing arguments with the new list 750 virtual OpInit *clone(ArrayRef<Init *> Operands) const = 0; 751 752 virtual unsigned getNumOperands() const = 0; 753 virtual Init *getOperand(unsigned i) const = 0; 754 755 Init *getBit(unsigned Bit) const override; 756 }; 757 758 /// !op (X) - Transform an init. 759 /// 760 class UnOpInit : public OpInit, public FoldingSetNode { 761 public: 762 enum UnaryOp : uint8_t { CAST, NOT, HEAD, TAIL, SIZE, EMPTY, GETDAGOP }; 763 764 private: 765 Init *LHS; 766 UnOpInit(UnaryOp opc,Init * lhs,RecTy * Type)767 UnOpInit(UnaryOp opc, Init *lhs, RecTy *Type) 768 : OpInit(IK_UnOpInit, Type, opc), LHS(lhs) {} 769 770 public: 771 UnOpInit(const UnOpInit &) = delete; 772 UnOpInit &operator=(const UnOpInit &) = delete; 773 classof(const Init * I)774 static bool classof(const Init *I) { 775 return I->getKind() == IK_UnOpInit; 776 } 777 778 static UnOpInit *get(UnaryOp opc, Init *lhs, RecTy *Type); 779 780 void Profile(FoldingSetNodeID &ID) const; 781 782 // Clone - Clone this operator, replacing arguments with the new list clone(ArrayRef<Init * > Operands)783 OpInit *clone(ArrayRef<Init *> Operands) const override { 784 assert(Operands.size() == 1 && 785 "Wrong number of operands for unary operation"); 786 return UnOpInit::get(getOpcode(), *Operands.begin(), getType()); 787 } 788 getNumOperands()789 unsigned getNumOperands() const override { return 1; } 790 getOperand(unsigned i)791 Init *getOperand(unsigned i) const override { 792 assert(i == 0 && "Invalid operand id for unary operator"); 793 return getOperand(); 794 } 795 getOpcode()796 UnaryOp getOpcode() const { return (UnaryOp)Opc; } getOperand()797 Init *getOperand() const { return LHS; } 798 799 // Fold - If possible, fold this to a simpler init. Return this if not 800 // possible to fold. 801 Init *Fold(Record *CurRec, bool IsFinal = false) const; 802 803 Init *resolveReferences(Resolver &R) const override; 804 805 std::string getAsString() const override; 806 }; 807 808 /// !op (X, Y) - Combine two inits. 809 class BinOpInit : public OpInit, public FoldingSetNode { 810 public: 811 enum BinaryOp : uint8_t { ADD, SUB, MUL, AND, OR, XOR, SHL, SRA, SRL, LISTCONCAT, 812 LISTSPLAT, STRCONCAT, INTERLEAVE, CONCAT, EQ, 813 NE, LE, LT, GE, GT, SETDAGOP }; 814 815 private: 816 Init *LHS, *RHS; 817 BinOpInit(BinaryOp opc,Init * lhs,Init * rhs,RecTy * Type)818 BinOpInit(BinaryOp opc, Init *lhs, Init *rhs, RecTy *Type) : 819 OpInit(IK_BinOpInit, Type, opc), LHS(lhs), RHS(rhs) {} 820 821 public: 822 BinOpInit(const BinOpInit &) = delete; 823 BinOpInit &operator=(const BinOpInit &) = delete; 824 classof(const Init * I)825 static bool classof(const Init *I) { 826 return I->getKind() == IK_BinOpInit; 827 } 828 829 static BinOpInit *get(BinaryOp opc, Init *lhs, Init *rhs, 830 RecTy *Type); 831 static Init *getStrConcat(Init *lhs, Init *rhs); 832 static Init *getListConcat(TypedInit *lhs, Init *rhs); 833 834 void Profile(FoldingSetNodeID &ID) const; 835 836 // Clone - Clone this operator, replacing arguments with the new list clone(ArrayRef<Init * > Operands)837 OpInit *clone(ArrayRef<Init *> Operands) const override { 838 assert(Operands.size() == 2 && 839 "Wrong number of operands for binary operation"); 840 return BinOpInit::get(getOpcode(), Operands[0], Operands[1], getType()); 841 } 842 getNumOperands()843 unsigned getNumOperands() const override { return 2; } getOperand(unsigned i)844 Init *getOperand(unsigned i) const override { 845 switch (i) { 846 default: llvm_unreachable("Invalid operand id for binary operator"); 847 case 0: return getLHS(); 848 case 1: return getRHS(); 849 } 850 } 851 getOpcode()852 BinaryOp getOpcode() const { return (BinaryOp)Opc; } getLHS()853 Init *getLHS() const { return LHS; } getRHS()854 Init *getRHS() const { return RHS; } 855 856 // Fold - If possible, fold this to a simpler init. Return this if not 857 // possible to fold. 858 Init *Fold(Record *CurRec) const; 859 860 Init *resolveReferences(Resolver &R) const override; 861 862 std::string getAsString() const override; 863 }; 864 865 /// !op (X, Y, Z) - Combine two inits. 866 class TernOpInit : public OpInit, public FoldingSetNode { 867 public: 868 enum TernaryOp : uint8_t { SUBST, FOREACH, FILTER, IF, DAG }; 869 870 private: 871 Init *LHS, *MHS, *RHS; 872 TernOpInit(TernaryOp opc,Init * lhs,Init * mhs,Init * rhs,RecTy * Type)873 TernOpInit(TernaryOp opc, Init *lhs, Init *mhs, Init *rhs, 874 RecTy *Type) : 875 OpInit(IK_TernOpInit, Type, opc), LHS(lhs), MHS(mhs), RHS(rhs) {} 876 877 public: 878 TernOpInit(const TernOpInit &) = delete; 879 TernOpInit &operator=(const TernOpInit &) = delete; 880 classof(const Init * I)881 static bool classof(const Init *I) { 882 return I->getKind() == IK_TernOpInit; 883 } 884 885 static TernOpInit *get(TernaryOp opc, Init *lhs, 886 Init *mhs, Init *rhs, 887 RecTy *Type); 888 889 void Profile(FoldingSetNodeID &ID) const; 890 891 // Clone - Clone this operator, replacing arguments with the new list clone(ArrayRef<Init * > Operands)892 OpInit *clone(ArrayRef<Init *> Operands) const override { 893 assert(Operands.size() == 3 && 894 "Wrong number of operands for ternary operation"); 895 return TernOpInit::get(getOpcode(), Operands[0], Operands[1], Operands[2], 896 getType()); 897 } 898 getNumOperands()899 unsigned getNumOperands() const override { return 3; } getOperand(unsigned i)900 Init *getOperand(unsigned i) const override { 901 switch (i) { 902 default: llvm_unreachable("Invalid operand id for ternary operator"); 903 case 0: return getLHS(); 904 case 1: return getMHS(); 905 case 2: return getRHS(); 906 } 907 } 908 getOpcode()909 TernaryOp getOpcode() const { return (TernaryOp)Opc; } getLHS()910 Init *getLHS() const { return LHS; } getMHS()911 Init *getMHS() const { return MHS; } getRHS()912 Init *getRHS() const { return RHS; } 913 914 // Fold - If possible, fold this to a simpler init. Return this if not 915 // possible to fold. 916 Init *Fold(Record *CurRec) const; 917 isComplete()918 bool isComplete() const override { 919 return LHS->isComplete() && MHS->isComplete() && RHS->isComplete(); 920 } 921 922 Init *resolveReferences(Resolver &R) const override; 923 924 std::string getAsString() const override; 925 }; 926 927 /// !cond(condition_1: value1, ... , condition_n: value) 928 /// Selects the first value for which condition is true. 929 /// Otherwise reports an error. 930 class CondOpInit final : public TypedInit, public FoldingSetNode, 931 public TrailingObjects<CondOpInit, Init *> { 932 unsigned NumConds; 933 RecTy *ValType; 934 CondOpInit(unsigned NC,RecTy * Type)935 CondOpInit(unsigned NC, RecTy *Type) 936 : TypedInit(IK_CondOpInit, Type), 937 NumConds(NC), ValType(Type) {} 938 numTrailingObjects(OverloadToken<Init * >)939 size_t numTrailingObjects(OverloadToken<Init *>) const { 940 return 2*NumConds; 941 } 942 943 public: 944 CondOpInit(const CondOpInit &) = delete; 945 CondOpInit &operator=(const CondOpInit &) = delete; 946 classof(const Init * I)947 static bool classof(const Init *I) { 948 return I->getKind() == IK_CondOpInit; 949 } 950 951 static CondOpInit *get(ArrayRef<Init*> C, ArrayRef<Init*> V, 952 RecTy *Type); 953 954 void Profile(FoldingSetNodeID &ID) const; 955 getValType()956 RecTy *getValType() const { return ValType; } 957 getNumConds()958 unsigned getNumConds() const { return NumConds; } 959 getCond(unsigned Num)960 Init *getCond(unsigned Num) const { 961 assert(Num < NumConds && "Condition number out of range!"); 962 return getTrailingObjects<Init *>()[Num]; 963 } 964 getVal(unsigned Num)965 Init *getVal(unsigned Num) const { 966 assert(Num < NumConds && "Val number out of range!"); 967 return getTrailingObjects<Init *>()[Num+NumConds]; 968 } 969 getConds()970 ArrayRef<Init *> getConds() const { 971 return makeArrayRef(getTrailingObjects<Init *>(), NumConds); 972 } 973 getVals()974 ArrayRef<Init *> getVals() const { 975 return makeArrayRef(getTrailingObjects<Init *>()+NumConds, NumConds); 976 } 977 978 Init *Fold(Record *CurRec) const; 979 980 Init *resolveReferences(Resolver &R) const override; 981 982 bool isConcrete() const override; 983 bool isComplete() const override; 984 std::string getAsString() const override; 985 986 using const_case_iterator = SmallVectorImpl<Init*>::const_iterator; 987 using const_val_iterator = SmallVectorImpl<Init*>::const_iterator; 988 arg_begin()989 inline const_case_iterator arg_begin() const { return getConds().begin(); } arg_end()990 inline const_case_iterator arg_end () const { return getConds().end(); } 991 case_size()992 inline size_t case_size () const { return NumConds; } case_empty()993 inline bool case_empty() const { return NumConds == 0; } 994 name_begin()995 inline const_val_iterator name_begin() const { return getVals().begin();} name_end()996 inline const_val_iterator name_end () const { return getVals().end(); } 997 val_size()998 inline size_t val_size () const { return NumConds; } val_empty()999 inline bool val_empty() const { return NumConds == 0; } 1000 1001 Init *getBit(unsigned Bit) const override; 1002 }; 1003 1004 /// !foldl (a, b, expr, start, lst) - Fold over a list. 1005 class FoldOpInit : public TypedInit, public FoldingSetNode { 1006 private: 1007 Init *Start; 1008 Init *List; 1009 Init *A; 1010 Init *B; 1011 Init *Expr; 1012 FoldOpInit(Init * Start,Init * List,Init * A,Init * B,Init * Expr,RecTy * Type)1013 FoldOpInit(Init *Start, Init *List, Init *A, Init *B, Init *Expr, RecTy *Type) 1014 : TypedInit(IK_FoldOpInit, Type), Start(Start), List(List), A(A), B(B), 1015 Expr(Expr) {} 1016 1017 public: 1018 FoldOpInit(const FoldOpInit &) = delete; 1019 FoldOpInit &operator=(const FoldOpInit &) = delete; 1020 classof(const Init * I)1021 static bool classof(const Init *I) { return I->getKind() == IK_FoldOpInit; } 1022 1023 static FoldOpInit *get(Init *Start, Init *List, Init *A, Init *B, Init *Expr, 1024 RecTy *Type); 1025 1026 void Profile(FoldingSetNodeID &ID) const; 1027 1028 // Fold - If possible, fold this to a simpler init. Return this if not 1029 // possible to fold. 1030 Init *Fold(Record *CurRec) const; 1031 isComplete()1032 bool isComplete() const override { return false; } 1033 1034 Init *resolveReferences(Resolver &R) const override; 1035 1036 Init *getBit(unsigned Bit) const override; 1037 1038 std::string getAsString() const override; 1039 }; 1040 1041 /// !isa<type>(expr) - Dynamically determine the type of an expression. 1042 class IsAOpInit : public TypedInit, public FoldingSetNode { 1043 private: 1044 RecTy *CheckType; 1045 Init *Expr; 1046 IsAOpInit(RecTy * CheckType,Init * Expr)1047 IsAOpInit(RecTy *CheckType, Init *Expr) 1048 : TypedInit(IK_IsAOpInit, IntRecTy::get()), CheckType(CheckType), 1049 Expr(Expr) {} 1050 1051 public: 1052 IsAOpInit(const IsAOpInit &) = delete; 1053 IsAOpInit &operator=(const IsAOpInit &) = delete; 1054 classof(const Init * I)1055 static bool classof(const Init *I) { return I->getKind() == IK_IsAOpInit; } 1056 1057 static IsAOpInit *get(RecTy *CheckType, Init *Expr); 1058 1059 void Profile(FoldingSetNodeID &ID) const; 1060 1061 // Fold - If possible, fold this to a simpler init. Return this if not 1062 // possible to fold. 1063 Init *Fold() const; 1064 isComplete()1065 bool isComplete() const override { return false; } 1066 1067 Init *resolveReferences(Resolver &R) const override; 1068 1069 Init *getBit(unsigned Bit) const override; 1070 1071 std::string getAsString() const override; 1072 }; 1073 1074 /// 'Opcode' - Represent a reference to an entire variable object. 1075 class VarInit : public TypedInit { 1076 Init *VarName; 1077 VarInit(Init * VN,RecTy * T)1078 explicit VarInit(Init *VN, RecTy *T) 1079 : TypedInit(IK_VarInit, T), VarName(VN) {} 1080 1081 public: 1082 VarInit(const VarInit &) = delete; 1083 VarInit &operator=(const VarInit &) = delete; 1084 classof(const Init * I)1085 static bool classof(const Init *I) { 1086 return I->getKind() == IK_VarInit; 1087 } 1088 1089 static VarInit *get(StringRef VN, RecTy *T); 1090 static VarInit *get(Init *VN, RecTy *T); 1091 1092 StringRef getName() const; getNameInit()1093 Init *getNameInit() const { return VarName; } 1094 getNameInitAsString()1095 std::string getNameInitAsString() const { 1096 return getNameInit()->getAsUnquotedString(); 1097 } 1098 1099 /// This method is used by classes that refer to other 1100 /// variables which may not be defined at the time they expression is formed. 1101 /// If a value is set for the variable later, this method will be called on 1102 /// users of the value to allow the value to propagate out. 1103 /// 1104 Init *resolveReferences(Resolver &R) const override; 1105 1106 Init *getBit(unsigned Bit) const override; 1107 getAsString()1108 std::string getAsString() const override { return std::string(getName()); } 1109 }; 1110 1111 /// Opcode{0} - Represent access to one bit of a variable or field. 1112 class VarBitInit final : public TypedInit { 1113 TypedInit *TI; 1114 unsigned Bit; 1115 VarBitInit(TypedInit * T,unsigned B)1116 VarBitInit(TypedInit *T, unsigned B) 1117 : TypedInit(IK_VarBitInit, BitRecTy::get()), TI(T), Bit(B) { 1118 assert(T->getType() && 1119 (isa<IntRecTy>(T->getType()) || 1120 (isa<BitsRecTy>(T->getType()) && 1121 cast<BitsRecTy>(T->getType())->getNumBits() > B)) && 1122 "Illegal VarBitInit expression!"); 1123 } 1124 1125 public: 1126 VarBitInit(const VarBitInit &) = delete; 1127 VarBitInit &operator=(const VarBitInit &) = delete; 1128 classof(const Init * I)1129 static bool classof(const Init *I) { 1130 return I->getKind() == IK_VarBitInit; 1131 } 1132 1133 static VarBitInit *get(TypedInit *T, unsigned B); 1134 getBitVar()1135 Init *getBitVar() const { return TI; } getBitNum()1136 unsigned getBitNum() const { return Bit; } 1137 1138 std::string getAsString() const override; 1139 Init *resolveReferences(Resolver &R) const override; 1140 getBit(unsigned B)1141 Init *getBit(unsigned B) const override { 1142 assert(B < 1 && "Bit index out of range!"); 1143 return const_cast<VarBitInit*>(this); 1144 } 1145 }; 1146 1147 /// List[4] - Represent access to one element of a var or 1148 /// field. 1149 class VarListElementInit : public TypedInit { 1150 TypedInit *TI; 1151 unsigned Element; 1152 VarListElementInit(TypedInit * T,unsigned E)1153 VarListElementInit(TypedInit *T, unsigned E) 1154 : TypedInit(IK_VarListElementInit, 1155 cast<ListRecTy>(T->getType())->getElementType()), 1156 TI(T), Element(E) { 1157 assert(T->getType() && isa<ListRecTy>(T->getType()) && 1158 "Illegal VarBitInit expression!"); 1159 } 1160 1161 public: 1162 VarListElementInit(const VarListElementInit &) = delete; 1163 VarListElementInit &operator=(const VarListElementInit &) = delete; 1164 classof(const Init * I)1165 static bool classof(const Init *I) { 1166 return I->getKind() == IK_VarListElementInit; 1167 } 1168 1169 static VarListElementInit *get(TypedInit *T, unsigned E); 1170 getVariable()1171 TypedInit *getVariable() const { return TI; } getElementNum()1172 unsigned getElementNum() const { return Element; } 1173 1174 std::string getAsString() const override; 1175 Init *resolveReferences(Resolver &R) const override; 1176 1177 Init *getBit(unsigned Bit) const override; 1178 }; 1179 1180 /// AL - Represent a reference to a 'def' in the description 1181 class DefInit : public TypedInit { 1182 friend class Record; 1183 1184 Record *Def; 1185 1186 explicit DefInit(Record *D); 1187 1188 public: 1189 DefInit(const DefInit &) = delete; 1190 DefInit &operator=(const DefInit &) = delete; 1191 classof(const Init * I)1192 static bool classof(const Init *I) { 1193 return I->getKind() == IK_DefInit; 1194 } 1195 1196 static DefInit *get(Record*); 1197 1198 Init *convertInitializerTo(RecTy *Ty) const override; 1199 getDef()1200 Record *getDef() const { return Def; } 1201 1202 //virtual Init *convertInitializerBitRange(ArrayRef<unsigned> Bits); 1203 1204 RecTy *getFieldType(StringInit *FieldName) const override; 1205 isConcrete()1206 bool isConcrete() const override { return true; } 1207 std::string getAsString() const override; 1208 getBit(unsigned Bit)1209 Init *getBit(unsigned Bit) const override { 1210 llvm_unreachable("Illegal bit reference off def"); 1211 } 1212 }; 1213 1214 /// classname<targs...> - Represent an uninstantiated anonymous class 1215 /// instantiation. 1216 class VarDefInit final : public TypedInit, public FoldingSetNode, 1217 public TrailingObjects<VarDefInit, Init *> { 1218 Record *Class; 1219 DefInit *Def = nullptr; // after instantiation 1220 unsigned NumArgs; 1221 VarDefInit(Record * Class,unsigned N)1222 explicit VarDefInit(Record *Class, unsigned N) 1223 : TypedInit(IK_VarDefInit, RecordRecTy::get(Class)), Class(Class), NumArgs(N) {} 1224 1225 DefInit *instantiate(); 1226 1227 public: 1228 VarDefInit(const VarDefInit &) = delete; 1229 VarDefInit &operator=(const VarDefInit &) = delete; 1230 1231 // Do not use sized deallocation due to trailing objects. delete(void * p)1232 void operator delete(void *p) { ::operator delete(p); } 1233 classof(const Init * I)1234 static bool classof(const Init *I) { 1235 return I->getKind() == IK_VarDefInit; 1236 } 1237 static VarDefInit *get(Record *Class, ArrayRef<Init *> Args); 1238 1239 void Profile(FoldingSetNodeID &ID) const; 1240 1241 Init *resolveReferences(Resolver &R) const override; 1242 Init *Fold() const; 1243 1244 std::string getAsString() const override; 1245 getArg(unsigned i)1246 Init *getArg(unsigned i) const { 1247 assert(i < NumArgs && "Argument index out of range!"); 1248 return getTrailingObjects<Init *>()[i]; 1249 } 1250 1251 using const_iterator = Init *const *; 1252 args_begin()1253 const_iterator args_begin() const { return getTrailingObjects<Init *>(); } args_end()1254 const_iterator args_end () const { return args_begin() + NumArgs; } 1255 args_size()1256 size_t args_size () const { return NumArgs; } args_empty()1257 bool args_empty() const { return NumArgs == 0; } 1258 args()1259 ArrayRef<Init *> args() const { return makeArrayRef(args_begin(), NumArgs); } 1260 getBit(unsigned Bit)1261 Init *getBit(unsigned Bit) const override { 1262 llvm_unreachable("Illegal bit reference off anonymous def"); 1263 } 1264 }; 1265 1266 /// X.Y - Represent a reference to a subfield of a variable 1267 class FieldInit : public TypedInit { 1268 Init *Rec; // Record we are referring to 1269 StringInit *FieldName; // Field we are accessing 1270 FieldInit(Init * R,StringInit * FN)1271 FieldInit(Init *R, StringInit *FN) 1272 : TypedInit(IK_FieldInit, R->getFieldType(FN)), Rec(R), FieldName(FN) { 1273 #ifndef NDEBUG 1274 if (!getType()) { 1275 llvm::errs() << "In Record = " << Rec->getAsString() 1276 << ", got FieldName = " << *FieldName 1277 << " with non-record type!\n"; 1278 llvm_unreachable("FieldInit with non-record type!"); 1279 } 1280 #endif 1281 } 1282 1283 public: 1284 FieldInit(const FieldInit &) = delete; 1285 FieldInit &operator=(const FieldInit &) = delete; 1286 classof(const Init * I)1287 static bool classof(const Init *I) { 1288 return I->getKind() == IK_FieldInit; 1289 } 1290 1291 static FieldInit *get(Init *R, StringInit *FN); 1292 getRecord()1293 Init *getRecord() const { return Rec; } getFieldName()1294 StringInit *getFieldName() const { return FieldName; } 1295 1296 Init *getBit(unsigned Bit) const override; 1297 1298 Init *resolveReferences(Resolver &R) const override; 1299 Init *Fold(Record *CurRec) const; 1300 1301 bool isConcrete() const override; getAsString()1302 std::string getAsString() const override { 1303 return Rec->getAsString() + "." + FieldName->getValue().str(); 1304 } 1305 }; 1306 1307 /// (v a, b) - Represent a DAG tree value. DAG inits are required 1308 /// to have at least one value then a (possibly empty) list of arguments. Each 1309 /// argument can have a name associated with it. 1310 class DagInit final : public TypedInit, public FoldingSetNode, 1311 public TrailingObjects<DagInit, Init *, StringInit *> { 1312 friend TrailingObjects; 1313 1314 Init *Val; 1315 StringInit *ValName; 1316 unsigned NumArgs; 1317 unsigned NumArgNames; 1318 DagInit(Init * V,StringInit * VN,unsigned NumArgs,unsigned NumArgNames)1319 DagInit(Init *V, StringInit *VN, unsigned NumArgs, unsigned NumArgNames) 1320 : TypedInit(IK_DagInit, DagRecTy::get()), Val(V), ValName(VN), 1321 NumArgs(NumArgs), NumArgNames(NumArgNames) {} 1322 numTrailingObjects(OverloadToken<Init * >)1323 size_t numTrailingObjects(OverloadToken<Init *>) const { return NumArgs; } 1324 1325 public: 1326 DagInit(const DagInit &) = delete; 1327 DagInit &operator=(const DagInit &) = delete; 1328 classof(const Init * I)1329 static bool classof(const Init *I) { 1330 return I->getKind() == IK_DagInit; 1331 } 1332 1333 static DagInit *get(Init *V, StringInit *VN, ArrayRef<Init *> ArgRange, 1334 ArrayRef<StringInit*> NameRange); 1335 static DagInit *get(Init *V, StringInit *VN, 1336 ArrayRef<std::pair<Init*, StringInit*>> Args); 1337 1338 void Profile(FoldingSetNodeID &ID) const; 1339 getOperator()1340 Init *getOperator() const { return Val; } 1341 Record *getOperatorAsDef(ArrayRef<SMLoc> Loc) const; 1342 getName()1343 StringInit *getName() const { return ValName; } 1344 getNameStr()1345 StringRef getNameStr() const { 1346 return ValName ? ValName->getValue() : StringRef(); 1347 } 1348 getNumArgs()1349 unsigned getNumArgs() const { return NumArgs; } 1350 getArg(unsigned Num)1351 Init *getArg(unsigned Num) const { 1352 assert(Num < NumArgs && "Arg number out of range!"); 1353 return getTrailingObjects<Init *>()[Num]; 1354 } 1355 getArgName(unsigned Num)1356 StringInit *getArgName(unsigned Num) const { 1357 assert(Num < NumArgNames && "Arg number out of range!"); 1358 return getTrailingObjects<StringInit *>()[Num]; 1359 } 1360 getArgNameStr(unsigned Num)1361 StringRef getArgNameStr(unsigned Num) const { 1362 StringInit *Init = getArgName(Num); 1363 return Init ? Init->getValue() : StringRef(); 1364 } 1365 getArgs()1366 ArrayRef<Init *> getArgs() const { 1367 return makeArrayRef(getTrailingObjects<Init *>(), NumArgs); 1368 } 1369 getArgNames()1370 ArrayRef<StringInit *> getArgNames() const { 1371 return makeArrayRef(getTrailingObjects<StringInit *>(), NumArgNames); 1372 } 1373 1374 Init *resolveReferences(Resolver &R) const override; 1375 1376 bool isConcrete() const override; 1377 std::string getAsString() const override; 1378 1379 using const_arg_iterator = SmallVectorImpl<Init*>::const_iterator; 1380 using const_name_iterator = SmallVectorImpl<StringInit*>::const_iterator; 1381 arg_begin()1382 inline const_arg_iterator arg_begin() const { return getArgs().begin(); } arg_end()1383 inline const_arg_iterator arg_end () const { return getArgs().end(); } 1384 arg_size()1385 inline size_t arg_size () const { return NumArgs; } arg_empty()1386 inline bool arg_empty() const { return NumArgs == 0; } 1387 name_begin()1388 inline const_name_iterator name_begin() const { return getArgNames().begin();} name_end()1389 inline const_name_iterator name_end () const { return getArgNames().end(); } 1390 name_size()1391 inline size_t name_size () const { return NumArgNames; } name_empty()1392 inline bool name_empty() const { return NumArgNames == 0; } 1393 getBit(unsigned Bit)1394 Init *getBit(unsigned Bit) const override { 1395 llvm_unreachable("Illegal bit reference off dag"); 1396 } 1397 }; 1398 1399 //===----------------------------------------------------------------------===// 1400 // High-Level Classes 1401 //===----------------------------------------------------------------------===// 1402 1403 /// This class represents a field in a record, including its name, type, 1404 /// value, and source location. 1405 class RecordVal { 1406 friend class Record; 1407 1408 Init *Name; 1409 SMLoc Loc; // Source location of definition of name. 1410 PointerIntPair<RecTy *, 1, bool> TyAndPrefix; 1411 Init *Value; 1412 1413 public: 1414 RecordVal(Init *N, RecTy *T, bool P); 1415 RecordVal(Init *N, SMLoc Loc, RecTy *T, bool P); 1416 1417 /// Get the name of the field as a StringRef. 1418 StringRef getName() const; 1419 1420 /// Get the name of the field as an Init. getNameInit()1421 Init *getNameInit() const { return Name; } 1422 1423 /// Get the name of the field as a std::string. getNameInitAsString()1424 std::string getNameInitAsString() const { 1425 return getNameInit()->getAsUnquotedString(); 1426 } 1427 1428 /// Get the source location of the point where the field was defined. getLoc()1429 const SMLoc &getLoc() const { return Loc; } 1430 getPrefix()1431 bool getPrefix() const { return TyAndPrefix.getInt(); } 1432 1433 /// Get the type of the field value as a RecTy. getType()1434 RecTy *getType() const { return TyAndPrefix.getPointer(); } 1435 1436 /// Get the value of the field as an Init. getValue()1437 Init *getValue() const { return Value; } 1438 1439 /// Set the value of the field from an Init. 1440 bool setValue(Init *V); 1441 1442 /// Set the value and source location of the field. 1443 bool setValue(Init *V, SMLoc NewLoc); 1444 1445 void dump() const; 1446 1447 /// Print the value to an output stream, possibly with a semicolon. 1448 void print(raw_ostream &OS, bool PrintSem = true) const; 1449 }; 1450 1451 inline raw_ostream &operator<<(raw_ostream &OS, const RecordVal &RV) { 1452 RV.print(OS << " "); 1453 return OS; 1454 } 1455 1456 class Record { 1457 static unsigned LastID; 1458 1459 Init *Name; 1460 // Location where record was instantiated, followed by the location of 1461 // multiclass prototypes used. 1462 SmallVector<SMLoc, 4> Locs; 1463 SmallVector<Init *, 0> TemplateArgs; 1464 SmallVector<RecordVal, 0> Values; 1465 1466 // All superclasses in the inheritance forest in post-order (yes, it 1467 // must be a forest; diamond-shaped inheritance is not allowed). 1468 SmallVector<std::pair<Record *, SMRange>, 0> SuperClasses; 1469 1470 // Tracks Record instances. Not owned by Record. 1471 RecordKeeper &TrackedRecords; 1472 1473 // The DefInit corresponding to this record. 1474 DefInit *CorrespondingDefInit = nullptr; 1475 1476 // Unique record ID. 1477 unsigned ID; 1478 1479 bool IsAnonymous; 1480 bool IsClass; 1481 1482 void checkName(); 1483 1484 public: 1485 // Constructs a record. 1486 explicit Record(Init *N, ArrayRef<SMLoc> locs, RecordKeeper &records, 1487 bool Anonymous = false, bool Class = false) Name(N)1488 : Name(N), Locs(locs.begin(), locs.end()), TrackedRecords(records), 1489 ID(LastID++), IsAnonymous(Anonymous), IsClass(Class) { 1490 checkName(); 1491 } 1492 1493 explicit Record(StringRef N, ArrayRef<SMLoc> locs, RecordKeeper &records, 1494 bool Class = false) Record(StringInit::get (N),locs,records,false,Class)1495 : Record(StringInit::get(N), locs, records, false, Class) {} 1496 1497 // When copy-constructing a Record, we must still guarantee a globally unique 1498 // ID number. Don't copy CorrespondingDefInit either, since it's owned by the 1499 // original record. All other fields can be copied normally. Record(const Record & O)1500 Record(const Record &O) 1501 : Name(O.Name), Locs(O.Locs), TemplateArgs(O.TemplateArgs), 1502 Values(O.Values), SuperClasses(O.SuperClasses), 1503 TrackedRecords(O.TrackedRecords), ID(LastID++), 1504 IsAnonymous(O.IsAnonymous), IsClass(O.IsClass) { } 1505 getNewUID()1506 static unsigned getNewUID() { return LastID++; } 1507 getID()1508 unsigned getID() const { return ID; } 1509 getName()1510 StringRef getName() const { return cast<StringInit>(Name)->getValue(); } 1511 getNameInit()1512 Init *getNameInit() const { 1513 return Name; 1514 } 1515 getNameInitAsString()1516 const std::string getNameInitAsString() const { 1517 return getNameInit()->getAsUnquotedString(); 1518 } 1519 1520 void setName(Init *Name); // Also updates RecordKeeper. 1521 getLoc()1522 ArrayRef<SMLoc> getLoc() const { return Locs; } appendLoc(SMLoc Loc)1523 void appendLoc(SMLoc Loc) { Locs.push_back(Loc); } 1524 1525 // Make the type that this record should have based on its superclasses. 1526 RecordRecTy *getType(); 1527 1528 /// get the corresponding DefInit. 1529 DefInit *getDefInit(); 1530 isClass()1531 bool isClass() const { return IsClass; } 1532 getTemplateArgs()1533 ArrayRef<Init *> getTemplateArgs() const { 1534 return TemplateArgs; 1535 } 1536 getValues()1537 ArrayRef<RecordVal> getValues() const { return Values; } 1538 getSuperClasses()1539 ArrayRef<std::pair<Record *, SMRange>> getSuperClasses() const { 1540 return SuperClasses; 1541 } 1542 1543 /// Determine whether this record has the specified direct superclass. 1544 bool hasDirectSuperClass(const Record *SuperClass) const; 1545 1546 /// Append the direct superclasses of this record to Classes. 1547 void getDirectSuperClasses(SmallVectorImpl<Record *> &Classes) const; 1548 isTemplateArg(Init * Name)1549 bool isTemplateArg(Init *Name) const { 1550 for (Init *TA : TemplateArgs) 1551 if (TA == Name) return true; 1552 return false; 1553 } 1554 getValue(const Init * Name)1555 const RecordVal *getValue(const Init *Name) const { 1556 for (const RecordVal &Val : Values) 1557 if (Val.Name == Name) return &Val; 1558 return nullptr; 1559 } 1560 getValue(StringRef Name)1561 const RecordVal *getValue(StringRef Name) const { 1562 return getValue(StringInit::get(Name)); 1563 } 1564 getValue(const Init * Name)1565 RecordVal *getValue(const Init *Name) { 1566 return const_cast<RecordVal *>(static_cast<const Record *>(this)->getValue(Name)); 1567 } 1568 getValue(StringRef Name)1569 RecordVal *getValue(StringRef Name) { 1570 return const_cast<RecordVal *>(static_cast<const Record *>(this)->getValue(Name)); 1571 } 1572 addTemplateArg(Init * Name)1573 void addTemplateArg(Init *Name) { 1574 assert(!isTemplateArg(Name) && "Template arg already defined!"); 1575 TemplateArgs.push_back(Name); 1576 } 1577 addValue(const RecordVal & RV)1578 void addValue(const RecordVal &RV) { 1579 assert(getValue(RV.getNameInit()) == nullptr && "Value already added!"); 1580 Values.push_back(RV); 1581 } 1582 removeValue(Init * Name)1583 void removeValue(Init *Name) { 1584 for (unsigned i = 0, e = Values.size(); i != e; ++i) 1585 if (Values[i].getNameInit() == Name) { 1586 Values.erase(Values.begin()+i); 1587 return; 1588 } 1589 llvm_unreachable("Cannot remove an entry that does not exist!"); 1590 } 1591 removeValue(StringRef Name)1592 void removeValue(StringRef Name) { 1593 removeValue(StringInit::get(Name)); 1594 } 1595 isSubClassOf(const Record * R)1596 bool isSubClassOf(const Record *R) const { 1597 for (const auto &SCPair : SuperClasses) 1598 if (SCPair.first == R) 1599 return true; 1600 return false; 1601 } 1602 isSubClassOf(StringRef Name)1603 bool isSubClassOf(StringRef Name) const { 1604 for (const auto &SCPair : SuperClasses) { 1605 if (const auto *SI = dyn_cast<StringInit>(SCPair.first->getNameInit())) { 1606 if (SI->getValue() == Name) 1607 return true; 1608 } else if (SCPair.first->getNameInitAsString() == Name) { 1609 return true; 1610 } 1611 } 1612 return false; 1613 } 1614 addSuperClass(Record * R,SMRange Range)1615 void addSuperClass(Record *R, SMRange Range) { 1616 assert(!CorrespondingDefInit && 1617 "changing type of record after it has been referenced"); 1618 assert(!isSubClassOf(R) && "Already subclassing record!"); 1619 SuperClasses.push_back(std::make_pair(R, Range)); 1620 } 1621 1622 /// If there are any field references that refer to fields 1623 /// that have been filled in, we can propagate the values now. 1624 /// 1625 /// This is a final resolve: any error messages, e.g. due to undefined 1626 /// !cast references, are generated now. 1627 void resolveReferences(); 1628 1629 /// Apply the resolver to the name of the record as well as to the 1630 /// initializers of all fields of the record except SkipVal. 1631 /// 1632 /// The resolver should not resolve any of the fields itself, to avoid 1633 /// recursion / infinite loops. 1634 void resolveReferences(Resolver &R, const RecordVal *SkipVal = nullptr); 1635 getRecords()1636 RecordKeeper &getRecords() const { 1637 return TrackedRecords; 1638 } 1639 isAnonymous()1640 bool isAnonymous() const { 1641 return IsAnonymous; 1642 } 1643 1644 void print(raw_ostream &OS) const; 1645 void dump() const; 1646 1647 //===--------------------------------------------------------------------===// 1648 // High-level methods useful to tablegen back-ends 1649 // 1650 1651 /// Return the initializer for a value with the specified name, 1652 /// or throw an exception if the field does not exist. 1653 Init *getValueInit(StringRef FieldName) const; 1654 1655 /// Return true if the named field is unset. isValueUnset(StringRef FieldName)1656 bool isValueUnset(StringRef FieldName) const { 1657 return isa<UnsetInit>(getValueInit(FieldName)); 1658 } 1659 1660 /// This method looks up the specified field and returns 1661 /// its value as a string, throwing an exception if the field does not exist 1662 /// or if the value is not a string. 1663 StringRef getValueAsString(StringRef FieldName) const; 1664 1665 /// This method looks up the specified field and returns 1666 /// its value as a string, throwing an exception if the field if the value is 1667 /// not a string and llvm::Optional() if the field does not exist. 1668 llvm::Optional<StringRef> getValueAsOptionalString(StringRef FieldName) const; 1669 1670 /// This method looks up the specified field and returns 1671 /// its value as a string, throwing an exception if the field if the value is 1672 /// not a code block and llvm::Optional() if the field does not exist. 1673 llvm::Optional<StringRef> getValueAsOptionalCode(StringRef FieldName) const; 1674 1675 /// This method looks up the specified field and returns 1676 /// its value as a BitsInit, throwing an exception if the field does not exist 1677 /// or if the value is not the right type. 1678 BitsInit *getValueAsBitsInit(StringRef FieldName) const; 1679 1680 /// This method looks up the specified field and returns 1681 /// its value as a ListInit, throwing an exception if the field does not exist 1682 /// or if the value is not the right type. 1683 ListInit *getValueAsListInit(StringRef FieldName) const; 1684 1685 /// This method looks up the specified field and 1686 /// returns its value as a vector of records, throwing an exception if the 1687 /// field does not exist or if the value is not the right type. 1688 std::vector<Record*> getValueAsListOfDefs(StringRef FieldName) const; 1689 1690 /// This method looks up the specified field and 1691 /// returns its value as a vector of integers, throwing an exception if the 1692 /// field does not exist or if the value is not the right type. 1693 std::vector<int64_t> getValueAsListOfInts(StringRef FieldName) const; 1694 1695 /// This method looks up the specified field and 1696 /// returns its value as a vector of strings, throwing an exception if the 1697 /// field does not exist or if the value is not the right type. 1698 std::vector<StringRef> getValueAsListOfStrings(StringRef FieldName) const; 1699 1700 /// This method looks up the specified field and returns its 1701 /// value as a Record, throwing an exception if the field does not exist or if 1702 /// the value is not the right type. 1703 Record *getValueAsDef(StringRef FieldName) const; 1704 1705 /// This method looks up the specified field and returns its value as a 1706 /// Record, returning null if the field exists but is "uninitialized" 1707 /// (i.e. set to `?`), and throwing an exception if the field does not 1708 /// exist or if its value is not the right type. 1709 Record *getValueAsOptionalDef(StringRef FieldName) const; 1710 1711 /// This method looks up the specified field and returns its 1712 /// value as a bit, throwing an exception if the field does not exist or if 1713 /// the value is not the right type. 1714 bool getValueAsBit(StringRef FieldName) const; 1715 1716 /// This method looks up the specified field and 1717 /// returns its value as a bit. If the field is unset, sets Unset to true and 1718 /// returns false. 1719 bool getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const; 1720 1721 /// This method looks up the specified field and returns its 1722 /// value as an int64_t, throwing an exception if the field does not exist or 1723 /// if the value is not the right type. 1724 int64_t getValueAsInt(StringRef FieldName) const; 1725 1726 /// This method looks up the specified field and returns its 1727 /// value as an Dag, throwing an exception if the field does not exist or if 1728 /// the value is not the right type. 1729 DagInit *getValueAsDag(StringRef FieldName) const; 1730 }; 1731 1732 raw_ostream &operator<<(raw_ostream &OS, const Record &R); 1733 1734 class RecordKeeper { 1735 friend class RecordRecTy; 1736 1737 using RecordMap = std::map<std::string, std::unique_ptr<Record>, std::less<>>; 1738 using GlobalMap = std::map<std::string, Init *, std::less<>>; 1739 1740 std::string InputFilename; 1741 RecordMap Classes, Defs; 1742 FoldingSet<RecordRecTy> RecordTypePool; 1743 std::map<std::string, Init *, std::less<>> ExtraGlobals; 1744 unsigned AnonCounter = 0; 1745 1746 public: 1747 /// Get the main TableGen input file's name. getInputFilename()1748 const std::string getInputFilename() const { return InputFilename; } 1749 1750 /// Get the map of classes. getClasses()1751 const RecordMap &getClasses() const { return Classes; } 1752 1753 /// Get the map of records (defs). getDefs()1754 const RecordMap &getDefs() const { return Defs; } 1755 1756 /// Get the map of global variables. getGlobals()1757 const GlobalMap &getGlobals() const { return ExtraGlobals; } 1758 1759 /// Get the class with the specified name. getClass(StringRef Name)1760 Record *getClass(StringRef Name) const { 1761 auto I = Classes.find(Name); 1762 return I == Classes.end() ? nullptr : I->second.get(); 1763 } 1764 1765 /// Get the concrete record with the specified name. getDef(StringRef Name)1766 Record *getDef(StringRef Name) const { 1767 auto I = Defs.find(Name); 1768 return I == Defs.end() ? nullptr : I->second.get(); 1769 } 1770 1771 /// Get the \p Init value of the specified global variable. getGlobal(StringRef Name)1772 Init *getGlobal(StringRef Name) const { 1773 if (Record *R = getDef(Name)) 1774 return R->getDefInit(); 1775 auto It = ExtraGlobals.find(Name); 1776 return It == ExtraGlobals.end() ? nullptr : It->second; 1777 } 1778 saveInputFilename(std::string Filename)1779 void saveInputFilename(std::string Filename) { 1780 InputFilename = Filename; 1781 } 1782 addClass(std::unique_ptr<Record> R)1783 void addClass(std::unique_ptr<Record> R) { 1784 bool Ins = Classes.insert(std::make_pair(std::string(R->getName()), 1785 std::move(R))).second; 1786 (void)Ins; 1787 assert(Ins && "Class already exists"); 1788 } 1789 addDef(std::unique_ptr<Record> R)1790 void addDef(std::unique_ptr<Record> R) { 1791 bool Ins = Defs.insert(std::make_pair(std::string(R->getName()), 1792 std::move(R))).second; 1793 (void)Ins; 1794 assert(Ins && "Record already exists"); 1795 } 1796 addExtraGlobal(StringRef Name,Init * I)1797 void addExtraGlobal(StringRef Name, Init *I) { 1798 bool Ins = ExtraGlobals.insert(std::make_pair(std::string(Name), I)).second; 1799 (void)Ins; 1800 assert(!getDef(Name)); 1801 assert(Ins && "Global already exists"); 1802 } 1803 1804 Init *getNewAnonymousName(); 1805 1806 //===--------------------------------------------------------------------===// 1807 // High-level helper methods, useful for tablegen backends. 1808 1809 /// Get all the concrete records that inherit from all the specified 1810 /// classes. The classes must be defined. 1811 std::vector<Record *> getAllDerivedDefinitions( 1812 const ArrayRef<StringRef> ClassNames) const; 1813 1814 /// Get all the concrete records that inherit from the one specified 1815 /// class. The class must be defined. getAllDerivedDefinitions(StringRef ClassName)1816 std::vector<Record *> getAllDerivedDefinitions(StringRef ClassName) const { 1817 1818 return getAllDerivedDefinitions(makeArrayRef(ClassName)); 1819 } 1820 1821 void dump() const; 1822 }; 1823 1824 /// Sorting predicate to sort record pointers by name. 1825 struct LessRecord { operatorLessRecord1826 bool operator()(const Record *Rec1, const Record *Rec2) const { 1827 return StringRef(Rec1->getName()).compare_numeric(Rec2->getName()) < 0; 1828 } 1829 }; 1830 1831 /// Sorting predicate to sort record pointers by their 1832 /// unique ID. If you just need a deterministic order, use this, since it 1833 /// just compares two `unsigned`; the other sorting predicates require 1834 /// string manipulation. 1835 struct LessRecordByID { operatorLessRecordByID1836 bool operator()(const Record *LHS, const Record *RHS) const { 1837 return LHS->getID() < RHS->getID(); 1838 } 1839 }; 1840 1841 /// Sorting predicate to sort record pointers by their 1842 /// name field. 1843 struct LessRecordFieldName { operatorLessRecordFieldName1844 bool operator()(const Record *Rec1, const Record *Rec2) const { 1845 return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name"); 1846 } 1847 }; 1848 1849 struct LessRecordRegister { ascii_isdigitLessRecordRegister1850 static bool ascii_isdigit(char x) { return x >= '0' && x <= '9'; } 1851 1852 struct RecordParts { 1853 SmallVector<std::pair< bool, StringRef>, 4> Parts; 1854 RecordPartsLessRecordRegister::RecordParts1855 RecordParts(StringRef Rec) { 1856 if (Rec.empty()) 1857 return; 1858 1859 size_t Len = 0; 1860 const char *Start = Rec.data(); 1861 const char *Curr = Start; 1862 bool isDigitPart = ascii_isdigit(Curr[0]); 1863 for (size_t I = 0, E = Rec.size(); I != E; ++I, ++Len) { 1864 bool isDigit = ascii_isdigit(Curr[I]); 1865 if (isDigit != isDigitPart) { 1866 Parts.push_back(std::make_pair(isDigitPart, StringRef(Start, Len))); 1867 Len = 0; 1868 Start = &Curr[I]; 1869 isDigitPart = ascii_isdigit(Curr[I]); 1870 } 1871 } 1872 // Push the last part. 1873 Parts.push_back(std::make_pair(isDigitPart, StringRef(Start, Len))); 1874 } 1875 sizeLessRecordRegister::RecordParts1876 size_t size() { return Parts.size(); } 1877 getPartLessRecordRegister::RecordParts1878 std::pair<bool, StringRef> getPart(size_t i) { 1879 assert (i < Parts.size() && "Invalid idx!"); 1880 return Parts[i]; 1881 } 1882 }; 1883 operatorLessRecordRegister1884 bool operator()(const Record *Rec1, const Record *Rec2) const { 1885 RecordParts LHSParts(StringRef(Rec1->getName())); 1886 RecordParts RHSParts(StringRef(Rec2->getName())); 1887 1888 size_t LHSNumParts = LHSParts.size(); 1889 size_t RHSNumParts = RHSParts.size(); 1890 assert (LHSNumParts && RHSNumParts && "Expected at least one part!"); 1891 1892 if (LHSNumParts != RHSNumParts) 1893 return LHSNumParts < RHSNumParts; 1894 1895 // We expect the registers to be of the form [_a-zA-Z]+([0-9]*[_a-zA-Z]*)*. 1896 for (size_t I = 0, E = LHSNumParts; I < E; I+=2) { 1897 std::pair<bool, StringRef> LHSPart = LHSParts.getPart(I); 1898 std::pair<bool, StringRef> RHSPart = RHSParts.getPart(I); 1899 // Expect even part to always be alpha. 1900 assert (LHSPart.first == false && RHSPart.first == false && 1901 "Expected both parts to be alpha."); 1902 if (int Res = LHSPart.second.compare(RHSPart.second)) 1903 return Res < 0; 1904 } 1905 for (size_t I = 1, E = LHSNumParts; I < E; I+=2) { 1906 std::pair<bool, StringRef> LHSPart = LHSParts.getPart(I); 1907 std::pair<bool, StringRef> RHSPart = RHSParts.getPart(I); 1908 // Expect odd part to always be numeric. 1909 assert (LHSPart.first == true && RHSPart.first == true && 1910 "Expected both parts to be numeric."); 1911 if (LHSPart.second.size() != RHSPart.second.size()) 1912 return LHSPart.second.size() < RHSPart.second.size(); 1913 1914 unsigned LHSVal, RHSVal; 1915 1916 bool LHSFailed = LHSPart.second.getAsInteger(10, LHSVal); (void)LHSFailed; 1917 assert(!LHSFailed && "Unable to convert LHS to integer."); 1918 bool RHSFailed = RHSPart.second.getAsInteger(10, RHSVal); (void)RHSFailed; 1919 assert(!RHSFailed && "Unable to convert RHS to integer."); 1920 1921 if (LHSVal != RHSVal) 1922 return LHSVal < RHSVal; 1923 } 1924 return LHSNumParts < RHSNumParts; 1925 } 1926 }; 1927 1928 raw_ostream &operator<<(raw_ostream &OS, const RecordKeeper &RK); 1929 1930 //===----------------------------------------------------------------------===// 1931 // Resolvers 1932 //===----------------------------------------------------------------------===// 1933 1934 /// Interface for looking up the initializer for a variable name, used by 1935 /// Init::resolveReferences. 1936 class Resolver { 1937 Record *CurRec; 1938 bool IsFinal = false; 1939 1940 public: Resolver(Record * CurRec)1941 explicit Resolver(Record *CurRec) : CurRec(CurRec) {} ~Resolver()1942 virtual ~Resolver() {} 1943 getCurrentRecord()1944 Record *getCurrentRecord() const { return CurRec; } 1945 1946 /// Return the initializer for the given variable name (should normally be a 1947 /// StringInit), or nullptr if the name could not be resolved. 1948 virtual Init *resolve(Init *VarName) = 0; 1949 1950 // Whether bits in a BitsInit should stay unresolved if resolving them would 1951 // result in a ? (UnsetInit). This behavior is used to represent instruction 1952 // encodings by keeping references to unset variables within a record. keepUnsetBits()1953 virtual bool keepUnsetBits() const { return false; } 1954 1955 // Whether this is the final resolve step before adding a record to the 1956 // RecordKeeper. Error reporting during resolve and related constant folding 1957 // should only happen when this is true. isFinal()1958 bool isFinal() const { return IsFinal; } 1959 setFinal(bool Final)1960 void setFinal(bool Final) { IsFinal = Final; } 1961 }; 1962 1963 /// Resolve arbitrary mappings. 1964 class MapResolver final : public Resolver { 1965 struct MappedValue { 1966 Init *V; 1967 bool Resolved; 1968 MappedValueMappedValue1969 MappedValue() : V(nullptr), Resolved(false) {} MappedValueMappedValue1970 MappedValue(Init *V, bool Resolved) : V(V), Resolved(Resolved) {} 1971 }; 1972 1973 DenseMap<Init *, MappedValue> Map; 1974 1975 public: Resolver(CurRec)1976 explicit MapResolver(Record *CurRec = nullptr) : Resolver(CurRec) {} 1977 set(Init * Key,Init * Value)1978 void set(Init *Key, Init *Value) { Map[Key] = {Value, false}; } 1979 1980 Init *resolve(Init *VarName) override; 1981 }; 1982 1983 /// Resolve all variables from a record except for unset variables. 1984 class RecordResolver final : public Resolver { 1985 DenseMap<Init *, Init *> Cache; 1986 SmallVector<Init *, 4> Stack; 1987 1988 public: RecordResolver(Record & R)1989 explicit RecordResolver(Record &R) : Resolver(&R) {} 1990 1991 Init *resolve(Init *VarName) override; 1992 keepUnsetBits()1993 bool keepUnsetBits() const override { return true; } 1994 }; 1995 1996 /// Resolve all references to a specific RecordVal. 1997 // 1998 // TODO: This is used for resolving references to template arguments, in a 1999 // rather inefficient way. Change those uses to resolve all template 2000 // arguments simultaneously and get rid of this class. 2001 class RecordValResolver final : public Resolver { 2002 const RecordVal *RV; 2003 2004 public: RecordValResolver(Record & R,const RecordVal * RV)2005 explicit RecordValResolver(Record &R, const RecordVal *RV) 2006 : Resolver(&R), RV(RV) {} 2007 resolve(Init * VarName)2008 Init *resolve(Init *VarName) override { 2009 if (VarName == RV->getNameInit()) 2010 return RV->getValue(); 2011 return nullptr; 2012 } 2013 }; 2014 2015 /// Delegate resolving to a sub-resolver, but shadow some variable names. 2016 class ShadowResolver final : public Resolver { 2017 Resolver &R; 2018 DenseSet<Init *> Shadowed; 2019 2020 public: ShadowResolver(Resolver & R)2021 explicit ShadowResolver(Resolver &R) 2022 : Resolver(R.getCurrentRecord()), R(R) { 2023 setFinal(R.isFinal()); 2024 } 2025 addShadow(Init * Key)2026 void addShadow(Init *Key) { Shadowed.insert(Key); } 2027 resolve(Init * VarName)2028 Init *resolve(Init *VarName) override { 2029 if (Shadowed.count(VarName)) 2030 return nullptr; 2031 return R.resolve(VarName); 2032 } 2033 }; 2034 2035 /// (Optionally) delegate resolving to a sub-resolver, and keep track whether 2036 /// there were unresolved references. 2037 class TrackUnresolvedResolver final : public Resolver { 2038 Resolver *R; 2039 bool FoundUnresolved = false; 2040 2041 public: 2042 explicit TrackUnresolvedResolver(Resolver *R = nullptr) 2043 : Resolver(R ? R->getCurrentRecord() : nullptr), R(R) {} 2044 foundUnresolved()2045 bool foundUnresolved() const { return FoundUnresolved; } 2046 2047 Init *resolve(Init *VarName) override; 2048 }; 2049 2050 /// Do not resolve anything, but keep track of whether a given variable was 2051 /// referenced. 2052 class HasReferenceResolver final : public Resolver { 2053 Init *VarNameToTrack; 2054 bool Found = false; 2055 2056 public: HasReferenceResolver(Init * VarNameToTrack)2057 explicit HasReferenceResolver(Init *VarNameToTrack) 2058 : Resolver(nullptr), VarNameToTrack(VarNameToTrack) {} 2059 found()2060 bool found() const { return Found; } 2061 2062 Init *resolve(Init *VarName) override; 2063 }; 2064 2065 void EmitDetailedRecords(RecordKeeper &RK, raw_ostream &OS); 2066 void EmitJSON(RecordKeeper &RK, raw_ostream &OS); 2067 2068 } // end namespace llvm 2069 2070 #endif // LLVM_TABLEGEN_RECORD_H 2071