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 TypedInit, public FoldingSetNode {
655 std::vector<Init*> Bits;
657 BitsInit(ArrayRef<Init *> Range)
658 : TypedInit(IK_BitsInit, BitsRecTy::get(Range.size())),
659 Bits(Range.begin(), Range.end()) {}
661 BitsInit(const BitsInit &Other) LLVM_DELETED_FUNCTION;
662 BitsInit &operator=(const BitsInit &Other) LLVM_DELETED_FUNCTION;
665 static bool classof(const Init *I) {
666 return I->getKind() == IK_BitsInit;
668 static BitsInit *get(ArrayRef<Init *> Range);
670 void Profile(FoldingSetNodeID &ID) const;
672 unsigned getNumBits() const { return Bits.size(); }
674 Init *convertInitializerTo(RecTy *Ty) const override {
675 return Ty->convertValue(const_cast<BitsInit *>(this));
678 convertInitializerBitRange(const std::vector<unsigned> &Bits) const override;
680 bool isComplete() const override {
681 for (unsigned i = 0; i != getNumBits(); ++i)
682 if (!getBit(i)->isComplete()) return false;
685 bool allInComplete() const {
686 for (unsigned i = 0; i != getNumBits(); ++i)
687 if (getBit(i)->isComplete()) return false;
690 std::string getAsString() const override;
692 /// resolveListElementReference - This method is used to implement
693 /// VarListElementInit::resolveReferences. If the list element is resolvable
694 /// now, we return the resolved value, otherwise we return null.
695 Init *resolveListElementReference(Record &R, const RecordVal *RV,
696 unsigned Elt) const override {
697 llvm_unreachable("Illegal element reference off bits<n>");
700 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
702 Init *getBit(unsigned Bit) const override {
703 assert(Bit < Bits.size() && "Bit index out of range!");
708 /// IntInit - 7 - Represent an initialization by a literal integer value.
710 class IntInit : public TypedInit {
713 explicit IntInit(int64_t V)
714 : TypedInit(IK_IntInit, IntRecTy::get()), Value(V) {}
716 IntInit(const IntInit &Other) LLVM_DELETED_FUNCTION;
717 IntInit &operator=(const IntInit &Other) LLVM_DELETED_FUNCTION;
720 static bool classof(const Init *I) {
721 return I->getKind() == IK_IntInit;
723 static IntInit *get(int64_t V);
725 int64_t getValue() const { return Value; }
727 Init *convertInitializerTo(RecTy *Ty) const override {
728 return Ty->convertValue(const_cast<IntInit *>(this));
731 convertInitializerBitRange(const std::vector<unsigned> &Bits) const override;
733 std::string getAsString() const override;
735 /// resolveListElementReference - This method is used to implement
736 /// VarListElementInit::resolveReferences. If the list element is resolvable
737 /// now, we return the resolved value, otherwise we return null.
738 Init *resolveListElementReference(Record &R, const RecordVal *RV,
739 unsigned Elt) const override {
740 llvm_unreachable("Illegal element reference off int");
743 Init *getBit(unsigned Bit) const override {
744 return BitInit::get((Value & (1ULL << Bit)) != 0);
748 /// StringInit - "foo" - Represent an initialization by a string value.
750 class StringInit : public TypedInit {
753 explicit StringInit(const std::string &V)
754 : TypedInit(IK_StringInit, StringRecTy::get()), Value(V) {}
756 StringInit(const StringInit &Other) LLVM_DELETED_FUNCTION;
757 StringInit &operator=(const StringInit &Other) LLVM_DELETED_FUNCTION;
758 void anchor() override;
761 static bool classof(const Init *I) {
762 return I->getKind() == IK_StringInit;
764 static StringInit *get(StringRef);
766 const std::string &getValue() const { return Value; }
768 Init *convertInitializerTo(RecTy *Ty) const override {
769 return Ty->convertValue(const_cast<StringInit *>(this));
772 std::string getAsString() const override { return "\"" + Value + "\""; }
773 std::string getAsUnquotedString() const override { return Value; }
775 /// resolveListElementReference - This method is used to implement
776 /// VarListElementInit::resolveReferences. If the list element is resolvable
777 /// now, we return the resolved value, otherwise we return null.
778 Init *resolveListElementReference(Record &R, const RecordVal *RV,
779 unsigned Elt) const override {
780 llvm_unreachable("Illegal element reference off string");
783 Init *getBit(unsigned Bit) const override {
784 llvm_unreachable("Illegal bit reference off string");
788 /// ListInit - [AL, AH, CL] - Represent a list of defs
790 class ListInit : public TypedInit, public FoldingSetNode {
791 std::vector<Init*> Values;
794 typedef std::vector<Init*>::const_iterator const_iterator;
797 explicit ListInit(ArrayRef<Init *> Range, RecTy *EltTy)
798 : TypedInit(IK_ListInit, ListRecTy::get(EltTy)),
799 Values(Range.begin(), Range.end()) {}
801 ListInit(const ListInit &Other) LLVM_DELETED_FUNCTION;
802 ListInit &operator=(const ListInit &Other) LLVM_DELETED_FUNCTION;
805 static bool classof(const Init *I) {
806 return I->getKind() == IK_ListInit;
808 static ListInit *get(ArrayRef<Init *> Range, RecTy *EltTy);
810 void Profile(FoldingSetNodeID &ID) const;
812 unsigned getSize() const { return Values.size(); }
813 Init *getElement(unsigned i) const {
814 assert(i < Values.size() && "List element index out of range!");
818 Record *getElementAsRecord(unsigned i) const;
821 convertInitListSlice(const std::vector<unsigned> &Elements) const override;
823 Init *convertInitializerTo(RecTy *Ty) const override {
824 return Ty->convertValue(const_cast<ListInit *>(this));
827 /// resolveReferences - This method is used by classes that refer to other
828 /// variables which may not be defined at the time they expression is formed.
829 /// If a value is set for the variable later, this method will be called on
830 /// users of the value to allow the value to propagate out.
832 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
834 std::string getAsString() const override;
836 ArrayRef<Init*> getValues() const { return Values; }
838 inline const_iterator begin() const { return Values.begin(); }
839 inline const_iterator end () const { return Values.end(); }
841 inline size_t size () const { return Values.size(); }
842 inline bool empty() const { return Values.empty(); }
844 /// resolveListElementReference - This method is used to implement
845 /// VarListElementInit::resolveReferences. If the list element is resolvable
846 /// now, we return the resolved value, otherwise we return null.
847 Init *resolveListElementReference(Record &R, const RecordVal *RV,
848 unsigned Elt) const override;
850 Init *getBit(unsigned Bit) const override {
851 llvm_unreachable("Illegal bit reference off list");
855 /// OpInit - Base class for operators
857 class OpInit : public TypedInit {
858 OpInit(const OpInit &Other) LLVM_DELETED_FUNCTION;
859 OpInit &operator=(OpInit &Other) LLVM_DELETED_FUNCTION;
862 explicit OpInit(InitKind K, RecTy *Type) : TypedInit(K, Type) {}
865 static bool classof(const Init *I) {
866 return I->getKind() >= IK_FirstOpInit &&
867 I->getKind() <= IK_LastOpInit;
869 // Clone - Clone this operator, replacing arguments with the new list
870 virtual OpInit *clone(std::vector<Init *> &Operands) const = 0;
872 virtual int getNumOperands() const = 0;
873 virtual Init *getOperand(int i) const = 0;
875 // Fold - If possible, fold this to a simpler init. Return this if not
877 virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const = 0;
879 Init *convertInitializerTo(RecTy *Ty) const override {
880 return Ty->convertValue(const_cast<OpInit *>(this));
883 Init *resolveListElementReference(Record &R, const RecordVal *RV,
884 unsigned Elt) const override;
886 Init *getBit(unsigned Bit) const override;
889 /// UnOpInit - !op (X) - Transform an init.
891 class UnOpInit : public OpInit {
893 enum UnaryOp { CAST, HEAD, TAIL, EMPTY };
899 UnOpInit(UnaryOp opc, Init *lhs, RecTy *Type)
900 : OpInit(IK_UnOpInit, Type), Opc(opc), LHS(lhs) {}
902 UnOpInit(const UnOpInit &Other) LLVM_DELETED_FUNCTION;
903 UnOpInit &operator=(const UnOpInit &Other) LLVM_DELETED_FUNCTION;
906 static bool classof(const Init *I) {
907 return I->getKind() == IK_UnOpInit;
909 static UnOpInit *get(UnaryOp opc, Init *lhs, RecTy *Type);
911 // Clone - Clone this operator, replacing arguments with the new list
912 OpInit *clone(std::vector<Init *> &Operands) const override {
913 assert(Operands.size() == 1 &&
914 "Wrong number of operands for unary operation");
915 return UnOpInit::get(getOpcode(), *Operands.begin(), getType());
918 int getNumOperands() const override { return 1; }
919 Init *getOperand(int i) const override {
920 assert(i == 0 && "Invalid operand id for unary operator");
924 UnaryOp getOpcode() const { return Opc; }
925 Init *getOperand() const { return LHS; }
927 // Fold - If possible, fold this to a simpler init. Return this if not
929 Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const override;
931 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
933 std::string getAsString() const override;
936 /// BinOpInit - !op (X, Y) - Combine two inits.
938 class BinOpInit : public OpInit {
940 enum BinaryOp { ADD, AND, SHL, SRA, SRL, LISTCONCAT, STRCONCAT, CONCAT, EQ };
946 BinOpInit(BinaryOp opc, Init *lhs, Init *rhs, RecTy *Type) :
947 OpInit(IK_BinOpInit, Type), Opc(opc), LHS(lhs), RHS(rhs) {}
949 BinOpInit(const BinOpInit &Other) LLVM_DELETED_FUNCTION;
950 BinOpInit &operator=(const BinOpInit &Other) LLVM_DELETED_FUNCTION;
953 static bool classof(const Init *I) {
954 return I->getKind() == IK_BinOpInit;
956 static BinOpInit *get(BinaryOp opc, Init *lhs, Init *rhs,
959 // Clone - Clone this operator, replacing arguments with the new list
960 OpInit *clone(std::vector<Init *> &Operands) const override {
961 assert(Operands.size() == 2 &&
962 "Wrong number of operands for binary operation");
963 return BinOpInit::get(getOpcode(), Operands[0], Operands[1], getType());
966 int getNumOperands() const override { return 2; }
967 Init *getOperand(int i) const override {
968 assert((i == 0 || i == 1) && "Invalid operand id for binary operator");
976 BinaryOp getOpcode() const { return Opc; }
977 Init *getLHS() const { return LHS; }
978 Init *getRHS() const { return RHS; }
980 // Fold - If possible, fold this to a simpler init. Return this if not
982 Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const override;
984 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
986 std::string getAsString() const override;
989 /// TernOpInit - !op (X, Y, Z) - Combine two inits.
991 class TernOpInit : public OpInit {
993 enum TernaryOp { SUBST, FOREACH, IF };
997 Init *LHS, *MHS, *RHS;
999 TernOpInit(TernaryOp opc, Init *lhs, Init *mhs, Init *rhs,
1001 OpInit(IK_TernOpInit, Type), Opc(opc), LHS(lhs), MHS(mhs), RHS(rhs) {}
1003 TernOpInit(const TernOpInit &Other) LLVM_DELETED_FUNCTION;
1004 TernOpInit &operator=(const TernOpInit &Other) LLVM_DELETED_FUNCTION;
1007 static bool classof(const Init *I) {
1008 return I->getKind() == IK_TernOpInit;
1010 static TernOpInit *get(TernaryOp opc, Init *lhs,
1011 Init *mhs, Init *rhs,
1014 // Clone - Clone this operator, replacing arguments with the new list
1015 OpInit *clone(std::vector<Init *> &Operands) const override {
1016 assert(Operands.size() == 3 &&
1017 "Wrong number of operands for ternary operation");
1018 return TernOpInit::get(getOpcode(), Operands[0], Operands[1], Operands[2],
1022 int getNumOperands() const override { return 3; }
1023 Init *getOperand(int i) const override {
1024 assert((i == 0 || i == 1 || i == 2) &&
1025 "Invalid operand id for ternary operator");
1028 } else if (i == 1) {
1035 TernaryOp getOpcode() const { return Opc; }
1036 Init *getLHS() const { return LHS; }
1037 Init *getMHS() const { return MHS; }
1038 Init *getRHS() const { return RHS; }
1040 // Fold - If possible, fold this to a simpler init. Return this if not
1041 // possible to fold.
1042 Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const override;
1044 bool isComplete() const override { return false; }
1046 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
1048 std::string getAsString() const override;
1051 /// VarInit - 'Opcode' - Represent a reference to an entire variable object.
1053 class VarInit : public TypedInit {
1056 explicit VarInit(const std::string &VN, RecTy *T)
1057 : TypedInit(IK_VarInit, T), VarName(StringInit::get(VN)) {}
1058 explicit VarInit(Init *VN, RecTy *T)
1059 : TypedInit(IK_VarInit, T), VarName(VN) {}
1061 VarInit(const VarInit &Other) LLVM_DELETED_FUNCTION;
1062 VarInit &operator=(const VarInit &Other) LLVM_DELETED_FUNCTION;
1065 static bool classof(const Init *I) {
1066 return I->getKind() == IK_VarInit;
1068 static VarInit *get(const std::string &VN, RecTy *T);
1069 static VarInit *get(Init *VN, RecTy *T);
1071 Init *convertInitializerTo(RecTy *Ty) const override {
1072 return Ty->convertValue(const_cast<VarInit *>(this));
1075 const std::string &getName() const;
1076 Init *getNameInit() const { return VarName; }
1077 std::string getNameInitAsString() const {
1078 return getNameInit()->getAsUnquotedString();
1081 Init *resolveListElementReference(Record &R, const RecordVal *RV,
1082 unsigned Elt) const override;
1084 RecTy *getFieldType(const std::string &FieldName) const override;
1085 Init *getFieldInit(Record &R, const RecordVal *RV,
1086 const std::string &FieldName) const override;
1088 /// resolveReferences - This method is used by classes that refer to other
1089 /// variables which may not be defined at the time they expression is formed.
1090 /// If a value is set for the variable later, this method will be called on
1091 /// users of the value to allow the value to propagate out.
1093 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
1095 Init *getBit(unsigned Bit) const override;
1097 std::string getAsString() const override { return getName(); }
1100 /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
1102 class VarBitInit : public Init {
1106 VarBitInit(TypedInit *T, unsigned B) : Init(IK_VarBitInit), TI(T), Bit(B) {
1107 assert(T->getType() &&
1108 (isa<IntRecTy>(T->getType()) ||
1109 (isa<BitsRecTy>(T->getType()) &&
1110 cast<BitsRecTy>(T->getType())->getNumBits() > B)) &&
1111 "Illegal VarBitInit expression!");
1114 VarBitInit(const VarBitInit &Other) LLVM_DELETED_FUNCTION;
1115 VarBitInit &operator=(const VarBitInit &Other) LLVM_DELETED_FUNCTION;
1118 static bool classof(const Init *I) {
1119 return I->getKind() == IK_VarBitInit;
1121 static VarBitInit *get(TypedInit *T, unsigned B);
1123 Init *convertInitializerTo(RecTy *Ty) const override {
1124 return Ty->convertValue(const_cast<VarBitInit *>(this));
1127 Init *getBitVar() const override { return TI; }
1128 unsigned getBitNum() const override { return Bit; }
1130 std::string getAsString() const override;
1131 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
1133 Init *getBit(unsigned B) const override {
1134 assert(B < 1 && "Bit index out of range!");
1135 return const_cast<VarBitInit*>(this);
1139 /// VarListElementInit - List[4] - Represent access to one element of a var or
1141 class VarListElementInit : public TypedInit {
1145 VarListElementInit(TypedInit *T, unsigned E)
1146 : TypedInit(IK_VarListElementInit,
1147 cast<ListRecTy>(T->getType())->getElementType()),
1149 assert(T->getType() && isa<ListRecTy>(T->getType()) &&
1150 "Illegal VarBitInit expression!");
1153 VarListElementInit(const VarListElementInit &Other) LLVM_DELETED_FUNCTION;
1154 void operator=(const VarListElementInit &Other) LLVM_DELETED_FUNCTION;
1157 static bool classof(const Init *I) {
1158 return I->getKind() == IK_VarListElementInit;
1160 static VarListElementInit *get(TypedInit *T, unsigned E);
1162 Init *convertInitializerTo(RecTy *Ty) const override {
1163 return Ty->convertValue(const_cast<VarListElementInit *>(this));
1166 TypedInit *getVariable() const { return TI; }
1167 unsigned getElementNum() const { return Element; }
1169 /// resolveListElementReference - This method is used to implement
1170 /// VarListElementInit::resolveReferences. If the list element is resolvable
1171 /// now, we return the resolved value, otherwise we return null.
1172 Init *resolveListElementReference(Record &R, const RecordVal *RV,
1173 unsigned Elt) const override;
1175 std::string getAsString() const override;
1176 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
1178 Init *getBit(unsigned Bit) const override;
1181 /// DefInit - AL - Represent a reference to a 'def' in the description
1183 class DefInit : public TypedInit {
1186 DefInit(Record *D, RecordRecTy *T) : TypedInit(IK_DefInit, T), Def(D) {}
1187 friend class Record;
1189 DefInit(const DefInit &Other) LLVM_DELETED_FUNCTION;
1190 DefInit &operator=(const DefInit &Other) LLVM_DELETED_FUNCTION;
1193 static bool classof(const Init *I) {
1194 return I->getKind() == IK_DefInit;
1196 static DefInit *get(Record*);
1198 Init *convertInitializerTo(RecTy *Ty) const override {
1199 return Ty->convertValue(const_cast<DefInit *>(this));
1202 Record *getDef() const { return Def; }
1204 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
1206 RecTy *getFieldType(const std::string &FieldName) const override;
1207 Init *getFieldInit(Record &R, const RecordVal *RV,
1208 const std::string &FieldName) const override;
1210 std::string getAsString() const override;
1212 Init *getBit(unsigned Bit) const override {
1213 llvm_unreachable("Illegal bit reference off def");
1216 /// resolveListElementReference - This method is used to implement
1217 /// VarListElementInit::resolveReferences. If the list element is resolvable
1218 /// now, we return the resolved value, otherwise we return null.
1219 Init *resolveListElementReference(Record &R, const RecordVal *RV,
1220 unsigned Elt) const override {
1221 llvm_unreachable("Illegal element reference off def");
1225 /// FieldInit - X.Y - Represent a reference to a subfield of a variable
1227 class FieldInit : public TypedInit {
1228 Init *Rec; // Record we are referring to
1229 std::string FieldName; // Field we are accessing
1231 FieldInit(Init *R, const std::string &FN)
1232 : TypedInit(IK_FieldInit, R->getFieldType(FN)), Rec(R), FieldName(FN) {
1233 assert(getType() && "FieldInit with non-record type!");
1236 FieldInit(const FieldInit &Other) LLVM_DELETED_FUNCTION;
1237 FieldInit &operator=(const FieldInit &Other) LLVM_DELETED_FUNCTION;
1240 static bool classof(const Init *I) {
1241 return I->getKind() == IK_FieldInit;
1243 static FieldInit *get(Init *R, const std::string &FN);
1244 static FieldInit *get(Init *R, const Init *FN);
1246 Init *convertInitializerTo(RecTy *Ty) const override {
1247 return Ty->convertValue(const_cast<FieldInit *>(this));
1250 Init *getBit(unsigned Bit) const override;
1252 Init *resolveListElementReference(Record &R, const RecordVal *RV,
1253 unsigned Elt) const override;
1255 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
1257 std::string getAsString() const override {
1258 return Rec->getAsString() + "." + FieldName;
1262 /// DagInit - (v a, b) - Represent a DAG tree value. DAG inits are required
1263 /// to have at least one value then a (possibly empty) list of arguments. Each
1264 /// argument can have a name associated with it.
1266 class DagInit : public TypedInit, public FoldingSetNode {
1268 std::string ValName;
1269 std::vector<Init*> Args;
1270 std::vector<std::string> ArgNames;
1272 DagInit(Init *V, const std::string &VN,
1273 ArrayRef<Init *> ArgRange,
1274 ArrayRef<std::string> NameRange)
1275 : TypedInit(IK_DagInit, DagRecTy::get()), Val(V), ValName(VN),
1276 Args(ArgRange.begin(), ArgRange.end()),
1277 ArgNames(NameRange.begin(), NameRange.end()) {}
1279 DagInit(const DagInit &Other) LLVM_DELETED_FUNCTION;
1280 DagInit &operator=(const DagInit &Other) LLVM_DELETED_FUNCTION;
1283 static bool classof(const Init *I) {
1284 return I->getKind() == IK_DagInit;
1286 static DagInit *get(Init *V, const std::string &VN,
1287 ArrayRef<Init *> ArgRange,
1288 ArrayRef<std::string> NameRange);
1289 static DagInit *get(Init *V, const std::string &VN,
1291 std::pair<Init*, std::string> > &args);
1293 void Profile(FoldingSetNodeID &ID) const;
1295 Init *convertInitializerTo(RecTy *Ty) const override {
1296 return Ty->convertValue(const_cast<DagInit *>(this));
1299 Init *getOperator() const { return Val; }
1301 const std::string &getName() const { return ValName; }
1303 unsigned getNumArgs() const { return Args.size(); }
1304 Init *getArg(unsigned Num) const {
1305 assert(Num < Args.size() && "Arg number out of range!");
1308 const std::string &getArgName(unsigned Num) const {
1309 assert(Num < ArgNames.size() && "Arg number out of range!");
1310 return ArgNames[Num];
1313 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
1315 std::string getAsString() const override;
1317 typedef std::vector<Init*>::const_iterator const_arg_iterator;
1318 typedef std::vector<std::string>::const_iterator const_name_iterator;
1320 inline const_arg_iterator arg_begin() const { return Args.begin(); }
1321 inline const_arg_iterator arg_end () const { return Args.end(); }
1323 inline size_t arg_size () const { return Args.size(); }
1324 inline bool arg_empty() const { return Args.empty(); }
1326 inline const_name_iterator name_begin() const { return ArgNames.begin(); }
1327 inline const_name_iterator name_end () const { return ArgNames.end(); }
1329 inline size_t name_size () const { return ArgNames.size(); }
1330 inline bool name_empty() const { return ArgNames.empty(); }
1332 Init *getBit(unsigned Bit) const override {
1333 llvm_unreachable("Illegal bit reference off dag");
1336 Init *resolveListElementReference(Record &R, const RecordVal *RV,
1337 unsigned Elt) const override {
1338 llvm_unreachable("Illegal element reference off dag");
1342 //===----------------------------------------------------------------------===//
1343 // High-Level Classes
1344 //===----------------------------------------------------------------------===//
1353 RecordVal(Init *N, RecTy *T, unsigned P);
1354 RecordVal(const std::string &N, RecTy *T, unsigned P);
1356 const std::string &getName() const;
1357 const Init *getNameInit() const { return Name; }
1358 std::string getNameInitAsString() const {
1359 return getNameInit()->getAsUnquotedString();
1362 unsigned getPrefix() const { return Prefix; }
1363 RecTy *getType() const { return Ty; }
1364 Init *getValue() const { return Value; }
1366 bool setValue(Init *V) {
1368 Value = V->convertInitializerTo(Ty);
1369 return Value == nullptr;
1376 void print(raw_ostream &OS, bool PrintSem = true) const;
1379 inline raw_ostream &operator<<(raw_ostream &OS, const RecordVal &RV) {
1380 RV.print(OS << " ");
1385 static unsigned LastID;
1387 // Unique record ID.
1390 // Location where record was instantiated, followed by the location of
1391 // multiclass prototypes used.
1392 SmallVector<SMLoc, 4> Locs;
1393 std::vector<Init *> TemplateArgs;
1394 std::vector<RecordVal> Values;
1395 std::vector<Record *> SuperClasses;
1396 std::vector<SMRange> SuperClassRanges;
1398 // Tracks Record instances. Not owned by Record.
1399 RecordKeeper &TrackedRecords;
1404 // Class-instance values can be used by other defs. For example, Struct<i>
1405 // is used here as a template argument to another class:
1407 // multiclass MultiClass<int i> {
1408 // def Def : Class<Struct<i>>;
1410 // These need to get fully resolved before instantiating any other
1411 // definitions that usie them (e.g. Def). However, inside a multiclass they
1412 // can't be immediately resolved so we mark them ResolveFirst to fully
1413 // resolve them later as soon as the multiclass is instantiated.
1420 // Constructs a record.
1421 explicit Record(const std::string &N, ArrayRef<SMLoc> locs,
1422 RecordKeeper &records, bool Anonymous = false) :
1423 ID(LastID++), Name(StringInit::get(N)), Locs(locs.begin(), locs.end()),
1424 TrackedRecords(records), TheInit(nullptr), IsAnonymous(Anonymous),
1425 ResolveFirst(false) {
1428 explicit Record(Init *N, ArrayRef<SMLoc> locs, RecordKeeper &records,
1429 bool Anonymous = false) :
1430 ID(LastID++), Name(N), Locs(locs.begin(), locs.end()),
1431 TrackedRecords(records), TheInit(nullptr), IsAnonymous(Anonymous),
1432 ResolveFirst(false) {
1436 // When copy-constructing a Record, we must still guarantee a globally unique
1437 // ID number. All other fields can be copied normally.
1438 Record(const Record &O) :
1439 ID(LastID++), Name(O.Name), Locs(O.Locs), TemplateArgs(O.TemplateArgs),
1440 Values(O.Values), SuperClasses(O.SuperClasses),
1441 SuperClassRanges(O.SuperClassRanges), TrackedRecords(O.TrackedRecords),
1442 TheInit(O.TheInit), IsAnonymous(O.IsAnonymous),
1443 ResolveFirst(O.ResolveFirst) { }
1447 static unsigned getNewUID() { return LastID++; }
1449 unsigned getID() const { return ID; }
1451 const std::string &getName() const;
1452 Init *getNameInit() const {
1455 const std::string getNameInitAsString() const {
1456 return getNameInit()->getAsUnquotedString();
1459 void setName(Init *Name); // Also updates RecordKeeper.
1460 void setName(const std::string &Name); // Also updates RecordKeeper.
1462 ArrayRef<SMLoc> getLoc() const { return Locs; }
1464 /// get the corresponding DefInit.
1465 DefInit *getDefInit();
1467 const std::vector<Init *> &getTemplateArgs() const {
1468 return TemplateArgs;
1470 const std::vector<RecordVal> &getValues() const { return Values; }
1471 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; }
1472 ArrayRef<SMRange> getSuperClassRanges() const { return SuperClassRanges; }
1474 bool isTemplateArg(Init *Name) const {
1475 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
1476 if (TemplateArgs[i] == Name) return true;
1479 bool isTemplateArg(StringRef Name) const {
1480 return isTemplateArg(StringInit::get(Name.str()));
1483 const RecordVal *getValue(const Init *Name) const {
1484 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1485 if (Values[i].getNameInit() == Name) return &Values[i];
1488 const RecordVal *getValue(StringRef Name) const {
1489 return getValue(StringInit::get(Name));
1491 RecordVal *getValue(const Init *Name) {
1492 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1493 if (Values[i].getNameInit() == Name) return &Values[i];
1496 RecordVal *getValue(StringRef Name) {
1497 return getValue(StringInit::get(Name));
1500 void addTemplateArg(Init *Name) {
1501 assert(!isTemplateArg(Name) && "Template arg already defined!");
1502 TemplateArgs.push_back(Name);
1504 void addTemplateArg(StringRef Name) {
1505 addTemplateArg(StringInit::get(Name.str()));
1508 void addValue(const RecordVal &RV) {
1509 assert(getValue(RV.getNameInit()) == nullptr && "Value already added!");
1510 Values.push_back(RV);
1511 if (Values.size() > 1)
1512 // Keep NAME at the end of the list. It makes record dumps a
1513 // bit prettier and allows TableGen tests to be written more
1514 // naturally. Tests can use CHECK-NEXT to look for Record
1515 // fields they expect to see after a def. They can't do that if
1516 // NAME is the first Record field.
1517 std::swap(Values[Values.size() - 2], Values[Values.size() - 1]);
1520 void removeValue(Init *Name) {
1521 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1522 if (Values[i].getNameInit() == Name) {
1523 Values.erase(Values.begin()+i);
1526 llvm_unreachable("Cannot remove an entry that does not exist!");
1529 void removeValue(StringRef Name) {
1530 removeValue(StringInit::get(Name.str()));
1533 bool isSubClassOf(const Record *R) const {
1534 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1535 if (SuperClasses[i] == R)
1540 bool isSubClassOf(StringRef Name) const {
1541 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1542 if (SuperClasses[i]->getNameInitAsString() == Name)
1547 void addSuperClass(Record *R, SMRange Range) {
1548 assert(!isSubClassOf(R) && "Already subclassing record!");
1549 SuperClasses.push_back(R);
1550 SuperClassRanges.push_back(Range);
1553 /// resolveReferences - If there are any field references that refer to fields
1554 /// that have been filled in, we can propagate the values now.
1556 void resolveReferences() { resolveReferencesTo(nullptr); }
1558 /// resolveReferencesTo - If anything in this record refers to RV, replace the
1559 /// reference to RV with the RHS of RV. If RV is null, we resolve all
1560 /// possible references.
1561 void resolveReferencesTo(const RecordVal *RV);
1563 RecordKeeper &getRecords() const {
1564 return TrackedRecords;
1567 bool isAnonymous() const {
1571 bool isResolveFirst() const {
1572 return ResolveFirst;
1575 void setResolveFirst(bool b) {
1581 //===--------------------------------------------------------------------===//
1582 // High-level methods useful to tablegen back-ends
1585 /// getValueInit - Return the initializer for a value with the specified name,
1586 /// or throw an exception if the field does not exist.
1588 Init *getValueInit(StringRef FieldName) const;
1590 /// Return true if the named field is unset.
1591 bool isValueUnset(StringRef FieldName) const {
1592 return getValueInit(FieldName) == UnsetInit::get();
1595 /// getValueAsString - This method looks up the specified field and returns
1596 /// its value as a string, throwing an exception if the field does not exist
1597 /// or if the value is not a string.
1599 std::string getValueAsString(StringRef FieldName) const;
1601 /// getValueAsBitsInit - This method looks up the specified field and returns
1602 /// its value as a BitsInit, throwing an exception if the field does not exist
1603 /// or if the value is not the right type.
1605 BitsInit *getValueAsBitsInit(StringRef FieldName) const;
1607 /// getValueAsListInit - This method looks up the specified field and returns
1608 /// its value as a ListInit, throwing an exception if the field does not exist
1609 /// or if the value is not the right type.
1611 ListInit *getValueAsListInit(StringRef FieldName) const;
1613 /// getValueAsListOfDefs - This method looks up the specified field and
1614 /// returns its value as a vector of records, throwing an exception if the
1615 /// field does not exist or if the value is not the right type.
1617 std::vector<Record*> getValueAsListOfDefs(StringRef FieldName) const;
1619 /// getValueAsListOfInts - This method looks up the specified field and
1620 /// returns its value as a vector of integers, throwing an exception if the
1621 /// field does not exist or if the value is not the right type.
1623 std::vector<int64_t> getValueAsListOfInts(StringRef FieldName) const;
1625 /// getValueAsListOfStrings - This method looks up the specified field and
1626 /// returns its value as a vector of strings, throwing an exception if the
1627 /// field does not exist or if the value is not the right type.
1629 std::vector<std::string> getValueAsListOfStrings(StringRef FieldName) const;
1631 /// getValueAsDef - This method looks up the specified field and returns its
1632 /// value as a Record, throwing an exception if the field does not exist or if
1633 /// the value is not the right type.
1635 Record *getValueAsDef(StringRef FieldName) const;
1637 /// getValueAsBit - This method looks up the specified field and returns its
1638 /// value as a bit, throwing an exception if the field does not exist or if
1639 /// the value is not the right type.
1641 bool getValueAsBit(StringRef FieldName) const;
1643 /// getValueAsBitOrUnset - This method looks up the specified field and
1644 /// returns its value as a bit. If the field is unset, sets Unset to true and
1647 bool getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const;
1649 /// getValueAsInt - This method looks up the specified field and returns its
1650 /// value as an int64_t, throwing an exception if the field does not exist or
1651 /// if the value is not the right type.
1653 int64_t getValueAsInt(StringRef FieldName) const;
1655 /// getValueAsDag - This method looks up the specified field and returns its
1656 /// value as an Dag, throwing an exception if the field does not exist or if
1657 /// the value is not the right type.
1659 DagInit *getValueAsDag(StringRef FieldName) const;
1662 raw_ostream &operator<<(raw_ostream &OS, const Record &R);
1665 Record Rec; // Placeholder for template args and Name.
1666 typedef std::vector<std::unique_ptr<Record>> RecordVector;
1667 RecordVector DefPrototypes;
1671 MultiClass(const std::string &Name, SMLoc Loc, RecordKeeper &Records) :
1672 Rec(Name, Loc, Records) {}
1675 class RecordKeeper {
1676 typedef std::map<std::string, std::unique_ptr<Record>> RecordMap;
1677 RecordMap Classes, Defs;
1680 const RecordMap &getClasses() const { return Classes; }
1681 const RecordMap &getDefs() const { return Defs; }
1683 Record *getClass(const std::string &Name) const {
1684 auto I = Classes.find(Name);
1685 return I == Classes.end() ? nullptr : I->second.get();
1687 Record *getDef(const std::string &Name) const {
1688 auto I = Defs.find(Name);
1689 return I == Defs.end() ? nullptr : I->second.get();
1691 void addClass(std::unique_ptr<Record> R) {
1692 bool Ins = Classes.insert(std::make_pair(R->getName(),
1693 std::move(R))).second;
1695 assert(Ins && "Class already exists");
1697 void addDef(std::unique_ptr<Record> R) {
1698 bool Ins = Defs.insert(std::make_pair(R->getName(),
1699 std::move(R))).second;
1701 assert(Ins && "Record already exists");
1704 //===--------------------------------------------------------------------===//
1705 // High-level helper methods, useful for tablegen backends...
1707 /// getAllDerivedDefinitions - This method returns all concrete definitions
1708 /// that derive from the specified class name. If a class with the specified
1709 /// name does not exist, an exception is thrown.
1710 std::vector<Record*>
1711 getAllDerivedDefinitions(const std::string &ClassName) const;
1716 /// LessRecord - Sorting predicate to sort record pointers by name.
1719 bool operator()(const Record *Rec1, const Record *Rec2) const {
1720 return StringRef(Rec1->getName()).compare_numeric(Rec2->getName()) < 0;
1724 /// LessRecordByID - Sorting predicate to sort record pointers by their
1725 /// unique ID. If you just need a deterministic order, use this, since it
1726 /// just compares two `unsigned`; the other sorting predicates require
1727 /// string manipulation.
1728 struct LessRecordByID {
1729 bool operator()(const Record *LHS, const Record *RHS) const {
1730 return LHS->getID() < RHS->getID();
1734 /// LessRecordFieldName - Sorting predicate to sort record pointers by their
1737 struct LessRecordFieldName {
1738 bool operator()(const Record *Rec1, const Record *Rec2) const {
1739 return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name");
1743 struct LessRecordRegister {
1744 static size_t min(size_t a, size_t b) { return a < b ? a : b; }
1745 static bool ascii_isdigit(char x) { return x >= '0' && x <= '9'; }
1747 struct RecordParts {
1748 SmallVector<std::pair< bool, StringRef>, 4> Parts;
1750 RecordParts(StringRef Rec) {
1755 const char *Start = Rec.data();
1756 const char *Curr = Start;
1757 bool isDigitPart = ascii_isdigit(Curr[0]);
1758 for (size_t I = 0, E = Rec.size(); I != E; ++I, ++Len) {
1759 bool isDigit = ascii_isdigit(Curr[I]);
1760 if (isDigit != isDigitPart) {
1761 Parts.push_back(std::make_pair(isDigitPart, StringRef(Start, Len)));
1764 isDigitPart = ascii_isdigit(Curr[I]);
1767 // Push the last part.
1768 Parts.push_back(std::make_pair(isDigitPart, StringRef(Start, Len)));
1771 size_t size() { return Parts.size(); }
1773 std::pair<bool, StringRef> getPart(size_t i) {
1774 assert (i < Parts.size() && "Invalid idx!");
1779 bool operator()(const Record *Rec1, const Record *Rec2) const {
1780 RecordParts LHSParts(StringRef(Rec1->getName()));
1781 RecordParts RHSParts(StringRef(Rec2->getName()));
1783 size_t LHSNumParts = LHSParts.size();
1784 size_t RHSNumParts = RHSParts.size();
1785 assert (LHSNumParts && RHSNumParts && "Expected at least one part!");
1787 if (LHSNumParts != RHSNumParts)
1788 return LHSNumParts < RHSNumParts;
1790 // We expect the registers to be of the form [_a-zA-z]+([0-9]*[_a-zA-Z]*)*.
1791 for (size_t I = 0, E = LHSNumParts; I < E; I+=2) {
1792 std::pair<bool, StringRef> LHSPart = LHSParts.getPart(I);
1793 std::pair<bool, StringRef> RHSPart = RHSParts.getPart(I);
1794 // Expect even part to always be alpha.
1795 assert (LHSPart.first == false && RHSPart.first == false &&
1796 "Expected both parts to be alpha.");
1797 if (int Res = LHSPart.second.compare(RHSPart.second))
1800 for (size_t I = 1, E = LHSNumParts; I < E; I+=2) {
1801 std::pair<bool, StringRef> LHSPart = LHSParts.getPart(I);
1802 std::pair<bool, StringRef> RHSPart = RHSParts.getPart(I);
1803 // Expect odd part to always be numeric.
1804 assert (LHSPart.first == true && RHSPart.first == true &&
1805 "Expected both parts to be numeric.");
1806 if (LHSPart.second.size() != RHSPart.second.size())
1807 return LHSPart.second.size() < RHSPart.second.size();
1809 unsigned LHSVal, RHSVal;
1811 bool LHSFailed = LHSPart.second.getAsInteger(10, LHSVal); (void)LHSFailed;
1812 assert(!LHSFailed && "Unable to convert LHS to integer.");
1813 bool RHSFailed = RHSPart.second.getAsInteger(10, RHSVal); (void)RHSFailed;
1814 assert(!RHSFailed && "Unable to convert RHS to integer.");
1816 if (LHSVal != RHSVal)
1817 return LHSVal < RHSVal;
1819 return LHSNumParts < RHSNumParts;
1823 raw_ostream &operator<<(raw_ostream &OS, const RecordKeeper &RK);
1825 /// QualifyName - Return an Init with a qualifier prefix referring
1826 /// to CurRec's name.
1827 Init *QualifyName(Record &CurRec, MultiClass *CurMultiClass,
1828 Init *Name, const std::string &Scoper);
1830 /// QualifyName - Return an Init with a qualifier prefix referring
1831 /// to CurRec's name.
1832 Init *QualifyName(Record &CurRec, MultiClass *CurMultiClass,
1833 const std::string &Name, const std::string &Scoper);
1835 } // End llvm namespace