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/SourceMgr.h"
22 #include "llvm/Support/DataTypes.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/raw_ostream.h"
56 class VarListElementInit;
64 //===----------------------------------------------------------------------===//
66 //===----------------------------------------------------------------------===//
70 virtual void anchor();
72 RecTy() : ListTy(0) {}
75 virtual std::string getAsString() const = 0;
76 void print(raw_ostream &OS) const { OS << getAsString(); }
79 /// typeIsConvertibleTo - Return true if all values of 'this' type can be
80 /// converted to the specified type.
81 virtual bool typeIsConvertibleTo(const RecTy *RHS) const = 0;
83 /// getListTy - Returns the type representing list<this>.
84 ListRecTy *getListTy();
86 public: // These methods should only be called from subclasses of Init
87 virtual Init *convertValue( UnsetInit *UI) { return 0; }
88 virtual Init *convertValue( BitInit *BI) { return 0; }
89 virtual Init *convertValue( BitsInit *BI) { return 0; }
90 virtual Init *convertValue( IntInit *II) { return 0; }
91 virtual Init *convertValue(StringInit *SI) { return 0; }
92 virtual Init *convertValue( ListInit *LI) { return 0; }
93 virtual Init *convertValue( UnOpInit *UI) {
94 return convertValue((TypedInit*)UI);
96 virtual Init *convertValue( BinOpInit *UI) {
97 return convertValue((TypedInit*)UI);
99 virtual Init *convertValue( TernOpInit *UI) {
100 return convertValue((TypedInit*)UI);
102 virtual Init *convertValue(VarBitInit *VB) { return 0; }
103 virtual Init *convertValue( DefInit *DI) { return 0; }
104 virtual Init *convertValue( DagInit *DI) { return 0; }
105 virtual Init *convertValue( TypedInit *TI) { return 0; }
106 virtual Init *convertValue( VarInit *VI) {
107 return convertValue((TypedInit*)VI);
109 virtual Init *convertValue( FieldInit *FI) {
110 return convertValue((TypedInit*)FI);
113 public: // These methods should only be called by subclasses of RecTy.
114 // baseClassOf - These virtual methods should be overloaded to return true iff
115 // all values of type 'RHS' can be converted to the 'this' type.
116 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
117 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
118 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
119 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
120 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
121 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
122 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
125 inline raw_ostream &operator<<(raw_ostream &OS, const RecTy &Ty) {
131 /// BitRecTy - 'bit' - Represent a single bit
133 class BitRecTy : public RecTy {
134 static BitRecTy Shared;
137 static BitRecTy *get() { return &Shared; }
139 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
140 virtual Init *convertValue( BitInit *BI) { return (Init*)BI; }
141 virtual Init *convertValue( BitsInit *BI);
142 virtual Init *convertValue( IntInit *II);
143 virtual Init *convertValue(StringInit *SI) { return 0; }
144 virtual Init *convertValue( ListInit *LI) { return 0; }
145 virtual Init *convertValue(VarBitInit *VB) { return (Init*)VB; }
146 virtual Init *convertValue( DefInit *DI) { return 0; }
147 virtual Init *convertValue( DagInit *DI) { return 0; }
148 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
149 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
150 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
151 virtual Init *convertValue( TypedInit *TI);
152 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
153 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
155 std::string getAsString() const { return "bit"; }
157 bool typeIsConvertibleTo(const RecTy *RHS) const {
158 return RHS->baseClassOf(this);
160 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
161 virtual bool baseClassOf(const BitsRecTy *RHS) const;
162 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
163 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
164 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
165 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
166 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
171 // BitsRecTy - 'bits<n>' - Represent a fixed number of bits
172 /// BitsRecTy - 'bits<n>' - Represent a fixed number of bits
174 class BitsRecTy : public RecTy {
176 explicit BitsRecTy(unsigned Sz) : Size(Sz) {}
178 static BitsRecTy *get(unsigned Sz);
180 unsigned getNumBits() const { return Size; }
182 virtual Init *convertValue( UnsetInit *UI);
183 virtual Init *convertValue( BitInit *UI);
184 virtual Init *convertValue( BitsInit *BI);
185 virtual Init *convertValue( IntInit *II);
186 virtual Init *convertValue(StringInit *SI) { return 0; }
187 virtual Init *convertValue( ListInit *LI) { return 0; }
188 virtual Init *convertValue(VarBitInit *VB) { return 0; }
189 virtual Init *convertValue( DefInit *DI) { return 0; }
190 virtual Init *convertValue( DagInit *DI) { return 0; }
191 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
192 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
193 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
194 virtual Init *convertValue( TypedInit *TI);
195 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
196 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
198 std::string getAsString() const;
200 bool typeIsConvertibleTo(const RecTy *RHS) const {
201 return RHS->baseClassOf(this);
203 virtual bool baseClassOf(const BitRecTy *RHS) const { return Size == 1; }
204 virtual bool baseClassOf(const BitsRecTy *RHS) const {
205 return RHS->Size == Size;
207 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
208 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
209 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
210 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
211 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
216 /// IntRecTy - 'int' - Represent an integer value of no particular size
218 class IntRecTy : public RecTy {
219 static IntRecTy Shared;
222 static IntRecTy *get() { return &Shared; }
224 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
225 virtual Init *convertValue( BitInit *BI);
226 virtual Init *convertValue( BitsInit *BI);
227 virtual Init *convertValue( IntInit *II) { return (Init*)II; }
228 virtual Init *convertValue(StringInit *SI) { return 0; }
229 virtual Init *convertValue( ListInit *LI) { return 0; }
230 virtual Init *convertValue(VarBitInit *VB) { return 0; }
231 virtual Init *convertValue( DefInit *DI) { return 0; }
232 virtual Init *convertValue( DagInit *DI) { return 0; }
233 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
234 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
235 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
236 virtual Init *convertValue( TypedInit *TI);
237 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
238 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
240 std::string getAsString() const { return "int"; }
242 bool typeIsConvertibleTo(const RecTy *RHS) const {
243 return RHS->baseClassOf(this);
246 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
247 virtual bool baseClassOf(const BitsRecTy *RHS) const { return true; }
248 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
249 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
250 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
251 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
252 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
256 /// StringRecTy - 'string' - Represent an string value
258 class StringRecTy : public RecTy {
259 static StringRecTy Shared;
262 static StringRecTy *get() { return &Shared; }
264 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
265 virtual Init *convertValue( BitInit *BI) { return 0; }
266 virtual Init *convertValue( BitsInit *BI) { return 0; }
267 virtual Init *convertValue( IntInit *II) { return 0; }
268 virtual Init *convertValue(StringInit *SI) { return (Init*)SI; }
269 virtual Init *convertValue( ListInit *LI) { return 0; }
270 virtual Init *convertValue( UnOpInit *BO);
271 virtual Init *convertValue( BinOpInit *BO);
272 virtual Init *convertValue( TernOpInit *BO) { return RecTy::convertValue(BO);}
274 virtual Init *convertValue(VarBitInit *VB) { return 0; }
275 virtual Init *convertValue( DefInit *DI) { return 0; }
276 virtual Init *convertValue( DagInit *DI) { return 0; }
277 virtual Init *convertValue( TypedInit *TI);
278 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
279 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
281 std::string getAsString() const { return "string"; }
283 bool typeIsConvertibleTo(const RecTy *RHS) const {
284 return RHS->baseClassOf(this);
287 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
288 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
289 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
290 virtual bool baseClassOf(const StringRecTy *RHS) const { return true; }
291 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
292 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
293 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
296 // ListRecTy - 'list<Ty>' - Represent a list of values, all of which must be of
297 // the specified type.
298 /// ListRecTy - 'list<Ty>' - Represent a list of values, all of which must
299 /// be of the specified type.
301 class ListRecTy : public RecTy {
303 explicit ListRecTy(RecTy *T) : Ty(T) {}
304 friend ListRecTy *RecTy::getListTy();
306 static ListRecTy *get(RecTy *T) { return T->getListTy(); }
307 RecTy *getElementType() const { return Ty; }
309 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
310 virtual Init *convertValue( BitInit *BI) { return 0; }
311 virtual Init *convertValue( BitsInit *BI) { return 0; }
312 virtual Init *convertValue( IntInit *II) { return 0; }
313 virtual Init *convertValue(StringInit *SI) { return 0; }
314 virtual Init *convertValue( ListInit *LI);
315 virtual Init *convertValue(VarBitInit *VB) { return 0; }
316 virtual Init *convertValue( DefInit *DI) { return 0; }
317 virtual Init *convertValue( DagInit *DI) { return 0; }
318 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
319 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
320 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
321 virtual Init *convertValue( TypedInit *TI);
322 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
323 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
325 std::string getAsString() const;
327 bool typeIsConvertibleTo(const RecTy *RHS) const {
328 return RHS->baseClassOf(this);
331 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
332 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
333 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
334 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
335 virtual bool baseClassOf(const ListRecTy *RHS) const {
336 return RHS->getElementType()->typeIsConvertibleTo(Ty);
338 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
339 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
342 /// DagRecTy - 'dag' - Represent a dag fragment
344 class DagRecTy : public RecTy {
345 static DagRecTy Shared;
348 static DagRecTy *get() { return &Shared; }
350 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
351 virtual Init *convertValue( BitInit *BI) { return 0; }
352 virtual Init *convertValue( BitsInit *BI) { return 0; }
353 virtual Init *convertValue( IntInit *II) { return 0; }
354 virtual Init *convertValue(StringInit *SI) { return 0; }
355 virtual Init *convertValue( ListInit *LI) { return 0; }
356 virtual Init *convertValue(VarBitInit *VB) { return 0; }
357 virtual Init *convertValue( DefInit *DI) { return 0; }
358 virtual Init *convertValue( UnOpInit *BO);
359 virtual Init *convertValue( BinOpInit *BO);
360 virtual Init *convertValue( TernOpInit *BO) { return RecTy::convertValue(BO);}
361 virtual Init *convertValue( DagInit *CI) { return (Init*)CI; }
362 virtual Init *convertValue( TypedInit *TI);
363 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
364 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
366 std::string getAsString() const { return "dag"; }
368 bool typeIsConvertibleTo(const RecTy *RHS) const {
369 return RHS->baseClassOf(this);
372 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
373 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
374 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
375 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
376 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
377 virtual bool baseClassOf(const DagRecTy *RHS) const { return true; }
378 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
382 /// RecordRecTy - '[classname]' - Represent an instance of a class, such as:
385 class RecordRecTy : public RecTy {
387 explicit RecordRecTy(Record *R) : Rec(R) {}
390 static RecordRecTy *get(Record *R);
392 Record *getRecord() const { return Rec; }
394 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
395 virtual Init *convertValue( BitInit *BI) { return 0; }
396 virtual Init *convertValue( BitsInit *BI) { return 0; }
397 virtual Init *convertValue( IntInit *II) { return 0; }
398 virtual Init *convertValue(StringInit *SI) { return 0; }
399 virtual Init *convertValue( ListInit *LI) { return 0; }
400 virtual Init *convertValue(VarBitInit *VB) { return 0; }
401 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
402 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
403 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
404 virtual Init *convertValue( DefInit *DI);
405 virtual Init *convertValue( DagInit *DI) { return 0; }
406 virtual Init *convertValue( TypedInit *VI);
407 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
408 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
410 std::string getAsString() const;
412 bool typeIsConvertibleTo(const RecTy *RHS) const {
413 return RHS->baseClassOf(this);
415 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
416 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
417 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
418 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
419 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
420 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
421 virtual bool baseClassOf(const RecordRecTy *RHS) const;
424 /// resolveTypes - Find a common type that T1 and T2 convert to.
425 /// Return 0 if no such type exists.
427 RecTy *resolveTypes(RecTy *T1, RecTy *T2);
429 //===----------------------------------------------------------------------===//
430 // Initializer Classes
431 //===----------------------------------------------------------------------===//
434 Init(const Init &); // Do not define.
435 Init &operator=(const Init &); // Do not define.
436 virtual void anchor();
444 /// isComplete - This virtual method should be overridden by values that may
445 /// not be completely specified yet.
446 virtual bool isComplete() const { return true; }
448 /// print - Print out this value.
449 void print(raw_ostream &OS) const { OS << getAsString(); }
451 /// getAsString - Convert this value to a string form.
452 virtual std::string getAsString() const = 0;
453 /// getAsUnquotedString - Convert this value to a string form,
454 /// without adding quote markers. This primaruly affects
455 /// StringInits where we will not surround the string value with
457 virtual std::string getAsUnquotedString() const { return getAsString(); }
459 /// dump - Debugging method that may be called through a debugger, just
460 /// invokes print on stderr.
463 /// convertInitializerTo - This virtual function is a simple call-back
464 /// function that should be overridden to call the appropriate
465 /// RecTy::convertValue method.
467 virtual Init *convertInitializerTo(RecTy *Ty) const = 0;
469 /// convertInitializerBitRange - This method is used to implement the bitrange
470 /// selection operator. Given an initializer, it selects the specified bits
471 /// out, returning them as a new init of bits type. If it is not legal to use
472 /// the bit subscript operator on this initializer, return null.
475 convertInitializerBitRange(const std::vector<unsigned> &Bits) const {
479 /// convertInitListSlice - This method is used to implement the list slice
480 /// selection operator. Given an initializer, it selects the specified list
481 /// elements, returning them as a new init of list type. If it is not legal
482 /// to take a slice of this, return null.
485 convertInitListSlice(const std::vector<unsigned> &Elements) const {
489 /// getFieldType - This method is used to implement the FieldInit class.
490 /// Implementors of this method should return the type of the named field if
491 /// they are of record type.
493 virtual RecTy *getFieldType(const std::string &FieldName) const { return 0; }
495 /// getFieldInit - This method complements getFieldType to return the
496 /// initializer for the specified field. If getFieldType returns non-null
497 /// this method should return non-null, otherwise it returns null.
499 virtual Init *getFieldInit(Record &R, const RecordVal *RV,
500 const std::string &FieldName) const {
504 /// resolveReferences - This method is used by classes that refer to other
505 /// variables which may not be defined at the time the expression is formed.
506 /// If a value is set for the variable later, this method will be called on
507 /// users of the value to allow the value to propagate out.
509 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const {
510 return const_cast<Init *>(this);
514 inline raw_ostream &operator<<(raw_ostream &OS, const Init &I) {
515 I.print(OS); return OS;
518 /// TypedInit - This is the common super-class of types that have a specific,
521 class TypedInit : public Init {
524 TypedInit(const TypedInit &Other); // Do not define.
525 TypedInit &operator=(const TypedInit &Other); // Do not define.
528 explicit TypedInit(RecTy *T) : Ty(T) {}
531 RecTy *getType() const { return Ty; }
534 convertInitializerBitRange(const std::vector<unsigned> &Bits) const;
536 convertInitListSlice(const std::vector<unsigned> &Elements) const;
538 /// getFieldType - This method is used to implement the FieldInit class.
539 /// Implementors of this method should return the type of the named field if
540 /// they are of record type.
542 virtual RecTy *getFieldType(const std::string &FieldName) const;
544 /// resolveBitReference - This method is used to implement
545 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
546 /// simply return the resolved value, otherwise we return null.
548 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
549 unsigned Bit) const = 0;
551 /// resolveListElementReference - This method is used to implement
552 /// VarListElementInit::resolveReferences. If the list element is resolvable
553 /// now, we return the resolved value, otherwise we return null.
554 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
555 unsigned Elt) const = 0;
559 /// UnsetInit - ? - Represents an uninitialized value
561 class UnsetInit : public Init {
562 UnsetInit() : Init() {}
563 UnsetInit(const UnsetInit &); // Do not define.
564 UnsetInit &operator=(const UnsetInit &Other); // Do not define.
565 virtual void anchor();
568 static UnsetInit *get();
570 virtual Init *convertInitializerTo(RecTy *Ty) const {
571 return Ty->convertValue(const_cast<UnsetInit *>(this));
574 virtual bool isComplete() const { return false; }
575 virtual std::string getAsString() const { return "?"; }
579 /// BitInit - true/false - Represent a concrete initializer for a bit.
581 class BitInit : public Init {
584 explicit BitInit(bool V) : Value(V) {}
585 BitInit(const BitInit &Other); // Do not define.
586 BitInit &operator=(BitInit &Other); // Do not define.
587 virtual void anchor();
590 static BitInit *get(bool V);
592 bool getValue() const { return Value; }
594 virtual Init *convertInitializerTo(RecTy *Ty) const {
595 return Ty->convertValue(const_cast<BitInit *>(this));
598 virtual std::string getAsString() const { return Value ? "1" : "0"; }
601 /// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
602 /// It contains a vector of bits, whose size is determined by the type.
604 class BitsInit : public Init, public FoldingSetNode {
605 std::vector<Init*> Bits;
607 BitsInit(ArrayRef<Init *> Range) : Bits(Range.begin(), Range.end()) {}
609 BitsInit(const BitsInit &Other); // Do not define.
610 BitsInit &operator=(const BitsInit &Other); // Do not define.
613 static BitsInit *get(ArrayRef<Init *> Range);
615 void Profile(FoldingSetNodeID &ID) const;
617 unsigned getNumBits() const { return Bits.size(); }
619 Init *getBit(unsigned Bit) const {
620 assert(Bit < Bits.size() && "Bit index out of range!");
624 virtual Init *convertInitializerTo(RecTy *Ty) const {
625 return Ty->convertValue(const_cast<BitsInit *>(this));
628 convertInitializerBitRange(const std::vector<unsigned> &Bits) const;
630 virtual bool isComplete() const {
631 for (unsigned i = 0; i != getNumBits(); ++i)
632 if (!getBit(i)->isComplete()) return false;
635 bool allInComplete() const {
636 for (unsigned i = 0; i != getNumBits(); ++i)
637 if (getBit(i)->isComplete()) return false;
640 virtual std::string getAsString() const;
642 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
646 /// IntInit - 7 - Represent an initalization by a literal integer value.
648 class IntInit : public TypedInit {
651 explicit IntInit(int64_t V) : TypedInit(IntRecTy::get()), Value(V) {}
653 IntInit(const IntInit &Other); // Do not define.
654 IntInit &operator=(const IntInit &Other); // Do note define.
657 static IntInit *get(int64_t V);
659 int64_t getValue() const { return Value; }
661 virtual Init *convertInitializerTo(RecTy *Ty) const {
662 return Ty->convertValue(const_cast<IntInit *>(this));
665 convertInitializerBitRange(const std::vector<unsigned> &Bits) const;
667 virtual std::string getAsString() const;
669 /// resolveBitReference - This method is used to implement
670 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
671 /// simply return the resolved value, otherwise we return null.
673 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
674 unsigned Bit) const {
675 llvm_unreachable("Illegal bit reference off int");
678 /// resolveListElementReference - This method is used to implement
679 /// VarListElementInit::resolveReferences. If the list element is resolvable
680 /// now, we return the resolved value, otherwise we return null.
681 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
682 unsigned Elt) const {
683 llvm_unreachable("Illegal element reference off int");
688 /// StringInit - "foo" - Represent an initialization by a string value.
690 class StringInit : public TypedInit {
693 explicit StringInit(const std::string &V)
694 : TypedInit(StringRecTy::get()), Value(V) {}
696 StringInit(const StringInit &Other); // Do not define.
697 StringInit &operator=(const StringInit &Other); // Do not define.
698 virtual void anchor();
701 static StringInit *get(StringRef);
703 const std::string &getValue() const { return Value; }
705 virtual Init *convertInitializerTo(RecTy *Ty) const {
706 return Ty->convertValue(const_cast<StringInit *>(this));
709 virtual std::string getAsString() const { return "\"" + Value + "\""; }
710 virtual std::string getAsUnquotedString() const { return Value; }
712 /// resolveBitReference - This method is used to implement
713 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
714 /// simply return the resolved value, otherwise we return null.
716 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
717 unsigned Bit) const {
718 llvm_unreachable("Illegal bit reference off string");
721 /// resolveListElementReference - This method is used to implement
722 /// VarListElementInit::resolveReferences. If the list element is resolvable
723 /// now, we return the resolved value, otherwise we return null.
724 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
725 unsigned Elt) const {
726 llvm_unreachable("Illegal element reference off string");
730 /// ListInit - [AL, AH, CL] - Represent a list of defs
732 class ListInit : public TypedInit, public FoldingSetNode {
733 std::vector<Init*> Values;
735 typedef std::vector<Init*>::const_iterator const_iterator;
738 explicit ListInit(ArrayRef<Init *> Range, RecTy *EltTy)
739 : TypedInit(ListRecTy::get(EltTy)), Values(Range.begin(), Range.end()) {}
741 ListInit(const ListInit &Other); // Do not define.
742 ListInit &operator=(const ListInit &Other); // Do not define.
745 static ListInit *get(ArrayRef<Init *> Range, RecTy *EltTy);
747 void Profile(FoldingSetNodeID &ID) const;
749 unsigned getSize() const { return Values.size(); }
750 Init *getElement(unsigned i) const {
751 assert(i < Values.size() && "List element index out of range!");
755 Record *getElementAsRecord(unsigned i) const;
757 Init *convertInitListSlice(const std::vector<unsigned> &Elements) const;
759 virtual Init *convertInitializerTo(RecTy *Ty) const {
760 return Ty->convertValue(const_cast<ListInit *>(this));
763 /// resolveReferences - This method is used by classes that refer to other
764 /// variables which may not be defined at the time they expression is formed.
765 /// If a value is set for the variable later, this method will be called on
766 /// users of the value to allow the value to propagate out.
768 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
770 virtual std::string getAsString() const;
772 ArrayRef<Init*> getValues() const { return Values; }
774 inline const_iterator begin() const { return Values.begin(); }
775 inline const_iterator end () const { return Values.end(); }
777 inline size_t size () const { return Values.size(); }
778 inline bool empty() const { return Values.empty(); }
780 /// resolveBitReference - This method is used to implement
781 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
782 /// simply return the resolved value, otherwise we return null.
784 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
785 unsigned Bit) const {
786 llvm_unreachable("Illegal bit reference off list");
789 /// resolveListElementReference - This method is used to implement
790 /// VarListElementInit::resolveReferences. If the list element is resolvable
791 /// now, we return the resolved value, otherwise we return null.
792 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
797 /// OpInit - Base class for operators
799 class OpInit : public TypedInit {
800 OpInit(const OpInit &Other); // Do not define.
801 OpInit &operator=(OpInit &Other); // Do not define.
804 explicit OpInit(RecTy *Type) : TypedInit(Type) {}
807 // Clone - Clone this operator, replacing arguments with the new list
808 virtual OpInit *clone(std::vector<Init *> &Operands) const = 0;
810 virtual int getNumOperands() const = 0;
811 virtual Init *getOperand(int i) const = 0;
813 // Fold - If possible, fold this to a simpler init. Return this if not
815 virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const = 0;
817 virtual Init *convertInitializerTo(RecTy *Ty) const {
818 return Ty->convertValue(const_cast<OpInit *>(this));
821 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
823 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
828 /// UnOpInit - !op (X) - Transform an init.
830 class UnOpInit : public OpInit {
832 enum UnaryOp { CAST, HEAD, TAIL, EMPTY };
837 UnOpInit(UnaryOp opc, Init *lhs, RecTy *Type)
838 : OpInit(Type), Opc(opc), LHS(lhs) {}
840 UnOpInit(const UnOpInit &Other); // Do not define.
841 UnOpInit &operator=(const UnOpInit &Other); // Do not define.
844 static UnOpInit *get(UnaryOp opc, Init *lhs, RecTy *Type);
846 // Clone - Clone this operator, replacing arguments with the new list
847 virtual OpInit *clone(std::vector<Init *> &Operands) const {
848 assert(Operands.size() == 1 &&
849 "Wrong number of operands for unary operation");
850 return UnOpInit::get(getOpcode(), *Operands.begin(), getType());
853 int getNumOperands() const { return 1; }
854 Init *getOperand(int i) const {
855 assert(i == 0 && "Invalid operand id for unary operator");
859 UnaryOp getOpcode() const { return Opc; }
860 Init *getOperand() const { return LHS; }
862 // Fold - If possible, fold this to a simpler init. Return this if not
864 Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const;
866 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
868 virtual std::string getAsString() const;
871 /// BinOpInit - !op (X, Y) - Combine two inits.
873 class BinOpInit : public OpInit {
875 enum BinaryOp { SHL, SRA, SRL, STRCONCAT, CONCAT, EQ };
880 BinOpInit(BinaryOp opc, Init *lhs, Init *rhs, RecTy *Type) :
881 OpInit(Type), Opc(opc), LHS(lhs), RHS(rhs) {}
883 BinOpInit(const BinOpInit &Other); // Do not define.
884 BinOpInit &operator=(const BinOpInit &Other); // Do not define.
887 static BinOpInit *get(BinaryOp opc, Init *lhs, Init *rhs,
890 // Clone - Clone this operator, replacing arguments with the new list
891 virtual OpInit *clone(std::vector<Init *> &Operands) const {
892 assert(Operands.size() == 2 &&
893 "Wrong number of operands for binary operation");
894 return BinOpInit::get(getOpcode(), Operands[0], Operands[1], getType());
897 int getNumOperands() const { return 2; }
898 Init *getOperand(int i) const {
899 assert((i == 0 || i == 1) && "Invalid operand id for binary operator");
907 BinaryOp getOpcode() const { return Opc; }
908 Init *getLHS() const { return LHS; }
909 Init *getRHS() const { return RHS; }
911 // Fold - If possible, fold this to a simpler init. Return this if not
913 Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const;
915 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
917 virtual std::string getAsString() const;
920 /// TernOpInit - !op (X, Y, Z) - Combine two inits.
922 class TernOpInit : public OpInit {
924 enum TernaryOp { SUBST, FOREACH, IF };
927 Init *LHS, *MHS, *RHS;
929 TernOpInit(TernaryOp opc, Init *lhs, Init *mhs, Init *rhs,
931 OpInit(Type), Opc(opc), LHS(lhs), MHS(mhs), RHS(rhs) {}
933 TernOpInit(const TernOpInit &Other); // Do not define.
934 TernOpInit &operator=(const TernOpInit &Other); // Do not define.
937 static TernOpInit *get(TernaryOp opc, Init *lhs,
938 Init *mhs, Init *rhs,
941 // Clone - Clone this operator, replacing arguments with the new list
942 virtual OpInit *clone(std::vector<Init *> &Operands) const {
943 assert(Operands.size() == 3 &&
944 "Wrong number of operands for ternary operation");
945 return TernOpInit::get(getOpcode(), Operands[0], Operands[1], Operands[2],
949 int getNumOperands() const { return 3; }
950 Init *getOperand(int i) const {
951 assert((i == 0 || i == 1 || i == 2) &&
952 "Invalid operand id for ternary operator");
962 TernaryOp getOpcode() const { return Opc; }
963 Init *getLHS() const { return LHS; }
964 Init *getMHS() const { return MHS; }
965 Init *getRHS() const { return RHS; }
967 // Fold - If possible, fold this to a simpler init. Return this if not
969 Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const;
971 virtual bool isComplete() const { return false; }
973 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
975 virtual std::string getAsString() const;
979 /// VarInit - 'Opcode' - Represent a reference to an entire variable object.
981 class VarInit : public TypedInit {
984 explicit VarInit(const std::string &VN, RecTy *T)
985 : TypedInit(T), VarName(StringInit::get(VN)) {}
986 explicit VarInit(Init *VN, RecTy *T)
987 : TypedInit(T), VarName(VN) {}
989 VarInit(const VarInit &Other); // Do not define.
990 VarInit &operator=(const VarInit &Other); // Do not define.
993 static VarInit *get(const std::string &VN, RecTy *T);
994 static VarInit *get(Init *VN, RecTy *T);
996 virtual Init *convertInitializerTo(RecTy *Ty) const {
997 return Ty->convertValue(const_cast<VarInit *>(this));
1000 const std::string &getName() const;
1001 Init *getNameInit() const { return VarName; }
1002 std::string getNameInitAsString() const {
1003 return getNameInit()->getAsUnquotedString();
1006 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
1007 unsigned Bit) const;
1008 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
1009 unsigned Elt) const;
1011 virtual RecTy *getFieldType(const std::string &FieldName) const;
1012 virtual Init *getFieldInit(Record &R, const RecordVal *RV,
1013 const std::string &FieldName) const;
1015 /// resolveReferences - This method is used by classes that refer to other
1016 /// variables which may not be defined at the time they expression is formed.
1017 /// If a value is set for the variable later, this method will be called on
1018 /// users of the value to allow the value to propagate out.
1020 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
1022 virtual std::string getAsString() const { return getName(); }
1026 /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
1028 class VarBitInit : public Init {
1032 VarBitInit(TypedInit *T, unsigned B) : TI(T), Bit(B) {
1033 assert(T->getType() && dynamic_cast<BitsRecTy*>(T->getType()) &&
1034 ((BitsRecTy*)T->getType())->getNumBits() > B &&
1035 "Illegal VarBitInit expression!");
1038 VarBitInit(const VarBitInit &Other); // Do not define.
1039 VarBitInit &operator=(const VarBitInit &Other); // Do not define.
1042 static VarBitInit *get(TypedInit *T, unsigned B);
1044 virtual Init *convertInitializerTo(RecTy *Ty) const {
1045 return Ty->convertValue(const_cast<VarBitInit *>(this));
1048 TypedInit *getVariable() const { return TI; }
1049 unsigned getBitNum() const { return Bit; }
1051 virtual std::string getAsString() const;
1052 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
1055 /// VarListElementInit - List[4] - Represent access to one element of a var or
1057 class VarListElementInit : public TypedInit {
1061 VarListElementInit(TypedInit *T, unsigned E)
1062 : TypedInit(dynamic_cast<ListRecTy*>(T->getType())->getElementType()),
1064 assert(T->getType() && dynamic_cast<ListRecTy*>(T->getType()) &&
1065 "Illegal VarBitInit expression!");
1068 VarListElementInit(const VarListElementInit &Other); // Do not define.
1069 VarListElementInit &operator=(const VarListElementInit &Other); // Do
1074 static VarListElementInit *get(TypedInit *T, unsigned E);
1076 virtual Init *convertInitializerTo(RecTy *Ty) const {
1077 return Ty->convertValue(const_cast<VarListElementInit *>(this));
1080 TypedInit *getVariable() const { return TI; }
1081 unsigned getElementNum() const { return Element; }
1083 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
1084 unsigned Bit) const;
1086 /// resolveListElementReference - This method is used to implement
1087 /// VarListElementInit::resolveReferences. If the list element is resolvable
1088 /// now, we return the resolved value, otherwise we return null.
1089 virtual Init *resolveListElementReference(Record &R,
1090 const RecordVal *RV,
1091 unsigned Elt) const;
1093 virtual std::string getAsString() const;
1094 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
1097 /// DefInit - AL - Represent a reference to a 'def' in the description
1099 class DefInit : public TypedInit {
1102 DefInit(Record *D, RecordRecTy *T) : TypedInit(T), Def(D) {}
1103 friend class Record;
1105 DefInit(const DefInit &Other); // Do not define.
1106 DefInit &operator=(const DefInit &Other); // Do not define.
1109 static DefInit *get(Record*);
1111 virtual Init *convertInitializerTo(RecTy *Ty) const {
1112 return Ty->convertValue(const_cast<DefInit *>(this));
1115 Record *getDef() const { return Def; }
1117 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
1119 virtual RecTy *getFieldType(const std::string &FieldName) const;
1120 virtual Init *getFieldInit(Record &R, const RecordVal *RV,
1121 const std::string &FieldName) const;
1123 virtual std::string getAsString() const;
1125 /// resolveBitReference - This method is used to implement
1126 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
1127 /// simply return the resolved value, otherwise we return null.
1129 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
1130 unsigned Bit) const {
1131 llvm_unreachable("Illegal bit reference off def");
1134 /// resolveListElementReference - This method is used to implement
1135 /// VarListElementInit::resolveReferences. If the list element is resolvable
1136 /// now, we return the resolved value, otherwise we return null.
1137 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
1138 unsigned Elt) const {
1139 llvm_unreachable("Illegal element reference off def");
1144 /// FieldInit - X.Y - Represent a reference to a subfield of a variable
1146 class FieldInit : public TypedInit {
1147 Init *Rec; // Record we are referring to
1148 std::string FieldName; // Field we are accessing
1150 FieldInit(Init *R, const std::string &FN)
1151 : TypedInit(R->getFieldType(FN)), Rec(R), FieldName(FN) {
1152 assert(getType() && "FieldInit with non-record type!");
1155 FieldInit(const FieldInit &Other); // Do not define.
1156 FieldInit &operator=(const FieldInit &Other); // Do not define.
1159 static FieldInit *get(Init *R, const std::string &FN);
1160 static FieldInit *get(Init *R, const Init *FN);
1162 virtual Init *convertInitializerTo(RecTy *Ty) const {
1163 return Ty->convertValue(const_cast<FieldInit *>(this));
1166 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
1167 unsigned Bit) const;
1168 virtual Init *resolveListElementReference(Record &R,
1169 const RecordVal *RV,
1170 unsigned Elt) const;
1172 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
1174 virtual std::string getAsString() const {
1175 return Rec->getAsString() + "." + FieldName;
1179 /// DagInit - (v a, b) - Represent a DAG tree value. DAG inits are required
1180 /// to have at least one value then a (possibly empty) list of arguments. Each
1181 /// argument can have a name associated with it.
1183 class DagInit : public TypedInit, public FoldingSetNode {
1185 std::string ValName;
1186 std::vector<Init*> Args;
1187 std::vector<std::string> ArgNames;
1189 DagInit(Init *V, const std::string &VN,
1190 ArrayRef<Init *> ArgRange,
1191 ArrayRef<std::string> NameRange)
1192 : TypedInit(DagRecTy::get()), Val(V), ValName(VN),
1193 Args(ArgRange.begin(), ArgRange.end()),
1194 ArgNames(NameRange.begin(), NameRange.end()) {}
1196 DagInit(const DagInit &Other); // Do not define.
1197 DagInit &operator=(const DagInit &Other); // Do not define.
1200 static DagInit *get(Init *V, const std::string &VN,
1201 ArrayRef<Init *> ArgRange,
1202 ArrayRef<std::string> NameRange);
1203 static DagInit *get(Init *V, const std::string &VN,
1205 std::pair<Init*, std::string> > &args);
1207 void Profile(FoldingSetNodeID &ID) const;
1209 virtual Init *convertInitializerTo(RecTy *Ty) const {
1210 return Ty->convertValue(const_cast<DagInit *>(this));
1213 Init *getOperator() const { return Val; }
1215 const std::string &getName() const { return ValName; }
1217 unsigned getNumArgs() const { return Args.size(); }
1218 Init *getArg(unsigned Num) const {
1219 assert(Num < Args.size() && "Arg number out of range!");
1222 const std::string &getArgName(unsigned Num) const {
1223 assert(Num < ArgNames.size() && "Arg number out of range!");
1224 return ArgNames[Num];
1227 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
1229 virtual std::string getAsString() const;
1231 typedef std::vector<Init*>::const_iterator const_arg_iterator;
1232 typedef std::vector<std::string>::const_iterator const_name_iterator;
1234 inline const_arg_iterator arg_begin() const { return Args.begin(); }
1235 inline const_arg_iterator arg_end () const { return Args.end(); }
1237 inline size_t arg_size () const { return Args.size(); }
1238 inline bool arg_empty() const { return Args.empty(); }
1240 inline const_name_iterator name_begin() const { return ArgNames.begin(); }
1241 inline const_name_iterator name_end () const { return ArgNames.end(); }
1243 inline size_t name_size () const { return ArgNames.size(); }
1244 inline bool name_empty() const { return ArgNames.empty(); }
1246 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
1247 unsigned Bit) const {
1248 llvm_unreachable("Illegal bit reference off dag");
1251 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
1252 unsigned Elt) const {
1253 llvm_unreachable("Illegal element reference off dag");
1257 //===----------------------------------------------------------------------===//
1258 // High-Level Classes
1259 //===----------------------------------------------------------------------===//
1267 RecordVal(Init *N, RecTy *T, unsigned P);
1268 RecordVal(const std::string &N, RecTy *T, unsigned P);
1270 const std::string &getName() const;
1271 const Init *getNameInit() const { return Name; }
1272 std::string getNameInitAsString() const {
1273 return getNameInit()->getAsUnquotedString();
1276 unsigned getPrefix() const { return Prefix; }
1277 RecTy *getType() const { return Ty; }
1278 Init *getValue() const { return Value; }
1280 bool setValue(Init *V) {
1282 Value = V->convertInitializerTo(Ty);
1290 void print(raw_ostream &OS, bool PrintSem = true) const;
1293 inline raw_ostream &operator<<(raw_ostream &OS, const RecordVal &RV) {
1294 RV.print(OS << " ");
1299 static unsigned LastID;
1301 // Unique record ID.
1305 std::vector<Init *> TemplateArgs;
1306 std::vector<RecordVal> Values;
1307 std::vector<Record*> SuperClasses;
1309 // Tracks Record instances. Not owned by Record.
1310 RecordKeeper &TrackedRecords;
1319 // Constructs a record.
1320 explicit Record(const std::string &N, SMLoc loc, RecordKeeper &records) :
1321 ID(LastID++), Name(StringInit::get(N)), Loc(loc), TrackedRecords(records),
1325 explicit Record(Init *N, SMLoc loc, RecordKeeper &records) :
1326 ID(LastID++), Name(N), Loc(loc), TrackedRecords(records), TheInit(0) {
1332 static unsigned getNewUID() { return LastID++; }
1335 unsigned getID() const { return ID; }
1337 const std::string &getName() const;
1338 Init *getNameInit() const {
1341 const std::string getNameInitAsString() const {
1342 return getNameInit()->getAsUnquotedString();
1345 void setName(Init *Name); // Also updates RecordKeeper.
1346 void setName(const std::string &Name); // Also updates RecordKeeper.
1348 SMLoc getLoc() const { return Loc; }
1350 /// get the corresponding DefInit.
1351 DefInit *getDefInit();
1353 const std::vector<Init *> &getTemplateArgs() const {
1354 return TemplateArgs;
1356 const std::vector<RecordVal> &getValues() const { return Values; }
1357 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; }
1359 bool isTemplateArg(Init *Name) const {
1360 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
1361 if (TemplateArgs[i] == Name) return true;
1364 bool isTemplateArg(StringRef Name) const {
1365 return isTemplateArg(StringInit::get(Name.str()));
1368 const RecordVal *getValue(const Init *Name) const {
1369 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1370 if (Values[i].getNameInit() == Name) return &Values[i];
1373 const RecordVal *getValue(StringRef Name) const {
1374 return getValue(StringInit::get(Name));
1376 RecordVal *getValue(const Init *Name) {
1377 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1378 if (Values[i].getNameInit() == Name) return &Values[i];
1381 RecordVal *getValue(StringRef Name) {
1382 return getValue(StringInit::get(Name));
1385 void addTemplateArg(Init *Name) {
1386 assert(!isTemplateArg(Name) && "Template arg already defined!");
1387 TemplateArgs.push_back(Name);
1389 void addTemplateArg(StringRef Name) {
1390 addTemplateArg(StringInit::get(Name.str()));
1393 void addValue(const RecordVal &RV) {
1394 assert(getValue(RV.getNameInit()) == 0 && "Value already added!");
1395 Values.push_back(RV);
1396 if (Values.size() > 1)
1397 // Keep NAME at the end of the list. It makes record dumps a
1398 // bit prettier and allows TableGen tests to be written more
1399 // naturally. Tests can use CHECK-NEXT to look for Record
1400 // fields they expect to see after a def. They can't do that if
1401 // NAME is the first Record field.
1402 std::swap(Values[Values.size() - 2], Values[Values.size() - 1]);
1405 void removeValue(Init *Name) {
1406 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1407 if (Values[i].getNameInit() == Name) {
1408 Values.erase(Values.begin()+i);
1411 llvm_unreachable("Cannot remove an entry that does not exist!");
1414 void removeValue(StringRef Name) {
1415 removeValue(StringInit::get(Name.str()));
1418 bool isSubClassOf(const Record *R) const {
1419 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1420 if (SuperClasses[i] == R)
1425 bool isSubClassOf(StringRef Name) const {
1426 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1427 if (SuperClasses[i]->getNameInitAsString() == Name)
1432 void addSuperClass(Record *R) {
1433 assert(!isSubClassOf(R) && "Already subclassing record!");
1434 SuperClasses.push_back(R);
1437 /// resolveReferences - If there are any field references that refer to fields
1438 /// that have been filled in, we can propagate the values now.
1440 void resolveReferences() { resolveReferencesTo(0); }
1442 /// resolveReferencesTo - If anything in this record refers to RV, replace the
1443 /// reference to RV with the RHS of RV. If RV is null, we resolve all
1444 /// possible references.
1445 void resolveReferencesTo(const RecordVal *RV);
1447 RecordKeeper &getRecords() const {
1448 return TrackedRecords;
1453 //===--------------------------------------------------------------------===//
1454 // High-level methods useful to tablegen back-ends
1457 /// getValueInit - Return the initializer for a value with the specified name,
1458 /// or throw an exception if the field does not exist.
1460 Init *getValueInit(StringRef FieldName) const;
1462 /// getValueAsString - This method looks up the specified field and returns
1463 /// its value as a string, throwing an exception if the field does not exist
1464 /// or if the value is not a string.
1466 std::string getValueAsString(StringRef FieldName) const;
1468 /// getValueAsBitsInit - This method looks up the specified field and returns
1469 /// its value as a BitsInit, throwing an exception if the field does not exist
1470 /// or if the value is not the right type.
1472 BitsInit *getValueAsBitsInit(StringRef FieldName) const;
1474 /// getValueAsListInit - This method looks up the specified field and returns
1475 /// its value as a ListInit, throwing an exception if the field does not exist
1476 /// or if the value is not the right type.
1478 ListInit *getValueAsListInit(StringRef FieldName) const;
1480 /// getValueAsListOfDefs - This method looks up the specified field and
1481 /// returns its value as a vector of records, throwing an exception if the
1482 /// field does not exist or if the value is not the right type.
1484 std::vector<Record*> getValueAsListOfDefs(StringRef FieldName) const;
1486 /// getValueAsListOfInts - This method looks up the specified field and
1487 /// returns its value as a vector of integers, throwing an exception if the
1488 /// field does not exist or if the value is not the right type.
1490 std::vector<int64_t> getValueAsListOfInts(StringRef FieldName) const;
1492 /// getValueAsListOfStrings - This method looks up the specified field and
1493 /// returns its value as a vector of strings, throwing an exception if the
1494 /// field does not exist or if the value is not the right type.
1496 std::vector<std::string> getValueAsListOfStrings(StringRef FieldName) const;
1498 /// getValueAsDef - This method looks up the specified field and returns its
1499 /// value as a Record, throwing an exception if the field does not exist or if
1500 /// the value is not the right type.
1502 Record *getValueAsDef(StringRef FieldName) const;
1504 /// getValueAsBit - This method looks up the specified field and returns its
1505 /// value as a bit, throwing an exception if the field does not exist or if
1506 /// the value is not the right type.
1508 bool getValueAsBit(StringRef FieldName) const;
1510 /// getValueAsInt - This method looks up the specified field and returns its
1511 /// value as an int64_t, throwing an exception if the field does not exist or
1512 /// if the value is not the right type.
1514 int64_t getValueAsInt(StringRef FieldName) const;
1516 /// getValueAsDag - This method looks up the specified field and returns its
1517 /// value as an Dag, throwing an exception if the field does not exist or if
1518 /// the value is not the right type.
1520 DagInit *getValueAsDag(StringRef FieldName) const;
1523 raw_ostream &operator<<(raw_ostream &OS, const Record &R);
1526 Record Rec; // Placeholder for template args and Name.
1527 typedef std::vector<Record*> RecordVector;
1528 RecordVector DefPrototypes;
1532 MultiClass(const std::string &Name, SMLoc Loc, RecordKeeper &Records) :
1533 Rec(Name, Loc, Records) {}
1536 class RecordKeeper {
1537 std::map<std::string, Record*> Classes, Defs;
1541 for (std::map<std::string, Record*>::iterator I = Classes.begin(),
1542 E = Classes.end(); I != E; ++I)
1544 for (std::map<std::string, Record*>::iterator I = Defs.begin(),
1545 E = Defs.end(); I != E; ++I)
1549 const std::map<std::string, Record*> &getClasses() const { return Classes; }
1550 const std::map<std::string, Record*> &getDefs() const { return Defs; }
1552 Record *getClass(const std::string &Name) const {
1553 std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
1554 return I == Classes.end() ? 0 : I->second;
1556 Record *getDef(const std::string &Name) const {
1557 std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
1558 return I == Defs.end() ? 0 : I->second;
1560 void addClass(Record *R) {
1561 bool Ins = Classes.insert(std::make_pair(R->getName(), R)).second;
1563 assert(Ins && "Class already exists");
1565 void addDef(Record *R) {
1566 bool Ins = Defs.insert(std::make_pair(R->getName(), R)).second;
1568 assert(Ins && "Record already exists");
1571 /// removeClass - Remove, but do not delete, the specified record.
1573 void removeClass(const std::string &Name) {
1574 assert(Classes.count(Name) && "Class does not exist!");
1575 Classes.erase(Name);
1577 /// removeDef - Remove, but do not delete, the specified record.
1579 void removeDef(const std::string &Name) {
1580 assert(Defs.count(Name) && "Def does not exist!");
1584 //===--------------------------------------------------------------------===//
1585 // High-level helper methods, useful for tablegen backends...
1587 /// getAllDerivedDefinitions - This method returns all concrete definitions
1588 /// that derive from the specified class name. If a class with the specified
1589 /// name does not exist, an exception is thrown.
1590 std::vector<Record*>
1591 getAllDerivedDefinitions(const std::string &ClassName) const;
1596 /// LessRecord - Sorting predicate to sort record pointers by name.
1599 bool operator()(const Record *Rec1, const Record *Rec2) const {
1600 return StringRef(Rec1->getName()).compare_numeric(Rec2->getName()) < 0;
1604 /// LessRecordFieldName - Sorting predicate to sort record pointers by their
1607 struct LessRecordFieldName {
1608 bool operator()(const Record *Rec1, const Record *Rec2) const {
1609 return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name");
1613 raw_ostream &operator<<(raw_ostream &OS, const RecordKeeper &RK);
1615 /// QualifyName - Return an Init with a qualifier prefix referring
1616 /// to CurRec's name.
1617 Init *QualifyName(Record &CurRec, MultiClass *CurMultiClass,
1618 Init *Name, const std::string &Scoper);
1620 /// QualifyName - Return an Init with a qualifier prefix referring
1621 /// to CurRec's name.
1622 Init *QualifyName(Record &CurRec, MultiClass *CurMultiClass,
1623 const std::string &Name, const std::string &Scoper);
1625 } // End llvm namespace