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.)
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 &) LLVM_DELETED_FUNCTION;
458 Init &operator=(const Init &) LLVM_DELETED_FUNCTION;
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) LLVM_DELETED_FUNCTION;
565 TypedInit &operator=(const TypedInit &Other) LLVM_DELETED_FUNCTION;
568 explicit TypedInit(InitKind K, RecTy *T) : Init(K), Ty(T) {}
571 static bool classof(const Init *I) {
572 return I->getKind() >= IK_FirstTypedInit &&
573 I->getKind() <= IK_LastTypedInit;
575 RecTy *getType() const { return Ty; }
578 convertInitializerBitRange(const std::vector<unsigned> &Bits) const override;
580 convertInitListSlice(const std::vector<unsigned> &Elements) const override;
582 /// getFieldType - This method is used to implement the FieldInit class.
583 /// Implementors of this method should return the type of the named field if
584 /// they are of record type.
586 RecTy *getFieldType(const std::string &FieldName) const override;
588 /// resolveListElementReference - This method is used to implement
589 /// VarListElementInit::resolveReferences. If the list element is resolvable
590 /// now, we return the resolved value, otherwise we return null.
591 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
592 unsigned Elt) const = 0;
595 /// UnsetInit - ? - Represents an uninitialized value
597 class UnsetInit : public Init {
598 UnsetInit() : Init(IK_UnsetInit) {}
599 UnsetInit(const UnsetInit &) LLVM_DELETED_FUNCTION;
600 UnsetInit &operator=(const UnsetInit &Other) LLVM_DELETED_FUNCTION;
601 void anchor() override;
604 static bool classof(const Init *I) {
605 return I->getKind() == IK_UnsetInit;
607 static UnsetInit *get();
609 Init *convertInitializerTo(RecTy *Ty) const override {
610 return Ty->convertValue(const_cast<UnsetInit *>(this));
613 Init *getBit(unsigned Bit) const override {
614 return const_cast<UnsetInit*>(this);
617 bool isComplete() const override { return false; }
618 std::string getAsString() const override { return "?"; }
621 /// BitInit - true/false - Represent a concrete initializer for a bit.
623 class BitInit : public Init {
626 explicit BitInit(bool V) : Init(IK_BitInit), Value(V) {}
627 BitInit(const BitInit &Other) LLVM_DELETED_FUNCTION;
628 BitInit &operator=(BitInit &Other) LLVM_DELETED_FUNCTION;
629 void anchor() override;
632 static bool classof(const Init *I) {
633 return I->getKind() == IK_BitInit;
635 static BitInit *get(bool V);
637 bool getValue() const { return Value; }
639 Init *convertInitializerTo(RecTy *Ty) const override {
640 return Ty->convertValue(const_cast<BitInit *>(this));
643 Init *getBit(unsigned Bit) const override {
644 assert(Bit < 1 && "Bit index out of range!");
645 return const_cast<BitInit*>(this);
648 std::string getAsString() const override { return Value ? "1" : "0"; }
651 /// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
652 /// It contains a vector of bits, whose size is determined by the type.
654 class BitsInit : public Init, public FoldingSetNode {
655 std::vector<Init*> Bits;
657 BitsInit(ArrayRef<Init *> Range)
658 : Init(IK_BitsInit), Bits(Range.begin(), Range.end()) {}
660 BitsInit(const BitsInit &Other) LLVM_DELETED_FUNCTION;
661 BitsInit &operator=(const BitsInit &Other) LLVM_DELETED_FUNCTION;
664 static bool classof(const Init *I) {
665 return I->getKind() == IK_BitsInit;
667 static BitsInit *get(ArrayRef<Init *> Range);
669 void Profile(FoldingSetNodeID &ID) const;
671 unsigned getNumBits() const { return Bits.size(); }
673 Init *convertInitializerTo(RecTy *Ty) const override {
674 return Ty->convertValue(const_cast<BitsInit *>(this));
677 convertInitializerBitRange(const std::vector<unsigned> &Bits) const override;
679 bool isComplete() const override {
680 for (unsigned i = 0; i != getNumBits(); ++i)
681 if (!getBit(i)->isComplete()) return false;
684 bool allInComplete() const {
685 for (unsigned i = 0; i != getNumBits(); ++i)
686 if (getBit(i)->isComplete()) return false;
689 std::string getAsString() const override;
691 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
693 Init *getBit(unsigned Bit) const override {
694 assert(Bit < Bits.size() && "Bit index out of range!");
699 /// IntInit - 7 - Represent an initialization by a literal integer value.
701 class IntInit : public TypedInit {
704 explicit IntInit(int64_t V)
705 : TypedInit(IK_IntInit, IntRecTy::get()), Value(V) {}
707 IntInit(const IntInit &Other) LLVM_DELETED_FUNCTION;
708 IntInit &operator=(const IntInit &Other) LLVM_DELETED_FUNCTION;
711 static bool classof(const Init *I) {
712 return I->getKind() == IK_IntInit;
714 static IntInit *get(int64_t V);
716 int64_t getValue() const { return Value; }
718 Init *convertInitializerTo(RecTy *Ty) const override {
719 return Ty->convertValue(const_cast<IntInit *>(this));
722 convertInitializerBitRange(const std::vector<unsigned> &Bits) const override;
724 std::string getAsString() const override;
726 /// resolveListElementReference - This method is used to implement
727 /// VarListElementInit::resolveReferences. If the list element is resolvable
728 /// now, we return the resolved value, otherwise we return null.
729 Init *resolveListElementReference(Record &R, const RecordVal *RV,
730 unsigned Elt) const override {
731 llvm_unreachable("Illegal element reference off int");
734 Init *getBit(unsigned Bit) const override {
735 return BitInit::get((Value & (1ULL << Bit)) != 0);
739 /// StringInit - "foo" - Represent an initialization by a string value.
741 class StringInit : public TypedInit {
744 explicit StringInit(const std::string &V)
745 : TypedInit(IK_StringInit, StringRecTy::get()), Value(V) {}
747 StringInit(const StringInit &Other) LLVM_DELETED_FUNCTION;
748 StringInit &operator=(const StringInit &Other) LLVM_DELETED_FUNCTION;
749 void anchor() override;
752 static bool classof(const Init *I) {
753 return I->getKind() == IK_StringInit;
755 static StringInit *get(StringRef);
757 const std::string &getValue() const { return Value; }
759 Init *convertInitializerTo(RecTy *Ty) const override {
760 return Ty->convertValue(const_cast<StringInit *>(this));
763 std::string getAsString() const override { return "\"" + Value + "\""; }
764 std::string getAsUnquotedString() const override { return Value; }
766 /// resolveListElementReference - This method is used to implement
767 /// VarListElementInit::resolveReferences. If the list element is resolvable
768 /// now, we return the resolved value, otherwise we return null.
769 Init *resolveListElementReference(Record &R, const RecordVal *RV,
770 unsigned Elt) const override {
771 llvm_unreachable("Illegal element reference off string");
774 Init *getBit(unsigned Bit) const override {
775 llvm_unreachable("Illegal bit reference off string");
779 /// ListInit - [AL, AH, CL] - Represent a list of defs
781 class ListInit : public TypedInit, public FoldingSetNode {
782 std::vector<Init*> Values;
785 typedef std::vector<Init*>::const_iterator const_iterator;
788 explicit ListInit(ArrayRef<Init *> Range, RecTy *EltTy)
789 : TypedInit(IK_ListInit, ListRecTy::get(EltTy)),
790 Values(Range.begin(), Range.end()) {}
792 ListInit(const ListInit &Other) LLVM_DELETED_FUNCTION;
793 ListInit &operator=(const ListInit &Other) LLVM_DELETED_FUNCTION;
796 static bool classof(const Init *I) {
797 return I->getKind() == IK_ListInit;
799 static ListInit *get(ArrayRef<Init *> Range, RecTy *EltTy);
801 void Profile(FoldingSetNodeID &ID) const;
803 unsigned getSize() const { return Values.size(); }
804 Init *getElement(unsigned i) const {
805 assert(i < Values.size() && "List element index out of range!");
809 Record *getElementAsRecord(unsigned i) const;
812 convertInitListSlice(const std::vector<unsigned> &Elements) const override;
814 Init *convertInitializerTo(RecTy *Ty) const override {
815 return Ty->convertValue(const_cast<ListInit *>(this));
818 /// resolveReferences - This method is used by classes that refer to other
819 /// variables which may not be defined at the time they expression is formed.
820 /// If a value is set for the variable later, this method will be called on
821 /// users of the value to allow the value to propagate out.
823 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
825 std::string getAsString() const override;
827 ArrayRef<Init*> getValues() const { return Values; }
829 inline const_iterator begin() const { return Values.begin(); }
830 inline const_iterator end () const { return Values.end(); }
832 inline size_t size () const { return Values.size(); }
833 inline bool empty() const { return Values.empty(); }
835 /// resolveListElementReference - This method is used to implement
836 /// VarListElementInit::resolveReferences. If the list element is resolvable
837 /// now, we return the resolved value, otherwise we return null.
838 Init *resolveListElementReference(Record &R, const RecordVal *RV,
839 unsigned Elt) const override;
841 Init *getBit(unsigned Bit) const override {
842 llvm_unreachable("Illegal bit reference off list");
846 /// OpInit - Base class for operators
848 class OpInit : public TypedInit {
849 OpInit(const OpInit &Other) LLVM_DELETED_FUNCTION;
850 OpInit &operator=(OpInit &Other) LLVM_DELETED_FUNCTION;
853 explicit OpInit(InitKind K, RecTy *Type) : TypedInit(K, Type) {}
856 static bool classof(const Init *I) {
857 return I->getKind() >= IK_FirstOpInit &&
858 I->getKind() <= IK_LastOpInit;
860 // Clone - Clone this operator, replacing arguments with the new list
861 virtual OpInit *clone(std::vector<Init *> &Operands) const = 0;
863 virtual int getNumOperands() const = 0;
864 virtual Init *getOperand(int i) const = 0;
866 // Fold - If possible, fold this to a simpler init. Return this if not
868 virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const = 0;
870 Init *convertInitializerTo(RecTy *Ty) const override {
871 return Ty->convertValue(const_cast<OpInit *>(this));
874 Init *resolveListElementReference(Record &R, const RecordVal *RV,
875 unsigned Elt) const override;
877 Init *getBit(unsigned Bit) const override;
880 /// UnOpInit - !op (X) - Transform an init.
882 class UnOpInit : public OpInit {
884 enum UnaryOp { CAST, HEAD, TAIL, EMPTY };
890 UnOpInit(UnaryOp opc, Init *lhs, RecTy *Type)
891 : OpInit(IK_UnOpInit, Type), Opc(opc), LHS(lhs) {}
893 UnOpInit(const UnOpInit &Other) LLVM_DELETED_FUNCTION;
894 UnOpInit &operator=(const UnOpInit &Other) LLVM_DELETED_FUNCTION;
897 static bool classof(const Init *I) {
898 return I->getKind() == IK_UnOpInit;
900 static UnOpInit *get(UnaryOp opc, Init *lhs, RecTy *Type);
902 // Clone - Clone this operator, replacing arguments with the new list
903 OpInit *clone(std::vector<Init *> &Operands) const override {
904 assert(Operands.size() == 1 &&
905 "Wrong number of operands for unary operation");
906 return UnOpInit::get(getOpcode(), *Operands.begin(), getType());
909 int getNumOperands() const override { return 1; }
910 Init *getOperand(int i) const override {
911 assert(i == 0 && "Invalid operand id for unary operator");
915 UnaryOp getOpcode() const { return Opc; }
916 Init *getOperand() const { return LHS; }
918 // Fold - If possible, fold this to a simpler init. Return this if not
920 Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const override;
922 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
924 std::string getAsString() const override;
927 /// BinOpInit - !op (X, Y) - Combine two inits.
929 class BinOpInit : public OpInit {
931 enum BinaryOp { ADD, SHL, SRA, SRL, LISTCONCAT, STRCONCAT, CONCAT, EQ };
937 BinOpInit(BinaryOp opc, Init *lhs, Init *rhs, RecTy *Type) :
938 OpInit(IK_BinOpInit, Type), Opc(opc), LHS(lhs), RHS(rhs) {}
940 BinOpInit(const BinOpInit &Other) LLVM_DELETED_FUNCTION;
941 BinOpInit &operator=(const BinOpInit &Other) LLVM_DELETED_FUNCTION;
944 static bool classof(const Init *I) {
945 return I->getKind() == IK_BinOpInit;
947 static BinOpInit *get(BinaryOp opc, Init *lhs, Init *rhs,
950 // Clone - Clone this operator, replacing arguments with the new list
951 OpInit *clone(std::vector<Init *> &Operands) const override {
952 assert(Operands.size() == 2 &&
953 "Wrong number of operands for binary operation");
954 return BinOpInit::get(getOpcode(), Operands[0], Operands[1], getType());
957 int getNumOperands() const override { return 2; }
958 Init *getOperand(int i) const override {
959 assert((i == 0 || i == 1) && "Invalid operand id for binary operator");
967 BinaryOp getOpcode() const { return Opc; }
968 Init *getLHS() const { return LHS; }
969 Init *getRHS() const { return RHS; }
971 // Fold - If possible, fold this to a simpler init. Return this if not
973 Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const override;
975 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
977 std::string getAsString() const override;
980 /// TernOpInit - !op (X, Y, Z) - Combine two inits.
982 class TernOpInit : public OpInit {
984 enum TernaryOp { SUBST, FOREACH, IF };
988 Init *LHS, *MHS, *RHS;
990 TernOpInit(TernaryOp opc, Init *lhs, Init *mhs, Init *rhs,
992 OpInit(IK_TernOpInit, Type), Opc(opc), LHS(lhs), MHS(mhs), RHS(rhs) {}
994 TernOpInit(const TernOpInit &Other) LLVM_DELETED_FUNCTION;
995 TernOpInit &operator=(const TernOpInit &Other) LLVM_DELETED_FUNCTION;
998 static bool classof(const Init *I) {
999 return I->getKind() == IK_TernOpInit;
1001 static TernOpInit *get(TernaryOp opc, Init *lhs,
1002 Init *mhs, Init *rhs,
1005 // Clone - Clone this operator, replacing arguments with the new list
1006 OpInit *clone(std::vector<Init *> &Operands) const override {
1007 assert(Operands.size() == 3 &&
1008 "Wrong number of operands for ternary operation");
1009 return TernOpInit::get(getOpcode(), Operands[0], Operands[1], Operands[2],
1013 int getNumOperands() const override { return 3; }
1014 Init *getOperand(int i) const override {
1015 assert((i == 0 || i == 1 || i == 2) &&
1016 "Invalid operand id for ternary operator");
1019 } else if (i == 1) {
1026 TernaryOp getOpcode() const { return Opc; }
1027 Init *getLHS() const { return LHS; }
1028 Init *getMHS() const { return MHS; }
1029 Init *getRHS() const { return RHS; }
1031 // Fold - If possible, fold this to a simpler init. Return this if not
1032 // possible to fold.
1033 Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const override;
1035 bool isComplete() const override { return false; }
1037 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
1039 std::string getAsString() const override;
1042 /// VarInit - 'Opcode' - Represent a reference to an entire variable object.
1044 class VarInit : public TypedInit {
1047 explicit VarInit(const std::string &VN, RecTy *T)
1048 : TypedInit(IK_VarInit, T), VarName(StringInit::get(VN)) {}
1049 explicit VarInit(Init *VN, RecTy *T)
1050 : TypedInit(IK_VarInit, T), VarName(VN) {}
1052 VarInit(const VarInit &Other) LLVM_DELETED_FUNCTION;
1053 VarInit &operator=(const VarInit &Other) LLVM_DELETED_FUNCTION;
1056 static bool classof(const Init *I) {
1057 return I->getKind() == IK_VarInit;
1059 static VarInit *get(const std::string &VN, RecTy *T);
1060 static VarInit *get(Init *VN, RecTy *T);
1062 Init *convertInitializerTo(RecTy *Ty) const override {
1063 return Ty->convertValue(const_cast<VarInit *>(this));
1066 const std::string &getName() const;
1067 Init *getNameInit() const { return VarName; }
1068 std::string getNameInitAsString() const {
1069 return getNameInit()->getAsUnquotedString();
1072 Init *resolveListElementReference(Record &R, const RecordVal *RV,
1073 unsigned Elt) const override;
1075 RecTy *getFieldType(const std::string &FieldName) const override;
1076 Init *getFieldInit(Record &R, const RecordVal *RV,
1077 const std::string &FieldName) const override;
1079 /// resolveReferences - This method is used by classes that refer to other
1080 /// variables which may not be defined at the time they expression is formed.
1081 /// If a value is set for the variable later, this method will be called on
1082 /// users of the value to allow the value to propagate out.
1084 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
1086 Init *getBit(unsigned Bit) const override;
1088 std::string getAsString() const override { return getName(); }
1091 /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
1093 class VarBitInit : public Init {
1097 VarBitInit(TypedInit *T, unsigned B) : Init(IK_VarBitInit), TI(T), Bit(B) {
1098 assert(T->getType() &&
1099 (isa<IntRecTy>(T->getType()) ||
1100 (isa<BitsRecTy>(T->getType()) &&
1101 cast<BitsRecTy>(T->getType())->getNumBits() > B)) &&
1102 "Illegal VarBitInit expression!");
1105 VarBitInit(const VarBitInit &Other) LLVM_DELETED_FUNCTION;
1106 VarBitInit &operator=(const VarBitInit &Other) LLVM_DELETED_FUNCTION;
1109 static bool classof(const Init *I) {
1110 return I->getKind() == IK_VarBitInit;
1112 static VarBitInit *get(TypedInit *T, unsigned B);
1114 Init *convertInitializerTo(RecTy *Ty) const override {
1115 return Ty->convertValue(const_cast<VarBitInit *>(this));
1118 Init *getBitVar() const override { return TI; }
1119 unsigned getBitNum() const override { return Bit; }
1121 std::string getAsString() const override;
1122 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
1124 Init *getBit(unsigned B) const override {
1125 assert(B < 1 && "Bit index out of range!");
1126 return const_cast<VarBitInit*>(this);
1130 /// VarListElementInit - List[4] - Represent access to one element of a var or
1132 class VarListElementInit : public TypedInit {
1136 VarListElementInit(TypedInit *T, unsigned E)
1137 : TypedInit(IK_VarListElementInit,
1138 cast<ListRecTy>(T->getType())->getElementType()),
1140 assert(T->getType() && isa<ListRecTy>(T->getType()) &&
1141 "Illegal VarBitInit expression!");
1144 VarListElementInit(const VarListElementInit &Other) LLVM_DELETED_FUNCTION;
1145 void operator=(const VarListElementInit &Other) LLVM_DELETED_FUNCTION;
1148 static bool classof(const Init *I) {
1149 return I->getKind() == IK_VarListElementInit;
1151 static VarListElementInit *get(TypedInit *T, unsigned E);
1153 Init *convertInitializerTo(RecTy *Ty) const override {
1154 return Ty->convertValue(const_cast<VarListElementInit *>(this));
1157 TypedInit *getVariable() const { return TI; }
1158 unsigned getElementNum() const { return Element; }
1160 /// resolveListElementReference - This method is used to implement
1161 /// VarListElementInit::resolveReferences. If the list element is resolvable
1162 /// now, we return the resolved value, otherwise we return null.
1163 Init *resolveListElementReference(Record &R, const RecordVal *RV,
1164 unsigned Elt) const override;
1166 std::string getAsString() const override;
1167 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
1169 Init *getBit(unsigned Bit) const override;
1172 /// DefInit - AL - Represent a reference to a 'def' in the description
1174 class DefInit : public TypedInit {
1177 DefInit(Record *D, RecordRecTy *T) : TypedInit(IK_DefInit, T), Def(D) {}
1178 friend class Record;
1180 DefInit(const DefInit &Other) LLVM_DELETED_FUNCTION;
1181 DefInit &operator=(const DefInit &Other) LLVM_DELETED_FUNCTION;
1184 static bool classof(const Init *I) {
1185 return I->getKind() == IK_DefInit;
1187 static DefInit *get(Record*);
1189 Init *convertInitializerTo(RecTy *Ty) const override {
1190 return Ty->convertValue(const_cast<DefInit *>(this));
1193 Record *getDef() const { return Def; }
1195 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
1197 RecTy *getFieldType(const std::string &FieldName) const override;
1198 Init *getFieldInit(Record &R, const RecordVal *RV,
1199 const std::string &FieldName) const override;
1201 std::string getAsString() const override;
1203 Init *getBit(unsigned Bit) const override {
1204 llvm_unreachable("Illegal bit reference off def");
1207 /// resolveListElementReference - This method is used to implement
1208 /// VarListElementInit::resolveReferences. If the list element is resolvable
1209 /// now, we return the resolved value, otherwise we return null.
1210 Init *resolveListElementReference(Record &R, const RecordVal *RV,
1211 unsigned Elt) const override {
1212 llvm_unreachable("Illegal element reference off def");
1216 /// FieldInit - X.Y - Represent a reference to a subfield of a variable
1218 class FieldInit : public TypedInit {
1219 Init *Rec; // Record we are referring to
1220 std::string FieldName; // Field we are accessing
1222 FieldInit(Init *R, const std::string &FN)
1223 : TypedInit(IK_FieldInit, R->getFieldType(FN)), Rec(R), FieldName(FN) {
1224 assert(getType() && "FieldInit with non-record type!");
1227 FieldInit(const FieldInit &Other) LLVM_DELETED_FUNCTION;
1228 FieldInit &operator=(const FieldInit &Other) LLVM_DELETED_FUNCTION;
1231 static bool classof(const Init *I) {
1232 return I->getKind() == IK_FieldInit;
1234 static FieldInit *get(Init *R, const std::string &FN);
1235 static FieldInit *get(Init *R, const Init *FN);
1237 Init *convertInitializerTo(RecTy *Ty) const override {
1238 return Ty->convertValue(const_cast<FieldInit *>(this));
1241 Init *getBit(unsigned Bit) const override;
1243 Init *resolveListElementReference(Record &R, const RecordVal *RV,
1244 unsigned Elt) const override;
1246 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
1248 std::string getAsString() const override {
1249 return Rec->getAsString() + "." + FieldName;
1253 /// DagInit - (v a, b) - Represent a DAG tree value. DAG inits are required
1254 /// to have at least one value then a (possibly empty) list of arguments. Each
1255 /// argument can have a name associated with it.
1257 class DagInit : public TypedInit, public FoldingSetNode {
1259 std::string ValName;
1260 std::vector<Init*> Args;
1261 std::vector<std::string> ArgNames;
1263 DagInit(Init *V, const std::string &VN,
1264 ArrayRef<Init *> ArgRange,
1265 ArrayRef<std::string> NameRange)
1266 : TypedInit(IK_DagInit, DagRecTy::get()), Val(V), ValName(VN),
1267 Args(ArgRange.begin(), ArgRange.end()),
1268 ArgNames(NameRange.begin(), NameRange.end()) {}
1270 DagInit(const DagInit &Other) LLVM_DELETED_FUNCTION;
1271 DagInit &operator=(const DagInit &Other) LLVM_DELETED_FUNCTION;
1274 static bool classof(const Init *I) {
1275 return I->getKind() == IK_DagInit;
1277 static DagInit *get(Init *V, const std::string &VN,
1278 ArrayRef<Init *> ArgRange,
1279 ArrayRef<std::string> NameRange);
1280 static DagInit *get(Init *V, const std::string &VN,
1282 std::pair<Init*, std::string> > &args);
1284 void Profile(FoldingSetNodeID &ID) const;
1286 Init *convertInitializerTo(RecTy *Ty) const override {
1287 return Ty->convertValue(const_cast<DagInit *>(this));
1290 Init *getOperator() const { return Val; }
1292 const std::string &getName() const { return ValName; }
1294 unsigned getNumArgs() const { return Args.size(); }
1295 Init *getArg(unsigned Num) const {
1296 assert(Num < Args.size() && "Arg number out of range!");
1299 const std::string &getArgName(unsigned Num) const {
1300 assert(Num < ArgNames.size() && "Arg number out of range!");
1301 return ArgNames[Num];
1304 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
1306 std::string getAsString() const override;
1308 typedef std::vector<Init*>::const_iterator const_arg_iterator;
1309 typedef std::vector<std::string>::const_iterator const_name_iterator;
1311 inline const_arg_iterator arg_begin() const { return Args.begin(); }
1312 inline const_arg_iterator arg_end () const { return Args.end(); }
1314 inline size_t arg_size () const { return Args.size(); }
1315 inline bool arg_empty() const { return Args.empty(); }
1317 inline const_name_iterator name_begin() const { return ArgNames.begin(); }
1318 inline const_name_iterator name_end () const { return ArgNames.end(); }
1320 inline size_t name_size () const { return ArgNames.size(); }
1321 inline bool name_empty() const { return ArgNames.empty(); }
1323 Init *getBit(unsigned Bit) const override {
1324 llvm_unreachable("Illegal bit reference off dag");
1327 Init *resolveListElementReference(Record &R, const RecordVal *RV,
1328 unsigned Elt) const override {
1329 llvm_unreachable("Illegal element reference off dag");
1333 //===----------------------------------------------------------------------===//
1334 // High-Level Classes
1335 //===----------------------------------------------------------------------===//
1344 RecordVal(Init *N, RecTy *T, unsigned P);
1345 RecordVal(const std::string &N, RecTy *T, unsigned P);
1347 const std::string &getName() const;
1348 const Init *getNameInit() const { return Name; }
1349 std::string getNameInitAsString() const {
1350 return getNameInit()->getAsUnquotedString();
1353 unsigned getPrefix() const { return Prefix; }
1354 RecTy *getType() const { return Ty; }
1355 Init *getValue() const { return Value; }
1357 bool setValue(Init *V) {
1359 Value = V->convertInitializerTo(Ty);
1360 return Value == nullptr;
1367 void print(raw_ostream &OS, bool PrintSem = true) const;
1370 inline raw_ostream &operator<<(raw_ostream &OS, const RecordVal &RV) {
1371 RV.print(OS << " ");
1376 static unsigned LastID;
1378 // Unique record ID.
1381 // Location where record was instantiated, followed by the location of
1382 // multiclass prototypes used.
1383 SmallVector<SMLoc, 4> Locs;
1384 std::vector<Init *> TemplateArgs;
1385 std::vector<RecordVal> Values;
1386 std::vector<Record *> SuperClasses;
1387 std::vector<SMRange> SuperClassRanges;
1389 // Tracks Record instances. Not owned by Record.
1390 RecordKeeper &TrackedRecords;
1399 // Constructs a record.
1400 explicit Record(const std::string &N, ArrayRef<SMLoc> locs,
1401 RecordKeeper &records, bool Anonymous = false) :
1402 ID(LastID++), Name(StringInit::get(N)), Locs(locs.begin(), locs.end()),
1403 TrackedRecords(records), TheInit(nullptr), IsAnonymous(Anonymous) {
1406 explicit Record(Init *N, ArrayRef<SMLoc> locs, RecordKeeper &records,
1407 bool Anonymous = false) :
1408 ID(LastID++), Name(N), Locs(locs.begin(), locs.end()),
1409 TrackedRecords(records), TheInit(nullptr), IsAnonymous(Anonymous) {
1413 // When copy-constructing a Record, we must still guarantee a globally unique
1414 // ID number. All other fields can be copied normally.
1415 Record(const Record &O) :
1416 ID(LastID++), Name(O.Name), Locs(O.Locs), TemplateArgs(O.TemplateArgs),
1417 Values(O.Values), SuperClasses(O.SuperClasses),
1418 SuperClassRanges(O.SuperClassRanges), TrackedRecords(O.TrackedRecords),
1419 TheInit(O.TheInit), IsAnonymous(O.IsAnonymous) { }
1423 static unsigned getNewUID() { return LastID++; }
1425 unsigned getID() const { return ID; }
1427 const std::string &getName() const;
1428 Init *getNameInit() const {
1431 const std::string getNameInitAsString() const {
1432 return getNameInit()->getAsUnquotedString();
1435 void setName(Init *Name); // Also updates RecordKeeper.
1436 void setName(const std::string &Name); // Also updates RecordKeeper.
1438 ArrayRef<SMLoc> getLoc() const { return Locs; }
1440 /// get the corresponding DefInit.
1441 DefInit *getDefInit();
1443 const std::vector<Init *> &getTemplateArgs() const {
1444 return TemplateArgs;
1446 const std::vector<RecordVal> &getValues() const { return Values; }
1447 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; }
1448 ArrayRef<SMRange> getSuperClassRanges() const { return SuperClassRanges; }
1450 bool isTemplateArg(Init *Name) const {
1451 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
1452 if (TemplateArgs[i] == Name) return true;
1455 bool isTemplateArg(StringRef Name) const {
1456 return isTemplateArg(StringInit::get(Name.str()));
1459 const RecordVal *getValue(const Init *Name) const {
1460 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1461 if (Values[i].getNameInit() == Name) return &Values[i];
1464 const RecordVal *getValue(StringRef Name) const {
1465 return getValue(StringInit::get(Name));
1467 RecordVal *getValue(const Init *Name) {
1468 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1469 if (Values[i].getNameInit() == Name) return &Values[i];
1472 RecordVal *getValue(StringRef Name) {
1473 return getValue(StringInit::get(Name));
1476 void addTemplateArg(Init *Name) {
1477 assert(!isTemplateArg(Name) && "Template arg already defined!");
1478 TemplateArgs.push_back(Name);
1480 void addTemplateArg(StringRef Name) {
1481 addTemplateArg(StringInit::get(Name.str()));
1484 void addValue(const RecordVal &RV) {
1485 assert(getValue(RV.getNameInit()) == nullptr && "Value already added!");
1486 Values.push_back(RV);
1487 if (Values.size() > 1)
1488 // Keep NAME at the end of the list. It makes record dumps a
1489 // bit prettier and allows TableGen tests to be written more
1490 // naturally. Tests can use CHECK-NEXT to look for Record
1491 // fields they expect to see after a def. They can't do that if
1492 // NAME is the first Record field.
1493 std::swap(Values[Values.size() - 2], Values[Values.size() - 1]);
1496 void removeValue(Init *Name) {
1497 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1498 if (Values[i].getNameInit() == Name) {
1499 Values.erase(Values.begin()+i);
1502 llvm_unreachable("Cannot remove an entry that does not exist!");
1505 void removeValue(StringRef Name) {
1506 removeValue(StringInit::get(Name.str()));
1509 bool isSubClassOf(const Record *R) const {
1510 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1511 if (SuperClasses[i] == R)
1516 bool isSubClassOf(StringRef Name) const {
1517 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1518 if (SuperClasses[i]->getNameInitAsString() == Name)
1523 void addSuperClass(Record *R, SMRange Range) {
1524 assert(!isSubClassOf(R) && "Already subclassing record!");
1525 SuperClasses.push_back(R);
1526 SuperClassRanges.push_back(Range);
1529 /// resolveReferences - If there are any field references that refer to fields
1530 /// that have been filled in, we can propagate the values now.
1532 void resolveReferences() { resolveReferencesTo(nullptr); }
1534 /// resolveReferencesTo - If anything in this record refers to RV, replace the
1535 /// reference to RV with the RHS of RV. If RV is null, we resolve all
1536 /// possible references.
1537 void resolveReferencesTo(const RecordVal *RV);
1539 RecordKeeper &getRecords() const {
1540 return TrackedRecords;
1543 bool isAnonymous() const {
1549 //===--------------------------------------------------------------------===//
1550 // High-level methods useful to tablegen back-ends
1553 /// getValueInit - Return the initializer for a value with the specified name,
1554 /// or throw an exception if the field does not exist.
1556 Init *getValueInit(StringRef FieldName) const;
1558 /// Return true if the named field is unset.
1559 bool isValueUnset(StringRef FieldName) const {
1560 return getValueInit(FieldName) == UnsetInit::get();
1563 /// getValueAsString - This method looks up the specified field and returns
1564 /// its value as a string, throwing an exception if the field does not exist
1565 /// or if the value is not a string.
1567 std::string getValueAsString(StringRef FieldName) const;
1569 /// getValueAsBitsInit - This method looks up the specified field and returns
1570 /// its value as a BitsInit, throwing an exception if the field does not exist
1571 /// or if the value is not the right type.
1573 BitsInit *getValueAsBitsInit(StringRef FieldName) const;
1575 /// getValueAsListInit - This method looks up the specified field and returns
1576 /// its value as a ListInit, throwing an exception if the field does not exist
1577 /// or if the value is not the right type.
1579 ListInit *getValueAsListInit(StringRef FieldName) const;
1581 /// getValueAsListOfDefs - This method looks up the specified field and
1582 /// returns its value as a vector of records, throwing an exception if the
1583 /// field does not exist or if the value is not the right type.
1585 std::vector<Record*> getValueAsListOfDefs(StringRef FieldName) const;
1587 /// getValueAsListOfInts - This method looks up the specified field and
1588 /// returns its value as a vector of integers, throwing an exception if the
1589 /// field does not exist or if the value is not the right type.
1591 std::vector<int64_t> getValueAsListOfInts(StringRef FieldName) const;
1593 /// getValueAsListOfStrings - This method looks up the specified field and
1594 /// returns its value as a vector of strings, throwing an exception if the
1595 /// field does not exist or if the value is not the right type.
1597 std::vector<std::string> getValueAsListOfStrings(StringRef FieldName) const;
1599 /// getValueAsDef - This method looks up the specified field and returns its
1600 /// value as a Record, throwing an exception if the field does not exist or if
1601 /// the value is not the right type.
1603 Record *getValueAsDef(StringRef FieldName) const;
1605 /// getValueAsBit - This method looks up the specified field and returns its
1606 /// value as a bit, throwing an exception if the field does not exist or if
1607 /// the value is not the right type.
1609 bool getValueAsBit(StringRef FieldName) const;
1611 /// getValueAsBitOrUnset - This method looks up the specified field and
1612 /// returns its value as a bit. If the field is unset, sets Unset to true and
1615 bool getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const;
1617 /// getValueAsInt - This method looks up the specified field and returns its
1618 /// value as an int64_t, throwing an exception if the field does not exist or
1619 /// if the value is not the right type.
1621 int64_t getValueAsInt(StringRef FieldName) const;
1623 /// getValueAsDag - This method looks up the specified field and returns its
1624 /// value as an Dag, throwing an exception if the field does not exist or if
1625 /// the value is not the right type.
1627 DagInit *getValueAsDag(StringRef FieldName) const;
1630 raw_ostream &operator<<(raw_ostream &OS, const Record &R);
1633 Record Rec; // Placeholder for template args and Name.
1634 typedef std::vector<Record*> RecordVector;
1635 RecordVector DefPrototypes;
1639 MultiClass(const std::string &Name, SMLoc Loc, RecordKeeper &Records) :
1640 Rec(Name, Loc, Records) {}
1643 class RecordKeeper {
1644 std::map<std::string, Record*> Classes, Defs;
1648 for (std::map<std::string, Record*>::iterator I = Classes.begin(),
1649 E = Classes.end(); I != E; ++I)
1651 for (std::map<std::string, Record*>::iterator I = Defs.begin(),
1652 E = Defs.end(); I != E; ++I)
1656 const std::map<std::string, Record*> &getClasses() const { return Classes; }
1657 const std::map<std::string, Record*> &getDefs() const { return Defs; }
1659 Record *getClass(const std::string &Name) const {
1660 std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
1661 return I == Classes.end() ? nullptr : I->second;
1663 Record *getDef(const std::string &Name) const {
1664 std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
1665 return I == Defs.end() ? nullptr : I->second;
1667 void addClass(Record *R) {
1668 bool Ins = Classes.insert(std::make_pair(R->getName(), R)).second;
1670 assert(Ins && "Class already exists");
1672 void addDef(Record *R) {
1673 bool Ins = Defs.insert(std::make_pair(R->getName(), R)).second;
1675 assert(Ins && "Record already exists");
1678 /// removeClass - Remove, but do not delete, the specified record.
1680 void removeClass(const std::string &Name) {
1681 assert(Classes.count(Name) && "Class does not exist!");
1682 Classes.erase(Name);
1684 /// removeDef - Remove, but do not delete, the specified record.
1686 void removeDef(const std::string &Name) {
1687 assert(Defs.count(Name) && "Def does not exist!");
1691 //===--------------------------------------------------------------------===//
1692 // High-level helper methods, useful for tablegen backends...
1694 /// getAllDerivedDefinitions - This method returns all concrete definitions
1695 /// that derive from the specified class name. If a class with the specified
1696 /// name does not exist, an exception is thrown.
1697 std::vector<Record*>
1698 getAllDerivedDefinitions(const std::string &ClassName) const;
1703 /// LessRecord - Sorting predicate to sort record pointers by name.
1706 bool operator()(const Record *Rec1, const Record *Rec2) const {
1707 return StringRef(Rec1->getName()).compare_numeric(Rec2->getName()) < 0;
1711 /// LessRecordByID - Sorting predicate to sort record pointers by their
1712 /// unique ID. If you just need a deterministic order, use this, since it
1713 /// just compares two `unsigned`; the other sorting predicates require
1714 /// string manipulation.
1715 struct LessRecordByID {
1716 bool operator()(const Record *LHS, const Record *RHS) const {
1717 return LHS->getID() < RHS->getID();
1721 /// LessRecordFieldName - Sorting predicate to sort record pointers by their
1724 struct LessRecordFieldName {
1725 bool operator()(const Record *Rec1, const Record *Rec2) const {
1726 return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name");
1730 struct LessRecordRegister {
1731 static size_t min(size_t a, size_t b) { return a < b ? a : b; }
1732 static bool ascii_isdigit(char x) { return x >= '0' && x <= '9'; }
1734 struct RecordParts {
1735 SmallVector<std::pair< bool, StringRef>, 4> Parts;
1737 RecordParts(StringRef Rec) {
1742 const char *Start = Rec.data();
1743 const char *Curr = Start;
1744 bool isDigitPart = ascii_isdigit(Curr[0]);
1745 for (size_t I = 0, E = Rec.size(); I != E; ++I, ++Len) {
1746 bool isDigit = ascii_isdigit(Curr[I]);
1747 if (isDigit != isDigitPart) {
1748 Parts.push_back(std::make_pair(isDigitPart, StringRef(Start, Len)));
1751 isDigitPart = ascii_isdigit(Curr[I]);
1754 // Push the last part.
1755 Parts.push_back(std::make_pair(isDigitPart, StringRef(Start, Len)));
1758 size_t size() { return Parts.size(); }
1760 std::pair<bool, StringRef> getPart(size_t i) {
1761 assert (i < Parts.size() && "Invalid idx!");
1766 bool operator()(const Record *Rec1, const Record *Rec2) const {
1767 RecordParts LHSParts(StringRef(Rec1->getName()));
1768 RecordParts RHSParts(StringRef(Rec2->getName()));
1770 size_t LHSNumParts = LHSParts.size();
1771 size_t RHSNumParts = RHSParts.size();
1772 assert (LHSNumParts && RHSNumParts && "Expected at least one part!");
1774 if (LHSNumParts != RHSNumParts)
1775 return LHSNumParts < RHSNumParts;
1777 // We expect the registers to be of the form [_a-zA-z]+([0-9]*[_a-zA-Z]*)*.
1778 for (size_t I = 0, E = LHSNumParts; I < E; I+=2) {
1779 std::pair<bool, StringRef> LHSPart = LHSParts.getPart(I);
1780 std::pair<bool, StringRef> RHSPart = RHSParts.getPart(I);
1781 // Expect even part to always be alpha.
1782 assert (LHSPart.first == false && RHSPart.first == false &&
1783 "Expected both parts to be alpha.");
1784 if (int Res = LHSPart.second.compare(RHSPart.second))
1787 for (size_t I = 1, E = LHSNumParts; I < E; I+=2) {
1788 std::pair<bool, StringRef> LHSPart = LHSParts.getPart(I);
1789 std::pair<bool, StringRef> RHSPart = RHSParts.getPart(I);
1790 // Expect odd part to always be numeric.
1791 assert (LHSPart.first == true && RHSPart.first == true &&
1792 "Expected both parts to be numeric.");
1793 if (LHSPart.second.size() != RHSPart.second.size())
1794 return LHSPart.second.size() < RHSPart.second.size();
1796 unsigned LHSVal, RHSVal;
1798 bool LHSFailed = LHSPart.second.getAsInteger(10, LHSVal); (void)LHSFailed;
1799 assert(!LHSFailed && "Unable to convert LHS to integer.");
1800 bool RHSFailed = RHSPart.second.getAsInteger(10, RHSVal); (void)RHSFailed;
1801 assert(!RHSFailed && "Unable to convert RHS to integer.");
1803 if (LHSVal != RHSVal)
1804 return LHSVal < RHSVal;
1806 return LHSNumParts < RHSNumParts;
1810 raw_ostream &operator<<(raw_ostream &OS, const RecordKeeper &RK);
1812 /// QualifyName - Return an Init with a qualifier prefix referring
1813 /// to CurRec's name.
1814 Init *QualifyName(Record &CurRec, MultiClass *CurMultiClass,
1815 Init *Name, const std::string &Scoper);
1817 /// QualifyName - Return an Init with a qualifier prefix referring
1818 /// to CurRec's name.
1819 Init *QualifyName(Record &CurRec, MultiClass *CurMultiClass,
1820 const std::string &Name, const std::string &Scoper);
1822 } // End llvm namespace