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"
57 class VarListElementInit;
65 //===----------------------------------------------------------------------===//
67 //===----------------------------------------------------------------------===//
71 /// \brief Subclass discriminator (for dyn_cast<> et al.)
84 std::unique_ptr<ListRecTy> ListTy;
85 virtual void anchor();
88 RecTyKind getRecTyKind() const { return Kind; }
90 RecTy(RecTyKind K) : Kind(K), ListTy(nullptr) {}
93 virtual std::string getAsString() const = 0;
94 void print(raw_ostream &OS) const { OS << getAsString(); }
97 /// typeIsConvertibleTo - Return true if all values of 'this' type can be
98 /// converted to the specified type.
99 virtual bool typeIsConvertibleTo(const RecTy *RHS) const = 0;
101 /// getListTy - Returns the type representing list<this>.
102 ListRecTy *getListTy();
104 public: // These methods should only be called from subclasses of Init
105 virtual Init *convertValue( UnsetInit *UI) { return nullptr; }
106 virtual Init *convertValue( BitInit *BI) { return nullptr; }
107 virtual Init *convertValue( BitsInit *BI) { return nullptr; }
108 virtual Init *convertValue( IntInit *II) { return nullptr; }
109 virtual Init *convertValue(StringInit *SI) { return nullptr; }
110 virtual Init *convertValue( ListInit *LI) { return nullptr; }
111 virtual Init *convertValue( UnOpInit *UI) {
112 return convertValue((TypedInit*)UI);
114 virtual Init *convertValue( BinOpInit *UI) {
115 return convertValue((TypedInit*)UI);
117 virtual Init *convertValue( TernOpInit *UI) {
118 return convertValue((TypedInit*)UI);
120 virtual Init *convertValue(VarBitInit *VB) { return nullptr; }
121 virtual Init *convertValue( DefInit *DI) { return nullptr; }
122 virtual Init *convertValue( DagInit *DI) { return nullptr; }
123 virtual Init *convertValue( TypedInit *TI) { return nullptr; }
124 virtual Init *convertValue( VarInit *VI) {
125 return convertValue((TypedInit*)VI);
127 virtual Init *convertValue( FieldInit *FI) {
128 return convertValue((TypedInit*)FI);
132 virtual bool baseClassOf(const RecTy*) const;
135 inline raw_ostream &operator<<(raw_ostream &OS, const RecTy &Ty) {
140 /// BitRecTy - 'bit' - Represent a single bit
142 class BitRecTy : public RecTy {
143 static BitRecTy Shared;
144 BitRecTy() : RecTy(BitRecTyKind) {}
147 static bool classof(const RecTy *RT) {
148 return RT->getRecTyKind() == BitRecTyKind;
151 static BitRecTy *get() { return &Shared; }
153 Init *convertValue( UnsetInit *UI) override { return (Init*)UI; }
154 Init *convertValue( BitInit *BI) override { return (Init*)BI; }
155 Init *convertValue( BitsInit *BI) override;
156 Init *convertValue( IntInit *II) override;
157 Init *convertValue(StringInit *SI) override { return nullptr; }
158 Init *convertValue( ListInit *LI) override { return nullptr; }
159 Init *convertValue(VarBitInit *VB) override { return (Init*)VB; }
160 Init *convertValue( DefInit *DI) override { return nullptr; }
161 Init *convertValue( DagInit *DI) override { return nullptr; }
162 Init *convertValue( UnOpInit *UI) override { return RecTy::convertValue(UI);}
163 Init *convertValue( BinOpInit *UI) override { return RecTy::convertValue(UI);}
164 Init *convertValue(TernOpInit *UI) override { return RecTy::convertValue(UI);}
165 Init *convertValue( TypedInit *TI) override;
166 Init *convertValue( VarInit *VI) override { return RecTy::convertValue(VI);}
167 Init *convertValue( FieldInit *FI) override { return RecTy::convertValue(FI);}
169 std::string getAsString() const override { return "bit"; }
171 bool typeIsConvertibleTo(const RecTy *RHS) const override {
172 return RHS->baseClassOf(this);
174 bool baseClassOf(const RecTy*) const override;
177 /// BitsRecTy - 'bits<n>' - Represent a fixed number of bits
179 class BitsRecTy : public RecTy {
181 explicit BitsRecTy(unsigned Sz) : RecTy(BitsRecTyKind), Size(Sz) {}
184 static bool classof(const RecTy *RT) {
185 return RT->getRecTyKind() == BitsRecTyKind;
188 static BitsRecTy *get(unsigned Sz);
190 unsigned getNumBits() const { return Size; }
192 Init *convertValue( UnsetInit *UI) override;
193 Init *convertValue( BitInit *UI) override;
194 Init *convertValue( BitsInit *BI) override;
195 Init *convertValue( IntInit *II) override;
196 Init *convertValue(StringInit *SI) override { return nullptr; }
197 Init *convertValue( ListInit *LI) override { return nullptr; }
198 Init *convertValue(VarBitInit *VB) override { return nullptr; }
199 Init *convertValue( DefInit *DI) override { return nullptr; }
200 Init *convertValue( DagInit *DI) override { return nullptr; }
201 Init *convertValue( UnOpInit *UI) override { return RecTy::convertValue(UI);}
202 Init *convertValue( BinOpInit *UI) override { return RecTy::convertValue(UI);}
203 Init *convertValue(TernOpInit *UI) override { return RecTy::convertValue(UI);}
204 Init *convertValue( TypedInit *TI) override;
205 Init *convertValue( VarInit *VI) override { return RecTy::convertValue(VI);}
206 Init *convertValue( FieldInit *FI) override { return RecTy::convertValue(FI);}
208 std::string getAsString() const override;
210 bool typeIsConvertibleTo(const RecTy *RHS) const override {
211 return RHS->baseClassOf(this);
213 bool baseClassOf(const RecTy*) const override;
216 /// IntRecTy - 'int' - Represent an integer value of no particular size
218 class IntRecTy : public RecTy {
219 static IntRecTy Shared;
220 IntRecTy() : RecTy(IntRecTyKind) {}
223 static bool classof(const RecTy *RT) {
224 return RT->getRecTyKind() == IntRecTyKind;
227 static IntRecTy *get() { return &Shared; }
229 Init *convertValue( UnsetInit *UI) override { return (Init*)UI; }
230 Init *convertValue( BitInit *BI) override;
231 Init *convertValue( BitsInit *BI) override;
232 Init *convertValue( IntInit *II) override { return (Init*)II; }
233 Init *convertValue(StringInit *SI) override { return nullptr; }
234 Init *convertValue( ListInit *LI) override { return nullptr; }
235 Init *convertValue(VarBitInit *VB) override { return nullptr; }
236 Init *convertValue( DefInit *DI) override { return nullptr; }
237 Init *convertValue( DagInit *DI) override { return nullptr; }
238 Init *convertValue( UnOpInit *UI) override { return RecTy::convertValue(UI);}
239 Init *convertValue( BinOpInit *UI) override { return RecTy::convertValue(UI);}
240 Init *convertValue(TernOpInit *UI) override { return RecTy::convertValue(UI);}
241 Init *convertValue( TypedInit *TI) override;
242 Init *convertValue( VarInit *VI) override { return RecTy::convertValue(VI);}
243 Init *convertValue( FieldInit *FI) override { return RecTy::convertValue(FI);}
245 std::string getAsString() const override { return "int"; }
247 bool typeIsConvertibleTo(const RecTy *RHS) const override {
248 return RHS->baseClassOf(this);
251 bool baseClassOf(const RecTy*) const override;
254 /// StringRecTy - 'string' - Represent an string value
256 class StringRecTy : public RecTy {
257 static StringRecTy Shared;
258 StringRecTy() : RecTy(StringRecTyKind) {}
261 static bool classof(const RecTy *RT) {
262 return RT->getRecTyKind() == StringRecTyKind;
265 static StringRecTy *get() { return &Shared; }
267 Init *convertValue( UnsetInit *UI) override { return (Init*)UI; }
268 Init *convertValue( BitInit *BI) override { return nullptr; }
269 Init *convertValue( BitsInit *BI) override { return nullptr; }
270 Init *convertValue( IntInit *II) override { return nullptr; }
271 Init *convertValue(StringInit *SI) override { return (Init*)SI; }
272 Init *convertValue( ListInit *LI) override { return nullptr; }
273 Init *convertValue( UnOpInit *BO) override;
274 Init *convertValue( BinOpInit *BO) override;
275 Init *convertValue(TernOpInit *BO) override { return RecTy::convertValue(BO);}
277 Init *convertValue(VarBitInit *VB) override { return nullptr; }
278 Init *convertValue( DefInit *DI) override { return nullptr; }
279 Init *convertValue( DagInit *DI) override { return nullptr; }
280 Init *convertValue( TypedInit *TI) override;
281 Init *convertValue( VarInit *VI) override { return RecTy::convertValue(VI);}
282 Init *convertValue( FieldInit *FI) override { return RecTy::convertValue(FI);}
284 std::string getAsString() const override { return "string"; }
286 bool typeIsConvertibleTo(const RecTy *RHS) const override {
287 return RHS->baseClassOf(this);
291 /// ListRecTy - 'list<Ty>' - Represent a list of values, all of which must be of
292 /// the specified type.
294 class ListRecTy : public RecTy {
296 explicit ListRecTy(RecTy *T) : RecTy(ListRecTyKind), Ty(T) {}
297 friend ListRecTy *RecTy::getListTy();
300 static bool classof(const RecTy *RT) {
301 return RT->getRecTyKind() == ListRecTyKind;
304 static ListRecTy *get(RecTy *T) { return T->getListTy(); }
305 RecTy *getElementType() const { return Ty; }
307 Init *convertValue( UnsetInit *UI) override { return (Init*)UI; }
308 Init *convertValue( BitInit *BI) override { return nullptr; }
309 Init *convertValue( BitsInit *BI) override { return nullptr; }
310 Init *convertValue( IntInit *II) override { return nullptr; }
311 Init *convertValue(StringInit *SI) override { return nullptr; }
312 Init *convertValue( ListInit *LI) override;
313 Init *convertValue(VarBitInit *VB) override { return nullptr; }
314 Init *convertValue( DefInit *DI) override { return nullptr; }
315 Init *convertValue( DagInit *DI) override { return nullptr; }
316 Init *convertValue( UnOpInit *UI) override { return RecTy::convertValue(UI);}
317 Init *convertValue( BinOpInit *UI) override { return RecTy::convertValue(UI);}
318 Init *convertValue(TernOpInit *UI) override { return RecTy::convertValue(UI);}
319 Init *convertValue( TypedInit *TI) override;
320 Init *convertValue( VarInit *VI) override { return RecTy::convertValue(VI);}
321 Init *convertValue( FieldInit *FI) override { return RecTy::convertValue(FI);}
323 std::string getAsString() const override;
325 bool typeIsConvertibleTo(const RecTy *RHS) const override {
326 return RHS->baseClassOf(this);
329 bool baseClassOf(const RecTy*) const override;
332 /// DagRecTy - 'dag' - Represent a dag fragment
334 class DagRecTy : public RecTy {
335 static DagRecTy Shared;
336 DagRecTy() : RecTy(DagRecTyKind) {}
339 static bool classof(const RecTy *RT) {
340 return RT->getRecTyKind() == DagRecTyKind;
343 static DagRecTy *get() { return &Shared; }
345 Init *convertValue( UnsetInit *UI) override { return (Init*)UI; }
346 Init *convertValue( BitInit *BI) override { return nullptr; }
347 Init *convertValue( BitsInit *BI) override { return nullptr; }
348 Init *convertValue( IntInit *II) override { return nullptr; }
349 Init *convertValue(StringInit *SI) override { return nullptr; }
350 Init *convertValue( ListInit *LI) override { return nullptr; }
351 Init *convertValue(VarBitInit *VB) override { return nullptr; }
352 Init *convertValue( DefInit *DI) override { return nullptr; }
353 Init *convertValue( UnOpInit *BO) override;
354 Init *convertValue( BinOpInit *BO) override;
355 Init *convertValue(TernOpInit *BO) override { return RecTy::convertValue(BO);}
356 Init *convertValue( DagInit *CI) override { return (Init*)CI; }
357 Init *convertValue( TypedInit *TI) override;
358 Init *convertValue( VarInit *VI) override { return RecTy::convertValue(VI);}
359 Init *convertValue( FieldInit *FI) override { return RecTy::convertValue(FI);}
361 std::string getAsString() const override { return "dag"; }
363 bool typeIsConvertibleTo(const RecTy *RHS) const override {
364 return RHS->baseClassOf(this);
368 /// RecordRecTy - '[classname]' - Represent an instance of a class, such as:
371 class RecordRecTy : public RecTy {
373 explicit RecordRecTy(Record *R) : RecTy(RecordRecTyKind), Rec(R) {}
377 static bool classof(const RecTy *RT) {
378 return RT->getRecTyKind() == RecordRecTyKind;
381 static RecordRecTy *get(Record *R);
383 Record *getRecord() const { return Rec; }
385 Init *convertValue( UnsetInit *UI) override { return (Init*)UI; }
386 Init *convertValue( BitInit *BI) override { return nullptr; }
387 Init *convertValue( BitsInit *BI) override { return nullptr; }
388 Init *convertValue( IntInit *II) override { return nullptr; }
389 Init *convertValue(StringInit *SI) override { return nullptr; }
390 Init *convertValue( ListInit *LI) override { return nullptr; }
391 Init *convertValue(VarBitInit *VB) override { return nullptr; }
392 Init *convertValue( UnOpInit *UI) override { return RecTy::convertValue(UI);}
393 Init *convertValue( BinOpInit *UI) override { return RecTy::convertValue(UI);}
394 Init *convertValue(TernOpInit *UI) override { return RecTy::convertValue(UI);}
395 Init *convertValue( DefInit *DI) override;
396 Init *convertValue( DagInit *DI) override { return nullptr; }
397 Init *convertValue( TypedInit *VI) override;
398 Init *convertValue( VarInit *VI) override { return RecTy::convertValue(VI);}
399 Init *convertValue( FieldInit *FI) override { return RecTy::convertValue(FI);}
401 std::string getAsString() const override;
403 bool typeIsConvertibleTo(const RecTy *RHS) const override {
404 return RHS->baseClassOf(this);
406 bool baseClassOf(const RecTy*) const override;
409 /// resolveTypes - Find a common type that T1 and T2 convert to.
410 /// Return 0 if no such type exists.
412 RecTy *resolveTypes(RecTy *T1, RecTy *T2);
414 //===----------------------------------------------------------------------===//
415 // Initializer Classes
416 //===----------------------------------------------------------------------===//
420 /// \brief Discriminator enum (for isa<>, dyn_cast<>, et al.)
422 /// This enum is laid out by a preorder traversal of the inheritance
423 /// hierarchy, and does not contain an entry for abstract classes, as per
424 /// the recommendation in docs/HowToSetUpLLVMStyleRTTI.rst.
426 /// We also explicitly include "first" and "last" values for each
427 /// interior node of the inheritance tree, to make it easier to read the
428 /// corresponding classof().
430 /// We could pack these a bit tighter by not having the IK_FirstXXXInit
431 /// and IK_LastXXXInit be their own values, but that would degrade
432 /// readability for really no benefit.
449 IK_VarListElementInit,
457 Init(const Init &) = delete;
458 Init &operator=(const Init &) = delete;
459 virtual void anchor();
462 InitKind getKind() const { return Kind; }
465 explicit Init(InitKind K) : Kind(K) {}
470 /// isComplete - This virtual method should be overridden by values that may
471 /// not be completely specified yet.
472 virtual bool isComplete() const { return true; }
474 /// print - Print out this value.
475 void print(raw_ostream &OS) const { OS << getAsString(); }
477 /// getAsString - Convert this value to a string form.
478 virtual std::string getAsString() const = 0;
479 /// getAsUnquotedString - Convert this value to a string form,
480 /// without adding quote markers. This primaruly affects
481 /// StringInits where we will not surround the string value with
483 virtual std::string getAsUnquotedString() const { return getAsString(); }
485 /// dump - Debugging method that may be called through a debugger, just
486 /// invokes print on stderr.
489 /// convertInitializerTo - This virtual function is a simple call-back
490 /// function that should be overridden to call the appropriate
491 /// RecTy::convertValue method.
493 virtual Init *convertInitializerTo(RecTy *Ty) const = 0;
495 /// convertInitializerBitRange - This method is used to implement the bitrange
496 /// selection operator. Given an initializer, it selects the specified bits
497 /// out, returning them as a new init of bits type. If it is not legal to use
498 /// the bit subscript operator on this initializer, return null.
501 convertInitializerBitRange(const std::vector<unsigned> &Bits) const {
505 /// convertInitListSlice - This method is used to implement the list slice
506 /// selection operator. Given an initializer, it selects the specified list
507 /// elements, returning them as a new init of list type. If it is not legal
508 /// to take a slice of this, return null.
511 convertInitListSlice(const std::vector<unsigned> &Elements) const {
515 /// getFieldType - This method is used to implement the FieldInit class.
516 /// Implementors of this method should return the type of the named field if
517 /// they are of record type.
519 virtual RecTy *getFieldType(const std::string &FieldName) const {
523 /// getFieldInit - This method complements getFieldType to return the
524 /// initializer for the specified field. If getFieldType returns non-null
525 /// this method should return non-null, otherwise it returns null.
527 virtual Init *getFieldInit(Record &R, const RecordVal *RV,
528 const std::string &FieldName) const {
532 /// resolveReferences - This method is used by classes that refer to other
533 /// variables which may not be defined at the time the expression is formed.
534 /// If a value is set for the variable later, this method will be called on
535 /// users of the value to allow the value to propagate out.
537 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const {
538 return const_cast<Init *>(this);
541 /// getBit - This method is used to return the initializer for the specified
543 virtual Init *getBit(unsigned Bit) const = 0;
545 /// getBitVar - This method is used to retrieve the initializer for bit
546 /// reference. For non-VarBitInit, it simply returns itself.
547 virtual Init *getBitVar() const { return const_cast<Init*>(this); }
549 /// getBitNum - This method is used to retrieve the bit number of a bit
550 /// reference. For non-VarBitInit, it simply returns 0.
551 virtual unsigned getBitNum() const { return 0; }
554 inline raw_ostream &operator<<(raw_ostream &OS, const Init &I) {
555 I.print(OS); return OS;
558 /// TypedInit - This is the common super-class of types that have a specific,
561 class TypedInit : public Init {
564 TypedInit(const TypedInit &Other) = delete;
565 TypedInit &operator=(const TypedInit &Other) = delete;
568 explicit TypedInit(InitKind K, RecTy *T) : Init(K), Ty(T) {}
570 // If this is a DefInit we need to delete the RecordRecTy.
571 if (getKind() == IK_DefInit)
576 static bool classof(const Init *I) {
577 return I->getKind() >= IK_FirstTypedInit &&
578 I->getKind() <= IK_LastTypedInit;
580 RecTy *getType() const { return Ty; }
583 convertInitializerBitRange(const std::vector<unsigned> &Bits) const override;
585 convertInitListSlice(const std::vector<unsigned> &Elements) const override;
587 /// getFieldType - This method is used to implement the FieldInit class.
588 /// Implementors of this method should return the type of the named field if
589 /// they are of record type.
591 RecTy *getFieldType(const std::string &FieldName) const override;
593 /// resolveListElementReference - This method is used to implement
594 /// VarListElementInit::resolveReferences. If the list element is resolvable
595 /// now, we return the resolved value, otherwise we return null.
596 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
597 unsigned Elt) const = 0;
600 /// UnsetInit - ? - Represents an uninitialized value
602 class UnsetInit : public Init {
603 UnsetInit() : Init(IK_UnsetInit) {}
604 UnsetInit(const UnsetInit &) = delete;
605 UnsetInit &operator=(const UnsetInit &Other) = delete;
606 void anchor() override;
609 static bool classof(const Init *I) {
610 return I->getKind() == IK_UnsetInit;
612 static UnsetInit *get();
614 Init *convertInitializerTo(RecTy *Ty) const override {
615 return Ty->convertValue(const_cast<UnsetInit *>(this));
618 Init *getBit(unsigned Bit) const override {
619 return const_cast<UnsetInit*>(this);
622 bool isComplete() const override { return false; }
623 std::string getAsString() const override { return "?"; }
626 /// BitInit - true/false - Represent a concrete initializer for a bit.
628 class BitInit : public Init {
631 explicit BitInit(bool V) : Init(IK_BitInit), Value(V) {}
632 BitInit(const BitInit &Other) = delete;
633 BitInit &operator=(BitInit &Other) = delete;
634 void anchor() override;
637 static bool classof(const Init *I) {
638 return I->getKind() == IK_BitInit;
640 static BitInit *get(bool V);
642 bool getValue() const { return Value; }
644 Init *convertInitializerTo(RecTy *Ty) const override {
645 return Ty->convertValue(const_cast<BitInit *>(this));
648 Init *getBit(unsigned Bit) const override {
649 assert(Bit < 1 && "Bit index out of range!");
650 return const_cast<BitInit*>(this);
653 std::string getAsString() const override { return Value ? "1" : "0"; }
656 /// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
657 /// It contains a vector of bits, whose size is determined by the type.
659 class BitsInit : public TypedInit, public FoldingSetNode {
660 std::vector<Init*> Bits;
662 BitsInit(ArrayRef<Init *> Range)
663 : TypedInit(IK_BitsInit, BitsRecTy::get(Range.size())),
664 Bits(Range.begin(), Range.end()) {}
666 BitsInit(const BitsInit &Other) = delete;
667 BitsInit &operator=(const BitsInit &Other) = delete;
670 static bool classof(const Init *I) {
671 return I->getKind() == IK_BitsInit;
673 static BitsInit *get(ArrayRef<Init *> Range);
675 void Profile(FoldingSetNodeID &ID) const;
677 unsigned getNumBits() const { return Bits.size(); }
679 Init *convertInitializerTo(RecTy *Ty) const override {
680 return Ty->convertValue(const_cast<BitsInit *>(this));
683 convertInitializerBitRange(const std::vector<unsigned> &Bits) const override;
685 bool isComplete() const override {
686 for (unsigned i = 0; i != getNumBits(); ++i)
687 if (!getBit(i)->isComplete()) return false;
690 bool allInComplete() const {
691 for (unsigned i = 0; i != getNumBits(); ++i)
692 if (getBit(i)->isComplete()) return false;
695 std::string getAsString() const override;
697 /// resolveListElementReference - This method is used to implement
698 /// VarListElementInit::resolveReferences. If the list element is resolvable
699 /// now, we return the resolved value, otherwise we return null.
700 Init *resolveListElementReference(Record &R, const RecordVal *RV,
701 unsigned Elt) const override {
702 llvm_unreachable("Illegal element reference off bits<n>");
705 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
707 Init *getBit(unsigned Bit) const override {
708 assert(Bit < Bits.size() && "Bit index out of range!");
713 /// IntInit - 7 - Represent an initialization by a literal integer value.
715 class IntInit : public TypedInit {
718 explicit IntInit(int64_t V)
719 : TypedInit(IK_IntInit, IntRecTy::get()), Value(V) {}
721 IntInit(const IntInit &Other) = delete;
722 IntInit &operator=(const IntInit &Other) = delete;
725 static bool classof(const Init *I) {
726 return I->getKind() == IK_IntInit;
728 static IntInit *get(int64_t V);
730 int64_t getValue() const { return Value; }
732 Init *convertInitializerTo(RecTy *Ty) const override {
733 return Ty->convertValue(const_cast<IntInit *>(this));
736 convertInitializerBitRange(const std::vector<unsigned> &Bits) const override;
738 std::string getAsString() const override;
740 /// resolveListElementReference - This method is used to implement
741 /// VarListElementInit::resolveReferences. If the list element is resolvable
742 /// now, we return the resolved value, otherwise we return null.
743 Init *resolveListElementReference(Record &R, const RecordVal *RV,
744 unsigned Elt) const override {
745 llvm_unreachable("Illegal element reference off int");
748 Init *getBit(unsigned Bit) const override {
749 return BitInit::get((Value & (1ULL << Bit)) != 0);
753 /// StringInit - "foo" - Represent an initialization by a string value.
755 class StringInit : public TypedInit {
758 explicit StringInit(const std::string &V)
759 : TypedInit(IK_StringInit, StringRecTy::get()), Value(V) {}
761 StringInit(const StringInit &Other) = delete;
762 StringInit &operator=(const StringInit &Other) = delete;
763 void anchor() override;
766 static bool classof(const Init *I) {
767 return I->getKind() == IK_StringInit;
769 static StringInit *get(StringRef);
771 const std::string &getValue() const { return Value; }
773 Init *convertInitializerTo(RecTy *Ty) const override {
774 return Ty->convertValue(const_cast<StringInit *>(this));
777 std::string getAsString() const override { return "\"" + Value + "\""; }
778 std::string getAsUnquotedString() const override { return Value; }
780 /// resolveListElementReference - This method is used to implement
781 /// VarListElementInit::resolveReferences. If the list element is resolvable
782 /// now, we return the resolved value, otherwise we return null.
783 Init *resolveListElementReference(Record &R, const RecordVal *RV,
784 unsigned Elt) const override {
785 llvm_unreachable("Illegal element reference off string");
788 Init *getBit(unsigned Bit) const override {
789 llvm_unreachable("Illegal bit reference off string");
793 /// ListInit - [AL, AH, CL] - Represent a list of defs
795 class ListInit : public TypedInit, public FoldingSetNode {
796 std::vector<Init*> Values;
799 typedef std::vector<Init*>::const_iterator const_iterator;
802 explicit ListInit(ArrayRef<Init *> Range, RecTy *EltTy)
803 : TypedInit(IK_ListInit, ListRecTy::get(EltTy)),
804 Values(Range.begin(), Range.end()) {}
806 ListInit(const ListInit &Other) = delete;
807 ListInit &operator=(const ListInit &Other) = delete;
810 static bool classof(const Init *I) {
811 return I->getKind() == IK_ListInit;
813 static ListInit *get(ArrayRef<Init *> Range, RecTy *EltTy);
815 void Profile(FoldingSetNodeID &ID) const;
817 unsigned getSize() const { return Values.size(); }
818 Init *getElement(unsigned i) const {
819 assert(i < Values.size() && "List element index out of range!");
823 Record *getElementAsRecord(unsigned i) const;
826 convertInitListSlice(const std::vector<unsigned> &Elements) const override;
828 Init *convertInitializerTo(RecTy *Ty) const override {
829 return Ty->convertValue(const_cast<ListInit *>(this));
832 /// resolveReferences - This method is used by classes that refer to other
833 /// variables which may not be defined at the time they expression is formed.
834 /// If a value is set for the variable later, this method will be called on
835 /// users of the value to allow the value to propagate out.
837 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
839 std::string getAsString() const override;
841 ArrayRef<Init*> getValues() const { return Values; }
843 inline const_iterator begin() const { return Values.begin(); }
844 inline const_iterator end () const { return Values.end(); }
846 inline size_t size () const { return Values.size(); }
847 inline bool empty() const { return Values.empty(); }
849 /// resolveListElementReference - This method is used to implement
850 /// VarListElementInit::resolveReferences. If the list element is resolvable
851 /// now, we return the resolved value, otherwise we return null.
852 Init *resolveListElementReference(Record &R, const RecordVal *RV,
853 unsigned Elt) const override;
855 Init *getBit(unsigned Bit) const override {
856 llvm_unreachable("Illegal bit reference off list");
860 /// OpInit - Base class for operators
862 class OpInit : public TypedInit {
863 OpInit(const OpInit &Other) = delete;
864 OpInit &operator=(OpInit &Other) = delete;
867 explicit OpInit(InitKind K, RecTy *Type) : TypedInit(K, Type) {}
870 static bool classof(const Init *I) {
871 return I->getKind() >= IK_FirstOpInit &&
872 I->getKind() <= IK_LastOpInit;
874 // Clone - Clone this operator, replacing arguments with the new list
875 virtual OpInit *clone(std::vector<Init *> &Operands) const = 0;
877 virtual int getNumOperands() const = 0;
878 virtual Init *getOperand(int i) const = 0;
880 // Fold - If possible, fold this to a simpler init. Return this if not
882 virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const = 0;
884 Init *convertInitializerTo(RecTy *Ty) const override {
885 return Ty->convertValue(const_cast<OpInit *>(this));
888 Init *resolveListElementReference(Record &R, const RecordVal *RV,
889 unsigned Elt) const override;
891 Init *getBit(unsigned Bit) const override;
894 /// UnOpInit - !op (X) - Transform an init.
896 class UnOpInit : public OpInit {
898 enum UnaryOp { CAST, HEAD, TAIL, EMPTY };
904 UnOpInit(UnaryOp opc, Init *lhs, RecTy *Type)
905 : OpInit(IK_UnOpInit, Type), Opc(opc), LHS(lhs) {}
907 UnOpInit(const UnOpInit &Other) = delete;
908 UnOpInit &operator=(const UnOpInit &Other) = delete;
911 static bool classof(const Init *I) {
912 return I->getKind() == IK_UnOpInit;
914 static UnOpInit *get(UnaryOp opc, Init *lhs, RecTy *Type);
916 // Clone - Clone this operator, replacing arguments with the new list
917 OpInit *clone(std::vector<Init *> &Operands) const override {
918 assert(Operands.size() == 1 &&
919 "Wrong number of operands for unary operation");
920 return UnOpInit::get(getOpcode(), *Operands.begin(), getType());
923 int getNumOperands() const override { return 1; }
924 Init *getOperand(int i) const override {
925 assert(i == 0 && "Invalid operand id for unary operator");
929 UnaryOp getOpcode() const { return Opc; }
930 Init *getOperand() const { return LHS; }
932 // Fold - If possible, fold this to a simpler init. Return this if not
934 Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const override;
936 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
938 std::string getAsString() const override;
941 /// BinOpInit - !op (X, Y) - Combine two inits.
943 class BinOpInit : public OpInit {
945 enum BinaryOp { ADD, AND, SHL, SRA, SRL, LISTCONCAT, STRCONCAT, CONCAT, EQ };
951 BinOpInit(BinaryOp opc, Init *lhs, Init *rhs, RecTy *Type) :
952 OpInit(IK_BinOpInit, Type), Opc(opc), LHS(lhs), RHS(rhs) {}
954 BinOpInit(const BinOpInit &Other) = delete;
955 BinOpInit &operator=(const BinOpInit &Other) = delete;
958 static bool classof(const Init *I) {
959 return I->getKind() == IK_BinOpInit;
961 static BinOpInit *get(BinaryOp opc, Init *lhs, Init *rhs,
964 // Clone - Clone this operator, replacing arguments with the new list
965 OpInit *clone(std::vector<Init *> &Operands) const override {
966 assert(Operands.size() == 2 &&
967 "Wrong number of operands for binary operation");
968 return BinOpInit::get(getOpcode(), Operands[0], Operands[1], getType());
971 int getNumOperands() const override { return 2; }
972 Init *getOperand(int i) const override {
973 assert((i == 0 || i == 1) && "Invalid operand id for binary operator");
981 BinaryOp getOpcode() const { return Opc; }
982 Init *getLHS() const { return LHS; }
983 Init *getRHS() const { return RHS; }
985 // Fold - If possible, fold this to a simpler init. Return this if not
987 Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const override;
989 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
991 std::string getAsString() const override;
994 /// TernOpInit - !op (X, Y, Z) - Combine two inits.
996 class TernOpInit : public OpInit {
998 enum TernaryOp { SUBST, FOREACH, IF };
1002 Init *LHS, *MHS, *RHS;
1004 TernOpInit(TernaryOp opc, Init *lhs, Init *mhs, Init *rhs,
1006 OpInit(IK_TernOpInit, Type), Opc(opc), LHS(lhs), MHS(mhs), RHS(rhs) {}
1008 TernOpInit(const TernOpInit &Other) = delete;
1009 TernOpInit &operator=(const TernOpInit &Other) = delete;
1012 static bool classof(const Init *I) {
1013 return I->getKind() == IK_TernOpInit;
1015 static TernOpInit *get(TernaryOp opc, Init *lhs,
1016 Init *mhs, Init *rhs,
1019 // Clone - Clone this operator, replacing arguments with the new list
1020 OpInit *clone(std::vector<Init *> &Operands) const override {
1021 assert(Operands.size() == 3 &&
1022 "Wrong number of operands for ternary operation");
1023 return TernOpInit::get(getOpcode(), Operands[0], Operands[1], Operands[2],
1027 int getNumOperands() const override { return 3; }
1028 Init *getOperand(int i) const override {
1029 assert((i == 0 || i == 1 || i == 2) &&
1030 "Invalid operand id for ternary operator");
1033 } else if (i == 1) {
1040 TernaryOp getOpcode() const { return Opc; }
1041 Init *getLHS() const { return LHS; }
1042 Init *getMHS() const { return MHS; }
1043 Init *getRHS() const { return RHS; }
1045 // Fold - If possible, fold this to a simpler init. Return this if not
1046 // possible to fold.
1047 Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const override;
1049 bool isComplete() const override { return false; }
1051 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
1053 std::string getAsString() const override;
1056 /// VarInit - 'Opcode' - Represent a reference to an entire variable object.
1058 class VarInit : public TypedInit {
1061 explicit VarInit(const std::string &VN, RecTy *T)
1062 : TypedInit(IK_VarInit, T), VarName(StringInit::get(VN)) {}
1063 explicit VarInit(Init *VN, RecTy *T)
1064 : TypedInit(IK_VarInit, T), VarName(VN) {}
1066 VarInit(const VarInit &Other) = delete;
1067 VarInit &operator=(const VarInit &Other) = delete;
1070 static bool classof(const Init *I) {
1071 return I->getKind() == IK_VarInit;
1073 static VarInit *get(const std::string &VN, RecTy *T);
1074 static VarInit *get(Init *VN, RecTy *T);
1076 Init *convertInitializerTo(RecTy *Ty) const override {
1077 return Ty->convertValue(const_cast<VarInit *>(this));
1080 const std::string &getName() const;
1081 Init *getNameInit() const { return VarName; }
1082 std::string getNameInitAsString() const {
1083 return getNameInit()->getAsUnquotedString();
1086 Init *resolveListElementReference(Record &R, const RecordVal *RV,
1087 unsigned Elt) const override;
1089 RecTy *getFieldType(const std::string &FieldName) const override;
1090 Init *getFieldInit(Record &R, const RecordVal *RV,
1091 const std::string &FieldName) const override;
1093 /// resolveReferences - This method is used by classes that refer to other
1094 /// variables which may not be defined at the time they expression is formed.
1095 /// If a value is set for the variable later, this method will be called on
1096 /// users of the value to allow the value to propagate out.
1098 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
1100 Init *getBit(unsigned Bit) const override;
1102 std::string getAsString() const override { return getName(); }
1105 /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
1107 class VarBitInit : public Init {
1111 VarBitInit(TypedInit *T, unsigned B) : Init(IK_VarBitInit), TI(T), Bit(B) {
1112 assert(T->getType() &&
1113 (isa<IntRecTy>(T->getType()) ||
1114 (isa<BitsRecTy>(T->getType()) &&
1115 cast<BitsRecTy>(T->getType())->getNumBits() > B)) &&
1116 "Illegal VarBitInit expression!");
1119 VarBitInit(const VarBitInit &Other) = delete;
1120 VarBitInit &operator=(const VarBitInit &Other) = delete;
1123 static bool classof(const Init *I) {
1124 return I->getKind() == IK_VarBitInit;
1126 static VarBitInit *get(TypedInit *T, unsigned B);
1128 Init *convertInitializerTo(RecTy *Ty) const override {
1129 return Ty->convertValue(const_cast<VarBitInit *>(this));
1132 Init *getBitVar() const override { return TI; }
1133 unsigned getBitNum() const override { return Bit; }
1135 std::string getAsString() const override;
1136 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
1138 Init *getBit(unsigned B) const override {
1139 assert(B < 1 && "Bit index out of range!");
1140 return const_cast<VarBitInit*>(this);
1144 /// VarListElementInit - List[4] - Represent access to one element of a var or
1146 class VarListElementInit : public TypedInit {
1150 VarListElementInit(TypedInit *T, unsigned E)
1151 : TypedInit(IK_VarListElementInit,
1152 cast<ListRecTy>(T->getType())->getElementType()),
1154 assert(T->getType() && isa<ListRecTy>(T->getType()) &&
1155 "Illegal VarBitInit expression!");
1158 VarListElementInit(const VarListElementInit &Other) = delete;
1159 void operator=(const VarListElementInit &Other) = delete;
1162 static bool classof(const Init *I) {
1163 return I->getKind() == IK_VarListElementInit;
1165 static VarListElementInit *get(TypedInit *T, unsigned E);
1167 Init *convertInitializerTo(RecTy *Ty) const override {
1168 return Ty->convertValue(const_cast<VarListElementInit *>(this));
1171 TypedInit *getVariable() const { return TI; }
1172 unsigned getElementNum() const { return Element; }
1174 /// resolveListElementReference - This method is used to implement
1175 /// VarListElementInit::resolveReferences. If the list element is resolvable
1176 /// now, we return the resolved value, otherwise we return null.
1177 Init *resolveListElementReference(Record &R, const RecordVal *RV,
1178 unsigned Elt) const override;
1180 std::string getAsString() const override;
1181 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
1183 Init *getBit(unsigned Bit) const override;
1186 /// DefInit - AL - Represent a reference to a 'def' in the description
1188 class DefInit : public TypedInit {
1191 DefInit(Record *D, RecordRecTy *T) : TypedInit(IK_DefInit, T), Def(D) {}
1192 friend class Record;
1194 DefInit(const DefInit &Other) = delete;
1195 DefInit &operator=(const DefInit &Other) = delete;
1198 static bool classof(const Init *I) {
1199 return I->getKind() == IK_DefInit;
1201 static DefInit *get(Record*);
1203 Init *convertInitializerTo(RecTy *Ty) const override {
1204 return Ty->convertValue(const_cast<DefInit *>(this));
1207 Record *getDef() const { return Def; }
1209 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
1211 RecTy *getFieldType(const std::string &FieldName) const override;
1212 Init *getFieldInit(Record &R, const RecordVal *RV,
1213 const std::string &FieldName) const override;
1215 std::string getAsString() const override;
1217 Init *getBit(unsigned Bit) const override {
1218 llvm_unreachable("Illegal bit reference off def");
1221 /// resolveListElementReference - This method is used to implement
1222 /// VarListElementInit::resolveReferences. If the list element is resolvable
1223 /// now, we return the resolved value, otherwise we return null.
1224 Init *resolveListElementReference(Record &R, const RecordVal *RV,
1225 unsigned Elt) const override {
1226 llvm_unreachable("Illegal element reference off def");
1230 /// FieldInit - X.Y - Represent a reference to a subfield of a variable
1232 class FieldInit : public TypedInit {
1233 Init *Rec; // Record we are referring to
1234 std::string FieldName; // Field we are accessing
1236 FieldInit(Init *R, const std::string &FN)
1237 : TypedInit(IK_FieldInit, R->getFieldType(FN)), Rec(R), FieldName(FN) {
1238 assert(getType() && "FieldInit with non-record type!");
1241 FieldInit(const FieldInit &Other) = delete;
1242 FieldInit &operator=(const FieldInit &Other) = delete;
1245 static bool classof(const Init *I) {
1246 return I->getKind() == IK_FieldInit;
1248 static FieldInit *get(Init *R, const std::string &FN);
1249 static FieldInit *get(Init *R, const Init *FN);
1251 Init *convertInitializerTo(RecTy *Ty) const override {
1252 return Ty->convertValue(const_cast<FieldInit *>(this));
1255 Init *getBit(unsigned Bit) const override;
1257 Init *resolveListElementReference(Record &R, const RecordVal *RV,
1258 unsigned Elt) const override;
1260 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
1262 std::string getAsString() const override {
1263 return Rec->getAsString() + "." + FieldName;
1267 /// DagInit - (v a, b) - Represent a DAG tree value. DAG inits are required
1268 /// to have at least one value then a (possibly empty) list of arguments. Each
1269 /// argument can have a name associated with it.
1271 class DagInit : public TypedInit, public FoldingSetNode {
1273 std::string ValName;
1274 std::vector<Init*> Args;
1275 std::vector<std::string> ArgNames;
1277 DagInit(Init *V, const std::string &VN,
1278 ArrayRef<Init *> ArgRange,
1279 ArrayRef<std::string> NameRange)
1280 : TypedInit(IK_DagInit, DagRecTy::get()), Val(V), ValName(VN),
1281 Args(ArgRange.begin(), ArgRange.end()),
1282 ArgNames(NameRange.begin(), NameRange.end()) {}
1284 DagInit(const DagInit &Other) = delete;
1285 DagInit &operator=(const DagInit &Other) = delete;
1288 static bool classof(const Init *I) {
1289 return I->getKind() == IK_DagInit;
1291 static DagInit *get(Init *V, const std::string &VN,
1292 ArrayRef<Init *> ArgRange,
1293 ArrayRef<std::string> NameRange);
1294 static DagInit *get(Init *V, const std::string &VN,
1296 std::pair<Init*, std::string> > &args);
1298 void Profile(FoldingSetNodeID &ID) const;
1300 Init *convertInitializerTo(RecTy *Ty) const override {
1301 return Ty->convertValue(const_cast<DagInit *>(this));
1304 Init *getOperator() const { return Val; }
1306 const std::string &getName() const { return ValName; }
1308 unsigned getNumArgs() const { return Args.size(); }
1309 Init *getArg(unsigned Num) const {
1310 assert(Num < Args.size() && "Arg number out of range!");
1313 const std::string &getArgName(unsigned Num) const {
1314 assert(Num < ArgNames.size() && "Arg number out of range!");
1315 return ArgNames[Num];
1318 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
1320 std::string getAsString() const override;
1322 typedef std::vector<Init*>::const_iterator const_arg_iterator;
1323 typedef std::vector<std::string>::const_iterator const_name_iterator;
1325 inline const_arg_iterator arg_begin() const { return Args.begin(); }
1326 inline const_arg_iterator arg_end () const { return Args.end(); }
1328 inline size_t arg_size () const { return Args.size(); }
1329 inline bool arg_empty() const { return Args.empty(); }
1331 inline const_name_iterator name_begin() const { return ArgNames.begin(); }
1332 inline const_name_iterator name_end () const { return ArgNames.end(); }
1334 inline size_t name_size () const { return ArgNames.size(); }
1335 inline bool name_empty() const { return ArgNames.empty(); }
1337 Init *getBit(unsigned Bit) const override {
1338 llvm_unreachable("Illegal bit reference off dag");
1341 Init *resolveListElementReference(Record &R, const RecordVal *RV,
1342 unsigned Elt) const override {
1343 llvm_unreachable("Illegal element reference off dag");
1347 //===----------------------------------------------------------------------===//
1348 // High-Level Classes
1349 //===----------------------------------------------------------------------===//
1358 RecordVal(Init *N, RecTy *T, unsigned P);
1359 RecordVal(const std::string &N, RecTy *T, unsigned P);
1361 const std::string &getName() const;
1362 const Init *getNameInit() const { return Name; }
1363 std::string getNameInitAsString() const {
1364 return getNameInit()->getAsUnquotedString();
1367 unsigned getPrefix() const { return Prefix; }
1368 RecTy *getType() const { return Ty; }
1369 Init *getValue() const { return Value; }
1371 bool setValue(Init *V) {
1373 Value = V->convertInitializerTo(Ty);
1374 return Value == nullptr;
1381 void print(raw_ostream &OS, bool PrintSem = true) const;
1384 inline raw_ostream &operator<<(raw_ostream &OS, const RecordVal &RV) {
1385 RV.print(OS << " ");
1390 static unsigned LastID;
1392 // Unique record ID.
1395 // Location where record was instantiated, followed by the location of
1396 // multiclass prototypes used.
1397 SmallVector<SMLoc, 4> Locs;
1398 std::vector<Init *> TemplateArgs;
1399 std::vector<RecordVal> Values;
1400 std::vector<Record *> SuperClasses;
1401 std::vector<SMRange> SuperClassRanges;
1403 // Tracks Record instances. Not owned by Record.
1404 RecordKeeper &TrackedRecords;
1409 // Class-instance values can be used by other defs. For example, Struct<i>
1410 // is used here as a template argument to another class:
1412 // multiclass MultiClass<int i> {
1413 // def Def : Class<Struct<i>>;
1415 // These need to get fully resolved before instantiating any other
1416 // definitions that usie them (e.g. Def). However, inside a multiclass they
1417 // can't be immediately resolved so we mark them ResolveFirst to fully
1418 // resolve them later as soon as the multiclass is instantiated.
1425 // Constructs a record.
1426 explicit Record(const std::string &N, ArrayRef<SMLoc> locs,
1427 RecordKeeper &records, bool Anonymous = false) :
1428 ID(LastID++), Name(StringInit::get(N)), Locs(locs.begin(), locs.end()),
1429 TrackedRecords(records), TheInit(nullptr), IsAnonymous(Anonymous),
1430 ResolveFirst(false) {
1433 explicit Record(Init *N, ArrayRef<SMLoc> locs, RecordKeeper &records,
1434 bool Anonymous = false) :
1435 ID(LastID++), Name(N), Locs(locs.begin(), locs.end()),
1436 TrackedRecords(records), TheInit(nullptr), IsAnonymous(Anonymous),
1437 ResolveFirst(false) {
1441 // When copy-constructing a Record, we must still guarantee a globally unique
1442 // ID number. All other fields can be copied normally.
1443 Record(const Record &O) :
1444 ID(LastID++), Name(O.Name), Locs(O.Locs), TemplateArgs(O.TemplateArgs),
1445 Values(O.Values), SuperClasses(O.SuperClasses),
1446 SuperClassRanges(O.SuperClassRanges), TrackedRecords(O.TrackedRecords),
1447 TheInit(O.TheInit), IsAnonymous(O.IsAnonymous),
1448 ResolveFirst(O.ResolveFirst) { }
1450 static unsigned getNewUID() { return LastID++; }
1452 unsigned getID() const { return ID; }
1454 const std::string &getName() const;
1455 Init *getNameInit() const {
1458 const std::string getNameInitAsString() const {
1459 return getNameInit()->getAsUnquotedString();
1462 void setName(Init *Name); // Also updates RecordKeeper.
1463 void setName(const std::string &Name); // Also updates RecordKeeper.
1465 ArrayRef<SMLoc> getLoc() const { return Locs; }
1467 /// get the corresponding DefInit.
1468 DefInit *getDefInit();
1470 const std::vector<Init *> &getTemplateArgs() const {
1471 return TemplateArgs;
1473 const std::vector<RecordVal> &getValues() const { return Values; }
1474 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; }
1475 ArrayRef<SMRange> getSuperClassRanges() const { return SuperClassRanges; }
1477 bool isTemplateArg(Init *Name) const {
1478 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
1479 if (TemplateArgs[i] == Name) return true;
1482 bool isTemplateArg(StringRef Name) const {
1483 return isTemplateArg(StringInit::get(Name));
1486 const RecordVal *getValue(const Init *Name) const {
1487 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1488 if (Values[i].getNameInit() == Name) return &Values[i];
1491 const RecordVal *getValue(StringRef Name) const {
1492 return getValue(StringInit::get(Name));
1494 RecordVal *getValue(const Init *Name) {
1495 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1496 if (Values[i].getNameInit() == Name) return &Values[i];
1499 RecordVal *getValue(StringRef Name) {
1500 return getValue(StringInit::get(Name));
1503 void addTemplateArg(Init *Name) {
1504 assert(!isTemplateArg(Name) && "Template arg already defined!");
1505 TemplateArgs.push_back(Name);
1507 void addTemplateArg(StringRef Name) {
1508 addTemplateArg(StringInit::get(Name));
1511 void addValue(const RecordVal &RV) {
1512 assert(getValue(RV.getNameInit()) == nullptr && "Value already added!");
1513 Values.push_back(RV);
1514 if (Values.size() > 1)
1515 // Keep NAME at the end of the list. It makes record dumps a
1516 // bit prettier and allows TableGen tests to be written more
1517 // naturally. Tests can use CHECK-NEXT to look for Record
1518 // fields they expect to see after a def. They can't do that if
1519 // NAME is the first Record field.
1520 std::swap(Values[Values.size() - 2], Values[Values.size() - 1]);
1523 void removeValue(Init *Name) {
1524 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1525 if (Values[i].getNameInit() == Name) {
1526 Values.erase(Values.begin()+i);
1529 llvm_unreachable("Cannot remove an entry that does not exist!");
1532 void removeValue(StringRef Name) {
1533 removeValue(StringInit::get(Name));
1536 bool isSubClassOf(const Record *R) const {
1537 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1538 if (SuperClasses[i] == R)
1543 bool isSubClassOf(StringRef Name) const {
1544 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1545 if (SuperClasses[i]->getNameInitAsString() == Name)
1550 void addSuperClass(Record *R, SMRange Range) {
1551 assert(!isSubClassOf(R) && "Already subclassing record!");
1552 SuperClasses.push_back(R);
1553 SuperClassRanges.push_back(Range);
1556 /// resolveReferences - If there are any field references that refer to fields
1557 /// that have been filled in, we can propagate the values now.
1559 void resolveReferences() { resolveReferencesTo(nullptr); }
1561 /// resolveReferencesTo - If anything in this record refers to RV, replace the
1562 /// reference to RV with the RHS of RV. If RV is null, we resolve all
1563 /// possible references.
1564 void resolveReferencesTo(const RecordVal *RV);
1566 RecordKeeper &getRecords() const {
1567 return TrackedRecords;
1570 bool isAnonymous() const {
1574 bool isResolveFirst() const {
1575 return ResolveFirst;
1578 void setResolveFirst(bool b) {
1584 //===--------------------------------------------------------------------===//
1585 // High-level methods useful to tablegen back-ends
1588 /// getValueInit - Return the initializer for a value with the specified name,
1589 /// or throw an exception if the field does not exist.
1591 Init *getValueInit(StringRef FieldName) const;
1593 /// Return true if the named field is unset.
1594 bool isValueUnset(StringRef FieldName) const {
1595 return getValueInit(FieldName) == UnsetInit::get();
1598 /// getValueAsString - This method looks up the specified field and returns
1599 /// its value as a string, throwing an exception if the field does not exist
1600 /// or if the value is not a string.
1602 std::string getValueAsString(StringRef FieldName) const;
1604 /// getValueAsBitsInit - This method looks up the specified field and returns
1605 /// its value as a BitsInit, throwing an exception if the field does not exist
1606 /// or if the value is not the right type.
1608 BitsInit *getValueAsBitsInit(StringRef FieldName) const;
1610 /// getValueAsListInit - This method looks up the specified field and returns
1611 /// its value as a ListInit, throwing an exception if the field does not exist
1612 /// or if the value is not the right type.
1614 ListInit *getValueAsListInit(StringRef FieldName) const;
1616 /// getValueAsListOfDefs - This method looks up the specified field and
1617 /// returns its value as a vector of records, throwing an exception if the
1618 /// field does not exist or if the value is not the right type.
1620 std::vector<Record*> getValueAsListOfDefs(StringRef FieldName) const;
1622 /// getValueAsListOfInts - This method looks up the specified field and
1623 /// returns its value as a vector of integers, throwing an exception if the
1624 /// field does not exist or if the value is not the right type.
1626 std::vector<int64_t> getValueAsListOfInts(StringRef FieldName) const;
1628 /// getValueAsListOfStrings - This method looks up the specified field and
1629 /// returns its value as a vector of strings, throwing an exception if the
1630 /// field does not exist or if the value is not the right type.
1632 std::vector<std::string> getValueAsListOfStrings(StringRef FieldName) const;
1634 /// getValueAsDef - This method looks up the specified field and returns its
1635 /// value as a Record, throwing an exception if the field does not exist or if
1636 /// the value is not the right type.
1638 Record *getValueAsDef(StringRef FieldName) const;
1640 /// getValueAsBit - This method looks up the specified field and returns its
1641 /// value as a bit, throwing an exception if the field does not exist or if
1642 /// the value is not the right type.
1644 bool getValueAsBit(StringRef FieldName) const;
1646 /// getValueAsBitOrUnset - This method looks up the specified field and
1647 /// returns its value as a bit. If the field is unset, sets Unset to true and
1650 bool getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const;
1652 /// getValueAsInt - This method looks up the specified field and returns its
1653 /// value as an int64_t, throwing an exception if the field does not exist or
1654 /// if the value is not the right type.
1656 int64_t getValueAsInt(StringRef FieldName) const;
1658 /// getValueAsDag - This method looks up the specified field and returns its
1659 /// value as an Dag, throwing an exception if the field does not exist or if
1660 /// the value is not the right type.
1662 DagInit *getValueAsDag(StringRef FieldName) const;
1665 raw_ostream &operator<<(raw_ostream &OS, const Record &R);
1668 Record Rec; // Placeholder for template args and Name.
1669 typedef std::vector<std::unique_ptr<Record>> RecordVector;
1670 RecordVector DefPrototypes;
1674 MultiClass(const std::string &Name, SMLoc Loc, RecordKeeper &Records) :
1675 Rec(Name, Loc, Records) {}
1678 class RecordKeeper {
1679 typedef std::map<std::string, std::unique_ptr<Record>> RecordMap;
1680 RecordMap Classes, Defs;
1683 const RecordMap &getClasses() const { return Classes; }
1684 const RecordMap &getDefs() const { return Defs; }
1686 Record *getClass(const std::string &Name) const {
1687 auto I = Classes.find(Name);
1688 return I == Classes.end() ? nullptr : I->second.get();
1690 Record *getDef(const std::string &Name) const {
1691 auto I = Defs.find(Name);
1692 return I == Defs.end() ? nullptr : I->second.get();
1694 void addClass(std::unique_ptr<Record> R) {
1695 bool Ins = Classes.insert(std::make_pair(R->getName(),
1696 std::move(R))).second;
1698 assert(Ins && "Class already exists");
1700 void addDef(std::unique_ptr<Record> R) {
1701 bool Ins = Defs.insert(std::make_pair(R->getName(),
1702 std::move(R))).second;
1704 assert(Ins && "Record already exists");
1707 //===--------------------------------------------------------------------===//
1708 // High-level helper methods, useful for tablegen backends...
1710 /// getAllDerivedDefinitions - This method returns all concrete definitions
1711 /// that derive from the specified class name. If a class with the specified
1712 /// name does not exist, an exception is thrown.
1713 std::vector<Record*>
1714 getAllDerivedDefinitions(const std::string &ClassName) const;
1719 /// LessRecord - Sorting predicate to sort record pointers by name.
1722 bool operator()(const Record *Rec1, const Record *Rec2) const {
1723 return StringRef(Rec1->getName()).compare_numeric(Rec2->getName()) < 0;
1727 /// LessRecordByID - Sorting predicate to sort record pointers by their
1728 /// unique ID. If you just need a deterministic order, use this, since it
1729 /// just compares two `unsigned`; the other sorting predicates require
1730 /// string manipulation.
1731 struct LessRecordByID {
1732 bool operator()(const Record *LHS, const Record *RHS) const {
1733 return LHS->getID() < RHS->getID();
1737 /// LessRecordFieldName - Sorting predicate to sort record pointers by their
1740 struct LessRecordFieldName {
1741 bool operator()(const Record *Rec1, const Record *Rec2) const {
1742 return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name");
1746 struct LessRecordRegister {
1747 static size_t min(size_t a, size_t b) { return a < b ? a : b; }
1748 static bool ascii_isdigit(char x) { return x >= '0' && x <= '9'; }
1750 struct RecordParts {
1751 SmallVector<std::pair< bool, StringRef>, 4> Parts;
1753 RecordParts(StringRef Rec) {
1758 const char *Start = Rec.data();
1759 const char *Curr = Start;
1760 bool isDigitPart = ascii_isdigit(Curr[0]);
1761 for (size_t I = 0, E = Rec.size(); I != E; ++I, ++Len) {
1762 bool isDigit = ascii_isdigit(Curr[I]);
1763 if (isDigit != isDigitPart) {
1764 Parts.push_back(std::make_pair(isDigitPart, StringRef(Start, Len)));
1767 isDigitPart = ascii_isdigit(Curr[I]);
1770 // Push the last part.
1771 Parts.push_back(std::make_pair(isDigitPart, StringRef(Start, Len)));
1774 size_t size() { return Parts.size(); }
1776 std::pair<bool, StringRef> getPart(size_t i) {
1777 assert (i < Parts.size() && "Invalid idx!");
1782 bool operator()(const Record *Rec1, const Record *Rec2) const {
1783 RecordParts LHSParts(StringRef(Rec1->getName()));
1784 RecordParts RHSParts(StringRef(Rec2->getName()));
1786 size_t LHSNumParts = LHSParts.size();
1787 size_t RHSNumParts = RHSParts.size();
1788 assert (LHSNumParts && RHSNumParts && "Expected at least one part!");
1790 if (LHSNumParts != RHSNumParts)
1791 return LHSNumParts < RHSNumParts;
1793 // We expect the registers to be of the form [_a-zA-z]+([0-9]*[_a-zA-Z]*)*.
1794 for (size_t I = 0, E = LHSNumParts; I < E; I+=2) {
1795 std::pair<bool, StringRef> LHSPart = LHSParts.getPart(I);
1796 std::pair<bool, StringRef> RHSPart = RHSParts.getPart(I);
1797 // Expect even part to always be alpha.
1798 assert (LHSPart.first == false && RHSPart.first == false &&
1799 "Expected both parts to be alpha.");
1800 if (int Res = LHSPart.second.compare(RHSPart.second))
1803 for (size_t I = 1, E = LHSNumParts; I < E; I+=2) {
1804 std::pair<bool, StringRef> LHSPart = LHSParts.getPart(I);
1805 std::pair<bool, StringRef> RHSPart = RHSParts.getPart(I);
1806 // Expect odd part to always be numeric.
1807 assert (LHSPart.first == true && RHSPart.first == true &&
1808 "Expected both parts to be numeric.");
1809 if (LHSPart.second.size() != RHSPart.second.size())
1810 return LHSPart.second.size() < RHSPart.second.size();
1812 unsigned LHSVal, RHSVal;
1814 bool LHSFailed = LHSPart.second.getAsInteger(10, LHSVal); (void)LHSFailed;
1815 assert(!LHSFailed && "Unable to convert LHS to integer.");
1816 bool RHSFailed = RHSPart.second.getAsInteger(10, RHSVal); (void)RHSFailed;
1817 assert(!RHSFailed && "Unable to convert RHS to integer.");
1819 if (LHSVal != RHSVal)
1820 return LHSVal < RHSVal;
1822 return LHSNumParts < RHSNumParts;
1826 raw_ostream &operator<<(raw_ostream &OS, const RecordKeeper &RK);
1828 /// QualifyName - Return an Init with a qualifier prefix referring
1829 /// to CurRec's name.
1830 Init *QualifyName(Record &CurRec, MultiClass *CurMultiClass,
1831 Init *Name, const std::string &Scoper);
1833 /// QualifyName - Return an Init with a qualifier prefix referring
1834 /// to CurRec's name.
1835 Init *QualifyName(Record &CurRec, MultiClass *CurMultiClass,
1836 const std::string &Name, const std::string &Scoper);
1838 } // End llvm namespace