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, STRCONCAT, CONCAT, EQ };
937 BinOpInit(BinaryOp opc, Init *lhs, Init *rhs, RecTy *Type) :
938 OpInit(IK_BinOpInit, Type), Opc(opc), LHS(lhs), RHS(rhs) {}
940 BinOpInit(const BinOpInit &Other) LLVM_DELETED_FUNCTION;
941 BinOpInit &operator=(const BinOpInit &Other) LLVM_DELETED_FUNCTION;
944 static bool classof(const Init *I) {
945 return I->getKind() == IK_BinOpInit;
947 static BinOpInit *get(BinaryOp opc, Init *lhs, Init *rhs,
950 // Clone - Clone this operator, replacing arguments with the new list
951 OpInit *clone(std::vector<Init *> &Operands) const override {
952 assert(Operands.size() == 2 &&
953 "Wrong number of operands for binary operation");
954 return BinOpInit::get(getOpcode(), Operands[0], Operands[1], getType());
957 int getNumOperands() const override { return 2; }
958 Init *getOperand(int i) const override {
959 assert((i == 0 || i == 1) && "Invalid operand id for binary operator");
967 BinaryOp getOpcode() const { return Opc; }
968 Init *getLHS() const { return LHS; }
969 Init *getRHS() const { return RHS; }
971 // Fold - If possible, fold this to a simpler init. Return this if not
973 Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const override;
975 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
977 std::string getAsString() const override;
980 /// TernOpInit - !op (X, Y, Z) - Combine two inits.
982 class TernOpInit : public OpInit {
984 enum TernaryOp { SUBST, FOREACH, IF };
987 Init *LHS, *MHS, *RHS;
989 TernOpInit(TernaryOp opc, Init *lhs, Init *mhs, Init *rhs,
991 OpInit(IK_TernOpInit, Type), Opc(opc), LHS(lhs), MHS(mhs), RHS(rhs) {}
993 TernOpInit(const TernOpInit &Other) LLVM_DELETED_FUNCTION;
994 TernOpInit &operator=(const TernOpInit &Other) LLVM_DELETED_FUNCTION;
997 static bool classof(const Init *I) {
998 return I->getKind() == IK_TernOpInit;
1000 static TernOpInit *get(TernaryOp opc, Init *lhs,
1001 Init *mhs, Init *rhs,
1004 // Clone - Clone this operator, replacing arguments with the new list
1005 OpInit *clone(std::vector<Init *> &Operands) const override {
1006 assert(Operands.size() == 3 &&
1007 "Wrong number of operands for ternary operation");
1008 return TernOpInit::get(getOpcode(), Operands[0], Operands[1], Operands[2],
1012 int getNumOperands() const override { return 3; }
1013 Init *getOperand(int i) const override {
1014 assert((i == 0 || i == 1 || i == 2) &&
1015 "Invalid operand id for ternary operator");
1018 } else if (i == 1) {
1025 TernaryOp getOpcode() const { return Opc; }
1026 Init *getLHS() const { return LHS; }
1027 Init *getMHS() const { return MHS; }
1028 Init *getRHS() const { return RHS; }
1030 // Fold - If possible, fold this to a simpler init. Return this if not
1031 // possible to fold.
1032 Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const override;
1034 bool isComplete() const override { return false; }
1036 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
1038 std::string getAsString() const override;
1042 /// VarInit - 'Opcode' - Represent a reference to an entire variable object.
1044 class VarInit : public TypedInit {
1047 explicit VarInit(const std::string &VN, RecTy *T)
1048 : TypedInit(IK_VarInit, T), VarName(StringInit::get(VN)) {}
1049 explicit VarInit(Init *VN, RecTy *T)
1050 : TypedInit(IK_VarInit, T), VarName(VN) {}
1052 VarInit(const VarInit &Other) LLVM_DELETED_FUNCTION;
1053 VarInit &operator=(const VarInit &Other) LLVM_DELETED_FUNCTION;
1056 static bool classof(const Init *I) {
1057 return I->getKind() == IK_VarInit;
1059 static VarInit *get(const std::string &VN, RecTy *T);
1060 static VarInit *get(Init *VN, RecTy *T);
1062 Init *convertInitializerTo(RecTy *Ty) const override {
1063 return Ty->convertValue(const_cast<VarInit *>(this));
1066 const std::string &getName() const;
1067 Init *getNameInit() const { return VarName; }
1068 std::string getNameInitAsString() const {
1069 return getNameInit()->getAsUnquotedString();
1072 Init *resolveListElementReference(Record &R, const RecordVal *RV,
1073 unsigned Elt) const override;
1075 RecTy *getFieldType(const std::string &FieldName) const override;
1076 Init *getFieldInit(Record &R, const RecordVal *RV,
1077 const std::string &FieldName) const override;
1079 /// resolveReferences - This method is used by classes that refer to other
1080 /// variables which may not be defined at the time they expression is formed.
1081 /// If a value is set for the variable later, this method will be called on
1082 /// users of the value to allow the value to propagate out.
1084 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
1086 Init *getBit(unsigned Bit) const override;
1088 std::string getAsString() const override { return getName(); }
1092 /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
1094 class VarBitInit : public Init {
1098 VarBitInit(TypedInit *T, unsigned B) : Init(IK_VarBitInit), TI(T), Bit(B) {
1099 assert(T->getType() &&
1100 (isa<IntRecTy>(T->getType()) ||
1101 (isa<BitsRecTy>(T->getType()) &&
1102 cast<BitsRecTy>(T->getType())->getNumBits() > B)) &&
1103 "Illegal VarBitInit expression!");
1106 VarBitInit(const VarBitInit &Other) LLVM_DELETED_FUNCTION;
1107 VarBitInit &operator=(const VarBitInit &Other) LLVM_DELETED_FUNCTION;
1110 static bool classof(const Init *I) {
1111 return I->getKind() == IK_VarBitInit;
1113 static VarBitInit *get(TypedInit *T, unsigned B);
1115 Init *convertInitializerTo(RecTy *Ty) const override {
1116 return Ty->convertValue(const_cast<VarBitInit *>(this));
1119 Init *getBitVar() const override { return TI; }
1120 unsigned getBitNum() const override { return Bit; }
1122 std::string getAsString() const override;
1123 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
1125 Init *getBit(unsigned B) const override {
1126 assert(B < 1 && "Bit index out of range!");
1127 return const_cast<VarBitInit*>(this);
1131 /// VarListElementInit - List[4] - Represent access to one element of a var or
1133 class VarListElementInit : public TypedInit {
1137 VarListElementInit(TypedInit *T, unsigned E)
1138 : TypedInit(IK_VarListElementInit,
1139 cast<ListRecTy>(T->getType())->getElementType()),
1141 assert(T->getType() && isa<ListRecTy>(T->getType()) &&
1142 "Illegal VarBitInit expression!");
1145 VarListElementInit(const VarListElementInit &Other) LLVM_DELETED_FUNCTION;
1146 void operator=(const VarListElementInit &Other) LLVM_DELETED_FUNCTION;
1149 static bool classof(const Init *I) {
1150 return I->getKind() == IK_VarListElementInit;
1152 static VarListElementInit *get(TypedInit *T, unsigned E);
1154 Init *convertInitializerTo(RecTy *Ty) const override {
1155 return Ty->convertValue(const_cast<VarListElementInit *>(this));
1158 TypedInit *getVariable() const { return TI; }
1159 unsigned getElementNum() const { return Element; }
1161 /// resolveListElementReference - This method is used to implement
1162 /// VarListElementInit::resolveReferences. If the list element is resolvable
1163 /// now, we return the resolved value, otherwise we return null.
1164 Init *resolveListElementReference(Record &R, const RecordVal *RV,
1165 unsigned Elt) const override;
1167 std::string getAsString() const override;
1168 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
1170 Init *getBit(unsigned Bit) const override;
1173 /// DefInit - AL - Represent a reference to a 'def' in the description
1175 class DefInit : public TypedInit {
1178 DefInit(Record *D, RecordRecTy *T) : TypedInit(IK_DefInit, T), Def(D) {}
1179 friend class Record;
1181 DefInit(const DefInit &Other) LLVM_DELETED_FUNCTION;
1182 DefInit &operator=(const DefInit &Other) LLVM_DELETED_FUNCTION;
1185 static bool classof(const Init *I) {
1186 return I->getKind() == IK_DefInit;
1188 static DefInit *get(Record*);
1190 Init *convertInitializerTo(RecTy *Ty) const override {
1191 return Ty->convertValue(const_cast<DefInit *>(this));
1194 Record *getDef() const { return Def; }
1196 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
1198 RecTy *getFieldType(const std::string &FieldName) const override;
1199 Init *getFieldInit(Record &R, const RecordVal *RV,
1200 const std::string &FieldName) const override;
1202 std::string getAsString() const override;
1204 Init *getBit(unsigned Bit) const override {
1205 llvm_unreachable("Illegal bit reference off def");
1208 /// resolveListElementReference - This method is used to implement
1209 /// VarListElementInit::resolveReferences. If the list element is resolvable
1210 /// now, we return the resolved value, otherwise we return null.
1211 Init *resolveListElementReference(Record &R, const RecordVal *RV,
1212 unsigned Elt) const override {
1213 llvm_unreachable("Illegal element reference off def");
1218 /// FieldInit - X.Y - Represent a reference to a subfield of a variable
1220 class FieldInit : public TypedInit {
1221 Init *Rec; // Record we are referring to
1222 std::string FieldName; // Field we are accessing
1224 FieldInit(Init *R, const std::string &FN)
1225 : TypedInit(IK_FieldInit, R->getFieldType(FN)), Rec(R), FieldName(FN) {
1226 assert(getType() && "FieldInit with non-record type!");
1229 FieldInit(const FieldInit &Other) LLVM_DELETED_FUNCTION;
1230 FieldInit &operator=(const FieldInit &Other) LLVM_DELETED_FUNCTION;
1233 static bool classof(const Init *I) {
1234 return I->getKind() == IK_FieldInit;
1236 static FieldInit *get(Init *R, const std::string &FN);
1237 static FieldInit *get(Init *R, const Init *FN);
1239 Init *convertInitializerTo(RecTy *Ty) const override {
1240 return Ty->convertValue(const_cast<FieldInit *>(this));
1243 Init *getBit(unsigned Bit) const override;
1245 Init *resolveListElementReference(Record &R, const RecordVal *RV,
1246 unsigned Elt) const override;
1248 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
1250 std::string getAsString() const override {
1251 return Rec->getAsString() + "." + FieldName;
1255 /// DagInit - (v a, b) - Represent a DAG tree value. DAG inits are required
1256 /// to have at least one value then a (possibly empty) list of arguments. Each
1257 /// argument can have a name associated with it.
1259 class DagInit : public TypedInit, public FoldingSetNode {
1261 std::string ValName;
1262 std::vector<Init*> Args;
1263 std::vector<std::string> ArgNames;
1265 DagInit(Init *V, const std::string &VN,
1266 ArrayRef<Init *> ArgRange,
1267 ArrayRef<std::string> NameRange)
1268 : TypedInit(IK_DagInit, DagRecTy::get()), Val(V), ValName(VN),
1269 Args(ArgRange.begin(), ArgRange.end()),
1270 ArgNames(NameRange.begin(), NameRange.end()) {}
1272 DagInit(const DagInit &Other) LLVM_DELETED_FUNCTION;
1273 DagInit &operator=(const DagInit &Other) LLVM_DELETED_FUNCTION;
1276 static bool classof(const Init *I) {
1277 return I->getKind() == IK_DagInit;
1279 static DagInit *get(Init *V, const std::string &VN,
1280 ArrayRef<Init *> ArgRange,
1281 ArrayRef<std::string> NameRange);
1282 static DagInit *get(Init *V, const std::string &VN,
1284 std::pair<Init*, std::string> > &args);
1286 void Profile(FoldingSetNodeID &ID) const;
1288 Init *convertInitializerTo(RecTy *Ty) const override {
1289 return Ty->convertValue(const_cast<DagInit *>(this));
1292 Init *getOperator() const { return Val; }
1294 const std::string &getName() const { return ValName; }
1296 unsigned getNumArgs() const { return Args.size(); }
1297 Init *getArg(unsigned Num) const {
1298 assert(Num < Args.size() && "Arg number out of range!");
1301 const std::string &getArgName(unsigned Num) const {
1302 assert(Num < ArgNames.size() && "Arg number out of range!");
1303 return ArgNames[Num];
1306 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
1308 std::string getAsString() const override;
1310 typedef std::vector<Init*>::const_iterator const_arg_iterator;
1311 typedef std::vector<std::string>::const_iterator const_name_iterator;
1313 inline const_arg_iterator arg_begin() const { return Args.begin(); }
1314 inline const_arg_iterator arg_end () const { return Args.end(); }
1316 inline size_t arg_size () const { return Args.size(); }
1317 inline bool arg_empty() const { return Args.empty(); }
1319 inline const_name_iterator name_begin() const { return ArgNames.begin(); }
1320 inline const_name_iterator name_end () const { return ArgNames.end(); }
1322 inline size_t name_size () const { return ArgNames.size(); }
1323 inline bool name_empty() const { return ArgNames.empty(); }
1325 Init *getBit(unsigned Bit) const override {
1326 llvm_unreachable("Illegal bit reference off dag");
1329 Init *resolveListElementReference(Record &R, const RecordVal *RV,
1330 unsigned Elt) const override {
1331 llvm_unreachable("Illegal element reference off dag");
1335 //===----------------------------------------------------------------------===//
1336 // High-Level Classes
1337 //===----------------------------------------------------------------------===//
1345 RecordVal(Init *N, RecTy *T, unsigned P);
1346 RecordVal(const std::string &N, RecTy *T, unsigned P);
1348 const std::string &getName() const;
1349 const Init *getNameInit() const { return Name; }
1350 std::string getNameInitAsString() const {
1351 return getNameInit()->getAsUnquotedString();
1354 unsigned getPrefix() const { return Prefix; }
1355 RecTy *getType() const { return Ty; }
1356 Init *getValue() const { return Value; }
1358 bool setValue(Init *V) {
1360 Value = V->convertInitializerTo(Ty);
1361 return Value == nullptr;
1368 void print(raw_ostream &OS, bool PrintSem = true) const;
1371 inline raw_ostream &operator<<(raw_ostream &OS, const RecordVal &RV) {
1372 RV.print(OS << " ");
1377 static unsigned LastID;
1379 // Unique record ID.
1382 // Location where record was instantiated, followed by the location of
1383 // multiclass prototypes used.
1384 SmallVector<SMLoc, 4> Locs;
1385 std::vector<Init *> TemplateArgs;
1386 std::vector<RecordVal> Values;
1387 std::vector<Record *> SuperClasses;
1388 std::vector<SMRange> SuperClassRanges;
1390 // Tracks Record instances. Not owned by Record.
1391 RecordKeeper &TrackedRecords;
1401 // Constructs a record.
1402 explicit Record(const std::string &N, ArrayRef<SMLoc> locs,
1403 RecordKeeper &records, bool Anonymous = false) :
1404 ID(LastID++), Name(StringInit::get(N)), Locs(locs.begin(), locs.end()),
1405 TrackedRecords(records), TheInit(nullptr), IsAnonymous(Anonymous) {
1408 explicit Record(Init *N, ArrayRef<SMLoc> locs, RecordKeeper &records,
1409 bool Anonymous = false) :
1410 ID(LastID++), Name(N), Locs(locs.begin(), locs.end()),
1411 TrackedRecords(records), TheInit(nullptr), IsAnonymous(Anonymous) {
1415 // When copy-constructing a Record, we must still guarantee a globally unique
1416 // ID number. All other fields can be copied normally.
1417 Record(const Record &O) :
1418 ID(LastID++), Name(O.Name), Locs(O.Locs), TemplateArgs(O.TemplateArgs),
1419 Values(O.Values), SuperClasses(O.SuperClasses),
1420 SuperClassRanges(O.SuperClassRanges), TrackedRecords(O.TrackedRecords),
1421 TheInit(O.TheInit), IsAnonymous(O.IsAnonymous) { }
1426 static unsigned getNewUID() { return LastID++; }
1429 unsigned getID() const { return ID; }
1431 const std::string &getName() const;
1432 Init *getNameInit() const {
1435 const std::string getNameInitAsString() const {
1436 return getNameInit()->getAsUnquotedString();
1439 void setName(Init *Name); // Also updates RecordKeeper.
1440 void setName(const std::string &Name); // Also updates RecordKeeper.
1442 ArrayRef<SMLoc> getLoc() const { return Locs; }
1444 /// get the corresponding DefInit.
1445 DefInit *getDefInit();
1447 const std::vector<Init *> &getTemplateArgs() const {
1448 return TemplateArgs;
1450 const std::vector<RecordVal> &getValues() const { return Values; }
1451 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; }
1452 ArrayRef<SMRange> getSuperClassRanges() const { return SuperClassRanges; }
1454 bool isTemplateArg(Init *Name) const {
1455 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
1456 if (TemplateArgs[i] == Name) return true;
1459 bool isTemplateArg(StringRef Name) const {
1460 return isTemplateArg(StringInit::get(Name.str()));
1463 const RecordVal *getValue(const Init *Name) const {
1464 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1465 if (Values[i].getNameInit() == Name) return &Values[i];
1468 const RecordVal *getValue(StringRef Name) const {
1469 return getValue(StringInit::get(Name));
1471 RecordVal *getValue(const Init *Name) {
1472 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1473 if (Values[i].getNameInit() == Name) return &Values[i];
1476 RecordVal *getValue(StringRef Name) {
1477 return getValue(StringInit::get(Name));
1480 void addTemplateArg(Init *Name) {
1481 assert(!isTemplateArg(Name) && "Template arg already defined!");
1482 TemplateArgs.push_back(Name);
1484 void addTemplateArg(StringRef Name) {
1485 addTemplateArg(StringInit::get(Name.str()));
1488 void addValue(const RecordVal &RV) {
1489 assert(getValue(RV.getNameInit()) == 0 && "Value already added!");
1490 Values.push_back(RV);
1491 if (Values.size() > 1)
1492 // Keep NAME at the end of the list. It makes record dumps a
1493 // bit prettier and allows TableGen tests to be written more
1494 // naturally. Tests can use CHECK-NEXT to look for Record
1495 // fields they expect to see after a def. They can't do that if
1496 // NAME is the first Record field.
1497 std::swap(Values[Values.size() - 2], Values[Values.size() - 1]);
1500 void removeValue(Init *Name) {
1501 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1502 if (Values[i].getNameInit() == Name) {
1503 Values.erase(Values.begin()+i);
1506 llvm_unreachable("Cannot remove an entry that does not exist!");
1509 void removeValue(StringRef Name) {
1510 removeValue(StringInit::get(Name.str()));
1513 bool isSubClassOf(const Record *R) const {
1514 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1515 if (SuperClasses[i] == R)
1520 bool isSubClassOf(StringRef Name) const {
1521 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1522 if (SuperClasses[i]->getNameInitAsString() == Name)
1527 void addSuperClass(Record *R, SMRange Range) {
1528 assert(!isSubClassOf(R) && "Already subclassing record!");
1529 SuperClasses.push_back(R);
1530 SuperClassRanges.push_back(Range);
1533 /// resolveReferences - If there are any field references that refer to fields
1534 /// that have been filled in, we can propagate the values now.
1536 void resolveReferences() { resolveReferencesTo(nullptr); }
1538 /// resolveReferencesTo - If anything in this record refers to RV, replace the
1539 /// reference to RV with the RHS of RV. If RV is null, we resolve all
1540 /// possible references.
1541 void resolveReferencesTo(const RecordVal *RV);
1543 RecordKeeper &getRecords() const {
1544 return TrackedRecords;
1547 bool isAnonymous() const {
1553 //===--------------------------------------------------------------------===//
1554 // High-level methods useful to tablegen back-ends
1557 /// getValueInit - Return the initializer for a value with the specified name,
1558 /// or throw an exception if the field does not exist.
1560 Init *getValueInit(StringRef FieldName) const;
1562 /// Return true if the named field is unset.
1563 bool isValueUnset(StringRef FieldName) const {
1564 return getValueInit(FieldName) == UnsetInit::get();
1567 /// getValueAsString - This method looks up the specified field and returns
1568 /// its value as a string, throwing an exception if the field does not exist
1569 /// or if the value is not a string.
1571 std::string getValueAsString(StringRef FieldName) const;
1573 /// getValueAsBitsInit - This method looks up the specified field and returns
1574 /// its value as a BitsInit, throwing an exception if the field does not exist
1575 /// or if the value is not the right type.
1577 BitsInit *getValueAsBitsInit(StringRef FieldName) const;
1579 /// getValueAsListInit - This method looks up the specified field and returns
1580 /// its value as a ListInit, throwing an exception if the field does not exist
1581 /// or if the value is not the right type.
1583 ListInit *getValueAsListInit(StringRef FieldName) const;
1585 /// getValueAsListOfDefs - This method looks up the specified field and
1586 /// returns its value as a vector of records, throwing an exception if the
1587 /// field does not exist or if the value is not the right type.
1589 std::vector<Record*> getValueAsListOfDefs(StringRef FieldName) const;
1591 /// getValueAsListOfInts - This method looks up the specified field and
1592 /// returns its value as a vector of integers, throwing an exception if the
1593 /// field does not exist or if the value is not the right type.
1595 std::vector<int64_t> getValueAsListOfInts(StringRef FieldName) const;
1597 /// getValueAsListOfStrings - This method looks up the specified field and
1598 /// returns its value as a vector of strings, throwing an exception if the
1599 /// field does not exist or if the value is not the right type.
1601 std::vector<std::string> getValueAsListOfStrings(StringRef FieldName) const;
1603 /// getValueAsDef - This method looks up the specified field and returns its
1604 /// value as a Record, throwing an exception if the field does not exist or if
1605 /// the value is not the right type.
1607 Record *getValueAsDef(StringRef FieldName) const;
1609 /// getValueAsBit - This method looks up the specified field and returns its
1610 /// value as a bit, throwing an exception if the field does not exist or if
1611 /// the value is not the right type.
1613 bool getValueAsBit(StringRef FieldName) const;
1615 /// getValueAsBitOrUnset - This method looks up the specified field and
1616 /// returns its value as a bit. If the field is unset, sets Unset to true and
1619 bool getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const;
1621 /// getValueAsInt - This method looks up the specified field and returns its
1622 /// value as an int64_t, throwing an exception if the field does not exist or
1623 /// if the value is not the right type.
1625 int64_t getValueAsInt(StringRef FieldName) const;
1627 /// getValueAsDag - This method looks up the specified field and returns its
1628 /// value as an Dag, throwing an exception if the field does not exist or if
1629 /// the value is not the right type.
1631 DagInit *getValueAsDag(StringRef FieldName) const;
1634 raw_ostream &operator<<(raw_ostream &OS, const Record &R);
1637 Record Rec; // Placeholder for template args and Name.
1638 typedef std::vector<Record*> RecordVector;
1639 RecordVector DefPrototypes;
1643 MultiClass(const std::string &Name, SMLoc Loc, RecordKeeper &Records) :
1644 Rec(Name, Loc, Records) {}
1647 class RecordKeeper {
1648 std::map<std::string, Record*> Classes, Defs;
1652 for (std::map<std::string, Record*>::iterator I = Classes.begin(),
1653 E = Classes.end(); I != E; ++I)
1655 for (std::map<std::string, Record*>::iterator I = Defs.begin(),
1656 E = Defs.end(); I != E; ++I)
1660 const std::map<std::string, Record*> &getClasses() const { return Classes; }
1661 const std::map<std::string, Record*> &getDefs() const { return Defs; }
1663 Record *getClass(const std::string &Name) const {
1664 std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
1665 return I == Classes.end() ? nullptr : I->second;
1667 Record *getDef(const std::string &Name) const {
1668 std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
1669 return I == Defs.end() ? nullptr : I->second;
1671 void addClass(Record *R) {
1672 bool Ins = Classes.insert(std::make_pair(R->getName(), R)).second;
1674 assert(Ins && "Class already exists");
1676 void addDef(Record *R) {
1677 bool Ins = Defs.insert(std::make_pair(R->getName(), R)).second;
1679 assert(Ins && "Record already exists");
1682 /// removeClass - Remove, but do not delete, the specified record.
1684 void removeClass(const std::string &Name) {
1685 assert(Classes.count(Name) && "Class does not exist!");
1686 Classes.erase(Name);
1688 /// removeDef - Remove, but do not delete, the specified record.
1690 void removeDef(const std::string &Name) {
1691 assert(Defs.count(Name) && "Def does not exist!");
1695 //===--------------------------------------------------------------------===//
1696 // High-level helper methods, useful for tablegen backends...
1698 /// getAllDerivedDefinitions - This method returns all concrete definitions
1699 /// that derive from the specified class name. If a class with the specified
1700 /// name does not exist, an exception is thrown.
1701 std::vector<Record*>
1702 getAllDerivedDefinitions(const std::string &ClassName) const;
1707 /// LessRecord - Sorting predicate to sort record pointers by name.
1710 bool operator()(const Record *Rec1, const Record *Rec2) const {
1711 return StringRef(Rec1->getName()).compare_numeric(Rec2->getName()) < 0;
1715 /// LessRecordByID - Sorting predicate to sort record pointers by their
1716 /// unique ID. If you just need a deterministic order, use this, since it
1717 /// just compares two `unsigned`; the other sorting predicates require
1718 /// string manipulation.
1719 struct LessRecordByID {
1720 bool operator()(const Record *LHS, const Record *RHS) const {
1721 return LHS->getID() < RHS->getID();
1725 /// LessRecordFieldName - Sorting predicate to sort record pointers by their
1728 struct LessRecordFieldName {
1729 bool operator()(const Record *Rec1, const Record *Rec2) const {
1730 return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name");
1734 struct LessRecordRegister {
1735 static size_t min(size_t a, size_t b) { return a < b ? a : b; }
1736 static bool ascii_isdigit(char x) { return x >= '0' && x <= '9'; }
1738 struct RecordParts {
1739 SmallVector<std::pair< bool, StringRef>, 4> Parts;
1741 RecordParts(StringRef Rec) {
1746 const char *Start = Rec.data();
1747 const char *Curr = Start;
1748 bool isDigitPart = ascii_isdigit(Curr[0]);
1749 for (size_t I = 0, E = Rec.size(); I != E; ++I, ++Len) {
1750 bool isDigit = ascii_isdigit(Curr[I]);
1751 if (isDigit != isDigitPart) {
1752 Parts.push_back(std::make_pair(isDigitPart, StringRef(Start, Len)));
1755 isDigitPart = ascii_isdigit(Curr[I]);
1758 // Push the last part.
1759 Parts.push_back(std::make_pair(isDigitPart, StringRef(Start, Len)));
1762 size_t size() { return Parts.size(); }
1764 std::pair<bool, StringRef> getPart(size_t i) {
1765 assert (i < Parts.size() && "Invalid idx!");
1770 bool operator()(const Record *Rec1, const Record *Rec2) const {
1771 RecordParts LHSParts(StringRef(Rec1->getName()));
1772 RecordParts RHSParts(StringRef(Rec2->getName()));
1774 size_t LHSNumParts = LHSParts.size();
1775 size_t RHSNumParts = RHSParts.size();
1776 assert (LHSNumParts && RHSNumParts && "Expected at least one part!");
1778 if (LHSNumParts != RHSNumParts)
1779 return LHSNumParts < RHSNumParts;
1781 // We expect the registers to be of the form [_a-zA-z]+([0-9]*[_a-zA-Z]*)*.
1782 for (size_t I = 0, E = LHSNumParts; I < E; I+=2) {
1783 std::pair<bool, StringRef> LHSPart = LHSParts.getPart(I);
1784 std::pair<bool, StringRef> RHSPart = RHSParts.getPart(I);
1785 // Expect even part to always be alpha.
1786 assert (LHSPart.first == false && RHSPart.first == false &&
1787 "Expected both parts to be alpha.");
1788 if (int Res = LHSPart.second.compare(RHSPart.second))
1791 for (size_t I = 1, 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 odd part to always be numeric.
1795 assert (LHSPart.first == true && RHSPart.first == true &&
1796 "Expected both parts to be numeric.");
1797 if (LHSPart.second.size() != RHSPart.second.size())
1798 return LHSPart.second.size() < RHSPart.second.size();
1800 unsigned LHSVal, RHSVal;
1802 bool LHSFailed = LHSPart.second.getAsInteger(10, LHSVal); (void)LHSFailed;
1803 assert(!LHSFailed && "Unable to convert LHS to integer.");
1804 bool RHSFailed = RHSPart.second.getAsInteger(10, RHSVal); (void)RHSFailed;
1805 assert(!RHSFailed && "Unable to convert RHS to integer.");
1807 if (LHSVal != RHSVal)
1808 return LHSVal < RHSVal;
1810 return LHSNumParts < RHSNumParts;
1814 raw_ostream &operator<<(raw_ostream &OS, const RecordKeeper &RK);
1816 /// QualifyName - Return an Init with a qualifier prefix referring
1817 /// to CurRec's name.
1818 Init *QualifyName(Record &CurRec, MultiClass *CurMultiClass,
1819 Init *Name, const std::string &Scoper);
1821 /// QualifyName - Return an Init with a qualifier prefix referring
1822 /// to CurRec's name.
1823 Init *QualifyName(Record &CurRec, MultiClass *CurMultiClass,
1824 const std::string &Name, const std::string &Scoper);
1826 } // End llvm namespace