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 static bool classof(const RecTy *) { return true; }
89 RecTyKind getRecTyKind() const { return Kind; }
91 RecTy(RecTyKind K) : Kind(K), ListTy(0) {}
94 virtual std::string getAsString() const = 0;
95 void print(raw_ostream &OS) const { OS << getAsString(); }
98 /// typeIsConvertibleTo - Return true if all values of 'this' type can be
99 /// converted to the specified type.
100 virtual bool typeIsConvertibleTo(const RecTy *RHS) const = 0;
102 /// getListTy - Returns the type representing list<this>.
103 ListRecTy *getListTy();
105 public: // These methods should only be called from subclasses of Init
106 virtual Init *convertValue( UnsetInit *UI) { return 0; }
107 virtual Init *convertValue( BitInit *BI) { return 0; }
108 virtual Init *convertValue( BitsInit *BI) { return 0; }
109 virtual Init *convertValue( IntInit *II) { return 0; }
110 virtual Init *convertValue(StringInit *SI) { return 0; }
111 virtual Init *convertValue( ListInit *LI) { return 0; }
112 virtual Init *convertValue( UnOpInit *UI) {
113 return convertValue((TypedInit*)UI);
115 virtual Init *convertValue( BinOpInit *UI) {
116 return convertValue((TypedInit*)UI);
118 virtual Init *convertValue( TernOpInit *UI) {
119 return convertValue((TypedInit*)UI);
121 virtual Init *convertValue(VarBitInit *VB) { return 0; }
122 virtual Init *convertValue( DefInit *DI) { return 0; }
123 virtual Init *convertValue( DagInit *DI) { return 0; }
124 virtual Init *convertValue( TypedInit *TI) { return 0; }
125 virtual Init *convertValue( VarInit *VI) {
126 return convertValue((TypedInit*)VI);
128 virtual Init *convertValue( FieldInit *FI) {
129 return convertValue((TypedInit*)FI);
132 public: // These methods should only be called by subclasses of RecTy.
133 // baseClassOf - These virtual methods should be overloaded to return true iff
134 // all values of type 'RHS' can be converted to the 'this' type.
135 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
136 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
137 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
138 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
139 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
140 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
141 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
144 inline raw_ostream &operator<<(raw_ostream &OS, const RecTy &Ty) {
150 /// BitRecTy - 'bit' - Represent a single bit
152 class BitRecTy : public RecTy {
153 static BitRecTy Shared;
154 BitRecTy() : RecTy(BitRecTyKind) {}
156 static bool classof(const BitRecTy *) { return true; }
157 static bool classof(const RecTy *RT) {
158 return RT->getRecTyKind() == BitRecTyKind;
161 static BitRecTy *get() { return &Shared; }
163 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
164 virtual Init *convertValue( BitInit *BI) { return (Init*)BI; }
165 virtual Init *convertValue( BitsInit *BI);
166 virtual Init *convertValue( IntInit *II);
167 virtual Init *convertValue(StringInit *SI) { return 0; }
168 virtual Init *convertValue( ListInit *LI) { return 0; }
169 virtual Init *convertValue(VarBitInit *VB) { return (Init*)VB; }
170 virtual Init *convertValue( DefInit *DI) { return 0; }
171 virtual Init *convertValue( DagInit *DI) { return 0; }
172 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
173 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
174 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
175 virtual Init *convertValue( TypedInit *TI);
176 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
177 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
179 virtual std::string getAsString() const { return "bit"; }
181 virtual bool typeIsConvertibleTo(const RecTy *RHS) const {
182 return RHS->baseClassOf(this);
184 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
185 virtual bool baseClassOf(const BitsRecTy *RHS) const;
186 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
187 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
188 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
189 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
190 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
195 // BitsRecTy - 'bits<n>' - Represent a fixed number of bits
196 /// BitsRecTy - 'bits<n>' - Represent a fixed number of bits
198 class BitsRecTy : public RecTy {
200 explicit BitsRecTy(unsigned Sz) : RecTy(BitsRecTyKind), Size(Sz) {}
202 static bool classof(const BitsRecTy *) { return true; }
203 static bool classof(const RecTy *RT) {
204 return RT->getRecTyKind() == BitsRecTyKind;
207 static BitsRecTy *get(unsigned Sz);
209 unsigned getNumBits() const { return Size; }
211 virtual Init *convertValue( UnsetInit *UI);
212 virtual Init *convertValue( BitInit *UI);
213 virtual Init *convertValue( BitsInit *BI);
214 virtual Init *convertValue( IntInit *II);
215 virtual Init *convertValue(StringInit *SI) { return 0; }
216 virtual Init *convertValue( ListInit *LI) { return 0; }
217 virtual Init *convertValue(VarBitInit *VB) { return 0; }
218 virtual Init *convertValue( DefInit *DI) { return 0; }
219 virtual Init *convertValue( DagInit *DI) { return 0; }
220 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
221 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
222 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
223 virtual Init *convertValue( TypedInit *TI);
224 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
225 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
227 virtual std::string getAsString() const;
229 virtual bool typeIsConvertibleTo(const RecTy *RHS) const {
230 return RHS->baseClassOf(this);
232 virtual bool baseClassOf(const BitRecTy *RHS) const { return Size == 1; }
233 virtual bool baseClassOf(const BitsRecTy *RHS) const {
234 return RHS->Size == Size;
236 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
237 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
238 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
239 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
240 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
245 /// IntRecTy - 'int' - Represent an integer value of no particular size
247 class IntRecTy : public RecTy {
248 static IntRecTy Shared;
249 IntRecTy() : RecTy(IntRecTyKind) {}
251 static bool classof(const IntRecTy *) { return true; }
252 static bool classof(const RecTy *RT) {
253 return RT->getRecTyKind() == IntRecTyKind;
256 static IntRecTy *get() { return &Shared; }
258 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
259 virtual Init *convertValue( BitInit *BI);
260 virtual Init *convertValue( BitsInit *BI);
261 virtual Init *convertValue( IntInit *II) { return (Init*)II; }
262 virtual Init *convertValue(StringInit *SI) { return 0; }
263 virtual Init *convertValue( ListInit *LI) { return 0; }
264 virtual Init *convertValue(VarBitInit *VB) { return 0; }
265 virtual Init *convertValue( DefInit *DI) { return 0; }
266 virtual Init *convertValue( DagInit *DI) { return 0; }
267 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
268 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
269 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
270 virtual Init *convertValue( TypedInit *TI);
271 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
272 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
274 virtual std::string getAsString() const { return "int"; }
276 virtual bool typeIsConvertibleTo(const RecTy *RHS) const {
277 return RHS->baseClassOf(this);
280 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
281 virtual bool baseClassOf(const BitsRecTy *RHS) const { return true; }
282 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
283 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
284 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
285 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
286 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
290 /// StringRecTy - 'string' - Represent an string value
292 class StringRecTy : public RecTy {
293 static StringRecTy Shared;
294 StringRecTy() : RecTy(StringRecTyKind) {}
296 static bool classof(const StringRecTy *) { return true; }
297 static bool classof(const RecTy *RT) {
298 return RT->getRecTyKind() == StringRecTyKind;
301 static StringRecTy *get() { return &Shared; }
303 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
304 virtual Init *convertValue( BitInit *BI) { return 0; }
305 virtual Init *convertValue( BitsInit *BI) { return 0; }
306 virtual Init *convertValue( IntInit *II) { return 0; }
307 virtual Init *convertValue(StringInit *SI) { return (Init*)SI; }
308 virtual Init *convertValue( ListInit *LI) { return 0; }
309 virtual Init *convertValue( UnOpInit *BO);
310 virtual Init *convertValue( BinOpInit *BO);
311 virtual Init *convertValue( TernOpInit *BO) { return RecTy::convertValue(BO);}
313 virtual Init *convertValue(VarBitInit *VB) { return 0; }
314 virtual Init *convertValue( DefInit *DI) { return 0; }
315 virtual Init *convertValue( DagInit *DI) { return 0; }
316 virtual Init *convertValue( TypedInit *TI);
317 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
318 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
320 virtual std::string getAsString() const { return "string"; }
322 virtual bool typeIsConvertibleTo(const RecTy *RHS) const {
323 return RHS->baseClassOf(this);
326 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
327 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
328 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
329 virtual bool baseClassOf(const StringRecTy *RHS) const { return true; }
330 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
331 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
332 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
335 // ListRecTy - 'list<Ty>' - Represent a list of values, all of which must be of
336 // the specified type.
337 /// ListRecTy - 'list<Ty>' - Represent a list of values, all of which must
338 /// be of the specified type.
340 class ListRecTy : public RecTy {
342 explicit ListRecTy(RecTy *T) : RecTy(ListRecTyKind), Ty(T) {}
343 friend ListRecTy *RecTy::getListTy();
345 static bool classof(const ListRecTy *) { return true; }
346 static bool classof(const RecTy *RT) {
347 return RT->getRecTyKind() == ListRecTyKind;
350 static ListRecTy *get(RecTy *T) { return T->getListTy(); }
351 RecTy *getElementType() const { return Ty; }
353 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
354 virtual Init *convertValue( BitInit *BI) { return 0; }
355 virtual Init *convertValue( BitsInit *BI) { return 0; }
356 virtual Init *convertValue( IntInit *II) { return 0; }
357 virtual Init *convertValue(StringInit *SI) { return 0; }
358 virtual Init *convertValue( ListInit *LI);
359 virtual Init *convertValue(VarBitInit *VB) { return 0; }
360 virtual Init *convertValue( DefInit *DI) { return 0; }
361 virtual Init *convertValue( DagInit *DI) { return 0; }
362 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
363 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
364 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
365 virtual Init *convertValue( TypedInit *TI);
366 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
367 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
369 virtual std::string getAsString() const;
371 virtual bool typeIsConvertibleTo(const RecTy *RHS) const {
372 return RHS->baseClassOf(this);
375 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
376 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
377 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
378 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
379 virtual bool baseClassOf(const ListRecTy *RHS) const {
380 return RHS->getElementType()->typeIsConvertibleTo(Ty);
382 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
383 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
386 /// DagRecTy - 'dag' - Represent a dag fragment
388 class DagRecTy : public RecTy {
389 static DagRecTy Shared;
390 DagRecTy() : RecTy(DagRecTyKind) {}
392 static bool classof(const DagRecTy *) { return true; }
393 static bool classof(const RecTy *RT) {
394 return RT->getRecTyKind() == DagRecTyKind;
397 static DagRecTy *get() { return &Shared; }
399 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
400 virtual Init *convertValue( BitInit *BI) { return 0; }
401 virtual Init *convertValue( BitsInit *BI) { return 0; }
402 virtual Init *convertValue( IntInit *II) { return 0; }
403 virtual Init *convertValue(StringInit *SI) { return 0; }
404 virtual Init *convertValue( ListInit *LI) { return 0; }
405 virtual Init *convertValue(VarBitInit *VB) { return 0; }
406 virtual Init *convertValue( DefInit *DI) { return 0; }
407 virtual Init *convertValue( UnOpInit *BO);
408 virtual Init *convertValue( BinOpInit *BO);
409 virtual Init *convertValue( TernOpInit *BO) { return RecTy::convertValue(BO);}
410 virtual Init *convertValue( DagInit *CI) { return (Init*)CI; }
411 virtual Init *convertValue( TypedInit *TI);
412 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
413 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
415 virtual std::string getAsString() const { return "dag"; }
417 virtual bool typeIsConvertibleTo(const RecTy *RHS) const {
418 return RHS->baseClassOf(this);
421 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
422 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
423 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
424 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
425 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
426 virtual bool baseClassOf(const DagRecTy *RHS) const { return true; }
427 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
431 /// RecordRecTy - '[classname]' - Represent an instance of a class, such as:
434 class RecordRecTy : public RecTy {
436 explicit RecordRecTy(Record *R) : RecTy(RecordRecTyKind), Rec(R) {}
439 static bool classof(const RecordRecTy *) { return true; }
440 static bool classof(const RecTy *RT) {
441 return RT->getRecTyKind() == RecordRecTyKind;
444 static RecordRecTy *get(Record *R);
446 Record *getRecord() const { return Rec; }
448 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
449 virtual Init *convertValue( BitInit *BI) { return 0; }
450 virtual Init *convertValue( BitsInit *BI) { return 0; }
451 virtual Init *convertValue( IntInit *II) { return 0; }
452 virtual Init *convertValue(StringInit *SI) { return 0; }
453 virtual Init *convertValue( ListInit *LI) { return 0; }
454 virtual Init *convertValue(VarBitInit *VB) { return 0; }
455 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
456 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
457 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
458 virtual Init *convertValue( DefInit *DI);
459 virtual Init *convertValue( DagInit *DI) { return 0; }
460 virtual Init *convertValue( TypedInit *VI);
461 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
462 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
464 virtual std::string getAsString() const;
466 virtual bool typeIsConvertibleTo(const RecTy *RHS) const {
467 return RHS->baseClassOf(this);
469 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
470 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
471 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
472 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
473 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
474 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
475 virtual bool baseClassOf(const RecordRecTy *RHS) const;
478 /// resolveTypes - Find a common type that T1 and T2 convert to.
479 /// Return 0 if no such type exists.
481 RecTy *resolveTypes(RecTy *T1, RecTy *T2);
483 //===----------------------------------------------------------------------===//
484 // Initializer Classes
485 //===----------------------------------------------------------------------===//
489 /// \brief Discriminator enum (for isa<>, dyn_cast<>, et al.)
491 /// This enum is laid out by a preorder traversal of the inheritance
492 /// hierarchy, and does not contain an entry for abstract classes, as per
493 /// the recommendation in docs/HowToSetUpLLVMStyleRTTI.rst.
495 /// We also explicitly include "first" and "last" values for each
496 /// interior node of the inheritance tree, to make it easier to read the
497 /// corresponding classof().
499 /// We could pack these a bit tighter by not having the IK_FirstXXXInit
500 /// and IK_LastXXXInit be their own values, but that would degrade
501 /// readability for really no benefit.
518 IK_VarListElementInit,
526 Init(const Init &) LLVM_DELETED_FUNCTION;
527 Init &operator=(const Init &) LLVM_DELETED_FUNCTION;
528 virtual void anchor();
531 InitKind getKind() const { return Kind; }
534 explicit Init(InitKind K) : Kind(K) {}
539 /// isComplete - This virtual method should be overridden by values that may
540 /// not be completely specified yet.
541 virtual bool isComplete() const { return true; }
543 /// print - Print out this value.
544 void print(raw_ostream &OS) const { OS << getAsString(); }
546 /// getAsString - Convert this value to a string form.
547 virtual std::string getAsString() const = 0;
548 /// getAsUnquotedString - Convert this value to a string form,
549 /// without adding quote markers. This primaruly affects
550 /// StringInits where we will not surround the string value with
552 virtual std::string getAsUnquotedString() const { return getAsString(); }
554 /// dump - Debugging method that may be called through a debugger, just
555 /// invokes print on stderr.
558 /// convertInitializerTo - This virtual function is a simple call-back
559 /// function that should be overridden to call the appropriate
560 /// RecTy::convertValue method.
562 virtual Init *convertInitializerTo(RecTy *Ty) const = 0;
564 /// convertInitializerBitRange - This method is used to implement the bitrange
565 /// selection operator. Given an initializer, it selects the specified bits
566 /// out, returning them as a new init of bits type. If it is not legal to use
567 /// the bit subscript operator on this initializer, return null.
570 convertInitializerBitRange(const std::vector<unsigned> &Bits) const {
574 /// convertInitListSlice - This method is used to implement the list slice
575 /// selection operator. Given an initializer, it selects the specified list
576 /// elements, returning them as a new init of list type. If it is not legal
577 /// to take a slice of this, return null.
580 convertInitListSlice(const std::vector<unsigned> &Elements) const {
584 /// getFieldType - This method is used to implement the FieldInit class.
585 /// Implementors of this method should return the type of the named field if
586 /// they are of record type.
588 virtual RecTy *getFieldType(const std::string &FieldName) const { return 0; }
590 /// getFieldInit - This method complements getFieldType to return the
591 /// initializer for the specified field. If getFieldType returns non-null
592 /// this method should return non-null, otherwise it returns null.
594 virtual Init *getFieldInit(Record &R, const RecordVal *RV,
595 const std::string &FieldName) const {
599 /// resolveReferences - This method is used by classes that refer to other
600 /// variables which may not be defined at the time the expression is formed.
601 /// If a value is set for the variable later, this method will be called on
602 /// users of the value to allow the value to propagate out.
604 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const {
605 return const_cast<Init *>(this);
608 /// getBit - This method is used to return the initializer for the specified
610 virtual Init *getBit(unsigned Bit) const = 0;
612 /// getBitVar - This method is used to retrieve the initializer for bit
613 /// reference. For non-VarBitInit, it simply returns itself.
614 virtual Init *getBitVar() const { return const_cast<Init*>(this); }
616 /// getBitNum - This method is used to retrieve the bit number of a bit
617 /// reference. For non-VarBitInit, it simply returns 0.
618 virtual unsigned getBitNum() const { return 0; }
621 inline raw_ostream &operator<<(raw_ostream &OS, const Init &I) {
622 I.print(OS); return OS;
625 /// TypedInit - This is the common super-class of types that have a specific,
628 class TypedInit : public Init {
631 TypedInit(const TypedInit &Other) LLVM_DELETED_FUNCTION;
632 TypedInit &operator=(const TypedInit &Other) LLVM_DELETED_FUNCTION;
635 explicit TypedInit(InitKind K, RecTy *T) : Init(K), Ty(T) {}
638 static bool classof(const Init *I) {
639 return I->getKind() >= IK_FirstTypedInit &&
640 I->getKind() <= IK_LastTypedInit;
642 RecTy *getType() const { return Ty; }
645 convertInitializerBitRange(const std::vector<unsigned> &Bits) const;
647 convertInitListSlice(const std::vector<unsigned> &Elements) const;
649 /// getFieldType - This method is used to implement the FieldInit class.
650 /// Implementors of this method should return the type of the named field if
651 /// they are of record type.
653 virtual RecTy *getFieldType(const std::string &FieldName) const;
655 /// resolveListElementReference - This method is used to implement
656 /// VarListElementInit::resolveReferences. If the list element is resolvable
657 /// now, we return the resolved value, otherwise we return null.
658 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
659 unsigned Elt) const = 0;
663 /// UnsetInit - ? - Represents an uninitialized value
665 class UnsetInit : public Init {
666 UnsetInit() : Init(IK_UnsetInit) {}
667 UnsetInit(const UnsetInit &) LLVM_DELETED_FUNCTION;
668 UnsetInit &operator=(const UnsetInit &Other) LLVM_DELETED_FUNCTION;
669 virtual void anchor();
672 static bool classof(const Init *I) {
673 return I->getKind() == IK_UnsetInit;
675 static UnsetInit *get();
677 virtual Init *convertInitializerTo(RecTy *Ty) const {
678 return Ty->convertValue(const_cast<UnsetInit *>(this));
681 virtual Init *getBit(unsigned Bit) const {
682 return const_cast<UnsetInit*>(this);
685 virtual bool isComplete() const { return false; }
686 virtual std::string getAsString() const { return "?"; }
690 /// BitInit - true/false - Represent a concrete initializer for a bit.
692 class BitInit : public Init {
695 explicit BitInit(bool V) : Init(IK_BitInit), Value(V) {}
696 BitInit(const BitInit &Other) LLVM_DELETED_FUNCTION;
697 BitInit &operator=(BitInit &Other) LLVM_DELETED_FUNCTION;
698 virtual void anchor();
701 static bool classof(const Init *I) {
702 return I->getKind() == IK_BitInit;
704 static BitInit *get(bool V);
706 bool getValue() const { return Value; }
708 virtual Init *convertInitializerTo(RecTy *Ty) const {
709 return Ty->convertValue(const_cast<BitInit *>(this));
712 virtual Init *getBit(unsigned Bit) const {
713 assert(Bit < 1 && "Bit index out of range!");
714 return const_cast<BitInit*>(this);
717 virtual std::string getAsString() const { return Value ? "1" : "0"; }
720 /// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
721 /// It contains a vector of bits, whose size is determined by the type.
723 class BitsInit : public Init, public FoldingSetNode {
724 std::vector<Init*> Bits;
726 BitsInit(ArrayRef<Init *> Range)
727 : Init(IK_BitsInit), Bits(Range.begin(), Range.end()) {}
729 BitsInit(const BitsInit &Other) LLVM_DELETED_FUNCTION;
730 BitsInit &operator=(const BitsInit &Other) LLVM_DELETED_FUNCTION;
733 static bool classof(const Init *I) {
734 return I->getKind() == IK_BitsInit;
736 static BitsInit *get(ArrayRef<Init *> Range);
738 void Profile(FoldingSetNodeID &ID) const;
740 unsigned getNumBits() const { return Bits.size(); }
742 virtual Init *convertInitializerTo(RecTy *Ty) const {
743 return Ty->convertValue(const_cast<BitsInit *>(this));
746 convertInitializerBitRange(const std::vector<unsigned> &Bits) const;
748 virtual bool isComplete() const {
749 for (unsigned i = 0; i != getNumBits(); ++i)
750 if (!getBit(i)->isComplete()) return false;
753 bool allInComplete() const {
754 for (unsigned i = 0; i != getNumBits(); ++i)
755 if (getBit(i)->isComplete()) return false;
758 virtual std::string getAsString() const;
760 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
762 virtual Init *getBit(unsigned Bit) const {
763 assert(Bit < Bits.size() && "Bit index out of range!");
769 /// IntInit - 7 - Represent an initalization by a literal integer value.
771 class IntInit : public TypedInit {
774 explicit IntInit(int64_t V)
775 : TypedInit(IK_IntInit, IntRecTy::get()), Value(V) {}
777 IntInit(const IntInit &Other) LLVM_DELETED_FUNCTION;
778 IntInit &operator=(const IntInit &Other) LLVM_DELETED_FUNCTION;
781 static bool classof(const Init *I) {
782 return I->getKind() == IK_IntInit;
784 static IntInit *get(int64_t V);
786 int64_t getValue() const { return Value; }
788 virtual Init *convertInitializerTo(RecTy *Ty) const {
789 return Ty->convertValue(const_cast<IntInit *>(this));
792 convertInitializerBitRange(const std::vector<unsigned> &Bits) const;
794 virtual std::string getAsString() const;
796 /// resolveListElementReference - This method is used to implement
797 /// VarListElementInit::resolveReferences. If the list element is resolvable
798 /// now, we return the resolved value, otherwise we return null.
799 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
800 unsigned Elt) const {
801 llvm_unreachable("Illegal element reference off int");
804 virtual Init *getBit(unsigned Bit) const {
805 return BitInit::get((Value & (1ULL << Bit)) != 0);
810 /// StringInit - "foo" - Represent an initialization by a string value.
812 class StringInit : public TypedInit {
815 explicit StringInit(const std::string &V)
816 : TypedInit(IK_StringInit, StringRecTy::get()), Value(V) {}
818 StringInit(const StringInit &Other) LLVM_DELETED_FUNCTION;
819 StringInit &operator=(const StringInit &Other) LLVM_DELETED_FUNCTION;
820 virtual void anchor();
823 static bool classof(const Init *I) {
824 return I->getKind() == IK_StringInit;
826 static StringInit *get(StringRef);
828 const std::string &getValue() const { return Value; }
830 virtual Init *convertInitializerTo(RecTy *Ty) const {
831 return Ty->convertValue(const_cast<StringInit *>(this));
834 virtual std::string getAsString() const { return "\"" + Value + "\""; }
835 virtual std::string getAsUnquotedString() const { return Value; }
837 /// resolveListElementReference - This method is used to implement
838 /// VarListElementInit::resolveReferences. If the list element is resolvable
839 /// now, we return the resolved value, otherwise we return null.
840 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
841 unsigned Elt) const {
842 llvm_unreachable("Illegal element reference off string");
845 virtual Init *getBit(unsigned Bit) const {
846 llvm_unreachable("Illegal bit reference off string");
850 /// ListInit - [AL, AH, CL] - Represent a list of defs
852 class ListInit : public TypedInit, public FoldingSetNode {
853 std::vector<Init*> Values;
855 typedef std::vector<Init*>::const_iterator const_iterator;
858 explicit ListInit(ArrayRef<Init *> Range, RecTy *EltTy)
859 : TypedInit(IK_ListInit, ListRecTy::get(EltTy)),
860 Values(Range.begin(), Range.end()) {}
862 ListInit(const ListInit &Other) LLVM_DELETED_FUNCTION;
863 ListInit &operator=(const ListInit &Other) LLVM_DELETED_FUNCTION;
866 static bool classof(const Init *I) {
867 return I->getKind() == IK_ListInit;
869 static ListInit *get(ArrayRef<Init *> Range, RecTy *EltTy);
871 void Profile(FoldingSetNodeID &ID) const;
873 unsigned getSize() const { return Values.size(); }
874 Init *getElement(unsigned i) const {
875 assert(i < Values.size() && "List element index out of range!");
879 Record *getElementAsRecord(unsigned i) const;
882 convertInitListSlice(const std::vector<unsigned> &Elements) const;
884 virtual Init *convertInitializerTo(RecTy *Ty) const {
885 return Ty->convertValue(const_cast<ListInit *>(this));
888 /// resolveReferences - This method is used by classes that refer to other
889 /// variables which may not be defined at the time they expression is formed.
890 /// If a value is set for the variable later, this method will be called on
891 /// users of the value to allow the value to propagate out.
893 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
895 virtual std::string getAsString() const;
897 ArrayRef<Init*> getValues() const { return Values; }
899 inline const_iterator begin() const { return Values.begin(); }
900 inline const_iterator end () const { return Values.end(); }
902 inline size_t size () const { return Values.size(); }
903 inline bool empty() const { return Values.empty(); }
905 /// resolveListElementReference - This method is used to implement
906 /// VarListElementInit::resolveReferences. If the list element is resolvable
907 /// now, we return the resolved value, otherwise we return null.
908 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
911 virtual Init *getBit(unsigned Bit) const {
912 llvm_unreachable("Illegal bit reference off list");
917 /// OpInit - Base class for operators
919 class OpInit : public TypedInit {
920 OpInit(const OpInit &Other) LLVM_DELETED_FUNCTION;
921 OpInit &operator=(OpInit &Other) LLVM_DELETED_FUNCTION;
924 explicit OpInit(InitKind K, RecTy *Type) : TypedInit(K, Type) {}
927 static bool classof(const Init *I) {
928 return I->getKind() >= IK_FirstOpInit &&
929 I->getKind() <= IK_LastOpInit;
931 // Clone - Clone this operator, replacing arguments with the new list
932 virtual OpInit *clone(std::vector<Init *> &Operands) const = 0;
934 virtual int getNumOperands() const = 0;
935 virtual Init *getOperand(int i) const = 0;
937 // Fold - If possible, fold this to a simpler init. Return this if not
939 virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const = 0;
941 virtual Init *convertInitializerTo(RecTy *Ty) const {
942 return Ty->convertValue(const_cast<OpInit *>(this));
945 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
948 virtual Init *getBit(unsigned Bit) const;
952 /// UnOpInit - !op (X) - Transform an init.
954 class UnOpInit : public OpInit {
956 enum UnaryOp { CAST, HEAD, TAIL, EMPTY };
961 UnOpInit(UnaryOp opc, Init *lhs, RecTy *Type)
962 : OpInit(IK_UnOpInit, Type), Opc(opc), LHS(lhs) {}
964 UnOpInit(const UnOpInit &Other) LLVM_DELETED_FUNCTION;
965 UnOpInit &operator=(const UnOpInit &Other) LLVM_DELETED_FUNCTION;
968 static bool classof(const Init *I) {
969 return I->getKind() == IK_UnOpInit;
971 static UnOpInit *get(UnaryOp opc, Init *lhs, RecTy *Type);
973 // Clone - Clone this operator, replacing arguments with the new list
974 virtual OpInit *clone(std::vector<Init *> &Operands) const {
975 assert(Operands.size() == 1 &&
976 "Wrong number of operands for unary operation");
977 return UnOpInit::get(getOpcode(), *Operands.begin(), getType());
980 virtual int getNumOperands() const { return 1; }
981 virtual Init *getOperand(int i) const {
982 assert(i == 0 && "Invalid operand id for unary operator");
986 UnaryOp getOpcode() const { return Opc; }
987 Init *getOperand() const { return LHS; }
989 // Fold - If possible, fold this to a simpler init. Return this if not
991 virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const;
993 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
995 virtual std::string getAsString() const;
998 /// BinOpInit - !op (X, Y) - Combine two inits.
1000 class BinOpInit : public OpInit {
1002 enum BinaryOp { SHL, SRA, SRL, STRCONCAT, CONCAT, EQ };
1007 BinOpInit(BinaryOp opc, Init *lhs, Init *rhs, RecTy *Type) :
1008 OpInit(IK_BinOpInit, Type), Opc(opc), LHS(lhs), RHS(rhs) {}
1010 BinOpInit(const BinOpInit &Other) LLVM_DELETED_FUNCTION;
1011 BinOpInit &operator=(const BinOpInit &Other) LLVM_DELETED_FUNCTION;
1014 static bool classof(const Init *I) {
1015 return I->getKind() == IK_BinOpInit;
1017 static BinOpInit *get(BinaryOp opc, Init *lhs, Init *rhs,
1020 // Clone - Clone this operator, replacing arguments with the new list
1021 virtual OpInit *clone(std::vector<Init *> &Operands) const {
1022 assert(Operands.size() == 2 &&
1023 "Wrong number of operands for binary operation");
1024 return BinOpInit::get(getOpcode(), Operands[0], Operands[1], getType());
1027 virtual int getNumOperands() const { return 2; }
1028 virtual Init *getOperand(int i) const {
1029 assert((i == 0 || i == 1) && "Invalid operand id for binary operator");
1037 BinaryOp getOpcode() const { return Opc; }
1038 Init *getLHS() const { return LHS; }
1039 Init *getRHS() const { return RHS; }
1041 // Fold - If possible, fold this to a simpler init. Return this if not
1042 // possible to fold.
1043 virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const;
1045 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
1047 virtual std::string getAsString() const;
1050 /// TernOpInit - !op (X, Y, Z) - Combine two inits.
1052 class TernOpInit : public OpInit {
1054 enum TernaryOp { SUBST, FOREACH, IF };
1057 Init *LHS, *MHS, *RHS;
1059 TernOpInit(TernaryOp opc, Init *lhs, Init *mhs, Init *rhs,
1061 OpInit(IK_TernOpInit, Type), Opc(opc), LHS(lhs), MHS(mhs), RHS(rhs) {}
1063 TernOpInit(const TernOpInit &Other) LLVM_DELETED_FUNCTION;
1064 TernOpInit &operator=(const TernOpInit &Other) LLVM_DELETED_FUNCTION;
1067 static bool classof(const Init *I) {
1068 return I->getKind() == IK_TernOpInit;
1070 static TernOpInit *get(TernaryOp opc, Init *lhs,
1071 Init *mhs, Init *rhs,
1074 // Clone - Clone this operator, replacing arguments with the new list
1075 virtual OpInit *clone(std::vector<Init *> &Operands) const {
1076 assert(Operands.size() == 3 &&
1077 "Wrong number of operands for ternary operation");
1078 return TernOpInit::get(getOpcode(), Operands[0], Operands[1], Operands[2],
1082 virtual int getNumOperands() const { return 3; }
1083 virtual Init *getOperand(int i) const {
1084 assert((i == 0 || i == 1 || i == 2) &&
1085 "Invalid operand id for ternary operator");
1088 } else if (i == 1) {
1095 TernaryOp getOpcode() const { return Opc; }
1096 Init *getLHS() const { return LHS; }
1097 Init *getMHS() const { return MHS; }
1098 Init *getRHS() const { return RHS; }
1100 // Fold - If possible, fold this to a simpler init. Return this if not
1101 // possible to fold.
1102 virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const;
1104 virtual bool isComplete() const { return false; }
1106 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
1108 virtual std::string getAsString() const;
1112 /// VarInit - 'Opcode' - Represent a reference to an entire variable object.
1114 class VarInit : public TypedInit {
1117 explicit VarInit(const std::string &VN, RecTy *T)
1118 : TypedInit(IK_VarInit, T), VarName(StringInit::get(VN)) {}
1119 explicit VarInit(Init *VN, RecTy *T)
1120 : TypedInit(IK_VarInit, T), VarName(VN) {}
1122 VarInit(const VarInit &Other) LLVM_DELETED_FUNCTION;
1123 VarInit &operator=(const VarInit &Other) LLVM_DELETED_FUNCTION;
1126 static bool classof(const Init *I) {
1127 return I->getKind() == IK_VarInit;
1129 static VarInit *get(const std::string &VN, RecTy *T);
1130 static VarInit *get(Init *VN, RecTy *T);
1132 virtual Init *convertInitializerTo(RecTy *Ty) const {
1133 return Ty->convertValue(const_cast<VarInit *>(this));
1136 const std::string &getName() const;
1137 Init *getNameInit() const { return VarName; }
1138 std::string getNameInitAsString() const {
1139 return getNameInit()->getAsUnquotedString();
1142 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
1143 unsigned Elt) const;
1145 virtual RecTy *getFieldType(const std::string &FieldName) const;
1146 virtual Init *getFieldInit(Record &R, const RecordVal *RV,
1147 const std::string &FieldName) const;
1149 /// resolveReferences - This method is used by classes that refer to other
1150 /// variables which may not be defined at the time they expression is formed.
1151 /// If a value is set for the variable later, this method will be called on
1152 /// users of the value to allow the value to propagate out.
1154 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
1156 virtual Init *getBit(unsigned Bit) const;
1158 virtual std::string getAsString() const { return getName(); }
1162 /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
1164 class VarBitInit : public Init {
1168 VarBitInit(TypedInit *T, unsigned B) : Init(IK_VarBitInit), TI(T), Bit(B) {
1169 assert(T->getType() &&
1170 (isa<IntRecTy>(T->getType()) ||
1171 (isa<BitsRecTy>(T->getType()) &&
1172 cast<BitsRecTy>(T->getType())->getNumBits() > B)) &&
1173 "Illegal VarBitInit expression!");
1176 VarBitInit(const VarBitInit &Other) LLVM_DELETED_FUNCTION;
1177 VarBitInit &operator=(const VarBitInit &Other) LLVM_DELETED_FUNCTION;
1180 static bool classof(const Init *I) {
1181 return I->getKind() == IK_VarBitInit;
1183 static VarBitInit *get(TypedInit *T, unsigned B);
1185 virtual Init *convertInitializerTo(RecTy *Ty) const {
1186 return Ty->convertValue(const_cast<VarBitInit *>(this));
1189 virtual Init *getBitVar() const { return TI; }
1190 virtual unsigned getBitNum() const { return Bit; }
1192 virtual std::string getAsString() const;
1193 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
1195 virtual Init *getBit(unsigned B) const {
1196 assert(B < 1 && "Bit index out of range!");
1197 return const_cast<VarBitInit*>(this);
1201 /// VarListElementInit - List[4] - Represent access to one element of a var or
1203 class VarListElementInit : public TypedInit {
1207 VarListElementInit(TypedInit *T, unsigned E)
1208 : TypedInit(IK_VarListElementInit,
1209 cast<ListRecTy>(T->getType())->getElementType()),
1211 assert(T->getType() && isa<ListRecTy>(T->getType()) &&
1212 "Illegal VarBitInit expression!");
1215 VarListElementInit(const VarListElementInit &Other) LLVM_DELETED_FUNCTION;
1216 void operator=(const VarListElementInit &Other) LLVM_DELETED_FUNCTION;
1219 static bool classof(const Init *I) {
1220 return I->getKind() == IK_VarListElementInit;
1222 static VarListElementInit *get(TypedInit *T, unsigned E);
1224 virtual Init *convertInitializerTo(RecTy *Ty) const {
1225 return Ty->convertValue(const_cast<VarListElementInit *>(this));
1228 TypedInit *getVariable() const { return TI; }
1229 unsigned getElementNum() const { return Element; }
1231 /// resolveListElementReference - This method is used to implement
1232 /// VarListElementInit::resolveReferences. If the list element is resolvable
1233 /// now, we return the resolved value, otherwise we return null.
1234 virtual Init *resolveListElementReference(Record &R,
1235 const RecordVal *RV,
1236 unsigned Elt) const;
1238 virtual std::string getAsString() const;
1239 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
1241 virtual Init *getBit(unsigned Bit) const;
1244 /// DefInit - AL - Represent a reference to a 'def' in the description
1246 class DefInit : public TypedInit {
1249 DefInit(Record *D, RecordRecTy *T) : TypedInit(IK_DefInit, T), Def(D) {}
1250 friend class Record;
1252 DefInit(const DefInit &Other) LLVM_DELETED_FUNCTION;
1253 DefInit &operator=(const DefInit &Other) LLVM_DELETED_FUNCTION;
1256 static bool classof(const Init *I) {
1257 return I->getKind() == IK_DefInit;
1259 static DefInit *get(Record*);
1261 virtual Init *convertInitializerTo(RecTy *Ty) const {
1262 return Ty->convertValue(const_cast<DefInit *>(this));
1265 Record *getDef() const { return Def; }
1267 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
1269 virtual RecTy *getFieldType(const std::string &FieldName) const;
1270 virtual Init *getFieldInit(Record &R, const RecordVal *RV,
1271 const std::string &FieldName) const;
1273 virtual std::string getAsString() const;
1275 virtual Init *getBit(unsigned Bit) const {
1276 llvm_unreachable("Illegal bit reference off def");
1279 /// resolveListElementReference - This method is used to implement
1280 /// VarListElementInit::resolveReferences. If the list element is resolvable
1281 /// now, we return the resolved value, otherwise we return null.
1282 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
1283 unsigned Elt) const {
1284 llvm_unreachable("Illegal element reference off def");
1289 /// FieldInit - X.Y - Represent a reference to a subfield of a variable
1291 class FieldInit : public TypedInit {
1292 Init *Rec; // Record we are referring to
1293 std::string FieldName; // Field we are accessing
1295 FieldInit(Init *R, const std::string &FN)
1296 : TypedInit(IK_FieldInit, R->getFieldType(FN)), Rec(R), FieldName(FN) {
1297 assert(getType() && "FieldInit with non-record type!");
1300 FieldInit(const FieldInit &Other) LLVM_DELETED_FUNCTION;
1301 FieldInit &operator=(const FieldInit &Other) LLVM_DELETED_FUNCTION;
1304 static bool classof(const Init *I) {
1305 return I->getKind() == IK_FieldInit;
1307 static FieldInit *get(Init *R, const std::string &FN);
1308 static FieldInit *get(Init *R, const Init *FN);
1310 virtual Init *convertInitializerTo(RecTy *Ty) const {
1311 return Ty->convertValue(const_cast<FieldInit *>(this));
1314 virtual Init *getBit(unsigned Bit) const;
1316 virtual Init *resolveListElementReference(Record &R,
1317 const RecordVal *RV,
1318 unsigned Elt) const;
1320 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
1322 virtual std::string getAsString() const {
1323 return Rec->getAsString() + "." + FieldName;
1327 /// DagInit - (v a, b) - Represent a DAG tree value. DAG inits are required
1328 /// to have at least one value then a (possibly empty) list of arguments. Each
1329 /// argument can have a name associated with it.
1331 class DagInit : public TypedInit, public FoldingSetNode {
1333 std::string ValName;
1334 std::vector<Init*> Args;
1335 std::vector<std::string> ArgNames;
1337 DagInit(Init *V, const std::string &VN,
1338 ArrayRef<Init *> ArgRange,
1339 ArrayRef<std::string> NameRange)
1340 : TypedInit(IK_DagInit, DagRecTy::get()), Val(V), ValName(VN),
1341 Args(ArgRange.begin(), ArgRange.end()),
1342 ArgNames(NameRange.begin(), NameRange.end()) {}
1344 DagInit(const DagInit &Other) LLVM_DELETED_FUNCTION;
1345 DagInit &operator=(const DagInit &Other) LLVM_DELETED_FUNCTION;
1348 static bool classof(const Init *I) {
1349 return I->getKind() == IK_DagInit;
1351 static DagInit *get(Init *V, const std::string &VN,
1352 ArrayRef<Init *> ArgRange,
1353 ArrayRef<std::string> NameRange);
1354 static DagInit *get(Init *V, const std::string &VN,
1356 std::pair<Init*, std::string> > &args);
1358 void Profile(FoldingSetNodeID &ID) const;
1360 virtual Init *convertInitializerTo(RecTy *Ty) const {
1361 return Ty->convertValue(const_cast<DagInit *>(this));
1364 Init *getOperator() const { return Val; }
1366 const std::string &getName() const { return ValName; }
1368 unsigned getNumArgs() const { return Args.size(); }
1369 Init *getArg(unsigned Num) const {
1370 assert(Num < Args.size() && "Arg number out of range!");
1373 const std::string &getArgName(unsigned Num) const {
1374 assert(Num < ArgNames.size() && "Arg number out of range!");
1375 return ArgNames[Num];
1378 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
1380 virtual std::string getAsString() const;
1382 typedef std::vector<Init*>::const_iterator const_arg_iterator;
1383 typedef std::vector<std::string>::const_iterator const_name_iterator;
1385 inline const_arg_iterator arg_begin() const { return Args.begin(); }
1386 inline const_arg_iterator arg_end () const { return Args.end(); }
1388 inline size_t arg_size () const { return Args.size(); }
1389 inline bool arg_empty() const { return Args.empty(); }
1391 inline const_name_iterator name_begin() const { return ArgNames.begin(); }
1392 inline const_name_iterator name_end () const { return ArgNames.end(); }
1394 inline size_t name_size () const { return ArgNames.size(); }
1395 inline bool name_empty() const { return ArgNames.empty(); }
1397 virtual Init *getBit(unsigned Bit) const {
1398 llvm_unreachable("Illegal bit reference off dag");
1401 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
1402 unsigned Elt) const {
1403 llvm_unreachable("Illegal element reference off dag");
1407 //===----------------------------------------------------------------------===//
1408 // High-Level Classes
1409 //===----------------------------------------------------------------------===//
1417 RecordVal(Init *N, RecTy *T, unsigned P);
1418 RecordVal(const std::string &N, RecTy *T, unsigned P);
1420 const std::string &getName() const;
1421 const Init *getNameInit() const { return Name; }
1422 std::string getNameInitAsString() const {
1423 return getNameInit()->getAsUnquotedString();
1426 unsigned getPrefix() const { return Prefix; }
1427 RecTy *getType() const { return Ty; }
1428 Init *getValue() const { return Value; }
1430 bool setValue(Init *V) {
1432 Value = V->convertInitializerTo(Ty);
1440 void print(raw_ostream &OS, bool PrintSem = true) const;
1443 inline raw_ostream &operator<<(raw_ostream &OS, const RecordVal &RV) {
1444 RV.print(OS << " ");
1449 static unsigned LastID;
1451 // Unique record ID.
1454 // Location where record was instantiated, followed by the location of
1455 // multiclass prototypes used.
1456 SmallVector<SMLoc, 4> Locs;
1457 std::vector<Init *> TemplateArgs;
1458 std::vector<RecordVal> Values;
1459 std::vector<Record*> SuperClasses;
1461 // Tracks Record instances. Not owned by Record.
1462 RecordKeeper &TrackedRecords;
1471 // Constructs a record.
1472 explicit Record(const std::string &N, ArrayRef<SMLoc> locs,
1473 RecordKeeper &records) :
1474 ID(LastID++), Name(StringInit::get(N)), Locs(locs.begin(), locs.end()),
1475 TrackedRecords(records), TheInit(0) {
1478 explicit Record(Init *N, ArrayRef<SMLoc> locs, RecordKeeper &records) :
1479 ID(LastID++), Name(N), Locs(locs.begin(), locs.end()),
1480 TrackedRecords(records), TheInit(0) {
1484 // When copy-constructing a Record, we must still guarantee a globally unique
1485 // ID number. All other fields can be copied normally.
1486 Record(const Record &O) :
1487 ID(LastID++), Name(O.Name), Locs(O.Locs), TemplateArgs(O.TemplateArgs),
1488 Values(O.Values), SuperClasses(O.SuperClasses),
1489 TrackedRecords(O.TrackedRecords), TheInit(O.TheInit) { }
1494 static unsigned getNewUID() { return LastID++; }
1497 unsigned getID() const { return ID; }
1499 const std::string &getName() const;
1500 Init *getNameInit() const {
1503 const std::string getNameInitAsString() const {
1504 return getNameInit()->getAsUnquotedString();
1507 void setName(Init *Name); // Also updates RecordKeeper.
1508 void setName(const std::string &Name); // Also updates RecordKeeper.
1510 ArrayRef<SMLoc> getLoc() const { return Locs; }
1512 /// get the corresponding DefInit.
1513 DefInit *getDefInit();
1515 const std::vector<Init *> &getTemplateArgs() const {
1516 return TemplateArgs;
1518 const std::vector<RecordVal> &getValues() const { return Values; }
1519 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; }
1521 bool isTemplateArg(Init *Name) const {
1522 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
1523 if (TemplateArgs[i] == Name) return true;
1526 bool isTemplateArg(StringRef Name) const {
1527 return isTemplateArg(StringInit::get(Name.str()));
1530 const RecordVal *getValue(const Init *Name) const {
1531 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1532 if (Values[i].getNameInit() == Name) return &Values[i];
1535 const RecordVal *getValue(StringRef Name) const {
1536 return getValue(StringInit::get(Name));
1538 RecordVal *getValue(const Init *Name) {
1539 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1540 if (Values[i].getNameInit() == Name) return &Values[i];
1543 RecordVal *getValue(StringRef Name) {
1544 return getValue(StringInit::get(Name));
1547 void addTemplateArg(Init *Name) {
1548 assert(!isTemplateArg(Name) && "Template arg already defined!");
1549 TemplateArgs.push_back(Name);
1551 void addTemplateArg(StringRef Name) {
1552 addTemplateArg(StringInit::get(Name.str()));
1555 void addValue(const RecordVal &RV) {
1556 assert(getValue(RV.getNameInit()) == 0 && "Value already added!");
1557 Values.push_back(RV);
1558 if (Values.size() > 1)
1559 // Keep NAME at the end of the list. It makes record dumps a
1560 // bit prettier and allows TableGen tests to be written more
1561 // naturally. Tests can use CHECK-NEXT to look for Record
1562 // fields they expect to see after a def. They can't do that if
1563 // NAME is the first Record field.
1564 std::swap(Values[Values.size() - 2], Values[Values.size() - 1]);
1567 void removeValue(Init *Name) {
1568 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1569 if (Values[i].getNameInit() == Name) {
1570 Values.erase(Values.begin()+i);
1573 llvm_unreachable("Cannot remove an entry that does not exist!");
1576 void removeValue(StringRef Name) {
1577 removeValue(StringInit::get(Name.str()));
1580 bool isSubClassOf(const Record *R) const {
1581 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1582 if (SuperClasses[i] == R)
1587 bool isSubClassOf(StringRef Name) const {
1588 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1589 if (SuperClasses[i]->getNameInitAsString() == Name)
1594 void addSuperClass(Record *R) {
1595 assert(!isSubClassOf(R) && "Already subclassing record!");
1596 SuperClasses.push_back(R);
1599 /// resolveReferences - If there are any field references that refer to fields
1600 /// that have been filled in, we can propagate the values now.
1602 void resolveReferences() { resolveReferencesTo(0); }
1604 /// resolveReferencesTo - If anything in this record refers to RV, replace the
1605 /// reference to RV with the RHS of RV. If RV is null, we resolve all
1606 /// possible references.
1607 void resolveReferencesTo(const RecordVal *RV);
1609 RecordKeeper &getRecords() const {
1610 return TrackedRecords;
1615 //===--------------------------------------------------------------------===//
1616 // High-level methods useful to tablegen back-ends
1619 /// getValueInit - Return the initializer for a value with the specified name,
1620 /// or throw an exception if the field does not exist.
1622 Init *getValueInit(StringRef FieldName) const;
1624 /// getValueAsString - This method looks up the specified field and returns
1625 /// its value as a string, throwing an exception if the field does not exist
1626 /// or if the value is not a string.
1628 std::string getValueAsString(StringRef FieldName) const;
1630 /// getValueAsBitsInit - This method looks up the specified field and returns
1631 /// its value as a BitsInit, throwing an exception if the field does not exist
1632 /// or if the value is not the right type.
1634 BitsInit *getValueAsBitsInit(StringRef FieldName) const;
1636 /// getValueAsListInit - This method looks up the specified field and returns
1637 /// its value as a ListInit, throwing an exception if the field does not exist
1638 /// or if the value is not the right type.
1640 ListInit *getValueAsListInit(StringRef FieldName) const;
1642 /// getValueAsListOfDefs - This method looks up the specified field and
1643 /// returns its value as a vector of records, throwing an exception if the
1644 /// field does not exist or if the value is not the right type.
1646 std::vector<Record*> getValueAsListOfDefs(StringRef FieldName) const;
1648 /// getValueAsListOfInts - This method looks up the specified field and
1649 /// returns its value as a vector of integers, throwing an exception if the
1650 /// field does not exist or if the value is not the right type.
1652 std::vector<int64_t> getValueAsListOfInts(StringRef FieldName) const;
1654 /// getValueAsListOfStrings - This method looks up the specified field and
1655 /// returns its value as a vector of strings, throwing an exception if the
1656 /// field does not exist or if the value is not the right type.
1658 std::vector<std::string> getValueAsListOfStrings(StringRef FieldName) const;
1660 /// getValueAsDef - This method looks up the specified field and returns its
1661 /// value as a Record, throwing an exception if the field does not exist or if
1662 /// the value is not the right type.
1664 Record *getValueAsDef(StringRef FieldName) const;
1666 /// getValueAsBit - This method looks up the specified field and returns its
1667 /// value as a bit, throwing an exception if the field does not exist or if
1668 /// the value is not the right type.
1670 bool getValueAsBit(StringRef FieldName) const;
1672 /// getValueAsBitOrUnset - This method looks up the specified field and
1673 /// returns its value as a bit. If the field is unset, sets Unset to true and
1676 bool getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const;
1678 /// getValueAsInt - This method looks up the specified field and returns its
1679 /// value as an int64_t, throwing an exception if the field does not exist or
1680 /// if the value is not the right type.
1682 int64_t getValueAsInt(StringRef FieldName) const;
1684 /// getValueAsDag - This method looks up the specified field and returns its
1685 /// value as an Dag, throwing an exception if the field does not exist or if
1686 /// the value is not the right type.
1688 DagInit *getValueAsDag(StringRef FieldName) const;
1691 raw_ostream &operator<<(raw_ostream &OS, const Record &R);
1694 Record Rec; // Placeholder for template args and Name.
1695 typedef std::vector<Record*> RecordVector;
1696 RecordVector DefPrototypes;
1700 MultiClass(const std::string &Name, SMLoc Loc, RecordKeeper &Records) :
1701 Rec(Name, Loc, Records) {}
1704 class RecordKeeper {
1705 std::map<std::string, Record*> Classes, Defs;
1709 for (std::map<std::string, Record*>::iterator I = Classes.begin(),
1710 E = Classes.end(); I != E; ++I)
1712 for (std::map<std::string, Record*>::iterator I = Defs.begin(),
1713 E = Defs.end(); I != E; ++I)
1717 const std::map<std::string, Record*> &getClasses() const { return Classes; }
1718 const std::map<std::string, Record*> &getDefs() const { return Defs; }
1720 Record *getClass(const std::string &Name) const {
1721 std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
1722 return I == Classes.end() ? 0 : I->second;
1724 Record *getDef(const std::string &Name) const {
1725 std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
1726 return I == Defs.end() ? 0 : I->second;
1728 void addClass(Record *R) {
1729 bool Ins = Classes.insert(std::make_pair(R->getName(), R)).second;
1731 assert(Ins && "Class already exists");
1733 void addDef(Record *R) {
1734 bool Ins = Defs.insert(std::make_pair(R->getName(), R)).second;
1736 assert(Ins && "Record already exists");
1739 /// removeClass - Remove, but do not delete, the specified record.
1741 void removeClass(const std::string &Name) {
1742 assert(Classes.count(Name) && "Class does not exist!");
1743 Classes.erase(Name);
1745 /// removeDef - Remove, but do not delete, the specified record.
1747 void removeDef(const std::string &Name) {
1748 assert(Defs.count(Name) && "Def does not exist!");
1752 //===--------------------------------------------------------------------===//
1753 // High-level helper methods, useful for tablegen backends...
1755 /// getAllDerivedDefinitions - This method returns all concrete definitions
1756 /// that derive from the specified class name. If a class with the specified
1757 /// name does not exist, an exception is thrown.
1758 std::vector<Record*>
1759 getAllDerivedDefinitions(const std::string &ClassName) const;
1764 /// LessRecord - Sorting predicate to sort record pointers by name.
1767 bool operator()(const Record *Rec1, const Record *Rec2) const {
1768 return StringRef(Rec1->getName()).compare_numeric(Rec2->getName()) < 0;
1772 /// LessRecordByID - Sorting predicate to sort record pointers by their
1773 /// unique ID. If you just need a deterministic order, use this, since it
1774 /// just compares two `unsigned`; the other sorting predicates require
1775 /// string manipulation.
1776 struct LessRecordByID {
1777 bool operator()(const Record *LHS, const Record *RHS) const {
1778 return LHS->getID() < RHS->getID();
1782 /// LessRecordFieldName - Sorting predicate to sort record pointers by their
1785 struct LessRecordFieldName {
1786 bool operator()(const Record *Rec1, const Record *Rec2) const {
1787 return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name");
1791 raw_ostream &operator<<(raw_ostream &OS, const RecordKeeper &RK);
1793 /// QualifyName - Return an Init with a qualifier prefix referring
1794 /// to CurRec's name.
1795 Init *QualifyName(Record &CurRec, MultiClass *CurMultiClass,
1796 Init *Name, const std::string &Scoper);
1798 /// QualifyName - Return an Init with a qualifier prefix referring
1799 /// to CurRec's name.
1800 Init *QualifyName(Record &CurRec, MultiClass *CurMultiClass,
1801 const std::string &Name, const std::string &Scoper);
1803 } // End llvm namespace