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/DataTypes.h"
53 class VarListElementInit;
59 //===----------------------------------------------------------------------===//
61 //===----------------------------------------------------------------------===//
66 virtual std::string getAsString() const = 0;
67 void print(std::ostream &OS) const { OS << getAsString(); }
70 /// typeIsConvertibleTo - Return true if all values of 'this' type can be
71 /// converted to the specified type.
72 virtual bool typeIsConvertibleTo(const RecTy *RHS) const = 0;
74 public: // These methods should only be called from subclasses of Init
75 virtual Init *convertValue( UnsetInit *UI) { return 0; }
76 virtual Init *convertValue( BitInit *BI) { return 0; }
77 virtual Init *convertValue( BitsInit *BI) { return 0; }
78 virtual Init *convertValue( IntInit *II) { return 0; }
79 virtual Init *convertValue(StringInit *SI) { return 0; }
80 virtual Init *convertValue( ListInit *LI) { return 0; }
81 virtual Init *convertValue( BinOpInit *UI) { return 0; }
82 virtual Init *convertValue( CodeInit *CI) { return 0; }
83 virtual Init *convertValue(VarBitInit *VB) { return 0; }
84 virtual Init *convertValue( DefInit *DI) { return 0; }
85 virtual Init *convertValue( DagInit *DI) { return 0; }
86 virtual Init *convertValue( TypedInit *TI) { return 0; }
87 virtual Init *convertValue( VarInit *VI) {
88 return convertValue((TypedInit*)VI);
90 virtual Init *convertValue( FieldInit *FI) {
91 return convertValue((TypedInit*)FI);
94 public: // These methods should only be called by subclasses of RecTy.
95 // baseClassOf - These virtual methods should be overloaded to return true iff
96 // all values of type 'RHS' can be converted to the 'this' type.
97 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
98 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
99 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
100 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
101 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
102 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
103 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
104 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
107 inline std::ostream &operator<<(std::ostream &OS, const RecTy &Ty) {
113 /// BitRecTy - 'bit' - Represent a single bit
115 class BitRecTy : public RecTy {
117 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
118 virtual Init *convertValue( BitInit *BI) { return (Init*)BI; }
119 virtual Init *convertValue( BitsInit *BI);
120 virtual Init *convertValue( IntInit *II);
121 virtual Init *convertValue(StringInit *SI) { return 0; }
122 virtual Init *convertValue( ListInit *LI) { return 0; }
123 virtual Init *convertValue( CodeInit *CI) { return 0; }
124 virtual Init *convertValue(VarBitInit *VB) { return (Init*)VB; }
125 virtual Init *convertValue( DefInit *DI) { return 0; }
126 virtual Init *convertValue( DagInit *DI) { return 0; }
127 virtual Init *convertValue( BinOpInit *UI) { return 0; }
128 virtual Init *convertValue( TypedInit *TI);
129 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
130 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
132 std::string getAsString() const { return "bit"; }
134 bool typeIsConvertibleTo(const RecTy *RHS) const {
135 return RHS->baseClassOf(this);
137 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
138 virtual bool baseClassOf(const BitsRecTy *RHS) const;
139 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
140 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
141 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
142 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
143 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
144 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
149 // BitsRecTy - 'bits<n>' - Represent a fixed number of bits
150 /// BitsRecTy - 'bits<n>' - Represent a fixed number of bits
152 class BitsRecTy : public RecTy {
155 explicit BitsRecTy(unsigned Sz) : Size(Sz) {}
157 unsigned getNumBits() const { return Size; }
159 virtual Init *convertValue( UnsetInit *UI);
160 virtual Init *convertValue( BitInit *UI);
161 virtual Init *convertValue( BitsInit *BI);
162 virtual Init *convertValue( IntInit *II);
163 virtual Init *convertValue(StringInit *SI) { return 0; }
164 virtual Init *convertValue( ListInit *LI) { return 0; }
165 virtual Init *convertValue( CodeInit *CI) { return 0; }
166 virtual Init *convertValue(VarBitInit *VB) { return 0; }
167 virtual Init *convertValue( DefInit *DI) { return 0; }
168 virtual Init *convertValue( DagInit *DI) { return 0; }
169 virtual Init *convertValue( BinOpInit *UI) { return 0; }
170 virtual Init *convertValue( TypedInit *TI);
171 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
172 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
174 std::string getAsString() const;
176 bool typeIsConvertibleTo(const RecTy *RHS) const {
177 return RHS->baseClassOf(this);
179 virtual bool baseClassOf(const BitRecTy *RHS) const { return Size == 1; }
180 virtual bool baseClassOf(const BitsRecTy *RHS) const {
181 return RHS->Size == Size;
183 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
184 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
185 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
186 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
187 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
188 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
193 /// IntRecTy - 'int' - Represent an integer value of no particular size
195 class IntRecTy : public RecTy {
197 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
198 virtual Init *convertValue( BitInit *BI);
199 virtual Init *convertValue( BitsInit *BI);
200 virtual Init *convertValue( IntInit *II) { return (Init*)II; }
201 virtual Init *convertValue(StringInit *SI) { return 0; }
202 virtual Init *convertValue( ListInit *LI) { return 0; }
203 virtual Init *convertValue( CodeInit *CI) { return 0; }
204 virtual Init *convertValue(VarBitInit *VB) { return 0; }
205 virtual Init *convertValue( DefInit *DI) { return 0; }
206 virtual Init *convertValue( DagInit *DI) { return 0; }
207 virtual Init *convertValue( BinOpInit *UI) { return 0; }
208 virtual Init *convertValue( TypedInit *TI);
209 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
210 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
212 std::string getAsString() const { return "int"; }
214 bool typeIsConvertibleTo(const RecTy *RHS) const {
215 return RHS->baseClassOf(this);
218 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
219 virtual bool baseClassOf(const BitsRecTy *RHS) const { return true; }
220 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
221 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
222 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
223 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
224 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
225 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
229 /// StringRecTy - 'string' - Represent an string value
231 class StringRecTy : public RecTy {
233 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
234 virtual Init *convertValue( BitInit *BI) { return 0; }
235 virtual Init *convertValue( BitsInit *BI) { return 0; }
236 virtual Init *convertValue( IntInit *II) { return 0; }
237 virtual Init *convertValue(StringInit *SI) { return (Init*)SI; }
238 virtual Init *convertValue( ListInit *LI) { return 0; }
239 virtual Init *convertValue( BinOpInit *BO);
240 virtual Init *convertValue( CodeInit *CI) { return 0; }
241 virtual Init *convertValue(VarBitInit *VB) { return 0; }
242 virtual Init *convertValue( DefInit *DI) { return 0; }
243 virtual Init *convertValue( DagInit *DI) { return 0; }
244 virtual Init *convertValue( TypedInit *TI);
245 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
246 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
248 std::string getAsString() const { return "string"; }
250 bool typeIsConvertibleTo(const RecTy *RHS) const {
251 return RHS->baseClassOf(this);
254 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
255 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
256 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
257 virtual bool baseClassOf(const StringRecTy *RHS) const { return true; }
258 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
259 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
260 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
261 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
264 // ListRecTy - 'list<Ty>' - Represent a list of values, all of which must be of
265 // the specified type.
266 /// ListRecTy - 'list<Ty>' - Represent a list of values, all of which must
267 /// be of the specified type.
269 class ListRecTy : public RecTy {
272 explicit ListRecTy(RecTy *T) : Ty(T) {}
274 RecTy *getElementType() const { return Ty; }
276 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
277 virtual Init *convertValue( BitInit *BI) { return 0; }
278 virtual Init *convertValue( BitsInit *BI) { return 0; }
279 virtual Init *convertValue( IntInit *II) { return 0; }
280 virtual Init *convertValue(StringInit *SI) { return 0; }
281 virtual Init *convertValue( ListInit *LI);
282 virtual Init *convertValue( CodeInit *CI) { return 0; }
283 virtual Init *convertValue(VarBitInit *VB) { return 0; }
284 virtual Init *convertValue( DefInit *DI) { return 0; }
285 virtual Init *convertValue( DagInit *DI) { return 0; }
286 virtual Init *convertValue( BinOpInit *UI) { return 0; }
287 virtual Init *convertValue( TypedInit *TI);
288 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
289 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
291 std::string getAsString() const;
293 bool typeIsConvertibleTo(const RecTy *RHS) const {
294 return RHS->baseClassOf(this);
297 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
298 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
299 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
300 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
301 virtual bool baseClassOf(const ListRecTy *RHS) const {
302 return RHS->getElementType()->typeIsConvertibleTo(Ty);
304 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
305 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
306 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
309 /// CodeRecTy - 'code' - Represent an code fragment, function or method.
311 class CodeRecTy : public RecTy {
313 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
314 virtual Init *convertValue( BitInit *BI) { return 0; }
315 virtual Init *convertValue( BitsInit *BI) { return 0; }
316 virtual Init *convertValue( IntInit *II) { return 0; }
317 virtual Init *convertValue(StringInit *SI) { return 0; }
318 virtual Init *convertValue( ListInit *LI) { return 0; }
319 virtual Init *convertValue( CodeInit *CI) { return (Init*)CI; }
320 virtual Init *convertValue(VarBitInit *VB) { return 0; }
321 virtual Init *convertValue( DefInit *DI) { return 0; }
322 virtual Init *convertValue( DagInit *DI) { return 0; }
323 virtual Init *convertValue( BinOpInit *UI) { return 0; }
324 virtual Init *convertValue( TypedInit *TI);
325 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
326 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
328 std::string getAsString() const { return "code"; }
330 bool typeIsConvertibleTo(const RecTy *RHS) const {
331 return RHS->baseClassOf(this);
333 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
334 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
335 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
336 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
337 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
338 virtual bool baseClassOf(const CodeRecTy *RHS) const { return true; }
339 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
340 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
343 /// DagRecTy - 'dag' - Represent a dag fragment
345 class DagRecTy : public RecTy {
347 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
348 virtual Init *convertValue( BitInit *BI) { return 0; }
349 virtual Init *convertValue( BitsInit *BI) { return 0; }
350 virtual Init *convertValue( IntInit *II) { return 0; }
351 virtual Init *convertValue(StringInit *SI) { return 0; }
352 virtual Init *convertValue( ListInit *LI) { return 0; }
353 virtual Init *convertValue( CodeInit *CI) { return 0; }
354 virtual Init *convertValue(VarBitInit *VB) { return 0; }
355 virtual Init *convertValue( DefInit *DI) { return 0; }
356 virtual Init *convertValue( BinOpInit *BO);
357 virtual Init *convertValue( DagInit *CI) { return (Init*)CI; }
358 virtual Init *convertValue( TypedInit *TI);
359 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
360 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
362 std::string getAsString() const { return "dag"; }
364 bool typeIsConvertibleTo(const RecTy *RHS) const {
365 return RHS->baseClassOf(this);
368 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
369 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
370 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
371 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
372 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
373 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
374 virtual bool baseClassOf(const DagRecTy *RHS) const { return true; }
375 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
379 /// RecordRecTy - '[classname]' - Represent an instance of a class, such as:
382 class RecordRecTy : public RecTy {
385 explicit RecordRecTy(Record *R) : Rec(R) {}
387 Record *getRecord() const { return Rec; }
389 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
390 virtual Init *convertValue( BitInit *BI) { return 0; }
391 virtual Init *convertValue( BitsInit *BI) { return 0; }
392 virtual Init *convertValue( IntInit *II) { return 0; }
393 virtual Init *convertValue(StringInit *SI) { return 0; }
394 virtual Init *convertValue( ListInit *LI) { return 0; }
395 virtual Init *convertValue( CodeInit *CI) { return 0; }
396 virtual Init *convertValue(VarBitInit *VB) { return 0; }
397 virtual Init *convertValue( BinOpInit *UI) { return 0; }
398 virtual Init *convertValue( DefInit *DI);
399 virtual Init *convertValue( DagInit *DI) { return 0; }
400 virtual Init *convertValue( TypedInit *VI);
401 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
402 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
404 std::string getAsString() const;
406 bool typeIsConvertibleTo(const RecTy *RHS) const {
407 return RHS->baseClassOf(this);
409 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
410 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
411 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
412 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
413 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
414 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
415 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
416 virtual bool baseClassOf(const RecordRecTy *RHS) const;
421 //===----------------------------------------------------------------------===//
422 // Initializer Classes
423 //===----------------------------------------------------------------------===//
428 /// isComplete - This virtual method should be overridden by values that may
429 /// not be completely specified yet.
430 virtual bool isComplete() const { return true; }
432 /// print - Print out this value.
433 void print(std::ostream &OS) const { OS << getAsString(); }
435 /// getAsString - Convert this value to a string form.
436 virtual std::string getAsString() const = 0;
438 /// dump - Debugging method that may be called through a debugger, just
439 /// invokes print on cerr.
442 /// convertInitializerTo - This virtual function is a simple call-back
443 /// function that should be overridden to call the appropriate
444 /// RecTy::convertValue method.
446 virtual Init *convertInitializerTo(RecTy *Ty) = 0;
448 /// convertInitializerBitRange - This method is used to implement the bitrange
449 /// selection operator. Given an initializer, it selects the specified bits
450 /// out, returning them as a new init of bits type. If it is not legal to use
451 /// the bit subscript operator on this initializer, return null.
453 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits) {
457 /// convertInitListSlice - This method is used to implement the list slice
458 /// selection operator. Given an initializer, it selects the specified list
459 /// elements, returning them as a new init of list type. If it is not legal
460 /// to take a slice of this, return null.
462 virtual Init *convertInitListSlice(const std::vector<unsigned> &Elements) {
466 /// getFieldType - This method is used to implement the FieldInit class.
467 /// Implementors of this method should return the type of the named field if
468 /// they are of record type.
470 virtual RecTy *getFieldType(const std::string &FieldName) const { return 0; }
472 /// getFieldInit - This method complements getFieldType to return the
473 /// initializer for the specified field. If getFieldType returns non-null
474 /// this method should return non-null, otherwise it returns null.
476 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const {
480 /// resolveReferences - This method is used by classes that refer to other
481 /// variables which may not be defined at the time they expression is formed.
482 /// If a value is set for the variable later, this method will be called on
483 /// users of the value to allow the value to propagate out.
485 virtual Init *resolveReferences(Record &R, const RecordVal *RV) {
490 inline std::ostream &operator<<(std::ostream &OS, const Init &I) {
491 I.print(OS); return OS;
495 /// UnsetInit - ? - Represents an uninitialized value
497 class UnsetInit : public Init {
499 virtual Init *convertInitializerTo(RecTy *Ty) {
500 return Ty->convertValue(this);
503 virtual bool isComplete() const { return false; }
504 virtual std::string getAsString() const { return "?"; }
508 /// BitInit - true/false - Represent a concrete initializer for a bit.
510 class BitInit : public Init {
513 explicit BitInit(bool V) : Value(V) {}
515 bool getValue() const { return Value; }
517 virtual Init *convertInitializerTo(RecTy *Ty) {
518 return Ty->convertValue(this);
521 virtual std::string getAsString() const { return Value ? "1" : "0"; }
524 /// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
525 /// It contains a vector of bits, whose size is determined by the type.
527 class BitsInit : public Init {
528 std::vector<Init*> Bits;
530 explicit BitsInit(unsigned Size) : Bits(Size) {}
532 unsigned getNumBits() const { return Bits.size(); }
534 Init *getBit(unsigned Bit) const {
535 assert(Bit < Bits.size() && "Bit index out of range!");
538 void setBit(unsigned Bit, Init *V) {
539 assert(Bit < Bits.size() && "Bit index out of range!");
540 assert(Bits[Bit] == 0 && "Bit already set!");
544 virtual Init *convertInitializerTo(RecTy *Ty) {
545 return Ty->convertValue(this);
547 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
549 virtual bool isComplete() const {
550 for (unsigned i = 0; i != getNumBits(); ++i)
551 if (!getBit(i)->isComplete()) return false;
554 virtual std::string getAsString() const;
556 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
558 // printXX - Print this bitstream with the specified format, returning true if
559 // it is not possible.
560 bool printInHex(std::ostream &OS) const;
561 bool printAsVariable(std::ostream &OS) const;
562 bool printAsUnset(std::ostream &OS) const;
566 /// IntInit - 7 - Represent an initalization by a literal integer value.
568 class IntInit : public Init {
571 explicit IntInit(int64_t V) : Value(V) {}
573 int64_t getValue() const { return Value; }
575 virtual Init *convertInitializerTo(RecTy *Ty) {
576 return Ty->convertValue(this);
578 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
580 virtual std::string getAsString() const;
584 /// StringInit - "foo" - Represent an initialization by a string value.
586 class StringInit : public Init {
589 explicit StringInit(const std::string &V) : Value(V) {}
591 const std::string &getValue() const { return Value; }
593 virtual Init *convertInitializerTo(RecTy *Ty) {
594 return Ty->convertValue(this);
597 virtual std::string getAsString() const { return "\"" + Value + "\""; }
600 /// CodeInit - "[{...}]" - Represent a code fragment.
602 class CodeInit : public Init {
605 explicit CodeInit(const std::string &V) : Value(V) {}
607 const std::string getValue() const { return Value; }
609 virtual Init *convertInitializerTo(RecTy *Ty) {
610 return Ty->convertValue(this);
613 virtual std::string getAsString() const { return "[{" + Value + "}]"; }
616 /// ListInit - [AL, AH, CL] - Represent a list of defs
618 class ListInit : public Init {
619 std::vector<Init*> Values;
621 explicit ListInit(std::vector<Init*> &Vs) {
625 unsigned getSize() const { return Values.size(); }
626 Init *getElement(unsigned i) const {
627 assert(i < Values.size() && "List element index out of range!");
631 Record *getElementAsRecord(unsigned i) const;
633 Init *convertInitListSlice(const std::vector<unsigned> &Elements);
635 virtual Init *convertInitializerTo(RecTy *Ty) {
636 return Ty->convertValue(this);
639 /// resolveReferences - This method is used by classes that refer to other
640 /// variables which may not be defined at the time they expression is formed.
641 /// If a value is set for the variable later, this method will be called on
642 /// users of the value to allow the value to propagate out.
644 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
646 virtual std::string getAsString() const;
648 typedef std::vector<Init*>::iterator iterator;
649 typedef std::vector<Init*>::const_iterator const_iterator;
651 inline iterator begin() { return Values.begin(); }
652 inline const_iterator begin() const { return Values.begin(); }
653 inline iterator end () { return Values.end(); }
654 inline const_iterator end () const { return Values.end(); }
656 inline size_t size () const { return Values.size(); }
657 inline bool empty() const { return Values.empty(); }
660 /// BinOpInit - !op (X, Y) - Combine two inits.
662 class BinOpInit : public Init {
664 enum BinaryOp { SHL, SRA, SRL, STRCONCAT, CONCAT };
669 BinOpInit(BinaryOp opc, Init *lhs, Init *rhs) : Opc(opc), LHS(lhs), RHS(rhs) {
672 BinaryOp getOpcode() const { return Opc; }
673 Init *getLHS() const { return LHS; }
674 Init *getRHS() const { return RHS; }
676 // Fold - If possible, fold this to a simpler init. Return this if not
680 virtual Init *convertInitializerTo(RecTy *Ty) {
681 return Ty->convertValue(this);
684 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
686 virtual std::string getAsString() const;
691 /// TypedInit - This is the common super-class of types that have a specific,
694 class TypedInit : public Init {
697 explicit TypedInit(RecTy *T) : Ty(T) {}
699 RecTy *getType() const { return Ty; }
701 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
702 virtual Init *convertInitListSlice(const std::vector<unsigned> &Elements);
704 /// resolveBitReference - This method is used to implement
705 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
706 /// simply return the resolved value, otherwise we return null.
708 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
711 /// resolveListElementReference - This method is used to implement
712 /// VarListElementInit::resolveReferences. If the list element is resolvable
713 /// now, we return the resolved value, otherwise we return null.
714 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
718 /// VarInit - 'Opcode' - Represent a reference to an entire variable object.
720 class VarInit : public TypedInit {
723 explicit VarInit(const std::string &VN, RecTy *T)
724 : TypedInit(T), VarName(VN) {}
726 virtual Init *convertInitializerTo(RecTy *Ty) {
727 return Ty->convertValue(this);
730 const std::string &getName() const { return VarName; }
732 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
734 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
737 virtual RecTy *getFieldType(const std::string &FieldName) const;
738 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
740 /// resolveReferences - This method is used by classes that refer to other
741 /// variables which may not be defined at the time they expression is formed.
742 /// If a value is set for the variable later, this method will be called on
743 /// users of the value to allow the value to propagate out.
745 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
747 virtual std::string getAsString() const { return VarName; }
751 /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
753 class VarBitInit : public Init {
757 VarBitInit(TypedInit *T, unsigned B) : TI(T), Bit(B) {
758 assert(T->getType() && dynamic_cast<BitsRecTy*>(T->getType()) &&
759 ((BitsRecTy*)T->getType())->getNumBits() > B &&
760 "Illegal VarBitInit expression!");
763 virtual Init *convertInitializerTo(RecTy *Ty) {
764 return Ty->convertValue(this);
767 TypedInit *getVariable() const { return TI; }
768 unsigned getBitNum() const { return Bit; }
770 virtual std::string getAsString() const;
771 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
774 /// VarListElementInit - List[4] - Represent access to one element of a var or
776 class VarListElementInit : public TypedInit {
780 VarListElementInit(TypedInit *T, unsigned E)
781 : TypedInit(dynamic_cast<ListRecTy*>(T->getType())->getElementType()),
783 assert(T->getType() && dynamic_cast<ListRecTy*>(T->getType()) &&
784 "Illegal VarBitInit expression!");
787 virtual Init *convertInitializerTo(RecTy *Ty) {
788 return Ty->convertValue(this);
791 TypedInit *getVariable() const { return TI; }
792 unsigned getElementNum() const { return Element; }
794 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
797 /// resolveListElementReference - This method is used to implement
798 /// VarListElementInit::resolveReferences. If the list element is resolvable
799 /// now, we return the resolved value, otherwise we return null.
800 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
803 virtual std::string getAsString() const;
804 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
807 /// DefInit - AL - Represent a reference to a 'def' in the description
809 class DefInit : public Init {
812 explicit DefInit(Record *D) : Def(D) {}
814 virtual Init *convertInitializerTo(RecTy *Ty) {
815 return Ty->convertValue(this);
818 Record *getDef() const { return Def; }
820 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
822 virtual RecTy *getFieldType(const std::string &FieldName) const;
823 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
825 virtual std::string getAsString() const;
829 /// FieldInit - X.Y - Represent a reference to a subfield of a variable
831 class FieldInit : public TypedInit {
832 Init *Rec; // Record we are referring to
833 std::string FieldName; // Field we are accessing
835 FieldInit(Init *R, const std::string &FN)
836 : TypedInit(R->getFieldType(FN)), Rec(R), FieldName(FN) {
837 assert(getType() && "FieldInit with non-record type!");
840 virtual Init *convertInitializerTo(RecTy *Ty) {
841 return Ty->convertValue(this);
844 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
846 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
849 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
851 virtual std::string getAsString() const {
852 return Rec->getAsString() + "." + FieldName;
856 /// DagInit - (v a, b) - Represent a DAG tree value. DAG inits are required
857 /// to have at least one value then a (possibly empty) list of arguments. Each
858 /// argument can have a name associated with it.
860 class DagInit : public Init {
862 std::vector<Init*> Args;
863 std::vector<std::string> ArgNames;
865 DagInit(Init *V, const std::vector<std::pair<Init*, std::string> > &args)
867 Args.reserve(args.size());
868 ArgNames.reserve(args.size());
869 for (unsigned i = 0, e = args.size(); i != e; ++i) {
870 Args.push_back(args[i].first);
871 ArgNames.push_back(args[i].second);
874 DagInit(Init *V, const std::vector<Init*> &args,
875 const std::vector<std::string> &argNames)
876 : Val(V), Args(args), ArgNames(argNames) {
879 virtual Init *convertInitializerTo(RecTy *Ty) {
880 return Ty->convertValue(this);
883 Init *getOperator() const { return Val; }
885 unsigned getNumArgs() const { return Args.size(); }
886 Init *getArg(unsigned Num) const {
887 assert(Num < Args.size() && "Arg number out of range!");
890 const std::string &getArgName(unsigned Num) const {
891 assert(Num < ArgNames.size() && "Arg number out of range!");
892 return ArgNames[Num];
895 void setArg(unsigned Num, Init *I) {
896 assert(Num < Args.size() && "Arg number out of range!");
900 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
902 virtual std::string getAsString() const;
904 typedef std::vector<Init*>::iterator arg_iterator;
905 typedef std::vector<Init*>::const_iterator const_arg_iterator;
906 typedef std::vector<std::string>::iterator name_iterator;
907 typedef std::vector<std::string>::const_iterator const_name_iterator;
909 inline arg_iterator arg_begin() { return Args.begin(); }
910 inline const_arg_iterator arg_begin() const { return Args.begin(); }
911 inline arg_iterator arg_end () { return Args.end(); }
912 inline const_arg_iterator arg_end () const { return Args.end(); }
914 inline size_t arg_size () const { return Args.size(); }
915 inline bool arg_empty() const { return Args.empty(); }
917 inline name_iterator name_begin() { return ArgNames.begin(); }
918 inline const_name_iterator name_begin() const { return ArgNames.begin(); }
919 inline name_iterator name_end () { return ArgNames.end(); }
920 inline const_name_iterator name_end () const { return ArgNames.end(); }
922 inline size_t name_size () const { return ArgNames.size(); }
923 inline bool name_empty() const { return ArgNames.empty(); }
927 //===----------------------------------------------------------------------===//
928 // High-Level Classes
929 //===----------------------------------------------------------------------===//
937 RecordVal(const std::string &N, RecTy *T, unsigned P);
939 const std::string &getName() const { return Name; }
941 unsigned getPrefix() const { return Prefix; }
942 RecTy *getType() const { return Ty; }
943 Init *getValue() const { return Value; }
945 bool setValue(Init *V) {
947 Value = V->convertInitializerTo(Ty);
955 void print(std::ostream &OS, bool PrintSem = true) const;
958 inline std::ostream &operator<<(std::ostream &OS, const RecordVal &RV) {
965 std::vector<std::string> TemplateArgs;
966 std::vector<RecordVal> Values;
967 std::vector<Record*> SuperClasses;
970 explicit Record(const std::string &N) : Name(N) {}
973 const std::string &getName() const { return Name; }
974 void setName(const std::string &Name); // Also updates RecordKeeper.
975 const std::vector<std::string> &getTemplateArgs() const {
978 const std::vector<RecordVal> &getValues() const { return Values; }
979 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; }
981 bool isTemplateArg(const std::string &Name) const {
982 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
983 if (TemplateArgs[i] == Name) return true;
987 const RecordVal *getValue(const std::string &Name) const {
988 for (unsigned i = 0, e = Values.size(); i != e; ++i)
989 if (Values[i].getName() == Name) return &Values[i];
992 RecordVal *getValue(const std::string &Name) {
993 for (unsigned i = 0, e = Values.size(); i != e; ++i)
994 if (Values[i].getName() == Name) return &Values[i];
998 void addTemplateArg(const std::string &Name) {
999 assert(!isTemplateArg(Name) && "Template arg already defined!");
1000 TemplateArgs.push_back(Name);
1003 void addValue(const RecordVal &RV) {
1004 assert(getValue(RV.getName()) == 0 && "Value already added!");
1005 Values.push_back(RV);
1008 void removeValue(const std::string &Name) {
1009 assert(getValue(Name) && "Cannot remove an entry that does not exist!");
1010 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1011 if (Values[i].getName() == Name) {
1012 Values.erase(Values.begin()+i);
1015 assert(0 && "Name does not exist in record!");
1018 bool isSubClassOf(Record *R) const {
1019 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1020 if (SuperClasses[i] == R)
1025 bool isSubClassOf(const std::string &Name) const {
1026 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1027 if (SuperClasses[i]->getName() == Name)
1032 void addSuperClass(Record *R) {
1033 assert(!isSubClassOf(R) && "Already subclassing record!");
1034 SuperClasses.push_back(R);
1037 /// resolveReferences - If there are any field references that refer to fields
1038 /// that have been filled in, we can propagate the values now.
1040 void resolveReferences() { resolveReferencesTo(0); }
1042 /// resolveReferencesTo - If anything in this record refers to RV, replace the
1043 /// reference to RV with the RHS of RV. If RV is null, we resolve all
1044 /// possible references.
1045 void resolveReferencesTo(const RecordVal *RV);
1049 //===--------------------------------------------------------------------===//
1050 // High-level methods useful to tablegen back-ends
1053 /// getValueInit - Return the initializer for a value with the specified name,
1054 /// or throw an exception if the field does not exist.
1056 Init *getValueInit(const std::string &FieldName) const;
1058 /// getValueAsString - This method looks up the specified field and returns
1059 /// its value as a string, throwing an exception if the field does not exist
1060 /// or if the value is not a string.
1062 std::string getValueAsString(const std::string &FieldName) const;
1064 /// getValueAsBitsInit - This method looks up the specified field and returns
1065 /// its value as a BitsInit, throwing an exception if the field does not exist
1066 /// or if the value is not the right type.
1068 BitsInit *getValueAsBitsInit(const std::string &FieldName) const;
1070 /// getValueAsListInit - This method looks up the specified field and returns
1071 /// its value as a ListInit, throwing an exception if the field does not exist
1072 /// or if the value is not the right type.
1074 ListInit *getValueAsListInit(const std::string &FieldName) const;
1076 /// getValueAsListOfDefs - This method looks up the specified field and
1077 /// returns its value as a vector of records, throwing an exception if the
1078 /// field does not exist or if the value is not the right type.
1080 std::vector<Record*> getValueAsListOfDefs(const std::string &FieldName) const;
1082 /// getValueAsListOfInts - This method looks up the specified field and returns
1083 /// its value as a vector of integers, throwing an exception if the field does
1084 /// not exist or if the value is not the right type.
1086 std::vector<int64_t> getValueAsListOfInts(const std::string &FieldName) const;
1088 /// getValueAsDef - This method looks up the specified field and returns its
1089 /// value as a Record, throwing an exception if the field does not exist or if
1090 /// the value is not the right type.
1092 Record *getValueAsDef(const std::string &FieldName) const;
1094 /// getValueAsBit - This method looks up the specified field and returns its
1095 /// value as a bit, throwing an exception if the field does not exist or if
1096 /// the value is not the right type.
1098 bool getValueAsBit(const std::string &FieldName) const;
1100 /// getValueAsInt - This method looks up the specified field and returns its
1101 /// value as an int64_t, throwing an exception if the field does not exist or
1102 /// if the value is not the right type.
1104 int64_t getValueAsInt(const std::string &FieldName) const;
1106 /// getValueAsDag - This method looks up the specified field and returns its
1107 /// value as an Dag, throwing an exception if the field does not exist or if
1108 /// the value is not the right type.
1110 DagInit *getValueAsDag(const std::string &FieldName) const;
1112 /// getValueAsCode - This method looks up the specified field and returns
1113 /// its value as the string data in a CodeInit, throwing an exception if the
1114 /// field does not exist or if the value is not a code object.
1116 std::string getValueAsCode(const std::string &FieldName) const;
1119 std::ostream &operator<<(std::ostream &OS, const Record &R);
1121 class RecordKeeper {
1122 std::map<std::string, Record*> Classes, Defs;
1125 for (std::map<std::string, Record*>::iterator I = Classes.begin(),
1126 E = Classes.end(); I != E; ++I)
1128 for (std::map<std::string, Record*>::iterator I = Defs.begin(),
1129 E = Defs.end(); I != E; ++I)
1133 const std::map<std::string, Record*> &getClasses() const { return Classes; }
1134 const std::map<std::string, Record*> &getDefs() const { return Defs; }
1136 Record *getClass(const std::string &Name) const {
1137 std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
1138 return I == Classes.end() ? 0 : I->second;
1140 Record *getDef(const std::string &Name) const {
1141 std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
1142 return I == Defs.end() ? 0 : I->second;
1144 void addClass(Record *R) {
1145 assert(getClass(R->getName()) == 0 && "Class already exists!");
1146 Classes.insert(std::make_pair(R->getName(), R));
1148 void addDef(Record *R) {
1149 assert(getDef(R->getName()) == 0 && "Def already exists!");
1150 Defs.insert(std::make_pair(R->getName(), R));
1153 /// removeClass - Remove, but do not delete, the specified record.
1155 void removeClass(const std::string &Name) {
1156 assert(Classes.count(Name) && "Class does not exist!");
1157 Classes.erase(Name);
1159 /// removeDef - Remove, but do not delete, the specified record.
1161 void removeDef(const std::string &Name) {
1162 assert(Defs.count(Name) && "Def does not exist!");
1166 //===--------------------------------------------------------------------===//
1167 // High-level helper methods, useful for tablegen backends...
1169 /// getAllDerivedDefinitions - This method returns all concrete definitions
1170 /// that derive from the specified class name. If a class with the specified
1171 /// name does not exist, an exception is thrown.
1172 std::vector<Record*>
1173 getAllDerivedDefinitions(const std::string &ClassName) const;
1179 /// LessRecord - Sorting predicate to sort record pointers by name.
1182 bool operator()(const Record *Rec1, const Record *Rec2) const {
1183 return Rec1->getName() < Rec2->getName();
1187 /// LessRecordFieldName - Sorting predicate to sort record pointers by their
1190 struct LessRecordFieldName {
1191 bool operator()(const Record *Rec1, const Record *Rec2) const {
1192 return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name");
1197 std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK);
1199 extern RecordKeeper Records;
1201 } // End llvm namespace