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(0) {}
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 0; }
106 virtual Init *convertValue( BitInit *BI) { return 0; }
107 virtual Init *convertValue( BitsInit *BI) { return 0; }
108 virtual Init *convertValue( IntInit *II) { return 0; }
109 virtual Init *convertValue(StringInit *SI) { return 0; }
110 virtual Init *convertValue( ListInit *LI) { return 0; }
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 0; }
121 virtual Init *convertValue( DefInit *DI) { return 0; }
122 virtual Init *convertValue( DagInit *DI) { return 0; }
123 virtual Init *convertValue( TypedInit *TI) { return 0; }
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 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
154 virtual Init *convertValue( BitInit *BI) { return (Init*)BI; }
155 virtual Init *convertValue( BitsInit *BI);
156 virtual Init *convertValue( IntInit *II);
157 virtual Init *convertValue(StringInit *SI) { return 0; }
158 virtual Init *convertValue( ListInit *LI) { return 0; }
159 virtual Init *convertValue(VarBitInit *VB) { return (Init*)VB; }
160 virtual Init *convertValue( DefInit *DI) { return 0; }
161 virtual Init *convertValue( DagInit *DI) { return 0; }
162 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
163 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
164 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
165 virtual Init *convertValue( TypedInit *TI);
166 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
167 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
169 virtual std::string getAsString() const { return "bit"; }
171 virtual bool typeIsConvertibleTo(const RecTy *RHS) const {
172 return RHS->baseClassOf(this);
174 virtual bool baseClassOf(const RecTy*) const;
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 virtual Init *convertValue( UnsetInit *UI);
193 virtual Init *convertValue( BitInit *UI);
194 virtual Init *convertValue( BitsInit *BI);
195 virtual Init *convertValue( IntInit *II);
196 virtual Init *convertValue(StringInit *SI) { return 0; }
197 virtual Init *convertValue( ListInit *LI) { return 0; }
198 virtual Init *convertValue(VarBitInit *VB) { return 0; }
199 virtual Init *convertValue( DefInit *DI) { return 0; }
200 virtual Init *convertValue( DagInit *DI) { return 0; }
201 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
202 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
203 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
204 virtual Init *convertValue( TypedInit *TI);
205 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
206 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
208 virtual std::string getAsString() const;
210 virtual bool typeIsConvertibleTo(const RecTy *RHS) const {
211 return RHS->baseClassOf(this);
213 virtual bool baseClassOf(const RecTy*) const;
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 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
230 virtual Init *convertValue( BitInit *BI);
231 virtual Init *convertValue( BitsInit *BI);
232 virtual Init *convertValue( IntInit *II) { return (Init*)II; }
233 virtual Init *convertValue(StringInit *SI) { return 0; }
234 virtual Init *convertValue( ListInit *LI) { return 0; }
235 virtual Init *convertValue(VarBitInit *VB) { return 0; }
236 virtual Init *convertValue( DefInit *DI) { return 0; }
237 virtual Init *convertValue( DagInit *DI) { return 0; }
238 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
239 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
240 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
241 virtual Init *convertValue( TypedInit *TI);
242 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
243 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
245 virtual std::string getAsString() const { return "int"; }
247 virtual bool typeIsConvertibleTo(const RecTy *RHS) const {
248 return RHS->baseClassOf(this);
251 virtual bool baseClassOf(const RecTy*) const;
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 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
267 virtual Init *convertValue( BitInit *BI) { return 0; }
268 virtual Init *convertValue( BitsInit *BI) { return 0; }
269 virtual Init *convertValue( IntInit *II) { return 0; }
270 virtual Init *convertValue(StringInit *SI) { return (Init*)SI; }
271 virtual Init *convertValue( ListInit *LI) { return 0; }
272 virtual Init *convertValue( UnOpInit *BO);
273 virtual Init *convertValue( BinOpInit *BO);
274 virtual Init *convertValue( TernOpInit *BO) { return RecTy::convertValue(BO);}
276 virtual Init *convertValue(VarBitInit *VB) { return 0; }
277 virtual Init *convertValue( DefInit *DI) { return 0; }
278 virtual Init *convertValue( DagInit *DI) { return 0; }
279 virtual Init *convertValue( TypedInit *TI);
280 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
281 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
283 virtual std::string getAsString() const { return "string"; }
285 virtual bool typeIsConvertibleTo(const RecTy *RHS) const {
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 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
306 virtual Init *convertValue( BitInit *BI) { return 0; }
307 virtual Init *convertValue( BitsInit *BI) { return 0; }
308 virtual Init *convertValue( IntInit *II) { return 0; }
309 virtual Init *convertValue(StringInit *SI) { return 0; }
310 virtual Init *convertValue( ListInit *LI);
311 virtual Init *convertValue(VarBitInit *VB) { return 0; }
312 virtual Init *convertValue( DefInit *DI) { return 0; }
313 virtual Init *convertValue( DagInit *DI) { return 0; }
314 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
315 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
316 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
317 virtual Init *convertValue( TypedInit *TI);
318 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
319 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
321 virtual std::string getAsString() const;
323 virtual bool typeIsConvertibleTo(const RecTy *RHS) const {
324 return RHS->baseClassOf(this);
327 virtual bool baseClassOf(const RecTy*) const;
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 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
343 virtual Init *convertValue( BitInit *BI) { return 0; }
344 virtual Init *convertValue( BitsInit *BI) { return 0; }
345 virtual Init *convertValue( IntInit *II) { return 0; }
346 virtual Init *convertValue(StringInit *SI) { return 0; }
347 virtual Init *convertValue( ListInit *LI) { return 0; }
348 virtual Init *convertValue(VarBitInit *VB) { return 0; }
349 virtual Init *convertValue( DefInit *DI) { return 0; }
350 virtual Init *convertValue( UnOpInit *BO);
351 virtual Init *convertValue( BinOpInit *BO);
352 virtual Init *convertValue( TernOpInit *BO) { return RecTy::convertValue(BO);}
353 virtual Init *convertValue( DagInit *CI) { return (Init*)CI; }
354 virtual Init *convertValue( TypedInit *TI);
355 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
356 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
358 virtual std::string getAsString() const { return "dag"; }
360 virtual bool typeIsConvertibleTo(const RecTy *RHS) const {
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 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
383 virtual Init *convertValue( BitInit *BI) { return 0; }
384 virtual Init *convertValue( BitsInit *BI) { return 0; }
385 virtual Init *convertValue( IntInit *II) { return 0; }
386 virtual Init *convertValue(StringInit *SI) { return 0; }
387 virtual Init *convertValue( ListInit *LI) { return 0; }
388 virtual Init *convertValue(VarBitInit *VB) { return 0; }
389 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
390 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
391 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
392 virtual Init *convertValue( DefInit *DI);
393 virtual Init *convertValue( DagInit *DI) { return 0; }
394 virtual Init *convertValue( TypedInit *VI);
395 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
396 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
398 virtual std::string getAsString() const;
400 virtual bool typeIsConvertibleTo(const RecTy *RHS) const {
401 return RHS->baseClassOf(this);
403 virtual bool baseClassOf(const RecTy*) const;
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 { return 0; }
518 /// getFieldInit - This method complements getFieldType to return the
519 /// initializer for the specified field. If getFieldType returns non-null
520 /// this method should return non-null, otherwise it returns null.
522 virtual Init *getFieldInit(Record &R, const RecordVal *RV,
523 const std::string &FieldName) const {
527 /// resolveReferences - This method is used by classes that refer to other
528 /// variables which may not be defined at the time the expression is formed.
529 /// If a value is set for the variable later, this method will be called on
530 /// users of the value to allow the value to propagate out.
532 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const {
533 return const_cast<Init *>(this);
536 /// getBit - This method is used to return the initializer for the specified
538 virtual Init *getBit(unsigned Bit) const = 0;
540 /// getBitVar - This method is used to retrieve the initializer for bit
541 /// reference. For non-VarBitInit, it simply returns itself.
542 virtual Init *getBitVar() const { return const_cast<Init*>(this); }
544 /// getBitNum - This method is used to retrieve the bit number of a bit
545 /// reference. For non-VarBitInit, it simply returns 0.
546 virtual unsigned getBitNum() const { return 0; }
549 inline raw_ostream &operator<<(raw_ostream &OS, const Init &I) {
550 I.print(OS); return OS;
553 /// TypedInit - This is the common super-class of types that have a specific,
556 class TypedInit : public Init {
559 TypedInit(const TypedInit &Other) LLVM_DELETED_FUNCTION;
560 TypedInit &operator=(const TypedInit &Other) LLVM_DELETED_FUNCTION;
563 explicit TypedInit(InitKind K, RecTy *T) : Init(K), Ty(T) {}
566 static bool classof(const Init *I) {
567 return I->getKind() >= IK_FirstTypedInit &&
568 I->getKind() <= IK_LastTypedInit;
570 RecTy *getType() const { return Ty; }
573 convertInitializerBitRange(const std::vector<unsigned> &Bits) const;
575 convertInitListSlice(const std::vector<unsigned> &Elements) const;
577 /// getFieldType - This method is used to implement the FieldInit class.
578 /// Implementors of this method should return the type of the named field if
579 /// they are of record type.
581 virtual RecTy *getFieldType(const std::string &FieldName) const;
583 /// resolveListElementReference - This method is used to implement
584 /// VarListElementInit::resolveReferences. If the list element is resolvable
585 /// now, we return the resolved value, otherwise we return null.
586 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
587 unsigned Elt) const = 0;
591 /// UnsetInit - ? - Represents an uninitialized value
593 class UnsetInit : public Init {
594 UnsetInit() : Init(IK_UnsetInit) {}
595 UnsetInit(const UnsetInit &) LLVM_DELETED_FUNCTION;
596 UnsetInit &operator=(const UnsetInit &Other) LLVM_DELETED_FUNCTION;
597 virtual void anchor();
600 static bool classof(const Init *I) {
601 return I->getKind() == IK_UnsetInit;
603 static UnsetInit *get();
605 virtual Init *convertInitializerTo(RecTy *Ty) const {
606 return Ty->convertValue(const_cast<UnsetInit *>(this));
609 virtual Init *getBit(unsigned Bit) const {
610 return const_cast<UnsetInit*>(this);
613 virtual bool isComplete() const { return false; }
614 virtual std::string getAsString() const { return "?"; }
618 /// BitInit - true/false - Represent a concrete initializer for a bit.
620 class BitInit : public Init {
623 explicit BitInit(bool V) : Init(IK_BitInit), Value(V) {}
624 BitInit(const BitInit &Other) LLVM_DELETED_FUNCTION;
625 BitInit &operator=(BitInit &Other) LLVM_DELETED_FUNCTION;
626 virtual void anchor();
629 static bool classof(const Init *I) {
630 return I->getKind() == IK_BitInit;
632 static BitInit *get(bool V);
634 bool getValue() const { return Value; }
636 virtual Init *convertInitializerTo(RecTy *Ty) const {
637 return Ty->convertValue(const_cast<BitInit *>(this));
640 virtual Init *getBit(unsigned Bit) const {
641 assert(Bit < 1 && "Bit index out of range!");
642 return const_cast<BitInit*>(this);
645 virtual std::string getAsString() const { return Value ? "1" : "0"; }
648 /// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
649 /// It contains a vector of bits, whose size is determined by the type.
651 class BitsInit : public Init, public FoldingSetNode {
652 std::vector<Init*> Bits;
654 BitsInit(ArrayRef<Init *> Range)
655 : Init(IK_BitsInit), Bits(Range.begin(), Range.end()) {}
657 BitsInit(const BitsInit &Other) LLVM_DELETED_FUNCTION;
658 BitsInit &operator=(const BitsInit &Other) LLVM_DELETED_FUNCTION;
661 static bool classof(const Init *I) {
662 return I->getKind() == IK_BitsInit;
664 static BitsInit *get(ArrayRef<Init *> Range);
666 void Profile(FoldingSetNodeID &ID) const;
668 unsigned getNumBits() const { return Bits.size(); }
670 virtual Init *convertInitializerTo(RecTy *Ty) const {
671 return Ty->convertValue(const_cast<BitsInit *>(this));
674 convertInitializerBitRange(const std::vector<unsigned> &Bits) const;
676 virtual bool isComplete() const {
677 for (unsigned i = 0; i != getNumBits(); ++i)
678 if (!getBit(i)->isComplete()) return false;
681 bool allInComplete() const {
682 for (unsigned i = 0; i != getNumBits(); ++i)
683 if (getBit(i)->isComplete()) return false;
686 virtual std::string getAsString() const;
688 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
690 virtual Init *getBit(unsigned Bit) const {
691 assert(Bit < Bits.size() && "Bit index out of range!");
697 /// IntInit - 7 - Represent an initialization by a literal integer value.
699 class IntInit : public TypedInit {
702 explicit IntInit(int64_t V)
703 : TypedInit(IK_IntInit, IntRecTy::get()), Value(V) {}
705 IntInit(const IntInit &Other) LLVM_DELETED_FUNCTION;
706 IntInit &operator=(const IntInit &Other) LLVM_DELETED_FUNCTION;
709 static bool classof(const Init *I) {
710 return I->getKind() == IK_IntInit;
712 static IntInit *get(int64_t V);
714 int64_t getValue() const { return Value; }
716 virtual Init *convertInitializerTo(RecTy *Ty) const {
717 return Ty->convertValue(const_cast<IntInit *>(this));
720 convertInitializerBitRange(const std::vector<unsigned> &Bits) const;
722 virtual std::string getAsString() const;
724 /// resolveListElementReference - This method is used to implement
725 /// VarListElementInit::resolveReferences. If the list element is resolvable
726 /// now, we return the resolved value, otherwise we return null.
727 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
728 unsigned Elt) const {
729 llvm_unreachable("Illegal element reference off int");
732 virtual Init *getBit(unsigned Bit) const {
733 return BitInit::get((Value & (1ULL << Bit)) != 0);
738 /// StringInit - "foo" - Represent an initialization by a string value.
740 class StringInit : public TypedInit {
743 explicit StringInit(const std::string &V)
744 : TypedInit(IK_StringInit, StringRecTy::get()), Value(V) {}
746 StringInit(const StringInit &Other) LLVM_DELETED_FUNCTION;
747 StringInit &operator=(const StringInit &Other) LLVM_DELETED_FUNCTION;
748 virtual void anchor();
751 static bool classof(const Init *I) {
752 return I->getKind() == IK_StringInit;
754 static StringInit *get(StringRef);
756 const std::string &getValue() const { return Value; }
758 virtual Init *convertInitializerTo(RecTy *Ty) const {
759 return Ty->convertValue(const_cast<StringInit *>(this));
762 virtual std::string getAsString() const { return "\"" + Value + "\""; }
763 virtual std::string getAsUnquotedString() const { return Value; }
765 /// resolveListElementReference - This method is used to implement
766 /// VarListElementInit::resolveReferences. If the list element is resolvable
767 /// now, we return the resolved value, otherwise we return null.
768 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
769 unsigned Elt) const {
770 llvm_unreachable("Illegal element reference off string");
773 virtual Init *getBit(unsigned Bit) const {
774 llvm_unreachable("Illegal bit reference off string");
778 /// ListInit - [AL, AH, CL] - Represent a list of defs
780 class ListInit : public TypedInit, public FoldingSetNode {
781 std::vector<Init*> Values;
783 typedef std::vector<Init*>::const_iterator const_iterator;
786 explicit ListInit(ArrayRef<Init *> Range, RecTy *EltTy)
787 : TypedInit(IK_ListInit, ListRecTy::get(EltTy)),
788 Values(Range.begin(), Range.end()) {}
790 ListInit(const ListInit &Other) LLVM_DELETED_FUNCTION;
791 ListInit &operator=(const ListInit &Other) LLVM_DELETED_FUNCTION;
794 static bool classof(const Init *I) {
795 return I->getKind() == IK_ListInit;
797 static ListInit *get(ArrayRef<Init *> Range, RecTy *EltTy);
799 void Profile(FoldingSetNodeID &ID) const;
801 unsigned getSize() const { return Values.size(); }
802 Init *getElement(unsigned i) const {
803 assert(i < Values.size() && "List element index out of range!");
807 Record *getElementAsRecord(unsigned i) const;
810 convertInitListSlice(const std::vector<unsigned> &Elements) const;
812 virtual Init *convertInitializerTo(RecTy *Ty) const {
813 return Ty->convertValue(const_cast<ListInit *>(this));
816 /// resolveReferences - This method is used by classes that refer to other
817 /// variables which may not be defined at the time they expression is formed.
818 /// If a value is set for the variable later, this method will be called on
819 /// users of the value to allow the value to propagate out.
821 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
823 virtual std::string getAsString() const;
825 ArrayRef<Init*> getValues() const { return Values; }
827 inline const_iterator begin() const { return Values.begin(); }
828 inline const_iterator end () const { return Values.end(); }
830 inline size_t size () const { return Values.size(); }
831 inline bool empty() const { return Values.empty(); }
833 /// resolveListElementReference - This method is used to implement
834 /// VarListElementInit::resolveReferences. If the list element is resolvable
835 /// now, we return the resolved value, otherwise we return null.
836 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
839 virtual Init *getBit(unsigned Bit) const {
840 llvm_unreachable("Illegal bit reference off list");
845 /// OpInit - Base class for operators
847 class OpInit : public TypedInit {
848 OpInit(const OpInit &Other) LLVM_DELETED_FUNCTION;
849 OpInit &operator=(OpInit &Other) LLVM_DELETED_FUNCTION;
852 explicit OpInit(InitKind K, RecTy *Type) : TypedInit(K, Type) {}
855 static bool classof(const Init *I) {
856 return I->getKind() >= IK_FirstOpInit &&
857 I->getKind() <= IK_LastOpInit;
859 // Clone - Clone this operator, replacing arguments with the new list
860 virtual OpInit *clone(std::vector<Init *> &Operands) const = 0;
862 virtual int getNumOperands() const = 0;
863 virtual Init *getOperand(int i) const = 0;
865 // Fold - If possible, fold this to a simpler init. Return this if not
867 virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const = 0;
869 virtual Init *convertInitializerTo(RecTy *Ty) const {
870 return Ty->convertValue(const_cast<OpInit *>(this));
873 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
876 virtual Init *getBit(unsigned Bit) const;
880 /// UnOpInit - !op (X) - Transform an init.
882 class UnOpInit : public OpInit {
884 enum UnaryOp { CAST, HEAD, TAIL, EMPTY };
889 UnOpInit(UnaryOp opc, Init *lhs, RecTy *Type)
890 : OpInit(IK_UnOpInit, Type), Opc(opc), LHS(lhs) {}
892 UnOpInit(const UnOpInit &Other) LLVM_DELETED_FUNCTION;
893 UnOpInit &operator=(const UnOpInit &Other) LLVM_DELETED_FUNCTION;
896 static bool classof(const Init *I) {
897 return I->getKind() == IK_UnOpInit;
899 static UnOpInit *get(UnaryOp opc, Init *lhs, RecTy *Type);
901 // Clone - Clone this operator, replacing arguments with the new list
902 virtual OpInit *clone(std::vector<Init *> &Operands) const {
903 assert(Operands.size() == 1 &&
904 "Wrong number of operands for unary operation");
905 return UnOpInit::get(getOpcode(), *Operands.begin(), getType());
908 virtual int getNumOperands() const { return 1; }
909 virtual Init *getOperand(int i) const {
910 assert(i == 0 && "Invalid operand id for unary operator");
914 UnaryOp getOpcode() const { return Opc; }
915 Init *getOperand() const { return LHS; }
917 // Fold - If possible, fold this to a simpler init. Return this if not
919 virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const;
921 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
923 virtual std::string getAsString() const;
926 /// BinOpInit - !op (X, Y) - Combine two inits.
928 class BinOpInit : public OpInit {
930 enum BinaryOp { ADD, SHL, SRA, SRL, STRCONCAT, CONCAT, EQ };
935 BinOpInit(BinaryOp opc, Init *lhs, Init *rhs, RecTy *Type) :
936 OpInit(IK_BinOpInit, Type), Opc(opc), LHS(lhs), RHS(rhs) {}
938 BinOpInit(const BinOpInit &Other) LLVM_DELETED_FUNCTION;
939 BinOpInit &operator=(const BinOpInit &Other) LLVM_DELETED_FUNCTION;
942 static bool classof(const Init *I) {
943 return I->getKind() == IK_BinOpInit;
945 static BinOpInit *get(BinaryOp opc, Init *lhs, Init *rhs,
948 // Clone - Clone this operator, replacing arguments with the new list
949 virtual OpInit *clone(std::vector<Init *> &Operands) const {
950 assert(Operands.size() == 2 &&
951 "Wrong number of operands for binary operation");
952 return BinOpInit::get(getOpcode(), Operands[0], Operands[1], getType());
955 virtual int getNumOperands() const { return 2; }
956 virtual Init *getOperand(int i) const {
957 assert((i == 0 || i == 1) && "Invalid operand id for binary operator");
965 BinaryOp getOpcode() const { return Opc; }
966 Init *getLHS() const { return LHS; }
967 Init *getRHS() const { return RHS; }
969 // Fold - If possible, fold this to a simpler init. Return this if not
971 virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const;
973 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
975 virtual std::string getAsString() const;
978 /// TernOpInit - !op (X, Y, Z) - Combine two inits.
980 class TernOpInit : public OpInit {
982 enum TernaryOp { SUBST, FOREACH, IF };
985 Init *LHS, *MHS, *RHS;
987 TernOpInit(TernaryOp opc, Init *lhs, Init *mhs, Init *rhs,
989 OpInit(IK_TernOpInit, Type), Opc(opc), LHS(lhs), MHS(mhs), RHS(rhs) {}
991 TernOpInit(const TernOpInit &Other) LLVM_DELETED_FUNCTION;
992 TernOpInit &operator=(const TernOpInit &Other) LLVM_DELETED_FUNCTION;
995 static bool classof(const Init *I) {
996 return I->getKind() == IK_TernOpInit;
998 static TernOpInit *get(TernaryOp opc, Init *lhs,
999 Init *mhs, Init *rhs,
1002 // Clone - Clone this operator, replacing arguments with the new list
1003 virtual OpInit *clone(std::vector<Init *> &Operands) const {
1004 assert(Operands.size() == 3 &&
1005 "Wrong number of operands for ternary operation");
1006 return TernOpInit::get(getOpcode(), Operands[0], Operands[1], Operands[2],
1010 virtual int getNumOperands() const { return 3; }
1011 virtual Init *getOperand(int i) const {
1012 assert((i == 0 || i == 1 || i == 2) &&
1013 "Invalid operand id for ternary operator");
1016 } else if (i == 1) {
1023 TernaryOp getOpcode() const { return Opc; }
1024 Init *getLHS() const { return LHS; }
1025 Init *getMHS() const { return MHS; }
1026 Init *getRHS() const { return RHS; }
1028 // Fold - If possible, fold this to a simpler init. Return this if not
1029 // possible to fold.
1030 virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const;
1032 virtual bool isComplete() const { return false; }
1034 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
1036 virtual std::string getAsString() const;
1040 /// VarInit - 'Opcode' - Represent a reference to an entire variable object.
1042 class VarInit : public TypedInit {
1045 explicit VarInit(const std::string &VN, RecTy *T)
1046 : TypedInit(IK_VarInit, T), VarName(StringInit::get(VN)) {}
1047 explicit VarInit(Init *VN, RecTy *T)
1048 : TypedInit(IK_VarInit, T), VarName(VN) {}
1050 VarInit(const VarInit &Other) LLVM_DELETED_FUNCTION;
1051 VarInit &operator=(const VarInit &Other) LLVM_DELETED_FUNCTION;
1054 static bool classof(const Init *I) {
1055 return I->getKind() == IK_VarInit;
1057 static VarInit *get(const std::string &VN, RecTy *T);
1058 static VarInit *get(Init *VN, RecTy *T);
1060 virtual Init *convertInitializerTo(RecTy *Ty) const {
1061 return Ty->convertValue(const_cast<VarInit *>(this));
1064 const std::string &getName() const;
1065 Init *getNameInit() const { return VarName; }
1066 std::string getNameInitAsString() const {
1067 return getNameInit()->getAsUnquotedString();
1070 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
1071 unsigned Elt) const;
1073 virtual RecTy *getFieldType(const std::string &FieldName) const;
1074 virtual Init *getFieldInit(Record &R, const RecordVal *RV,
1075 const std::string &FieldName) const;
1077 /// resolveReferences - This method is used by classes that refer to other
1078 /// variables which may not be defined at the time they expression is formed.
1079 /// If a value is set for the variable later, this method will be called on
1080 /// users of the value to allow the value to propagate out.
1082 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
1084 virtual Init *getBit(unsigned Bit) const;
1086 virtual std::string getAsString() const { return getName(); }
1090 /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
1092 class VarBitInit : public Init {
1096 VarBitInit(TypedInit *T, unsigned B) : Init(IK_VarBitInit), TI(T), Bit(B) {
1097 assert(T->getType() &&
1098 (isa<IntRecTy>(T->getType()) ||
1099 (isa<BitsRecTy>(T->getType()) &&
1100 cast<BitsRecTy>(T->getType())->getNumBits() > B)) &&
1101 "Illegal VarBitInit expression!");
1104 VarBitInit(const VarBitInit &Other) LLVM_DELETED_FUNCTION;
1105 VarBitInit &operator=(const VarBitInit &Other) LLVM_DELETED_FUNCTION;
1108 static bool classof(const Init *I) {
1109 return I->getKind() == IK_VarBitInit;
1111 static VarBitInit *get(TypedInit *T, unsigned B);
1113 virtual Init *convertInitializerTo(RecTy *Ty) const {
1114 return Ty->convertValue(const_cast<VarBitInit *>(this));
1117 virtual Init *getBitVar() const { return TI; }
1118 virtual unsigned getBitNum() const { return Bit; }
1120 virtual std::string getAsString() const;
1121 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
1123 virtual Init *getBit(unsigned B) const {
1124 assert(B < 1 && "Bit index out of range!");
1125 return const_cast<VarBitInit*>(this);
1129 /// VarListElementInit - List[4] - Represent access to one element of a var or
1131 class VarListElementInit : public TypedInit {
1135 VarListElementInit(TypedInit *T, unsigned E)
1136 : TypedInit(IK_VarListElementInit,
1137 cast<ListRecTy>(T->getType())->getElementType()),
1139 assert(T->getType() && isa<ListRecTy>(T->getType()) &&
1140 "Illegal VarBitInit expression!");
1143 VarListElementInit(const VarListElementInit &Other) LLVM_DELETED_FUNCTION;
1144 void operator=(const VarListElementInit &Other) LLVM_DELETED_FUNCTION;
1147 static bool classof(const Init *I) {
1148 return I->getKind() == IK_VarListElementInit;
1150 static VarListElementInit *get(TypedInit *T, unsigned E);
1152 virtual Init *convertInitializerTo(RecTy *Ty) const {
1153 return Ty->convertValue(const_cast<VarListElementInit *>(this));
1156 TypedInit *getVariable() const { return TI; }
1157 unsigned getElementNum() const { return Element; }
1159 /// resolveListElementReference - This method is used to implement
1160 /// VarListElementInit::resolveReferences. If the list element is resolvable
1161 /// now, we return the resolved value, otherwise we return null.
1162 virtual Init *resolveListElementReference(Record &R,
1163 const RecordVal *RV,
1164 unsigned Elt) const;
1166 virtual std::string getAsString() const;
1167 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
1169 virtual Init *getBit(unsigned Bit) const;
1172 /// DefInit - AL - Represent a reference to a 'def' in the description
1174 class DefInit : public TypedInit {
1177 DefInit(Record *D, RecordRecTy *T) : TypedInit(IK_DefInit, T), Def(D) {}
1178 friend class Record;
1180 DefInit(const DefInit &Other) LLVM_DELETED_FUNCTION;
1181 DefInit &operator=(const DefInit &Other) LLVM_DELETED_FUNCTION;
1184 static bool classof(const Init *I) {
1185 return I->getKind() == IK_DefInit;
1187 static DefInit *get(Record*);
1189 virtual Init *convertInitializerTo(RecTy *Ty) const {
1190 return Ty->convertValue(const_cast<DefInit *>(this));
1193 Record *getDef() const { return Def; }
1195 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
1197 virtual RecTy *getFieldType(const std::string &FieldName) const;
1198 virtual Init *getFieldInit(Record &R, const RecordVal *RV,
1199 const std::string &FieldName) const;
1201 virtual std::string getAsString() const;
1203 virtual Init *getBit(unsigned Bit) const {
1204 llvm_unreachable("Illegal bit reference off def");
1207 /// resolveListElementReference - This method is used to implement
1208 /// VarListElementInit::resolveReferences. If the list element is resolvable
1209 /// now, we return the resolved value, otherwise we return null.
1210 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
1211 unsigned Elt) const {
1212 llvm_unreachable("Illegal element reference off def");
1217 /// FieldInit - X.Y - Represent a reference to a subfield of a variable
1219 class FieldInit : public TypedInit {
1220 Init *Rec; // Record we are referring to
1221 std::string FieldName; // Field we are accessing
1223 FieldInit(Init *R, const std::string &FN)
1224 : TypedInit(IK_FieldInit, R->getFieldType(FN)), Rec(R), FieldName(FN) {
1225 assert(getType() && "FieldInit with non-record type!");
1228 FieldInit(const FieldInit &Other) LLVM_DELETED_FUNCTION;
1229 FieldInit &operator=(const FieldInit &Other) LLVM_DELETED_FUNCTION;
1232 static bool classof(const Init *I) {
1233 return I->getKind() == IK_FieldInit;
1235 static FieldInit *get(Init *R, const std::string &FN);
1236 static FieldInit *get(Init *R, const Init *FN);
1238 virtual Init *convertInitializerTo(RecTy *Ty) const {
1239 return Ty->convertValue(const_cast<FieldInit *>(this));
1242 virtual Init *getBit(unsigned Bit) const;
1244 virtual Init *resolveListElementReference(Record &R,
1245 const RecordVal *RV,
1246 unsigned Elt) const;
1248 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
1250 virtual std::string getAsString() const {
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 virtual Init *convertInitializerTo(RecTy *Ty) const {
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 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
1308 virtual std::string getAsString() const;
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 virtual Init *getBit(unsigned Bit) const {
1326 llvm_unreachable("Illegal bit reference off dag");
1329 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
1330 unsigned Elt) const {
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);
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(0), 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(0), 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(0); }
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() ? 0 : 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() ? 0 : 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