2 //===- Record.h - Classes to represent Table Records ------------*- C++ -*-===//
4 // The LLVM Compiler Infrastructure
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 // This file defines the main TableGen data structures, including the TableGen
12 // types, values, and high-level data structures.
14 //===----------------------------------------------------------------------===//
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/FoldingSet.h"
21 #include "llvm/Support/Allocator.h"
22 #include "llvm/Support/SourceMgr.h"
23 #include "llvm/Support/DataTypes.h"
24 #include "llvm/Support/raw_ostream.h"
58 class VarListElementInit;
66 //===----------------------------------------------------------------------===//
68 //===----------------------------------------------------------------------===//
73 RecTy() : ListTy(0) {}
76 virtual std::string getAsString() const = 0;
77 void print(raw_ostream &OS) const { OS << getAsString(); }
80 /// typeIsConvertibleTo - Return true if all values of 'this' type can be
81 /// converted to the specified type.
82 virtual bool typeIsConvertibleTo(const RecTy *RHS) const = 0;
84 /// getListTy - Returns the type representing list<this>.
85 ListRecTy *getListTy();
87 public: // These methods should only be called from subclasses of Init
88 virtual Init *convertValue( UnsetInit *UI) { return 0; }
89 virtual Init *convertValue( BitInit *BI) { return 0; }
90 virtual Init *convertValue( BitsInit *BI) { return 0; }
91 virtual Init *convertValue( IntInit *II) { return 0; }
92 virtual Init *convertValue(StringInit *SI) { return 0; }
93 virtual Init *convertValue( ListInit *LI) { return 0; }
94 virtual Init *convertValue( UnOpInit *UI) {
95 return convertValue((TypedInit*)UI);
97 virtual Init *convertValue( BinOpInit *UI) {
98 return convertValue((TypedInit*)UI);
100 virtual Init *convertValue( TernOpInit *UI) {
101 return convertValue((TypedInit*)UI);
103 virtual Init *convertValue( CodeInit *CI) { return 0; }
104 virtual Init *convertValue(VarBitInit *VB) { return 0; }
105 virtual Init *convertValue( DefInit *DI) { return 0; }
106 virtual Init *convertValue( DagInit *DI) { return 0; }
107 virtual Init *convertValue( TypedInit *TI) { return 0; }
108 virtual Init *convertValue( VarInit *VI) {
109 return convertValue((TypedInit*)VI);
111 virtual Init *convertValue( FieldInit *FI) {
112 return convertValue((TypedInit*)FI);
115 public: // These methods should only be called by subclasses of RecTy.
116 // baseClassOf - These virtual methods should be overloaded to return true iff
117 // all values of type 'RHS' can be converted to the 'this' type.
118 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
119 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
120 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
121 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
122 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
123 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
124 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
125 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
128 inline raw_ostream &operator<<(raw_ostream &OS, const RecTy &Ty) {
134 /// BitRecTy - 'bit' - Represent a single bit
136 class BitRecTy : public RecTy {
137 static BitRecTy Shared;
140 static BitRecTy *get() { return &Shared; }
142 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
143 virtual Init *convertValue( BitInit *BI) { return (Init*)BI; }
144 virtual Init *convertValue( BitsInit *BI);
145 virtual Init *convertValue( IntInit *II);
146 virtual Init *convertValue(StringInit *SI) { return 0; }
147 virtual Init *convertValue( ListInit *LI) { return 0; }
148 virtual Init *convertValue( CodeInit *CI) { return 0; }
149 virtual Init *convertValue(VarBitInit *VB) { return (Init*)VB; }
150 virtual Init *convertValue( DefInit *DI) { return 0; }
151 virtual Init *convertValue( DagInit *DI) { return 0; }
152 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
153 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
154 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
155 virtual Init *convertValue( TypedInit *TI);
156 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
157 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
159 std::string getAsString() const { return "bit"; }
161 bool typeIsConvertibleTo(const RecTy *RHS) const {
162 return RHS->baseClassOf(this);
164 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
165 virtual bool baseClassOf(const BitsRecTy *RHS) const;
166 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
167 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
168 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
169 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
170 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
171 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
176 // BitsRecTy - 'bits<n>' - Represent a fixed number of bits
177 /// BitsRecTy - 'bits<n>' - Represent a fixed number of bits
179 class BitsRecTy : public RecTy {
181 explicit BitsRecTy(unsigned Sz) : Size(Sz) {}
183 static BitsRecTy *get(unsigned Sz);
185 unsigned getNumBits() const { return Size; }
187 virtual Init *convertValue( UnsetInit *UI);
188 virtual Init *convertValue( BitInit *UI);
189 virtual Init *convertValue( BitsInit *BI);
190 virtual Init *convertValue( IntInit *II);
191 virtual Init *convertValue(StringInit *SI) { return 0; }
192 virtual Init *convertValue( ListInit *LI) { return 0; }
193 virtual Init *convertValue( CodeInit *CI) { return 0; }
194 virtual Init *convertValue(VarBitInit *VB) { return 0; }
195 virtual Init *convertValue( DefInit *DI) { return 0; }
196 virtual Init *convertValue( DagInit *DI) { return 0; }
197 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
198 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
199 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
200 virtual Init *convertValue( TypedInit *TI);
201 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
202 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
204 std::string getAsString() const;
206 bool typeIsConvertibleTo(const RecTy *RHS) const {
207 return RHS->baseClassOf(this);
209 virtual bool baseClassOf(const BitRecTy *RHS) const { return Size == 1; }
210 virtual bool baseClassOf(const BitsRecTy *RHS) const {
211 return RHS->Size == Size;
213 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
214 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
215 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
216 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
217 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
218 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
223 /// IntRecTy - 'int' - Represent an integer value of no particular size
225 class IntRecTy : public RecTy {
226 static IntRecTy Shared;
229 static IntRecTy *get() { return &Shared; }
231 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
232 virtual Init *convertValue( BitInit *BI);
233 virtual Init *convertValue( BitsInit *BI);
234 virtual Init *convertValue( IntInit *II) { return (Init*)II; }
235 virtual Init *convertValue(StringInit *SI) { return 0; }
236 virtual Init *convertValue( ListInit *LI) { return 0; }
237 virtual Init *convertValue( CodeInit *CI) { return 0; }
238 virtual Init *convertValue(VarBitInit *VB) { return 0; }
239 virtual Init *convertValue( DefInit *DI) { return 0; }
240 virtual Init *convertValue( DagInit *DI) { return 0; }
241 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
242 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
243 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
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 "int"; }
250 bool typeIsConvertibleTo(const RecTy *RHS) const {
251 return RHS->baseClassOf(this);
254 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
255 virtual bool baseClassOf(const BitsRecTy *RHS) const { return true; }
256 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
257 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
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; }
265 /// StringRecTy - 'string' - Represent an string value
267 class StringRecTy : public RecTy {
268 static StringRecTy Shared;
271 static StringRecTy *get() { return &Shared; }
273 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
274 virtual Init *convertValue( BitInit *BI) { return 0; }
275 virtual Init *convertValue( BitsInit *BI) { return 0; }
276 virtual Init *convertValue( IntInit *II) { return 0; }
277 virtual Init *convertValue(StringInit *SI) { return (Init*)SI; }
278 virtual Init *convertValue( ListInit *LI) { return 0; }
279 virtual Init *convertValue( UnOpInit *BO);
280 virtual Init *convertValue( BinOpInit *BO);
281 virtual Init *convertValue( TernOpInit *BO) { return RecTy::convertValue(BO);}
283 virtual Init *convertValue( CodeInit *CI) { return 0; }
284 virtual Init *convertValue(VarBitInit *VB) { return 0; }
285 virtual Init *convertValue( DefInit *DI) { return 0; }
286 virtual Init *convertValue( DagInit *DI) { 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 { return "string"; }
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 true; }
301 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
302 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
303 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
304 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
307 // ListRecTy - 'list<Ty>' - Represent a list of values, all of which must be of
308 // the specified type.
309 /// ListRecTy - 'list<Ty>' - Represent a list of values, all of which must
310 /// be of the specified type.
312 class ListRecTy : public RecTy {
314 explicit ListRecTy(RecTy *T) : Ty(T) {}
315 friend ListRecTy *RecTy::getListTy();
317 static ListRecTy *get(RecTy *T) { return T->getListTy(); }
318 RecTy *getElementType() const { return Ty; }
320 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
321 virtual Init *convertValue( BitInit *BI) { return 0; }
322 virtual Init *convertValue( BitsInit *BI) { return 0; }
323 virtual Init *convertValue( IntInit *II) { return 0; }
324 virtual Init *convertValue(StringInit *SI) { return 0; }
325 virtual Init *convertValue( ListInit *LI);
326 virtual Init *convertValue( CodeInit *CI) { return 0; }
327 virtual Init *convertValue(VarBitInit *VB) { return 0; }
328 virtual Init *convertValue( DefInit *DI) { return 0; }
329 virtual Init *convertValue( DagInit *DI) { return 0; }
330 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
331 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
332 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
333 virtual Init *convertValue( TypedInit *TI);
334 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
335 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
337 std::string getAsString() const;
339 bool typeIsConvertibleTo(const RecTy *RHS) const {
340 return RHS->baseClassOf(this);
343 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
344 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
345 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
346 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
347 virtual bool baseClassOf(const ListRecTy *RHS) const {
348 return RHS->getElementType()->typeIsConvertibleTo(Ty);
350 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
351 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
352 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
355 /// CodeRecTy - 'code' - Represent an code fragment, function or method.
357 class CodeRecTy : public RecTy {
358 static CodeRecTy Shared;
361 static CodeRecTy *get() { return &Shared; }
363 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
364 virtual Init *convertValue( BitInit *BI) { return 0; }
365 virtual Init *convertValue( BitsInit *BI) { return 0; }
366 virtual Init *convertValue( IntInit *II) { return 0; }
367 virtual Init *convertValue(StringInit *SI) { return 0; }
368 virtual Init *convertValue( ListInit *LI) { return 0; }
369 virtual Init *convertValue( CodeInit *CI) { return (Init*)CI; }
370 virtual Init *convertValue(VarBitInit *VB) { return 0; }
371 virtual Init *convertValue( DefInit *DI) { return 0; }
372 virtual Init *convertValue( DagInit *DI) { return 0; }
373 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
374 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
375 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
376 virtual Init *convertValue( TypedInit *TI);
377 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
378 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
380 std::string getAsString() const { return "code"; }
382 bool typeIsConvertibleTo(const RecTy *RHS) const {
383 return RHS->baseClassOf(this);
385 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
386 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
387 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
388 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
389 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
390 virtual bool baseClassOf(const CodeRecTy *RHS) const { return true; }
391 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
392 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
395 /// DagRecTy - 'dag' - Represent a dag fragment
397 class DagRecTy : public RecTy {
398 static DagRecTy Shared;
401 static DagRecTy *get() { return &Shared; }
403 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
404 virtual Init *convertValue( BitInit *BI) { return 0; }
405 virtual Init *convertValue( BitsInit *BI) { return 0; }
406 virtual Init *convertValue( IntInit *II) { return 0; }
407 virtual Init *convertValue(StringInit *SI) { return 0; }
408 virtual Init *convertValue( ListInit *LI) { return 0; }
409 virtual Init *convertValue( CodeInit *CI) { return 0; }
410 virtual Init *convertValue(VarBitInit *VB) { return 0; }
411 virtual Init *convertValue( DefInit *DI) { return 0; }
412 virtual Init *convertValue( UnOpInit *BO);
413 virtual Init *convertValue( BinOpInit *BO);
414 virtual Init *convertValue( TernOpInit *BO) { return RecTy::convertValue(BO);}
415 virtual Init *convertValue( DagInit *CI) { return (Init*)CI; }
416 virtual Init *convertValue( TypedInit *TI);
417 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
418 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
420 std::string getAsString() const { return "dag"; }
422 bool typeIsConvertibleTo(const RecTy *RHS) const {
423 return RHS->baseClassOf(this);
426 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
427 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
428 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
429 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
430 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
431 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
432 virtual bool baseClassOf(const DagRecTy *RHS) const { return true; }
433 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
437 /// RecordRecTy - '[classname]' - Represent an instance of a class, such as:
440 class RecordRecTy : public RecTy {
442 explicit RecordRecTy(Record *R) : Rec(R) {}
445 static RecordRecTy *get(Record *R);
447 Record *getRecord() const { return Rec; }
449 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
450 virtual Init *convertValue( BitInit *BI) { return 0; }
451 virtual Init *convertValue( BitsInit *BI) { return 0; }
452 virtual Init *convertValue( IntInit *II) { return 0; }
453 virtual Init *convertValue(StringInit *SI) { return 0; }
454 virtual Init *convertValue( ListInit *LI) { return 0; }
455 virtual Init *convertValue( CodeInit *CI) { return 0; }
456 virtual Init *convertValue(VarBitInit *VB) { return 0; }
457 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
458 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
459 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
460 virtual Init *convertValue( DefInit *DI);
461 virtual Init *convertValue( DagInit *DI) { return 0; }
462 virtual Init *convertValue( TypedInit *VI);
463 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
464 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
466 std::string getAsString() const;
468 bool typeIsConvertibleTo(const RecTy *RHS) const {
469 return RHS->baseClassOf(this);
471 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
472 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
473 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
474 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
475 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
476 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
477 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
478 virtual bool baseClassOf(const RecordRecTy *RHS) const;
481 /// resolveTypes - Find a common type that T1 and T2 convert to.
482 /// Return 0 if no such type exists.
484 RecTy *resolveTypes(RecTy *T1, RecTy *T2);
486 //===----------------------------------------------------------------------===//
487 // Initializer Classes
488 //===----------------------------------------------------------------------===//
491 Init(const Init &); // Do not define.
492 Init &operator=(const Init &); // Do not define.
500 /// isComplete - This virtual method should be overridden by values that may
501 /// not be completely specified yet.
502 virtual bool isComplete() const { return true; }
504 /// print - Print out this value.
505 void print(raw_ostream &OS) const { OS << getAsString(); }
507 /// getAsString - Convert this value to a string form.
508 virtual std::string getAsString() const = 0;
510 /// dump - Debugging method that may be called through a debugger, just
511 /// invokes print on stderr.
514 /// convertInitializerTo - This virtual function is a simple call-back
515 /// function that should be overridden to call the appropriate
516 /// RecTy::convertValue method.
518 virtual Init *convertInitializerTo(RecTy *Ty) const = 0;
520 /// convertInitializerBitRange - This method is used to implement the bitrange
521 /// selection operator. Given an initializer, it selects the specified bits
522 /// out, returning them as a new init of bits type. If it is not legal to use
523 /// the bit subscript operator on this initializer, return null.
526 convertInitializerBitRange(const std::vector<unsigned> &Bits) const {
530 /// convertInitListSlice - This method is used to implement the list slice
531 /// selection operator. Given an initializer, it selects the specified list
532 /// elements, returning them as a new init of list type. If it is not legal
533 /// to take a slice of this, return null.
536 convertInitListSlice(const std::vector<unsigned> &Elements) const {
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 { return 0; }
546 /// getFieldInit - This method complements getFieldType to return the
547 /// initializer for the specified field. If getFieldType returns non-null
548 /// this method should return non-null, otherwise it returns null.
550 virtual Init *getFieldInit(Record &R, const RecordVal *RV,
551 const std::string &FieldName) const {
555 /// resolveReferences - This method is used by classes that refer to other
556 /// variables which may not be defined at the time the expression is formed.
557 /// If a value is set for the variable later, this method will be called on
558 /// users of the value to allow the value to propagate out.
560 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const {
561 return const_cast<Init *>(this);
565 inline raw_ostream &operator<<(raw_ostream &OS, const Init &I) {
566 I.print(OS); return OS;
569 /// TypedInit - This is the common super-class of types that have a specific,
572 class TypedInit : public Init {
575 TypedInit(const TypedInit &Other); // Do not define.
576 TypedInit &operator=(const TypedInit &Other); // Do not define.
579 explicit TypedInit(RecTy *T) : Ty(T) {}
582 RecTy *getType() const { return Ty; }
585 convertInitializerBitRange(const std::vector<unsigned> &Bits) const;
587 convertInitListSlice(const std::vector<unsigned> &Elements) const;
589 /// getFieldType - This method is used to implement the FieldInit class.
590 /// Implementors of this method should return the type of the named field if
591 /// they are of record type.
593 virtual RecTy *getFieldType(const std::string &FieldName) const;
595 /// resolveBitReference - This method is used to implement
596 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
597 /// simply return the resolved value, otherwise we return null.
599 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
600 unsigned Bit) const = 0;
602 /// resolveListElementReference - This method is used to implement
603 /// VarListElementInit::resolveReferences. If the list element is resolvable
604 /// now, we return the resolved value, otherwise we return null.
605 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
606 unsigned Elt) const = 0;
610 /// UnsetInit - ? - Represents an uninitialized value
612 class UnsetInit : public Init {
613 UnsetInit() : Init() {}
614 UnsetInit(const UnsetInit &); // Do not define.
615 UnsetInit &operator=(const UnsetInit &Other); // Do not define.
618 static UnsetInit *get();
620 virtual Init *convertInitializerTo(RecTy *Ty) const {
621 return Ty->convertValue(const_cast<UnsetInit *>(this));
624 virtual bool isComplete() const { return false; }
625 virtual std::string getAsString() const { return "?"; }
629 /// BitInit - true/false - Represent a concrete initializer for a bit.
631 class BitInit : public Init {
634 explicit BitInit(bool V) : Value(V) {}
635 BitInit(const BitInit &Other); // Do not define.
636 BitInit &operator=(BitInit &Other); // Do not define.
639 static BitInit *get(bool V);
641 bool getValue() const { return Value; }
643 virtual Init *convertInitializerTo(RecTy *Ty) const {
644 return Ty->convertValue(const_cast<BitInit *>(this));
647 virtual std::string getAsString() const { return Value ? "1" : "0"; }
650 /// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
651 /// It contains a vector of bits, whose size is determined by the type.
653 class BitsInit : public Init, public FoldingSetNode {
654 std::vector<Init*> Bits;
656 BitsInit(ArrayRef<Init *> Range) : Bits(Range.begin(), Range.end()) {}
658 BitsInit(const BitsInit &Other); // Do not define.
659 BitsInit &operator=(const BitsInit &Other); // Do not define.
662 static BitsInit *get(ArrayRef<Init *> Range);
664 void Profile(FoldingSetNodeID &ID) const;
666 unsigned getNumBits() const { return Bits.size(); }
668 Init *getBit(unsigned Bit) const {
669 assert(Bit < Bits.size() && "Bit index out of range!");
673 virtual Init *convertInitializerTo(RecTy *Ty) const {
674 return Ty->convertValue(const_cast<BitsInit *>(this));
677 convertInitializerBitRange(const std::vector<unsigned> &Bits) const;
679 virtual bool isComplete() const {
680 for (unsigned i = 0; i != getNumBits(); ++i)
681 if (!getBit(i)->isComplete()) return false;
684 bool allInComplete() const {
685 for (unsigned i = 0; i != getNumBits(); ++i)
686 if (getBit(i)->isComplete()) return false;
689 virtual std::string getAsString() const;
691 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
695 /// IntInit - 7 - Represent an initalization by a literal integer value.
697 class IntInit : public TypedInit {
700 explicit IntInit(int64_t V) : TypedInit(IntRecTy::get()), Value(V) {}
702 IntInit(const IntInit &Other); // Do not define.
703 IntInit &operator=(const IntInit &Other); // Do note define.
706 static IntInit *get(int64_t V);
708 int64_t getValue() const { return Value; }
710 virtual Init *convertInitializerTo(RecTy *Ty) const {
711 return Ty->convertValue(const_cast<IntInit *>(this));
714 convertInitializerBitRange(const std::vector<unsigned> &Bits) const;
716 virtual std::string getAsString() const;
718 /// resolveBitReference - This method is used to implement
719 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
720 /// simply return the resolved value, otherwise we return null.
722 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
723 unsigned Bit) const {
724 assert(0 && "Illegal bit reference off int");
728 /// resolveListElementReference - This method is used to implement
729 /// VarListElementInit::resolveReferences. If the list element is resolvable
730 /// now, we return the resolved value, otherwise we return null.
731 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
732 unsigned Elt) const {
733 assert(0 && "Illegal element reference off int");
739 /// StringInit - "foo" - Represent an initialization by a string value.
741 class StringInit : public TypedInit {
744 explicit StringInit(const std::string &V)
745 : TypedInit(StringRecTy::get()), Value(V) {}
747 StringInit(const StringInit &Other); // Do not define.
748 StringInit &operator=(const StringInit &Other); // Do not define.
751 static StringInit *get(const std::string &V);
753 const std::string &getValue() const { return Value; }
755 virtual Init *convertInitializerTo(RecTy *Ty) const {
756 return Ty->convertValue(const_cast<StringInit *>(this));
759 virtual std::string getAsString() const { return "\"" + Value + "\""; }
761 /// resolveBitReference - This method is used to implement
762 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
763 /// simply return the resolved value, otherwise we return null.
765 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
766 unsigned Bit) const {
767 assert(0 && "Illegal bit reference off string");
771 /// resolveListElementReference - This method is used to implement
772 /// VarListElementInit::resolveReferences. If the list element is resolvable
773 /// now, we return the resolved value, otherwise we return null.
774 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
775 unsigned Elt) const {
776 assert(0 && "Illegal element reference off string");
781 /// CodeInit - "[{...}]" - Represent a code fragment.
783 class CodeInit : public Init {
786 explicit CodeInit(const std::string &V) : Value(V) {}
788 CodeInit(const CodeInit &Other); // Do not define.
789 CodeInit &operator=(const CodeInit &Other); // Do not define.
792 static CodeInit *get(const std::string &V);
794 const std::string &getValue() const { return Value; }
796 virtual Init *convertInitializerTo(RecTy *Ty) const {
797 return Ty->convertValue(const_cast<CodeInit *>(this));
800 virtual std::string getAsString() const { return "[{" + Value + "}]"; }
803 /// ListInit - [AL, AH, CL] - Represent a list of defs
805 class ListInit : public TypedInit, public FoldingSetNode {
806 std::vector<Init*> Values;
808 typedef std::vector<Init*>::const_iterator const_iterator;
811 explicit ListInit(ArrayRef<Init *> Range, RecTy *EltTy)
812 : TypedInit(ListRecTy::get(EltTy)), Values(Range.begin(), Range.end()) {}
814 ListInit(const ListInit &Other); // Do not define.
815 ListInit &operator=(const ListInit &Other); // Do not define.
818 static ListInit *get(ArrayRef<Init *> Range, RecTy *EltTy);
820 void Profile(FoldingSetNodeID &ID) const;
822 unsigned getSize() const { return Values.size(); }
823 Init *getElement(unsigned i) const {
824 assert(i < Values.size() && "List element index out of range!");
828 Record *getElementAsRecord(unsigned i) const;
830 Init *convertInitListSlice(const std::vector<unsigned> &Elements) const;
832 virtual Init *convertInitializerTo(RecTy *Ty) const {
833 return Ty->convertValue(const_cast<ListInit *>(this));
836 /// resolveReferences - This method is used by classes that refer to other
837 /// variables which may not be defined at the time they expression is formed.
838 /// If a value is set for the variable later, this method will be called on
839 /// users of the value to allow the value to propagate out.
841 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
843 virtual std::string getAsString() const;
845 ArrayRef<Init*> getValues() const { return Values; }
847 inline const_iterator begin() const { return Values.begin(); }
848 inline const_iterator end () const { return Values.end(); }
850 inline size_t size () const { return Values.size(); }
851 inline bool empty() const { return Values.empty(); }
853 /// resolveBitReference - This method is used to implement
854 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
855 /// simply return the resolved value, otherwise we return null.
857 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
858 unsigned Bit) const {
859 assert(0 && "Illegal bit reference off list");
863 /// resolveListElementReference - This method is used to implement
864 /// VarListElementInit::resolveReferences. If the list element is resolvable
865 /// now, we return the resolved value, otherwise we return null.
866 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
871 /// OpInit - Base class for operators
873 class OpInit : public TypedInit {
874 OpInit(const OpInit &Other); // Do not define.
875 OpInit &operator=(OpInit &Other); // Do not define.
878 explicit OpInit(RecTy *Type) : TypedInit(Type) {}
881 // Clone - Clone this operator, replacing arguments with the new list
882 virtual OpInit *clone(std::vector<Init *> &Operands) const = 0;
884 virtual int getNumOperands() const = 0;
885 virtual Init *getOperand(int i) const = 0;
887 // Fold - If possible, fold this to a simpler init. Return this if not
889 virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const = 0;
891 virtual Init *convertInitializerTo(RecTy *Ty) const {
892 return Ty->convertValue(const_cast<OpInit *>(this));
895 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
897 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
902 /// UnOpInit - !op (X) - Transform an init.
904 class UnOpInit : public OpInit {
906 enum UnaryOp { CAST, HEAD, TAIL, EMPTY };
911 UnOpInit(UnaryOp opc, Init *lhs, RecTy *Type)
912 : OpInit(Type), Opc(opc), LHS(lhs) {}
914 UnOpInit(const UnOpInit &Other); // Do not define.
915 UnOpInit &operator=(const UnOpInit &Other); // Do not define.
918 static UnOpInit *get(UnaryOp opc, Init *lhs, RecTy *Type);
920 // Clone - Clone this operator, replacing arguments with the new list
921 virtual OpInit *clone(std::vector<Init *> &Operands) const {
922 assert(Operands.size() == 1 &&
923 "Wrong number of operands for unary operation");
924 return UnOpInit::get(getOpcode(), *Operands.begin(), getType());
927 int getNumOperands() const { return 1; }
928 Init *getOperand(int i) const {
929 assert(i == 0 && "Invalid operand id for unary operator");
933 UnaryOp getOpcode() const { return Opc; }
934 Init *getOperand() const { return LHS; }
936 // Fold - If possible, fold this to a simpler init. Return this if not
938 Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const;
940 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
942 virtual std::string getAsString() const;
945 /// BinOpInit - !op (X, Y) - Combine two inits.
947 class BinOpInit : public OpInit {
949 enum BinaryOp { SHL, SRA, SRL, STRCONCAT, CONCAT, EQ };
954 BinOpInit(BinaryOp opc, Init *lhs, Init *rhs, RecTy *Type) :
955 OpInit(Type), Opc(opc), LHS(lhs), RHS(rhs) {}
957 BinOpInit(const BinOpInit &Other); // Do not define.
958 BinOpInit &operator=(const BinOpInit &Other); // Do not define.
961 static BinOpInit *get(BinaryOp opc, Init *lhs, Init *rhs,
964 // Clone - Clone this operator, replacing arguments with the new list
965 virtual OpInit *clone(std::vector<Init *> &Operands) const {
966 assert(Operands.size() == 2 &&
967 "Wrong number of operands for binary operation");
968 return BinOpInit::get(getOpcode(), Operands[0], Operands[1], getType());
971 int getNumOperands() const { return 2; }
972 Init *getOperand(int i) const {
973 assert((i == 0 || i == 1) && "Invalid operand id for binary operator");
981 BinaryOp getOpcode() const { return Opc; }
982 Init *getLHS() const { return LHS; }
983 Init *getRHS() const { return RHS; }
985 // Fold - If possible, fold this to a simpler init. Return this if not
987 Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const;
989 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
991 virtual std::string getAsString() const;
994 /// TernOpInit - !op (X, Y, Z) - Combine two inits.
996 class TernOpInit : public OpInit {
998 enum TernaryOp { SUBST, FOREACH, IF };
1001 Init *LHS, *MHS, *RHS;
1003 TernOpInit(TernaryOp opc, Init *lhs, Init *mhs, Init *rhs,
1005 OpInit(Type), Opc(opc), LHS(lhs), MHS(mhs), RHS(rhs) {}
1007 TernOpInit(const TernOpInit &Other); // Do not define.
1008 TernOpInit &operator=(const TernOpInit &Other); // Do not define.
1011 static TernOpInit *get(TernaryOp opc, Init *lhs,
1012 Init *mhs, Init *rhs,
1015 // Clone - Clone this operator, replacing arguments with the new list
1016 virtual OpInit *clone(std::vector<Init *> &Operands) const {
1017 assert(Operands.size() == 3 &&
1018 "Wrong number of operands for ternary operation");
1019 return TernOpInit::get(getOpcode(), Operands[0], Operands[1], Operands[2],
1023 int getNumOperands() const { return 3; }
1024 Init *getOperand(int i) const {
1025 assert((i == 0 || i == 1 || i == 2) &&
1026 "Invalid operand id for ternary operator");
1029 } else if (i == 1) {
1036 TernaryOp getOpcode() const { return Opc; }
1037 Init *getLHS() const { return LHS; }
1038 Init *getMHS() const { return MHS; }
1039 Init *getRHS() const { return RHS; }
1041 // Fold - If possible, fold this to a simpler init. Return this if not
1042 // possible to fold.
1043 Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const;
1045 virtual bool isComplete() const { return false; }
1047 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
1049 virtual std::string getAsString() const;
1053 /// VarInit - 'Opcode' - Represent a reference to an entire variable object.
1055 class VarInit : public TypedInit {
1056 std::string VarName;
1058 explicit VarInit(const std::string &VN, RecTy *T)
1059 : TypedInit(T), VarName(VN) {}
1061 VarInit(const VarInit &Other); // Do not define.
1062 VarInit &operator=(const VarInit &Other); // Do not define.
1065 static VarInit *get(const std::string &VN, RecTy *T);
1066 static VarInit *get(Init *VN, RecTy *T);
1068 virtual Init *convertInitializerTo(RecTy *Ty) const {
1069 return Ty->convertValue(const_cast<VarInit *>(this));
1072 const std::string &getName() const { return VarName; }
1074 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
1075 unsigned Bit) const;
1076 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
1077 unsigned Elt) const;
1079 virtual RecTy *getFieldType(const std::string &FieldName) const;
1080 virtual Init *getFieldInit(Record &R, const RecordVal *RV,
1081 const std::string &FieldName) const;
1083 /// resolveReferences - This method is used by classes that refer to other
1084 /// variables which may not be defined at the time they expression is formed.
1085 /// If a value is set for the variable later, this method will be called on
1086 /// users of the value to allow the value to propagate out.
1088 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
1090 virtual std::string getAsString() const { return VarName; }
1094 /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
1096 class VarBitInit : public Init {
1100 VarBitInit(TypedInit *T, unsigned B) : TI(T), Bit(B) {
1101 assert(T->getType() && dynamic_cast<BitsRecTy*>(T->getType()) &&
1102 ((BitsRecTy*)T->getType())->getNumBits() > B &&
1103 "Illegal VarBitInit expression!");
1106 VarBitInit(const VarBitInit &Other); // Do not define.
1107 VarBitInit &operator=(const VarBitInit &Other); // Do not define.
1110 static VarBitInit *get(TypedInit *T, unsigned B);
1112 virtual Init *convertInitializerTo(RecTy *Ty) const {
1113 return Ty->convertValue(const_cast<VarBitInit *>(this));
1116 TypedInit *getVariable() const { return TI; }
1117 unsigned getBitNum() const { return Bit; }
1119 virtual std::string getAsString() const;
1120 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
1123 /// VarListElementInit - List[4] - Represent access to one element of a var or
1125 class VarListElementInit : public TypedInit {
1129 VarListElementInit(TypedInit *T, unsigned E)
1130 : TypedInit(dynamic_cast<ListRecTy*>(T->getType())->getElementType()),
1132 assert(T->getType() && dynamic_cast<ListRecTy*>(T->getType()) &&
1133 "Illegal VarBitInit expression!");
1136 VarListElementInit(const VarListElementInit &Other); // Do not define.
1137 VarListElementInit &operator=(const VarListElementInit &Other); // Do
1142 static VarListElementInit *get(TypedInit *T, unsigned E);
1144 virtual Init *convertInitializerTo(RecTy *Ty) const {
1145 return Ty->convertValue(const_cast<VarListElementInit *>(this));
1148 TypedInit *getVariable() const { return TI; }
1149 unsigned getElementNum() const { return Element; }
1151 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
1152 unsigned Bit) const;
1154 /// resolveListElementReference - This method is used to implement
1155 /// VarListElementInit::resolveReferences. If the list element is resolvable
1156 /// now, we return the resolved value, otherwise we return null.
1157 virtual Init *resolveListElementReference(Record &R,
1158 const RecordVal *RV,
1159 unsigned Elt) const;
1161 virtual std::string getAsString() const;
1162 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
1165 /// DefInit - AL - Represent a reference to a 'def' in the description
1167 class DefInit : public TypedInit {
1170 DefInit(Record *D, RecordRecTy *T) : TypedInit(T), Def(D) {}
1171 friend class Record;
1173 DefInit(const DefInit &Other); // Do not define.
1174 DefInit &operator=(const DefInit &Other); // Do not define.
1177 static DefInit *get(Record*);
1179 virtual Init *convertInitializerTo(RecTy *Ty) const {
1180 return Ty->convertValue(const_cast<DefInit *>(this));
1183 Record *getDef() const { return Def; }
1185 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
1187 virtual RecTy *getFieldType(const std::string &FieldName) const;
1188 virtual Init *getFieldInit(Record &R, const RecordVal *RV,
1189 const std::string &FieldName) const;
1191 virtual std::string getAsString() const;
1193 /// resolveBitReference - This method is used to implement
1194 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
1195 /// simply return the resolved value, otherwise we return null.
1197 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
1198 unsigned Bit) const {
1199 assert(0 && "Illegal bit reference off def");
1203 /// resolveListElementReference - This method is used to implement
1204 /// VarListElementInit::resolveReferences. If the list element is resolvable
1205 /// now, we return the resolved value, otherwise we return null.
1206 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
1207 unsigned Elt) const {
1208 assert(0 && "Illegal element reference off def");
1214 /// FieldInit - X.Y - Represent a reference to a subfield of a variable
1216 class FieldInit : public TypedInit {
1217 Init *Rec; // Record we are referring to
1218 std::string FieldName; // Field we are accessing
1220 FieldInit(Init *R, const std::string &FN)
1221 : TypedInit(R->getFieldType(FN)), Rec(R), FieldName(FN) {
1222 assert(getType() && "FieldInit with non-record type!");
1225 FieldInit(const FieldInit &Other); // Do not define.
1226 FieldInit &operator=(const FieldInit &Other); // Do not define.
1229 static FieldInit *get(Init *R, const std::string &FN);
1230 static FieldInit *get(Init *R, const Init *FN);
1232 virtual Init *convertInitializerTo(RecTy *Ty) const {
1233 return Ty->convertValue(const_cast<FieldInit *>(this));
1236 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
1237 unsigned Bit) const;
1238 virtual Init *resolveListElementReference(Record &R,
1239 const RecordVal *RV,
1240 unsigned Elt) const;
1242 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
1244 virtual std::string getAsString() const {
1245 return Rec->getAsString() + "." + FieldName;
1249 /// DagInit - (v a, b) - Represent a DAG tree value. DAG inits are required
1250 /// to have at least one value then a (possibly empty) list of arguments. Each
1251 /// argument can have a name associated with it.
1253 class DagInit : public TypedInit, public FoldingSetNode {
1255 std::string ValName;
1256 std::vector<Init*> Args;
1257 std::vector<std::string> ArgNames;
1259 DagInit(Init *V, const std::string &VN,
1260 ArrayRef<Init *> ArgRange,
1261 ArrayRef<std::string> NameRange)
1262 : TypedInit(DagRecTy::get()), Val(V), ValName(VN),
1263 Args(ArgRange.begin(), ArgRange.end()),
1264 ArgNames(NameRange.begin(), NameRange.end()) {}
1266 DagInit(const DagInit &Other); // Do not define.
1267 DagInit &operator=(const DagInit &Other); // Do not define.
1270 static DagInit *get(Init *V, const std::string &VN,
1271 ArrayRef<Init *> ArgRange,
1272 ArrayRef<std::string> NameRange);
1273 static DagInit *get(Init *V, const std::string &VN,
1275 std::pair<Init*, std::string> > &args);
1277 void Profile(FoldingSetNodeID &ID) const;
1279 virtual Init *convertInitializerTo(RecTy *Ty) const {
1280 return Ty->convertValue(const_cast<DagInit *>(this));
1283 Init *getOperator() const { return Val; }
1285 const std::string &getName() const { return ValName; }
1287 unsigned getNumArgs() const { return Args.size(); }
1288 Init *getArg(unsigned Num) const {
1289 assert(Num < Args.size() && "Arg number out of range!");
1292 const std::string &getArgName(unsigned Num) const {
1293 assert(Num < ArgNames.size() && "Arg number out of range!");
1294 return ArgNames[Num];
1297 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
1299 virtual std::string getAsString() const;
1301 typedef std::vector<Init*>::const_iterator const_arg_iterator;
1302 typedef std::vector<std::string>::const_iterator const_name_iterator;
1304 inline const_arg_iterator arg_begin() const { return Args.begin(); }
1305 inline const_arg_iterator arg_end () const { return Args.end(); }
1307 inline size_t arg_size () const { return Args.size(); }
1308 inline bool arg_empty() const { return Args.empty(); }
1310 inline const_name_iterator name_begin() const { return ArgNames.begin(); }
1311 inline const_name_iterator name_end () const { return ArgNames.end(); }
1313 inline size_t name_size () const { return ArgNames.size(); }
1314 inline bool name_empty() const { return ArgNames.empty(); }
1316 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
1317 unsigned Bit) const {
1318 assert(0 && "Illegal bit reference off dag");
1322 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
1323 unsigned Elt) const {
1324 assert(0 && "Illegal element reference off dag");
1329 //===----------------------------------------------------------------------===//
1330 // High-Level Classes
1331 //===----------------------------------------------------------------------===//
1339 RecordVal(const std::string &N, RecTy *T, unsigned P);
1341 const std::string &getName() const { return Name; }
1343 unsigned getPrefix() const { return Prefix; }
1344 RecTy *getType() const { return Ty; }
1345 Init *getValue() const { return Value; }
1347 bool setValue(Init *V) {
1349 Value = V->convertInitializerTo(Ty);
1357 void print(raw_ostream &OS, bool PrintSem = true) const;
1360 inline raw_ostream &operator<<(raw_ostream &OS, const RecordVal &RV) {
1361 RV.print(OS << " ");
1366 static unsigned LastID;
1368 // Unique record ID.
1372 std::vector<std::string> TemplateArgs;
1373 std::vector<RecordVal> Values;
1374 std::vector<Record*> SuperClasses;
1376 // Tracks Record instances. Not owned by Record.
1377 RecordKeeper &TrackedRecords;
1383 // Constructs a record.
1384 explicit Record(const std::string &N, SMLoc loc, RecordKeeper &records) :
1385 ID(LastID++), Name(N), Loc(loc), TrackedRecords(records), TheInit(0) {}
1389 static unsigned getNewUID() { return LastID++; }
1392 unsigned getID() const { return ID; }
1394 const std::string &getName() const { return Name; }
1395 void setName(const std::string &Name); // Also updates RecordKeeper.
1397 SMLoc getLoc() const { return Loc; }
1399 /// get the corresponding DefInit.
1400 DefInit *getDefInit();
1402 const std::vector<std::string> &getTemplateArgs() const {
1403 return TemplateArgs;
1405 const std::vector<RecordVal> &getValues() const { return Values; }
1406 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; }
1408 bool isTemplateArg(StringRef Name) const {
1409 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
1410 if (TemplateArgs[i] == Name) return true;
1414 const RecordVal *getValue(StringRef Name) const {
1415 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1416 if (Values[i].getName() == Name) return &Values[i];
1419 RecordVal *getValue(StringRef Name) {
1420 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1421 if (Values[i].getName() == Name) return &Values[i];
1425 void addTemplateArg(StringRef Name) {
1426 assert(!isTemplateArg(Name) && "Template arg already defined!");
1427 TemplateArgs.push_back(Name);
1430 void addValue(const RecordVal &RV) {
1431 assert(getValue(RV.getName()) == 0 && "Value already added!");
1432 Values.push_back(RV);
1435 void removeValue(StringRef Name) {
1436 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1437 if (Values[i].getName() == Name) {
1438 Values.erase(Values.begin()+i);
1441 assert(0 && "Cannot remove an entry that does not exist!");
1444 bool isSubClassOf(const Record *R) const {
1445 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1446 if (SuperClasses[i] == R)
1451 bool isSubClassOf(StringRef Name) const {
1452 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1453 if (SuperClasses[i]->getName() == Name)
1458 void addSuperClass(Record *R) {
1459 assert(!isSubClassOf(R) && "Already subclassing record!");
1460 SuperClasses.push_back(R);
1463 /// resolveReferences - If there are any field references that refer to fields
1464 /// that have been filled in, we can propagate the values now.
1466 void resolveReferences() { resolveReferencesTo(0); }
1468 /// resolveReferencesTo - If anything in this record refers to RV, replace the
1469 /// reference to RV with the RHS of RV. If RV is null, we resolve all
1470 /// possible references.
1471 void resolveReferencesTo(const RecordVal *RV);
1473 RecordKeeper &getRecords() const {
1474 return TrackedRecords;
1479 //===--------------------------------------------------------------------===//
1480 // High-level methods useful to tablegen back-ends
1483 /// getValueInit - Return the initializer for a value with the specified name,
1484 /// or throw an exception if the field does not exist.
1486 Init *getValueInit(StringRef FieldName) const;
1488 /// getValueAsString - This method looks up the specified field and returns
1489 /// its value as a string, throwing an exception if the field does not exist
1490 /// or if the value is not a string.
1492 std::string getValueAsString(StringRef FieldName) const;
1494 /// getValueAsBitsInit - This method looks up the specified field and returns
1495 /// its value as a BitsInit, throwing an exception if the field does not exist
1496 /// or if the value is not the right type.
1498 BitsInit *getValueAsBitsInit(StringRef FieldName) const;
1500 /// getValueAsListInit - This method looks up the specified field and returns
1501 /// its value as a ListInit, throwing an exception if the field does not exist
1502 /// or if the value is not the right type.
1504 ListInit *getValueAsListInit(StringRef FieldName) const;
1506 /// getValueAsListOfDefs - This method looks up the specified field and
1507 /// returns its value as a vector of records, throwing an exception if the
1508 /// field does not exist or if the value is not the right type.
1510 std::vector<Record*> getValueAsListOfDefs(StringRef FieldName) const;
1512 /// getValueAsListOfInts - This method looks up the specified field and
1513 /// returns its value as a vector of integers, throwing an exception if the
1514 /// field does not exist or if the value is not the right type.
1516 std::vector<int64_t> getValueAsListOfInts(StringRef FieldName) const;
1518 /// getValueAsListOfStrings - This method looks up the specified field and
1519 /// returns its value as a vector of strings, throwing an exception if the
1520 /// field does not exist or if the value is not the right type.
1522 std::vector<std::string> getValueAsListOfStrings(StringRef FieldName) const;
1524 /// getValueAsDef - This method looks up the specified field and returns its
1525 /// value as a Record, throwing an exception if the field does not exist or if
1526 /// the value is not the right type.
1528 Record *getValueAsDef(StringRef FieldName) const;
1530 /// getValueAsBit - This method looks up the specified field and returns its
1531 /// value as a bit, throwing an exception if the field does not exist or if
1532 /// the value is not the right type.
1534 bool getValueAsBit(StringRef FieldName) const;
1536 /// getValueAsInt - This method looks up the specified field and returns its
1537 /// value as an int64_t, throwing an exception if the field does not exist or
1538 /// if the value is not the right type.
1540 int64_t getValueAsInt(StringRef FieldName) const;
1542 /// getValueAsDag - This method looks up the specified field and returns its
1543 /// value as an Dag, throwing an exception if the field does not exist or if
1544 /// the value is not the right type.
1546 DagInit *getValueAsDag(StringRef FieldName) const;
1548 /// getValueAsCode - This method looks up the specified field and returns
1549 /// its value as the string data in a CodeInit, throwing an exception if the
1550 /// field does not exist or if the value is not a code object.
1552 std::string getValueAsCode(StringRef FieldName) const;
1555 raw_ostream &operator<<(raw_ostream &OS, const Record &R);
1558 Record Rec; // Placeholder for template args and Name.
1559 typedef std::vector<Record*> RecordVector;
1560 RecordVector DefPrototypes;
1564 MultiClass(const std::string &Name, SMLoc Loc, RecordKeeper &Records) :
1565 Rec(Name, Loc, Records) {}
1568 class RecordKeeper {
1569 std::map<std::string, Record*> Classes, Defs;
1572 for (std::map<std::string, Record*>::iterator I = Classes.begin(),
1573 E = Classes.end(); I != E; ++I)
1575 for (std::map<std::string, Record*>::iterator I = Defs.begin(),
1576 E = Defs.end(); I != E; ++I)
1580 const std::map<std::string, Record*> &getClasses() const { return Classes; }
1581 const std::map<std::string, Record*> &getDefs() const { return Defs; }
1583 Record *getClass(const std::string &Name) const {
1584 std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
1585 return I == Classes.end() ? 0 : I->second;
1587 Record *getDef(const std::string &Name) const {
1588 std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
1589 return I == Defs.end() ? 0 : I->second;
1591 void addClass(Record *R) {
1592 assert(getClass(R->getName()) == 0 && "Class already exists!");
1593 Classes.insert(std::make_pair(R->getName(), R));
1595 void addDef(Record *R) {
1596 assert(getDef(R->getName()) == 0 && "Def already exists!");
1597 Defs.insert(std::make_pair(R->getName(), R));
1600 /// removeClass - Remove, but do not delete, the specified record.
1602 void removeClass(const std::string &Name) {
1603 assert(Classes.count(Name) && "Class does not exist!");
1604 Classes.erase(Name);
1606 /// removeDef - Remove, but do not delete, the specified record.
1608 void removeDef(const std::string &Name) {
1609 assert(Defs.count(Name) && "Def does not exist!");
1613 //===--------------------------------------------------------------------===//
1614 // High-level helper methods, useful for tablegen backends...
1616 /// getAllDerivedDefinitions - This method returns all concrete definitions
1617 /// that derive from the specified class name. If a class with the specified
1618 /// name does not exist, an exception is thrown.
1619 std::vector<Record*>
1620 getAllDerivedDefinitions(const std::string &ClassName) const;
1625 /// LessRecord - Sorting predicate to sort record pointers by name.
1628 bool operator()(const Record *Rec1, const Record *Rec2) const {
1629 return StringRef(Rec1->getName()).compare_numeric(Rec2->getName()) < 0;
1633 /// LessRecordFieldName - Sorting predicate to sort record pointers by their
1636 struct LessRecordFieldName {
1637 bool operator()(const Record *Rec1, const Record *Rec2) const {
1638 return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name");
1642 raw_ostream &operator<<(raw_ostream &OS, const RecordKeeper &RK);
1644 } // End llvm namespace