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) {
141 /// BitRecTy - 'bit' - Represent a single bit
143 class BitRecTy : public RecTy {
144 static BitRecTy Shared;
145 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;
178 /// BitsRecTy - 'bits<n>' - Represent a fixed number of bits
180 class BitsRecTy : public RecTy {
182 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;
217 /// IntRecTy - 'int' - Represent an integer value of no particular size
219 class IntRecTy : public RecTy {
220 static IntRecTy Shared;
221 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) {}
260 static bool classof(const RecTy *RT) {
261 return RT->getRecTyKind() == StringRecTyKind;
264 static StringRecTy *get() { return &Shared; }
266 Init *convertValue( UnsetInit *UI) override { return (Init*)UI; }
267 Init *convertValue( BitInit *BI) override { return nullptr; }
268 Init *convertValue( BitsInit *BI) override { return nullptr; }
269 Init *convertValue( IntInit *II) override { return nullptr; }
270 Init *convertValue(StringInit *SI) override { return (Init*)SI; }
271 Init *convertValue( ListInit *LI) override { return nullptr; }
272 Init *convertValue( UnOpInit *BO) override;
273 Init *convertValue( BinOpInit *BO) override;
274 Init *convertValue( TernOpInit *BO) override {return RecTy::convertValue(BO);}
276 Init *convertValue(VarBitInit *VB) override { return nullptr; }
277 Init *convertValue( DefInit *DI) override { return nullptr; }
278 Init *convertValue( DagInit *DI) override { return nullptr; }
279 Init *convertValue( TypedInit *TI) override;
280 Init *convertValue( VarInit *VI) override { return RecTy::convertValue(VI);}
281 Init *convertValue( FieldInit *FI) override { return RecTy::convertValue(FI);}
283 std::string getAsString() const override { return "string"; }
285 bool typeIsConvertibleTo(const RecTy *RHS) const override {
286 return RHS->baseClassOf(this);
290 /// ListRecTy - 'list<Ty>' - Represent a list of values, all of which must be of
291 /// the specified type.
293 class ListRecTy : public RecTy {
295 explicit ListRecTy(RecTy *T) : RecTy(ListRecTyKind), Ty(T) {}
296 friend ListRecTy *RecTy::getListTy();
298 static bool classof(const RecTy *RT) {
299 return RT->getRecTyKind() == ListRecTyKind;
302 static ListRecTy *get(RecTy *T) { return T->getListTy(); }
303 RecTy *getElementType() const { return Ty; }
305 Init *convertValue( UnsetInit *UI) override { return (Init*)UI; }
306 Init *convertValue( BitInit *BI) override { return nullptr; }
307 Init *convertValue( BitsInit *BI) override { return nullptr; }
308 Init *convertValue( IntInit *II) override { return nullptr; }
309 Init *convertValue(StringInit *SI) override { return nullptr; }
310 Init *convertValue( ListInit *LI) override;
311 Init *convertValue(VarBitInit *VB) override { return nullptr; }
312 Init *convertValue( DefInit *DI) override { return nullptr; }
313 Init *convertValue( DagInit *DI) override { return nullptr; }
314 Init *convertValue( UnOpInit *UI) override { return RecTy::convertValue(UI);}
315 Init *convertValue( BinOpInit *UI) override { return RecTy::convertValue(UI);}
316 Init *convertValue( TernOpInit *UI) override{ return RecTy::convertValue(UI);}
317 Init *convertValue( TypedInit *TI) override;
318 Init *convertValue( VarInit *VI) override { return RecTy::convertValue(VI);}
319 Init *convertValue( FieldInit *FI) override { return RecTy::convertValue(FI);}
321 std::string getAsString() const override;
323 bool typeIsConvertibleTo(const RecTy *RHS) const override{
324 return RHS->baseClassOf(this);
327 bool baseClassOf(const RecTy*) const override;
330 /// DagRecTy - 'dag' - Represent a dag fragment
332 class DagRecTy : public RecTy {
333 static DagRecTy Shared;
334 DagRecTy() : RecTy(DagRecTyKind) {}
336 static bool classof(const RecTy *RT) {
337 return RT->getRecTyKind() == DagRecTyKind;
340 static DagRecTy *get() { return &Shared; }
342 Init *convertValue( UnsetInit *UI) override { return (Init*)UI; }
343 Init *convertValue( BitInit *BI) override { return nullptr; }
344 Init *convertValue( BitsInit *BI) override { return nullptr; }
345 Init *convertValue( IntInit *II) override { return nullptr; }
346 Init *convertValue(StringInit *SI) override { return nullptr; }
347 Init *convertValue( ListInit *LI) override { return nullptr; }
348 Init *convertValue(VarBitInit *VB) override { return nullptr; }
349 Init *convertValue( DefInit *DI) override { return nullptr; }
350 Init *convertValue( UnOpInit *BO) override;
351 Init *convertValue( BinOpInit *BO) override;
352 Init *convertValue( TernOpInit *BO) override {return RecTy::convertValue(BO);}
353 Init *convertValue( DagInit *CI) override { return (Init*)CI; }
354 Init *convertValue( TypedInit *TI) override;
355 Init *convertValue( VarInit *VI) override { return RecTy::convertValue(VI);}
356 Init *convertValue( FieldInit *FI) override { return RecTy::convertValue(FI);}
358 std::string getAsString() const override { return "dag"; }
360 bool typeIsConvertibleTo(const RecTy *RHS) const override{
361 return RHS->baseClassOf(this);
366 /// RecordRecTy - '[classname]' - Represent an instance of a class, such as:
369 class RecordRecTy : public RecTy {
371 explicit RecordRecTy(Record *R) : RecTy(RecordRecTyKind), Rec(R) {}
374 static bool classof(const RecTy *RT) {
375 return RT->getRecTyKind() == RecordRecTyKind;
378 static RecordRecTy *get(Record *R);
380 Record *getRecord() const { return Rec; }
382 Init *convertValue( UnsetInit *UI) override { return (Init*)UI; }
383 Init *convertValue( BitInit *BI) override { return nullptr; }
384 Init *convertValue( BitsInit *BI) override { return nullptr; }
385 Init *convertValue( IntInit *II) override { return nullptr; }
386 Init *convertValue(StringInit *SI) override { return nullptr; }
387 Init *convertValue( ListInit *LI) override { return nullptr; }
388 Init *convertValue(VarBitInit *VB) override { return nullptr; }
389 Init *convertValue( UnOpInit *UI) override { return RecTy::convertValue(UI);}
390 Init *convertValue( BinOpInit *UI) override { return RecTy::convertValue(UI);}
391 Init *convertValue( TernOpInit *UI) override {return RecTy::convertValue(UI);}
392 Init *convertValue( DefInit *DI) override;
393 Init *convertValue( DagInit *DI) override { return nullptr; }
394 Init *convertValue( TypedInit *VI) override;
395 Init *convertValue( VarInit *VI) override { return RecTy::convertValue(VI);}
396 Init *convertValue( FieldInit *FI) override { return RecTy::convertValue(FI);}
398 std::string getAsString() const override;
400 bool typeIsConvertibleTo(const RecTy *RHS) const override {
401 return RHS->baseClassOf(this);
403 bool baseClassOf(const RecTy*) const override;
406 /// resolveTypes - Find a common type that T1 and T2 convert to.
407 /// Return 0 if no such type exists.
409 RecTy *resolveTypes(RecTy *T1, RecTy *T2);
411 //===----------------------------------------------------------------------===//
412 // Initializer Classes
413 //===----------------------------------------------------------------------===//
417 /// \brief Discriminator enum (for isa<>, dyn_cast<>, et al.)
419 /// This enum is laid out by a preorder traversal of the inheritance
420 /// hierarchy, and does not contain an entry for abstract classes, as per
421 /// the recommendation in docs/HowToSetUpLLVMStyleRTTI.rst.
423 /// We also explicitly include "first" and "last" values for each
424 /// interior node of the inheritance tree, to make it easier to read the
425 /// corresponding classof().
427 /// We could pack these a bit tighter by not having the IK_FirstXXXInit
428 /// and IK_LastXXXInit be their own values, but that would degrade
429 /// readability for really no benefit.
446 IK_VarListElementInit,
454 Init(const Init &) LLVM_DELETED_FUNCTION;
455 Init &operator=(const Init &) LLVM_DELETED_FUNCTION;
456 virtual void anchor();
459 InitKind getKind() const { return Kind; }
462 explicit Init(InitKind K) : Kind(K) {}
467 /// isComplete - This virtual method should be overridden by values that may
468 /// not be completely specified yet.
469 virtual bool isComplete() const { return true; }
471 /// print - Print out this value.
472 void print(raw_ostream &OS) const { OS << getAsString(); }
474 /// getAsString - Convert this value to a string form.
475 virtual std::string getAsString() const = 0;
476 /// getAsUnquotedString - Convert this value to a string form,
477 /// without adding quote markers. This primaruly affects
478 /// StringInits where we will not surround the string value with
480 virtual std::string getAsUnquotedString() const { return getAsString(); }
482 /// dump - Debugging method that may be called through a debugger, just
483 /// invokes print on stderr.
486 /// convertInitializerTo - This virtual function is a simple call-back
487 /// function that should be overridden to call the appropriate
488 /// RecTy::convertValue method.
490 virtual Init *convertInitializerTo(RecTy *Ty) const = 0;
492 /// convertInitializerBitRange - This method is used to implement the bitrange
493 /// selection operator. Given an initializer, it selects the specified bits
494 /// out, returning them as a new init of bits type. If it is not legal to use
495 /// the bit subscript operator on this initializer, return null.
498 convertInitializerBitRange(const std::vector<unsigned> &Bits) const {
502 /// convertInitListSlice - This method is used to implement the list slice
503 /// selection operator. Given an initializer, it selects the specified list
504 /// elements, returning them as a new init of list type. If it is not legal
505 /// to take a slice of this, return null.
508 convertInitListSlice(const std::vector<unsigned> &Elements) const {
512 /// getFieldType - This method is used to implement the FieldInit class.
513 /// Implementors of this method should return the type of the named field if
514 /// they are of record type.
516 virtual RecTy *getFieldType(const std::string &FieldName) const {
520 /// getFieldInit - This method complements getFieldType to return the
521 /// initializer for the specified field. If getFieldType returns non-null
522 /// this method should return non-null, otherwise it returns null.
524 virtual Init *getFieldInit(Record &R, const RecordVal *RV,
525 const std::string &FieldName) const {
529 /// resolveReferences - This method is used by classes that refer to other
530 /// variables which may not be defined at the time the expression is formed.
531 /// If a value is set for the variable later, this method will be called on
532 /// users of the value to allow the value to propagate out.
534 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const {
535 return const_cast<Init *>(this);
538 /// getBit - This method is used to return the initializer for the specified
540 virtual Init *getBit(unsigned Bit) const = 0;
542 /// getBitVar - This method is used to retrieve the initializer for bit
543 /// reference. For non-VarBitInit, it simply returns itself.
544 virtual Init *getBitVar() const { return const_cast<Init*>(this); }
546 /// getBitNum - This method is used to retrieve the bit number of a bit
547 /// reference. For non-VarBitInit, it simply returns 0.
548 virtual unsigned getBitNum() const { return 0; }
551 inline raw_ostream &operator<<(raw_ostream &OS, const Init &I) {
552 I.print(OS); return OS;
555 /// TypedInit - This is the common super-class of types that have a specific,
558 class TypedInit : public Init {
561 TypedInit(const TypedInit &Other) LLVM_DELETED_FUNCTION;
562 TypedInit &operator=(const TypedInit &Other) LLVM_DELETED_FUNCTION;
565 explicit TypedInit(InitKind K, RecTy *T) : Init(K), Ty(T) {}
568 static bool classof(const Init *I) {
569 return I->getKind() >= IK_FirstTypedInit &&
570 I->getKind() <= IK_LastTypedInit;
572 RecTy *getType() const { return Ty; }
575 convertInitializerBitRange(const std::vector<unsigned> &Bits) const override;
577 convertInitListSlice(const std::vector<unsigned> &Elements) const override;
579 /// getFieldType - This method is used to implement the FieldInit class.
580 /// Implementors of this method should return the type of the named field if
581 /// they are of record type.
583 RecTy *getFieldType(const std::string &FieldName) const override;
585 /// resolveListElementReference - This method is used to implement
586 /// VarListElementInit::resolveReferences. If the list element is resolvable
587 /// now, we return the resolved value, otherwise we return null.
588 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
589 unsigned Elt) const = 0;
593 /// UnsetInit - ? - Represents an uninitialized value
595 class UnsetInit : public Init {
596 UnsetInit() : Init(IK_UnsetInit) {}
597 UnsetInit(const UnsetInit &) LLVM_DELETED_FUNCTION;
598 UnsetInit &operator=(const UnsetInit &Other) LLVM_DELETED_FUNCTION;
599 void anchor() override;
602 static bool classof(const Init *I) {
603 return I->getKind() == IK_UnsetInit;
605 static UnsetInit *get();
607 Init *convertInitializerTo(RecTy *Ty) const override {
608 return Ty->convertValue(const_cast<UnsetInit *>(this));
611 Init *getBit(unsigned Bit) const override {
612 return const_cast<UnsetInit*>(this);
615 bool isComplete() const override { return false; }
616 std::string getAsString() const override { return "?"; }
620 /// BitInit - true/false - Represent a concrete initializer for a bit.
622 class BitInit : public Init {
625 explicit BitInit(bool V) : Init(IK_BitInit), Value(V) {}
626 BitInit(const BitInit &Other) LLVM_DELETED_FUNCTION;
627 BitInit &operator=(BitInit &Other) LLVM_DELETED_FUNCTION;
628 void anchor() override;
631 static bool classof(const Init *I) {
632 return I->getKind() == IK_BitInit;
634 static BitInit *get(bool V);
636 bool getValue() const { return Value; }
638 Init *convertInitializerTo(RecTy *Ty) const override {
639 return Ty->convertValue(const_cast<BitInit *>(this));
642 Init *getBit(unsigned Bit) const override {
643 assert(Bit < 1 && "Bit index out of range!");
644 return const_cast<BitInit*>(this);
647 std::string getAsString() const override { return Value ? "1" : "0"; }
650 /// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
651 /// It contains a vector of bits, whose size is determined by the type.
653 class BitsInit : public Init, public FoldingSetNode {
654 std::vector<Init*> Bits;
656 BitsInit(ArrayRef<Init *> Range)
657 : Init(IK_BitsInit), Bits(Range.begin(), Range.end()) {}
659 BitsInit(const BitsInit &Other) LLVM_DELETED_FUNCTION;
660 BitsInit &operator=(const BitsInit &Other) LLVM_DELETED_FUNCTION;
663 static bool classof(const Init *I) {
664 return I->getKind() == IK_BitsInit;
666 static BitsInit *get(ArrayRef<Init *> Range);
668 void Profile(FoldingSetNodeID &ID) const;
670 unsigned getNumBits() const { return Bits.size(); }
672 Init *convertInitializerTo(RecTy *Ty) const override {
673 return Ty->convertValue(const_cast<BitsInit *>(this));
676 convertInitializerBitRange(const std::vector<unsigned> &Bits) const override;
678 bool isComplete() const override {
679 for (unsigned i = 0; i != getNumBits(); ++i)
680 if (!getBit(i)->isComplete()) return false;
683 bool allInComplete() const {
684 for (unsigned i = 0; i != getNumBits(); ++i)
685 if (getBit(i)->isComplete()) return false;
688 std::string getAsString() const override;
690 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
692 Init *getBit(unsigned Bit) const override {
693 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);
740 /// StringInit - "foo" - Represent an initialization by a string value.
742 class StringInit : public TypedInit {
745 explicit StringInit(const std::string &V)
746 : TypedInit(IK_StringInit, StringRecTy::get()), Value(V) {}
748 StringInit(const StringInit &Other) LLVM_DELETED_FUNCTION;
749 StringInit &operator=(const StringInit &Other) LLVM_DELETED_FUNCTION;
750 void anchor() override;
753 static bool classof(const Init *I) {
754 return I->getKind() == IK_StringInit;
756 static StringInit *get(StringRef);
758 const std::string &getValue() const { return Value; }
760 Init *convertInitializerTo(RecTy *Ty) const override {
761 return Ty->convertValue(const_cast<StringInit *>(this));
764 std::string getAsString() const override { return "\"" + Value + "\""; }
765 std::string getAsUnquotedString() const override { return Value; }
767 /// resolveListElementReference - This method is used to implement
768 /// VarListElementInit::resolveReferences. If the list element is resolvable
769 /// now, we return the resolved value, otherwise we return null.
770 Init *resolveListElementReference(Record &R, const RecordVal *RV,
771 unsigned Elt) const override {
772 llvm_unreachable("Illegal element reference off string");
775 Init *getBit(unsigned Bit) const override {
776 llvm_unreachable("Illegal bit reference off string");
780 /// ListInit - [AL, AH, CL] - Represent a list of defs
782 class ListInit : public TypedInit, public FoldingSetNode {
783 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");
847 /// OpInit - Base class for operators
849 class OpInit : public TypedInit {
850 OpInit(const OpInit &Other) LLVM_DELETED_FUNCTION;
851 OpInit &operator=(OpInit &Other) LLVM_DELETED_FUNCTION;
854 explicit OpInit(InitKind K, RecTy *Type) : TypedInit(K, Type) {}
857 static bool classof(const Init *I) {
858 return I->getKind() >= IK_FirstOpInit &&
859 I->getKind() <= IK_LastOpInit;
861 // Clone - Clone this operator, replacing arguments with the new list
862 virtual OpInit *clone(std::vector<Init *> &Operands) const = 0;
864 virtual int getNumOperands() const = 0;
865 virtual Init *getOperand(int i) const = 0;
867 // Fold - If possible, fold this to a simpler init. Return this if not
869 virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const = 0;
871 Init *convertInitializerTo(RecTy *Ty) const override {
872 return Ty->convertValue(const_cast<OpInit *>(this));
875 Init *resolveListElementReference(Record &R, const RecordVal *RV,
876 unsigned Elt) const override;
878 Init *getBit(unsigned Bit) const override;
882 /// UnOpInit - !op (X) - Transform an init.
884 class UnOpInit : public OpInit {
886 enum UnaryOp { CAST, HEAD, TAIL, EMPTY };
891 UnOpInit(UnaryOp opc, Init *lhs, RecTy *Type)
892 : OpInit(IK_UnOpInit, Type), Opc(opc), LHS(lhs) {}
894 UnOpInit(const UnOpInit &Other) LLVM_DELETED_FUNCTION;
895 UnOpInit &operator=(const UnOpInit &Other) LLVM_DELETED_FUNCTION;
898 static bool classof(const Init *I) {
899 return I->getKind() == IK_UnOpInit;
901 static UnOpInit *get(UnaryOp opc, Init *lhs, RecTy *Type);
903 // Clone - Clone this operator, replacing arguments with the new list
904 OpInit *clone(std::vector<Init *> &Operands) const override {
905 assert(Operands.size() == 1 &&
906 "Wrong number of operands for unary operation");
907 return UnOpInit::get(getOpcode(), *Operands.begin(), getType());
910 int getNumOperands() const override { return 1; }
911 Init *getOperand(int i) const override {
912 assert(i == 0 && "Invalid operand id for unary operator");
916 UnaryOp getOpcode() const { return Opc; }
917 Init *getOperand() const { return LHS; }
919 // Fold - If possible, fold this to a simpler init. Return this if not
921 Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const override;
923 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
925 std::string getAsString() const override;
928 /// BinOpInit - !op (X, Y) - Combine two inits.
930 class BinOpInit : public OpInit {
932 enum BinaryOp { ADD, SHL, SRA, SRL, LISTCONCAT, STRCONCAT, CONCAT, EQ };
938 BinOpInit(BinaryOp opc, Init *lhs, Init *rhs, RecTy *Type) :
939 OpInit(IK_BinOpInit, Type), Opc(opc), LHS(lhs), RHS(rhs) {}
941 BinOpInit(const BinOpInit &Other) LLVM_DELETED_FUNCTION;
942 BinOpInit &operator=(const BinOpInit &Other) LLVM_DELETED_FUNCTION;
945 static bool classof(const Init *I) {
946 return I->getKind() == IK_BinOpInit;
948 static BinOpInit *get(BinaryOp opc, Init *lhs, Init *rhs,
951 // Clone - Clone this operator, replacing arguments with the new list
952 OpInit *clone(std::vector<Init *> &Operands) const override {
953 assert(Operands.size() == 2 &&
954 "Wrong number of operands for binary operation");
955 return BinOpInit::get(getOpcode(), Operands[0], Operands[1], getType());
958 int getNumOperands() const override { return 2; }
959 Init *getOperand(int i) const override {
960 assert((i == 0 || i == 1) && "Invalid operand id for binary operator");
968 BinaryOp getOpcode() const { return Opc; }
969 Init *getLHS() const { return LHS; }
970 Init *getRHS() const { return RHS; }
972 // Fold - If possible, fold this to a simpler init. Return this if not
974 Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const override;
976 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
978 std::string getAsString() const override;
981 /// TernOpInit - !op (X, Y, Z) - Combine two inits.
983 class TernOpInit : public OpInit {
985 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;
1043 /// VarInit - 'Opcode' - Represent a reference to an entire variable object.
1045 class VarInit : public TypedInit {
1048 explicit VarInit(const std::string &VN, RecTy *T)
1049 : TypedInit(IK_VarInit, T), VarName(StringInit::get(VN)) {}
1050 explicit VarInit(Init *VN, RecTy *T)
1051 : TypedInit(IK_VarInit, T), VarName(VN) {}
1053 VarInit(const VarInit &Other) LLVM_DELETED_FUNCTION;
1054 VarInit &operator=(const VarInit &Other) LLVM_DELETED_FUNCTION;
1057 static bool classof(const Init *I) {
1058 return I->getKind() == IK_VarInit;
1060 static VarInit *get(const std::string &VN, RecTy *T);
1061 static VarInit *get(Init *VN, RecTy *T);
1063 Init *convertInitializerTo(RecTy *Ty) const override {
1064 return Ty->convertValue(const_cast<VarInit *>(this));
1067 const std::string &getName() const;
1068 Init *getNameInit() const { return VarName; }
1069 std::string getNameInitAsString() const {
1070 return getNameInit()->getAsUnquotedString();
1073 Init *resolveListElementReference(Record &R, const RecordVal *RV,
1074 unsigned Elt) const override;
1076 RecTy *getFieldType(const std::string &FieldName) const override;
1077 Init *getFieldInit(Record &R, const RecordVal *RV,
1078 const std::string &FieldName) const override;
1080 /// resolveReferences - This method is used by classes that refer to other
1081 /// variables which may not be defined at the time they expression is formed.
1082 /// If a value is set for the variable later, this method will be called on
1083 /// users of the value to allow the value to propagate out.
1085 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
1087 Init *getBit(unsigned Bit) const override;
1089 std::string getAsString() const override { return getName(); }
1093 /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
1095 class VarBitInit : public Init {
1099 VarBitInit(TypedInit *T, unsigned B) : Init(IK_VarBitInit), TI(T), Bit(B) {
1100 assert(T->getType() &&
1101 (isa<IntRecTy>(T->getType()) ||
1102 (isa<BitsRecTy>(T->getType()) &&
1103 cast<BitsRecTy>(T->getType())->getNumBits() > B)) &&
1104 "Illegal VarBitInit expression!");
1107 VarBitInit(const VarBitInit &Other) LLVM_DELETED_FUNCTION;
1108 VarBitInit &operator=(const VarBitInit &Other) LLVM_DELETED_FUNCTION;
1111 static bool classof(const Init *I) {
1112 return I->getKind() == IK_VarBitInit;
1114 static VarBitInit *get(TypedInit *T, unsigned B);
1116 Init *convertInitializerTo(RecTy *Ty) const override {
1117 return Ty->convertValue(const_cast<VarBitInit *>(this));
1120 Init *getBitVar() const override { return TI; }
1121 unsigned getBitNum() const override { return Bit; }
1123 std::string getAsString() const override;
1124 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
1126 Init *getBit(unsigned B) const override {
1127 assert(B < 1 && "Bit index out of range!");
1128 return const_cast<VarBitInit*>(this);
1132 /// VarListElementInit - List[4] - Represent access to one element of a var or
1134 class VarListElementInit : public TypedInit {
1138 VarListElementInit(TypedInit *T, unsigned E)
1139 : TypedInit(IK_VarListElementInit,
1140 cast<ListRecTy>(T->getType())->getElementType()),
1142 assert(T->getType() && isa<ListRecTy>(T->getType()) &&
1143 "Illegal VarBitInit expression!");
1146 VarListElementInit(const VarListElementInit &Other) LLVM_DELETED_FUNCTION;
1147 void operator=(const VarListElementInit &Other) LLVM_DELETED_FUNCTION;
1150 static bool classof(const Init *I) {
1151 return I->getKind() == IK_VarListElementInit;
1153 static VarListElementInit *get(TypedInit *T, unsigned E);
1155 Init *convertInitializerTo(RecTy *Ty) const override {
1156 return Ty->convertValue(const_cast<VarListElementInit *>(this));
1159 TypedInit *getVariable() const { return TI; }
1160 unsigned getElementNum() const { return Element; }
1162 /// resolveListElementReference - This method is used to implement
1163 /// VarListElementInit::resolveReferences. If the list element is resolvable
1164 /// now, we return the resolved value, otherwise we return null.
1165 Init *resolveListElementReference(Record &R, const RecordVal *RV,
1166 unsigned Elt) const override;
1168 std::string getAsString() const override;
1169 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
1171 Init *getBit(unsigned Bit) const override;
1174 /// DefInit - AL - Represent a reference to a 'def' in the description
1176 class DefInit : public TypedInit {
1179 DefInit(Record *D, RecordRecTy *T) : TypedInit(IK_DefInit, T), Def(D) {}
1180 friend class Record;
1182 DefInit(const DefInit &Other) LLVM_DELETED_FUNCTION;
1183 DefInit &operator=(const DefInit &Other) LLVM_DELETED_FUNCTION;
1186 static bool classof(const Init *I) {
1187 return I->getKind() == IK_DefInit;
1189 static DefInit *get(Record*);
1191 Init *convertInitializerTo(RecTy *Ty) const override {
1192 return Ty->convertValue(const_cast<DefInit *>(this));
1195 Record *getDef() const { return Def; }
1197 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
1199 RecTy *getFieldType(const std::string &FieldName) const override;
1200 Init *getFieldInit(Record &R, const RecordVal *RV,
1201 const std::string &FieldName) const override;
1203 std::string getAsString() const override;
1205 Init *getBit(unsigned Bit) const override {
1206 llvm_unreachable("Illegal bit reference off def");
1209 /// resolveListElementReference - This method is used to implement
1210 /// VarListElementInit::resolveReferences. If the list element is resolvable
1211 /// now, we return the resolved value, otherwise we return null.
1212 Init *resolveListElementReference(Record &R, const RecordVal *RV,
1213 unsigned Elt) const override {
1214 llvm_unreachable("Illegal element reference off def");
1219 /// FieldInit - X.Y - Represent a reference to a subfield of a variable
1221 class FieldInit : public TypedInit {
1222 Init *Rec; // Record we are referring to
1223 std::string FieldName; // Field we are accessing
1225 FieldInit(Init *R, const std::string &FN)
1226 : TypedInit(IK_FieldInit, R->getFieldType(FN)), Rec(R), FieldName(FN) {
1227 assert(getType() && "FieldInit with non-record type!");
1230 FieldInit(const FieldInit &Other) LLVM_DELETED_FUNCTION;
1231 FieldInit &operator=(const FieldInit &Other) LLVM_DELETED_FUNCTION;
1234 static bool classof(const Init *I) {
1235 return I->getKind() == IK_FieldInit;
1237 static FieldInit *get(Init *R, const std::string &FN);
1238 static FieldInit *get(Init *R, const Init *FN);
1240 Init *convertInitializerTo(RecTy *Ty) const override {
1241 return Ty->convertValue(const_cast<FieldInit *>(this));
1244 Init *getBit(unsigned Bit) const override;
1246 Init *resolveListElementReference(Record &R, const RecordVal *RV,
1247 unsigned Elt) const override;
1249 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
1251 std::string getAsString() const override {
1252 return Rec->getAsString() + "." + FieldName;
1256 /// DagInit - (v a, b) - Represent a DAG tree value. DAG inits are required
1257 /// to have at least one value then a (possibly empty) list of arguments. Each
1258 /// argument can have a name associated with it.
1260 class DagInit : public TypedInit, public FoldingSetNode {
1262 std::string ValName;
1263 std::vector<Init*> Args;
1264 std::vector<std::string> ArgNames;
1266 DagInit(Init *V, const std::string &VN,
1267 ArrayRef<Init *> ArgRange,
1268 ArrayRef<std::string> NameRange)
1269 : TypedInit(IK_DagInit, DagRecTy::get()), Val(V), ValName(VN),
1270 Args(ArgRange.begin(), ArgRange.end()),
1271 ArgNames(NameRange.begin(), NameRange.end()) {}
1273 DagInit(const DagInit &Other) LLVM_DELETED_FUNCTION;
1274 DagInit &operator=(const DagInit &Other) LLVM_DELETED_FUNCTION;
1277 static bool classof(const Init *I) {
1278 return I->getKind() == IK_DagInit;
1280 static DagInit *get(Init *V, const std::string &VN,
1281 ArrayRef<Init *> ArgRange,
1282 ArrayRef<std::string> NameRange);
1283 static DagInit *get(Init *V, const std::string &VN,
1285 std::pair<Init*, std::string> > &args);
1287 void Profile(FoldingSetNodeID &ID) const;
1289 Init *convertInitializerTo(RecTy *Ty) const override {
1290 return Ty->convertValue(const_cast<DagInit *>(this));
1293 Init *getOperator() const { return Val; }
1295 const std::string &getName() const { return ValName; }
1297 unsigned getNumArgs() const { return Args.size(); }
1298 Init *getArg(unsigned Num) const {
1299 assert(Num < Args.size() && "Arg number out of range!");
1302 const std::string &getArgName(unsigned Num) const {
1303 assert(Num < ArgNames.size() && "Arg number out of range!");
1304 return ArgNames[Num];
1307 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
1309 std::string getAsString() const override;
1311 typedef std::vector<Init*>::const_iterator const_arg_iterator;
1312 typedef std::vector<std::string>::const_iterator const_name_iterator;
1314 inline const_arg_iterator arg_begin() const { return Args.begin(); }
1315 inline const_arg_iterator arg_end () const { return Args.end(); }
1317 inline size_t arg_size () const { return Args.size(); }
1318 inline bool arg_empty() const { return Args.empty(); }
1320 inline const_name_iterator name_begin() const { return ArgNames.begin(); }
1321 inline const_name_iterator name_end () const { return ArgNames.end(); }
1323 inline size_t name_size () const { return ArgNames.size(); }
1324 inline bool name_empty() const { return ArgNames.empty(); }
1326 Init *getBit(unsigned Bit) const override {
1327 llvm_unreachable("Illegal bit reference off dag");
1330 Init *resolveListElementReference(Record &R, const RecordVal *RV,
1331 unsigned Elt) const override {
1332 llvm_unreachable("Illegal element reference off dag");
1336 //===----------------------------------------------------------------------===//
1337 // High-Level Classes
1338 //===----------------------------------------------------------------------===//
1346 RecordVal(Init *N, RecTy *T, unsigned P);
1347 RecordVal(const std::string &N, RecTy *T, unsigned P);
1349 const std::string &getName() const;
1350 const Init *getNameInit() const { return Name; }
1351 std::string getNameInitAsString() const {
1352 return getNameInit()->getAsUnquotedString();
1355 unsigned getPrefix() const { return Prefix; }
1356 RecTy *getType() const { return Ty; }
1357 Init *getValue() const { return Value; }
1359 bool setValue(Init *V) {
1361 Value = V->convertInitializerTo(Ty);
1362 return Value == nullptr;
1369 void print(raw_ostream &OS, bool PrintSem = true) const;
1372 inline raw_ostream &operator<<(raw_ostream &OS, const RecordVal &RV) {
1373 RV.print(OS << " ");
1378 static unsigned LastID;
1380 // Unique record ID.
1383 // Location where record was instantiated, followed by the location of
1384 // multiclass prototypes used.
1385 SmallVector<SMLoc, 4> Locs;
1386 std::vector<Init *> TemplateArgs;
1387 std::vector<RecordVal> Values;
1388 std::vector<Record *> SuperClasses;
1389 std::vector<SMRange> SuperClassRanges;
1391 // Tracks Record instances. Not owned by Record.
1392 RecordKeeper &TrackedRecords;
1402 // Constructs a record.
1403 explicit Record(const std::string &N, ArrayRef<SMLoc> locs,
1404 RecordKeeper &records, bool Anonymous = false) :
1405 ID(LastID++), Name(StringInit::get(N)), Locs(locs.begin(), locs.end()),
1406 TrackedRecords(records), TheInit(nullptr), IsAnonymous(Anonymous) {
1409 explicit Record(Init *N, ArrayRef<SMLoc> locs, RecordKeeper &records,
1410 bool Anonymous = false) :
1411 ID(LastID++), Name(N), Locs(locs.begin(), locs.end()),
1412 TrackedRecords(records), TheInit(nullptr), IsAnonymous(Anonymous) {
1416 // When copy-constructing a Record, we must still guarantee a globally unique
1417 // ID number. All other fields can be copied normally.
1418 Record(const Record &O) :
1419 ID(LastID++), Name(O.Name), Locs(O.Locs), TemplateArgs(O.TemplateArgs),
1420 Values(O.Values), SuperClasses(O.SuperClasses),
1421 SuperClassRanges(O.SuperClassRanges), TrackedRecords(O.TrackedRecords),
1422 TheInit(O.TheInit), IsAnonymous(O.IsAnonymous) { }
1427 static unsigned getNewUID() { return LastID++; }
1430 unsigned getID() const { return ID; }
1432 const std::string &getName() const;
1433 Init *getNameInit() const {
1436 const std::string getNameInitAsString() const {
1437 return getNameInit()->getAsUnquotedString();
1440 void setName(Init *Name); // Also updates RecordKeeper.
1441 void setName(const std::string &Name); // Also updates RecordKeeper.
1443 ArrayRef<SMLoc> getLoc() const { return Locs; }
1445 /// get the corresponding DefInit.
1446 DefInit *getDefInit();
1448 const std::vector<Init *> &getTemplateArgs() const {
1449 return TemplateArgs;
1451 const std::vector<RecordVal> &getValues() const { return Values; }
1452 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; }
1453 ArrayRef<SMRange> getSuperClassRanges() const { return SuperClassRanges; }
1455 bool isTemplateArg(Init *Name) const {
1456 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
1457 if (TemplateArgs[i] == Name) return true;
1460 bool isTemplateArg(StringRef Name) const {
1461 return isTemplateArg(StringInit::get(Name.str()));
1464 const RecordVal *getValue(const Init *Name) const {
1465 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1466 if (Values[i].getNameInit() == Name) return &Values[i];
1469 const RecordVal *getValue(StringRef Name) const {
1470 return getValue(StringInit::get(Name));
1472 RecordVal *getValue(const Init *Name) {
1473 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1474 if (Values[i].getNameInit() == Name) return &Values[i];
1477 RecordVal *getValue(StringRef Name) {
1478 return getValue(StringInit::get(Name));
1481 void addTemplateArg(Init *Name) {
1482 assert(!isTemplateArg(Name) && "Template arg already defined!");
1483 TemplateArgs.push_back(Name);
1485 void addTemplateArg(StringRef Name) {
1486 addTemplateArg(StringInit::get(Name.str()));
1489 void addValue(const RecordVal &RV) {
1490 assert(getValue(RV.getNameInit()) == nullptr && "Value already added!");
1491 Values.push_back(RV);
1492 if (Values.size() > 1)
1493 // Keep NAME at the end of the list. It makes record dumps a
1494 // bit prettier and allows TableGen tests to be written more
1495 // naturally. Tests can use CHECK-NEXT to look for Record
1496 // fields they expect to see after a def. They can't do that if
1497 // NAME is the first Record field.
1498 std::swap(Values[Values.size() - 2], Values[Values.size() - 1]);
1501 void removeValue(Init *Name) {
1502 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1503 if (Values[i].getNameInit() == Name) {
1504 Values.erase(Values.begin()+i);
1507 llvm_unreachable("Cannot remove an entry that does not exist!");
1510 void removeValue(StringRef Name) {
1511 removeValue(StringInit::get(Name.str()));
1514 bool isSubClassOf(const Record *R) const {
1515 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1516 if (SuperClasses[i] == R)
1521 bool isSubClassOf(StringRef Name) const {
1522 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1523 if (SuperClasses[i]->getNameInitAsString() == Name)
1528 void addSuperClass(Record *R, SMRange Range) {
1529 assert(!isSubClassOf(R) && "Already subclassing record!");
1530 SuperClasses.push_back(R);
1531 SuperClassRanges.push_back(Range);
1534 /// resolveReferences - If there are any field references that refer to fields
1535 /// that have been filled in, we can propagate the values now.
1537 void resolveReferences() { resolveReferencesTo(nullptr); }
1539 /// resolveReferencesTo - If anything in this record refers to RV, replace the
1540 /// reference to RV with the RHS of RV. If RV is null, we resolve all
1541 /// possible references.
1542 void resolveReferencesTo(const RecordVal *RV);
1544 RecordKeeper &getRecords() const {
1545 return TrackedRecords;
1548 bool isAnonymous() const {
1554 //===--------------------------------------------------------------------===//
1555 // High-level methods useful to tablegen back-ends
1558 /// getValueInit - Return the initializer for a value with the specified name,
1559 /// or throw an exception if the field does not exist.
1561 Init *getValueInit(StringRef FieldName) const;
1563 /// Return true if the named field is unset.
1564 bool isValueUnset(StringRef FieldName) const {
1565 return getValueInit(FieldName) == UnsetInit::get();
1568 /// getValueAsString - This method looks up the specified field and returns
1569 /// its value as a string, throwing an exception if the field does not exist
1570 /// or if the value is not a string.
1572 std::string getValueAsString(StringRef FieldName) const;
1574 /// getValueAsBitsInit - This method looks up the specified field and returns
1575 /// its value as a BitsInit, throwing an exception if the field does not exist
1576 /// or if the value is not the right type.
1578 BitsInit *getValueAsBitsInit(StringRef FieldName) const;
1580 /// getValueAsListInit - This method looks up the specified field and returns
1581 /// its value as a ListInit, throwing an exception if the field does not exist
1582 /// or if the value is not the right type.
1584 ListInit *getValueAsListInit(StringRef FieldName) const;
1586 /// getValueAsListOfDefs - This method looks up the specified field and
1587 /// returns its value as a vector of records, throwing an exception if the
1588 /// field does not exist or if the value is not the right type.
1590 std::vector<Record*> getValueAsListOfDefs(StringRef FieldName) const;
1592 /// getValueAsListOfInts - This method looks up the specified field and
1593 /// returns its value as a vector of integers, throwing an exception if the
1594 /// field does not exist or if the value is not the right type.
1596 std::vector<int64_t> getValueAsListOfInts(StringRef FieldName) const;
1598 /// getValueAsListOfStrings - This method looks up the specified field and
1599 /// returns its value as a vector of strings, throwing an exception if the
1600 /// field does not exist or if the value is not the right type.
1602 std::vector<std::string> getValueAsListOfStrings(StringRef FieldName) const;
1604 /// getValueAsDef - This method looks up the specified field and returns its
1605 /// value as a Record, throwing an exception if the field does not exist or if
1606 /// the value is not the right type.
1608 Record *getValueAsDef(StringRef FieldName) const;
1610 /// getValueAsBit - This method looks up the specified field and returns its
1611 /// value as a bit, throwing an exception if the field does not exist or if
1612 /// the value is not the right type.
1614 bool getValueAsBit(StringRef FieldName) const;
1616 /// getValueAsBitOrUnset - This method looks up the specified field and
1617 /// returns its value as a bit. If the field is unset, sets Unset to true and
1620 bool getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const;
1622 /// getValueAsInt - This method looks up the specified field and returns its
1623 /// value as an int64_t, throwing an exception if the field does not exist or
1624 /// if the value is not the right type.
1626 int64_t getValueAsInt(StringRef FieldName) const;
1628 /// getValueAsDag - This method looks up the specified field and returns its
1629 /// value as an Dag, throwing an exception if the field does not exist or if
1630 /// the value is not the right type.
1632 DagInit *getValueAsDag(StringRef FieldName) const;
1635 raw_ostream &operator<<(raw_ostream &OS, const Record &R);
1638 Record Rec; // Placeholder for template args and Name.
1639 typedef std::vector<Record*> RecordVector;
1640 RecordVector DefPrototypes;
1644 MultiClass(const std::string &Name, SMLoc Loc, RecordKeeper &Records) :
1645 Rec(Name, Loc, Records) {}
1648 class RecordKeeper {
1649 std::map<std::string, Record*> Classes, Defs;
1653 for (std::map<std::string, Record*>::iterator I = Classes.begin(),
1654 E = Classes.end(); I != E; ++I)
1656 for (std::map<std::string, Record*>::iterator I = Defs.begin(),
1657 E = Defs.end(); I != E; ++I)
1661 const std::map<std::string, Record*> &getClasses() const { return Classes; }
1662 const std::map<std::string, Record*> &getDefs() const { return Defs; }
1664 Record *getClass(const std::string &Name) const {
1665 std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
1666 return I == Classes.end() ? nullptr : I->second;
1668 Record *getDef(const std::string &Name) const {
1669 std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
1670 return I == Defs.end() ? nullptr : I->second;
1672 void addClass(Record *R) {
1673 bool Ins = Classes.insert(std::make_pair(R->getName(), R)).second;
1675 assert(Ins && "Class already exists");
1677 void addDef(Record *R) {
1678 bool Ins = Defs.insert(std::make_pair(R->getName(), R)).second;
1680 assert(Ins && "Record already exists");
1683 /// removeClass - Remove, but do not delete, the specified record.
1685 void removeClass(const std::string &Name) {
1686 assert(Classes.count(Name) && "Class does not exist!");
1687 Classes.erase(Name);
1689 /// removeDef - Remove, but do not delete, the specified record.
1691 void removeDef(const std::string &Name) {
1692 assert(Defs.count(Name) && "Def does not exist!");
1696 //===--------------------------------------------------------------------===//
1697 // High-level helper methods, useful for tablegen backends...
1699 /// getAllDerivedDefinitions - This method returns all concrete definitions
1700 /// that derive from the specified class name. If a class with the specified
1701 /// name does not exist, an exception is thrown.
1702 std::vector<Record*>
1703 getAllDerivedDefinitions(const std::string &ClassName) const;
1708 /// LessRecord - Sorting predicate to sort record pointers by name.
1711 bool operator()(const Record *Rec1, const Record *Rec2) const {
1712 return StringRef(Rec1->getName()).compare_numeric(Rec2->getName()) < 0;
1716 /// LessRecordByID - Sorting predicate to sort record pointers by their
1717 /// unique ID. If you just need a deterministic order, use this, since it
1718 /// just compares two `unsigned`; the other sorting predicates require
1719 /// string manipulation.
1720 struct LessRecordByID {
1721 bool operator()(const Record *LHS, const Record *RHS) const {
1722 return LHS->getID() < RHS->getID();
1726 /// LessRecordFieldName - Sorting predicate to sort record pointers by their
1729 struct LessRecordFieldName {
1730 bool operator()(const Record *Rec1, const Record *Rec2) const {
1731 return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name");
1735 struct LessRecordRegister {
1736 static size_t min(size_t a, size_t b) { return a < b ? a : b; }
1737 static bool ascii_isdigit(char x) { return x >= '0' && x <= '9'; }
1739 struct RecordParts {
1740 SmallVector<std::pair< bool, StringRef>, 4> Parts;
1742 RecordParts(StringRef Rec) {
1747 const char *Start = Rec.data();
1748 const char *Curr = Start;
1749 bool isDigitPart = ascii_isdigit(Curr[0]);
1750 for (size_t I = 0, E = Rec.size(); I != E; ++I, ++Len) {
1751 bool isDigit = ascii_isdigit(Curr[I]);
1752 if (isDigit != isDigitPart) {
1753 Parts.push_back(std::make_pair(isDigitPart, StringRef(Start, Len)));
1756 isDigitPart = ascii_isdigit(Curr[I]);
1759 // Push the last part.
1760 Parts.push_back(std::make_pair(isDigitPart, StringRef(Start, Len)));
1763 size_t size() { return Parts.size(); }
1765 std::pair<bool, StringRef> getPart(size_t i) {
1766 assert (i < Parts.size() && "Invalid idx!");
1771 bool operator()(const Record *Rec1, const Record *Rec2) const {
1772 RecordParts LHSParts(StringRef(Rec1->getName()));
1773 RecordParts RHSParts(StringRef(Rec2->getName()));
1775 size_t LHSNumParts = LHSParts.size();
1776 size_t RHSNumParts = RHSParts.size();
1777 assert (LHSNumParts && RHSNumParts && "Expected at least one part!");
1779 if (LHSNumParts != RHSNumParts)
1780 return LHSNumParts < RHSNumParts;
1782 // We expect the registers to be of the form [_a-zA-z]+([0-9]*[_a-zA-Z]*)*.
1783 for (size_t I = 0, E = LHSNumParts; I < E; I+=2) {
1784 std::pair<bool, StringRef> LHSPart = LHSParts.getPart(I);
1785 std::pair<bool, StringRef> RHSPart = RHSParts.getPart(I);
1786 // Expect even part to always be alpha.
1787 assert (LHSPart.first == false && RHSPart.first == false &&
1788 "Expected both parts to be alpha.");
1789 if (int Res = LHSPart.second.compare(RHSPart.second))
1792 for (size_t I = 1, E = LHSNumParts; I < E; I+=2) {
1793 std::pair<bool, StringRef> LHSPart = LHSParts.getPart(I);
1794 std::pair<bool, StringRef> RHSPart = RHSParts.getPart(I);
1795 // Expect odd part to always be numeric.
1796 assert (LHSPart.first == true && RHSPart.first == true &&
1797 "Expected both parts to be numeric.");
1798 if (LHSPart.second.size() != RHSPart.second.size())
1799 return LHSPart.second.size() < RHSPart.second.size();
1801 unsigned LHSVal, RHSVal;
1803 bool LHSFailed = LHSPart.second.getAsInteger(10, LHSVal); (void)LHSFailed;
1804 assert(!LHSFailed && "Unable to convert LHS to integer.");
1805 bool RHSFailed = RHSPart.second.getAsInteger(10, RHSVal); (void)RHSFailed;
1806 assert(!RHSFailed && "Unable to convert RHS to integer.");
1808 if (LHSVal != RHSVal)
1809 return LHSVal < RHSVal;
1811 return LHSNumParts < RHSNumParts;
1815 raw_ostream &operator<<(raw_ostream &OS, const RecordKeeper &RK);
1817 /// QualifyName - Return an Init with a qualifier prefix referring
1818 /// to CurRec's name.
1819 Init *QualifyName(Record &CurRec, MultiClass *CurMultiClass,
1820 Init *Name, const std::string &Scoper);
1822 /// QualifyName - Return an Init with a qualifier prefix referring
1823 /// to CurRec's name.
1824 Init *QualifyName(Record &CurRec, MultiClass *CurMultiClass,
1825 const std::string &Name, const std::string &Scoper);
1827 } // End llvm namespace