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/SourceMgr.h"
23 #include "llvm/Support/DataTypes.h"
24 #include "llvm/Support/ErrorHandling.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);
131 public: // These methods should only be called by subclasses of RecTy.
132 // baseClassOf - These virtual methods should be overloaded to return true iff
133 // all values of type 'RHS' can be converted to the 'this' type.
134 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
135 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
136 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
137 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
138 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
139 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
140 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
143 inline raw_ostream &operator<<(raw_ostream &OS, const RecTy &Ty) {
149 /// BitRecTy - 'bit' - Represent a single bit
151 class BitRecTy : public RecTy {
152 static BitRecTy Shared;
153 BitRecTy() : RecTy(BitRecTyKind) {}
155 static bool classof(const RecTy *RT) {
156 return RT->getRecTyKind() == BitRecTyKind;
159 static BitRecTy *get() { return &Shared; }
161 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
162 virtual Init *convertValue( BitInit *BI) { return (Init*)BI; }
163 virtual Init *convertValue( BitsInit *BI);
164 virtual Init *convertValue( IntInit *II);
165 virtual Init *convertValue(StringInit *SI) { return 0; }
166 virtual Init *convertValue( ListInit *LI) { return 0; }
167 virtual Init *convertValue(VarBitInit *VB) { return (Init*)VB; }
168 virtual Init *convertValue( DefInit *DI) { return 0; }
169 virtual Init *convertValue( DagInit *DI) { return 0; }
170 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
171 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
172 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
173 virtual Init *convertValue( TypedInit *TI);
174 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
175 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
177 virtual std::string getAsString() const { return "bit"; }
179 virtual bool typeIsConvertibleTo(const RecTy *RHS) const {
180 return RHS->baseClassOf(this);
182 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
183 virtual bool baseClassOf(const BitsRecTy *RHS) const;
184 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
185 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
186 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
187 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
188 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
193 // BitsRecTy - 'bits<n>' - Represent a fixed number of bits
194 /// BitsRecTy - 'bits<n>' - Represent a fixed number of bits
196 class BitsRecTy : public RecTy {
198 explicit BitsRecTy(unsigned Sz) : RecTy(BitsRecTyKind), Size(Sz) {}
200 static bool classof(const RecTy *RT) {
201 return RT->getRecTyKind() == BitsRecTyKind;
204 static BitsRecTy *get(unsigned Sz);
206 unsigned getNumBits() const { return Size; }
208 virtual Init *convertValue( UnsetInit *UI);
209 virtual Init *convertValue( BitInit *UI);
210 virtual Init *convertValue( BitsInit *BI);
211 virtual Init *convertValue( IntInit *II);
212 virtual Init *convertValue(StringInit *SI) { return 0; }
213 virtual Init *convertValue( ListInit *LI) { return 0; }
214 virtual Init *convertValue(VarBitInit *VB) { return 0; }
215 virtual Init *convertValue( DefInit *DI) { return 0; }
216 virtual Init *convertValue( DagInit *DI) { return 0; }
217 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
218 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
219 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
220 virtual Init *convertValue( TypedInit *TI);
221 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
222 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
224 virtual std::string getAsString() const;
226 virtual bool typeIsConvertibleTo(const RecTy *RHS) const {
227 return RHS->baseClassOf(this);
229 virtual bool baseClassOf(const BitRecTy *RHS) const { return Size == 1; }
230 virtual bool baseClassOf(const BitsRecTy *RHS) const {
231 return RHS->Size == Size;
233 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
234 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
235 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
236 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
237 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
242 /// IntRecTy - 'int' - Represent an integer value of no particular size
244 class IntRecTy : public RecTy {
245 static IntRecTy Shared;
246 IntRecTy() : RecTy(IntRecTyKind) {}
248 static bool classof(const RecTy *RT) {
249 return RT->getRecTyKind() == IntRecTyKind;
252 static IntRecTy *get() { return &Shared; }
254 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
255 virtual Init *convertValue( BitInit *BI);
256 virtual Init *convertValue( BitsInit *BI);
257 virtual Init *convertValue( IntInit *II) { return (Init*)II; }
258 virtual Init *convertValue(StringInit *SI) { return 0; }
259 virtual Init *convertValue( ListInit *LI) { return 0; }
260 virtual Init *convertValue(VarBitInit *VB) { return 0; }
261 virtual Init *convertValue( DefInit *DI) { return 0; }
262 virtual Init *convertValue( DagInit *DI) { return 0; }
263 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
264 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
265 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
266 virtual Init *convertValue( TypedInit *TI);
267 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
268 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
270 virtual std::string getAsString() const { return "int"; }
272 virtual bool typeIsConvertibleTo(const RecTy *RHS) const {
273 return RHS->baseClassOf(this);
276 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
277 virtual bool baseClassOf(const BitsRecTy *RHS) const { return true; }
278 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
279 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
280 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
281 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
282 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
286 /// StringRecTy - 'string' - Represent an string value
288 class StringRecTy : public RecTy {
289 static StringRecTy Shared;
290 StringRecTy() : RecTy(StringRecTyKind) {}
292 static bool classof(const RecTy *RT) {
293 return RT->getRecTyKind() == StringRecTyKind;
296 static StringRecTy *get() { return &Shared; }
298 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
299 virtual Init *convertValue( BitInit *BI) { return 0; }
300 virtual Init *convertValue( BitsInit *BI) { return 0; }
301 virtual Init *convertValue( IntInit *II) { return 0; }
302 virtual Init *convertValue(StringInit *SI) { return (Init*)SI; }
303 virtual Init *convertValue( ListInit *LI) { return 0; }
304 virtual Init *convertValue( UnOpInit *BO);
305 virtual Init *convertValue( BinOpInit *BO);
306 virtual Init *convertValue( TernOpInit *BO) { return RecTy::convertValue(BO);}
308 virtual Init *convertValue(VarBitInit *VB) { return 0; }
309 virtual Init *convertValue( DefInit *DI) { return 0; }
310 virtual Init *convertValue( DagInit *DI) { return 0; }
311 virtual Init *convertValue( TypedInit *TI);
312 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
313 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
315 virtual std::string getAsString() const { return "string"; }
317 virtual bool typeIsConvertibleTo(const RecTy *RHS) const {
318 return RHS->baseClassOf(this);
321 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
322 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
323 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
324 virtual bool baseClassOf(const StringRecTy *RHS) const { return true; }
325 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
326 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
327 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
330 // ListRecTy - 'list<Ty>' - Represent a list of values, all of which must be of
331 // the specified type.
332 /// ListRecTy - 'list<Ty>' - Represent a list of values, all of which must
333 /// be of the specified type.
335 class ListRecTy : public RecTy {
337 explicit ListRecTy(RecTy *T) : RecTy(ListRecTyKind), Ty(T) {}
338 friend ListRecTy *RecTy::getListTy();
340 static bool classof(const RecTy *RT) {
341 return RT->getRecTyKind() == ListRecTyKind;
344 static ListRecTy *get(RecTy *T) { return T->getListTy(); }
345 RecTy *getElementType() const { return Ty; }
347 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
348 virtual Init *convertValue( BitInit *BI) { return 0; }
349 virtual Init *convertValue( BitsInit *BI) { return 0; }
350 virtual Init *convertValue( IntInit *II) { return 0; }
351 virtual Init *convertValue(StringInit *SI) { return 0; }
352 virtual Init *convertValue( ListInit *LI);
353 virtual Init *convertValue(VarBitInit *VB) { return 0; }
354 virtual Init *convertValue( DefInit *DI) { return 0; }
355 virtual Init *convertValue( DagInit *DI) { return 0; }
356 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
357 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
358 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
359 virtual Init *convertValue( TypedInit *TI);
360 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
361 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
363 virtual std::string getAsString() const;
365 virtual bool typeIsConvertibleTo(const RecTy *RHS) const {
366 return RHS->baseClassOf(this);
369 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
370 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
371 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
372 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
373 virtual bool baseClassOf(const ListRecTy *RHS) const {
374 return RHS->getElementType()->typeIsConvertibleTo(Ty);
376 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
377 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
380 /// DagRecTy - 'dag' - Represent a dag fragment
382 class DagRecTy : public RecTy {
383 static DagRecTy Shared;
384 DagRecTy() : RecTy(DagRecTyKind) {}
386 static bool classof(const RecTy *RT) {
387 return RT->getRecTyKind() == DagRecTyKind;
390 static DagRecTy *get() { return &Shared; }
392 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
393 virtual Init *convertValue( BitInit *BI) { return 0; }
394 virtual Init *convertValue( BitsInit *BI) { return 0; }
395 virtual Init *convertValue( IntInit *II) { return 0; }
396 virtual Init *convertValue(StringInit *SI) { return 0; }
397 virtual Init *convertValue( ListInit *LI) { return 0; }
398 virtual Init *convertValue(VarBitInit *VB) { return 0; }
399 virtual Init *convertValue( DefInit *DI) { return 0; }
400 virtual Init *convertValue( UnOpInit *BO);
401 virtual Init *convertValue( BinOpInit *BO);
402 virtual Init *convertValue( TernOpInit *BO) { return RecTy::convertValue(BO);}
403 virtual Init *convertValue( DagInit *CI) { return (Init*)CI; }
404 virtual Init *convertValue( TypedInit *TI);
405 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
406 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
408 virtual std::string getAsString() const { return "dag"; }
410 virtual bool typeIsConvertibleTo(const RecTy *RHS) const {
411 return RHS->baseClassOf(this);
414 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
415 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
416 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
417 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
418 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
419 virtual bool baseClassOf(const DagRecTy *RHS) const { return true; }
420 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
424 /// RecordRecTy - '[classname]' - Represent an instance of a class, such as:
427 class RecordRecTy : public RecTy {
429 explicit RecordRecTy(Record *R) : RecTy(RecordRecTyKind), Rec(R) {}
432 static bool classof(const RecTy *RT) {
433 return RT->getRecTyKind() == RecordRecTyKind;
436 static RecordRecTy *get(Record *R);
438 Record *getRecord() const { return Rec; }
440 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
441 virtual Init *convertValue( BitInit *BI) { return 0; }
442 virtual Init *convertValue( BitsInit *BI) { return 0; }
443 virtual Init *convertValue( IntInit *II) { return 0; }
444 virtual Init *convertValue(StringInit *SI) { return 0; }
445 virtual Init *convertValue( ListInit *LI) { return 0; }
446 virtual Init *convertValue(VarBitInit *VB) { return 0; }
447 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
448 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
449 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
450 virtual Init *convertValue( DefInit *DI);
451 virtual Init *convertValue( DagInit *DI) { return 0; }
452 virtual Init *convertValue( TypedInit *VI);
453 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
454 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
456 virtual std::string getAsString() const;
458 virtual bool typeIsConvertibleTo(const RecTy *RHS) const {
459 return RHS->baseClassOf(this);
461 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
462 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
463 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
464 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
465 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
466 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
467 virtual bool baseClassOf(const RecordRecTy *RHS) const;
470 /// resolveTypes - Find a common type that T1 and T2 convert to.
471 /// Return 0 if no such type exists.
473 RecTy *resolveTypes(RecTy *T1, RecTy *T2);
475 //===----------------------------------------------------------------------===//
476 // Initializer Classes
477 //===----------------------------------------------------------------------===//
481 /// \brief Discriminator enum (for isa<>, dyn_cast<>, et al.)
483 /// This enum is laid out by a preorder traversal of the inheritance
484 /// hierarchy, and does not contain an entry for abstract classes, as per
485 /// the recommendation in docs/HowToSetUpLLVMStyleRTTI.rst.
487 /// We also explicitly include "first" and "last" values for each
488 /// interior node of the inheritance tree, to make it easier to read the
489 /// corresponding classof().
491 /// We could pack these a bit tighter by not having the IK_FirstXXXInit
492 /// and IK_LastXXXInit be their own values, but that would degrade
493 /// readability for really no benefit.
510 IK_VarListElementInit,
518 Init(const Init &) LLVM_DELETED_FUNCTION;
519 Init &operator=(const Init &) LLVM_DELETED_FUNCTION;
520 virtual void anchor();
523 InitKind getKind() const { return Kind; }
526 explicit Init(InitKind K) : Kind(K) {}
531 /// isComplete - This virtual method should be overridden by values that may
532 /// not be completely specified yet.
533 virtual bool isComplete() const { return true; }
535 /// print - Print out this value.
536 void print(raw_ostream &OS) const { OS << getAsString(); }
538 /// getAsString - Convert this value to a string form.
539 virtual std::string getAsString() const = 0;
540 /// getAsUnquotedString - Convert this value to a string form,
541 /// without adding quote markers. This primaruly affects
542 /// StringInits where we will not surround the string value with
544 virtual std::string getAsUnquotedString() const { return getAsString(); }
546 /// dump - Debugging method that may be called through a debugger, just
547 /// invokes print on stderr.
550 /// convertInitializerTo - This virtual function is a simple call-back
551 /// function that should be overridden to call the appropriate
552 /// RecTy::convertValue method.
554 virtual Init *convertInitializerTo(RecTy *Ty) const = 0;
556 /// convertInitializerBitRange - This method is used to implement the bitrange
557 /// selection operator. Given an initializer, it selects the specified bits
558 /// out, returning them as a new init of bits type. If it is not legal to use
559 /// the bit subscript operator on this initializer, return null.
562 convertInitializerBitRange(const std::vector<unsigned> &Bits) const {
566 /// convertInitListSlice - This method is used to implement the list slice
567 /// selection operator. Given an initializer, it selects the specified list
568 /// elements, returning them as a new init of list type. If it is not legal
569 /// to take a slice of this, return null.
572 convertInitListSlice(const std::vector<unsigned> &Elements) const {
576 /// getFieldType - This method is used to implement the FieldInit class.
577 /// Implementors of this method should return the type of the named field if
578 /// they are of record type.
580 virtual RecTy *getFieldType(const std::string &FieldName) const { return 0; }
582 /// getFieldInit - This method complements getFieldType to return the
583 /// initializer for the specified field. If getFieldType returns non-null
584 /// this method should return non-null, otherwise it returns null.
586 virtual Init *getFieldInit(Record &R, const RecordVal *RV,
587 const std::string &FieldName) const {
591 /// resolveReferences - This method is used by classes that refer to other
592 /// variables which may not be defined at the time the expression is formed.
593 /// If a value is set for the variable later, this method will be called on
594 /// users of the value to allow the value to propagate out.
596 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const {
597 return const_cast<Init *>(this);
600 /// getBit - This method is used to return the initializer for the specified
602 virtual Init *getBit(unsigned Bit) const = 0;
604 /// getBitVar - This method is used to retrieve the initializer for bit
605 /// reference. For non-VarBitInit, it simply returns itself.
606 virtual Init *getBitVar() const { return const_cast<Init*>(this); }
608 /// getBitNum - This method is used to retrieve the bit number of a bit
609 /// reference. For non-VarBitInit, it simply returns 0.
610 virtual unsigned getBitNum() const { return 0; }
613 inline raw_ostream &operator<<(raw_ostream &OS, const Init &I) {
614 I.print(OS); return OS;
617 /// TypedInit - This is the common super-class of types that have a specific,
620 class TypedInit : public Init {
623 TypedInit(const TypedInit &Other) LLVM_DELETED_FUNCTION;
624 TypedInit &operator=(const TypedInit &Other) LLVM_DELETED_FUNCTION;
627 explicit TypedInit(InitKind K, RecTy *T) : Init(K), Ty(T) {}
630 static bool classof(const Init *I) {
631 return I->getKind() >= IK_FirstTypedInit &&
632 I->getKind() <= IK_LastTypedInit;
634 RecTy *getType() const { return Ty; }
637 convertInitializerBitRange(const std::vector<unsigned> &Bits) const;
639 convertInitListSlice(const std::vector<unsigned> &Elements) const;
641 /// getFieldType - This method is used to implement the FieldInit class.
642 /// Implementors of this method should return the type of the named field if
643 /// they are of record type.
645 virtual RecTy *getFieldType(const std::string &FieldName) const;
647 /// resolveListElementReference - This method is used to implement
648 /// VarListElementInit::resolveReferences. If the list element is resolvable
649 /// now, we return the resolved value, otherwise we return null.
650 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
651 unsigned Elt) const = 0;
655 /// UnsetInit - ? - Represents an uninitialized value
657 class UnsetInit : public Init {
658 UnsetInit() : Init(IK_UnsetInit) {}
659 UnsetInit(const UnsetInit &) LLVM_DELETED_FUNCTION;
660 UnsetInit &operator=(const UnsetInit &Other) LLVM_DELETED_FUNCTION;
661 virtual void anchor();
664 static bool classof(const Init *I) {
665 return I->getKind() == IK_UnsetInit;
667 static UnsetInit *get();
669 virtual Init *convertInitializerTo(RecTy *Ty) const {
670 return Ty->convertValue(const_cast<UnsetInit *>(this));
673 virtual Init *getBit(unsigned Bit) const {
674 return const_cast<UnsetInit*>(this);
677 virtual bool isComplete() const { return false; }
678 virtual std::string getAsString() const { return "?"; }
682 /// BitInit - true/false - Represent a concrete initializer for a bit.
684 class BitInit : public Init {
687 explicit BitInit(bool V) : Init(IK_BitInit), Value(V) {}
688 BitInit(const BitInit &Other) LLVM_DELETED_FUNCTION;
689 BitInit &operator=(BitInit &Other) LLVM_DELETED_FUNCTION;
690 virtual void anchor();
693 static bool classof(const Init *I) {
694 return I->getKind() == IK_BitInit;
696 static BitInit *get(bool V);
698 bool getValue() const { return Value; }
700 virtual Init *convertInitializerTo(RecTy *Ty) const {
701 return Ty->convertValue(const_cast<BitInit *>(this));
704 virtual Init *getBit(unsigned Bit) const {
705 assert(Bit < 1 && "Bit index out of range!");
706 return const_cast<BitInit*>(this);
709 virtual std::string getAsString() const { return Value ? "1" : "0"; }
712 /// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
713 /// It contains a vector of bits, whose size is determined by the type.
715 class BitsInit : public Init, public FoldingSetNode {
716 std::vector<Init*> Bits;
718 BitsInit(ArrayRef<Init *> Range)
719 : Init(IK_BitsInit), Bits(Range.begin(), Range.end()) {}
721 BitsInit(const BitsInit &Other) LLVM_DELETED_FUNCTION;
722 BitsInit &operator=(const BitsInit &Other) LLVM_DELETED_FUNCTION;
725 static bool classof(const Init *I) {
726 return I->getKind() == IK_BitsInit;
728 static BitsInit *get(ArrayRef<Init *> Range);
730 void Profile(FoldingSetNodeID &ID) const;
732 unsigned getNumBits() const { return Bits.size(); }
734 virtual Init *convertInitializerTo(RecTy *Ty) const {
735 return Ty->convertValue(const_cast<BitsInit *>(this));
738 convertInitializerBitRange(const std::vector<unsigned> &Bits) const;
740 virtual bool isComplete() const {
741 for (unsigned i = 0; i != getNumBits(); ++i)
742 if (!getBit(i)->isComplete()) return false;
745 bool allInComplete() const {
746 for (unsigned i = 0; i != getNumBits(); ++i)
747 if (getBit(i)->isComplete()) return false;
750 virtual std::string getAsString() const;
752 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
754 virtual Init *getBit(unsigned Bit) const {
755 assert(Bit < Bits.size() && "Bit index out of range!");
761 /// IntInit - 7 - Represent an initalization by a literal integer value.
763 class IntInit : public TypedInit {
766 explicit IntInit(int64_t V)
767 : TypedInit(IK_IntInit, IntRecTy::get()), Value(V) {}
769 IntInit(const IntInit &Other) LLVM_DELETED_FUNCTION;
770 IntInit &operator=(const IntInit &Other) LLVM_DELETED_FUNCTION;
773 static bool classof(const Init *I) {
774 return I->getKind() == IK_IntInit;
776 static IntInit *get(int64_t V);
778 int64_t getValue() const { return Value; }
780 virtual Init *convertInitializerTo(RecTy *Ty) const {
781 return Ty->convertValue(const_cast<IntInit *>(this));
784 convertInitializerBitRange(const std::vector<unsigned> &Bits) const;
786 virtual std::string getAsString() const;
788 /// resolveListElementReference - This method is used to implement
789 /// VarListElementInit::resolveReferences. If the list element is resolvable
790 /// now, we return the resolved value, otherwise we return null.
791 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
792 unsigned Elt) const {
793 llvm_unreachable("Illegal element reference off int");
796 virtual Init *getBit(unsigned Bit) const {
797 return BitInit::get((Value & (1ULL << Bit)) != 0);
802 /// StringInit - "foo" - Represent an initialization by a string value.
804 class StringInit : public TypedInit {
807 explicit StringInit(const std::string &V)
808 : TypedInit(IK_StringInit, StringRecTy::get()), Value(V) {}
810 StringInit(const StringInit &Other) LLVM_DELETED_FUNCTION;
811 StringInit &operator=(const StringInit &Other) LLVM_DELETED_FUNCTION;
812 virtual void anchor();
815 static bool classof(const Init *I) {
816 return I->getKind() == IK_StringInit;
818 static StringInit *get(StringRef);
820 const std::string &getValue() const { return Value; }
822 virtual Init *convertInitializerTo(RecTy *Ty) const {
823 return Ty->convertValue(const_cast<StringInit *>(this));
826 virtual std::string getAsString() const { return "\"" + Value + "\""; }
827 virtual std::string getAsUnquotedString() const { return Value; }
829 /// resolveListElementReference - This method is used to implement
830 /// VarListElementInit::resolveReferences. If the list element is resolvable
831 /// now, we return the resolved value, otherwise we return null.
832 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
833 unsigned Elt) const {
834 llvm_unreachable("Illegal element reference off string");
837 virtual Init *getBit(unsigned Bit) const {
838 llvm_unreachable("Illegal bit reference off string");
842 /// ListInit - [AL, AH, CL] - Represent a list of defs
844 class ListInit : public TypedInit, public FoldingSetNode {
845 std::vector<Init*> Values;
847 typedef std::vector<Init*>::const_iterator const_iterator;
850 explicit ListInit(ArrayRef<Init *> Range, RecTy *EltTy)
851 : TypedInit(IK_ListInit, ListRecTy::get(EltTy)),
852 Values(Range.begin(), Range.end()) {}
854 ListInit(const ListInit &Other) LLVM_DELETED_FUNCTION;
855 ListInit &operator=(const ListInit &Other) LLVM_DELETED_FUNCTION;
858 static bool classof(const Init *I) {
859 return I->getKind() == IK_ListInit;
861 static ListInit *get(ArrayRef<Init *> Range, RecTy *EltTy);
863 void Profile(FoldingSetNodeID &ID) const;
865 unsigned getSize() const { return Values.size(); }
866 Init *getElement(unsigned i) const {
867 assert(i < Values.size() && "List element index out of range!");
871 Record *getElementAsRecord(unsigned i) const;
874 convertInitListSlice(const std::vector<unsigned> &Elements) const;
876 virtual Init *convertInitializerTo(RecTy *Ty) const {
877 return Ty->convertValue(const_cast<ListInit *>(this));
880 /// resolveReferences - This method is used by classes that refer to other
881 /// variables which may not be defined at the time they expression is formed.
882 /// If a value is set for the variable later, this method will be called on
883 /// users of the value to allow the value to propagate out.
885 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
887 virtual std::string getAsString() const;
889 ArrayRef<Init*> getValues() const { return Values; }
891 inline const_iterator begin() const { return Values.begin(); }
892 inline const_iterator end () const { return Values.end(); }
894 inline size_t size () const { return Values.size(); }
895 inline bool empty() const { return Values.empty(); }
897 /// resolveListElementReference - This method is used to implement
898 /// VarListElementInit::resolveReferences. If the list element is resolvable
899 /// now, we return the resolved value, otherwise we return null.
900 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
903 virtual Init *getBit(unsigned Bit) const {
904 llvm_unreachable("Illegal bit reference off list");
909 /// OpInit - Base class for operators
911 class OpInit : public TypedInit {
912 OpInit(const OpInit &Other) LLVM_DELETED_FUNCTION;
913 OpInit &operator=(OpInit &Other) LLVM_DELETED_FUNCTION;
916 explicit OpInit(InitKind K, RecTy *Type) : TypedInit(K, Type) {}
919 static bool classof(const Init *I) {
920 return I->getKind() >= IK_FirstOpInit &&
921 I->getKind() <= IK_LastOpInit;
923 // Clone - Clone this operator, replacing arguments with the new list
924 virtual OpInit *clone(std::vector<Init *> &Operands) const = 0;
926 virtual int getNumOperands() const = 0;
927 virtual Init *getOperand(int i) const = 0;
929 // Fold - If possible, fold this to a simpler init. Return this if not
931 virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const = 0;
933 virtual Init *convertInitializerTo(RecTy *Ty) const {
934 return Ty->convertValue(const_cast<OpInit *>(this));
937 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
940 virtual Init *getBit(unsigned Bit) const;
944 /// UnOpInit - !op (X) - Transform an init.
946 class UnOpInit : public OpInit {
948 enum UnaryOp { CAST, HEAD, TAIL, EMPTY };
953 UnOpInit(UnaryOp opc, Init *lhs, RecTy *Type)
954 : OpInit(IK_UnOpInit, Type), Opc(opc), LHS(lhs) {}
956 UnOpInit(const UnOpInit &Other) LLVM_DELETED_FUNCTION;
957 UnOpInit &operator=(const UnOpInit &Other) LLVM_DELETED_FUNCTION;
960 static bool classof(const Init *I) {
961 return I->getKind() == IK_UnOpInit;
963 static UnOpInit *get(UnaryOp opc, Init *lhs, RecTy *Type);
965 // Clone - Clone this operator, replacing arguments with the new list
966 virtual OpInit *clone(std::vector<Init *> &Operands) const {
967 assert(Operands.size() == 1 &&
968 "Wrong number of operands for unary operation");
969 return UnOpInit::get(getOpcode(), *Operands.begin(), getType());
972 virtual int getNumOperands() const { return 1; }
973 virtual Init *getOperand(int i) const {
974 assert(i == 0 && "Invalid operand id for unary operator");
978 UnaryOp getOpcode() const { return Opc; }
979 Init *getOperand() const { return LHS; }
981 // Fold - If possible, fold this to a simpler init. Return this if not
983 virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const;
985 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
987 virtual std::string getAsString() const;
990 /// BinOpInit - !op (X, Y) - Combine two inits.
992 class BinOpInit : public OpInit {
994 enum BinaryOp { SHL, SRA, SRL, STRCONCAT, CONCAT, EQ };
999 BinOpInit(BinaryOp opc, Init *lhs, Init *rhs, RecTy *Type) :
1000 OpInit(IK_BinOpInit, Type), Opc(opc), LHS(lhs), RHS(rhs) {}
1002 BinOpInit(const BinOpInit &Other) LLVM_DELETED_FUNCTION;
1003 BinOpInit &operator=(const BinOpInit &Other) LLVM_DELETED_FUNCTION;
1006 static bool classof(const Init *I) {
1007 return I->getKind() == IK_BinOpInit;
1009 static BinOpInit *get(BinaryOp opc, Init *lhs, Init *rhs,
1012 // Clone - Clone this operator, replacing arguments with the new list
1013 virtual OpInit *clone(std::vector<Init *> &Operands) const {
1014 assert(Operands.size() == 2 &&
1015 "Wrong number of operands for binary operation");
1016 return BinOpInit::get(getOpcode(), Operands[0], Operands[1], getType());
1019 virtual int getNumOperands() const { return 2; }
1020 virtual Init *getOperand(int i) const {
1021 assert((i == 0 || i == 1) && "Invalid operand id for binary operator");
1029 BinaryOp getOpcode() const { return Opc; }
1030 Init *getLHS() const { return LHS; }
1031 Init *getRHS() const { return RHS; }
1033 // Fold - If possible, fold this to a simpler init. Return this if not
1034 // possible to fold.
1035 virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const;
1037 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
1039 virtual std::string getAsString() const;
1042 /// TernOpInit - !op (X, Y, Z) - Combine two inits.
1044 class TernOpInit : public OpInit {
1046 enum TernaryOp { SUBST, FOREACH, IF };
1049 Init *LHS, *MHS, *RHS;
1051 TernOpInit(TernaryOp opc, Init *lhs, Init *mhs, Init *rhs,
1053 OpInit(IK_TernOpInit, Type), Opc(opc), LHS(lhs), MHS(mhs), RHS(rhs) {}
1055 TernOpInit(const TernOpInit &Other) LLVM_DELETED_FUNCTION;
1056 TernOpInit &operator=(const TernOpInit &Other) LLVM_DELETED_FUNCTION;
1059 static bool classof(const Init *I) {
1060 return I->getKind() == IK_TernOpInit;
1062 static TernOpInit *get(TernaryOp opc, Init *lhs,
1063 Init *mhs, Init *rhs,
1066 // Clone - Clone this operator, replacing arguments with the new list
1067 virtual OpInit *clone(std::vector<Init *> &Operands) const {
1068 assert(Operands.size() == 3 &&
1069 "Wrong number of operands for ternary operation");
1070 return TernOpInit::get(getOpcode(), Operands[0], Operands[1], Operands[2],
1074 virtual int getNumOperands() const { return 3; }
1075 virtual Init *getOperand(int i) const {
1076 assert((i == 0 || i == 1 || i == 2) &&
1077 "Invalid operand id for ternary operator");
1080 } else if (i == 1) {
1087 TernaryOp getOpcode() const { return Opc; }
1088 Init *getLHS() const { return LHS; }
1089 Init *getMHS() const { return MHS; }
1090 Init *getRHS() const { return RHS; }
1092 // Fold - If possible, fold this to a simpler init. Return this if not
1093 // possible to fold.
1094 virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const;
1096 virtual bool isComplete() const { return false; }
1098 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
1100 virtual std::string getAsString() const;
1104 /// VarInit - 'Opcode' - Represent a reference to an entire variable object.
1106 class VarInit : public TypedInit {
1109 explicit VarInit(const std::string &VN, RecTy *T)
1110 : TypedInit(IK_VarInit, T), VarName(StringInit::get(VN)) {}
1111 explicit VarInit(Init *VN, RecTy *T)
1112 : TypedInit(IK_VarInit, T), VarName(VN) {}
1114 VarInit(const VarInit &Other) LLVM_DELETED_FUNCTION;
1115 VarInit &operator=(const VarInit &Other) LLVM_DELETED_FUNCTION;
1118 static bool classof(const Init *I) {
1119 return I->getKind() == IK_VarInit;
1121 static VarInit *get(const std::string &VN, RecTy *T);
1122 static VarInit *get(Init *VN, RecTy *T);
1124 virtual Init *convertInitializerTo(RecTy *Ty) const {
1125 return Ty->convertValue(const_cast<VarInit *>(this));
1128 const std::string &getName() const;
1129 Init *getNameInit() const { return VarName; }
1130 std::string getNameInitAsString() const {
1131 return getNameInit()->getAsUnquotedString();
1134 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
1135 unsigned Elt) const;
1137 virtual RecTy *getFieldType(const std::string &FieldName) const;
1138 virtual Init *getFieldInit(Record &R, const RecordVal *RV,
1139 const std::string &FieldName) const;
1141 /// resolveReferences - This method is used by classes that refer to other
1142 /// variables which may not be defined at the time they expression is formed.
1143 /// If a value is set for the variable later, this method will be called on
1144 /// users of the value to allow the value to propagate out.
1146 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
1148 virtual Init *getBit(unsigned Bit) const;
1150 virtual std::string getAsString() const { return getName(); }
1154 /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
1156 class VarBitInit : public Init {
1160 VarBitInit(TypedInit *T, unsigned B) : Init(IK_VarBitInit), TI(T), Bit(B) {
1161 assert(T->getType() &&
1162 (isa<IntRecTy>(T->getType()) ||
1163 (isa<BitsRecTy>(T->getType()) &&
1164 cast<BitsRecTy>(T->getType())->getNumBits() > B)) &&
1165 "Illegal VarBitInit expression!");
1168 VarBitInit(const VarBitInit &Other) LLVM_DELETED_FUNCTION;
1169 VarBitInit &operator=(const VarBitInit &Other) LLVM_DELETED_FUNCTION;
1172 static bool classof(const Init *I) {
1173 return I->getKind() == IK_VarBitInit;
1175 static VarBitInit *get(TypedInit *T, unsigned B);
1177 virtual Init *convertInitializerTo(RecTy *Ty) const {
1178 return Ty->convertValue(const_cast<VarBitInit *>(this));
1181 virtual Init *getBitVar() const { return TI; }
1182 virtual unsigned getBitNum() const { return Bit; }
1184 virtual std::string getAsString() const;
1185 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
1187 virtual Init *getBit(unsigned B) const {
1188 assert(B < 1 && "Bit index out of range!");
1189 return const_cast<VarBitInit*>(this);
1193 /// VarListElementInit - List[4] - Represent access to one element of a var or
1195 class VarListElementInit : public TypedInit {
1199 VarListElementInit(TypedInit *T, unsigned E)
1200 : TypedInit(IK_VarListElementInit,
1201 cast<ListRecTy>(T->getType())->getElementType()),
1203 assert(T->getType() && isa<ListRecTy>(T->getType()) &&
1204 "Illegal VarBitInit expression!");
1207 VarListElementInit(const VarListElementInit &Other) LLVM_DELETED_FUNCTION;
1208 void operator=(const VarListElementInit &Other) LLVM_DELETED_FUNCTION;
1211 static bool classof(const Init *I) {
1212 return I->getKind() == IK_VarListElementInit;
1214 static VarListElementInit *get(TypedInit *T, unsigned E);
1216 virtual Init *convertInitializerTo(RecTy *Ty) const {
1217 return Ty->convertValue(const_cast<VarListElementInit *>(this));
1220 TypedInit *getVariable() const { return TI; }
1221 unsigned getElementNum() const { return Element; }
1223 /// resolveListElementReference - This method is used to implement
1224 /// VarListElementInit::resolveReferences. If the list element is resolvable
1225 /// now, we return the resolved value, otherwise we return null.
1226 virtual Init *resolveListElementReference(Record &R,
1227 const RecordVal *RV,
1228 unsigned Elt) const;
1230 virtual std::string getAsString() const;
1231 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
1233 virtual Init *getBit(unsigned Bit) const;
1236 /// DefInit - AL - Represent a reference to a 'def' in the description
1238 class DefInit : public TypedInit {
1241 DefInit(Record *D, RecordRecTy *T) : TypedInit(IK_DefInit, T), Def(D) {}
1242 friend class Record;
1244 DefInit(const DefInit &Other) LLVM_DELETED_FUNCTION;
1245 DefInit &operator=(const DefInit &Other) LLVM_DELETED_FUNCTION;
1248 static bool classof(const Init *I) {
1249 return I->getKind() == IK_DefInit;
1251 static DefInit *get(Record*);
1253 virtual Init *convertInitializerTo(RecTy *Ty) const {
1254 return Ty->convertValue(const_cast<DefInit *>(this));
1257 Record *getDef() const { return Def; }
1259 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
1261 virtual RecTy *getFieldType(const std::string &FieldName) const;
1262 virtual Init *getFieldInit(Record &R, const RecordVal *RV,
1263 const std::string &FieldName) const;
1265 virtual std::string getAsString() const;
1267 virtual Init *getBit(unsigned Bit) const {
1268 llvm_unreachable("Illegal bit reference off def");
1271 /// resolveListElementReference - This method is used to implement
1272 /// VarListElementInit::resolveReferences. If the list element is resolvable
1273 /// now, we return the resolved value, otherwise we return null.
1274 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
1275 unsigned Elt) const {
1276 llvm_unreachable("Illegal element reference off def");
1281 /// FieldInit - X.Y - Represent a reference to a subfield of a variable
1283 class FieldInit : public TypedInit {
1284 Init *Rec; // Record we are referring to
1285 std::string FieldName; // Field we are accessing
1287 FieldInit(Init *R, const std::string &FN)
1288 : TypedInit(IK_FieldInit, R->getFieldType(FN)), Rec(R), FieldName(FN) {
1289 assert(getType() && "FieldInit with non-record type!");
1292 FieldInit(const FieldInit &Other) LLVM_DELETED_FUNCTION;
1293 FieldInit &operator=(const FieldInit &Other) LLVM_DELETED_FUNCTION;
1296 static bool classof(const Init *I) {
1297 return I->getKind() == IK_FieldInit;
1299 static FieldInit *get(Init *R, const std::string &FN);
1300 static FieldInit *get(Init *R, const Init *FN);
1302 virtual Init *convertInitializerTo(RecTy *Ty) const {
1303 return Ty->convertValue(const_cast<FieldInit *>(this));
1306 virtual Init *getBit(unsigned Bit) const;
1308 virtual Init *resolveListElementReference(Record &R,
1309 const RecordVal *RV,
1310 unsigned Elt) const;
1312 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
1314 virtual std::string getAsString() const {
1315 return Rec->getAsString() + "." + FieldName;
1319 /// DagInit - (v a, b) - Represent a DAG tree value. DAG inits are required
1320 /// to have at least one value then a (possibly empty) list of arguments. Each
1321 /// argument can have a name associated with it.
1323 class DagInit : public TypedInit, public FoldingSetNode {
1325 std::string ValName;
1326 std::vector<Init*> Args;
1327 std::vector<std::string> ArgNames;
1329 DagInit(Init *V, const std::string &VN,
1330 ArrayRef<Init *> ArgRange,
1331 ArrayRef<std::string> NameRange)
1332 : TypedInit(IK_DagInit, DagRecTy::get()), Val(V), ValName(VN),
1333 Args(ArgRange.begin(), ArgRange.end()),
1334 ArgNames(NameRange.begin(), NameRange.end()) {}
1336 DagInit(const DagInit &Other) LLVM_DELETED_FUNCTION;
1337 DagInit &operator=(const DagInit &Other) LLVM_DELETED_FUNCTION;
1340 static bool classof(const Init *I) {
1341 return I->getKind() == IK_DagInit;
1343 static DagInit *get(Init *V, const std::string &VN,
1344 ArrayRef<Init *> ArgRange,
1345 ArrayRef<std::string> NameRange);
1346 static DagInit *get(Init *V, const std::string &VN,
1348 std::pair<Init*, std::string> > &args);
1350 void Profile(FoldingSetNodeID &ID) const;
1352 virtual Init *convertInitializerTo(RecTy *Ty) const {
1353 return Ty->convertValue(const_cast<DagInit *>(this));
1356 Init *getOperator() const { return Val; }
1358 const std::string &getName() const { return ValName; }
1360 unsigned getNumArgs() const { return Args.size(); }
1361 Init *getArg(unsigned Num) const {
1362 assert(Num < Args.size() && "Arg number out of range!");
1365 const std::string &getArgName(unsigned Num) const {
1366 assert(Num < ArgNames.size() && "Arg number out of range!");
1367 return ArgNames[Num];
1370 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
1372 virtual std::string getAsString() const;
1374 typedef std::vector<Init*>::const_iterator const_arg_iterator;
1375 typedef std::vector<std::string>::const_iterator const_name_iterator;
1377 inline const_arg_iterator arg_begin() const { return Args.begin(); }
1378 inline const_arg_iterator arg_end () const { return Args.end(); }
1380 inline size_t arg_size () const { return Args.size(); }
1381 inline bool arg_empty() const { return Args.empty(); }
1383 inline const_name_iterator name_begin() const { return ArgNames.begin(); }
1384 inline const_name_iterator name_end () const { return ArgNames.end(); }
1386 inline size_t name_size () const { return ArgNames.size(); }
1387 inline bool name_empty() const { return ArgNames.empty(); }
1389 virtual Init *getBit(unsigned Bit) const {
1390 llvm_unreachable("Illegal bit reference off dag");
1393 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
1394 unsigned Elt) const {
1395 llvm_unreachable("Illegal element reference off dag");
1399 //===----------------------------------------------------------------------===//
1400 // High-Level Classes
1401 //===----------------------------------------------------------------------===//
1409 RecordVal(Init *N, RecTy *T, unsigned P);
1410 RecordVal(const std::string &N, RecTy *T, unsigned P);
1412 const std::string &getName() const;
1413 const Init *getNameInit() const { return Name; }
1414 std::string getNameInitAsString() const {
1415 return getNameInit()->getAsUnquotedString();
1418 unsigned getPrefix() const { return Prefix; }
1419 RecTy *getType() const { return Ty; }
1420 Init *getValue() const { return Value; }
1422 bool setValue(Init *V) {
1424 Value = V->convertInitializerTo(Ty);
1432 void print(raw_ostream &OS, bool PrintSem = true) const;
1435 inline raw_ostream &operator<<(raw_ostream &OS, const RecordVal &RV) {
1436 RV.print(OS << " ");
1441 static unsigned LastID;
1443 // Unique record ID.
1446 // Location where record was instantiated, followed by the location of
1447 // multiclass prototypes used.
1448 SmallVector<SMLoc, 4> Locs;
1449 std::vector<Init *> TemplateArgs;
1450 std::vector<RecordVal> Values;
1451 std::vector<Record*> SuperClasses;
1453 // Tracks Record instances. Not owned by Record.
1454 RecordKeeper &TrackedRecords;
1463 // Constructs a record.
1464 explicit Record(const std::string &N, ArrayRef<SMLoc> locs,
1465 RecordKeeper &records) :
1466 ID(LastID++), Name(StringInit::get(N)), Locs(locs.begin(), locs.end()),
1467 TrackedRecords(records), TheInit(0) {
1470 explicit Record(Init *N, ArrayRef<SMLoc> locs, RecordKeeper &records) :
1471 ID(LastID++), Name(N), Locs(locs.begin(), locs.end()),
1472 TrackedRecords(records), TheInit(0) {
1476 // When copy-constructing a Record, we must still guarantee a globally unique
1477 // ID number. All other fields can be copied normally.
1478 Record(const Record &O) :
1479 ID(LastID++), Name(O.Name), Locs(O.Locs), TemplateArgs(O.TemplateArgs),
1480 Values(O.Values), SuperClasses(O.SuperClasses),
1481 TrackedRecords(O.TrackedRecords), TheInit(O.TheInit) { }
1486 static unsigned getNewUID() { return LastID++; }
1489 unsigned getID() const { return ID; }
1491 const std::string &getName() const;
1492 Init *getNameInit() const {
1495 const std::string getNameInitAsString() const {
1496 return getNameInit()->getAsUnquotedString();
1499 void setName(Init *Name); // Also updates RecordKeeper.
1500 void setName(const std::string &Name); // Also updates RecordKeeper.
1502 ArrayRef<SMLoc> getLoc() const { return Locs; }
1504 /// get the corresponding DefInit.
1505 DefInit *getDefInit();
1507 const std::vector<Init *> &getTemplateArgs() const {
1508 return TemplateArgs;
1510 const std::vector<RecordVal> &getValues() const { return Values; }
1511 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; }
1513 bool isTemplateArg(Init *Name) const {
1514 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
1515 if (TemplateArgs[i] == Name) return true;
1518 bool isTemplateArg(StringRef Name) const {
1519 return isTemplateArg(StringInit::get(Name.str()));
1522 const RecordVal *getValue(const Init *Name) const {
1523 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1524 if (Values[i].getNameInit() == Name) return &Values[i];
1527 const RecordVal *getValue(StringRef Name) const {
1528 return getValue(StringInit::get(Name));
1530 RecordVal *getValue(const Init *Name) {
1531 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1532 if (Values[i].getNameInit() == Name) return &Values[i];
1535 RecordVal *getValue(StringRef Name) {
1536 return getValue(StringInit::get(Name));
1539 void addTemplateArg(Init *Name) {
1540 assert(!isTemplateArg(Name) && "Template arg already defined!");
1541 TemplateArgs.push_back(Name);
1543 void addTemplateArg(StringRef Name) {
1544 addTemplateArg(StringInit::get(Name.str()));
1547 void addValue(const RecordVal &RV) {
1548 assert(getValue(RV.getNameInit()) == 0 && "Value already added!");
1549 Values.push_back(RV);
1550 if (Values.size() > 1)
1551 // Keep NAME at the end of the list. It makes record dumps a
1552 // bit prettier and allows TableGen tests to be written more
1553 // naturally. Tests can use CHECK-NEXT to look for Record
1554 // fields they expect to see after a def. They can't do that if
1555 // NAME is the first Record field.
1556 std::swap(Values[Values.size() - 2], Values[Values.size() - 1]);
1559 void removeValue(Init *Name) {
1560 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1561 if (Values[i].getNameInit() == Name) {
1562 Values.erase(Values.begin()+i);
1565 llvm_unreachable("Cannot remove an entry that does not exist!");
1568 void removeValue(StringRef Name) {
1569 removeValue(StringInit::get(Name.str()));
1572 bool isSubClassOf(const Record *R) const {
1573 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1574 if (SuperClasses[i] == R)
1579 bool isSubClassOf(StringRef Name) const {
1580 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1581 if (SuperClasses[i]->getNameInitAsString() == Name)
1586 void addSuperClass(Record *R) {
1587 assert(!isSubClassOf(R) && "Already subclassing record!");
1588 SuperClasses.push_back(R);
1591 /// resolveReferences - If there are any field references that refer to fields
1592 /// that have been filled in, we can propagate the values now.
1594 void resolveReferences() { resolveReferencesTo(0); }
1596 /// resolveReferencesTo - If anything in this record refers to RV, replace the
1597 /// reference to RV with the RHS of RV. If RV is null, we resolve all
1598 /// possible references.
1599 void resolveReferencesTo(const RecordVal *RV);
1601 RecordKeeper &getRecords() const {
1602 return TrackedRecords;
1607 //===--------------------------------------------------------------------===//
1608 // High-level methods useful to tablegen back-ends
1611 /// getValueInit - Return the initializer for a value with the specified name,
1612 /// or throw an exception if the field does not exist.
1614 Init *getValueInit(StringRef FieldName) const;
1616 /// getValueAsString - This method looks up the specified field and returns
1617 /// its value as a string, throwing an exception if the field does not exist
1618 /// or if the value is not a string.
1620 std::string getValueAsString(StringRef FieldName) const;
1622 /// getValueAsBitsInit - This method looks up the specified field and returns
1623 /// its value as a BitsInit, throwing an exception if the field does not exist
1624 /// or if the value is not the right type.
1626 BitsInit *getValueAsBitsInit(StringRef FieldName) const;
1628 /// getValueAsListInit - This method looks up the specified field and returns
1629 /// its value as a ListInit, throwing an exception if the field does not exist
1630 /// or if the value is not the right type.
1632 ListInit *getValueAsListInit(StringRef FieldName) const;
1634 /// getValueAsListOfDefs - This method looks up the specified field and
1635 /// returns its value as a vector of records, throwing an exception if the
1636 /// field does not exist or if the value is not the right type.
1638 std::vector<Record*> getValueAsListOfDefs(StringRef FieldName) const;
1640 /// getValueAsListOfInts - This method looks up the specified field and
1641 /// returns its value as a vector of integers, throwing an exception if the
1642 /// field does not exist or if the value is not the right type.
1644 std::vector<int64_t> getValueAsListOfInts(StringRef FieldName) const;
1646 /// getValueAsListOfStrings - This method looks up the specified field and
1647 /// returns its value as a vector of strings, throwing an exception if the
1648 /// field does not exist or if the value is not the right type.
1650 std::vector<std::string> getValueAsListOfStrings(StringRef FieldName) const;
1652 /// getValueAsDef - This method looks up the specified field and returns its
1653 /// value as a Record, throwing an exception if the field does not exist or if
1654 /// the value is not the right type.
1656 Record *getValueAsDef(StringRef FieldName) const;
1658 /// getValueAsBit - This method looks up the specified field and returns its
1659 /// value as a bit, throwing an exception if the field does not exist or if
1660 /// the value is not the right type.
1662 bool getValueAsBit(StringRef FieldName) const;
1664 /// getValueAsBitOrUnset - This method looks up the specified field and
1665 /// returns its value as a bit. If the field is unset, sets Unset to true and
1668 bool getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const;
1670 /// getValueAsInt - This method looks up the specified field and returns its
1671 /// value as an int64_t, throwing an exception if the field does not exist or
1672 /// if the value is not the right type.
1674 int64_t getValueAsInt(StringRef FieldName) const;
1676 /// getValueAsDag - This method looks up the specified field and returns its
1677 /// value as an Dag, throwing an exception if the field does not exist or if
1678 /// the value is not the right type.
1680 DagInit *getValueAsDag(StringRef FieldName) const;
1683 raw_ostream &operator<<(raw_ostream &OS, const Record &R);
1686 Record Rec; // Placeholder for template args and Name.
1687 typedef std::vector<Record*> RecordVector;
1688 RecordVector DefPrototypes;
1692 MultiClass(const std::string &Name, SMLoc Loc, RecordKeeper &Records) :
1693 Rec(Name, Loc, Records) {}
1696 class RecordKeeper {
1697 std::map<std::string, Record*> Classes, Defs;
1701 for (std::map<std::string, Record*>::iterator I = Classes.begin(),
1702 E = Classes.end(); I != E; ++I)
1704 for (std::map<std::string, Record*>::iterator I = Defs.begin(),
1705 E = Defs.end(); I != E; ++I)
1709 const std::map<std::string, Record*> &getClasses() const { return Classes; }
1710 const std::map<std::string, Record*> &getDefs() const { return Defs; }
1712 Record *getClass(const std::string &Name) const {
1713 std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
1714 return I == Classes.end() ? 0 : I->second;
1716 Record *getDef(const std::string &Name) const {
1717 std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
1718 return I == Defs.end() ? 0 : I->second;
1720 void addClass(Record *R) {
1721 bool Ins = Classes.insert(std::make_pair(R->getName(), R)).second;
1723 assert(Ins && "Class already exists");
1725 void addDef(Record *R) {
1726 bool Ins = Defs.insert(std::make_pair(R->getName(), R)).second;
1728 assert(Ins && "Record already exists");
1731 /// removeClass - Remove, but do not delete, the specified record.
1733 void removeClass(const std::string &Name) {
1734 assert(Classes.count(Name) && "Class does not exist!");
1735 Classes.erase(Name);
1737 /// removeDef - Remove, but do not delete, the specified record.
1739 void removeDef(const std::string &Name) {
1740 assert(Defs.count(Name) && "Def does not exist!");
1744 //===--------------------------------------------------------------------===//
1745 // High-level helper methods, useful for tablegen backends...
1747 /// getAllDerivedDefinitions - This method returns all concrete definitions
1748 /// that derive from the specified class name. If a class with the specified
1749 /// name does not exist, an exception is thrown.
1750 std::vector<Record*>
1751 getAllDerivedDefinitions(const std::string &ClassName) const;
1756 /// LessRecord - Sorting predicate to sort record pointers by name.
1759 bool operator()(const Record *Rec1, const Record *Rec2) const {
1760 return StringRef(Rec1->getName()).compare_numeric(Rec2->getName()) < 0;
1764 /// LessRecordByID - Sorting predicate to sort record pointers by their
1765 /// unique ID. If you just need a deterministic order, use this, since it
1766 /// just compares two `unsigned`; the other sorting predicates require
1767 /// string manipulation.
1768 struct LessRecordByID {
1769 bool operator()(const Record *LHS, const Record *RHS) const {
1770 return LHS->getID() < RHS->getID();
1774 /// LessRecordFieldName - Sorting predicate to sort record pointers by their
1777 struct LessRecordFieldName {
1778 bool operator()(const Record *Rec1, const Record *Rec2) const {
1779 return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name");
1783 raw_ostream &operator<<(raw_ostream &OS, const RecordKeeper &RK);
1785 /// QualifyName - Return an Init with a qualifier prefix referring
1786 /// to CurRec's name.
1787 Init *QualifyName(Record &CurRec, MultiClass *CurMultiClass,
1788 Init *Name, const std::string &Scoper);
1790 /// QualifyName - Return an Init with a qualifier prefix referring
1791 /// to CurRec's name.
1792 Init *QualifyName(Record &CurRec, MultiClass *CurMultiClass,
1793 const std::string &Name, const std::string &Scoper);
1795 } // End llvm namespace