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
179 /// BitsRecTy - 'bits<n>' - Represent a fixed number of bits
181 class BitsRecTy : public RecTy {
183 explicit BitsRecTy(unsigned Sz) : RecTy(BitsRecTyKind), Size(Sz) {}
185 static bool classof(const RecTy *RT) {
186 return RT->getRecTyKind() == BitsRecTyKind;
189 static BitsRecTy *get(unsigned Sz);
191 unsigned getNumBits() const { return Size; }
193 virtual Init *convertValue( UnsetInit *UI);
194 virtual Init *convertValue( BitInit *UI);
195 virtual Init *convertValue( BitsInit *BI);
196 virtual Init *convertValue( IntInit *II);
197 virtual Init *convertValue(StringInit *SI) { return 0; }
198 virtual Init *convertValue( ListInit *LI) { return 0; }
199 virtual Init *convertValue(VarBitInit *VB) { return 0; }
200 virtual Init *convertValue( DefInit *DI) { return 0; }
201 virtual Init *convertValue( DagInit *DI) { return 0; }
202 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
203 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
204 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
205 virtual Init *convertValue( TypedInit *TI);
206 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
207 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
209 virtual std::string getAsString() const;
211 virtual bool typeIsConvertibleTo(const RecTy *RHS) const {
212 return RHS->baseClassOf(this);
214 virtual bool baseClassOf(const RecTy*) const;
218 /// IntRecTy - 'int' - Represent an integer value of no particular size
220 class IntRecTy : public RecTy {
221 static IntRecTy Shared;
222 IntRecTy() : RecTy(IntRecTyKind) {}
224 static bool classof(const RecTy *RT) {
225 return RT->getRecTyKind() == IntRecTyKind;
228 static IntRecTy *get() { return &Shared; }
230 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
231 virtual Init *convertValue( BitInit *BI);
232 virtual Init *convertValue( BitsInit *BI);
233 virtual Init *convertValue( IntInit *II) { return (Init*)II; }
234 virtual Init *convertValue(StringInit *SI) { return 0; }
235 virtual Init *convertValue( ListInit *LI) { return 0; }
236 virtual Init *convertValue(VarBitInit *VB) { return 0; }
237 virtual Init *convertValue( DefInit *DI) { return 0; }
238 virtual Init *convertValue( DagInit *DI) { return 0; }
239 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
240 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
241 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
242 virtual Init *convertValue( TypedInit *TI);
243 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
244 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
246 virtual std::string getAsString() const { return "int"; }
248 virtual bool typeIsConvertibleTo(const RecTy *RHS) const {
249 return RHS->baseClassOf(this);
252 virtual bool baseClassOf(const RecTy*) const;
255 /// StringRecTy - 'string' - Represent an string value
257 class StringRecTy : public RecTy {
258 static StringRecTy Shared;
259 StringRecTy() : RecTy(StringRecTyKind) {}
261 static bool classof(const RecTy *RT) {
262 return RT->getRecTyKind() == StringRecTyKind;
265 static StringRecTy *get() { return &Shared; }
267 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
268 virtual Init *convertValue( BitInit *BI) { return 0; }
269 virtual Init *convertValue( BitsInit *BI) { return 0; }
270 virtual Init *convertValue( IntInit *II) { return 0; }
271 virtual Init *convertValue(StringInit *SI) { return (Init*)SI; }
272 virtual Init *convertValue( ListInit *LI) { return 0; }
273 virtual Init *convertValue( UnOpInit *BO);
274 virtual Init *convertValue( BinOpInit *BO);
275 virtual Init *convertValue( TernOpInit *BO) { return RecTy::convertValue(BO);}
277 virtual Init *convertValue(VarBitInit *VB) { return 0; }
278 virtual Init *convertValue( DefInit *DI) { return 0; }
279 virtual Init *convertValue( DagInit *DI) { return 0; }
280 virtual Init *convertValue( TypedInit *TI);
281 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
282 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
284 virtual std::string getAsString() const { return "string"; }
286 virtual bool typeIsConvertibleTo(const RecTy *RHS) const {
287 return RHS->baseClassOf(this);
291 // ListRecTy - 'list<Ty>' - Represent a list of values, all of which must be of
292 // the specified type.
293 /// ListRecTy - 'list<Ty>' - Represent a list of values, all of which must
294 /// be of the specified type.
296 class ListRecTy : public RecTy {
298 explicit ListRecTy(RecTy *T) : RecTy(ListRecTyKind), Ty(T) {}
299 friend ListRecTy *RecTy::getListTy();
301 static bool classof(const RecTy *RT) {
302 return RT->getRecTyKind() == ListRecTyKind;
305 static ListRecTy *get(RecTy *T) { return T->getListTy(); }
306 RecTy *getElementType() const { return Ty; }
308 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
309 virtual Init *convertValue( BitInit *BI) { return 0; }
310 virtual Init *convertValue( BitsInit *BI) { return 0; }
311 virtual Init *convertValue( IntInit *II) { return 0; }
312 virtual Init *convertValue(StringInit *SI) { return 0; }
313 virtual Init *convertValue( ListInit *LI);
314 virtual Init *convertValue(VarBitInit *VB) { return 0; }
315 virtual Init *convertValue( DefInit *DI) { return 0; }
316 virtual Init *convertValue( DagInit *DI) { return 0; }
317 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
318 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
319 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
320 virtual Init *convertValue( TypedInit *TI);
321 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
322 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
324 virtual std::string getAsString() const;
326 virtual bool typeIsConvertibleTo(const RecTy *RHS) const {
327 return RHS->baseClassOf(this);
330 virtual bool baseClassOf(const RecTy*) const;
333 /// DagRecTy - 'dag' - Represent a dag fragment
335 class DagRecTy : public RecTy {
336 static DagRecTy Shared;
337 DagRecTy() : RecTy(DagRecTyKind) {}
339 static bool classof(const RecTy *RT) {
340 return RT->getRecTyKind() == DagRecTyKind;
343 static DagRecTy *get() { return &Shared; }
345 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
346 virtual Init *convertValue( BitInit *BI) { return 0; }
347 virtual Init *convertValue( BitsInit *BI) { return 0; }
348 virtual Init *convertValue( IntInit *II) { return 0; }
349 virtual Init *convertValue(StringInit *SI) { return 0; }
350 virtual Init *convertValue( ListInit *LI) { return 0; }
351 virtual Init *convertValue(VarBitInit *VB) { return 0; }
352 virtual Init *convertValue( DefInit *DI) { return 0; }
353 virtual Init *convertValue( UnOpInit *BO);
354 virtual Init *convertValue( BinOpInit *BO);
355 virtual Init *convertValue( TernOpInit *BO) { return RecTy::convertValue(BO);}
356 virtual Init *convertValue( DagInit *CI) { return (Init*)CI; }
357 virtual Init *convertValue( TypedInit *TI);
358 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
359 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
361 virtual std::string getAsString() const { return "dag"; }
363 virtual bool typeIsConvertibleTo(const RecTy *RHS) const {
364 return RHS->baseClassOf(this);
369 /// RecordRecTy - '[classname]' - Represent an instance of a class, such as:
372 class RecordRecTy : public RecTy {
374 explicit RecordRecTy(Record *R) : RecTy(RecordRecTyKind), Rec(R) {}
377 static bool classof(const RecTy *RT) {
378 return RT->getRecTyKind() == RecordRecTyKind;
381 static RecordRecTy *get(Record *R);
383 Record *getRecord() const { return Rec; }
385 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
386 virtual Init *convertValue( BitInit *BI) { return 0; }
387 virtual Init *convertValue( BitsInit *BI) { return 0; }
388 virtual Init *convertValue( IntInit *II) { return 0; }
389 virtual Init *convertValue(StringInit *SI) { return 0; }
390 virtual Init *convertValue( ListInit *LI) { return 0; }
391 virtual Init *convertValue(VarBitInit *VB) { return 0; }
392 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
393 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
394 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
395 virtual Init *convertValue( DefInit *DI);
396 virtual Init *convertValue( DagInit *DI) { return 0; }
397 virtual Init *convertValue( TypedInit *VI);
398 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
399 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
401 virtual std::string getAsString() const;
403 virtual bool typeIsConvertibleTo(const RecTy *RHS) const {
404 return RHS->baseClassOf(this);
406 virtual bool baseClassOf(const RecTy*) const;
409 /// resolveTypes - Find a common type that T1 and T2 convert to.
410 /// Return 0 if no such type exists.
412 RecTy *resolveTypes(RecTy *T1, RecTy *T2);
414 //===----------------------------------------------------------------------===//
415 // Initializer Classes
416 //===----------------------------------------------------------------------===//
420 /// \brief Discriminator enum (for isa<>, dyn_cast<>, et al.)
422 /// This enum is laid out by a preorder traversal of the inheritance
423 /// hierarchy, and does not contain an entry for abstract classes, as per
424 /// the recommendation in docs/HowToSetUpLLVMStyleRTTI.rst.
426 /// We also explicitly include "first" and "last" values for each
427 /// interior node of the inheritance tree, to make it easier to read the
428 /// corresponding classof().
430 /// We could pack these a bit tighter by not having the IK_FirstXXXInit
431 /// and IK_LastXXXInit be their own values, but that would degrade
432 /// readability for really no benefit.
449 IK_VarListElementInit,
457 Init(const Init &) LLVM_DELETED_FUNCTION;
458 Init &operator=(const Init &) LLVM_DELETED_FUNCTION;
459 virtual void anchor();
462 InitKind getKind() const { return Kind; }
465 explicit Init(InitKind K) : Kind(K) {}
470 /// isComplete - This virtual method should be overridden by values that may
471 /// not be completely specified yet.
472 virtual bool isComplete() const { return true; }
474 /// print - Print out this value.
475 void print(raw_ostream &OS) const { OS << getAsString(); }
477 /// getAsString - Convert this value to a string form.
478 virtual std::string getAsString() const = 0;
479 /// getAsUnquotedString - Convert this value to a string form,
480 /// without adding quote markers. This primaruly affects
481 /// StringInits where we will not surround the string value with
483 virtual std::string getAsUnquotedString() const { return getAsString(); }
485 /// dump - Debugging method that may be called through a debugger, just
486 /// invokes print on stderr.
489 /// convertInitializerTo - This virtual function is a simple call-back
490 /// function that should be overridden to call the appropriate
491 /// RecTy::convertValue method.
493 virtual Init *convertInitializerTo(RecTy *Ty) const = 0;
495 /// convertInitializerBitRange - This method is used to implement the bitrange
496 /// selection operator. Given an initializer, it selects the specified bits
497 /// out, returning them as a new init of bits type. If it is not legal to use
498 /// the bit subscript operator on this initializer, return null.
501 convertInitializerBitRange(const std::vector<unsigned> &Bits) const {
505 /// convertInitListSlice - This method is used to implement the list slice
506 /// selection operator. Given an initializer, it selects the specified list
507 /// elements, returning them as a new init of list type. If it is not legal
508 /// to take a slice of this, return null.
511 convertInitListSlice(const std::vector<unsigned> &Elements) const {
515 /// getFieldType - This method is used to implement the FieldInit class.
516 /// Implementors of this method should return the type of the named field if
517 /// they are of record type.
519 virtual RecTy *getFieldType(const std::string &FieldName) const { return 0; }
521 /// getFieldInit - This method complements getFieldType to return the
522 /// initializer for the specified field. If getFieldType returns non-null
523 /// this method should return non-null, otherwise it returns null.
525 virtual Init *getFieldInit(Record &R, const RecordVal *RV,
526 const std::string &FieldName) const {
530 /// resolveReferences - This method is used by classes that refer to other
531 /// variables which may not be defined at the time the expression is formed.
532 /// If a value is set for the variable later, this method will be called on
533 /// users of the value to allow the value to propagate out.
535 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const {
536 return const_cast<Init *>(this);
539 /// getBit - This method is used to return the initializer for the specified
541 virtual Init *getBit(unsigned Bit) const = 0;
543 /// getBitVar - This method is used to retrieve the initializer for bit
544 /// reference. For non-VarBitInit, it simply returns itself.
545 virtual Init *getBitVar() const { return const_cast<Init*>(this); }
547 /// getBitNum - This method is used to retrieve the bit number of a bit
548 /// reference. For non-VarBitInit, it simply returns 0.
549 virtual unsigned getBitNum() const { return 0; }
552 inline raw_ostream &operator<<(raw_ostream &OS, const Init &I) {
553 I.print(OS); return OS;
556 /// TypedInit - This is the common super-class of types that have a specific,
559 class TypedInit : public Init {
562 TypedInit(const TypedInit &Other) LLVM_DELETED_FUNCTION;
563 TypedInit &operator=(const TypedInit &Other) LLVM_DELETED_FUNCTION;
566 explicit TypedInit(InitKind K, RecTy *T) : Init(K), Ty(T) {}
569 static bool classof(const Init *I) {
570 return I->getKind() >= IK_FirstTypedInit &&
571 I->getKind() <= IK_LastTypedInit;
573 RecTy *getType() const { return Ty; }
576 convertInitializerBitRange(const std::vector<unsigned> &Bits) const;
578 convertInitListSlice(const std::vector<unsigned> &Elements) const;
580 /// getFieldType - This method is used to implement the FieldInit class.
581 /// Implementors of this method should return the type of the named field if
582 /// they are of record type.
584 virtual RecTy *getFieldType(const std::string &FieldName) const;
586 /// resolveListElementReference - This method is used to implement
587 /// VarListElementInit::resolveReferences. If the list element is resolvable
588 /// now, we return the resolved value, otherwise we return null.
589 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
590 unsigned Elt) const = 0;
594 /// UnsetInit - ? - Represents an uninitialized value
596 class UnsetInit : public Init {
597 UnsetInit() : Init(IK_UnsetInit) {}
598 UnsetInit(const UnsetInit &) LLVM_DELETED_FUNCTION;
599 UnsetInit &operator=(const UnsetInit &Other) LLVM_DELETED_FUNCTION;
600 virtual void anchor();
603 static bool classof(const Init *I) {
604 return I->getKind() == IK_UnsetInit;
606 static UnsetInit *get();
608 virtual Init *convertInitializerTo(RecTy *Ty) const {
609 return Ty->convertValue(const_cast<UnsetInit *>(this));
612 virtual Init *getBit(unsigned Bit) const {
613 return const_cast<UnsetInit*>(this);
616 virtual bool isComplete() const { return false; }
617 virtual std::string getAsString() const { return "?"; }
621 /// BitInit - true/false - Represent a concrete initializer for a bit.
623 class BitInit : public Init {
626 explicit BitInit(bool V) : Init(IK_BitInit), Value(V) {}
627 BitInit(const BitInit &Other) LLVM_DELETED_FUNCTION;
628 BitInit &operator=(BitInit &Other) LLVM_DELETED_FUNCTION;
629 virtual void anchor();
632 static bool classof(const Init *I) {
633 return I->getKind() == IK_BitInit;
635 static BitInit *get(bool V);
637 bool getValue() const { return Value; }
639 virtual Init *convertInitializerTo(RecTy *Ty) const {
640 return Ty->convertValue(const_cast<BitInit *>(this));
643 virtual Init *getBit(unsigned Bit) const {
644 assert(Bit < 1 && "Bit index out of range!");
645 return const_cast<BitInit*>(this);
648 virtual std::string getAsString() const { return Value ? "1" : "0"; }
651 /// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
652 /// It contains a vector of bits, whose size is determined by the type.
654 class BitsInit : public Init, public FoldingSetNode {
655 std::vector<Init*> Bits;
657 BitsInit(ArrayRef<Init *> Range)
658 : Init(IK_BitsInit), Bits(Range.begin(), Range.end()) {}
660 BitsInit(const BitsInit &Other) LLVM_DELETED_FUNCTION;
661 BitsInit &operator=(const BitsInit &Other) LLVM_DELETED_FUNCTION;
664 static bool classof(const Init *I) {
665 return I->getKind() == IK_BitsInit;
667 static BitsInit *get(ArrayRef<Init *> Range);
669 void Profile(FoldingSetNodeID &ID) const;
671 unsigned getNumBits() const { return Bits.size(); }
673 virtual Init *convertInitializerTo(RecTy *Ty) const {
674 return Ty->convertValue(const_cast<BitsInit *>(this));
677 convertInitializerBitRange(const std::vector<unsigned> &Bits) const;
679 virtual bool isComplete() const {
680 for (unsigned i = 0; i != getNumBits(); ++i)
681 if (!getBit(i)->isComplete()) return false;
684 bool allInComplete() const {
685 for (unsigned i = 0; i != getNumBits(); ++i)
686 if (getBit(i)->isComplete()) return false;
689 virtual std::string getAsString() const;
691 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
693 virtual Init *getBit(unsigned Bit) const {
694 assert(Bit < Bits.size() && "Bit index out of range!");
700 /// IntInit - 7 - Represent an initalization by a literal integer value.
702 class IntInit : public TypedInit {
705 explicit IntInit(int64_t V)
706 : TypedInit(IK_IntInit, IntRecTy::get()), Value(V) {}
708 IntInit(const IntInit &Other) LLVM_DELETED_FUNCTION;
709 IntInit &operator=(const IntInit &Other) LLVM_DELETED_FUNCTION;
712 static bool classof(const Init *I) {
713 return I->getKind() == IK_IntInit;
715 static IntInit *get(int64_t V);
717 int64_t getValue() const { return Value; }
719 virtual Init *convertInitializerTo(RecTy *Ty) const {
720 return Ty->convertValue(const_cast<IntInit *>(this));
723 convertInitializerBitRange(const std::vector<unsigned> &Bits) const;
725 virtual std::string getAsString() const;
727 /// resolveListElementReference - This method is used to implement
728 /// VarListElementInit::resolveReferences. If the list element is resolvable
729 /// now, we return the resolved value, otherwise we return null.
730 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
731 unsigned Elt) const {
732 llvm_unreachable("Illegal element reference off int");
735 virtual Init *getBit(unsigned Bit) const {
736 return BitInit::get((Value & (1ULL << Bit)) != 0);
741 /// StringInit - "foo" - Represent an initialization by a string value.
743 class StringInit : public TypedInit {
746 explicit StringInit(const std::string &V)
747 : TypedInit(IK_StringInit, StringRecTy::get()), Value(V) {}
749 StringInit(const StringInit &Other) LLVM_DELETED_FUNCTION;
750 StringInit &operator=(const StringInit &Other) LLVM_DELETED_FUNCTION;
751 virtual void anchor();
754 static bool classof(const Init *I) {
755 return I->getKind() == IK_StringInit;
757 static StringInit *get(StringRef);
759 const std::string &getValue() const { return Value; }
761 virtual Init *convertInitializerTo(RecTy *Ty) const {
762 return Ty->convertValue(const_cast<StringInit *>(this));
765 virtual std::string getAsString() const { return "\"" + Value + "\""; }
766 virtual std::string getAsUnquotedString() const { return Value; }
768 /// resolveListElementReference - This method is used to implement
769 /// VarListElementInit::resolveReferences. If the list element is resolvable
770 /// now, we return the resolved value, otherwise we return null.
771 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
772 unsigned Elt) const {
773 llvm_unreachable("Illegal element reference off string");
776 virtual Init *getBit(unsigned Bit) const {
777 llvm_unreachable("Illegal bit reference off string");
781 /// ListInit - [AL, AH, CL] - Represent a list of defs
783 class ListInit : public TypedInit, public FoldingSetNode {
784 std::vector<Init*> Values;
786 typedef std::vector<Init*>::const_iterator const_iterator;
789 explicit ListInit(ArrayRef<Init *> Range, RecTy *EltTy)
790 : TypedInit(IK_ListInit, ListRecTy::get(EltTy)),
791 Values(Range.begin(), Range.end()) {}
793 ListInit(const ListInit &Other) LLVM_DELETED_FUNCTION;
794 ListInit &operator=(const ListInit &Other) LLVM_DELETED_FUNCTION;
797 static bool classof(const Init *I) {
798 return I->getKind() == IK_ListInit;
800 static ListInit *get(ArrayRef<Init *> Range, RecTy *EltTy);
802 void Profile(FoldingSetNodeID &ID) const;
804 unsigned getSize() const { return Values.size(); }
805 Init *getElement(unsigned i) const {
806 assert(i < Values.size() && "List element index out of range!");
810 Record *getElementAsRecord(unsigned i) const;
813 convertInitListSlice(const std::vector<unsigned> &Elements) const;
815 virtual Init *convertInitializerTo(RecTy *Ty) const {
816 return Ty->convertValue(const_cast<ListInit *>(this));
819 /// resolveReferences - This method is used by classes that refer to other
820 /// variables which may not be defined at the time they expression is formed.
821 /// If a value is set for the variable later, this method will be called on
822 /// users of the value to allow the value to propagate out.
824 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
826 virtual std::string getAsString() const;
828 ArrayRef<Init*> getValues() const { return Values; }
830 inline const_iterator begin() const { return Values.begin(); }
831 inline const_iterator end () const { return Values.end(); }
833 inline size_t size () const { return Values.size(); }
834 inline bool empty() const { return Values.empty(); }
836 /// resolveListElementReference - This method is used to implement
837 /// VarListElementInit::resolveReferences. If the list element is resolvable
838 /// now, we return the resolved value, otherwise we return null.
839 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
842 virtual Init *getBit(unsigned Bit) const {
843 llvm_unreachable("Illegal bit reference off list");
848 /// OpInit - Base class for operators
850 class OpInit : public TypedInit {
851 OpInit(const OpInit &Other) LLVM_DELETED_FUNCTION;
852 OpInit &operator=(OpInit &Other) LLVM_DELETED_FUNCTION;
855 explicit OpInit(InitKind K, RecTy *Type) : TypedInit(K, Type) {}
858 static bool classof(const Init *I) {
859 return I->getKind() >= IK_FirstOpInit &&
860 I->getKind() <= IK_LastOpInit;
862 // Clone - Clone this operator, replacing arguments with the new list
863 virtual OpInit *clone(std::vector<Init *> &Operands) const = 0;
865 virtual int getNumOperands() const = 0;
866 virtual Init *getOperand(int i) const = 0;
868 // Fold - If possible, fold this to a simpler init. Return this if not
870 virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const = 0;
872 virtual Init *convertInitializerTo(RecTy *Ty) const {
873 return Ty->convertValue(const_cast<OpInit *>(this));
876 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
879 virtual Init *getBit(unsigned Bit) const;
883 /// UnOpInit - !op (X) - Transform an init.
885 class UnOpInit : public OpInit {
887 enum UnaryOp { CAST, HEAD, TAIL, EMPTY };
892 UnOpInit(UnaryOp opc, Init *lhs, RecTy *Type)
893 : OpInit(IK_UnOpInit, Type), Opc(opc), LHS(lhs) {}
895 UnOpInit(const UnOpInit &Other) LLVM_DELETED_FUNCTION;
896 UnOpInit &operator=(const UnOpInit &Other) LLVM_DELETED_FUNCTION;
899 static bool classof(const Init *I) {
900 return I->getKind() == IK_UnOpInit;
902 static UnOpInit *get(UnaryOp opc, Init *lhs, RecTy *Type);
904 // Clone - Clone this operator, replacing arguments with the new list
905 virtual OpInit *clone(std::vector<Init *> &Operands) const {
906 assert(Operands.size() == 1 &&
907 "Wrong number of operands for unary operation");
908 return UnOpInit::get(getOpcode(), *Operands.begin(), getType());
911 virtual int getNumOperands() const { return 1; }
912 virtual Init *getOperand(int i) const {
913 assert(i == 0 && "Invalid operand id for unary operator");
917 UnaryOp getOpcode() const { return Opc; }
918 Init *getOperand() const { return LHS; }
920 // Fold - If possible, fold this to a simpler init. Return this if not
922 virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const;
924 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
926 virtual std::string getAsString() const;
929 /// BinOpInit - !op (X, Y) - Combine two inits.
931 class BinOpInit : public OpInit {
933 enum BinaryOp { SHL, SRA, SRL, STRCONCAT, CONCAT, EQ };
938 BinOpInit(BinaryOp opc, Init *lhs, Init *rhs, RecTy *Type) :
939 OpInit(IK_BinOpInit, Type), Opc(opc), LHS(lhs), RHS(rhs) {}
941 BinOpInit(const BinOpInit &Other) LLVM_DELETED_FUNCTION;
942 BinOpInit &operator=(const BinOpInit &Other) LLVM_DELETED_FUNCTION;
945 static bool classof(const Init *I) {
946 return I->getKind() == IK_BinOpInit;
948 static BinOpInit *get(BinaryOp opc, Init *lhs, Init *rhs,
951 // Clone - Clone this operator, replacing arguments with the new list
952 virtual OpInit *clone(std::vector<Init *> &Operands) const {
953 assert(Operands.size() == 2 &&
954 "Wrong number of operands for binary operation");
955 return BinOpInit::get(getOpcode(), Operands[0], Operands[1], getType());
958 virtual int getNumOperands() const { return 2; }
959 virtual Init *getOperand(int i) const {
960 assert((i == 0 || i == 1) && "Invalid operand id for binary operator");
968 BinaryOp getOpcode() const { return Opc; }
969 Init *getLHS() const { return LHS; }
970 Init *getRHS() const { return RHS; }
972 // Fold - If possible, fold this to a simpler init. Return this if not
974 virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const;
976 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
978 virtual std::string getAsString() const;
981 /// TernOpInit - !op (X, Y, Z) - Combine two inits.
983 class TernOpInit : public OpInit {
985 enum TernaryOp { SUBST, FOREACH, IF };
988 Init *LHS, *MHS, *RHS;
990 TernOpInit(TernaryOp opc, Init *lhs, Init *mhs, Init *rhs,
992 OpInit(IK_TernOpInit, Type), Opc(opc), LHS(lhs), MHS(mhs), RHS(rhs) {}
994 TernOpInit(const TernOpInit &Other) LLVM_DELETED_FUNCTION;
995 TernOpInit &operator=(const TernOpInit &Other) LLVM_DELETED_FUNCTION;
998 static bool classof(const Init *I) {
999 return I->getKind() == IK_TernOpInit;
1001 static TernOpInit *get(TernaryOp opc, Init *lhs,
1002 Init *mhs, Init *rhs,
1005 // Clone - Clone this operator, replacing arguments with the new list
1006 virtual OpInit *clone(std::vector<Init *> &Operands) const {
1007 assert(Operands.size() == 3 &&
1008 "Wrong number of operands for ternary operation");
1009 return TernOpInit::get(getOpcode(), Operands[0], Operands[1], Operands[2],
1013 virtual int getNumOperands() const { return 3; }
1014 virtual Init *getOperand(int i) const {
1015 assert((i == 0 || i == 1 || i == 2) &&
1016 "Invalid operand id for ternary operator");
1019 } else if (i == 1) {
1026 TernaryOp getOpcode() const { return Opc; }
1027 Init *getLHS() const { return LHS; }
1028 Init *getMHS() const { return MHS; }
1029 Init *getRHS() const { return RHS; }
1031 // Fold - If possible, fold this to a simpler init. Return this if not
1032 // possible to fold.
1033 virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const;
1035 virtual bool isComplete() const { return false; }
1037 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
1039 virtual std::string getAsString() const;
1043 /// VarInit - 'Opcode' - Represent a reference to an entire variable object.
1045 class VarInit : public TypedInit {
1048 explicit VarInit(const std::string &VN, RecTy *T)
1049 : TypedInit(IK_VarInit, T), VarName(StringInit::get(VN)) {}
1050 explicit VarInit(Init *VN, RecTy *T)
1051 : TypedInit(IK_VarInit, T), VarName(VN) {}
1053 VarInit(const VarInit &Other) LLVM_DELETED_FUNCTION;
1054 VarInit &operator=(const VarInit &Other) LLVM_DELETED_FUNCTION;
1057 static bool classof(const Init *I) {
1058 return I->getKind() == IK_VarInit;
1060 static VarInit *get(const std::string &VN, RecTy *T);
1061 static VarInit *get(Init *VN, RecTy *T);
1063 virtual Init *convertInitializerTo(RecTy *Ty) const {
1064 return Ty->convertValue(const_cast<VarInit *>(this));
1067 const std::string &getName() const;
1068 Init *getNameInit() const { return VarName; }
1069 std::string getNameInitAsString() const {
1070 return getNameInit()->getAsUnquotedString();
1073 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
1074 unsigned Elt) const;
1076 virtual RecTy *getFieldType(const std::string &FieldName) const;
1077 virtual Init *getFieldInit(Record &R, const RecordVal *RV,
1078 const std::string &FieldName) const;
1080 /// resolveReferences - This method is used by classes that refer to other
1081 /// variables which may not be defined at the time they expression is formed.
1082 /// If a value is set for the variable later, this method will be called on
1083 /// users of the value to allow the value to propagate out.
1085 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
1087 virtual Init *getBit(unsigned Bit) const;
1089 virtual std::string getAsString() const { return getName(); }
1093 /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
1095 class VarBitInit : public Init {
1099 VarBitInit(TypedInit *T, unsigned B) : Init(IK_VarBitInit), TI(T), Bit(B) {
1100 assert(T->getType() &&
1101 (isa<IntRecTy>(T->getType()) ||
1102 (isa<BitsRecTy>(T->getType()) &&
1103 cast<BitsRecTy>(T->getType())->getNumBits() > B)) &&
1104 "Illegal VarBitInit expression!");
1107 VarBitInit(const VarBitInit &Other) LLVM_DELETED_FUNCTION;
1108 VarBitInit &operator=(const VarBitInit &Other) LLVM_DELETED_FUNCTION;
1111 static bool classof(const Init *I) {
1112 return I->getKind() == IK_VarBitInit;
1114 static VarBitInit *get(TypedInit *T, unsigned B);
1116 virtual Init *convertInitializerTo(RecTy *Ty) const {
1117 return Ty->convertValue(const_cast<VarBitInit *>(this));
1120 virtual Init *getBitVar() const { return TI; }
1121 virtual unsigned getBitNum() const { return Bit; }
1123 virtual std::string getAsString() const;
1124 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
1126 virtual Init *getBit(unsigned B) const {
1127 assert(B < 1 && "Bit index out of range!");
1128 return const_cast<VarBitInit*>(this);
1132 /// VarListElementInit - List[4] - Represent access to one element of a var or
1134 class VarListElementInit : public TypedInit {
1138 VarListElementInit(TypedInit *T, unsigned E)
1139 : TypedInit(IK_VarListElementInit,
1140 cast<ListRecTy>(T->getType())->getElementType()),
1142 assert(T->getType() && isa<ListRecTy>(T->getType()) &&
1143 "Illegal VarBitInit expression!");
1146 VarListElementInit(const VarListElementInit &Other) LLVM_DELETED_FUNCTION;
1147 void operator=(const VarListElementInit &Other) LLVM_DELETED_FUNCTION;
1150 static bool classof(const Init *I) {
1151 return I->getKind() == IK_VarListElementInit;
1153 static VarListElementInit *get(TypedInit *T, unsigned E);
1155 virtual Init *convertInitializerTo(RecTy *Ty) const {
1156 return Ty->convertValue(const_cast<VarListElementInit *>(this));
1159 TypedInit *getVariable() const { return TI; }
1160 unsigned getElementNum() const { return Element; }
1162 /// resolveListElementReference - This method is used to implement
1163 /// VarListElementInit::resolveReferences. If the list element is resolvable
1164 /// now, we return the resolved value, otherwise we return null.
1165 virtual Init *resolveListElementReference(Record &R,
1166 const RecordVal *RV,
1167 unsigned Elt) const;
1169 virtual std::string getAsString() const;
1170 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
1172 virtual Init *getBit(unsigned Bit) const;
1175 /// DefInit - AL - Represent a reference to a 'def' in the description
1177 class DefInit : public TypedInit {
1180 DefInit(Record *D, RecordRecTy *T) : TypedInit(IK_DefInit, T), Def(D) {}
1181 friend class Record;
1183 DefInit(const DefInit &Other) LLVM_DELETED_FUNCTION;
1184 DefInit &operator=(const DefInit &Other) LLVM_DELETED_FUNCTION;
1187 static bool classof(const Init *I) {
1188 return I->getKind() == IK_DefInit;
1190 static DefInit *get(Record*);
1192 virtual Init *convertInitializerTo(RecTy *Ty) const {
1193 return Ty->convertValue(const_cast<DefInit *>(this));
1196 Record *getDef() const { return Def; }
1198 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
1200 virtual RecTy *getFieldType(const std::string &FieldName) const;
1201 virtual Init *getFieldInit(Record &R, const RecordVal *RV,
1202 const std::string &FieldName) const;
1204 virtual std::string getAsString() const;
1206 virtual Init *getBit(unsigned Bit) const {
1207 llvm_unreachable("Illegal bit reference off def");
1210 /// resolveListElementReference - This method is used to implement
1211 /// VarListElementInit::resolveReferences. If the list element is resolvable
1212 /// now, we return the resolved value, otherwise we return null.
1213 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
1214 unsigned Elt) const {
1215 llvm_unreachable("Illegal element reference off def");
1220 /// FieldInit - X.Y - Represent a reference to a subfield of a variable
1222 class FieldInit : public TypedInit {
1223 Init *Rec; // Record we are referring to
1224 std::string FieldName; // Field we are accessing
1226 FieldInit(Init *R, const std::string &FN)
1227 : TypedInit(IK_FieldInit, R->getFieldType(FN)), Rec(R), FieldName(FN) {
1228 assert(getType() && "FieldInit with non-record type!");
1231 FieldInit(const FieldInit &Other) LLVM_DELETED_FUNCTION;
1232 FieldInit &operator=(const FieldInit &Other) LLVM_DELETED_FUNCTION;
1235 static bool classof(const Init *I) {
1236 return I->getKind() == IK_FieldInit;
1238 static FieldInit *get(Init *R, const std::string &FN);
1239 static FieldInit *get(Init *R, const Init *FN);
1241 virtual Init *convertInitializerTo(RecTy *Ty) const {
1242 return Ty->convertValue(const_cast<FieldInit *>(this));
1245 virtual Init *getBit(unsigned Bit) const;
1247 virtual Init *resolveListElementReference(Record &R,
1248 const RecordVal *RV,
1249 unsigned Elt) const;
1251 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
1253 virtual std::string getAsString() const {
1254 return Rec->getAsString() + "." + FieldName;
1258 /// DagInit - (v a, b) - Represent a DAG tree value. DAG inits are required
1259 /// to have at least one value then a (possibly empty) list of arguments. Each
1260 /// argument can have a name associated with it.
1262 class DagInit : public TypedInit, public FoldingSetNode {
1264 std::string ValName;
1265 std::vector<Init*> Args;
1266 std::vector<std::string> ArgNames;
1268 DagInit(Init *V, const std::string &VN,
1269 ArrayRef<Init *> ArgRange,
1270 ArrayRef<std::string> NameRange)
1271 : TypedInit(IK_DagInit, DagRecTy::get()), Val(V), ValName(VN),
1272 Args(ArgRange.begin(), ArgRange.end()),
1273 ArgNames(NameRange.begin(), NameRange.end()) {}
1275 DagInit(const DagInit &Other) LLVM_DELETED_FUNCTION;
1276 DagInit &operator=(const DagInit &Other) LLVM_DELETED_FUNCTION;
1279 static bool classof(const Init *I) {
1280 return I->getKind() == IK_DagInit;
1282 static DagInit *get(Init *V, const std::string &VN,
1283 ArrayRef<Init *> ArgRange,
1284 ArrayRef<std::string> NameRange);
1285 static DagInit *get(Init *V, const std::string &VN,
1287 std::pair<Init*, std::string> > &args);
1289 void Profile(FoldingSetNodeID &ID) const;
1291 virtual Init *convertInitializerTo(RecTy *Ty) const {
1292 return Ty->convertValue(const_cast<DagInit *>(this));
1295 Init *getOperator() const { return Val; }
1297 const std::string &getName() const { return ValName; }
1299 unsigned getNumArgs() const { return Args.size(); }
1300 Init *getArg(unsigned Num) const {
1301 assert(Num < Args.size() && "Arg number out of range!");
1304 const std::string &getArgName(unsigned Num) const {
1305 assert(Num < ArgNames.size() && "Arg number out of range!");
1306 return ArgNames[Num];
1309 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
1311 virtual std::string getAsString() const;
1313 typedef std::vector<Init*>::const_iterator const_arg_iterator;
1314 typedef std::vector<std::string>::const_iterator const_name_iterator;
1316 inline const_arg_iterator arg_begin() const { return Args.begin(); }
1317 inline const_arg_iterator arg_end () const { return Args.end(); }
1319 inline size_t arg_size () const { return Args.size(); }
1320 inline bool arg_empty() const { return Args.empty(); }
1322 inline const_name_iterator name_begin() const { return ArgNames.begin(); }
1323 inline const_name_iterator name_end () const { return ArgNames.end(); }
1325 inline size_t name_size () const { return ArgNames.size(); }
1326 inline bool name_empty() const { return ArgNames.empty(); }
1328 virtual Init *getBit(unsigned Bit) const {
1329 llvm_unreachable("Illegal bit reference off dag");
1332 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
1333 unsigned Elt) const {
1334 llvm_unreachable("Illegal element reference off dag");
1338 //===----------------------------------------------------------------------===//
1339 // High-Level Classes
1340 //===----------------------------------------------------------------------===//
1348 RecordVal(Init *N, RecTy *T, unsigned P);
1349 RecordVal(const std::string &N, RecTy *T, unsigned P);
1351 const std::string &getName() const;
1352 const Init *getNameInit() const { return Name; }
1353 std::string getNameInitAsString() const {
1354 return getNameInit()->getAsUnquotedString();
1357 unsigned getPrefix() const { return Prefix; }
1358 RecTy *getType() const { return Ty; }
1359 Init *getValue() const { return Value; }
1361 bool setValue(Init *V) {
1363 Value = V->convertInitializerTo(Ty);
1371 void print(raw_ostream &OS, bool PrintSem = true) const;
1374 inline raw_ostream &operator<<(raw_ostream &OS, const RecordVal &RV) {
1375 RV.print(OS << " ");
1380 static unsigned LastID;
1382 // Unique record ID.
1385 // Location where record was instantiated, followed by the location of
1386 // multiclass prototypes used.
1387 SmallVector<SMLoc, 4> Locs;
1388 std::vector<Init *> TemplateArgs;
1389 std::vector<RecordVal> Values;
1390 std::vector<Record*> SuperClasses;
1392 // Tracks Record instances. Not owned by Record.
1393 RecordKeeper &TrackedRecords;
1402 // Constructs a record.
1403 explicit Record(const std::string &N, ArrayRef<SMLoc> locs,
1404 RecordKeeper &records) :
1405 ID(LastID++), Name(StringInit::get(N)), Locs(locs.begin(), locs.end()),
1406 TrackedRecords(records), TheInit(0) {
1409 explicit Record(Init *N, ArrayRef<SMLoc> locs, RecordKeeper &records) :
1410 ID(LastID++), Name(N), Locs(locs.begin(), locs.end()),
1411 TrackedRecords(records), TheInit(0) {
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 TrackedRecords(O.TrackedRecords), TheInit(O.TheInit) { }
1425 static unsigned getNewUID() { return LastID++; }
1428 unsigned getID() const { return ID; }
1430 const std::string &getName() const;
1431 Init *getNameInit() const {
1434 const std::string getNameInitAsString() const {
1435 return getNameInit()->getAsUnquotedString();
1438 void setName(Init *Name); // Also updates RecordKeeper.
1439 void setName(const std::string &Name); // Also updates RecordKeeper.
1441 ArrayRef<SMLoc> getLoc() const { return Locs; }
1443 /// get the corresponding DefInit.
1444 DefInit *getDefInit();
1446 const std::vector<Init *> &getTemplateArgs() const {
1447 return TemplateArgs;
1449 const std::vector<RecordVal> &getValues() const { return Values; }
1450 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; }
1452 bool isTemplateArg(Init *Name) const {
1453 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
1454 if (TemplateArgs[i] == Name) return true;
1457 bool isTemplateArg(StringRef Name) const {
1458 return isTemplateArg(StringInit::get(Name.str()));
1461 const RecordVal *getValue(const Init *Name) const {
1462 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1463 if (Values[i].getNameInit() == Name) return &Values[i];
1466 const RecordVal *getValue(StringRef Name) const {
1467 return getValue(StringInit::get(Name));
1469 RecordVal *getValue(const Init *Name) {
1470 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1471 if (Values[i].getNameInit() == Name) return &Values[i];
1474 RecordVal *getValue(StringRef Name) {
1475 return getValue(StringInit::get(Name));
1478 void addTemplateArg(Init *Name) {
1479 assert(!isTemplateArg(Name) && "Template arg already defined!");
1480 TemplateArgs.push_back(Name);
1482 void addTemplateArg(StringRef Name) {
1483 addTemplateArg(StringInit::get(Name.str()));
1486 void addValue(const RecordVal &RV) {
1487 assert(getValue(RV.getNameInit()) == 0 && "Value already added!");
1488 Values.push_back(RV);
1489 if (Values.size() > 1)
1490 // Keep NAME at the end of the list. It makes record dumps a
1491 // bit prettier and allows TableGen tests to be written more
1492 // naturally. Tests can use CHECK-NEXT to look for Record
1493 // fields they expect to see after a def. They can't do that if
1494 // NAME is the first Record field.
1495 std::swap(Values[Values.size() - 2], Values[Values.size() - 1]);
1498 void removeValue(Init *Name) {
1499 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1500 if (Values[i].getNameInit() == Name) {
1501 Values.erase(Values.begin()+i);
1504 llvm_unreachable("Cannot remove an entry that does not exist!");
1507 void removeValue(StringRef Name) {
1508 removeValue(StringInit::get(Name.str()));
1511 bool isSubClassOf(const Record *R) const {
1512 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1513 if (SuperClasses[i] == R)
1518 bool isSubClassOf(StringRef Name) const {
1519 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1520 if (SuperClasses[i]->getNameInitAsString() == Name)
1525 void addSuperClass(Record *R) {
1526 assert(!isSubClassOf(R) && "Already subclassing record!");
1527 SuperClasses.push_back(R);
1530 /// resolveReferences - If there are any field references that refer to fields
1531 /// that have been filled in, we can propagate the values now.
1533 void resolveReferences() { resolveReferencesTo(0); }
1535 /// resolveReferencesTo - If anything in this record refers to RV, replace the
1536 /// reference to RV with the RHS of RV. If RV is null, we resolve all
1537 /// possible references.
1538 void resolveReferencesTo(const RecordVal *RV);
1540 RecordKeeper &getRecords() const {
1541 return TrackedRecords;
1546 //===--------------------------------------------------------------------===//
1547 // High-level methods useful to tablegen back-ends
1550 /// getValueInit - Return the initializer for a value with the specified name,
1551 /// or throw an exception if the field does not exist.
1553 Init *getValueInit(StringRef FieldName) const;
1555 /// getValueAsString - This method looks up the specified field and returns
1556 /// its value as a string, throwing an exception if the field does not exist
1557 /// or if the value is not a string.
1559 std::string getValueAsString(StringRef FieldName) const;
1561 /// getValueAsBitsInit - This method looks up the specified field and returns
1562 /// its value as a BitsInit, throwing an exception if the field does not exist
1563 /// or if the value is not the right type.
1565 BitsInit *getValueAsBitsInit(StringRef FieldName) const;
1567 /// getValueAsListInit - This method looks up the specified field and returns
1568 /// its value as a ListInit, throwing an exception if the field does not exist
1569 /// or if the value is not the right type.
1571 ListInit *getValueAsListInit(StringRef FieldName) const;
1573 /// getValueAsListOfDefs - This method looks up the specified field and
1574 /// returns its value as a vector of records, throwing an exception if the
1575 /// field does not exist or if the value is not the right type.
1577 std::vector<Record*> getValueAsListOfDefs(StringRef FieldName) const;
1579 /// getValueAsListOfInts - This method looks up the specified field and
1580 /// returns its value as a vector of integers, throwing an exception if the
1581 /// field does not exist or if the value is not the right type.
1583 std::vector<int64_t> getValueAsListOfInts(StringRef FieldName) const;
1585 /// getValueAsListOfStrings - This method looks up the specified field and
1586 /// returns its value as a vector of strings, throwing an exception if the
1587 /// field does not exist or if the value is not the right type.
1589 std::vector<std::string> getValueAsListOfStrings(StringRef FieldName) const;
1591 /// getValueAsDef - This method looks up the specified field and returns its
1592 /// value as a Record, throwing an exception if the field does not exist or if
1593 /// the value is not the right type.
1595 Record *getValueAsDef(StringRef FieldName) const;
1597 /// getValueAsBit - This method looks up the specified field and returns its
1598 /// value as a bit, throwing an exception if the field does not exist or if
1599 /// the value is not the right type.
1601 bool getValueAsBit(StringRef FieldName) const;
1603 /// getValueAsBitOrUnset - This method looks up the specified field and
1604 /// returns its value as a bit. If the field is unset, sets Unset to true and
1607 bool getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const;
1609 /// getValueAsInt - This method looks up the specified field and returns its
1610 /// value as an int64_t, throwing an exception if the field does not exist or
1611 /// if the value is not the right type.
1613 int64_t getValueAsInt(StringRef FieldName) const;
1615 /// getValueAsDag - This method looks up the specified field and returns its
1616 /// value as an Dag, throwing an exception if the field does not exist or if
1617 /// the value is not the right type.
1619 DagInit *getValueAsDag(StringRef FieldName) const;
1622 raw_ostream &operator<<(raw_ostream &OS, const Record &R);
1625 Record Rec; // Placeholder for template args and Name.
1626 typedef std::vector<Record*> RecordVector;
1627 RecordVector DefPrototypes;
1631 MultiClass(const std::string &Name, SMLoc Loc, RecordKeeper &Records) :
1632 Rec(Name, Loc, Records) {}
1635 class RecordKeeper {
1636 std::map<std::string, Record*> Classes, Defs;
1640 for (std::map<std::string, Record*>::iterator I = Classes.begin(),
1641 E = Classes.end(); I != E; ++I)
1643 for (std::map<std::string, Record*>::iterator I = Defs.begin(),
1644 E = Defs.end(); I != E; ++I)
1648 const std::map<std::string, Record*> &getClasses() const { return Classes; }
1649 const std::map<std::string, Record*> &getDefs() const { return Defs; }
1651 Record *getClass(const std::string &Name) const {
1652 std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
1653 return I == Classes.end() ? 0 : I->second;
1655 Record *getDef(const std::string &Name) const {
1656 std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
1657 return I == Defs.end() ? 0 : I->second;
1659 void addClass(Record *R) {
1660 bool Ins = Classes.insert(std::make_pair(R->getName(), R)).second;
1662 assert(Ins && "Class already exists");
1664 void addDef(Record *R) {
1665 bool Ins = Defs.insert(std::make_pair(R->getName(), R)).second;
1667 assert(Ins && "Record already exists");
1670 /// removeClass - Remove, but do not delete, the specified record.
1672 void removeClass(const std::string &Name) {
1673 assert(Classes.count(Name) && "Class does not exist!");
1674 Classes.erase(Name);
1676 /// removeDef - Remove, but do not delete, the specified record.
1678 void removeDef(const std::string &Name) {
1679 assert(Defs.count(Name) && "Def does not exist!");
1683 //===--------------------------------------------------------------------===//
1684 // High-level helper methods, useful for tablegen backends...
1686 /// getAllDerivedDefinitions - This method returns all concrete definitions
1687 /// that derive from the specified class name. If a class with the specified
1688 /// name does not exist, an exception is thrown.
1689 std::vector<Record*>
1690 getAllDerivedDefinitions(const std::string &ClassName) const;
1695 /// LessRecord - Sorting predicate to sort record pointers by name.
1698 bool operator()(const Record *Rec1, const Record *Rec2) const {
1699 return StringRef(Rec1->getName()).compare_numeric(Rec2->getName()) < 0;
1703 /// LessRecordByID - Sorting predicate to sort record pointers by their
1704 /// unique ID. If you just need a deterministic order, use this, since it
1705 /// just compares two `unsigned`; the other sorting predicates require
1706 /// string manipulation.
1707 struct LessRecordByID {
1708 bool operator()(const Record *LHS, const Record *RHS) const {
1709 return LHS->getID() < RHS->getID();
1713 /// LessRecordFieldName - Sorting predicate to sort record pointers by their
1716 struct LessRecordFieldName {
1717 bool operator()(const Record *Rec1, const Record *Rec2) const {
1718 return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name");
1722 raw_ostream &operator<<(raw_ostream &OS, const RecordKeeper &RK);
1724 /// QualifyName - Return an Init with a qualifier prefix referring
1725 /// to CurRec's name.
1726 Init *QualifyName(Record &CurRec, MultiClass *CurMultiClass,
1727 Init *Name, const std::string &Scoper);
1729 /// QualifyName - Return an Init with a qualifier prefix referring
1730 /// to CurRec's name.
1731 Init *QualifyName(Record &CurRec, MultiClass *CurMultiClass,
1732 const std::string &Name, const std::string &Scoper);
1734 } // End llvm namespace