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/FoldingSet.h"
20 #include "llvm/Support/Allocator.h"
21 #include "llvm/Support/Casting.h"
22 #include "llvm/Support/DataTypes.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/SourceMgr.h"
25 #include "llvm/Support/raw_ostream.h"
56 class VarListElementInit;
64 //===----------------------------------------------------------------------===//
66 //===----------------------------------------------------------------------===//
70 /// \brief Subclass discriminator (for dyn_cast<> et al.)
83 std::unique_ptr<ListRecTy> ListTy;
84 virtual void anchor();
87 RecTyKind getRecTyKind() const { return Kind; }
89 RecTy(RecTyKind K) : Kind(K), ListTy(nullptr) {}
92 virtual std::string getAsString() const = 0;
93 void print(raw_ostream &OS) const { OS << getAsString(); }
96 /// typeIsConvertibleTo - Return true if all values of 'this' type can be
97 /// converted to the specified type.
98 virtual bool typeIsConvertibleTo(const RecTy *RHS) const = 0;
100 /// getListTy - Returns the type representing list<this>.
101 ListRecTy *getListTy();
103 public: // These methods should only be called from subclasses of Init
104 virtual Init *convertValue( UnsetInit *UI) { return nullptr; }
105 virtual Init *convertValue( BitInit *BI) { return nullptr; }
106 virtual Init *convertValue( BitsInit *BI) { return nullptr; }
107 virtual Init *convertValue( IntInit *II) { return nullptr; }
108 virtual Init *convertValue(StringInit *SI) { return nullptr; }
109 virtual Init *convertValue( ListInit *LI) { return nullptr; }
110 virtual Init *convertValue( UnOpInit *UI) {
111 return convertValue((TypedInit*)UI);
113 virtual Init *convertValue( BinOpInit *UI) {
114 return convertValue((TypedInit*)UI);
116 virtual Init *convertValue( TernOpInit *UI) {
117 return convertValue((TypedInit*)UI);
119 virtual Init *convertValue(VarBitInit *VB) { return nullptr; }
120 virtual Init *convertValue( DefInit *DI) { return nullptr; }
121 virtual Init *convertValue( DagInit *DI) { return nullptr; }
122 virtual Init *convertValue( TypedInit *TI) { return nullptr; }
123 virtual Init *convertValue( VarInit *VI) {
124 return convertValue((TypedInit*)VI);
126 virtual Init *convertValue( FieldInit *FI) {
127 return convertValue((TypedInit*)FI);
131 virtual bool baseClassOf(const RecTy*) const;
134 inline raw_ostream &operator<<(raw_ostream &OS, const RecTy &Ty) {
139 /// BitRecTy - 'bit' - Represent a single bit
141 class BitRecTy : public RecTy {
142 static BitRecTy Shared;
143 BitRecTy() : RecTy(BitRecTyKind) {}
146 static bool classof(const RecTy *RT) {
147 return RT->getRecTyKind() == BitRecTyKind;
150 static BitRecTy *get() { return &Shared; }
152 Init *convertValue( UnsetInit *UI) override { return (Init*)UI; }
153 Init *convertValue( BitInit *BI) override { return (Init*)BI; }
154 Init *convertValue( BitsInit *BI) override;
155 Init *convertValue( IntInit *II) override;
156 Init *convertValue(StringInit *SI) override { return nullptr; }
157 Init *convertValue( ListInit *LI) override { return nullptr; }
158 Init *convertValue(VarBitInit *VB) override { return (Init*)VB; }
159 Init *convertValue( DefInit *DI) override { return nullptr; }
160 Init *convertValue( DagInit *DI) override { return nullptr; }
161 Init *convertValue( UnOpInit *UI) override { return RecTy::convertValue(UI);}
162 Init *convertValue( BinOpInit *UI) override { return RecTy::convertValue(UI);}
163 Init *convertValue(TernOpInit *UI) override { return RecTy::convertValue(UI);}
164 Init *convertValue( TypedInit *TI) override;
165 Init *convertValue( VarInit *VI) override { return RecTy::convertValue(VI);}
166 Init *convertValue( FieldInit *FI) override { return RecTy::convertValue(FI);}
168 std::string getAsString() const override { return "bit"; }
170 bool typeIsConvertibleTo(const RecTy *RHS) const override {
171 return RHS->baseClassOf(this);
173 bool baseClassOf(const RecTy*) const override;
176 /// BitsRecTy - 'bits<n>' - Represent a fixed number of bits
178 class BitsRecTy : public RecTy {
180 explicit BitsRecTy(unsigned Sz) : RecTy(BitsRecTyKind), Size(Sz) {}
183 static bool classof(const RecTy *RT) {
184 return RT->getRecTyKind() == BitsRecTyKind;
187 static BitsRecTy *get(unsigned Sz);
189 unsigned getNumBits() const { return Size; }
191 Init *convertValue( UnsetInit *UI) override;
192 Init *convertValue( BitInit *UI) override;
193 Init *convertValue( BitsInit *BI) override;
194 Init *convertValue( IntInit *II) override;
195 Init *convertValue(StringInit *SI) override { return nullptr; }
196 Init *convertValue( ListInit *LI) override { return nullptr; }
197 Init *convertValue(VarBitInit *VB) override { return nullptr; }
198 Init *convertValue( DefInit *DI) override { return nullptr; }
199 Init *convertValue( DagInit *DI) override { return nullptr; }
200 Init *convertValue( UnOpInit *UI) override { return RecTy::convertValue(UI);}
201 Init *convertValue( BinOpInit *UI) override { return RecTy::convertValue(UI);}
202 Init *convertValue(TernOpInit *UI) override { return RecTy::convertValue(UI);}
203 Init *convertValue( TypedInit *TI) override;
204 Init *convertValue( VarInit *VI) override { return RecTy::convertValue(VI);}
205 Init *convertValue( FieldInit *FI) override { return RecTy::convertValue(FI);}
207 std::string getAsString() const override;
209 bool typeIsConvertibleTo(const RecTy *RHS) const override {
210 return RHS->baseClassOf(this);
212 bool baseClassOf(const RecTy*) const override;
215 /// IntRecTy - 'int' - Represent an integer value of no particular size
217 class IntRecTy : public RecTy {
218 static IntRecTy Shared;
219 IntRecTy() : RecTy(IntRecTyKind) {}
222 static bool classof(const RecTy *RT) {
223 return RT->getRecTyKind() == IntRecTyKind;
226 static IntRecTy *get() { return &Shared; }
228 Init *convertValue( UnsetInit *UI) override { return (Init*)UI; }
229 Init *convertValue( BitInit *BI) override;
230 Init *convertValue( BitsInit *BI) override;
231 Init *convertValue( IntInit *II) override { return (Init*)II; }
232 Init *convertValue(StringInit *SI) override { return nullptr; }
233 Init *convertValue( ListInit *LI) override { return nullptr; }
234 Init *convertValue(VarBitInit *VB) override { return nullptr; }
235 Init *convertValue( DefInit *DI) override { return nullptr; }
236 Init *convertValue( DagInit *DI) override { return nullptr; }
237 Init *convertValue( UnOpInit *UI) override { return RecTy::convertValue(UI);}
238 Init *convertValue( BinOpInit *UI) override { return RecTy::convertValue(UI);}
239 Init *convertValue(TernOpInit *UI) override { return RecTy::convertValue(UI);}
240 Init *convertValue( TypedInit *TI) override;
241 Init *convertValue( VarInit *VI) override { return RecTy::convertValue(VI);}
242 Init *convertValue( FieldInit *FI) override { return RecTy::convertValue(FI);}
244 std::string getAsString() const override { return "int"; }
246 bool typeIsConvertibleTo(const RecTy *RHS) const override {
247 return RHS->baseClassOf(this);
250 bool baseClassOf(const RecTy*) const override;
253 /// StringRecTy - 'string' - Represent an string value
255 class StringRecTy : public RecTy {
256 static StringRecTy Shared;
257 StringRecTy() : RecTy(StringRecTyKind) {}
260 static bool classof(const RecTy *RT) {
261 return RT->getRecTyKind() == StringRecTyKind;
264 static StringRecTy *get() { return &Shared; }
266 Init *convertValue( UnsetInit *UI) override { return (Init*)UI; }
267 Init *convertValue( BitInit *BI) override { return nullptr; }
268 Init *convertValue( BitsInit *BI) override { return nullptr; }
269 Init *convertValue( IntInit *II) override { return nullptr; }
270 Init *convertValue(StringInit *SI) override { return (Init*)SI; }
271 Init *convertValue( ListInit *LI) override { return nullptr; }
272 Init *convertValue( UnOpInit *BO) override;
273 Init *convertValue( BinOpInit *BO) override;
274 Init *convertValue(TernOpInit *BO) override { return RecTy::convertValue(BO);}
276 Init *convertValue(VarBitInit *VB) override { return nullptr; }
277 Init *convertValue( DefInit *DI) override { return nullptr; }
278 Init *convertValue( DagInit *DI) override { return nullptr; }
279 Init *convertValue( TypedInit *TI) override;
280 Init *convertValue( VarInit *VI) override { return RecTy::convertValue(VI);}
281 Init *convertValue( FieldInit *FI) override { return RecTy::convertValue(FI);}
283 std::string getAsString() const override { return "string"; }
285 bool typeIsConvertibleTo(const RecTy *RHS) const override {
286 return RHS->baseClassOf(this);
290 /// ListRecTy - 'list<Ty>' - Represent a list of values, all of which must be of
291 /// the specified type.
293 class ListRecTy : public RecTy {
295 explicit ListRecTy(RecTy *T) : RecTy(ListRecTyKind), Ty(T) {}
296 friend ListRecTy *RecTy::getListTy();
299 static bool classof(const RecTy *RT) {
300 return RT->getRecTyKind() == ListRecTyKind;
303 static ListRecTy *get(RecTy *T) { return T->getListTy(); }
304 RecTy *getElementType() const { return Ty; }
306 Init *convertValue( UnsetInit *UI) override { return (Init*)UI; }
307 Init *convertValue( BitInit *BI) override { return nullptr; }
308 Init *convertValue( BitsInit *BI) override { return nullptr; }
309 Init *convertValue( IntInit *II) override { return nullptr; }
310 Init *convertValue(StringInit *SI) override { return nullptr; }
311 Init *convertValue( ListInit *LI) override;
312 Init *convertValue(VarBitInit *VB) override { return nullptr; }
313 Init *convertValue( DefInit *DI) override { return nullptr; }
314 Init *convertValue( DagInit *DI) override { return nullptr; }
315 Init *convertValue( UnOpInit *UI) override { return RecTy::convertValue(UI);}
316 Init *convertValue( BinOpInit *UI) override { return RecTy::convertValue(UI);}
317 Init *convertValue(TernOpInit *UI) override { return RecTy::convertValue(UI);}
318 Init *convertValue( TypedInit *TI) override;
319 Init *convertValue( VarInit *VI) override { return RecTy::convertValue(VI);}
320 Init *convertValue( FieldInit *FI) override { return RecTy::convertValue(FI);}
322 std::string getAsString() const override;
324 bool typeIsConvertibleTo(const RecTy *RHS) const override {
325 return RHS->baseClassOf(this);
328 bool baseClassOf(const RecTy*) const override;
331 /// DagRecTy - 'dag' - Represent a dag fragment
333 class DagRecTy : public RecTy {
334 static DagRecTy Shared;
335 DagRecTy() : RecTy(DagRecTyKind) {}
338 static bool classof(const RecTy *RT) {
339 return RT->getRecTyKind() == DagRecTyKind;
342 static DagRecTy *get() { return &Shared; }
344 Init *convertValue( UnsetInit *UI) override { return (Init*)UI; }
345 Init *convertValue( BitInit *BI) override { return nullptr; }
346 Init *convertValue( BitsInit *BI) override { return nullptr; }
347 Init *convertValue( IntInit *II) override { return nullptr; }
348 Init *convertValue(StringInit *SI) override { return nullptr; }
349 Init *convertValue( ListInit *LI) override { return nullptr; }
350 Init *convertValue(VarBitInit *VB) override { return nullptr; }
351 Init *convertValue( DefInit *DI) override { return nullptr; }
352 Init *convertValue( UnOpInit *BO) override;
353 Init *convertValue( BinOpInit *BO) override;
354 Init *convertValue(TernOpInit *BO) override { return RecTy::convertValue(BO);}
355 Init *convertValue( DagInit *CI) override { return (Init*)CI; }
356 Init *convertValue( TypedInit *TI) override;
357 Init *convertValue( VarInit *VI) override { return RecTy::convertValue(VI);}
358 Init *convertValue( FieldInit *FI) override { return RecTy::convertValue(FI);}
360 std::string getAsString() const override { return "dag"; }
362 bool typeIsConvertibleTo(const RecTy *RHS) const override {
363 return RHS->baseClassOf(this);
367 /// RecordRecTy - '[classname]' - Represent an instance of a class, such as:
370 class RecordRecTy : public RecTy {
372 explicit RecordRecTy(Record *R) : RecTy(RecordRecTyKind), Rec(R) {}
376 static bool classof(const RecTy *RT) {
377 return RT->getRecTyKind() == RecordRecTyKind;
380 static RecordRecTy *get(Record *R);
382 Record *getRecord() const { return Rec; }
384 Init *convertValue( UnsetInit *UI) override { return (Init*)UI; }
385 Init *convertValue( BitInit *BI) override { return nullptr; }
386 Init *convertValue( BitsInit *BI) override { return nullptr; }
387 Init *convertValue( IntInit *II) override { return nullptr; }
388 Init *convertValue(StringInit *SI) override { return nullptr; }
389 Init *convertValue( ListInit *LI) override { return nullptr; }
390 Init *convertValue(VarBitInit *VB) override { return nullptr; }
391 Init *convertValue( UnOpInit *UI) override { return RecTy::convertValue(UI);}
392 Init *convertValue( BinOpInit *UI) override { return RecTy::convertValue(UI);}
393 Init *convertValue(TernOpInit *UI) override { return RecTy::convertValue(UI);}
394 Init *convertValue( DefInit *DI) override;
395 Init *convertValue( DagInit *DI) override { return nullptr; }
396 Init *convertValue( TypedInit *VI) override;
397 Init *convertValue( VarInit *VI) override { return RecTy::convertValue(VI);}
398 Init *convertValue( FieldInit *FI) override { return RecTy::convertValue(FI);}
400 std::string getAsString() const override;
402 bool typeIsConvertibleTo(const RecTy *RHS) const override {
403 return RHS->baseClassOf(this);
405 bool baseClassOf(const RecTy*) const override;
408 /// resolveTypes - Find a common type that T1 and T2 convert to.
409 /// Return 0 if no such type exists.
411 RecTy *resolveTypes(RecTy *T1, RecTy *T2);
413 //===----------------------------------------------------------------------===//
414 // Initializer Classes
415 //===----------------------------------------------------------------------===//
419 /// \brief Discriminator enum (for isa<>, dyn_cast<>, et al.)
421 /// This enum is laid out by a preorder traversal of the inheritance
422 /// hierarchy, and does not contain an entry for abstract classes, as per
423 /// the recommendation in docs/HowToSetUpLLVMStyleRTTI.rst.
425 /// We also explicitly include "first" and "last" values for each
426 /// interior node of the inheritance tree, to make it easier to read the
427 /// corresponding classof().
429 /// We could pack these a bit tighter by not having the IK_FirstXXXInit
430 /// and IK_LastXXXInit be their own values, but that would degrade
431 /// readability for really no benefit.
448 IK_VarListElementInit,
456 Init(const Init &) = delete;
457 Init &operator=(const Init &) = delete;
458 virtual void anchor();
461 InitKind getKind() const { return Kind; }
464 explicit Init(InitKind K) : Kind(K) {}
469 /// isComplete - This virtual method should be overridden by values that may
470 /// not be completely specified yet.
471 virtual bool isComplete() const { return true; }
473 /// print - Print out this value.
474 void print(raw_ostream &OS) const { OS << getAsString(); }
476 /// getAsString - Convert this value to a string form.
477 virtual std::string getAsString() const = 0;
478 /// getAsUnquotedString - Convert this value to a string form,
479 /// without adding quote markers. This primaruly affects
480 /// StringInits where we will not surround the string value with
482 virtual std::string getAsUnquotedString() const { return getAsString(); }
484 /// dump - Debugging method that may be called through a debugger, just
485 /// invokes print on stderr.
488 /// convertInitializerTo - This virtual function is a simple call-back
489 /// function that should be overridden to call the appropriate
490 /// RecTy::convertValue method.
492 virtual Init *convertInitializerTo(RecTy *Ty) const = 0;
494 /// convertInitializerBitRange - This method is used to implement the bitrange
495 /// selection operator. Given an initializer, it selects the specified bits
496 /// out, returning them as a new init of bits type. If it is not legal to use
497 /// the bit subscript operator on this initializer, return null.
500 convertInitializerBitRange(const std::vector<unsigned> &Bits) const {
504 /// convertInitListSlice - This method is used to implement the list slice
505 /// selection operator. Given an initializer, it selects the specified list
506 /// elements, returning them as a new init of list type. If it is not legal
507 /// to take a slice of this, return null.
510 convertInitListSlice(const std::vector<unsigned> &Elements) const {
514 /// getFieldType - This method is used to implement the FieldInit class.
515 /// Implementors of this method should return the type of the named field if
516 /// they are of record type.
518 virtual RecTy *getFieldType(const std::string &FieldName) const {
522 /// getFieldInit - This method complements getFieldType to return the
523 /// initializer for the specified field. If getFieldType returns non-null
524 /// this method should return non-null, otherwise it returns null.
526 virtual Init *getFieldInit(Record &R, const RecordVal *RV,
527 const std::string &FieldName) const {
531 /// resolveReferences - This method is used by classes that refer to other
532 /// variables which may not be defined at the time the expression is formed.
533 /// If a value is set for the variable later, this method will be called on
534 /// users of the value to allow the value to propagate out.
536 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const {
537 return const_cast<Init *>(this);
540 /// getBit - This method is used to return the initializer for the specified
542 virtual Init *getBit(unsigned Bit) const = 0;
544 /// getBitVar - This method is used to retrieve the initializer for bit
545 /// reference. For non-VarBitInit, it simply returns itself.
546 virtual Init *getBitVar() const { return const_cast<Init*>(this); }
548 /// getBitNum - This method is used to retrieve the bit number of a bit
549 /// reference. For non-VarBitInit, it simply returns 0.
550 virtual unsigned getBitNum() const { return 0; }
553 inline raw_ostream &operator<<(raw_ostream &OS, const Init &I) {
554 I.print(OS); return OS;
557 /// TypedInit - This is the common super-class of types that have a specific,
560 class TypedInit : public Init {
563 TypedInit(const TypedInit &Other) = delete;
564 TypedInit &operator=(const TypedInit &Other) = delete;
567 explicit TypedInit(InitKind K, RecTy *T) : Init(K), Ty(T) {}
569 // If this is a DefInit we need to delete the RecordRecTy.
570 if (getKind() == IK_DefInit)
575 static bool classof(const Init *I) {
576 return I->getKind() >= IK_FirstTypedInit &&
577 I->getKind() <= IK_LastTypedInit;
579 RecTy *getType() const { return Ty; }
582 convertInitializerBitRange(const std::vector<unsigned> &Bits) const override;
584 convertInitListSlice(const std::vector<unsigned> &Elements) const override;
586 /// getFieldType - This method is used to implement the FieldInit class.
587 /// Implementors of this method should return the type of the named field if
588 /// they are of record type.
590 RecTy *getFieldType(const std::string &FieldName) const override;
592 /// resolveListElementReference - This method is used to implement
593 /// VarListElementInit::resolveReferences. If the list element is resolvable
594 /// now, we return the resolved value, otherwise we return null.
595 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
596 unsigned Elt) const = 0;
599 /// UnsetInit - ? - Represents an uninitialized value
601 class UnsetInit : public Init {
602 UnsetInit() : Init(IK_UnsetInit) {}
603 UnsetInit(const UnsetInit &) = delete;
604 UnsetInit &operator=(const UnsetInit &Other) = delete;
605 void anchor() override;
608 static bool classof(const Init *I) {
609 return I->getKind() == IK_UnsetInit;
611 static UnsetInit *get();
613 Init *convertInitializerTo(RecTy *Ty) const override {
614 return Ty->convertValue(const_cast<UnsetInit *>(this));
617 Init *getBit(unsigned Bit) const override {
618 return const_cast<UnsetInit*>(this);
621 bool isComplete() const override { return false; }
622 std::string getAsString() const override { return "?"; }
625 /// BitInit - true/false - Represent a concrete initializer for a bit.
627 class BitInit : public Init {
630 explicit BitInit(bool V) : Init(IK_BitInit), Value(V) {}
631 BitInit(const BitInit &Other) = delete;
632 BitInit &operator=(BitInit &Other) = delete;
633 void anchor() override;
636 static bool classof(const Init *I) {
637 return I->getKind() == IK_BitInit;
639 static BitInit *get(bool V);
641 bool getValue() const { return Value; }
643 Init *convertInitializerTo(RecTy *Ty) const override {
644 return Ty->convertValue(const_cast<BitInit *>(this));
647 Init *getBit(unsigned Bit) const override {
648 assert(Bit < 1 && "Bit index out of range!");
649 return const_cast<BitInit*>(this);
652 std::string getAsString() const override { return Value ? "1" : "0"; }
655 /// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
656 /// It contains a vector of bits, whose size is determined by the type.
658 class BitsInit : public TypedInit, public FoldingSetNode {
659 std::vector<Init*> Bits;
661 BitsInit(ArrayRef<Init *> Range)
662 : TypedInit(IK_BitsInit, BitsRecTy::get(Range.size())),
663 Bits(Range.begin(), Range.end()) {}
665 BitsInit(const BitsInit &Other) = delete;
666 BitsInit &operator=(const BitsInit &Other) = delete;
669 static bool classof(const Init *I) {
670 return I->getKind() == IK_BitsInit;
672 static BitsInit *get(ArrayRef<Init *> Range);
674 void Profile(FoldingSetNodeID &ID) const;
676 unsigned getNumBits() const { return Bits.size(); }
678 Init *convertInitializerTo(RecTy *Ty) const override {
679 return Ty->convertValue(const_cast<BitsInit *>(this));
682 convertInitializerBitRange(const std::vector<unsigned> &Bits) const override;
684 bool isComplete() const override {
685 for (unsigned i = 0; i != getNumBits(); ++i)
686 if (!getBit(i)->isComplete()) return false;
689 bool allInComplete() const {
690 for (unsigned i = 0; i != getNumBits(); ++i)
691 if (getBit(i)->isComplete()) return false;
694 std::string getAsString() const override;
696 /// resolveListElementReference - This method is used to implement
697 /// VarListElementInit::resolveReferences. If the list element is resolvable
698 /// now, we return the resolved value, otherwise we return null.
699 Init *resolveListElementReference(Record &R, const RecordVal *RV,
700 unsigned Elt) const override {
701 llvm_unreachable("Illegal element reference off bits<n>");
704 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
706 Init *getBit(unsigned Bit) const override {
707 assert(Bit < Bits.size() && "Bit index out of range!");
712 /// IntInit - 7 - Represent an initialization by a literal integer value.
714 class IntInit : public TypedInit {
717 explicit IntInit(int64_t V)
718 : TypedInit(IK_IntInit, IntRecTy::get()), Value(V) {}
720 IntInit(const IntInit &Other) = delete;
721 IntInit &operator=(const IntInit &Other) = delete;
724 static bool classof(const Init *I) {
725 return I->getKind() == IK_IntInit;
727 static IntInit *get(int64_t V);
729 int64_t getValue() const { return Value; }
731 Init *convertInitializerTo(RecTy *Ty) const override {
732 return Ty->convertValue(const_cast<IntInit *>(this));
735 convertInitializerBitRange(const std::vector<unsigned> &Bits) const override;
737 std::string getAsString() const override;
739 /// resolveListElementReference - This method is used to implement
740 /// VarListElementInit::resolveReferences. If the list element is resolvable
741 /// now, we return the resolved value, otherwise we return null.
742 Init *resolveListElementReference(Record &R, const RecordVal *RV,
743 unsigned Elt) const override {
744 llvm_unreachable("Illegal element reference off int");
747 Init *getBit(unsigned Bit) const override {
748 return BitInit::get((Value & (1ULL << Bit)) != 0);
752 /// StringInit - "foo" - Represent an initialization by a string value.
754 class StringInit : public TypedInit {
757 explicit StringInit(const std::string &V)
758 : TypedInit(IK_StringInit, StringRecTy::get()), Value(V) {}
760 StringInit(const StringInit &Other) = delete;
761 StringInit &operator=(const StringInit &Other) = delete;
762 void anchor() override;
765 static bool classof(const Init *I) {
766 return I->getKind() == IK_StringInit;
768 static StringInit *get(StringRef);
770 const std::string &getValue() const { return Value; }
772 Init *convertInitializerTo(RecTy *Ty) const override {
773 return Ty->convertValue(const_cast<StringInit *>(this));
776 std::string getAsString() const override { return "\"" + Value + "\""; }
777 std::string getAsUnquotedString() const override { return Value; }
779 /// resolveListElementReference - This method is used to implement
780 /// VarListElementInit::resolveReferences. If the list element is resolvable
781 /// now, we return the resolved value, otherwise we return null.
782 Init *resolveListElementReference(Record &R, const RecordVal *RV,
783 unsigned Elt) const override {
784 llvm_unreachable("Illegal element reference off string");
787 Init *getBit(unsigned Bit) const override {
788 llvm_unreachable("Illegal bit reference off string");
792 /// ListInit - [AL, AH, CL] - Represent a list of defs
794 class ListInit : public TypedInit, public FoldingSetNode {
795 std::vector<Init*> Values;
798 typedef std::vector<Init*>::const_iterator const_iterator;
801 explicit ListInit(ArrayRef<Init *> Range, RecTy *EltTy)
802 : TypedInit(IK_ListInit, ListRecTy::get(EltTy)),
803 Values(Range.begin(), Range.end()) {}
805 ListInit(const ListInit &Other) = delete;
806 ListInit &operator=(const ListInit &Other) = delete;
809 static bool classof(const Init *I) {
810 return I->getKind() == IK_ListInit;
812 static ListInit *get(ArrayRef<Init *> Range, RecTy *EltTy);
814 void Profile(FoldingSetNodeID &ID) const;
816 unsigned getSize() const { return Values.size(); }
817 Init *getElement(unsigned i) const {
818 assert(i < Values.size() && "List element index out of range!");
822 Record *getElementAsRecord(unsigned i) const;
825 convertInitListSlice(const std::vector<unsigned> &Elements) const override;
827 Init *convertInitializerTo(RecTy *Ty) const override {
828 return Ty->convertValue(const_cast<ListInit *>(this));
831 /// resolveReferences - This method is used by classes that refer to other
832 /// variables which may not be defined at the time they expression is formed.
833 /// If a value is set for the variable later, this method will be called on
834 /// users of the value to allow the value to propagate out.
836 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
838 std::string getAsString() const override;
840 ArrayRef<Init*> getValues() const { return Values; }
842 inline const_iterator begin() const { return Values.begin(); }
843 inline const_iterator end () const { return Values.end(); }
845 inline bool empty() const { return Values.empty(); }
847 /// resolveListElementReference - This method is used to implement
848 /// VarListElementInit::resolveReferences. If the list element is resolvable
849 /// now, we return the resolved value, otherwise we return null.
850 Init *resolveListElementReference(Record &R, const RecordVal *RV,
851 unsigned Elt) const override;
853 Init *getBit(unsigned Bit) const override {
854 llvm_unreachable("Illegal bit reference off list");
858 /// OpInit - Base class for operators
860 class OpInit : public TypedInit {
861 OpInit(const OpInit &Other) = delete;
862 OpInit &operator=(OpInit &Other) = delete;
865 explicit OpInit(InitKind K, RecTy *Type) : TypedInit(K, Type) {}
868 static bool classof(const Init *I) {
869 return I->getKind() >= IK_FirstOpInit &&
870 I->getKind() <= IK_LastOpInit;
872 // Clone - Clone this operator, replacing arguments with the new list
873 virtual OpInit *clone(std::vector<Init *> &Operands) const = 0;
875 virtual int getNumOperands() const = 0;
876 virtual Init *getOperand(int i) const = 0;
878 // Fold - If possible, fold this to a simpler init. Return this if not
880 virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const = 0;
882 Init *convertInitializerTo(RecTy *Ty) const override {
883 return Ty->convertValue(const_cast<OpInit *>(this));
886 Init *resolveListElementReference(Record &R, const RecordVal *RV,
887 unsigned Elt) const override;
889 Init *getBit(unsigned Bit) const override;
892 /// UnOpInit - !op (X) - Transform an init.
894 class UnOpInit : public OpInit {
896 enum UnaryOp { CAST, HEAD, TAIL, EMPTY };
902 UnOpInit(UnaryOp opc, Init *lhs, RecTy *Type)
903 : OpInit(IK_UnOpInit, Type), Opc(opc), LHS(lhs) {}
905 UnOpInit(const UnOpInit &Other) = delete;
906 UnOpInit &operator=(const UnOpInit &Other) = delete;
909 static bool classof(const Init *I) {
910 return I->getKind() == IK_UnOpInit;
912 static UnOpInit *get(UnaryOp opc, Init *lhs, RecTy *Type);
914 // Clone - Clone this operator, replacing arguments with the new list
915 OpInit *clone(std::vector<Init *> &Operands) const override {
916 assert(Operands.size() == 1 &&
917 "Wrong number of operands for unary operation");
918 return UnOpInit::get(getOpcode(), *Operands.begin(), getType());
921 int getNumOperands() const override { return 1; }
922 Init *getOperand(int i) const override {
923 assert(i == 0 && "Invalid operand id for unary operator");
927 UnaryOp getOpcode() const { return Opc; }
928 Init *getOperand() const { return LHS; }
930 // Fold - If possible, fold this to a simpler init. Return this if not
932 Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const override;
934 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
936 std::string getAsString() const override;
939 /// BinOpInit - !op (X, Y) - Combine two inits.
941 class BinOpInit : public OpInit {
943 enum BinaryOp { ADD, AND, SHL, SRA, SRL, LISTCONCAT, STRCONCAT, CONCAT, EQ };
949 BinOpInit(BinaryOp opc, Init *lhs, Init *rhs, RecTy *Type) :
950 OpInit(IK_BinOpInit, Type), Opc(opc), LHS(lhs), RHS(rhs) {}
952 BinOpInit(const BinOpInit &Other) = delete;
953 BinOpInit &operator=(const BinOpInit &Other) = delete;
956 static bool classof(const Init *I) {
957 return I->getKind() == IK_BinOpInit;
959 static BinOpInit *get(BinaryOp opc, Init *lhs, Init *rhs,
962 // Clone - Clone this operator, replacing arguments with the new list
963 OpInit *clone(std::vector<Init *> &Operands) const override {
964 assert(Operands.size() == 2 &&
965 "Wrong number of operands for binary operation");
966 return BinOpInit::get(getOpcode(), Operands[0], Operands[1], getType());
969 int getNumOperands() const override { return 2; }
970 Init *getOperand(int i) const override {
971 assert((i == 0 || i == 1) && "Invalid operand id for binary operator");
979 BinaryOp getOpcode() const { return Opc; }
980 Init *getLHS() const { return LHS; }
981 Init *getRHS() const { return RHS; }
983 // Fold - If possible, fold this to a simpler init. Return this if not
985 Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const override;
987 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
989 std::string getAsString() const override;
992 /// TernOpInit - !op (X, Y, Z) - Combine two inits.
994 class TernOpInit : public OpInit {
996 enum TernaryOp { SUBST, FOREACH, IF };
1000 Init *LHS, *MHS, *RHS;
1002 TernOpInit(TernaryOp opc, Init *lhs, Init *mhs, Init *rhs,
1004 OpInit(IK_TernOpInit, Type), Opc(opc), LHS(lhs), MHS(mhs), RHS(rhs) {}
1006 TernOpInit(const TernOpInit &Other) = delete;
1007 TernOpInit &operator=(const TernOpInit &Other) = delete;
1010 static bool classof(const Init *I) {
1011 return I->getKind() == IK_TernOpInit;
1013 static TernOpInit *get(TernaryOp opc, Init *lhs,
1014 Init *mhs, Init *rhs,
1017 // Clone - Clone this operator, replacing arguments with the new list
1018 OpInit *clone(std::vector<Init *> &Operands) const override {
1019 assert(Operands.size() == 3 &&
1020 "Wrong number of operands for ternary operation");
1021 return TernOpInit::get(getOpcode(), Operands[0], Operands[1], Operands[2],
1025 int getNumOperands() const override { return 3; }
1026 Init *getOperand(int i) const override {
1027 assert((i == 0 || i == 1 || i == 2) &&
1028 "Invalid operand id for ternary operator");
1031 } else if (i == 1) {
1038 TernaryOp getOpcode() const { return Opc; }
1039 Init *getLHS() const { return LHS; }
1040 Init *getMHS() const { return MHS; }
1041 Init *getRHS() const { return RHS; }
1043 // Fold - If possible, fold this to a simpler init. Return this if not
1044 // possible to fold.
1045 Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const override;
1047 bool isComplete() const override { return false; }
1049 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
1051 std::string getAsString() const override;
1054 /// VarInit - 'Opcode' - Represent a reference to an entire variable object.
1056 class VarInit : public TypedInit {
1059 explicit VarInit(const std::string &VN, RecTy *T)
1060 : TypedInit(IK_VarInit, T), VarName(StringInit::get(VN)) {}
1061 explicit VarInit(Init *VN, RecTy *T)
1062 : TypedInit(IK_VarInit, T), VarName(VN) {}
1064 VarInit(const VarInit &Other) = delete;
1065 VarInit &operator=(const VarInit &Other) = delete;
1068 static bool classof(const Init *I) {
1069 return I->getKind() == IK_VarInit;
1071 static VarInit *get(const std::string &VN, RecTy *T);
1072 static VarInit *get(Init *VN, RecTy *T);
1074 Init *convertInitializerTo(RecTy *Ty) const override {
1075 return Ty->convertValue(const_cast<VarInit *>(this));
1078 const std::string &getName() const;
1079 Init *getNameInit() const { return VarName; }
1080 std::string getNameInitAsString() const {
1081 return getNameInit()->getAsUnquotedString();
1084 Init *resolveListElementReference(Record &R, const RecordVal *RV,
1085 unsigned Elt) const override;
1087 RecTy *getFieldType(const std::string &FieldName) const override;
1088 Init *getFieldInit(Record &R, const RecordVal *RV,
1089 const std::string &FieldName) const override;
1091 /// resolveReferences - This method is used by classes that refer to other
1092 /// variables which may not be defined at the time they expression is formed.
1093 /// If a value is set for the variable later, this method will be called on
1094 /// users of the value to allow the value to propagate out.
1096 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
1098 Init *getBit(unsigned Bit) const override;
1100 std::string getAsString() const override { return getName(); }
1103 /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
1105 class VarBitInit : public Init {
1109 VarBitInit(TypedInit *T, unsigned B) : Init(IK_VarBitInit), TI(T), Bit(B) {
1110 assert(T->getType() &&
1111 (isa<IntRecTy>(T->getType()) ||
1112 (isa<BitsRecTy>(T->getType()) &&
1113 cast<BitsRecTy>(T->getType())->getNumBits() > B)) &&
1114 "Illegal VarBitInit expression!");
1117 VarBitInit(const VarBitInit &Other) = delete;
1118 VarBitInit &operator=(const VarBitInit &Other) = delete;
1121 static bool classof(const Init *I) {
1122 return I->getKind() == IK_VarBitInit;
1124 static VarBitInit *get(TypedInit *T, unsigned B);
1126 Init *convertInitializerTo(RecTy *Ty) const override {
1127 return Ty->convertValue(const_cast<VarBitInit *>(this));
1130 Init *getBitVar() const override { return TI; }
1131 unsigned getBitNum() const override { return Bit; }
1133 std::string getAsString() const override;
1134 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
1136 Init *getBit(unsigned B) const override {
1137 assert(B < 1 && "Bit index out of range!");
1138 return const_cast<VarBitInit*>(this);
1142 /// VarListElementInit - List[4] - Represent access to one element of a var or
1144 class VarListElementInit : public TypedInit {
1148 VarListElementInit(TypedInit *T, unsigned E)
1149 : TypedInit(IK_VarListElementInit,
1150 cast<ListRecTy>(T->getType())->getElementType()),
1152 assert(T->getType() && isa<ListRecTy>(T->getType()) &&
1153 "Illegal VarBitInit expression!");
1156 VarListElementInit(const VarListElementInit &Other) = delete;
1157 void operator=(const VarListElementInit &Other) = delete;
1160 static bool classof(const Init *I) {
1161 return I->getKind() == IK_VarListElementInit;
1163 static VarListElementInit *get(TypedInit *T, unsigned E);
1165 Init *convertInitializerTo(RecTy *Ty) const override {
1166 return Ty->convertValue(const_cast<VarListElementInit *>(this));
1169 TypedInit *getVariable() const { return TI; }
1170 unsigned getElementNum() const { return Element; }
1172 /// resolveListElementReference - This method is used to implement
1173 /// VarListElementInit::resolveReferences. If the list element is resolvable
1174 /// now, we return the resolved value, otherwise we return null.
1175 Init *resolveListElementReference(Record &R, const RecordVal *RV,
1176 unsigned Elt) const override;
1178 std::string getAsString() const override;
1179 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
1181 Init *getBit(unsigned Bit) const override;
1184 /// DefInit - AL - Represent a reference to a 'def' in the description
1186 class DefInit : public TypedInit {
1189 DefInit(Record *D, RecordRecTy *T) : TypedInit(IK_DefInit, T), Def(D) {}
1190 friend class Record;
1192 DefInit(const DefInit &Other) = delete;
1193 DefInit &operator=(const DefInit &Other) = delete;
1196 static bool classof(const Init *I) {
1197 return I->getKind() == IK_DefInit;
1199 static DefInit *get(Record*);
1201 Init *convertInitializerTo(RecTy *Ty) const override {
1202 return Ty->convertValue(const_cast<DefInit *>(this));
1205 Record *getDef() const { return Def; }
1207 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
1209 RecTy *getFieldType(const std::string &FieldName) const override;
1210 Init *getFieldInit(Record &R, const RecordVal *RV,
1211 const std::string &FieldName) const override;
1213 std::string getAsString() const override;
1215 Init *getBit(unsigned Bit) const override {
1216 llvm_unreachable("Illegal bit reference off def");
1219 /// resolveListElementReference - This method is used to implement
1220 /// VarListElementInit::resolveReferences. If the list element is resolvable
1221 /// now, we return the resolved value, otherwise we return null.
1222 Init *resolveListElementReference(Record &R, const RecordVal *RV,
1223 unsigned Elt) const override {
1224 llvm_unreachable("Illegal element reference off def");
1228 /// FieldInit - X.Y - Represent a reference to a subfield of a variable
1230 class FieldInit : public TypedInit {
1231 Init *Rec; // Record we are referring to
1232 std::string FieldName; // Field we are accessing
1234 FieldInit(Init *R, const std::string &FN)
1235 : TypedInit(IK_FieldInit, R->getFieldType(FN)), Rec(R), FieldName(FN) {
1236 assert(getType() && "FieldInit with non-record type!");
1239 FieldInit(const FieldInit &Other) = delete;
1240 FieldInit &operator=(const FieldInit &Other) = delete;
1243 static bool classof(const Init *I) {
1244 return I->getKind() == IK_FieldInit;
1246 static FieldInit *get(Init *R, const std::string &FN);
1247 static FieldInit *get(Init *R, const Init *FN);
1249 Init *convertInitializerTo(RecTy *Ty) const override {
1250 return Ty->convertValue(const_cast<FieldInit *>(this));
1253 Init *getBit(unsigned Bit) const override;
1255 Init *resolveListElementReference(Record &R, const RecordVal *RV,
1256 unsigned Elt) const override;
1258 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
1260 std::string getAsString() const override {
1261 return Rec->getAsString() + "." + FieldName;
1265 /// DagInit - (v a, b) - Represent a DAG tree value. DAG inits are required
1266 /// to have at least one value then a (possibly empty) list of arguments. Each
1267 /// argument can have a name associated with it.
1269 class DagInit : public TypedInit, public FoldingSetNode {
1271 std::string ValName;
1272 std::vector<Init*> Args;
1273 std::vector<std::string> ArgNames;
1275 DagInit(Init *V, const std::string &VN,
1276 ArrayRef<Init *> ArgRange,
1277 ArrayRef<std::string> NameRange)
1278 : TypedInit(IK_DagInit, DagRecTy::get()), Val(V), ValName(VN),
1279 Args(ArgRange.begin(), ArgRange.end()),
1280 ArgNames(NameRange.begin(), NameRange.end()) {}
1282 DagInit(const DagInit &Other) = delete;
1283 DagInit &operator=(const DagInit &Other) = delete;
1286 static bool classof(const Init *I) {
1287 return I->getKind() == IK_DagInit;
1289 static DagInit *get(Init *V, const std::string &VN,
1290 ArrayRef<Init *> ArgRange,
1291 ArrayRef<std::string> NameRange);
1292 static DagInit *get(Init *V, const std::string &VN,
1294 std::pair<Init*, std::string> > &args);
1296 void Profile(FoldingSetNodeID &ID) const;
1298 Init *convertInitializerTo(RecTy *Ty) const override {
1299 return Ty->convertValue(const_cast<DagInit *>(this));
1302 Init *getOperator() const { return Val; }
1304 const std::string &getName() const { return ValName; }
1306 unsigned getNumArgs() const { return Args.size(); }
1307 Init *getArg(unsigned Num) const {
1308 assert(Num < Args.size() && "Arg number out of range!");
1311 const std::string &getArgName(unsigned Num) const {
1312 assert(Num < ArgNames.size() && "Arg number out of range!");
1313 return ArgNames[Num];
1316 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
1318 std::string getAsString() const override;
1320 typedef std::vector<Init*>::const_iterator const_arg_iterator;
1321 typedef std::vector<std::string>::const_iterator const_name_iterator;
1323 inline const_arg_iterator arg_begin() const { return Args.begin(); }
1324 inline const_arg_iterator arg_end () const { return Args.end(); }
1326 inline size_t arg_size () const { return Args.size(); }
1327 inline bool arg_empty() const { return Args.empty(); }
1329 inline const_name_iterator name_begin() const { return ArgNames.begin(); }
1330 inline const_name_iterator name_end () const { return ArgNames.end(); }
1332 inline size_t name_size () const { return ArgNames.size(); }
1333 inline bool name_empty() const { return ArgNames.empty(); }
1335 Init *getBit(unsigned Bit) const override {
1336 llvm_unreachable("Illegal bit reference off dag");
1339 Init *resolveListElementReference(Record &R, const RecordVal *RV,
1340 unsigned Elt) const override {
1341 llvm_unreachable("Illegal element reference off dag");
1345 //===----------------------------------------------------------------------===//
1346 // High-Level Classes
1347 //===----------------------------------------------------------------------===//
1356 RecordVal(Init *N, RecTy *T, unsigned P);
1357 RecordVal(const std::string &N, RecTy *T, unsigned P);
1359 const std::string &getName() const;
1360 const Init *getNameInit() const { return Name; }
1361 std::string getNameInitAsString() const {
1362 return getNameInit()->getAsUnquotedString();
1365 unsigned getPrefix() const { return Prefix; }
1366 RecTy *getType() const { return Ty; }
1367 Init *getValue() const { return Value; }
1369 bool setValue(Init *V) {
1371 Value = V->convertInitializerTo(Ty);
1372 return Value == nullptr;
1379 void print(raw_ostream &OS, bool PrintSem = true) const;
1382 inline raw_ostream &operator<<(raw_ostream &OS, const RecordVal &RV) {
1383 RV.print(OS << " ");
1388 static unsigned LastID;
1390 // Unique record ID.
1393 // Location where record was instantiated, followed by the location of
1394 // multiclass prototypes used.
1395 SmallVector<SMLoc, 4> Locs;
1396 std::vector<Init *> TemplateArgs;
1397 std::vector<RecordVal> Values;
1398 std::vector<Record *> SuperClasses;
1399 std::vector<SMRange> SuperClassRanges;
1401 // Tracks Record instances. Not owned by Record.
1402 RecordKeeper &TrackedRecords;
1407 // Class-instance values can be used by other defs. For example, Struct<i>
1408 // is used here as a template argument to another class:
1410 // multiclass MultiClass<int i> {
1411 // def Def : Class<Struct<i>>;
1413 // These need to get fully resolved before instantiating any other
1414 // definitions that usie them (e.g. Def). However, inside a multiclass they
1415 // can't be immediately resolved so we mark them ResolveFirst to fully
1416 // resolve them later as soon as the multiclass is instantiated.
1423 // Constructs a record.
1424 explicit Record(const std::string &N, ArrayRef<SMLoc> locs,
1425 RecordKeeper &records, bool Anonymous = false) :
1426 ID(LastID++), Name(StringInit::get(N)), Locs(locs.begin(), locs.end()),
1427 TrackedRecords(records), TheInit(nullptr), IsAnonymous(Anonymous),
1428 ResolveFirst(false) {
1431 explicit Record(Init *N, ArrayRef<SMLoc> locs, RecordKeeper &records,
1432 bool Anonymous = false) :
1433 ID(LastID++), Name(N), Locs(locs.begin(), locs.end()),
1434 TrackedRecords(records), TheInit(nullptr), IsAnonymous(Anonymous),
1435 ResolveFirst(false) {
1439 // When copy-constructing a Record, we must still guarantee a globally unique
1440 // ID number. All other fields can be copied normally.
1441 Record(const Record &O) :
1442 ID(LastID++), Name(O.Name), Locs(O.Locs), TemplateArgs(O.TemplateArgs),
1443 Values(O.Values), SuperClasses(O.SuperClasses),
1444 SuperClassRanges(O.SuperClassRanges), TrackedRecords(O.TrackedRecords),
1445 TheInit(O.TheInit), IsAnonymous(O.IsAnonymous),
1446 ResolveFirst(O.ResolveFirst) { }
1448 static unsigned getNewUID() { return LastID++; }
1450 unsigned getID() const { return ID; }
1452 const std::string &getName() const;
1453 Init *getNameInit() const {
1456 const std::string getNameInitAsString() const {
1457 return getNameInit()->getAsUnquotedString();
1460 void setName(Init *Name); // Also updates RecordKeeper.
1461 void setName(const std::string &Name); // Also updates RecordKeeper.
1463 ArrayRef<SMLoc> getLoc() const { return Locs; }
1465 /// get the corresponding DefInit.
1466 DefInit *getDefInit();
1468 const std::vector<Init *> &getTemplateArgs() const {
1469 return TemplateArgs;
1471 const std::vector<RecordVal> &getValues() const { return Values; }
1472 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; }
1473 ArrayRef<SMRange> getSuperClassRanges() const { return SuperClassRanges; }
1475 bool isTemplateArg(Init *Name) const {
1476 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
1477 if (TemplateArgs[i] == Name) return true;
1480 bool isTemplateArg(StringRef Name) const {
1481 return isTemplateArg(StringInit::get(Name));
1484 const RecordVal *getValue(const Init *Name) const {
1485 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1486 if (Values[i].getNameInit() == Name) return &Values[i];
1489 const RecordVal *getValue(StringRef Name) const {
1490 return getValue(StringInit::get(Name));
1492 RecordVal *getValue(const Init *Name) {
1493 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1494 if (Values[i].getNameInit() == Name) return &Values[i];
1497 RecordVal *getValue(StringRef Name) {
1498 return getValue(StringInit::get(Name));
1501 void addTemplateArg(Init *Name) {
1502 assert(!isTemplateArg(Name) && "Template arg already defined!");
1503 TemplateArgs.push_back(Name);
1505 void addTemplateArg(StringRef Name) {
1506 addTemplateArg(StringInit::get(Name));
1509 void addValue(const RecordVal &RV) {
1510 assert(getValue(RV.getNameInit()) == nullptr && "Value already added!");
1511 Values.push_back(RV);
1512 if (Values.size() > 1)
1513 // Keep NAME at the end of the list. It makes record dumps a
1514 // bit prettier and allows TableGen tests to be written more
1515 // naturally. Tests can use CHECK-NEXT to look for Record
1516 // fields they expect to see after a def. They can't do that if
1517 // NAME is the first Record field.
1518 std::swap(Values[Values.size() - 2], Values[Values.size() - 1]);
1521 void removeValue(Init *Name) {
1522 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1523 if (Values[i].getNameInit() == Name) {
1524 Values.erase(Values.begin()+i);
1527 llvm_unreachable("Cannot remove an entry that does not exist!");
1530 void removeValue(StringRef Name) {
1531 removeValue(StringInit::get(Name));
1534 bool isSubClassOf(const Record *R) const {
1535 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1536 if (SuperClasses[i] == R)
1541 bool isSubClassOf(StringRef Name) const {
1542 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1543 if (SuperClasses[i]->getNameInitAsString() == Name)
1548 void addSuperClass(Record *R, SMRange Range) {
1549 assert(!isSubClassOf(R) && "Already subclassing record!");
1550 SuperClasses.push_back(R);
1551 SuperClassRanges.push_back(Range);
1554 /// resolveReferences - If there are any field references that refer to fields
1555 /// that have been filled in, we can propagate the values now.
1557 void resolveReferences() { resolveReferencesTo(nullptr); }
1559 /// resolveReferencesTo - If anything in this record refers to RV, replace the
1560 /// reference to RV with the RHS of RV. If RV is null, we resolve all
1561 /// possible references.
1562 void resolveReferencesTo(const RecordVal *RV);
1564 RecordKeeper &getRecords() const {
1565 return TrackedRecords;
1568 bool isAnonymous() const {
1572 bool isResolveFirst() const {
1573 return ResolveFirst;
1576 void setResolveFirst(bool b) {
1582 //===--------------------------------------------------------------------===//
1583 // High-level methods useful to tablegen back-ends
1586 /// getValueInit - Return the initializer for a value with the specified name,
1587 /// or throw an exception if the field does not exist.
1589 Init *getValueInit(StringRef FieldName) const;
1591 /// Return true if the named field is unset.
1592 bool isValueUnset(StringRef FieldName) const {
1593 return getValueInit(FieldName) == UnsetInit::get();
1596 /// getValueAsString - This method looks up the specified field and returns
1597 /// its value as a string, throwing an exception if the field does not exist
1598 /// or if the value is not a string.
1600 std::string getValueAsString(StringRef FieldName) const;
1602 /// getValueAsBitsInit - This method looks up the specified field and returns
1603 /// its value as a BitsInit, throwing an exception if the field does not exist
1604 /// or if the value is not the right type.
1606 BitsInit *getValueAsBitsInit(StringRef FieldName) const;
1608 /// getValueAsListInit - This method looks up the specified field and returns
1609 /// its value as a ListInit, throwing an exception if the field does not exist
1610 /// or if the value is not the right type.
1612 ListInit *getValueAsListInit(StringRef FieldName) const;
1614 /// getValueAsListOfDefs - This method looks up the specified field and
1615 /// returns its value as a vector of records, throwing an exception if the
1616 /// field does not exist or if the value is not the right type.
1618 std::vector<Record*> getValueAsListOfDefs(StringRef FieldName) const;
1620 /// getValueAsListOfInts - This method looks up the specified field and
1621 /// returns its value as a vector of integers, throwing an exception if the
1622 /// field does not exist or if the value is not the right type.
1624 std::vector<int64_t> getValueAsListOfInts(StringRef FieldName) const;
1626 /// getValueAsListOfStrings - This method looks up the specified field and
1627 /// returns its value as a vector of strings, throwing an exception if the
1628 /// field does not exist or if the value is not the right type.
1630 std::vector<std::string> getValueAsListOfStrings(StringRef FieldName) const;
1632 /// getValueAsDef - This method looks up the specified field and returns its
1633 /// value as a Record, throwing an exception if the field does not exist or if
1634 /// the value is not the right type.
1636 Record *getValueAsDef(StringRef FieldName) const;
1638 /// getValueAsBit - This method looks up the specified field and returns its
1639 /// value as a bit, throwing an exception if the field does not exist or if
1640 /// the value is not the right type.
1642 bool getValueAsBit(StringRef FieldName) const;
1644 /// getValueAsBitOrUnset - This method looks up the specified field and
1645 /// returns its value as a bit. If the field is unset, sets Unset to true and
1648 bool getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const;
1650 /// getValueAsInt - This method looks up the specified field and returns its
1651 /// value as an int64_t, throwing an exception if the field does not exist or
1652 /// if the value is not the right type.
1654 int64_t getValueAsInt(StringRef FieldName) const;
1656 /// getValueAsDag - This method looks up the specified field and returns its
1657 /// value as an Dag, throwing an exception if the field does not exist or if
1658 /// the value is not the right type.
1660 DagInit *getValueAsDag(StringRef FieldName) const;
1663 raw_ostream &operator<<(raw_ostream &OS, const Record &R);
1666 Record Rec; // Placeholder for template args and Name.
1667 typedef std::vector<std::unique_ptr<Record>> RecordVector;
1668 RecordVector DefPrototypes;
1672 MultiClass(const std::string &Name, SMLoc Loc, RecordKeeper &Records) :
1673 Rec(Name, Loc, Records) {}
1676 class RecordKeeper {
1677 typedef std::map<std::string, std::unique_ptr<Record>> RecordMap;
1678 RecordMap Classes, Defs;
1681 const RecordMap &getClasses() const { return Classes; }
1682 const RecordMap &getDefs() const { return Defs; }
1684 Record *getClass(const std::string &Name) const {
1685 auto I = Classes.find(Name);
1686 return I == Classes.end() ? nullptr : I->second.get();
1688 Record *getDef(const std::string &Name) const {
1689 auto I = Defs.find(Name);
1690 return I == Defs.end() ? nullptr : I->second.get();
1692 void addClass(std::unique_ptr<Record> R) {
1693 bool Ins = Classes.insert(std::make_pair(R->getName(),
1694 std::move(R))).second;
1696 assert(Ins && "Class already exists");
1698 void addDef(std::unique_ptr<Record> R) {
1699 bool Ins = Defs.insert(std::make_pair(R->getName(),
1700 std::move(R))).second;
1702 assert(Ins && "Record already exists");
1705 //===--------------------------------------------------------------------===//
1706 // High-level helper methods, useful for tablegen backends...
1708 /// getAllDerivedDefinitions - This method returns all concrete definitions
1709 /// that derive from the specified class name. If a class with the specified
1710 /// name does not exist, an exception is thrown.
1711 std::vector<Record*>
1712 getAllDerivedDefinitions(const std::string &ClassName) const;
1717 /// LessRecord - Sorting predicate to sort record pointers by name.
1720 bool operator()(const Record *Rec1, const Record *Rec2) const {
1721 return StringRef(Rec1->getName()).compare_numeric(Rec2->getName()) < 0;
1725 /// LessRecordByID - Sorting predicate to sort record pointers by their
1726 /// unique ID. If you just need a deterministic order, use this, since it
1727 /// just compares two `unsigned`; the other sorting predicates require
1728 /// string manipulation.
1729 struct LessRecordByID {
1730 bool operator()(const Record *LHS, const Record *RHS) const {
1731 return LHS->getID() < RHS->getID();
1735 /// LessRecordFieldName - Sorting predicate to sort record pointers by their
1738 struct LessRecordFieldName {
1739 bool operator()(const Record *Rec1, const Record *Rec2) const {
1740 return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name");
1744 struct LessRecordRegister {
1745 static size_t min(size_t a, size_t b) { return a < b ? a : b; }
1746 static bool ascii_isdigit(char x) { return x >= '0' && x <= '9'; }
1748 struct RecordParts {
1749 SmallVector<std::pair< bool, StringRef>, 4> Parts;
1751 RecordParts(StringRef Rec) {
1756 const char *Start = Rec.data();
1757 const char *Curr = Start;
1758 bool isDigitPart = ascii_isdigit(Curr[0]);
1759 for (size_t I = 0, E = Rec.size(); I != E; ++I, ++Len) {
1760 bool isDigit = ascii_isdigit(Curr[I]);
1761 if (isDigit != isDigitPart) {
1762 Parts.push_back(std::make_pair(isDigitPart, StringRef(Start, Len)));
1765 isDigitPart = ascii_isdigit(Curr[I]);
1768 // Push the last part.
1769 Parts.push_back(std::make_pair(isDigitPart, StringRef(Start, Len)));
1772 size_t size() { return Parts.size(); }
1774 std::pair<bool, StringRef> getPart(size_t i) {
1775 assert (i < Parts.size() && "Invalid idx!");
1780 bool operator()(const Record *Rec1, const Record *Rec2) const {
1781 RecordParts LHSParts(StringRef(Rec1->getName()));
1782 RecordParts RHSParts(StringRef(Rec2->getName()));
1784 size_t LHSNumParts = LHSParts.size();
1785 size_t RHSNumParts = RHSParts.size();
1786 assert (LHSNumParts && RHSNumParts && "Expected at least one part!");
1788 if (LHSNumParts != RHSNumParts)
1789 return LHSNumParts < RHSNumParts;
1791 // We expect the registers to be of the form [_a-zA-z]+([0-9]*[_a-zA-Z]*)*.
1792 for (size_t I = 0, E = LHSNumParts; I < E; I+=2) {
1793 std::pair<bool, StringRef> LHSPart = LHSParts.getPart(I);
1794 std::pair<bool, StringRef> RHSPart = RHSParts.getPart(I);
1795 // Expect even part to always be alpha.
1796 assert (LHSPart.first == false && RHSPart.first == false &&
1797 "Expected both parts to be alpha.");
1798 if (int Res = LHSPart.second.compare(RHSPart.second))
1801 for (size_t I = 1, E = LHSNumParts; I < E; I+=2) {
1802 std::pair<bool, StringRef> LHSPart = LHSParts.getPart(I);
1803 std::pair<bool, StringRef> RHSPart = RHSParts.getPart(I);
1804 // Expect odd part to always be numeric.
1805 assert (LHSPart.first == true && RHSPart.first == true &&
1806 "Expected both parts to be numeric.");
1807 if (LHSPart.second.size() != RHSPart.second.size())
1808 return LHSPart.second.size() < RHSPart.second.size();
1810 unsigned LHSVal, RHSVal;
1812 bool LHSFailed = LHSPart.second.getAsInteger(10, LHSVal); (void)LHSFailed;
1813 assert(!LHSFailed && "Unable to convert LHS to integer.");
1814 bool RHSFailed = RHSPart.second.getAsInteger(10, RHSVal); (void)RHSFailed;
1815 assert(!RHSFailed && "Unable to convert RHS to integer.");
1817 if (LHSVal != RHSVal)
1818 return LHSVal < RHSVal;
1820 return LHSNumParts < RHSNumParts;
1824 raw_ostream &operator<<(raw_ostream &OS, const RecordKeeper &RK);
1826 /// QualifyName - Return an Init with a qualifier prefix referring
1827 /// to CurRec's name.
1828 Init *QualifyName(Record &CurRec, MultiClass *CurMultiClass,
1829 Init *Name, const std::string &Scoper);
1831 /// QualifyName - Return an Init with a qualifier prefix referring
1832 /// to CurRec's name.
1833 Init *QualifyName(Record &CurRec, MultiClass *CurMultiClass,
1834 const std::string &Name, const std::string &Scoper);
1836 } // End llvm namespace