1 //===- llvm/TableGen/Record.h - Classes for Table Records -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the main TableGen data structures, including the TableGen
11 // types, values, and high-level data structures.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_TABLEGEN_RECORD_H
16 #define LLVM_TABLEGEN_RECORD_H
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/DenseSet.h"
21 #include "llvm/ADT/FoldingSet.h"
22 #include "llvm/ADT/PointerIntPair.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/ADT/StringRef.h"
25 #include "llvm/Support/Casting.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/SMLoc.h"
28 #include "llvm/Support/TrailingObjects.h"
29 #include "llvm/Support/raw_ostream.h"
51 //===----------------------------------------------------------------------===//
53 //===----------------------------------------------------------------------===//
57 /// \brief Subclass discriminator (for dyn_cast<> et al.)
71 ListRecTy *ListTy = nullptr;
74 RecTy(RecTyKind K) : Kind(K) {}
75 virtual ~RecTy() = default;
77 RecTyKind getRecTyKind() const { return Kind; }
79 virtual std::string getAsString() const = 0;
80 void print(raw_ostream &OS) const { OS << getAsString(); }
83 /// Return true if all values of 'this' type can be converted to the specified
85 virtual bool typeIsConvertibleTo(const RecTy *RHS) const;
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;
91 /// Returns the type representing list<this>.
92 ListRecTy *getListTy();
95 inline raw_ostream &operator<<(raw_ostream &OS, const RecTy &Ty) {
100 /// 'bit' - Represent a single bit
101 class BitRecTy : public RecTy {
102 static BitRecTy Shared;
104 BitRecTy() : RecTy(BitRecTyKind) {}
107 static bool classof(const RecTy *RT) {
108 return RT->getRecTyKind() == BitRecTyKind;
111 static BitRecTy *get() { return &Shared; }
113 std::string getAsString() const override { return "bit"; }
115 bool typeIsConvertibleTo(const RecTy *RHS) const override;
118 /// 'bits<n>' - Represent a fixed number of bits
119 class BitsRecTy : public RecTy {
122 explicit BitsRecTy(unsigned Sz) : RecTy(BitsRecTyKind), Size(Sz) {}
125 static bool classof(const RecTy *RT) {
126 return RT->getRecTyKind() == BitsRecTyKind;
129 static BitsRecTy *get(unsigned Sz);
131 unsigned getNumBits() const { return Size; }
133 std::string getAsString() const override;
135 bool typeIsConvertibleTo(const RecTy *RHS) const override;
137 bool typeIsA(const RecTy *RHS) const override;
140 /// 'code' - Represent a code fragment
141 class CodeRecTy : public RecTy {
142 static CodeRecTy Shared;
144 CodeRecTy() : RecTy(CodeRecTyKind) {}
147 static bool classof(const RecTy *RT) {
148 return RT->getRecTyKind() == CodeRecTyKind;
151 static CodeRecTy *get() { return &Shared; }
153 std::string getAsString() const override { return "code"; }
155 bool typeIsConvertibleTo(const RecTy *RHS) const override;
158 /// 'int' - Represent an integer value of no particular size
159 class IntRecTy : public RecTy {
160 static IntRecTy Shared;
162 IntRecTy() : RecTy(IntRecTyKind) {}
165 static bool classof(const RecTy *RT) {
166 return RT->getRecTyKind() == IntRecTyKind;
169 static IntRecTy *get() { return &Shared; }
171 std::string getAsString() const override { return "int"; }
173 bool typeIsConvertibleTo(const RecTy *RHS) const override;
176 /// 'string' - Represent an string value
177 class StringRecTy : public RecTy {
178 static StringRecTy Shared;
180 StringRecTy() : RecTy(StringRecTyKind) {}
183 static bool classof(const RecTy *RT) {
184 return RT->getRecTyKind() == StringRecTyKind;
187 static StringRecTy *get() { return &Shared; }
189 std::string getAsString() const override;
191 bool typeIsConvertibleTo(const RecTy *RHS) const override;
194 /// 'list<Ty>' - Represent a list of values, all of which must be of
195 /// the specified type.
196 class ListRecTy : public RecTy {
197 friend ListRecTy *RecTy::getListTy();
201 explicit ListRecTy(RecTy *T) : RecTy(ListRecTyKind), Ty(T) {}
204 static bool classof(const RecTy *RT) {
205 return RT->getRecTyKind() == ListRecTyKind;
208 static ListRecTy *get(RecTy *T) { return T->getListTy(); }
209 RecTy *getElementType() const { return Ty; }
211 std::string getAsString() const override;
213 bool typeIsConvertibleTo(const RecTy *RHS) const override;
215 bool typeIsA(const RecTy *RHS) const override;
218 /// 'dag' - Represent a dag fragment
219 class DagRecTy : public RecTy {
220 static DagRecTy Shared;
222 DagRecTy() : RecTy(DagRecTyKind) {}
225 static bool classof(const RecTy *RT) {
226 return RT->getRecTyKind() == DagRecTyKind;
229 static DagRecTy *get() { return &Shared; }
231 std::string getAsString() const override;
234 /// '[classname]' - Type of record values that have zero or more superclasses.
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 *> {
244 explicit RecordRecTy(unsigned Num)
245 : RecTy(RecordRecTyKind), NumClasses(Num) {}
248 RecordRecTy(const RecordRecTy &) = delete;
249 RecordRecTy &operator=(const RecordRecTy &) = delete;
251 // Do not use sized deallocation due to trailing objects.
252 void operator delete(void *p) { ::operator delete(p); }
254 static bool classof(const RecTy *RT) {
255 return RT->getRecTyKind() == RecordRecTyKind;
258 /// Get the record type with the given non-redundant list of superclasses.
259 static RecordRecTy *get(ArrayRef<Record *> Classes);
261 void Profile(FoldingSetNodeID &ID) const;
263 ArrayRef<Record *> getClasses() const {
264 return makeArrayRef(getTrailingObjects<Record *>(), NumClasses);
267 using const_record_iterator = Record * const *;
269 const_record_iterator classes_begin() const { return getClasses().begin(); }
270 const_record_iterator classes_end() const { return getClasses().end(); }
272 std::string getAsString() const override;
274 bool isSubClassOf(Record *Class) const;
275 bool typeIsConvertibleTo(const RecTy *RHS) const override;
277 bool typeIsA(const RecTy *RHS) const override;
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);
284 //===----------------------------------------------------------------------===//
285 // Initializer Classes
286 //===----------------------------------------------------------------------===//
290 /// \brief Discriminator enum (for isa<>, dyn_cast<>, et al.)
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.
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().
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
323 IK_VarListElementInit,
334 uint8_t Opc; // Used by UnOpInit, BinOpInit, and TernOpInit
337 virtual void anchor();
340 InitKind getKind() const { return Kind; }
343 explicit Init(InitKind K, uint8_t Opc = 0) : Kind(K), Opc(Opc) {}
346 Init(const Init &) = delete;
347 Init &operator=(const Init &) = delete;
348 virtual ~Init() = default;
350 /// This virtual method should be overridden by values that may
351 /// not be completely specified yet.
352 virtual bool isComplete() const { return true; }
354 /// Is this a concrete and fully resolved value without any references or
355 /// stuck operations? Unset values are concrete.
356 virtual bool isConcrete() const { return false; }
358 /// Print out this value.
359 void print(raw_ostream &OS) const { OS << getAsString(); }
361 /// Convert this value to a string form.
362 virtual std::string getAsString() const = 0;
363 /// Convert this value to a string form,
364 /// without adding quote markers. This primaruly affects
365 /// StringInits where we will not surround the string value with
367 virtual std::string getAsUnquotedString() const { return getAsString(); }
369 /// Debugging method that may be called through a debugger, just
370 /// invokes print on stderr.
373 /// If this initializer is convertible to Ty, return an initializer whose
374 /// type is-a Ty, generating a !cast operation if required. Otherwise, return
376 virtual Init *getCastTo(RecTy *Ty) const = 0;
378 /// Convert to an initializer whose type is-a Ty, or return nullptr if this
379 /// is not possible (this can happen if the initializer's type is convertible
380 /// to Ty, but there are unresolved references).
381 virtual Init *convertInitializerTo(RecTy *Ty) const = 0;
383 /// This method is used to implement the bitrange
384 /// selection operator. Given an initializer, it selects the specified bits
385 /// out, returning them as a new init of bits type. If it is not legal to use
386 /// the bit subscript operator on this initializer, return null.
387 virtual Init *convertInitializerBitRange(ArrayRef<unsigned> Bits) const {
391 /// This method is used to implement the list slice
392 /// selection operator. Given an initializer, it selects the specified list
393 /// elements, returning them as a new init of list type. If it is not legal
394 /// to take a slice of this, return null.
395 virtual Init *convertInitListSlice(ArrayRef<unsigned> Elements) const {
399 /// This method is used to implement the FieldInit class.
400 /// Implementors of this method should return the type of the named field if
401 /// they are of record type.
402 virtual RecTy *getFieldType(StringInit *FieldName) const {
406 /// This method 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.
410 virtual Init *resolveReferences(Resolver &R) const {
411 return const_cast<Init *>(this);
414 /// This method is used to return the initializer for the specified
416 virtual Init *getBit(unsigned Bit) const = 0;
419 inline raw_ostream &operator<<(raw_ostream &OS, const Init &I) {
420 I.print(OS); return OS;
423 /// This is the common super-class of types that have a specific,
425 class TypedInit : public Init {
429 explicit TypedInit(InitKind K, RecTy *T, uint8_t Opc = 0)
430 : Init(K, Opc), Ty(T) {}
433 TypedInit(const TypedInit &) = delete;
434 TypedInit &operator=(const TypedInit &) = delete;
436 static bool classof(const Init *I) {
437 return I->getKind() >= IK_FirstTypedInit &&
438 I->getKind() <= IK_LastTypedInit;
441 RecTy *getType() const { return Ty; }
443 Init *getCastTo(RecTy *Ty) const override;
444 Init *convertInitializerTo(RecTy *Ty) const override;
446 Init *convertInitializerBitRange(ArrayRef<unsigned> Bits) const override;
447 Init *convertInitListSlice(ArrayRef<unsigned> Elements) const override;
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 record type.
453 RecTy *getFieldType(StringInit *FieldName) const override;
456 /// '?' - Represents an uninitialized value
457 class UnsetInit : public Init {
458 UnsetInit() : Init(IK_UnsetInit) {}
461 UnsetInit(const UnsetInit &) = delete;
462 UnsetInit &operator=(const UnsetInit &) = delete;
464 static bool classof(const Init *I) {
465 return I->getKind() == IK_UnsetInit;
468 static UnsetInit *get();
470 Init *getCastTo(RecTy *Ty) const override;
471 Init *convertInitializerTo(RecTy *Ty) const override;
473 Init *getBit(unsigned Bit) const override {
474 return const_cast<UnsetInit*>(this);
477 bool isComplete() const override { return false; }
478 bool isConcrete() const override { return true; }
479 std::string getAsString() const override { return "?"; }
482 /// 'true'/'false' - Represent a concrete initializer for a bit.
483 class BitInit final : public TypedInit {
486 explicit BitInit(bool V) : TypedInit(IK_BitInit, BitRecTy::get()), Value(V) {}
489 BitInit(const BitInit &) = delete;
490 BitInit &operator=(BitInit &) = delete;
492 static bool classof(const Init *I) {
493 return I->getKind() == IK_BitInit;
496 static BitInit *get(bool V);
498 bool getValue() const { return Value; }
500 Init *convertInitializerTo(RecTy *Ty) const override;
502 Init *getBit(unsigned Bit) const override {
503 assert(Bit < 1 && "Bit index out of range!");
504 return const_cast<BitInit*>(this);
507 bool isConcrete() const override { return true; }
508 std::string getAsString() const override { return Value ? "1" : "0"; }
511 /// '{ a, b, c }' - Represents an initializer for a BitsRecTy value.
512 /// It contains a vector of bits, whose size is determined by the type.
513 class BitsInit final : public TypedInit, public FoldingSetNode,
514 public TrailingObjects<BitsInit, Init *> {
518 : TypedInit(IK_BitsInit, BitsRecTy::get(N)), NumBits(N) {}
521 BitsInit(const BitsInit &) = delete;
522 BitsInit &operator=(const BitsInit &) = delete;
524 // Do not use sized deallocation due to trailing objects.
525 void operator delete(void *p) { ::operator delete(p); }
527 static bool classof(const Init *I) {
528 return I->getKind() == IK_BitsInit;
531 static BitsInit *get(ArrayRef<Init *> Range);
533 void Profile(FoldingSetNodeID &ID) const;
535 unsigned getNumBits() const { return NumBits; }
537 Init *convertInitializerTo(RecTy *Ty) const override;
538 Init *convertInitializerBitRange(ArrayRef<unsigned> Bits) const override;
540 bool isComplete() const override {
541 for (unsigned i = 0; i != getNumBits(); ++i)
542 if (!getBit(i)->isComplete()) return false;
546 bool allInComplete() const {
547 for (unsigned i = 0; i != getNumBits(); ++i)
548 if (getBit(i)->isComplete()) return false;
552 bool isConcrete() const override;
553 std::string getAsString() const override;
555 Init *resolveReferences(Resolver &R) const override;
557 Init *getBit(unsigned Bit) const override {
558 assert(Bit < NumBits && "Bit index out of range!");
559 return getTrailingObjects<Init *>()[Bit];
563 /// '7' - Represent an initialization by a literal integer value.
564 class IntInit : public TypedInit {
567 explicit IntInit(int64_t V)
568 : TypedInit(IK_IntInit, IntRecTy::get()), Value(V) {}
571 IntInit(const IntInit &) = delete;
572 IntInit &operator=(const IntInit &) = delete;
574 static bool classof(const Init *I) {
575 return I->getKind() == IK_IntInit;
578 static IntInit *get(int64_t V);
580 int64_t getValue() const { return Value; }
582 Init *convertInitializerTo(RecTy *Ty) const override;
583 Init *convertInitializerBitRange(ArrayRef<unsigned> Bits) const override;
585 bool isConcrete() const override { return true; }
586 std::string getAsString() const override;
588 Init *getBit(unsigned Bit) const override {
589 return BitInit::get((Value & (1ULL << Bit)) != 0);
593 /// "foo" - Represent an initialization by a string value.
594 class StringInit : public TypedInit {
597 explicit StringInit(StringRef V)
598 : TypedInit(IK_StringInit, StringRecTy::get()), Value(V) {}
601 StringInit(const StringInit &) = delete;
602 StringInit &operator=(const StringInit &) = delete;
604 static bool classof(const Init *I) {
605 return I->getKind() == IK_StringInit;
608 static StringInit *get(StringRef);
610 StringRef getValue() const { return Value; }
612 Init *convertInitializerTo(RecTy *Ty) const override;
614 bool isConcrete() const override { return true; }
615 std::string getAsString() const override { return "\"" + Value.str() + "\""; }
617 std::string getAsUnquotedString() const override { return Value; }
619 Init *getBit(unsigned Bit) const override {
620 llvm_unreachable("Illegal bit reference off string");
624 class CodeInit : public TypedInit {
627 explicit CodeInit(StringRef V)
628 : TypedInit(IK_CodeInit, static_cast<RecTy *>(CodeRecTy::get())),
632 CodeInit(const StringInit &) = delete;
633 CodeInit &operator=(const StringInit &) = delete;
635 static bool classof(const Init *I) {
636 return I->getKind() == IK_CodeInit;
639 static CodeInit *get(StringRef);
641 StringRef getValue() const { return Value; }
643 Init *convertInitializerTo(RecTy *Ty) const override;
645 bool isConcrete() const override { return true; }
646 std::string getAsString() const override {
647 return "[{" + Value.str() + "}]";
650 std::string getAsUnquotedString() const override { return Value; }
652 Init *getBit(unsigned Bit) const override {
653 llvm_unreachable("Illegal bit reference off string");
657 /// [AL, AH, CL] - Represent a list of defs
659 class ListInit final : public TypedInit, public FoldingSetNode,
660 public TrailingObjects<ListInit, Init *> {
664 using const_iterator = Init *const *;
667 explicit ListInit(unsigned N, RecTy *EltTy)
668 : TypedInit(IK_ListInit, ListRecTy::get(EltTy)), NumValues(N) {}
671 ListInit(const ListInit &) = delete;
672 ListInit &operator=(const ListInit &) = delete;
674 // Do not use sized deallocation due to trailing objects.
675 void operator delete(void *p) { ::operator delete(p); }
677 static bool classof(const Init *I) {
678 return I->getKind() == IK_ListInit;
680 static ListInit *get(ArrayRef<Init *> Range, RecTy *EltTy);
682 void Profile(FoldingSetNodeID &ID) const;
684 Init *getElement(unsigned i) const {
685 assert(i < NumValues && "List element index out of range!");
686 return getTrailingObjects<Init *>()[i];
688 RecTy *getElementType() const {
689 return cast<ListRecTy>(getType())->getElementType();
692 Record *getElementAsRecord(unsigned i) const;
694 Init *convertInitListSlice(ArrayRef<unsigned> Elements) const override;
696 Init *convertInitializerTo(RecTy *Ty) const override;
698 /// This method is used by classes that refer to other
699 /// variables which may not be defined at the time they expression is formed.
700 /// If a value is set for the variable later, this method will be called on
701 /// users of the value to allow the value to propagate out.
703 Init *resolveReferences(Resolver &R) const override;
705 bool isConcrete() const override;
706 std::string getAsString() const override;
708 ArrayRef<Init*> getValues() const {
709 return makeArrayRef(getTrailingObjects<Init *>(), NumValues);
712 const_iterator begin() const { return getTrailingObjects<Init *>(); }
713 const_iterator end () const { return begin() + NumValues; }
715 size_t size () const { return NumValues; }
716 bool empty() const { return NumValues == 0; }
718 Init *getBit(unsigned Bit) const override {
719 llvm_unreachable("Illegal bit reference off list");
723 /// Base class for operators
725 class OpInit : public TypedInit {
727 explicit OpInit(InitKind K, RecTy *Type, uint8_t Opc)
728 : TypedInit(K, Type, Opc) {}
731 OpInit(const OpInit &) = delete;
732 OpInit &operator=(OpInit &) = delete;
734 static bool classof(const Init *I) {
735 return I->getKind() >= IK_FirstOpInit &&
736 I->getKind() <= IK_LastOpInit;
739 // Clone - Clone this operator, replacing arguments with the new list
740 virtual OpInit *clone(ArrayRef<Init *> Operands) const = 0;
742 virtual unsigned getNumOperands() const = 0;
743 virtual Init *getOperand(unsigned i) const = 0;
745 // Fold - If possible, fold this to a simpler init. Return this if not
747 virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const = 0;
749 Init *getBit(unsigned Bit) const override;
752 /// !op (X) - Transform an init.
754 class UnOpInit : public OpInit, public FoldingSetNode {
756 enum UnaryOp : uint8_t { CAST, HEAD, TAIL, SIZE, EMPTY };
761 UnOpInit(UnaryOp opc, Init *lhs, RecTy *Type)
762 : OpInit(IK_UnOpInit, Type, opc), LHS(lhs) {}
765 UnOpInit(const UnOpInit &) = delete;
766 UnOpInit &operator=(const UnOpInit &) = delete;
768 static bool classof(const Init *I) {
769 return I->getKind() == IK_UnOpInit;
772 static UnOpInit *get(UnaryOp opc, Init *lhs, RecTy *Type);
774 void Profile(FoldingSetNodeID &ID) const;
776 // Clone - Clone this operator, replacing arguments with the new list
777 OpInit *clone(ArrayRef<Init *> Operands) const override {
778 assert(Operands.size() == 1 &&
779 "Wrong number of operands for unary operation");
780 return UnOpInit::get(getOpcode(), *Operands.begin(), getType());
783 unsigned getNumOperands() const override { return 1; }
785 Init *getOperand(unsigned i) const override {
786 assert(i == 0 && "Invalid operand id for unary operator");
790 UnaryOp getOpcode() const { return (UnaryOp)Opc; }
791 Init *getOperand() const { return LHS; }
793 // Fold - If possible, fold this to a simpler init. Return this if not
795 Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const override;
797 Init *resolveReferences(Resolver &R) const override;
799 std::string getAsString() const override;
802 /// !op (X, Y) - Combine two inits.
803 class BinOpInit : public OpInit, public FoldingSetNode {
805 enum BinaryOp : uint8_t { ADD, AND, OR, SHL, SRA, SRL, LISTCONCAT,
806 STRCONCAT, CONCAT, EQ };
811 BinOpInit(BinaryOp opc, Init *lhs, Init *rhs, RecTy *Type) :
812 OpInit(IK_BinOpInit, Type, opc), LHS(lhs), RHS(rhs) {}
815 BinOpInit(const BinOpInit &) = delete;
816 BinOpInit &operator=(const BinOpInit &) = delete;
818 static bool classof(const Init *I) {
819 return I->getKind() == IK_BinOpInit;
822 static BinOpInit *get(BinaryOp opc, Init *lhs, Init *rhs,
825 void Profile(FoldingSetNodeID &ID) const;
827 // Clone - Clone this operator, replacing arguments with the new list
828 OpInit *clone(ArrayRef<Init *> Operands) const override {
829 assert(Operands.size() == 2 &&
830 "Wrong number of operands for binary operation");
831 return BinOpInit::get(getOpcode(), Operands[0], Operands[1], getType());
834 unsigned getNumOperands() const override { return 2; }
835 Init *getOperand(unsigned i) const override {
837 default: llvm_unreachable("Invalid operand id for binary operator");
838 case 0: return getLHS();
839 case 1: return getRHS();
843 BinaryOp getOpcode() const { return (BinaryOp)Opc; }
844 Init *getLHS() const { return LHS; }
845 Init *getRHS() const { return RHS; }
847 // Fold - If possible, fold this to a simpler init. Return this if not
849 Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const override;
851 Init *resolveReferences(Resolver &R) const override;
853 std::string getAsString() const override;
856 /// !op (X, Y, Z) - Combine two inits.
857 class TernOpInit : public OpInit, public FoldingSetNode {
859 enum TernaryOp : uint8_t { SUBST, FOREACH, IF };
862 Init *LHS, *MHS, *RHS;
864 TernOpInit(TernaryOp opc, Init *lhs, Init *mhs, Init *rhs,
866 OpInit(IK_TernOpInit, Type, opc), LHS(lhs), MHS(mhs), RHS(rhs) {}
869 TernOpInit(const TernOpInit &) = delete;
870 TernOpInit &operator=(const TernOpInit &) = delete;
872 static bool classof(const Init *I) {
873 return I->getKind() == IK_TernOpInit;
876 static TernOpInit *get(TernaryOp opc, Init *lhs,
877 Init *mhs, Init *rhs,
880 void Profile(FoldingSetNodeID &ID) const;
882 // Clone - Clone this operator, replacing arguments with the new list
883 OpInit *clone(ArrayRef<Init *> Operands) const override {
884 assert(Operands.size() == 3 &&
885 "Wrong number of operands for ternary operation");
886 return TernOpInit::get(getOpcode(), Operands[0], Operands[1], Operands[2],
890 unsigned getNumOperands() const override { return 3; }
891 Init *getOperand(unsigned i) const override {
893 default: llvm_unreachable("Invalid operand id for ternary operator");
894 case 0: return getLHS();
895 case 1: return getMHS();
896 case 2: return getRHS();
900 TernaryOp getOpcode() const { return (TernaryOp)Opc; }
901 Init *getLHS() const { return LHS; }
902 Init *getMHS() const { return MHS; }
903 Init *getRHS() const { return RHS; }
905 // Fold - If possible, fold this to a simpler init. Return this if not
907 Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const override;
909 bool isComplete() const override {
910 return LHS->isComplete() && MHS->isComplete() && RHS->isComplete();
913 Init *resolveReferences(Resolver &R) const override;
915 std::string getAsString() const override;
918 /// !foldl (a, b, expr, start, lst) - Fold over a list.
919 class FoldOpInit : public TypedInit, public FoldingSetNode {
927 FoldOpInit(Init *Start, Init *List, Init *A, Init *B, Init *Expr, RecTy *Type)
928 : TypedInit(IK_FoldOpInit, Type), Start(Start), List(List), A(A), B(B),
932 FoldOpInit(const FoldOpInit &) = delete;
933 FoldOpInit &operator=(const FoldOpInit &) = delete;
935 static bool classof(const Init *I) { return I->getKind() == IK_FoldOpInit; }
937 static FoldOpInit *get(Init *Start, Init *List, Init *A, Init *B, Init *Expr,
940 void Profile(FoldingSetNodeID &ID) const;
942 // Fold - If possible, fold this to a simpler init. Return this if not
944 Init *Fold(Record *CurRec) const;
946 bool isComplete() const override { return false; }
948 Init *resolveReferences(Resolver &R) const override;
950 Init *getBit(unsigned Bit) const override;
952 std::string getAsString() const override;
955 /// !isa<type>(expr) - Dynamically determine the type of an expression.
956 class IsAOpInit : public TypedInit, public FoldingSetNode {
961 IsAOpInit(RecTy *CheckType, Init *Expr)
962 : TypedInit(IK_IsAOpInit, IntRecTy::get()), CheckType(CheckType),
966 IsAOpInit(const IsAOpInit &) = delete;
967 IsAOpInit &operator=(const IsAOpInit &) = delete;
969 static bool classof(const Init *I) { return I->getKind() == IK_IsAOpInit; }
971 static IsAOpInit *get(RecTy *CheckType, Init *Expr);
973 void Profile(FoldingSetNodeID &ID) const;
975 // Fold - If possible, fold this to a simpler init. Return this if not
979 bool isComplete() const override { return false; }
981 Init *resolveReferences(Resolver &R) const override;
983 Init *getBit(unsigned Bit) const override;
985 std::string getAsString() const override;
988 /// 'Opcode' - Represent a reference to an entire variable object.
989 class VarInit : public TypedInit {
992 explicit VarInit(Init *VN, RecTy *T)
993 : TypedInit(IK_VarInit, T), VarName(VN) {}
996 VarInit(const VarInit &) = delete;
997 VarInit &operator=(const VarInit &) = delete;
999 static bool classof(const Init *I) {
1000 return I->getKind() == IK_VarInit;
1003 static VarInit *get(StringRef VN, RecTy *T);
1004 static VarInit *get(Init *VN, RecTy *T);
1006 StringRef getName() const;
1007 Init *getNameInit() const { return VarName; }
1009 std::string getNameInitAsString() const {
1010 return getNameInit()->getAsUnquotedString();
1013 /// This method is used by classes that refer to other
1014 /// variables which may not be defined at the time they expression is formed.
1015 /// If a value is set for the variable later, this method will be called on
1016 /// users of the value to allow the value to propagate out.
1018 Init *resolveReferences(Resolver &R) const override;
1020 Init *getBit(unsigned Bit) const override;
1022 std::string getAsString() const override { return getName(); }
1025 /// Opcode{0} - Represent access to one bit of a variable or field.
1026 class VarBitInit final : public TypedInit {
1030 VarBitInit(TypedInit *T, unsigned B)
1031 : TypedInit(IK_VarBitInit, BitRecTy::get()), TI(T), Bit(B) {
1032 assert(T->getType() &&
1033 (isa<IntRecTy>(T->getType()) ||
1034 (isa<BitsRecTy>(T->getType()) &&
1035 cast<BitsRecTy>(T->getType())->getNumBits() > B)) &&
1036 "Illegal VarBitInit expression!");
1040 VarBitInit(const VarBitInit &) = delete;
1041 VarBitInit &operator=(const VarBitInit &) = delete;
1043 static bool classof(const Init *I) {
1044 return I->getKind() == IK_VarBitInit;
1047 static VarBitInit *get(TypedInit *T, unsigned B);
1049 Init *getBitVar() const { return TI; }
1050 unsigned getBitNum() const { return Bit; }
1052 std::string getAsString() const override;
1053 Init *resolveReferences(Resolver &R) const override;
1055 Init *getBit(unsigned B) const override {
1056 assert(B < 1 && "Bit index out of range!");
1057 return const_cast<VarBitInit*>(this);
1061 /// List[4] - Represent access to one element of a var or
1063 class VarListElementInit : public TypedInit {
1067 VarListElementInit(TypedInit *T, unsigned E)
1068 : TypedInit(IK_VarListElementInit,
1069 cast<ListRecTy>(T->getType())->getElementType()),
1071 assert(T->getType() && isa<ListRecTy>(T->getType()) &&
1072 "Illegal VarBitInit expression!");
1076 VarListElementInit(const VarListElementInit &) = delete;
1077 VarListElementInit &operator=(const VarListElementInit &) = delete;
1079 static bool classof(const Init *I) {
1080 return I->getKind() == IK_VarListElementInit;
1083 static VarListElementInit *get(TypedInit *T, unsigned E);
1085 TypedInit *getVariable() const { return TI; }
1086 unsigned getElementNum() const { return Element; }
1088 std::string getAsString() const override;
1089 Init *resolveReferences(Resolver &R) const override;
1091 Init *getBit(unsigned Bit) const override;
1094 /// AL - Represent a reference to a 'def' in the description
1095 class DefInit : public TypedInit {
1096 friend class Record;
1100 explicit DefInit(Record *D);
1103 DefInit(const DefInit &) = delete;
1104 DefInit &operator=(const DefInit &) = delete;
1106 static bool classof(const Init *I) {
1107 return I->getKind() == IK_DefInit;
1110 static DefInit *get(Record*);
1112 Init *convertInitializerTo(RecTy *Ty) const override;
1114 Record *getDef() const { return Def; }
1116 //virtual Init *convertInitializerBitRange(ArrayRef<unsigned> Bits);
1118 RecTy *getFieldType(StringInit *FieldName) const override;
1120 bool isConcrete() const override { return true; }
1121 std::string getAsString() const override;
1123 Init *getBit(unsigned Bit) const override {
1124 llvm_unreachable("Illegal bit reference off def");
1128 /// classname<targs...> - Represent an uninstantiated anonymous class
1130 class VarDefInit final : public TypedInit, public FoldingSetNode,
1131 public TrailingObjects<VarDefInit, Init *> {
1133 DefInit *Def = nullptr; // after instantiation
1136 explicit VarDefInit(Record *Class, unsigned N)
1137 : TypedInit(IK_VarDefInit, RecordRecTy::get(Class)), Class(Class), NumArgs(N) {}
1139 DefInit *instantiate();
1142 VarDefInit(const VarDefInit &) = delete;
1143 VarDefInit &operator=(const VarDefInit &) = delete;
1145 // Do not use sized deallocation due to trailing objects.
1146 void operator delete(void *p) { ::operator delete(p); }
1148 static bool classof(const Init *I) {
1149 return I->getKind() == IK_VarDefInit;
1151 static VarDefInit *get(Record *Class, ArrayRef<Init *> Args);
1153 void Profile(FoldingSetNodeID &ID) const;
1155 Init *resolveReferences(Resolver &R) const override;
1158 std::string getAsString() const override;
1160 Init *getArg(unsigned i) const {
1161 assert(i < NumArgs && "Argument index out of range!");
1162 return getTrailingObjects<Init *>()[i];
1165 using const_iterator = Init *const *;
1167 const_iterator args_begin() const { return getTrailingObjects<Init *>(); }
1168 const_iterator args_end () const { return args_begin() + NumArgs; }
1170 size_t args_size () const { return NumArgs; }
1171 bool args_empty() const { return NumArgs == 0; }
1173 ArrayRef<Init *> args() const { return makeArrayRef(args_begin(), NumArgs); }
1175 Init *getBit(unsigned Bit) const override {
1176 llvm_unreachable("Illegal bit reference off anonymous def");
1180 /// X.Y - Represent a reference to a subfield of a variable
1181 class FieldInit : public TypedInit {
1182 Init *Rec; // Record we are referring to
1183 StringInit *FieldName; // Field we are accessing
1185 FieldInit(Init *R, StringInit *FN)
1186 : TypedInit(IK_FieldInit, R->getFieldType(FN)), Rec(R), FieldName(FN) {
1187 assert(getType() && "FieldInit with non-record type!");
1191 FieldInit(const FieldInit &) = delete;
1192 FieldInit &operator=(const FieldInit &) = delete;
1194 static bool classof(const Init *I) {
1195 return I->getKind() == IK_FieldInit;
1198 static FieldInit *get(Init *R, StringInit *FN);
1200 Init *getRecord() const { return Rec; }
1201 StringInit *getFieldName() const { return FieldName; }
1203 Init *getBit(unsigned Bit) const override;
1205 Init *resolveReferences(Resolver &R) const override;
1208 std::string getAsString() const override {
1209 return Rec->getAsString() + "." + FieldName->getValue().str();
1213 /// (v a, b) - Represent a DAG tree value. DAG inits are required
1214 /// to have at least one value then a (possibly empty) list of arguments. Each
1215 /// argument can have a name associated with it.
1216 class DagInit final : public TypedInit, public FoldingSetNode,
1217 public TrailingObjects<DagInit, Init *, StringInit *> {
1218 friend TrailingObjects;
1221 StringInit *ValName;
1223 unsigned NumArgNames;
1225 DagInit(Init *V, StringInit *VN, unsigned NumArgs, unsigned NumArgNames)
1226 : TypedInit(IK_DagInit, DagRecTy::get()), Val(V), ValName(VN),
1227 NumArgs(NumArgs), NumArgNames(NumArgNames) {}
1229 size_t numTrailingObjects(OverloadToken<Init *>) const { return NumArgs; }
1232 DagInit(const DagInit &) = delete;
1233 DagInit &operator=(const DagInit &) = delete;
1235 static bool classof(const Init *I) {
1236 return I->getKind() == IK_DagInit;
1239 static DagInit *get(Init *V, StringInit *VN, ArrayRef<Init *> ArgRange,
1240 ArrayRef<StringInit*> NameRange);
1241 static DagInit *get(Init *V, StringInit *VN,
1242 ArrayRef<std::pair<Init*, StringInit*>> Args);
1244 void Profile(FoldingSetNodeID &ID) const;
1246 Init *getOperator() const { return Val; }
1248 StringInit *getName() const { return ValName; }
1250 StringRef getNameStr() const {
1251 return ValName ? ValName->getValue() : StringRef();
1254 unsigned getNumArgs() const { return NumArgs; }
1256 Init *getArg(unsigned Num) const {
1257 assert(Num < NumArgs && "Arg number out of range!");
1258 return getTrailingObjects<Init *>()[Num];
1261 StringInit *getArgName(unsigned Num) const {
1262 assert(Num < NumArgNames && "Arg number out of range!");
1263 return getTrailingObjects<StringInit *>()[Num];
1266 StringRef getArgNameStr(unsigned Num) const {
1267 StringInit *Init = getArgName(Num);
1268 return Init ? Init->getValue() : StringRef();
1271 ArrayRef<Init *> getArgs() const {
1272 return makeArrayRef(getTrailingObjects<Init *>(), NumArgs);
1275 ArrayRef<StringInit *> getArgNames() const {
1276 return makeArrayRef(getTrailingObjects<StringInit *>(), NumArgNames);
1279 Init *resolveReferences(Resolver &R) const override;
1281 bool isConcrete() const override;
1282 std::string getAsString() const override;
1284 using const_arg_iterator = SmallVectorImpl<Init*>::const_iterator;
1285 using const_name_iterator = SmallVectorImpl<StringInit*>::const_iterator;
1287 inline const_arg_iterator arg_begin() const { return getArgs().begin(); }
1288 inline const_arg_iterator arg_end () const { return getArgs().end(); }
1290 inline size_t arg_size () const { return NumArgs; }
1291 inline bool arg_empty() const { return NumArgs == 0; }
1293 inline const_name_iterator name_begin() const { return getArgNames().begin();}
1294 inline const_name_iterator name_end () const { return getArgNames().end(); }
1296 inline size_t name_size () const { return NumArgNames; }
1297 inline bool name_empty() const { return NumArgNames == 0; }
1299 Init *getBit(unsigned Bit) const override {
1300 llvm_unreachable("Illegal bit reference off dag");
1304 //===----------------------------------------------------------------------===//
1305 // High-Level Classes
1306 //===----------------------------------------------------------------------===//
1309 friend class Record;
1312 PointerIntPair<RecTy *, 1, bool> TyAndPrefix;
1316 RecordVal(Init *N, RecTy *T, bool P);
1318 StringRef getName() const;
1319 Init *getNameInit() const { return Name; }
1321 std::string getNameInitAsString() const {
1322 return getNameInit()->getAsUnquotedString();
1325 bool getPrefix() const { return TyAndPrefix.getInt(); }
1326 RecTy *getType() const { return TyAndPrefix.getPointer(); }
1327 Init *getValue() const { return Value; }
1329 bool setValue(Init *V);
1332 void print(raw_ostream &OS, bool PrintSem = true) const;
1335 inline raw_ostream &operator<<(raw_ostream &OS, const RecordVal &RV) {
1336 RV.print(OS << " ");
1341 static unsigned LastID;
1344 // Location where record was instantiated, followed by the location of
1345 // multiclass prototypes used.
1346 SmallVector<SMLoc, 4> Locs;
1347 SmallVector<Init *, 0> TemplateArgs;
1348 SmallVector<RecordVal, 0> Values;
1350 // All superclasses in the inheritance forest in reverse preorder (yes, it
1351 // must be a forest; diamond-shaped inheritance is not allowed).
1352 SmallVector<std::pair<Record *, SMRange>, 0> SuperClasses;
1354 // Tracks Record instances. Not owned by Record.
1355 RecordKeeper &TrackedRecords;
1357 DefInit *TheInit = nullptr;
1359 // Unique record ID.
1368 // Constructs a record.
1369 explicit Record(Init *N, ArrayRef<SMLoc> locs, RecordKeeper &records,
1370 bool Anonymous = false) :
1371 Name(N), Locs(locs.begin(), locs.end()), TrackedRecords(records),
1372 ID(LastID++), IsAnonymous(Anonymous) {
1376 explicit Record(StringRef N, ArrayRef<SMLoc> locs, RecordKeeper &records,
1377 bool Anonymous = false)
1378 : Record(StringInit::get(N), locs, records, Anonymous) {}
1380 // When copy-constructing a Record, we must still guarantee a globally unique
1381 // ID number. Don't copy TheInit either since it's owned by the original
1382 // record. All other fields can be copied normally.
1383 Record(const Record &O) :
1384 Name(O.Name), Locs(O.Locs), TemplateArgs(O.TemplateArgs),
1385 Values(O.Values), SuperClasses(O.SuperClasses),
1386 TrackedRecords(O.TrackedRecords), ID(LastID++),
1387 IsAnonymous(O.IsAnonymous) { }
1389 static unsigned getNewUID() { return LastID++; }
1391 unsigned getID() const { return ID; }
1393 StringRef getName() const { return cast<StringInit>(Name)->getValue(); }
1395 Init *getNameInit() const {
1399 const std::string getNameInitAsString() const {
1400 return getNameInit()->getAsUnquotedString();
1403 void setName(Init *Name); // Also updates RecordKeeper.
1405 ArrayRef<SMLoc> getLoc() const { return Locs; }
1407 /// get the corresponding DefInit.
1408 DefInit *getDefInit();
1410 ArrayRef<Init *> getTemplateArgs() const {
1411 return TemplateArgs;
1414 ArrayRef<RecordVal> getValues() const { return Values; }
1416 ArrayRef<std::pair<Record *, SMRange>> getSuperClasses() const {
1417 return SuperClasses;
1420 /// Append the direct super classes of this record to Classes.
1421 void getDirectSuperClasses(SmallVectorImpl<Record *> &Classes) const;
1423 bool isTemplateArg(Init *Name) const {
1424 for (Init *TA : TemplateArgs)
1425 if (TA == Name) return true;
1429 const RecordVal *getValue(const Init *Name) const {
1430 for (const RecordVal &Val : Values)
1431 if (Val.Name == Name) return &Val;
1435 const RecordVal *getValue(StringRef Name) const {
1436 return getValue(StringInit::get(Name));
1439 RecordVal *getValue(const Init *Name) {
1440 return const_cast<RecordVal *>(static_cast<const Record *>(this)->getValue(Name));
1443 RecordVal *getValue(StringRef Name) {
1444 return const_cast<RecordVal *>(static_cast<const Record *>(this)->getValue(Name));
1447 void addTemplateArg(Init *Name) {
1448 assert(!isTemplateArg(Name) && "Template arg already defined!");
1449 TemplateArgs.push_back(Name);
1452 void addValue(const RecordVal &RV) {
1453 assert(getValue(RV.getNameInit()) == nullptr && "Value already added!");
1454 Values.push_back(RV);
1455 if (Values.size() > 1)
1456 // Keep NAME at the end of the list. It makes record dumps a
1457 // bit prettier and allows TableGen tests to be written more
1458 // naturally. Tests can use CHECK-NEXT to look for Record
1459 // fields they expect to see after a def. They can't do that if
1460 // NAME is the first Record field.
1461 std::swap(Values[Values.size() - 2], Values[Values.size() - 1]);
1464 void removeValue(Init *Name) {
1465 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1466 if (Values[i].getNameInit() == Name) {
1467 Values.erase(Values.begin()+i);
1470 llvm_unreachable("Cannot remove an entry that does not exist!");
1473 void removeValue(StringRef Name) {
1474 removeValue(StringInit::get(Name));
1477 bool isSubClassOf(const Record *R) const {
1478 for (const auto &SCPair : SuperClasses)
1479 if (SCPair.first == R)
1484 bool isSubClassOf(StringRef Name) const {
1485 for (const auto &SCPair : SuperClasses) {
1486 if (const auto *SI = dyn_cast<StringInit>(SCPair.first->getNameInit())) {
1487 if (SI->getValue() == Name)
1489 } else if (SCPair.first->getNameInitAsString() == Name) {
1496 void addSuperClass(Record *R, SMRange Range) {
1497 assert(!isSubClassOf(R) && "Already subclassing record!");
1498 SuperClasses.push_back(std::make_pair(R, Range));
1501 /// If there are any field references that refer to fields
1502 /// that have been filled in, we can propagate the values now.
1503 void resolveReferences();
1505 /// Apply the resolver to the name of the record as well as to the
1506 /// initializers of all fields of the record except SkipVal.
1508 /// The resolver should not resolve any of the fields itself, to avoid
1509 /// recursion / infinite loops.
1510 void resolveReferences(Resolver &R, const RecordVal *SkipVal = nullptr);
1512 /// If anything in this record refers to RV, replace the
1513 /// reference to RV with the RHS of RV. If RV is null, we resolve all
1514 /// possible references.
1515 void resolveReferencesTo(const RecordVal *RV);
1517 RecordKeeper &getRecords() const {
1518 return TrackedRecords;
1521 bool isAnonymous() const {
1525 void print(raw_ostream &OS) const;
1528 //===--------------------------------------------------------------------===//
1529 // High-level methods useful to tablegen back-ends
1532 /// Return the initializer for a value with the specified name,
1533 /// or throw an exception if the field does not exist.
1534 Init *getValueInit(StringRef FieldName) const;
1536 /// Return true if the named field is unset.
1537 bool isValueUnset(StringRef FieldName) const {
1538 return isa<UnsetInit>(getValueInit(FieldName));
1541 /// This method looks up the specified field and returns
1542 /// its value as a string, throwing an exception if the field does not exist
1543 /// or if the value is not a string.
1544 StringRef getValueAsString(StringRef FieldName) const;
1546 /// This method looks up the specified field and returns
1547 /// its value as a BitsInit, throwing an exception if the field does not exist
1548 /// or if the value is not the right type.
1549 BitsInit *getValueAsBitsInit(StringRef FieldName) const;
1551 /// This method looks up the specified field and returns
1552 /// its value as a ListInit, throwing an exception if the field does not exist
1553 /// or if the value is not the right type.
1554 ListInit *getValueAsListInit(StringRef FieldName) const;
1556 /// This method looks up the specified field and
1557 /// returns its value as a vector of records, throwing an exception if the
1558 /// field does not exist or if the value is not the right type.
1559 std::vector<Record*> getValueAsListOfDefs(StringRef FieldName) const;
1561 /// This method looks up the specified field and
1562 /// returns its value as a vector of integers, throwing an exception if the
1563 /// field does not exist or if the value is not the right type.
1564 std::vector<int64_t> getValueAsListOfInts(StringRef FieldName) const;
1566 /// This method looks up the specified field and
1567 /// returns its value as a vector of strings, throwing an exception if the
1568 /// field does not exist or if the value is not the right type.
1569 std::vector<StringRef> getValueAsListOfStrings(StringRef FieldName) const;
1571 /// This method looks up the specified field and returns its
1572 /// value as a Record, throwing an exception if the field does not exist or if
1573 /// the value is not the right type.
1574 Record *getValueAsDef(StringRef FieldName) const;
1576 /// This method looks up the specified field and returns its
1577 /// value as a bit, throwing an exception if the field does not exist or if
1578 /// the value is not the right type.
1579 bool getValueAsBit(StringRef FieldName) const;
1581 /// This method looks up the specified field and
1582 /// returns its value as a bit. If the field is unset, sets Unset to true and
1584 bool getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const;
1586 /// This method looks up the specified field and returns its
1587 /// value as an int64_t, throwing an exception if the field does not exist or
1588 /// if the value is not the right type.
1589 int64_t getValueAsInt(StringRef FieldName) const;
1591 /// This method looks up the specified field and returns its
1592 /// value as an Dag, throwing an exception if the field does not exist or if
1593 /// the value is not the right type.
1594 DagInit *getValueAsDag(StringRef FieldName) const;
1597 raw_ostream &operator<<(raw_ostream &OS, const Record &R);
1600 Record Rec; // Placeholder for template args and Name.
1601 using RecordVector = std::vector<std::unique_ptr<Record>>;
1602 RecordVector DefPrototypes;
1606 MultiClass(StringRef Name, SMLoc Loc, RecordKeeper &Records) :
1607 Rec(Name, Loc, Records) {}
1610 class RecordKeeper {
1611 friend class RecordRecTy;
1612 using RecordMap = std::map<std::string, std::unique_ptr<Record>>;
1613 RecordMap Classes, Defs;
1614 FoldingSet<RecordRecTy> RecordTypePool;
1615 unsigned AnonCounter = 0;
1618 const RecordMap &getClasses() const { return Classes; }
1619 const RecordMap &getDefs() const { return Defs; }
1621 Record *getClass(StringRef Name) const {
1622 auto I = Classes.find(Name);
1623 return I == Classes.end() ? nullptr : I->second.get();
1626 Record *getDef(StringRef Name) const {
1627 auto I = Defs.find(Name);
1628 return I == Defs.end() ? nullptr : I->second.get();
1631 void addClass(std::unique_ptr<Record> R) {
1632 bool Ins = Classes.insert(std::make_pair(R->getName(),
1633 std::move(R))).second;
1635 assert(Ins && "Class already exists");
1638 void addDef(std::unique_ptr<Record> R) {
1639 bool Ins = Defs.insert(std::make_pair(R->getName(),
1640 std::move(R))).second;
1642 assert(Ins && "Record already exists");
1645 Init *getNewAnonymousName();
1647 //===--------------------------------------------------------------------===//
1648 // High-level helper methods, useful for tablegen backends...
1650 /// This method returns all concrete definitions
1651 /// that derive from the specified class name. A class with the specified
1652 /// name must exist.
1653 std::vector<Record *> getAllDerivedDefinitions(StringRef ClassName) const;
1658 /// Sorting predicate to sort record pointers by name.
1660 bool operator()(const Record *Rec1, const Record *Rec2) const {
1661 return StringRef(Rec1->getName()).compare_numeric(Rec2->getName()) < 0;
1665 /// Sorting predicate to sort record pointers by their
1666 /// unique ID. If you just need a deterministic order, use this, since it
1667 /// just compares two `unsigned`; the other sorting predicates require
1668 /// string manipulation.
1669 struct LessRecordByID {
1670 bool operator()(const Record *LHS, const Record *RHS) const {
1671 return LHS->getID() < RHS->getID();
1675 /// Sorting predicate to sort record pointers by their
1677 struct LessRecordFieldName {
1678 bool operator()(const Record *Rec1, const Record *Rec2) const {
1679 return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name");
1683 struct LessRecordRegister {
1684 static bool ascii_isdigit(char x) { return x >= '0' && x <= '9'; }
1686 struct RecordParts {
1687 SmallVector<std::pair< bool, StringRef>, 4> Parts;
1689 RecordParts(StringRef Rec) {
1694 const char *Start = Rec.data();
1695 const char *Curr = Start;
1696 bool isDigitPart = ascii_isdigit(Curr[0]);
1697 for (size_t I = 0, E = Rec.size(); I != E; ++I, ++Len) {
1698 bool isDigit = ascii_isdigit(Curr[I]);
1699 if (isDigit != isDigitPart) {
1700 Parts.push_back(std::make_pair(isDigitPart, StringRef(Start, Len)));
1703 isDigitPart = ascii_isdigit(Curr[I]);
1706 // Push the last part.
1707 Parts.push_back(std::make_pair(isDigitPart, StringRef(Start, Len)));
1710 size_t size() { return Parts.size(); }
1712 std::pair<bool, StringRef> getPart(size_t i) {
1713 assert (i < Parts.size() && "Invalid idx!");
1718 bool operator()(const Record *Rec1, const Record *Rec2) const {
1719 RecordParts LHSParts(StringRef(Rec1->getName()));
1720 RecordParts RHSParts(StringRef(Rec2->getName()));
1722 size_t LHSNumParts = LHSParts.size();
1723 size_t RHSNumParts = RHSParts.size();
1724 assert (LHSNumParts && RHSNumParts && "Expected at least one part!");
1726 if (LHSNumParts != RHSNumParts)
1727 return LHSNumParts < RHSNumParts;
1729 // We expect the registers to be of the form [_a-zA-Z]+([0-9]*[_a-zA-Z]*)*.
1730 for (size_t I = 0, E = LHSNumParts; I < E; I+=2) {
1731 std::pair<bool, StringRef> LHSPart = LHSParts.getPart(I);
1732 std::pair<bool, StringRef> RHSPart = RHSParts.getPart(I);
1733 // Expect even part to always be alpha.
1734 assert (LHSPart.first == false && RHSPart.first == false &&
1735 "Expected both parts to be alpha.");
1736 if (int Res = LHSPart.second.compare(RHSPart.second))
1739 for (size_t I = 1, E = LHSNumParts; I < E; I+=2) {
1740 std::pair<bool, StringRef> LHSPart = LHSParts.getPart(I);
1741 std::pair<bool, StringRef> RHSPart = RHSParts.getPart(I);
1742 // Expect odd part to always be numeric.
1743 assert (LHSPart.first == true && RHSPart.first == true &&
1744 "Expected both parts to be numeric.");
1745 if (LHSPart.second.size() != RHSPart.second.size())
1746 return LHSPart.second.size() < RHSPart.second.size();
1748 unsigned LHSVal, RHSVal;
1750 bool LHSFailed = LHSPart.second.getAsInteger(10, LHSVal); (void)LHSFailed;
1751 assert(!LHSFailed && "Unable to convert LHS to integer.");
1752 bool RHSFailed = RHSPart.second.getAsInteger(10, RHSVal); (void)RHSFailed;
1753 assert(!RHSFailed && "Unable to convert RHS to integer.");
1755 if (LHSVal != RHSVal)
1756 return LHSVal < RHSVal;
1758 return LHSNumParts < RHSNumParts;
1762 raw_ostream &operator<<(raw_ostream &OS, const RecordKeeper &RK);
1764 /// Return an Init with a qualifier prefix referring
1765 /// to CurRec's name.
1766 Init *QualifyName(Record &CurRec, MultiClass *CurMultiClass,
1767 Init *Name, StringRef Scoper);
1769 //===----------------------------------------------------------------------===//
1771 //===----------------------------------------------------------------------===//
1773 /// Interface for looking up the initializer for a variable name, used by
1774 /// Init::resolveReferences.
1779 explicit Resolver(Record *CurRec) : CurRec(CurRec) {}
1780 virtual ~Resolver() {}
1782 Record *getCurrentRecord() const { return CurRec; }
1784 /// Return the initializer for the given variable name (should normally be a
1785 /// StringInit), or nullptr if the name could not be resolved.
1786 virtual Init *resolve(Init *VarName) = 0;
1788 // Whether bits in a BitsInit should stay unresolved if resolving them would
1789 // result in a ? (UnsetInit). This behavior is used to represent instruction
1790 // encodings by keeping references to unset variables within a record.
1791 virtual bool keepUnsetBits() const { return false; }
1794 /// Resolve arbitrary mappings.
1795 class MapResolver final : public Resolver {
1796 struct MappedValue {
1800 MappedValue() : V(nullptr), Resolved(false) {}
1801 MappedValue(Init *V, bool Resolved) : V(V), Resolved(Resolved) {}
1804 DenseMap<Init *, MappedValue> Map;
1807 explicit MapResolver(Record *CurRec = nullptr) : Resolver(CurRec) {}
1809 void set(Init *Key, Init *Value) { Map[Key] = {Value, false}; }
1811 Init *resolve(Init *VarName) override;
1814 /// Resolve all variables from a record except for unset variables.
1815 class RecordResolver final : public Resolver {
1816 DenseMap<Init *, Init *> Cache;
1817 SmallVector<Init *, 4> Stack;
1820 explicit RecordResolver(Record &R) : Resolver(&R) {}
1822 Init *resolve(Init *VarName) override;
1824 bool keepUnsetBits() const override { return true; }
1827 /// Resolve all references to a specific RecordVal.
1829 // TODO: This is used for resolving references to template arguments, in a
1830 // rather inefficient way. Change those uses to resolve all template
1831 // arguments simultaneously and get rid of this class.
1832 class RecordValResolver final : public Resolver {
1833 const RecordVal *RV;
1836 explicit RecordValResolver(Record &R, const RecordVal *RV)
1837 : Resolver(&R), RV(RV) {}
1839 Init *resolve(Init *VarName) override {
1840 if (VarName == RV->getNameInit())
1841 return RV->getValue();
1846 /// Delegate resolving to a sub-resolver, but shadow some variable names.
1847 class ShadowResolver final : public Resolver {
1849 DenseSet<Init *> Shadowed;
1852 explicit ShadowResolver(Resolver &R) : Resolver(R.getCurrentRecord()), R(R) {}
1854 void addShadow(Init *Key) { Shadowed.insert(Key); }
1856 Init *resolve(Init *VarName) override {
1857 if (Shadowed.count(VarName))
1859 return R.resolve(VarName);
1863 /// (Optionally) delegate resolving to a sub-resolver, and keep track whether
1864 /// there were unresolved references.
1865 class TrackUnresolvedResolver final : public Resolver {
1867 bool FoundUnresolved = false;
1870 explicit TrackUnresolvedResolver(Resolver *R = nullptr)
1871 : Resolver(R ? R->getCurrentRecord() : nullptr), R(R) {}
1873 bool foundUnresolved() const { return FoundUnresolved; }
1875 Init *resolve(Init *VarName) override;
1878 } // end namespace llvm
1880 #endif // LLVM_TABLEGEN_RECORD_H