1 //===- llvm/TableGen/Record.h - Classes for Table Records -------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines the main TableGen data structures, including the TableGen
10 // types, values, and high-level data structures.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_TABLEGEN_RECORD_H
15 #define LLVM_TABLEGEN_RECORD_H
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"
50 //===----------------------------------------------------------------------===//
52 //===----------------------------------------------------------------------===//
56 /// Subclass discriminator (for dyn_cast<> et al.)
70 ListRecTy
*ListTy
= nullptr;
73 RecTy(RecTyKind K
) : Kind(K
) {}
74 virtual ~RecTy() = default;
76 RecTyKind
getRecTyKind() const { return Kind
; }
78 virtual std::string
getAsString() const = 0;
79 void print(raw_ostream
&OS
) const { OS
<< getAsString(); }
82 /// Return true if all values of 'this' type can be converted to the specified
84 virtual bool typeIsConvertibleTo(const RecTy
*RHS
) const;
86 /// Return true if 'this' type is equal to or a subtype of RHS. For example,
87 /// a bit set is not an int, but they are convertible.
88 virtual bool typeIsA(const RecTy
*RHS
) const;
90 /// Returns the type representing list<this>.
91 ListRecTy
*getListTy();
94 inline raw_ostream
&operator<<(raw_ostream
&OS
, const RecTy
&Ty
) {
99 /// 'bit' - Represent a single bit
100 class BitRecTy
: public RecTy
{
101 static BitRecTy Shared
;
103 BitRecTy() : RecTy(BitRecTyKind
) {}
106 static bool classof(const RecTy
*RT
) {
107 return RT
->getRecTyKind() == BitRecTyKind
;
110 static BitRecTy
*get() { return &Shared
; }
112 std::string
getAsString() const override
{ return "bit"; }
114 bool typeIsConvertibleTo(const RecTy
*RHS
) const override
;
117 /// 'bits<n>' - Represent a fixed number of bits
118 class BitsRecTy
: public RecTy
{
121 explicit BitsRecTy(unsigned Sz
) : RecTy(BitsRecTyKind
), Size(Sz
) {}
124 static bool classof(const RecTy
*RT
) {
125 return RT
->getRecTyKind() == BitsRecTyKind
;
128 static BitsRecTy
*get(unsigned Sz
);
130 unsigned getNumBits() const { return Size
; }
132 std::string
getAsString() const override
;
134 bool typeIsConvertibleTo(const RecTy
*RHS
) const override
;
136 bool typeIsA(const RecTy
*RHS
) const override
;
139 /// 'code' - Represent a code fragment
140 class CodeRecTy
: public RecTy
{
141 static CodeRecTy Shared
;
143 CodeRecTy() : RecTy(CodeRecTyKind
) {}
146 static bool classof(const RecTy
*RT
) {
147 return RT
->getRecTyKind() == CodeRecTyKind
;
150 static CodeRecTy
*get() { return &Shared
; }
152 std::string
getAsString() const override
{ return "code"; }
154 bool typeIsConvertibleTo(const RecTy
*RHS
) const override
;
157 /// 'int' - Represent an integer value of no particular size
158 class IntRecTy
: public RecTy
{
159 static IntRecTy Shared
;
161 IntRecTy() : RecTy(IntRecTyKind
) {}
164 static bool classof(const RecTy
*RT
) {
165 return RT
->getRecTyKind() == IntRecTyKind
;
168 static IntRecTy
*get() { return &Shared
; }
170 std::string
getAsString() const override
{ return "int"; }
172 bool typeIsConvertibleTo(const RecTy
*RHS
) const override
;
175 /// 'string' - Represent an string value
176 class StringRecTy
: public RecTy
{
177 static StringRecTy Shared
;
179 StringRecTy() : RecTy(StringRecTyKind
) {}
182 static bool classof(const RecTy
*RT
) {
183 return RT
->getRecTyKind() == StringRecTyKind
;
186 static StringRecTy
*get() { return &Shared
; }
188 std::string
getAsString() const override
;
190 bool typeIsConvertibleTo(const RecTy
*RHS
) const override
;
193 /// 'list<Ty>' - Represent a list of values, all of which must be of
194 /// the specified type.
195 class ListRecTy
: public RecTy
{
196 friend ListRecTy
*RecTy::getListTy();
200 explicit ListRecTy(RecTy
*T
) : RecTy(ListRecTyKind
), Ty(T
) {}
203 static bool classof(const RecTy
*RT
) {
204 return RT
->getRecTyKind() == ListRecTyKind
;
207 static ListRecTy
*get(RecTy
*T
) { return T
->getListTy(); }
208 RecTy
*getElementType() const { return Ty
; }
210 std::string
getAsString() const override
;
212 bool typeIsConvertibleTo(const RecTy
*RHS
) const override
;
214 bool typeIsA(const RecTy
*RHS
) const override
;
217 /// 'dag' - Represent a dag fragment
218 class DagRecTy
: public RecTy
{
219 static DagRecTy Shared
;
221 DagRecTy() : RecTy(DagRecTyKind
) {}
224 static bool classof(const RecTy
*RT
) {
225 return RT
->getRecTyKind() == DagRecTyKind
;
228 static DagRecTy
*get() { return &Shared
; }
230 std::string
getAsString() const override
;
233 /// '[classname]' - Type of record values that have zero or more superclasses.
235 /// The list of superclasses is non-redundant, i.e. only contains classes that
236 /// are not the superclass of some other listed class.
237 class RecordRecTy final
: public RecTy
, public FoldingSetNode
,
238 public TrailingObjects
<RecordRecTy
, Record
*> {
243 explicit RecordRecTy(unsigned Num
)
244 : RecTy(RecordRecTyKind
), NumClasses(Num
) {}
247 RecordRecTy(const RecordRecTy
&) = delete;
248 RecordRecTy
&operator=(const RecordRecTy
&) = delete;
250 // Do not use sized deallocation due to trailing objects.
251 void operator delete(void *p
) { ::operator delete(p
); }
253 static bool classof(const RecTy
*RT
) {
254 return RT
->getRecTyKind() == RecordRecTyKind
;
257 /// Get the record type with the given non-redundant list of superclasses.
258 static RecordRecTy
*get(ArrayRef
<Record
*> Classes
);
260 void Profile(FoldingSetNodeID
&ID
) const;
262 ArrayRef
<Record
*> getClasses() const {
263 return makeArrayRef(getTrailingObjects
<Record
*>(), NumClasses
);
266 using const_record_iterator
= Record
* const *;
268 const_record_iterator
classes_begin() const { return getClasses().begin(); }
269 const_record_iterator
classes_end() const { return getClasses().end(); }
271 std::string
getAsString() const override
;
273 bool isSubClassOf(Record
*Class
) const;
274 bool typeIsConvertibleTo(const RecTy
*RHS
) const override
;
276 bool typeIsA(const RecTy
*RHS
) const override
;
279 /// Find a common type that T1 and T2 convert to.
280 /// Return 0 if no such type exists.
281 RecTy
*resolveTypes(RecTy
*T1
, RecTy
*T2
);
283 //===----------------------------------------------------------------------===//
284 // Initializer Classes
285 //===----------------------------------------------------------------------===//
289 /// Discriminator enum (for isa<>, dyn_cast<>, et al.)
291 /// This enum is laid out by a preorder traversal of the inheritance
292 /// hierarchy, and does not contain an entry for abstract classes, as per
293 /// the recommendation in docs/HowToSetUpLLVMStyleRTTI.rst.
295 /// We also explicitly include "first" and "last" values for each
296 /// interior node of the inheritance tree, to make it easier to read the
297 /// corresponding classof().
299 /// We could pack these a bit tighter by not having the IK_FirstXXXInit
300 /// and IK_LastXXXInit be their own values, but that would degrade
301 /// readability for really no benefit.
302 enum InitKind
: uint8_t {
303 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
{
628 explicit CodeInit(StringRef V
, const SMLoc
&Loc
)
629 : TypedInit(IK_CodeInit
, static_cast<RecTy
*>(CodeRecTy::get())),
630 Value(V
), Loc(Loc
) {}
633 CodeInit(const StringInit
&) = delete;
634 CodeInit
&operator=(const StringInit
&) = delete;
636 static bool classof(const Init
*I
) {
637 return I
->getKind() == IK_CodeInit
;
640 static CodeInit
*get(StringRef
, const SMLoc
&Loc
);
642 StringRef
getValue() const { return Value
; }
643 const SMLoc
&getLoc() const { return Loc
; }
645 Init
*convertInitializerTo(RecTy
*Ty
) const override
;
647 bool isConcrete() const override
{ return true; }
648 std::string
getAsString() const override
{
649 return "[{" + Value
.str() + "}]";
652 std::string
getAsUnquotedString() const override
{ return Value
; }
654 Init
*getBit(unsigned Bit
) const override
{
655 llvm_unreachable("Illegal bit reference off string");
659 /// [AL, AH, CL] - Represent a list of defs
661 class ListInit final
: public TypedInit
, public FoldingSetNode
,
662 public TrailingObjects
<ListInit
, Init
*> {
666 using const_iterator
= Init
*const *;
669 explicit ListInit(unsigned N
, RecTy
*EltTy
)
670 : TypedInit(IK_ListInit
, ListRecTy::get(EltTy
)), NumValues(N
) {}
673 ListInit(const ListInit
&) = delete;
674 ListInit
&operator=(const ListInit
&) = delete;
676 // Do not use sized deallocation due to trailing objects.
677 void operator delete(void *p
) { ::operator delete(p
); }
679 static bool classof(const Init
*I
) {
680 return I
->getKind() == IK_ListInit
;
682 static ListInit
*get(ArrayRef
<Init
*> Range
, RecTy
*EltTy
);
684 void Profile(FoldingSetNodeID
&ID
) const;
686 Init
*getElement(unsigned i
) const {
687 assert(i
< NumValues
&& "List element index out of range!");
688 return getTrailingObjects
<Init
*>()[i
];
690 RecTy
*getElementType() const {
691 return cast
<ListRecTy
>(getType())->getElementType();
694 Record
*getElementAsRecord(unsigned i
) const;
696 Init
*convertInitListSlice(ArrayRef
<unsigned> Elements
) const override
;
698 Init
*convertInitializerTo(RecTy
*Ty
) const override
;
700 /// This method is used by classes that refer to other
701 /// variables which may not be defined at the time they expression is formed.
702 /// If a value is set for the variable later, this method will be called on
703 /// users of the value to allow the value to propagate out.
705 Init
*resolveReferences(Resolver
&R
) const override
;
707 bool isConcrete() const override
;
708 std::string
getAsString() const override
;
710 ArrayRef
<Init
*> getValues() const {
711 return makeArrayRef(getTrailingObjects
<Init
*>(), NumValues
);
714 const_iterator
begin() const { return getTrailingObjects
<Init
*>(); }
715 const_iterator
end () const { return begin() + NumValues
; }
717 size_t size () const { return NumValues
; }
718 bool empty() const { return NumValues
== 0; }
720 Init
*getBit(unsigned Bit
) const override
{
721 llvm_unreachable("Illegal bit reference off list");
725 /// Base class for operators
727 class OpInit
: public TypedInit
{
729 explicit OpInit(InitKind K
, RecTy
*Type
, uint8_t Opc
)
730 : TypedInit(K
, Type
, Opc
) {}
733 OpInit(const OpInit
&) = delete;
734 OpInit
&operator=(OpInit
&) = delete;
736 static bool classof(const Init
*I
) {
737 return I
->getKind() >= IK_FirstOpInit
&&
738 I
->getKind() <= IK_LastOpInit
;
741 // Clone - Clone this operator, replacing arguments with the new list
742 virtual OpInit
*clone(ArrayRef
<Init
*> Operands
) const = 0;
744 virtual unsigned getNumOperands() const = 0;
745 virtual Init
*getOperand(unsigned i
) const = 0;
747 Init
*getBit(unsigned Bit
) const override
;
750 /// !op (X) - Transform an init.
752 class UnOpInit
: public OpInit
, public FoldingSetNode
{
754 enum UnaryOp
: uint8_t { CAST
, HEAD
, TAIL
, SIZE
, EMPTY
};
759 UnOpInit(UnaryOp opc
, Init
*lhs
, RecTy
*Type
)
760 : OpInit(IK_UnOpInit
, Type
, opc
), LHS(lhs
) {}
763 UnOpInit(const UnOpInit
&) = delete;
764 UnOpInit
&operator=(const UnOpInit
&) = delete;
766 static bool classof(const Init
*I
) {
767 return I
->getKind() == IK_UnOpInit
;
770 static UnOpInit
*get(UnaryOp opc
, Init
*lhs
, RecTy
*Type
);
772 void Profile(FoldingSetNodeID
&ID
) const;
774 // Clone - Clone this operator, replacing arguments with the new list
775 OpInit
*clone(ArrayRef
<Init
*> Operands
) const override
{
776 assert(Operands
.size() == 1 &&
777 "Wrong number of operands for unary operation");
778 return UnOpInit::get(getOpcode(), *Operands
.begin(), getType());
781 unsigned getNumOperands() const override
{ return 1; }
783 Init
*getOperand(unsigned i
) const override
{
784 assert(i
== 0 && "Invalid operand id for unary operator");
788 UnaryOp
getOpcode() const { return (UnaryOp
)Opc
; }
789 Init
*getOperand() const { return LHS
; }
791 // Fold - If possible, fold this to a simpler init. Return this if not
793 Init
*Fold(Record
*CurRec
, bool IsFinal
= false) const;
795 Init
*resolveReferences(Resolver
&R
) const override
;
797 std::string
getAsString() const override
;
800 /// !op (X, Y) - Combine two inits.
801 class BinOpInit
: public OpInit
, public FoldingSetNode
{
803 enum BinaryOp
: uint8_t { ADD
, MUL
, AND
, OR
, SHL
, SRA
, SRL
, LISTCONCAT
,
804 LISTSPLAT
, STRCONCAT
, CONCAT
, EQ
, NE
, LE
, LT
, GE
,
810 BinOpInit(BinaryOp opc
, Init
*lhs
, Init
*rhs
, RecTy
*Type
) :
811 OpInit(IK_BinOpInit
, Type
, opc
), LHS(lhs
), RHS(rhs
) {}
814 BinOpInit(const BinOpInit
&) = delete;
815 BinOpInit
&operator=(const BinOpInit
&) = delete;
817 static bool classof(const Init
*I
) {
818 return I
->getKind() == IK_BinOpInit
;
821 static BinOpInit
*get(BinaryOp opc
, Init
*lhs
, Init
*rhs
,
823 static Init
*getStrConcat(Init
*lhs
, Init
*rhs
);
824 static Init
*getListConcat(TypedInit
*lhs
, Init
*rhs
);
825 static Init
*getListSplat(TypedInit
*lhs
, Init
*rhs
);
827 void Profile(FoldingSetNodeID
&ID
) const;
829 // Clone - Clone this operator, replacing arguments with the new list
830 OpInit
*clone(ArrayRef
<Init
*> Operands
) const override
{
831 assert(Operands
.size() == 2 &&
832 "Wrong number of operands for binary operation");
833 return BinOpInit::get(getOpcode(), Operands
[0], Operands
[1], getType());
836 unsigned getNumOperands() const override
{ return 2; }
837 Init
*getOperand(unsigned i
) const override
{
839 default: llvm_unreachable("Invalid operand id for binary operator");
840 case 0: return getLHS();
841 case 1: return getRHS();
845 BinaryOp
getOpcode() const { return (BinaryOp
)Opc
; }
846 Init
*getLHS() const { return LHS
; }
847 Init
*getRHS() const { return RHS
; }
849 // Fold - If possible, fold this to a simpler init. Return this if not
851 Init
*Fold(Record
*CurRec
) const;
853 Init
*resolveReferences(Resolver
&R
) const override
;
855 std::string
getAsString() const override
;
858 /// !op (X, Y, Z) - Combine two inits.
859 class TernOpInit
: public OpInit
, public FoldingSetNode
{
861 enum TernaryOp
: uint8_t { SUBST
, FOREACH
, IF
, DAG
};
864 Init
*LHS
, *MHS
, *RHS
;
866 TernOpInit(TernaryOp opc
, Init
*lhs
, Init
*mhs
, Init
*rhs
,
868 OpInit(IK_TernOpInit
, Type
, opc
), LHS(lhs
), MHS(mhs
), RHS(rhs
) {}
871 TernOpInit(const TernOpInit
&) = delete;
872 TernOpInit
&operator=(const TernOpInit
&) = delete;
874 static bool classof(const Init
*I
) {
875 return I
->getKind() == IK_TernOpInit
;
878 static TernOpInit
*get(TernaryOp opc
, Init
*lhs
,
879 Init
*mhs
, Init
*rhs
,
882 void Profile(FoldingSetNodeID
&ID
) const;
884 // Clone - Clone this operator, replacing arguments with the new list
885 OpInit
*clone(ArrayRef
<Init
*> Operands
) const override
{
886 assert(Operands
.size() == 3 &&
887 "Wrong number of operands for ternary operation");
888 return TernOpInit::get(getOpcode(), Operands
[0], Operands
[1], Operands
[2],
892 unsigned getNumOperands() const override
{ return 3; }
893 Init
*getOperand(unsigned i
) const override
{
895 default: llvm_unreachable("Invalid operand id for ternary operator");
896 case 0: return getLHS();
897 case 1: return getMHS();
898 case 2: return getRHS();
902 TernaryOp
getOpcode() const { return (TernaryOp
)Opc
; }
903 Init
*getLHS() const { return LHS
; }
904 Init
*getMHS() const { return MHS
; }
905 Init
*getRHS() const { return RHS
; }
907 // Fold - If possible, fold this to a simpler init. Return this if not
909 Init
*Fold(Record
*CurRec
) const;
911 bool isComplete() const override
{
912 return LHS
->isComplete() && MHS
->isComplete() && RHS
->isComplete();
915 Init
*resolveReferences(Resolver
&R
) const override
;
917 std::string
getAsString() const override
;
920 /// !cond(condition_1: value1, ... , condition_n: value)
921 /// Selects the first value for which condition is true.
922 /// Otherwise reports an error.
923 class CondOpInit final
: public TypedInit
, public FoldingSetNode
,
924 public TrailingObjects
<CondOpInit
, Init
*> {
928 CondOpInit(unsigned NC
, RecTy
*Type
)
929 : TypedInit(IK_CondOpInit
, Type
),
930 NumConds(NC
), ValType(Type
) {}
932 size_t numTrailingObjects(OverloadToken
<Init
*>) const {
937 CondOpInit(const CondOpInit
&) = delete;
938 CondOpInit
&operator=(const CondOpInit
&) = delete;
940 static bool classof(const Init
*I
) {
941 return I
->getKind() == IK_CondOpInit
;
944 static CondOpInit
*get(ArrayRef
<Init
*> C
, ArrayRef
<Init
*> V
,
947 void Profile(FoldingSetNodeID
&ID
) const;
949 RecTy
*getValType() const { return ValType
; }
951 unsigned getNumConds() const { return NumConds
; }
953 Init
*getCond(unsigned Num
) const {
954 assert(Num
< NumConds
&& "Condition number out of range!");
955 return getTrailingObjects
<Init
*>()[Num
];
958 Init
*getVal(unsigned Num
) const {
959 assert(Num
< NumConds
&& "Val number out of range!");
960 return getTrailingObjects
<Init
*>()[Num
+NumConds
];
963 ArrayRef
<Init
*> getConds() const {
964 return makeArrayRef(getTrailingObjects
<Init
*>(), NumConds
);
967 ArrayRef
<Init
*> getVals() const {
968 return makeArrayRef(getTrailingObjects
<Init
*>()+NumConds
, NumConds
);
971 Init
*Fold(Record
*CurRec
) const;
973 Init
*resolveReferences(Resolver
&R
) const override
;
975 bool isConcrete() const override
;
976 bool isComplete() const override
;
977 std::string
getAsString() const override
;
979 using const_case_iterator
= SmallVectorImpl
<Init
*>::const_iterator
;
980 using const_val_iterator
= SmallVectorImpl
<Init
*>::const_iterator
;
982 inline const_case_iterator
arg_begin() const { return getConds().begin(); }
983 inline const_case_iterator
arg_end () const { return getConds().end(); }
985 inline size_t case_size () const { return NumConds
; }
986 inline bool case_empty() const { return NumConds
== 0; }
988 inline const_val_iterator
name_begin() const { return getVals().begin();}
989 inline const_val_iterator
name_end () const { return getVals().end(); }
991 inline size_t val_size () const { return NumConds
; }
992 inline bool val_empty() const { return NumConds
== 0; }
994 Init
*getBit(unsigned Bit
) const override
;
997 /// !foldl (a, b, expr, start, lst) - Fold over a list.
998 class FoldOpInit
: public TypedInit
, public FoldingSetNode
{
1006 FoldOpInit(Init
*Start
, Init
*List
, Init
*A
, Init
*B
, Init
*Expr
, RecTy
*Type
)
1007 : TypedInit(IK_FoldOpInit
, Type
), Start(Start
), List(List
), A(A
), B(B
),
1011 FoldOpInit(const FoldOpInit
&) = delete;
1012 FoldOpInit
&operator=(const FoldOpInit
&) = delete;
1014 static bool classof(const Init
*I
) { return I
->getKind() == IK_FoldOpInit
; }
1016 static FoldOpInit
*get(Init
*Start
, Init
*List
, Init
*A
, Init
*B
, Init
*Expr
,
1019 void Profile(FoldingSetNodeID
&ID
) const;
1021 // Fold - If possible, fold this to a simpler init. Return this if not
1022 // possible to fold.
1023 Init
*Fold(Record
*CurRec
) const;
1025 bool isComplete() const override
{ return false; }
1027 Init
*resolveReferences(Resolver
&R
) const override
;
1029 Init
*getBit(unsigned Bit
) const override
;
1031 std::string
getAsString() const override
;
1034 /// !isa<type>(expr) - Dynamically determine the type of an expression.
1035 class IsAOpInit
: public TypedInit
, public FoldingSetNode
{
1040 IsAOpInit(RecTy
*CheckType
, Init
*Expr
)
1041 : TypedInit(IK_IsAOpInit
, IntRecTy::get()), CheckType(CheckType
),
1045 IsAOpInit(const IsAOpInit
&) = delete;
1046 IsAOpInit
&operator=(const IsAOpInit
&) = delete;
1048 static bool classof(const Init
*I
) { return I
->getKind() == IK_IsAOpInit
; }
1050 static IsAOpInit
*get(RecTy
*CheckType
, Init
*Expr
);
1052 void Profile(FoldingSetNodeID
&ID
) const;
1054 // Fold - If possible, fold this to a simpler init. Return this if not
1055 // possible to fold.
1058 bool isComplete() const override
{ return false; }
1060 Init
*resolveReferences(Resolver
&R
) const override
;
1062 Init
*getBit(unsigned Bit
) const override
;
1064 std::string
getAsString() const override
;
1067 /// 'Opcode' - Represent a reference to an entire variable object.
1068 class VarInit
: public TypedInit
{
1071 explicit VarInit(Init
*VN
, RecTy
*T
)
1072 : TypedInit(IK_VarInit
, T
), VarName(VN
) {}
1075 VarInit(const VarInit
&) = delete;
1076 VarInit
&operator=(const VarInit
&) = delete;
1078 static bool classof(const Init
*I
) {
1079 return I
->getKind() == IK_VarInit
;
1082 static VarInit
*get(StringRef VN
, RecTy
*T
);
1083 static VarInit
*get(Init
*VN
, RecTy
*T
);
1085 StringRef
getName() const;
1086 Init
*getNameInit() const { return VarName
; }
1088 std::string
getNameInitAsString() const {
1089 return getNameInit()->getAsUnquotedString();
1092 /// This method is used by classes that refer to other
1093 /// variables which may not be defined at the time they expression is formed.
1094 /// If a value is set for the variable later, this method will be called on
1095 /// users of the value to allow the value to propagate out.
1097 Init
*resolveReferences(Resolver
&R
) const override
;
1099 Init
*getBit(unsigned Bit
) const override
;
1101 std::string
getAsString() const override
{ return getName(); }
1104 /// Opcode{0} - Represent access to one bit of a variable or field.
1105 class VarBitInit final
: public TypedInit
{
1109 VarBitInit(TypedInit
*T
, unsigned B
)
1110 : TypedInit(IK_VarBitInit
, BitRecTy::get()), TI(T
), Bit(B
) {
1111 assert(T
->getType() &&
1112 (isa
<IntRecTy
>(T
->getType()) ||
1113 (isa
<BitsRecTy
>(T
->getType()) &&
1114 cast
<BitsRecTy
>(T
->getType())->getNumBits() > B
)) &&
1115 "Illegal VarBitInit expression!");
1119 VarBitInit(const VarBitInit
&) = delete;
1120 VarBitInit
&operator=(const VarBitInit
&) = delete;
1122 static bool classof(const Init
*I
) {
1123 return I
->getKind() == IK_VarBitInit
;
1126 static VarBitInit
*get(TypedInit
*T
, unsigned B
);
1128 Init
*getBitVar() const { return TI
; }
1129 unsigned getBitNum() const { return Bit
; }
1131 std::string
getAsString() const override
;
1132 Init
*resolveReferences(Resolver
&R
) const override
;
1134 Init
*getBit(unsigned B
) const override
{
1135 assert(B
< 1 && "Bit index out of range!");
1136 return const_cast<VarBitInit
*>(this);
1140 /// List[4] - Represent access to one element of a var or
1142 class VarListElementInit
: public TypedInit
{
1146 VarListElementInit(TypedInit
*T
, unsigned E
)
1147 : TypedInit(IK_VarListElementInit
,
1148 cast
<ListRecTy
>(T
->getType())->getElementType()),
1150 assert(T
->getType() && isa
<ListRecTy
>(T
->getType()) &&
1151 "Illegal VarBitInit expression!");
1155 VarListElementInit(const VarListElementInit
&) = delete;
1156 VarListElementInit
&operator=(const VarListElementInit
&) = delete;
1158 static bool classof(const Init
*I
) {
1159 return I
->getKind() == IK_VarListElementInit
;
1162 static VarListElementInit
*get(TypedInit
*T
, unsigned E
);
1164 TypedInit
*getVariable() const { return TI
; }
1165 unsigned getElementNum() const { return Element
; }
1167 std::string
getAsString() const override
;
1168 Init
*resolveReferences(Resolver
&R
) const override
;
1170 Init
*getBit(unsigned Bit
) const override
;
1173 /// AL - Represent a reference to a 'def' in the description
1174 class DefInit
: public TypedInit
{
1175 friend class Record
;
1179 explicit DefInit(Record
*D
);
1182 DefInit(const DefInit
&) = delete;
1183 DefInit
&operator=(const DefInit
&) = delete;
1185 static bool classof(const Init
*I
) {
1186 return I
->getKind() == IK_DefInit
;
1189 static DefInit
*get(Record
*);
1191 Init
*convertInitializerTo(RecTy
*Ty
) const override
;
1193 Record
*getDef() const { return Def
; }
1195 //virtual Init *convertInitializerBitRange(ArrayRef<unsigned> Bits);
1197 RecTy
*getFieldType(StringInit
*FieldName
) const override
;
1199 bool isConcrete() const override
{ return true; }
1200 std::string
getAsString() const override
;
1202 Init
*getBit(unsigned Bit
) const override
{
1203 llvm_unreachable("Illegal bit reference off def");
1207 /// classname<targs...> - Represent an uninstantiated anonymous class
1209 class VarDefInit final
: public TypedInit
, public FoldingSetNode
,
1210 public TrailingObjects
<VarDefInit
, Init
*> {
1212 DefInit
*Def
= nullptr; // after instantiation
1215 explicit VarDefInit(Record
*Class
, unsigned N
)
1216 : TypedInit(IK_VarDefInit
, RecordRecTy::get(Class
)), Class(Class
), NumArgs(N
) {}
1218 DefInit
*instantiate();
1221 VarDefInit(const VarDefInit
&) = delete;
1222 VarDefInit
&operator=(const VarDefInit
&) = delete;
1224 // Do not use sized deallocation due to trailing objects.
1225 void operator delete(void *p
) { ::operator delete(p
); }
1227 static bool classof(const Init
*I
) {
1228 return I
->getKind() == IK_VarDefInit
;
1230 static VarDefInit
*get(Record
*Class
, ArrayRef
<Init
*> Args
);
1232 void Profile(FoldingSetNodeID
&ID
) const;
1234 Init
*resolveReferences(Resolver
&R
) const override
;
1237 std::string
getAsString() const override
;
1239 Init
*getArg(unsigned i
) const {
1240 assert(i
< NumArgs
&& "Argument index out of range!");
1241 return getTrailingObjects
<Init
*>()[i
];
1244 using const_iterator
= Init
*const *;
1246 const_iterator
args_begin() const { return getTrailingObjects
<Init
*>(); }
1247 const_iterator
args_end () const { return args_begin() + NumArgs
; }
1249 size_t args_size () const { return NumArgs
; }
1250 bool args_empty() const { return NumArgs
== 0; }
1252 ArrayRef
<Init
*> args() const { return makeArrayRef(args_begin(), NumArgs
); }
1254 Init
*getBit(unsigned Bit
) const override
{
1255 llvm_unreachable("Illegal bit reference off anonymous def");
1259 /// X.Y - Represent a reference to a subfield of a variable
1260 class FieldInit
: public TypedInit
{
1261 Init
*Rec
; // Record we are referring to
1262 StringInit
*FieldName
; // Field we are accessing
1264 FieldInit(Init
*R
, StringInit
*FN
)
1265 : TypedInit(IK_FieldInit
, R
->getFieldType(FN
)), Rec(R
), FieldName(FN
) {
1268 llvm::errs() << "In Record = " << Rec
->getAsString()
1269 << ", got FieldName = " << *FieldName
1270 << " with non-record type!\n";
1271 llvm_unreachable("FieldInit with non-record type!");
1277 FieldInit(const FieldInit
&) = delete;
1278 FieldInit
&operator=(const FieldInit
&) = delete;
1280 static bool classof(const Init
*I
) {
1281 return I
->getKind() == IK_FieldInit
;
1284 static FieldInit
*get(Init
*R
, StringInit
*FN
);
1286 Init
*getRecord() const { return Rec
; }
1287 StringInit
*getFieldName() const { return FieldName
; }
1289 Init
*getBit(unsigned Bit
) const override
;
1291 Init
*resolveReferences(Resolver
&R
) const override
;
1292 Init
*Fold(Record
*CurRec
) const;
1294 std::string
getAsString() const override
{
1295 return Rec
->getAsString() + "." + FieldName
->getValue().str();
1299 /// (v a, b) - Represent a DAG tree value. DAG inits are required
1300 /// to have at least one value then a (possibly empty) list of arguments. Each
1301 /// argument can have a name associated with it.
1302 class DagInit final
: public TypedInit
, public FoldingSetNode
,
1303 public TrailingObjects
<DagInit
, Init
*, StringInit
*> {
1304 friend TrailingObjects
;
1307 StringInit
*ValName
;
1309 unsigned NumArgNames
;
1311 DagInit(Init
*V
, StringInit
*VN
, unsigned NumArgs
, unsigned NumArgNames
)
1312 : TypedInit(IK_DagInit
, DagRecTy::get()), Val(V
), ValName(VN
),
1313 NumArgs(NumArgs
), NumArgNames(NumArgNames
) {}
1315 size_t numTrailingObjects(OverloadToken
<Init
*>) const { return NumArgs
; }
1318 DagInit(const DagInit
&) = delete;
1319 DagInit
&operator=(const DagInit
&) = delete;
1321 static bool classof(const Init
*I
) {
1322 return I
->getKind() == IK_DagInit
;
1325 static DagInit
*get(Init
*V
, StringInit
*VN
, ArrayRef
<Init
*> ArgRange
,
1326 ArrayRef
<StringInit
*> NameRange
);
1327 static DagInit
*get(Init
*V
, StringInit
*VN
,
1328 ArrayRef
<std::pair
<Init
*, StringInit
*>> Args
);
1330 void Profile(FoldingSetNodeID
&ID
) const;
1332 Init
*getOperator() const { return Val
; }
1334 StringInit
*getName() const { return ValName
; }
1336 StringRef
getNameStr() const {
1337 return ValName
? ValName
->getValue() : StringRef();
1340 unsigned getNumArgs() const { return NumArgs
; }
1342 Init
*getArg(unsigned Num
) const {
1343 assert(Num
< NumArgs
&& "Arg number out of range!");
1344 return getTrailingObjects
<Init
*>()[Num
];
1347 StringInit
*getArgName(unsigned Num
) const {
1348 assert(Num
< NumArgNames
&& "Arg number out of range!");
1349 return getTrailingObjects
<StringInit
*>()[Num
];
1352 StringRef
getArgNameStr(unsigned Num
) const {
1353 StringInit
*Init
= getArgName(Num
);
1354 return Init
? Init
->getValue() : StringRef();
1357 ArrayRef
<Init
*> getArgs() const {
1358 return makeArrayRef(getTrailingObjects
<Init
*>(), NumArgs
);
1361 ArrayRef
<StringInit
*> getArgNames() const {
1362 return makeArrayRef(getTrailingObjects
<StringInit
*>(), NumArgNames
);
1365 Init
*resolveReferences(Resolver
&R
) const override
;
1367 bool isConcrete() const override
;
1368 std::string
getAsString() const override
;
1370 using const_arg_iterator
= SmallVectorImpl
<Init
*>::const_iterator
;
1371 using const_name_iterator
= SmallVectorImpl
<StringInit
*>::const_iterator
;
1373 inline const_arg_iterator
arg_begin() const { return getArgs().begin(); }
1374 inline const_arg_iterator
arg_end () const { return getArgs().end(); }
1376 inline size_t arg_size () const { return NumArgs
; }
1377 inline bool arg_empty() const { return NumArgs
== 0; }
1379 inline const_name_iterator
name_begin() const { return getArgNames().begin();}
1380 inline const_name_iterator
name_end () const { return getArgNames().end(); }
1382 inline size_t name_size () const { return NumArgNames
; }
1383 inline bool name_empty() const { return NumArgNames
== 0; }
1385 Init
*getBit(unsigned Bit
) const override
{
1386 llvm_unreachable("Illegal bit reference off dag");
1390 //===----------------------------------------------------------------------===//
1391 // High-Level Classes
1392 //===----------------------------------------------------------------------===//
1395 friend class Record
;
1398 PointerIntPair
<RecTy
*, 1, bool> TyAndPrefix
;
1402 RecordVal(Init
*N
, RecTy
*T
, bool P
);
1404 StringRef
getName() const;
1405 Init
*getNameInit() const { return Name
; }
1407 std::string
getNameInitAsString() const {
1408 return getNameInit()->getAsUnquotedString();
1411 bool getPrefix() const { return TyAndPrefix
.getInt(); }
1412 RecTy
*getType() const { return TyAndPrefix
.getPointer(); }
1413 Init
*getValue() const { return Value
; }
1415 bool setValue(Init
*V
);
1418 void print(raw_ostream
&OS
, bool PrintSem
= true) const;
1421 inline raw_ostream
&operator<<(raw_ostream
&OS
, const RecordVal
&RV
) {
1422 RV
.print(OS
<< " ");
1427 static unsigned LastID
;
1430 // Location where record was instantiated, followed by the location of
1431 // multiclass prototypes used.
1432 SmallVector
<SMLoc
, 4> Locs
;
1433 SmallVector
<Init
*, 0> TemplateArgs
;
1434 SmallVector
<RecordVal
, 0> Values
;
1436 // All superclasses in the inheritance forest in reverse preorder (yes, it
1437 // must be a forest; diamond-shaped inheritance is not allowed).
1438 SmallVector
<std::pair
<Record
*, SMRange
>, 0> SuperClasses
;
1440 // Tracks Record instances. Not owned by Record.
1441 RecordKeeper
&TrackedRecords
;
1443 DefInit
*TheInit
= nullptr;
1445 // Unique record ID.
1454 // Constructs a record.
1455 explicit Record(Init
*N
, ArrayRef
<SMLoc
> locs
, RecordKeeper
&records
,
1456 bool Anonymous
= false, bool Class
= false)
1457 : Name(N
), Locs(locs
.begin(), locs
.end()), TrackedRecords(records
),
1458 ID(LastID
++), IsAnonymous(Anonymous
), IsClass(Class
) {
1462 explicit Record(StringRef N
, ArrayRef
<SMLoc
> locs
, RecordKeeper
&records
,
1464 : Record(StringInit::get(N
), locs
, records
, false, Class
) {}
1466 // When copy-constructing a Record, we must still guarantee a globally unique
1467 // ID number. Don't copy TheInit either since it's owned by the original
1468 // record. All other fields can be copied normally.
1469 Record(const Record
&O
)
1470 : Name(O
.Name
), Locs(O
.Locs
), TemplateArgs(O
.TemplateArgs
),
1471 Values(O
.Values
), SuperClasses(O
.SuperClasses
),
1472 TrackedRecords(O
.TrackedRecords
), ID(LastID
++),
1473 IsAnonymous(O
.IsAnonymous
), IsClass(O
.IsClass
) { }
1475 static unsigned getNewUID() { return LastID
++; }
1477 unsigned getID() const { return ID
; }
1479 StringRef
getName() const { return cast
<StringInit
>(Name
)->getValue(); }
1481 Init
*getNameInit() const {
1485 const std::string
getNameInitAsString() const {
1486 return getNameInit()->getAsUnquotedString();
1489 void setName(Init
*Name
); // Also updates RecordKeeper.
1491 ArrayRef
<SMLoc
> getLoc() const { return Locs
; }
1492 void appendLoc(SMLoc Loc
) { Locs
.push_back(Loc
); }
1494 // Make the type that this record should have based on its superclasses.
1495 RecordRecTy
*getType();
1497 /// get the corresponding DefInit.
1498 DefInit
*getDefInit();
1500 bool isClass() const { return IsClass
; }
1502 ArrayRef
<Init
*> getTemplateArgs() const {
1503 return TemplateArgs
;
1506 ArrayRef
<RecordVal
> getValues() const { return Values
; }
1508 ArrayRef
<std::pair
<Record
*, SMRange
>> getSuperClasses() const {
1509 return SuperClasses
;
1512 /// Append the direct super classes of this record to Classes.
1513 void getDirectSuperClasses(SmallVectorImpl
<Record
*> &Classes
) const;
1515 bool isTemplateArg(Init
*Name
) const {
1516 for (Init
*TA
: TemplateArgs
)
1517 if (TA
== Name
) return true;
1521 const RecordVal
*getValue(const Init
*Name
) const {
1522 for (const RecordVal
&Val
: Values
)
1523 if (Val
.Name
== Name
) return &Val
;
1527 const RecordVal
*getValue(StringRef Name
) const {
1528 return getValue(StringInit::get(Name
));
1531 RecordVal
*getValue(const Init
*Name
) {
1532 return const_cast<RecordVal
*>(static_cast<const Record
*>(this)->getValue(Name
));
1535 RecordVal
*getValue(StringRef Name
) {
1536 return const_cast<RecordVal
*>(static_cast<const Record
*>(this)->getValue(Name
));
1539 void addTemplateArg(Init
*Name
) {
1540 assert(!isTemplateArg(Name
) && "Template arg already defined!");
1541 TemplateArgs
.push_back(Name
);
1544 void addValue(const RecordVal
&RV
) {
1545 assert(getValue(RV
.getNameInit()) == nullptr && "Value already added!");
1546 Values
.push_back(RV
);
1549 void removeValue(Init
*Name
) {
1550 for (unsigned i
= 0, e
= Values
.size(); i
!= e
; ++i
)
1551 if (Values
[i
].getNameInit() == Name
) {
1552 Values
.erase(Values
.begin()+i
);
1555 llvm_unreachable("Cannot remove an entry that does not exist!");
1558 void removeValue(StringRef Name
) {
1559 removeValue(StringInit::get(Name
));
1562 bool isSubClassOf(const Record
*R
) const {
1563 for (const auto &SCPair
: SuperClasses
)
1564 if (SCPair
.first
== R
)
1569 bool isSubClassOf(StringRef Name
) const {
1570 for (const auto &SCPair
: SuperClasses
) {
1571 if (const auto *SI
= dyn_cast
<StringInit
>(SCPair
.first
->getNameInit())) {
1572 if (SI
->getValue() == Name
)
1574 } else if (SCPair
.first
->getNameInitAsString() == Name
) {
1581 void addSuperClass(Record
*R
, SMRange Range
) {
1582 assert(!TheInit
&& "changing type of record after it has been referenced");
1583 assert(!isSubClassOf(R
) && "Already subclassing record!");
1584 SuperClasses
.push_back(std::make_pair(R
, Range
));
1587 /// If there are any field references that refer to fields
1588 /// that have been filled in, we can propagate the values now.
1590 /// This is a final resolve: any error messages, e.g. due to undefined
1591 /// !cast references, are generated now.
1592 void resolveReferences();
1594 /// Apply the resolver to the name of the record as well as to the
1595 /// initializers of all fields of the record except SkipVal.
1597 /// The resolver should not resolve any of the fields itself, to avoid
1598 /// recursion / infinite loops.
1599 void resolveReferences(Resolver
&R
, const RecordVal
*SkipVal
= nullptr);
1601 /// If anything in this record refers to RV, replace the
1602 /// reference to RV with the RHS of RV. If RV is null, we resolve all
1603 /// possible references.
1604 void resolveReferencesTo(const RecordVal
*RV
);
1606 RecordKeeper
&getRecords() const {
1607 return TrackedRecords
;
1610 bool isAnonymous() const {
1614 void print(raw_ostream
&OS
) const;
1617 //===--------------------------------------------------------------------===//
1618 // High-level methods useful to tablegen back-ends
1621 /// Return the initializer for a value with the specified name,
1622 /// or throw an exception if the field does not exist.
1623 Init
*getValueInit(StringRef FieldName
) const;
1625 /// Return true if the named field is unset.
1626 bool isValueUnset(StringRef FieldName
) const {
1627 return isa
<UnsetInit
>(getValueInit(FieldName
));
1630 /// This method looks up the specified field and returns
1631 /// its value as a string, throwing an exception if the field does not exist
1632 /// or if the value is not a string.
1633 StringRef
getValueAsString(StringRef FieldName
) const;
1635 /// This method looks up the specified field and returns
1636 /// its value as a BitsInit, throwing an exception if the field does not exist
1637 /// or if the value is not the right type.
1638 BitsInit
*getValueAsBitsInit(StringRef FieldName
) const;
1640 /// This method looks up the specified field and returns
1641 /// its value as a ListInit, throwing an exception if the field does not exist
1642 /// or if the value is not the right type.
1643 ListInit
*getValueAsListInit(StringRef FieldName
) const;
1645 /// This method looks up the specified field and
1646 /// returns its value as a vector of records, throwing an exception if the
1647 /// field does not exist or if the value is not the right type.
1648 std::vector
<Record
*> getValueAsListOfDefs(StringRef FieldName
) const;
1650 /// This method looks up the specified field and
1651 /// returns its value as a vector of integers, throwing an exception if the
1652 /// field does not exist or if the value is not the right type.
1653 std::vector
<int64_t> getValueAsListOfInts(StringRef FieldName
) const;
1655 /// This method looks up the specified field and
1656 /// returns its value as a vector of strings, throwing an exception if the
1657 /// field does not exist or if the value is not the right type.
1658 std::vector
<StringRef
> getValueAsListOfStrings(StringRef FieldName
) const;
1660 /// This method looks up the specified field and returns its
1661 /// value as a Record, throwing an exception if the field does not exist or if
1662 /// the value is not the right type.
1663 Record
*getValueAsDef(StringRef FieldName
) const;
1665 /// This method looks up the specified field and returns its
1666 /// value as a bit, throwing an exception if the field does not exist or if
1667 /// the value is not the right type.
1668 bool getValueAsBit(StringRef FieldName
) const;
1670 /// This method looks up the specified field and
1671 /// returns its value as a bit. If the field is unset, sets Unset to true and
1673 bool getValueAsBitOrUnset(StringRef FieldName
, bool &Unset
) const;
1675 /// This method looks up the specified field and returns its
1676 /// value as an int64_t, throwing an exception if the field does not exist or
1677 /// if the value is not the right type.
1678 int64_t getValueAsInt(StringRef FieldName
) const;
1680 /// This method looks up the specified field and returns its
1681 /// value as an Dag, throwing an exception if the field does not exist or if
1682 /// the value is not the right type.
1683 DagInit
*getValueAsDag(StringRef FieldName
) const;
1686 raw_ostream
&operator<<(raw_ostream
&OS
, const Record
&R
);
1688 class RecordKeeper
{
1689 friend class RecordRecTy
;
1690 using RecordMap
= std::map
<std::string
, std::unique_ptr
<Record
>, std::less
<>>;
1691 RecordMap Classes
, Defs
;
1692 FoldingSet
<RecordRecTy
> RecordTypePool
;
1693 std::map
<std::string
, Init
*, std::less
<>> ExtraGlobals
;
1694 unsigned AnonCounter
= 0;
1697 const RecordMap
&getClasses() const { return Classes
; }
1698 const RecordMap
&getDefs() const { return Defs
; }
1700 Record
*getClass(StringRef Name
) const {
1701 auto I
= Classes
.find(Name
);
1702 return I
== Classes
.end() ? nullptr : I
->second
.get();
1705 Record
*getDef(StringRef Name
) const {
1706 auto I
= Defs
.find(Name
);
1707 return I
== Defs
.end() ? nullptr : I
->second
.get();
1710 Init
*getGlobal(StringRef Name
) const {
1711 if (Record
*R
= getDef(Name
))
1712 return R
->getDefInit();
1713 auto It
= ExtraGlobals
.find(Name
);
1714 return It
== ExtraGlobals
.end() ? nullptr : It
->second
;
1717 void addClass(std::unique_ptr
<Record
> R
) {
1718 bool Ins
= Classes
.insert(std::make_pair(R
->getName(),
1719 std::move(R
))).second
;
1721 assert(Ins
&& "Class already exists");
1724 void addDef(std::unique_ptr
<Record
> R
) {
1725 bool Ins
= Defs
.insert(std::make_pair(R
->getName(),
1726 std::move(R
))).second
;
1728 assert(Ins
&& "Record already exists");
1731 void addExtraGlobal(StringRef Name
, Init
*I
) {
1732 bool Ins
= ExtraGlobals
.insert(std::make_pair(Name
, I
)).second
;
1734 assert(!getDef(Name
));
1735 assert(Ins
&& "Global already exists");
1738 Init
*getNewAnonymousName();
1740 //===--------------------------------------------------------------------===//
1741 // High-level helper methods, useful for tablegen backends...
1743 /// This method returns all concrete definitions
1744 /// that derive from the specified class name. A class with the specified
1745 /// name must exist.
1746 std::vector
<Record
*> getAllDerivedDefinitions(StringRef ClassName
) const;
1751 /// Sorting predicate to sort record pointers by name.
1753 bool operator()(const Record
*Rec1
, const Record
*Rec2
) const {
1754 return StringRef(Rec1
->getName()).compare_numeric(Rec2
->getName()) < 0;
1758 /// Sorting predicate to sort record pointers by their
1759 /// unique ID. If you just need a deterministic order, use this, since it
1760 /// just compares two `unsigned`; the other sorting predicates require
1761 /// string manipulation.
1762 struct LessRecordByID
{
1763 bool operator()(const Record
*LHS
, const Record
*RHS
) const {
1764 return LHS
->getID() < RHS
->getID();
1768 /// Sorting predicate to sort record pointers by their
1770 struct LessRecordFieldName
{
1771 bool operator()(const Record
*Rec1
, const Record
*Rec2
) const {
1772 return Rec1
->getValueAsString("Name") < Rec2
->getValueAsString("Name");
1776 struct LessRecordRegister
{
1777 static bool ascii_isdigit(char x
) { return x
>= '0' && x
<= '9'; }
1779 struct RecordParts
{
1780 SmallVector
<std::pair
< bool, StringRef
>, 4> Parts
;
1782 RecordParts(StringRef Rec
) {
1787 const char *Start
= Rec
.data();
1788 const char *Curr
= Start
;
1789 bool isDigitPart
= ascii_isdigit(Curr
[0]);
1790 for (size_t I
= 0, E
= Rec
.size(); I
!= E
; ++I
, ++Len
) {
1791 bool isDigit
= ascii_isdigit(Curr
[I
]);
1792 if (isDigit
!= isDigitPart
) {
1793 Parts
.push_back(std::make_pair(isDigitPart
, StringRef(Start
, Len
)));
1796 isDigitPart
= ascii_isdigit(Curr
[I
]);
1799 // Push the last part.
1800 Parts
.push_back(std::make_pair(isDigitPart
, StringRef(Start
, Len
)));
1803 size_t size() { return Parts
.size(); }
1805 std::pair
<bool, StringRef
> getPart(size_t i
) {
1806 assert (i
< Parts
.size() && "Invalid idx!");
1811 bool operator()(const Record
*Rec1
, const Record
*Rec2
) const {
1812 RecordParts
LHSParts(StringRef(Rec1
->getName()));
1813 RecordParts
RHSParts(StringRef(Rec2
->getName()));
1815 size_t LHSNumParts
= LHSParts
.size();
1816 size_t RHSNumParts
= RHSParts
.size();
1817 assert (LHSNumParts
&& RHSNumParts
&& "Expected at least one part!");
1819 if (LHSNumParts
!= RHSNumParts
)
1820 return LHSNumParts
< RHSNumParts
;
1822 // We expect the registers to be of the form [_a-zA-Z]+([0-9]*[_a-zA-Z]*)*.
1823 for (size_t I
= 0, E
= LHSNumParts
; I
< E
; I
+=2) {
1824 std::pair
<bool, StringRef
> LHSPart
= LHSParts
.getPart(I
);
1825 std::pair
<bool, StringRef
> RHSPart
= RHSParts
.getPart(I
);
1826 // Expect even part to always be alpha.
1827 assert (LHSPart
.first
== false && RHSPart
.first
== false &&
1828 "Expected both parts to be alpha.");
1829 if (int Res
= LHSPart
.second
.compare(RHSPart
.second
))
1832 for (size_t I
= 1, E
= LHSNumParts
; I
< E
; I
+=2) {
1833 std::pair
<bool, StringRef
> LHSPart
= LHSParts
.getPart(I
);
1834 std::pair
<bool, StringRef
> RHSPart
= RHSParts
.getPart(I
);
1835 // Expect odd part to always be numeric.
1836 assert (LHSPart
.first
== true && RHSPart
.first
== true &&
1837 "Expected both parts to be numeric.");
1838 if (LHSPart
.second
.size() != RHSPart
.second
.size())
1839 return LHSPart
.second
.size() < RHSPart
.second
.size();
1841 unsigned LHSVal
, RHSVal
;
1843 bool LHSFailed
= LHSPart
.second
.getAsInteger(10, LHSVal
); (void)LHSFailed
;
1844 assert(!LHSFailed
&& "Unable to convert LHS to integer.");
1845 bool RHSFailed
= RHSPart
.second
.getAsInteger(10, RHSVal
); (void)RHSFailed
;
1846 assert(!RHSFailed
&& "Unable to convert RHS to integer.");
1848 if (LHSVal
!= RHSVal
)
1849 return LHSVal
< RHSVal
;
1851 return LHSNumParts
< RHSNumParts
;
1855 raw_ostream
&operator<<(raw_ostream
&OS
, const RecordKeeper
&RK
);
1857 //===----------------------------------------------------------------------===//
1859 //===----------------------------------------------------------------------===//
1861 /// Interface for looking up the initializer for a variable name, used by
1862 /// Init::resolveReferences.
1865 bool IsFinal
= false;
1868 explicit Resolver(Record
*CurRec
) : CurRec(CurRec
) {}
1869 virtual ~Resolver() {}
1871 Record
*getCurrentRecord() const { return CurRec
; }
1873 /// Return the initializer for the given variable name (should normally be a
1874 /// StringInit), or nullptr if the name could not be resolved.
1875 virtual Init
*resolve(Init
*VarName
) = 0;
1877 // Whether bits in a BitsInit should stay unresolved if resolving them would
1878 // result in a ? (UnsetInit). This behavior is used to represent instruction
1879 // encodings by keeping references to unset variables within a record.
1880 virtual bool keepUnsetBits() const { return false; }
1882 // Whether this is the final resolve step before adding a record to the
1883 // RecordKeeper. Error reporting during resolve and related constant folding
1884 // should only happen when this is true.
1885 bool isFinal() const { return IsFinal
; }
1887 void setFinal(bool Final
) { IsFinal
= Final
; }
1890 /// Resolve arbitrary mappings.
1891 class MapResolver final
: public Resolver
{
1892 struct MappedValue
{
1896 MappedValue() : V(nullptr), Resolved(false) {}
1897 MappedValue(Init
*V
, bool Resolved
) : V(V
), Resolved(Resolved
) {}
1900 DenseMap
<Init
*, MappedValue
> Map
;
1903 explicit MapResolver(Record
*CurRec
= nullptr) : Resolver(CurRec
) {}
1905 void set(Init
*Key
, Init
*Value
) { Map
[Key
] = {Value
, false}; }
1907 Init
*resolve(Init
*VarName
) override
;
1910 /// Resolve all variables from a record except for unset variables.
1911 class RecordResolver final
: public Resolver
{
1912 DenseMap
<Init
*, Init
*> Cache
;
1913 SmallVector
<Init
*, 4> Stack
;
1916 explicit RecordResolver(Record
&R
) : Resolver(&R
) {}
1918 Init
*resolve(Init
*VarName
) override
;
1920 bool keepUnsetBits() const override
{ return true; }
1923 /// Resolve all references to a specific RecordVal.
1925 // TODO: This is used for resolving references to template arguments, in a
1926 // rather inefficient way. Change those uses to resolve all template
1927 // arguments simultaneously and get rid of this class.
1928 class RecordValResolver final
: public Resolver
{
1929 const RecordVal
*RV
;
1932 explicit RecordValResolver(Record
&R
, const RecordVal
*RV
)
1933 : Resolver(&R
), RV(RV
) {}
1935 Init
*resolve(Init
*VarName
) override
{
1936 if (VarName
== RV
->getNameInit())
1937 return RV
->getValue();
1942 /// Delegate resolving to a sub-resolver, but shadow some variable names.
1943 class ShadowResolver final
: public Resolver
{
1945 DenseSet
<Init
*> Shadowed
;
1948 explicit ShadowResolver(Resolver
&R
)
1949 : Resolver(R
.getCurrentRecord()), R(R
) {
1950 setFinal(R
.isFinal());
1953 void addShadow(Init
*Key
) { Shadowed
.insert(Key
); }
1955 Init
*resolve(Init
*VarName
) override
{
1956 if (Shadowed
.count(VarName
))
1958 return R
.resolve(VarName
);
1962 /// (Optionally) delegate resolving to a sub-resolver, and keep track whether
1963 /// there were unresolved references.
1964 class TrackUnresolvedResolver final
: public Resolver
{
1966 bool FoundUnresolved
= false;
1969 explicit TrackUnresolvedResolver(Resolver
*R
= nullptr)
1970 : Resolver(R
? R
->getCurrentRecord() : nullptr), R(R
) {}
1972 bool foundUnresolved() const { return FoundUnresolved
; }
1974 Init
*resolve(Init
*VarName
) override
;
1977 /// Do not resolve anything, but keep track of whether a given variable was
1979 class HasReferenceResolver final
: public Resolver
{
1980 Init
*VarNameToTrack
;
1984 explicit HasReferenceResolver(Init
*VarNameToTrack
)
1985 : Resolver(nullptr), VarNameToTrack(VarNameToTrack
) {}
1987 bool found() const { return Found
; }
1989 Init
*resolve(Init
*VarName
) override
;
1992 void EmitJSON(RecordKeeper
&RK
, raw_ostream
&OS
);
1994 } // end namespace llvm
1996 #endif // LLVM_TABLEGEN_RECORD_H