1 //===- Record.h - Classes to represent 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 //===----------------------------------------------------------------------===//
18 #include "llvm/Support/SourceMgr.h"
19 #include "llvm/Support/DataTypes.h"
20 #include "llvm/Support/raw_ostream.h"
54 class VarListElementInit;
62 //===----------------------------------------------------------------------===//
64 //===----------------------------------------------------------------------===//
69 virtual std::string getAsString() const = 0;
70 void print(raw_ostream &OS) const { OS << getAsString(); }
73 /// typeIsConvertibleTo - Return true if all values of 'this' type can be
74 /// converted to the specified type.
75 virtual bool typeIsConvertibleTo(const RecTy *RHS) const = 0;
77 public: // These methods should only be called from subclasses of Init
78 virtual Init *convertValue( UnsetInit *UI) { return 0; }
79 virtual Init *convertValue( BitInit *BI) { return 0; }
80 virtual Init *convertValue( BitsInit *BI) { return 0; }
81 virtual Init *convertValue( IntInit *II) { return 0; }
82 virtual Init *convertValue(StringInit *SI) { return 0; }
83 virtual Init *convertValue( ListInit *LI) { return 0; }
84 virtual Init *convertValue( UnOpInit *UI) {
85 return convertValue((TypedInit*)UI);
87 virtual Init *convertValue( BinOpInit *UI) {
88 return convertValue((TypedInit*)UI);
90 virtual Init *convertValue( TernOpInit *UI) {
91 return convertValue((TypedInit*)UI);
93 virtual Init *convertValue( CodeInit *CI) { return 0; }
94 virtual Init *convertValue(VarBitInit *VB) { return 0; }
95 virtual Init *convertValue( DefInit *DI) { return 0; }
96 virtual Init *convertValue( DagInit *DI) { return 0; }
97 virtual Init *convertValue( TypedInit *TI) { return 0; }
98 virtual Init *convertValue( VarInit *VI) {
99 return convertValue((TypedInit*)VI);
101 virtual Init *convertValue( FieldInit *FI) {
102 return convertValue((TypedInit*)FI);
105 public: // These methods should only be called by subclasses of RecTy.
106 // baseClassOf - These virtual methods should be overloaded to return true iff
107 // all values of type 'RHS' can be converted to the 'this' type.
108 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
109 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
110 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
111 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
112 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
113 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
114 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
115 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
118 inline raw_ostream &operator<<(raw_ostream &OS, const RecTy &Ty) {
124 /// BitRecTy - 'bit' - Represent a single bit
126 class BitRecTy : public RecTy {
128 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
129 virtual Init *convertValue( BitInit *BI) { return (Init*)BI; }
130 virtual Init *convertValue( BitsInit *BI);
131 virtual Init *convertValue( IntInit *II);
132 virtual Init *convertValue(StringInit *SI) { return 0; }
133 virtual Init *convertValue( ListInit *LI) { return 0; }
134 virtual Init *convertValue( CodeInit *CI) { return 0; }
135 virtual Init *convertValue(VarBitInit *VB) { return (Init*)VB; }
136 virtual Init *convertValue( DefInit *DI) { return 0; }
137 virtual Init *convertValue( DagInit *DI) { return 0; }
138 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
139 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
140 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
141 virtual Init *convertValue( TypedInit *TI);
142 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
143 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
145 std::string getAsString() const { return "bit"; }
147 bool typeIsConvertibleTo(const RecTy *RHS) const {
148 return RHS->baseClassOf(this);
150 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
151 virtual bool baseClassOf(const BitsRecTy *RHS) const;
152 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
153 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
154 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
155 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
156 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
157 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
162 // BitsRecTy - 'bits<n>' - Represent a fixed number of bits
163 /// BitsRecTy - 'bits<n>' - Represent a fixed number of bits
165 class BitsRecTy : public RecTy {
168 explicit BitsRecTy(unsigned Sz) : Size(Sz) {}
170 unsigned getNumBits() const { return Size; }
172 virtual Init *convertValue( UnsetInit *UI);
173 virtual Init *convertValue( BitInit *UI);
174 virtual Init *convertValue( BitsInit *BI);
175 virtual Init *convertValue( IntInit *II);
176 virtual Init *convertValue(StringInit *SI) { return 0; }
177 virtual Init *convertValue( ListInit *LI) { return 0; }
178 virtual Init *convertValue( CodeInit *CI) { return 0; }
179 virtual Init *convertValue(VarBitInit *VB) { return 0; }
180 virtual Init *convertValue( DefInit *DI) { return 0; }
181 virtual Init *convertValue( DagInit *DI) { return 0; }
182 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
183 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
184 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
185 virtual Init *convertValue( TypedInit *TI);
186 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
187 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
189 std::string getAsString() const;
191 bool typeIsConvertibleTo(const RecTy *RHS) const {
192 return RHS->baseClassOf(this);
194 virtual bool baseClassOf(const BitRecTy *RHS) const { return Size == 1; }
195 virtual bool baseClassOf(const BitsRecTy *RHS) const {
196 return RHS->Size == Size;
198 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
199 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
200 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
201 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
202 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
203 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
208 /// IntRecTy - 'int' - Represent an integer value of no particular size
210 class IntRecTy : public RecTy {
212 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
213 virtual Init *convertValue( BitInit *BI);
214 virtual Init *convertValue( BitsInit *BI);
215 virtual Init *convertValue( IntInit *II) { return (Init*)II; }
216 virtual Init *convertValue(StringInit *SI) { return 0; }
217 virtual Init *convertValue( ListInit *LI) { return 0; }
218 virtual Init *convertValue( CodeInit *CI) { return 0; }
219 virtual Init *convertValue(VarBitInit *VB) { return 0; }
220 virtual Init *convertValue( DefInit *DI) { return 0; }
221 virtual Init *convertValue( DagInit *DI) { return 0; }
222 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
223 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
224 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
225 virtual Init *convertValue( TypedInit *TI);
226 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
227 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
229 std::string getAsString() const { return "int"; }
231 bool typeIsConvertibleTo(const RecTy *RHS) const {
232 return RHS->baseClassOf(this);
235 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
236 virtual bool baseClassOf(const BitsRecTy *RHS) const { return true; }
237 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
238 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
239 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
240 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
241 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
242 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
246 /// StringRecTy - 'string' - Represent an string value
248 class StringRecTy : public RecTy {
250 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
251 virtual Init *convertValue( BitInit *BI) { return 0; }
252 virtual Init *convertValue( BitsInit *BI) { return 0; }
253 virtual Init *convertValue( IntInit *II) { return 0; }
254 virtual Init *convertValue(StringInit *SI) { return (Init*)SI; }
255 virtual Init *convertValue( ListInit *LI) { return 0; }
256 virtual Init *convertValue( UnOpInit *BO);
257 virtual Init *convertValue( BinOpInit *BO);
258 virtual Init *convertValue( TernOpInit *BO) { return RecTy::convertValue(BO);}
260 virtual Init *convertValue( CodeInit *CI) { return 0; }
261 virtual Init *convertValue(VarBitInit *VB) { return 0; }
262 virtual Init *convertValue( DefInit *DI) { return 0; }
263 virtual Init *convertValue( DagInit *DI) { return 0; }
264 virtual Init *convertValue( TypedInit *TI);
265 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
266 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
268 std::string getAsString() const { return "string"; }
270 bool typeIsConvertibleTo(const RecTy *RHS) const {
271 return RHS->baseClassOf(this);
274 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
275 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
276 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
277 virtual bool baseClassOf(const StringRecTy *RHS) const { return true; }
278 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
279 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
280 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
281 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
284 // ListRecTy - 'list<Ty>' - Represent a list of values, all of which must be of
285 // the specified type.
286 /// ListRecTy - 'list<Ty>' - Represent a list of values, all of which must
287 /// be of the specified type.
289 class ListRecTy : public RecTy {
292 explicit ListRecTy(RecTy *T) : Ty(T) {}
294 RecTy *getElementType() const { return Ty; }
296 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
297 virtual Init *convertValue( BitInit *BI) { return 0; }
298 virtual Init *convertValue( BitsInit *BI) { return 0; }
299 virtual Init *convertValue( IntInit *II) { return 0; }
300 virtual Init *convertValue(StringInit *SI) { return 0; }
301 virtual Init *convertValue( ListInit *LI);
302 virtual Init *convertValue( CodeInit *CI) { return 0; }
303 virtual Init *convertValue(VarBitInit *VB) { return 0; }
304 virtual Init *convertValue( DefInit *DI) { return 0; }
305 virtual Init *convertValue( DagInit *DI) { return 0; }
306 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
307 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
308 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
309 virtual Init *convertValue( TypedInit *TI);
310 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
311 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
313 std::string getAsString() const;
315 bool typeIsConvertibleTo(const RecTy *RHS) const {
316 return RHS->baseClassOf(this);
319 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
320 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
321 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
322 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
323 virtual bool baseClassOf(const ListRecTy *RHS) const {
324 return RHS->getElementType()->typeIsConvertibleTo(Ty);
326 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
327 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
328 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
331 /// CodeRecTy - 'code' - Represent an code fragment, function or method.
333 class CodeRecTy : public RecTy {
335 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
336 virtual Init *convertValue( BitInit *BI) { return 0; }
337 virtual Init *convertValue( BitsInit *BI) { return 0; }
338 virtual Init *convertValue( IntInit *II) { return 0; }
339 virtual Init *convertValue(StringInit *SI) { return 0; }
340 virtual Init *convertValue( ListInit *LI) { return 0; }
341 virtual Init *convertValue( CodeInit *CI) { return (Init*)CI; }
342 virtual Init *convertValue(VarBitInit *VB) { return 0; }
343 virtual Init *convertValue( DefInit *DI) { return 0; }
344 virtual Init *convertValue( DagInit *DI) { return 0; }
345 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
346 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
347 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
348 virtual Init *convertValue( TypedInit *TI);
349 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
350 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
352 std::string getAsString() const { return "code"; }
354 bool typeIsConvertibleTo(const RecTy *RHS) const {
355 return RHS->baseClassOf(this);
357 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
358 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
359 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
360 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
361 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
362 virtual bool baseClassOf(const CodeRecTy *RHS) const { return true; }
363 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
364 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
367 /// DagRecTy - 'dag' - Represent a dag fragment
369 class DagRecTy : public RecTy {
371 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
372 virtual Init *convertValue( BitInit *BI) { return 0; }
373 virtual Init *convertValue( BitsInit *BI) { return 0; }
374 virtual Init *convertValue( IntInit *II) { return 0; }
375 virtual Init *convertValue(StringInit *SI) { return 0; }
376 virtual Init *convertValue( ListInit *LI) { return 0; }
377 virtual Init *convertValue( CodeInit *CI) { return 0; }
378 virtual Init *convertValue(VarBitInit *VB) { return 0; }
379 virtual Init *convertValue( DefInit *DI) { return 0; }
380 virtual Init *convertValue( UnOpInit *BO);
381 virtual Init *convertValue( BinOpInit *BO);
382 virtual Init *convertValue( TernOpInit *BO) { return RecTy::convertValue(BO);}
383 virtual Init *convertValue( DagInit *CI) { return (Init*)CI; }
384 virtual Init *convertValue( TypedInit *TI);
385 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
386 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
388 std::string getAsString() const { return "dag"; }
390 bool typeIsConvertibleTo(const RecTy *RHS) const {
391 return RHS->baseClassOf(this);
394 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
395 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
396 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
397 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
398 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
399 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
400 virtual bool baseClassOf(const DagRecTy *RHS) const { return true; }
401 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
405 /// RecordRecTy - '[classname]' - Represent an instance of a class, such as:
408 class RecordRecTy : public RecTy {
411 explicit RecordRecTy(Record *R) : Rec(R) {}
413 Record *getRecord() const { return Rec; }
415 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
416 virtual Init *convertValue( BitInit *BI) { return 0; }
417 virtual Init *convertValue( BitsInit *BI) { return 0; }
418 virtual Init *convertValue( IntInit *II) { return 0; }
419 virtual Init *convertValue(StringInit *SI) { return 0; }
420 virtual Init *convertValue( ListInit *LI) { return 0; }
421 virtual Init *convertValue( CodeInit *CI) { return 0; }
422 virtual Init *convertValue(VarBitInit *VB) { return 0; }
423 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
424 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
425 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
426 virtual Init *convertValue( DefInit *DI);
427 virtual Init *convertValue( DagInit *DI) { return 0; }
428 virtual Init *convertValue( TypedInit *VI);
429 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
430 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
432 std::string getAsString() const;
434 bool typeIsConvertibleTo(const RecTy *RHS) const {
435 return RHS->baseClassOf(this);
437 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
438 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
439 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
440 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
441 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
442 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
443 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
444 virtual bool baseClassOf(const RecordRecTy *RHS) const;
447 /// resolveTypes - Find a common type that T1 and T2 convert to.
448 /// Return 0 if no such type exists.
450 RecTy *resolveTypes(RecTy *T1, RecTy *T2);
452 //===----------------------------------------------------------------------===//
453 // Initializer Classes
454 //===----------------------------------------------------------------------===//
460 /// isComplete - This virtual method should be overridden by values that may
461 /// not be completely specified yet.
462 virtual bool isComplete() const { return true; }
464 /// print - Print out this value.
465 void print(raw_ostream &OS) const { OS << getAsString(); }
467 /// getAsString - Convert this value to a string form.
468 virtual std::string getAsString() const = 0;
470 /// dump - Debugging method that may be called through a debugger, just
471 /// invokes print on stderr.
474 /// convertInitializerTo - This virtual function is a simple call-back
475 /// function that should be overridden to call the appropriate
476 /// RecTy::convertValue method.
478 virtual Init *convertInitializerTo(RecTy *Ty) = 0;
480 /// convertInitializerBitRange - This method is used to implement the bitrange
481 /// selection operator. Given an initializer, it selects the specified bits
482 /// out, returning them as a new init of bits type. If it is not legal to use
483 /// the bit subscript operator on this initializer, return null.
485 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits) {
489 /// convertInitListSlice - This method is used to implement the list slice
490 /// selection operator. Given an initializer, it selects the specified list
491 /// elements, returning them as a new init of list type. If it is not legal
492 /// to take a slice of this, return null.
494 virtual Init *convertInitListSlice(const std::vector<unsigned> &Elements) {
498 /// getFieldType - This method is used to implement the FieldInit class.
499 /// Implementors of this method should return the type of the named field if
500 /// they are of record type.
502 virtual RecTy *getFieldType(const std::string &FieldName) const { return 0; }
504 /// getFieldInit - This method complements getFieldType to return the
505 /// initializer for the specified field. If getFieldType returns non-null
506 /// this method should return non-null, otherwise it returns null.
508 virtual Init *getFieldInit(Record &R, const RecordVal *RV,
509 const std::string &FieldName) const {
513 /// resolveReferences - This method is used by classes that refer to other
514 /// variables which may not be defined at the time the expression is formed.
515 /// If a value is set for the variable later, this method will be called on
516 /// users of the value to allow the value to propagate out.
518 virtual Init *resolveReferences(Record &R, const RecordVal *RV) {
523 inline raw_ostream &operator<<(raw_ostream &OS, const Init &I) {
524 I.print(OS); return OS;
527 /// TypedInit - This is the common super-class of types that have a specific,
530 class TypedInit : public Init {
533 explicit TypedInit(RecTy *T) : Ty(T) {}
535 RecTy *getType() const { return Ty; }
537 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
538 virtual Init *convertInitListSlice(const std::vector<unsigned> &Elements);
540 /// getFieldType - This method is used to implement the FieldInit class.
541 /// Implementors of this method should return the type of the named field if
542 /// they are of record type.
544 virtual RecTy *getFieldType(const std::string &FieldName) const;
546 /// resolveBitReference - This method is used to implement
547 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
548 /// simply return the resolved value, otherwise we return null.
550 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
553 /// resolveListElementReference - This method is used to implement
554 /// VarListElementInit::resolveReferences. If the list element is resolvable
555 /// now, we return the resolved value, otherwise we return null.
556 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
561 /// UnsetInit - ? - Represents an uninitialized value
563 class UnsetInit : public Init {
565 virtual Init *convertInitializerTo(RecTy *Ty) {
566 return Ty->convertValue(this);
569 virtual bool isComplete() const { return false; }
570 virtual std::string getAsString() const { return "?"; }
574 /// BitInit - true/false - Represent a concrete initializer for a bit.
576 class BitInit : public Init {
579 explicit BitInit(bool V) : Value(V) {}
581 bool getValue() const { return Value; }
583 virtual Init *convertInitializerTo(RecTy *Ty) {
584 return Ty->convertValue(this);
587 virtual std::string getAsString() const { return Value ? "1" : "0"; }
590 /// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
591 /// It contains a vector of bits, whose size is determined by the type.
593 class BitsInit : public Init {
594 std::vector<Init*> Bits;
596 explicit BitsInit(unsigned Size) : Bits(Size) {}
598 unsigned getNumBits() const { return Bits.size(); }
600 Init *getBit(unsigned Bit) const {
601 assert(Bit < Bits.size() && "Bit index out of range!");
604 void setBit(unsigned Bit, Init *V) {
605 assert(Bit < Bits.size() && "Bit index out of range!");
606 assert(Bits[Bit] == 0 && "Bit already set!");
610 virtual Init *convertInitializerTo(RecTy *Ty) {
611 return Ty->convertValue(this);
613 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
615 virtual bool isComplete() const {
616 for (unsigned i = 0; i != getNumBits(); ++i)
617 if (!getBit(i)->isComplete()) return false;
620 bool allInComplete() const {
621 for (unsigned i = 0; i != getNumBits(); ++i)
622 if (getBit(i)->isComplete()) return false;
625 virtual std::string getAsString() const;
627 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
631 /// IntInit - 7 - Represent an initalization by a literal integer value.
633 class IntInit : public TypedInit {
636 explicit IntInit(int64_t V) : TypedInit(new IntRecTy), Value(V) {}
638 int64_t getValue() const { return Value; }
640 virtual Init *convertInitializerTo(RecTy *Ty) {
641 return Ty->convertValue(this);
643 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
645 virtual std::string getAsString() const;
647 /// resolveBitReference - This method is used to implement
648 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
649 /// simply return the resolved value, otherwise we return null.
651 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
653 assert(0 && "Illegal bit reference off int");
657 /// resolveListElementReference - This method is used to implement
658 /// VarListElementInit::resolveReferences. If the list element is resolvable
659 /// now, we return the resolved value, otherwise we return null.
660 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
662 assert(0 && "Illegal element reference off int");
668 /// StringInit - "foo" - Represent an initialization by a string value.
670 class StringInit : public TypedInit {
673 explicit StringInit(const std::string &V)
674 : TypedInit(new StringRecTy), Value(V) {}
676 const std::string &getValue() const { return Value; }
678 virtual Init *convertInitializerTo(RecTy *Ty) {
679 return Ty->convertValue(this);
682 virtual std::string getAsString() const { return "\"" + Value + "\""; }
684 /// resolveBitReference - This method is used to implement
685 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
686 /// simply return the resolved value, otherwise we return null.
688 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
690 assert(0 && "Illegal bit reference off string");
694 /// resolveListElementReference - This method is used to implement
695 /// VarListElementInit::resolveReferences. If the list element is resolvable
696 /// now, we return the resolved value, otherwise we return null.
697 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
699 assert(0 && "Illegal element reference off string");
704 /// CodeInit - "[{...}]" - Represent a code fragment.
706 class CodeInit : public Init {
709 explicit CodeInit(const std::string &V) : Value(V) {}
711 const std::string &getValue() const { return Value; }
713 virtual Init *convertInitializerTo(RecTy *Ty) {
714 return Ty->convertValue(this);
717 virtual std::string getAsString() const { return "[{" + Value + "}]"; }
720 /// ListInit - [AL, AH, CL] - Represent a list of defs
722 class ListInit : public TypedInit {
723 std::vector<Init*> Values;
725 typedef std::vector<Init*>::iterator iterator;
726 typedef std::vector<Init*>::const_iterator const_iterator;
728 explicit ListInit(std::vector<Init*> &Vs, RecTy *EltTy)
729 : TypedInit(new ListRecTy(EltTy)) {
732 explicit ListInit(iterator Start, iterator End, RecTy *EltTy)
733 : TypedInit(new ListRecTy(EltTy)), Values(Start, End) {}
735 unsigned getSize() const { return Values.size(); }
736 Init *getElement(unsigned i) const {
737 assert(i < Values.size() && "List element index out of range!");
741 Record *getElementAsRecord(unsigned i) const;
743 Init *convertInitListSlice(const std::vector<unsigned> &Elements);
745 virtual Init *convertInitializerTo(RecTy *Ty) {
746 return Ty->convertValue(this);
749 /// resolveReferences - This method is used by classes that refer to other
750 /// variables which may not be defined at the time they expression is formed.
751 /// If a value is set for the variable later, this method will be called on
752 /// users of the value to allow the value to propagate out.
754 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
756 virtual std::string getAsString() const;
758 inline iterator begin() { return Values.begin(); }
759 inline const_iterator begin() const { return Values.begin(); }
760 inline iterator end () { return Values.end(); }
761 inline const_iterator end () const { return Values.end(); }
763 inline size_t size () const { return Values.size(); }
764 inline bool empty() const { return Values.empty(); }
766 /// resolveBitReference - This method is used to implement
767 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
768 /// simply return the resolved value, otherwise we return null.
770 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
772 assert(0 && "Illegal bit reference off list");
776 /// resolveListElementReference - This method is used to implement
777 /// VarListElementInit::resolveReferences. If the list element is resolvable
778 /// now, we return the resolved value, otherwise we return null.
779 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
784 /// OpInit - Base class for operators
786 class OpInit : public TypedInit {
788 OpInit(RecTy *Type) : TypedInit(Type) {}
790 // Clone - Clone this operator, replacing arguments with the new list
791 virtual OpInit *clone(std::vector<Init *> &Operands) = 0;
793 virtual int getNumOperands() const = 0;
794 virtual Init *getOperand(int i) = 0;
796 // Fold - If possible, fold this to a simpler init. Return this if not
798 virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) = 0;
800 virtual Init *convertInitializerTo(RecTy *Ty) {
801 return Ty->convertValue(this);
804 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
806 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
811 /// UnOpInit - !op (X) - Transform an init.
813 class UnOpInit : public OpInit {
815 enum UnaryOp { CAST, HEAD, TAIL, EMPTY };
820 UnOpInit(UnaryOp opc, Init *lhs, RecTy *Type) :
821 OpInit(Type), Opc(opc), LHS(lhs) {
824 // Clone - Clone this operator, replacing arguments with the new list
825 virtual OpInit *clone(std::vector<Init *> &Operands) {
826 assert(Operands.size() == 1 &&
827 "Wrong number of operands for unary operation");
828 return new UnOpInit(getOpcode(), *Operands.begin(), getType());
831 int getNumOperands() const { return 1; }
832 Init *getOperand(int i) {
833 assert(i == 0 && "Invalid operand id for unary operator");
837 UnaryOp getOpcode() const { return Opc; }
838 Init *getOperand() const { return LHS; }
840 // Fold - If possible, fold this to a simpler init. Return this if not
842 Init *Fold(Record *CurRec, MultiClass *CurMultiClass);
844 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
846 virtual std::string getAsString() const;
849 /// BinOpInit - !op (X, Y) - Combine two inits.
851 class BinOpInit : public OpInit {
853 enum BinaryOp { SHL, SRA, SRL, STRCONCAT, CONCAT, EQ };
858 BinOpInit(BinaryOp opc, Init *lhs, Init *rhs, RecTy *Type) :
859 OpInit(Type), Opc(opc), LHS(lhs), RHS(rhs) {
862 // Clone - Clone this operator, replacing arguments with the new list
863 virtual OpInit *clone(std::vector<Init *> &Operands) {
864 assert(Operands.size() == 2 &&
865 "Wrong number of operands for binary operation");
866 return new BinOpInit(getOpcode(), Operands[0], Operands[1], getType());
869 int getNumOperands() const { return 2; }
870 Init *getOperand(int i) {
871 assert((i == 0 || i == 1) && "Invalid operand id for binary operator");
879 BinaryOp getOpcode() const { return Opc; }
880 Init *getLHS() const { return LHS; }
881 Init *getRHS() const { return RHS; }
883 // Fold - If possible, fold this to a simpler init. Return this if not
885 Init *Fold(Record *CurRec, MultiClass *CurMultiClass);
887 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
889 virtual std::string getAsString() const;
892 /// TernOpInit - !op (X, Y, Z) - Combine two inits.
894 class TernOpInit : public OpInit {
896 enum TernaryOp { SUBST, FOREACH, IF };
899 Init *LHS, *MHS, *RHS;
901 TernOpInit(TernaryOp opc, Init *lhs, Init *mhs, Init *rhs, RecTy *Type) :
902 OpInit(Type), Opc(opc), LHS(lhs), MHS(mhs), RHS(rhs) {
905 // Clone - Clone this operator, replacing arguments with the new list
906 virtual OpInit *clone(std::vector<Init *> &Operands) {
907 assert(Operands.size() == 3 &&
908 "Wrong number of operands for ternary operation");
909 return new TernOpInit(getOpcode(), Operands[0], Operands[1], Operands[2],
913 int getNumOperands() const { return 3; }
914 Init *getOperand(int i) {
915 assert((i == 0 || i == 1 || i == 2) &&
916 "Invalid operand id for ternary operator");
926 TernaryOp getOpcode() const { return Opc; }
927 Init *getLHS() const { return LHS; }
928 Init *getMHS() const { return MHS; }
929 Init *getRHS() const { return RHS; }
931 // Fold - If possible, fold this to a simpler init. Return this if not
933 Init *Fold(Record *CurRec, MultiClass *CurMultiClass);
935 virtual bool isComplete() const { return false; }
937 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
939 virtual std::string getAsString() const;
943 /// VarInit - 'Opcode' - Represent a reference to an entire variable object.
945 class VarInit : public TypedInit {
948 explicit VarInit(const std::string &VN, RecTy *T)
949 : TypedInit(T), VarName(VN) {}
951 virtual Init *convertInitializerTo(RecTy *Ty) {
952 return Ty->convertValue(this);
955 const std::string &getName() const { return VarName; }
957 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
959 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
962 virtual RecTy *getFieldType(const std::string &FieldName) const;
963 virtual Init *getFieldInit(Record &R, const RecordVal *RV,
964 const std::string &FieldName) const;
966 /// resolveReferences - This method is used by classes that refer to other
967 /// variables which may not be defined at the time they expression is formed.
968 /// If a value is set for the variable later, this method will be called on
969 /// users of the value to allow the value to propagate out.
971 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
973 virtual std::string getAsString() const { return VarName; }
977 /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
979 class VarBitInit : public Init {
983 VarBitInit(TypedInit *T, unsigned B) : TI(T), Bit(B) {
984 assert(T->getType() && dynamic_cast<BitsRecTy*>(T->getType()) &&
985 ((BitsRecTy*)T->getType())->getNumBits() > B &&
986 "Illegal VarBitInit expression!");
989 virtual Init *convertInitializerTo(RecTy *Ty) {
990 return Ty->convertValue(this);
993 TypedInit *getVariable() const { return TI; }
994 unsigned getBitNum() const { return Bit; }
996 virtual std::string getAsString() const;
997 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
1000 /// VarListElementInit - List[4] - Represent access to one element of a var or
1002 class VarListElementInit : public TypedInit {
1006 VarListElementInit(TypedInit *T, unsigned E)
1007 : TypedInit(dynamic_cast<ListRecTy*>(T->getType())->getElementType()),
1009 assert(T->getType() && dynamic_cast<ListRecTy*>(T->getType()) &&
1010 "Illegal VarBitInit expression!");
1013 virtual Init *convertInitializerTo(RecTy *Ty) {
1014 return Ty->convertValue(this);
1017 TypedInit *getVariable() const { return TI; }
1018 unsigned getElementNum() const { return Element; }
1020 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
1023 /// resolveListElementReference - This method is used to implement
1024 /// VarListElementInit::resolveReferences. If the list element is resolvable
1025 /// now, we return the resolved value, otherwise we return null.
1026 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
1029 virtual std::string getAsString() const;
1030 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
1033 /// DefInit - AL - Represent a reference to a 'def' in the description
1035 class DefInit : public TypedInit {
1038 explicit DefInit(Record *D) : TypedInit(new RecordRecTy(D)), Def(D) {}
1040 virtual Init *convertInitializerTo(RecTy *Ty) {
1041 return Ty->convertValue(this);
1044 Record *getDef() const { return Def; }
1046 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
1048 virtual RecTy *getFieldType(const std::string &FieldName) const;
1049 virtual Init *getFieldInit(Record &R, const RecordVal *RV,
1050 const std::string &FieldName) const;
1052 virtual std::string getAsString() const;
1054 /// resolveBitReference - This method is used to implement
1055 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
1056 /// simply return the resolved value, otherwise we return null.
1058 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
1060 assert(0 && "Illegal bit reference off def");
1064 /// resolveListElementReference - This method is used to implement
1065 /// VarListElementInit::resolveReferences. If the list element is resolvable
1066 /// now, we return the resolved value, otherwise we return null.
1067 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
1069 assert(0 && "Illegal element reference off def");
1075 /// FieldInit - X.Y - Represent a reference to a subfield of a variable
1077 class FieldInit : public TypedInit {
1078 Init *Rec; // Record we are referring to
1079 std::string FieldName; // Field we are accessing
1081 FieldInit(Init *R, const std::string &FN)
1082 : TypedInit(R->getFieldType(FN)), Rec(R), FieldName(FN) {
1083 assert(getType() && "FieldInit with non-record type!");
1086 virtual Init *convertInitializerTo(RecTy *Ty) {
1087 return Ty->convertValue(this);
1090 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
1092 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
1095 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
1097 virtual std::string getAsString() const {
1098 return Rec->getAsString() + "." + FieldName;
1102 /// DagInit - (v a, b) - Represent a DAG tree value. DAG inits are required
1103 /// to have at least one value then a (possibly empty) list of arguments. Each
1104 /// argument can have a name associated with it.
1106 class DagInit : public TypedInit {
1108 std::string ValName;
1109 std::vector<Init*> Args;
1110 std::vector<std::string> ArgNames;
1112 DagInit(Init *V, std::string VN,
1113 const std::vector<std::pair<Init*, std::string> > &args)
1114 : TypedInit(new DagRecTy), Val(V), ValName(VN) {
1115 Args.reserve(args.size());
1116 ArgNames.reserve(args.size());
1117 for (unsigned i = 0, e = args.size(); i != e; ++i) {
1118 Args.push_back(args[i].first);
1119 ArgNames.push_back(args[i].second);
1122 DagInit(Init *V, std::string VN, const std::vector<Init*> &args,
1123 const std::vector<std::string> &argNames)
1124 : TypedInit(new DagRecTy), Val(V), ValName(VN), Args(args),
1125 ArgNames(argNames) { }
1127 virtual Init *convertInitializerTo(RecTy *Ty) {
1128 return Ty->convertValue(this);
1131 Init *getOperator() const { return Val; }
1133 const std::string &getName() const { return ValName; }
1135 unsigned getNumArgs() const { return Args.size(); }
1136 Init *getArg(unsigned Num) const {
1137 assert(Num < Args.size() && "Arg number out of range!");
1140 const std::string &getArgName(unsigned Num) const {
1141 assert(Num < ArgNames.size() && "Arg number out of range!");
1142 return ArgNames[Num];
1145 void setArg(unsigned Num, Init *I) {
1146 assert(Num < Args.size() && "Arg number out of range!");
1150 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
1152 virtual std::string getAsString() const;
1154 typedef std::vector<Init*>::iterator arg_iterator;
1155 typedef std::vector<Init*>::const_iterator const_arg_iterator;
1156 typedef std::vector<std::string>::iterator name_iterator;
1157 typedef std::vector<std::string>::const_iterator const_name_iterator;
1159 inline arg_iterator arg_begin() { return Args.begin(); }
1160 inline const_arg_iterator arg_begin() const { return Args.begin(); }
1161 inline arg_iterator arg_end () { return Args.end(); }
1162 inline const_arg_iterator arg_end () const { return Args.end(); }
1164 inline size_t arg_size () const { return Args.size(); }
1165 inline bool arg_empty() const { return Args.empty(); }
1167 inline name_iterator name_begin() { return ArgNames.begin(); }
1168 inline const_name_iterator name_begin() const { return ArgNames.begin(); }
1169 inline name_iterator name_end () { return ArgNames.end(); }
1170 inline const_name_iterator name_end () const { return ArgNames.end(); }
1172 inline size_t name_size () const { return ArgNames.size(); }
1173 inline bool name_empty() const { return ArgNames.empty(); }
1175 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
1177 assert(0 && "Illegal bit reference off dag");
1181 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
1183 assert(0 && "Illegal element reference off dag");
1188 //===----------------------------------------------------------------------===//
1189 // High-Level Classes
1190 //===----------------------------------------------------------------------===//
1198 RecordVal(const std::string &N, RecTy *T, unsigned P);
1200 const std::string &getName() const { return Name; }
1202 unsigned getPrefix() const { return Prefix; }
1203 RecTy *getType() const { return Ty; }
1204 Init *getValue() const { return Value; }
1206 bool setValue(Init *V) {
1208 Value = V->convertInitializerTo(Ty);
1216 void print(raw_ostream &OS, bool PrintSem = true) const;
1219 inline raw_ostream &operator<<(raw_ostream &OS, const RecordVal &RV) {
1220 RV.print(OS << " ");
1225 static unsigned LastID;
1227 // Unique record ID.
1231 std::vector<std::string> TemplateArgs;
1232 std::vector<RecordVal> Values;
1233 std::vector<Record*> SuperClasses;
1235 // Tracks Record instances. Not owned by Record.
1236 RecordKeeper &TrackedRecords;
1240 // Constructs a record.
1241 explicit Record(const std::string &N, SMLoc loc, RecordKeeper &records) :
1242 ID(LastID++), Name(N), Loc(loc), TrackedRecords(records) {}
1246 static unsigned getNewUID() { return LastID++; }
1249 unsigned getID() const { return ID; }
1251 const std::string &getName() const { return Name; }
1252 void setName(const std::string &Name); // Also updates RecordKeeper.
1254 SMLoc getLoc() const { return Loc; }
1256 const std::vector<std::string> &getTemplateArgs() const {
1257 return TemplateArgs;
1259 const std::vector<RecordVal> &getValues() const { return Values; }
1260 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; }
1262 bool isTemplateArg(StringRef Name) const {
1263 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
1264 if (TemplateArgs[i] == Name) return true;
1268 const RecordVal *getValue(StringRef Name) const {
1269 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1270 if (Values[i].getName() == Name) return &Values[i];
1273 RecordVal *getValue(StringRef Name) {
1274 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1275 if (Values[i].getName() == Name) return &Values[i];
1279 void addTemplateArg(StringRef Name) {
1280 assert(!isTemplateArg(Name) && "Template arg already defined!");
1281 TemplateArgs.push_back(Name);
1284 void addValue(const RecordVal &RV) {
1285 assert(getValue(RV.getName()) == 0 && "Value already added!");
1286 Values.push_back(RV);
1289 void removeValue(StringRef Name) {
1290 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1291 if (Values[i].getName() == Name) {
1292 Values.erase(Values.begin()+i);
1295 assert(0 && "Cannot remove an entry that does not exist!");
1298 bool isSubClassOf(const Record *R) const {
1299 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1300 if (SuperClasses[i] == R)
1305 bool isSubClassOf(StringRef Name) const {
1306 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1307 if (SuperClasses[i]->getName() == Name)
1312 void addSuperClass(Record *R) {
1313 assert(!isSubClassOf(R) && "Already subclassing record!");
1314 SuperClasses.push_back(R);
1317 /// resolveReferences - If there are any field references that refer to fields
1318 /// that have been filled in, we can propagate the values now.
1320 void resolveReferences() { resolveReferencesTo(0); }
1322 /// resolveReferencesTo - If anything in this record refers to RV, replace the
1323 /// reference to RV with the RHS of RV. If RV is null, we resolve all
1324 /// possible references.
1325 void resolveReferencesTo(const RecordVal *RV);
1327 RecordKeeper &getRecords() const {
1328 return TrackedRecords;
1333 //===--------------------------------------------------------------------===//
1334 // High-level methods useful to tablegen back-ends
1337 /// getValueInit - Return the initializer for a value with the specified name,
1338 /// or throw an exception if the field does not exist.
1340 Init *getValueInit(StringRef FieldName) const;
1342 /// getValueAsString - This method looks up the specified field and returns
1343 /// its value as a string, throwing an exception if the field does not exist
1344 /// or if the value is not a string.
1346 std::string getValueAsString(StringRef FieldName) const;
1348 /// getValueAsBitsInit - This method looks up the specified field and returns
1349 /// its value as a BitsInit, throwing an exception if the field does not exist
1350 /// or if the value is not the right type.
1352 BitsInit *getValueAsBitsInit(StringRef FieldName) const;
1354 /// getValueAsListInit - This method looks up the specified field and returns
1355 /// its value as a ListInit, throwing an exception if the field does not exist
1356 /// or if the value is not the right type.
1358 ListInit *getValueAsListInit(StringRef FieldName) const;
1360 /// getValueAsListOfDefs - This method looks up the specified field and
1361 /// returns its value as a vector of records, throwing an exception if the
1362 /// field does not exist or if the value is not the right type.
1364 std::vector<Record*> getValueAsListOfDefs(StringRef FieldName) const;
1366 /// getValueAsListOfInts - This method looks up the specified field and
1367 /// returns its value as a vector of integers, throwing an exception if the
1368 /// field does not exist or if the value is not the right type.
1370 std::vector<int64_t> getValueAsListOfInts(StringRef FieldName) const;
1372 /// getValueAsListOfStrings - This method looks up the specified field and
1373 /// returns its value as a vector of strings, throwing an exception if the
1374 /// field does not exist or if the value is not the right type.
1376 std::vector<std::string> getValueAsListOfStrings(StringRef FieldName) const;
1378 /// getValueAsDef - This method looks up the specified field and returns its
1379 /// value as a Record, throwing an exception if the field does not exist or if
1380 /// the value is not the right type.
1382 Record *getValueAsDef(StringRef FieldName) const;
1384 /// getValueAsBit - This method looks up the specified field and returns its
1385 /// value as a bit, throwing an exception if the field does not exist or if
1386 /// the value is not the right type.
1388 bool getValueAsBit(StringRef FieldName) const;
1390 /// getValueAsInt - This method looks up the specified field and returns its
1391 /// value as an int64_t, throwing an exception if the field does not exist or
1392 /// if the value is not the right type.
1394 int64_t getValueAsInt(StringRef FieldName) const;
1396 /// getValueAsDag - This method looks up the specified field and returns its
1397 /// value as an Dag, throwing an exception if the field does not exist or if
1398 /// the value is not the right type.
1400 DagInit *getValueAsDag(StringRef FieldName) const;
1402 /// getValueAsCode - This method looks up the specified field and returns
1403 /// its value as the string data in a CodeInit, throwing an exception if the
1404 /// field does not exist or if the value is not a code object.
1406 std::string getValueAsCode(StringRef FieldName) const;
1409 raw_ostream &operator<<(raw_ostream &OS, const Record &R);
1412 Record Rec; // Placeholder for template args and Name.
1413 typedef std::vector<Record*> RecordVector;
1414 RecordVector DefPrototypes;
1418 MultiClass(const std::string &Name, SMLoc Loc, RecordKeeper &Records) :
1419 Rec(Name, Loc, Records) {}
1422 class RecordKeeper {
1423 std::map<std::string, Record*> Classes, Defs;
1426 for (std::map<std::string, Record*>::iterator I = Classes.begin(),
1427 E = Classes.end(); I != E; ++I)
1429 for (std::map<std::string, Record*>::iterator I = Defs.begin(),
1430 E = Defs.end(); I != E; ++I)
1434 const std::map<std::string, Record*> &getClasses() const { return Classes; }
1435 const std::map<std::string, Record*> &getDefs() const { return Defs; }
1437 Record *getClass(const std::string &Name) const {
1438 std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
1439 return I == Classes.end() ? 0 : I->second;
1441 Record *getDef(const std::string &Name) const {
1442 std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
1443 return I == Defs.end() ? 0 : I->second;
1445 void addClass(Record *R) {
1446 assert(getClass(R->getName()) == 0 && "Class already exists!");
1447 Classes.insert(std::make_pair(R->getName(), R));
1449 void addDef(Record *R) {
1450 assert(getDef(R->getName()) == 0 && "Def already exists!");
1451 Defs.insert(std::make_pair(R->getName(), R));
1454 /// removeClass - Remove, but do not delete, the specified record.
1456 void removeClass(const std::string &Name) {
1457 assert(Classes.count(Name) && "Class does not exist!");
1458 Classes.erase(Name);
1460 /// removeDef - Remove, but do not delete, the specified record.
1462 void removeDef(const std::string &Name) {
1463 assert(Defs.count(Name) && "Def does not exist!");
1467 //===--------------------------------------------------------------------===//
1468 // High-level helper methods, useful for tablegen backends...
1470 /// getAllDerivedDefinitions - This method returns all concrete definitions
1471 /// that derive from the specified class name. If a class with the specified
1472 /// name does not exist, an exception is thrown.
1473 std::vector<Record*>
1474 getAllDerivedDefinitions(const std::string &ClassName) const;
1479 /// LessRecord - Sorting predicate to sort record pointers by name.
1482 bool operator()(const Record *Rec1, const Record *Rec2) const {
1483 return StringRef(Rec1->getName()).compare_numeric(Rec2->getName()) < 0;
1487 /// LessRecordFieldName - Sorting predicate to sort record pointers by their
1490 struct LessRecordFieldName {
1491 bool operator()(const Record *Rec1, const Record *Rec2) const {
1492 return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name");
1496 raw_ostream &operator<<(raw_ostream &OS, const RecordKeeper &RK);
1498 } // End llvm namespace