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 //===----------------------------------------------------------------------===//
52 class VarListElementInit;
58 //===----------------------------------------------------------------------===//
60 //===----------------------------------------------------------------------===//
65 virtual std::string getAsString() const = 0;
66 void print(std::ostream &OS) const { OS << getAsString(); }
69 /// typeIsConvertibleTo - Return true if all values of 'this' type can be
70 /// converted to the specified type.
71 virtual bool typeIsConvertibleTo(const RecTy *RHS) const = 0;
73 public: // These methods should only be called from subclasses of Init
74 virtual Init *convertValue( UnsetInit *UI) { return 0; }
75 virtual Init *convertValue( BitInit *BI) { return 0; }
76 virtual Init *convertValue( BitsInit *BI) { return 0; }
77 virtual Init *convertValue( IntInit *II) { return 0; }
78 virtual Init *convertValue(StringInit *SI) { return 0; }
79 virtual Init *convertValue( ListInit *LI) { return 0; }
80 virtual Init *convertValue( BinOpInit *UI) { return 0; }
81 virtual Init *convertValue( CodeInit *CI) { return 0; }
82 virtual Init *convertValue(VarBitInit *VB) { return 0; }
83 virtual Init *convertValue( DefInit *DI) { return 0; }
84 virtual Init *convertValue( DagInit *DI) { return 0; }
85 virtual Init *convertValue( TypedInit *TI) { return 0; }
86 virtual Init *convertValue( VarInit *VI) {
87 return convertValue((TypedInit*)VI);
89 virtual Init *convertValue( FieldInit *FI) {
90 return convertValue((TypedInit*)FI);
93 public: // These methods should only be called by subclasses of RecTy.
94 // baseClassOf - These virtual methods should be overloaded to return true iff
95 // all values of type 'RHS' can be converted to the 'this' type.
96 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
97 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
98 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
99 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
100 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
101 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
102 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
103 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
106 inline std::ostream &operator<<(std::ostream &OS, const RecTy &Ty) {
112 /// BitRecTy - 'bit' - Represent a single bit
114 class BitRecTy : public RecTy {
116 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
117 virtual Init *convertValue( BitInit *BI) { return (Init*)BI; }
118 virtual Init *convertValue( BitsInit *BI);
119 virtual Init *convertValue( IntInit *II);
120 virtual Init *convertValue(StringInit *SI) { return 0; }
121 virtual Init *convertValue( ListInit *LI) { return 0; }
122 virtual Init *convertValue( CodeInit *CI) { return 0; }
123 virtual Init *convertValue(VarBitInit *VB) { return (Init*)VB; }
124 virtual Init *convertValue( DefInit *DI) { return 0; }
125 virtual Init *convertValue( DagInit *DI) { return 0; }
126 virtual Init *convertValue( BinOpInit *UI) { return 0; }
127 virtual Init *convertValue( TypedInit *TI);
128 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
129 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
131 std::string getAsString() const { return "bit"; }
133 bool typeIsConvertibleTo(const RecTy *RHS) const {
134 return RHS->baseClassOf(this);
136 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
137 virtual bool baseClassOf(const BitsRecTy *RHS) const;
138 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
139 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
140 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
141 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
142 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
143 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
148 // BitsRecTy - 'bits<n>' - Represent a fixed number of bits
149 /// BitsRecTy - 'bits<n>' - Represent a fixed number of bits
151 class BitsRecTy : public RecTy {
154 explicit BitsRecTy(unsigned Sz) : Size(Sz) {}
156 unsigned getNumBits() const { return Size; }
158 virtual Init *convertValue( UnsetInit *UI);
159 virtual Init *convertValue( BitInit *UI);
160 virtual Init *convertValue( BitsInit *BI);
161 virtual Init *convertValue( IntInit *II);
162 virtual Init *convertValue(StringInit *SI) { return 0; }
163 virtual Init *convertValue( ListInit *LI) { return 0; }
164 virtual Init *convertValue( CodeInit *CI) { return 0; }
165 virtual Init *convertValue(VarBitInit *VB) { return 0; }
166 virtual Init *convertValue( DefInit *DI) { return 0; }
167 virtual Init *convertValue( DagInit *DI) { return 0; }
168 virtual Init *convertValue( BinOpInit *UI) { return 0; }
169 virtual Init *convertValue( TypedInit *TI);
170 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
171 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
173 std::string getAsString() const;
175 bool typeIsConvertibleTo(const RecTy *RHS) const {
176 return RHS->baseClassOf(this);
178 virtual bool baseClassOf(const BitRecTy *RHS) const { return Size == 1; }
179 virtual bool baseClassOf(const BitsRecTy *RHS) const {
180 return RHS->Size == Size;
182 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
183 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
184 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
185 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
186 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
187 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
192 /// IntRecTy - 'int' - Represent an integer value of no particular size
194 class IntRecTy : public RecTy {
196 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
197 virtual Init *convertValue( BitInit *BI);
198 virtual Init *convertValue( BitsInit *BI);
199 virtual Init *convertValue( IntInit *II) { return (Init*)II; }
200 virtual Init *convertValue(StringInit *SI) { return 0; }
201 virtual Init *convertValue( ListInit *LI) { return 0; }
202 virtual Init *convertValue( CodeInit *CI) { return 0; }
203 virtual Init *convertValue(VarBitInit *VB) { return 0; }
204 virtual Init *convertValue( DefInit *DI) { return 0; }
205 virtual Init *convertValue( DagInit *DI) { return 0; }
206 virtual Init *convertValue( BinOpInit *UI) { return 0; }
207 virtual Init *convertValue( TypedInit *TI);
208 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
209 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
211 std::string getAsString() const { return "int"; }
213 bool typeIsConvertibleTo(const RecTy *RHS) const {
214 return RHS->baseClassOf(this);
217 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
218 virtual bool baseClassOf(const BitsRecTy *RHS) const { return true; }
219 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
220 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
221 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
222 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
223 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
224 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
228 /// StringRecTy - 'string' - Represent an string value
230 class StringRecTy : public RecTy {
232 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
233 virtual Init *convertValue( BitInit *BI) { return 0; }
234 virtual Init *convertValue( BitsInit *BI) { return 0; }
235 virtual Init *convertValue( IntInit *II) { return 0; }
236 virtual Init *convertValue(StringInit *SI) { return (Init*)SI; }
237 virtual Init *convertValue( ListInit *LI) { return 0; }
238 virtual Init *convertValue( BinOpInit *BO);
239 virtual Init *convertValue( CodeInit *CI) { return 0; }
240 virtual Init *convertValue(VarBitInit *VB) { return 0; }
241 virtual Init *convertValue( DefInit *DI) { return 0; }
242 virtual Init *convertValue( DagInit *DI) { return 0; }
243 virtual Init *convertValue( TypedInit *TI);
244 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
245 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
247 std::string getAsString() const { return "string"; }
249 bool typeIsConvertibleTo(const RecTy *RHS) const {
250 return RHS->baseClassOf(this);
253 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
254 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
255 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
256 virtual bool baseClassOf(const StringRecTy *RHS) const { return true; }
257 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
258 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
259 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
260 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
263 // ListRecTy - 'list<Ty>' - Represent a list of values, all of which must be of
264 // the specified type.
265 /// ListRecTy - 'list<Ty>' - Represent a list of values, all of which must
266 /// be of the specified type.
268 class ListRecTy : public RecTy {
271 explicit ListRecTy(RecTy *T) : Ty(T) {}
273 RecTy *getElementType() const { return Ty; }
275 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
276 virtual Init *convertValue( BitInit *BI) { return 0; }
277 virtual Init *convertValue( BitsInit *BI) { return 0; }
278 virtual Init *convertValue( IntInit *II) { return 0; }
279 virtual Init *convertValue(StringInit *SI) { return 0; }
280 virtual Init *convertValue( ListInit *LI);
281 virtual Init *convertValue( CodeInit *CI) { return 0; }
282 virtual Init *convertValue(VarBitInit *VB) { return 0; }
283 virtual Init *convertValue( DefInit *DI) { return 0; }
284 virtual Init *convertValue( DagInit *DI) { return 0; }
285 virtual Init *convertValue( BinOpInit *UI) { return 0; }
286 virtual Init *convertValue( TypedInit *TI);
287 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
288 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
290 std::string getAsString() const;
292 bool typeIsConvertibleTo(const RecTy *RHS) const {
293 return RHS->baseClassOf(this);
296 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
297 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
298 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
299 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
300 virtual bool baseClassOf(const ListRecTy *RHS) const {
301 return RHS->getElementType()->typeIsConvertibleTo(Ty);
303 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
304 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
305 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
308 /// CodeRecTy - 'code' - Represent an code fragment, function or method.
310 class CodeRecTy : public RecTy {
312 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
313 virtual Init *convertValue( BitInit *BI) { return 0; }
314 virtual Init *convertValue( BitsInit *BI) { return 0; }
315 virtual Init *convertValue( IntInit *II) { return 0; }
316 virtual Init *convertValue(StringInit *SI) { return 0; }
317 virtual Init *convertValue( ListInit *LI) { return 0; }
318 virtual Init *convertValue( CodeInit *CI) { return (Init*)CI; }
319 virtual Init *convertValue(VarBitInit *VB) { return 0; }
320 virtual Init *convertValue( DefInit *DI) { return 0; }
321 virtual Init *convertValue( DagInit *DI) { return 0; }
322 virtual Init *convertValue( BinOpInit *UI) { return 0; }
323 virtual Init *convertValue( TypedInit *TI);
324 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
325 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
327 std::string getAsString() const { return "code"; }
329 bool typeIsConvertibleTo(const RecTy *RHS) const {
330 return RHS->baseClassOf(this);
332 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
333 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
334 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
335 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
336 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
337 virtual bool baseClassOf(const CodeRecTy *RHS) const { return true; }
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 {
346 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
347 virtual Init *convertValue( BitInit *BI) { return 0; }
348 virtual Init *convertValue( BitsInit *BI) { return 0; }
349 virtual Init *convertValue( IntInit *II) { return 0; }
350 virtual Init *convertValue(StringInit *SI) { return 0; }
351 virtual Init *convertValue( ListInit *LI) { return 0; }
352 virtual Init *convertValue( CodeInit *CI) { return 0; }
353 virtual Init *convertValue(VarBitInit *VB) { return 0; }
354 virtual Init *convertValue( DefInit *DI) { return 0; }
355 virtual Init *convertValue( BinOpInit *BO);
356 virtual Init *convertValue( DagInit *CI) { return (Init*)CI; }
357 virtual Init *convertValue( TypedInit *TI);
358 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
359 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
361 std::string getAsString() const { return "dag"; }
363 bool typeIsConvertibleTo(const RecTy *RHS) const {
364 return RHS->baseClassOf(this);
367 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
368 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
369 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
370 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
371 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
372 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
373 virtual bool baseClassOf(const DagRecTy *RHS) const { return true; }
374 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
378 /// RecordRecTy - '[classname]' - Represent an instance of a class, such as:
381 class RecordRecTy : public RecTy {
384 explicit RecordRecTy(Record *R) : Rec(R) {}
386 Record *getRecord() const { return Rec; }
388 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
389 virtual Init *convertValue( BitInit *BI) { return 0; }
390 virtual Init *convertValue( BitsInit *BI) { return 0; }
391 virtual Init *convertValue( IntInit *II) { return 0; }
392 virtual Init *convertValue(StringInit *SI) { return 0; }
393 virtual Init *convertValue( ListInit *LI) { return 0; }
394 virtual Init *convertValue( CodeInit *CI) { return 0; }
395 virtual Init *convertValue(VarBitInit *VB) { return 0; }
396 virtual Init *convertValue( BinOpInit *UI) { return 0; }
397 virtual Init *convertValue( DefInit *DI);
398 virtual Init *convertValue( DagInit *DI) { return 0; }
399 virtual Init *convertValue( TypedInit *VI);
400 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
401 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
403 std::string getAsString() const;
405 bool typeIsConvertibleTo(const RecTy *RHS) const {
406 return RHS->baseClassOf(this);
408 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
409 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
410 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
411 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
412 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
413 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
414 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
415 virtual bool baseClassOf(const RecordRecTy *RHS) const;
420 //===----------------------------------------------------------------------===//
421 // Initializer Classes
422 //===----------------------------------------------------------------------===//
427 /// isComplete - This virtual method should be overridden by values that may
428 /// not be completely specified yet.
429 virtual bool isComplete() const { return true; }
431 /// print - Print out this value.
432 void print(std::ostream &OS) const { OS << getAsString(); }
434 /// getAsString - Convert this value to a string form.
435 virtual std::string getAsString() const = 0;
437 /// dump - Debugging method that may be called through a debugger, just
438 /// invokes print on cerr.
441 /// convertInitializerTo - This virtual function is a simple call-back
442 /// function that should be overridden to call the appropriate
443 /// RecTy::convertValue method.
445 virtual Init *convertInitializerTo(RecTy *Ty) = 0;
447 /// convertInitializerBitRange - This method is used to implement the bitrange
448 /// selection operator. Given an initializer, it selects the specified bits
449 /// out, returning them as a new init of bits type. If it is not legal to use
450 /// the bit subscript operator on this initializer, return null.
452 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits) {
456 /// convertInitListSlice - This method is used to implement the list slice
457 /// selection operator. Given an initializer, it selects the specified list
458 /// elements, returning them as a new init of list type. If it is not legal
459 /// to take a slice of this, return null.
461 virtual Init *convertInitListSlice(const std::vector<unsigned> &Elements) {
465 /// getFieldType - This method is used to implement the FieldInit class.
466 /// Implementors of this method should return the type of the named field if
467 /// they are of record type.
469 virtual RecTy *getFieldType(const std::string &FieldName) const { return 0; }
471 /// getFieldInit - This method complements getFieldType to return the
472 /// initializer for the specified field. If getFieldType returns non-null
473 /// this method should return non-null, otherwise it returns null.
475 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const {
479 /// resolveReferences - This method is used by classes that refer to other
480 /// variables which may not be defined at the time they expression is formed.
481 /// If a value is set for the variable later, this method will be called on
482 /// users of the value to allow the value to propagate out.
484 virtual Init *resolveReferences(Record &R, const RecordVal *RV) {
489 inline std::ostream &operator<<(std::ostream &OS, const Init &I) {
490 I.print(OS); return OS;
494 /// UnsetInit - ? - Represents an uninitialized value
496 class UnsetInit : public Init {
498 virtual Init *convertInitializerTo(RecTy *Ty) {
499 return Ty->convertValue(this);
502 virtual bool isComplete() const { return false; }
503 virtual std::string getAsString() const { return "?"; }
507 /// BitInit - true/false - Represent a concrete initializer for a bit.
509 class BitInit : public Init {
512 explicit BitInit(bool V) : Value(V) {}
514 bool getValue() const { return Value; }
516 virtual Init *convertInitializerTo(RecTy *Ty) {
517 return Ty->convertValue(this);
520 virtual std::string getAsString() const { return Value ? "1" : "0"; }
523 /// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
524 /// It contains a vector of bits, whose size is determined by the type.
526 class BitsInit : public Init {
527 std::vector<Init*> Bits;
529 explicit BitsInit(unsigned Size) : Bits(Size) {}
531 unsigned getNumBits() const { return Bits.size(); }
533 Init *getBit(unsigned Bit) const {
534 assert(Bit < Bits.size() && "Bit index out of range!");
537 void setBit(unsigned Bit, Init *V) {
538 assert(Bit < Bits.size() && "Bit index out of range!");
539 assert(Bits[Bit] == 0 && "Bit already set!");
543 virtual Init *convertInitializerTo(RecTy *Ty) {
544 return Ty->convertValue(this);
546 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
548 virtual bool isComplete() const {
549 for (unsigned i = 0; i != getNumBits(); ++i)
550 if (!getBit(i)->isComplete()) return false;
553 virtual std::string getAsString() const;
555 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
557 // printXX - Print this bitstream with the specified format, returning true if
558 // it is not possible.
559 bool printInHex(std::ostream &OS) const;
560 bool printAsVariable(std::ostream &OS) const;
561 bool printAsUnset(std::ostream &OS) const;
565 /// IntInit - 7 - Represent an initalization by a literal integer value.
567 class IntInit : public Init {
570 explicit IntInit(int V) : Value(V) {}
572 int getValue() const { return Value; }
574 virtual Init *convertInitializerTo(RecTy *Ty) {
575 return Ty->convertValue(this);
577 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
579 virtual std::string getAsString() const;
583 /// StringInit - "foo" - Represent an initialization by a string value.
585 class StringInit : public Init {
588 explicit StringInit(const std::string &V) : Value(V) {}
590 const std::string &getValue() const { return Value; }
592 virtual Init *convertInitializerTo(RecTy *Ty) {
593 return Ty->convertValue(this);
596 virtual std::string getAsString() const { return "\"" + Value + "\""; }
599 /// CodeInit - "[{...}]" - Represent a code fragment.
601 class CodeInit : public Init {
604 explicit CodeInit(const std::string &V) : Value(V) {}
606 const std::string getValue() const { return Value; }
608 virtual Init *convertInitializerTo(RecTy *Ty) {
609 return Ty->convertValue(this);
612 virtual std::string getAsString() const { return "[{" + Value + "}]"; }
615 /// ListInit - [AL, AH, CL] - Represent a list of defs
617 class ListInit : public Init {
618 std::vector<Init*> Values;
620 explicit ListInit(std::vector<Init*> &Vs) {
624 unsigned getSize() const { return Values.size(); }
625 Init *getElement(unsigned i) const {
626 assert(i < Values.size() && "List element index out of range!");
630 Record *getElementAsRecord(unsigned i) const;
632 Init *convertInitListSlice(const std::vector<unsigned> &Elements);
634 virtual Init *convertInitializerTo(RecTy *Ty) {
635 return Ty->convertValue(this);
638 /// resolveReferences - This method is used by classes that refer to other
639 /// variables which may not be defined at the time they expression is formed.
640 /// If a value is set for the variable later, this method will be called on
641 /// users of the value to allow the value to propagate out.
643 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
645 virtual std::string getAsString() const;
647 typedef std::vector<Init*>::iterator iterator;
648 typedef std::vector<Init*>::const_iterator const_iterator;
650 inline iterator begin() { return Values.begin(); }
651 inline const_iterator begin() const { return Values.begin(); }
652 inline iterator end () { return Values.end(); }
653 inline const_iterator end () const { return Values.end(); }
655 inline size_t size () const { return Values.size(); }
656 inline bool empty() const { return Values.empty(); }
659 /// BinOpInit - !op (X, Y) - Combine two inits.
661 class BinOpInit : public Init {
663 enum BinaryOp { SHL, SRA, SRL, STRCONCAT, CONCAT };
668 BinOpInit(BinaryOp opc, Init *lhs, Init *rhs) : Opc(opc), LHS(lhs), RHS(rhs) {
671 BinaryOp getOpcode() const { return Opc; }
672 Init *getLHS() const { return LHS; }
673 Init *getRHS() const { return RHS; }
675 // Fold - If possible, fold this to a simpler init. Return this if not
679 virtual Init *convertInitializerTo(RecTy *Ty) {
680 return Ty->convertValue(this);
683 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
685 virtual std::string getAsString() const;
690 /// TypedInit - This is the common super-class of types that have a specific,
693 class TypedInit : public Init {
696 explicit TypedInit(RecTy *T) : Ty(T) {}
698 RecTy *getType() const { return Ty; }
700 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
701 virtual Init *convertInitListSlice(const std::vector<unsigned> &Elements);
703 /// resolveBitReference - This method is used to implement
704 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
705 /// simply return the resolved value, otherwise we return null.
707 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
710 /// resolveListElementReference - This method is used to implement
711 /// VarListElementInit::resolveReferences. If the list element is resolvable
712 /// now, we return the resolved value, otherwise we return null.
713 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
717 /// VarInit - 'Opcode' - Represent a reference to an entire variable object.
719 class VarInit : public TypedInit {
722 explicit VarInit(const std::string &VN, RecTy *T)
723 : TypedInit(T), VarName(VN) {}
725 virtual Init *convertInitializerTo(RecTy *Ty) {
726 return Ty->convertValue(this);
729 const std::string &getName() const { return VarName; }
731 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
733 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
736 virtual RecTy *getFieldType(const std::string &FieldName) const;
737 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
739 /// resolveReferences - This method is used by classes that refer to other
740 /// variables which may not be defined at the time they expression is formed.
741 /// If a value is set for the variable later, this method will be called on
742 /// users of the value to allow the value to propagate out.
744 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
746 virtual std::string getAsString() const { return VarName; }
750 /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
752 class VarBitInit : public Init {
756 VarBitInit(TypedInit *T, unsigned B) : TI(T), Bit(B) {
757 assert(T->getType() && dynamic_cast<BitsRecTy*>(T->getType()) &&
758 ((BitsRecTy*)T->getType())->getNumBits() > B &&
759 "Illegal VarBitInit expression!");
762 virtual Init *convertInitializerTo(RecTy *Ty) {
763 return Ty->convertValue(this);
766 TypedInit *getVariable() const { return TI; }
767 unsigned getBitNum() const { return Bit; }
769 virtual std::string getAsString() const;
770 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
773 /// VarListElementInit - List[4] - Represent access to one element of a var or
775 class VarListElementInit : public TypedInit {
779 VarListElementInit(TypedInit *T, unsigned E)
780 : TypedInit(dynamic_cast<ListRecTy*>(T->getType())->getElementType()),
782 assert(T->getType() && dynamic_cast<ListRecTy*>(T->getType()) &&
783 "Illegal VarBitInit expression!");
786 virtual Init *convertInitializerTo(RecTy *Ty) {
787 return Ty->convertValue(this);
790 TypedInit *getVariable() const { return TI; }
791 unsigned getElementNum() const { return Element; }
793 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
796 /// resolveListElementReference - This method is used to implement
797 /// VarListElementInit::resolveReferences. If the list element is resolvable
798 /// now, we return the resolved value, otherwise we return null.
799 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
802 virtual std::string getAsString() const;
803 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
806 /// DefInit - AL - Represent a reference to a 'def' in the description
808 class DefInit : public Init {
811 explicit DefInit(Record *D) : Def(D) {}
813 virtual Init *convertInitializerTo(RecTy *Ty) {
814 return Ty->convertValue(this);
817 Record *getDef() const { return Def; }
819 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
821 virtual RecTy *getFieldType(const std::string &FieldName) const;
822 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
824 virtual std::string getAsString() const;
828 /// FieldInit - X.Y - Represent a reference to a subfield of a variable
830 class FieldInit : public TypedInit {
831 Init *Rec; // Record we are referring to
832 std::string FieldName; // Field we are accessing
834 FieldInit(Init *R, const std::string &FN)
835 : TypedInit(R->getFieldType(FN)), Rec(R), FieldName(FN) {
836 assert(getType() && "FieldInit with non-record type!");
839 virtual Init *convertInitializerTo(RecTy *Ty) {
840 return Ty->convertValue(this);
843 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
845 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
848 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
850 virtual std::string getAsString() const {
851 return Rec->getAsString() + "." + FieldName;
855 /// DagInit - (v a, b) - Represent a DAG tree value. DAG inits are required
856 /// to have at least one value then a (possibly empty) list of arguments. Each
857 /// argument can have a name associated with it.
859 class DagInit : public Init {
861 std::vector<Init*> Args;
862 std::vector<std::string> ArgNames;
864 DagInit(Init *V, const std::vector<std::pair<Init*, std::string> > &args)
866 Args.reserve(args.size());
867 ArgNames.reserve(args.size());
868 for (unsigned i = 0, e = args.size(); i != e; ++i) {
869 Args.push_back(args[i].first);
870 ArgNames.push_back(args[i].second);
873 DagInit(Init *V, const std::vector<Init*> &args,
874 const std::vector<std::string> &argNames)
875 : Val(V), Args(args), ArgNames(argNames) {
878 virtual Init *convertInitializerTo(RecTy *Ty) {
879 return Ty->convertValue(this);
882 Init *getOperator() const { return Val; }
884 unsigned getNumArgs() const { return Args.size(); }
885 Init *getArg(unsigned Num) const {
886 assert(Num < Args.size() && "Arg number out of range!");
889 const std::string &getArgName(unsigned Num) const {
890 assert(Num < ArgNames.size() && "Arg number out of range!");
891 return ArgNames[Num];
894 void setArg(unsigned Num, Init *I) {
895 assert(Num < Args.size() && "Arg number out of range!");
899 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
901 virtual std::string getAsString() const;
903 typedef std::vector<Init*>::iterator arg_iterator;
904 typedef std::vector<Init*>::const_iterator const_arg_iterator;
905 typedef std::vector<std::string>::iterator name_iterator;
906 typedef std::vector<std::string>::const_iterator const_name_iterator;
908 inline arg_iterator arg_begin() { return Args.begin(); }
909 inline const_arg_iterator arg_begin() const { return Args.begin(); }
910 inline arg_iterator arg_end () { return Args.end(); }
911 inline const_arg_iterator arg_end () const { return Args.end(); }
913 inline size_t arg_size () const { return Args.size(); }
914 inline bool arg_empty() const { return Args.empty(); }
916 inline name_iterator name_begin() { return ArgNames.begin(); }
917 inline const_name_iterator name_begin() const { return ArgNames.begin(); }
918 inline name_iterator name_end () { return ArgNames.end(); }
919 inline const_name_iterator name_end () const { return ArgNames.end(); }
921 inline size_t name_size () const { return ArgNames.size(); }
922 inline bool name_empty() const { return ArgNames.empty(); }
926 //===----------------------------------------------------------------------===//
927 // High-Level Classes
928 //===----------------------------------------------------------------------===//
936 RecordVal(const std::string &N, RecTy *T, unsigned P);
938 const std::string &getName() const { return Name; }
940 unsigned getPrefix() const { return Prefix; }
941 RecTy *getType() const { return Ty; }
942 Init *getValue() const { return Value; }
944 bool setValue(Init *V) {
946 Value = V->convertInitializerTo(Ty);
954 void print(std::ostream &OS, bool PrintSem = true) const;
957 inline std::ostream &operator<<(std::ostream &OS, const RecordVal &RV) {
964 std::vector<std::string> TemplateArgs;
965 std::vector<RecordVal> Values;
966 std::vector<Record*> SuperClasses;
969 explicit Record(const std::string &N) : Name(N) {}
972 const std::string &getName() const { return Name; }
973 void setName(const std::string &Name); // Also updates RecordKeeper.
974 const std::vector<std::string> &getTemplateArgs() const {
977 const std::vector<RecordVal> &getValues() const { return Values; }
978 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; }
980 bool isTemplateArg(const std::string &Name) const {
981 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
982 if (TemplateArgs[i] == Name) return true;
986 const RecordVal *getValue(const std::string &Name) const {
987 for (unsigned i = 0, e = Values.size(); i != e; ++i)
988 if (Values[i].getName() == Name) return &Values[i];
991 RecordVal *getValue(const std::string &Name) {
992 for (unsigned i = 0, e = Values.size(); i != e; ++i)
993 if (Values[i].getName() == Name) return &Values[i];
997 void addTemplateArg(const std::string &Name) {
998 assert(!isTemplateArg(Name) && "Template arg already defined!");
999 TemplateArgs.push_back(Name);
1002 void addValue(const RecordVal &RV) {
1003 assert(getValue(RV.getName()) == 0 && "Value already added!");
1004 Values.push_back(RV);
1007 void removeValue(const std::string &Name) {
1008 assert(getValue(Name) && "Cannot remove an entry that does not exist!");
1009 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1010 if (Values[i].getName() == Name) {
1011 Values.erase(Values.begin()+i);
1014 assert(0 && "Name does not exist in record!");
1017 bool isSubClassOf(Record *R) const {
1018 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1019 if (SuperClasses[i] == R)
1024 bool isSubClassOf(const std::string &Name) const {
1025 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1026 if (SuperClasses[i]->getName() == Name)
1031 void addSuperClass(Record *R) {
1032 assert(!isSubClassOf(R) && "Already subclassing record!");
1033 SuperClasses.push_back(R);
1036 /// resolveReferences - If there are any field references that refer to fields
1037 /// that have been filled in, we can propagate the values now.
1039 void resolveReferences() { resolveReferencesTo(0); }
1041 /// resolveReferencesTo - If anything in this record refers to RV, replace the
1042 /// reference to RV with the RHS of RV. If RV is null, we resolve all
1043 /// possible references.
1044 void resolveReferencesTo(const RecordVal *RV);
1048 //===--------------------------------------------------------------------===//
1049 // High-level methods useful to tablegen back-ends
1052 /// getValueInit - Return the initializer for a value with the specified name,
1053 /// or throw an exception if the field does not exist.
1055 Init *getValueInit(const std::string &FieldName) const;
1057 /// getValueAsString - This method looks up the specified field and returns
1058 /// its value as a string, throwing an exception if the field does not exist
1059 /// or if the value is not a string.
1061 std::string getValueAsString(const std::string &FieldName) const;
1063 /// getValueAsBitsInit - This method looks up the specified field and returns
1064 /// its value as a BitsInit, throwing an exception if the field does not exist
1065 /// or if the value is not the right type.
1067 BitsInit *getValueAsBitsInit(const std::string &FieldName) const;
1069 /// getValueAsListInit - This method looks up the specified field and returns
1070 /// its value as a ListInit, throwing an exception if the field does not exist
1071 /// or if the value is not the right type.
1073 ListInit *getValueAsListInit(const std::string &FieldName) const;
1075 /// getValueAsListOfDefs - This method looks up the specified field and
1076 /// returns its value as a vector of records, throwing an exception if the
1077 /// field does not exist or if the value is not the right type.
1079 std::vector<Record*> getValueAsListOfDefs(const std::string &FieldName) const;
1081 /// getValueAsListOfInts - This method looks up the specified field and returns
1082 /// its value as a vector of integers, throwing an exception if the field does
1083 /// not exist or if the value is not the right type.
1085 std::vector<int> getValueAsListOfInts(const std::string &FieldName) const;
1087 /// getValueAsDef - This method looks up the specified field and returns its
1088 /// value as a Record, throwing an exception if the field does not exist or if
1089 /// the value is not the right type.
1091 Record *getValueAsDef(const std::string &FieldName) const;
1093 /// getValueAsBit - This method looks up the specified field and returns its
1094 /// value as a bit, throwing an exception if the field does not exist or if
1095 /// the value is not the right type.
1097 bool getValueAsBit(const std::string &FieldName) const;
1099 /// getValueAsInt - This method looks up the specified field and returns its
1100 /// value as an int, throwing an exception if the field does not exist or if
1101 /// the value is not the right type.
1103 int getValueAsInt(const std::string &FieldName) const;
1105 /// getValueAsDag - This method looks up the specified field and returns its
1106 /// value as an Dag, throwing an exception if the field does not exist or if
1107 /// the value is not the right type.
1109 DagInit *getValueAsDag(const std::string &FieldName) const;
1111 /// getValueAsCode - This method looks up the specified field and returns
1112 /// its value as the string data in a CodeInit, throwing an exception if the
1113 /// field does not exist or if the value is not a code object.
1115 std::string getValueAsCode(const std::string &FieldName) const;
1118 std::ostream &operator<<(std::ostream &OS, const Record &R);
1120 class RecordKeeper {
1121 std::map<std::string, Record*> Classes, Defs;
1124 for (std::map<std::string, Record*>::iterator I = Classes.begin(),
1125 E = Classes.end(); I != E; ++I)
1127 for (std::map<std::string, Record*>::iterator I = Defs.begin(),
1128 E = Defs.end(); I != E; ++I)
1132 const std::map<std::string, Record*> &getClasses() const { return Classes; }
1133 const std::map<std::string, Record*> &getDefs() const { return Defs; }
1135 Record *getClass(const std::string &Name) const {
1136 std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
1137 return I == Classes.end() ? 0 : I->second;
1139 Record *getDef(const std::string &Name) const {
1140 std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
1141 return I == Defs.end() ? 0 : I->second;
1143 void addClass(Record *R) {
1144 assert(getClass(R->getName()) == 0 && "Class already exists!");
1145 Classes.insert(std::make_pair(R->getName(), R));
1147 void addDef(Record *R) {
1148 assert(getDef(R->getName()) == 0 && "Def already exists!");
1149 Defs.insert(std::make_pair(R->getName(), R));
1152 /// removeClass - Remove, but do not delete, the specified record.
1154 void removeClass(const std::string &Name) {
1155 assert(Classes.count(Name) && "Class does not exist!");
1156 Classes.erase(Name);
1158 /// removeDef - Remove, but do not delete, the specified record.
1160 void removeDef(const std::string &Name) {
1161 assert(Defs.count(Name) && "Def does not exist!");
1165 //===--------------------------------------------------------------------===//
1166 // High-level helper methods, useful for tablegen backends...
1168 /// getAllDerivedDefinitions - This method returns all concrete definitions
1169 /// that derive from the specified class name. If a class with the specified
1170 /// name does not exist, an exception is thrown.
1171 std::vector<Record*>
1172 getAllDerivedDefinitions(const std::string &ClassName) const;
1178 /// LessRecord - Sorting predicate to sort record pointers by name.
1181 bool operator()(const Record *Rec1, const Record *Rec2) const {
1182 return Rec1->getName() < Rec2->getName();
1186 /// LessRecordFieldName - Sorting predicate to sort record pointers by their
1189 struct LessRecordFieldName {
1190 bool operator()(const Record *Rec1, const Record *Rec2) const {
1191 return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name");
1196 std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK);
1198 extern RecordKeeper Records;
1200 } // End llvm namespace